[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 | |
[email protected] | 7eef394 | 2013-08-14 02:53:49 | [diff] [blame] | 7 | #include "chrome/browser/extensions/api/permissions/permissions_api.h" |
[email protected] | c80b8ee | 2011-12-03 04:26:52 | [diff] [blame] | 8 | #include "chrome/browser/extensions/extension_apitest.h" |
jochen | 7923c2a | 2015-07-14 10:04:45 | [diff] [blame] | 9 | #include "chrome/browser/net/url_request_mock_util.h" |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 10 | #include "chrome/browser/ui/browser.h" |
rdevlin.cronin | 83a4b3a | 2015-10-28 21:43:58 | [diff] [blame] | 11 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 12 | #include "chrome/test/base/ui_test_utils.h" |
jochen | 7923c2a | 2015-07-14 10:04:45 | [diff] [blame] | 13 | #include "content/public/browser/browser_thread.h" |
[email protected] | 7d478cb | 2012-07-24 17:19:42 | [diff] [blame] | 14 | #include "content/public/test/browser_test_utils.h" |
[email protected] | 22401dc | 2014-03-21 01:38:57 | [diff] [blame] | 15 | #include "extensions/browser/extension_host.h" |
[email protected] | 98b6d94 | 2013-11-10 00:34:07 | [diff] [blame] | 16 | #include "extensions/browser/process_manager.h" |
lfg | 910f2f9 | 2014-09-19 05:31:09 | [diff] [blame] | 17 | #include "extensions/test/extension_test_message_listener.h" |
yoz | e8dc2f1 | 2014-09-09 23:16:32 | [diff] [blame] | 18 | #include "extensions/test/result_catcher.h" |
rdevlin.cronin | 83a4b3a | 2015-10-28 21:43:58 | [diff] [blame] | 19 | #include "net/test/embedded_test_server/embedded_test_server.h" |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 20 | |
[email protected] | adafe5b | 2013-08-09 10:35:04 | [diff] [blame] | 21 | namespace extensions { |
| 22 | namespace { |
| 23 | |
jochen | 7923c2a | 2015-07-14 10:04:45 | [diff] [blame] | 24 | class ExtensionBindingsApiTest : public ExtensionApiTest { |
| 25 | public: |
| 26 | void SetUpOnMainThread() override { |
| 27 | content::BrowserThread::PostTask( |
| 28 | content::BrowserThread::IO, FROM_HERE, |
| 29 | base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); |
| 30 | } |
| 31 | }; |
[email protected] | adafe5b | 2013-08-09 10:35:04 | [diff] [blame] | 32 | |
| 33 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, |
[email protected] | 7eef394 | 2013-08-14 02:53:49 | [diff] [blame] | 34 | UnavailableBindingsNeverRegistered) { |
| 35 | // Test will request the 'storage' permission. |
| 36 | PermissionsRequestFunction::SetIgnoreUserGestureForTests(true); |
| 37 | ASSERT_TRUE(RunExtensionTest( |
| 38 | "bindings/unavailable_bindings_never_registered")) << message_; |
| 39 | } |
| 40 | |
| 41 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, |
[email protected] | adafe5b | 2013-08-09 10:35:04 | [diff] [blame] | 42 | ExceptionInHandlerShouldNotCrash) { |
[email protected] | c80b8ee | 2011-12-03 04:26:52 | [diff] [blame] | 43 | ASSERT_TRUE(RunExtensionSubtest( |
| 44 | "bindings/exception_in_handler_should_not_crash", |
| 45 | "page.html")) << message_; |
| 46 | } |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 47 | |
| 48 | // Tests that an error raised during an async function still fires |
[email protected] | 754ea8b7 | 2013-01-08 15:10:31 | [diff] [blame] | 49 | // the callback, but sets chrome.runtime.lastError. |
[email protected] | fc03448 | 2013-08-09 20:25:14 | [diff] [blame] | 50 | // FIXME should be in ExtensionBindingsApiTest. |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 51 | IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, LastError) { |
| 52 | ASSERT_TRUE(LoadExtension( |
| 53 | test_data_dir_.AppendASCII("browsertest").AppendASCII("last_error"))); |
| 54 | |
| 55 | // Get the ExtensionHost that is hosting our background page. |
[email protected] | 98b6d94 | 2013-11-10 00:34:07 | [diff] [blame] | 56 | extensions::ProcessManager* manager = |
reillyg | 0ea3fa90 | 2014-10-28 15:30:23 | [diff] [blame] | 57 | extensions::ProcessManager::Get(browser()->profile()); |
[email protected] | 3a1dc57 | 2012-07-31 22:25:13 | [diff] [blame] | 58 | extensions::ExtensionHost* host = FindHostWithPath(manager, "/bg.html", 1); |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 59 | |
| 60 | bool result = false; |
[email protected] | b6987e0 | 2013-01-04 18:30:43 | [diff] [blame] | 61 | ASSERT_TRUE(content::ExecuteScriptAndExtractBool( |
| 62 | host->render_view_host(), "testLastError()", &result)); |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 63 | EXPECT_TRUE(result); |
| 64 | } |
[email protected] | 52eafbd | 2013-04-03 04:43:19 | [diff] [blame] | 65 | |
[email protected] | adafe5b | 2013-08-09 10:35:04 | [diff] [blame] | 66 | // Regression test that we don't delete our own bindings with about:blank |
| 67 | // iframes. |
| 68 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, AboutBlankIframe) { |
| 69 | ResultCatcher catcher; |
| 70 | ExtensionTestMessageListener listener("load", true); |
| 71 | |
| 72 | ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("bindings") |
| 73 | .AppendASCII("about_blank_iframe"))); |
| 74 | |
| 75 | ASSERT_TRUE(listener.WaitUntilSatisfied()); |
| 76 | |
| 77 | const Extension* extension = LoadExtension( |
| 78 | test_data_dir_.AppendASCII("bindings") |
| 79 | .AppendASCII("internal_apis_not_on_chrome_object")); |
| 80 | ASSERT_TRUE(extension); |
| 81 | listener.Reply(extension->id()); |
| 82 | |
| 83 | ASSERT_TRUE(catcher.GetNextResult()) << message_; |
| 84 | } |
| 85 | |
| 86 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, |
| 87 | InternalAPIsNotOnChromeObject) { |
[email protected] | 52eafbd | 2013-04-03 04:43:19 | [diff] [blame] | 88 | ASSERT_TRUE(RunExtensionSubtest( |
| 89 | "bindings/internal_apis_not_on_chrome_object", |
| 90 | "page.html")) << message_; |
| 91 | } |
[email protected] | adafe5b | 2013-08-09 10:35:04 | [diff] [blame] | 92 | |
[email protected] | fc03448 | 2013-08-09 20:25:14 | [diff] [blame] | 93 | // Tests that we don't override events when bindings are re-injected. |
| 94 | // Regression test for http://crbug.com/269149. |
rpaquay | 96bf3b7d | 2014-11-26 00:19:08 | [diff] [blame] | 95 | // Regression test for http://crbug.com/436593. |
[email protected] | fc03448 | 2013-08-09 20:25:14 | [diff] [blame] | 96 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, EventOverriding) { |
| 97 | ASSERT_TRUE(RunExtensionTest("bindings/event_overriding")) << message_; |
| 98 | } |
| 99 | |
kalman | 1bd5b18 | 2015-01-13 19:01:18 | [diff] [blame] | 100 | // Tests the effectiveness of the 'nocompile' feature file property. |
| 101 | // Regression test for http://crbug.com/356133. |
| 102 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, Nocompile) { |
| 103 | ASSERT_TRUE(RunExtensionSubtest("bindings/nocompile", "page.html")) |
| 104 | << message_; |
| 105 | } |
| 106 | |
rdevlin.cronin | 2ba3c88d | 2015-03-03 01:18:22 | [diff] [blame] | 107 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, ApiEnums) { |
| 108 | ASSERT_TRUE(RunExtensionTest("bindings/api_enums")) << message_; |
| 109 | }; |
| 110 | |
jochen | 7923c2a | 2015-07-14 10:04:45 | [diff] [blame] | 111 | // Regression test for http://crbug.com/504011 - proper access checks on |
| 112 | // getModuleSystem(). |
| 113 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, ModuleSystem) { |
| 114 | ASSERT_TRUE(RunExtensionTest("bindings/module_system")) << message_; |
| 115 | } |
| 116 | |
rdevlin.cronin | 83a4b3a | 2015-10-28 21:43:58 | [diff] [blame] | 117 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, NoExportOverriding) { |
| 118 | ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 119 | |
| 120 | // We need to create runtime bindings in the web page. An extension that's |
| 121 | // externally connectable will do that for us. |
| 122 | ASSERT_TRUE(LoadExtension( |
| 123 | test_data_dir_.AppendASCII("bindings") |
| 124 | .AppendASCII("externally_connectable_everywhere"))); |
| 125 | |
| 126 | ui_test_utils::NavigateToURL( |
| 127 | browser(), |
| 128 | embedded_test_server()->GetURL( |
| 129 | "/extensions/api_test/bindings/override_exports.html")); |
| 130 | |
| 131 | // See chrome/test/data/extensions/api_test/bindings/override_exports.html. |
| 132 | std::string result; |
| 133 | EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 134 | browser()->tab_strip_model()->GetActiveWebContents(), |
| 135 | "window.domAutomationController.send(" |
| 136 | "document.getElementById('status').textContent.trim());", |
| 137 | &result)); |
| 138 | EXPECT_EQ("success", result); |
| 139 | } |
| 140 | |
rdevlin.cronin | 415b73b | 2015-11-13 01:14:47 | [diff] [blame^] | 141 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, NoGinDefineOverriding) { |
| 142 | ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 143 | |
| 144 | // We need to create runtime bindings in the web page. An extension that's |
| 145 | // externally connectable will do that for us. |
| 146 | ASSERT_TRUE(LoadExtension( |
| 147 | test_data_dir_.AppendASCII("bindings") |
| 148 | .AppendASCII("externally_connectable_everywhere"))); |
| 149 | |
| 150 | ui_test_utils::NavigateToURL( |
| 151 | browser(), |
| 152 | embedded_test_server()->GetURL( |
| 153 | "/extensions/api_test/bindings/override_gin_define.html")); |
| 154 | ASSERT_FALSE( |
| 155 | browser()->tab_strip_model()->GetActiveWebContents()->IsCrashed()); |
| 156 | |
| 157 | // See chrome/test/data/extensions/api_test/bindings/override_gin_define.html. |
| 158 | std::string result; |
| 159 | EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 160 | browser()->tab_strip_model()->GetActiveWebContents(), |
| 161 | "window.domAutomationController.send(" |
| 162 | "document.getElementById('status').textContent.trim());", |
| 163 | &result)); |
| 164 | EXPECT_EQ("success", result); |
| 165 | } |
| 166 | |
rdevlin.cronin | a5ecbc8 | 2015-10-29 23:41:29 | [diff] [blame] | 167 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, HandlerFunctionTypeChecking) { |
| 168 | ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 169 | ui_test_utils::NavigateToURL( |
| 170 | browser(), |
| 171 | embedded_test_server()->GetURL( |
| 172 | "/extensions/api_test/bindings/handler_function_type_checking.html")); |
| 173 | content::WebContents* web_contents = |
| 174 | browser()->tab_strip_model()->GetActiveWebContents(); |
| 175 | EXPECT_FALSE(web_contents->IsCrashed()); |
| 176 | // See handler_function_type_checking.html. |
| 177 | std::string result; |
| 178 | EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 179 | web_contents, |
| 180 | "window.domAutomationController.send(" |
| 181 | "document.getElementById('status').textContent.trim());", |
| 182 | &result)); |
| 183 | EXPECT_EQ("success", result); |
| 184 | } |
| 185 | |
[email protected] | adafe5b | 2013-08-09 10:35:04 | [diff] [blame] | 186 | } // namespace |
| 187 | } // namespace extensions |