1 // Copyright (c) 2011 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/command_line.h"
6 #include "chrome/browser/extensions/extension_apitest.h"
7 #include "chrome/browser/profiles/profile.h"
8 #include "chrome/browser/ui/browser.h"
9 #include "chrome/test/base/test_switches.h"
10 #include "chrome/test/base/ui_test_utils.h"
11 #include "components/crx_file/id_util.h"
12 #include "extensions/browser/extension_registry.h"
13 #include "extensions/common/extension.h"
14 #include "extensions/common/extensions_client.h"
15 #include "extensions/test/extension_test_message_listener.h"
16 #include "net/test/embedded_test_server/embedded_test_server.h"
18 const std::string kAllUrlsTarget
= "/extensions/api_test/all_urls/index.html";
20 typedef ExtensionApiTest AllUrlsApiTest
;
22 #if defined(OS_WIN) && !defined(NDEBUG)
23 // http://crbug.com/174341
24 #define MAYBE_WhitelistedExtension DISABLED_WhitelistedExtension
26 #define MAYBE_WhitelistedExtension WhitelistedExtension
28 IN_PROC_BROWSER_TEST_F(AllUrlsApiTest
, MAYBE_WhitelistedExtension
) {
29 #if defined(OS_WIN) && defined(USE_ASH)
30 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
31 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
32 switches::kAshBrowserTests
))
36 // First setup the two extensions.
37 base::FilePath extension_dir1
= test_data_dir_
.AppendASCII("all_urls")
38 .AppendASCII("content_script");
39 base::FilePath extension_dir2
= test_data_dir_
.AppendASCII("all_urls")
40 .AppendASCII("execute_script");
42 // Then add the two extensions to the whitelist.
43 extensions::ExtensionsClient::ScriptingWhitelist whitelist
;
44 whitelist
.push_back(crx_file::id_util::GenerateIdForPath(extension_dir1
));
45 whitelist
.push_back(crx_file::id_util::GenerateIdForPath(extension_dir2
));
46 extensions::ExtensionsClient::Get()->SetScriptingWhitelist(whitelist
);
48 // Then load extensions.
49 extensions::ExtensionRegistry
* registry
=
50 extensions::ExtensionRegistry::Get(browser()->profile());
51 const size_t size_before
= registry
->enabled_extensions().size();
52 ASSERT_TRUE(LoadExtension(extension_dir1
));
53 ASSERT_TRUE(LoadExtension(extension_dir2
));
54 EXPECT_EQ(size_before
+ 2, registry
->enabled_extensions().size());
58 // Now verify we run content scripts on chrome://newtab/.
59 url
= "chrome://newtab/";
60 ExtensionTestMessageListener
listener1a("content script: " + url
, false);
61 ExtensionTestMessageListener
listener1b("execute: " + url
, false);
62 ui_test_utils::NavigateToURL(browser(), GURL(url
));
63 ASSERT_TRUE(listener1a
.WaitUntilSatisfied());
64 ASSERT_TRUE(listener1b
.WaitUntilSatisfied());
66 // Now verify data: urls.
67 url
= "data:text/html;charset=utf-8,<html>asdf</html>";
68 ExtensionTestMessageListener
listener2a("content script: " + url
, false);
69 ExtensionTestMessageListener
listener2b("execute: " + url
, false);
70 ui_test_utils::NavigateToURL(browser(), GURL(url
));
71 ASSERT_TRUE(listener2a
.WaitUntilSatisfied());
72 ASSERT_TRUE(listener2b
.WaitUntilSatisfied());
74 // Now verify chrome://version/.
75 url
= "chrome://version/";
76 ExtensionTestMessageListener
listener3a("content script: " + url
, false);
77 ExtensionTestMessageListener
listener3b("execute: " + url
, false);
78 ui_test_utils::NavigateToURL(browser(), GURL(url
));
79 ASSERT_TRUE(listener3a
.WaitUntilSatisfied());
80 ASSERT_TRUE(listener3b
.WaitUntilSatisfied());
82 // Now verify about:blank.
84 ExtensionTestMessageListener
listener4a("content script: " + url
, false);
85 ExtensionTestMessageListener
listener4b("execute: " + url
, false);
86 ui_test_utils::NavigateToURL(browser(), GURL(url
));
87 ASSERT_TRUE(listener4a
.WaitUntilSatisfied());
88 ASSERT_TRUE(listener4b
.WaitUntilSatisfied());
90 // Now verify we can script a regular http page.
91 ASSERT_TRUE(StartEmbeddedTestServer());
92 GURL page_url
= embedded_test_server()->GetURL(kAllUrlsTarget
);
93 ExtensionTestMessageListener
listener5a("content script: " + page_url
.spec(),
95 ExtensionTestMessageListener
listener5b("execute: " + page_url
.spec(), false);
96 ui_test_utils::NavigateToURL(browser(), page_url
);
97 ASSERT_TRUE(listener5a
.WaitUntilSatisfied());
98 ASSERT_TRUE(listener5b
.WaitUntilSatisfied());
101 // Test that an extension NOT whitelisted for scripting can ask for <all_urls>
102 // and run scripts on non-restricted all pages.
103 IN_PROC_BROWSER_TEST_F(AllUrlsApiTest
, RegularExtensions
) {
104 // First load the two extensions.
105 base::FilePath extension_dir1
= test_data_dir_
.AppendASCII("all_urls")
106 .AppendASCII("content_script");
107 base::FilePath extension_dir2
= test_data_dir_
.AppendASCII("all_urls")
108 .AppendASCII("execute_script");
110 extensions::ExtensionRegistry
* registry
=
111 extensions::ExtensionRegistry::Get(browser()->profile());
112 const size_t size_before
= registry
->enabled_extensions().size();
113 ASSERT_TRUE(LoadExtension(extension_dir1
));
114 ASSERT_TRUE(LoadExtension(extension_dir2
));
115 EXPECT_EQ(size_before
+ 2, registry
->enabled_extensions().size());
117 // Now verify we can script a regular http page.
118 ASSERT_TRUE(StartEmbeddedTestServer());
119 GURL page_url
= embedded_test_server()->GetURL(kAllUrlsTarget
);
120 ExtensionTestMessageListener
listener1a("content script: " + page_url
.spec(),
122 ExtensionTestMessageListener
listener1b("execute: " + page_url
.spec(), false);
123 ui_test_utils::NavigateToURL(browser(), page_url
);
124 ASSERT_TRUE(listener1a
.WaitUntilSatisfied());
125 ASSERT_TRUE(listener1b
.WaitUntilSatisfied());