Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(179)

Side by Side Diff: content/browser/service_worker/service_worker_job_unittest.cc

Issue 2771823002: Implement updateViaCache flag and no-cache by default for main service worker scripts
Patch Set: change useCache to updateViaCache Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 std::vector<ServiceWorkerRemoteProviderEndpoint> remote_endpoints_; 176 std::vector<ServiceWorkerRemoteProviderEndpoint> remote_endpoints_;
177 }; 177 };
178 178
179 scoped_refptr<ServiceWorkerRegistration> ServiceWorkerJobTest::RunRegisterJob( 179 scoped_refptr<ServiceWorkerRegistration> ServiceWorkerJobTest::RunRegisterJob(
180 const GURL& pattern, 180 const GURL& pattern,
181 const GURL& script_url, 181 const GURL& script_url,
182 ServiceWorkerStatusCode expected_status) { 182 ServiceWorkerStatusCode expected_status) {
183 scoped_refptr<ServiceWorkerRegistration> registration; 183 scoped_refptr<ServiceWorkerRegistration> registration;
184 bool called; 184 bool called;
185 job_coordinator()->Register( 185 job_coordinator()->Register(
186 pattern, script_url, NULL, 186 script_url, ServiceWorkerRegistrationOptions(pattern), NULL,
187 SaveRegistration(expected_status, &called, &registration)); 187 SaveRegistration(expected_status, &called, &registration));
188 EXPECT_FALSE(called); 188 EXPECT_FALSE(called);
189 base::RunLoop().RunUntilIdle(); 189 base::RunLoop().RunUntilIdle();
190 EXPECT_TRUE(called); 190 EXPECT_TRUE(called);
191 return registration; 191 return registration;
192 } 192 }
193 193
194 void ServiceWorkerJobTest::RunUnregisterJob( 194 void ServiceWorkerJobTest::RunUnregisterJob(
195 const GURL& pattern, 195 const GURL& pattern,
196 ServiceWorkerStatusCode expected_status) { 196 ServiceWorkerStatusCode expected_status) {
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 base::RunLoop().RunUntilIdle(); 270 base::RunLoop().RunUntilIdle();
271 EXPECT_TRUE(called); 271 EXPECT_TRUE(called);
272 ASSERT_EQ(registration1, original_registration); 272 ASSERT_EQ(registration1, original_registration);
273 ASSERT_EQ(registration1, registration2); 273 ASSERT_EQ(registration1, registration2);
274 } 274 }
275 275
276 TEST_F(ServiceWorkerJobTest, DifferentMatchDifferentRegistration) { 276 TEST_F(ServiceWorkerJobTest, DifferentMatchDifferentRegistration) {
277 bool called1; 277 bool called1;
278 scoped_refptr<ServiceWorkerRegistration> original_registration1; 278 scoped_refptr<ServiceWorkerRegistration> original_registration1;
279 job_coordinator()->Register( 279 job_coordinator()->Register(
280 GURL("http://www.example.com/one/"),
281 GURL("http://www.example.com/service_worker.js"), 280 GURL("http://www.example.com/service_worker.js"),
281 ServiceWorkerRegistrationOptions(GURL("http://www.example.com/one/")),
282 NULL, 282 NULL,
283 SaveRegistration(SERVICE_WORKER_OK, &called1, &original_registration1)); 283 SaveRegistration(SERVICE_WORKER_OK, &called1, &original_registration1));
284 284
285 bool called2; 285 bool called2;
286 scoped_refptr<ServiceWorkerRegistration> original_registration2; 286 scoped_refptr<ServiceWorkerRegistration> original_registration2;
287 job_coordinator()->Register( 287 job_coordinator()->Register(
288 GURL("http://www.example.com/two/"),
289 GURL("http://www.example.com/service_worker.js"), 288 GURL("http://www.example.com/service_worker.js"),
289 ServiceWorkerRegistrationOptions(GURL("http://www.example.com/two/")),
290 NULL, 290 NULL,
291 SaveRegistration(SERVICE_WORKER_OK, &called2, &original_registration2)); 291 SaveRegistration(SERVICE_WORKER_OK, &called2, &original_registration2));
292 292
293 EXPECT_FALSE(called1); 293 EXPECT_FALSE(called1);
294 EXPECT_FALSE(called2); 294 EXPECT_FALSE(called2);
295 base::RunLoop().RunUntilIdle(); 295 base::RunLoop().RunUntilIdle();
296 EXPECT_TRUE(called2); 296 EXPECT_TRUE(called2);
297 EXPECT_TRUE(called1); 297 EXPECT_TRUE(called1);
298 298
299 scoped_refptr<ServiceWorkerRegistration> registration1; 299 scoped_refptr<ServiceWorkerRegistration> registration1;
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
461 461
462 // Register and then unregister the pattern, in parallel. Job coordinator should 462 // Register and then unregister the pattern, in parallel. Job coordinator should
463 // process jobs until the last job. 463 // process jobs until the last job.
464 TEST_F(ServiceWorkerJobTest, ParallelRegUnreg) { 464 TEST_F(ServiceWorkerJobTest, ParallelRegUnreg) {
465 GURL pattern("http://www.example.com/"); 465 GURL pattern("http://www.example.com/");
466 GURL script_url("http://www.example.com/service_worker.js"); 466 GURL script_url("http://www.example.com/service_worker.js");
467 467
468 bool registration_called = false; 468 bool registration_called = false;
469 scoped_refptr<ServiceWorkerRegistration> registration; 469 scoped_refptr<ServiceWorkerRegistration> registration;
470 job_coordinator()->Register( 470 job_coordinator()->Register(
471 pattern, 471 script_url, ServiceWorkerRegistrationOptions(pattern), NULL,
472 script_url,
473 NULL,
474 SaveRegistration(SERVICE_WORKER_OK, &registration_called, &registration)); 472 SaveRegistration(SERVICE_WORKER_OK, &registration_called, &registration));
475 473
476 bool unregistration_called = false; 474 bool unregistration_called = false;
477 job_coordinator()->Unregister( 475 job_coordinator()->Unregister(
478 pattern, 476 pattern,
479 SaveUnregistration(SERVICE_WORKER_OK, &unregistration_called)); 477 SaveUnregistration(SERVICE_WORKER_OK, &unregistration_called));
480 478
481 ASSERT_FALSE(registration_called); 479 ASSERT_FALSE(registration_called);
482 ASSERT_FALSE(unregistration_called); 480 ASSERT_FALSE(unregistration_called);
483 base::RunLoop().RunUntilIdle(); 481 base::RunLoop().RunUntilIdle();
484 ASSERT_TRUE(registration_called); 482 ASSERT_TRUE(registration_called);
485 ASSERT_TRUE(unregistration_called); 483 ASSERT_TRUE(unregistration_called);
486 484
487 registration = FindRegistrationForPattern(pattern, 485 registration = FindRegistrationForPattern(pattern,
488 SERVICE_WORKER_ERROR_NOT_FOUND); 486 SERVICE_WORKER_ERROR_NOT_FOUND);
489 487
490 ASSERT_EQ(scoped_refptr<ServiceWorkerRegistration>(), registration); 488 ASSERT_EQ(scoped_refptr<ServiceWorkerRegistration>(), registration);
491 } 489 }
492 490
493 // Register conflicting scripts for the same pattern. The most recent 491 // Register conflicting scripts for the same pattern. The most recent
494 // registration should win, and the old registration should have been 492 // registration should win, and the old registration should have been
495 // shutdown. 493 // shutdown.
496 TEST_F(ServiceWorkerJobTest, ParallelRegNewScript) { 494 TEST_F(ServiceWorkerJobTest, ParallelRegNewScript) {
497 GURL pattern("http://www.example.com/"); 495 GURL pattern("http://www.example.com/");
498 496
499 GURL script_url1("http://www.example.com/service_worker1.js"); 497 GURL script_url1("http://www.example.com/service_worker1.js");
500 bool registration1_called = false; 498 bool registration1_called = false;
501 scoped_refptr<ServiceWorkerRegistration> registration1; 499 scoped_refptr<ServiceWorkerRegistration> registration1;
502 job_coordinator()->Register( 500 job_coordinator()->Register(
503 pattern, 501 script_url1, ServiceWorkerRegistrationOptions(pattern), NULL,
504 script_url1, 502 SaveRegistration(SERVICE_WORKER_OK, &registration1_called,
505 NULL, 503 &registration1));
506 SaveRegistration(
507 SERVICE_WORKER_OK, &registration1_called, &registration1));
508 504
509 GURL script_url2("http://www.example.com/service_worker2.js"); 505 GURL script_url2("http://www.example.com/service_worker2.js");
510 bool registration2_called = false; 506 bool registration2_called = false;
511 scoped_refptr<ServiceWorkerRegistration> registration2; 507 scoped_refptr<ServiceWorkerRegistration> registration2;
512 job_coordinator()->Register( 508 job_coordinator()->Register(
513 pattern, 509 script_url2, ServiceWorkerRegistrationOptions(pattern), NULL,
514 script_url2, 510 SaveRegistration(SERVICE_WORKER_OK, &registration2_called,
515 NULL, 511 &registration2));
516 SaveRegistration(
517 SERVICE_WORKER_OK, &registration2_called, &registration2));
518 512
519 ASSERT_FALSE(registration1_called); 513 ASSERT_FALSE(registration1_called);
520 ASSERT_FALSE(registration2_called); 514 ASSERT_FALSE(registration2_called);
521 base::RunLoop().RunUntilIdle(); 515 base::RunLoop().RunUntilIdle();
522 ASSERT_TRUE(registration1_called); 516 ASSERT_TRUE(registration1_called);
523 ASSERT_TRUE(registration2_called); 517 ASSERT_TRUE(registration2_called);
524 518
525 scoped_refptr<ServiceWorkerRegistration> registration = 519 scoped_refptr<ServiceWorkerRegistration> registration =
526 FindRegistrationForPattern(pattern); 520 FindRegistrationForPattern(pattern);
527 521
528 ASSERT_EQ(registration2, registration); 522 ASSERT_EQ(registration2, registration);
529 } 523 }
530 524
531 // Register the exact same pattern + script. Requests should be 525 // Register the exact same pattern + script. Requests should be
532 // coalesced such that both callers get the exact same registration 526 // coalesced such that both callers get the exact same registration
533 // object. 527 // object.
534 TEST_F(ServiceWorkerJobTest, ParallelRegSameScript) { 528 TEST_F(ServiceWorkerJobTest, ParallelRegSameScript) {
535 GURL pattern("http://www.example.com/"); 529 GURL pattern("http://www.example.com/");
536 530
537 GURL script_url("http://www.example.com/service_worker1.js"); 531 GURL script_url("http://www.example.com/service_worker1.js");
538 bool registration1_called = false; 532 bool registration1_called = false;
539 scoped_refptr<ServiceWorkerRegistration> registration1; 533 scoped_refptr<ServiceWorkerRegistration> registration1;
540 job_coordinator()->Register( 534 job_coordinator()->Register(
541 pattern, 535 script_url, ServiceWorkerRegistrationOptions(pattern), NULL,
542 script_url, 536 SaveRegistration(SERVICE_WORKER_OK, &registration1_called,
543 NULL, 537 &registration1));
544 SaveRegistration(
545 SERVICE_WORKER_OK, &registration1_called, &registration1));
546 538
547 bool registration2_called = false; 539 bool registration2_called = false;
548 scoped_refptr<ServiceWorkerRegistration> registration2; 540 scoped_refptr<ServiceWorkerRegistration> registration2;
549 job_coordinator()->Register( 541 job_coordinator()->Register(
550 pattern, 542 script_url, ServiceWorkerRegistrationOptions(pattern), NULL,
551 script_url, 543 SaveRegistration(SERVICE_WORKER_OK, &registration2_called,
552 NULL, 544 &registration2));
553 SaveRegistration(
554 SERVICE_WORKER_OK, &registration2_called, &registration2));
555 545
556 ASSERT_FALSE(registration1_called); 546 ASSERT_FALSE(registration1_called);
557 ASSERT_FALSE(registration2_called); 547 ASSERT_FALSE(registration2_called);
558 base::RunLoop().RunUntilIdle(); 548 base::RunLoop().RunUntilIdle();
559 ASSERT_TRUE(registration1_called); 549 ASSERT_TRUE(registration1_called);
560 ASSERT_TRUE(registration2_called); 550 ASSERT_TRUE(registration2_called);
561 551
562 ASSERT_EQ(registration1, registration2); 552 ASSERT_EQ(registration1, registration2);
563 553
564 scoped_refptr<ServiceWorkerRegistration> registration = 554 scoped_refptr<ServiceWorkerRegistration> registration =
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
601 591
602 TEST_F(ServiceWorkerJobTest, AbortAll_Register) { 592 TEST_F(ServiceWorkerJobTest, AbortAll_Register) {
603 GURL pattern1("http://www1.example.com/"); 593 GURL pattern1("http://www1.example.com/");
604 GURL pattern2("http://www2.example.com/"); 594 GURL pattern2("http://www2.example.com/");
605 GURL script_url1("http://www1.example.com/service_worker.js"); 595 GURL script_url1("http://www1.example.com/service_worker.js");
606 GURL script_url2("http://www2.example.com/service_worker.js"); 596 GURL script_url2("http://www2.example.com/service_worker.js");
607 597
608 bool registration_called1 = false; 598 bool registration_called1 = false;
609 scoped_refptr<ServiceWorkerRegistration> registration1; 599 scoped_refptr<ServiceWorkerRegistration> registration1;
610 job_coordinator()->Register( 600 job_coordinator()->Register(
611 pattern1, 601 script_url1, ServiceWorkerRegistrationOptions(pattern1), NULL,
612 script_url1, 602 SaveRegistration(SERVICE_WORKER_ERROR_ABORT, &registration_called1,
613 NULL, 603 &registration1));
614 SaveRegistration(SERVICE_WORKER_ERROR_ABORT,
615 &registration_called1, &registration1));
616 604
617 bool registration_called2 = false; 605 bool registration_called2 = false;
618 scoped_refptr<ServiceWorkerRegistration> registration2; 606 scoped_refptr<ServiceWorkerRegistration> registration2;
619 job_coordinator()->Register( 607 job_coordinator()->Register(
620 pattern2, 608 script_url2, ServiceWorkerRegistrationOptions(pattern2), NULL,
621 script_url2, 609 SaveRegistration(SERVICE_WORKER_ERROR_ABORT, &registration_called2,
622 NULL, 610 &registration2));
623 SaveRegistration(SERVICE_WORKER_ERROR_ABORT,
624 &registration_called2, &registration2));
625 611
626 ASSERT_FALSE(registration_called1); 612 ASSERT_FALSE(registration_called1);
627 ASSERT_FALSE(registration_called2); 613 ASSERT_FALSE(registration_called2);
628 job_coordinator()->AbortAll(); 614 job_coordinator()->AbortAll();
629 615
630 base::RunLoop().RunUntilIdle(); 616 base::RunLoop().RunUntilIdle();
631 ASSERT_TRUE(registration_called1); 617 ASSERT_TRUE(registration_called1);
632 ASSERT_TRUE(registration_called2); 618 ASSERT_TRUE(registration_called2);
633 619
634 bool find_called1 = false; 620 bool find_called1 = false;
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
676 ASSERT_TRUE(unregistration_called2); 662 ASSERT_TRUE(unregistration_called2);
677 } 663 }
678 664
679 TEST_F(ServiceWorkerJobTest, AbortAll_RegUnreg) { 665 TEST_F(ServiceWorkerJobTest, AbortAll_RegUnreg) {
680 GURL pattern("http://www.example.com/"); 666 GURL pattern("http://www.example.com/");
681 GURL script_url("http://www.example.com/service_worker.js"); 667 GURL script_url("http://www.example.com/service_worker.js");
682 668
683 bool registration_called = false; 669 bool registration_called = false;
684 scoped_refptr<ServiceWorkerRegistration> registration; 670 scoped_refptr<ServiceWorkerRegistration> registration;
685 job_coordinator()->Register( 671 job_coordinator()->Register(
686 pattern, 672 script_url, ServiceWorkerRegistrationOptions(pattern), NULL,
687 script_url, 673 SaveRegistration(SERVICE_WORKER_ERROR_ABORT, &registration_called,
688 NULL, 674 &registration));
689 SaveRegistration(SERVICE_WORKER_ERROR_ABORT,
690 &registration_called, &registration));
691 675
692 bool unregistration_called = false; 676 bool unregistration_called = false;
693 job_coordinator()->Unregister( 677 job_coordinator()->Unregister(
694 pattern, 678 pattern,
695 SaveUnregistration(SERVICE_WORKER_ERROR_ABORT, 679 SaveUnregistration(SERVICE_WORKER_ERROR_ABORT,
696 &unregistration_called)); 680 &unregistration_called));
697 681
698 ASSERT_FALSE(registration_called); 682 ASSERT_FALSE(registration_called);
699 ASSERT_FALSE(unregistration_called); 683 ASSERT_FALSE(unregistration_called);
700 job_coordinator()->AbortAll(); 684 job_coordinator()->AbortAll();
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
875 859
876 void set_force_start_worker_failure(bool force_start_worker_failure) { 860 void set_force_start_worker_failure(bool force_start_worker_failure) {
877 force_start_worker_failure_ = force_start_worker_failure; 861 force_start_worker_failure_ = force_start_worker_failure;
878 } 862 }
879 863
880 scoped_refptr<ServiceWorkerRegistration> SetupInitialRegistration( 864 scoped_refptr<ServiceWorkerRegistration> SetupInitialRegistration(
881 const GURL& test_origin) { 865 const GURL& test_origin) {
882 scoped_refptr<ServiceWorkerRegistration> registration; 866 scoped_refptr<ServiceWorkerRegistration> registration;
883 bool called = false; 867 bool called = false;
884 job_coordinator()->Register( 868 job_coordinator()->Register(
885 test_origin.Resolve(kScope),
886 test_origin.Resolve(kScript), 869 test_origin.Resolve(kScript),
887 NULL, 870 ServiceWorkerRegistrationOptions(test_origin.Resolve(kScope)), NULL,
888 SaveRegistration(SERVICE_WORKER_OK, &called, &registration)); 871 SaveRegistration(SERVICE_WORKER_OK, &called, &registration));
889 base::RunLoop().RunUntilIdle(); 872 base::RunLoop().RunUntilIdle();
890 EXPECT_TRUE(called); 873 EXPECT_TRUE(called);
891 EXPECT_TRUE(registration.get()); 874 EXPECT_TRUE(registration.get());
892 EXPECT_TRUE(registration->active_version()); 875 EXPECT_TRUE(registration->active_version());
893 EXPECT_FALSE(registration->installing_version()); 876 EXPECT_FALSE(registration->installing_version());
894 EXPECT_FALSE(registration->waiting_version()); 877 EXPECT_FALSE(registration->waiting_version());
895 registration_ = registration; 878 registration_ = registration;
896 return registration; 879 return registration;
897 } 880 }
(...skipping 905 matching lines...) Expand 10 before | Expand all | Expand 10 after
1803 // should not be promoted to ACTIVATED because failure occur 1786 // should not be promoted to ACTIVATED because failure occur
1804 // during shutdown. 1787 // during shutdown.
1805 runner->RunPendingTasks(); 1788 runner->RunPendingTasks();
1806 base::RunLoop().RunUntilIdle(); 1789 base::RunLoop().RunUntilIdle();
1807 EXPECT_EQ(new_version.get(), registration->active_version()); 1790 EXPECT_EQ(new_version.get(), registration->active_version());
1808 EXPECT_EQ(ServiceWorkerVersion::ACTIVATING, new_version->status()); 1791 EXPECT_EQ(ServiceWorkerVersion::ACTIVATING, new_version->status());
1809 registration->RemoveListener(update_helper); 1792 registration->RemoveListener(update_helper);
1810 } 1793 }
1811 1794
1812 } // namespace content 1795 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698