blob: 9929dc239269405bf3b3ce250bc74d2108debe5b [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]7c6ef352013-09-26 07:23:147#include "base/path_service.h"
Avi Drissman5f0fb8c2018-12-25 23:20:498#include "base/stl_util.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"
alexmoscf5145bb2016-11-09 23:19:4518#include "chrome/common/url_constants.h"
[email protected]af44e7fb2011-07-29 18:32:3219#include "chrome/test/base/ui_test_utils.h"
nick6cfe5c72017-05-22 22:00:4220#include "content/public/browser/navigation_entry.h"
alexmos1c3da4f2016-11-03 06:06:1121#include "content/public/browser/notification_service.h"
22#include "content/public/browser/notification_types.h"
23#include "content/public/browser/render_frame_host.h"
[email protected]1dd66b72012-09-19 20:41:4824#include "content/public/browser/render_process_host.h"
[email protected]fad73672012-06-15 23:26:0625#include "content/public/browser/web_contents.h"
[email protected]1dd66b72012-09-19 20:41:4826#include "content/public/common/result_codes.h"
[email protected]99bee642014-05-31 22:36:4327#include "content/public/common/url_constants.h"
[email protected]7d478cb2012-07-24 17:19:4228#include "content/public/test/browser_test_utils.h"
alexmoscf5145bb2016-11-09 23:19:4529#include "content/public/test/test_navigation_observer.h"
[email protected]22401dc2014-03-21 01:38:5730#include "extensions/browser/extension_host.h"
[email protected]98b6d942013-11-10 00:34:0731#include "extensions/browser/process_manager.h"
[email protected]99bee642014-05-31 22:36:4332#include "extensions/common/constants.h"
[email protected]e4452d32013-11-15 23:07:4133#include "extensions/common/extension.h"
lfg910f2f92014-09-19 05:31:0934#include "extensions/test/extension_test_message_listener.h"
yoze8dc2f12014-09-09 23:16:3235#include "extensions/test/result_catcher.h"
[email protected]f2cb3cf2013-03-21 01:40:5336#include "net/dns/mock_host_resolver.h"
[email protected]7c6ef352013-09-26 07:23:1437#include "net/test/embedded_test_server/embedded_test_server.h"
[email protected]bc44f4142013-02-12 06:15:5638#include "testing/gtest/include/gtest/gtest.h"
Ivan Sandrkc30341b2017-11-09 19:13:2639#include "ui/base/base_window.h"
40
41#if defined(OS_CHROMEOS)
Xiyuan Xia4f992f22019-06-05 15:33:4642#include "ash/public/cpp/window_pin_type.h"
Ivan Sandrkc30341b2017-11-09 19:13:2643#include "ash/public/cpp/window_properties.h"
Ivan Sandrk9669d0e2017-12-15 23:50:2044#include "chrome/app/chrome_command_ids.h"
Ivan Sandrkc30341b2017-11-09 19:13:2645#include "chrome/browser/extensions/window_controller.h"
46#include "chrome/browser/extensions/window_controller_list.h"
Ivan Sandrk9669d0e2017-12-15 23:50:2047#include "chrome/browser/ui/browser_command_controller.h"
Ivan Sandrkc30341b2017-11-09 19:13:2648#include "ui/aura/window.h"
49#endif
[email protected]32b7c492010-09-01 04:15:0050
[email protected]e5d549d2011-12-28 01:29:2051using content::OpenURLParams;
52using content::Referrer;
[email protected]fad73672012-06-15 23:26:0653using content::WebContents;
[email protected]e5d549d2011-12-28 01:29:2054
Ivan Sandrkc30341b2017-11-09 19:13:2655namespace aura {
56class Window;
57}
58
Devlin Cronin836f545d2018-05-09 00:25:0559namespace extensions {
60
jam1a5b5582017-05-01 16:50:1061class WindowOpenApiTest : public ExtensionApiTest {
62 void SetUpOnMainThread() override {
63 ExtensionApiTest::SetUpOnMainThread();
64 host_resolver()->AddRule("*", "127.0.0.1");
65 }
66};
67
robf8cd8ce2015-02-21 12:18:0468// The test uses the chrome.browserAction.openPopup API, which requires that the
69// window can automatically be activated.
70// See comments at BrowserActionInteractiveTest::ShouldRunPopupTest
johnme7e30b512015-04-16 14:57:3571// Fails flakily on all platforms. https://crbug.com/477691
jam1a5b5582017-05-01 16:50:1072IN_PROC_BROWSER_TEST_F(WindowOpenApiTest, DISABLED_WindowOpen) {
yoze8dc2f12014-09-09 23:16:3273 extensions::ResultCatcher catcher;
[email protected]32b7c492010-09-01 04:15:0074 ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_
75 .AppendASCII("window_open").AppendASCII("spanning")));
76 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
77}
[email protected]4026ce1e2010-09-14 19:35:0478
Joel Hockey2687ab52019-08-14 23:59:4679bool WaitForTabsPopupsApps(Browser* browser,
80 int num_tabs,
81 int num_popups,
82 int num_apps) {
[email protected]58b349b2011-11-23 02:07:1583 SCOPED_TRACE(
Joel Hockey2687ab52019-08-14 23:59:4684 base::StringPrintf("WaitForTabsPopupsApps tabs:%d, popups:%d, apps:%d",
85 num_tabs, num_popups, num_apps));
[email protected]4026ce1e2010-09-14 19:35:0486 // We start with one tab and one browser already open.
87 ++num_tabs;
Joel Hockey2687ab52019-08-14 23:59:4688 size_t num_browsers = static_cast<size_t>(num_popups + num_apps) + 1;
[email protected]4026ce1e2010-09-14 19:35:0489
[email protected]f1c102b2013-02-15 07:44:1290 const base::TimeDelta kWaitTime = base::TimeDelta::FromSeconds(10);
[email protected]0a5bdc6522011-06-08 19:04:4991 base::TimeTicks end_time = base::TimeTicks::Now() + kWaitTime;
92 while (base::TimeTicks::Now() < end_time) {
scottmg34c5dd882016-02-03 05:21:5493 if (chrome::GetBrowserCount(browser->profile()) == num_browsers &&
dimich566d3642016-08-23 16:39:0694 browser->tab_strip_model()->count() == num_tabs)
[email protected]0a5bdc6522011-06-08 19:04:4995 break;
96
[email protected]b8deecd2012-07-30 21:09:4497 content::RunAllPendingInMessageLoop();
[email protected]0a5bdc6522011-06-08 19:04:4998 }
99
scottmg34c5dd882016-02-03 05:21:54100 EXPECT_EQ(num_browsers, chrome::GetBrowserCount(browser->profile()));
[email protected]e0448872013-01-11 19:35:02101 EXPECT_EQ(num_tabs, browser->tab_strip_model()->count());
[email protected]0a5bdc6522011-06-08 19:04:49102
[email protected]f9e82d92011-10-29 00:50:45103 int num_popups_seen = 0;
Joel Hockey2687ab52019-08-14 23:59:46104 int num_apps_seen = 0;
scottmg8abbff832016-01-28 22:57:37105 for (auto* b : *BrowserList::GetInstance()) {
106 if (b == browser)
[email protected]4026ce1e2010-09-14 19:35:04107 continue;
[email protected]4026ce1e2010-09-14 19:35:04108
Joel Hockey2687ab52019-08-14 23:59:46109 EXPECT_TRUE(b->is_type_popup() || b->is_type_app());
110 if (b->is_type_popup())
111 ++num_popups_seen;
112 else if (b->is_type_app())
113 ++num_apps_seen;
[email protected]4026ce1e2010-09-14 19:35:04114 }
[email protected]f9e82d92011-10-29 00:50:45115 EXPECT_EQ(num_popups, num_popups_seen);
Joel Hockey2687ab52019-08-14 23:59:46116 EXPECT_EQ(num_apps, num_apps_seen);
[email protected]f1c102b2013-02-15 07:44:12117
scottmg34c5dd882016-02-03 05:21:54118 return ((num_browsers == chrome::GetBrowserCount(browser->profile())) &&
[email protected]f1c102b2013-02-15 07:44:12119 (num_tabs == browser->tab_strip_model()->count()) &&
Joel Hockey2687ab52019-08-14 23:59:46120 (num_popups == num_popups_seen) && (num_apps == num_apps_seen));
[email protected]4026ce1e2010-09-14 19:35:04121}
122
jam1a5b5582017-05-01 16:50:10123IN_PROC_BROWSER_TEST_F(WindowOpenApiTest, BrowserIsApp) {
[email protected]c1dffe82013-06-26 20:59:05124 ASSERT_TRUE(StartEmbeddedTestServer());
[email protected]f112b0f2011-05-26 01:53:52125 ASSERT_TRUE(LoadExtension(
126 test_data_dir_.AppendASCII("window_open").AppendASCII("browser_is_app")));
127
Joel Hockey2687ab52019-08-14 23:59:46128 EXPECT_TRUE(WaitForTabsPopupsApps(browser(), 0, 0, 2));
[email protected]f112b0f2011-05-26 01:53:52129
scottmg8abbff832016-01-28 22:57:37130 for (auto* b : *BrowserList::GetInstance()) {
131 if (b == browser())
Joel Hockey2687ab52019-08-14 23:59:46132 ASSERT_FALSE(b->is_type_app());
[email protected]f112b0f2011-05-26 01:53:52133 else
Joel Hockey2687ab52019-08-14 23:59:46134 ASSERT_TRUE(b->is_type_app());
[email protected]f112b0f2011-05-26 01:53:52135 }
136}
137
jam1a5b5582017-05-01 16:50:10138IN_PROC_BROWSER_TEST_F(WindowOpenApiTest, WindowOpenPopupDefault) {
[email protected]c1dffe82013-06-26 20:59:05139 ASSERT_TRUE(StartEmbeddedTestServer());
[email protected]0a5bdc6522011-06-08 19:04:49140 ASSERT_TRUE(LoadExtension(
141 test_data_dir_.AppendASCII("window_open").AppendASCII("popup")));
142
Joel Hockey2687ab52019-08-14 23:59:46143 EXPECT_TRUE(WaitForTabsPopupsApps(browser(), 1, 0, 0));
[email protected]0a5bdc6522011-06-08 19:04:49144}
145
jam1a5b5582017-05-01 16:50:10146IN_PROC_BROWSER_TEST_F(WindowOpenApiTest, WindowOpenPopupIframe) {
[email protected]7c6ef352013-09-26 07:23:14147 base::FilePath test_data_dir;
Avi Drissman9098f9002018-05-04 00:11:52148 base::PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir);
[email protected]7c6ef352013-09-26 07:23:14149 embedded_test_server()->ServeFilesFromDirectory(test_data_dir);
martijnf9b885b72016-11-22 22:03:47150 ASSERT_TRUE(StartEmbeddedTestServer());
[email protected]7c6ef352013-09-26 07:23:14151 ASSERT_TRUE(LoadExtension(
152 test_data_dir_.AppendASCII("window_open").AppendASCII("popup_iframe")));
153
Joel Hockey2687ab52019-08-14 23:59:46154 EXPECT_TRUE(WaitForTabsPopupsApps(browser(), 1, 0, 0));
[email protected]7c6ef352013-09-26 07:23:14155}
156
jam1a5b5582017-05-01 16:50:10157IN_PROC_BROWSER_TEST_F(WindowOpenApiTest, WindowOpenPopupLarge) {
[email protected]c1dffe82013-06-26 20:59:05158 ASSERT_TRUE(StartEmbeddedTestServer());
[email protected]0a5bdc6522011-06-08 19:04:49159 ASSERT_TRUE(LoadExtension(
160 test_data_dir_.AppendASCII("window_open").AppendASCII("popup_large")));
161
[email protected]0a5bdc6522011-06-08 19:04:49162 // On other systems this should open a new popup window.
Joel Hockey2687ab52019-08-14 23:59:46163 EXPECT_TRUE(WaitForTabsPopupsApps(browser(), 0, 0, 1));
[email protected]0a5bdc6522011-06-08 19:04:49164}
165
jam1a5b5582017-05-01 16:50:10166IN_PROC_BROWSER_TEST_F(WindowOpenApiTest, WindowOpenPopupSmall) {
[email protected]c1dffe82013-06-26 20:59:05167 ASSERT_TRUE(StartEmbeddedTestServer());
[email protected]0a5bdc6522011-06-08 19:04:49168 ASSERT_TRUE(LoadExtension(
169 test_data_dir_.AppendASCII("window_open").AppendASCII("popup_small")));
170
171 // On ChromeOS this should open a new panel (acts like a new popup window).
172 // On other systems this should open a new popup window.
Joel Hockey2687ab52019-08-14 23:59:46173 EXPECT_TRUE(WaitForTabsPopupsApps(browser(), 0, 0, 1));
[email protected]0a5bdc6522011-06-08 19:04:49174}
175
[email protected]73fa1fce2013-02-21 20:10:05176// 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
jam1a5b5582017-05-01 16:50:10182IN_PROC_BROWSER_TEST_F(WindowOpenApiTest, MAYBE_PopupBlockingExtension) {
[email protected]c1dffe82013-06-26 20:59:05183 ASSERT_TRUE(StartEmbeddedTestServer());
[email protected]4026ce1e2010-09-14 19:35:04184
185 ASSERT_TRUE(LoadExtension(
186 test_data_dir_.AppendASCII("window_open").AppendASCII("popup_blocking")
187 .AppendASCII("extension")));
188
Joel Hockey2687ab52019-08-14 23:59:46189 EXPECT_TRUE(WaitForTabsPopupsApps(browser(), 5, 2, 1));
[email protected]4026ce1e2010-09-14 19:35:04190}
191
jam1a5b5582017-05-01 16:50:10192IN_PROC_BROWSER_TEST_F(WindowOpenApiTest, PopupBlockingHostedApp) {
svaldeza01f7d92015-11-18 17:47:56193 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]4026ce1e2010-09-14 19:35:04194
195 ASSERT_TRUE(LoadExtension(
196 test_data_dir_.AppendASCII("window_open").AppendASCII("popup_blocking")
197 .AppendASCII("hosted_app")));
198
[email protected]fe3048872010-10-18 14:58:59199 // The app being tested owns the domain a.com . The test URLs we navigate
200 // to below must be within that domain, so that they fall within the app's
201 // web extent.
202 GURL::Replacements replace_host;
mgiuca77752c32015-02-05 07:31:18203 replace_host.SetHostStr("a.com");
[email protected]fe3048872010-10-18 14:58:59204
205 const std::string popup_app_contents_path(
svaldeza01f7d92015-11-18 17:47:56206 "/extensions/api_test/window_open/popup_blocking/hosted_app/");
[email protected]fe3048872010-10-18 14:58:59207
svaldeza01f7d92015-11-18 17:47:56208 GURL open_tab = embedded_test_server()
209 ->GetURL(popup_app_contents_path + "open_tab.html")
210 .ReplaceComponents(replace_host);
211 GURL open_popup = embedded_test_server()
212 ->GetURL(popup_app_contents_path + "open_popup.html")
213 .ReplaceComponents(replace_host);
[email protected]fe3048872010-10-18 14:58:59214
nick3b04f322016-08-31 19:29:19215 browser()->OpenURL(OpenURLParams(open_tab, Referrer(),
216 WindowOpenDisposition::NEW_FOREGROUND_TAB,
217 ui::PAGE_TRANSITION_TYPED, false));
218 browser()->OpenURL(OpenURLParams(open_popup, Referrer(),
219 WindowOpenDisposition::NEW_FOREGROUND_TAB,
220 ui::PAGE_TRANSITION_TYPED, false));
[email protected]4026ce1e2010-09-14 19:35:04221
Joel Hockey2687ab52019-08-14 23:59:46222 EXPECT_TRUE(WaitForTabsPopupsApps(browser(), 3, 1, 0));
[email protected]4026ce1e2010-09-14 19:35:04223}
[email protected]d4db6c702011-03-28 21:49:14224
jam1a5b5582017-05-01 16:50:10225IN_PROC_BROWSER_TEST_F(WindowOpenApiTest, WindowArgumentsOverflow) {
[email protected]e7f90562011-05-23 21:38:24226 ASSERT_TRUE(RunExtensionTest("window_open/argument_overflow")) << message_;
227}
228
jam1a5b5582017-05-01 16:50:10229IN_PROC_BROWSER_TEST_F(WindowOpenApiTest, DISABLED_WindowOpener) {
[email protected]54edcea2011-07-27 01:56:38230 ASSERT_TRUE(RunExtensionTest("window_open/opener")) << message_;
231}
[email protected]fad73672012-06-15 23:26:06232
[email protected]fa7c1602013-04-03 23:00:20233#if defined(OS_MACOSX)
234// Extension popup windows are incorrectly sized on OSX, crbug.com/225601
235#define MAYBE_WindowOpenSized DISABLED_WindowOpenSized
236#else
237#define MAYBE_WindowOpenSized WindowOpenSized
238#endif
239// Ensure that the width and height properties of a window opened with
240// chrome.windows.create match the creation parameters. See crbug.com/173831.
jam1a5b5582017-05-01 16:50:10241IN_PROC_BROWSER_TEST_F(WindowOpenApiTest, MAYBE_WindowOpenSized) {
[email protected]fa7c1602013-04-03 23:00:20242 ASSERT_TRUE(RunExtensionTest("window_open/window_size")) << message_;
Joel Hockey2687ab52019-08-14 23:59:46243 EXPECT_TRUE(WaitForTabsPopupsApps(browser(), 0, 0, 1));
[email protected]fa7c1602013-04-03 23:00:20244}
245
[email protected]fad73672012-06-15 23:26:06246// Tests that an extension page can call window.open to an extension URL and
247// the new window has extension privileges.
248IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, WindowOpenExtension) {
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]f3e73f62012-10-20 05:35:16256 WebContents* newtab = NULL;
[email protected]e0448872013-01-11 19:35:02257 ASSERT_NO_FATAL_FAILURE(
258 OpenWindow(browser()->tab_strip_model()->GetActiveWebContents(),
Devlin Croninf1afce252017-08-19 01:21:48259 start_url.Resolve("newtab.html"), true, true, &newtab));
[email protected]fad73672012-06-15 23:26:06260
261 bool result = false;
[email protected]b6987e02013-01-04 18:30:43262 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(newtab, "testExtensionApi()",
263 &result));
[email protected]fad73672012-06-15 23:26:06264 EXPECT_TRUE(result);
265}
266
267// Tests that if an extension page calls window.open to an invalid extension
268// URL, the browser doesn't crash.
269IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, WindowOpenInvalidExtension) {
nick6cfe5c72017-05-22 22:00:42270 const extensions::Extension* extension = LoadExtension(
271 test_data_dir_.AppendASCII("uitest").AppendASCII("window_open"));
272 ASSERT_TRUE(extension);
[email protected]fad73672012-06-15 23:26:06273
nick6cfe5c72017-05-22 22:00:42274 GURL start_url = extension->GetResourceURL("/test.html");
[email protected]fad73672012-06-15 23:26:06275 ui_test_utils::NavigateToURL(browser(), start_url);
nick6cfe5c72017-05-22 22:00:42276 WebContents* newtab = nullptr;
Lukasz Anforowiczfc417492019-09-25 19:53:28277 bool new_page_in_same_process = false;
Devlin Croninf1afce252017-08-19 01:21:48278 bool expect_success = false;
Lukasz Anforowiczfc417492019-09-25 19:53:28279 GURL broken_extension_url(
280 "chrome-extension://thisissurelynotavalidextensionid/newtab.html");
nick6cfe5c72017-05-22 22:00:42281 ASSERT_NO_FATAL_FAILURE(OpenWindow(
282 browser()->tab_strip_model()->GetActiveWebContents(),
Lukasz Anforowiczfc417492019-09-25 19:53:28283 broken_extension_url, new_page_in_same_process, expect_success, &newtab));
[email protected]fad73672012-06-15 23:26:06284
Lukasz Anforowiczfc417492019-09-25 19:53:28285 EXPECT_EQ(broken_extension_url,
286 newtab->GetMainFrame()->GetLastCommittedURL());
287 EXPECT_EQ(content::PAGE_TYPE_ERROR,
288 newtab->GetController().GetLastCommittedEntry()->GetPageType());
[email protected]fad73672012-06-15 23:26:06289}
290
291// Tests that calling window.open from the newtab page to an extension URL
292// gives the new window extension privileges - even though the opening page
293// does not have extension privileges, we break the script connection, so
294// there is no privilege leak.
295IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, WindowOpenNoPrivileges) {
296 ASSERT_TRUE(LoadExtension(
297 test_data_dir_.AppendASCII("uitest").AppendASCII("window_open")));
298
299 ui_test_utils::NavigateToURL(browser(), GURL("about:blank"));
[email protected]f3e73f62012-10-20 05:35:16300 WebContents* newtab = NULL;
[email protected]e0448872013-01-11 19:35:02301 ASSERT_NO_FATAL_FAILURE(
302 OpenWindow(browser()->tab_strip_model()->GetActiveWebContents(),
[email protected]99bee642014-05-31 22:36:43303 GURL(std::string(extensions::kExtensionScheme) +
Devlin Croninf1afce252017-08-19 01:21:48304 url::kStandardSchemeSeparator +
305 last_loaded_extension_id() + "/newtab.html"),
306 false, true, &newtab));
[email protected]fad73672012-06-15 23:26:06307
308 // Extension API should succeed.
309 bool result = false;
[email protected]b6987e02013-01-04 18:30:43310 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(newtab, "testExtensionApi()",
311 &result));
[email protected]fad73672012-06-15 23:26:06312 EXPECT_TRUE(result);
313}
alexmos1c3da4f2016-11-03 06:06:11314
315// Tests that calling window.open for an extension URL from a non-HTTP or HTTPS
316// URL on a new tab cannot access non-web-accessible resources.
317IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest,
318 WindowOpenInaccessibleResourceFromDataURL) {
alexmoscf5145bb2016-11-09 23:19:45319 const extensions::Extension* extension = LoadExtension(
320 test_data_dir_.AppendASCII("uitest").AppendASCII("window_open"));
321 ASSERT_TRUE(extension);
alexmos1c3da4f2016-11-03 06:06:11322
323 ui_test_utils::NavigateToURL(browser(), GURL("data:text/html,foo"));
324
325 // test.html is not web-accessible and should not be loaded.
alexmoscf5145bb2016-11-09 23:19:45326 GURL extension_url(extension->GetResourceURL("test.html"));
alexmos1c3da4f2016-11-03 06:06:11327 content::WindowedNotificationObserver windowed_observer(
328 content::NOTIFICATION_LOAD_STOP,
329 content::NotificationService::AllSources());
330 ASSERT_TRUE(content::ExecuteScript(
331 browser()->tab_strip_model()->GetActiveWebContents(),
332 "window.open('" + extension_url.spec() + "');"));
333 windowed_observer.Wait();
334 content::NavigationController* controller =
335 content::Source<content::NavigationController>(windowed_observer.source())
336 .ptr();
337 content::WebContents* newtab = controller->GetWebContents();
338 ASSERT_TRUE(newtab);
339
Lukasz Anforowiczfc417492019-09-25 19:53:28340 EXPECT_EQ(content::PAGE_TYPE_ERROR,
341 newtab->GetController().GetLastCommittedEntry()->GetPageType());
342 EXPECT_EQ(extension_url, newtab->GetMainFrame()->GetLastCommittedURL());
alexmos1c3da4f2016-11-03 06:06:11343 EXPECT_FALSE(newtab->GetMainFrame()->GetSiteInstance()->GetSiteURL().SchemeIs(
344 extensions::kExtensionScheme));
alexmos1c3da4f2016-11-03 06:06:11345}
alexmoscf5145bb2016-11-09 23:19:45346
347// Test that navigating to an extension URL is allowed on chrome:// and
348// chrome-search:// pages, even for URLs that are not web-accessible.
349// See https://crbug.com/662602.
350IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest,
Alex Moshchuk7c7368f2018-04-25 00:49:25351 NavigateToInaccessibleResourceFromChromeURL) {
alexmoscf5145bb2016-11-09 23:19:45352 // Mint an extension URL which is not web-accessible.
353 const extensions::Extension* extension = LoadExtension(
354 test_data_dir_.AppendASCII("uitest").AppendASCII("window_open"));
355 ASSERT_TRUE(extension);
356 GURL extension_url(extension->GetResourceURL("test.html"));
357
358 content::WebContents* tab =
359 browser()->tab_strip_model()->GetActiveWebContents();
360
361 // Navigate to the non-web-accessible URL from chrome:// and
362 // chrome-search:// pages. Verify that the page loads correctly.
363 GURL history_url(chrome::kChromeUIHistoryURL);
364 GURL ntp_url(chrome::kChromeSearchLocalNtpUrl);
365 ASSERT_TRUE(history_url.SchemeIs(content::kChromeUIScheme));
366 ASSERT_TRUE(ntp_url.SchemeIs(chrome::kChromeSearchScheme));
367 GURL start_urls[] = {history_url, ntp_url};
Avi Drissman5f0fb8c2018-12-25 23:20:49368 for (size_t i = 0; i < base::size(start_urls); i++) {
alexmoscf5145bb2016-11-09 23:19:45369 ui_test_utils::NavigateToURL(browser(), start_urls[i]);
370 EXPECT_EQ(start_urls[i], tab->GetMainFrame()->GetLastCommittedURL());
371
372 content::TestNavigationObserver observer(tab);
373 ASSERT_TRUE(content::ExecuteScript(
374 tab, "location.href = '" + extension_url.spec() + "';"));
375 observer.Wait();
376 EXPECT_EQ(extension_url, tab->GetMainFrame()->GetLastCommittedURL());
377 std::string result;
378 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
379 tab, "domAutomationController.send(document.body.innerText)", &result));
380 EXPECT_EQ("HOWDIE!!!", result);
381 }
382}
Ivan Sandrkc30341b2017-11-09 19:13:26383
384#if defined(OS_CHROMEOS)
385
386namespace {
387
Ivan Sandrkc9eb2962017-11-16 15:35:10388aura::Window* GetCurrentWindow() {
Ivan Sandrkc30341b2017-11-09 19:13:26389 extensions::WindowController* controller = nullptr;
390 for (auto* iter :
391 extensions::WindowControllerList::GetInstance()->windows()) {
392 if (iter->window()->IsActive()) {
393 controller = iter;
394 break;
395 }
396 }
397 EXPECT_TRUE(controller);
Ivan Sandrkc9eb2962017-11-16 15:35:10398 return controller->window()->GetNativeWindow();
399}
400
Xiyuan Xia4f992f22019-06-05 15:33:46401ash::WindowPinType GetCurrentWindowPinType() {
402 ash::WindowPinType type =
Ivan Sandrkc9eb2962017-11-16 15:35:10403 GetCurrentWindow()->GetProperty(ash::kWindowPinTypeKey);
Ivan Sandrkc30341b2017-11-09 19:13:26404 return type;
405}
406
Xiyuan Xia4f992f22019-06-05 15:33:46407void SetCurrentWindowPinType(ash::WindowPinType type) {
Ivan Sandrkc9eb2962017-11-16 15:35:10408 GetCurrentWindow()->SetProperty(ash::kWindowPinTypeKey, type);
409}
410
Ivan Sandrkc30341b2017-11-09 19:13:26411} // namespace
412
413IN_PROC_BROWSER_TEST_F(WindowOpenApiTest, OpenLockedFullscreenWindow) {
414 ASSERT_TRUE(RunExtensionTestWithArg("locked_fullscreen/with_permission",
415 "openLockedFullscreenWindow"))
416 << message_;
417
418 // Make sure the newly created window is "trusted pinned" (which means that
419 // it's in locked fullscreen mode).
Xiyuan Xia4f992f22019-06-05 15:33:46420 EXPECT_EQ(ash::WindowPinType::kTrustedPinned, GetCurrentWindowPinType());
Ivan Sandrkc30341b2017-11-09 19:13:26421}
422
423IN_PROC_BROWSER_TEST_F(WindowOpenApiTest, UpdateWindowToLockedFullscreen) {
424 ASSERT_TRUE(RunExtensionTestWithArg("locked_fullscreen/with_permission",
425 "updateWindowToLockedFullscreen"))
426 << message_;
427
428 // Make sure the current window is put into the "trusted pinned" state.
Xiyuan Xia4f992f22019-06-05 15:33:46429 EXPECT_EQ(ash::WindowPinType::kTrustedPinned, GetCurrentWindowPinType());
Ivan Sandrkc30341b2017-11-09 19:13:26430}
431
Ivan Sandrkc9eb2962017-11-16 15:35:10432IN_PROC_BROWSER_TEST_F(WindowOpenApiTest, RemoveLockedFullscreenFromWindow) {
Ivan Sandrk9669d0e2017-12-15 23:50:20433 // After locking the window, do a LockedFullscreenStateChanged so the
434 // command_controller state catches up as well.
Xiyuan Xia4f992f22019-06-05 15:33:46435 SetCurrentWindowPinType(ash::WindowPinType::kTrustedPinned);
Ivan Sandrk9669d0e2017-12-15 23:50:20436 browser()->command_controller()->LockedFullscreenStateChanged();
Ivan Sandrkc9eb2962017-11-16 15:35:10437
438 ASSERT_TRUE(RunExtensionTestWithArg("locked_fullscreen/with_permission",
439 "removeLockedFullscreenFromWindow"))
440 << message_;
441
442 // Make sure the current window is removed from locked-fullscreen state.
Xiyuan Xia4f992f22019-06-05 15:33:46443 EXPECT_EQ(ash::WindowPinType::kNone, GetCurrentWindowPinType());
Ivan Sandrkc9eb2962017-11-16 15:35:10444}
445
Ivan Sandrk9669d0e2017-12-15 23:50:20446// Make sure that commands disabling code works in locked fullscreen mode.
447IN_PROC_BROWSER_TEST_F(WindowOpenApiTest, VerifyCommandsInLockedFullscreen) {
448 // IDC_EXIT is always enabled in regular mode so it's a perfect candidate for
449 // testing.
450 EXPECT_TRUE(browser()->command_controller()->IsCommandEnabled(IDC_EXIT));
451 ASSERT_TRUE(RunExtensionTestWithArg("locked_fullscreen/with_permission",
452 "updateWindowToLockedFullscreen"))
453 << message_;
454
Ivan Sandrk6ac9c9a2018-12-14 14:15:07455 // IDC_EXIT should always be disabled in locked fullscreen.
Ivan Sandrk9669d0e2017-12-15 23:50:20456 EXPECT_FALSE(browser()->command_controller()->IsCommandEnabled(IDC_EXIT));
457
Ivan Sandrk6ac9c9a2018-12-14 14:15:07458 // Some other disabled commands.
459 EXPECT_FALSE(browser()->command_controller()->IsCommandEnabled(IDC_FIND));
460 EXPECT_FALSE(
461 browser()->command_controller()->IsCommandEnabled(IDC_ZOOM_PLUS));
462
Ivan Sandrk9669d0e2017-12-15 23:50:20463 // Verify some whitelisted commands.
464 EXPECT_TRUE(browser()->command_controller()->IsCommandEnabled(IDC_COPY));
Ivan Sandrk6ac9c9a2018-12-14 14:15:07465 EXPECT_TRUE(browser()->command_controller()->IsCommandEnabled(IDC_PASTE));
Ivan Sandrk9669d0e2017-12-15 23:50:20466}
467
Ivan Sandrkc30341b2017-11-09 19:13:26468IN_PROC_BROWSER_TEST_F(WindowOpenApiTest,
469 OpenLockedFullscreenWindowWithoutPermission) {
470 ASSERT_TRUE(RunExtensionTestWithArg("locked_fullscreen/without_permission",
471 "openLockedFullscreenWindow"))
472 << message_;
473
474 // Make sure no new windows get created (so only the one created by default
475 // exists) since the call to chrome.windows.create fails on the javascript
476 // side.
477 EXPECT_EQ(1u,
478 extensions::WindowControllerList::GetInstance()->windows().size());
479}
480
481IN_PROC_BROWSER_TEST_F(WindowOpenApiTest,
482 UpdateWindowToLockedFullscreenWithoutPermission) {
483 ASSERT_TRUE(RunExtensionTestWithArg("locked_fullscreen/without_permission",
484 "updateWindowToLockedFullscreen"))
485 << message_;
486
487 // chrome.windows.update call fails since this extension doesn't have the
488 // correct permission and hence the current window has NONE as WindowPinType.
Xiyuan Xia4f992f22019-06-05 15:33:46489 EXPECT_EQ(ash::WindowPinType::kNone, GetCurrentWindowPinType());
Ivan Sandrkc9eb2962017-11-16 15:35:10490}
491
492IN_PROC_BROWSER_TEST_F(WindowOpenApiTest,
493 RemoveLockedFullscreenFromWindowWithoutPermission) {
Xiyuan Xia4f992f22019-06-05 15:33:46494 SetCurrentWindowPinType(ash::WindowPinType::kTrustedPinned);
Ivan Sandrk9669d0e2017-12-15 23:50:20495 browser()->command_controller()->LockedFullscreenStateChanged();
Ivan Sandrkc9eb2962017-11-16 15:35:10496
497 ASSERT_TRUE(RunExtensionTestWithArg("locked_fullscreen/without_permission",
498 "removeLockedFullscreenFromWindow"))
499 << message_;
500
501 // The current window is still locked-fullscreen.
Xiyuan Xia4f992f22019-06-05 15:33:46502 EXPECT_EQ(ash::WindowPinType::kTrustedPinned, GetCurrentWindowPinType());
Ivan Sandrkc30341b2017-11-09 19:13:26503}
504#endif // defined(OS_CHROMEOS)
505
506#if !defined(OS_CHROMEOS)
507// Loading an extension requiring the 'lockWindowFullscreenPrivate' permission
508// on non Chrome OS platforms should always fail since the API is available only
509// on Chrome OS.
510IN_PROC_BROWSER_TEST_F(WindowOpenApiTest,
511 OpenLockedFullscreenWindowNonChromeOS) {
512 const extensions::Extension* extension = LoadExtensionWithFlags(
513 test_data_dir_.AppendASCII("locked_fullscreen/with_permission"),
David Bertoni4ec9371a2019-12-26 17:14:53514 kFlagIgnoreManifestWarnings);
Ivan Sandrkc30341b2017-11-09 19:13:26515 ASSERT_TRUE(extension);
516 EXPECT_EQ(1u, extension->install_warnings().size());
517 EXPECT_EQ(std::string("'lockWindowFullscreenPrivate' "
518 "is not allowed for specified platform."),
519 extension->install_warnings().front().message);
520}
521#endif
Devlin Cronin836f545d2018-05-09 00:25:05522
523} // namespace extensions