[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | c80b8ee | 2011-12-03 04:26:52 | [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 | |
| 5 | // Contains holistic tests of the bindings infrastructure |
| 6 | |
Gabriel Charette | 078e366 | 2017-08-28 22:59:04 | [diff] [blame] | 7 | #include "base/run_loop.h" |
Devlin Cronin | a3fe3d60 | 2017-11-22 04:47:43 | [diff] [blame] | 8 | #include "base/test/scoped_feature_list.h" |
[email protected] | 7eef394 | 2013-08-14 02:53:49 | [diff] [blame] | 9 | #include "chrome/browser/extensions/api/permissions/permissions_api.h" |
[email protected] | c80b8ee | 2011-12-03 04:26:52 | [diff] [blame] | 10 | #include "chrome/browser/extensions/extension_apitest.h" |
Devlin Cronin | d238908 | 2018-08-14 01:53:26 | [diff] [blame] | 11 | #include "chrome/browser/sessions/session_tab_helper.h" |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 12 | #include "chrome/browser/ui/browser.h" |
Devlin Cronin | d238908 | 2018-08-14 01:53:26 | [diff] [blame] | 13 | #include "chrome/browser/ui/browser_tabstrip.h" |
rdevlin.cronin | 83a4b3a | 2015-10-28 21:43:58 | [diff] [blame] | 14 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
asargent | 79b64c3 | 2016-08-04 17:17:14 | [diff] [blame] | 15 | #include "chrome/common/chrome_switches.h" |
rdevlin.cronin | 83a4b3a | 2015-10-28 21:43:58 | [diff] [blame] | 16 | #include "chrome/test/base/ui_test_utils.h" |
jochen | 7923c2a | 2015-07-14 10:04:45 | [diff] [blame] | 17 | #include "content/public/browser/browser_thread.h" |
Devlin Cronin | b15c4f4e | 2017-12-15 21:20:11 | [diff] [blame] | 18 | #include "content/public/browser/render_frame_host.h" |
Devlin Cronin | e80e6fc1 | 2019-02-05 00:44:23 | [diff] [blame] | 19 | #include "content/public/browser/render_view_host.h" |
| 20 | #include "content/public/common/content_features.h" |
[email protected] | 7d478cb | 2012-07-24 17:19:42 | [diff] [blame] | 21 | #include "content/public/test/browser_test_utils.h" |
Devlin Cronin | d900688a | 2019-02-01 01:36:11 | [diff] [blame] | 22 | #include "content/public/test/test_navigation_observer.h" |
Devlin Cronin | d238908 | 2018-08-14 01:53:26 | [diff] [blame] | 23 | #include "extensions/browser/event_router.h" |
[email protected] | 22401dc | 2014-03-21 01:38:57 | [diff] [blame] | 24 | #include "extensions/browser/extension_host.h" |
[email protected] | 98b6d94 | 2013-11-10 00:34:07 | [diff] [blame] | 25 | #include "extensions/browser/process_manager.h" |
lfg | 910f2f9 | 2014-09-19 05:31:09 | [diff] [blame] | 26 | #include "extensions/test/extension_test_message_listener.h" |
yoz | e8dc2f1 | 2014-09-09 23:16:32 | [diff] [blame] | 27 | #include "extensions/test/result_catcher.h" |
Devlin Cronin | d238908 | 2018-08-14 01:53:26 | [diff] [blame] | 28 | #include "extensions/test/test_extension_dir.h" |
rdevlin.cronin | e6e2002 | 2017-06-13 18:23:40 | [diff] [blame] | 29 | #include "net/dns/mock_host_resolver.h" |
rdevlin.cronin | 83a4b3a | 2015-10-28 21:43:58 | [diff] [blame] | 30 | #include "net/test/embedded_test_server/embedded_test_server.h" |
Devlin Cronin | e80e6fc1 | 2019-02-05 00:44:23 | [diff] [blame] | 31 | #include "third_party/blink/public/platform/web_mouse_event.h" |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 32 | |
[email protected] | adafe5b | 2013-08-09 10:35:04 | [diff] [blame] | 33 | namespace extensions { |
| 34 | namespace { |
| 35 | |
Devlin Cronin | e80e6fc1 | 2019-02-05 00:44:23 | [diff] [blame] | 36 | void MouseDownInWebContents(content::WebContents* web_contents) { |
| 37 | blink::WebMouseEvent mouse_event( |
| 38 | blink::WebInputEvent::kMouseDown, blink::WebInputEvent::kNoModifiers, |
| 39 | blink::WebInputEvent::GetStaticTimeStampForTests()); |
| 40 | mouse_event.button = blink::WebMouseEvent::Button::kLeft; |
| 41 | mouse_event.SetPositionInWidget(10, 10); |
| 42 | mouse_event.click_count = 1; |
| 43 | web_contents->GetRenderViewHost()->GetWidget()->ForwardMouseEvent( |
| 44 | mouse_event); |
| 45 | } |
| 46 | |
| 47 | void MouseUpInWebContents(content::WebContents* web_contents) { |
| 48 | blink::WebMouseEvent mouse_event( |
| 49 | blink::WebInputEvent::kMouseUp, blink::WebInputEvent::kNoModifiers, |
| 50 | blink::WebInputEvent::GetStaticTimeStampForTests()); |
| 51 | mouse_event.button = blink::WebMouseEvent::Button::kLeft; |
| 52 | mouse_event.SetPositionInWidget(10, 10); |
| 53 | mouse_event.click_count = 1; |
| 54 | web_contents->GetRenderViewHost()->GetWidget()->ForwardMouseEvent( |
| 55 | mouse_event); |
| 56 | } |
| 57 | |
Devlin Cronin | 242d19d2 | 2019-03-12 18:08:48 | [diff] [blame] | 58 | class ExtensionBindingsApiTest : public ExtensionApiTest { |
jochen | 7923c2a | 2015-07-14 10:04:45 | [diff] [blame] | 59 | public: |
Devlin Cronin | a3fe3d60 | 2017-11-22 04:47:43 | [diff] [blame] | 60 | ExtensionBindingsApiTest() {} |
| 61 | ~ExtensionBindingsApiTest() override {} |
| 62 | |
jochen | 7923c2a | 2015-07-14 10:04:45 | [diff] [blame] | 63 | void SetUpOnMainThread() override { |
rdevlin.cronin | 17160cc6 | 2016-11-23 05:33:08 | [diff] [blame] | 64 | ExtensionApiTest::SetUpOnMainThread(); |
rdevlin.cronin | e6e2002 | 2017-06-13 18:23:40 | [diff] [blame] | 65 | host_resolver()->AddRule("*", "127.0.0.1"); |
| 66 | ASSERT_TRUE(StartEmbeddedTestServer()); |
jochen | 7923c2a | 2015-07-14 10:04:45 | [diff] [blame] | 67 | } |
Devlin Cronin | a3fe3d60 | 2017-11-22 04:47:43 | [diff] [blame] | 68 | |
| 69 | private: |
Devlin Cronin | a3fe3d60 | 2017-11-22 04:47:43 | [diff] [blame] | 70 | DISALLOW_COPY_AND_ASSIGN(ExtensionBindingsApiTest); |
jochen | 7923c2a | 2015-07-14 10:04:45 | [diff] [blame] | 71 | }; |
[email protected] | adafe5b | 2013-08-09 10:35:04 | [diff] [blame] | 72 | |
Devlin Cronin | 242d19d2 | 2019-03-12 18:08:48 | [diff] [blame] | 73 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, |
[email protected] | 7eef394 | 2013-08-14 02:53:49 | [diff] [blame] | 74 | UnavailableBindingsNeverRegistered) { |
| 75 | // Test will request the 'storage' permission. |
| 76 | PermissionsRequestFunction::SetIgnoreUserGestureForTests(true); |
| 77 | ASSERT_TRUE(RunExtensionTest( |
| 78 | "bindings/unavailable_bindings_never_registered")) << message_; |
| 79 | } |
| 80 | |
Devlin Cronin | 242d19d2 | 2019-03-12 18:08:48 | [diff] [blame] | 81 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, |
[email protected] | adafe5b | 2013-08-09 10:35:04 | [diff] [blame] | 82 | ExceptionInHandlerShouldNotCrash) { |
[email protected] | c80b8ee | 2011-12-03 04:26:52 | [diff] [blame] | 83 | ASSERT_TRUE(RunExtensionSubtest( |
| 84 | "bindings/exception_in_handler_should_not_crash", |
| 85 | "page.html")) << message_; |
| 86 | } |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 87 | |
| 88 | // Tests that an error raised during an async function still fires |
[email protected] | 754ea8b7 | 2013-01-08 15:10:31 | [diff] [blame] | 89 | // the callback, but sets chrome.runtime.lastError. |
Devlin Cronin | 242d19d2 | 2019-03-12 18:08:48 | [diff] [blame] | 90 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, LastError) { |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 91 | ASSERT_TRUE(LoadExtension( |
Devlin Cronin | c3a1e507 | 2017-08-17 17:02:49 | [diff] [blame] | 92 | test_data_dir_.AppendASCII("bindings").AppendASCII("last_error"))); |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 93 | |
| 94 | // Get the ExtensionHost that is hosting our background page. |
[email protected] | 98b6d94 | 2013-11-10 00:34:07 | [diff] [blame] | 95 | extensions::ProcessManager* manager = |
reillyg | 0ea3fa90 | 2014-10-28 15:30:23 | [diff] [blame] | 96 | extensions::ProcessManager::Get(browser()->profile()); |
[email protected] | 3a1dc57 | 2012-07-31 22:25:13 | [diff] [blame] | 97 | extensions::ExtensionHost* host = FindHostWithPath(manager, "/bg.html", 1); |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 98 | |
| 99 | bool result = false; |
David Benjamin | 5a79265 | 2018-06-08 02:15:42 | [diff] [blame] | 100 | ASSERT_TRUE(content::ExecuteScriptAndExtractBool(host->host_contents(), |
| 101 | "testLastError()", &result)); |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 102 | EXPECT_TRUE(result); |
| 103 | } |
[email protected] | 52eafbd | 2013-04-03 04:43:19 | [diff] [blame] | 104 | |
[email protected] | adafe5b | 2013-08-09 10:35:04 | [diff] [blame] | 105 | // Regression test that we don't delete our own bindings with about:blank |
| 106 | // iframes. |
Devlin Cronin | 242d19d2 | 2019-03-12 18:08:48 | [diff] [blame] | 107 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, AboutBlankIframe) { |
[email protected] | adafe5b | 2013-08-09 10:35:04 | [diff] [blame] | 108 | ResultCatcher catcher; |
| 109 | ExtensionTestMessageListener listener("load", true); |
| 110 | |
| 111 | ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("bindings") |
| 112 | .AppendASCII("about_blank_iframe"))); |
| 113 | |
| 114 | ASSERT_TRUE(listener.WaitUntilSatisfied()); |
| 115 | |
| 116 | const Extension* extension = LoadExtension( |
| 117 | test_data_dir_.AppendASCII("bindings") |
| 118 | .AppendASCII("internal_apis_not_on_chrome_object")); |
| 119 | ASSERT_TRUE(extension); |
| 120 | listener.Reply(extension->id()); |
| 121 | |
| 122 | ASSERT_TRUE(catcher.GetNextResult()) << message_; |
| 123 | } |
| 124 | |
Devlin Cronin | 242d19d2 | 2019-03-12 18:08:48 | [diff] [blame] | 125 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, |
[email protected] | adafe5b | 2013-08-09 10:35:04 | [diff] [blame] | 126 | InternalAPIsNotOnChromeObject) { |
[email protected] | 52eafbd | 2013-04-03 04:43:19 | [diff] [blame] | 127 | ASSERT_TRUE(RunExtensionSubtest( |
| 128 | "bindings/internal_apis_not_on_chrome_object", |
| 129 | "page.html")) << message_; |
| 130 | } |
[email protected] | adafe5b | 2013-08-09 10:35:04 | [diff] [blame] | 131 | |
[email protected] | fc03448 | 2013-08-09 20:25:14 | [diff] [blame] | 132 | // Tests that we don't override events when bindings are re-injected. |
| 133 | // Regression test for http://crbug.com/269149. |
rpaquay | 96bf3b7d | 2014-11-26 00:19:08 | [diff] [blame] | 134 | // Regression test for http://crbug.com/436593. |
Henrik Grunell | 6b7d9db | 2017-06-14 10:27:20 | [diff] [blame] | 135 | // Flaky on Mac. http://crbug.com/733064. |
Giovanni Ortuño Urquidi | 7ce21545 | 2017-06-14 03:34:08 | [diff] [blame] | 136 | #if defined(OS_MACOSX) |
| 137 | #define MAYBE_EventOverriding DISABLED_EventOverriding |
| 138 | #else |
| 139 | #define MAYBE_EventOverriding EventOverriding |
| 140 | #endif |
Devlin Cronin | 242d19d2 | 2019-03-12 18:08:48 | [diff] [blame] | 141 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, MAYBE_EventOverriding) { |
[email protected] | fc03448 | 2013-08-09 20:25:14 | [diff] [blame] | 142 | ASSERT_TRUE(RunExtensionTest("bindings/event_overriding")) << message_; |
rdevlin.cronin | d734f68 | 2017-06-13 21:23:11 | [diff] [blame] | 143 | // The extension test removes a window and, during window removal, sends the |
| 144 | // success message. Make sure we flush all pending tasks. |
| 145 | base::RunLoop().RunUntilIdle(); |
[email protected] | fc03448 | 2013-08-09 20:25:14 | [diff] [blame] | 146 | } |
| 147 | |
kalman | 1bd5b18 | 2015-01-13 19:01:18 | [diff] [blame] | 148 | // Tests the effectiveness of the 'nocompile' feature file property. |
| 149 | // Regression test for http://crbug.com/356133. |
Devlin Cronin | 242d19d2 | 2019-03-12 18:08:48 | [diff] [blame] | 150 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, Nocompile) { |
kalman | 1bd5b18 | 2015-01-13 19:01:18 | [diff] [blame] | 151 | ASSERT_TRUE(RunExtensionSubtest("bindings/nocompile", "page.html")) |
| 152 | << message_; |
| 153 | } |
| 154 | |
Devlin Cronin | 242d19d2 | 2019-03-12 18:08:48 | [diff] [blame] | 155 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, ApiEnums) { |
rdevlin.cronin | 2ba3c88d | 2015-03-03 01:18:22 | [diff] [blame] | 156 | ASSERT_TRUE(RunExtensionTest("bindings/api_enums")) << message_; |
Nico Weber | 42c88d6 | 2019-02-11 03:09:25 | [diff] [blame] | 157 | } |
rdevlin.cronin | 2ba3c88d | 2015-03-03 01:18:22 | [diff] [blame] | 158 | |
jochen | 7923c2a | 2015-07-14 10:04:45 | [diff] [blame] | 159 | // Regression test for http://crbug.com/504011 - proper access checks on |
| 160 | // getModuleSystem(). |
Devlin Cronin | 242d19d2 | 2019-03-12 18:08:48 | [diff] [blame] | 161 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, ModuleSystem) { |
jochen | 7923c2a | 2015-07-14 10:04:45 | [diff] [blame] | 162 | ASSERT_TRUE(RunExtensionTest("bindings/module_system")) << message_; |
| 163 | } |
| 164 | |
Devlin Cronin | 242d19d2 | 2019-03-12 18:08:48 | [diff] [blame] | 165 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, NoExportOverriding) { |
rdevlin.cronin | 83a4b3a | 2015-10-28 21:43:58 | [diff] [blame] | 166 | // We need to create runtime bindings in the web page. An extension that's |
| 167 | // externally connectable will do that for us. |
| 168 | ASSERT_TRUE(LoadExtension( |
| 169 | test_data_dir_.AppendASCII("bindings") |
| 170 | .AppendASCII("externally_connectable_everywhere"))); |
| 171 | |
| 172 | ui_test_utils::NavigateToURL( |
| 173 | browser(), |
| 174 | embedded_test_server()->GetURL( |
| 175 | "/extensions/api_test/bindings/override_exports.html")); |
| 176 | |
| 177 | // See chrome/test/data/extensions/api_test/bindings/override_exports.html. |
| 178 | std::string result; |
| 179 | EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 180 | browser()->tab_strip_model()->GetActiveWebContents(), |
| 181 | "window.domAutomationController.send(" |
| 182 | "document.getElementById('status').textContent.trim());", |
| 183 | &result)); |
| 184 | EXPECT_EQ("success", result); |
| 185 | } |
| 186 | |
Devlin Cronin | 242d19d2 | 2019-03-12 18:08:48 | [diff] [blame] | 187 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, NoGinDefineOverriding) { |
rdevlin.cronin | 415b73b | 2015-11-13 01:14:47 | [diff] [blame] | 188 | // We need to create runtime bindings in the web page. An extension that's |
| 189 | // externally connectable will do that for us. |
| 190 | ASSERT_TRUE(LoadExtension( |
| 191 | test_data_dir_.AppendASCII("bindings") |
| 192 | .AppendASCII("externally_connectable_everywhere"))); |
| 193 | |
| 194 | ui_test_utils::NavigateToURL( |
| 195 | browser(), |
| 196 | embedded_test_server()->GetURL( |
| 197 | "/extensions/api_test/bindings/override_gin_define.html")); |
| 198 | ASSERT_FALSE( |
| 199 | browser()->tab_strip_model()->GetActiveWebContents()->IsCrashed()); |
| 200 | |
| 201 | // See chrome/test/data/extensions/api_test/bindings/override_gin_define.html. |
| 202 | std::string result; |
| 203 | EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 204 | browser()->tab_strip_model()->GetActiveWebContents(), |
| 205 | "window.domAutomationController.send(" |
| 206 | "document.getElementById('status').textContent.trim());", |
| 207 | &result)); |
| 208 | EXPECT_EQ("success", result); |
| 209 | } |
| 210 | |
Devlin Cronin | 242d19d2 | 2019-03-12 18:08:48 | [diff] [blame] | 211 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, HandlerFunctionTypeChecking) { |
rdevlin.cronin | a5ecbc8 | 2015-10-29 23:41:29 | [diff] [blame] | 212 | ui_test_utils::NavigateToURL( |
| 213 | browser(), |
| 214 | embedded_test_server()->GetURL( |
| 215 | "/extensions/api_test/bindings/handler_function_type_checking.html")); |
| 216 | content::WebContents* web_contents = |
| 217 | browser()->tab_strip_model()->GetActiveWebContents(); |
| 218 | EXPECT_FALSE(web_contents->IsCrashed()); |
| 219 | // See handler_function_type_checking.html. |
| 220 | std::string result; |
| 221 | EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 222 | web_contents, |
| 223 | "window.domAutomationController.send(" |
| 224 | "document.getElementById('status').textContent.trim());", |
| 225 | &result)); |
| 226 | EXPECT_EQ("success", result); |
| 227 | } |
| 228 | |
Devlin Cronin | 242d19d2 | 2019-03-12 18:08:48 | [diff] [blame] | 229 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, |
rdevlin.cronin | 75b803b | 2016-03-02 00:13:47 | [diff] [blame] | 230 | MoreNativeFunctionInterceptionTests) { |
rdevlin.cronin | 75b803b | 2016-03-02 00:13:47 | [diff] [blame] | 231 | // We need to create runtime bindings in the web page. An extension that's |
| 232 | // externally connectable will do that for us. |
| 233 | ASSERT_TRUE( |
| 234 | LoadExtension(test_data_dir_.AppendASCII("bindings") |
| 235 | .AppendASCII("externally_connectable_everywhere"))); |
| 236 | |
| 237 | ui_test_utils::NavigateToURL( |
| 238 | browser(), |
| 239 | embedded_test_server()->GetURL( |
| 240 | "/extensions/api_test/bindings/function_interceptions.html")); |
| 241 | content::WebContents* web_contents = |
| 242 | browser()->tab_strip_model()->GetActiveWebContents(); |
| 243 | EXPECT_FALSE(web_contents->IsCrashed()); |
| 244 | // See function_interceptions.html. |
| 245 | std::string result; |
| 246 | EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 247 | web_contents, "window.domAutomationController.send(window.testStatus);", |
| 248 | &result)); |
| 249 | EXPECT_EQ("success", result); |
| 250 | } |
| 251 | |
asargent | 79b64c3 | 2016-08-04 17:17:14 | [diff] [blame] | 252 | class FramesExtensionBindingsApiTest : public ExtensionBindingsApiTest { |
| 253 | public: |
| 254 | void SetUpCommandLine(base::CommandLine* command_line) override { |
| 255 | ExtensionBindingsApiTest::SetUpCommandLine(command_line); |
Devlin Cronin | c3a1e507 | 2017-08-17 17:02:49 | [diff] [blame] | 256 | command_line->AppendSwitch(::switches::kDisablePopupBlocking); |
asargent | 79b64c3 | 2016-08-04 17:17:14 | [diff] [blame] | 257 | } |
| 258 | }; |
| 259 | |
| 260 | // This tests that web pages with iframes or child windows pointing at |
| 261 | // chrome-extenison:// urls, both web_accessible and nonexistent pages, don't |
| 262 | // get improper extensions bindings injected while they briefly still point at |
| 263 | // about:blank and are still scriptable by their parent. |
| 264 | // |
| 265 | // The general idea is to load up 2 extensions, one which listens for external |
| 266 | // messages ("receiver") and one which we'll try first faking messages from in |
| 267 | // the web page's iframe, as well as actually send a message from later |
| 268 | // ("sender"). |
Devlin Cronin | 242d19d2 | 2019-03-12 18:08:48 | [diff] [blame] | 269 | IN_PROC_BROWSER_TEST_F(FramesExtensionBindingsApiTest, FramesBeforeNavigation) { |
asargent | 79b64c3 | 2016-08-04 17:17:14 | [diff] [blame] | 270 | // Load the sender and receiver extensions, and make sure they are ready. |
| 271 | ExtensionTestMessageListener sender_ready("sender_ready", true); |
| 272 | const Extension* sender = LoadExtension( |
| 273 | test_data_dir_.AppendASCII("bindings").AppendASCII("message_sender")); |
| 274 | ASSERT_NE(nullptr, sender); |
| 275 | ASSERT_TRUE(sender_ready.WaitUntilSatisfied()); |
| 276 | |
| 277 | ExtensionTestMessageListener receiver_ready("receiver_ready", false); |
| 278 | const Extension* receiver = |
| 279 | LoadExtension(test_data_dir_.AppendASCII("bindings") |
| 280 | .AppendASCII("external_message_listener")); |
| 281 | ASSERT_NE(nullptr, receiver); |
| 282 | ASSERT_TRUE(receiver_ready.WaitUntilSatisfied()); |
| 283 | |
| 284 | // Load the web page which tries to impersonate the sender extension via |
| 285 | // scripting iframes/child windows before they finish navigating to pages |
| 286 | // within the sender extension. |
asargent | 79b64c3 | 2016-08-04 17:17:14 | [diff] [blame] | 287 | ui_test_utils::NavigateToURL( |
| 288 | browser(), |
| 289 | embedded_test_server()->GetURL( |
| 290 | "/extensions/api_test/bindings/frames_before_navigation.html")); |
| 291 | |
| 292 | bool page_success = false; |
| 293 | ASSERT_TRUE(content::ExecuteScriptAndExtractBool( |
| 294 | browser()->tab_strip_model()->GetWebContentsAt(0), "getResult()", |
| 295 | &page_success)); |
| 296 | EXPECT_TRUE(page_success); |
| 297 | |
| 298 | // Reply to |sender|, causing it to send a message over to |receiver|, and |
| 299 | // then ask |receiver| for the total message count. It should be 1 since |
| 300 | // |receiver| should not have received any impersonated messages. |
| 301 | sender_ready.Reply(receiver->id()); |
| 302 | int message_count = 0; |
| 303 | ASSERT_TRUE(content::ExecuteScriptAndExtractInt( |
| 304 | ProcessManager::Get(profile()) |
| 305 | ->GetBackgroundHostForExtension(receiver->id()) |
| 306 | ->host_contents(), |
| 307 | "getMessageCountAfterReceivingRealSenderMessage()", &message_count)); |
| 308 | EXPECT_EQ(1, message_count); |
| 309 | } |
| 310 | |
Devlin Cronin | 242d19d2 | 2019-03-12 18:08:48 | [diff] [blame] | 311 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, TestFreezingChrome) { |
rdevlin.cronin | 741da00 | 2017-04-24 20:27:41 | [diff] [blame] | 312 | ui_test_utils::NavigateToURL( |
| 313 | browser(), embedded_test_server()->GetURL( |
| 314 | "/extensions/api_test/bindings/freeze.html")); |
| 315 | content::WebContents* web_contents = |
| 316 | browser()->tab_strip_model()->GetActiveWebContents(); |
| 317 | ASSERT_FALSE(web_contents->IsCrashed()); |
| 318 | } |
| 319 | |
rdevlin.cronin | e6e2002 | 2017-06-13 18:23:40 | [diff] [blame] | 320 | // Tests interaction with event filter parsing. |
Devlin Cronin | 242d19d2 | 2019-03-12 18:08:48 | [diff] [blame] | 321 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, TestEventFilterParsing) { |
rdevlin.cronin | e6e2002 | 2017-06-13 18:23:40 | [diff] [blame] | 322 | ExtensionTestMessageListener listener("ready", false); |
| 323 | ASSERT_TRUE( |
| 324 | LoadExtension(test_data_dir_.AppendASCII("bindings/event_filter"))); |
| 325 | ASSERT_TRUE(listener.WaitUntilSatisfied()); |
| 326 | |
| 327 | ResultCatcher catcher; |
| 328 | ui_test_utils::NavigateToURL( |
| 329 | browser(), embedded_test_server()->GetURL("example.com", "/title1.html")); |
| 330 | ASSERT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 331 | } |
| 332 | |
rdevlin.cronin | 350824d4 | 2017-06-16 14:47:35 | [diff] [blame] | 333 | // crbug.com/733337 |
Devlin Cronin | 242d19d2 | 2019-03-12 18:08:48 | [diff] [blame] | 334 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, ValidationInterception) { |
rdevlin.cronin | 350824d4 | 2017-06-16 14:47:35 | [diff] [blame] | 335 | // We need to create runtime bindings in the web page. An extension that's |
| 336 | // externally connectable will do that for us. |
| 337 | ASSERT_TRUE( |
| 338 | LoadExtension(test_data_dir_.AppendASCII("bindings") |
| 339 | .AppendASCII("externally_connectable_everywhere"))); |
| 340 | |
| 341 | content::WebContents* web_contents = |
| 342 | browser()->tab_strip_model()->GetActiveWebContents(); |
| 343 | ui_test_utils::NavigateToURL( |
| 344 | browser(), |
| 345 | embedded_test_server()->GetURL( |
| 346 | "/extensions/api_test/bindings/validation_interception.html")); |
| 347 | content::WaitForLoadStop(web_contents); |
| 348 | ASSERT_FALSE(web_contents->IsCrashed()); |
| 349 | bool caught = false; |
| 350 | ASSERT_TRUE(content::ExecuteScriptAndExtractBool( |
| 351 | web_contents, "domAutomationController.send(caught)", &caught)); |
| 352 | EXPECT_TRUE(caught); |
| 353 | } |
| 354 | |
Devlin Cronin | 242d19d2 | 2019-03-12 18:08:48 | [diff] [blame] | 355 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, UncaughtExceptionLogging) { |
Devlin Cronin | 5cf20f0 | 2017-10-10 14:25:04 | [diff] [blame] | 356 | ASSERT_TRUE(RunExtensionTest("bindings/uncaught_exception_logging")) |
| 357 | << message_; |
| 358 | } |
| 359 | |
Alex Moshchuk | e63b9e9 | 2017-10-14 00:27:22 | [diff] [blame] | 360 | // Verify that when a web frame embeds an extension subframe, and that subframe |
| 361 | // is the only active portion of the extension, the subframe gets proper JS |
| 362 | // bindings. See https://crbug.com/760341. |
Devlin Cronin | 242d19d2 | 2019-03-12 18:08:48 | [diff] [blame] | 363 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, |
Alex Moshchuk | e63b9e9 | 2017-10-14 00:27:22 | [diff] [blame] | 364 | ExtensionSubframeGetsBindings) { |
| 365 | // Load an extension that does not have a background page or popup, so it |
| 366 | // won't be activated just yet. |
| 367 | const extensions::Extension* extension = |
| 368 | LoadExtension(test_data_dir_.AppendASCII("bindings") |
| 369 | .AppendASCII("extension_subframe_gets_bindings")); |
| 370 | ASSERT_TRUE(extension); |
| 371 | |
| 372 | // Navigate current tab to a web URL with a subframe. |
| 373 | content::WebContents* web_contents = |
| 374 | browser()->tab_strip_model()->GetActiveWebContents(); |
| 375 | ui_test_utils::NavigateToURL(browser(), |
| 376 | embedded_test_server()->GetURL("/iframe.html")); |
| 377 | |
| 378 | // Navigate the subframe to the extension URL, which should activate the |
| 379 | // extension. |
| 380 | GURL extension_url(extension->GetResourceURL("page.html")); |
| 381 | ResultCatcher catcher; |
| 382 | content::NavigateIframeToURL(web_contents, "test", extension_url); |
| 383 | ASSERT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 384 | } |
| 385 | |
Devlin Cronin | 242d19d2 | 2019-03-12 18:08:48 | [diff] [blame] | 386 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, |
Devlin Cronin | b15c4f4e | 2017-12-15 21:20:11 | [diff] [blame] | 387 | ExtensionListenersRemoveContext) { |
| 388 | const Extension* extension = LoadExtension( |
| 389 | test_data_dir_.AppendASCII("bindings/listeners_destroy_context")); |
| 390 | ASSERT_TRUE(extension); |
| 391 | |
| 392 | ExtensionTestMessageListener listener("ready", true); |
| 393 | |
| 394 | // Navigate to a web page with an iframe (the iframe is title1.html). |
| 395 | GURL main_frame_url = embedded_test_server()->GetURL("a.com", "/iframe.html"); |
| 396 | ui_test_utils::NavigateToURL(browser(), main_frame_url); |
| 397 | |
| 398 | content::WebContents* tab = |
| 399 | browser()->tab_strip_model()->GetActiveWebContents(); |
| 400 | |
| 401 | content::RenderFrameHost* main_frame = tab->GetMainFrame(); |
| 402 | content::RenderFrameHost* subframe = ChildFrameAt(main_frame, 0); |
| 403 | content::RenderFrameDeletedObserver subframe_deleted(subframe); |
| 404 | |
| 405 | // Wait for the extension's content script to be ready. |
| 406 | ASSERT_TRUE(listener.WaitUntilSatisfied()); |
| 407 | |
| 408 | // It's actually critical to the test that these frames are in the same |
| 409 | // process, because otherwise a crash in the iframe wouldn't be detectable |
| 410 | // (since we rely on JS execution in the main frame to tell if the renderer |
| 411 | // crashed - see comment below). |
| 412 | content::RenderProcessHost* main_frame_process = main_frame->GetProcess(); |
| 413 | EXPECT_EQ(main_frame_process, subframe->GetProcess()); |
| 414 | |
| 415 | ExtensionTestMessageListener failure_listener("failed", false); |
| 416 | |
| 417 | // Tell the extension to register listeners that will remove the iframe, and |
| 418 | // trigger them. |
| 419 | listener.Reply("go!"); |
| 420 | |
| 421 | // The frame will be deleted. |
| 422 | subframe_deleted.WaitUntilDeleted(); |
| 423 | |
| 424 | // Unfortunately, we don't have a good way of checking if something crashed |
| 425 | // after the frame was removed. WebContents::IsCrashed() seems like it should |
| 426 | // work, but is insufficient. Instead, use JS execution as the source of |
| 427 | // true. |
| 428 | EXPECT_FALSE(tab->IsCrashed()); |
| 429 | EXPECT_EQ(main_frame_url, main_frame->GetLastCommittedURL()); |
| 430 | EXPECT_EQ(main_frame_process, main_frame->GetProcess()); |
| 431 | bool renderer_valid = false; |
| 432 | EXPECT_TRUE(content::ExecuteScriptAndExtractBool( |
| 433 | main_frame, "domAutomationController.send(true);", &renderer_valid)); |
| 434 | EXPECT_TRUE(renderer_valid); |
| 435 | EXPECT_FALSE(failure_listener.was_satisfied()); |
| 436 | } |
| 437 | |
Devlin Cronin | 242d19d2 | 2019-03-12 18:08:48 | [diff] [blame] | 438 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, UseAPIsAfterContextRemoval) { |
Devlin Cronin | b48b6f9 | 2018-01-12 23:17:59 | [diff] [blame] | 439 | EXPECT_TRUE(RunExtensionTest("bindings/invalidate_context")) << message_; |
| 440 | } |
| 441 | |
Devlin Cronin | 6553cc38 | 2019-03-28 22:46:46 | [diff] [blame] | 442 | // Tests that we don't crash if the extension invalidates the context in a |
| 443 | // callback with a runtime.lastError present. Regression test for |
| 444 | // https://crbug.com/944014. |
| 445 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, |
| 446 | InvalidateContextInCallbackWithLastError) { |
| 447 | TestExtensionDir dir; |
| 448 | dir.WriteManifest( |
| 449 | R"({ |
| 450 | "name": "Invalidate Context in onDisconnect", |
| 451 | "version": "0.1", |
| 452 | "manifest_version": 2, |
| 453 | "background": {"scripts": ["background.js"]} |
| 454 | })"); |
| 455 | |
| 456 | constexpr char kFrameHtml[] = |
| 457 | R"(<html> |
| 458 | <body></body> |
| 459 | <script src="frame.js"></script> |
| 460 | </html>)"; |
| 461 | constexpr char kFrameJs[] = |
| 462 | R"(chrome.tabs.executeScript({code: ''}, () => { |
| 463 | // We expect a last error to be present, since we don't have access |
| 464 | // to the tab. |
| 465 | chrome.test.assertTrue(!!chrome.runtime.lastError); |
| 466 | // Remove the frame from the DOM. This causes blink to remove the |
| 467 | // associated script contexts. |
| 468 | parent.document.body.removeChild( |
| 469 | parent.document.body.querySelector('iframe')); |
| 470 | });)"; |
| 471 | constexpr char kBackgroundJs[] = |
| 472 | R"(let frame = document.createElement('iframe'); |
| 473 | frame.src = 'frame.html'; |
| 474 | let observer = new MutationObserver((mutationList) => { |
| 475 | for (let mutation of mutationList) { |
| 476 | if (mutation.removedNodes.length == 0) |
| 477 | continue; |
| 478 | chrome.test.assertEq(1, mutation.removedNodes.length); |
| 479 | chrome.test.assertEq('IFRAME', mutation.removedNodes[0].tagName); |
| 480 | chrome.test.notifyPass(); |
| 481 | break; |
| 482 | } |
| 483 | }); |
| 484 | observer.observe(document.body, {childList: true}); |
| 485 | document.body.appendChild(frame);)"; |
| 486 | dir.WriteFile(FILE_PATH_LITERAL("frame.html"), kFrameHtml); |
| 487 | dir.WriteFile(FILE_PATH_LITERAL("frame.js"), kFrameJs); |
| 488 | dir.WriteFile(FILE_PATH_LITERAL("background.js"), kBackgroundJs); |
| 489 | |
| 490 | ResultCatcher catcher; |
| 491 | const Extension* extension = LoadExtension(dir.UnpackedPath()); |
| 492 | ASSERT_TRUE(extension); |
| 493 | EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 494 | } |
| 495 | |
Devlin Cronin | 47e7904 | 2018-02-02 18:32:26 | [diff] [blame] | 496 | // TODO(devlin): Can this be combined with |
| 497 | // ExtensionBindingsApiTest.UseAPIsAfterContextRemoval? |
Devlin Cronin | 242d19d2 | 2019-03-12 18:08:48 | [diff] [blame] | 498 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, UseAppAPIAfterFrameRemoval) { |
Devlin Cronin | 47e7904 | 2018-02-02 18:32:26 | [diff] [blame] | 499 | ASSERT_TRUE(RunExtensionTest("crazy_extension")); |
| 500 | } |
| 501 | |
Devlin Cronin | d238908 | 2018-08-14 01:53:26 | [diff] [blame] | 502 | // Tests attaching two listeners from the same extension but different pages, |
| 503 | // then removing one, and ensuring the second is still notified. |
| 504 | // Regression test for https://crbug.com/868763. |
Devlin Cronin | 242d19d2 | 2019-03-12 18:08:48 | [diff] [blame] | 505 | IN_PROC_BROWSER_TEST_F( |
Devlin Cronin | d238908 | 2018-08-14 01:53:26 | [diff] [blame] | 506 | ExtensionBindingsApiTest, |
| 507 | MultipleEventListenersFromDifferentContextsAndTheSameExtension) { |
| 508 | // A script that listens for tab creation and populates the result in a |
| 509 | // global variable. |
| 510 | constexpr char kTestPageScript[] = R"( |
| 511 | window.tabEventId = -1; |
| 512 | function registerListener() { |
| 513 | chrome.tabs.onCreated.addListener((tab) => { |
| 514 | window.tabEventId = tab.id; |
| 515 | }); |
| 516 | } |
| 517 | )"; |
| 518 | TestExtensionDir test_dir; |
| 519 | test_dir.WriteManifest(R"( |
| 520 | { |
| 521 | "name": "Duplicate event listeners", |
| 522 | "manifest_version": 2, |
| 523 | "version": "0.1" |
| 524 | })"); |
| 525 | test_dir.WriteFile(FILE_PATH_LITERAL("page.html"), |
| 526 | R"(<html><script src="page.js"></script></html>)"); |
| 527 | test_dir.WriteFile(FILE_PATH_LITERAL("page.js"), kTestPageScript); |
| 528 | |
| 529 | const Extension* extension = LoadExtension(test_dir.UnpackedPath()); |
| 530 | ASSERT_TRUE(extension); |
| 531 | |
| 532 | // Set up: open two tabs to the same extension page, and wait for each to |
| 533 | // load. |
| 534 | const GURL page_url = extension->GetResourceURL("page.html"); |
| 535 | ui_test_utils::NavigateToURLWithDisposition( |
| 536 | browser(), page_url, WindowOpenDisposition::NEW_FOREGROUND_TAB, |
| 537 | ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 538 | content::WebContents* first_tab = |
| 539 | browser()->tab_strip_model()->GetActiveWebContents(); |
| 540 | ui_test_utils::NavigateToURLWithDisposition( |
| 541 | browser(), page_url, WindowOpenDisposition::NEW_FOREGROUND_TAB, |
| 542 | ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 543 | content::WebContents* second_tab = |
| 544 | browser()->tab_strip_model()->GetActiveWebContents(); |
| 545 | |
| 546 | // Initially, there are no listeners registered. |
| 547 | EventRouter* event_router = EventRouter::Get(profile()); |
| 548 | EXPECT_FALSE(event_router->ExtensionHasEventListener(extension->id(), |
| 549 | "tabs.onCreated")); |
| 550 | |
| 551 | // Register both lsiteners, and verify they were added. |
| 552 | ASSERT_TRUE(content::ExecuteScript(first_tab, "registerListener()")); |
| 553 | ASSERT_TRUE(content::ExecuteScript(second_tab, "registerListener()")); |
| 554 | EXPECT_TRUE(event_router->ExtensionHasEventListener(extension->id(), |
| 555 | "tabs.onCreated")); |
| 556 | |
| 557 | // Close one of the extension pages. |
| 558 | constexpr bool add_to_history = false; |
| 559 | content::WebContentsDestroyedWatcher watcher(second_tab); |
| 560 | chrome::CloseWebContents(browser(), second_tab, add_to_history); |
| 561 | watcher.Wait(); |
| 562 | // Hacky round trip to the renderer to flush IPCs. |
| 563 | ASSERT_TRUE(content::ExecuteScript(first_tab, "")); |
| 564 | |
| 565 | // Since the second page is still open, the extension should still be |
| 566 | // registered as a listener. |
| 567 | EXPECT_TRUE(event_router->ExtensionHasEventListener(extension->id(), |
| 568 | "tabs.onCreated")); |
| 569 | |
| 570 | // Open a new tab. |
| 571 | ui_test_utils::NavigateToURLWithDisposition( |
| 572 | browser(), GURL("chrome://newtab"), |
| 573 | WindowOpenDisposition::NEW_FOREGROUND_TAB, |
| 574 | ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 575 | content::WebContents* new_tab = |
| 576 | browser()->tab_strip_model()->GetActiveWebContents(); |
| 577 | |
| 578 | // The extension should have been notified about the new tab, and have |
| 579 | // recorded the result. |
| 580 | int result_tab_id = -1; |
| 581 | EXPECT_TRUE(content::ExecuteScriptAndExtractInt( |
| 582 | first_tab, "domAutomationController.send(window.tabEventId)", |
| 583 | &result_tab_id)); |
| 584 | EXPECT_EQ(SessionTabHelper::IdForTab(new_tab).id(), result_tab_id); |
| 585 | } |
| 586 | |
Devlin Cronin | 242d19d2 | 2019-03-12 18:08:48 | [diff] [blame] | 587 | enum UserActivationType { |
| 588 | kUserActivationV1, |
| 589 | kUserActivationV2, |
| 590 | }; |
| 591 | |
| 592 | class ExtensionBindingsUserGestureTest |
| 593 | : public ExtensionBindingsApiTest, |
| 594 | public ::testing::WithParamInterface<UserActivationType> { |
| 595 | public: |
| 596 | ExtensionBindingsUserGestureTest() {} |
| 597 | ~ExtensionBindingsUserGestureTest() override {} |
| 598 | |
| 599 | void SetUp() override { |
| 600 | UserActivationType user_activation_type = GetParam(); |
| 601 | if (user_activation_type == kUserActivationV2) { |
| 602 | scoped_feature_list_.InitAndEnableFeature(features::kUserActivationV2); |
| 603 | } else { |
| 604 | DCHECK_EQ(kUserActivationV1, user_activation_type); |
| 605 | scoped_feature_list_.InitAndDisableFeature(features::kUserActivationV2); |
| 606 | } |
| 607 | |
| 608 | ExtensionBindingsApiTest::SetUp(); |
| 609 | } |
| 610 | |
| 611 | private: |
| 612 | base::test::ScopedFeatureList scoped_feature_list_; |
| 613 | |
| 614 | DISALLOW_COPY_AND_ASSIGN(ExtensionBindingsUserGestureTest); |
| 615 | }; |
Devlin Cronin | e80e6fc1 | 2019-02-05 00:44:23 | [diff] [blame] | 616 | |
Devlin Cronin | c3d01799 | 2018-09-11 01:21:20 | [diff] [blame] | 617 | // Verifies that user gestures are carried through extension messages. |
Devlin Cronin | e80e6fc1 | 2019-02-05 00:44:23 | [diff] [blame] | 618 | IN_PROC_BROWSER_TEST_P(ExtensionBindingsUserGestureTest, |
Devlin Cronin | c3d01799 | 2018-09-11 01:21:20 | [diff] [blame] | 619 | UserGestureFromExtensionMessageTest) { |
| 620 | TestExtensionDir test_dir; |
| 621 | test_dir.WriteManifest( |
| 622 | R"({ |
| 623 | "name": "User Gesture Content Script", |
| 624 | "manifest_version": 2, |
| 625 | "version": "0.1", |
| 626 | "background": { "scripts": ["background.js"] }, |
| 627 | "content_scripts": [{ |
| 628 | "matches": ["*://*.example.com:*/*"], |
| 629 | "js": ["content_script.js"], |
| 630 | "run_at": "document_end" |
| 631 | }] |
| 632 | })"); |
| 633 | test_dir.WriteFile(FILE_PATH_LITERAL("content_script.js"), |
| 634 | R"(const button = document.getElementById('go-button'); |
| 635 | button.addEventListener('click', () => { |
| 636 | chrome.runtime.sendMessage('clicked'); |
| 637 | });)"); |
| 638 | test_dir.WriteFile(FILE_PATH_LITERAL("background.js"), |
| 639 | R"(chrome.runtime.onMessage.addListener((message) => { |
| 640 | chrome.test.sendMessage( |
| 641 | 'Clicked: ' + |
| 642 | chrome.test.isProcessingUserGesture()); |
| 643 | });)"); |
| 644 | |
| 645 | const Extension* extension = LoadExtension(test_dir.UnpackedPath()); |
| 646 | ASSERT_TRUE(extension); |
| 647 | |
| 648 | const GURL url = embedded_test_server()->GetURL( |
| 649 | "example.com", "/extensions/page_with_button.html"); |
| 650 | ui_test_utils::NavigateToURL(browser(), url); |
| 651 | |
| 652 | content::WebContents* tab = |
| 653 | browser()->tab_strip_model()->GetActiveWebContents(); |
| 654 | |
| 655 | { |
| 656 | // Passing a message without an active user gesture shouldn't result in a |
| 657 | // gesture being active on the receiving end. |
| 658 | ExtensionTestMessageListener listener(false); |
| 659 | content::EvalJsResult result = |
| 660 | content::EvalJs(tab, "document.getElementById('go-button').click()", |
| 661 | content::EXECUTE_SCRIPT_NO_USER_GESTURE); |
| 662 | EXPECT_TRUE(result.value.is_none()); |
| 663 | |
| 664 | EXPECT_TRUE(listener.WaitUntilSatisfied()); |
| 665 | EXPECT_EQ("Clicked: false", listener.message()); |
| 666 | } |
| 667 | |
| 668 | { |
| 669 | // If there is an active user gesture when the message is sent, we should |
| 670 | // synthesize a user gesture on the receiving end. |
| 671 | ExtensionTestMessageListener listener(false); |
| 672 | content::EvalJsResult result = |
| 673 | content::EvalJs(tab, "document.getElementById('go-button').click()"); |
| 674 | EXPECT_TRUE(result.value.is_none()); |
| 675 | |
| 676 | EXPECT_TRUE(listener.WaitUntilSatisfied()); |
| 677 | EXPECT_EQ("Clicked: true", listener.message()); |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | // Verifies that user gestures from API calls are active when the callback is |
| 682 | // triggered. |
Devlin Cronin | e80e6fc1 | 2019-02-05 00:44:23 | [diff] [blame] | 683 | IN_PROC_BROWSER_TEST_P(ExtensionBindingsUserGestureTest, |
Devlin Cronin | c3d01799 | 2018-09-11 01:21:20 | [diff] [blame] | 684 | UserGestureInExtensionAPICallback) { |
| 685 | TestExtensionDir test_dir; |
| 686 | test_dir.WriteManifest( |
| 687 | R"({ |
| 688 | "name": "User Gesture Extension API Callback", |
| 689 | "manifest_version": 2, |
| 690 | "version": "0.1" |
| 691 | })"); |
| 692 | test_dir.WriteFile(FILE_PATH_LITERAL("page.html"), "<html></html>"); |
| 693 | |
| 694 | const Extension* extension = LoadExtension(test_dir.UnpackedPath()); |
| 695 | ASSERT_TRUE(extension); |
| 696 | |
| 697 | const GURL extension_page = extension->GetResourceURL("page.html"); |
| 698 | ui_test_utils::NavigateToURL(browser(), extension_page); |
| 699 | |
| 700 | content::WebContents* tab = |
| 701 | browser()->tab_strip_model()->GetActiveWebContents(); |
| 702 | |
| 703 | constexpr char kScript[] = |
| 704 | R"(chrome.tabs.query({}, (tabs) => { |
| 705 | let message; |
| 706 | if (chrome.runtime.lastError) |
| 707 | message = 'Unexpected error: ' + chrome.runtime.lastError; |
| 708 | else |
| 709 | message = 'Has gesture: ' + chrome.test.isProcessingUserGesture(); |
| 710 | domAutomationController.send(message); |
| 711 | });)"; |
| 712 | |
| 713 | { |
| 714 | // Triggering an API without an active gesture shouldn't result in a |
| 715 | // gesture in the callback. |
| 716 | std::string message; |
| 717 | EXPECT_TRUE(content::ExecuteScriptWithoutUserGestureAndExtractString( |
| 718 | tab, kScript, &message)); |
| 719 | EXPECT_EQ("Has gesture: false", message); |
| 720 | } |
| 721 | { |
| 722 | // If there was an active gesture at the time of the API call, there should |
| 723 | // be an active gesture in the callback. |
| 724 | std::string message; |
| 725 | EXPECT_TRUE(content::ExecuteScriptAndExtractString(tab, kScript, &message)); |
| 726 | EXPECT_EQ("Has gesture: true", message); |
| 727 | } |
| 728 | } |
| 729 | |
Devlin Cronin | e80e6fc1 | 2019-02-05 00:44:23 | [diff] [blame] | 730 | // Tests that a web page can consume a user gesture after an extension sends and |
| 731 | // receives a reply during the same user gesture. |
| 732 | // Regression test for https://crbug.com/921141. |
| 733 | IN_PROC_BROWSER_TEST_P(ExtensionBindingsUserGestureTest, |
| 734 | WebUserGestureAfterMessagingCallback) { |
| 735 | TestExtensionDir test_dir; |
| 736 | test_dir.WriteManifest( |
| 737 | R"({ |
| 738 | "name": "User Gesture Messaging Test", |
| 739 | "version": "0.1", |
| 740 | "manifest_version": 2, |
| 741 | "content_scripts": [{ |
| 742 | "matches": ["*://*/*"], |
| 743 | "js": ["content_script.js"], |
| 744 | "run_at": "document_start" |
| 745 | }], |
| 746 | "background": { |
| 747 | "scripts": ["background.js"] |
| 748 | } |
| 749 | })"); |
| 750 | test_dir.WriteFile(FILE_PATH_LITERAL("content_script.js"), |
| 751 | R"(window.addEventListener('mousedown', () => { |
| 752 | chrome.runtime.sendMessage('hello', () => { |
| 753 | let message = chrome.test.isProcessingUserGesture() ? |
| 754 | 'got reply' : 'no user gesture'; |
| 755 | chrome.test.sendMessage(message); |
| 756 | }); |
| 757 | });)"); |
| 758 | test_dir.WriteFile( |
| 759 | FILE_PATH_LITERAL("background.js"), |
| 760 | R"(chrome.runtime.onMessage.addListener((message, sender, respond) => { |
| 761 | respond('reply'); |
| 762 | }); |
| 763 | chrome.test.sendMessage('ready');)"); |
| 764 | |
| 765 | const Extension* extension = nullptr; |
| 766 | { |
| 767 | ExtensionTestMessageListener listener("ready", false); |
| 768 | extension = LoadExtension(test_dir.UnpackedPath()); |
| 769 | ASSERT_TRUE(extension); |
| 770 | EXPECT_TRUE(listener.WaitUntilSatisfied()); |
| 771 | } |
| 772 | |
| 773 | ui_test_utils::NavigateToURL( |
| 774 | browser(), embedded_test_server()->GetURL( |
| 775 | "/extensions/api_test/bindings/user_gesture_test.html")); |
| 776 | content::WebContents* web_contents = |
| 777 | browser()->tab_strip_model()->GetActiveWebContents(); |
| 778 | ASSERT_TRUE(web_contents); |
| 779 | |
| 780 | { |
| 781 | ExtensionTestMessageListener listener("got reply", false); |
| 782 | listener.set_failure_message("no user gesture"); |
| 783 | MouseDownInWebContents(web_contents); |
| 784 | EXPECT_TRUE(listener.WaitUntilSatisfied()); |
| 785 | } |
| 786 | |
| 787 | MouseUpInWebContents(web_contents); |
| 788 | |
| 789 | EXPECT_EQ("success", |
| 790 | content::EvalJs(web_contents, "window.getEnteredFullscreen", |
| 791 | content::EXECUTE_SCRIPT_NO_USER_GESTURE)); |
| 792 | } |
| 793 | |
| 794 | // Tests that a web page can consume a user gesture after an extension calls a |
| 795 | // method and receives the response in the callback. |
| 796 | // Regression test for https://crbug.com/921141. |
| 797 | IN_PROC_BROWSER_TEST_P(ExtensionBindingsUserGestureTest, |
| 798 | WebUserGestureAfterApiCallback) { |
| 799 | TestExtensionDir test_dir; |
| 800 | test_dir.WriteManifest( |
| 801 | R"({ |
| 802 | "name": "User Gesture Messaging Test", |
| 803 | "version": "0.1", |
| 804 | "manifest_version": 2, |
| 805 | "content_scripts": [{ |
| 806 | "matches": ["*://*/*"], |
| 807 | "js": ["content_script.js"], |
| 808 | "run_at": "document_start" |
| 809 | }], |
| 810 | "permissions": ["storage"] |
| 811 | })"); |
| 812 | test_dir.WriteFile(FILE_PATH_LITERAL("content_script.js"), |
| 813 | R"(window.addEventListener('mousedown', () => { |
| 814 | chrome.storage.local.get('foo', () => { |
| 815 | let message = chrome.test.isProcessingUserGesture() ? |
| 816 | 'got reply' : 'no user gesture'; |
| 817 | chrome.test.sendMessage(message); |
| 818 | }); |
| 819 | });)"); |
| 820 | |
| 821 | const Extension* extension = LoadExtension(test_dir.UnpackedPath()); |
| 822 | ASSERT_TRUE(extension); |
| 823 | |
| 824 | ui_test_utils::NavigateToURL( |
| 825 | browser(), embedded_test_server()->GetURL( |
| 826 | "/extensions/api_test/bindings/user_gesture_test.html")); |
| 827 | content::WebContents* web_contents = |
| 828 | browser()->tab_strip_model()->GetActiveWebContents(); |
| 829 | ASSERT_TRUE(web_contents); |
| 830 | |
| 831 | { |
| 832 | ExtensionTestMessageListener listener("got reply", false); |
| 833 | listener.set_failure_message("no user gesture"); |
| 834 | MouseDownInWebContents(web_contents); |
| 835 | EXPECT_TRUE(listener.WaitUntilSatisfied()); |
| 836 | } |
| 837 | |
| 838 | MouseUpInWebContents(web_contents); |
| 839 | |
| 840 | EXPECT_EQ("success", |
| 841 | content::EvalJs(web_contents, "window.getEnteredFullscreen", |
| 842 | content::EXECUTE_SCRIPT_NO_USER_GESTURE)); |
| 843 | } |
| 844 | |
Devlin Cronin | d900688a | 2019-02-01 01:36:11 | [diff] [blame] | 845 | // Tests that bindings are properly instantiated for a window navigated to an |
| 846 | // extension URL after being opened with an undefined URL. |
| 847 | // Regression test for https://crbug.com/925118. |
Devlin Cronin | 242d19d2 | 2019-03-12 18:08:48 | [diff] [blame] | 848 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, |
Devlin Cronin | d900688a | 2019-02-01 01:36:11 | [diff] [blame] | 849 | TestBindingsAvailableWithNavigatedBlankWindow) { |
| 850 | constexpr char kManifest[] = |
| 851 | R"({ |
| 852 | "name": "chrome.runtime bug checker", |
| 853 | "description": "test case for crbug.com/925118", |
| 854 | "version": "0", |
| 855 | "manifest_version": 2 |
| 856 | })"; |
| 857 | constexpr char kOpenerHTML[] = |
| 858 | R"(<!DOCTYPE html> |
| 859 | <html> |
| 860 | <head> |
| 861 | <script src='opener.js'></script> |
| 862 | </head> |
| 863 | <body> |
| 864 | </body> |
| 865 | </html>)"; |
| 866 | // opener.js opens a blank window and then navigates it to an extension URL |
| 867 | // (where extension APIs should be available). |
| 868 | constexpr char kOpenerJS[] = |
| 869 | R"(const url = chrome.runtime.getURL('/page.html'); |
| 870 | const win = window.open(undefined, ''); |
| 871 | win.location = url; |
| 872 | chrome.test.notifyPass())"; |
| 873 | constexpr char kPageHTML[] = |
| 874 | R"(<!DOCTYPE html> |
| 875 | <html> |
| 876 | This space intentionally left blank. |
| 877 | </html>)"; |
| 878 | TestExtensionDir extension_dir; |
| 879 | extension_dir.WriteManifest(kManifest); |
| 880 | extension_dir.WriteFile(FILE_PATH_LITERAL("opener.html"), kOpenerHTML); |
| 881 | extension_dir.WriteFile(FILE_PATH_LITERAL("opener.js"), kOpenerJS); |
| 882 | extension_dir.WriteFile(FILE_PATH_LITERAL("page.html"), kPageHTML); |
| 883 | |
| 884 | const Extension* extension = LoadExtension(extension_dir.UnpackedPath()); |
| 885 | const GURL target_url = extension->GetResourceURL("page.html"); |
| 886 | |
| 887 | ResultCatcher catcher; |
| 888 | content::TestNavigationObserver observer(target_url); |
| 889 | observer.StartWatchingNewWebContents(); |
| 890 | ui_test_utils::NavigateToURL(browser(), |
| 891 | extension->GetResourceURL("opener.html")); |
| 892 | EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 893 | observer.Wait(); |
| 894 | EXPECT_TRUE(observer.last_navigation_succeeded()); |
| 895 | |
| 896 | content::WebContents* web_contents = |
| 897 | browser()->tab_strip_model()->GetActiveWebContents(); |
| 898 | EXPECT_EQ(target_url, web_contents->GetLastCommittedURL()); |
| 899 | |
| 900 | // Check whether bindings are available. They should be. |
| 901 | constexpr char kScript[] = |
| 902 | R"(let message; |
| 903 | if (!chrome.runtime) |
| 904 | message = 'Runtime not defined'; |
| 905 | else if (!chrome.tabs) |
| 906 | message = 'Tabs not defined'; |
| 907 | else |
| 908 | message = 'success'; |
| 909 | domAutomationController.send(message);)"; |
| 910 | std::string result; |
| 911 | // Note: Can't use EvalJs() because of CSP in extension pages. |
| 912 | EXPECT_TRUE( |
| 913 | content::ExecuteScriptAndExtractString(web_contents, kScript, &result)); |
| 914 | EXPECT_EQ("success", result); |
| 915 | } |
| 916 | |
Devlin Cronin | a08e7d26 | 2019-04-08 17:29:59 | [diff] [blame] | 917 | // Tests the aliasing of chrome.extension methods to their chrome.runtime |
| 918 | // equivalents. |
| 919 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, |
| 920 | ChromeExtensionIsAliasedToChromeRuntime) { |
| 921 | constexpr char kManifest[] = |
| 922 | R"({ |
| 923 | "name": "Test", |
| 924 | "version": "0.1", |
| 925 | "manifest_version": 2, |
| 926 | "background": { "scripts": ["background.js"] } |
| 927 | })"; |
| 928 | constexpr char kBackground[] = |
| 929 | R"(chrome.test.runTests([ |
| 930 | function chromeExtensionIsAliased() { |
| 931 | // Sanity check: chrome.extension is directly aliased to |
| 932 | // chrome.runtime. |
| 933 | chrome.test.assertTrue(!!chrome.runtime); |
| 934 | chrome.test.assertTrue(!!chrome.runtime.sendMessage); |
| 935 | chrome.test.assertEq(chrome.runtime.sendMessage, |
| 936 | chrome.extension.sendMessage); |
| 937 | chrome.test.succeed(); |
| 938 | }, |
| 939 | function testOverridingFailsGracefully() { |
| 940 | let intercepted = false; |
| 941 | // Modify the chrome.runtime object, which is the source for the |
| 942 | // chrome.extension API, to throw an error when sendMessage is |
| 943 | // accessed. Nothing should blow up. |
| 944 | // Regression test for https://crbug.com/949170. |
| 945 | Object.defineProperty( |
| 946 | chrome.runtime, |
| 947 | 'sendMessage', |
| 948 | { |
| 949 | get() { |
| 950 | intercepted = true; |
| 951 | throw new Error('Mwahaha'); |
| 952 | } |
| 953 | }); |
| 954 | chrome.extension.sendMessage; |
| 955 | chrome.test.assertTrue(intercepted); |
| 956 | chrome.test.succeed(); |
| 957 | } |
| 958 | ]);)"; |
| 959 | |
| 960 | TestExtensionDir extension_dir; |
| 961 | extension_dir.WriteManifest(kManifest); |
| 962 | extension_dir.WriteFile(FILE_PATH_LITERAL("background.js"), kBackground); |
| 963 | ResultCatcher catcher; |
| 964 | ASSERT_TRUE(LoadExtension(extension_dir.UnpackedPath())); |
| 965 | EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 966 | } |
| 967 | |
Devlin Cronin | e80e6fc1 | 2019-02-05 00:44:23 | [diff] [blame] | 968 | INSTANTIATE_TEST_SUITE_P(, |
| 969 | ExtensionBindingsUserGestureTest, |
Devlin Cronin | 242d19d2 | 2019-03-12 18:08:48 | [diff] [blame] | 970 | ::testing::Values(kUserActivationV1, |
| 971 | kUserActivationV2)); |
Devlin Cronin | c3a1e507 | 2017-08-17 17:02:49 | [diff] [blame] | 972 | |
[email protected] | adafe5b | 2013-08-09 10:35:04 | [diff] [blame] | 973 | } // namespace |
| 974 | } // namespace extensions |