blob: 38b8823b4d1ea8ab18c8c22fc3a5b09781f7fc13 [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());
66 }
[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);
74 file_util::Delete(extensions_dir_, true));
[email protected]f0488f2f2009-07-01 05:25:2275 }
76
[email protected]919ddc82009-07-15 04:30:1277 void WaitForServicesToStart(int num_expected_extensions,
78 bool expect_extensions_enabled) {
79 ExtensionsService* service = browser()->profile()->GetExtensionsService();
[email protected]3e59bac2010-04-08 16:16:5580 if (!service->is_ready())
81 ui_test_utils::WaitForNotification(NotificationType::EXTENSIONS_READY);
82 ASSERT_TRUE(service->is_ready());
[email protected]919ddc82009-07-15 04:30:1283
84 ASSERT_EQ(static_cast<uint32>(num_expected_extensions),
85 service->extensions()->size());
86 ASSERT_EQ(expect_extensions_enabled, service->extensions_enabled());
87
88 UserScriptMaster* master = browser()->profile()->GetUserScriptMaster();
89 if (!master->ScriptsReady()) {
[email protected]3e59bac2010-04-08 16:16:5590 ui_test_utils::WaitForNotification(
91 NotificationType::USER_SCRIPTS_UPDATED);
[email protected]919ddc82009-07-15 04:30:1292 }
93 ASSERT_TRUE(master->ScriptsReady());
94 }
95
96 void TestInjection(bool expect_css, bool expect_script) {
97 // Load a page affected by the content script and test to see the effect.
[email protected]62771442009-11-22 02:25:0498 FilePath test_file;
99 PathService::Get(chrome::DIR_TEST_DATA, &test_file);
100 test_file = test_file.AppendASCII("extensions")
101 .AppendASCII("test_file.html");
102
103 ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(test_file));
[email protected]919ddc82009-07-15 04:30:12104
105 bool result = false;
106 ui_test_utils::ExecuteJavaScriptAndExtractBool(
107 browser()->GetSelectedTabContents()->render_view_host(), L"",
108 L"window.domAutomationController.send("
109 L"document.defaultView.getComputedStyle(document.body, null)."
110 L"getPropertyValue('background-color') == 'rgb(245, 245, 220)')",
111 &result);
112 EXPECT_EQ(expect_css, result);
113
114 result = false;
115 ui_test_utils::ExecuteJavaScriptAndExtractBool(
116 browser()->GetSelectedTabContents()->render_view_host(), L"",
117 L"window.domAutomationController.send(document.title == 'Modified')",
118 &result);
119 EXPECT_EQ(expect_script, result);
120 }
121
[email protected]f0488f2f2009-07-01 05:25:22122 FilePath preferences_file_;
123 FilePath extensions_dir_;
124 FilePath user_scripts_dir_;
125 bool enable_extensions_;
[email protected]919ddc82009-07-15 04:30:12126 FilePath load_extension_;
[email protected]f0488f2f2009-07-01 05:25:22127};
128
129
130// ExtensionsStartupTest
131// Ensures that we can startup the browser with --enable-extensions and some
132// extensions installed and see them run and do basic things.
133
134class ExtensionsStartupTest : public ExtensionStartupTestBase {
135 public:
136 ExtensionsStartupTest() {
137 enable_extensions_ = true;
138 }
139};
140
141IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest, Test) {
[email protected]4b776892010-03-19 22:57:33142 WaitForServicesToStart(4, true); // 1 component extension and 3 others.
[email protected]919ddc82009-07-15 04:30:12143 TestInjection(true, true);
144}
[email protected]919ddc82009-07-15 04:30:12145
146// ExtensionsLoadTest
147// Ensures that we can startup the browser with --load-extension and see them
148// run.
149
150class ExtensionsLoadTest : public ExtensionStartupTestBase {
151 public:
152 ExtensionsLoadTest() {
153 PathService::Get(chrome::DIR_TEST_DATA, &load_extension_);
154 load_extension_ = load_extension_
155 .AppendASCII("extensions")
156 .AppendASCII("good")
157 .AppendASCII("Extensions")
158 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
159 .AppendASCII("1.0.0.0");
[email protected]f0488f2f2009-07-01 05:25:22160 }
[email protected]919ddc82009-07-15 04:30:12161};
[email protected]f0488f2f2009-07-01 05:25:22162
[email protected]919ddc82009-07-15 04:30:12163IN_PROC_BROWSER_TEST_F(ExtensionsLoadTest, Test) {
164 WaitForServicesToStart(1, false);
165 TestInjection(true, true);
[email protected]f0488f2f2009-07-01 05:25:22166}