blob: 0d056d0813ac6bc4cafba7b0b2b53815fc292ef9 [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]f0488f2f2009-07-01 05:25:2234 }
35
36 protected:
37 // InProcessBrowserTest
38 virtual void SetUpCommandLine(CommandLine* command_line) {
[email protected]17c4f3c2009-07-04 16:36:2539 EnableDOMAutomation();
40
[email protected]f0488f2f2009-07-01 05:25:2241 FilePath profile_dir;
42 PathService::Get(chrome::DIR_USER_DATA, &profile_dir);
43 profile_dir = profile_dir.AppendASCII("Default");
44 file_util::CreateDirectory(profile_dir);
45
46 preferences_file_ = profile_dir.AppendASCII("Preferences");
47 user_scripts_dir_ = profile_dir.AppendASCII("User Scripts");
48 extensions_dir_ = profile_dir.AppendASCII("Extensions");
49
50 if (enable_extensions_) {
[email protected]f0488f2f2009-07-01 05:25:2251 FilePath src_dir;
52 PathService::Get(chrome::DIR_TEST_DATA, &src_dir);
53 src_dir = src_dir.AppendASCII("extensions").AppendASCII("good");
54
55 file_util::CopyFile(src_dir.AppendASCII("Preferences"),
56 preferences_file_);
57 file_util::CopyDirectory(src_dir.AppendASCII("Extensions"),
58 profile_dir, true); // recursive
[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]919ddc82009-07-15 04:30:1263 if (!load_extension_.value().empty()) {
64 command_line->AppendSwitchWithValue(switches::kLoadExtension,
65 load_extension_.ToWStringHack());
[email protected]05c82182010-06-24 17:49:0866 command_line->AppendSwitch(switches::kDisableExtensionsFileAccessCheck);
[email protected]919ddc82009-07-15 04:30:1267 }
[email protected]f0488f2f2009-07-01 05:25:2268 }
69
[email protected]f0488f2f2009-07-01 05:25:2270 virtual void TearDown() {
[email protected]3e59bac2010-04-08 16:16:5571 EXPECT_TRUE(file_util::Delete(preferences_file_, false));
[email protected]020f49e2010-04-08 19:51:1272
73 // TODO(phajdan.jr): Check return values of the functions below, carefully.
74 file_util::Delete(user_scripts_dir_, true);
[email protected]9eaa021d2010-04-08 19:55:3475 file_util::Delete(extensions_dir_, true);
[email protected]f0488f2f2009-07-01 05:25:2276 }
77
[email protected]919ddc82009-07-15 04:30:1278 void WaitForServicesToStart(int num_expected_extensions,
79 bool expect_extensions_enabled) {
80 ExtensionsService* service = browser()->profile()->GetExtensionsService();
[email protected]3e59bac2010-04-08 16:16:5581 if (!service->is_ready())
82 ui_test_utils::WaitForNotification(NotificationType::EXTENSIONS_READY);
83 ASSERT_TRUE(service->is_ready());
[email protected]919ddc82009-07-15 04:30:1284
85 ASSERT_EQ(static_cast<uint32>(num_expected_extensions),
86 service->extensions()->size());
87 ASSERT_EQ(expect_extensions_enabled, service->extensions_enabled());
88
89 UserScriptMaster* master = browser()->profile()->GetUserScriptMaster();
90 if (!master->ScriptsReady()) {
[email protected]3e59bac2010-04-08 16:16:5591 ui_test_utils::WaitForNotification(
92 NotificationType::USER_SCRIPTS_UPDATED);
[email protected]919ddc82009-07-15 04:30:1293 }
94 ASSERT_TRUE(master->ScriptsReady());
95 }
96
97 void TestInjection(bool expect_css, bool expect_script) {
98 // Load a page affected by the content script and test to see the effect.
[email protected]62771442009-11-22 02:25:0499 FilePath test_file;
100 PathService::Get(chrome::DIR_TEST_DATA, &test_file);
101 test_file = test_file.AppendASCII("extensions")
102 .AppendASCII("test_file.html");
103
104 ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(test_file));
[email protected]919ddc82009-07-15 04:30:12105
106 bool result = false;
107 ui_test_utils::ExecuteJavaScriptAndExtractBool(
108 browser()->GetSelectedTabContents()->render_view_host(), L"",
109 L"window.domAutomationController.send("
110 L"document.defaultView.getComputedStyle(document.body, null)."
111 L"getPropertyValue('background-color') == 'rgb(245, 245, 220)')",
112 &result);
113 EXPECT_EQ(expect_css, result);
114
115 result = false;
116 ui_test_utils::ExecuteJavaScriptAndExtractBool(
117 browser()->GetSelectedTabContents()->render_view_host(), L"",
118 L"window.domAutomationController.send(document.title == 'Modified')",
119 &result);
120 EXPECT_EQ(expect_script, result);
121 }
122
[email protected]f0488f2f2009-07-01 05:25:22123 FilePath preferences_file_;
124 FilePath extensions_dir_;
125 FilePath user_scripts_dir_;
126 bool enable_extensions_;
[email protected]919ddc82009-07-15 04:30:12127 FilePath load_extension_;
[email protected]f0488f2f2009-07-01 05:25:22128};
129
130
131// ExtensionsStartupTest
132// Ensures that we can startup the browser with --enable-extensions and some
133// extensions installed and see them run and do basic things.
134
135class ExtensionsStartupTest : public ExtensionStartupTestBase {
136 public:
137 ExtensionsStartupTest() {
138 enable_extensions_ = true;
139 }
140};
141
[email protected]c5b2c6112010-06-11 00:02:14142IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest, Test) {
[email protected]4b776892010-03-19 22:57:33143 WaitForServicesToStart(4, true); // 1 component extension and 3 others.
[email protected]919ddc82009-07-15 04:30:12144 TestInjection(true, true);
145}
[email protected]919ddc82009-07-15 04:30:12146
[email protected]b2f665f52010-07-02 01:32:09147// Sometimes times out on Mac. http://crbug.com/48151
148#if defined(OS_MACOSX)
149#define MAYBE_NoFileAccess DISABLED_NoFileAccess
150#else
151#define MAYBE_NoFileAccess NoFileAccess
152#endif
[email protected]05c82182010-06-24 17:49:08153// Tests that disallowing file access on an extension prevents it from injecting
154// script into a page with a file URL.
[email protected]b2f665f52010-07-02 01:32:09155IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest, MAYBE_NoFileAccess) {
[email protected]05c82182010-06-24 17:49:08156 WaitForServicesToStart(4, true); // 1 component extension and 3 others.
157
158 ExtensionsService* service = browser()->profile()->GetExtensionsService();
159 for (size_t i = 0; i < service->extensions()->size(); ++i) {
160 if (service->AllowFileAccess(service->extensions()->at(i))) {
161 service->SetAllowFileAccess(service->extensions()->at(i), false);
162 ui_test_utils::WaitForNotification(
163 NotificationType::USER_SCRIPTS_UPDATED);
164 }
165 }
166
167 TestInjection(false, false);
168}
169
[email protected]919ddc82009-07-15 04:30:12170// ExtensionsLoadTest
171// Ensures that we can startup the browser with --load-extension and see them
172// run.
173
174class ExtensionsLoadTest : public ExtensionStartupTestBase {
175 public:
176 ExtensionsLoadTest() {
177 PathService::Get(chrome::DIR_TEST_DATA, &load_extension_);
178 load_extension_ = load_extension_
179 .AppendASCII("extensions")
180 .AppendASCII("good")
181 .AppendASCII("Extensions")
182 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
183 .AppendASCII("1.0.0.0");
[email protected]f0488f2f2009-07-01 05:25:22184 }
[email protected]919ddc82009-07-15 04:30:12185};
[email protected]f0488f2f2009-07-01 05:25:22186
[email protected]27d537382010-06-10 23:55:02187// Flaky (times out) on Mac/Windows. http://crbug.com/46301.
[email protected]5e34a852010-06-21 14:44:49188#if defined(OS_MACOSX) || defined(OS_WIN)
[email protected]27d537382010-06-10 23:55:02189#define MAYBE_Test FLAKY_Test
190#else
191#define MAYBE_Test Test
192#endif
193IN_PROC_BROWSER_TEST_F(ExtensionsLoadTest, MAYBE_Test) {
[email protected]919ddc82009-07-15 04:30:12194 WaitForServicesToStart(1, false);
195 TestInjection(true, true);
[email protected]f0488f2f2009-07-01 05:25:22196}