OLD | NEW |
---|---|
1 #include <vector> | 1 #include <vector> |
2 | 2 |
3 #include "base/command_line.h" | 3 #include "base/command_line.h" |
4 #include "base/file_path.h" | 4 #include "base/file_path.h" |
5 #include "base/file_util.h" | 5 #include "base/file_util.h" |
6 #include "base/path_service.h" | 6 #include "base/path_service.h" |
7 #include "chrome/browser/browser.h" | 7 #include "chrome/browser/browser.h" |
8 #include "chrome/browser/extensions/extensions_service.h" | 8 #include "chrome/browser/extensions/extensions_service.h" |
9 #include "chrome/browser/profile.h" | 9 #include "chrome/browser/profile.h" |
10 #include "chrome/browser/tab_contents/tab_contents.h" | 10 #include "chrome/browser/tab_contents/tab_contents.h" |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
64 PathService::Get(chrome::DIR_TEST_DATA, &src_dir); | 64 PathService::Get(chrome::DIR_TEST_DATA, &src_dir); |
65 src_dir = src_dir.AppendASCII("extensions").AppendASCII("good") | 65 src_dir = src_dir.AppendASCII("extensions").AppendASCII("good") |
66 .AppendASCII("Extensions") | 66 .AppendASCII("Extensions") |
67 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj") | 67 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj") |
68 .AppendASCII("1.0.0.0"); | 68 .AppendASCII("1.0.0.0"); |
69 | 69 |
70 file_util::CreateDirectory(user_scripts_dir_); | 70 file_util::CreateDirectory(user_scripts_dir_); |
71 file_util::CopyFile(src_dir.AppendASCII("script2.js"), | 71 file_util::CopyFile(src_dir.AppendASCII("script2.js"), |
72 user_scripts_dir_.AppendASCII("script2.user.js")); | 72 user_scripts_dir_.AppendASCII("script2.user.js")); |
73 } | 73 } |
74 | |
75 if (!load_extension_.value().empty()) { | |
76 command_line->AppendSwitchWithValue(switches::kLoadExtension, | |
77 load_extension_.ToWStringHack()); | |
78 } | |
74 } | 79 } |
75 | 80 |
76 // NotificationObserver | 81 // NotificationObserver |
77 virtual void Observe(NotificationType type, | 82 virtual void Observe(NotificationType type, |
78 const NotificationSource& source, | 83 const NotificationSource& source, |
79 const NotificationDetails& details) { | 84 const NotificationDetails& details) { |
80 switch (type.value) { | 85 switch (type.value) { |
81 case NotificationType::EXTENSIONS_READY: | 86 case NotificationType::EXTENSIONS_READY: |
82 case NotificationType::USER_SCRIPTS_UPDATED: | 87 case NotificationType::USER_SCRIPTS_UPDATED: |
83 MessageLoopForUI::current()->Quit(); | 88 MessageLoopForUI::current()->Quit(); |
84 break; | 89 break; |
85 } | 90 } |
86 } | 91 } |
87 | 92 |
88 virtual void TearDown() { | 93 virtual void TearDown() { |
89 file_util::Delete(preferences_file_, false); | 94 file_util::Delete(preferences_file_, false); |
90 file_util::Delete(user_scripts_dir_, true); | 95 file_util::Delete(user_scripts_dir_, true); |
91 file_util::Delete(extensions_dir_, true); | 96 file_util::Delete(extensions_dir_, true); |
92 } | 97 } |
93 | 98 |
99 void WaitForServicesToStart(int num_expected_extensions, | |
Erik does not do reviews
2009/07/14 20:22:38
seems like it would be useful to have a num_expect
Aaron Boodman
2009/07/14 20:35:44
It would be good. We can't do this easily now beca
| |
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 | |
94 FilePath preferences_file_; | 152 FilePath preferences_file_; |
95 FilePath extensions_dir_; | 153 FilePath extensions_dir_; |
96 FilePath user_scripts_dir_; | 154 FilePath user_scripts_dir_; |
97 bool enable_extensions_; | 155 bool enable_extensions_; |
98 bool enable_user_scripts_; | 156 bool enable_user_scripts_; |
157 FilePath load_extension_; | |
99 NotificationRegistrar registrar_; | 158 NotificationRegistrar registrar_; |
100 }; | 159 }; |
101 | 160 |
102 | 161 |
103 // ExtensionsStartupTest | 162 // ExtensionsStartupTest |
104 // Ensures that we can startup the browser with --enable-extensions and some | 163 // Ensures that we can startup the browser with --enable-extensions and some |
105 // extensions installed and see them run and do basic things. | 164 // extensions installed and see them run and do basic things. |
106 | 165 |
107 class ExtensionsStartupTest : public ExtensionStartupTestBase { | 166 class ExtensionsStartupTest : public ExtensionStartupTestBase { |
108 public: | 167 public: |
109 ExtensionsStartupTest() { | 168 ExtensionsStartupTest() { |
110 enable_extensions_ = true; | 169 enable_extensions_ = true; |
111 } | 170 } |
112 }; | 171 }; |
113 | 172 |
114 IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest, Test) { | 173 IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest, Test) { |
115 ExtensionsService* service = browser()->profile()->GetExtensionsService(); | 174 WaitForServicesToStart(3, true); |
Erik does not do reviews
2009/07/14 20:22:38
we've got multiple tests that depend on the number
Aaron Boodman
2009/07/14 20:35:44
There are two places that I see: ExtensionsService
| |
116 if (!service->is_ready()) { | 175 TestInjection(true, true); |
117 registrar_.Add(this, NotificationType::EXTENSIONS_READY, | 176 } |
118 NotificationService::AllSources()); | 177 |
119 ui_test_utils::RunMessageLoop(); | 178 |
120 registrar_.Remove(this, NotificationType::EXTENSIONS_READY, | 179 // ExtensionsLoadTest |
121 NotificationService::AllSources()); | 180 // Ensures that we can startup the browser with --load-extension and see them |
181 // run. | |
182 | |
183 class 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"); | |
122 } | 193 } |
123 ASSERT_EQ(3u, service->extensions()->size()); | 194 }; |
124 ASSERT_TRUE(service->extensions_enabled()); | |
125 | 195 |
126 UserScriptMaster* master = browser()->profile()->GetUserScriptMaster(); | 196 IN_PROC_BROWSER_TEST_F(ExtensionsLoadTest, Test) { |
127 if (!master->ScriptsReady()) { | 197 WaitForServicesToStart(1, false); |
128 // Wait for UserScriptMaster to finish its scan. | 198 TestInjection(true, true); |
129 registrar_.Add(this, NotificationType::USER_SCRIPTS_UPDATED, | |
130 NotificationService::AllSources()); | |
131 ui_test_utils::RunMessageLoop(); | |
132 registrar_.Remove(this, NotificationType::USER_SCRIPTS_UPDATED, | |
133 NotificationService::AllSources()); | |
134 } | |
135 ASSERT_TRUE(master->ScriptsReady()); | |
136 | |
137 FilePath test_file; | |
138 PathService::Get(chrome::DIR_TEST_DATA, &test_file); | |
139 test_file = test_file.AppendASCII("extensions") | |
140 .AppendASCII("test_file.html"); | |
141 | |
142 // Now we should be able to load a page affected by the content script and see | |
143 // the effect. | |
144 ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(test_file)); | |
145 | |
146 // Test that the content script ran. | |
147 bool result = false; | |
148 ui_test_utils::ExecuteJavaScriptAndExtractBool( | |
149 browser()->GetSelectedTabContents()->render_view_host(), L"", | |
150 L"window.domAutomationController.send(" | |
151 L"document.defaultView.getComputedStyle(document.body, null)." | |
152 L"getPropertyValue('background-color') == 'rgb(245, 245, 220)')", | |
153 &result); | |
154 EXPECT_TRUE(result); | |
155 | |
156 result = false; | |
157 ui_test_utils::ExecuteJavaScriptAndExtractBool( | |
158 browser()->GetSelectedTabContents()->render_view_host(), L"", | |
159 L"window.domAutomationController.send(document.title == 'Modified')", | |
160 &result); | |
161 EXPECT_TRUE(result); | |
162 } | 199 } |
163 | 200 |
164 | 201 |
165 // ExtensionsStartupUserScriptTest | 202 // ExtensionsStartupUserScriptTest |
166 // Tests that we can startup with --enable-user-scripts and run user sripts and | 203 // Tests that we can startup with --enable-user-scripts and run user scripts and |
167 // see them do basic things. | 204 // see them do basic things. |
168 | 205 |
169 class ExtensionsStartupUserScriptTest : public ExtensionStartupTestBase { | 206 class ExtensionsStartupUserScriptTest : public ExtensionStartupTestBase { |
170 public: | 207 public: |
171 ExtensionsStartupUserScriptTest() { | 208 ExtensionsStartupUserScriptTest() { |
172 enable_user_scripts_ = true; | 209 enable_user_scripts_ = true; |
173 } | 210 } |
174 }; | 211 }; |
175 | 212 |
176 IN_PROC_BROWSER_TEST_F(ExtensionsStartupUserScriptTest, Test) { | 213 IN_PROC_BROWSER_TEST_F(ExtensionsStartupUserScriptTest, Test) { |
177 UserScriptMaster* master = browser()->profile()->GetUserScriptMaster(); | 214 WaitForServicesToStart(0, false); |
178 if (!master->ScriptsReady()) { | 215 TestInjection(false, true); |
179 // Wait for UserScriptMaster to finish its scan. | 216 } |
180 registrar_.Add(this, NotificationType::USER_SCRIPTS_UPDATED, | |
181 NotificationService::AllSources()); | |
182 ui_test_utils::RunMessageLoop(); | |
183 registrar_.Remove(this, NotificationType::USER_SCRIPTS_UPDATED, | |
184 NotificationService::AllSources()); | |
185 } | |
186 ASSERT_TRUE(master->ScriptsReady()); | |
187 | 217 |
188 FilePath test_file; | 218 // Ensure we don't inject into chrome:// URLs |
189 PathService::Get(chrome::DIR_TEST_DATA, &test_file); | 219 IN_PROC_BROWSER_TEST_F(ExtensionsStartupUserScriptTest, NoInjectIntoChrome) { |
190 test_file = test_file.AppendASCII("extensions") | 220 WaitForServicesToStart(0, false); |
191 .AppendASCII("test_file.html"); | |
192 | 221 |
193 // Now we should be able to load a page affected by the content script and see | 222 ui_test_utils::NavigateToURL(browser(), GURL("chrome://newtab")); |
194 // the effect. | |
195 ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(test_file)); | |
196 | 223 |
197 // Test that the user script ran. | |
198 bool result = false; | 224 bool result = false; |
199 ui_test_utils::ExecuteJavaScriptAndExtractBool( | 225 ui_test_utils::ExecuteJavaScriptAndExtractBool( |
200 browser()->GetSelectedTabContents()->render_view_host(), L"", | 226 browser()->GetSelectedTabContents()->render_view_host(), L"", |
201 L"window.domAutomationController.send(document.title == 'Modified')", | 227 L"window.domAutomationController.send(document.title == 'Modified')", |
202 &result); | 228 &result); |
203 EXPECT_TRUE(result); | 229 EXPECT_FALSE(result); |
204 } | 230 } |
OLD | NEW |