| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" | |
| 6 | |
| 7 #include "base/command_line.h" | |
| 8 #include "base/metrics/field_trial.h" | |
| 9 #include "build/build_config.h" | |
| 10 #include "chrome/browser/permissions/permission_context_base.h" | |
| 11 #include "chrome/browser/permissions/permission_util.h" | |
| 12 #include "chrome/browser/ui/browser.h" | |
| 13 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
| 14 #include "chrome/browser/ui/website_settings/mock_permission_bubble_factory.h" | |
| 15 #include "chrome/test/base/in_process_browser_test.h" | |
| 16 #include "chrome/test/base/ui_test_utils.h" | |
| 17 #include "components/variations/variations_associated_data.h" | |
| 18 #include "content/public/browser/permission_type.h" | |
| 19 #include "content/public/test/browser_test_utils.h" | |
| 20 #include "content/public/test/test_utils.h" | |
| 21 #include "net/test/embedded_test_server/embedded_test_server.h" | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 const char* kPermissionsKillSwitchFieldStudy = | |
| 26 PermissionContextBase::kPermissionsKillSwitchFieldStudy; | |
| 27 const char* kPermissionsKillSwitchBlockedValue = | |
| 28 PermissionContextBase::kPermissionsKillSwitchBlockedValue; | |
| 29 const char kPermissionsKillSwitchTestGroup[] = "TestGroup"; | |
| 30 | |
| 31 class PermissionBubbleManagerBrowserTest : public InProcessBrowserTest { | |
| 32 public: | |
| 33 PermissionBubbleManagerBrowserTest() = default; | |
| 34 ~PermissionBubbleManagerBrowserTest() override = default; | |
| 35 | |
| 36 void SetUpOnMainThread() override { | |
| 37 InProcessBrowserTest::SetUpOnMainThread(); | |
| 38 PermissionBubbleManager* manager = GetPermissionBubbleManager(); | |
| 39 mock_permission_bubble_factory_.reset( | |
| 40 new MockPermissionBubbleFactory(true, manager)); | |
| 41 manager->DisplayPendingRequests(); | |
| 42 } | |
| 43 | |
| 44 void TearDownOnMainThread() override { | |
| 45 mock_permission_bubble_factory_.reset(); | |
| 46 InProcessBrowserTest::TearDownOnMainThread(); | |
| 47 } | |
| 48 | |
| 49 PermissionBubbleManager* GetPermissionBubbleManager() { | |
| 50 return PermissionBubbleManager::FromWebContents( | |
| 51 browser()->tab_strip_model()->GetActiveWebContents()); | |
| 52 } | |
| 53 | |
| 54 void WaitForPermissionBubble() { | |
| 55 if (bubble_factory()->is_visible()) | |
| 56 return; | |
| 57 content::RunMessageLoop(); | |
| 58 } | |
| 59 | |
| 60 MockPermissionBubbleFactory* bubble_factory() { | |
| 61 return mock_permission_bubble_factory_.get(); | |
| 62 } | |
| 63 | |
| 64 void EnableKillSwitch(content::PermissionType permission_type) { | |
| 65 std::map<std::string, std::string> params; | |
| 66 params[PermissionUtil::GetPermissionString(permission_type)] = | |
| 67 kPermissionsKillSwitchBlockedValue; | |
| 68 variations::AssociateVariationParams( | |
| 69 kPermissionsKillSwitchFieldStudy, kPermissionsKillSwitchTestGroup, | |
| 70 params); | |
| 71 base::FieldTrialList::CreateFieldTrial(kPermissionsKillSwitchFieldStudy, | |
| 72 kPermissionsKillSwitchTestGroup); | |
| 73 } | |
| 74 | |
| 75 private: | |
| 76 std::unique_ptr<MockPermissionBubbleFactory> mock_permission_bubble_factory_; | |
| 77 }; | |
| 78 | |
| 79 // Requests before the load event should be bundled into one bubble. | |
| 80 // http://crbug.com/512849 flaky | |
| 81 IN_PROC_BROWSER_TEST_F(PermissionBubbleManagerBrowserTest, | |
| 82 DISABLED_RequestsBeforeLoad) { | |
| 83 ASSERT_TRUE(embedded_test_server()->Start()); | |
| 84 | |
| 85 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( | |
| 86 browser(), | |
| 87 embedded_test_server()->GetURL("/permissions/requests-before-load.html"), | |
| 88 1); | |
| 89 WaitForPermissionBubble(); | |
| 90 | |
| 91 EXPECT_EQ(1, bubble_factory()->show_count()); | |
| 92 EXPECT_EQ(2, bubble_factory()->total_request_count()); | |
| 93 } | |
| 94 | |
| 95 // Requests before the load should not be bundled with a request after the load. | |
| 96 IN_PROC_BROWSER_TEST_F(PermissionBubbleManagerBrowserTest, | |
| 97 RequestsBeforeAfterLoad) { | |
| 98 ASSERT_TRUE(embedded_test_server()->Start()); | |
| 99 | |
| 100 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( | |
| 101 browser(), | |
| 102 embedded_test_server()->GetURL( | |
| 103 "/permissions/requests-before-after-load.html"), | |
| 104 1); | |
| 105 WaitForPermissionBubble(); | |
| 106 | |
| 107 EXPECT_EQ(1, bubble_factory()->show_count()); | |
| 108 EXPECT_EQ(1, bubble_factory()->total_request_count()); | |
| 109 } | |
| 110 | |
| 111 // Navigating twice to the same URL should be equivalent to refresh. This means | |
| 112 // showing the bubbles twice. | |
| 113 // http://crbug.com/512849 flaky | |
| 114 #if defined(OS_WIN) | |
| 115 #define MAYBE_NavTwice DISABLED_NavTwice | |
| 116 #else | |
| 117 #define MAYBE_NavTwice NavTwice | |
| 118 #endif | |
| 119 IN_PROC_BROWSER_TEST_F(PermissionBubbleManagerBrowserTest, MAYBE_NavTwice) { | |
| 120 ASSERT_TRUE(embedded_test_server()->Start()); | |
| 121 | |
| 122 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( | |
| 123 browser(), | |
| 124 embedded_test_server()->GetURL("/permissions/requests-before-load.html"), | |
| 125 1); | |
| 126 WaitForPermissionBubble(); | |
| 127 | |
| 128 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( | |
| 129 browser(), | |
| 130 embedded_test_server()->GetURL("/permissions/requests-before-load.html"), | |
| 131 1); | |
| 132 WaitForPermissionBubble(); | |
| 133 | |
| 134 EXPECT_EQ(2, bubble_factory()->show_count()); | |
| 135 EXPECT_EQ(4, bubble_factory()->total_request_count()); | |
| 136 } | |
| 137 | |
| 138 // Navigating twice to the same URL with a hash should be navigation within the | |
| 139 // page. This means the bubble is only shown once. | |
| 140 // http://crbug.com/512849 flaky | |
| 141 #if defined(OS_WIN) | |
| 142 #define MAYBE_NavTwiceWithHash DISABLED_NavTwiceWithHash | |
| 143 #else | |
| 144 #define MAYBE_NavTwiceWithHash NavTwiceWithHash | |
| 145 #endif | |
| 146 IN_PROC_BROWSER_TEST_F(PermissionBubbleManagerBrowserTest, | |
| 147 MAYBE_NavTwiceWithHash) { | |
| 148 ASSERT_TRUE(embedded_test_server()->Start()); | |
| 149 | |
| 150 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( | |
| 151 browser(), | |
| 152 embedded_test_server()->GetURL("/permissions/requests-before-load.html"), | |
| 153 1); | |
| 154 WaitForPermissionBubble(); | |
| 155 | |
| 156 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( | |
| 157 browser(), | |
| 158 embedded_test_server()->GetURL( | |
| 159 "/permissions/requests-before-load.html#0"), | |
| 160 1); | |
| 161 WaitForPermissionBubble(); | |
| 162 | |
| 163 EXPECT_EQ(1, bubble_factory()->show_count()); | |
| 164 EXPECT_EQ(2, bubble_factory()->total_request_count()); | |
| 165 } | |
| 166 | |
| 167 // Bubble requests should be shown after in-page navigation. | |
| 168 IN_PROC_BROWSER_TEST_F(PermissionBubbleManagerBrowserTest, InPageNavigation) { | |
| 169 ASSERT_TRUE(embedded_test_server()->Start()); | |
| 170 | |
| 171 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( | |
| 172 browser(), | |
| 173 embedded_test_server()->GetURL("/empty.html"), | |
| 174 1); | |
| 175 | |
| 176 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( | |
| 177 browser(), | |
| 178 embedded_test_server()->GetURL("/empty.html#0"), | |
| 179 1); | |
| 180 | |
| 181 // Request 'geolocation' permission. | |
| 182 ExecuteScriptAndGetValue( | |
| 183 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(), | |
| 184 "navigator.geolocation.getCurrentPosition(function(){});"); | |
| 185 WaitForPermissionBubble(); | |
| 186 | |
| 187 EXPECT_EQ(1, bubble_factory()->show_count()); | |
| 188 EXPECT_EQ(1, bubble_factory()->total_request_count()); | |
| 189 } | |
| 190 | |
| 191 // Bubble requests should not be shown when the killswitch is on. | |
| 192 IN_PROC_BROWSER_TEST_F(PermissionBubbleManagerBrowserTest, | |
| 193 KillSwitchGeolocation) { | |
| 194 ASSERT_TRUE(embedded_test_server()->Start()); | |
| 195 | |
| 196 ui_test_utils::NavigateToURL( | |
| 197 browser(), | |
| 198 embedded_test_server()->GetURL("/permissions/killswitch_tester.html")); | |
| 199 | |
| 200 // Now enable the geolocation killswitch. | |
| 201 EnableKillSwitch(content::PermissionType::GEOLOCATION); | |
| 202 content::WebContents* web_contents = | |
| 203 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 204 | |
| 205 std::string result; | |
| 206 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | |
| 207 web_contents, "requestGeolocation();", &result)); | |
| 208 EXPECT_EQ("denied", result); | |
| 209 EXPECT_EQ(0, bubble_factory()->show_count()); | |
| 210 EXPECT_EQ(0, bubble_factory()->total_request_count()); | |
| 211 | |
| 212 // Disable the trial. | |
| 213 variations::testing::ClearAllVariationParams(); | |
| 214 | |
| 215 // Reload the page to get around blink layer caching for geolocation | |
| 216 // requests. | |
| 217 ui_test_utils::NavigateToURL( | |
| 218 browser(), | |
| 219 embedded_test_server()->GetURL("/permissions/killswitch_tester.html")); | |
| 220 | |
| 221 EXPECT_TRUE(content::ExecuteScript(web_contents, "requestGeolocation();")); | |
| 222 WaitForPermissionBubble(); | |
| 223 EXPECT_EQ(1, bubble_factory()->show_count()); | |
| 224 EXPECT_EQ(1, bubble_factory()->total_request_count()); | |
| 225 } | |
| 226 | |
| 227 // Bubble requests should not be shown when the killswitch is on. | |
| 228 IN_PROC_BROWSER_TEST_F(PermissionBubbleManagerBrowserTest, | |
| 229 KillSwitchNotifications) { | |
| 230 ASSERT_TRUE(embedded_test_server()->Start()); | |
| 231 | |
| 232 ui_test_utils::NavigateToURL( | |
| 233 browser(), | |
| 234 embedded_test_server()->GetURL("/permissions/killswitch_tester.html")); | |
| 235 | |
| 236 // Now enable the notifications killswitch. | |
| 237 EnableKillSwitch(content::PermissionType::NOTIFICATIONS); | |
| 238 content::WebContents* web_contents = | |
| 239 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 240 | |
| 241 std::string result; | |
| 242 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | |
| 243 web_contents, "requestNotification();", &result)); | |
| 244 EXPECT_EQ("denied", result); | |
| 245 EXPECT_EQ(0, bubble_factory()->show_count()); | |
| 246 EXPECT_EQ(0, bubble_factory()->total_request_count()); | |
| 247 | |
| 248 // Disable the trial. | |
| 249 variations::testing::ClearAllVariationParams(); | |
| 250 | |
| 251 EXPECT_TRUE(content::ExecuteScript(web_contents, "requestNotification();")); | |
| 252 WaitForPermissionBubble(); | |
| 253 EXPECT_EQ(1, bubble_factory()->show_count()); | |
| 254 EXPECT_EQ(1, bubble_factory()->total_request_count()); | |
| 255 } | |
| 256 | |
| 257 } // anonymous namespace | |
| OLD | NEW |