OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/files/scoped_temp_dir.h" | 5 #include "base/files/scoped_temp_dir.h" |
6 #include "base/logging.h" | 6 #include "base/logging.h" |
7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
8 #include "content/browser/browser_thread_impl.h" | 8 #include "content/browser/browser_thread_impl.h" |
9 #include "content/browser/service_worker/service_worker_job_coordinator.h" | 9 #include "content/browser/service_worker/service_worker_job_coordinator.h" |
10 #include "content/browser/service_worker/service_worker_registration.h" | 10 #include "content/browser/service_worker/service_worker_registration.h" |
(...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
361 SaveFoundRegistration( | 361 SaveFoundRegistration( |
362 true, REGISTRATION_OK, &called, &new_registration_by_pattern)); | 362 true, REGISTRATION_OK, &called, &new_registration_by_pattern)); |
363 | 363 |
364 ASSERT_FALSE(called); | 364 ASSERT_FALSE(called); |
365 base::RunLoop().RunUntilIdle(); | 365 base::RunLoop().RunUntilIdle(); |
366 ASSERT_TRUE(called); | 366 ASSERT_TRUE(called); |
367 | 367 |
368 ASSERT_EQ(new_registration, old_registration); | 368 ASSERT_EQ(new_registration, old_registration); |
369 } | 369 } |
370 | 370 |
| 371 // Register and then unregister the pattern, in parallel. Job coordinator should |
| 372 // process jobs until the last job. |
| 373 TEST_F(ServiceWorkerJobTest, ParallelRegUnreg) { |
| 374 GURL pattern("http://www.example.com/*"); |
| 375 GURL script_url("http://www.example.com/service_worker.js"); |
| 376 |
| 377 bool registration_called = false; |
| 378 scoped_refptr<ServiceWorkerRegistration> registration; |
| 379 job_coordinator_->Register( |
| 380 pattern, |
| 381 script_url, |
| 382 SaveRegistration(REGISTRATION_OK, ®istration_called, ®istration)); |
| 383 |
| 384 bool unregistration_called = false; |
| 385 job_coordinator_->Unregister( |
| 386 pattern, SaveUnregistration(REGISTRATION_OK, &unregistration_called)); |
| 387 |
| 388 ASSERT_FALSE(registration_called); |
| 389 ASSERT_FALSE(unregistration_called); |
| 390 base::RunLoop().RunUntilIdle(); |
| 391 ASSERT_TRUE(registration_called); |
| 392 ASSERT_TRUE(unregistration_called); |
| 393 |
| 394 ASSERT_TRUE(registration->is_shutdown()); |
| 395 |
| 396 bool find_called = false; |
| 397 storage_->FindRegistrationForPattern( |
| 398 pattern, |
| 399 SaveFoundRegistration( |
| 400 false, REGISTRATION_OK, &find_called, ®istration)); |
| 401 |
| 402 base::RunLoop().RunUntilIdle(); |
| 403 |
| 404 ASSERT_EQ(scoped_refptr<ServiceWorkerRegistration>(NULL), registration); |
| 405 } |
| 406 |
| 407 // Register conflicting scripts for the same pattern. The most recent |
| 408 // registration should win, and the old registration should have been |
| 409 // shutdown. |
| 410 TEST_F(ServiceWorkerJobTest, ParallelRegNewScript) { |
| 411 GURL pattern("http://www.example.com/*"); |
| 412 |
| 413 GURL script_url1("http://www.example.com/service_worker1.js"); |
| 414 bool registration1_called = false; |
| 415 scoped_refptr<ServiceWorkerRegistration> registration1; |
| 416 job_coordinator_->Register( |
| 417 pattern, |
| 418 script_url1, |
| 419 SaveRegistration(REGISTRATION_OK, ®istration1_called, ®istration1)); |
| 420 |
| 421 GURL script_url2("http://www.example.com/service_worker2.js"); |
| 422 bool registration2_called = false; |
| 423 scoped_refptr<ServiceWorkerRegistration> registration2; |
| 424 job_coordinator_->Register( |
| 425 pattern, |
| 426 script_url2, |
| 427 SaveRegistration(REGISTRATION_OK, ®istration2_called, ®istration2)); |
| 428 |
| 429 ASSERT_FALSE(registration1_called); |
| 430 ASSERT_FALSE(registration2_called); |
| 431 base::RunLoop().RunUntilIdle(); |
| 432 ASSERT_TRUE(registration1_called); |
| 433 ASSERT_TRUE(registration2_called); |
| 434 |
| 435 scoped_refptr<ServiceWorkerRegistration> registration; |
| 436 bool find_called = false; |
| 437 storage_->FindRegistrationForPattern( |
| 438 pattern, |
| 439 SaveFoundRegistration( |
| 440 true, REGISTRATION_OK, &find_called, ®istration)); |
| 441 |
| 442 base::RunLoop().RunUntilIdle(); |
| 443 |
| 444 EXPECT_TRUE(registration1->is_shutdown()); |
| 445 EXPECT_FALSE(registration2->is_shutdown()); |
| 446 ASSERT_EQ(registration2, registration); |
| 447 } |
| 448 |
| 449 // Register the exact same pattern + script. Requests should be |
| 450 // coalesced such that both callers get the exact same registration |
| 451 // object. |
| 452 TEST_F(ServiceWorkerJobTest, ParallelRegSameScript) { |
| 453 GURL pattern("http://www.example.com/*"); |
| 454 |
| 455 GURL script_url("http://www.example.com/service_worker1.js"); |
| 456 bool registration1_called = false; |
| 457 scoped_refptr<ServiceWorkerRegistration> registration1; |
| 458 job_coordinator_->Register( |
| 459 pattern, |
| 460 script_url, |
| 461 SaveRegistration(REGISTRATION_OK, ®istration1_called, ®istration1)); |
| 462 |
| 463 bool registration2_called = false; |
| 464 scoped_refptr<ServiceWorkerRegistration> registration2; |
| 465 job_coordinator_->Register( |
| 466 pattern, |
| 467 script_url, |
| 468 SaveRegistration(REGISTRATION_OK, ®istration2_called, ®istration2)); |
| 469 |
| 470 ASSERT_FALSE(registration1_called); |
| 471 ASSERT_FALSE(registration2_called); |
| 472 base::RunLoop().RunUntilIdle(); |
| 473 ASSERT_TRUE(registration1_called); |
| 474 ASSERT_TRUE(registration2_called); |
| 475 |
| 476 ASSERT_EQ(registration1, registration2); |
| 477 |
| 478 scoped_refptr<ServiceWorkerRegistration> registration; |
| 479 bool find_called = false; |
| 480 storage_->FindRegistrationForPattern( |
| 481 pattern, |
| 482 SaveFoundRegistration( |
| 483 true, REGISTRATION_OK, &find_called, ®istration)); |
| 484 |
| 485 base::RunLoop().RunUntilIdle(); |
| 486 ASSERT_EQ(registration, registration1); |
| 487 } |
| 488 |
371 } // namespace content | 489 } // namespace content |
OLD | NEW |