blob: 48323814c2c63bcfc1e7faa04b550db0c174b3b4 [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]8cb5d5b2010-02-09 11:36:1613#include "chrome/browser/extensions/user_script_master.h"
[email protected]8ecad5e2010-12-02 21:18:3314#include "chrome/browser/profiles/profile.h"
[email protected]7b5dc002010-11-16 23:08:1015#include "chrome/browser/ui/browser.h"
[email protected]52877dbc62012-06-29 22:22:0316#include "chrome/browser/ui/browser_tabstrip.h"
[email protected]432115822011-07-10 15:52:2717#include "chrome/common/chrome_notification_types.h"
[email protected]f0488f2f2009-07-01 05:25:2218#include "chrome/common/chrome_paths.h"
19#include "chrome/common/chrome_switches.h"
[email protected]af44e7fb2011-07-29 18:32:3220#include "chrome/test/base/in_process_browser_test.h"
[email protected]89dbb1772012-07-17 13:47:2521#include "chrome/test/base/testing_profile.h"
[email protected]af44e7fb2011-07-29 18:32:3222#include "chrome/test/base/ui_test_utils.h"
[email protected]6c2381d2011-10-19 02:52:5323#include "content/public/browser/notification_details.h"
[email protected]ad50def52011-10-19 23:17:0724#include "content/public/browser/notification_service.h"
[email protected]4ca15302012-01-03 05:53:2025#include "content/public/browser/web_contents.h"
[email protected]7d478cb2012-07-24 17:19:4226#include "content/public/test/browser_test_utils.h"
[email protected]f0488f2f2009-07-01 05:25:2227#include "net/base/net_util.h"
28
[email protected]17c4f3c2009-07-04 16:36:2529// This file contains high-level startup tests for the extensions system. We've
30// had many silly bugs where command line flags did not get propagated correctly
31// into the services, so we didn't start correctly.
[email protected]f0488f2f2009-07-01 05:25:2232
[email protected]3e59bac2010-04-08 16:16:5533class ExtensionStartupTestBase : public InProcessBrowserTest {
[email protected]f0488f2f2009-07-01 05:25:2234 public:
[email protected]faed6e12009-11-24 22:38:3635 ExtensionStartupTestBase() : enable_extensions_(false) {
[email protected]fe13bf62010-08-26 14:33:1936 num_expected_extensions_ = 3;
[email protected]f0488f2f2009-07-01 05:25:2237 }
38
39 protected:
40 // InProcessBrowserTest
41 virtual void SetUpCommandLine(CommandLine* command_line) {
[email protected]10f6b142012-04-14 19:22:4342 if (!enable_extensions_)
43 command_line->AppendSwitch(switches::kDisableExtensions);
44
45 if (!load_extensions_.empty()) {
46 FilePath::StringType paths = JoinString(load_extensions_, ',');
47 command_line->AppendSwitchNative(switches::kLoadExtension,
48 paths);
49 command_line->AppendSwitch(switches::kDisableExtensionsFileAccessCheck);
50 }
51 }
52
53 virtual bool SetUpUserDataDirectory() {
[email protected]f0488f2f2009-07-01 05:25:2254 FilePath profile_dir;
55 PathService::Get(chrome::DIR_USER_DATA, &profile_dir);
[email protected]89dbb1772012-07-17 13:47:2556 profile_dir = profile_dir.AppendASCII(TestingProfile::kTestUserProfileDir);
[email protected]f0488f2f2009-07-01 05:25:2257 file_util::CreateDirectory(profile_dir);
58
59 preferences_file_ = profile_dir.AppendASCII("Preferences");
60 user_scripts_dir_ = profile_dir.AppendASCII("User Scripts");
61 extensions_dir_ = profile_dir.AppendASCII("Extensions");
62
[email protected]10f6b142012-04-14 19:22:4363 if (enable_extensions_ && load_extensions_.empty()) {
64 FilePath src_dir;
65 PathService::Get(chrome::DIR_TEST_DATA, &src_dir);
66 src_dir = src_dir.AppendASCII("extensions").AppendASCII("good");
[email protected]f0488f2f2009-07-01 05:25:2267
[email protected]10f6b142012-04-14 19:22:4368 file_util::CopyFile(src_dir.AppendASCII("Preferences"),
69 preferences_file_);
70 file_util::CopyDirectory(src_dir.AppendASCII("Extensions"),
71 profile_dir, true); // recursive
[email protected]f0488f2f2009-07-01 05:25:2272 }
[email protected]10f6b142012-04-14 19:22:4373 return true;
[email protected]f0488f2f2009-07-01 05:25:2274 }
75
[email protected]f0488f2f2009-07-01 05:25:2276 virtual void TearDown() {
[email protected]3e59bac2010-04-08 16:16:5577 EXPECT_TRUE(file_util::Delete(preferences_file_, false));
[email protected]020f49e2010-04-08 19:51:1278
79 // TODO(phajdan.jr): Check return values of the functions below, carefully.
80 file_util::Delete(user_scripts_dir_, true);
[email protected]9eaa021d2010-04-08 19:55:3481 file_util::Delete(extensions_dir_, true);
[email protected]36fb2c7c2011-04-04 15:49:0882
83 InProcessBrowserTest::TearDown();
[email protected]f0488f2f2009-07-01 05:25:2284 }
85
[email protected]919ddc82009-07-15 04:30:1286 void WaitForServicesToStart(int num_expected_extensions,
87 bool expect_extensions_enabled) {
[email protected]eaa7dd182010-12-14 11:09:0088 ExtensionService* service = browser()->profile()->GetExtensionService();
[email protected]919ddc82009-07-15 04:30:1289
[email protected]fe13bf62010-08-26 14:33:1990 // Count the number of non-component extensions.
91 int found_extensions = 0;
[email protected]84df8332011-12-06 18:22:4692 for (ExtensionSet::const_iterator it = service->extensions()->begin();
93 it != service->extensions()->end(); ++it)
[email protected]1c321ee2012-05-21 03:02:3494 if ((*it)->location() != extensions::Extension::COMPONENT)
[email protected]fe13bf62010-08-26 14:33:1995 found_extensions++;
96
[email protected]919ddc82009-07-15 04:30:1297 ASSERT_EQ(static_cast<uint32>(num_expected_extensions),
[email protected]fe13bf62010-08-26 14:33:1998 static_cast<uint32>(found_extensions));
[email protected]919ddc82009-07-15 04:30:1299 ASSERT_EQ(expect_extensions_enabled, service->extensions_enabled());
100
[email protected]a7fe9112012-07-20 02:34:45101 content::WindowedNotificationObserver user_scripts_observer(
[email protected]120abf132011-09-27 21:38:06102 chrome::NOTIFICATION_USER_SCRIPTS_UPDATED,
[email protected]ad50def52011-10-19 23:17:07103 content::NotificationService::AllSources());
[email protected]20f97c92012-07-13 23:12:37104 extensions::UserScriptMaster* master =
105 browser()->profile()->GetUserScriptMaster();
[email protected]120abf132011-09-27 21:38:06106 if (!master->ScriptsReady())
107 user_scripts_observer.Wait();
[email protected]919ddc82009-07-15 04:30:12108 ASSERT_TRUE(master->ScriptsReady());
109 }
110
111 void TestInjection(bool expect_css, bool expect_script) {
112 // Load a page affected by the content script and test to see the effect.
[email protected]62771442009-11-22 02:25:04113 FilePath test_file;
114 PathService::Get(chrome::DIR_TEST_DATA, &test_file);
115 test_file = test_file.AppendASCII("extensions")
116 .AppendASCII("test_file.html");
117
118 ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(test_file));
[email protected]919ddc82009-07-15 04:30:12119
120 bool result = false;
[email protected]7d478cb2012-07-24 17:19:42121 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractBool(
[email protected]52877dbc62012-06-29 22:22:03122 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), L"",
[email protected]919ddc82009-07-15 04:30:12123 L"window.domAutomationController.send("
124 L"document.defaultView.getComputedStyle(document.body, null)."
125 L"getPropertyValue('background-color') == 'rgb(245, 245, 220)')",
[email protected]9fabbf72010-09-30 21:50:05126 &result));
[email protected]919ddc82009-07-15 04:30:12127 EXPECT_EQ(expect_css, result);
128
129 result = false;
[email protected]7d478cb2012-07-24 17:19:42130 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractBool(
[email protected]52877dbc62012-06-29 22:22:03131 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), L"",
[email protected]919ddc82009-07-15 04:30:12132 L"window.domAutomationController.send(document.title == 'Modified')",
[email protected]9fabbf72010-09-30 21:50:05133 &result));
[email protected]919ddc82009-07-15 04:30:12134 EXPECT_EQ(expect_script, result);
135 }
136
[email protected]f0488f2f2009-07-01 05:25:22137 FilePath preferences_file_;
138 FilePath extensions_dir_;
139 FilePath user_scripts_dir_;
140 bool enable_extensions_;
[email protected]bfc93562011-12-14 19:40:04141 // Extensions to load from the command line.
142 std::vector<FilePath::StringType> load_extensions_;
[email protected]e50013c32010-08-18 21:05:24143
144 int num_expected_extensions_;
[email protected]f0488f2f2009-07-01 05:25:22145};
146
147
148// ExtensionsStartupTest
149// Ensures that we can startup the browser with --enable-extensions and some
150// extensions installed and see them run and do basic things.
151
152class ExtensionsStartupTest : public ExtensionStartupTestBase {
153 public:
154 ExtensionsStartupTest() {
155 enable_extensions_ = true;
156 }
157};
158
[email protected]c5b2c6112010-06-11 00:02:14159IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest, Test) {
[email protected]e50013c32010-08-18 21:05:24160 WaitForServicesToStart(num_expected_extensions_, true);
[email protected]919ddc82009-07-15 04:30:12161 TestInjection(true, true);
162}
[email protected]919ddc82009-07-15 04:30:12163
[email protected]b2f665f52010-07-02 01:32:09164// Sometimes times out on Mac. http://crbug.com/48151
165#if defined(OS_MACOSX)
166#define MAYBE_NoFileAccess DISABLED_NoFileAccess
167#else
168#define MAYBE_NoFileAccess NoFileAccess
169#endif
[email protected]05c82182010-06-24 17:49:08170// Tests that disallowing file access on an extension prevents it from injecting
171// script into a page with a file URL.
[email protected]b2f665f52010-07-02 01:32:09172IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest, MAYBE_NoFileAccess) {
[email protected]e50013c32010-08-18 21:05:24173 WaitForServicesToStart(num_expected_extensions_, true);
[email protected]05c82182010-06-24 17:49:08174
[email protected]84df8332011-12-06 18:22:46175 // Keep a separate list of extensions for which to disable file access, since
176 // doing so reloads them.
[email protected]1c321ee2012-05-21 03:02:34177 std::vector<const extensions::Extension*> extension_list;
[email protected]84df8332011-12-06 18:22:46178
[email protected]eaa7dd182010-12-14 11:09:00179 ExtensionService* service = browser()->profile()->GetExtensionService();
[email protected]84df8332011-12-06 18:22:46180 for (ExtensionSet::const_iterator it = service->extensions()->begin();
181 it != service->extensions()->end(); ++it) {
[email protected]1c321ee2012-05-21 03:02:34182 if ((*it)->location() == extensions::Extension::COMPONENT)
[email protected]fe13bf62010-08-26 14:33:19183 continue;
[email protected]84df8332011-12-06 18:22:46184 if (service->AllowFileAccess(*it))
185 extension_list.push_back(*it);
186 }
187
188 for (size_t i = 0; i < extension_list.size(); ++i) {
[email protected]a7fe9112012-07-20 02:34:45189 content::WindowedNotificationObserver user_scripts_observer(
[email protected]84df8332011-12-06 18:22:46190 chrome::NOTIFICATION_USER_SCRIPTS_UPDATED,
191 content::NotificationService::AllSources());
192 service->SetAllowFileAccess(extension_list[i], false);
193 user_scripts_observer.Wait();
[email protected]05c82182010-06-24 17:49:08194 }
195
196 TestInjection(false, false);
197}
198
[email protected]919ddc82009-07-15 04:30:12199// ExtensionsLoadTest
200// Ensures that we can startup the browser with --load-extension and see them
201// run.
202
203class ExtensionsLoadTest : public ExtensionStartupTestBase {
204 public:
205 ExtensionsLoadTest() {
[email protected]d728e002010-12-08 04:46:23206 enable_extensions_ = true;
[email protected]bfc93562011-12-14 19:40:04207 FilePath one_extension_path;
208 PathService::Get(chrome::DIR_TEST_DATA, &one_extension_path);
209 one_extension_path = one_extension_path
[email protected]919ddc82009-07-15 04:30:12210 .AppendASCII("extensions")
211 .AppendASCII("good")
212 .AppendASCII("Extensions")
213 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
214 .AppendASCII("1.0.0.0");
[email protected]bfc93562011-12-14 19:40:04215 load_extensions_.push_back(one_extension_path.value());
[email protected]f0488f2f2009-07-01 05:25:22216 }
[email protected]919ddc82009-07-15 04:30:12217};
[email protected]f0488f2f2009-07-01 05:25:22218
[email protected]4b109052011-04-29 12:53:24219// Fails inconsistently on Linux x64. http://crbug.com/80961
[email protected]dc128022012-02-17 23:14:58220// TODO(dpapad): Has not failed since October 2011, let's reenable, monitor
221// and act accordingly.
222IN_PROC_BROWSER_TEST_F(ExtensionsLoadTest, Test) {
[email protected]d728e002010-12-08 04:46:23223 WaitForServicesToStart(1, true);
[email protected]919ddc82009-07-15 04:30:12224 TestInjection(true, true);
[email protected]f0488f2f2009-07-01 05:25:22225}
[email protected]bfc93562011-12-14 19:40:04226
227// ExtensionsLoadMultipleTest
228// Ensures that we can startup the browser with multiple extensions
229// via --load-extension=X1,X2,X3.
230class ExtensionsLoadMultipleTest : public ExtensionStartupTestBase {
231 public:
232 ExtensionsLoadMultipleTest() {
233 enable_extensions_ = true;
234 FilePath one_extension_path;
235 PathService::Get(chrome::DIR_TEST_DATA, &one_extension_path);
236 one_extension_path = one_extension_path
237 .AppendASCII("extensions")
238 .AppendASCII("good")
239 .AppendASCII("Extensions")
240 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
241 .AppendASCII("1.0.0.0");
242 load_extensions_.push_back(one_extension_path.value());
243
244 FilePath second_extension_path;
245 PathService::Get(chrome::DIR_TEST_DATA, &second_extension_path);
246 second_extension_path = second_extension_path
247 .AppendASCII("extensions")
248 .AppendASCII("app");
249 load_extensions_.push_back(second_extension_path.value());
250
251 FilePath third_extension_path;
252 PathService::Get(chrome::DIR_TEST_DATA, &third_extension_path);
253 third_extension_path = third_extension_path
254 .AppendASCII("extensions")
255 .AppendASCII("app1");
256 load_extensions_.push_back(third_extension_path.value());
257
258 FilePath fourth_extension_path;
259 PathService::Get(chrome::DIR_TEST_DATA, &fourth_extension_path);
260 fourth_extension_path = fourth_extension_path
261 .AppendASCII("extensions")
262 .AppendASCII("app2");
263 load_extensions_.push_back(fourth_extension_path.value());
264 }
265};
266
267IN_PROC_BROWSER_TEST_F(ExtensionsLoadMultipleTest, Test) {
268 WaitForServicesToStart(4, true);
269 TestInjection(true, true);
270}