Prevent chrome://net-internals/#export from flickering
[chromium-blink-merge.git] / chrome / browser / extensions / window_open_apitest.cc
blob011ff07c8fc732bb381d62f0b0997bed52b8ec7a
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"
36 #if defined(USE_ASH)
37 #include "extensions/browser/app_window/app_window_registry.h"
38 #endif
40 #if defined(USE_ASH) && defined(OS_CHROMEOS)
41 // TODO(stevenjb): Figure out the correct behavior for Ash + Win
42 #define USE_ASH_PANELS
43 #endif
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 #if defined(OS_MACOSX)
53 #define MAYBE_WindowOpen DISABLED_WindowOpen
54 #else
55 #define MAYBE_WindowOpen WindowOpen
56 #endif
57 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, MAYBE_WindowOpen) {
58 extensions::ResultCatcher catcher;
59 ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_
60 .AppendASCII("window_open").AppendASCII("spanning")));
61 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
64 int GetPanelCount(Browser* browser) {
65 #if defined(USE_ASH_PANELS)
66 return static_cast<int>(extensions::AppWindowRegistry::Get(
67 browser->profile())->app_windows().size());
68 #else
69 return PanelManager::GetInstance()->num_panels();
70 #endif
73 bool WaitForTabsAndPopups(Browser* browser,
74 int num_tabs,
75 int num_popups,
76 int num_panels) {
77 SCOPED_TRACE(
78 base::StringPrintf("WaitForTabsAndPopups tabs:%d, popups:%d, panels:%d",
79 num_tabs, num_popups, num_panels));
80 // We start with one tab and one browser already open.
81 ++num_tabs;
82 size_t num_browsers = static_cast<size_t>(num_popups) + 1;
84 const base::TimeDelta kWaitTime = base::TimeDelta::FromSeconds(10);
85 base::TimeTicks end_time = base::TimeTicks::Now() + kWaitTime;
86 while (base::TimeTicks::Now() < end_time) {
87 if (chrome::GetBrowserCount(browser->profile(),
88 browser->host_desktop_type()) == num_browsers &&
89 browser->tab_strip_model()->count() == num_tabs &&
90 GetPanelCount(browser) == num_panels)
91 break;
93 content::RunAllPendingInMessageLoop();
96 EXPECT_EQ(num_browsers,
97 chrome::GetBrowserCount(browser->profile(),
98 browser->host_desktop_type()));
99 EXPECT_EQ(num_tabs, browser->tab_strip_model()->count());
100 EXPECT_EQ(num_panels, GetPanelCount(browser));
102 int num_popups_seen = 0;
103 for (chrome::BrowserIterator iter; !iter.done(); iter.Next()) {
104 if (*iter == browser)
105 continue;
107 EXPECT_TRUE((*iter)->is_type_popup());
108 ++num_popups_seen;
110 EXPECT_EQ(num_popups, num_popups_seen);
112 return ((num_browsers ==
113 chrome::GetBrowserCount(browser->profile(),
114 browser->host_desktop_type())) &&
115 (num_tabs == browser->tab_strip_model()->count()) &&
116 (num_panels == GetPanelCount(browser)) &&
117 (num_popups == num_popups_seen));
120 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, BrowserIsApp) {
121 host_resolver()->AddRule("a.com", "127.0.0.1");
122 ASSERT_TRUE(StartEmbeddedTestServer());
123 ASSERT_TRUE(LoadExtension(
124 test_data_dir_.AppendASCII("window_open").AppendASCII("browser_is_app")));
126 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 0, 2, 0));
128 for (chrome::BrowserIterator iter; !iter.done(); iter.Next()) {
129 if (*iter == browser())
130 ASSERT_FALSE(iter->is_app());
131 else
132 ASSERT_TRUE(iter->is_app());
136 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowOpenPopupDefault) {
137 ASSERT_TRUE(StartEmbeddedTestServer());
138 ASSERT_TRUE(LoadExtension(
139 test_data_dir_.AppendASCII("window_open").AppendASCII("popup")));
141 const int num_tabs = 1;
142 const int num_popups = 0;
143 EXPECT_TRUE(WaitForTabsAndPopups(browser(), num_tabs, num_popups, 0));
146 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowOpenPopupIframe) {
147 ASSERT_TRUE(StartEmbeddedTestServer());
148 base::FilePath test_data_dir;
149 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir);
150 embedded_test_server()->ServeFilesFromDirectory(test_data_dir);
151 ASSERT_TRUE(LoadExtension(
152 test_data_dir_.AppendASCII("window_open").AppendASCII("popup_iframe")));
154 const int num_tabs = 1;
155 const int num_popups = 0;
156 EXPECT_TRUE(WaitForTabsAndPopups(browser(), num_tabs, num_popups, 0));
159 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowOpenPopupLarge) {
160 ASSERT_TRUE(StartEmbeddedTestServer());
161 ASSERT_TRUE(LoadExtension(
162 test_data_dir_.AppendASCII("window_open").AppendASCII("popup_large")));
164 // On other systems this should open a new popup window.
165 const int num_tabs = 0;
166 const int num_popups = 1;
167 EXPECT_TRUE(WaitForTabsAndPopups(browser(), num_tabs, num_popups, 0));
170 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowOpenPopupSmall) {
171 ASSERT_TRUE(StartEmbeddedTestServer());
172 ASSERT_TRUE(LoadExtension(
173 test_data_dir_.AppendASCII("window_open").AppendASCII("popup_small")));
175 // On ChromeOS this should open a new panel (acts like a new popup window).
176 // On other systems this should open a new popup window.
177 const int num_tabs = 0;
178 const int num_popups = 1;
179 EXPECT_TRUE(WaitForTabsAndPopups(browser(), num_tabs, num_popups, 0));
182 // Disabled on Windows. Often times out or fails: crbug.com/177530
183 #if defined(OS_WIN)
184 #define MAYBE_PopupBlockingExtension DISABLED_PopupBlockingExtension
185 #else
186 #define MAYBE_PopupBlockingExtension PopupBlockingExtension
187 #endif
188 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, MAYBE_PopupBlockingExtension) {
189 host_resolver()->AddRule("*", "127.0.0.1");
190 ASSERT_TRUE(StartEmbeddedTestServer());
192 ASSERT_TRUE(LoadExtension(
193 test_data_dir_.AppendASCII("window_open").AppendASCII("popup_blocking")
194 .AppendASCII("extension")));
196 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 5, 3, 0));
199 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, PopupBlockingHostedApp) {
200 host_resolver()->AddRule("*", "127.0.0.1");
201 ASSERT_TRUE(test_server()->Start());
203 ASSERT_TRUE(LoadExtension(
204 test_data_dir_.AppendASCII("window_open").AppendASCII("popup_blocking")
205 .AppendASCII("hosted_app")));
207 // The app being tested owns the domain a.com . The test URLs we navigate
208 // to below must be within that domain, so that they fall within the app's
209 // web extent.
210 GURL::Replacements replace_host;
211 replace_host.SetHostStr("a.com");
213 const std::string popup_app_contents_path(
214 "files/extensions/api_test/window_open/popup_blocking/hosted_app/");
216 GURL open_tab =
217 test_server()->GetURL(popup_app_contents_path + "open_tab.html")
218 .ReplaceComponents(replace_host);
219 GURL open_popup =
220 test_server()->GetURL(popup_app_contents_path + "open_popup.html")
221 .ReplaceComponents(replace_host);
223 browser()->OpenURL(OpenURLParams(
224 open_tab, Referrer(), NEW_FOREGROUND_TAB, ui::PAGE_TRANSITION_TYPED,
225 false));
226 browser()->OpenURL(OpenURLParams(
227 open_popup, Referrer(), NEW_FOREGROUND_TAB,
228 ui::PAGE_TRANSITION_TYPED, false));
230 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 3, 1, 0));
233 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowArgumentsOverflow) {
234 ASSERT_TRUE(RunExtensionTest("window_open/argument_overflow")) << message_;
237 class WindowOpenPanelDisabledTest : public ExtensionApiTest {
238 void SetUpCommandLine(base::CommandLine* command_line) override {
239 ExtensionApiTest::SetUpCommandLine(command_line);
240 // TODO(jennb): Re-enable when panels are enabled by default.
241 // command_line->AppendSwitch(switches::kDisablePanels);
245 IN_PROC_BROWSER_TEST_F(WindowOpenPanelDisabledTest,
246 DISABLED_WindowOpenPanelNotEnabled) {
247 ASSERT_TRUE(RunExtensionTest("window_open/panel_not_enabled")) << message_;
250 class WindowOpenPanelTest : public ExtensionApiTest {
251 void SetUpCommandLine(base::CommandLine* command_line) override {
252 ExtensionApiTest::SetUpCommandLine(command_line);
253 command_line->AppendSwitch(switches::kEnablePanels);
257 #if defined(USE_ASH_PANELS)
258 // On Ash, this currently fails because we're currently opening new panel
259 // windows as popup windows instead.
260 #define MAYBE_WindowOpenPanel DISABLED_WindowOpenPanel
261 #else
262 #define MAYBE_WindowOpenPanel WindowOpenPanel
263 #endif
264 IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest, MAYBE_WindowOpenPanel) {
265 ASSERT_TRUE(RunExtensionTest("window_open/panel")) << message_;
268 #if defined(USE_ASH_PANELS) || defined(OS_LINUX)
269 // On Ash, this currently fails because we're currently opening new panel
270 // windows as popup windows instead.
271 // We're also failing on Linux-aura due to the panel is not opened in the
272 // right origin.
273 #define MAYBE_WindowOpenPanelDetached DISABLED_WindowOpenPanelDetached
274 #else
275 #define MAYBE_WindowOpenPanelDetached WindowOpenPanelDetached
276 #endif
277 IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest, MAYBE_WindowOpenPanelDetached) {
278 ASSERT_TRUE(RunExtensionTest("window_open/panel_detached")) << message_;
281 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
282 // TODO(erg): Bring up ash http://crbug.com/300084
283 #define MAYBE_CloseNonExtensionPanelsOnUninstall \
284 DISABLED_CloseNonExtensionPanelsOnUninstall
285 #else
286 #define MAYBE_CloseNonExtensionPanelsOnUninstall \
287 CloseNonExtensionPanelsOnUninstall
288 #endif
289 IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest,
290 MAYBE_CloseNonExtensionPanelsOnUninstall) {
291 #if defined(OS_WIN) && defined(USE_ASH)
292 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
293 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
294 switches::kAshBrowserTests))
295 return;
296 #endif
298 #if defined(USE_ASH_PANELS)
299 // On Ash, new panel windows open as popup windows instead.
300 int num_popups, num_panels;
301 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
302 switches::kEnablePanels)) {
303 num_popups = 2;
304 num_panels = 2;
305 } else {
306 num_popups = 4;
307 num_panels = 0;
309 #else
310 int num_popups = 2;
311 int num_panels = 2;
312 #endif
313 ASSERT_TRUE(StartEmbeddedTestServer());
315 // Setup listeners to wait on strings we expect the extension pages to send.
316 std::vector<std::string> test_strings;
317 test_strings.push_back("content_tab");
318 if (num_panels)
319 test_strings.push_back("content_panel");
320 test_strings.push_back("content_popup");
322 ScopedVector<ExtensionTestMessageListener> listeners;
323 for (size_t i = 0; i < test_strings.size(); ++i) {
324 listeners.push_back(
325 new ExtensionTestMessageListener(test_strings[i], false));
328 const extensions::Extension* extension = LoadExtension(
329 test_data_dir_.AppendASCII("window_open").AppendASCII(
330 "close_panels_on_uninstall"));
331 ASSERT_TRUE(extension);
333 // Two tabs. One in extension domain and one in non-extension domain.
334 // Two popups - one in extension domain and one in non-extension domain.
335 // Two panels - one in extension domain and one in non-extension domain.
336 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 2, num_popups, num_panels));
338 // Wait on test messages to make sure the pages loaded.
339 for (size_t i = 0; i < listeners.size(); ++i)
340 ASSERT_TRUE(listeners[i]->WaitUntilSatisfied());
342 UninstallExtension(extension->id());
344 // Wait for the tabs and popups in non-extension domain to stay open.
345 // Expect everything else, including panels, to close.
346 num_popups -= 1;
347 #if defined(USE_ASH_PANELS)
348 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
349 switches::kEnablePanels)) {
350 // On Ash, new panel windows open as popup windows instead, so there are 2
351 // extension domain popups that will close (instead of 1 popup on non-Ash).
352 num_popups -= 1;
354 #endif
355 #if defined(USE_ASH)
356 #if !defined(OS_WIN)
357 // On linux ash we close all popup applications when closing its extension.
358 num_popups = 0;
359 #endif
360 #endif
361 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 1, num_popups, 0));
364 // This test isn't applicable on Chrome OS, which automatically reloads crashed
365 // pages.
366 #if !defined(OS_CHROMEOS)
367 IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest, ClosePanelsOnExtensionCrash) {
368 #if defined(USE_ASH_PANELS)
369 // On Ash, new panel windows open as popup windows instead.
370 int num_popups = 4;
371 int num_panels = 0;
372 #else
373 int num_popups = 2;
374 int num_panels = 2;
375 #endif
376 ASSERT_TRUE(StartEmbeddedTestServer());
378 // Setup listeners to wait on strings we expect the extension pages to send.
379 std::vector<std::string> test_strings;
380 test_strings.push_back("content_tab");
381 if (num_panels)
382 test_strings.push_back("content_panel");
383 test_strings.push_back("content_popup");
385 ScopedVector<ExtensionTestMessageListener> listeners;
386 for (size_t i = 0; i < test_strings.size(); ++i) {
387 listeners.push_back(
388 new ExtensionTestMessageListener(test_strings[i], false));
391 const extensions::Extension* extension = LoadExtension(
392 test_data_dir_.AppendASCII("window_open").AppendASCII(
393 "close_panels_on_uninstall"));
394 ASSERT_TRUE(extension);
396 // Two tabs. One in extension domain and one in non-extension domain.
397 // Two popups - one in extension domain and one in non-extension domain.
398 // Two panels - one in extension domain and one in non-extension domain.
399 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 2, num_popups, num_panels));
401 // Wait on test messages to make sure the pages loaded.
402 for (size_t i = 0; i < listeners.size(); ++i)
403 ASSERT_TRUE(listeners[i]->WaitUntilSatisfied());
405 // Crash the extension.
406 extensions::ExtensionHost* extension_host =
407 extensions::ProcessManager::Get(browser()->profile())
408 ->GetBackgroundHostForExtension(extension->id());
409 ASSERT_TRUE(extension_host);
410 extension_host->render_process_host()->Shutdown(content::RESULT_CODE_KILLED,
411 false);
412 WaitForExtensionCrash(extension->id());
414 // Only expect panels to close. The rest stay open to show a sad-tab.
415 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 2, num_popups, 0));
417 #endif // !defined(OS_CHROMEOS)
419 #if defined(USE_ASH_PANELS)
420 // This test is not applicable on Ash. The modified window.open behavior only
421 // applies to non-Ash panel windows.
422 #define MAYBE_WindowOpenFromPanel DISABLED_WindowOpenFromPanel
423 #else
424 #define MAYBE_WindowOpenFromPanel WindowOpenFromPanel
425 #endif
426 IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest, MAYBE_WindowOpenFromPanel) {
427 ASSERT_TRUE(StartEmbeddedTestServer());
429 // Load the extension that will open a panel which then calls window.open.
430 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("window_open").
431 AppendASCII("panel_window_open")));
433 // Expect one panel (opened by extension) and one tab (from the panel calling
434 // window.open). Panels modify the WindowOpenDisposition in window.open
435 // to always open in a tab.
436 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 1, 0, 1));
439 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, DISABLED_WindowOpener) {
440 ASSERT_TRUE(RunExtensionTest("window_open/opener")) << message_;
443 #if defined(OS_MACOSX)
444 // Extension popup windows are incorrectly sized on OSX, crbug.com/225601
445 #define MAYBE_WindowOpenSized DISABLED_WindowOpenSized
446 #else
447 #define MAYBE_WindowOpenSized WindowOpenSized
448 #endif
449 // Ensure that the width and height properties of a window opened with
450 // chrome.windows.create match the creation parameters. See crbug.com/173831.
451 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, MAYBE_WindowOpenSized) {
452 ASSERT_TRUE(RunExtensionTest("window_open/window_size")) << message_;
453 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 0, 1, 0));
456 // Tests that an extension page can call window.open to an extension URL and
457 // the new window has extension privileges.
458 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, WindowOpenExtension) {
459 ASSERT_TRUE(LoadExtension(
460 test_data_dir_.AppendASCII("uitest").AppendASCII("window_open")));
462 GURL start_url(std::string(extensions::kExtensionScheme) +
463 url::kStandardSchemeSeparator +
464 last_loaded_extension_id() + "/test.html");
465 ui_test_utils::NavigateToURL(browser(), start_url);
466 WebContents* newtab = NULL;
467 ASSERT_NO_FATAL_FAILURE(
468 OpenWindow(browser()->tab_strip_model()->GetActiveWebContents(),
469 start_url.Resolve("newtab.html"), true, &newtab));
471 bool result = false;
472 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(newtab, "testExtensionApi()",
473 &result));
474 EXPECT_TRUE(result);
477 // Tests that if an extension page calls window.open to an invalid extension
478 // URL, the browser doesn't crash.
479 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, WindowOpenInvalidExtension) {
480 ASSERT_TRUE(LoadExtension(
481 test_data_dir_.AppendASCII("uitest").AppendASCII("window_open")));
483 GURL start_url(std::string(extensions::kExtensionScheme) +
484 url::kStandardSchemeSeparator +
485 last_loaded_extension_id() + "/test.html");
486 ui_test_utils::NavigateToURL(browser(), start_url);
487 ASSERT_NO_FATAL_FAILURE(
488 OpenWindow(browser()->tab_strip_model()->GetActiveWebContents(),
489 GURL("chrome-extension://thisissurelynotavalidextensionid/newtab.html"),
490 false, NULL));
492 // If we got to this point, we didn't crash, so we're good.
495 // Tests that calling window.open from the newtab page to an extension URL
496 // gives the new window extension privileges - even though the opening page
497 // does not have extension privileges, we break the script connection, so
498 // there is no privilege leak.
499 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, WindowOpenNoPrivileges) {
500 ASSERT_TRUE(LoadExtension(
501 test_data_dir_.AppendASCII("uitest").AppendASCII("window_open")));
503 ui_test_utils::NavigateToURL(browser(), GURL("about:blank"));
504 WebContents* newtab = NULL;
505 ASSERT_NO_FATAL_FAILURE(
506 OpenWindow(browser()->tab_strip_model()->GetActiveWebContents(),
507 GURL(std::string(extensions::kExtensionScheme) +
508 url::kStandardSchemeSeparator +
509 last_loaded_extension_id() + "/newtab.html"),
510 false,
511 &newtab));
513 // Extension API should succeed.
514 bool result = false;
515 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(newtab, "testExtensionApi()",
516 &result));
517 EXPECT_TRUE(result);