blob: 5cc06773b1c639366333688dfc8f9a0fc0001465 [file] [log] [blame]
[email protected]35abc332012-02-24 23:48:381// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]32b7c492010-09-01 04:15:002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
avia2f4804a2015-12-24 23:11:135#include <stddef.h>
6
[email protected]32b7c492010-09-01 04:15:007#include "base/command_line.h"
[email protected]f9e82d92011-10-29 00:50:458#include "base/memory/scoped_vector.h"
[email protected]7c6ef352013-09-26 07:23:149#include "base/path_service.h"
[email protected]46acbf12013-06-10 18:43:4210#include "base/strings/stringprintf.h"
avia2f4804a2015-12-24 23:11:1311#include "build/build_config.h"
[email protected]32b7c492010-09-01 04:15:0012#include "chrome/browser/extensions/extension_apitest.h"
[email protected]01996a592013-09-13 06:55:0413#include "chrome/browser/profiles/profile.h"
[email protected]2ad4a902010-11-17 06:05:1314#include "chrome/browser/ui/browser.h"
[email protected]d8748142012-05-16 21:13:4315#include "chrome/browser/ui/browser_finder.h"
scottmg8abbff832016-01-28 22:57:3716#include "chrome/browser/ui/browser_list.h"
[email protected]f6ffa7d2012-08-17 17:27:0817#include "chrome/browser/ui/panels/panel_manager.h"
[email protected]e0448872013-01-11 19:35:0218#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]7c6ef352013-09-26 07:23:1419#include "chrome/common/chrome_paths.h"
[email protected]32b7c492010-09-01 04:15:0020#include "chrome/common/chrome_switches.h"
[email protected]b65d4bdcc2013-07-31 00:37:3321#include "chrome/test/base/test_switches.h"
[email protected]af44e7fb2011-07-29 18:32:3222#include "chrome/test/base/ui_test_utils.h"
[email protected]1dd66b72012-09-19 20:41:4823#include "content/public/browser/render_process_host.h"
[email protected]fad73672012-06-15 23:26:0624#include "content/public/browser/web_contents.h"
[email protected]1dd66b72012-09-19 20:41:4825#include "content/public/common/result_codes.h"
[email protected]99bee642014-05-31 22:36:4326#include "content/public/common/url_constants.h"
[email protected]7d478cb2012-07-24 17:19:4227#include "content/public/test/browser_test_utils.h"
[email protected]22401dc2014-03-21 01:38:5728#include "extensions/browser/extension_host.h"
[email protected]98b6d942013-11-10 00:34:0729#include "extensions/browser/process_manager.h"
[email protected]99bee642014-05-31 22:36:4330#include "extensions/common/constants.h"
[email protected]e4452d32013-11-15 23:07:4131#include "extensions/common/extension.h"
[email protected]c8d02992013-07-31 22:16:5132#include "extensions/common/switches.h"
lfg910f2f92014-09-19 05:31:0933#include "extensions/test/extension_test_message_listener.h"
yoze8dc2f12014-09-09 23:16:3234#include "extensions/test/result_catcher.h"
[email protected]f2cb3cf2013-03-21 01:40:5335#include "net/dns/mock_host_resolver.h"
[email protected]7c6ef352013-09-26 07:23:1436#include "net/test/embedded_test_server/embedded_test_server.h"
[email protected]bc44f4142013-02-12 06:15:5637#include "testing/gtest/include/gtest/gtest.h"
[email protected]32b7c492010-09-01 04:15:0038
[email protected]f1c102b2013-02-15 07:44:1239#if defined(USE_ASH)
hashimotoad3c6872014-08-29 09:46:5740#include "extensions/browser/app_window/app_window_registry.h"
[email protected]f1c102b2013-02-15 07:44:1241#endif
42
[email protected]15119302014-05-20 19:23:5843#if defined(USE_ASH) && defined(OS_CHROMEOS)
[email protected]f1c102b2013-02-15 07:44:1244// TODO(stevenjb): Figure out the correct behavior for Ash + Win
45#define USE_ASH_PANELS
46#endif
47
[email protected]e5d549d2011-12-28 01:29:2048using content::OpenURLParams;
49using content::Referrer;
[email protected]fad73672012-06-15 23:26:0650using content::WebContents;
[email protected]e5d549d2011-12-28 01:29:2051
robf8cd8ce2015-02-21 12:18:0452// The test uses the chrome.browserAction.openPopup API, which requires that the
53// window can automatically be activated.
54// See comments at BrowserActionInteractiveTest::ShouldRunPopupTest
johnme7e30b512015-04-16 14:57:3555// Fails flakily on all platforms. https://crbug.com/477691
56IN_PROC_BROWSER_TEST_F(ExtensionApiTest, DISABLED_WindowOpen) {
yoze8dc2f12014-09-09 23:16:3257 extensions::ResultCatcher catcher;
[email protected]32b7c492010-09-01 04:15:0058 ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_
59 .AppendASCII("window_open").AppendASCII("spanning")));
60 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
61}
[email protected]4026ce1e2010-09-14 19:35:0462
[email protected]f1c102b2013-02-15 07:44:1263int GetPanelCount(Browser* browser) {
64#if defined(USE_ASH_PANELS)
hashimotoad3c6872014-08-29 09:46:5765 return static_cast<int>(extensions::AppWindowRegistry::Get(
66 browser->profile())->app_windows().size());
[email protected]f1c102b2013-02-15 07:44:1267#else
68 return PanelManager::GetInstance()->num_panels();
69#endif
70}
71
72bool WaitForTabsAndPopups(Browser* browser,
[email protected]f9e82d92011-10-29 00:50:4573 int num_tabs,
74 int num_popups,
75 int num_panels) {
[email protected]58b349b2011-11-23 02:07:1576 SCOPED_TRACE(
[email protected]7d3cbc92013-03-18 22:33:0477 base::StringPrintf("WaitForTabsAndPopups tabs:%d, popups:%d, panels:%d",
78 num_tabs, num_popups, num_panels));
[email protected]4026ce1e2010-09-14 19:35:0479 // We start with one tab and one browser already open.
80 ++num_tabs;
[email protected]f6ffa7d2012-08-17 17:27:0881 size_t num_browsers = static_cast<size_t>(num_popups) + 1;
[email protected]4026ce1e2010-09-14 19:35:0482
[email protected]f1c102b2013-02-15 07:44:1283 const base::TimeDelta kWaitTime = base::TimeDelta::FromSeconds(10);
[email protected]0a5bdc6522011-06-08 19:04:4984 base::TimeTicks end_time = base::TimeTicks::Now() + kWaitTime;
85 while (base::TimeTicks::Now() < end_time) {
[email protected]c987a242013-02-28 01:17:4186 if (chrome::GetBrowserCount(browser->profile(),
87 browser->host_desktop_type()) == num_browsers &&
[email protected]e0448872013-01-11 19:35:0288 browser->tab_strip_model()->count() == num_tabs &&
[email protected]f1c102b2013-02-15 07:44:1289 GetPanelCount(browser) == num_panels)
[email protected]0a5bdc6522011-06-08 19:04:4990 break;
91
[email protected]b8deecd2012-07-30 21:09:4492 content::RunAllPendingInMessageLoop();
[email protected]0a5bdc6522011-06-08 19:04:4993 }
94
[email protected]c987a242013-02-28 01:17:4195 EXPECT_EQ(num_browsers,
96 chrome::GetBrowserCount(browser->profile(),
97 browser->host_desktop_type()));
[email protected]e0448872013-01-11 19:35:0298 EXPECT_EQ(num_tabs, browser->tab_strip_model()->count());
[email protected]f1c102b2013-02-15 07:44:1299 EXPECT_EQ(num_panels, GetPanelCount(browser));
[email protected]0a5bdc6522011-06-08 19:04:49100
[email protected]f9e82d92011-10-29 00:50:45101 int num_popups_seen = 0;
scottmg8abbff832016-01-28 22:57:37102 for (auto* b : *BrowserList::GetInstance()) {
103 if (b == browser)
[email protected]4026ce1e2010-09-14 19:35:04104 continue;
[email protected]4026ce1e2010-09-14 19:35:04105
scottmg8abbff832016-01-28 22:57:37106 EXPECT_TRUE(b->is_type_popup());
[email protected]f6ffa7d2012-08-17 17:27:08107 ++num_popups_seen;
[email protected]4026ce1e2010-09-14 19:35:04108 }
[email protected]f9e82d92011-10-29 00:50:45109 EXPECT_EQ(num_popups, num_popups_seen);
[email protected]f1c102b2013-02-15 07:44:12110
[email protected]c987a242013-02-28 01:17:41111 return ((num_browsers ==
112 chrome::GetBrowserCount(browser->profile(),
113 browser->host_desktop_type())) &&
[email protected]f1c102b2013-02-15 07:44:12114 (num_tabs == browser->tab_strip_model()->count()) &&
115 (num_panels == GetPanelCount(browser)) &&
116 (num_popups == num_popups_seen));
[email protected]4026ce1e2010-09-14 19:35:04117}
118
[email protected]f112b0f2011-05-26 01:53:52119IN_PROC_BROWSER_TEST_F(ExtensionApiTest, BrowserIsApp) {
120 host_resolver()->AddRule("a.com", "127.0.0.1");
[email protected]c1dffe82013-06-26 20:59:05121 ASSERT_TRUE(StartEmbeddedTestServer());
[email protected]f112b0f2011-05-26 01:53:52122 ASSERT_TRUE(LoadExtension(
123 test_data_dir_.AppendASCII("window_open").AppendASCII("browser_is_app")));
124
[email protected]f1c102b2013-02-15 07:44:12125 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 0, 2, 0));
[email protected]f112b0f2011-05-26 01:53:52126
scottmg8abbff832016-01-28 22:57:37127 for (auto* b : *BrowserList::GetInstance()) {
128 if (b == browser())
129 ASSERT_FALSE(b->is_app());
[email protected]f112b0f2011-05-26 01:53:52130 else
scottmg8abbff832016-01-28 22:57:37131 ASSERT_TRUE(b->is_app());
[email protected]f112b0f2011-05-26 01:53:52132 }
133}
134
[email protected]0a5bdc6522011-06-08 19:04:49135IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowOpenPopupDefault) {
[email protected]c1dffe82013-06-26 20:59:05136 ASSERT_TRUE(StartEmbeddedTestServer());
[email protected]0a5bdc6522011-06-08 19:04:49137 ASSERT_TRUE(LoadExtension(
138 test_data_dir_.AppendASCII("window_open").AppendASCII("popup")));
139
140 const int num_tabs = 1;
141 const int num_popups = 0;
[email protected]f1c102b2013-02-15 07:44:12142 EXPECT_TRUE(WaitForTabsAndPopups(browser(), num_tabs, num_popups, 0));
[email protected]0a5bdc6522011-06-08 19:04:49143}
144
[email protected]7c6ef352013-09-26 07:23:14145IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowOpenPopupIframe) {
146 ASSERT_TRUE(StartEmbeddedTestServer());
147 base::FilePath test_data_dir;
148 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir);
149 embedded_test_server()->ServeFilesFromDirectory(test_data_dir);
150 ASSERT_TRUE(LoadExtension(
151 test_data_dir_.AppendASCII("window_open").AppendASCII("popup_iframe")));
152
jochenc5cfa642014-09-02 15:30:03153 const int num_tabs = 1;
154 const int num_popups = 0;
[email protected]7c6ef352013-09-26 07:23:14155 EXPECT_TRUE(WaitForTabsAndPopups(browser(), num_tabs, num_popups, 0));
156}
157
[email protected]0a5bdc6522011-06-08 19:04:49158IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowOpenPopupLarge) {
[email protected]c1dffe82013-06-26 20:59:05159 ASSERT_TRUE(StartEmbeddedTestServer());
[email protected]0a5bdc6522011-06-08 19:04:49160 ASSERT_TRUE(LoadExtension(
161 test_data_dir_.AppendASCII("window_open").AppendASCII("popup_large")));
162
[email protected]0a5bdc6522011-06-08 19:04:49163 // On other systems this should open a new popup window.
164 const int num_tabs = 0;
165 const int num_popups = 1;
[email protected]f1c102b2013-02-15 07:44:12166 EXPECT_TRUE(WaitForTabsAndPopups(browser(), num_tabs, num_popups, 0));
[email protected]0a5bdc6522011-06-08 19:04:49167}
168
169IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowOpenPopupSmall) {
[email protected]c1dffe82013-06-26 20:59:05170 ASSERT_TRUE(StartEmbeddedTestServer());
[email protected]0a5bdc6522011-06-08 19:04:49171 ASSERT_TRUE(LoadExtension(
172 test_data_dir_.AppendASCII("window_open").AppendASCII("popup_small")));
173
174 // On ChromeOS this should open a new panel (acts like a new popup window).
175 // On other systems this should open a new popup window.
176 const int num_tabs = 0;
177 const int num_popups = 1;
[email protected]f1c102b2013-02-15 07:44:12178 EXPECT_TRUE(WaitForTabsAndPopups(browser(), num_tabs, num_popups, 0));
[email protected]0a5bdc6522011-06-08 19:04:49179}
180
[email protected]73fa1fce2013-02-21 20:10:05181// Disabled on Windows. Often times out or fails: crbug.com/177530
182#if defined(OS_WIN)
183#define MAYBE_PopupBlockingExtension DISABLED_PopupBlockingExtension
184#else
185#define MAYBE_PopupBlockingExtension PopupBlockingExtension
186#endif
187IN_PROC_BROWSER_TEST_F(ExtensionApiTest, MAYBE_PopupBlockingExtension) {
[email protected]4026ce1e2010-09-14 19:35:04188 host_resolver()->AddRule("*", "127.0.0.1");
[email protected]c1dffe82013-06-26 20:59:05189 ASSERT_TRUE(StartEmbeddedTestServer());
[email protected]4026ce1e2010-09-14 19:35:04190
191 ASSERT_TRUE(LoadExtension(
192 test_data_dir_.AppendASCII("window_open").AppendASCII("popup_blocking")
193 .AppendASCII("extension")));
194
[email protected]f1c102b2013-02-15 07:44:12195 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 5, 3, 0));
[email protected]4026ce1e2010-09-14 19:35:04196}
197
198IN_PROC_BROWSER_TEST_F(ExtensionApiTest, PopupBlockingHostedApp) {
199 host_resolver()->AddRule("*", "127.0.0.1");
svaldeza01f7d92015-11-18 17:47:56200 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]4026ce1e2010-09-14 19:35:04201
202 ASSERT_TRUE(LoadExtension(
203 test_data_dir_.AppendASCII("window_open").AppendASCII("popup_blocking")
204 .AppendASCII("hosted_app")));
205
[email protected]fe3048872010-10-18 14:58:59206 // The app being tested owns the domain a.com . The test URLs we navigate
207 // to below must be within that domain, so that they fall within the app's
208 // web extent.
209 GURL::Replacements replace_host;
mgiuca77752c32015-02-05 07:31:18210 replace_host.SetHostStr("a.com");
[email protected]fe3048872010-10-18 14:58:59211
212 const std::string popup_app_contents_path(
svaldeza01f7d92015-11-18 17:47:56213 "/extensions/api_test/window_open/popup_blocking/hosted_app/");
[email protected]fe3048872010-10-18 14:58:59214
svaldeza01f7d92015-11-18 17:47:56215 GURL open_tab = embedded_test_server()
216 ->GetURL(popup_app_contents_path + "open_tab.html")
217 .ReplaceComponents(replace_host);
218 GURL open_popup = embedded_test_server()
219 ->GetURL(popup_app_contents_path + "open_popup.html")
220 .ReplaceComponents(replace_host);
[email protected]fe3048872010-10-18 14:58:59221
[email protected]e5d549d2011-12-28 01:29:20222 browser()->OpenURL(OpenURLParams(
Sylvain Defresnec6ccc77d2014-09-19 10:19:35223 open_tab, Referrer(), NEW_FOREGROUND_TAB, ui::PAGE_TRANSITION_TYPED,
[email protected]e5d549d2011-12-28 01:29:20224 false));
225 browser()->OpenURL(OpenURLParams(
226 open_popup, Referrer(), NEW_FOREGROUND_TAB,
Sylvain Defresnec6ccc77d2014-09-19 10:19:35227 ui::PAGE_TRANSITION_TYPED, false));
[email protected]4026ce1e2010-09-14 19:35:04228
[email protected]f1c102b2013-02-15 07:44:12229 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 3, 1, 0));
[email protected]4026ce1e2010-09-14 19:35:04230}
[email protected]d4db6c702011-03-28 21:49:14231
[email protected]e7f90562011-05-23 21:38:24232IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowArgumentsOverflow) {
233 ASSERT_TRUE(RunExtensionTest("window_open/argument_overflow")) << message_;
234}
235
[email protected]d5c14062011-10-27 00:02:13236class WindowOpenPanelDisabledTest : public ExtensionApiTest {
avi3ef9ec9e2014-12-22 22:50:17237 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]08aa0c32011-04-11 22:18:37238 ExtensionApiTest::SetUpCommandLine(command_line);
[email protected]7e7a28092011-12-09 22:24:55239 // TODO(jennb): Re-enable when panels are enabled by default.
240 // command_line->AppendSwitch(switches::kDisablePanels);
[email protected]08aa0c32011-04-11 22:18:37241 }
242};
243
[email protected]7e7a28092011-12-09 22:24:55244IN_PROC_BROWSER_TEST_F(WindowOpenPanelDisabledTest,
245 DISABLED_WindowOpenPanelNotEnabled) {
[email protected]d5c14062011-10-27 00:02:13246 ASSERT_TRUE(RunExtensionTest("window_open/panel_not_enabled")) << message_;
247}
248
[email protected]af6b54512011-12-14 06:11:21249class WindowOpenPanelTest : public ExtensionApiTest {
avi3ef9ec9e2014-12-22 22:50:17250 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]af6b54512011-12-14 06:11:21251 ExtensionApiTest::SetUpCommandLine(command_line);
252 command_line->AppendSwitch(switches::kEnablePanels);
253 }
254};
255
[email protected]f1c102b2013-02-15 07:44:12256#if defined(USE_ASH_PANELS)
[email protected]87d722a2012-07-12 15:38:33257// On Ash, this currently fails because we're currently opening new panel
[email protected]d101b0c2012-03-16 00:30:57258// windows as popup windows instead.
[email protected]e00ccc92012-11-01 17:32:30259#define MAYBE_WindowOpenPanel DISABLED_WindowOpenPanel
[email protected]d101b0c2012-03-16 00:30:57260#else
261#define MAYBE_WindowOpenPanel WindowOpenPanel
262#endif
263IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest, MAYBE_WindowOpenPanel) {
[email protected]08aa0c32011-04-11 22:18:37264 ASSERT_TRUE(RunExtensionTest("window_open/panel")) << message_;
265}
[email protected]54edcea2011-07-27 01:56:38266
lukasza43df8592015-12-18 17:40:48267// Test verifying that panel-subframe can use window.open to find
268// background-subframe (see the picture below). In other words, the test
269// verifies that the everything on the picture below stays in the same
270// BrowsingInstance.
271//
272// +-extension background page---+ +-panel-----------------------------+
273// | | | |
274// | chrome.windows.create( | | +-panel-subframe----------------+ |
275// | 'type':'panel') -------------> | | (foo.com) | |
276// | | | | | |
277// | +-background-subframe-+ | | | w = window.open(..., | |
278// | | (foo.com) | | | | "background-subframe-name") | |
279// | | | <--------------/ | |
280// | +---------------------+ | | +-------------------------------+ |
281// | | | |
282// +-----------------------------+ +-----------------------------------+
283//
284// See also crbug.com/568357 for more info / context.
285IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest, BrowsingInstanceTest) {
286 host_resolver()->AddRule("*", "127.0.0.1");
287 ASSERT_TRUE(StartEmbeddedTestServer());
288
289 ASSERT_TRUE(RunExtensionTest("window_open/panel_browsing_instance"))
290 << message_;
291}
292
[email protected]a60837b2014-03-11 03:49:20293#if defined(USE_ASH_PANELS) || defined(OS_LINUX)
[email protected]ca29ed12012-09-06 09:10:50294// On Ash, this currently fails because we're currently opening new panel
295// windows as popup windows instead.
[email protected]a60837b2014-03-11 03:49:20296// We're also failing on Linux-aura due to the panel is not opened in the
297// right origin.
[email protected]cb1849e2013-07-08 21:12:53298#define MAYBE_WindowOpenPanelDetached DISABLED_WindowOpenPanelDetached
299#else
300#define MAYBE_WindowOpenPanelDetached WindowOpenPanelDetached
301#endif
302IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest, MAYBE_WindowOpenPanelDetached) {
[email protected]ca29ed12012-09-06 09:10:50303 ASSERT_TRUE(RunExtensionTest("window_open/panel_detached")) << message_;
304}
305
[email protected]15119302014-05-20 19:23:58306#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
307// TODO(erg): Bring up ash http://crbug.com/300084
308#define MAYBE_CloseNonExtensionPanelsOnUninstall \
309 DISABLED_CloseNonExtensionPanelsOnUninstall
310#else
311#define MAYBE_CloseNonExtensionPanelsOnUninstall \
312 CloseNonExtensionPanelsOnUninstall
313#endif
[email protected]af6b54512011-12-14 06:11:21314IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest,
[email protected]15119302014-05-20 19:23:58315 MAYBE_CloseNonExtensionPanelsOnUninstall) {
[email protected]45c6f6722013-07-23 03:02:19316#if defined(OS_WIN) && defined(USE_ASH)
317 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
avi3ef9ec9e2014-12-22 22:50:17318 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
319 switches::kAshBrowserTests))
[email protected]45c6f6722013-07-23 03:02:19320 return;
321#endif
322
[email protected]f1c102b2013-02-15 07:44:12323#if defined(USE_ASH_PANELS)
[email protected]1dd66b72012-09-19 20:41:48324 // On Ash, new panel windows open as popup windows instead.
[email protected]f1c102b2013-02-15 07:44:12325 int num_popups, num_panels;
avi3ef9ec9e2014-12-22 22:50:17326 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
327 switches::kEnablePanels)) {
[email protected]f1c102b2013-02-15 07:44:12328 num_popups = 2;
329 num_panels = 2;
330 } else {
331 num_popups = 4;
332 num_panels = 0;
333 }
[email protected]1dd66b72012-09-19 20:41:48334#else
335 int num_popups = 2;
336 int num_panels = 2;
337#endif
[email protected]c1dffe82013-06-26 20:59:05338 ASSERT_TRUE(StartEmbeddedTestServer());
[email protected]1dd66b72012-09-19 20:41:48339
340 // Setup listeners to wait on strings we expect the extension pages to send.
341 std::vector<std::string> test_strings;
342 test_strings.push_back("content_tab");
343 if (num_panels)
344 test_strings.push_back("content_panel");
345 test_strings.push_back("content_popup");
346
347 ScopedVector<ExtensionTestMessageListener> listeners;
348 for (size_t i = 0; i < test_strings.size(); ++i) {
349 listeners.push_back(
350 new ExtensionTestMessageListener(test_strings[i], false));
351 }
352
353 const extensions::Extension* extension = LoadExtension(
354 test_data_dir_.AppendASCII("window_open").AppendASCII(
355 "close_panels_on_uninstall"));
356 ASSERT_TRUE(extension);
357
358 // Two tabs. One in extension domain and one in non-extension domain.
359 // Two popups - one in extension domain and one in non-extension domain.
360 // Two panels - one in extension domain and one in non-extension domain.
[email protected]f1c102b2013-02-15 07:44:12361 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 2, num_popups, num_panels));
[email protected]4cdd8572013-02-15 00:33:25362
363 // Wait on test messages to make sure the pages loaded.
364 for (size_t i = 0; i < listeners.size(); ++i)
365 ASSERT_TRUE(listeners[i]->WaitUntilSatisfied());
366
367 UninstallExtension(extension->id());
368
369 // Wait for the tabs and popups in non-extension domain to stay open.
370 // Expect everything else, including panels, to close.
[email protected]4cdd8572013-02-15 00:33:25371 num_popups -= 1;
[email protected]f1c102b2013-02-15 07:44:12372#if defined(USE_ASH_PANELS)
avi3ef9ec9e2014-12-22 22:50:17373 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
374 switches::kEnablePanels)) {
[email protected]f1c102b2013-02-15 07:44:12375 // On Ash, new panel windows open as popup windows instead, so there are 2
376 // extension domain popups that will close (instead of 1 popup on non-Ash).
377 num_popups -= 1;
378 }
[email protected]4cdd8572013-02-15 00:33:25379#endif
[email protected]9111fff2013-06-12 18:01:55380#if defined(USE_ASH)
381#if !defined(OS_WIN)
382 // On linux ash we close all popup applications when closing its extension.
383 num_popups = 0;
384#endif
385#endif
[email protected]f1c102b2013-02-15 07:44:12386 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 1, num_popups, 0));
[email protected]4cdd8572013-02-15 00:33:25387}
388
[email protected]15119302014-05-20 19:23:58389// This test isn't applicable on Chrome OS, which automatically reloads crashed
390// pages.
[email protected]4ad85f42013-07-24 02:31:57391#if !defined(OS_CHROMEOS)
392IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest, ClosePanelsOnExtensionCrash) {
[email protected]f1c102b2013-02-15 07:44:12393#if defined(USE_ASH_PANELS)
[email protected]4cdd8572013-02-15 00:33:25394 // On Ash, new panel windows open as popup windows instead.
395 int num_popups = 4;
396 int num_panels = 0;
397#else
398 int num_popups = 2;
399 int num_panels = 2;
400#endif
[email protected]c1dffe82013-06-26 20:59:05401 ASSERT_TRUE(StartEmbeddedTestServer());
[email protected]4cdd8572013-02-15 00:33:25402
403 // Setup listeners to wait on strings we expect the extension pages to send.
404 std::vector<std::string> test_strings;
405 test_strings.push_back("content_tab");
406 if (num_panels)
407 test_strings.push_back("content_panel");
408 test_strings.push_back("content_popup");
409
410 ScopedVector<ExtensionTestMessageListener> listeners;
411 for (size_t i = 0; i < test_strings.size(); ++i) {
412 listeners.push_back(
413 new ExtensionTestMessageListener(test_strings[i], false));
414 }
415
416 const extensions::Extension* extension = LoadExtension(
417 test_data_dir_.AppendASCII("window_open").AppendASCII(
418 "close_panels_on_uninstall"));
419 ASSERT_TRUE(extension);
420
421 // Two tabs. One in extension domain and one in non-extension domain.
422 // Two popups - one in extension domain and one in non-extension domain.
423 // Two panels - one in extension domain and one in non-extension domain.
[email protected]f1c102b2013-02-15 07:44:12424 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 2, num_popups, num_panels));
[email protected]1dd66b72012-09-19 20:41:48425
426 // Wait on test messages to make sure the pages loaded.
427 for (size_t i = 0; i < listeners.size(); ++i)
428 ASSERT_TRUE(listeners[i]->WaitUntilSatisfied());
429
430 // Crash the extension.
431 extensions::ExtensionHost* extension_host =
reillyg0ea3fa902014-10-28 15:30:23432 extensions::ProcessManager::Get(browser()->profile())
433 ->GetBackgroundHostForExtension(extension->id());
[email protected]1dd66b72012-09-19 20:41:48434 ASSERT_TRUE(extension_host);
jaekyun37e572a32014-12-04 23:33:35435 extension_host->render_process_host()->Shutdown(content::RESULT_CODE_KILLED,
436 false);
[email protected]1dd66b72012-09-19 20:41:48437 WaitForExtensionCrash(extension->id());
438
439 // Only expect panels to close. The rest stay open to show a sad-tab.
[email protected]f1c102b2013-02-15 07:44:12440 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 2, num_popups, 0));
[email protected]1dd66b72012-09-19 20:41:48441}
[email protected]4ad85f42013-07-24 02:31:57442#endif // !defined(OS_CHROMEOS)
[email protected]1dd66b72012-09-19 20:41:48443
[email protected]f1c102b2013-02-15 07:44:12444#if defined(USE_ASH_PANELS)
445// This test is not applicable on Ash. The modified window.open behavior only
446// applies to non-Ash panel windows.
[email protected]99f4fdb92012-08-30 23:13:51447#define MAYBE_WindowOpenFromPanel DISABLED_WindowOpenFromPanel
448#else
449#define MAYBE_WindowOpenFromPanel WindowOpenFromPanel
450#endif
451IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest, MAYBE_WindowOpenFromPanel) {
[email protected]c1dffe82013-06-26 20:59:05452 ASSERT_TRUE(StartEmbeddedTestServer());
[email protected]6798c332012-08-29 02:16:51453
[email protected]99f4fdb92012-08-30 23:13:51454 // Load the extension that will open a panel which then calls window.open.
[email protected]6798c332012-08-29 02:16:51455 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("window_open").
456 AppendASCII("panel_window_open")));
[email protected]99f4fdb92012-08-30 23:13:51457
458 // Expect one panel (opened by extension) and one tab (from the panel calling
459 // window.open). Panels modify the WindowOpenDisposition in window.open
460 // to always open in a tab.
[email protected]f1c102b2013-02-15 07:44:12461 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 1, 0, 1));
[email protected]6798c332012-08-29 02:16:51462}
463
[email protected]24c6bcfc2012-02-29 21:06:36464IN_PROC_BROWSER_TEST_F(ExtensionApiTest, DISABLED_WindowOpener) {
[email protected]54edcea2011-07-27 01:56:38465 ASSERT_TRUE(RunExtensionTest("window_open/opener")) << message_;
466}
[email protected]fad73672012-06-15 23:26:06467
[email protected]fa7c1602013-04-03 23:00:20468#if defined(OS_MACOSX)
469// Extension popup windows are incorrectly sized on OSX, crbug.com/225601
470#define MAYBE_WindowOpenSized DISABLED_WindowOpenSized
471#else
472#define MAYBE_WindowOpenSized WindowOpenSized
473#endif
474// Ensure that the width and height properties of a window opened with
475// chrome.windows.create match the creation parameters. See crbug.com/173831.
476IN_PROC_BROWSER_TEST_F(ExtensionApiTest, MAYBE_WindowOpenSized) {
477 ASSERT_TRUE(RunExtensionTest("window_open/window_size")) << message_;
478 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 0, 1, 0));
479}
480
[email protected]fad73672012-06-15 23:26:06481// Tests that an extension page can call window.open to an extension URL and
482// the new window has extension privileges.
483IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, WindowOpenExtension) {
484 ASSERT_TRUE(LoadExtension(
485 test_data_dir_.AppendASCII("uitest").AppendASCII("window_open")));
486
[email protected]99bee642014-05-31 22:36:43487 GURL start_url(std::string(extensions::kExtensionScheme) +
[email protected]fb4fe0952014-06-05 09:44:24488 url::kStandardSchemeSeparator +
[email protected]99bee642014-05-31 22:36:43489 last_loaded_extension_id() + "/test.html");
[email protected]fad73672012-06-15 23:26:06490 ui_test_utils::NavigateToURL(browser(), start_url);
[email protected]f3e73f62012-10-20 05:35:16491 WebContents* newtab = NULL;
[email protected]e0448872013-01-11 19:35:02492 ASSERT_NO_FATAL_FAILURE(
493 OpenWindow(browser()->tab_strip_model()->GetActiveWebContents(),
494 start_url.Resolve("newtab.html"), true, &newtab));
[email protected]fad73672012-06-15 23:26:06495
496 bool result = false;
[email protected]b6987e02013-01-04 18:30:43497 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(newtab, "testExtensionApi()",
498 &result));
[email protected]fad73672012-06-15 23:26:06499 EXPECT_TRUE(result);
500}
501
502// Tests that if an extension page calls window.open to an invalid extension
503// URL, the browser doesn't crash.
504IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, WindowOpenInvalidExtension) {
505 ASSERT_TRUE(LoadExtension(
506 test_data_dir_.AppendASCII("uitest").AppendASCII("window_open")));
507
[email protected]99bee642014-05-31 22:36:43508 GURL start_url(std::string(extensions::kExtensionScheme) +
[email protected]fb4fe0952014-06-05 09:44:24509 url::kStandardSchemeSeparator +
[email protected]99bee642014-05-31 22:36:43510 last_loaded_extension_id() + "/test.html");
[email protected]fad73672012-06-15 23:26:06511 ui_test_utils::NavigateToURL(browser(), start_url);
[email protected]e0448872013-01-11 19:35:02512 ASSERT_NO_FATAL_FAILURE(
513 OpenWindow(browser()->tab_strip_model()->GetActiveWebContents(),
[email protected]fad73672012-06-15 23:26:06514 GURL("chrome-extension://thisissurelynotavalidextensionid/newtab.html"),
515 false, NULL));
516
517 // If we got to this point, we didn't crash, so we're good.
518}
519
520// Tests that calling window.open from the newtab page to an extension URL
521// gives the new window extension privileges - even though the opening page
522// does not have extension privileges, we break the script connection, so
523// there is no privilege leak.
524IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, WindowOpenNoPrivileges) {
525 ASSERT_TRUE(LoadExtension(
526 test_data_dir_.AppendASCII("uitest").AppendASCII("window_open")));
527
528 ui_test_utils::NavigateToURL(browser(), GURL("about:blank"));
[email protected]f3e73f62012-10-20 05:35:16529 WebContents* newtab = NULL;
[email protected]e0448872013-01-11 19:35:02530 ASSERT_NO_FATAL_FAILURE(
531 OpenWindow(browser()->tab_strip_model()->GetActiveWebContents(),
[email protected]99bee642014-05-31 22:36:43532 GURL(std::string(extensions::kExtensionScheme) +
[email protected]fb4fe0952014-06-05 09:44:24533 url::kStandardSchemeSeparator +
[email protected]99bee642014-05-31 22:36:43534 last_loaded_extension_id() + "/newtab.html"),
535 false,
536 &newtab));
[email protected]fad73672012-06-15 23:26:06537
538 // Extension API should succeed.
539 bool result = false;
[email protected]b6987e02013-01-04 18:30:43540 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(newtab, "testExtensionApi()",
541 &result));
[email protected]fad73672012-06-15 23:26:06542 EXPECT_TRUE(result);
543}