EME test page application.
[chromium-blink-merge.git] / chrome / browser / extensions / window_open_apitest.cc
blob90bfdae50eddafe23386c2c7b77660470bd5ce3d
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_test_message_listener.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/browser_finder.h"
14 #include "chrome/browser/ui/browser_iterator.h"
15 #include "chrome/browser/ui/panels/panel_manager.h"
16 #include "chrome/browser/ui/tabs/tab_strip_model.h"
17 #include "chrome/common/chrome_paths.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/test/base/test_switches.h"
20 #include "chrome/test/base/ui_test_utils.h"
21 #include "content/public/browser/render_process_host.h"
22 #include "content/public/browser/web_contents.h"
23 #include "content/public/common/result_codes.h"
24 #include "content/public/test/browser_test_utils.h"
25 #include "extensions/browser/extension_host.h"
26 #include "extensions/browser/extension_system.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"
34 #if defined(USE_ASH)
35 #include "apps/app_window_registry.h"
36 #endif
38 #if defined(USE_ASH) && defined(OS_CHROMEOS)
39 // TODO(stevenjb): Figure out the correct behavior for Ash + Win
40 #define USE_ASH_PANELS
41 #endif
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>(
61 apps::AppWindowRegistry::Get(browser->profile())->app_windows().size());
62 #else
63 return PanelManager::GetInstance()->num_panels();
64 #endif
67 bool WaitForTabsAndPopups(Browser* browser,
68 int num_tabs,
69 int num_popups,
70 int num_panels) {
71 SCOPED_TRACE(
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.
75 ++num_tabs;
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)
85 break;
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()) {
98 if (*iter == browser)
99 continue;
101 EXPECT_TRUE((*iter)->is_type_popup());
102 ++num_popups_seen;
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());
125 else
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
177 #if defined(OS_WIN)
178 #define MAYBE_PopupBlockingExtension DISABLED_PopupBlockingExtension
179 #else
180 #define MAYBE_PopupBlockingExtension PopupBlockingExtension
181 #endif
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
203 // web extent.
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/");
211 GURL open_tab =
212 test_server()->GetURL(popup_app_contents_path + "open_tab.html")
213 .ReplaceComponents(replace_host);
214 GURL open_popup =
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,
220 false));
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
256 #else
257 #define MAYBE_WindowOpenPanel WindowOpenPanel
258 #endif
259 IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest, MAYBE_WindowOpenPanel) {
260 ASSERT_TRUE(RunExtensionTest("window_open/panel")) << message_;
263 #if defined(USE_ASH_PANELS) || defined(OS_LINUX)
264 // On Ash, this currently fails because we're currently opening new panel
265 // windows as popup windows instead.
266 // We're also failing on Linux-aura due to the panel is not opened in the
267 // right origin.
268 #define MAYBE_WindowOpenPanelDetached DISABLED_WindowOpenPanelDetached
269 #else
270 #define MAYBE_WindowOpenPanelDetached WindowOpenPanelDetached
271 #endif
272 IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest, MAYBE_WindowOpenPanelDetached) {
273 ASSERT_TRUE(RunExtensionTest("window_open/panel_detached")) << message_;
276 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
277 // TODO(erg): Bring up ash http://crbug.com/300084
278 #define MAYBE_CloseNonExtensionPanelsOnUninstall \
279 DISABLED_CloseNonExtensionPanelsOnUninstall
280 #else
281 #define MAYBE_CloseNonExtensionPanelsOnUninstall \
282 CloseNonExtensionPanelsOnUninstall
283 #endif
284 IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest,
285 MAYBE_CloseNonExtensionPanelsOnUninstall) {
286 #if defined(OS_WIN) && defined(USE_ASH)
287 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
288 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
289 return;
290 #endif
292 #if defined(USE_ASH_PANELS)
293 // On Ash, new panel windows open as popup windows instead.
294 int num_popups, num_panels;
295 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnablePanels)) {
296 num_popups = 2;
297 num_panels = 2;
298 } else {
299 num_popups = 4;
300 num_panels = 0;
302 #else
303 int num_popups = 2;
304 int num_panels = 2;
305 #endif
306 ASSERT_TRUE(StartEmbeddedTestServer());
308 // Setup listeners to wait on strings we expect the extension pages to send.
309 std::vector<std::string> test_strings;
310 test_strings.push_back("content_tab");
311 if (num_panels)
312 test_strings.push_back("content_panel");
313 test_strings.push_back("content_popup");
315 ScopedVector<ExtensionTestMessageListener> listeners;
316 for (size_t i = 0; i < test_strings.size(); ++i) {
317 listeners.push_back(
318 new ExtensionTestMessageListener(test_strings[i], false));
321 const extensions::Extension* extension = LoadExtension(
322 test_data_dir_.AppendASCII("window_open").AppendASCII(
323 "close_panels_on_uninstall"));
324 ASSERT_TRUE(extension);
326 // Two tabs. One in extension domain and one in non-extension domain.
327 // Two popups - one in extension domain and one in non-extension domain.
328 // Two panels - one in extension domain and one in non-extension domain.
329 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 2, num_popups, num_panels));
331 // Wait on test messages to make sure the pages loaded.
332 for (size_t i = 0; i < listeners.size(); ++i)
333 ASSERT_TRUE(listeners[i]->WaitUntilSatisfied());
335 UninstallExtension(extension->id());
337 // Wait for the tabs and popups in non-extension domain to stay open.
338 // Expect everything else, including panels, to close.
339 num_popups -= 1;
340 #if defined(USE_ASH_PANELS)
341 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnablePanels)) {
342 // On Ash, new panel windows open as popup windows instead, so there are 2
343 // extension domain popups that will close (instead of 1 popup on non-Ash).
344 num_popups -= 1;
346 #endif
347 #if defined(USE_ASH)
348 #if !defined(OS_WIN)
349 // On linux ash we close all popup applications when closing its extension.
350 num_popups = 0;
351 #endif
352 #endif
353 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 1, num_popups, 0));
356 // This test isn't applicable on Chrome OS, which automatically reloads crashed
357 // pages.
358 #if !defined(OS_CHROMEOS)
359 IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest, ClosePanelsOnExtensionCrash) {
360 #if defined(USE_ASH_PANELS)
361 // On Ash, new panel windows open as popup windows instead.
362 int num_popups = 4;
363 int num_panels = 0;
364 #else
365 int num_popups = 2;
366 int num_panels = 2;
367 #endif
368 ASSERT_TRUE(StartEmbeddedTestServer());
370 // Setup listeners to wait on strings we expect the extension pages to send.
371 std::vector<std::string> test_strings;
372 test_strings.push_back("content_tab");
373 if (num_panels)
374 test_strings.push_back("content_panel");
375 test_strings.push_back("content_popup");
377 ScopedVector<ExtensionTestMessageListener> listeners;
378 for (size_t i = 0; i < test_strings.size(); ++i) {
379 listeners.push_back(
380 new ExtensionTestMessageListener(test_strings[i], false));
383 const extensions::Extension* extension = LoadExtension(
384 test_data_dir_.AppendASCII("window_open").AppendASCII(
385 "close_panels_on_uninstall"));
386 ASSERT_TRUE(extension);
388 // Two tabs. One in extension domain and one in non-extension domain.
389 // Two popups - one in extension domain and one in non-extension domain.
390 // Two panels - one in extension domain and one in non-extension domain.
391 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 2, num_popups, num_panels));
393 // Wait on test messages to make sure the pages loaded.
394 for (size_t i = 0; i < listeners.size(); ++i)
395 ASSERT_TRUE(listeners[i]->WaitUntilSatisfied());
397 // Crash the extension.
398 extensions::ExtensionHost* extension_host =
399 extensions::ExtensionSystem::Get(browser()->profile())->
400 process_manager()->GetBackgroundHostForExtension(extension->id());
401 ASSERT_TRUE(extension_host);
402 base::KillProcess(extension_host->render_process_host()->GetHandle(),
403 content::RESULT_CODE_KILLED, false);
404 WaitForExtensionCrash(extension->id());
406 // Only expect panels to close. The rest stay open to show a sad-tab.
407 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 2, num_popups, 0));
409 #endif // !defined(OS_CHROMEOS)
411 #if defined(USE_ASH_PANELS)
412 // This test is not applicable on Ash. The modified window.open behavior only
413 // applies to non-Ash panel windows.
414 #define MAYBE_WindowOpenFromPanel DISABLED_WindowOpenFromPanel
415 #else
416 #define MAYBE_WindowOpenFromPanel WindowOpenFromPanel
417 #endif
418 IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest, MAYBE_WindowOpenFromPanel) {
419 ASSERT_TRUE(StartEmbeddedTestServer());
421 // Load the extension that will open a panel which then calls window.open.
422 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("window_open").
423 AppendASCII("panel_window_open")));
425 // Expect one panel (opened by extension) and one tab (from the panel calling
426 // window.open). Panels modify the WindowOpenDisposition in window.open
427 // to always open in a tab.
428 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 1, 0, 1));
431 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, DISABLED_WindowOpener) {
432 ASSERT_TRUE(RunExtensionTest("window_open/opener")) << message_;
435 #if defined(OS_MACOSX)
436 // Extension popup windows are incorrectly sized on OSX, crbug.com/225601
437 #define MAYBE_WindowOpenSized DISABLED_WindowOpenSized
438 #else
439 #define MAYBE_WindowOpenSized WindowOpenSized
440 #endif
441 // Ensure that the width and height properties of a window opened with
442 // chrome.windows.create match the creation parameters. See crbug.com/173831.
443 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, MAYBE_WindowOpenSized) {
444 ASSERT_TRUE(RunExtensionTest("window_open/window_size")) << message_;
445 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 0, 1, 0));
448 // Tests that an extension page can call window.open to an extension URL and
449 // the new window has extension privileges.
450 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, WindowOpenExtension) {
451 ASSERT_TRUE(LoadExtension(
452 test_data_dir_.AppendASCII("uitest").AppendASCII("window_open")));
454 GURL start_url(std::string("chrome-extension://") +
455 last_loaded_extension_id() + "/test.html");
456 ui_test_utils::NavigateToURL(browser(), start_url);
457 WebContents* newtab = NULL;
458 ASSERT_NO_FATAL_FAILURE(
459 OpenWindow(browser()->tab_strip_model()->GetActiveWebContents(),
460 start_url.Resolve("newtab.html"), true, &newtab));
462 bool result = false;
463 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(newtab, "testExtensionApi()",
464 &result));
465 EXPECT_TRUE(result);
468 // Tests that if an extension page calls window.open to an invalid extension
469 // URL, the browser doesn't crash.
470 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, WindowOpenInvalidExtension) {
471 ASSERT_TRUE(LoadExtension(
472 test_data_dir_.AppendASCII("uitest").AppendASCII("window_open")));
474 GURL start_url(std::string("chrome-extension://") +
475 last_loaded_extension_id() + "/test.html");
476 ui_test_utils::NavigateToURL(browser(), start_url);
477 ASSERT_NO_FATAL_FAILURE(
478 OpenWindow(browser()->tab_strip_model()->GetActiveWebContents(),
479 GURL("chrome-extension://thisissurelynotavalidextensionid/newtab.html"),
480 false, NULL));
482 // If we got to this point, we didn't crash, so we're good.
485 // Tests that calling window.open from the newtab page to an extension URL
486 // gives the new window extension privileges - even though the opening page
487 // does not have extension privileges, we break the script connection, so
488 // there is no privilege leak.
489 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, WindowOpenNoPrivileges) {
490 ASSERT_TRUE(LoadExtension(
491 test_data_dir_.AppendASCII("uitest").AppendASCII("window_open")));
493 ui_test_utils::NavigateToURL(browser(), GURL("about:blank"));
494 WebContents* newtab = NULL;
495 ASSERT_NO_FATAL_FAILURE(
496 OpenWindow(browser()->tab_strip_model()->GetActiveWebContents(),
497 GURL(std::string("chrome-extension://") + last_loaded_extension_id() +
498 "/newtab.html"), false, &newtab));
500 // Extension API should succeed.
501 bool result = false;
502 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(newtab, "testExtensionApi()",
503 &result));
504 EXPECT_TRUE(result);