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

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

Issue 1146913004: Service Worker: Add ServiceWorkerContainer.getRegistrations() method. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Make GetRegistrationsForOrigin() handle nullptr for resource param. Created 5 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 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 <string> 5 #include <string>
6 6
7 #include "base/files/scoped_temp_dir.h" 7 #include "base/files/scoped_temp_dir.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "base/thread_task_runner_handle.h" 10 #include "base/thread_task_runner_handle.h"
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 *found = registration; 59 *found = registration;
60 } 60 }
61 61
62 ServiceWorkerStorage::FindRegistrationCallback MakeFindCallback( 62 ServiceWorkerStorage::FindRegistrationCallback MakeFindCallback(
63 bool* was_called, 63 bool* was_called,
64 ServiceWorkerStatusCode* result, 64 ServiceWorkerStatusCode* result,
65 scoped_refptr<ServiceWorkerRegistration>* found) { 65 scoped_refptr<ServiceWorkerRegistration>* found) {
66 return base::Bind(&FindCallback, was_called, result, found); 66 return base::Bind(&FindCallback, was_called, result, found);
67 } 67 }
68 68
69 void GetAllCallback( 69 void GetAllCallback(bool* was_called,
70 std::vector<ServiceWorkerRegistration*>* all_out,
71 const std::vector<ServiceWorkerRegistration*>& all) {
72 *was_called = true;
73 *all_out = all;
74 }
75
76 void GetAllInfosCallback(
70 bool* was_called, 77 bool* was_called,
71 std::vector<ServiceWorkerRegistrationInfo>* all_out, 78 std::vector<ServiceWorkerRegistrationInfo>* all_out,
72 const std::vector<ServiceWorkerRegistrationInfo>& all) { 79 const std::vector<ServiceWorkerRegistrationInfo>& all) {
73 *was_called = true; 80 *was_called = true;
74 *all_out = all; 81 *all_out = all;
75 } 82 }
76 83
84 ServiceWorkerStorage::GetRegistrationsCallback MakeGetRegistrationsCallback(
85 bool* was_called,
86 std::vector<ServiceWorkerRegistration*>* all,
87 std::vector<std::vector<ResourceRecord>>* resource_lists) {
88 return base::Bind(&GetAllCallback, was_called, all);
89 }
90
77 ServiceWorkerStorage::GetRegistrationsInfosCallback 91 ServiceWorkerStorage::GetRegistrationsInfosCallback
78 MakeGetRegistrationsCallback(bool* was_called, 92 MakeGetRegistrationsInfosCallback(
79 std::vector<ServiceWorkerRegistrationInfo>* all) { 93 bool* was_called,
80 return base::Bind(&GetAllCallback, was_called, all); 94 std::vector<ServiceWorkerRegistrationInfo>* all) {
95 return base::Bind(&GetAllInfosCallback, was_called, all);
81 } 96 }
82 97
83 void GetUserDataCallback( 98 void GetUserDataCallback(
84 bool* was_called, 99 bool* was_called,
85 std::string* data_out, 100 std::string* data_out,
86 ServiceWorkerStatusCode* status_out, 101 ServiceWorkerStatusCode* status_out,
87 const std::string& data, 102 const std::string& data,
88 ServiceWorkerStatusCode status) { 103 ServiceWorkerStatusCode status) {
89 *was_called = true; 104 *was_called = true;
90 *data_out = data; 105 *data_out = data;
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 bool was_called = false; 337 bool was_called = false;
323 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED; 338 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED;
324 storage()->DeleteRegistration( 339 storage()->DeleteRegistration(
325 registration_id, origin, MakeStatusCallback(&was_called, &result)); 340 registration_id, origin, MakeStatusCallback(&was_called, &result));
326 EXPECT_FALSE(was_called); // always async 341 EXPECT_FALSE(was_called); // always async
327 base::RunLoop().RunUntilIdle(); 342 base::RunLoop().RunUntilIdle();
328 EXPECT_TRUE(was_called); 343 EXPECT_TRUE(was_called);
329 return result; 344 return result;
330 } 345 }
331 346
332 void GetAllRegistrations( 347 void GetAllRegistrationsInfos(
333 std::vector<ServiceWorkerRegistrationInfo>* registrations) { 348 std::vector<ServiceWorkerRegistrationInfo>* registrations) {
334 bool was_called = false; 349 bool was_called = false;
335 storage()->GetAllRegistrations( 350 storage()->GetAllRegistrationsInfos(
336 MakeGetRegistrationsCallback(&was_called, registrations)); 351 MakeGetRegistrationsInfosCallback(&was_called, registrations));
337 EXPECT_FALSE(was_called); // always async 352 EXPECT_FALSE(was_called); // always async
338 base::RunLoop().RunUntilIdle(); 353 base::RunLoop().RunUntilIdle();
339 EXPECT_TRUE(was_called); 354 EXPECT_TRUE(was_called);
340 } 355 }
341 356
342 void GetRegistrationsForOrigin( 357 void GetRegistrationsForOrigin(
343 const GURL& origin, 358 const GURL& origin,
344 std::vector<ServiceWorkerRegistrationInfo>* registrations) { 359 std::vector<ServiceWorkerRegistration*>* registrations,
360 std::vector<std::vector<ResourceRecord>>* resource_lists) {
345 bool was_called = false; 361 bool was_called = false;
346 storage()->GetRegistrationsForOrigin( 362 storage()->GetRegistrationsForOrigin(
347 origin, 363 origin, MakeGetRegistrationsCallback(&was_called, registrations,
348 MakeGetRegistrationsCallback(&was_called, registrations)); 364 resource_lists));
349 EXPECT_FALSE(was_called); // always async 365 EXPECT_FALSE(was_called); // always async
350 base::RunLoop().RunUntilIdle(); 366 base::RunLoop().RunUntilIdle();
351 EXPECT_TRUE(was_called); 367 EXPECT_TRUE(was_called);
352 } 368 }
353 369
354 ServiceWorkerStatusCode GetUserData( 370 ServiceWorkerStatusCode GetUserData(
355 int64 registration_id, 371 int64 registration_id,
356 const std::string& key, 372 const std::string& key,
357 std::string* data) { 373 std::string* data) {
358 bool was_called = false; 374 bool was_called = false;
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after
572 FindRegistrationForDocument(kDocumentUrl, &found_registration)); 588 FindRegistrationForDocument(kDocumentUrl, &found_registration));
573 ASSERT_TRUE(found_registration.get()); 589 ASSERT_TRUE(found_registration.get());
574 EXPECT_EQ(kRegistrationId, found_registration->id()); 590 EXPECT_EQ(kRegistrationId, found_registration->id());
575 EXPECT_TRUE(found_registration->HasOneRef()); 591 EXPECT_TRUE(found_registration->HasOneRef());
576 592
577 // Check that sizes are populated correctly 593 // Check that sizes are populated correctly
578 EXPECT_EQ(live_version.get(), found_registration->waiting_version()); 594 EXPECT_EQ(live_version.get(), found_registration->waiting_version());
579 EXPECT_EQ(kResource1Size + kResource2Size, 595 EXPECT_EQ(kResource1Size + kResource2Size,
580 found_registration->resources_total_size_bytes()); 596 found_registration->resources_total_size_bytes());
581 std::vector<ServiceWorkerRegistrationInfo> all_registrations; 597 std::vector<ServiceWorkerRegistrationInfo> all_registrations;
582 GetAllRegistrations(&all_registrations); 598 GetAllRegistrationsInfos(&all_registrations);
583 EXPECT_EQ(1u, all_registrations.size()); 599 EXPECT_EQ(1u, all_registrations.size());
584 ServiceWorkerRegistrationInfo info = all_registrations[0]; 600 ServiceWorkerRegistrationInfo info = all_registrations[0];
585 EXPECT_EQ(kResource1Size + kResource2Size, info.stored_version_size_bytes); 601 EXPECT_EQ(kResource1Size + kResource2Size, info.stored_version_size_bytes);
586 all_registrations.clear(); 602 all_registrations.clear();
587 603
588 // Finding by origin should provide the same result iif origin is kScope. 604 // Finding by origin should provide the same result iif origin is kScope.
589 std::vector<ServiceWorkerRegistrationInfo> registrations_origin; 605 std::vector<ServiceWorkerRegistration*> registrations_origin;
590 GetRegistrationsForOrigin(kScope.GetOrigin(), &registrations_origin); 606 std::vector<std::vector<ResourceRecord>> resource_lists;
607 GetRegistrationsForOrigin(kScope.GetOrigin(), &registrations_origin,
608 &resource_lists);
591 EXPECT_EQ(1u, registrations_origin.size()); 609 EXPECT_EQ(1u, registrations_origin.size());
592 registrations_origin.clear(); 610 registrations_origin.clear();
593 611
594 GetRegistrationsForOrigin( 612 GetRegistrationsForOrigin(GURL("http://example.com/").GetOrigin(),
595 GURL("http://example.com/").GetOrigin(), 613 &registrations_origin, &resource_lists);
596 &registrations_origin);
597 EXPECT_TRUE(registrations_origin.empty()); 614 EXPECT_TRUE(registrations_origin.empty());
598 615
599 found_registration = NULL; 616 found_registration = NULL;
600 617
601 // Drop the live version too. 618 // Drop the live version too.
602 live_version = NULL; 619 live_version = NULL;
603 620
604 // And FindRegistrationForPattern is always async. 621 // And FindRegistrationForPattern is always async.
605 EXPECT_EQ(SERVICE_WORKER_OK, 622 EXPECT_EQ(SERVICE_WORKER_OK,
606 FindRegistrationForPattern(kScope, &found_registration)); 623 FindRegistrationForPattern(kScope, &found_registration));
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
678 // Create an unstored registration. 695 // Create an unstored registration.
679 scoped_refptr<ServiceWorkerRegistration> live_registration = 696 scoped_refptr<ServiceWorkerRegistration> live_registration =
680 new ServiceWorkerRegistration( 697 new ServiceWorkerRegistration(
681 kScope, kRegistrationId, context_ptr_); 698 kScope, kRegistrationId, context_ptr_);
682 scoped_refptr<ServiceWorkerVersion> live_version = 699 scoped_refptr<ServiceWorkerVersion> live_version =
683 new ServiceWorkerVersion( 700 new ServiceWorkerVersion(
684 live_registration.get(), kScript, kVersionId, context_ptr_); 701 live_registration.get(), kScript, kVersionId, context_ptr_);
685 live_version->SetStatus(ServiceWorkerVersion::INSTALLING); 702 live_version->SetStatus(ServiceWorkerVersion::INSTALLING);
686 live_registration->SetWaitingVersion(live_version); 703 live_registration->SetWaitingVersion(live_version);
687 704
688 // Should not be findable, including by GetAllRegistrations. 705 // Should not be findable, including by GetAllRegistrationsInfos.
689 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 706 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
690 FindRegistrationForId( 707 FindRegistrationForId(
691 kRegistrationId, kScope.GetOrigin(), &found_registration)); 708 kRegistrationId, kScope.GetOrigin(), &found_registration));
692 EXPECT_FALSE(found_registration.get()); 709 EXPECT_FALSE(found_registration.get());
693 710
694 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 711 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
695 FindRegistrationForIdOnly(kRegistrationId, &found_registration)); 712 FindRegistrationForIdOnly(kRegistrationId, &found_registration));
696 EXPECT_FALSE(found_registration.get()); 713 EXPECT_FALSE(found_registration.get());
697 714
698 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 715 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
699 FindRegistrationForDocument(kDocumentUrl, &found_registration)); 716 FindRegistrationForDocument(kDocumentUrl, &found_registration));
700 EXPECT_FALSE(found_registration.get()); 717 EXPECT_FALSE(found_registration.get());
701 718
702 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 719 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
703 FindRegistrationForPattern(kScope, &found_registration)); 720 FindRegistrationForPattern(kScope, &found_registration));
704 EXPECT_FALSE(found_registration.get()); 721 EXPECT_FALSE(found_registration.get());
705 722
706 std::vector<ServiceWorkerRegistrationInfo> all_registrations; 723 std::vector<ServiceWorkerRegistrationInfo> all_registrations;
707 GetAllRegistrations(&all_registrations); 724 GetAllRegistrationsInfos(&all_registrations);
708 EXPECT_TRUE(all_registrations.empty()); 725 EXPECT_TRUE(all_registrations.empty());
709 726
710 std::vector<ServiceWorkerRegistrationInfo> registrations_origin; 727 std::vector<ServiceWorkerRegistration*> registrations_origin;
711 GetRegistrationsForOrigin(kScope.GetOrigin(), &registrations_origin); 728 std::vector<std::vector<ResourceRecord>> resource_lists;
729 GetRegistrationsForOrigin(kScope.GetOrigin(), &registrations_origin,
730 &resource_lists);
712 EXPECT_TRUE(registrations_origin.empty()); 731 EXPECT_TRUE(registrations_origin.empty());
713 732
714 GetRegistrationsForOrigin( 733 GetRegistrationsForOrigin(GURL("http://example.com/").GetOrigin(),
715 GURL("http://example.com/").GetOrigin(), 734 &registrations_origin, &resource_lists);
716 &registrations_origin);
717 EXPECT_TRUE(registrations_origin.empty()); 735 EXPECT_TRUE(registrations_origin.empty());
718 736
719 // Notify storage of it being installed. 737 // Notify storage of it being installed.
720 storage()->NotifyInstallingRegistration(live_registration.get()); 738 storage()->NotifyInstallingRegistration(live_registration.get());
721 739
722 // Now should be findable. 740 // Now should be findable.
723 EXPECT_EQ(SERVICE_WORKER_OK, 741 EXPECT_EQ(SERVICE_WORKER_OK,
724 FindRegistrationForId( 742 FindRegistrationForId(
725 kRegistrationId, kScope.GetOrigin(), &found_registration)); 743 kRegistrationId, kScope.GetOrigin(), &found_registration));
726 EXPECT_EQ(live_registration, found_registration); 744 EXPECT_EQ(live_registration, found_registration);
727 found_registration = NULL; 745 found_registration = NULL;
728 746
729 EXPECT_EQ(SERVICE_WORKER_OK, 747 EXPECT_EQ(SERVICE_WORKER_OK,
730 FindRegistrationForIdOnly(kRegistrationId, &found_registration)); 748 FindRegistrationForIdOnly(kRegistrationId, &found_registration));
731 EXPECT_EQ(live_registration, found_registration); 749 EXPECT_EQ(live_registration, found_registration);
732 found_registration = NULL; 750 found_registration = NULL;
733 751
734 EXPECT_EQ(SERVICE_WORKER_OK, 752 EXPECT_EQ(SERVICE_WORKER_OK,
735 FindRegistrationForDocument(kDocumentUrl, &found_registration)); 753 FindRegistrationForDocument(kDocumentUrl, &found_registration));
736 EXPECT_EQ(live_registration, found_registration); 754 EXPECT_EQ(live_registration, found_registration);
737 found_registration = NULL; 755 found_registration = NULL;
738 756
739 EXPECT_EQ(SERVICE_WORKER_OK, 757 EXPECT_EQ(SERVICE_WORKER_OK,
740 FindRegistrationForPattern(kScope, &found_registration)); 758 FindRegistrationForPattern(kScope, &found_registration));
741 EXPECT_EQ(live_registration, found_registration); 759 EXPECT_EQ(live_registration, found_registration);
742 found_registration = NULL; 760 found_registration = NULL;
743 761
744 GetAllRegistrations(&all_registrations); 762 GetAllRegistrationsInfos(&all_registrations);
745 EXPECT_EQ(1u, all_registrations.size()); 763 EXPECT_EQ(1u, all_registrations.size());
746 all_registrations.clear(); 764 all_registrations.clear();
747 765
748 // Finding by origin should provide the same result iif origin is kScope. 766 // Finding by origin should provide the same result iif origin is kScope.
749 GetRegistrationsForOrigin(kScope.GetOrigin(), &registrations_origin); 767 GetRegistrationsForOrigin(kScope.GetOrigin(), &registrations_origin,
768 &resource_lists);
750 EXPECT_EQ(1u, registrations_origin.size()); 769 EXPECT_EQ(1u, registrations_origin.size());
751 registrations_origin.clear(); 770 registrations_origin.clear();
752 771
753 GetRegistrationsForOrigin( 772 GetRegistrationsForOrigin(GURL("http://example.com/").GetOrigin(),
754 GURL("http://example.com/").GetOrigin(), 773 &registrations_origin, &resource_lists);
755 &registrations_origin);
756 EXPECT_TRUE(registrations_origin.empty()); 774 EXPECT_TRUE(registrations_origin.empty());
757 775
758 // Notify storage of installation no longer happening. 776 // Notify storage of installation no longer happening.
759 storage()->NotifyDoneInstallingRegistration( 777 storage()->NotifyDoneInstallingRegistration(
760 live_registration.get(), NULL, SERVICE_WORKER_OK); 778 live_registration.get(), NULL, SERVICE_WORKER_OK);
761 779
762 // Once again, should not be findable. 780 // Once again, should not be findable.
763 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 781 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
764 FindRegistrationForId( 782 FindRegistrationForId(
765 kRegistrationId, kScope.GetOrigin(), &found_registration)); 783 kRegistrationId, kScope.GetOrigin(), &found_registration));
766 EXPECT_FALSE(found_registration.get()); 784 EXPECT_FALSE(found_registration.get());
767 785
768 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 786 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
769 FindRegistrationForIdOnly(kRegistrationId, &found_registration)); 787 FindRegistrationForIdOnly(kRegistrationId, &found_registration));
770 EXPECT_FALSE(found_registration.get()); 788 EXPECT_FALSE(found_registration.get());
771 789
772 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 790 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
773 FindRegistrationForDocument(kDocumentUrl, &found_registration)); 791 FindRegistrationForDocument(kDocumentUrl, &found_registration));
774 EXPECT_FALSE(found_registration.get()); 792 EXPECT_FALSE(found_registration.get());
775 793
776 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 794 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
777 FindRegistrationForPattern(kScope, &found_registration)); 795 FindRegistrationForPattern(kScope, &found_registration));
778 EXPECT_FALSE(found_registration.get()); 796 EXPECT_FALSE(found_registration.get());
779 797
780 GetAllRegistrations(&all_registrations); 798 GetAllRegistrationsInfos(&all_registrations);
781 EXPECT_TRUE(all_registrations.empty()); 799 EXPECT_TRUE(all_registrations.empty());
782 800
783 GetRegistrationsForOrigin(kScope.GetOrigin(), &registrations_origin); 801 GetRegistrationsForOrigin(kScope.GetOrigin(), &registrations_origin,
802 &resource_lists);
784 EXPECT_TRUE(registrations_origin.empty()); 803 EXPECT_TRUE(registrations_origin.empty());
785 804
786 GetRegistrationsForOrigin( 805 GetRegistrationsForOrigin(GURL("http://example.com/").GetOrigin(),
787 GURL("http://example.com/").GetOrigin(), 806 &registrations_origin, &resource_lists);
788 &registrations_origin);
789 EXPECT_TRUE(registrations_origin.empty()); 807 EXPECT_TRUE(registrations_origin.empty());
790 } 808 }
791 809
792 TEST_F(ServiceWorkerStorageTest, StoreUserData) { 810 TEST_F(ServiceWorkerStorageTest, StoreUserData) {
793 const GURL kScope("http://www.test.not/scope/"); 811 const GURL kScope("http://www.test.not/scope/");
794 const GURL kScript("http://www.test.not/script.js"); 812 const GURL kScript("http://www.test.not/script.js");
795 const int64 kRegistrationId = 0; 813 const int64 kRegistrationId = 0;
796 const int64 kVersionId = 0; 814 const int64 kVersionId = 0;
797 815
798 LazyInitialize(); 816 LazyInitialize();
(...skipping 630 matching lines...) Expand 10 before | Expand all | Expand 10 after
1429 are_equal = true; 1447 are_equal = true;
1430 storage()->CompareScriptResources( 1448 storage()->CompareScriptResources(
1431 5, 6, 1449 5, 6,
1432 base::Bind(&OnCompareComplete, &status, &are_equal)); 1450 base::Bind(&OnCompareComplete, &status, &are_equal));
1433 base::RunLoop().RunUntilIdle(); 1451 base::RunLoop().RunUntilIdle();
1434 EXPECT_EQ(SERVICE_WORKER_OK, status); 1452 EXPECT_EQ(SERVICE_WORKER_OK, status);
1435 EXPECT_FALSE(are_equal); 1453 EXPECT_FALSE(are_equal);
1436 } 1454 }
1437 1455
1438 } // namespace content 1456 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698