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.
4
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/common/extensions/extension.h"
14#include "chrome/test/base/ui_test_utils.h"
15#include "content/public/browser/web_contents.h"
16#include "content/public/test/browser_test_utils.h"
17#include "net/dns/mock_host_resolver.h"
18#include "net/test/embedded_test_server/embedded_test_server.h"
19#include "url/gurl.h"
20
21IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ContentScriptAllFrames) {
22  ASSERT_TRUE(StartEmbeddedTestServer());
23  ASSERT_TRUE(RunExtensionTest("content_scripts/all_frames")) << message_;
24}
25
26IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ContentScriptAboutBlankIframes) {
27  ASSERT_TRUE(StartEmbeddedTestServer());
28  ASSERT_TRUE(
29      RunExtensionTest("content_scripts/about_blank_iframes")) << message_;
30}
31
32IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ContentScriptExtensionIframe) {
33  ASSERT_TRUE(StartEmbeddedTestServer());
34  ASSERT_TRUE(RunExtensionTest("content_scripts/extension_iframe")) << message_;
35}
36
37IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ContentScriptExtensionProcess) {
38  ASSERT_TRUE(StartEmbeddedTestServer());
39  ASSERT_TRUE(
40      RunExtensionTest("content_scripts/extension_process")) << message_;
41}
42
43IN_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_;
47}
48
49// Times out on Linux: http://crbug.com/163097
50#if defined(OS_LINUX)
51#define MAYBE_ContentScriptIsolatedWorlds DISABLED_ContentScriptIsolatedWorlds
52#else
53#define MAYBE_ContentScriptIsolatedWorlds ContentScriptIsolatedWorlds
54#endif
55IN_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_;
60
61  // Now load a different extension, inject into same page, verify worlds aren't
62  // shared.
63  ASSERT_TRUE(RunExtensionTest("content_scripts/isolated_world2")) << message_;
64}
65
66IN_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_;
72}
73
74// crbug.com/39249 -- content scripts js should not run on view source.
75IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ContentScriptViewSource) {
76  ASSERT_TRUE(StartEmbeddedTestServer());
77  ASSERT_TRUE(RunExtensionTest("content_scripts/view_source")) << message_;
78}
79
80// crbug.com/126257 -- content scripts should not get injected into other
81// extensions.
82IN_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"))
87      << message_;
88  // Then load targeted extension to make sure its content isn't changed.
89  ASSERT_TRUE(RunExtensionTest("content_scripts/other_extensions/victim"))
90      << message_;
91}
92
93// crbug.com/120762
94IN_PROC_BROWSER_TEST_F(
95    ExtensionApiTest,
96    DISABLED_ContentScriptStylesInjectedIntoExistingRenderers) {
97  ASSERT_TRUE(StartEmbeddedTestServer());
98
99  content::WindowedNotificationObserver signal(
100      chrome::NOTIFICATION_USER_SCRIPTS_UPDATED,
101      content::Source<Profile>(browser()->profile()));
102
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);
106
107  LoadExtension(
108      test_data_dir_.AppendASCII("content_scripts/existing_renderers"));
109
110  signal.Wait();
111
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)')",
119      &styles_injected));
120  ASSERT_TRUE(styles_injected);
121}
122
123IN_PROC_BROWSER_TEST_F(ExtensionApiTest,
124                       ContentScriptCSSLocalization) {
125  ASSERT_TRUE(StartEmbeddedTestServer());
126  ASSERT_TRUE(RunExtensionTest("content_scripts/css_l10n")) << message_;
127}
128
129IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ContentScriptExtensionAPIs) {
130  ASSERT_TRUE(StartEmbeddedTestServer());
131
132  const extensions::Extension* extension = LoadExtension(
133      test_data_dir_.AppendASCII("content_scripts/extension_api"));
134
135  ResultCatcher catcher;
136  ui_test_utils::NavigateToURL(
137      browser(),
138      embedded_test_server()->GetURL(
139          "/extensions/api_test/content_scripts/extension_api/functions.html"));
140  EXPECT_TRUE(catcher.GetNextResult());
141
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(
145      browser(),
146      embedded_test_server()->GetURL(
147          "/extensions/api_test/content_scripts/extension_api/events.html"));
148
149  // Navigate to an extension page that will fire the event events.js is
150  // listening for.
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());
155}
156
157// Flaky on Windows. http://crbug.com/248418
158#if defined(OS_WIN)
159#define MAYBE_ContentScriptPermissionsApi DISABLED_ContentScriptPermissionsApi
160#else
161#define MAYBE_ContentScriptPermissionsApi ContentScriptPermissionsApi
162#endif
163IN_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_;
169}
170
171IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ContentScriptBypassPageCSP) {
172  ASSERT_TRUE(StartEmbeddedTestServer());
173  ASSERT_TRUE(RunExtensionTest("content_scripts/bypass_page_csp")) << message_;
174}
175