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/extensions/extension_host.h"
11 #include "chrome/browser/extensions/extension_system.h"
12 #include "chrome/browser/extensions/extension_test_message_listener.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_finder.h"
16 #include "chrome/browser/ui/browser_iterator.h"
17 #include "chrome/browser/ui/panels/panel_manager.h"
18 #include "chrome/browser/ui/tabs/tab_strip_model.h"
19 #include "chrome/common/chrome_paths.h"
20 #include "chrome/common/chrome_switches.h"
21 #include "chrome/test/base/test_switches.h"
22 #include "chrome/test/base/ui_test_utils.h"
23 #include "content/public/browser/render_process_host.h"
24 #include "content/public/browser/web_contents.h"
25 #include "content/public/common/result_codes.h"
26 #include "content/public/test/browser_test_utils.h"
27 #include "extensions/browser/process_manager.h"
28 #include "extensions/common/extension.h"
29 #include "extensions/common/switches.h"
30 #include "net/dns/mock_host_resolver.h"
31 #include "net/test/embedded_test_server/embedded_test_server.h"
32 #include "testing/gtest/include/gtest/gtest.h"
35 #include "apps/shell_window_registry.h"
38 #if defined(USE_ASH) && !defined(OS_WIN)
39 // TODO(stevenjb): Figure out the correct behavior for Ash + Win
40 #define USE_ASH_PANELS
43 using content::OpenURLParams
;
44 using content::Referrer
;
45 using content::WebContents
;
47 // Disabled, http://crbug.com/64899.
48 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, DISABLED_WindowOpen
) {
49 CommandLine::ForCurrentProcess()->AppendSwitch(
50 extensions::switches::kEnableExperimentalExtensionApis
);
52 ResultCatcher catcher
;
53 ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_
54 .AppendASCII("window_open").AppendASCII("spanning")));
55 EXPECT_TRUE(catcher
.GetNextResult()) << catcher
.message();
58 int GetPanelCount(Browser
* browser
) {
59 #if defined(USE_ASH_PANELS)
60 return static_cast<int>(apps::ShellWindowRegistry::Get(
61 browser
->profile())->shell_windows().size());
63 return PanelManager::GetInstance()->num_panels();
67 bool WaitForTabsAndPopups(Browser
* browser
,
72 base::StringPrintf("WaitForTabsAndPopups tabs:%d, popups:%d, panels:%d",
73 num_tabs
, num_popups
, num_panels
));
74 // We start with one tab and one browser already open.
76 size_t num_browsers
= static_cast<size_t>(num_popups
) + 1;
78 const base::TimeDelta kWaitTime
= base::TimeDelta::FromSeconds(10);
79 base::TimeTicks end_time
= base::TimeTicks::Now() + kWaitTime
;
80 while (base::TimeTicks::Now() < end_time
) {
81 if (chrome::GetBrowserCount(browser
->profile(),
82 browser
->host_desktop_type()) == num_browsers
&&
83 browser
->tab_strip_model()->count() == num_tabs
&&
84 GetPanelCount(browser
) == num_panels
)
87 content::RunAllPendingInMessageLoop();
90 EXPECT_EQ(num_browsers
,
91 chrome::GetBrowserCount(browser
->profile(),
92 browser
->host_desktop_type()));
93 EXPECT_EQ(num_tabs
, browser
->tab_strip_model()->count());
94 EXPECT_EQ(num_panels
, GetPanelCount(browser
));
96 int num_popups_seen
= 0;
97 for (chrome::BrowserIterator iter
; !iter
.done(); iter
.Next()) {
101 EXPECT_TRUE((*iter
)->is_type_popup());
104 EXPECT_EQ(num_popups
, num_popups_seen
);
106 return ((num_browsers
==
107 chrome::GetBrowserCount(browser
->profile(),
108 browser
->host_desktop_type())) &&
109 (num_tabs
== browser
->tab_strip_model()->count()) &&
110 (num_panels
== GetPanelCount(browser
)) &&
111 (num_popups
== num_popups_seen
));
114 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, BrowserIsApp
) {
115 host_resolver()->AddRule("a.com", "127.0.0.1");
116 ASSERT_TRUE(StartEmbeddedTestServer());
117 ASSERT_TRUE(LoadExtension(
118 test_data_dir_
.AppendASCII("window_open").AppendASCII("browser_is_app")));
120 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 0, 2, 0));
122 for (chrome::BrowserIterator iter
; !iter
.done(); iter
.Next()) {
123 if (*iter
== browser())
124 ASSERT_FALSE(iter
->is_app());
126 ASSERT_TRUE(iter
->is_app());
130 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, WindowOpenPopupDefault
) {
131 ASSERT_TRUE(StartEmbeddedTestServer());
132 ASSERT_TRUE(LoadExtension(
133 test_data_dir_
.AppendASCII("window_open").AppendASCII("popup")));
135 const int num_tabs
= 1;
136 const int num_popups
= 0;
137 EXPECT_TRUE(WaitForTabsAndPopups(browser(), num_tabs
, num_popups
, 0));
140 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, WindowOpenPopupIframe
) {
141 ASSERT_TRUE(StartEmbeddedTestServer());
142 base::FilePath test_data_dir
;
143 PathService::Get(chrome::DIR_TEST_DATA
, &test_data_dir
);
144 embedded_test_server()->ServeFilesFromDirectory(test_data_dir
);
145 ASSERT_TRUE(LoadExtension(
146 test_data_dir_
.AppendASCII("window_open").AppendASCII("popup_iframe")));
148 const int num_tabs
= 0;
149 const int num_popups
= 1;
150 EXPECT_TRUE(WaitForTabsAndPopups(browser(), num_tabs
, num_popups
, 0));
153 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, WindowOpenPopupLarge
) {
154 ASSERT_TRUE(StartEmbeddedTestServer());
155 ASSERT_TRUE(LoadExtension(
156 test_data_dir_
.AppendASCII("window_open").AppendASCII("popup_large")));
158 // On other systems this should open a new popup window.
159 const int num_tabs
= 0;
160 const int num_popups
= 1;
161 EXPECT_TRUE(WaitForTabsAndPopups(browser(), num_tabs
, num_popups
, 0));
164 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, WindowOpenPopupSmall
) {
165 ASSERT_TRUE(StartEmbeddedTestServer());
166 ASSERT_TRUE(LoadExtension(
167 test_data_dir_
.AppendASCII("window_open").AppendASCII("popup_small")));
169 // On ChromeOS this should open a new panel (acts like a new popup window).
170 // On other systems this should open a new popup window.
171 const int num_tabs
= 0;
172 const int num_popups
= 1;
173 EXPECT_TRUE(WaitForTabsAndPopups(browser(), num_tabs
, num_popups
, 0));
176 // Disabled on Windows. Often times out or fails: crbug.com/177530
178 #define MAYBE_PopupBlockingExtension DISABLED_PopupBlockingExtension
180 #define MAYBE_PopupBlockingExtension PopupBlockingExtension
182 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, MAYBE_PopupBlockingExtension
) {
183 host_resolver()->AddRule("*", "127.0.0.1");
184 ASSERT_TRUE(StartEmbeddedTestServer());
186 ASSERT_TRUE(LoadExtension(
187 test_data_dir_
.AppendASCII("window_open").AppendASCII("popup_blocking")
188 .AppendASCII("extension")));
190 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 5, 3, 0));
193 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, PopupBlockingHostedApp
) {
194 host_resolver()->AddRule("*", "127.0.0.1");
195 ASSERT_TRUE(test_server()->Start());
197 ASSERT_TRUE(LoadExtension(
198 test_data_dir_
.AppendASCII("window_open").AppendASCII("popup_blocking")
199 .AppendASCII("hosted_app")));
201 // The app being tested owns the domain a.com . The test URLs we navigate
202 // to below must be within that domain, so that they fall within the app's
204 GURL::Replacements replace_host
;
205 std::string a_dot_com
= "a.com";
206 replace_host
.SetHostStr(a_dot_com
);
208 const std::string
popup_app_contents_path(
209 "files/extensions/api_test/window_open/popup_blocking/hosted_app/");
212 test_server()->GetURL(popup_app_contents_path
+ "open_tab.html")
213 .ReplaceComponents(replace_host
);
215 test_server()->GetURL(popup_app_contents_path
+ "open_popup.html")
216 .ReplaceComponents(replace_host
);
218 browser()->OpenURL(OpenURLParams(
219 open_tab
, Referrer(), NEW_FOREGROUND_TAB
, content::PAGE_TRANSITION_TYPED
,
221 browser()->OpenURL(OpenURLParams(
222 open_popup
, Referrer(), NEW_FOREGROUND_TAB
,
223 content::PAGE_TRANSITION_TYPED
, false));
225 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 3, 1, 0));
228 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, WindowArgumentsOverflow
) {
229 ASSERT_TRUE(RunExtensionTest("window_open/argument_overflow")) << message_
;
232 class WindowOpenPanelDisabledTest
: public ExtensionApiTest
{
233 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
234 ExtensionApiTest::SetUpCommandLine(command_line
);
235 // TODO(jennb): Re-enable when panels are enabled by default.
236 // command_line->AppendSwitch(switches::kDisablePanels);
240 IN_PROC_BROWSER_TEST_F(WindowOpenPanelDisabledTest
,
241 DISABLED_WindowOpenPanelNotEnabled
) {
242 ASSERT_TRUE(RunExtensionTest("window_open/panel_not_enabled")) << message_
;
245 class WindowOpenPanelTest
: public ExtensionApiTest
{
246 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
247 ExtensionApiTest::SetUpCommandLine(command_line
);
248 command_line
->AppendSwitch(switches::kEnablePanels
);
252 #if defined(USE_ASH_PANELS)
253 // On Ash, this currently fails because we're currently opening new panel
254 // windows as popup windows instead.
255 #define MAYBE_WindowOpenPanel DISABLED_WindowOpenPanel
257 #define MAYBE_WindowOpenPanel WindowOpenPanel
259 IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest
, MAYBE_WindowOpenPanel
) {
260 ASSERT_TRUE(RunExtensionTest("window_open/panel")) << message_
;
263 #if defined(USE_ASH_PANELS)
264 // On Ash, this currently fails because we're currently opening new panel
265 // windows as popup windows instead.
266 #define MAYBE_WindowOpenPanelDetached DISABLED_WindowOpenPanelDetached
268 #define MAYBE_WindowOpenPanelDetached WindowOpenPanelDetached
270 IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest
, MAYBE_WindowOpenPanelDetached
) {
271 ASSERT_TRUE(RunExtensionTest("window_open/panel_detached")) << message_
;
274 IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest
,
275 CloseNonExtensionPanelsOnUninstall
) {
276 #if defined(OS_WIN) && defined(USE_ASH)
277 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
278 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
282 #if defined(USE_ASH_PANELS)
283 // On Ash, new panel windows open as popup windows instead.
284 int num_popups
, num_panels
;
285 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnablePanels
)) {
296 ASSERT_TRUE(StartEmbeddedTestServer());
298 // Setup listeners to wait on strings we expect the extension pages to send.
299 std::vector
<std::string
> test_strings
;
300 test_strings
.push_back("content_tab");
302 test_strings
.push_back("content_panel");
303 test_strings
.push_back("content_popup");
305 ScopedVector
<ExtensionTestMessageListener
> listeners
;
306 for (size_t i
= 0; i
< test_strings
.size(); ++i
) {
308 new ExtensionTestMessageListener(test_strings
[i
], false));
311 const extensions::Extension
* extension
= LoadExtension(
312 test_data_dir_
.AppendASCII("window_open").AppendASCII(
313 "close_panels_on_uninstall"));
314 ASSERT_TRUE(extension
);
316 // Two tabs. One in extension domain and one in non-extension domain.
317 // Two popups - one in extension domain and one in non-extension domain.
318 // Two panels - one in extension domain and one in non-extension domain.
319 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 2, num_popups
, num_panels
));
321 // Wait on test messages to make sure the pages loaded.
322 for (size_t i
= 0; i
< listeners
.size(); ++i
)
323 ASSERT_TRUE(listeners
[i
]->WaitUntilSatisfied());
325 UninstallExtension(extension
->id());
327 // Wait for the tabs and popups in non-extension domain to stay open.
328 // Expect everything else, including panels, to close.
330 #if defined(USE_ASH_PANELS)
331 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnablePanels
)) {
332 // On Ash, new panel windows open as popup windows instead, so there are 2
333 // extension domain popups that will close (instead of 1 popup on non-Ash).
339 // On linux ash we close all popup applications when closing its extension.
343 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 1, num_popups
, 0));
346 // This test isn't applicable on Chrome OS, which automatically reloads
348 #if !defined(OS_CHROMEOS)
349 IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest
, ClosePanelsOnExtensionCrash
) {
350 #if defined(USE_ASH_PANELS)
351 // On Ash, new panel windows open as popup windows instead.
358 ASSERT_TRUE(StartEmbeddedTestServer());
360 // Setup listeners to wait on strings we expect the extension pages to send.
361 std::vector
<std::string
> test_strings
;
362 test_strings
.push_back("content_tab");
364 test_strings
.push_back("content_panel");
365 test_strings
.push_back("content_popup");
367 ScopedVector
<ExtensionTestMessageListener
> listeners
;
368 for (size_t i
= 0; i
< test_strings
.size(); ++i
) {
370 new ExtensionTestMessageListener(test_strings
[i
], false));
373 const extensions::Extension
* extension
= LoadExtension(
374 test_data_dir_
.AppendASCII("window_open").AppendASCII(
375 "close_panels_on_uninstall"));
376 ASSERT_TRUE(extension
);
378 // Two tabs. One in extension domain and one in non-extension domain.
379 // Two popups - one in extension domain and one in non-extension domain.
380 // Two panels - one in extension domain and one in non-extension domain.
381 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 2, num_popups
, num_panels
));
383 // Wait on test messages to make sure the pages loaded.
384 for (size_t i
= 0; i
< listeners
.size(); ++i
)
385 ASSERT_TRUE(listeners
[i
]->WaitUntilSatisfied());
387 // Crash the extension.
388 extensions::ExtensionHost
* extension_host
=
389 extensions::ExtensionSystem::Get(browser()->profile())->
390 process_manager()->GetBackgroundHostForExtension(extension
->id());
391 ASSERT_TRUE(extension_host
);
392 base::KillProcess(extension_host
->render_process_host()->GetHandle(),
393 content::RESULT_CODE_KILLED
, false);
394 WaitForExtensionCrash(extension
->id());
396 // Only expect panels to close. The rest stay open to show a sad-tab.
397 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 2, num_popups
, 0));
399 #endif // !defined(OS_CHROMEOS)
401 #if defined(USE_ASH_PANELS)
402 // This test is not applicable on Ash. The modified window.open behavior only
403 // applies to non-Ash panel windows.
404 #define MAYBE_WindowOpenFromPanel DISABLED_WindowOpenFromPanel
406 #define MAYBE_WindowOpenFromPanel WindowOpenFromPanel
408 IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest
, MAYBE_WindowOpenFromPanel
) {
409 ASSERT_TRUE(StartEmbeddedTestServer());
411 // Load the extension that will open a panel which then calls window.open.
412 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("window_open").
413 AppendASCII("panel_window_open")));
415 // Expect one panel (opened by extension) and one tab (from the panel calling
416 // window.open). Panels modify the WindowOpenDisposition in window.open
417 // to always open in a tab.
418 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 1, 0, 1));
421 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, DISABLED_WindowOpener
) {
422 ASSERT_TRUE(RunExtensionTest("window_open/opener")) << message_
;
425 #if defined(OS_MACOSX)
426 // Extension popup windows are incorrectly sized on OSX, crbug.com/225601
427 #define MAYBE_WindowOpenSized DISABLED_WindowOpenSized
429 #define MAYBE_WindowOpenSized WindowOpenSized
431 // Ensure that the width and height properties of a window opened with
432 // chrome.windows.create match the creation parameters. See crbug.com/173831.
433 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, MAYBE_WindowOpenSized
) {
434 ASSERT_TRUE(RunExtensionTest("window_open/window_size")) << message_
;
435 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 0, 1, 0));
438 // Tests that an extension page can call window.open to an extension URL and
439 // the new window has extension privileges.
440 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest
, WindowOpenExtension
) {
441 ASSERT_TRUE(LoadExtension(
442 test_data_dir_
.AppendASCII("uitest").AppendASCII("window_open")));
444 GURL
start_url(std::string("chrome-extension://") +
445 last_loaded_extension_id() + "/test.html");
446 ui_test_utils::NavigateToURL(browser(), start_url
);
447 WebContents
* newtab
= NULL
;
448 ASSERT_NO_FATAL_FAILURE(
449 OpenWindow(browser()->tab_strip_model()->GetActiveWebContents(),
450 start_url
.Resolve("newtab.html"), true, &newtab
));
453 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(newtab
, "testExtensionApi()",
458 // Tests that if an extension page calls window.open to an invalid extension
459 // URL, the browser doesn't crash.
460 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest
, WindowOpenInvalidExtension
) {
461 ASSERT_TRUE(LoadExtension(
462 test_data_dir_
.AppendASCII("uitest").AppendASCII("window_open")));
464 GURL
start_url(std::string("chrome-extension://") +
465 last_loaded_extension_id() + "/test.html");
466 ui_test_utils::NavigateToURL(browser(), start_url
);
467 ASSERT_NO_FATAL_FAILURE(
468 OpenWindow(browser()->tab_strip_model()->GetActiveWebContents(),
469 GURL("chrome-extension://thisissurelynotavalidextensionid/newtab.html"),
472 // If we got to this point, we didn't crash, so we're good.
475 // Tests that calling window.open from the newtab page to an extension URL
476 // gives the new window extension privileges - even though the opening page
477 // does not have extension privileges, we break the script connection, so
478 // there is no privilege leak.
479 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest
, WindowOpenNoPrivileges
) {
480 ASSERT_TRUE(LoadExtension(
481 test_data_dir_
.AppendASCII("uitest").AppendASCII("window_open")));
483 ui_test_utils::NavigateToURL(browser(), GURL("about:blank"));
484 WebContents
* newtab
= NULL
;
485 ASSERT_NO_FATAL_FAILURE(
486 OpenWindow(browser()->tab_strip_model()->GetActiveWebContents(),
487 GURL(std::string("chrome-extension://") + last_loaded_extension_id() +
488 "/newtab.html"), false, &newtab
));
490 // Extension API should succeed.
492 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(newtab
, "testExtensionApi()",