blob: b2cf8948f7cf06a2db2d7aa0145fc60acde16846 [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"
[email protected]f0488f2f2009-07-01 05:25:228#include "base/file_util.h"
[email protected]57999812013-02-24 05:40:529#include "base/files/file_path.h"
[email protected]f0488f2f2009-07-01 05:25:2210#include "base/path_service.h"
[email protected]00e7bef2013-06-10 20:35:1711#include "base/strings/string_util.h"
[email protected]9ea0cd32013-07-12 01:50:3612#include "chrome/browser/chrome_notification_types.h"
[email protected]eaa7dd182010-12-14 11:09:0013#include "chrome/browser/extensions/extension_service.h"
[email protected]a7ff4b72013-10-17 20:56:0214#include "chrome/browser/extensions/extension_util.h"
[email protected]8cb5d5b2010-02-09 11:36:1615#include "chrome/browser/extensions/user_script_master.h"
[email protected]8ecad5e2010-12-02 21:18:3316#include "chrome/browser/profiles/profile.h"
[email protected]7b5dc002010-11-16 23:08:1017#include "chrome/browser/ui/browser.h"
[email protected]47ae23372013-01-29 01:50:4818#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]f0488f2f2009-07-01 05:25:2219#include "chrome/common/chrome_paths.h"
20#include "chrome/common/chrome_switches.h"
[email protected]af44e7fb2011-07-29 18:32:3221#include "chrome/test/base/in_process_browser_test.h"
[email protected]89dbb1772012-07-17 13:47:2522#include "chrome/test/base/testing_profile.h"
[email protected]af44e7fb2011-07-29 18:32:3223#include "chrome/test/base/ui_test_utils.h"
[email protected]6c2381d2011-10-19 02:52:5324#include "content/public/browser/notification_details.h"
[email protected]ad50def52011-10-19 23:17:0725#include "content/public/browser/notification_service.h"
[email protected]4ca15302012-01-03 05:53:2026#include "content/public/browser/web_contents.h"
[email protected]7d478cb2012-07-24 17:19:4227#include "content/public/test/browser_test_utils.h"
[email protected]1d5cf4142014-01-24 18:25:2228#include "extensions/browser/extension_registry.h"
[email protected]59b0e602014-01-30 00:41:2429#include "extensions/browser/extension_system.h"
[email protected]289c44b2013-12-17 03:26:5730#include "extensions/common/extension.h"
31#include "extensions/common/extension_set.h"
[email protected]5ef835a2013-11-08 20:42:5732#include "extensions/common/feature_switch.h"
[email protected]f0488f2f2009-07-01 05:25:2233#include "net/base/net_util.h"
34
[email protected]00b5d0a52012-10-30 13:13:5335using extensions::FeatureSwitch;
36
[email protected]17c4f3c2009-07-04 16:36:2537// This file contains high-level startup tests for the extensions system. We've
38// had many silly bugs where command line flags did not get propagated correctly
39// into the services, so we didn't start correctly.
[email protected]f0488f2f2009-07-01 05:25:2240
[email protected]3e59bac2010-04-08 16:16:5541class ExtensionStartupTestBase : public InProcessBrowserTest {
[email protected]f0488f2f2009-07-01 05:25:2242 public:
[email protected]00b5d0a52012-10-30 13:13:5343 ExtensionStartupTestBase() :
[email protected]90c780ac2013-04-16 09:26:2844 enable_extensions_(false) {
[email protected]fe13bf62010-08-26 14:33:1945 num_expected_extensions_ = 3;
[email protected]f0488f2f2009-07-01 05:25:2246 }
47
48 protected:
49 // InProcessBrowserTest
[email protected]49aeab62013-02-07 02:53:1150 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
[email protected]10f6b142012-04-14 19:22:4351 if (!enable_extensions_)
52 command_line->AppendSwitch(switches::kDisableExtensions);
53
54 if (!load_extensions_.empty()) {
[email protected]dd6d36672013-05-17 22:50:0255 base::FilePath::StringType paths = JoinString(load_extensions_, ',');
[email protected]10f6b142012-04-14 19:22:4356 command_line->AppendSwitchNative(switches::kLoadExtension,
57 paths);
58 command_line->AppendSwitch(switches::kDisableExtensionsFileAccessCheck);
59 }
60 }
61
[email protected]49aeab62013-02-07 02:53:1162 virtual bool SetUpUserDataDirectory() OVERRIDE {
[email protected]650b2d52013-02-10 03:41:4563 base::FilePath profile_dir;
[email protected]f0488f2f2009-07-01 05:25:2264 PathService::Get(chrome::DIR_USER_DATA, &profile_dir);
[email protected]89dbb1772012-07-17 13:47:2565 profile_dir = profile_dir.AppendASCII(TestingProfile::kTestUserProfileDir);
[email protected]426d1c92013-12-03 20:08:5466 base::CreateDirectory(profile_dir);
[email protected]f0488f2f2009-07-01 05:25:2267
68 preferences_file_ = profile_dir.AppendASCII("Preferences");
69 user_scripts_dir_ = profile_dir.AppendASCII("User Scripts");
70 extensions_dir_ = profile_dir.AppendASCII("Extensions");
71
[email protected]10f6b142012-04-14 19:22:4372 if (enable_extensions_ && load_extensions_.empty()) {
[email protected]650b2d52013-02-10 03:41:4573 base::FilePath src_dir;
[email protected]10f6b142012-04-14 19:22:4374 PathService::Get(chrome::DIR_TEST_DATA, &src_dir);
75 src_dir = src_dir.AppendASCII("extensions").AppendASCII("good");
[email protected]f0488f2f2009-07-01 05:25:2276
[email protected]f0ff2ad2013-07-09 17:42:2677 base::CopyFile(src_dir.AppendASCII("Preferences"), preferences_file_);
78 base::CopyDirectory(src_dir.AppendASCII("Extensions"),
79 profile_dir, true); // recursive
[email protected]f0488f2f2009-07-01 05:25:2280 }
[email protected]10f6b142012-04-14 19:22:4381 return true;
[email protected]f0488f2f2009-07-01 05:25:2282 }
83
[email protected]f0488f2f2009-07-01 05:25:2284 virtual void TearDown() {
[email protected]dd3aa792013-07-16 19:10:2385 EXPECT_TRUE(base::DeleteFile(preferences_file_, false));
[email protected]020f49e2010-04-08 19:51:1286
87 // TODO(phajdan.jr): Check return values of the functions below, carefully.
[email protected]dd3aa792013-07-16 19:10:2388 base::DeleteFile(user_scripts_dir_, true);
89 base::DeleteFile(extensions_dir_, true);
[email protected]36fb2c7c2011-04-04 15:49:0890
91 InProcessBrowserTest::TearDown();
[email protected]f0488f2f2009-07-01 05:25:2292 }
93
[email protected]919ddc82009-07-15 04:30:1294 void WaitForServicesToStart(int num_expected_extensions,
95 bool expect_extensions_enabled) {
[email protected]06bdd2b2012-11-30 18:47:1396 ExtensionService* service = extensions::ExtensionSystem::Get(
97 browser()->profile())->extension_service();
[email protected]919ddc82009-07-15 04:30:1298
[email protected]fe13bf62010-08-26 14:33:1999 // Count the number of non-component extensions.
100 int found_extensions = 0;
[email protected]289c44b2013-12-17 03:26:57101 for (extensions::ExtensionSet::const_iterator it =
102 service->extensions()->begin();
[email protected]1d5cf4142014-01-24 18:25:22103 it != service->extensions()->end(); ++it) {
[email protected]1d5e58b2013-01-31 08:41:40104 if ((*it)->location() != extensions::Manifest::COMPONENT)
[email protected]fe13bf62010-08-26 14:33:19105 found_extensions++;
[email protected]1d5cf4142014-01-24 18:25:22106 }
[email protected]fe13bf62010-08-26 14:33:19107
[email protected]919ddc82009-07-15 04:30:12108 ASSERT_EQ(static_cast<uint32>(num_expected_extensions),
[email protected]fe13bf62010-08-26 14:33:19109 static_cast<uint32>(found_extensions));
[email protected]919ddc82009-07-15 04:30:12110 ASSERT_EQ(expect_extensions_enabled, service->extensions_enabled());
111
[email protected]a7fe9112012-07-20 02:34:45112 content::WindowedNotificationObserver user_scripts_observer(
[email protected]120abf132011-09-27 21:38:06113 chrome::NOTIFICATION_USER_SCRIPTS_UPDATED,
[email protected]ad50def52011-10-19 23:17:07114 content::NotificationService::AllSources());
[email protected]20f97c92012-07-13 23:12:37115 extensions::UserScriptMaster* master =
[email protected]5d272a42012-10-29 13:28:22116 extensions::ExtensionSystem::Get(browser()->profile())->
117 user_script_master();
[email protected]120abf132011-09-27 21:38:06118 if (!master->ScriptsReady())
119 user_scripts_observer.Wait();
[email protected]919ddc82009-07-15 04:30:12120 ASSERT_TRUE(master->ScriptsReady());
121 }
122
123 void TestInjection(bool expect_css, bool expect_script) {
124 // Load a page affected by the content script and test to see the effect.
[email protected]650b2d52013-02-10 03:41:45125 base::FilePath test_file;
[email protected]62771442009-11-22 02:25:04126 PathService::Get(chrome::DIR_TEST_DATA, &test_file);
127 test_file = test_file.AppendASCII("extensions")
128 .AppendASCII("test_file.html");
129
130 ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(test_file));
[email protected]919ddc82009-07-15 04:30:12131
132 bool result = false;
[email protected]b6987e02013-01-04 18:30:43133 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
[email protected]47ae23372013-01-29 01:50:48134 browser()->tab_strip_model()->GetActiveWebContents(),
[email protected]06bc5d92013-01-02 22:44:13135 "window.domAutomationController.send("
[email protected]b6987e02013-01-04 18:30:43136 " document.defaultView.getComputedStyle(document.body, null)."
137 " getPropertyValue('background-color') == 'rgb(245, 245, 220)')",
[email protected]9fabbf72010-09-30 21:50:05138 &result));
[email protected]919ddc82009-07-15 04:30:12139 EXPECT_EQ(expect_css, result);
140
141 result = false;
[email protected]b6987e02013-01-04 18:30:43142 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
[email protected]47ae23372013-01-29 01:50:48143 browser()->tab_strip_model()->GetActiveWebContents(),
[email protected]06bc5d92013-01-02 22:44:13144 "window.domAutomationController.send(document.title == 'Modified')",
[email protected]9fabbf72010-09-30 21:50:05145 &result));
[email protected]919ddc82009-07-15 04:30:12146 EXPECT_EQ(expect_script, result);
147 }
148
[email protected]650b2d52013-02-10 03:41:45149 base::FilePath preferences_file_;
150 base::FilePath extensions_dir_;
151 base::FilePath user_scripts_dir_;
[email protected]f0488f2f2009-07-01 05:25:22152 bool enable_extensions_;
[email protected]bfc93562011-12-14 19:40:04153 // Extensions to load from the command line.
[email protected]650b2d52013-02-10 03:41:45154 std::vector<base::FilePath::StringType> load_extensions_;
[email protected]e50013c32010-08-18 21:05:24155
156 int num_expected_extensions_;
[email protected]f0488f2f2009-07-01 05:25:22157};
158
159
160// ExtensionsStartupTest
161// Ensures that we can startup the browser with --enable-extensions and some
162// extensions installed and see them run and do basic things.
163
164class ExtensionsStartupTest : public ExtensionStartupTestBase {
165 public:
166 ExtensionsStartupTest() {
167 enable_extensions_ = true;
168 }
169};
170
[email protected]c5b2c6112010-06-11 00:02:14171IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest, Test) {
[email protected]e50013c32010-08-18 21:05:24172 WaitForServicesToStart(num_expected_extensions_, true);
[email protected]919ddc82009-07-15 04:30:12173 TestInjection(true, true);
174}
[email protected]919ddc82009-07-15 04:30:12175
[email protected]b2f665f52010-07-02 01:32:09176// Sometimes times out on Mac. http://crbug.com/48151
[email protected]11170a772013-12-13 11:38:32177#if defined(OS_MACOSX)
[email protected]b2f665f52010-07-02 01:32:09178#define MAYBE_NoFileAccess DISABLED_NoFileAccess
179#else
180#define MAYBE_NoFileAccess NoFileAccess
181#endif
[email protected]05c82182010-06-24 17:49:08182// Tests that disallowing file access on an extension prevents it from injecting
183// script into a page with a file URL.
[email protected]b2f665f52010-07-02 01:32:09184IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest, MAYBE_NoFileAccess) {
[email protected]e50013c32010-08-18 21:05:24185 WaitForServicesToStart(num_expected_extensions_, true);
[email protected]05c82182010-06-24 17:49:08186
[email protected]84df8332011-12-06 18:22:46187 // Keep a separate list of extensions for which to disable file access, since
188 // doing so reloads them.
[email protected]1c321ee2012-05-21 03:02:34189 std::vector<const extensions::Extension*> extension_list;
[email protected]84df8332011-12-06 18:22:46190
[email protected]1d5cf4142014-01-24 18:25:22191 extensions::ExtensionRegistry* registry =
192 extensions::ExtensionRegistry::Get(browser()->profile());
[email protected]289c44b2013-12-17 03:26:57193 for (extensions::ExtensionSet::const_iterator it =
[email protected]1d5cf4142014-01-24 18:25:22194 registry->enabled_extensions().begin();
195 it != registry->enabled_extensions().end(); ++it) {
[email protected]1d5e58b2013-01-31 08:41:40196 if ((*it)->location() == extensions::Manifest::COMPONENT)
[email protected]fe13bf62010-08-26 14:33:19197 continue;
[email protected]1d5cf4142014-01-24 18:25:22198 if (extensions::util::AllowFileAccess((*it)->id(), browser()->profile()))
[email protected]cadac622013-06-11 16:46:36199 extension_list.push_back(it->get());
[email protected]84df8332011-12-06 18:22:46200 }
201
202 for (size_t i = 0; i < extension_list.size(); ++i) {
[email protected]a7fe9112012-07-20 02:34:45203 content::WindowedNotificationObserver user_scripts_observer(
[email protected]84df8332011-12-06 18:22:46204 chrome::NOTIFICATION_USER_SCRIPTS_UPDATED,
205 content::NotificationService::AllSources());
[email protected]1d5cf4142014-01-24 18:25:22206 extensions::util::SetAllowFileAccess(
207 extension_list[i]->id(), browser()->profile(), false);
[email protected]84df8332011-12-06 18:22:46208 user_scripts_observer.Wait();
[email protected]05c82182010-06-24 17:49:08209 }
210
211 TestInjection(false, false);
212}
213
[email protected]919ddc82009-07-15 04:30:12214// ExtensionsLoadTest
215// Ensures that we can startup the browser with --load-extension and see them
216// run.
217
218class ExtensionsLoadTest : public ExtensionStartupTestBase {
219 public:
220 ExtensionsLoadTest() {
[email protected]d728e002010-12-08 04:46:23221 enable_extensions_ = true;
[email protected]650b2d52013-02-10 03:41:45222 base::FilePath one_extension_path;
[email protected]bfc93562011-12-14 19:40:04223 PathService::Get(chrome::DIR_TEST_DATA, &one_extension_path);
224 one_extension_path = one_extension_path
[email protected]919ddc82009-07-15 04:30:12225 .AppendASCII("extensions")
226 .AppendASCII("good")
227 .AppendASCII("Extensions")
228 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
229 .AppendASCII("1.0.0.0");
[email protected]bfc93562011-12-14 19:40:04230 load_extensions_.push_back(one_extension_path.value());
[email protected]f0488f2f2009-07-01 05:25:22231 }
[email protected]919ddc82009-07-15 04:30:12232};
[email protected]f0488f2f2009-07-01 05:25:22233
[email protected]4b109052011-04-29 12:53:24234// Fails inconsistently on Linux x64. http://crbug.com/80961
[email protected]dc128022012-02-17 23:14:58235// TODO(dpapad): Has not failed since October 2011, let's reenable, monitor
236// and act accordingly.
237IN_PROC_BROWSER_TEST_F(ExtensionsLoadTest, Test) {
[email protected]d728e002010-12-08 04:46:23238 WaitForServicesToStart(1, true);
[email protected]919ddc82009-07-15 04:30:12239 TestInjection(true, true);
[email protected]f0488f2f2009-07-01 05:25:22240}
[email protected]bfc93562011-12-14 19:40:04241
242// ExtensionsLoadMultipleTest
243// Ensures that we can startup the browser with multiple extensions
244// via --load-extension=X1,X2,X3.
245class ExtensionsLoadMultipleTest : public ExtensionStartupTestBase {
246 public:
247 ExtensionsLoadMultipleTest() {
248 enable_extensions_ = true;
[email protected]650b2d52013-02-10 03:41:45249 base::FilePath one_extension_path;
[email protected]bfc93562011-12-14 19:40:04250 PathService::Get(chrome::DIR_TEST_DATA, &one_extension_path);
251 one_extension_path = one_extension_path
252 .AppendASCII("extensions")
253 .AppendASCII("good")
254 .AppendASCII("Extensions")
255 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
256 .AppendASCII("1.0.0.0");
257 load_extensions_.push_back(one_extension_path.value());
258
[email protected]650b2d52013-02-10 03:41:45259 base::FilePath second_extension_path;
[email protected]bfc93562011-12-14 19:40:04260 PathService::Get(chrome::DIR_TEST_DATA, &second_extension_path);
261 second_extension_path = second_extension_path
262 .AppendASCII("extensions")
263 .AppendASCII("app");
264 load_extensions_.push_back(second_extension_path.value());
265
[email protected]650b2d52013-02-10 03:41:45266 base::FilePath third_extension_path;
[email protected]bfc93562011-12-14 19:40:04267 PathService::Get(chrome::DIR_TEST_DATA, &third_extension_path);
268 third_extension_path = third_extension_path
269 .AppendASCII("extensions")
270 .AppendASCII("app1");
271 load_extensions_.push_back(third_extension_path.value());
272
[email protected]650b2d52013-02-10 03:41:45273 base::FilePath fourth_extension_path;
[email protected]bfc93562011-12-14 19:40:04274 PathService::Get(chrome::DIR_TEST_DATA, &fourth_extension_path);
275 fourth_extension_path = fourth_extension_path
276 .AppendASCII("extensions")
277 .AppendASCII("app2");
278 load_extensions_.push_back(fourth_extension_path.value());
279 }
280};
281
282IN_PROC_BROWSER_TEST_F(ExtensionsLoadMultipleTest, Test) {
283 WaitForServicesToStart(4, true);
284 TestInjection(true, true);
285}