OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/command_line.h" | 5 #include "base/command_line.h" |
6 #include "chrome/browser/extensions/extension_apitest.h" | 6 #include "chrome/browser/extensions/extension_apitest.h" |
7 #include "chrome/browser/extensions/extension_service.h" | |
8 #include "chrome/browser/profiles/profile.h" | 7 #include "chrome/browser/profiles/profile.h" |
9 #include "chrome/browser/ui/browser.h" | 8 #include "chrome/browser/ui/browser.h" |
10 #include "chrome/test/base/test_switches.h" | 9 #include "chrome/test/base/test_switches.h" |
11 #include "chrome/test/base/ui_test_utils.h" | 10 #include "chrome/test/base/ui_test_utils.h" |
12 #include "components/crx_file/id_util.h" | 11 #include "components/crx_file/id_util.h" |
13 #include "extensions/browser/extension_registry.h" | 12 #include "extensions/browser/extension_registry.h" |
14 #include "extensions/common/extension.h" | 13 #include "extensions/common/extension.h" |
15 #include "extensions/common/extensions_client.h" | 14 #include "extensions/common/extensions_client.h" |
16 #include "extensions/test/extension_test_message_listener.h" | 15 #include "extensions/test/extension_test_message_listener.h" |
17 #include "net/test/embedded_test_server/embedded_test_server.h" | 16 #include "net/test/embedded_test_server/embedded_test_server.h" |
18 | 17 |
19 namespace extensions { | 18 const std::string kAllUrlsTarget = "/extensions/api_test/all_urls/index.html"; |
20 | 19 |
21 namespace { | 20 typedef ExtensionApiTest AllUrlsApiTest; |
22 const std::string kAllUrlsTarget = "/extensions/api_test/all_urls/index.html"; | |
23 } | |
24 | |
25 class AllUrlsApiTest : public ExtensionApiTest { | |
26 protected: | |
27 AllUrlsApiTest() {} | |
28 ~AllUrlsApiTest() override {} | |
29 | |
30 const Extension* content_script() const { return content_script_.get(); } | |
31 const Extension* execute_script() const { return execute_script_.get(); } | |
32 | |
33 void WhitelistExtensions() { | |
34 ExtensionsClient::ScriptingWhitelist whitelist; | |
35 whitelist.push_back(content_script_->id()); | |
36 whitelist.push_back(execute_script_->id()); | |
37 ExtensionsClient::Get()->SetScriptingWhitelist(whitelist); | |
38 // Extensions will have certain permissions withheld at initialization if | |
39 // they aren't whitelisted, so we need to reload them. | |
40 extension_service()->ReloadExtension(content_script_->id()); | |
41 extension_service()->ReloadExtension(execute_script_->id()); | |
42 } | |
43 | |
44 private: | |
45 void SetUpOnMainThread() override { | |
46 ExtensionApiTest::SetUpOnMainThread(); | |
47 base::FilePath data_dir = test_data_dir_.AppendASCII("all_urls"); | |
48 content_script_ = LoadExtension(data_dir.AppendASCII("content_script")); | |
49 ASSERT_TRUE(content_script_); | |
50 execute_script_ = LoadExtension(data_dir.AppendASCII("execute_script")); | |
51 ASSERT_TRUE(execute_script_); | |
52 } | |
53 | |
54 scoped_refptr<const Extension> content_script_; | |
55 scoped_refptr<const Extension> execute_script_; | |
56 | |
57 DISALLOW_COPY_AND_ASSIGN(AllUrlsApiTest); | |
58 }; | |
59 | 21 |
60 #if (defined(OS_WIN) && !defined(NDEBUG)) || defined(OS_CHROMEOS) | 22 #if (defined(OS_WIN) && !defined(NDEBUG)) || defined(OS_CHROMEOS) |
61 // http://crbug.com/174341 | 23 // http://crbug.com/174341 |
62 #define MAYBE_WhitelistedExtension DISABLED_WhitelistedExtension | 24 #define MAYBE_WhitelistedExtension DISABLED_WhitelistedExtension |
63 #else | 25 #else |
64 #define MAYBE_WhitelistedExtension WhitelistedExtension | 26 #define MAYBE_WhitelistedExtension WhitelistedExtension |
65 #endif | 27 #endif |
66 IN_PROC_BROWSER_TEST_F(AllUrlsApiTest, MAYBE_WhitelistedExtension) { | 28 IN_PROC_BROWSER_TEST_F(AllUrlsApiTest, MAYBE_WhitelistedExtension) { |
67 #if defined(OS_WIN) && defined(USE_ASH) | 29 #if defined(OS_WIN) && defined(USE_ASH) |
68 // Disable this test in Metro+Ash for now (http://crbug.com/262796). | 30 // Disable this test in Metro+Ash for now (http://crbug.com/262796). |
69 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 31 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
70 switches::kAshBrowserTests)) | 32 switches::kAshBrowserTests)) |
71 return; | 33 return; |
72 #endif | 34 #endif |
73 | 35 |
74 WhitelistExtensions(); | 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"); |
| 41 |
| 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); |
| 47 |
| 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()); |
75 | 55 |
76 std::string url; | 56 std::string url; |
77 | 57 |
78 // Now verify we run content scripts on chrome://newtab/. | 58 // Now verify we run content scripts on chrome://newtab/. |
79 url = "chrome://newtab/"; | 59 url = "chrome://newtab/"; |
80 ExtensionTestMessageListener listener1a("content script: " + url, false); | 60 ExtensionTestMessageListener listener1a("content script: " + url, false); |
81 ExtensionTestMessageListener listener1b("execute: " + url, false); | 61 ExtensionTestMessageListener listener1b("execute: " + url, false); |
82 ui_test_utils::NavigateToURL(browser(), GURL(url)); | 62 ui_test_utils::NavigateToURL(browser(), GURL(url)); |
83 ASSERT_TRUE(listener1a.WaitUntilSatisfied()); | 63 ASSERT_TRUE(listener1a.WaitUntilSatisfied()); |
84 ASSERT_TRUE(listener1b.WaitUntilSatisfied()); | 64 ASSERT_TRUE(listener1b.WaitUntilSatisfied()); |
(...skipping 29 matching lines...) Expand all Loading... |
114 false); | 94 false); |
115 ExtensionTestMessageListener listener5b("execute: " + page_url.spec(), false); | 95 ExtensionTestMessageListener listener5b("execute: " + page_url.spec(), false); |
116 ui_test_utils::NavigateToURL(browser(), page_url); | 96 ui_test_utils::NavigateToURL(browser(), page_url); |
117 ASSERT_TRUE(listener5a.WaitUntilSatisfied()); | 97 ASSERT_TRUE(listener5a.WaitUntilSatisfied()); |
118 ASSERT_TRUE(listener5b.WaitUntilSatisfied()); | 98 ASSERT_TRUE(listener5b.WaitUntilSatisfied()); |
119 } | 99 } |
120 | 100 |
121 // Test that an extension NOT whitelisted for scripting can ask for <all_urls> | 101 // Test that an extension NOT whitelisted for scripting can ask for <all_urls> |
122 // and run scripts on non-restricted all pages. | 102 // and run scripts on non-restricted all pages. |
123 IN_PROC_BROWSER_TEST_F(AllUrlsApiTest, RegularExtensions) { | 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"); |
| 109 |
| 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()); |
| 116 |
124 // Now verify we can script a regular http page. | 117 // Now verify we can script a regular http page. |
125 ASSERT_TRUE(StartEmbeddedTestServer()); | 118 ASSERT_TRUE(StartEmbeddedTestServer()); |
126 GURL page_url = embedded_test_server()->GetURL(kAllUrlsTarget); | 119 GURL page_url = embedded_test_server()->GetURL(kAllUrlsTarget); |
127 ExtensionTestMessageListener listener1a("content script: " + page_url.spec(), | 120 ExtensionTestMessageListener listener1a("content script: " + page_url.spec(), |
128 false); | 121 false); |
129 ExtensionTestMessageListener listener1b("execute: " + page_url.spec(), false); | 122 ExtensionTestMessageListener listener1b("execute: " + page_url.spec(), false); |
130 ui_test_utils::NavigateToURL(browser(), page_url); | 123 ui_test_utils::NavigateToURL(browser(), page_url); |
131 ASSERT_TRUE(listener1a.WaitUntilSatisfied()); | 124 ASSERT_TRUE(listener1a.WaitUntilSatisfied()); |
132 ASSERT_TRUE(listener1b.WaitUntilSatisfied()); | 125 ASSERT_TRUE(listener1b.WaitUntilSatisfied()); |
133 } | 126 } |
134 | |
135 IN_PROC_BROWSER_TEST_F(AllUrlsApiTest, | |
136 WhitelistedExtensionRunsOnExtensionPages) { | |
137 WhitelistExtensions(); | |
138 const Extension* bystander = | |
139 LoadExtension(test_data_dir_.AppendASCII("all_urls") | |
140 .AppendASCII("bystander")); | |
141 ASSERT_TRUE(bystander); | |
142 | |
143 // TODO(devlin): This test should probably go in the WhitelistedExtension test | |
144 // above, but that one has a bunch of disableds, so it wouldn't be very | |
145 // useful. | |
146 GURL url(bystander->GetResourceURL("page.html")); | |
147 ExtensionTestMessageListener listenerA( | |
148 "content script: " + url.spec(), false); | |
149 ExtensionTestMessageListener listenerB("execute: " + url.spec(), false); | |
150 ui_test_utils::NavigateToURL(browser(), GURL(url)); | |
151 ASSERT_TRUE(listenerA.WaitUntilSatisfied()); | |
152 ASSERT_TRUE(listenerB.WaitUntilSatisfied()); | |
153 } | |
154 | |
155 } // namespace extensions | |
OLD | NEW |