1 // Copyright 2013 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/extensions/browser_action_test_util.h"
6 #include "chrome/browser/extensions/extension_action.h"
7 #include "chrome/browser/extensions/extension_action_manager.h"
8 #include "chrome/browser/extensions/extension_apitest.h"
9 #include "chrome/browser/extensions/extension_service.h"
10 #include "chrome/browser/extensions/extension_system.h"
11 #include "chrome/browser/extensions/extension_tab_util.h"
12 #include "chrome/browser/extensions/extension_test_message_listener.h"
13 #include "chrome/browser/ui/browser_finder.h"
14 #include "chrome/browser/ui/browser_list.h"
15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
16 #include "chrome/common/extensions/permissions/permissions_data.h"
17 #include "chrome/test/base/ui_test_utils.h"
18 #include "content/public/browser/notification_service.h"
19 #include "content/public/browser/web_contents.h"
21 namespace extensions {
24 // chrome.browserAction API tests that interact with the UI in such a way that
25 // they cannot be run concurrently (i.e. openPopup API tests that require the
26 // window be focused/active).
27 class BrowserActionInteractiveTest : public ExtensionApiTest {
29 BrowserActionInteractiveTest() {}
30 virtual ~BrowserActionInteractiveTest() {}
33 // Function to control whether to run popup tests for the current platform.
34 // These tests require RunExtensionSubtest to work as expected and the browser
35 // window to able to be made active automatically. Returns false for platforms
36 // where these conditions are not met.
37 bool ShouldRunPopupTest() {
38 // TODO(justinlin): http://crbug.com/177163
39 #if defined(OS_WIN) && !defined(NDEBUG)
41 #elif defined(OS_MACOSX)
42 // TODO(justinlin): Browser window do not become active on Mac even when
43 // Activate() is called on them. Enable when/if it's possible to fix.
51 // Tests opening a popup using the chrome.browserAction.openPopup API. This test
52 // opens a popup in the starting window, closes the popup, creates a new window
53 // and opens a popup in the new window. Both popups should succeed in opening.
54 IN_PROC_BROWSER_TEST_F(BrowserActionInteractiveTest, TestOpenPopup) {
55 if (!ShouldRunPopupTest())
58 BrowserActionTestUtil browserActionBar = BrowserActionTestUtil(browser());
59 // Setup extension message listener to wait for javascript to finish running.
60 ExtensionTestMessageListener listener("ready", true);
62 // Setup the notification observer to wait for the popup to finish loading.
63 content::WindowedNotificationObserver frame_observer(
64 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
65 content::NotificationService::AllSources());
66 // Show first popup in first window and expect it to have loaded.
67 ASSERT_TRUE(RunExtensionSubtest("browser_action/open_popup",
68 "open_popup_succeeds.html")) << message_;
69 frame_observer.Wait();
70 EXPECT_TRUE(browserActionBar.HasPopup());
71 browserActionBar.HidePopup();
74 EXPECT_TRUE(listener.WaitUntilSatisfied());
75 Browser* new_browser = NULL;
77 content::WindowedNotificationObserver frame_observer(
78 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
79 content::NotificationService::AllSources());
81 new_browser = chrome::FindBrowserWithWebContents(
82 browser()->OpenURL(content::OpenURLParams(
83 GURL("about:"), content::Referrer(), NEW_WINDOW,
84 content::PAGE_TRANSITION_TYPED, false)));
86 // Hide all the buttons to test that it opens even when browser action is
87 // in the overflow bucket.
88 // TODO(justinlin): Implement for other platforms.
89 browserActionBar.SetIconVisibilityCount(0);
91 frame_observer.Wait();
94 EXPECT_TRUE(new_browser != NULL);
96 // Flaky on non-aura linux http://crbug.com/309749
97 #if !(defined(OS_LINUX) && !defined(USE_AURA))
98 ResultCatcher catcher;
100 content::WindowedNotificationObserver frame_observer(
101 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
102 content::NotificationService::AllSources());
103 // Show second popup in new window.
105 frame_observer.Wait();
106 EXPECT_TRUE(BrowserActionTestUtil(new_browser).HasPopup());
108 ASSERT_TRUE(catcher.GetNextResult()) << message_;
112 // Tests opening a popup in an incognito window.
113 IN_PROC_BROWSER_TEST_F(BrowserActionInteractiveTest, TestOpenPopupIncognito) {
114 if (!ShouldRunPopupTest())
117 content::WindowedNotificationObserver frame_observer(
118 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
119 content::NotificationService::AllSources());
120 ASSERT_TRUE(RunExtensionSubtest("browser_action/open_popup",
121 "open_popup_succeeds.html",
122 kFlagEnableIncognito | kFlagUseIncognito))
124 frame_observer.Wait();
125 // Non-Aura Linux uses a singleton for the popup, so it looks like all windows
126 // have popups if there is any popup open.
127 #if !(defined(OS_LINUX) && !defined(USE_AURA))
128 // Starting window does not have a popup.
129 EXPECT_FALSE(BrowserActionTestUtil(browser()).HasPopup());
131 // Incognito window should have a popup.
132 EXPECT_TRUE(BrowserActionTestUtil(BrowserList::GetInstance(
133 chrome::GetActiveDesktop())->GetLastActive()).HasPopup());
136 #if defined(OS_LINUX)
137 #define MAYBE_TestOpenPopupDoesNotCloseOtherPopups DISABLED_TestOpenPopupDoesNotCloseOtherPopups
139 #define MAYBE_TestOpenPopupDoesNotCloseOtherPopups TestOpenPopupDoesNotCloseOtherPopups
141 // Tests if there is already a popup open (by a user click or otherwise), that
142 // the openPopup API does not override it.
143 IN_PROC_BROWSER_TEST_F(BrowserActionInteractiveTest,
144 MAYBE_TestOpenPopupDoesNotCloseOtherPopups) {
145 if (!ShouldRunPopupTest())
148 // Load a first extension that can open a popup.
149 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(
150 "browser_action/popup")));
151 const Extension* extension = GetSingleLoadedExtension();
152 ASSERT_TRUE(extension) << message_;
154 ExtensionTestMessageListener listener("ready", true);
155 // Load the test extension which will do nothing except notifyPass() to
156 // return control here.
157 ASSERT_TRUE(RunExtensionSubtest("browser_action/open_popup",
158 "open_popup_fails.html")) << message_;
159 EXPECT_TRUE(listener.WaitUntilSatisfied());
161 content::WindowedNotificationObserver frame_observer(
162 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
163 content::NotificationService::AllSources());
164 // Open popup in the first extension.
165 BrowserActionTestUtil(browser()).Press(0);
166 frame_observer.Wait();
167 EXPECT_TRUE(BrowserActionTestUtil(browser()).HasPopup());
169 ResultCatcher catcher;
170 // Return control to javascript to validate that opening a popup fails now.
172 ASSERT_TRUE(catcher.GetNextResult()) << message_;
175 // Test that openPopup does not grant tab permissions like for browser action
176 // clicks if the activeTab permission is set.
177 IN_PROC_BROWSER_TEST_F(BrowserActionInteractiveTest,
178 TestOpenPopupDoesNotGrantTabPermissions) {
179 if (!ShouldRunPopupTest())
182 content::WindowedNotificationObserver frame_observer(
183 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
184 content::NotificationService::AllSources());
185 ASSERT_TRUE(RunExtensionSubtest("browser_action/open_popup",
186 "open_popup_succeeds.html")) << message_;
187 frame_observer.Wait();
189 ExtensionService* service = extensions::ExtensionSystem::Get(
190 browser()->profile())->extension_service();
191 ASSERT_FALSE(PermissionsData::HasAPIPermissionForTab(
192 service->GetExtensionById(last_loaded_extension_id(), false),
193 SessionID::IdForTab(browser()->tab_strip_model()->GetActiveWebContents()),
194 APIPermission::kTab));
198 } // namespace extensions