OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/bind.h" | 5 #include "base/bind.h" |
6 #include "base/callback.h" | 6 #include "base/callback.h" |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "content/browser/service_worker/embedded_worker_instance.h" | 9 #include "content/browser/service_worker/embedded_worker_instance.h" |
10 #include "content/browser/service_worker/embedded_worker_registry.h" | 10 #include "content/browser/service_worker/embedded_worker_registry.h" |
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 status = SERVICE_WORKER_ERROR_FAILED; | 252 status = SERVICE_WORKER_ERROR_FAILED; |
253 base::RunLoop stop_run_loop; | 253 base::RunLoop stop_run_loop; |
254 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 254 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
255 base::Bind(&self::StopOnIOThread, this, | 255 base::Bind(&self::StopOnIOThread, this, |
256 stop_run_loop.QuitClosure(), | 256 stop_run_loop.QuitClosure(), |
257 &status)); | 257 &status)); |
258 stop_run_loop.Run(); | 258 stop_run_loop.Run(); |
259 ASSERT_EQ(SERVICE_WORKER_OK, status); | 259 ASSERT_EQ(SERVICE_WORKER_OK, status); |
260 } | 260 } |
261 | 261 |
262 void FetchTestHelper(const std::string& worker_url, | 262 void FetchOnRegisteredWorker(ServiceWorkerFetchEventResult* result, |
263 ServiceWorkerFetchEventResult* result, | 263 ServiceWorkerResponse* response) { |
264 ServiceWorkerResponse* response) { | |
265 RunOnIOThread( | |
266 base::Bind(&self::SetUpRegistrationOnIOThread, this, worker_url)); | |
267 | |
268 FetchResult fetch_result; | 264 FetchResult fetch_result; |
269 fetch_result.status = SERVICE_WORKER_ERROR_FAILED; | 265 fetch_result.status = SERVICE_WORKER_ERROR_FAILED; |
270 base::RunLoop fetch_run_loop; | 266 base::RunLoop fetch_run_loop; |
271 BrowserThread::PostTask(BrowserThread::IO, | 267 BrowserThread::PostTask(BrowserThread::IO, |
272 FROM_HERE, | 268 FROM_HERE, |
273 base::Bind(&self::FetchOnIOThread, | 269 base::Bind(&self::FetchOnIOThread, |
274 this, | 270 this, |
275 fetch_run_loop.QuitClosure(), | 271 fetch_run_loop.QuitClosure(), |
276 &fetch_result)); | 272 &fetch_result)); |
277 fetch_run_loop.Run(); | 273 fetch_run_loop.Run(); |
278 *result = fetch_result.result; | 274 *result = fetch_result.result; |
279 *response = fetch_result.response; | 275 *response = fetch_result.response; |
280 ASSERT_EQ(SERVICE_WORKER_OK, fetch_result.status); | 276 ASSERT_EQ(SERVICE_WORKER_OK, fetch_result.status); |
281 } | 277 } |
282 | 278 |
| 279 void FetchTestHelper(const std::string& worker_url, |
| 280 ServiceWorkerFetchEventResult* result, |
| 281 ServiceWorkerResponse* response) { |
| 282 RunOnIOThread( |
| 283 base::Bind(&self::SetUpRegistrationOnIOThread, this, worker_url)); |
| 284 |
| 285 FetchOnRegisteredWorker(result, response); |
| 286 } |
| 287 |
283 void SetUpRegistrationOnIOThread(const std::string& worker_url) { | 288 void SetUpRegistrationOnIOThread(const std::string& worker_url) { |
284 const int64 version_id = 1L; | 289 const int64 version_id = 1L; |
285 registration_ = new ServiceWorkerRegistration( | 290 registration_ = new ServiceWorkerRegistration( |
286 embedded_test_server()->GetURL("/*"), | 291 embedded_test_server()->GetURL("/*"), |
287 embedded_test_server()->GetURL(worker_url), | 292 embedded_test_server()->GetURL(worker_url), |
288 next_registration_id_++); | 293 next_registration_id_++); |
289 version_ = new ServiceWorkerVersion( | 294 version_ = new ServiceWorkerVersion( |
290 registration_, | 295 registration_, |
291 wrapper()->context()->embedded_worker_registry(), | 296 wrapper()->context()->embedded_worker_registry(), |
292 version_id); | 297 version_id); |
(...skipping 23 matching lines...) Expand all Loading... |
316 version_->DispatchFetchEvent( | 321 version_->DispatchFetchEvent( |
317 request, CreateResponseReceiver(BrowserThread::UI, done, result)); | 322 request, CreateResponseReceiver(BrowserThread::UI, done, result)); |
318 } | 323 } |
319 | 324 |
320 void StopOnIOThread(const base::Closure& done, | 325 void StopOnIOThread(const base::Closure& done, |
321 ServiceWorkerStatusCode* result) { | 326 ServiceWorkerStatusCode* result) { |
322 ASSERT_TRUE(version_); | 327 ASSERT_TRUE(version_); |
323 version_->StopWorker(CreateReceiver(BrowserThread::UI, done, result)); | 328 version_->StopWorker(CreateReceiver(BrowserThread::UI, done, result)); |
324 } | 329 } |
325 | 330 |
| 331 void SyncEventOnIOThread(const base::Closure& done, |
| 332 ServiceWorkerStatusCode* result) { |
| 333 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 334 version_->DispatchSyncEvent( |
| 335 CreateReceiver(BrowserThread::UI, done, result)); |
| 336 } |
| 337 |
326 protected: | 338 protected: |
327 int64 next_registration_id_; | 339 int64 next_registration_id_; |
328 scoped_refptr<ServiceWorkerRegistration> registration_; | 340 scoped_refptr<ServiceWorkerRegistration> registration_; |
329 scoped_refptr<ServiceWorkerVersion> version_; | 341 scoped_refptr<ServiceWorkerVersion> version_; |
330 }; | 342 }; |
331 | 343 |
332 IN_PROC_BROWSER_TEST_F(EmbeddedWorkerBrowserTest, StartAndStop) { | 344 IN_PROC_BROWSER_TEST_F(EmbeddedWorkerBrowserTest, StartAndStop) { |
333 // Start a worker and wait until OnStarted() is called. | 345 // Start a worker and wait until OnStarted() is called. |
334 base::RunLoop start_run_loop; | 346 base::RunLoop start_run_loop; |
335 done_closure_ = start_run_loop.QuitClosure(); | 347 done_closure_ = start_run_loop.QuitClosure(); |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
426 ASSERT_EQ(SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, result); | 438 ASSERT_EQ(SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, result); |
427 } | 439 } |
428 | 440 |
429 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, FetchEvent_Rejected) { | 441 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, FetchEvent_Rejected) { |
430 ServiceWorkerFetchEventResult result; | 442 ServiceWorkerFetchEventResult result; |
431 ServiceWorkerResponse response; | 443 ServiceWorkerResponse response; |
432 FetchTestHelper("/service_worker/fetch_event_error.js", &result, &response); | 444 FetchTestHelper("/service_worker/fetch_event_error.js", &result, &response); |
433 ASSERT_EQ(SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, result); | 445 ASSERT_EQ(SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, result); |
434 } | 446 } |
435 | 447 |
| 448 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, SyncEventHandled) { |
| 449 RunOnIOThread(base::Bind(&self::SetUpRegistrationOnIOThread, this, |
| 450 "/service_worker/sync.js")); |
| 451 ServiceWorkerFetchEventResult result; |
| 452 ServiceWorkerResponse response; |
| 453 |
| 454 // Should 404 before sync event. |
| 455 FetchOnRegisteredWorker(&result, &response); |
| 456 EXPECT_EQ(404, response.status_code); |
| 457 |
| 458 // Run the sync event. |
| 459 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; |
| 460 base::RunLoop sync_run_loop; |
| 461 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 462 base::Bind(&self::SyncEventOnIOThread, this, |
| 463 sync_run_loop.QuitClosure(), |
| 464 &status)); |
| 465 sync_run_loop.Run(); |
| 466 ASSERT_EQ(SERVICE_WORKER_OK, status); |
| 467 |
| 468 // Should 200 after sync event. |
| 469 FetchOnRegisteredWorker(&result, &response); |
| 470 EXPECT_EQ(200, response.status_code); |
| 471 } |
| 472 |
436 class ServiceWorkerBlackBoxBrowserTest : public ServiceWorkerBrowserTest { | 473 class ServiceWorkerBlackBoxBrowserTest : public ServiceWorkerBrowserTest { |
437 public: | 474 public: |
438 typedef ServiceWorkerBlackBoxBrowserTest self; | 475 typedef ServiceWorkerBlackBoxBrowserTest self; |
439 | 476 |
440 static void ExpectResultAndRun(bool expected, | 477 static void ExpectResultAndRun(bool expected, |
441 const base::Closure& continuation, | 478 const base::Closure& continuation, |
442 bool actual) { | 479 bool actual) { |
443 EXPECT_EQ(expected, actual); | 480 EXPECT_EQ(expected, actual); |
444 continuation.Run(); | 481 continuation.Run(); |
445 } | 482 } |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
519 base::Bind(&ServiceWorkerBlackBoxBrowserTest::FindRegistrationOnIO, | 556 base::Bind(&ServiceWorkerBlackBoxBrowserTest::FindRegistrationOnIO, |
520 this, | 557 this, |
521 embedded_test_server()->GetURL("/service_worker/empty.html"), | 558 embedded_test_server()->GetURL("/service_worker/empty.html"), |
522 &status, | 559 &status, |
523 &script_url)); | 560 &script_url)); |
524 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, status); | 561 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, status); |
525 } | 562 } |
526 } | 563 } |
527 | 564 |
528 } // namespace content | 565 } // namespace content |
OLD | NEW |