blob: 81921da3995402a436b38f6e66c8251091bdd370 [file] [log] [blame]
[email protected]4d2451652012-02-14 23:54:261// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]038d52e12009-10-14 16:53:412// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]f0488f2f2009-07-01 05:25:225#include <vector>
6
7#include "base/command_line.h"
8#include "base/file_path.h"
9#include "base/file_util.h"
10#include "base/path_service.h"
[email protected]bfc93562011-12-14 19:40:0411#include "base/string_util.h"
[email protected]eaa7dd182010-12-14 11:09:0012#include "chrome/browser/extensions/extension_service.h"
[email protected]5d272a42012-10-29 13:28:2213#include "chrome/browser/extensions/extension_system.h"
[email protected]8cb5d5b2010-02-09 11:36:1614#include "chrome/browser/extensions/user_script_master.h"
[email protected]8ecad5e2010-12-02 21:18:3315#include "chrome/browser/profiles/profile.h"
[email protected]7b5dc002010-11-16 23:08:1016#include "chrome/browser/ui/browser.h"
[email protected]47ae23372013-01-29 01:50:4817#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]432115822011-07-10 15:52:2718#include "chrome/common/chrome_notification_types.h"
[email protected]f0488f2f2009-07-01 05:25:2219#include "chrome/common/chrome_paths.h"
20#include "chrome/common/chrome_switches.h"
[email protected]00b5d0a52012-10-30 13:13:5321#include "chrome/common/extensions/feature_switch.h"
[email protected]af44e7fb2011-07-29 18:32:3222#include "chrome/test/base/in_process_browser_test.h"
[email protected]89dbb1772012-07-17 13:47:2523#include "chrome/test/base/testing_profile.h"
[email protected]af44e7fb2011-07-29 18:32:3224#include "chrome/test/base/ui_test_utils.h"
[email protected]6c2381d2011-10-19 02:52:5325#include "content/public/browser/notification_details.h"
[email protected]ad50def52011-10-19 23:17:0726#include "content/public/browser/notification_service.h"
[email protected]4ca15302012-01-03 05:53:2027#include "content/public/browser/web_contents.h"
[email protected]7d478cb2012-07-24 17:19:4228#include "content/public/test/browser_test_utils.h"
[email protected]f0488f2f2009-07-01 05:25:2229#include "net/base/net_util.h"
30
[email protected]00b5d0a52012-10-30 13:13:5331using extensions::FeatureSwitch;
32
[email protected]17c4f3c2009-07-04 16:36:2533// This file contains high-level startup tests for the extensions system. We've
34// had many silly bugs where command line flags did not get propagated correctly
35// into the services, so we didn't start correctly.
[email protected]f0488f2f2009-07-01 05:25:2236
[email protected]3e59bac2010-04-08 16:16:5537class ExtensionStartupTestBase : public InProcessBrowserTest {
[email protected]f0488f2f2009-07-01 05:25:2238 public:
[email protected]00b5d0a52012-10-30 13:13:5339 ExtensionStartupTestBase() :
40 enable_extensions_(false),
41 override_sideload_wipeout_(FeatureSwitch::sideload_wipeout(), false) {
[email protected]fe13bf62010-08-26 14:33:1942 num_expected_extensions_ = 3;
[email protected]f0488f2f2009-07-01 05:25:2243 }
44
45 protected:
46 // InProcessBrowserTest
47 virtual void SetUpCommandLine(CommandLine* command_line) {
[email protected]10f6b142012-04-14 19:22:4348 if (!enable_extensions_)
49 command_line->AppendSwitch(switches::kDisableExtensions);
50
51 if (!load_extensions_.empty()) {
52 FilePath::StringType paths = JoinString(load_extensions_, ',');
53 command_line->AppendSwitchNative(switches::kLoadExtension,
54 paths);
55 command_line->AppendSwitch(switches::kDisableExtensionsFileAccessCheck);
56 }
57 }
58
59 virtual bool SetUpUserDataDirectory() {
[email protected]f0488f2f2009-07-01 05:25:2260 FilePath profile_dir;
61 PathService::Get(chrome::DIR_USER_DATA, &profile_dir);
[email protected]89dbb1772012-07-17 13:47:2562 profile_dir = profile_dir.AppendASCII(TestingProfile::kTestUserProfileDir);
[email protected]f0488f2f2009-07-01 05:25:2263 file_util::CreateDirectory(profile_dir);
64
65 preferences_file_ = profile_dir.AppendASCII("Preferences");
66 user_scripts_dir_ = profile_dir.AppendASCII("User Scripts");
67 extensions_dir_ = profile_dir.AppendASCII("Extensions");
68
[email protected]10f6b142012-04-14 19:22:4369 if (enable_extensions_ && load_extensions_.empty()) {
70 FilePath src_dir;
71 PathService::Get(chrome::DIR_TEST_DATA, &src_dir);
72 src_dir = src_dir.AppendASCII("extensions").AppendASCII("good");
[email protected]f0488f2f2009-07-01 05:25:2273
[email protected]10f6b142012-04-14 19:22:4374 file_util::CopyFile(src_dir.AppendASCII("Preferences"),
75 preferences_file_);
76 file_util::CopyDirectory(src_dir.AppendASCII("Extensions"),
77 profile_dir, true); // recursive
[email protected]f0488f2f2009-07-01 05:25:2278 }
[email protected]10f6b142012-04-14 19:22:4379 return true;
[email protected]f0488f2f2009-07-01 05:25:2280 }
81
[email protected]f0488f2f2009-07-01 05:25:2282 virtual void TearDown() {
[email protected]3e59bac2010-04-08 16:16:5583 EXPECT_TRUE(file_util::Delete(preferences_file_, false));
[email protected]020f49e2010-04-08 19:51:1284
85 // TODO(phajdan.jr): Check return values of the functions below, carefully.
86 file_util::Delete(user_scripts_dir_, true);
[email protected]9eaa021d2010-04-08 19:55:3487 file_util::Delete(extensions_dir_, true);
[email protected]36fb2c7c2011-04-04 15:49:0888
89 InProcessBrowserTest::TearDown();
[email protected]f0488f2f2009-07-01 05:25:2290 }
91
[email protected]919ddc82009-07-15 04:30:1292 void WaitForServicesToStart(int num_expected_extensions,
93 bool expect_extensions_enabled) {
[email protected]06bdd2b2012-11-30 18:47:1394 ExtensionService* service = extensions::ExtensionSystem::Get(
95 browser()->profile())->extension_service();
[email protected]919ddc82009-07-15 04:30:1296
[email protected]fe13bf62010-08-26 14:33:1997 // Count the number of non-component extensions.
98 int found_extensions = 0;
[email protected]84df8332011-12-06 18:22:4699 for (ExtensionSet::const_iterator it = service->extensions()->begin();
100 it != service->extensions()->end(); ++it)
[email protected]1d5e58b2013-01-31 08:41:40101 if ((*it)->location() != extensions::Manifest::COMPONENT)
[email protected]fe13bf62010-08-26 14:33:19102 found_extensions++;
103
[email protected]919ddc82009-07-15 04:30:12104 ASSERT_EQ(static_cast<uint32>(num_expected_extensions),
[email protected]fe13bf62010-08-26 14:33:19105 static_cast<uint32>(found_extensions));
[email protected]919ddc82009-07-15 04:30:12106 ASSERT_EQ(expect_extensions_enabled, service->extensions_enabled());
107
[email protected]a7fe9112012-07-20 02:34:45108 content::WindowedNotificationObserver user_scripts_observer(
[email protected]120abf132011-09-27 21:38:06109 chrome::NOTIFICATION_USER_SCRIPTS_UPDATED,
[email protected]ad50def52011-10-19 23:17:07110 content::NotificationService::AllSources());
[email protected]20f97c92012-07-13 23:12:37111 extensions::UserScriptMaster* master =
[email protected]5d272a42012-10-29 13:28:22112 extensions::ExtensionSystem::Get(browser()->profile())->
113 user_script_master();
[email protected]120abf132011-09-27 21:38:06114 if (!master->ScriptsReady())
115 user_scripts_observer.Wait();
[email protected]919ddc82009-07-15 04:30:12116 ASSERT_TRUE(master->ScriptsReady());
117 }
118
119 void TestInjection(bool expect_css, bool expect_script) {
120 // Load a page affected by the content script and test to see the effect.
[email protected]62771442009-11-22 02:25:04121 FilePath test_file;
122 PathService::Get(chrome::DIR_TEST_DATA, &test_file);
123 test_file = test_file.AppendASCII("extensions")
124 .AppendASCII("test_file.html");
125
126 ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(test_file));
[email protected]919ddc82009-07-15 04:30:12127
128 bool result = false;
[email protected]b6987e02013-01-04 18:30:43129 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
[email protected]47ae23372013-01-29 01:50:48130 browser()->tab_strip_model()->GetActiveWebContents(),
[email protected]06bc5d92013-01-02 22:44:13131 "window.domAutomationController.send("
[email protected]b6987e02013-01-04 18:30:43132 " document.defaultView.getComputedStyle(document.body, null)."
133 " getPropertyValue('background-color') == 'rgb(245, 245, 220)')",
[email protected]9fabbf72010-09-30 21:50:05134 &result));
[email protected]919ddc82009-07-15 04:30:12135 EXPECT_EQ(expect_css, result);
136
137 result = false;
[email protected]b6987e02013-01-04 18:30:43138 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
[email protected]47ae23372013-01-29 01:50:48139 browser()->tab_strip_model()->GetActiveWebContents(),
[email protected]06bc5d92013-01-02 22:44:13140 "window.domAutomationController.send(document.title == 'Modified')",
[email protected]9fabbf72010-09-30 21:50:05141 &result));
[email protected]919ddc82009-07-15 04:30:12142 EXPECT_EQ(expect_script, result);
143 }
144
[email protected]f0488f2f2009-07-01 05:25:22145 FilePath preferences_file_;
146 FilePath extensions_dir_;
147 FilePath user_scripts_dir_;
148 bool enable_extensions_;
[email protected]bfc93562011-12-14 19:40:04149 // Extensions to load from the command line.
150 std::vector<FilePath::StringType> load_extensions_;
[email protected]e50013c32010-08-18 21:05:24151
[email protected]00b5d0a52012-10-30 13:13:53152 // Disable the sideload wipeout UI.
153 FeatureSwitch::ScopedOverride override_sideload_wipeout_;
154
[email protected]e50013c32010-08-18 21:05:24155 int num_expected_extensions_;
[email protected]f0488f2f2009-07-01 05:25:22156};
157
158
159// ExtensionsStartupTest
160// Ensures that we can startup the browser with --enable-extensions and some
161// extensions installed and see them run and do basic things.
162
163class ExtensionsStartupTest : public ExtensionStartupTestBase {
164 public:
165 ExtensionsStartupTest() {
166 enable_extensions_ = true;
167 }
168};
169
[email protected]c5b2c6112010-06-11 00:02:14170IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest, Test) {
[email protected]e50013c32010-08-18 21:05:24171 WaitForServicesToStart(num_expected_extensions_, true);
[email protected]919ddc82009-07-15 04:30:12172 TestInjection(true, true);
173}
[email protected]919ddc82009-07-15 04:30:12174
[email protected]b2f665f52010-07-02 01:32:09175// Sometimes times out on Mac. http://crbug.com/48151
176#if defined(OS_MACOSX)
177#define MAYBE_NoFileAccess DISABLED_NoFileAccess
178#else
179#define MAYBE_NoFileAccess NoFileAccess
180#endif
[email protected]05c82182010-06-24 17:49:08181// Tests that disallowing file access on an extension prevents it from injecting
182// script into a page with a file URL.
[email protected]b2f665f52010-07-02 01:32:09183IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest, MAYBE_NoFileAccess) {
[email protected]e50013c32010-08-18 21:05:24184 WaitForServicesToStart(num_expected_extensions_, true);
[email protected]05c82182010-06-24 17:49:08185
[email protected]84df8332011-12-06 18:22:46186 // Keep a separate list of extensions for which to disable file access, since
187 // doing so reloads them.
[email protected]1c321ee2012-05-21 03:02:34188 std::vector<const extensions::Extension*> extension_list;
[email protected]84df8332011-12-06 18:22:46189
[email protected]06bdd2b2012-11-30 18:47:13190 ExtensionService* service = extensions::ExtensionSystem::Get(
191 browser()->profile())->extension_service();
[email protected]84df8332011-12-06 18:22:46192 for (ExtensionSet::const_iterator it = service->extensions()->begin();
193 it != service->extensions()->end(); ++it) {
[email protected]1d5e58b2013-01-31 08:41:40194 if ((*it)->location() == extensions::Manifest::COMPONENT)
[email protected]fe13bf62010-08-26 14:33:19195 continue;
[email protected]84df8332011-12-06 18:22:46196 if (service->AllowFileAccess(*it))
197 extension_list.push_back(*it);
198 }
199
200 for (size_t i = 0; i < extension_list.size(); ++i) {
[email protected]a7fe9112012-07-20 02:34:45201 content::WindowedNotificationObserver user_scripts_observer(
[email protected]84df8332011-12-06 18:22:46202 chrome::NOTIFICATION_USER_SCRIPTS_UPDATED,
203 content::NotificationService::AllSources());
204 service->SetAllowFileAccess(extension_list[i], false);
205 user_scripts_observer.Wait();
[email protected]05c82182010-06-24 17:49:08206 }
207
208 TestInjection(false, false);
209}
210
[email protected]919ddc82009-07-15 04:30:12211// ExtensionsLoadTest
212// Ensures that we can startup the browser with --load-extension and see them
213// run.
214
215class ExtensionsLoadTest : public ExtensionStartupTestBase {
216 public:
217 ExtensionsLoadTest() {
[email protected]d728e002010-12-08 04:46:23218 enable_extensions_ = true;
[email protected]bfc93562011-12-14 19:40:04219 FilePath one_extension_path;
220 PathService::Get(chrome::DIR_TEST_DATA, &one_extension_path);
221 one_extension_path = one_extension_path
[email protected]919ddc82009-07-15 04:30:12222 .AppendASCII("extensions")
223 .AppendASCII("good")
224 .AppendASCII("Extensions")
225 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
226 .AppendASCII("1.0.0.0");
[email protected]bfc93562011-12-14 19:40:04227 load_extensions_.push_back(one_extension_path.value());
[email protected]f0488f2f2009-07-01 05:25:22228 }
[email protected]919ddc82009-07-15 04:30:12229};
[email protected]f0488f2f2009-07-01 05:25:22230
[email protected]4b109052011-04-29 12:53:24231// Fails inconsistently on Linux x64. http://crbug.com/80961
[email protected]dc128022012-02-17 23:14:58232// TODO(dpapad): Has not failed since October 2011, let's reenable, monitor
233// and act accordingly.
234IN_PROC_BROWSER_TEST_F(ExtensionsLoadTest, Test) {
[email protected]d728e002010-12-08 04:46:23235 WaitForServicesToStart(1, true);
[email protected]919ddc82009-07-15 04:30:12236 TestInjection(true, true);
[email protected]f0488f2f2009-07-01 05:25:22237}
[email protected]bfc93562011-12-14 19:40:04238
239// ExtensionsLoadMultipleTest
240// Ensures that we can startup the browser with multiple extensions
241// via --load-extension=X1,X2,X3.
242class ExtensionsLoadMultipleTest : public ExtensionStartupTestBase {
243 public:
244 ExtensionsLoadMultipleTest() {
245 enable_extensions_ = true;
246 FilePath one_extension_path;
247 PathService::Get(chrome::DIR_TEST_DATA, &one_extension_path);
248 one_extension_path = one_extension_path
249 .AppendASCII("extensions")
250 .AppendASCII("good")
251 .AppendASCII("Extensions")
252 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
253 .AppendASCII("1.0.0.0");
254 load_extensions_.push_back(one_extension_path.value());
255
256 FilePath second_extension_path;
257 PathService::Get(chrome::DIR_TEST_DATA, &second_extension_path);
258 second_extension_path = second_extension_path
259 .AppendASCII("extensions")
260 .AppendASCII("app");
261 load_extensions_.push_back(second_extension_path.value());
262
263 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());
269
270 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());
276 }
277};
278
279IN_PROC_BROWSER_TEST_F(ExtensionsLoadMultipleTest, Test) {
280 WaitForServicesToStart(4, true);
281 TestInjection(true, true);
282}