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/sessions/session_tab_helper.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/browser_commands.h"
13 #include "chrome/browser/ui/browser_finder.h"
14 #include "chrome/browser/ui/browser_list.h"
15 #include "chrome/browser/ui/browser_window.h"
16 #include "chrome/browser/ui/tabs/tab_strip_model.h"
17 #include "chrome/browser/ui/toolbar/toolbar_actions_model.h"
18 #include "chrome/test/base/interactive_test_utils.h"
19 #include "chrome/test/base/ui_test_utils.h"
20 #include "content/public/browser/notification_service.h"
21 #include "extensions/browser/extension_registry.h"
22 #include "extensions/browser/extension_system.h"
23 #include "extensions/common/extension.h"
24 #include "extensions/common/extension_set.h"
25 #include "extensions/common/permissions/permissions_data.h"
26 #include "extensions/test/extension_test_message_listener.h"
27 #include "extensions/test/result_catcher.h"
30 #include "ui/views/win/hwnd_util.h"
33 namespace extensions
{
36 // chrome.browserAction API tests that interact with the UI in such a way that
37 // they cannot be run concurrently (i.e. openPopup API tests that require the
38 // window be focused/active).
39 class BrowserActionInteractiveTest
: public ExtensionApiTest
{
41 BrowserActionInteractiveTest() {}
42 ~BrowserActionInteractiveTest() override
{}
45 // Function to control whether to run popup tests for the current platform.
46 // These tests require RunExtensionSubtest to work as expected and the browser
47 // window to able to be made active automatically. Returns false for platforms
48 // where these conditions are not met.
49 bool ShouldRunPopupTest() {
50 // TODO(justinlin): http://crbug.com/177163
51 #if defined(OS_WIN) && !defined(NDEBUG)
53 #elif defined(OS_MACOSX)
54 // TODO(justinlin): Browser window do not become active on Mac even when
55 // Activate() is called on them. Enable when/if it's possible to fix.
62 // Open an extension popup via the chrome.browserAction.openPopup API.
63 void OpenExtensionPopupViaAPI() {
64 // Setup the notification observer to wait for the popup to finish loading.
65 content::WindowedNotificationObserver
frame_observer(
66 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME
,
67 content::NotificationService::AllSources());
68 // Show first popup in first window and expect it to have loaded.
69 ASSERT_TRUE(RunExtensionSubtest("browser_action/open_popup",
70 "open_popup_succeeds.html")) << message_
;
71 frame_observer
.Wait();
72 EXPECT_TRUE(BrowserActionTestUtil(browser()).HasPopup());
76 // Tests opening a popup using the chrome.browserAction.openPopup API. This test
77 // opens a popup in the starting window, closes the popup, creates a new window
78 // and opens a popup in the new window. Both popups should succeed in opening.
79 IN_PROC_BROWSER_TEST_F(BrowserActionInteractiveTest
, TestOpenPopup
) {
80 if (!ShouldRunPopupTest())
83 BrowserActionTestUtil
browserActionBar(browser());
84 // Setup extension message listener to wait for javascript to finish running.
85 ExtensionTestMessageListener
listener("ready", true);
87 OpenExtensionPopupViaAPI();
88 EXPECT_TRUE(browserActionBar
.HasPopup());
89 browserActionBar
.HidePopup();
92 EXPECT_TRUE(listener
.WaitUntilSatisfied());
93 Browser
* new_browser
= NULL
;
95 content::WindowedNotificationObserver
frame_observer(
96 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME
,
97 content::NotificationService::AllSources());
99 new_browser
= chrome::FindBrowserWithWebContents(
100 browser()->OpenURL(content::OpenURLParams(
101 GURL("about:"), content::Referrer(), NEW_WINDOW
,
102 ui::PAGE_TRANSITION_TYPED
, false)));
103 // Hide all the buttons to test that it opens even when the browser action
104 // is in the overflow bucket.
105 ToolbarActionsModel::Get(profile())->SetVisibleIconCount(0);
106 frame_observer
.Wait();
109 EXPECT_TRUE(new_browser
!= NULL
);
111 // Flaky on non-aura linux http://crbug.com/309749
112 #if !(defined(OS_LINUX) && !defined(USE_AURA))
113 ResultCatcher catcher
;
115 content::WindowedNotificationObserver
frame_observer(
116 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME
,
117 content::NotificationService::AllSources());
118 // Show second popup in new window.
120 frame_observer
.Wait();
121 EXPECT_TRUE(BrowserActionTestUtil(new_browser
).HasPopup());
123 ASSERT_TRUE(catcher
.GetNextResult()) << message_
;
127 // Tests opening a popup in an incognito window.
128 IN_PROC_BROWSER_TEST_F(BrowserActionInteractiveTest
, TestOpenPopupIncognito
) {
129 if (!ShouldRunPopupTest())
132 content::WindowedNotificationObserver
frame_observer(
133 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME
,
134 content::NotificationService::AllSources());
135 ASSERT_TRUE(RunExtensionSubtest("browser_action/open_popup",
136 "open_popup_succeeds.html",
137 kFlagEnableIncognito
| kFlagUseIncognito
))
139 frame_observer
.Wait();
140 // Non-Aura Linux uses a singleton for the popup, so it looks like all windows
141 // have popups if there is any popup open.
142 #if !(defined(OS_LINUX) && !defined(USE_AURA))
143 // Starting window does not have a popup.
144 EXPECT_FALSE(BrowserActionTestUtil(browser()).HasPopup());
146 // Incognito window should have a popup.
147 EXPECT_TRUE(BrowserActionTestUtil(BrowserList::GetInstance(
148 chrome::GetActiveDesktop())->GetLastActive()).HasPopup());
151 // Tests that an extension can open a popup in the last active incognito window
152 // even from a background page with a non-incognito profile.
153 // (crbug.com/448853)
155 // Fails on XP: http://crbug.com/515717
156 #define MAYBE_TestOpenPopupIncognitoFromBackground \
157 DISABLED_TestOpenPopupIncognitoFromBackground
159 #define MAYBE_TestOpenPopupIncognitoFromBackground \
160 TestOpenPopupIncognitoFromBackground
162 IN_PROC_BROWSER_TEST_F(BrowserActionInteractiveTest
,
163 MAYBE_TestOpenPopupIncognitoFromBackground
) {
164 if (!ShouldRunPopupTest())
167 const Extension
* extension
=
168 LoadExtensionIncognito(test_data_dir_
.AppendASCII("browser_action").
169 AppendASCII("open_popup_background"));
170 ASSERT_TRUE(extension
);
171 ExtensionTestMessageListener
listener(false);
172 listener
.set_extension_id(extension
->id());
174 Browser
* incognito_browser
=
175 OpenURLOffTheRecord(profile(), GURL("chrome://newtab/"));
176 listener
.WaitUntilSatisfied();
177 EXPECT_EQ(std::string("opened"), listener
.message());
178 EXPECT_TRUE(BrowserActionTestUtil(incognito_browser
).HasPopup());
181 #if defined(OS_LINUX)
182 #define MAYBE_TestOpenPopupDoesNotCloseOtherPopups DISABLED_TestOpenPopupDoesNotCloseOtherPopups
184 #define MAYBE_TestOpenPopupDoesNotCloseOtherPopups TestOpenPopupDoesNotCloseOtherPopups
186 // Tests if there is already a popup open (by a user click or otherwise), that
187 // the openPopup API does not override it.
188 IN_PROC_BROWSER_TEST_F(BrowserActionInteractiveTest
,
189 MAYBE_TestOpenPopupDoesNotCloseOtherPopups
) {
190 if (!ShouldRunPopupTest())
193 // Load a first extension that can open a popup.
194 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII(
195 "browser_action/popup")));
196 const Extension
* extension
= GetSingleLoadedExtension();
197 ASSERT_TRUE(extension
) << message_
;
199 ExtensionTestMessageListener
listener("ready", true);
200 // Load the test extension which will do nothing except notifyPass() to
201 // return control here.
202 ASSERT_TRUE(RunExtensionSubtest("browser_action/open_popup",
203 "open_popup_fails.html")) << message_
;
204 EXPECT_TRUE(listener
.WaitUntilSatisfied());
206 content::WindowedNotificationObserver
frame_observer(
207 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME
,
208 content::NotificationService::AllSources());
209 // Open popup in the first extension.
210 BrowserActionTestUtil(browser()).Press(0);
211 frame_observer
.Wait();
212 EXPECT_TRUE(BrowserActionTestUtil(browser()).HasPopup());
214 ResultCatcher catcher
;
215 // Return control to javascript to validate that opening a popup fails now.
217 ASSERT_TRUE(catcher
.GetNextResult()) << message_
;
220 // Test that openPopup does not grant tab permissions like for browser action
221 // clicks if the activeTab permission is set.
222 IN_PROC_BROWSER_TEST_F(BrowserActionInteractiveTest
,
223 TestOpenPopupDoesNotGrantTabPermissions
) {
224 if (!ShouldRunPopupTest())
227 OpenExtensionPopupViaAPI();
228 ExtensionService
* service
= extensions::ExtensionSystem::Get(
229 browser()->profile())->extension_service();
231 service
->GetExtensionById(last_loaded_extension_id(), false)
233 ->HasAPIPermissionForTab(
234 SessionTabHelper::IdForTab(
235 browser()->tab_strip_model()->GetActiveWebContents()),
236 APIPermission::kTab
));
239 // Test that the extension popup is closed when the browser window is clicked.
240 IN_PROC_BROWSER_TEST_F(BrowserActionInteractiveTest
, BrowserClickClosesPopup1
) {
241 if (!ShouldRunPopupTest())
244 // Open an extension popup via the chrome.browserAction.openPopup API.
245 content::WindowedNotificationObserver
frame_observer(
246 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME
,
247 content::NotificationService::AllSources());
248 ASSERT_TRUE(RunExtensionSubtest("browser_action/open_popup",
249 "open_popup_succeeds.html")) << message_
;
250 frame_observer
.Wait();
251 EXPECT_TRUE(BrowserActionTestUtil(browser()).HasPopup());
253 // Click on the omnibox to close the extension popup.
254 ui_test_utils::ClickOnView(browser(), VIEW_ID_OMNIBOX
);
255 EXPECT_FALSE(BrowserActionTestUtil(browser()).HasPopup());
258 // Test that the extension popup is closed when the browser window is clicked.
259 IN_PROC_BROWSER_TEST_F(BrowserActionInteractiveTest
, BrowserClickClosesPopup2
) {
260 if (!ShouldRunPopupTest())
263 // Load a first extension that can open a popup.
264 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII(
265 "browser_action/popup")));
266 const Extension
* extension
= GetSingleLoadedExtension();
267 ASSERT_TRUE(extension
) << message_
;
269 // Open an extension popup by clicking the browser action button.
270 content::WindowedNotificationObserver
frame_observer(
271 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME
,
272 content::NotificationService::AllSources());
273 BrowserActionTestUtil(browser()).Press(0);
274 frame_observer
.Wait();
275 EXPECT_TRUE(BrowserActionTestUtil(browser()).HasPopup());
277 // Click on the omnibox to close the extension popup.
278 ui_test_utils::ClickOnView(browser(), VIEW_ID_OMNIBOX
);
279 EXPECT_FALSE(BrowserActionTestUtil(browser()).HasPopup());
282 // Test that the extension popup is closed on browser tab switches.
283 IN_PROC_BROWSER_TEST_F(BrowserActionInteractiveTest
, TabSwitchClosesPopup
) {
284 if (!ShouldRunPopupTest())
287 // Add a second tab to the browser and open an extension popup.
288 chrome::NewTab(browser());
289 ASSERT_EQ(2, browser()->tab_strip_model()->count());
290 OpenExtensionPopupViaAPI();
292 // Press CTRL+TAB to change active tabs, the extension popup should close.
293 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
294 browser(), ui::VKEY_TAB
, true, false, false, false));
295 EXPECT_FALSE(BrowserActionTestUtil(browser()).HasPopup());
298 IN_PROC_BROWSER_TEST_F(BrowserActionInteractiveTest
,
299 DeleteBrowserActionWithPopupOpen
) {
300 if (!ShouldRunPopupTest())
303 // First, we open a popup.
304 OpenExtensionPopupViaAPI();
305 BrowserActionTestUtil
browser_action_test_util(browser());
306 EXPECT_TRUE(browser_action_test_util
.HasPopup());
308 // Then, find the extension that created it.
309 content::WebContents
* active_web_contents
=
310 browser()->tab_strip_model()->GetActiveWebContents();
311 ASSERT_TRUE(active_web_contents
);
312 GURL url
= active_web_contents
->GetLastCommittedURL();
313 const Extension
* extension
= ExtensionRegistry::Get(browser()->profile())->
314 enabled_extensions().GetExtensionOrAppByURL(url
);
315 ASSERT_TRUE(extension
);
317 // Finally, uninstall the extension, which causes the view to be deleted and
318 // the popup to go away. This should not crash.
319 UninstallExtension(extension
->id());
320 EXPECT_FALSE(browser_action_test_util
.HasPopup());
323 #if defined(TOOLKIT_VIEWS)
324 // Test closing the browser while inspecting an extension popup with dev tools.
325 IN_PROC_BROWSER_TEST_F(BrowserActionInteractiveTest
, CloseBrowserWithDevTools
) {
326 if (!ShouldRunPopupTest())
329 // Load a first extension that can open a popup.
330 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII(
331 "browser_action/popup")));
332 const Extension
* extension
= GetSingleLoadedExtension();
333 ASSERT_TRUE(extension
) << message_
;
335 // Open an extension popup by clicking the browser action button.
336 content::WindowedNotificationObserver
frame_observer(
337 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME
,
338 content::NotificationService::AllSources());
339 BrowserActionTestUtil(browser()).InspectPopup(0);
340 frame_observer
.Wait();
341 EXPECT_TRUE(BrowserActionTestUtil(browser()).HasPopup());
343 // Close the browser window, this should not cause a crash.
344 chrome::CloseWindow(browser());
346 #endif // TOOLKIT_VIEWS
349 // Test that forcibly closing the browser and popup HWND does not cause a crash.
350 // http://crbug.com/400646
351 IN_PROC_BROWSER_TEST_F(BrowserActionInteractiveTest
,
352 DISABLED_DestroyHWNDDoesNotCrash
) {
353 if (!ShouldRunPopupTest())
356 OpenExtensionPopupViaAPI();
357 BrowserActionTestUtil
test_util(browser());
358 const gfx::NativeView view
= test_util
.GetPopupNativeView();
359 EXPECT_NE(static_cast<gfx::NativeView
>(NULL
), view
);
360 const HWND hwnd
= views::HWNDForNativeView(view
);
362 views::HWNDForNativeView(browser()->window()->GetNativeWindow()));
363 EXPECT_EQ(TRUE
, ::IsWindow(hwnd
));
365 // Create a new browser window to prevent the message loop from terminating.
366 browser()->OpenURL(content::OpenURLParams(GURL("about:"), content::Referrer(),
368 ui::PAGE_TRANSITION_TYPED
, false));
370 // Forcibly closing the browser HWND should not cause a crash.
371 EXPECT_EQ(TRUE
, ::CloseWindow(hwnd
));
372 EXPECT_EQ(TRUE
, ::DestroyWindow(hwnd
));
373 EXPECT_EQ(FALSE
, ::IsWindow(hwnd
));
378 } // namespace extensions