blob: d99e574ffb78f3034d52e1810b3ebb61151e82f4 [file] [log] [blame]
[email protected]fad73672012-06-15 23:26:061// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]c80b8ee2011-12-03 04:26:522// 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]7eef3942013-08-14 02:53:497#include "chrome/browser/extensions/api/permissions/permissions_api.h"
[email protected]c80b8ee2011-12-03 04:26:528#include "chrome/browser/extensions/extension_apitest.h"
jochen7923c2a2015-07-14 10:04:459#include "chrome/browser/net/url_request_mock_util.h"
[email protected]fad73672012-06-15 23:26:0610#include "chrome/browser/ui/browser.h"
rdevlin.cronin83a4b3a2015-10-28 21:43:5811#include "chrome/browser/ui/tabs/tab_strip_model.h"
12#include "chrome/test/base/ui_test_utils.h"
jochen7923c2a2015-07-14 10:04:4513#include "content/public/browser/browser_thread.h"
[email protected]7d478cb2012-07-24 17:19:4214#include "content/public/test/browser_test_utils.h"
[email protected]22401dc2014-03-21 01:38:5715#include "extensions/browser/extension_host.h"
[email protected]98b6d942013-11-10 00:34:0716#include "extensions/browser/process_manager.h"
lfg910f2f92014-09-19 05:31:0917#include "extensions/test/extension_test_message_listener.h"
yoze8dc2f12014-09-09 23:16:3218#include "extensions/test/result_catcher.h"
rdevlin.cronin83a4b3a2015-10-28 21:43:5819#include "net/test/embedded_test_server/embedded_test_server.h"
[email protected]fad73672012-06-15 23:26:0620
[email protected]adafe5b2013-08-09 10:35:0421namespace extensions {
22namespace {
23
jochen7923c2a2015-07-14 10:04:4524class 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]adafe5b2013-08-09 10:35:0432
33IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest,
[email protected]7eef3942013-08-14 02:53:4934 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
41IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest,
[email protected]adafe5b2013-08-09 10:35:0442 ExceptionInHandlerShouldNotCrash) {
[email protected]c80b8ee2011-12-03 04:26:5243 ASSERT_TRUE(RunExtensionSubtest(
44 "bindings/exception_in_handler_should_not_crash",
45 "page.html")) << message_;
46}
[email protected]fad73672012-06-15 23:26:0647
48// Tests that an error raised during an async function still fires
[email protected]754ea8b72013-01-08 15:10:3149// the callback, but sets chrome.runtime.lastError.
[email protected]fc034482013-08-09 20:25:1450// FIXME should be in ExtensionBindingsApiTest.
[email protected]fad73672012-06-15 23:26:0651IN_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]98b6d942013-11-10 00:34:0756 extensions::ProcessManager* manager =
reillyg0ea3fa902014-10-28 15:30:2357 extensions::ProcessManager::Get(browser()->profile());
[email protected]3a1dc572012-07-31 22:25:1358 extensions::ExtensionHost* host = FindHostWithPath(manager, "/bg.html", 1);
[email protected]fad73672012-06-15 23:26:0659
60 bool result = false;
[email protected]b6987e02013-01-04 18:30:4361 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
62 host->render_view_host(), "testLastError()", &result));
[email protected]fad73672012-06-15 23:26:0663 EXPECT_TRUE(result);
64}
[email protected]52eafbd2013-04-03 04:43:1965
[email protected]adafe5b2013-08-09 10:35:0466// Regression test that we don't delete our own bindings with about:blank
67// iframes.
68IN_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
86IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest,
87 InternalAPIsNotOnChromeObject) {
[email protected]52eafbd2013-04-03 04:43:1988 ASSERT_TRUE(RunExtensionSubtest(
89 "bindings/internal_apis_not_on_chrome_object",
90 "page.html")) << message_;
91}
[email protected]adafe5b2013-08-09 10:35:0492
[email protected]fc034482013-08-09 20:25:1493// Tests that we don't override events when bindings are re-injected.
94// Regression test for http://crbug.com/269149.
rpaquay96bf3b7d2014-11-26 00:19:0895// Regression test for http://crbug.com/436593.
[email protected]fc034482013-08-09 20:25:1496IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, EventOverriding) {
97 ASSERT_TRUE(RunExtensionTest("bindings/event_overriding")) << message_;
98}
99
kalman1bd5b182015-01-13 19:01:18100// Tests the effectiveness of the 'nocompile' feature file property.
101// Regression test for http://crbug.com/356133.
102IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, Nocompile) {
103 ASSERT_TRUE(RunExtensionSubtest("bindings/nocompile", "page.html"))
104 << message_;
105}
106
rdevlin.cronin2ba3c88d2015-03-03 01:18:22107IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, ApiEnums) {
108 ASSERT_TRUE(RunExtensionTest("bindings/api_enums")) << message_;
109};
110
jochen7923c2a2015-07-14 10:04:45111// Regression test for http://crbug.com/504011 - proper access checks on
112// getModuleSystem().
113IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, ModuleSystem) {
114 ASSERT_TRUE(RunExtensionTest("bindings/module_system")) << message_;
115}
116
rdevlin.cronin83a4b3a2015-10-28 21:43:58117IN_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.cronin415b73b2015-11-13 01:14:47141IN_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.cronina5ecbc82015-10-29 23:41:29167IN_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]adafe5b2013-08-09 10:35:04186} // namespace
187} // namespace extensions