| 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 <stdint.h> | 5 #include <stdint.h> |
| 6 #include <tuple> | 6 #include <tuple> |
| 7 | 7 |
| 8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; | 155 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; |
| 156 }; | 156 }; |
| 157 | 157 |
| 158 scoped_refptr<ServiceWorkerRegistration> ServiceWorkerJobTest::RunRegisterJob( | 158 scoped_refptr<ServiceWorkerRegistration> ServiceWorkerJobTest::RunRegisterJob( |
| 159 const GURL& pattern, | 159 const GURL& pattern, |
| 160 const GURL& script_url, | 160 const GURL& script_url, |
| 161 ServiceWorkerStatusCode expected_status) { | 161 ServiceWorkerStatusCode expected_status) { |
| 162 scoped_refptr<ServiceWorkerRegistration> registration; | 162 scoped_refptr<ServiceWorkerRegistration> registration; |
| 163 bool called; | 163 bool called; |
| 164 job_coordinator()->Register( | 164 job_coordinator()->Register( |
| 165 pattern, script_url, NULL, | 165 script_url, ServiceWorkerRegistrationOptions(pattern), NULL, |
| 166 SaveRegistration(expected_status, &called, ®istration)); | 166 SaveRegistration(expected_status, &called, ®istration)); |
| 167 EXPECT_FALSE(called); | 167 EXPECT_FALSE(called); |
| 168 base::RunLoop().RunUntilIdle(); | 168 base::RunLoop().RunUntilIdle(); |
| 169 EXPECT_TRUE(called); | 169 EXPECT_TRUE(called); |
| 170 return registration; | 170 return registration; |
| 171 } | 171 } |
| 172 | 172 |
| 173 void ServiceWorkerJobTest::RunUnregisterJob( | 173 void ServiceWorkerJobTest::RunUnregisterJob( |
| 174 const GURL& pattern, | 174 const GURL& pattern, |
| 175 ServiceWorkerStatusCode expected_status) { | 175 ServiceWorkerStatusCode expected_status) { |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 base::RunLoop().RunUntilIdle(); | 247 base::RunLoop().RunUntilIdle(); |
| 248 EXPECT_TRUE(called); | 248 EXPECT_TRUE(called); |
| 249 ASSERT_EQ(registration1, original_registration); | 249 ASSERT_EQ(registration1, original_registration); |
| 250 ASSERT_EQ(registration1, registration2); | 250 ASSERT_EQ(registration1, registration2); |
| 251 } | 251 } |
| 252 | 252 |
| 253 TEST_F(ServiceWorkerJobTest, DifferentMatchDifferentRegistration) { | 253 TEST_F(ServiceWorkerJobTest, DifferentMatchDifferentRegistration) { |
| 254 bool called1; | 254 bool called1; |
| 255 scoped_refptr<ServiceWorkerRegistration> original_registration1; | 255 scoped_refptr<ServiceWorkerRegistration> original_registration1; |
| 256 job_coordinator()->Register( | 256 job_coordinator()->Register( |
| 257 GURL("http://www.example.com/one/"), | |
| 258 GURL("http://www.example.com/service_worker.js"), | 257 GURL("http://www.example.com/service_worker.js"), |
| 258 ServiceWorkerRegistrationOptions(GURL("http://www.example.com/one/")), |
| 259 NULL, | 259 NULL, |
| 260 SaveRegistration(SERVICE_WORKER_OK, &called1, &original_registration1)); | 260 SaveRegistration(SERVICE_WORKER_OK, &called1, &original_registration1)); |
| 261 | 261 |
| 262 bool called2; | 262 bool called2; |
| 263 scoped_refptr<ServiceWorkerRegistration> original_registration2; | 263 scoped_refptr<ServiceWorkerRegistration> original_registration2; |
| 264 job_coordinator()->Register( | 264 job_coordinator()->Register( |
| 265 GURL("http://www.example.com/two/"), | |
| 266 GURL("http://www.example.com/service_worker.js"), | 265 GURL("http://www.example.com/service_worker.js"), |
| 266 ServiceWorkerRegistrationOptions(GURL("http://www.example.com/two/")), |
| 267 NULL, | 267 NULL, |
| 268 SaveRegistration(SERVICE_WORKER_OK, &called2, &original_registration2)); | 268 SaveRegistration(SERVICE_WORKER_OK, &called2, &original_registration2)); |
| 269 | 269 |
| 270 EXPECT_FALSE(called1); | 270 EXPECT_FALSE(called1); |
| 271 EXPECT_FALSE(called2); | 271 EXPECT_FALSE(called2); |
| 272 base::RunLoop().RunUntilIdle(); | 272 base::RunLoop().RunUntilIdle(); |
| 273 EXPECT_TRUE(called2); | 273 EXPECT_TRUE(called2); |
| 274 EXPECT_TRUE(called1); | 274 EXPECT_TRUE(called1); |
| 275 | 275 |
| 276 scoped_refptr<ServiceWorkerRegistration> registration1; | 276 scoped_refptr<ServiceWorkerRegistration> registration1; |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 404 | 404 |
| 405 // Register and then unregister the pattern, in parallel. Job coordinator should | 405 // Register and then unregister the pattern, in parallel. Job coordinator should |
| 406 // process jobs until the last job. | 406 // process jobs until the last job. |
| 407 TEST_F(ServiceWorkerJobTest, ParallelRegUnreg) { | 407 TEST_F(ServiceWorkerJobTest, ParallelRegUnreg) { |
| 408 GURL pattern("http://www.example.com/"); | 408 GURL pattern("http://www.example.com/"); |
| 409 GURL script_url("http://www.example.com/service_worker.js"); | 409 GURL script_url("http://www.example.com/service_worker.js"); |
| 410 | 410 |
| 411 bool registration_called = false; | 411 bool registration_called = false; |
| 412 scoped_refptr<ServiceWorkerRegistration> registration; | 412 scoped_refptr<ServiceWorkerRegistration> registration; |
| 413 job_coordinator()->Register( | 413 job_coordinator()->Register( |
| 414 pattern, | 414 script_url, ServiceWorkerRegistrationOptions(pattern), NULL, |
| 415 script_url, | |
| 416 NULL, | |
| 417 SaveRegistration(SERVICE_WORKER_OK, ®istration_called, ®istration)); | 415 SaveRegistration(SERVICE_WORKER_OK, ®istration_called, ®istration)); |
| 418 | 416 |
| 419 bool unregistration_called = false; | 417 bool unregistration_called = false; |
| 420 job_coordinator()->Unregister( | 418 job_coordinator()->Unregister( |
| 421 pattern, | 419 pattern, |
| 422 SaveUnregistration(SERVICE_WORKER_OK, &unregistration_called)); | 420 SaveUnregistration(SERVICE_WORKER_OK, &unregistration_called)); |
| 423 | 421 |
| 424 ASSERT_FALSE(registration_called); | 422 ASSERT_FALSE(registration_called); |
| 425 ASSERT_FALSE(unregistration_called); | 423 ASSERT_FALSE(unregistration_called); |
| 426 base::RunLoop().RunUntilIdle(); | 424 base::RunLoop().RunUntilIdle(); |
| 427 ASSERT_TRUE(registration_called); | 425 ASSERT_TRUE(registration_called); |
| 428 ASSERT_TRUE(unregistration_called); | 426 ASSERT_TRUE(unregistration_called); |
| 429 | 427 |
| 430 registration = FindRegistrationForPattern(pattern, | 428 registration = FindRegistrationForPattern(pattern, |
| 431 SERVICE_WORKER_ERROR_NOT_FOUND); | 429 SERVICE_WORKER_ERROR_NOT_FOUND); |
| 432 | 430 |
| 433 ASSERT_EQ(scoped_refptr<ServiceWorkerRegistration>(), registration); | 431 ASSERT_EQ(scoped_refptr<ServiceWorkerRegistration>(), registration); |
| 434 } | 432 } |
| 435 | 433 |
| 436 // Register conflicting scripts for the same pattern. The most recent | 434 // Register conflicting scripts for the same pattern. The most recent |
| 437 // registration should win, and the old registration should have been | 435 // registration should win, and the old registration should have been |
| 438 // shutdown. | 436 // shutdown. |
| 439 TEST_F(ServiceWorkerJobTest, ParallelRegNewScript) { | 437 TEST_F(ServiceWorkerJobTest, ParallelRegNewScript) { |
| 440 GURL pattern("http://www.example.com/"); | 438 GURL pattern("http://www.example.com/"); |
| 441 | 439 |
| 442 GURL script_url1("http://www.example.com/service_worker1.js"); | 440 GURL script_url1("http://www.example.com/service_worker1.js"); |
| 443 bool registration1_called = false; | 441 bool registration1_called = false; |
| 444 scoped_refptr<ServiceWorkerRegistration> registration1; | 442 scoped_refptr<ServiceWorkerRegistration> registration1; |
| 445 job_coordinator()->Register( | 443 job_coordinator()->Register( |
| 446 pattern, | 444 script_url1, ServiceWorkerRegistrationOptions(pattern), NULL, |
| 447 script_url1, | 445 SaveRegistration(SERVICE_WORKER_OK, ®istration1_called, |
| 448 NULL, | 446 ®istration1)); |
| 449 SaveRegistration( | |
| 450 SERVICE_WORKER_OK, ®istration1_called, ®istration1)); | |
| 451 | 447 |
| 452 GURL script_url2("http://www.example.com/service_worker2.js"); | 448 GURL script_url2("http://www.example.com/service_worker2.js"); |
| 453 bool registration2_called = false; | 449 bool registration2_called = false; |
| 454 scoped_refptr<ServiceWorkerRegistration> registration2; | 450 scoped_refptr<ServiceWorkerRegistration> registration2; |
| 455 job_coordinator()->Register( | 451 job_coordinator()->Register( |
| 456 pattern, | 452 script_url2, ServiceWorkerRegistrationOptions(pattern), NULL, |
| 457 script_url2, | 453 SaveRegistration(SERVICE_WORKER_OK, ®istration2_called, |
| 458 NULL, | 454 ®istration2)); |
| 459 SaveRegistration( | |
| 460 SERVICE_WORKER_OK, ®istration2_called, ®istration2)); | |
| 461 | 455 |
| 462 ASSERT_FALSE(registration1_called); | 456 ASSERT_FALSE(registration1_called); |
| 463 ASSERT_FALSE(registration2_called); | 457 ASSERT_FALSE(registration2_called); |
| 464 base::RunLoop().RunUntilIdle(); | 458 base::RunLoop().RunUntilIdle(); |
| 465 ASSERT_TRUE(registration1_called); | 459 ASSERT_TRUE(registration1_called); |
| 466 ASSERT_TRUE(registration2_called); | 460 ASSERT_TRUE(registration2_called); |
| 467 | 461 |
| 468 scoped_refptr<ServiceWorkerRegistration> registration = | 462 scoped_refptr<ServiceWorkerRegistration> registration = |
| 469 FindRegistrationForPattern(pattern); | 463 FindRegistrationForPattern(pattern); |
| 470 | 464 |
| 471 ASSERT_EQ(registration2, registration); | 465 ASSERT_EQ(registration2, registration); |
| 472 } | 466 } |
| 473 | 467 |
| 474 // Register the exact same pattern + script. Requests should be | 468 // Register the exact same pattern + script. Requests should be |
| 475 // coalesced such that both callers get the exact same registration | 469 // coalesced such that both callers get the exact same registration |
| 476 // object. | 470 // object. |
| 477 TEST_F(ServiceWorkerJobTest, ParallelRegSameScript) { | 471 TEST_F(ServiceWorkerJobTest, ParallelRegSameScript) { |
| 478 GURL pattern("http://www.example.com/"); | 472 GURL pattern("http://www.example.com/"); |
| 479 | 473 |
| 480 GURL script_url("http://www.example.com/service_worker1.js"); | 474 GURL script_url("http://www.example.com/service_worker1.js"); |
| 481 bool registration1_called = false; | 475 bool registration1_called = false; |
| 482 scoped_refptr<ServiceWorkerRegistration> registration1; | 476 scoped_refptr<ServiceWorkerRegistration> registration1; |
| 483 job_coordinator()->Register( | 477 job_coordinator()->Register( |
| 484 pattern, | 478 script_url, ServiceWorkerRegistrationOptions(pattern), NULL, |
| 485 script_url, | 479 SaveRegistration(SERVICE_WORKER_OK, ®istration1_called, |
| 486 NULL, | 480 ®istration1)); |
| 487 SaveRegistration( | |
| 488 SERVICE_WORKER_OK, ®istration1_called, ®istration1)); | |
| 489 | 481 |
| 490 bool registration2_called = false; | 482 bool registration2_called = false; |
| 491 scoped_refptr<ServiceWorkerRegistration> registration2; | 483 scoped_refptr<ServiceWorkerRegistration> registration2; |
| 492 job_coordinator()->Register( | 484 job_coordinator()->Register( |
| 493 pattern, | 485 script_url, ServiceWorkerRegistrationOptions(pattern), NULL, |
| 494 script_url, | 486 SaveRegistration(SERVICE_WORKER_OK, ®istration2_called, |
| 495 NULL, | 487 ®istration2)); |
| 496 SaveRegistration( | |
| 497 SERVICE_WORKER_OK, ®istration2_called, ®istration2)); | |
| 498 | 488 |
| 499 ASSERT_FALSE(registration1_called); | 489 ASSERT_FALSE(registration1_called); |
| 500 ASSERT_FALSE(registration2_called); | 490 ASSERT_FALSE(registration2_called); |
| 501 base::RunLoop().RunUntilIdle(); | 491 base::RunLoop().RunUntilIdle(); |
| 502 ASSERT_TRUE(registration1_called); | 492 ASSERT_TRUE(registration1_called); |
| 503 ASSERT_TRUE(registration2_called); | 493 ASSERT_TRUE(registration2_called); |
| 504 | 494 |
| 505 ASSERT_EQ(registration1, registration2); | 495 ASSERT_EQ(registration1, registration2); |
| 506 | 496 |
| 507 scoped_refptr<ServiceWorkerRegistration> registration = | 497 scoped_refptr<ServiceWorkerRegistration> registration = |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 544 | 534 |
| 545 TEST_F(ServiceWorkerJobTest, AbortAll_Register) { | 535 TEST_F(ServiceWorkerJobTest, AbortAll_Register) { |
| 546 GURL pattern1("http://www1.example.com/"); | 536 GURL pattern1("http://www1.example.com/"); |
| 547 GURL pattern2("http://www2.example.com/"); | 537 GURL pattern2("http://www2.example.com/"); |
| 548 GURL script_url1("http://www1.example.com/service_worker.js"); | 538 GURL script_url1("http://www1.example.com/service_worker.js"); |
| 549 GURL script_url2("http://www2.example.com/service_worker.js"); | 539 GURL script_url2("http://www2.example.com/service_worker.js"); |
| 550 | 540 |
| 551 bool registration_called1 = false; | 541 bool registration_called1 = false; |
| 552 scoped_refptr<ServiceWorkerRegistration> registration1; | 542 scoped_refptr<ServiceWorkerRegistration> registration1; |
| 553 job_coordinator()->Register( | 543 job_coordinator()->Register( |
| 554 pattern1, | 544 script_url1, ServiceWorkerRegistrationOptions(pattern1), NULL, |
| 555 script_url1, | 545 SaveRegistration(SERVICE_WORKER_ERROR_ABORT, ®istration_called1, |
| 556 NULL, | 546 ®istration1)); |
| 557 SaveRegistration(SERVICE_WORKER_ERROR_ABORT, | |
| 558 ®istration_called1, ®istration1)); | |
| 559 | 547 |
| 560 bool registration_called2 = false; | 548 bool registration_called2 = false; |
| 561 scoped_refptr<ServiceWorkerRegistration> registration2; | 549 scoped_refptr<ServiceWorkerRegistration> registration2; |
| 562 job_coordinator()->Register( | 550 job_coordinator()->Register( |
| 563 pattern2, | 551 script_url2, ServiceWorkerRegistrationOptions(pattern2), NULL, |
| 564 script_url2, | 552 SaveRegistration(SERVICE_WORKER_ERROR_ABORT, ®istration_called2, |
| 565 NULL, | 553 ®istration2)); |
| 566 SaveRegistration(SERVICE_WORKER_ERROR_ABORT, | |
| 567 ®istration_called2, ®istration2)); | |
| 568 | 554 |
| 569 ASSERT_FALSE(registration_called1); | 555 ASSERT_FALSE(registration_called1); |
| 570 ASSERT_FALSE(registration_called2); | 556 ASSERT_FALSE(registration_called2); |
| 571 job_coordinator()->AbortAll(); | 557 job_coordinator()->AbortAll(); |
| 572 | 558 |
| 573 base::RunLoop().RunUntilIdle(); | 559 base::RunLoop().RunUntilIdle(); |
| 574 ASSERT_TRUE(registration_called1); | 560 ASSERT_TRUE(registration_called1); |
| 575 ASSERT_TRUE(registration_called2); | 561 ASSERT_TRUE(registration_called2); |
| 576 | 562 |
| 577 bool find_called1 = false; | 563 bool find_called1 = false; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 619 ASSERT_TRUE(unregistration_called2); | 605 ASSERT_TRUE(unregistration_called2); |
| 620 } | 606 } |
| 621 | 607 |
| 622 TEST_F(ServiceWorkerJobTest, AbortAll_RegUnreg) { | 608 TEST_F(ServiceWorkerJobTest, AbortAll_RegUnreg) { |
| 623 GURL pattern("http://www.example.com/"); | 609 GURL pattern("http://www.example.com/"); |
| 624 GURL script_url("http://www.example.com/service_worker.js"); | 610 GURL script_url("http://www.example.com/service_worker.js"); |
| 625 | 611 |
| 626 bool registration_called = false; | 612 bool registration_called = false; |
| 627 scoped_refptr<ServiceWorkerRegistration> registration; | 613 scoped_refptr<ServiceWorkerRegistration> registration; |
| 628 job_coordinator()->Register( | 614 job_coordinator()->Register( |
| 629 pattern, | 615 script_url, ServiceWorkerRegistrationOptions(pattern), NULL, |
| 630 script_url, | 616 SaveRegistration(SERVICE_WORKER_ERROR_ABORT, ®istration_called, |
| 631 NULL, | 617 ®istration)); |
| 632 SaveRegistration(SERVICE_WORKER_ERROR_ABORT, | |
| 633 ®istration_called, ®istration)); | |
| 634 | 618 |
| 635 bool unregistration_called = false; | 619 bool unregistration_called = false; |
| 636 job_coordinator()->Unregister( | 620 job_coordinator()->Unregister( |
| 637 pattern, | 621 pattern, |
| 638 SaveUnregistration(SERVICE_WORKER_ERROR_ABORT, | 622 SaveUnregistration(SERVICE_WORKER_ERROR_ABORT, |
| 639 &unregistration_called)); | 623 &unregistration_called)); |
| 640 | 624 |
| 641 ASSERT_FALSE(registration_called); | 625 ASSERT_FALSE(registration_called); |
| 642 ASSERT_FALSE(unregistration_called); | 626 ASSERT_FALSE(unregistration_called); |
| 643 job_coordinator()->AbortAll(); | 627 job_coordinator()->AbortAll(); |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 818 | 802 |
| 819 void set_force_start_worker_failure(bool force_start_worker_failure) { | 803 void set_force_start_worker_failure(bool force_start_worker_failure) { |
| 820 force_start_worker_failure_ = force_start_worker_failure; | 804 force_start_worker_failure_ = force_start_worker_failure; |
| 821 } | 805 } |
| 822 | 806 |
| 823 scoped_refptr<ServiceWorkerRegistration> SetupInitialRegistration( | 807 scoped_refptr<ServiceWorkerRegistration> SetupInitialRegistration( |
| 824 const GURL& test_origin) { | 808 const GURL& test_origin) { |
| 825 scoped_refptr<ServiceWorkerRegistration> registration; | 809 scoped_refptr<ServiceWorkerRegistration> registration; |
| 826 bool called = false; | 810 bool called = false; |
| 827 job_coordinator()->Register( | 811 job_coordinator()->Register( |
| 828 test_origin.Resolve(kScope), | |
| 829 test_origin.Resolve(kScript), | 812 test_origin.Resolve(kScript), |
| 830 NULL, | 813 ServiceWorkerRegistrationOptions(test_origin.Resolve(kScope)), NULL, |
| 831 SaveRegistration(SERVICE_WORKER_OK, &called, ®istration)); | 814 SaveRegistration(SERVICE_WORKER_OK, &called, ®istration)); |
| 832 base::RunLoop().RunUntilIdle(); | 815 base::RunLoop().RunUntilIdle(); |
| 833 EXPECT_TRUE(called); | 816 EXPECT_TRUE(called); |
| 834 EXPECT_TRUE(registration.get()); | 817 EXPECT_TRUE(registration.get()); |
| 835 EXPECT_TRUE(registration->active_version()); | 818 EXPECT_TRUE(registration->active_version()); |
| 836 EXPECT_FALSE(registration->installing_version()); | 819 EXPECT_FALSE(registration->installing_version()); |
| 837 EXPECT_FALSE(registration->waiting_version()); | 820 EXPECT_FALSE(registration->waiting_version()); |
| 838 registration_ = registration; | 821 registration_ = registration; |
| 839 return registration; | 822 return registration; |
| 840 } | 823 } |
| (...skipping 896 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1737 // should not be promoted to ACTIVATED because failure occur | 1720 // should not be promoted to ACTIVATED because failure occur |
| 1738 // during shutdown. | 1721 // during shutdown. |
| 1739 runner->RunUntilIdle(); | 1722 runner->RunUntilIdle(); |
| 1740 base::RunLoop().RunUntilIdle(); | 1723 base::RunLoop().RunUntilIdle(); |
| 1741 EXPECT_EQ(new_version.get(), registration->active_version()); | 1724 EXPECT_EQ(new_version.get(), registration->active_version()); |
| 1742 EXPECT_EQ(ServiceWorkerVersion::ACTIVATING, new_version->status()); | 1725 EXPECT_EQ(ServiceWorkerVersion::ACTIVATING, new_version->status()); |
| 1743 registration->RemoveListener(update_helper); | 1726 registration->RemoveListener(update_helper); |
| 1744 } | 1727 } |
| 1745 | 1728 |
| 1746 } // namespace content | 1729 } // namespace content |
| OLD | NEW |