blob: 74d40a95ec4a8db037d279cd0753d4e8c6a846f0 [file] [log] [blame]
[email protected]f0488f2f2009-07-01 05:25:221#include <vector>
2
3#include "base/command_line.h"
4#include "base/file_path.h"
5#include "base/file_util.h"
6#include "base/path_service.h"
7#include "chrome/browser/browser.h"
8#include "chrome/browser/extensions/extensions_service.h"
9#include "chrome/browser/profile.h"
[email protected]17c4f3c2009-07-04 16:36:2510#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]f0488f2f2009-07-01 05:25:2211#include "chrome/common/chrome_paths.h"
12#include "chrome/common/chrome_switches.h"
13#include "chrome/common/notification_details.h"
14#include "chrome/common/notification_observer.h"
15#include "chrome/common/notification_registrar.h"
16#include "chrome/common/notification_service.h"
17#include "chrome/common/notification_type.h"
18#include "chrome/test/in_process_browser_test.h"
19#include "chrome/test/ui_test_utils.h"
20#include "net/base/net_util.h"
21
[email protected]17c4f3c2009-07-04 16:36:2522// This file contains high-level startup tests for the extensions system. We've
23// had many silly bugs where command line flags did not get propagated correctly
24// into the services, so we didn't start correctly.
[email protected]f0488f2f2009-07-01 05:25:2225
26class ExtensionStartupTestBase
27 : public InProcessBrowserTest, public NotificationObserver {
28 public:
29 ExtensionStartupTestBase()
30 : enable_extensions_(false), enable_user_scripts_(false) {
[email protected]f0488f2f2009-07-01 05:25:2231 }
32
33 protected:
34 // InProcessBrowserTest
35 virtual void SetUpCommandLine(CommandLine* command_line) {
[email protected]17c4f3c2009-07-04 16:36:2536 EnableDOMAutomation();
37
[email protected]f0488f2f2009-07-01 05:25:2238 FilePath profile_dir;
39 PathService::Get(chrome::DIR_USER_DATA, &profile_dir);
40 profile_dir = profile_dir.AppendASCII("Default");
41 file_util::CreateDirectory(profile_dir);
42
43 preferences_file_ = profile_dir.AppendASCII("Preferences");
44 user_scripts_dir_ = profile_dir.AppendASCII("User Scripts");
45 extensions_dir_ = profile_dir.AppendASCII("Extensions");
46
47 if (enable_extensions_) {
48 command_line->AppendSwitch(switches::kEnableExtensions);
49
50 FilePath src_dir;
51 PathService::Get(chrome::DIR_TEST_DATA, &src_dir);
52 src_dir = src_dir.AppendASCII("extensions").AppendASCII("good");
53
54 file_util::CopyFile(src_dir.AppendASCII("Preferences"),
55 preferences_file_);
56 file_util::CopyDirectory(src_dir.AppendASCII("Extensions"),
57 profile_dir, true); // recursive
58 }
59
60 if (enable_user_scripts_) {
61 command_line->AppendSwitch(switches::kEnableUserScripts);
62
63 FilePath src_dir;
64 PathService::Get(chrome::DIR_TEST_DATA, &src_dir);
65 src_dir = src_dir.AppendASCII("extensions").AppendASCII("good")
66 .AppendASCII("Extensions")
67 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
68 .AppendASCII("1.0.0.0");
69
70 file_util::CreateDirectory(user_scripts_dir_);
71 file_util::CopyFile(src_dir.AppendASCII("script2.js"),
72 user_scripts_dir_.AppendASCII("script2.user.js"));
73 }
[email protected]919ddc82009-07-15 04:30:1274
75 if (!load_extension_.value().empty()) {
76 command_line->AppendSwitchWithValue(switches::kLoadExtension,
77 load_extension_.ToWStringHack());
78 }
[email protected]f0488f2f2009-07-01 05:25:2279 }
80
81 // NotificationObserver
82 virtual void Observe(NotificationType type,
83 const NotificationSource& source,
84 const NotificationDetails& details) {
85 switch (type.value) {
86 case NotificationType::EXTENSIONS_READY:
87 case NotificationType::USER_SCRIPTS_UPDATED:
88 MessageLoopForUI::current()->Quit();
89 break;
90 }
91 }
92
93 virtual void TearDown() {
94 file_util::Delete(preferences_file_, false);
95 file_util::Delete(user_scripts_dir_, true);
96 file_util::Delete(extensions_dir_, true);
97 }
98
[email protected]919ddc82009-07-15 04:30:1299 void WaitForServicesToStart(int num_expected_extensions,
100 bool expect_extensions_enabled) {
101 ExtensionsService* service = browser()->profile()->GetExtensionsService();
102 if (!service->is_ready()) {
103 registrar_.Add(this, NotificationType::EXTENSIONS_READY,
104 NotificationService::AllSources());
105 ui_test_utils::RunMessageLoop();
106 registrar_.Remove(this, NotificationType::EXTENSIONS_READY,
107 NotificationService::AllSources());
108 }
109
110 ASSERT_EQ(static_cast<uint32>(num_expected_extensions),
111 service->extensions()->size());
112 ASSERT_EQ(expect_extensions_enabled, service->extensions_enabled());
113
114 UserScriptMaster* master = browser()->profile()->GetUserScriptMaster();
115 if (!master->ScriptsReady()) {
116 // Wait for UserScriptMaster to finish its scan.
117 registrar_.Add(this, NotificationType::USER_SCRIPTS_UPDATED,
118 NotificationService::AllSources());
119 ui_test_utils::RunMessageLoop();
120 registrar_.Remove(this, NotificationType::USER_SCRIPTS_UPDATED,
121 NotificationService::AllSources());
122 }
123 ASSERT_TRUE(master->ScriptsReady());
124 }
125
126 void TestInjection(bool expect_css, bool expect_script) {
127 // Load a page affected by the content script and test to see the effect.
128 FilePath test_file;
129 PathService::Get(chrome::DIR_TEST_DATA, &test_file);
130 test_file = test_file.AppendASCII("extensions")
131 .AppendASCII("test_file.html");
132
133 ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(test_file));
134
135 bool result = false;
136 ui_test_utils::ExecuteJavaScriptAndExtractBool(
137 browser()->GetSelectedTabContents()->render_view_host(), L"",
138 L"window.domAutomationController.send("
139 L"document.defaultView.getComputedStyle(document.body, null)."
140 L"getPropertyValue('background-color') == 'rgb(245, 245, 220)')",
141 &result);
142 EXPECT_EQ(expect_css, result);
143
144 result = false;
145 ui_test_utils::ExecuteJavaScriptAndExtractBool(
146 browser()->GetSelectedTabContents()->render_view_host(), L"",
147 L"window.domAutomationController.send(document.title == 'Modified')",
148 &result);
149 EXPECT_EQ(expect_script, result);
150 }
151
[email protected]f0488f2f2009-07-01 05:25:22152 FilePath preferences_file_;
153 FilePath extensions_dir_;
154 FilePath user_scripts_dir_;
155 bool enable_extensions_;
156 bool enable_user_scripts_;
[email protected]919ddc82009-07-15 04:30:12157 FilePath load_extension_;
[email protected]f0488f2f2009-07-01 05:25:22158 NotificationRegistrar registrar_;
159};
160
161
162// ExtensionsStartupTest
163// Ensures that we can startup the browser with --enable-extensions and some
164// extensions installed and see them run and do basic things.
165
166class ExtensionsStartupTest : public ExtensionStartupTestBase {
167 public:
168 ExtensionsStartupTest() {
169 enable_extensions_ = true;
170 }
171};
172
173IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest, Test) {
[email protected]919ddc82009-07-15 04:30:12174 WaitForServicesToStart(3, true);
175 TestInjection(true, true);
176}
177
178
179// ExtensionsLoadTest
180// Ensures that we can startup the browser with --load-extension and see them
181// run.
182
183class ExtensionsLoadTest : public ExtensionStartupTestBase {
184 public:
185 ExtensionsLoadTest() {
186 PathService::Get(chrome::DIR_TEST_DATA, &load_extension_);
187 load_extension_ = load_extension_
188 .AppendASCII("extensions")
189 .AppendASCII("good")
190 .AppendASCII("Extensions")
191 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
192 .AppendASCII("1.0.0.0");
[email protected]f0488f2f2009-07-01 05:25:22193 }
[email protected]919ddc82009-07-15 04:30:12194};
[email protected]f0488f2f2009-07-01 05:25:22195
[email protected]919ddc82009-07-15 04:30:12196IN_PROC_BROWSER_TEST_F(ExtensionsLoadTest, Test) {
197 WaitForServicesToStart(1, false);
198 TestInjection(true, true);
[email protected]f0488f2f2009-07-01 05:25:22199}
200
201
202// ExtensionsStartupUserScriptTest
[email protected]919ddc82009-07-15 04:30:12203// Tests that we can startup with --enable-user-scripts and run user scripts and
[email protected]f0488f2f2009-07-01 05:25:22204// see them do basic things.
205
206class ExtensionsStartupUserScriptTest : public ExtensionStartupTestBase {
207 public:
208 ExtensionsStartupUserScriptTest() {
209 enable_user_scripts_ = true;
210 }
211};
212
213IN_PROC_BROWSER_TEST_F(ExtensionsStartupUserScriptTest, Test) {
[email protected]919ddc82009-07-15 04:30:12214 WaitForServicesToStart(0, false);
215 TestInjection(false, true);
216}
[email protected]f0488f2f2009-07-01 05:25:22217
[email protected]919ddc82009-07-15 04:30:12218// Ensure we don't inject into chrome:// URLs
219IN_PROC_BROWSER_TEST_F(ExtensionsStartupUserScriptTest, NoInjectIntoChrome) {
220 WaitForServicesToStart(0, false);
[email protected]f0488f2f2009-07-01 05:25:22221
[email protected]919ddc82009-07-15 04:30:12222 ui_test_utils::NavigateToURL(browser(), GURL("chrome://newtab"));
[email protected]f0488f2f2009-07-01 05:25:22223
[email protected]f0488f2f2009-07-01 05:25:22224 bool result = false;
225 ui_test_utils::ExecuteJavaScriptAndExtractBool(
[email protected]17c4f3c2009-07-04 16:36:25226 browser()->GetSelectedTabContents()->render_view_host(), L"",
[email protected]f0488f2f2009-07-01 05:25:22227 L"window.domAutomationController.send(document.title == 'Modified')",
228 &result);
[email protected]919ddc82009-07-15 04:30:12229 EXPECT_FALSE(result);
[email protected]f0488f2f2009-07-01 05:25:22230}