| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Contains holistic tests of the bindings infrastructure | 5 // Contains holistic tests of the bindings infrastructure |
| 6 | 6 |
| 7 #include "chrome/browser/extensions/api/permissions/permissions_api.h" | 7 #include "chrome/browser/extensions/api/permissions/permissions_api.h" |
| 8 #include "chrome/browser/extensions/extension_apitest.h" | 8 #include "chrome/browser/extensions/extension_apitest.h" |
| 9 #include "chrome/browser/net/url_request_mock_util.h" | 9 #include "chrome/browser/net/url_request_mock_util.h" |
| 10 #include "chrome/browser/ui/browser.h" | 10 #include "chrome/browser/ui/browser.h" |
| 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 12 #include "chrome/common/chrome_switches.h" | 12 #include "chrome/common/chrome_switches.h" |
| 13 #include "chrome/test/base/ui_test_utils.h" | 13 #include "chrome/test/base/ui_test_utils.h" |
| 14 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 15 #include "content/public/test/browser_test_utils.h" | 15 #include "content/public/test/browser_test_utils.h" |
| 16 #include "extensions/browser/extension_host.h" | 16 #include "extensions/browser/extension_host.h" |
| 17 #include "extensions/browser/process_manager.h" | 17 #include "extensions/browser/process_manager.h" |
| 18 #include "extensions/test/extension_test_message_listener.h" | 18 #include "extensions/test/extension_test_message_listener.h" |
| 19 #include "extensions/test/result_catcher.h" | 19 #include "extensions/test/result_catcher.h" |
| 20 #include "net/dns/mock_host_resolver.h" |
| 20 #include "net/test/embedded_test_server/embedded_test_server.h" | 21 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 21 | 22 |
| 22 namespace extensions { | 23 namespace extensions { |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 class ExtensionBindingsApiTest : public ExtensionApiTest { | 26 class ExtensionBindingsApiTest : public ExtensionApiTest { |
| 26 public: | 27 public: |
| 27 void SetUpOnMainThread() override { | 28 void SetUpOnMainThread() override { |
| 28 ExtensionApiTest::SetUpOnMainThread(); | 29 ExtensionApiTest::SetUpOnMainThread(); |
| 30 host_resolver()->AddRule("*", "127.0.0.1"); |
| 31 ASSERT_TRUE(StartEmbeddedTestServer()); |
| 32 |
| 29 content::BrowserThread::PostTask( | 33 content::BrowserThread::PostTask( |
| 30 content::BrowserThread::IO, FROM_HERE, | 34 content::BrowserThread::IO, FROM_HERE, |
| 31 base::BindOnce(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); | 35 base::BindOnce(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); |
| 32 } | 36 } |
| 33 }; | 37 }; |
| 34 | 38 |
| 35 IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, | 39 IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, |
| 36 UnavailableBindingsNeverRegistered) { | 40 UnavailableBindingsNeverRegistered) { |
| 37 // Test will request the 'storage' permission. | 41 // Test will request the 'storage' permission. |
| 38 PermissionsRequestFunction::SetIgnoreUserGestureForTests(true); | 42 PermissionsRequestFunction::SetIgnoreUserGestureForTests(true); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 ASSERT_TRUE(RunExtensionTest("bindings/api_enums")) << message_; | 114 ASSERT_TRUE(RunExtensionTest("bindings/api_enums")) << message_; |
| 111 }; | 115 }; |
| 112 | 116 |
| 113 // Regression test for http://crbug.com/504011 - proper access checks on | 117 // Regression test for http://crbug.com/504011 - proper access checks on |
| 114 // getModuleSystem(). | 118 // getModuleSystem(). |
| 115 IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, ModuleSystem) { | 119 IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, ModuleSystem) { |
| 116 ASSERT_TRUE(RunExtensionTest("bindings/module_system")) << message_; | 120 ASSERT_TRUE(RunExtensionTest("bindings/module_system")) << message_; |
| 117 } | 121 } |
| 118 | 122 |
| 119 IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, NoExportOverriding) { | 123 IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, NoExportOverriding) { |
| 120 ASSERT_TRUE(embedded_test_server()->Start()); | |
| 121 | |
| 122 // We need to create runtime bindings in the web page. An extension that's | 124 // We need to create runtime bindings in the web page. An extension that's |
| 123 // externally connectable will do that for us. | 125 // externally connectable will do that for us. |
| 124 ASSERT_TRUE(LoadExtension( | 126 ASSERT_TRUE(LoadExtension( |
| 125 test_data_dir_.AppendASCII("bindings") | 127 test_data_dir_.AppendASCII("bindings") |
| 126 .AppendASCII("externally_connectable_everywhere"))); | 128 .AppendASCII("externally_connectable_everywhere"))); |
| 127 | 129 |
| 128 ui_test_utils::NavigateToURL( | 130 ui_test_utils::NavigateToURL( |
| 129 browser(), | 131 browser(), |
| 130 embedded_test_server()->GetURL( | 132 embedded_test_server()->GetURL( |
| 131 "/extensions/api_test/bindings/override_exports.html")); | 133 "/extensions/api_test/bindings/override_exports.html")); |
| 132 | 134 |
| 133 // See chrome/test/data/extensions/api_test/bindings/override_exports.html. | 135 // See chrome/test/data/extensions/api_test/bindings/override_exports.html. |
| 134 std::string result; | 136 std::string result; |
| 135 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | 137 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 136 browser()->tab_strip_model()->GetActiveWebContents(), | 138 browser()->tab_strip_model()->GetActiveWebContents(), |
| 137 "window.domAutomationController.send(" | 139 "window.domAutomationController.send(" |
| 138 "document.getElementById('status').textContent.trim());", | 140 "document.getElementById('status').textContent.trim());", |
| 139 &result)); | 141 &result)); |
| 140 EXPECT_EQ("success", result); | 142 EXPECT_EQ("success", result); |
| 141 } | 143 } |
| 142 | 144 |
| 143 IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, NoGinDefineOverriding) { | 145 IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, NoGinDefineOverriding) { |
| 144 ASSERT_TRUE(embedded_test_server()->Start()); | |
| 145 | |
| 146 // We need to create runtime bindings in the web page. An extension that's | 146 // We need to create runtime bindings in the web page. An extension that's |
| 147 // externally connectable will do that for us. | 147 // externally connectable will do that for us. |
| 148 ASSERT_TRUE(LoadExtension( | 148 ASSERT_TRUE(LoadExtension( |
| 149 test_data_dir_.AppendASCII("bindings") | 149 test_data_dir_.AppendASCII("bindings") |
| 150 .AppendASCII("externally_connectable_everywhere"))); | 150 .AppendASCII("externally_connectable_everywhere"))); |
| 151 | 151 |
| 152 ui_test_utils::NavigateToURL( | 152 ui_test_utils::NavigateToURL( |
| 153 browser(), | 153 browser(), |
| 154 embedded_test_server()->GetURL( | 154 embedded_test_server()->GetURL( |
| 155 "/extensions/api_test/bindings/override_gin_define.html")); | 155 "/extensions/api_test/bindings/override_gin_define.html")); |
| 156 ASSERT_FALSE( | 156 ASSERT_FALSE( |
| 157 browser()->tab_strip_model()->GetActiveWebContents()->IsCrashed()); | 157 browser()->tab_strip_model()->GetActiveWebContents()->IsCrashed()); |
| 158 | 158 |
| 159 // See chrome/test/data/extensions/api_test/bindings/override_gin_define.html. | 159 // See chrome/test/data/extensions/api_test/bindings/override_gin_define.html. |
| 160 std::string result; | 160 std::string result; |
| 161 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | 161 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 162 browser()->tab_strip_model()->GetActiveWebContents(), | 162 browser()->tab_strip_model()->GetActiveWebContents(), |
| 163 "window.domAutomationController.send(" | 163 "window.domAutomationController.send(" |
| 164 "document.getElementById('status').textContent.trim());", | 164 "document.getElementById('status').textContent.trim());", |
| 165 &result)); | 165 &result)); |
| 166 EXPECT_EQ("success", result); | 166 EXPECT_EQ("success", result); |
| 167 } | 167 } |
| 168 | 168 |
| 169 IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, HandlerFunctionTypeChecking) { | 169 IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, HandlerFunctionTypeChecking) { |
| 170 ASSERT_TRUE(embedded_test_server()->Start()); | |
| 171 ui_test_utils::NavigateToURL( | 170 ui_test_utils::NavigateToURL( |
| 172 browser(), | 171 browser(), |
| 173 embedded_test_server()->GetURL( | 172 embedded_test_server()->GetURL( |
| 174 "/extensions/api_test/bindings/handler_function_type_checking.html")); | 173 "/extensions/api_test/bindings/handler_function_type_checking.html")); |
| 175 content::WebContents* web_contents = | 174 content::WebContents* web_contents = |
| 176 browser()->tab_strip_model()->GetActiveWebContents(); | 175 browser()->tab_strip_model()->GetActiveWebContents(); |
| 177 EXPECT_FALSE(web_contents->IsCrashed()); | 176 EXPECT_FALSE(web_contents->IsCrashed()); |
| 178 // See handler_function_type_checking.html. | 177 // See handler_function_type_checking.html. |
| 179 std::string result; | 178 std::string result; |
| 180 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | 179 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 181 web_contents, | 180 web_contents, |
| 182 "window.domAutomationController.send(" | 181 "window.domAutomationController.send(" |
| 183 "document.getElementById('status').textContent.trim());", | 182 "document.getElementById('status').textContent.trim());", |
| 184 &result)); | 183 &result)); |
| 185 EXPECT_EQ("success", result); | 184 EXPECT_EQ("success", result); |
| 186 } | 185 } |
| 187 | 186 |
| 188 IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, | 187 IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, |
| 189 MoreNativeFunctionInterceptionTests) { | 188 MoreNativeFunctionInterceptionTests) { |
| 190 ASSERT_TRUE(embedded_test_server()->Start()); | |
| 191 | |
| 192 // We need to create runtime bindings in the web page. An extension that's | 189 // We need to create runtime bindings in the web page. An extension that's |
| 193 // externally connectable will do that for us. | 190 // externally connectable will do that for us. |
| 194 ASSERT_TRUE( | 191 ASSERT_TRUE( |
| 195 LoadExtension(test_data_dir_.AppendASCII("bindings") | 192 LoadExtension(test_data_dir_.AppendASCII("bindings") |
| 196 .AppendASCII("externally_connectable_everywhere"))); | 193 .AppendASCII("externally_connectable_everywhere"))); |
| 197 | 194 |
| 198 ui_test_utils::NavigateToURL( | 195 ui_test_utils::NavigateToURL( |
| 199 browser(), | 196 browser(), |
| 200 embedded_test_server()->GetURL( | 197 embedded_test_server()->GetURL( |
| 201 "/extensions/api_test/bindings/function_interceptions.html")); | 198 "/extensions/api_test/bindings/function_interceptions.html")); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 ExtensionTestMessageListener receiver_ready("receiver_ready", false); | 235 ExtensionTestMessageListener receiver_ready("receiver_ready", false); |
| 239 const Extension* receiver = | 236 const Extension* receiver = |
| 240 LoadExtension(test_data_dir_.AppendASCII("bindings") | 237 LoadExtension(test_data_dir_.AppendASCII("bindings") |
| 241 .AppendASCII("external_message_listener")); | 238 .AppendASCII("external_message_listener")); |
| 242 ASSERT_NE(nullptr, receiver); | 239 ASSERT_NE(nullptr, receiver); |
| 243 ASSERT_TRUE(receiver_ready.WaitUntilSatisfied()); | 240 ASSERT_TRUE(receiver_ready.WaitUntilSatisfied()); |
| 244 | 241 |
| 245 // Load the web page which tries to impersonate the sender extension via | 242 // Load the web page which tries to impersonate the sender extension via |
| 246 // scripting iframes/child windows before they finish navigating to pages | 243 // scripting iframes/child windows before they finish navigating to pages |
| 247 // within the sender extension. | 244 // within the sender extension. |
| 248 ASSERT_TRUE(embedded_test_server()->Start()); | |
| 249 ui_test_utils::NavigateToURL( | 245 ui_test_utils::NavigateToURL( |
| 250 browser(), | 246 browser(), |
| 251 embedded_test_server()->GetURL( | 247 embedded_test_server()->GetURL( |
| 252 "/extensions/api_test/bindings/frames_before_navigation.html")); | 248 "/extensions/api_test/bindings/frames_before_navigation.html")); |
| 253 | 249 |
| 254 bool page_success = false; | 250 bool page_success = false; |
| 255 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( | 251 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( |
| 256 browser()->tab_strip_model()->GetWebContentsAt(0), "getResult()", | 252 browser()->tab_strip_model()->GetWebContentsAt(0), "getResult()", |
| 257 &page_success)); | 253 &page_success)); |
| 258 EXPECT_TRUE(page_success); | 254 EXPECT_TRUE(page_success); |
| 259 | 255 |
| 260 // Reply to |sender|, causing it to send a message over to |receiver|, and | 256 // Reply to |sender|, causing it to send a message over to |receiver|, and |
| 261 // then ask |receiver| for the total message count. It should be 1 since | 257 // then ask |receiver| for the total message count. It should be 1 since |
| 262 // |receiver| should not have received any impersonated messages. | 258 // |receiver| should not have received any impersonated messages. |
| 263 sender_ready.Reply(receiver->id()); | 259 sender_ready.Reply(receiver->id()); |
| 264 int message_count = 0; | 260 int message_count = 0; |
| 265 ASSERT_TRUE(content::ExecuteScriptAndExtractInt( | 261 ASSERT_TRUE(content::ExecuteScriptAndExtractInt( |
| 266 ProcessManager::Get(profile()) | 262 ProcessManager::Get(profile()) |
| 267 ->GetBackgroundHostForExtension(receiver->id()) | 263 ->GetBackgroundHostForExtension(receiver->id()) |
| 268 ->host_contents(), | 264 ->host_contents(), |
| 269 "getMessageCountAfterReceivingRealSenderMessage()", &message_count)); | 265 "getMessageCountAfterReceivingRealSenderMessage()", &message_count)); |
| 270 EXPECT_EQ(1, message_count); | 266 EXPECT_EQ(1, message_count); |
| 271 } | 267 } |
| 272 | 268 |
| 273 IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, TestFreezingChrome) { | 269 IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, TestFreezingChrome) { |
| 274 ASSERT_TRUE(embedded_test_server()->Start()); | |
| 275 ui_test_utils::NavigateToURL( | 270 ui_test_utils::NavigateToURL( |
| 276 browser(), embedded_test_server()->GetURL( | 271 browser(), embedded_test_server()->GetURL( |
| 277 "/extensions/api_test/bindings/freeze.html")); | 272 "/extensions/api_test/bindings/freeze.html")); |
| 278 content::WebContents* web_contents = | 273 content::WebContents* web_contents = |
| 279 browser()->tab_strip_model()->GetActiveWebContents(); | 274 browser()->tab_strip_model()->GetActiveWebContents(); |
| 280 ASSERT_FALSE(web_contents->IsCrashed()); | 275 ASSERT_FALSE(web_contents->IsCrashed()); |
| 281 } | 276 } |
| 282 | 277 |
| 278 // Tests interaction with event filter parsing. |
| 279 IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, TestEventFilterParsing) { |
| 280 ExtensionTestMessageListener listener("ready", false); |
| 281 ASSERT_TRUE( |
| 282 LoadExtension(test_data_dir_.AppendASCII("bindings/event_filter"))); |
| 283 ASSERT_TRUE(listener.WaitUntilSatisfied()); |
| 284 |
| 285 ResultCatcher catcher; |
| 286 ui_test_utils::NavigateToURL( |
| 287 browser(), embedded_test_server()->GetURL("example.com", "/title1.html")); |
| 288 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 289 } |
| 290 |
| 283 } // namespace | 291 } // namespace |
| 284 } // namespace extensions | 292 } // namespace extensions |
| OLD | NEW |