Add ability to gather metrics to BubbleManager.
[chromium-blink-merge.git] / chrome / browser / ui / website_settings / permission_bubble_manager_browsertest.cc
blobd5b434010decd436ec771ef1d847da626eb3976e
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"
17 namespace {
19 class PermissionBubbleManagerBrowserTest : public InProcessBrowserTest {
20 public:
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())
43 return;
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
54 #if defined(OS_WIN)
55 #define MAYBE_RequestsBeforeLoad DISABLED_RequestsBeforeLoad
56 #else
57 #define MAYBE_RequestsBeforeLoad RequestsBeforeLoad
58 #endif
59 IN_PROC_BROWSER_TEST_F(PermissionBubbleManagerBrowserTest,
60 MAYBE_RequestsBeforeLoad) {
61 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
63 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
64 browser(),
65 embedded_test_server()->GetURL("/permissions/requests-before-load.html"),
66 1);
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(
79 browser(),
80 embedded_test_server()->GetURL(
81 "/permissions/requests-before-after-load.html"),
82 1);
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
92 #if defined(OS_WIN)
93 #define MAYBE_NavTwice DISABLED_NavTwice
94 #else
95 #define MAYBE_NavTwice NavTwice
96 #endif
97 IN_PROC_BROWSER_TEST_F(PermissionBubbleManagerBrowserTest, MAYBE_NavTwice) {
98 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
100 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
101 browser(),
102 embedded_test_server()->GetURL("/permissions/requests-before-load.html"),
104 WaitForPermissionBubble();
106 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
107 browser(),
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
119 #if defined(OS_WIN)
120 #define MAYBE_NavTwiceWithHash DISABLED_NavTwiceWithHash
121 #else
122 #define MAYBE_NavTwiceWithHash NavTwiceWithHash
123 #endif
124 IN_PROC_BROWSER_TEST_F(PermissionBubbleManagerBrowserTest,
125 MAYBE_NavTwiceWithHash) {
126 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
128 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
129 browser(),
130 embedded_test_server()->GetURL("/permissions/requests-before-load.html"),
132 WaitForPermissionBubble();
134 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
135 browser(),
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(
150 browser(),
151 embedded_test_server()->GetURL("/empty.html"),
154 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
155 browser(),
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