OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/file_util.h" | 5 #include "base/file_util.h" |
6 #include "base/json/json_file_value_serializer.h" | 6 #include "base/json/json_file_value_serializer.h" |
7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
8 #include "base/threading/thread.h" | 8 #include "base/threading/thread.h" |
9 #include "chrome/browser/extensions/extension_service_unittest.h" | 9 #include "chrome/browser/extensions/extension_service_unittest.h" |
10 #include "chrome/browser/extensions/unpacked_installer.h" | 10 #include "chrome/browser/extensions/unpacked_installer.h" |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 "google.com"); | 136 "google.com"); |
137 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler("http", | 137 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler("http", |
138 "example.com"); | 138 "example.com"); |
139 } | 139 } |
140 | 140 |
141 virtual void SetUp() OVERRIDE { | 141 virtual void SetUp() OVERRIDE { |
142 ExtensionServiceTestBase::SetUp(); | 142 ExtensionServiceTestBase::SetUp(); |
143 | 143 |
144 InitializeEmptyExtensionService(); | 144 InitializeEmptyExtensionService(); |
145 service_->Init(); | 145 service_->Init(); |
146 MessageLoop::current()->RunUntilIdle(); | 146 base::MessageLoop::current()->RunUntilIdle(); |
147 | 147 |
148 listener_ = new UserScriptListener(); | 148 listener_ = new UserScriptListener(); |
149 } | 149 } |
150 | 150 |
151 virtual void TearDown() OVERRIDE { | 151 virtual void TearDown() OVERRIDE { |
152 listener_ = NULL; | 152 listener_ = NULL; |
153 MessageLoop::current()->RunUntilIdle(); | 153 base::MessageLoop::current()->RunUntilIdle(); |
154 ExtensionServiceTestBase::TearDown(); | 154 ExtensionServiceTestBase::TearDown(); |
155 } | 155 } |
156 | 156 |
157 protected: | 157 protected: |
158 net::TestURLRequest* StartTestRequest(net::URLRequest::Delegate* delegate, | 158 net::TestURLRequest* StartTestRequest(net::URLRequest::Delegate* delegate, |
159 const std::string& url_string, | 159 const std::string& url_string, |
160 net::TestURLRequestContext* context) { | 160 net::TestURLRequestContext* context) { |
161 GURL url(url_string); | 161 GURL url(url_string); |
162 net::TestURLRequest* request = | 162 net::TestURLRequest* request = |
163 new net::TestURLRequest(url, delegate, context, NULL); | 163 new net::TestURLRequest(url, delegate, context, NULL); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 extension_misc::UNLOAD_REASON_DISABLE); | 196 extension_misc::UNLOAD_REASON_DISABLE); |
197 } | 197 } |
198 | 198 |
199 scoped_refptr<UserScriptListener> listener_; | 199 scoped_refptr<UserScriptListener> listener_; |
200 }; | 200 }; |
201 | 201 |
202 namespace { | 202 namespace { |
203 | 203 |
204 TEST_F(UserScriptListenerTest, DelayAndUpdate) { | 204 TEST_F(UserScriptListenerTest, DelayAndUpdate) { |
205 LoadTestExtension(); | 205 LoadTestExtension(); |
206 MessageLoop::current()->RunUntilIdle(); | 206 base::MessageLoop::current()->RunUntilIdle(); |
207 | 207 |
208 net::TestDelegate delegate; | 208 net::TestDelegate delegate; |
209 net::TestURLRequestContext context; | 209 net::TestURLRequestContext context; |
210 scoped_ptr<net::TestURLRequest> request( | 210 scoped_ptr<net::TestURLRequest> request( |
211 StartTestRequest(&delegate, kMatchingUrl, &context)); | 211 StartTestRequest(&delegate, kMatchingUrl, &context)); |
212 ASSERT_FALSE(request->is_pending()); | 212 ASSERT_FALSE(request->is_pending()); |
213 | 213 |
214 content::NotificationService::current()->Notify( | 214 content::NotificationService::current()->Notify( |
215 chrome::NOTIFICATION_USER_SCRIPTS_UPDATED, | 215 chrome::NOTIFICATION_USER_SCRIPTS_UPDATED, |
216 content::Source<Profile>(profile_.get()), | 216 content::Source<Profile>(profile_.get()), |
217 content::NotificationService::NoDetails()); | 217 content::NotificationService::NoDetails()); |
218 MessageLoop::current()->RunUntilIdle(); | 218 base::MessageLoop::current()->RunUntilIdle(); |
219 EXPECT_EQ(kTestData, delegate.data_received()); | 219 EXPECT_EQ(kTestData, delegate.data_received()); |
220 } | 220 } |
221 | 221 |
222 TEST_F(UserScriptListenerTest, DelayAndUnload) { | 222 TEST_F(UserScriptListenerTest, DelayAndUnload) { |
223 LoadTestExtension(); | 223 LoadTestExtension(); |
224 MessageLoop::current()->RunUntilIdle(); | 224 base::MessageLoop::current()->RunUntilIdle(); |
225 | 225 |
226 net::TestDelegate delegate; | 226 net::TestDelegate delegate; |
227 net::TestURLRequestContext context; | 227 net::TestURLRequestContext context; |
228 scoped_ptr<net::TestURLRequest> request( | 228 scoped_ptr<net::TestURLRequest> request( |
229 StartTestRequest(&delegate, kMatchingUrl, &context)); | 229 StartTestRequest(&delegate, kMatchingUrl, &context)); |
230 ASSERT_FALSE(request->is_pending()); | 230 ASSERT_FALSE(request->is_pending()); |
231 | 231 |
232 UnloadTestExtension(); | 232 UnloadTestExtension(); |
233 MessageLoop::current()->RunUntilIdle(); | 233 base::MessageLoop::current()->RunUntilIdle(); |
234 | 234 |
235 // This is still not enough to start delayed requests. We have to notify the | 235 // This is still not enough to start delayed requests. We have to notify the |
236 // listener that the user scripts have been updated. | 236 // listener that the user scripts have been updated. |
237 ASSERT_FALSE(request->is_pending()); | 237 ASSERT_FALSE(request->is_pending()); |
238 | 238 |
239 content::NotificationService::current()->Notify( | 239 content::NotificationService::current()->Notify( |
240 chrome::NOTIFICATION_USER_SCRIPTS_UPDATED, | 240 chrome::NOTIFICATION_USER_SCRIPTS_UPDATED, |
241 content::Source<Profile>(profile_.get()), | 241 content::Source<Profile>(profile_.get()), |
242 content::NotificationService::NoDetails()); | 242 content::NotificationService::NoDetails()); |
243 MessageLoop::current()->RunUntilIdle(); | 243 base::MessageLoop::current()->RunUntilIdle(); |
244 EXPECT_EQ(kTestData, delegate.data_received()); | 244 EXPECT_EQ(kTestData, delegate.data_received()); |
245 } | 245 } |
246 | 246 |
247 TEST_F(UserScriptListenerTest, NoDelayNoExtension) { | 247 TEST_F(UserScriptListenerTest, NoDelayNoExtension) { |
248 net::TestDelegate delegate; | 248 net::TestDelegate delegate; |
249 net::TestURLRequestContext context; | 249 net::TestURLRequestContext context; |
250 scoped_ptr<net::TestURLRequest> request( | 250 scoped_ptr<net::TestURLRequest> request( |
251 StartTestRequest(&delegate, kMatchingUrl, &context)); | 251 StartTestRequest(&delegate, kMatchingUrl, &context)); |
252 | 252 |
253 // The request should be started immediately. | 253 // The request should be started immediately. |
254 ASSERT_TRUE(request->is_pending()); | 254 ASSERT_TRUE(request->is_pending()); |
255 | 255 |
256 MessageLoop::current()->RunUntilIdle(); | 256 base::MessageLoop::current()->RunUntilIdle(); |
257 EXPECT_EQ(kTestData, delegate.data_received()); | 257 EXPECT_EQ(kTestData, delegate.data_received()); |
258 } | 258 } |
259 | 259 |
260 TEST_F(UserScriptListenerTest, NoDelayNotMatching) { | 260 TEST_F(UserScriptListenerTest, NoDelayNotMatching) { |
261 LoadTestExtension(); | 261 LoadTestExtension(); |
262 MessageLoop::current()->RunUntilIdle(); | 262 base::MessageLoop::current()->RunUntilIdle(); |
263 | 263 |
264 net::TestDelegate delegate; | 264 net::TestDelegate delegate; |
265 net::TestURLRequestContext context; | 265 net::TestURLRequestContext context; |
266 scoped_ptr<net::TestURLRequest> request(StartTestRequest(&delegate, | 266 scoped_ptr<net::TestURLRequest> request(StartTestRequest(&delegate, |
267 kNotMatchingUrl, | 267 kNotMatchingUrl, |
268 &context)); | 268 &context)); |
269 | 269 |
270 // The request should be started immediately. | 270 // The request should be started immediately. |
271 ASSERT_TRUE(request->is_pending()); | 271 ASSERT_TRUE(request->is_pending()); |
272 | 272 |
273 MessageLoop::current()->RunUntilIdle(); | 273 base::MessageLoop::current()->RunUntilIdle(); |
274 EXPECT_EQ(kTestData, delegate.data_received()); | 274 EXPECT_EQ(kTestData, delegate.data_received()); |
275 } | 275 } |
276 | 276 |
277 TEST_F(UserScriptListenerTest, MultiProfile) { | 277 TEST_F(UserScriptListenerTest, MultiProfile) { |
278 LoadTestExtension(); | 278 LoadTestExtension(); |
279 MessageLoop::current()->RunUntilIdle(); | 279 base::MessageLoop::current()->RunUntilIdle(); |
280 | 280 |
281 // Fire up a second profile and have it load and extension with a content | 281 // Fire up a second profile and have it load and extension with a content |
282 // script. | 282 // script. |
283 TestingProfile profile2; | 283 TestingProfile profile2; |
284 std::string error; | 284 std::string error; |
285 scoped_refptr<Extension> extension = LoadExtension( | 285 scoped_refptr<Extension> extension = LoadExtension( |
286 "content_script_yahoo.json", &error); | 286 "content_script_yahoo.json", &error); |
287 ASSERT_TRUE(extension.get()); | 287 ASSERT_TRUE(extension.get()); |
288 | 288 |
289 content::NotificationService::current()->Notify( | 289 content::NotificationService::current()->Notify( |
290 chrome::NOTIFICATION_EXTENSION_LOADED, | 290 chrome::NOTIFICATION_EXTENSION_LOADED, |
291 content::Source<Profile>(&profile2), | 291 content::Source<Profile>(&profile2), |
292 content::Details<Extension>(extension.get())); | 292 content::Details<Extension>(extension.get())); |
293 | 293 |
294 net::TestDelegate delegate; | 294 net::TestDelegate delegate; |
295 net::TestURLRequestContext context; | 295 net::TestURLRequestContext context; |
296 scoped_ptr<net::TestURLRequest> request( | 296 scoped_ptr<net::TestURLRequest> request( |
297 StartTestRequest(&delegate, kMatchingUrl, &context)); | 297 StartTestRequest(&delegate, kMatchingUrl, &context)); |
298 ASSERT_FALSE(request->is_pending()); | 298 ASSERT_FALSE(request->is_pending()); |
299 | 299 |
300 // When the first profile's user scripts are ready, the request should still | 300 // When the first profile's user scripts are ready, the request should still |
301 // be blocked waiting for profile2. | 301 // be blocked waiting for profile2. |
302 content::NotificationService::current()->Notify( | 302 content::NotificationService::current()->Notify( |
303 chrome::NOTIFICATION_USER_SCRIPTS_UPDATED, | 303 chrome::NOTIFICATION_USER_SCRIPTS_UPDATED, |
304 content::Source<Profile>(profile_.get()), | 304 content::Source<Profile>(profile_.get()), |
305 content::NotificationService::NoDetails()); | 305 content::NotificationService::NoDetails()); |
306 MessageLoop::current()->RunUntilIdle(); | 306 base::MessageLoop::current()->RunUntilIdle(); |
307 ASSERT_FALSE(request->is_pending()); | 307 ASSERT_FALSE(request->is_pending()); |
308 EXPECT_TRUE(delegate.data_received().empty()); | 308 EXPECT_TRUE(delegate.data_received().empty()); |
309 | 309 |
310 // After profile2 is ready, the request should proceed. | 310 // After profile2 is ready, the request should proceed. |
311 content::NotificationService::current()->Notify( | 311 content::NotificationService::current()->Notify( |
312 chrome::NOTIFICATION_USER_SCRIPTS_UPDATED, | 312 chrome::NOTIFICATION_USER_SCRIPTS_UPDATED, |
313 content::Source<Profile>(&profile2), | 313 content::Source<Profile>(&profile2), |
314 content::NotificationService::NoDetails()); | 314 content::NotificationService::NoDetails()); |
315 MessageLoop::current()->RunUntilIdle(); | 315 base::MessageLoop::current()->RunUntilIdle(); |
316 EXPECT_EQ(kTestData, delegate.data_received()); | 316 EXPECT_EQ(kTestData, delegate.data_received()); |
317 } | 317 } |
318 | 318 |
319 // Test when the script updated notification occurs before the throttle's | 319 // Test when the script updated notification occurs before the throttle's |
320 // WillStartRequest function is called. This can occur when there are multiple | 320 // WillStartRequest function is called. This can occur when there are multiple |
321 // throttles. | 321 // throttles. |
322 TEST_F(UserScriptListenerTest, ResumeBeforeStart) { | 322 TEST_F(UserScriptListenerTest, ResumeBeforeStart) { |
323 LoadTestExtension(); | 323 LoadTestExtension(); |
324 MessageLoop::current()->RunUntilIdle(); | 324 base::MessageLoop::current()->RunUntilIdle(); |
325 net::TestDelegate delegate; | 325 net::TestDelegate delegate; |
326 net::TestURLRequestContext context; | 326 net::TestURLRequestContext context; |
327 GURL url(kMatchingUrl); | 327 GURL url(kMatchingUrl); |
328 scoped_ptr<net::TestURLRequest> request( | 328 scoped_ptr<net::TestURLRequest> request( |
329 new net::TestURLRequest(url, &delegate, &context, NULL)); | 329 new net::TestURLRequest(url, &delegate, &context, NULL)); |
330 | 330 |
331 ResourceThrottle* throttle = | 331 ResourceThrottle* throttle = |
332 listener_->CreateResourceThrottle(url, ResourceType::MAIN_FRAME); | 332 listener_->CreateResourceThrottle(url, ResourceType::MAIN_FRAME); |
333 ASSERT_TRUE(throttle); | 333 ASSERT_TRUE(throttle); |
334 request->SetUserData(NULL, new ThrottleController(request.get(), throttle)); | 334 request->SetUserData(NULL, new ThrottleController(request.get(), throttle)); |
335 | 335 |
336 ASSERT_FALSE(request->is_pending()); | 336 ASSERT_FALSE(request->is_pending()); |
337 | 337 |
338 content::NotificationService::current()->Notify( | 338 content::NotificationService::current()->Notify( |
339 chrome::NOTIFICATION_USER_SCRIPTS_UPDATED, | 339 chrome::NOTIFICATION_USER_SCRIPTS_UPDATED, |
340 content::Source<Profile>(profile_.get()), | 340 content::Source<Profile>(profile_.get()), |
341 content::NotificationService::NoDetails()); | 341 content::NotificationService::NoDetails()); |
342 MessageLoop::current()->RunUntilIdle(); | 342 base::MessageLoop::current()->RunUntilIdle(); |
343 | 343 |
344 bool defer = false; | 344 bool defer = false; |
345 throttle->WillStartRequest(&defer); | 345 throttle->WillStartRequest(&defer); |
346 ASSERT_FALSE(defer); | 346 ASSERT_FALSE(defer); |
347 } | 347 } |
348 | 348 |
349 } // namespace | 349 } // namespace |
350 | 350 |
351 } // namespace extensions | 351 } // namespace extensions |
OLD | NEW |