blob: 147c313a37524b20aa5f2cbbc358d7a04e56db04 [file] [log] [blame]
[email protected]55692c052011-01-27 20:13:051// Copyright (c) 2011 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]eaa7dd182010-12-14 11:09:0011#include "chrome/browser/extensions/extension_service.h"
[email protected]8cb5d5b2010-02-09 11:36:1612#include "chrome/browser/extensions/user_script_master.h"
[email protected]8ecad5e2010-12-02 21:18:3313#include "chrome/browser/profiles/profile.h"
[email protected]7b5dc002010-11-16 23:08:1014#include "chrome/browser/ui/browser.h"
[email protected]432115822011-07-10 15:52:2715#include "chrome/common/chrome_notification_types.h"
[email protected]f0488f2f2009-07-01 05:25:2216#include "chrome/common/chrome_paths.h"
17#include "chrome/common/chrome_switches.h"
[email protected]af44e7fb2011-07-29 18:32:3218#include "chrome/test/base/in_process_browser_test.h"
19#include "chrome/test/base/ui_test_utils.h"
[email protected]21f11682011-03-02 16:45:4220#include "content/browser/tab_contents/tab_contents.h"
[email protected]6c2381d2011-10-19 02:52:5321#include "content/public/browser/notification_details.h"
[email protected]f0488f2f2009-07-01 05:25:2222#include "net/base/net_util.h"
23
[email protected]17c4f3c2009-07-04 16:36:2524// This file contains high-level startup tests for the extensions system. We've
25// had many silly bugs where command line flags did not get propagated correctly
26// into the services, so we didn't start correctly.
[email protected]f0488f2f2009-07-01 05:25:2227
[email protected]3e59bac2010-04-08 16:16:5528class ExtensionStartupTestBase : public InProcessBrowserTest {
[email protected]f0488f2f2009-07-01 05:25:2229 public:
[email protected]faed6e12009-11-24 22:38:3630 ExtensionStartupTestBase() : enable_extensions_(false) {
[email protected]fe13bf62010-08-26 14:33:1931 num_expected_extensions_ = 3;
[email protected]f0488f2f2009-07-01 05:25:2232 }
33
34 protected:
35 // InProcessBrowserTest
36 virtual void SetUpCommandLine(CommandLine* command_line) {
[email protected]17c4f3c2009-07-04 16:36:2537 EnableDOMAutomation();
38
[email protected]f0488f2f2009-07-01 05:25:2239 FilePath profile_dir;
40 PathService::Get(chrome::DIR_USER_DATA, &profile_dir);
41 profile_dir = profile_dir.AppendASCII("Default");
42 file_util::CreateDirectory(profile_dir);
43
44 preferences_file_ = profile_dir.AppendASCII("Preferences");
45 user_scripts_dir_ = profile_dir.AppendASCII("User Scripts");
46 extensions_dir_ = profile_dir.AppendASCII("Extensions");
47
48 if (enable_extensions_) {
[email protected]d728e002010-12-08 04:46:2349 if (load_extension_.empty()) {
50 FilePath src_dir;
51 PathService::Get(chrome::DIR_TEST_DATA, &src_dir);
52 src_dir = src_dir.AppendASCII("extensions").AppendASCII("good");
[email protected]f0488f2f2009-07-01 05:25:2253
[email protected]d728e002010-12-08 04:46:2354 file_util::CopyFile(src_dir.AppendASCII("Preferences"),
55 preferences_file_);
56 file_util::CopyDirectory(src_dir.AppendASCII("Extensions"),
57 profile_dir, true); // recursive
58 }
[email protected]6d60703b2009-08-29 01:29:2359 } else {
60 command_line->AppendSwitch(switches::kDisableExtensions);
[email protected]f0488f2f2009-07-01 05:25:2261 }
62
[email protected]4f08c83f2010-07-29 23:02:3463 if (!load_extension_.empty()) {
64 command_line->AppendSwitchPath(switches::kLoadExtension, load_extension_);
[email protected]05c82182010-06-24 17:49:0865 command_line->AppendSwitch(switches::kDisableExtensionsFileAccessCheck);
[email protected]919ddc82009-07-15 04:30:1266 }
[email protected]f0488f2f2009-07-01 05:25:2267 }
68
[email protected]f0488f2f2009-07-01 05:25:2269 virtual void TearDown() {
[email protected]3e59bac2010-04-08 16:16:5570 EXPECT_TRUE(file_util::Delete(preferences_file_, false));
[email protected]020f49e2010-04-08 19:51:1271
72 // TODO(phajdan.jr): Check return values of the functions below, carefully.
73 file_util::Delete(user_scripts_dir_, true);
[email protected]9eaa021d2010-04-08 19:55:3474 file_util::Delete(extensions_dir_, true);
[email protected]36fb2c7c2011-04-04 15:49:0875
76 InProcessBrowserTest::TearDown();
[email protected]f0488f2f2009-07-01 05:25:2277 }
78
[email protected]919ddc82009-07-15 04:30:1279 void WaitForServicesToStart(int num_expected_extensions,
80 bool expect_extensions_enabled) {
[email protected]eaa7dd182010-12-14 11:09:0081 ExtensionService* service = browser()->profile()->GetExtensionService();
[email protected]919ddc82009-07-15 04:30:1282
[email protected]fe13bf62010-08-26 14:33:1983 // Count the number of non-component extensions.
84 int found_extensions = 0;
85 for (size_t i = 0; i < service->extensions()->size(); i++)
86 if (service->extensions()->at(i)->location() != Extension::COMPONENT)
87 found_extensions++;
88
[email protected]919ddc82009-07-15 04:30:1289 ASSERT_EQ(static_cast<uint32>(num_expected_extensions),
[email protected]fe13bf62010-08-26 14:33:1990 static_cast<uint32>(found_extensions));
[email protected]919ddc82009-07-15 04:30:1291 ASSERT_EQ(expect_extensions_enabled, service->extensions_enabled());
92
[email protected]120abf132011-09-27 21:38:0693 ui_test_utils::WindowedNotificationObserver user_scripts_observer(
94 chrome::NOTIFICATION_USER_SCRIPTS_UPDATED,
95 NotificationService::AllSources());
[email protected]919ddc82009-07-15 04:30:1296 UserScriptMaster* master = browser()->profile()->GetUserScriptMaster();
[email protected]120abf132011-09-27 21:38:0697 if (!master->ScriptsReady())
98 user_scripts_observer.Wait();
[email protected]919ddc82009-07-15 04:30:1299 ASSERT_TRUE(master->ScriptsReady());
100 }
101
102 void TestInjection(bool expect_css, bool expect_script) {
103 // Load a page affected by the content script and test to see the effect.
[email protected]62771442009-11-22 02:25:04104 FilePath test_file;
105 PathService::Get(chrome::DIR_TEST_DATA, &test_file);
106 test_file = test_file.AppendASCII("extensions")
107 .AppendASCII("test_file.html");
108
109 ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(test_file));
[email protected]919ddc82009-07-15 04:30:12110
111 bool result = false;
[email protected]9fabbf72010-09-30 21:50:05112 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool(
[email protected]919ddc82009-07-15 04:30:12113 browser()->GetSelectedTabContents()->render_view_host(), L"",
114 L"window.domAutomationController.send("
115 L"document.defaultView.getComputedStyle(document.body, null)."
116 L"getPropertyValue('background-color') == 'rgb(245, 245, 220)')",
[email protected]9fabbf72010-09-30 21:50:05117 &result));
[email protected]919ddc82009-07-15 04:30:12118 EXPECT_EQ(expect_css, result);
119
120 result = false;
[email protected]9fabbf72010-09-30 21:50:05121 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool(
[email protected]919ddc82009-07-15 04:30:12122 browser()->GetSelectedTabContents()->render_view_host(), L"",
123 L"window.domAutomationController.send(document.title == 'Modified')",
[email protected]9fabbf72010-09-30 21:50:05124 &result));
[email protected]919ddc82009-07-15 04:30:12125 EXPECT_EQ(expect_script, result);
126 }
127
[email protected]f0488f2f2009-07-01 05:25:22128 FilePath preferences_file_;
129 FilePath extensions_dir_;
130 FilePath user_scripts_dir_;
131 bool enable_extensions_;
[email protected]919ddc82009-07-15 04:30:12132 FilePath load_extension_;
[email protected]e50013c32010-08-18 21:05:24133
134 int num_expected_extensions_;
[email protected]f0488f2f2009-07-01 05:25:22135};
136
137
138// ExtensionsStartupTest
139// Ensures that we can startup the browser with --enable-extensions and some
140// extensions installed and see them run and do basic things.
141
142class ExtensionsStartupTest : public ExtensionStartupTestBase {
143 public:
144 ExtensionsStartupTest() {
145 enable_extensions_ = true;
146 }
147};
148
[email protected]c5b2c6112010-06-11 00:02:14149IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest, Test) {
[email protected]e50013c32010-08-18 21:05:24150 WaitForServicesToStart(num_expected_extensions_, true);
[email protected]919ddc82009-07-15 04:30:12151 TestInjection(true, true);
152}
[email protected]919ddc82009-07-15 04:30:12153
[email protected]b2f665f52010-07-02 01:32:09154// Sometimes times out on Mac. http://crbug.com/48151
155#if defined(OS_MACOSX)
156#define MAYBE_NoFileAccess DISABLED_NoFileAccess
157#else
158#define MAYBE_NoFileAccess NoFileAccess
159#endif
[email protected]05c82182010-06-24 17:49:08160// Tests that disallowing file access on an extension prevents it from injecting
161// script into a page with a file URL.
[email protected]b2f665f52010-07-02 01:32:09162IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest, MAYBE_NoFileAccess) {
[email protected]e50013c32010-08-18 21:05:24163 WaitForServicesToStart(num_expected_extensions_, true);
[email protected]05c82182010-06-24 17:49:08164
[email protected]eaa7dd182010-12-14 11:09:00165 ExtensionService* service = browser()->profile()->GetExtensionService();
[email protected]05c82182010-06-24 17:49:08166 for (size_t i = 0; i < service->extensions()->size(); ++i) {
[email protected]fe13bf62010-08-26 14:33:19167 if (service->extensions()->at(i)->location() == Extension::COMPONENT)
168 continue;
[email protected]05c82182010-06-24 17:49:08169 if (service->AllowFileAccess(service->extensions()->at(i))) {
[email protected]120abf132011-09-27 21:38:06170 ui_test_utils::WindowedNotificationObserver user_scripts_observer(
171 chrome::NOTIFICATION_USER_SCRIPTS_UPDATED,
172 NotificationService::AllSources());
[email protected]05c82182010-06-24 17:49:08173 service->SetAllowFileAccess(service->extensions()->at(i), false);
[email protected]120abf132011-09-27 21:38:06174 user_scripts_observer.Wait();
[email protected]05c82182010-06-24 17:49:08175 }
176 }
177
178 TestInjection(false, false);
179}
180
[email protected]919ddc82009-07-15 04:30:12181// ExtensionsLoadTest
182// Ensures that we can startup the browser with --load-extension and see them
183// run.
184
185class ExtensionsLoadTest : public ExtensionStartupTestBase {
186 public:
187 ExtensionsLoadTest() {
[email protected]d728e002010-12-08 04:46:23188 enable_extensions_ = true;
[email protected]919ddc82009-07-15 04:30:12189 PathService::Get(chrome::DIR_TEST_DATA, &load_extension_);
190 load_extension_ = load_extension_
191 .AppendASCII("extensions")
192 .AppendASCII("good")
193 .AppendASCII("Extensions")
194 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
195 .AppendASCII("1.0.0.0");
[email protected]f0488f2f2009-07-01 05:25:22196 }
[email protected]919ddc82009-07-15 04:30:12197};
[email protected]f0488f2f2009-07-01 05:25:22198
[email protected]4b109052011-04-29 12:53:24199// Fails inconsistently on Linux x64. http://crbug.com/80961
200#if defined(OS_LINUX) && defined(ARCH_CPU_64_BITS)
201#define Maybe_Test FLAKY_Test
202#else
203#define Maybe_Test Test
204#endif
205
206IN_PROC_BROWSER_TEST_F(ExtensionsLoadTest, Maybe_Test) {
[email protected]d728e002010-12-08 04:46:23207 WaitForServicesToStart(1, true);
[email protected]919ddc82009-07-15 04:30:12208 TestInjection(true, true);
[email protected]f0488f2f2009-07-01 05:25:22209}