1 // Copyright (c) 2012 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 "base/command_line.h"
6 #include "base/memory/scoped_vector.h"
7 #include "base/path_service.h"
8 #include "base/strings/stringprintf.h"
9 #include "chrome/browser/extensions/extension_apitest.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/browser_finder.h"
13 #include "chrome/browser/ui/browser_iterator.h"
14 #include "chrome/browser/ui/panels/panel_manager.h"
15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
16 #include "chrome/common/chrome_paths.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "chrome/test/base/test_switches.h"
19 #include "chrome/test/base/ui_test_utils.h"
20 #include "content/public/browser/render_process_host.h"
21 #include "content/public/browser/web_contents.h"
22 #include "content/public/common/result_codes.h"
23 #include "content/public/common/url_constants.h"
24 #include "content/public/test/browser_test_utils.h"
25 #include "extensions/browser/extension_host.h"
26 #include "extensions/browser/process_manager.h"
27 #include "extensions/common/constants.h"
28 #include "extensions/common/extension.h"
29 #include "extensions/common/switches.h"
30 #include "extensions/test/extension_test_message_listener.h"
31 #include "extensions/test/result_catcher.h"
32 #include "net/dns/mock_host_resolver.h"
33 #include "net/test/embedded_test_server/embedded_test_server.h"
34 #include "testing/gtest/include/gtest/gtest.h"
37 #include "extensions/browser/app_window/app_window_registry.h"
40 #if defined(USE_ASH) && defined(OS_CHROMEOS)
41 // TODO(stevenjb): Figure out the correct behavior for Ash + Win
42 #define USE_ASH_PANELS
45 using content::OpenURLParams
;
46 using content::Referrer
;
47 using content::WebContents
;
49 // The test uses the chrome.browserAction.openPopup API, which requires that the
50 // window can automatically be activated.
51 // See comments at BrowserActionInteractiveTest::ShouldRunPopupTest
52 // Fails flakily on all platforms. https://crbug.com/477691
53 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, DISABLED_WindowOpen
) {
54 extensions::ResultCatcher catcher
;
55 ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_
56 .AppendASCII("window_open").AppendASCII("spanning")));
57 EXPECT_TRUE(catcher
.GetNextResult()) << catcher
.message();
60 int GetPanelCount(Browser
* browser
) {
61 #if defined(USE_ASH_PANELS)
62 return static_cast<int>(extensions::AppWindowRegistry::Get(
63 browser
->profile())->app_windows().size());
65 return PanelManager::GetInstance()->num_panels();
69 bool WaitForTabsAndPopups(Browser
* browser
,
74 base::StringPrintf("WaitForTabsAndPopups tabs:%d, popups:%d, panels:%d",
75 num_tabs
, num_popups
, num_panels
));
76 // We start with one tab and one browser already open.
78 size_t num_browsers
= static_cast<size_t>(num_popups
) + 1;
80 const base::TimeDelta kWaitTime
= base::TimeDelta::FromSeconds(10);
81 base::TimeTicks end_time
= base::TimeTicks::Now() + kWaitTime
;
82 while (base::TimeTicks::Now() < end_time
) {
83 if (chrome::GetBrowserCount(browser
->profile(),
84 browser
->host_desktop_type()) == num_browsers
&&
85 browser
->tab_strip_model()->count() == num_tabs
&&
86 GetPanelCount(browser
) == num_panels
)
89 content::RunAllPendingInMessageLoop();
92 EXPECT_EQ(num_browsers
,
93 chrome::GetBrowserCount(browser
->profile(),
94 browser
->host_desktop_type()));
95 EXPECT_EQ(num_tabs
, browser
->tab_strip_model()->count());
96 EXPECT_EQ(num_panels
, GetPanelCount(browser
));
98 int num_popups_seen
= 0;
99 for (chrome::BrowserIterator iter
; !iter
.done(); iter
.Next()) {
100 if (*iter
== browser
)
103 EXPECT_TRUE((*iter
)->is_type_popup());
106 EXPECT_EQ(num_popups
, num_popups_seen
);
108 return ((num_browsers
==
109 chrome::GetBrowserCount(browser
->profile(),
110 browser
->host_desktop_type())) &&
111 (num_tabs
== browser
->tab_strip_model()->count()) &&
112 (num_panels
== GetPanelCount(browser
)) &&
113 (num_popups
== num_popups_seen
));
116 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, BrowserIsApp
) {
117 host_resolver()->AddRule("a.com", "127.0.0.1");
118 ASSERT_TRUE(StartEmbeddedTestServer());
119 ASSERT_TRUE(LoadExtension(
120 test_data_dir_
.AppendASCII("window_open").AppendASCII("browser_is_app")));
122 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 0, 2, 0));
124 for (chrome::BrowserIterator iter
; !iter
.done(); iter
.Next()) {
125 if (*iter
== browser())
126 ASSERT_FALSE(iter
->is_app());
128 ASSERT_TRUE(iter
->is_app());
132 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, WindowOpenPopupDefault
) {
133 ASSERT_TRUE(StartEmbeddedTestServer());
134 ASSERT_TRUE(LoadExtension(
135 test_data_dir_
.AppendASCII("window_open").AppendASCII("popup")));
137 const int num_tabs
= 1;
138 const int num_popups
= 0;
139 EXPECT_TRUE(WaitForTabsAndPopups(browser(), num_tabs
, num_popups
, 0));
142 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, WindowOpenPopupIframe
) {
143 ASSERT_TRUE(StartEmbeddedTestServer());
144 base::FilePath test_data_dir
;
145 PathService::Get(chrome::DIR_TEST_DATA
, &test_data_dir
);
146 embedded_test_server()->ServeFilesFromDirectory(test_data_dir
);
147 ASSERT_TRUE(LoadExtension(
148 test_data_dir_
.AppendASCII("window_open").AppendASCII("popup_iframe")));
150 const int num_tabs
= 1;
151 const int num_popups
= 0;
152 EXPECT_TRUE(WaitForTabsAndPopups(browser(), num_tabs
, num_popups
, 0));
155 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, WindowOpenPopupLarge
) {
156 ASSERT_TRUE(StartEmbeddedTestServer());
157 ASSERT_TRUE(LoadExtension(
158 test_data_dir_
.AppendASCII("window_open").AppendASCII("popup_large")));
160 // On other systems this should open a new popup window.
161 const int num_tabs
= 0;
162 const int num_popups
= 1;
163 EXPECT_TRUE(WaitForTabsAndPopups(browser(), num_tabs
, num_popups
, 0));
166 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, WindowOpenPopupSmall
) {
167 ASSERT_TRUE(StartEmbeddedTestServer());
168 ASSERT_TRUE(LoadExtension(
169 test_data_dir_
.AppendASCII("window_open").AppendASCII("popup_small")));
171 // On ChromeOS this should open a new panel (acts like a new popup window).
172 // On other systems this should open a new popup window.
173 const int num_tabs
= 0;
174 const int num_popups
= 1;
175 EXPECT_TRUE(WaitForTabsAndPopups(browser(), num_tabs
, num_popups
, 0));
178 // Disabled on Windows. Often times out or fails: crbug.com/177530
180 #define MAYBE_PopupBlockingExtension DISABLED_PopupBlockingExtension
182 #define MAYBE_PopupBlockingExtension PopupBlockingExtension
184 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, MAYBE_PopupBlockingExtension
) {
185 host_resolver()->AddRule("*", "127.0.0.1");
186 ASSERT_TRUE(StartEmbeddedTestServer());
188 ASSERT_TRUE(LoadExtension(
189 test_data_dir_
.AppendASCII("window_open").AppendASCII("popup_blocking")
190 .AppendASCII("extension")));
192 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 5, 3, 0));
195 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, PopupBlockingHostedApp
) {
196 host_resolver()->AddRule("*", "127.0.0.1");
197 ASSERT_TRUE(test_server()->Start());
199 ASSERT_TRUE(LoadExtension(
200 test_data_dir_
.AppendASCII("window_open").AppendASCII("popup_blocking")
201 .AppendASCII("hosted_app")));
203 // The app being tested owns the domain a.com . The test URLs we navigate
204 // to below must be within that domain, so that they fall within the app's
206 GURL::Replacements replace_host
;
207 replace_host
.SetHostStr("a.com");
209 const std::string
popup_app_contents_path(
210 "files/extensions/api_test/window_open/popup_blocking/hosted_app/");
213 test_server()->GetURL(popup_app_contents_path
+ "open_tab.html")
214 .ReplaceComponents(replace_host
);
216 test_server()->GetURL(popup_app_contents_path
+ "open_popup.html")
217 .ReplaceComponents(replace_host
);
219 browser()->OpenURL(OpenURLParams(
220 open_tab
, Referrer(), NEW_FOREGROUND_TAB
, ui::PAGE_TRANSITION_TYPED
,
222 browser()->OpenURL(OpenURLParams(
223 open_popup
, Referrer(), NEW_FOREGROUND_TAB
,
224 ui::PAGE_TRANSITION_TYPED
, false));
226 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 3, 1, 0));
229 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, WindowArgumentsOverflow
) {
230 ASSERT_TRUE(RunExtensionTest("window_open/argument_overflow")) << message_
;
233 class WindowOpenPanelDisabledTest
: public ExtensionApiTest
{
234 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
235 ExtensionApiTest::SetUpCommandLine(command_line
);
236 // TODO(jennb): Re-enable when panels are enabled by default.
237 // command_line->AppendSwitch(switches::kDisablePanels);
241 IN_PROC_BROWSER_TEST_F(WindowOpenPanelDisabledTest
,
242 DISABLED_WindowOpenPanelNotEnabled
) {
243 ASSERT_TRUE(RunExtensionTest("window_open/panel_not_enabled")) << message_
;
246 class WindowOpenPanelTest
: public ExtensionApiTest
{
247 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
248 ExtensionApiTest::SetUpCommandLine(command_line
);
249 command_line
->AppendSwitch(switches::kEnablePanels
);
253 #if defined(USE_ASH_PANELS)
254 // On Ash, this currently fails because we're currently opening new panel
255 // windows as popup windows instead.
256 #define MAYBE_WindowOpenPanel DISABLED_WindowOpenPanel
258 #define MAYBE_WindowOpenPanel WindowOpenPanel
260 IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest
, MAYBE_WindowOpenPanel
) {
261 ASSERT_TRUE(RunExtensionTest("window_open/panel")) << message_
;
264 #if defined(USE_ASH_PANELS) || defined(OS_LINUX)
265 // On Ash, this currently fails because we're currently opening new panel
266 // windows as popup windows instead.
267 // We're also failing on Linux-aura due to the panel is not opened in the
269 #define MAYBE_WindowOpenPanelDetached DISABLED_WindowOpenPanelDetached
271 #define MAYBE_WindowOpenPanelDetached WindowOpenPanelDetached
273 IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest
, MAYBE_WindowOpenPanelDetached
) {
274 ASSERT_TRUE(RunExtensionTest("window_open/panel_detached")) << message_
;
277 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
278 // TODO(erg): Bring up ash http://crbug.com/300084
279 #define MAYBE_CloseNonExtensionPanelsOnUninstall \
280 DISABLED_CloseNonExtensionPanelsOnUninstall
282 #define MAYBE_CloseNonExtensionPanelsOnUninstall \
283 CloseNonExtensionPanelsOnUninstall
285 IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest
,
286 MAYBE_CloseNonExtensionPanelsOnUninstall
) {
287 #if defined(OS_WIN) && defined(USE_ASH)
288 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
289 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
290 switches::kAshBrowserTests
))
294 #if defined(USE_ASH_PANELS)
295 // On Ash, new panel windows open as popup windows instead.
296 int num_popups
, num_panels
;
297 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
298 switches::kEnablePanels
)) {
309 ASSERT_TRUE(StartEmbeddedTestServer());
311 // Setup listeners to wait on strings we expect the extension pages to send.
312 std::vector
<std::string
> test_strings
;
313 test_strings
.push_back("content_tab");
315 test_strings
.push_back("content_panel");
316 test_strings
.push_back("content_popup");
318 ScopedVector
<ExtensionTestMessageListener
> listeners
;
319 for (size_t i
= 0; i
< test_strings
.size(); ++i
) {
321 new ExtensionTestMessageListener(test_strings
[i
], false));
324 const extensions::Extension
* extension
= LoadExtension(
325 test_data_dir_
.AppendASCII("window_open").AppendASCII(
326 "close_panels_on_uninstall"));
327 ASSERT_TRUE(extension
);
329 // Two tabs. One in extension domain and one in non-extension domain.
330 // Two popups - one in extension domain and one in non-extension domain.
331 // Two panels - one in extension domain and one in non-extension domain.
332 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 2, num_popups
, num_panels
));
334 // Wait on test messages to make sure the pages loaded.
335 for (size_t i
= 0; i
< listeners
.size(); ++i
)
336 ASSERT_TRUE(listeners
[i
]->WaitUntilSatisfied());
338 UninstallExtension(extension
->id());
340 // Wait for the tabs and popups in non-extension domain to stay open.
341 // Expect everything else, including panels, to close.
343 #if defined(USE_ASH_PANELS)
344 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
345 switches::kEnablePanels
)) {
346 // On Ash, new panel windows open as popup windows instead, so there are 2
347 // extension domain popups that will close (instead of 1 popup on non-Ash).
353 // On linux ash we close all popup applications when closing its extension.
357 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 1, num_popups
, 0));
360 // This test isn't applicable on Chrome OS, which automatically reloads crashed
362 #if !defined(OS_CHROMEOS)
363 IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest
, ClosePanelsOnExtensionCrash
) {
364 #if defined(USE_ASH_PANELS)
365 // On Ash, new panel windows open as popup windows instead.
372 ASSERT_TRUE(StartEmbeddedTestServer());
374 // Setup listeners to wait on strings we expect the extension pages to send.
375 std::vector
<std::string
> test_strings
;
376 test_strings
.push_back("content_tab");
378 test_strings
.push_back("content_panel");
379 test_strings
.push_back("content_popup");
381 ScopedVector
<ExtensionTestMessageListener
> listeners
;
382 for (size_t i
= 0; i
< test_strings
.size(); ++i
) {
384 new ExtensionTestMessageListener(test_strings
[i
], false));
387 const extensions::Extension
* extension
= LoadExtension(
388 test_data_dir_
.AppendASCII("window_open").AppendASCII(
389 "close_panels_on_uninstall"));
390 ASSERT_TRUE(extension
);
392 // Two tabs. One in extension domain and one in non-extension domain.
393 // Two popups - one in extension domain and one in non-extension domain.
394 // Two panels - one in extension domain and one in non-extension domain.
395 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 2, num_popups
, num_panels
));
397 // Wait on test messages to make sure the pages loaded.
398 for (size_t i
= 0; i
< listeners
.size(); ++i
)
399 ASSERT_TRUE(listeners
[i
]->WaitUntilSatisfied());
401 // Crash the extension.
402 extensions::ExtensionHost
* extension_host
=
403 extensions::ProcessManager::Get(browser()->profile())
404 ->GetBackgroundHostForExtension(extension
->id());
405 ASSERT_TRUE(extension_host
);
406 extension_host
->render_process_host()->Shutdown(content::RESULT_CODE_KILLED
,
408 WaitForExtensionCrash(extension
->id());
410 // Only expect panels to close. The rest stay open to show a sad-tab.
411 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 2, num_popups
, 0));
413 #endif // !defined(OS_CHROMEOS)
415 #if defined(USE_ASH_PANELS)
416 // This test is not applicable on Ash. The modified window.open behavior only
417 // applies to non-Ash panel windows.
418 #define MAYBE_WindowOpenFromPanel DISABLED_WindowOpenFromPanel
420 #define MAYBE_WindowOpenFromPanel WindowOpenFromPanel
422 IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest
, MAYBE_WindowOpenFromPanel
) {
423 ASSERT_TRUE(StartEmbeddedTestServer());
425 // Load the extension that will open a panel which then calls window.open.
426 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("window_open").
427 AppendASCII("panel_window_open")));
429 // Expect one panel (opened by extension) and one tab (from the panel calling
430 // window.open). Panels modify the WindowOpenDisposition in window.open
431 // to always open in a tab.
432 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 1, 0, 1));
435 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, DISABLED_WindowOpener
) {
436 ASSERT_TRUE(RunExtensionTest("window_open/opener")) << message_
;
439 #if defined(OS_MACOSX)
440 // Extension popup windows are incorrectly sized on OSX, crbug.com/225601
441 #define MAYBE_WindowOpenSized DISABLED_WindowOpenSized
443 #define MAYBE_WindowOpenSized WindowOpenSized
445 // Ensure that the width and height properties of a window opened with
446 // chrome.windows.create match the creation parameters. See crbug.com/173831.
447 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, MAYBE_WindowOpenSized
) {
448 ASSERT_TRUE(RunExtensionTest("window_open/window_size")) << message_
;
449 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 0, 1, 0));
452 // Tests that an extension page can call window.open to an extension URL and
453 // the new window has extension privileges.
454 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest
, WindowOpenExtension
) {
455 ASSERT_TRUE(LoadExtension(
456 test_data_dir_
.AppendASCII("uitest").AppendASCII("window_open")));
458 GURL
start_url(std::string(extensions::kExtensionScheme
) +
459 url::kStandardSchemeSeparator
+
460 last_loaded_extension_id() + "/test.html");
461 ui_test_utils::NavigateToURL(browser(), start_url
);
462 WebContents
* newtab
= NULL
;
463 ASSERT_NO_FATAL_FAILURE(
464 OpenWindow(browser()->tab_strip_model()->GetActiveWebContents(),
465 start_url
.Resolve("newtab.html"), true, &newtab
));
468 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(newtab
, "testExtensionApi()",
473 // Tests that if an extension page calls window.open to an invalid extension
474 // URL, the browser doesn't crash.
475 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest
, WindowOpenInvalidExtension
) {
476 ASSERT_TRUE(LoadExtension(
477 test_data_dir_
.AppendASCII("uitest").AppendASCII("window_open")));
479 GURL
start_url(std::string(extensions::kExtensionScheme
) +
480 url::kStandardSchemeSeparator
+
481 last_loaded_extension_id() + "/test.html");
482 ui_test_utils::NavigateToURL(browser(), start_url
);
483 ASSERT_NO_FATAL_FAILURE(
484 OpenWindow(browser()->tab_strip_model()->GetActiveWebContents(),
485 GURL("chrome-extension://thisissurelynotavalidextensionid/newtab.html"),
488 // If we got to this point, we didn't crash, so we're good.
491 // Tests that calling window.open from the newtab page to an extension URL
492 // gives the new window extension privileges - even though the opening page
493 // does not have extension privileges, we break the script connection, so
494 // there is no privilege leak.
495 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest
, WindowOpenNoPrivileges
) {
496 ASSERT_TRUE(LoadExtension(
497 test_data_dir_
.AppendASCII("uitest").AppendASCII("window_open")));
499 ui_test_utils::NavigateToURL(browser(), GURL("about:blank"));
500 WebContents
* newtab
= NULL
;
501 ASSERT_NO_FATAL_FAILURE(
502 OpenWindow(browser()->tab_strip_model()->GetActiveWebContents(),
503 GURL(std::string(extensions::kExtensionScheme
) +
504 url::kStandardSchemeSeparator
+
505 last_loaded_extension_id() + "/newtab.html"),
509 // Extension API should succeed.
511 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(newtab
, "testExtensionApi()",