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
3 // found in the LICENSE file.
5 #include "base/strings/utf_string_conversions.h"
6 #include "chrome/browser/chrome_notification_types.h"
7 #include "chrome/browser/extensions/api/permissions/permissions_api.h"
8 #include "chrome/browser/extensions/extension_apitest.h"
9 #include "chrome/browser/extensions/extension_service.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/tabs/tab_strip_model.h"
12 #include "chrome/common/chrome_switches.h"
13 #include "chrome/test/base/ui_test_utils.h"
14 #include "content/public/browser/web_contents.h"
15 #include "content/public/test/browser_test_utils.h"
16 #include "extensions/common/extension.h"
17 #include "net/dns/mock_host_resolver.h"
18 #include "net/test/embedded_test_server/embedded_test_server.h"
21 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, ContentScriptAllFrames
) {
22 ASSERT_TRUE(StartEmbeddedTestServer());
23 ASSERT_TRUE(RunExtensionTest("content_scripts/all_frames")) << message_
;
26 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, ContentScriptAboutBlankIframes
) {
27 ASSERT_TRUE(StartEmbeddedTestServer());
29 RunExtensionTest("content_scripts/about_blank_iframes")) << message_
;
32 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, ContentScriptExtensionIframe
) {
33 ASSERT_TRUE(StartEmbeddedTestServer());
34 ASSERT_TRUE(RunExtensionTest("content_scripts/extension_iframe")) << message_
;
37 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, ContentScriptExtensionProcess
) {
38 ASSERT_TRUE(StartEmbeddedTestServer());
40 RunExtensionTest("content_scripts/extension_process")) << message_
;
43 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, ContentScriptFragmentNavigation
) {
44 ASSERT_TRUE(StartEmbeddedTestServer());
45 const char* extension_name
= "content_scripts/fragment";
46 ASSERT_TRUE(RunExtensionTest(extension_name
)) << message_
;
49 // Times out on Linux: http://crbug.com/163097
51 #define MAYBE_ContentScriptIsolatedWorlds DISABLED_ContentScriptIsolatedWorlds
53 #define MAYBE_ContentScriptIsolatedWorlds ContentScriptIsolatedWorlds
55 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, MAYBE_ContentScriptIsolatedWorlds
) {
56 // This extension runs various bits of script and tests that they all run in
57 // the same isolated world.
58 ASSERT_TRUE(StartEmbeddedTestServer());
59 ASSERT_TRUE(RunExtensionTest("content_scripts/isolated_world1")) << message_
;
61 // Now load a different extension, inject into same page, verify worlds aren't
63 ASSERT_TRUE(RunExtensionTest("content_scripts/isolated_world2")) << message_
;
66 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, ContentScriptIgnoreHostPermissions
) {
67 host_resolver()->AddRule("a.com", "127.0.0.1");
68 host_resolver()->AddRule("b.com", "127.0.0.1");
69 ASSERT_TRUE(StartEmbeddedTestServer());
70 ASSERT_TRUE(RunExtensionTest(
71 "content_scripts/dont_match_host_permissions")) << message_
;
74 // crbug.com/39249 -- content scripts js should not run on view source.
75 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, ContentScriptViewSource
) {
76 ASSERT_TRUE(StartEmbeddedTestServer());
77 ASSERT_TRUE(RunExtensionTest("content_scripts/view_source")) << message_
;
80 // crbug.com/126257 -- content scripts should not get injected into other
82 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, ContentScriptOtherExtensions
) {
83 host_resolver()->AddRule("a.com", "127.0.0.1");
84 ASSERT_TRUE(StartEmbeddedTestServer());
85 // First, load extension that sets up content script.
86 ASSERT_TRUE(RunExtensionTest("content_scripts/other_extensions/injector"))
88 // Then load targeted extension to make sure its content isn't changed.
89 ASSERT_TRUE(RunExtensionTest("content_scripts/other_extensions/victim"))
94 IN_PROC_BROWSER_TEST_F(
96 DISABLED_ContentScriptStylesInjectedIntoExistingRenderers
) {
97 ASSERT_TRUE(StartEmbeddedTestServer());
99 content::WindowedNotificationObserver
signal(
100 chrome::NOTIFICATION_USER_SCRIPTS_UPDATED
,
101 content::Source
<Profile
>(browser()->profile()));
103 // Start with a renderer already open at a URL.
104 GURL
url(test_server()->GetURL("file/extensions/test_file.html"));
105 ui_test_utils::NavigateToURL(browser(), url
);
108 test_data_dir_
.AppendASCII("content_scripts/existing_renderers"));
112 // And check that its styles were affected by the styles that just got loaded.
113 bool styles_injected
;
114 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
115 browser()->tab_strip_model()->GetActiveWebContents(),
116 "window.domAutomationController.send("
117 " document.defaultView.getComputedStyle(document.body, null)."
118 " getPropertyValue('background-color') == 'rgb(255, 0, 0)')",
120 ASSERT_TRUE(styles_injected
);
123 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
,
124 ContentScriptCSSLocalization
) {
125 ASSERT_TRUE(StartEmbeddedTestServer());
126 ASSERT_TRUE(RunExtensionTest("content_scripts/css_l10n")) << message_
;
129 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, ContentScriptExtensionAPIs
) {
130 ASSERT_TRUE(StartEmbeddedTestServer());
132 const extensions::Extension
* extension
= LoadExtension(
133 test_data_dir_
.AppendASCII("content_scripts/extension_api"));
135 ResultCatcher catcher
;
136 ui_test_utils::NavigateToURL(
138 embedded_test_server()->GetURL(
139 "/extensions/api_test/content_scripts/extension_api/functions.html"));
140 EXPECT_TRUE(catcher
.GetNextResult());
142 // Navigate to a page that will cause a content script to run that starts
143 // listening for an extension event.
144 ui_test_utils::NavigateToURL(
146 embedded_test_server()->GetURL(
147 "/extensions/api_test/content_scripts/extension_api/events.html"));
149 // Navigate to an extension page that will fire the event events.js is
151 ui_test_utils::NavigateToURLWithDisposition(
152 browser(), extension
->GetResourceURL("fire_event.html"),
153 NEW_FOREGROUND_TAB
, ui_test_utils::BROWSER_TEST_NONE
);
154 EXPECT_TRUE(catcher
.GetNextResult());
157 // Flaky on Windows. http://crbug.com/248418
159 #define MAYBE_ContentScriptPermissionsApi DISABLED_ContentScriptPermissionsApi
161 #define MAYBE_ContentScriptPermissionsApi ContentScriptPermissionsApi
163 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, MAYBE_ContentScriptPermissionsApi
) {
164 extensions::PermissionsRequestFunction::SetIgnoreUserGestureForTests(true);
165 extensions::PermissionsRequestFunction::SetAutoConfirmForTests(true);
166 host_resolver()->AddRule("*.com", "127.0.0.1");
167 ASSERT_TRUE(StartEmbeddedTestServer());
168 ASSERT_TRUE(RunExtensionTest("content_scripts/permissions")) << message_
;
171 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, ContentScriptBypassPageCSP
) {
172 ASSERT_TRUE(StartEmbeddedTestServer());
173 ASSERT_TRUE(RunExtensionTest("content_scripts/bypass_page_csp")) << message_
;