blob: 7b1ade13511a48e6ab8993812a4bfc415c188b29 [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
5#include "base/command_line.h"
[email protected]f9e82d92011-10-29 00:50:456#include "base/memory/scoped_vector.h"
[email protected]58b349b2011-11-23 02:07:157#include "base/stringprintf.h"
[email protected]32b7c492010-09-01 04:15:008#include "chrome/browser/extensions/extension_apitest.h"
[email protected]f9e82d92011-10-29 00:50:459#include "chrome/browser/extensions/extension_test_message_listener.h"
[email protected]2ad4a902010-11-17 06:05:1310#include "chrome/browser/ui/browser.h"
[email protected]d8748142012-05-16 21:13:4311#include "chrome/browser/ui/browser_finder.h"
[email protected]2ad4a902010-11-17 06:05:1312#include "chrome/browser/ui/browser_list.h"
[email protected]52877dbc62012-06-29 22:22:0313#include "chrome/browser/ui/browser_tabstrip.h"
[email protected]32b7c492010-09-01 04:15:0014#include "chrome/common/chrome_switches.h"
[email protected]f9e82d92011-10-29 00:50:4515#include "chrome/common/extensions/extension.h"
[email protected]af44e7fb2011-07-29 18:32:3216#include "chrome/test/base/ui_test_utils.h"
[email protected]fad73672012-06-15 23:26:0617#include "content/public/browser/web_contents.h"
[email protected]58b349b2011-11-23 02:07:1518#include "testing/gtest/include/gtest/gtest.h"
[email protected]4026ce1e2010-09-14 19:35:0419#include "net/base/mock_host_resolver.h"
[email protected]32b7c492010-09-01 04:15:0020
[email protected]e5d549d2011-12-28 01:29:2021using content::OpenURLParams;
22using content::Referrer;
[email protected]fad73672012-06-15 23:26:0623using content::WebContents;
[email protected]e5d549d2011-12-28 01:29:2024
[email protected]e78eb292010-12-22 08:35:3925// Disabled, http://crbug.com/64899.
26IN_PROC_BROWSER_TEST_F(ExtensionApiTest, DISABLED_WindowOpen) {
[email protected]32b7c492010-09-01 04:15:0027 CommandLine::ForCurrentProcess()->AppendSwitch(
28 switches::kEnableExperimentalExtensionApis);
29
30 ResultCatcher catcher;
31 ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_
32 .AppendASCII("window_open").AppendASCII("spanning")));
33 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
34}
[email protected]4026ce1e2010-09-14 19:35:0435
[email protected]f9e82d92011-10-29 00:50:4536void WaitForTabsAndPopups(Browser* browser,
37 int num_tabs,
38 int num_popups,
39 int num_panels) {
[email protected]58b349b2011-11-23 02:07:1540 SCOPED_TRACE(
41 StringPrintf("WaitForTabsAndPopups tabs:%d, popups:%d, panels:%d",
42 num_tabs, num_popups, num_panels));
[email protected]4026ce1e2010-09-14 19:35:0443 // We start with one tab and one browser already open.
44 ++num_tabs;
[email protected]f9e82d92011-10-29 00:50:4545 size_t num_browsers = static_cast<size_t>(num_popups + num_panels) + 1;
[email protected]4026ce1e2010-09-14 19:35:0446
[email protected]0a5bdc6522011-06-08 19:04:4947 const base::TimeDelta kWaitTime = base::TimeDelta::FromSeconds(15);
48 base::TimeTicks end_time = base::TimeTicks::Now() + kWaitTime;
49 while (base::TimeTicks::Now() < end_time) {
[email protected]d8748142012-05-16 21:13:4350 if (browser::GetBrowserCount(browser->profile()) == num_browsers &&
[email protected]f9e82d92011-10-29 00:50:4551 browser->tab_count() == num_tabs)
[email protected]0a5bdc6522011-06-08 19:04:4952 break;
53
[email protected]58b349b2011-11-23 02:07:1554 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]0a5bdc6522011-06-08 19:04:4955 }
56
[email protected]d8748142012-05-16 21:13:4357 EXPECT_EQ(num_browsers, browser::GetBrowserCount(browser->profile()));
[email protected]0a5bdc6522011-06-08 19:04:4958 EXPECT_EQ(num_tabs, browser->tab_count());
59
[email protected]f9e82d92011-10-29 00:50:4560 int num_popups_seen = 0;
61 int num_panels_seen = 0;
[email protected]0a5bdc6522011-06-08 19:04:4962 for (BrowserList::const_iterator iter = BrowserList::begin();
63 iter != BrowserList::end(); ++iter) {
64 if (*iter == browser)
[email protected]4026ce1e2010-09-14 19:35:0465 continue;
[email protected]4026ce1e2010-09-14 19:35:0466
[email protected]0a5bdc6522011-06-08 19:04:4967 // Check for TYPE_POPUP or TYPE_PANEL.
68 EXPECT_TRUE((*iter)->is_type_popup() || (*iter)->is_type_panel());
[email protected]f9e82d92011-10-29 00:50:4569 if ((*iter)->is_type_popup())
70 ++num_popups_seen;
71 else
72 ++num_panels_seen;
[email protected]4026ce1e2010-09-14 19:35:0473 }
[email protected]f9e82d92011-10-29 00:50:4574 EXPECT_EQ(num_popups, num_popups_seen);
75 EXPECT_EQ(num_panels, num_panels_seen);
[email protected]4026ce1e2010-09-14 19:35:0476}
77
[email protected]f112b0f2011-05-26 01:53:5278IN_PROC_BROWSER_TEST_F(ExtensionApiTest, BrowserIsApp) {
79 host_resolver()->AddRule("a.com", "127.0.0.1");
80 ASSERT_TRUE(StartTestServer());
81 ASSERT_TRUE(LoadExtension(
82 test_data_dir_.AppendASCII("window_open").AppendASCII("browser_is_app")));
83
[email protected]f9e82d92011-10-29 00:50:4584 WaitForTabsAndPopups(browser(), 0, 2, 0);
[email protected]f112b0f2011-05-26 01:53:5285
86 for (BrowserList::const_iterator iter = BrowserList::begin();
87 iter != BrowserList::end(); ++iter) {
88 if (*iter == browser())
89 ASSERT_FALSE((*iter)->is_app());
90 else
91 ASSERT_TRUE((*iter)->is_app());
92 }
93}
94
[email protected]0a5bdc6522011-06-08 19:04:4995IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowOpenPopupDefault) {
96 ASSERT_TRUE(StartTestServer());
97 ASSERT_TRUE(LoadExtension(
98 test_data_dir_.AppendASCII("window_open").AppendASCII("popup")));
99
100 const int num_tabs = 1;
101 const int num_popups = 0;
[email protected]f9e82d92011-10-29 00:50:45102 WaitForTabsAndPopups(browser(), num_tabs, num_popups, 0);
[email protected]0a5bdc6522011-06-08 19:04:49103}
104
105IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowOpenPopupLarge) {
106 ASSERT_TRUE(StartTestServer());
107 ASSERT_TRUE(LoadExtension(
108 test_data_dir_.AppendASCII("window_open").AppendASCII("popup_large")));
109
[email protected]0a5bdc6522011-06-08 19:04:49110 // On other systems this should open a new popup window.
111 const int num_tabs = 0;
112 const int num_popups = 1;
[email protected]f9e82d92011-10-29 00:50:45113 WaitForTabsAndPopups(browser(), num_tabs, num_popups, 0);
[email protected]0a5bdc6522011-06-08 19:04:49114}
115
116IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowOpenPopupSmall) {
117 ASSERT_TRUE(StartTestServer());
118 ASSERT_TRUE(LoadExtension(
119 test_data_dir_.AppendASCII("window_open").AppendASCII("popup_small")));
120
121 // On ChromeOS this should open a new panel (acts like a new popup window).
122 // On other systems this should open a new popup window.
123 const int num_tabs = 0;
124 const int num_popups = 1;
[email protected]f9e82d92011-10-29 00:50:45125 WaitForTabsAndPopups(browser(), num_tabs, num_popups, 0);
[email protected]0a5bdc6522011-06-08 19:04:49126}
127
[email protected]4026ce1e2010-09-14 19:35:04128IN_PROC_BROWSER_TEST_F(ExtensionApiTest, PopupBlockingExtension) {
129 host_resolver()->AddRule("*", "127.0.0.1");
[email protected]e18a19b2010-10-19 20:32:30130 ASSERT_TRUE(StartTestServer());
[email protected]4026ce1e2010-09-14 19:35:04131
132 ASSERT_TRUE(LoadExtension(
133 test_data_dir_.AppendASCII("window_open").AppendASCII("popup_blocking")
134 .AppendASCII("extension")));
135
[email protected]f9e82d92011-10-29 00:50:45136 WaitForTabsAndPopups(browser(), 5, 3, 0);
[email protected]4026ce1e2010-09-14 19:35:04137}
138
139IN_PROC_BROWSER_TEST_F(ExtensionApiTest, PopupBlockingHostedApp) {
140 host_resolver()->AddRule("*", "127.0.0.1");
141 ASSERT_TRUE(test_server()->Start());
142
143 ASSERT_TRUE(LoadExtension(
144 test_data_dir_.AppendASCII("window_open").AppendASCII("popup_blocking")
145 .AppendASCII("hosted_app")));
146
[email protected]fe3048872010-10-18 14:58:59147 // The app being tested owns the domain a.com . The test URLs we navigate
148 // to below must be within that domain, so that they fall within the app's
149 // web extent.
150 GURL::Replacements replace_host;
151 std::string a_dot_com = "a.com";
152 replace_host.SetHostStr(a_dot_com);
153
154 const std::string popup_app_contents_path(
155 "files/extensions/api_test/window_open/popup_blocking/hosted_app/");
156
157 GURL open_tab =
158 test_server()->GetURL(popup_app_contents_path + "open_tab.html")
159 .ReplaceComponents(replace_host);
160 GURL open_popup =
161 test_server()->GetURL(popup_app_contents_path + "open_popup.html")
162 .ReplaceComponents(replace_host);
163
[email protected]e5d549d2011-12-28 01:29:20164 browser()->OpenURL(OpenURLParams(
165 open_tab, Referrer(), NEW_FOREGROUND_TAB, content::PAGE_TRANSITION_TYPED,
166 false));
167 browser()->OpenURL(OpenURLParams(
168 open_popup, Referrer(), NEW_FOREGROUND_TAB,
169 content::PAGE_TRANSITION_TYPED, false));
[email protected]4026ce1e2010-09-14 19:35:04170
[email protected]f9e82d92011-10-29 00:50:45171 WaitForTabsAndPopups(browser(), 3, 1, 0);
[email protected]4026ce1e2010-09-14 19:35:04172}
[email protected]d4db6c702011-03-28 21:49:14173
[email protected]e7f90562011-05-23 21:38:24174IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowArgumentsOverflow) {
175 ASSERT_TRUE(RunExtensionTest("window_open/argument_overflow")) << message_;
176}
177
[email protected]d5c14062011-10-27 00:02:13178class WindowOpenPanelDisabledTest : public ExtensionApiTest {
[email protected]08aa0c32011-04-11 22:18:37179 virtual void SetUpCommandLine(CommandLine* command_line) {
180 ExtensionApiTest::SetUpCommandLine(command_line);
[email protected]7e7a28092011-12-09 22:24:55181 // TODO(jennb): Re-enable when panels are enabled by default.
182 // command_line->AppendSwitch(switches::kDisablePanels);
[email protected]08aa0c32011-04-11 22:18:37183 }
184};
185
[email protected]7e7a28092011-12-09 22:24:55186IN_PROC_BROWSER_TEST_F(WindowOpenPanelDisabledTest,
187 DISABLED_WindowOpenPanelNotEnabled) {
[email protected]d5c14062011-10-27 00:02:13188 ASSERT_TRUE(RunExtensionTest("window_open/panel_not_enabled")) << message_;
189}
190
[email protected]af6b54512011-12-14 06:11:21191class WindowOpenPanelTest : public ExtensionApiTest {
192 virtual void SetUpCommandLine(CommandLine* command_line) {
193 ExtensionApiTest::SetUpCommandLine(command_line);
194 command_line->AppendSwitch(switches::kEnablePanels);
195 }
196};
197
[email protected]87d722a2012-07-12 15:38:33198#if defined(USE_ASH)
199// On Ash, this currently fails because we're currently opening new panel
[email protected]d101b0c2012-03-16 00:30:57200// windows as popup windows instead.
201#define MAYBE_WindowOpenPanel FAILS_WindowOpenPanel
202#else
203#define MAYBE_WindowOpenPanel WindowOpenPanel
204#endif
205IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest, MAYBE_WindowOpenPanel) {
[email protected]08aa0c32011-04-11 22:18:37206 ASSERT_TRUE(RunExtensionTest("window_open/panel")) << message_;
207}
[email protected]54edcea2011-07-27 01:56:38208
[email protected]4ff65f12011-10-06 00:35:54209#if defined(OS_MACOSX) || defined(OS_WIN)
210// Focus test fails if there is no window manager on Linux.
[email protected]af6b54512011-12-14 06:11:21211IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest, WindowOpenFocus) {
[email protected]4ff65f12011-10-06 00:35:54212 ASSERT_TRUE(RunExtensionTest("window_open/focus")) << message_;
213}
214#endif
215
[email protected]af6b54512011-12-14 06:11:21216IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest,
[email protected]f9e82d92011-10-29 00:50:45217 CloseNonExtensionPanelsOnUninstall) {
[email protected]87d722a2012-07-12 15:38:33218#if defined(USE_ASH)
219 int num_popups = 4;
220 int num_panels = 0;
221#else
222 int num_popups = 2;
223 int num_panels = 2;
224#endif
[email protected]f9e82d92011-10-29 00:50:45225 ASSERT_TRUE(StartTestServer());
226
227 // Setup listeners to wait on strings we expect the extension pages to send.
228 std::vector<std::string> test_strings;
229 test_strings.push_back("content_tab");
[email protected]87d722a2012-07-12 15:38:33230 if (num_panels)
231 test_strings.push_back("content_panel");
[email protected]f9e82d92011-10-29 00:50:45232 test_strings.push_back("content_popup");
233
234 ScopedVector<ExtensionTestMessageListener> listeners;
235 for (size_t i = 0; i < test_strings.size(); ++i) {
236 listeners.push_back(
237 new ExtensionTestMessageListener(test_strings[i], false));
238 }
239
[email protected]1c321ee2012-05-21 03:02:34240 const extensions::Extension* extension = LoadExtension(
[email protected]f9e82d92011-10-29 00:50:45241 test_data_dir_.AppendASCII("window_open").AppendASCII(
242 "close_panels_on_uninstall"));
243 ASSERT_TRUE(extension);
244
245 // Two tabs. One in extension domain and one in non-extension domain.
246 // Two popups - one in extension domain and one in non-extension domain.
247 // Two panels - one in extension domain and one in non-extension domain.
[email protected]87d722a2012-07-12 15:38:33248 WaitForTabsAndPopups(browser(), 2, num_popups, num_panels);
[email protected]f9e82d92011-10-29 00:50:45249
250 // Wait on test messages to make sure the pages loaded.
251 for (size_t i = 0; i < listeners.size(); ++i)
252 ASSERT_TRUE(listeners[i]->WaitUntilSatisfied());
253
254 UninstallExtension(extension->id());
255
256 // Wait for one tab and one popup in non-extension domain to stay open.
257 // Expect everything else, including panels, to close.
[email protected]87d722a2012-07-12 15:38:33258#if defined(USE_ASH)
259 // In Ash and additional popup remains for the "panel" non-extension domain.
260 num_popups = 2;
261#else
262 num_popups = 1;
263#endif
264 WaitForTabsAndPopups(browser(), 1, num_popups, 0);
[email protected]f9e82d92011-10-29 00:50:45265}
266
[email protected]24c6bcfc2012-02-29 21:06:36267IN_PROC_BROWSER_TEST_F(ExtensionApiTest, DISABLED_WindowOpener) {
[email protected]54edcea2011-07-27 01:56:38268 ASSERT_TRUE(RunExtensionTest("window_open/opener")) << message_;
269}
[email protected]fad73672012-06-15 23:26:06270
271// Tests that an extension page can call window.open to an extension URL and
272// the new window has extension privileges.
273IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, WindowOpenExtension) {
274 ASSERT_TRUE(LoadExtension(
275 test_data_dir_.AppendASCII("uitest").AppendASCII("window_open")));
276
277 GURL start_url(std::string("chrome-extension://") +
278 last_loaded_extension_id_ + "/test.html");
279 ui_test_utils::NavigateToURL(browser(), start_url);
280 WebContents* newtab;
[email protected]52877dbc62012-06-29 22:22:03281 ASSERT_NO_FATAL_FAILURE(OpenWindow(chrome::GetActiveWebContents(browser()),
[email protected]fad73672012-06-15 23:26:06282 start_url.Resolve("newtab.html"), true, &newtab));
283
284 bool result = false;
285 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool(
286 newtab->GetRenderViewHost(), L"", L"testExtensionApi()", &result));
287 EXPECT_TRUE(result);
288}
289
290// Tests that if an extension page calls window.open to an invalid extension
291// URL, the browser doesn't crash.
292IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, WindowOpenInvalidExtension) {
293 ASSERT_TRUE(LoadExtension(
294 test_data_dir_.AppendASCII("uitest").AppendASCII("window_open")));
295
296 GURL start_url(std::string("chrome-extension://") +
297 last_loaded_extension_id_ + "/test.html");
298 ui_test_utils::NavigateToURL(browser(), start_url);
[email protected]52877dbc62012-06-29 22:22:03299 ASSERT_NO_FATAL_FAILURE(OpenWindow(chrome::GetActiveWebContents(browser()),
[email protected]fad73672012-06-15 23:26:06300 GURL("chrome-extension://thisissurelynotavalidextensionid/newtab.html"),
301 false, NULL));
302
303 // If we got to this point, we didn't crash, so we're good.
304}
305
306// Tests that calling window.open from the newtab page to an extension URL
307// gives the new window extension privileges - even though the opening page
308// does not have extension privileges, we break the script connection, so
309// there is no privilege leak.
310IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, WindowOpenNoPrivileges) {
311 ASSERT_TRUE(LoadExtension(
312 test_data_dir_.AppendASCII("uitest").AppendASCII("window_open")));
313
314 ui_test_utils::NavigateToURL(browser(), GURL("about:blank"));
315 WebContents* newtab;
[email protected]52877dbc62012-06-29 22:22:03316 ASSERT_NO_FATAL_FAILURE(OpenWindow(chrome::GetActiveWebContents(browser()),
[email protected]fad73672012-06-15 23:26:06317 GURL(std::string("chrome-extension://") + last_loaded_extension_id_ +
318 "/newtab.html"), false, &newtab));
319
320 // Extension API should succeed.
321 bool result = false;
322 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool(
323 newtab->GetRenderViewHost(), L"", L"testExtensionApi()", &result));
324 EXPECT_TRUE(result);
325}