blob: 9e70eb61600c8dd6aafa9a42e6e010f1904a04c0 [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]7d478cb2012-07-24 17:19:4218#include "content/public/test/browser_test_utils.h"
[email protected]58b349b2011-11-23 02:07:1519#include "testing/gtest/include/gtest/gtest.h"
[email protected]4026ce1e2010-09-14 19:35:0420#include "net/base/mock_host_resolver.h"
[email protected]32b7c492010-09-01 04:15:0021
[email protected]e5d549d2011-12-28 01:29:2022using content::OpenURLParams;
23using content::Referrer;
[email protected]fad73672012-06-15 23:26:0624using content::WebContents;
[email protected]e5d549d2011-12-28 01:29:2025
[email protected]e78eb292010-12-22 08:35:3926// Disabled, http://crbug.com/64899.
27IN_PROC_BROWSER_TEST_F(ExtensionApiTest, DISABLED_WindowOpen) {
[email protected]32b7c492010-09-01 04:15:0028 CommandLine::ForCurrentProcess()->AppendSwitch(
29 switches::kEnableExperimentalExtensionApis);
30
31 ResultCatcher catcher;
32 ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_
33 .AppendASCII("window_open").AppendASCII("spanning")));
34 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
35}
[email protected]4026ce1e2010-09-14 19:35:0436
[email protected]f9e82d92011-10-29 00:50:4537void WaitForTabsAndPopups(Browser* browser,
38 int num_tabs,
39 int num_popups,
40 int num_panels) {
[email protected]58b349b2011-11-23 02:07:1541 SCOPED_TRACE(
42 StringPrintf("WaitForTabsAndPopups tabs:%d, popups:%d, panels:%d",
43 num_tabs, num_popups, num_panels));
[email protected]4026ce1e2010-09-14 19:35:0444 // We start with one tab and one browser already open.
45 ++num_tabs;
[email protected]f9e82d92011-10-29 00:50:4546 size_t num_browsers = static_cast<size_t>(num_popups + num_panels) + 1;
[email protected]4026ce1e2010-09-14 19:35:0447
[email protected]0a5bdc6522011-06-08 19:04:4948 const base::TimeDelta kWaitTime = base::TimeDelta::FromSeconds(15);
49 base::TimeTicks end_time = base::TimeTicks::Now() + kWaitTime;
50 while (base::TimeTicks::Now() < end_time) {
[email protected]d8748142012-05-16 21:13:4351 if (browser::GetBrowserCount(browser->profile()) == num_browsers &&
[email protected]f9e82d92011-10-29 00:50:4552 browser->tab_count() == num_tabs)
[email protected]0a5bdc6522011-06-08 19:04:4953 break;
54
[email protected]b8deecd2012-07-30 21:09:4455 content::RunAllPendingInMessageLoop();
[email protected]0a5bdc6522011-06-08 19:04:4956 }
57
[email protected]d8748142012-05-16 21:13:4358 EXPECT_EQ(num_browsers, browser::GetBrowserCount(browser->profile()));
[email protected]0a5bdc6522011-06-08 19:04:4959 EXPECT_EQ(num_tabs, browser->tab_count());
60
[email protected]f9e82d92011-10-29 00:50:4561 int num_popups_seen = 0;
62 int num_panels_seen = 0;
[email protected]0a5bdc6522011-06-08 19:04:4963 for (BrowserList::const_iterator iter = BrowserList::begin();
64 iter != BrowserList::end(); ++iter) {
65 if (*iter == browser)
[email protected]4026ce1e2010-09-14 19:35:0466 continue;
[email protected]4026ce1e2010-09-14 19:35:0467
[email protected]0a5bdc6522011-06-08 19:04:4968 // Check for TYPE_POPUP or TYPE_PANEL.
69 EXPECT_TRUE((*iter)->is_type_popup() || (*iter)->is_type_panel());
[email protected]f9e82d92011-10-29 00:50:4570 if ((*iter)->is_type_popup())
71 ++num_popups_seen;
72 else
73 ++num_panels_seen;
[email protected]4026ce1e2010-09-14 19:35:0474 }
[email protected]f9e82d92011-10-29 00:50:4575 EXPECT_EQ(num_popups, num_popups_seen);
76 EXPECT_EQ(num_panels, num_panels_seen);
[email protected]4026ce1e2010-09-14 19:35:0477}
78
[email protected]f112b0f2011-05-26 01:53:5279IN_PROC_BROWSER_TEST_F(ExtensionApiTest, BrowserIsApp) {
80 host_resolver()->AddRule("a.com", "127.0.0.1");
81 ASSERT_TRUE(StartTestServer());
82 ASSERT_TRUE(LoadExtension(
83 test_data_dir_.AppendASCII("window_open").AppendASCII("browser_is_app")));
84
[email protected]f9e82d92011-10-29 00:50:4585 WaitForTabsAndPopups(browser(), 0, 2, 0);
[email protected]f112b0f2011-05-26 01:53:5286
87 for (BrowserList::const_iterator iter = BrowserList::begin();
88 iter != BrowserList::end(); ++iter) {
89 if (*iter == browser())
90 ASSERT_FALSE((*iter)->is_app());
91 else
92 ASSERT_TRUE((*iter)->is_app());
93 }
94}
95
[email protected]0a5bdc6522011-06-08 19:04:4996IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowOpenPopupDefault) {
97 ASSERT_TRUE(StartTestServer());
98 ASSERT_TRUE(LoadExtension(
99 test_data_dir_.AppendASCII("window_open").AppendASCII("popup")));
100
101 const int num_tabs = 1;
102 const int num_popups = 0;
[email protected]f9e82d92011-10-29 00:50:45103 WaitForTabsAndPopups(browser(), num_tabs, num_popups, 0);
[email protected]0a5bdc6522011-06-08 19:04:49104}
105
106IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowOpenPopupLarge) {
107 ASSERT_TRUE(StartTestServer());
108 ASSERT_TRUE(LoadExtension(
109 test_data_dir_.AppendASCII("window_open").AppendASCII("popup_large")));
110
[email protected]0a5bdc6522011-06-08 19:04:49111 // On other systems this should open a new popup window.
112 const int num_tabs = 0;
113 const int num_popups = 1;
[email protected]f9e82d92011-10-29 00:50:45114 WaitForTabsAndPopups(browser(), num_tabs, num_popups, 0);
[email protected]0a5bdc6522011-06-08 19:04:49115}
116
117IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowOpenPopupSmall) {
118 ASSERT_TRUE(StartTestServer());
119 ASSERT_TRUE(LoadExtension(
120 test_data_dir_.AppendASCII("window_open").AppendASCII("popup_small")));
121
122 // On ChromeOS this should open a new panel (acts like a new popup window).
123 // On other systems this should open a new popup window.
124 const int num_tabs = 0;
125 const int num_popups = 1;
[email protected]f9e82d92011-10-29 00:50:45126 WaitForTabsAndPopups(browser(), num_tabs, num_popups, 0);
[email protected]0a5bdc6522011-06-08 19:04:49127}
128
[email protected]4026ce1e2010-09-14 19:35:04129IN_PROC_BROWSER_TEST_F(ExtensionApiTest, PopupBlockingExtension) {
130 host_resolver()->AddRule("*", "127.0.0.1");
[email protected]e18a19b2010-10-19 20:32:30131 ASSERT_TRUE(StartTestServer());
[email protected]4026ce1e2010-09-14 19:35:04132
133 ASSERT_TRUE(LoadExtension(
134 test_data_dir_.AppendASCII("window_open").AppendASCII("popup_blocking")
135 .AppendASCII("extension")));
136
[email protected]f9e82d92011-10-29 00:50:45137 WaitForTabsAndPopups(browser(), 5, 3, 0);
[email protected]4026ce1e2010-09-14 19:35:04138}
139
140IN_PROC_BROWSER_TEST_F(ExtensionApiTest, PopupBlockingHostedApp) {
141 host_resolver()->AddRule("*", "127.0.0.1");
142 ASSERT_TRUE(test_server()->Start());
143
144 ASSERT_TRUE(LoadExtension(
145 test_data_dir_.AppendASCII("window_open").AppendASCII("popup_blocking")
146 .AppendASCII("hosted_app")));
147
[email protected]fe3048872010-10-18 14:58:59148 // The app being tested owns the domain a.com . The test URLs we navigate
149 // to below must be within that domain, so that they fall within the app's
150 // web extent.
151 GURL::Replacements replace_host;
152 std::string a_dot_com = "a.com";
153 replace_host.SetHostStr(a_dot_com);
154
155 const std::string popup_app_contents_path(
156 "files/extensions/api_test/window_open/popup_blocking/hosted_app/");
157
158 GURL open_tab =
159 test_server()->GetURL(popup_app_contents_path + "open_tab.html")
160 .ReplaceComponents(replace_host);
161 GURL open_popup =
162 test_server()->GetURL(popup_app_contents_path + "open_popup.html")
163 .ReplaceComponents(replace_host);
164
[email protected]e5d549d2011-12-28 01:29:20165 browser()->OpenURL(OpenURLParams(
166 open_tab, Referrer(), NEW_FOREGROUND_TAB, content::PAGE_TRANSITION_TYPED,
167 false));
168 browser()->OpenURL(OpenURLParams(
169 open_popup, Referrer(), NEW_FOREGROUND_TAB,
170 content::PAGE_TRANSITION_TYPED, false));
[email protected]4026ce1e2010-09-14 19:35:04171
[email protected]f9e82d92011-10-29 00:50:45172 WaitForTabsAndPopups(browser(), 3, 1, 0);
[email protected]4026ce1e2010-09-14 19:35:04173}
[email protected]d4db6c702011-03-28 21:49:14174
[email protected]e7f90562011-05-23 21:38:24175IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowArgumentsOverflow) {
176 ASSERT_TRUE(RunExtensionTest("window_open/argument_overflow")) << message_;
177}
178
[email protected]d5c14062011-10-27 00:02:13179class WindowOpenPanelDisabledTest : public ExtensionApiTest {
[email protected]08aa0c32011-04-11 22:18:37180 virtual void SetUpCommandLine(CommandLine* command_line) {
181 ExtensionApiTest::SetUpCommandLine(command_line);
[email protected]7e7a28092011-12-09 22:24:55182 // TODO(jennb): Re-enable when panels are enabled by default.
183 // command_line->AppendSwitch(switches::kDisablePanels);
[email protected]08aa0c32011-04-11 22:18:37184 }
185};
186
[email protected]7e7a28092011-12-09 22:24:55187IN_PROC_BROWSER_TEST_F(WindowOpenPanelDisabledTest,
188 DISABLED_WindowOpenPanelNotEnabled) {
[email protected]d5c14062011-10-27 00:02:13189 ASSERT_TRUE(RunExtensionTest("window_open/panel_not_enabled")) << message_;
190}
191
[email protected]af6b54512011-12-14 06:11:21192class WindowOpenPanelTest : public ExtensionApiTest {
193 virtual void SetUpCommandLine(CommandLine* command_line) {
194 ExtensionApiTest::SetUpCommandLine(command_line);
195 command_line->AppendSwitch(switches::kEnablePanels);
196 }
197};
198
[email protected]87d722a2012-07-12 15:38:33199#if defined(USE_ASH)
200// On Ash, this currently fails because we're currently opening new panel
[email protected]d101b0c2012-03-16 00:30:57201// windows as popup windows instead.
202#define MAYBE_WindowOpenPanel FAILS_WindowOpenPanel
203#else
204#define MAYBE_WindowOpenPanel WindowOpenPanel
205#endif
206IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest, MAYBE_WindowOpenPanel) {
[email protected]08aa0c32011-04-11 22:18:37207 ASSERT_TRUE(RunExtensionTest("window_open/panel")) << message_;
208}
[email protected]54edcea2011-07-27 01:56:38209
[email protected]4ff65f12011-10-06 00:35:54210#if defined(OS_MACOSX) || defined(OS_WIN)
211// Focus test fails if there is no window manager on Linux.
[email protected]af6b54512011-12-14 06:11:21212IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest, WindowOpenFocus) {
[email protected]4ff65f12011-10-06 00:35:54213 ASSERT_TRUE(RunExtensionTest("window_open/focus")) << message_;
214}
215#endif
216
[email protected]af6b54512011-12-14 06:11:21217IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest,
[email protected]f9e82d92011-10-29 00:50:45218 CloseNonExtensionPanelsOnUninstall) {
[email protected]87d722a2012-07-12 15:38:33219#if defined(USE_ASH)
220 int num_popups = 4;
221 int num_panels = 0;
222#else
223 int num_popups = 2;
224 int num_panels = 2;
225#endif
[email protected]f9e82d92011-10-29 00:50:45226 ASSERT_TRUE(StartTestServer());
227
228 // Setup listeners to wait on strings we expect the extension pages to send.
229 std::vector<std::string> test_strings;
230 test_strings.push_back("content_tab");
[email protected]87d722a2012-07-12 15:38:33231 if (num_panels)
232 test_strings.push_back("content_panel");
[email protected]f9e82d92011-10-29 00:50:45233 test_strings.push_back("content_popup");
234
235 ScopedVector<ExtensionTestMessageListener> listeners;
236 for (size_t i = 0; i < test_strings.size(); ++i) {
237 listeners.push_back(
238 new ExtensionTestMessageListener(test_strings[i], false));
239 }
240
[email protected]1c321ee2012-05-21 03:02:34241 const extensions::Extension* extension = LoadExtension(
[email protected]f9e82d92011-10-29 00:50:45242 test_data_dir_.AppendASCII("window_open").AppendASCII(
243 "close_panels_on_uninstall"));
244 ASSERT_TRUE(extension);
245
246 // Two tabs. One in extension domain and one in non-extension domain.
247 // Two popups - one in extension domain and one in non-extension domain.
248 // Two panels - one in extension domain and one in non-extension domain.
[email protected]87d722a2012-07-12 15:38:33249 WaitForTabsAndPopups(browser(), 2, num_popups, num_panels);
[email protected]f9e82d92011-10-29 00:50:45250
251 // Wait on test messages to make sure the pages loaded.
252 for (size_t i = 0; i < listeners.size(); ++i)
253 ASSERT_TRUE(listeners[i]->WaitUntilSatisfied());
254
255 UninstallExtension(extension->id());
256
257 // Wait for one tab and one popup in non-extension domain to stay open.
258 // Expect everything else, including panels, to close.
[email protected]87d722a2012-07-12 15:38:33259#if defined(USE_ASH)
260 // In Ash and additional popup remains for the "panel" non-extension domain.
261 num_popups = 2;
262#else
263 num_popups = 1;
264#endif
265 WaitForTabsAndPopups(browser(), 1, num_popups, 0);
[email protected]f9e82d92011-10-29 00:50:45266}
267
[email protected]24c6bcfc2012-02-29 21:06:36268IN_PROC_BROWSER_TEST_F(ExtensionApiTest, DISABLED_WindowOpener) {
[email protected]54edcea2011-07-27 01:56:38269 ASSERT_TRUE(RunExtensionTest("window_open/opener")) << message_;
270}
[email protected]fad73672012-06-15 23:26:06271
272// Tests that an extension page can call window.open to an extension URL and
273// the new window has extension privileges.
274IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, WindowOpenExtension) {
275 ASSERT_TRUE(LoadExtension(
276 test_data_dir_.AppendASCII("uitest").AppendASCII("window_open")));
277
278 GURL start_url(std::string("chrome-extension://") +
279 last_loaded_extension_id_ + "/test.html");
280 ui_test_utils::NavigateToURL(browser(), start_url);
281 WebContents* newtab;
[email protected]52877dbc62012-06-29 22:22:03282 ASSERT_NO_FATAL_FAILURE(OpenWindow(chrome::GetActiveWebContents(browser()),
[email protected]fad73672012-06-15 23:26:06283 start_url.Resolve("newtab.html"), true, &newtab));
284
285 bool result = false;
[email protected]7d478cb2012-07-24 17:19:42286 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractBool(
[email protected]fad73672012-06-15 23:26:06287 newtab->GetRenderViewHost(), L"", L"testExtensionApi()", &result));
288 EXPECT_TRUE(result);
289}
290
291// Tests that if an extension page calls window.open to an invalid extension
292// URL, the browser doesn't crash.
293IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, WindowOpenInvalidExtension) {
294 ASSERT_TRUE(LoadExtension(
295 test_data_dir_.AppendASCII("uitest").AppendASCII("window_open")));
296
297 GURL start_url(std::string("chrome-extension://") +
298 last_loaded_extension_id_ + "/test.html");
299 ui_test_utils::NavigateToURL(browser(), start_url);
[email protected]52877dbc62012-06-29 22:22:03300 ASSERT_NO_FATAL_FAILURE(OpenWindow(chrome::GetActiveWebContents(browser()),
[email protected]fad73672012-06-15 23:26:06301 GURL("chrome-extension://thisissurelynotavalidextensionid/newtab.html"),
302 false, NULL));
303
304 // If we got to this point, we didn't crash, so we're good.
305}
306
307// Tests that calling window.open from the newtab page to an extension URL
308// gives the new window extension privileges - even though the opening page
309// does not have extension privileges, we break the script connection, so
310// there is no privilege leak.
311IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, WindowOpenNoPrivileges) {
312 ASSERT_TRUE(LoadExtension(
313 test_data_dir_.AppendASCII("uitest").AppendASCII("window_open")));
314
315 ui_test_utils::NavigateToURL(browser(), GURL("about:blank"));
316 WebContents* newtab;
[email protected]52877dbc62012-06-29 22:22:03317 ASSERT_NO_FATAL_FAILURE(OpenWindow(chrome::GetActiveWebContents(browser()),
[email protected]fad73672012-06-15 23:26:06318 GURL(std::string("chrome-extension://") + last_loaded_extension_id_ +
319 "/newtab.html"), false, &newtab));
320
321 // Extension API should succeed.
322 bool result = false;
[email protected]7d478cb2012-07-24 17:19:42323 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractBool(
[email protected]fad73672012-06-15 23:26:06324 newtab->GetRenderViewHost(), L"", L"testExtensionApi()", &result));
325 EXPECT_TRUE(result);
326}