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_system.h"
15 #include "chrome/browser/extensions/extension_util.h"
16 #include "chrome/browser/extensions/user_script_master.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/ui/browser.h"
19 #include "chrome/browser/ui/tabs/tab_strip_model.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/common/extension.h"
30 #include "extensions/common/extension_set.h"
31 #include "extensions/common/feature_switch.h"
32 #include "net/base/net_util.h"
34 using extensions::FeatureSwitch
;
36 // This file contains high-level startup tests for the extensions system. We've
37 // had many silly bugs where command line flags did not get propagated correctly
38 // into the services, so we didn't start correctly.
40 class ExtensionStartupTestBase
: public InProcessBrowserTest
{
42 ExtensionStartupTestBase() :
43 enable_extensions_(false) {
44 num_expected_extensions_
= 3;
48 // InProcessBrowserTest
49 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
50 if (!enable_extensions_
)
51 command_line
->AppendSwitch(switches::kDisableExtensions
);
53 if (!load_extensions_
.empty()) {
54 base::FilePath::StringType paths
= JoinString(load_extensions_
, ',');
55 command_line
->AppendSwitchNative(switches::kLoadExtension
,
57 command_line
->AppendSwitch(switches::kDisableExtensionsFileAccessCheck
);
61 virtual bool SetUpUserDataDirectory() OVERRIDE
{
62 base::FilePath profile_dir
;
63 PathService::Get(chrome::DIR_USER_DATA
, &profile_dir
);
64 profile_dir
= profile_dir
.AppendASCII(TestingProfile::kTestUserProfileDir
);
65 base::CreateDirectory(profile_dir
);
67 preferences_file_
= profile_dir
.AppendASCII("Preferences");
68 user_scripts_dir_
= profile_dir
.AppendASCII("User Scripts");
69 extensions_dir_
= profile_dir
.AppendASCII("Extensions");
71 if (enable_extensions_
&& load_extensions_
.empty()) {
72 base::FilePath src_dir
;
73 PathService::Get(chrome::DIR_TEST_DATA
, &src_dir
);
74 src_dir
= src_dir
.AppendASCII("extensions").AppendASCII("good");
76 base::CopyFile(src_dir
.AppendASCII("Preferences"), preferences_file_
);
77 base::CopyDirectory(src_dir
.AppendASCII("Extensions"),
78 profile_dir
, true); // recursive
83 virtual void TearDown() {
84 EXPECT_TRUE(base::DeleteFile(preferences_file_
, false));
86 // TODO(phajdan.jr): Check return values of the functions below, carefully.
87 base::DeleteFile(user_scripts_dir_
, true);
88 base::DeleteFile(extensions_dir_
, true);
90 InProcessBrowserTest::TearDown();
93 void WaitForServicesToStart(int num_expected_extensions
,
94 bool expect_extensions_enabled
) {
95 ExtensionService
* service
= extensions::ExtensionSystem::Get(
96 browser()->profile())->extension_service();
98 // Count the number of non-component extensions.
99 int found_extensions
= 0;
100 for (extensions::ExtensionSet::const_iterator it
=
101 service
->extensions()->begin();
102 it
!= service
->extensions()->end(); ++it
)
103 if ((*it
)->location() != extensions::Manifest::COMPONENT
)
106 ASSERT_EQ(static_cast<uint32
>(num_expected_extensions
),
107 static_cast<uint32
>(found_extensions
));
108 ASSERT_EQ(expect_extensions_enabled
, service
->extensions_enabled());
110 content::WindowedNotificationObserver
user_scripts_observer(
111 chrome::NOTIFICATION_USER_SCRIPTS_UPDATED
,
112 content::NotificationService::AllSources());
113 extensions::UserScriptMaster
* master
=
114 extensions::ExtensionSystem::Get(browser()->profile())->
115 user_script_master();
116 if (!master
->ScriptsReady())
117 user_scripts_observer
.Wait();
118 ASSERT_TRUE(master
->ScriptsReady());
121 void TestInjection(bool expect_css
, bool expect_script
) {
122 // Load a page affected by the content script and test to see the effect.
123 base::FilePath test_file
;
124 PathService::Get(chrome::DIR_TEST_DATA
, &test_file
);
125 test_file
= test_file
.AppendASCII("extensions")
126 .AppendASCII("test_file.html");
128 ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(test_file
));
131 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
132 browser()->tab_strip_model()->GetActiveWebContents(),
133 "window.domAutomationController.send("
134 " document.defaultView.getComputedStyle(document.body, null)."
135 " getPropertyValue('background-color') == 'rgb(245, 245, 220)')",
137 EXPECT_EQ(expect_css
, result
);
140 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
141 browser()->tab_strip_model()->GetActiveWebContents(),
142 "window.domAutomationController.send(document.title == 'Modified')",
144 EXPECT_EQ(expect_script
, result
);
147 base::FilePath preferences_file_
;
148 base::FilePath extensions_dir_
;
149 base::FilePath user_scripts_dir_
;
150 bool enable_extensions_
;
151 // Extensions to load from the command line.
152 std::vector
<base::FilePath::StringType
> load_extensions_
;
154 int num_expected_extensions_
;
158 // ExtensionsStartupTest
159 // Ensures that we can startup the browser with --enable-extensions and some
160 // extensions installed and see them run and do basic things.
162 class ExtensionsStartupTest
: public ExtensionStartupTestBase
{
164 ExtensionsStartupTest() {
165 enable_extensions_
= true;
169 IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest
, Test
) {
170 WaitForServicesToStart(num_expected_extensions_
, true);
171 TestInjection(true, true);
174 // Sometimes times out on Mac. http://crbug.com/48151
175 #if defined(OS_MACOSX)
176 #define MAYBE_NoFileAccess DISABLED_NoFileAccess
178 #define MAYBE_NoFileAccess NoFileAccess
180 // Tests that disallowing file access on an extension prevents it from injecting
181 // script into a page with a file URL.
182 IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest
, MAYBE_NoFileAccess
) {
183 WaitForServicesToStart(num_expected_extensions_
, true);
185 // Keep a separate list of extensions for which to disable file access, since
186 // doing so reloads them.
187 std::vector
<const extensions::Extension
*> extension_list
;
189 ExtensionService
* service
= extensions::ExtensionSystem::Get(
190 browser()->profile())->extension_service();
191 for (extensions::ExtensionSet::const_iterator it
=
192 service
->extensions()->begin();
193 it
!= service
->extensions()->end(); ++it
) {
194 if ((*it
)->location() == extensions::Manifest::COMPONENT
)
196 if (extension_util::AllowFileAccess(it
->get(), service
))
197 extension_list
.push_back(it
->get());
200 for (size_t i
= 0; i
< extension_list
.size(); ++i
) {
201 content::WindowedNotificationObserver
user_scripts_observer(
202 chrome::NOTIFICATION_USER_SCRIPTS_UPDATED
,
203 content::NotificationService::AllSources());
204 extension_util::SetAllowFileAccess(extension_list
[i
], service
, false);
205 user_scripts_observer
.Wait();
208 TestInjection(false, false);
211 // ExtensionsLoadTest
212 // Ensures that we can startup the browser with --load-extension and see them
215 class ExtensionsLoadTest
: public ExtensionStartupTestBase
{
217 ExtensionsLoadTest() {
218 enable_extensions_
= true;
219 base::FilePath one_extension_path
;
220 PathService::Get(chrome::DIR_TEST_DATA
, &one_extension_path
);
221 one_extension_path
= one_extension_path
222 .AppendASCII("extensions")
224 .AppendASCII("Extensions")
225 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
226 .AppendASCII("1.0.0.0");
227 load_extensions_
.push_back(one_extension_path
.value());
231 // Fails inconsistently on Linux x64. http://crbug.com/80961
232 // TODO(dpapad): Has not failed since October 2011, let's reenable, monitor
233 // and act accordingly.
234 IN_PROC_BROWSER_TEST_F(ExtensionsLoadTest
, Test
) {
235 WaitForServicesToStart(1, true);
236 TestInjection(true, true);
239 // ExtensionsLoadMultipleTest
240 // Ensures that we can startup the browser with multiple extensions
241 // via --load-extension=X1,X2,X3.
242 class ExtensionsLoadMultipleTest
: public ExtensionStartupTestBase
{
244 ExtensionsLoadMultipleTest() {
245 enable_extensions_
= true;
246 base::FilePath one_extension_path
;
247 PathService::Get(chrome::DIR_TEST_DATA
, &one_extension_path
);
248 one_extension_path
= one_extension_path
249 .AppendASCII("extensions")
251 .AppendASCII("Extensions")
252 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
253 .AppendASCII("1.0.0.0");
254 load_extensions_
.push_back(one_extension_path
.value());
256 base::FilePath second_extension_path
;
257 PathService::Get(chrome::DIR_TEST_DATA
, &second_extension_path
);
258 second_extension_path
= second_extension_path
259 .AppendASCII("extensions")
261 load_extensions_
.push_back(second_extension_path
.value());
263 base::FilePath third_extension_path
;
264 PathService::Get(chrome::DIR_TEST_DATA
, &third_extension_path
);
265 third_extension_path
= third_extension_path
266 .AppendASCII("extensions")
267 .AppendASCII("app1");
268 load_extensions_
.push_back(third_extension_path
.value());
270 base::FilePath fourth_extension_path
;
271 PathService::Get(chrome::DIR_TEST_DATA
, &fourth_extension_path
);
272 fourth_extension_path
= fourth_extension_path
273 .AppendASCII("extensions")
274 .AppendASCII("app2");
275 load_extensions_
.push_back(fourth_extension_path
.value());
279 IN_PROC_BROWSER_TEST_F(ExtensionsLoadMultipleTest
, Test
) {
280 WaitForServicesToStart(4, true);
281 TestInjection(true, true);