blob: 0efb161f03dcc217ef8885496f1dcade2296a7ae [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_) {
[email protected]f0488f2f2009-07-01 05:25:2248 FilePath src_dir;
49 PathService::Get(chrome::DIR_TEST_DATA, &src_dir);
50 src_dir = src_dir.AppendASCII("extensions").AppendASCII("good");
51
52 file_util::CopyFile(src_dir.AppendASCII("Preferences"),
53 preferences_file_);
54 file_util::CopyDirectory(src_dir.AppendASCII("Extensions"),
55 profile_dir, true); // recursive
[email protected]6d60703b2009-08-29 01:29:2356 } else {
57 command_line->AppendSwitch(switches::kDisableExtensions);
[email protected]f0488f2f2009-07-01 05:25:2258 }
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;
[email protected]041cc772009-08-28 04:09:5690 default:
91 NOTREACHED();
[email protected]f0488f2f2009-07-01 05:25:2292 }
93 }
94
95 virtual void TearDown() {
96 file_util::Delete(preferences_file_, false);
97 file_util::Delete(user_scripts_dir_, true);
98 file_util::Delete(extensions_dir_, true);
99 }
100
[email protected]919ddc82009-07-15 04:30:12101 void WaitForServicesToStart(int num_expected_extensions,
102 bool expect_extensions_enabled) {
103 ExtensionsService* service = browser()->profile()->GetExtensionsService();
104 if (!service->is_ready()) {
105 registrar_.Add(this, NotificationType::EXTENSIONS_READY,
106 NotificationService::AllSources());
107 ui_test_utils::RunMessageLoop();
108 registrar_.Remove(this, NotificationType::EXTENSIONS_READY,
109 NotificationService::AllSources());
110 }
111
112 ASSERT_EQ(static_cast<uint32>(num_expected_extensions),
113 service->extensions()->size());
114 ASSERT_EQ(expect_extensions_enabled, service->extensions_enabled());
115
116 UserScriptMaster* master = browser()->profile()->GetUserScriptMaster();
117 if (!master->ScriptsReady()) {
118 // Wait for UserScriptMaster to finish its scan.
119 registrar_.Add(this, NotificationType::USER_SCRIPTS_UPDATED,
120 NotificationService::AllSources());
121 ui_test_utils::RunMessageLoop();
122 registrar_.Remove(this, NotificationType::USER_SCRIPTS_UPDATED,
123 NotificationService::AllSources());
124 }
125 ASSERT_TRUE(master->ScriptsReady());
126 }
127
128 void TestInjection(bool expect_css, bool expect_script) {
129 // Load a page affected by the content script and test to see the effect.
130 FilePath test_file;
131 PathService::Get(chrome::DIR_TEST_DATA, &test_file);
132 test_file = test_file.AppendASCII("extensions")
133 .AppendASCII("test_file.html");
134
135 ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(test_file));
136
137 bool result = false;
138 ui_test_utils::ExecuteJavaScriptAndExtractBool(
139 browser()->GetSelectedTabContents()->render_view_host(), L"",
140 L"window.domAutomationController.send("
141 L"document.defaultView.getComputedStyle(document.body, null)."
142 L"getPropertyValue('background-color') == 'rgb(245, 245, 220)')",
143 &result);
144 EXPECT_EQ(expect_css, result);
145
146 result = false;
147 ui_test_utils::ExecuteJavaScriptAndExtractBool(
148 browser()->GetSelectedTabContents()->render_view_host(), L"",
149 L"window.domAutomationController.send(document.title == 'Modified')",
150 &result);
151 EXPECT_EQ(expect_script, result);
152 }
153
[email protected]f0488f2f2009-07-01 05:25:22154 FilePath preferences_file_;
155 FilePath extensions_dir_;
156 FilePath user_scripts_dir_;
157 bool enable_extensions_;
158 bool enable_user_scripts_;
[email protected]919ddc82009-07-15 04:30:12159 FilePath load_extension_;
[email protected]f0488f2f2009-07-01 05:25:22160 NotificationRegistrar registrar_;
161};
162
163
164// ExtensionsStartupTest
165// Ensures that we can startup the browser with --enable-extensions and some
166// extensions installed and see them run and do basic things.
167
168class ExtensionsStartupTest : public ExtensionStartupTestBase {
169 public:
170 ExtensionsStartupTest() {
171 enable_extensions_ = true;
172 }
173};
174
[email protected]041cc772009-08-28 04:09:56175#if defined(OS_WIN)
[email protected]f0488f2f2009-07-01 05:25:22176IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest, Test) {
[email protected]919ddc82009-07-15 04:30:12177 WaitForServicesToStart(3, true);
178 TestInjection(true, true);
179}
[email protected]041cc772009-08-28 04:09:56180#endif // defined(OS_WIN)
[email protected]919ddc82009-07-15 04:30:12181
182// ExtensionsLoadTest
183// Ensures that we can startup the browser with --load-extension and see them
184// run.
185
186class ExtensionsLoadTest : public ExtensionStartupTestBase {
187 public:
188 ExtensionsLoadTest() {
189 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]919ddc82009-07-15 04:30:12199IN_PROC_BROWSER_TEST_F(ExtensionsLoadTest, Test) {
200 WaitForServicesToStart(1, false);
201 TestInjection(true, true);
[email protected]f0488f2f2009-07-01 05:25:22202}
203
204
205// ExtensionsStartupUserScriptTest
[email protected]919ddc82009-07-15 04:30:12206// Tests that we can startup with --enable-user-scripts and run user scripts and
[email protected]f0488f2f2009-07-01 05:25:22207// see them do basic things.
208
209class ExtensionsStartupUserScriptTest : public ExtensionStartupTestBase {
210 public:
211 ExtensionsStartupUserScriptTest() {
212 enable_user_scripts_ = true;
213 }
214};
215
216IN_PROC_BROWSER_TEST_F(ExtensionsStartupUserScriptTest, Test) {
[email protected]919ddc82009-07-15 04:30:12217 WaitForServicesToStart(0, false);
218 TestInjection(false, true);
219}
[email protected]f0488f2f2009-07-01 05:25:22220
[email protected]919ddc82009-07-15 04:30:12221// Ensure we don't inject into chrome:// URLs
222IN_PROC_BROWSER_TEST_F(ExtensionsStartupUserScriptTest, NoInjectIntoChrome) {
223 WaitForServicesToStart(0, false);
[email protected]f0488f2f2009-07-01 05:25:22224
[email protected]919ddc82009-07-15 04:30:12225 ui_test_utils::NavigateToURL(browser(), GURL("chrome://newtab"));
[email protected]f0488f2f2009-07-01 05:25:22226
[email protected]f0488f2f2009-07-01 05:25:22227 bool result = false;
228 ui_test_utils::ExecuteJavaScriptAndExtractBool(
[email protected]17c4f3c2009-07-04 16:36:25229 browser()->GetSelectedTabContents()->render_view_host(), L"",
[email protected]f0488f2f2009-07-01 05:25:22230 L"window.domAutomationController.send(document.title == 'Modified')",
231 &result);
[email protected]919ddc82009-07-15 04:30:12232 EXPECT_FALSE(result);
[email protected]f0488f2f2009-07-01 05:25:22233}