blob: f7d9d92f2b8f26154cf682774ed21110521f6088 [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]f9e82d92011-10-29 00:50:457#include "base/memory/scoped_vector.h"
[email protected]7c6ef352013-09-26 07:23:148#include "base/path_service.h"
[email protected]46acbf12013-06-10 18:43:429#include "base/strings/stringprintf.h"
avia2f4804a2015-12-24 23:11:1310#include "build/build_config.h"
[email protected]32b7c492010-09-01 04:15:0011#include "chrome/browser/extensions/extension_apitest.h"
[email protected]01996a592013-09-13 06:55:0412#include "chrome/browser/profiles/profile.h"
[email protected]2ad4a902010-11-17 06:05:1313#include "chrome/browser/ui/browser.h"
[email protected]d8748142012-05-16 21:13:4314#include "chrome/browser/ui/browser_finder.h"
scottmg8abbff832016-01-28 22:57:3715#include "chrome/browser/ui/browser_list.h"
[email protected]e0448872013-01-11 19:35:0216#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]7c6ef352013-09-26 07:23:1417#include "chrome/common/chrome_paths.h"
[email protected]af44e7fb2011-07-29 18:32:3218#include "chrome/test/base/ui_test_utils.h"
[email protected]1dd66b72012-09-19 20:41:4819#include "content/public/browser/render_process_host.h"
[email protected]fad73672012-06-15 23:26:0620#include "content/public/browser/web_contents.h"
[email protected]1dd66b72012-09-19 20:41:4821#include "content/public/common/result_codes.h"
[email protected]99bee642014-05-31 22:36:4322#include "content/public/common/url_constants.h"
[email protected]7d478cb2012-07-24 17:19:4223#include "content/public/test/browser_test_utils.h"
[email protected]22401dc2014-03-21 01:38:5724#include "extensions/browser/extension_host.h"
[email protected]98b6d942013-11-10 00:34:0725#include "extensions/browser/process_manager.h"
[email protected]99bee642014-05-31 22:36:4326#include "extensions/common/constants.h"
[email protected]e4452d32013-11-15 23:07:4127#include "extensions/common/extension.h"
lfg910f2f92014-09-19 05:31:0928#include "extensions/test/extension_test_message_listener.h"
yoze8dc2f12014-09-09 23:16:3229#include "extensions/test/result_catcher.h"
[email protected]f2cb3cf2013-03-21 01:40:5330#include "net/dns/mock_host_resolver.h"
[email protected]7c6ef352013-09-26 07:23:1431#include "net/test/embedded_test_server/embedded_test_server.h"
[email protected]bc44f4142013-02-12 06:15:5632#include "testing/gtest/include/gtest/gtest.h"
[email protected]32b7c492010-09-01 04:15:0033
[email protected]f1c102b2013-02-15 07:44:1234#if defined(USE_ASH)
hashimotoad3c6872014-08-29 09:46:5735#include "extensions/browser/app_window/app_window_registry.h"
[email protected]f1c102b2013-02-15 07:44:1236#endif
37
[email protected]e5d549d2011-12-28 01:29:2038using content::OpenURLParams;
39using content::Referrer;
[email protected]fad73672012-06-15 23:26:0640using content::WebContents;
[email protected]e5d549d2011-12-28 01:29:2041
robf8cd8ce2015-02-21 12:18:0442// The test uses the chrome.browserAction.openPopup API, which requires that the
43// window can automatically be activated.
44// See comments at BrowserActionInteractiveTest::ShouldRunPopupTest
johnme7e30b512015-04-16 14:57:3545// Fails flakily on all platforms. https://crbug.com/477691
46IN_PROC_BROWSER_TEST_F(ExtensionApiTest, DISABLED_WindowOpen) {
yoze8dc2f12014-09-09 23:16:3247 extensions::ResultCatcher catcher;
[email protected]32b7c492010-09-01 04:15:0048 ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_
49 .AppendASCII("window_open").AppendASCII("spanning")));
50 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
51}
[email protected]4026ce1e2010-09-14 19:35:0452
[email protected]f1c102b2013-02-15 07:44:1253bool WaitForTabsAndPopups(Browser* browser,
[email protected]f9e82d92011-10-29 00:50:4554 int num_tabs,
dimich566d3642016-08-23 16:39:0655 int num_popups) {
[email protected]58b349b2011-11-23 02:07:1556 SCOPED_TRACE(
dimich566d3642016-08-23 16:39:0657 base::StringPrintf("WaitForTabsAndPopups tabs:%d, popups:%d",
58 num_tabs, num_popups));
[email protected]4026ce1e2010-09-14 19:35:0459 // We start with one tab and one browser already open.
60 ++num_tabs;
[email protected]f6ffa7d2012-08-17 17:27:0861 size_t num_browsers = static_cast<size_t>(num_popups) + 1;
[email protected]4026ce1e2010-09-14 19:35:0462
[email protected]f1c102b2013-02-15 07:44:1263 const base::TimeDelta kWaitTime = base::TimeDelta::FromSeconds(10);
[email protected]0a5bdc6522011-06-08 19:04:4964 base::TimeTicks end_time = base::TimeTicks::Now() + kWaitTime;
65 while (base::TimeTicks::Now() < end_time) {
scottmg34c5dd882016-02-03 05:21:5466 if (chrome::GetBrowserCount(browser->profile()) == num_browsers &&
dimich566d3642016-08-23 16:39:0667 browser->tab_strip_model()->count() == num_tabs)
[email protected]0a5bdc6522011-06-08 19:04:4968 break;
69
[email protected]b8deecd2012-07-30 21:09:4470 content::RunAllPendingInMessageLoop();
[email protected]0a5bdc6522011-06-08 19:04:4971 }
72
scottmg34c5dd882016-02-03 05:21:5473 EXPECT_EQ(num_browsers, chrome::GetBrowserCount(browser->profile()));
[email protected]e0448872013-01-11 19:35:0274 EXPECT_EQ(num_tabs, browser->tab_strip_model()->count());
[email protected]0a5bdc6522011-06-08 19:04:4975
[email protected]f9e82d92011-10-29 00:50:4576 int num_popups_seen = 0;
scottmg8abbff832016-01-28 22:57:3777 for (auto* b : *BrowserList::GetInstance()) {
78 if (b == browser)
[email protected]4026ce1e2010-09-14 19:35:0479 continue;
[email protected]4026ce1e2010-09-14 19:35:0480
scottmg8abbff832016-01-28 22:57:3781 EXPECT_TRUE(b->is_type_popup());
[email protected]f6ffa7d2012-08-17 17:27:0882 ++num_popups_seen;
[email protected]4026ce1e2010-09-14 19:35:0483 }
[email protected]f9e82d92011-10-29 00:50:4584 EXPECT_EQ(num_popups, num_popups_seen);
[email protected]f1c102b2013-02-15 07:44:1285
scottmg34c5dd882016-02-03 05:21:5486 return ((num_browsers == chrome::GetBrowserCount(browser->profile())) &&
[email protected]f1c102b2013-02-15 07:44:1287 (num_tabs == browser->tab_strip_model()->count()) &&
[email protected]f1c102b2013-02-15 07:44:1288 (num_popups == num_popups_seen));
[email protected]4026ce1e2010-09-14 19:35:0489}
90
[email protected]f112b0f2011-05-26 01:53:5291IN_PROC_BROWSER_TEST_F(ExtensionApiTest, BrowserIsApp) {
92 host_resolver()->AddRule("a.com", "127.0.0.1");
[email protected]c1dffe82013-06-26 20:59:0593 ASSERT_TRUE(StartEmbeddedTestServer());
[email protected]f112b0f2011-05-26 01:53:5294 ASSERT_TRUE(LoadExtension(
95 test_data_dir_.AppendASCII("window_open").AppendASCII("browser_is_app")));
96
dimich566d3642016-08-23 16:39:0697 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 0, 2));
[email protected]f112b0f2011-05-26 01:53:5298
scottmg8abbff832016-01-28 22:57:3799 for (auto* b : *BrowserList::GetInstance()) {
100 if (b == browser())
101 ASSERT_FALSE(b->is_app());
[email protected]f112b0f2011-05-26 01:53:52102 else
scottmg8abbff832016-01-28 22:57:37103 ASSERT_TRUE(b->is_app());
[email protected]f112b0f2011-05-26 01:53:52104 }
105}
106
[email protected]0a5bdc6522011-06-08 19:04:49107IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowOpenPopupDefault) {
[email protected]c1dffe82013-06-26 20:59:05108 ASSERT_TRUE(StartEmbeddedTestServer());
[email protected]0a5bdc6522011-06-08 19:04:49109 ASSERT_TRUE(LoadExtension(
110 test_data_dir_.AppendASCII("window_open").AppendASCII("popup")));
111
112 const int num_tabs = 1;
113 const int num_popups = 0;
dimich566d3642016-08-23 16:39:06114 EXPECT_TRUE(WaitForTabsAndPopups(browser(), num_tabs, num_popups));
[email protected]0a5bdc6522011-06-08 19:04:49115}
116
[email protected]7c6ef352013-09-26 07:23:14117IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowOpenPopupIframe) {
118 ASSERT_TRUE(StartEmbeddedTestServer());
119 base::FilePath test_data_dir;
120 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir);
121 embedded_test_server()->ServeFilesFromDirectory(test_data_dir);
122 ASSERT_TRUE(LoadExtension(
123 test_data_dir_.AppendASCII("window_open").AppendASCII("popup_iframe")));
124
jochenc5cfa642014-09-02 15:30:03125 const int num_tabs = 1;
126 const int num_popups = 0;
dimich566d3642016-08-23 16:39:06127 EXPECT_TRUE(WaitForTabsAndPopups(browser(), num_tabs, num_popups));
[email protected]7c6ef352013-09-26 07:23:14128}
129
[email protected]0a5bdc6522011-06-08 19:04:49130IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowOpenPopupLarge) {
[email protected]c1dffe82013-06-26 20:59:05131 ASSERT_TRUE(StartEmbeddedTestServer());
[email protected]0a5bdc6522011-06-08 19:04:49132 ASSERT_TRUE(LoadExtension(
133 test_data_dir_.AppendASCII("window_open").AppendASCII("popup_large")));
134
[email protected]0a5bdc6522011-06-08 19:04:49135 // On other systems this should open a new popup window.
136 const int num_tabs = 0;
137 const int num_popups = 1;
dimich566d3642016-08-23 16:39:06138 EXPECT_TRUE(WaitForTabsAndPopups(browser(), num_tabs, num_popups));
[email protected]0a5bdc6522011-06-08 19:04:49139}
140
141IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowOpenPopupSmall) {
[email protected]c1dffe82013-06-26 20:59:05142 ASSERT_TRUE(StartEmbeddedTestServer());
[email protected]0a5bdc6522011-06-08 19:04:49143 ASSERT_TRUE(LoadExtension(
144 test_data_dir_.AppendASCII("window_open").AppendASCII("popup_small")));
145
146 // On ChromeOS this should open a new panel (acts like a new popup window).
147 // On other systems this should open a new popup window.
148 const int num_tabs = 0;
149 const int num_popups = 1;
dimich566d3642016-08-23 16:39:06150 EXPECT_TRUE(WaitForTabsAndPopups(browser(), num_tabs, num_popups));
[email protected]0a5bdc6522011-06-08 19:04:49151}
152
[email protected]73fa1fce2013-02-21 20:10:05153// Disabled on Windows. Often times out or fails: crbug.com/177530
154#if defined(OS_WIN)
155#define MAYBE_PopupBlockingExtension DISABLED_PopupBlockingExtension
156#else
157#define MAYBE_PopupBlockingExtension PopupBlockingExtension
158#endif
159IN_PROC_BROWSER_TEST_F(ExtensionApiTest, MAYBE_PopupBlockingExtension) {
[email protected]4026ce1e2010-09-14 19:35:04160 host_resolver()->AddRule("*", "127.0.0.1");
[email protected]c1dffe82013-06-26 20:59:05161 ASSERT_TRUE(StartEmbeddedTestServer());
[email protected]4026ce1e2010-09-14 19:35:04162
163 ASSERT_TRUE(LoadExtension(
164 test_data_dir_.AppendASCII("window_open").AppendASCII("popup_blocking")
165 .AppendASCII("extension")));
166
dimich566d3642016-08-23 16:39:06167 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 5, 3));
[email protected]4026ce1e2010-09-14 19:35:04168}
169
170IN_PROC_BROWSER_TEST_F(ExtensionApiTest, PopupBlockingHostedApp) {
171 host_resolver()->AddRule("*", "127.0.0.1");
svaldeza01f7d92015-11-18 17:47:56172 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]4026ce1e2010-09-14 19:35:04173
174 ASSERT_TRUE(LoadExtension(
175 test_data_dir_.AppendASCII("window_open").AppendASCII("popup_blocking")
176 .AppendASCII("hosted_app")));
177
[email protected]fe3048872010-10-18 14:58:59178 // The app being tested owns the domain a.com . The test URLs we navigate
179 // to below must be within that domain, so that they fall within the app's
180 // web extent.
181 GURL::Replacements replace_host;
mgiuca77752c32015-02-05 07:31:18182 replace_host.SetHostStr("a.com");
[email protected]fe3048872010-10-18 14:58:59183
184 const std::string popup_app_contents_path(
svaldeza01f7d92015-11-18 17:47:56185 "/extensions/api_test/window_open/popup_blocking/hosted_app/");
[email protected]fe3048872010-10-18 14:58:59186
svaldeza01f7d92015-11-18 17:47:56187 GURL open_tab = embedded_test_server()
188 ->GetURL(popup_app_contents_path + "open_tab.html")
189 .ReplaceComponents(replace_host);
190 GURL open_popup = embedded_test_server()
191 ->GetURL(popup_app_contents_path + "open_popup.html")
192 .ReplaceComponents(replace_host);
[email protected]fe3048872010-10-18 14:58:59193
[email protected]e5d549d2011-12-28 01:29:20194 browser()->OpenURL(OpenURLParams(
Sylvain Defresnec6ccc77d2014-09-19 10:19:35195 open_tab, Referrer(), NEW_FOREGROUND_TAB, ui::PAGE_TRANSITION_TYPED,
[email protected]e5d549d2011-12-28 01:29:20196 false));
197 browser()->OpenURL(OpenURLParams(
198 open_popup, Referrer(), NEW_FOREGROUND_TAB,
Sylvain Defresnec6ccc77d2014-09-19 10:19:35199 ui::PAGE_TRANSITION_TYPED, false));
[email protected]4026ce1e2010-09-14 19:35:04200
dimich566d3642016-08-23 16:39:06201 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 3, 1));
[email protected]4026ce1e2010-09-14 19:35:04202}
[email protected]d4db6c702011-03-28 21:49:14203
[email protected]e7f90562011-05-23 21:38:24204IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowArgumentsOverflow) {
205 ASSERT_TRUE(RunExtensionTest("window_open/argument_overflow")) << message_;
206}
207
[email protected]24c6bcfc2012-02-29 21:06:36208IN_PROC_BROWSER_TEST_F(ExtensionApiTest, DISABLED_WindowOpener) {
[email protected]54edcea2011-07-27 01:56:38209 ASSERT_TRUE(RunExtensionTest("window_open/opener")) << message_;
210}
[email protected]fad73672012-06-15 23:26:06211
[email protected]fa7c1602013-04-03 23:00:20212#if defined(OS_MACOSX)
213// Extension popup windows are incorrectly sized on OSX, crbug.com/225601
214#define MAYBE_WindowOpenSized DISABLED_WindowOpenSized
215#else
216#define MAYBE_WindowOpenSized WindowOpenSized
217#endif
218// Ensure that the width and height properties of a window opened with
219// chrome.windows.create match the creation parameters. See crbug.com/173831.
220IN_PROC_BROWSER_TEST_F(ExtensionApiTest, MAYBE_WindowOpenSized) {
221 ASSERT_TRUE(RunExtensionTest("window_open/window_size")) << message_;
dimich566d3642016-08-23 16:39:06222 EXPECT_TRUE(WaitForTabsAndPopups(browser(), 0, 1));
[email protected]fa7c1602013-04-03 23:00:20223}
224
[email protected]fad73672012-06-15 23:26:06225// Tests that an extension page can call window.open to an extension URL and
226// the new window has extension privileges.
227IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, WindowOpenExtension) {
228 ASSERT_TRUE(LoadExtension(
229 test_data_dir_.AppendASCII("uitest").AppendASCII("window_open")));
230
[email protected]99bee642014-05-31 22:36:43231 GURL start_url(std::string(extensions::kExtensionScheme) +
[email protected]fb4fe0952014-06-05 09:44:24232 url::kStandardSchemeSeparator +
[email protected]99bee642014-05-31 22:36:43233 last_loaded_extension_id() + "/test.html");
[email protected]fad73672012-06-15 23:26:06234 ui_test_utils::NavigateToURL(browser(), start_url);
[email protected]f3e73f62012-10-20 05:35:16235 WebContents* newtab = NULL;
[email protected]e0448872013-01-11 19:35:02236 ASSERT_NO_FATAL_FAILURE(
237 OpenWindow(browser()->tab_strip_model()->GetActiveWebContents(),
238 start_url.Resolve("newtab.html"), true, &newtab));
[email protected]fad73672012-06-15 23:26:06239
240 bool result = false;
[email protected]b6987e02013-01-04 18:30:43241 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(newtab, "testExtensionApi()",
242 &result));
[email protected]fad73672012-06-15 23:26:06243 EXPECT_TRUE(result);
244}
245
246// Tests that if an extension page calls window.open to an invalid extension
247// URL, the browser doesn't crash.
248IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, WindowOpenInvalidExtension) {
249 ASSERT_TRUE(LoadExtension(
250 test_data_dir_.AppendASCII("uitest").AppendASCII("window_open")));
251
[email protected]99bee642014-05-31 22:36:43252 GURL start_url(std::string(extensions::kExtensionScheme) +
[email protected]fb4fe0952014-06-05 09:44:24253 url::kStandardSchemeSeparator +
[email protected]99bee642014-05-31 22:36:43254 last_loaded_extension_id() + "/test.html");
[email protected]fad73672012-06-15 23:26:06255 ui_test_utils::NavigateToURL(browser(), start_url);
[email protected]e0448872013-01-11 19:35:02256 ASSERT_NO_FATAL_FAILURE(
257 OpenWindow(browser()->tab_strip_model()->GetActiveWebContents(),
[email protected]fad73672012-06-15 23:26:06258 GURL("chrome-extension://thisissurelynotavalidextensionid/newtab.html"),
259 false, NULL));
260
261 // If we got to this point, we didn't crash, so we're good.
262}
263
264// Tests that calling window.open from the newtab page to an extension URL
265// gives the new window extension privileges - even though the opening page
266// does not have extension privileges, we break the script connection, so
267// there is no privilege leak.
268IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, WindowOpenNoPrivileges) {
269 ASSERT_TRUE(LoadExtension(
270 test_data_dir_.AppendASCII("uitest").AppendASCII("window_open")));
271
272 ui_test_utils::NavigateToURL(browser(), GURL("about:blank"));
[email protected]f3e73f62012-10-20 05:35:16273 WebContents* newtab = NULL;
[email protected]e0448872013-01-11 19:35:02274 ASSERT_NO_FATAL_FAILURE(
275 OpenWindow(browser()->tab_strip_model()->GetActiveWebContents(),
[email protected]99bee642014-05-31 22:36:43276 GURL(std::string(extensions::kExtensionScheme) +
[email protected]fb4fe0952014-06-05 09:44:24277 url::kStandardSchemeSeparator +
[email protected]99bee642014-05-31 22:36:43278 last_loaded_extension_id() + "/newtab.html"),
279 false,
280 &newtab));
[email protected]fad73672012-06-15 23:26:06281
282 // Extension API should succeed.
283 bool result = false;
[email protected]b6987e02013-01-04 18:30:43284 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(newtab, "testExtensionApi()",
285 &result));
[email protected]fad73672012-06-15 23:26:06286 EXPECT_TRUE(result);
287}