blob: 9ca4d081ee7d194dd18f2e4dc66f71732a7199f3 [file] [log] [blame]
[email protected]038d52e12009-10-14 16:53:411// Copyright (c) 2008 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.
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"
11#include "chrome/browser/browser.h"
12#include "chrome/browser/extensions/extensions_service.h"
[email protected]8cb5d5b2010-02-09 11:36:1613#include "chrome/browser/extensions/user_script_master.h"
[email protected]f0488f2f2009-07-01 05:25:2214#include "chrome/browser/profile.h"
[email protected]17c4f3c2009-07-04 16:36:2515#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]f0488f2f2009-07-01 05:25:2216#include "chrome/common/chrome_paths.h"
17#include "chrome/common/chrome_switches.h"
18#include "chrome/common/notification_details.h"
19#include "chrome/common/notification_observer.h"
20#include "chrome/common/notification_registrar.h"
21#include "chrome/common/notification_service.h"
22#include "chrome/common/notification_type.h"
23#include "chrome/test/in_process_browser_test.h"
24#include "chrome/test/ui_test_utils.h"
25#include "net/base/net_util.h"
26
[email protected]17c4f3c2009-07-04 16:36:2527// This file contains high-level startup tests for the extensions system. We've
28// had many silly bugs where command line flags did not get propagated correctly
29// into the services, so we didn't start correctly.
[email protected]f0488f2f2009-07-01 05:25:2230
[email protected]3e59bac2010-04-08 16:16:5531class ExtensionStartupTestBase : public InProcessBrowserTest {
[email protected]f0488f2f2009-07-01 05:25:2232 public:
[email protected]faed6e12009-11-24 22:38:3633 ExtensionStartupTestBase() : enable_extensions_(false) {
[email protected]e50013c32010-08-18 21:05:2434#if defined(OS_CHROMEOS)
35 num_expected_extensions_ = 3;
36#else
37 num_expected_extensions_ = 4;
38#endif
[email protected]f0488f2f2009-07-01 05:25:2239 }
40
41 protected:
42 // InProcessBrowserTest
43 virtual void SetUpCommandLine(CommandLine* command_line) {
[email protected]17c4f3c2009-07-04 16:36:2544 EnableDOMAutomation();
45
[email protected]f0488f2f2009-07-01 05:25:2246 FilePath profile_dir;
47 PathService::Get(chrome::DIR_USER_DATA, &profile_dir);
48 profile_dir = profile_dir.AppendASCII("Default");
49 file_util::CreateDirectory(profile_dir);
50
51 preferences_file_ = profile_dir.AppendASCII("Preferences");
52 user_scripts_dir_ = profile_dir.AppendASCII("User Scripts");
53 extensions_dir_ = profile_dir.AppendASCII("Extensions");
54
55 if (enable_extensions_) {
[email protected]f0488f2f2009-07-01 05:25:2256 FilePath src_dir;
57 PathService::Get(chrome::DIR_TEST_DATA, &src_dir);
58 src_dir = src_dir.AppendASCII("extensions").AppendASCII("good");
59
60 file_util::CopyFile(src_dir.AppendASCII("Preferences"),
61 preferences_file_);
62 file_util::CopyDirectory(src_dir.AppendASCII("Extensions"),
63 profile_dir, true); // recursive
[email protected]6d60703b2009-08-29 01:29:2364 } else {
65 command_line->AppendSwitch(switches::kDisableExtensions);
[email protected]f0488f2f2009-07-01 05:25:2266 }
67
[email protected]4f08c83f2010-07-29 23:02:3468 if (!load_extension_.empty()) {
69 command_line->AppendSwitchPath(switches::kLoadExtension, load_extension_);
[email protected]05c82182010-06-24 17:49:0870 command_line->AppendSwitch(switches::kDisableExtensionsFileAccessCheck);
[email protected]919ddc82009-07-15 04:30:1271 }
[email protected]f0488f2f2009-07-01 05:25:2272 }
73
[email protected]f0488f2f2009-07-01 05:25:2274 virtual void TearDown() {
[email protected]3e59bac2010-04-08 16:16:5575 EXPECT_TRUE(file_util::Delete(preferences_file_, false));
[email protected]020f49e2010-04-08 19:51:1276
77 // TODO(phajdan.jr): Check return values of the functions below, carefully.
78 file_util::Delete(user_scripts_dir_, true);
[email protected]9eaa021d2010-04-08 19:55:3479 file_util::Delete(extensions_dir_, true);
[email protected]f0488f2f2009-07-01 05:25:2280 }
81
[email protected]919ddc82009-07-15 04:30:1282 void WaitForServicesToStart(int num_expected_extensions,
83 bool expect_extensions_enabled) {
84 ExtensionsService* service = browser()->profile()->GetExtensionsService();
[email protected]3e59bac2010-04-08 16:16:5585 if (!service->is_ready())
86 ui_test_utils::WaitForNotification(NotificationType::EXTENSIONS_READY);
87 ASSERT_TRUE(service->is_ready());
[email protected]919ddc82009-07-15 04:30:1288
89 ASSERT_EQ(static_cast<uint32>(num_expected_extensions),
90 service->extensions()->size());
91 ASSERT_EQ(expect_extensions_enabled, service->extensions_enabled());
92
93 UserScriptMaster* master = browser()->profile()->GetUserScriptMaster();
94 if (!master->ScriptsReady()) {
[email protected]3e59bac2010-04-08 16:16:5595 ui_test_utils::WaitForNotification(
96 NotificationType::USER_SCRIPTS_UPDATED);
[email protected]919ddc82009-07-15 04:30:1297 }
98 ASSERT_TRUE(master->ScriptsReady());
99 }
100
101 void TestInjection(bool expect_css, bool expect_script) {
102 // Load a page affected by the content script and test to see the effect.
[email protected]62771442009-11-22 02:25:04103 FilePath test_file;
104 PathService::Get(chrome::DIR_TEST_DATA, &test_file);
105 test_file = test_file.AppendASCII("extensions")
106 .AppendASCII("test_file.html");
107
108 ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(test_file));
[email protected]919ddc82009-07-15 04:30:12109
110 bool result = false;
111 ui_test_utils::ExecuteJavaScriptAndExtractBool(
112 browser()->GetSelectedTabContents()->render_view_host(), L"",
113 L"window.domAutomationController.send("
114 L"document.defaultView.getComputedStyle(document.body, null)."
115 L"getPropertyValue('background-color') == 'rgb(245, 245, 220)')",
116 &result);
117 EXPECT_EQ(expect_css, result);
118
119 result = false;
120 ui_test_utils::ExecuteJavaScriptAndExtractBool(
121 browser()->GetSelectedTabContents()->render_view_host(), L"",
122 L"window.domAutomationController.send(document.title == 'Modified')",
123 &result);
124 EXPECT_EQ(expect_script, result);
125 }
126
[email protected]f0488f2f2009-07-01 05:25:22127 FilePath preferences_file_;
128 FilePath extensions_dir_;
129 FilePath user_scripts_dir_;
130 bool enable_extensions_;
[email protected]919ddc82009-07-15 04:30:12131 FilePath load_extension_;
[email protected]e50013c32010-08-18 21:05:24132
133 int num_expected_extensions_;
[email protected]f0488f2f2009-07-01 05:25:22134};
135
136
137// ExtensionsStartupTest
138// Ensures that we can startup the browser with --enable-extensions and some
139// extensions installed and see them run and do basic things.
140
141class ExtensionsStartupTest : public ExtensionStartupTestBase {
142 public:
143 ExtensionsStartupTest() {
144 enable_extensions_ = true;
145 }
146};
147
[email protected]c5b2c6112010-06-11 00:02:14148IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest, Test) {
[email protected]e50013c32010-08-18 21:05:24149 // 1 component extension and 2 or 3 others, depending on the platform.
150 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 // 1 component extension and 2 or 3 others, depending on the platform.
164 WaitForServicesToStart(num_expected_extensions_, true);
[email protected]05c82182010-06-24 17:49:08165
166 ExtensionsService* service = browser()->profile()->GetExtensionsService();
167 for (size_t i = 0; i < service->extensions()->size(); ++i) {
168 if (service->AllowFileAccess(service->extensions()->at(i))) {
169 service->SetAllowFileAccess(service->extensions()->at(i), false);
170 ui_test_utils::WaitForNotification(
171 NotificationType::USER_SCRIPTS_UPDATED);
172 }
173 }
174
175 TestInjection(false, false);
176}
177
[email protected]919ddc82009-07-15 04:30:12178// ExtensionsLoadTest
179// Ensures that we can startup the browser with --load-extension and see them
180// run.
181
182class ExtensionsLoadTest : public ExtensionStartupTestBase {
183 public:
184 ExtensionsLoadTest() {
185 PathService::Get(chrome::DIR_TEST_DATA, &load_extension_);
186 load_extension_ = load_extension_
187 .AppendASCII("extensions")
188 .AppendASCII("good")
189 .AppendASCII("Extensions")
190 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
191 .AppendASCII("1.0.0.0");
[email protected]f0488f2f2009-07-01 05:25:22192 }
[email protected]919ddc82009-07-15 04:30:12193};
[email protected]f0488f2f2009-07-01 05:25:22194
[email protected]27d537382010-06-10 23:55:02195// Flaky (times out) on Mac/Windows. http://crbug.com/46301.
[email protected]5e34a852010-06-21 14:44:49196#if defined(OS_MACOSX) || defined(OS_WIN)
[email protected]27d537382010-06-10 23:55:02197#define MAYBE_Test FLAKY_Test
198#else
199#define MAYBE_Test Test
200#endif
201IN_PROC_BROWSER_TEST_F(ExtensionsLoadTest, MAYBE_Test) {
[email protected]919ddc82009-07-15 04:30:12202 WaitForServicesToStart(1, false);
203 TestInjection(true, true);
[email protected]f0488f2f2009-07-01 05:25:22204}