[email protected] | 35abc33 | 2012-02-24 23:48:38 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 32b7c49 | 2010-09-01 04:15:00 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 5 | #include <stddef.h> |
| 6 | |
[email protected] | 7c6ef35 | 2013-09-26 07:23:14 | [diff] [blame] | 7 | #include "base/path_service.h" |
[email protected] | 46acbf1 | 2013-06-10 18:43:42 | [diff] [blame] | 8 | #include "base/strings/stringprintf.h" |
alexmos | 1c3da4f | 2016-11-03 06:06:11 | [diff] [blame] | 9 | #include "base/test/histogram_tester.h" |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 10 | #include "build/build_config.h" |
[email protected] | 32b7c49 | 2010-09-01 04:15:00 | [diff] [blame] | 11 | #include "chrome/browser/extensions/extension_apitest.h" |
[email protected] | 01996a59 | 2013-09-13 06:55:04 | [diff] [blame] | 12 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 2ad4a90 | 2010-11-17 06:05:13 | [diff] [blame] | 13 | #include "chrome/browser/ui/browser.h" |
[email protected] | d874814 | 2012-05-16 21:13:43 | [diff] [blame] | 14 | #include "chrome/browser/ui/browser_finder.h" |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 15 | #include "chrome/browser/ui/browser_list.h" |
[email protected] | e044887 | 2013-01-11 19:35:02 | [diff] [blame] | 16 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
[email protected] | 7c6ef35 | 2013-09-26 07:23:14 | [diff] [blame] | 17 | #include "chrome/common/chrome_paths.h" |
alexmos | cf5145bb | 2016-11-09 23:19:45 | [diff] [blame] | 18 | #include "chrome/common/url_constants.h" |
[email protected] | af44e7fb | 2011-07-29 18:32:32 | [diff] [blame] | 19 | #include "chrome/test/base/ui_test_utils.h" |
nick | 6cfe5c7 | 2017-05-22 22:00:42 | [diff] [blame] | 20 | #include "content/public/browser/navigation_entry.h" |
alexmos | 1c3da4f | 2016-11-03 06:06:11 | [diff] [blame] | 21 | #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] | 1dd66b7 | 2012-09-19 20:41:48 | [diff] [blame] | 24 | #include "content/public/browser/render_process_host.h" |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 25 | #include "content/public/browser/web_contents.h" |
nick | 6cfe5c7 | 2017-05-22 22:00:42 | [diff] [blame] | 26 | #include "content/public/common/browser_side_navigation_policy.h" |
[email protected] | 1dd66b7 | 2012-09-19 20:41:48 | [diff] [blame] | 27 | #include "content/public/common/result_codes.h" |
[email protected] | 99bee64 | 2014-05-31 22:36:43 | [diff] [blame] | 28 | #include "content/public/common/url_constants.h" |
[email protected] | 7d478cb | 2012-07-24 17:19:42 | [diff] [blame] | 29 | #include "content/public/test/browser_test_utils.h" |
alexmos | cf5145bb | 2016-11-09 23:19:45 | [diff] [blame] | 30 | #include "content/public/test/test_navigation_observer.h" |
[email protected] | 22401dc | 2014-03-21 01:38:57 | [diff] [blame] | 31 | #include "extensions/browser/extension_host.h" |
[email protected] | 98b6d94 | 2013-11-10 00:34:07 | [diff] [blame] | 32 | #include "extensions/browser/process_manager.h" |
[email protected] | 99bee64 | 2014-05-31 22:36:43 | [diff] [blame] | 33 | #include "extensions/common/constants.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 34 | #include "extensions/common/extension.h" |
lfg | 910f2f9 | 2014-09-19 05:31:09 | [diff] [blame] | 35 | #include "extensions/test/extension_test_message_listener.h" |
yoz | e8dc2f1 | 2014-09-09 23:16:32 | [diff] [blame] | 36 | #include "extensions/test/result_catcher.h" |
[email protected] | f2cb3cf | 2013-03-21 01:40:53 | [diff] [blame] | 37 | #include "net/dns/mock_host_resolver.h" |
[email protected] | 7c6ef35 | 2013-09-26 07:23:14 | [diff] [blame] | 38 | #include "net/test/embedded_test_server/embedded_test_server.h" |
[email protected] | bc44f414 | 2013-02-12 06:15:56 | [diff] [blame] | 39 | #include "testing/gtest/include/gtest/gtest.h" |
Ivan Sandrk | c30341b | 2017-11-09 19:13:26 | [diff] [blame] | 40 | #include "ui/base/base_window.h" |
| 41 | |
| 42 | #if defined(OS_CHROMEOS) |
| 43 | #include "ash/public/cpp/window_properties.h" |
| 44 | #include "ash/public/interfaces/window_pin_type.mojom.h" |
Ivan Sandrk | 9669d0e | 2017-12-15 23:50:20 | [diff] [blame^] | 45 | #include "chrome/app/chrome_command_ids.h" |
Ivan Sandrk | c30341b | 2017-11-09 19:13:26 | [diff] [blame] | 46 | #include "chrome/browser/extensions/window_controller.h" |
| 47 | #include "chrome/browser/extensions/window_controller_list.h" |
Ivan Sandrk | 9669d0e | 2017-12-15 23:50:20 | [diff] [blame^] | 48 | #include "chrome/browser/ui/browser_command_controller.h" |
Ivan Sandrk | c30341b | 2017-11-09 19:13:26 | [diff] [blame] | 49 | #include "ui/aura/window.h" |
| 50 | #endif |
[email protected] | 32b7c49 | 2010-09-01 04:15:00 | [diff] [blame] | 51 | |
[email protected] | e5d549d | 2011-12-28 01:29:20 | [diff] [blame] | 52 | using content::OpenURLParams; |
| 53 | using content::Referrer; |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 54 | using content::WebContents; |
[email protected] | e5d549d | 2011-12-28 01:29:20 | [diff] [blame] | 55 | |
Ivan Sandrk | c30341b | 2017-11-09 19:13:26 | [diff] [blame] | 56 | namespace aura { |
| 57 | class Window; |
| 58 | } |
| 59 | |
jam | 1a5b558 | 2017-05-01 16:50:10 | [diff] [blame] | 60 | class WindowOpenApiTest : public ExtensionApiTest { |
| 61 | void SetUpOnMainThread() override { |
| 62 | ExtensionApiTest::SetUpOnMainThread(); |
| 63 | host_resolver()->AddRule("*", "127.0.0.1"); |
| 64 | } |
| 65 | }; |
| 66 | |
rob | f8cd8ce | 2015-02-21 12:18:04 | [diff] [blame] | 67 | // The test uses the chrome.browserAction.openPopup API, which requires that the |
| 68 | // window can automatically be activated. |
| 69 | // See comments at BrowserActionInteractiveTest::ShouldRunPopupTest |
johnme | 7e30b51 | 2015-04-16 14:57:35 | [diff] [blame] | 70 | // Fails flakily on all platforms. https://crbug.com/477691 |
jam | 1a5b558 | 2017-05-01 16:50:10 | [diff] [blame] | 71 | IN_PROC_BROWSER_TEST_F(WindowOpenApiTest, DISABLED_WindowOpen) { |
yoz | e8dc2f1 | 2014-09-09 23:16:32 | [diff] [blame] | 72 | extensions::ResultCatcher catcher; |
[email protected] | 32b7c49 | 2010-09-01 04:15:00 | [diff] [blame] | 73 | ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_ |
| 74 | .AppendASCII("window_open").AppendASCII("spanning"))); |
| 75 | EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 76 | } |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 77 | |
[email protected] | f1c102b | 2013-02-15 07:44:12 | [diff] [blame] | 78 | bool WaitForTabsAndPopups(Browser* browser, |
[email protected] | f9e82d9 | 2011-10-29 00:50:45 | [diff] [blame] | 79 | int num_tabs, |
dimich | 566d364 | 2016-08-23 16:39:06 | [diff] [blame] | 80 | int num_popups) { |
[email protected] | 58b349b | 2011-11-23 02:07:15 | [diff] [blame] | 81 | SCOPED_TRACE( |
dimich | 566d364 | 2016-08-23 16:39:06 | [diff] [blame] | 82 | base::StringPrintf("WaitForTabsAndPopups tabs:%d, popups:%d", |
| 83 | num_tabs, num_popups)); |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 84 | // We start with one tab and one browser already open. |
| 85 | ++num_tabs; |
[email protected] | f6ffa7d | 2012-08-17 17:27:08 | [diff] [blame] | 86 | size_t num_browsers = static_cast<size_t>(num_popups) + 1; |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 87 | |
[email protected] | f1c102b | 2013-02-15 07:44:12 | [diff] [blame] | 88 | const base::TimeDelta kWaitTime = base::TimeDelta::FromSeconds(10); |
[email protected] | 0a5bdc652 | 2011-06-08 19:04:49 | [diff] [blame] | 89 | base::TimeTicks end_time = base::TimeTicks::Now() + kWaitTime; |
| 90 | while (base::TimeTicks::Now() < end_time) { |
scottmg | 34c5dd88 | 2016-02-03 05:21:54 | [diff] [blame] | 91 | if (chrome::GetBrowserCount(browser->profile()) == num_browsers && |
dimich | 566d364 | 2016-08-23 16:39:06 | [diff] [blame] | 92 | browser->tab_strip_model()->count() == num_tabs) |
[email protected] | 0a5bdc652 | 2011-06-08 19:04:49 | [diff] [blame] | 93 | break; |
| 94 | |
[email protected] | b8deecd | 2012-07-30 21:09:44 | [diff] [blame] | 95 | content::RunAllPendingInMessageLoop(); |
[email protected] | 0a5bdc652 | 2011-06-08 19:04:49 | [diff] [blame] | 96 | } |
| 97 | |
scottmg | 34c5dd88 | 2016-02-03 05:21:54 | [diff] [blame] | 98 | EXPECT_EQ(num_browsers, chrome::GetBrowserCount(browser->profile())); |
[email protected] | e044887 | 2013-01-11 19:35:02 | [diff] [blame] | 99 | EXPECT_EQ(num_tabs, browser->tab_strip_model()->count()); |
[email protected] | 0a5bdc652 | 2011-06-08 19:04:49 | [diff] [blame] | 100 | |
[email protected] | f9e82d9 | 2011-10-29 00:50:45 | [diff] [blame] | 101 | int num_popups_seen = 0; |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 102 | for (auto* b : *BrowserList::GetInstance()) { |
| 103 | if (b == browser) |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 104 | continue; |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 105 | |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 106 | EXPECT_TRUE(b->is_type_popup()); |
[email protected] | f6ffa7d | 2012-08-17 17:27:08 | [diff] [blame] | 107 | ++num_popups_seen; |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 108 | } |
[email protected] | f9e82d9 | 2011-10-29 00:50:45 | [diff] [blame] | 109 | EXPECT_EQ(num_popups, num_popups_seen); |
[email protected] | f1c102b | 2013-02-15 07:44:12 | [diff] [blame] | 110 | |
scottmg | 34c5dd88 | 2016-02-03 05:21:54 | [diff] [blame] | 111 | return ((num_browsers == chrome::GetBrowserCount(browser->profile())) && |
[email protected] | f1c102b | 2013-02-15 07:44:12 | [diff] [blame] | 112 | (num_tabs == browser->tab_strip_model()->count()) && |
[email protected] | f1c102b | 2013-02-15 07:44:12 | [diff] [blame] | 113 | (num_popups == num_popups_seen)); |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 114 | } |
| 115 | |
jam | 1a5b558 | 2017-05-01 16:50:10 | [diff] [blame] | 116 | IN_PROC_BROWSER_TEST_F(WindowOpenApiTest, BrowserIsApp) { |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 117 | ASSERT_TRUE(StartEmbeddedTestServer()); |
[email protected] | f112b0f | 2011-05-26 01:53:52 | [diff] [blame] | 118 | ASSERT_TRUE(LoadExtension( |
| 119 | test_data_dir_.AppendASCII("window_open").AppendASCII("browser_is_app"))); |
| 120 | |
dimich | 566d364 | 2016-08-23 16:39:06 | [diff] [blame] | 121 | EXPECT_TRUE(WaitForTabsAndPopups(browser(), 0, 2)); |
[email protected] | f112b0f | 2011-05-26 01:53:52 | [diff] [blame] | 122 | |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 123 | for (auto* b : *BrowserList::GetInstance()) { |
| 124 | if (b == browser()) |
| 125 | ASSERT_FALSE(b->is_app()); |
[email protected] | f112b0f | 2011-05-26 01:53:52 | [diff] [blame] | 126 | else |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 127 | ASSERT_TRUE(b->is_app()); |
[email protected] | f112b0f | 2011-05-26 01:53:52 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
jam | 1a5b558 | 2017-05-01 16:50:10 | [diff] [blame] | 131 | IN_PROC_BROWSER_TEST_F(WindowOpenApiTest, WindowOpenPopupDefault) { |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 132 | ASSERT_TRUE(StartEmbeddedTestServer()); |
[email protected] | 0a5bdc652 | 2011-06-08 19:04:49 | [diff] [blame] | 133 | ASSERT_TRUE(LoadExtension( |
| 134 | test_data_dir_.AppendASCII("window_open").AppendASCII("popup"))); |
| 135 | |
| 136 | const int num_tabs = 1; |
| 137 | const int num_popups = 0; |
dimich | 566d364 | 2016-08-23 16:39:06 | [diff] [blame] | 138 | EXPECT_TRUE(WaitForTabsAndPopups(browser(), num_tabs, num_popups)); |
[email protected] | 0a5bdc652 | 2011-06-08 19:04:49 | [diff] [blame] | 139 | } |
| 140 | |
jam | 1a5b558 | 2017-05-01 16:50:10 | [diff] [blame] | 141 | IN_PROC_BROWSER_TEST_F(WindowOpenApiTest, WindowOpenPopupIframe) { |
[email protected] | 7c6ef35 | 2013-09-26 07:23:14 | [diff] [blame] | 142 | base::FilePath test_data_dir; |
| 143 | PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir); |
| 144 | embedded_test_server()->ServeFilesFromDirectory(test_data_dir); |
martijn | f9b885b7 | 2016-11-22 22:03:47 | [diff] [blame] | 145 | ASSERT_TRUE(StartEmbeddedTestServer()); |
[email protected] | 7c6ef35 | 2013-09-26 07:23:14 | [diff] [blame] | 146 | ASSERT_TRUE(LoadExtension( |
| 147 | test_data_dir_.AppendASCII("window_open").AppendASCII("popup_iframe"))); |
| 148 | |
jochen | c5cfa64 | 2014-09-02 15:30:03 | [diff] [blame] | 149 | const int num_tabs = 1; |
| 150 | const int num_popups = 0; |
dimich | 566d364 | 2016-08-23 16:39:06 | [diff] [blame] | 151 | EXPECT_TRUE(WaitForTabsAndPopups(browser(), num_tabs, num_popups)); |
[email protected] | 7c6ef35 | 2013-09-26 07:23:14 | [diff] [blame] | 152 | } |
| 153 | |
jam | 1a5b558 | 2017-05-01 16:50:10 | [diff] [blame] | 154 | IN_PROC_BROWSER_TEST_F(WindowOpenApiTest, WindowOpenPopupLarge) { |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 155 | ASSERT_TRUE(StartEmbeddedTestServer()); |
[email protected] | 0a5bdc652 | 2011-06-08 19:04:49 | [diff] [blame] | 156 | ASSERT_TRUE(LoadExtension( |
| 157 | test_data_dir_.AppendASCII("window_open").AppendASCII("popup_large"))); |
| 158 | |
[email protected] | 0a5bdc652 | 2011-06-08 19:04:49 | [diff] [blame] | 159 | // On other systems this should open a new popup window. |
| 160 | const int num_tabs = 0; |
| 161 | const int num_popups = 1; |
dimich | 566d364 | 2016-08-23 16:39:06 | [diff] [blame] | 162 | EXPECT_TRUE(WaitForTabsAndPopups(browser(), num_tabs, num_popups)); |
[email protected] | 0a5bdc652 | 2011-06-08 19:04:49 | [diff] [blame] | 163 | } |
| 164 | |
jam | 1a5b558 | 2017-05-01 16:50:10 | [diff] [blame] | 165 | IN_PROC_BROWSER_TEST_F(WindowOpenApiTest, WindowOpenPopupSmall) { |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 166 | ASSERT_TRUE(StartEmbeddedTestServer()); |
[email protected] | 0a5bdc652 | 2011-06-08 19:04:49 | [diff] [blame] | 167 | ASSERT_TRUE(LoadExtension( |
| 168 | test_data_dir_.AppendASCII("window_open").AppendASCII("popup_small"))); |
| 169 | |
| 170 | // On ChromeOS this should open a new panel (acts like a new popup window). |
| 171 | // On other systems this should open a new popup window. |
| 172 | const int num_tabs = 0; |
| 173 | const int num_popups = 1; |
dimich | 566d364 | 2016-08-23 16:39:06 | [diff] [blame] | 174 | EXPECT_TRUE(WaitForTabsAndPopups(browser(), num_tabs, num_popups)); |
[email protected] | 0a5bdc652 | 2011-06-08 19:04:49 | [diff] [blame] | 175 | } |
| 176 | |
[email protected] | 73fa1fce | 2013-02-21 20:10:05 | [diff] [blame] | 177 | // Disabled on Windows. Often times out or fails: crbug.com/177530 |
| 178 | #if defined(OS_WIN) |
| 179 | #define MAYBE_PopupBlockingExtension DISABLED_PopupBlockingExtension |
| 180 | #else |
| 181 | #define MAYBE_PopupBlockingExtension PopupBlockingExtension |
| 182 | #endif |
jam | 1a5b558 | 2017-05-01 16:50:10 | [diff] [blame] | 183 | IN_PROC_BROWSER_TEST_F(WindowOpenApiTest, MAYBE_PopupBlockingExtension) { |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 184 | ASSERT_TRUE(StartEmbeddedTestServer()); |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 185 | |
| 186 | ASSERT_TRUE(LoadExtension( |
| 187 | test_data_dir_.AppendASCII("window_open").AppendASCII("popup_blocking") |
| 188 | .AppendASCII("extension"))); |
| 189 | |
dimich | 566d364 | 2016-08-23 16:39:06 | [diff] [blame] | 190 | EXPECT_TRUE(WaitForTabsAndPopups(browser(), 5, 3)); |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 191 | } |
| 192 | |
jam | 1a5b558 | 2017-05-01 16:50:10 | [diff] [blame] | 193 | IN_PROC_BROWSER_TEST_F(WindowOpenApiTest, PopupBlockingHostedApp) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 194 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 195 | |
| 196 | ASSERT_TRUE(LoadExtension( |
| 197 | test_data_dir_.AppendASCII("window_open").AppendASCII("popup_blocking") |
| 198 | .AppendASCII("hosted_app"))); |
| 199 | |
[email protected] | fe304887 | 2010-10-18 14:58:59 | [diff] [blame] | 200 | // The app being tested owns the domain a.com . The test URLs we navigate |
| 201 | // to below must be within that domain, so that they fall within the app's |
| 202 | // web extent. |
| 203 | GURL::Replacements replace_host; |
mgiuca | 77752c3 | 2015-02-05 07:31:18 | [diff] [blame] | 204 | replace_host.SetHostStr("a.com"); |
[email protected] | fe304887 | 2010-10-18 14:58:59 | [diff] [blame] | 205 | |
| 206 | const std::string popup_app_contents_path( |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 207 | "/extensions/api_test/window_open/popup_blocking/hosted_app/"); |
[email protected] | fe304887 | 2010-10-18 14:58:59 | [diff] [blame] | 208 | |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 209 | GURL open_tab = embedded_test_server() |
| 210 | ->GetURL(popup_app_contents_path + "open_tab.html") |
| 211 | .ReplaceComponents(replace_host); |
| 212 | GURL open_popup = embedded_test_server() |
| 213 | ->GetURL(popup_app_contents_path + "open_popup.html") |
| 214 | .ReplaceComponents(replace_host); |
[email protected] | fe304887 | 2010-10-18 14:58:59 | [diff] [blame] | 215 | |
nick | 3b04f32 | 2016-08-31 19:29:19 | [diff] [blame] | 216 | browser()->OpenURL(OpenURLParams(open_tab, Referrer(), |
| 217 | WindowOpenDisposition::NEW_FOREGROUND_TAB, |
| 218 | ui::PAGE_TRANSITION_TYPED, false)); |
| 219 | browser()->OpenURL(OpenURLParams(open_popup, Referrer(), |
| 220 | WindowOpenDisposition::NEW_FOREGROUND_TAB, |
| 221 | ui::PAGE_TRANSITION_TYPED, false)); |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 222 | |
dimich | 566d364 | 2016-08-23 16:39:06 | [diff] [blame] | 223 | EXPECT_TRUE(WaitForTabsAndPopups(browser(), 3, 1)); |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 224 | } |
[email protected] | d4db6c70 | 2011-03-28 21:49:14 | [diff] [blame] | 225 | |
jam | 1a5b558 | 2017-05-01 16:50:10 | [diff] [blame] | 226 | IN_PROC_BROWSER_TEST_F(WindowOpenApiTest, WindowArgumentsOverflow) { |
[email protected] | e7f9056 | 2011-05-23 21:38:24 | [diff] [blame] | 227 | ASSERT_TRUE(RunExtensionTest("window_open/argument_overflow")) << message_; |
| 228 | } |
| 229 | |
jam | 1a5b558 | 2017-05-01 16:50:10 | [diff] [blame] | 230 | IN_PROC_BROWSER_TEST_F(WindowOpenApiTest, DISABLED_WindowOpener) { |
[email protected] | 54edcea | 2011-07-27 01:56:38 | [diff] [blame] | 231 | ASSERT_TRUE(RunExtensionTest("window_open/opener")) << message_; |
| 232 | } |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 233 | |
[email protected] | fa7c160 | 2013-04-03 23:00:20 | [diff] [blame] | 234 | #if defined(OS_MACOSX) |
| 235 | // Extension popup windows are incorrectly sized on OSX, crbug.com/225601 |
| 236 | #define MAYBE_WindowOpenSized DISABLED_WindowOpenSized |
| 237 | #else |
| 238 | #define MAYBE_WindowOpenSized WindowOpenSized |
| 239 | #endif |
| 240 | // Ensure that the width and height properties of a window opened with |
| 241 | // chrome.windows.create match the creation parameters. See crbug.com/173831. |
jam | 1a5b558 | 2017-05-01 16:50:10 | [diff] [blame] | 242 | IN_PROC_BROWSER_TEST_F(WindowOpenApiTest, MAYBE_WindowOpenSized) { |
[email protected] | fa7c160 | 2013-04-03 23:00:20 | [diff] [blame] | 243 | ASSERT_TRUE(RunExtensionTest("window_open/window_size")) << message_; |
dimich | 566d364 | 2016-08-23 16:39:06 | [diff] [blame] | 244 | EXPECT_TRUE(WaitForTabsAndPopups(browser(), 0, 1)); |
[email protected] | fa7c160 | 2013-04-03 23:00:20 | [diff] [blame] | 245 | } |
| 246 | |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 247 | // Tests that an extension page can call window.open to an extension URL and |
| 248 | // the new window has extension privileges. |
| 249 | IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, WindowOpenExtension) { |
| 250 | ASSERT_TRUE(LoadExtension( |
| 251 | test_data_dir_.AppendASCII("uitest").AppendASCII("window_open"))); |
| 252 | |
[email protected] | 99bee64 | 2014-05-31 22:36:43 | [diff] [blame] | 253 | GURL start_url(std::string(extensions::kExtensionScheme) + |
[email protected] | fb4fe095 | 2014-06-05 09:44:24 | [diff] [blame] | 254 | url::kStandardSchemeSeparator + |
[email protected] | 99bee64 | 2014-05-31 22:36:43 | [diff] [blame] | 255 | last_loaded_extension_id() + "/test.html"); |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 256 | ui_test_utils::NavigateToURL(browser(), start_url); |
[email protected] | f3e73f6 | 2012-10-20 05:35:16 | [diff] [blame] | 257 | WebContents* newtab = NULL; |
[email protected] | e044887 | 2013-01-11 19:35:02 | [diff] [blame] | 258 | ASSERT_NO_FATAL_FAILURE( |
| 259 | OpenWindow(browser()->tab_strip_model()->GetActiveWebContents(), |
Devlin Cronin | f1afce25 | 2017-08-19 01:21:48 | [diff] [blame] | 260 | start_url.Resolve("newtab.html"), true, true, &newtab)); |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 261 | |
| 262 | bool result = false; |
[email protected] | b6987e0 | 2013-01-04 18:30:43 | [diff] [blame] | 263 | ASSERT_TRUE(content::ExecuteScriptAndExtractBool(newtab, "testExtensionApi()", |
| 264 | &result)); |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 265 | EXPECT_TRUE(result); |
| 266 | } |
| 267 | |
| 268 | // Tests that if an extension page calls window.open to an invalid extension |
| 269 | // URL, the browser doesn't crash. |
| 270 | IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, WindowOpenInvalidExtension) { |
nick | 6cfe5c7 | 2017-05-22 22:00:42 | [diff] [blame] | 271 | const extensions::Extension* extension = LoadExtension( |
| 272 | test_data_dir_.AppendASCII("uitest").AppendASCII("window_open")); |
| 273 | ASSERT_TRUE(extension); |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 274 | |
nick | 6cfe5c7 | 2017-05-22 22:00:42 | [diff] [blame] | 275 | GURL start_url = extension->GetResourceURL("/test.html"); |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 276 | ui_test_utils::NavigateToURL(browser(), start_url); |
nick | 6cfe5c7 | 2017-05-22 22:00:42 | [diff] [blame] | 277 | WebContents* newtab = nullptr; |
Devlin Cronin | f1afce25 | 2017-08-19 01:21:48 | [diff] [blame] | 278 | bool new_page_in_same_process = true; |
| 279 | bool expect_success = false; |
nick | 6cfe5c7 | 2017-05-22 22:00:42 | [diff] [blame] | 280 | ASSERT_NO_FATAL_FAILURE(OpenWindow( |
| 281 | browser()->tab_strip_model()->GetActiveWebContents(), |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 282 | GURL("chrome-extension://thisissurelynotavalidextensionid/newtab.html"), |
Devlin Cronin | f1afce25 | 2017-08-19 01:21:48 | [diff] [blame] | 283 | new_page_in_same_process, expect_success, &newtab)); |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 284 | |
Devlin Cronin | f1afce25 | 2017-08-19 01:21:48 | [diff] [blame] | 285 | // This is expected to redirect to about:blank. |
| 286 | EXPECT_EQ(GURL(url::kAboutBlankURL), newtab->GetLastCommittedURL()); |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | // Tests that calling window.open from the newtab page to an extension URL |
| 290 | // gives the new window extension privileges - even though the opening page |
| 291 | // does not have extension privileges, we break the script connection, so |
| 292 | // there is no privilege leak. |
| 293 | IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, WindowOpenNoPrivileges) { |
| 294 | ASSERT_TRUE(LoadExtension( |
| 295 | test_data_dir_.AppendASCII("uitest").AppendASCII("window_open"))); |
| 296 | |
| 297 | ui_test_utils::NavigateToURL(browser(), GURL("about:blank")); |
[email protected] | f3e73f6 | 2012-10-20 05:35:16 | [diff] [blame] | 298 | WebContents* newtab = NULL; |
[email protected] | e044887 | 2013-01-11 19:35:02 | [diff] [blame] | 299 | ASSERT_NO_FATAL_FAILURE( |
| 300 | OpenWindow(browser()->tab_strip_model()->GetActiveWebContents(), |
[email protected] | 99bee64 | 2014-05-31 22:36:43 | [diff] [blame] | 301 | GURL(std::string(extensions::kExtensionScheme) + |
Devlin Cronin | f1afce25 | 2017-08-19 01:21:48 | [diff] [blame] | 302 | url::kStandardSchemeSeparator + |
| 303 | last_loaded_extension_id() + "/newtab.html"), |
| 304 | false, true, &newtab)); |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 305 | |
| 306 | // Extension API should succeed. |
| 307 | bool result = false; |
[email protected] | b6987e0 | 2013-01-04 18:30:43 | [diff] [blame] | 308 | ASSERT_TRUE(content::ExecuteScriptAndExtractBool(newtab, "testExtensionApi()", |
| 309 | &result)); |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 310 | EXPECT_TRUE(result); |
| 311 | } |
alexmos | 1c3da4f | 2016-11-03 06:06:11 | [diff] [blame] | 312 | |
| 313 | // Tests that calling window.open for an extension URL from a non-HTTP or HTTPS |
| 314 | // URL on a new tab cannot access non-web-accessible resources. |
| 315 | IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, |
| 316 | WindowOpenInaccessibleResourceFromDataURL) { |
| 317 | base::HistogramTester uma; |
alexmos | cf5145bb | 2016-11-09 23:19:45 | [diff] [blame] | 318 | const extensions::Extension* extension = LoadExtension( |
| 319 | test_data_dir_.AppendASCII("uitest").AppendASCII("window_open")); |
| 320 | ASSERT_TRUE(extension); |
alexmos | 1c3da4f | 2016-11-03 06:06:11 | [diff] [blame] | 321 | |
| 322 | ui_test_utils::NavigateToURL(browser(), GURL("data:text/html,foo")); |
| 323 | |
| 324 | // test.html is not web-accessible and should not be loaded. |
alexmos | cf5145bb | 2016-11-09 23:19:45 | [diff] [blame] | 325 | GURL extension_url(extension->GetResourceURL("test.html")); |
alexmos | 1c3da4f | 2016-11-03 06:06:11 | [diff] [blame] | 326 | content::WindowedNotificationObserver windowed_observer( |
| 327 | content::NOTIFICATION_LOAD_STOP, |
| 328 | content::NotificationService::AllSources()); |
| 329 | ASSERT_TRUE(content::ExecuteScript( |
| 330 | browser()->tab_strip_model()->GetActiveWebContents(), |
| 331 | "window.open('" + extension_url.spec() + "');")); |
| 332 | windowed_observer.Wait(); |
| 333 | content::NavigationController* controller = |
| 334 | content::Source<content::NavigationController>(windowed_observer.source()) |
| 335 | .ptr(); |
| 336 | content::WebContents* newtab = controller->GetWebContents(); |
| 337 | ASSERT_TRUE(newtab); |
| 338 | |
| 339 | EXPECT_NE(extension_url, newtab->GetMainFrame()->GetLastCommittedURL()); |
| 340 | EXPECT_FALSE(newtab->GetMainFrame()->GetSiteInstance()->GetSiteURL().SchemeIs( |
| 341 | extensions::kExtensionScheme)); |
| 342 | |
| 343 | // Verify that the blocking was recorded correctly in UMA. |
| 344 | uma.ExpectUniqueSample("Extensions.ShouldAllowOpenURL.Failure", |
| 345 | 2, /* FAILURE_SCHEME_NOT_HTTP_OR_HTTPS_OR_EXTENSION */ |
| 346 | 1); |
alexmos | 7f7bae6 | 2017-04-05 21:15:04 | [diff] [blame] | 347 | uma.ExpectUniqueSample("Extensions.ShouldAllowOpenURL.Failure.Scheme", |
| 348 | 6 /* SCHEME_DATA */, 1); |
alexmos | 1c3da4f | 2016-11-03 06:06:11 | [diff] [blame] | 349 | } |
alexmos | cf5145bb | 2016-11-09 23:19:45 | [diff] [blame] | 350 | |
| 351 | // Test that navigating to an extension URL is allowed on chrome:// and |
| 352 | // chrome-search:// pages, even for URLs that are not web-accessible. |
| 353 | // See https://crbug.com/662602. |
| 354 | IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, |
| 355 | NavigateToInaccessibleResourceFromChromeURL) { |
| 356 | // Mint an extension URL which is not web-accessible. |
| 357 | const extensions::Extension* extension = LoadExtension( |
| 358 | test_data_dir_.AppendASCII("uitest").AppendASCII("window_open")); |
| 359 | ASSERT_TRUE(extension); |
| 360 | GURL extension_url(extension->GetResourceURL("test.html")); |
| 361 | |
| 362 | content::WebContents* tab = |
| 363 | browser()->tab_strip_model()->GetActiveWebContents(); |
| 364 | |
| 365 | // Navigate to the non-web-accessible URL from chrome:// and |
| 366 | // chrome-search:// pages. Verify that the page loads correctly. |
| 367 | GURL history_url(chrome::kChromeUIHistoryURL); |
| 368 | GURL ntp_url(chrome::kChromeSearchLocalNtpUrl); |
| 369 | ASSERT_TRUE(history_url.SchemeIs(content::kChromeUIScheme)); |
| 370 | ASSERT_TRUE(ntp_url.SchemeIs(chrome::kChromeSearchScheme)); |
| 371 | GURL start_urls[] = {history_url, ntp_url}; |
| 372 | for (size_t i = 0; i < arraysize(start_urls); i++) { |
| 373 | ui_test_utils::NavigateToURL(browser(), start_urls[i]); |
| 374 | EXPECT_EQ(start_urls[i], tab->GetMainFrame()->GetLastCommittedURL()); |
| 375 | |
| 376 | content::TestNavigationObserver observer(tab); |
| 377 | ASSERT_TRUE(content::ExecuteScript( |
| 378 | tab, "location.href = '" + extension_url.spec() + "';")); |
| 379 | observer.Wait(); |
| 380 | EXPECT_EQ(extension_url, tab->GetMainFrame()->GetLastCommittedURL()); |
| 381 | std::string result; |
| 382 | ASSERT_TRUE(content::ExecuteScriptAndExtractString( |
| 383 | tab, "domAutomationController.send(document.body.innerText)", &result)); |
| 384 | EXPECT_EQ("HOWDIE!!!", result); |
| 385 | } |
| 386 | } |
Ivan Sandrk | c30341b | 2017-11-09 19:13:26 | [diff] [blame] | 387 | |
| 388 | #if defined(OS_CHROMEOS) |
| 389 | |
| 390 | namespace { |
| 391 | |
Ivan Sandrk | c9eb296 | 2017-11-16 15:35:10 | [diff] [blame] | 392 | aura::Window* GetCurrentWindow() { |
Ivan Sandrk | c30341b | 2017-11-09 19:13:26 | [diff] [blame] | 393 | extensions::WindowController* controller = nullptr; |
| 394 | for (auto* iter : |
| 395 | extensions::WindowControllerList::GetInstance()->windows()) { |
| 396 | if (iter->window()->IsActive()) { |
| 397 | controller = iter; |
| 398 | break; |
| 399 | } |
| 400 | } |
| 401 | EXPECT_TRUE(controller); |
Ivan Sandrk | c9eb296 | 2017-11-16 15:35:10 | [diff] [blame] | 402 | return controller->window()->GetNativeWindow(); |
| 403 | } |
| 404 | |
| 405 | ash::mojom::WindowPinType GetCurrentWindowPinType() { |
| 406 | ash::mojom::WindowPinType type = |
| 407 | GetCurrentWindow()->GetProperty(ash::kWindowPinTypeKey); |
Ivan Sandrk | c30341b | 2017-11-09 19:13:26 | [diff] [blame] | 408 | return type; |
| 409 | } |
| 410 | |
Ivan Sandrk | c9eb296 | 2017-11-16 15:35:10 | [diff] [blame] | 411 | void SetCurrentWindowPinType(ash::mojom::WindowPinType type) { |
| 412 | GetCurrentWindow()->SetProperty(ash::kWindowPinTypeKey, type); |
| 413 | } |
| 414 | |
Ivan Sandrk | c30341b | 2017-11-09 19:13:26 | [diff] [blame] | 415 | } // namespace |
| 416 | |
| 417 | IN_PROC_BROWSER_TEST_F(WindowOpenApiTest, OpenLockedFullscreenWindow) { |
| 418 | ASSERT_TRUE(RunExtensionTestWithArg("locked_fullscreen/with_permission", |
| 419 | "openLockedFullscreenWindow")) |
| 420 | << message_; |
| 421 | |
| 422 | // Make sure the newly created window is "trusted pinned" (which means that |
| 423 | // it's in locked fullscreen mode). |
| 424 | EXPECT_EQ(ash::mojom::WindowPinType::TRUSTED_PINNED, |
| 425 | GetCurrentWindowPinType()); |
| 426 | } |
| 427 | |
| 428 | IN_PROC_BROWSER_TEST_F(WindowOpenApiTest, UpdateWindowToLockedFullscreen) { |
| 429 | ASSERT_TRUE(RunExtensionTestWithArg("locked_fullscreen/with_permission", |
| 430 | "updateWindowToLockedFullscreen")) |
| 431 | << message_; |
| 432 | |
| 433 | // Make sure the current window is put into the "trusted pinned" state. |
| 434 | EXPECT_EQ(ash::mojom::WindowPinType::TRUSTED_PINNED, |
| 435 | GetCurrentWindowPinType()); |
| 436 | } |
| 437 | |
Ivan Sandrk | c9eb296 | 2017-11-16 15:35:10 | [diff] [blame] | 438 | IN_PROC_BROWSER_TEST_F(WindowOpenApiTest, RemoveLockedFullscreenFromWindow) { |
Ivan Sandrk | 9669d0e | 2017-12-15 23:50:20 | [diff] [blame^] | 439 | // After locking the window, do a LockedFullscreenStateChanged so the |
| 440 | // command_controller state catches up as well. |
Ivan Sandrk | c9eb296 | 2017-11-16 15:35:10 | [diff] [blame] | 441 | SetCurrentWindowPinType(ash::mojom::WindowPinType::TRUSTED_PINNED); |
Ivan Sandrk | 9669d0e | 2017-12-15 23:50:20 | [diff] [blame^] | 442 | browser()->command_controller()->LockedFullscreenStateChanged(); |
Ivan Sandrk | c9eb296 | 2017-11-16 15:35:10 | [diff] [blame] | 443 | |
| 444 | ASSERT_TRUE(RunExtensionTestWithArg("locked_fullscreen/with_permission", |
| 445 | "removeLockedFullscreenFromWindow")) |
| 446 | << message_; |
| 447 | |
| 448 | // Make sure the current window is removed from locked-fullscreen state. |
| 449 | EXPECT_EQ(ash::mojom::WindowPinType::NONE, GetCurrentWindowPinType()); |
| 450 | } |
| 451 | |
Ivan Sandrk | 9669d0e | 2017-12-15 23:50:20 | [diff] [blame^] | 452 | // Make sure that commands disabling code works in locked fullscreen mode. |
| 453 | IN_PROC_BROWSER_TEST_F(WindowOpenApiTest, VerifyCommandsInLockedFullscreen) { |
| 454 | // IDC_EXIT is always enabled in regular mode so it's a perfect candidate for |
| 455 | // testing. |
| 456 | EXPECT_TRUE(browser()->command_controller()->IsCommandEnabled(IDC_EXIT)); |
| 457 | ASSERT_TRUE(RunExtensionTestWithArg("locked_fullscreen/with_permission", |
| 458 | "updateWindowToLockedFullscreen")) |
| 459 | << message_; |
| 460 | |
| 461 | // IDC_EXIT is not enabled in locked fullscreen. |
| 462 | EXPECT_FALSE(browser()->command_controller()->IsCommandEnabled(IDC_EXIT)); |
| 463 | |
| 464 | // Verify some whitelisted commands. |
| 465 | EXPECT_TRUE(browser()->command_controller()->IsCommandEnabled(IDC_COPY)); |
| 466 | EXPECT_TRUE(browser()->command_controller()->IsCommandEnabled(IDC_FIND)); |
| 467 | EXPECT_TRUE(browser()->command_controller()->IsCommandEnabled(IDC_ZOOM_PLUS)); |
| 468 | } |
| 469 | |
Ivan Sandrk | c30341b | 2017-11-09 19:13:26 | [diff] [blame] | 470 | IN_PROC_BROWSER_TEST_F(WindowOpenApiTest, |
| 471 | OpenLockedFullscreenWindowWithoutPermission) { |
| 472 | ASSERT_TRUE(RunExtensionTestWithArg("locked_fullscreen/without_permission", |
| 473 | "openLockedFullscreenWindow")) |
| 474 | << message_; |
| 475 | |
| 476 | // Make sure no new windows get created (so only the one created by default |
| 477 | // exists) since the call to chrome.windows.create fails on the javascript |
| 478 | // side. |
| 479 | EXPECT_EQ(1u, |
| 480 | extensions::WindowControllerList::GetInstance()->windows().size()); |
| 481 | } |
| 482 | |
| 483 | IN_PROC_BROWSER_TEST_F(WindowOpenApiTest, |
| 484 | UpdateWindowToLockedFullscreenWithoutPermission) { |
| 485 | ASSERT_TRUE(RunExtensionTestWithArg("locked_fullscreen/without_permission", |
| 486 | "updateWindowToLockedFullscreen")) |
| 487 | << message_; |
| 488 | |
| 489 | // chrome.windows.update call fails since this extension doesn't have the |
| 490 | // correct permission and hence the current window has NONE as WindowPinType. |
Ivan Sandrk | c9eb296 | 2017-11-16 15:35:10 | [diff] [blame] | 491 | EXPECT_EQ(ash::mojom::WindowPinType::NONE, GetCurrentWindowPinType()); |
| 492 | } |
| 493 | |
| 494 | IN_PROC_BROWSER_TEST_F(WindowOpenApiTest, |
| 495 | RemoveLockedFullscreenFromWindowWithoutPermission) { |
| 496 | SetCurrentWindowPinType(ash::mojom::WindowPinType::TRUSTED_PINNED); |
Ivan Sandrk | 9669d0e | 2017-12-15 23:50:20 | [diff] [blame^] | 497 | browser()->command_controller()->LockedFullscreenStateChanged(); |
Ivan Sandrk | c9eb296 | 2017-11-16 15:35:10 | [diff] [blame] | 498 | |
| 499 | ASSERT_TRUE(RunExtensionTestWithArg("locked_fullscreen/without_permission", |
| 500 | "removeLockedFullscreenFromWindow")) |
| 501 | << message_; |
| 502 | |
| 503 | // The current window is still locked-fullscreen. |
| 504 | EXPECT_EQ(ash::mojom::WindowPinType::TRUSTED_PINNED, |
Ivan Sandrk | c30341b | 2017-11-09 19:13:26 | [diff] [blame] | 505 | GetCurrentWindowPinType()); |
| 506 | } |
| 507 | #endif // defined(OS_CHROMEOS) |
| 508 | |
| 509 | #if !defined(OS_CHROMEOS) |
| 510 | // Loading an extension requiring the 'lockWindowFullscreenPrivate' permission |
| 511 | // on non Chrome OS platforms should always fail since the API is available only |
| 512 | // on Chrome OS. |
| 513 | IN_PROC_BROWSER_TEST_F(WindowOpenApiTest, |
| 514 | OpenLockedFullscreenWindowNonChromeOS) { |
| 515 | const extensions::Extension* extension = LoadExtensionWithFlags( |
| 516 | test_data_dir_.AppendASCII("locked_fullscreen/with_permission"), |
| 517 | ExtensionBrowserTest::kFlagIgnoreManifestWarnings); |
| 518 | ASSERT_TRUE(extension); |
| 519 | EXPECT_EQ(1u, extension->install_warnings().size()); |
| 520 | EXPECT_EQ(std::string("'lockWindowFullscreenPrivate' " |
| 521 | "is not allowed for specified platform."), |
| 522 | extension->install_warnings().front().message); |
| 523 | } |
| 524 | #endif |