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.
7 #include "base/command_line.h"
8 #include "base/file_util.h"
9 #include "base/files/file_path.h"
10 #include "base/path_service.h"
11 #include "base/strings/string_util.h"
12 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/extensions/extension_service.h"
14 #include "chrome/browser/extensions/extension_util.h"
15 #include "chrome/browser/extensions/user_script_master.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/ui/browser.h"
18 #include "chrome/browser/ui/tabs/tab_strip_model.h"
19 #include "chrome/common/chrome_constants.h"
20 #include "chrome/common/chrome_paths.h"
21 #include "chrome/common/chrome_switches.h"
22 #include "chrome/test/base/in_process_browser_test.h"
23 #include "chrome/test/base/testing_profile.h"
24 #include "chrome/test/base/ui_test_utils.h"
25 #include "content/public/browser/notification_details.h"
26 #include "content/public/browser/notification_service.h"
27 #include "content/public/browser/web_contents.h"
28 #include "content/public/test/browser_test_utils.h"
29 #include "extensions/browser/extension_registry.h"
30 #include "extensions/browser/extension_system.h"
31 #include "extensions/common/extension.h"
32 #include "extensions/common/extension_set.h"
33 #include "extensions/common/feature_switch.h"
34 #include "net/base/net_util.h"
36 using extensions::FeatureSwitch
;
38 // This file contains high-level startup tests for the extensions system. We've
39 // had many silly bugs where command line flags did not get propagated correctly
40 // into the services, so we didn't start correctly.
42 class ExtensionStartupTestBase
: public InProcessBrowserTest
{
44 ExtensionStartupTestBase() :
45 enable_extensions_(false) {
46 num_expected_extensions_
= 3;
50 // InProcessBrowserTest
51 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
52 if (!enable_extensions_
)
53 command_line
->AppendSwitch(switches::kDisableExtensions
);
55 if (!load_extensions_
.empty()) {
56 base::FilePath::StringType paths
= JoinString(load_extensions_
, ',');
57 command_line
->AppendSwitchNative(switches::kLoadExtension
,
59 command_line
->AppendSwitch(switches::kDisableExtensionsFileAccessCheck
);
63 virtual bool SetUpUserDataDirectory() OVERRIDE
{
64 base::FilePath profile_dir
;
65 PathService::Get(chrome::DIR_USER_DATA
, &profile_dir
);
66 profile_dir
= profile_dir
.AppendASCII(TestingProfile::kTestUserProfileDir
);
67 base::CreateDirectory(profile_dir
);
69 preferences_file_
= profile_dir
.Append(chrome::kPreferencesFilename
);
70 user_scripts_dir_
= profile_dir
.AppendASCII("User Scripts");
71 extensions_dir_
= profile_dir
.AppendASCII("Extensions");
73 if (enable_extensions_
&& load_extensions_
.empty()) {
74 base::FilePath src_dir
;
75 PathService::Get(chrome::DIR_TEST_DATA
, &src_dir
);
76 src_dir
= src_dir
.AppendASCII("extensions").AppendASCII("good");
78 base::CopyFile(src_dir
.Append(chrome::kPreferencesFilename
),
80 base::CopyDirectory(src_dir
.AppendASCII("Extensions"),
81 profile_dir
, true); // recursive
86 virtual void TearDown() {
87 EXPECT_TRUE(base::DeleteFile(preferences_file_
, false));
89 // TODO(phajdan.jr): Check return values of the functions below, carefully.
90 base::DeleteFile(user_scripts_dir_
, true);
91 base::DeleteFile(extensions_dir_
, true);
93 InProcessBrowserTest::TearDown();
96 void WaitForServicesToStart(int num_expected_extensions
,
97 bool expect_extensions_enabled
) {
98 ExtensionService
* service
= extensions::ExtensionSystem::Get(
99 browser()->profile())->extension_service();
101 // Count the number of non-component extensions.
102 int found_extensions
= 0;
103 for (extensions::ExtensionSet::const_iterator it
=
104 service
->extensions()->begin();
105 it
!= service
->extensions()->end(); ++it
) {
106 if ((*it
)->location() != extensions::Manifest::COMPONENT
)
110 ASSERT_EQ(static_cast<uint32
>(num_expected_extensions
),
111 static_cast<uint32
>(found_extensions
));
112 ASSERT_EQ(expect_extensions_enabled
, service
->extensions_enabled());
114 content::WindowedNotificationObserver
user_scripts_observer(
115 chrome::NOTIFICATION_USER_SCRIPTS_UPDATED
,
116 content::NotificationService::AllSources());
117 extensions::UserScriptMaster
* master
=
118 extensions::ExtensionSystem::Get(browser()->profile())->
119 user_script_master();
120 if (!master
->ScriptsReady())
121 user_scripts_observer
.Wait();
122 ASSERT_TRUE(master
->ScriptsReady());
125 void TestInjection(bool expect_css
, bool expect_script
) {
126 // Load a page affected by the content script and test to see the effect.
127 base::FilePath test_file
;
128 PathService::Get(chrome::DIR_TEST_DATA
, &test_file
);
129 test_file
= test_file
.AppendASCII("extensions")
130 .AppendASCII("test_file.html");
132 ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(test_file
));
135 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
136 browser()->tab_strip_model()->GetActiveWebContents(),
137 "window.domAutomationController.send("
138 " document.defaultView.getComputedStyle(document.body, null)."
139 " getPropertyValue('background-color') == 'rgb(245, 245, 220)')",
141 EXPECT_EQ(expect_css
, result
);
144 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
145 browser()->tab_strip_model()->GetActiveWebContents(),
146 "window.domAutomationController.send(document.title == 'Modified')",
148 EXPECT_EQ(expect_script
, result
);
151 base::FilePath preferences_file_
;
152 base::FilePath extensions_dir_
;
153 base::FilePath user_scripts_dir_
;
154 bool enable_extensions_
;
155 // Extensions to load from the command line.
156 std::vector
<base::FilePath::StringType
> load_extensions_
;
158 int num_expected_extensions_
;
162 // ExtensionsStartupTest
163 // Ensures that we can startup the browser with --enable-extensions and some
164 // extensions installed and see them run and do basic things.
166 class ExtensionsStartupTest
: public ExtensionStartupTestBase
{
168 ExtensionsStartupTest() {
169 enable_extensions_
= true;
173 IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest
, Test
) {
174 WaitForServicesToStart(num_expected_extensions_
, true);
175 TestInjection(true, true);
178 // Sometimes times out on Mac. http://crbug.com/48151
179 #if defined(OS_MACOSX)
180 #define MAYBE_NoFileAccess DISABLED_NoFileAccess
182 #define MAYBE_NoFileAccess NoFileAccess
184 // Tests that disallowing file access on an extension prevents it from injecting
185 // script into a page with a file URL.
186 IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest
, MAYBE_NoFileAccess
) {
187 WaitForServicesToStart(num_expected_extensions_
, true);
189 // Keep a separate list of extensions for which to disable file access, since
190 // doing so reloads them.
191 std::vector
<const extensions::Extension
*> extension_list
;
193 extensions::ExtensionRegistry
* registry
=
194 extensions::ExtensionRegistry::Get(browser()->profile());
195 for (extensions::ExtensionSet::const_iterator it
=
196 registry
->enabled_extensions().begin();
197 it
!= registry
->enabled_extensions().end(); ++it
) {
198 if ((*it
)->location() == extensions::Manifest::COMPONENT
)
200 if (extensions::util::AllowFileAccess((*it
)->id(), browser()->profile()))
201 extension_list
.push_back(it
->get());
204 for (size_t i
= 0; i
< extension_list
.size(); ++i
) {
205 content::WindowedNotificationObserver
user_scripts_observer(
206 chrome::NOTIFICATION_USER_SCRIPTS_UPDATED
,
207 content::NotificationService::AllSources());
208 extensions::util::SetAllowFileAccess(
209 extension_list
[i
]->id(), browser()->profile(), false);
210 user_scripts_observer
.Wait();
213 TestInjection(false, false);
216 // ExtensionsLoadTest
217 // Ensures that we can startup the browser with --load-extension and see them
220 class ExtensionsLoadTest
: public ExtensionStartupTestBase
{
222 ExtensionsLoadTest() {
223 enable_extensions_
= true;
224 base::FilePath one_extension_path
;
225 PathService::Get(chrome::DIR_TEST_DATA
, &one_extension_path
);
226 one_extension_path
= one_extension_path
227 .AppendASCII("extensions")
229 .AppendASCII("Extensions")
230 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
231 .AppendASCII("1.0.0.0");
232 load_extensions_
.push_back(one_extension_path
.value());
236 // Fails inconsistently on Linux x64. http://crbug.com/80961
237 // TODO(dpapad): Has not failed since October 2011, let's reenable, monitor
238 // and act accordingly.
239 IN_PROC_BROWSER_TEST_F(ExtensionsLoadTest
, Test
) {
240 WaitForServicesToStart(1, true);
241 TestInjection(true, true);
244 // ExtensionsLoadMultipleTest
245 // Ensures that we can startup the browser with multiple extensions
246 // via --load-extension=X1,X2,X3.
247 class ExtensionsLoadMultipleTest
: public ExtensionStartupTestBase
{
249 ExtensionsLoadMultipleTest() {
250 enable_extensions_
= true;
251 base::FilePath one_extension_path
;
252 PathService::Get(chrome::DIR_TEST_DATA
, &one_extension_path
);
253 one_extension_path
= one_extension_path
254 .AppendASCII("extensions")
256 .AppendASCII("Extensions")
257 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
258 .AppendASCII("1.0.0.0");
259 load_extensions_
.push_back(one_extension_path
.value());
261 base::FilePath second_extension_path
;
262 PathService::Get(chrome::DIR_TEST_DATA
, &second_extension_path
);
263 second_extension_path
= second_extension_path
264 .AppendASCII("extensions")
266 load_extensions_
.push_back(second_extension_path
.value());
268 base::FilePath third_extension_path
;
269 PathService::Get(chrome::DIR_TEST_DATA
, &third_extension_path
);
270 third_extension_path
= third_extension_path
271 .AppendASCII("extensions")
272 .AppendASCII("app1");
273 load_extensions_
.push_back(third_extension_path
.value());
275 base::FilePath fourth_extension_path
;
276 PathService::Get(chrome::DIR_TEST_DATA
, &fourth_extension_path
);
277 fourth_extension_path
= fourth_extension_path
278 .AppendASCII("extensions")
279 .AppendASCII("app2");
280 load_extensions_
.push_back(fourth_extension_path
.value());
284 IN_PROC_BROWSER_TEST_F(ExtensionsLoadMultipleTest
, Test
) {
285 WaitForServicesToStart(4, true);
286 TestInjection(true, true);