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.
5 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
7 #include "base/command_line.h"
8 #include "chrome/browser/ui/browser.h"
9 #include "chrome/browser/ui/tabs/tab_strip_model.h"
10 #include "chrome/browser/ui/website_settings/mock_permission_bubble_view.h"
11 #include "chrome/common/chrome_switches.h"
12 #include "chrome/test/base/in_process_browser_test.h"
13 #include "chrome/test/base/ui_test_utils.h"
14 #include "content/public/test/test_utils.h"
15 #include "net/test/embedded_test_server/embedded_test_server.h"
19 class PermissionBubbleManagerBrowserTest
: public InProcessBrowserTest
{
21 PermissionBubbleManagerBrowserTest() = default;
22 ~PermissionBubbleManagerBrowserTest() override
= default;
24 void SetUpOnMainThread() override
{
25 PermissionBubbleManager
* manager
= GetPermissionBubbleManager();
26 MockPermissionBubbleView::SetFactory(manager
, true);
27 manager
->DisplayPendingRequests();
28 InProcessBrowserTest::SetUpOnMainThread();
31 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
32 command_line
->AppendSwitch(switches::kEnablePermissionsBubbles
);
33 InProcessBrowserTest::SetUpCommandLine(command_line
);
36 PermissionBubbleManager
* GetPermissionBubbleManager() {
37 return PermissionBubbleManager::FromWebContents(
38 browser()->tab_strip_model()->GetActiveWebContents());
41 void WaitForPermissionBubble() {
42 if (bubble_view()->IsVisible())
44 content::RunMessageLoop();
47 MockPermissionBubbleView
* bubble_view() {
48 return MockPermissionBubbleView::GetFrom(GetPermissionBubbleManager());
52 // Requests before the load event should be bundled into one bubble.
53 // http://crbug.com/512849 flaky
55 #define MAYBE_RequestsBeforeLoad DISABLED_RequestsBeforeLoad
57 #define MAYBE_RequestsBeforeLoad RequestsBeforeLoad
59 IN_PROC_BROWSER_TEST_F(PermissionBubbleManagerBrowserTest
,
60 MAYBE_RequestsBeforeLoad
) {
61 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
63 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
65 embedded_test_server()->GetURL("/permissions/requests-before-load.html"),
67 WaitForPermissionBubble();
69 EXPECT_EQ(1, bubble_view()->show_count());
70 EXPECT_EQ(2, bubble_view()->requests_count());
73 // Requests before the load should not be bundled with a request after the load.
74 IN_PROC_BROWSER_TEST_F(PermissionBubbleManagerBrowserTest
,
75 RequestsBeforeAfterLoad
) {
76 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
78 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
80 embedded_test_server()->GetURL(
81 "/permissions/requests-before-after-load.html"),
83 WaitForPermissionBubble();
85 EXPECT_EQ(1, bubble_view()->show_count());
86 EXPECT_EQ(1, bubble_view()->requests_count());
89 // Navigating twice to the same URL should be equivalent to refresh. This means
90 // showing the bubbles twice.
91 // http://crbug.com/512849 flaky
93 #define MAYBE_NavTwice DISABLED_NavTwice
95 #define MAYBE_NavTwice NavTwice
97 IN_PROC_BROWSER_TEST_F(PermissionBubbleManagerBrowserTest
, MAYBE_NavTwice
) {
98 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
100 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
102 embedded_test_server()->GetURL("/permissions/requests-before-load.html"),
104 WaitForPermissionBubble();
106 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
108 embedded_test_server()->GetURL("/permissions/requests-before-load.html"),
110 WaitForPermissionBubble();
112 EXPECT_EQ(2, bubble_view()->show_count());
113 EXPECT_EQ(4, bubble_view()->requests_count());
116 // Navigating twice to the same URL with a hash should be navigation within the
117 // page. This means the bubble is only shown once.
118 // http://crbug.com/512849 flaky
120 #define MAYBE_NavTwiceWithHash DISABLED_NavTwiceWithHash
122 #define MAYBE_NavTwiceWithHash NavTwiceWithHash
124 IN_PROC_BROWSER_TEST_F(PermissionBubbleManagerBrowserTest
,
125 MAYBE_NavTwiceWithHash
) {
126 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
128 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
130 embedded_test_server()->GetURL("/permissions/requests-before-load.html"),
132 WaitForPermissionBubble();
134 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
136 embedded_test_server()->GetURL(
137 "/permissions/requests-before-load.html#0"),
139 WaitForPermissionBubble();
141 EXPECT_EQ(1, bubble_view()->show_count());
142 EXPECT_EQ(2, bubble_view()->requests_count());
145 // Bubble requests should be shown after in-page navigation.
146 IN_PROC_BROWSER_TEST_F(PermissionBubbleManagerBrowserTest
, InPageNavigation
) {
147 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
149 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
151 embedded_test_server()->GetURL("/empty.html"),
154 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
156 embedded_test_server()->GetURL("/empty.html#0"),
159 // Request 'geolocation' permission.
160 ExecuteScriptAndGetValue(
161 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(),
162 "navigator.geolocation.getCurrentPosition(function(){});");
163 WaitForPermissionBubble();
165 EXPECT_EQ(1, bubble_view()->show_count());
166 EXPECT_EQ(1, bubble_view()->requests_count());
169 } // anonymous namespace