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

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: Use scoped_refptr to ServiceWorkerRegistration and rename variables. 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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
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(
70 bool* was_called, 70 bool* was_called,
71 std::vector<scoped_refptr<ServiceWorkerRegistration>>* all_out,
72 const std::vector<scoped_refptr<ServiceWorkerRegistration>>& all) {
73 *was_called = true;
74 *all_out = all;
75 }
76
77 void GetAllInfosCallback(
78 bool* was_called,
71 std::vector<ServiceWorkerRegistrationInfo>* all_out, 79 std::vector<ServiceWorkerRegistrationInfo>* all_out,
72 const std::vector<ServiceWorkerRegistrationInfo>& all) { 80 const std::vector<ServiceWorkerRegistrationInfo>& all) {
73 *was_called = true; 81 *was_called = true;
74 *all_out = all; 82 *all_out = all;
75 } 83 }
76 84
85 ServiceWorkerStorage::GetRegistrationsCallback MakeGetRegistrationsCallback(
86 bool* was_called,
87 std::vector<scoped_refptr<ServiceWorkerRegistration>>* all,
88 std::vector<std::vector<ResourceRecord>>* resource_lists) {
89 return base::Bind(&GetAllCallback, was_called, all);
90 }
91
77 ServiceWorkerStorage::GetRegistrationsInfosCallback 92 ServiceWorkerStorage::GetRegistrationsInfosCallback
78 MakeGetRegistrationsCallback(bool* was_called, 93 MakeGetRegistrationsInfosCallback(
79 std::vector<ServiceWorkerRegistrationInfo>* all) { 94 bool* was_called,
80 return base::Bind(&GetAllCallback, was_called, all); 95 std::vector<ServiceWorkerRegistrationInfo>* all) {
96 return base::Bind(&GetAllInfosCallback, was_called, all);
81 } 97 }
82 98
83 void GetUserDataCallback( 99 void GetUserDataCallback(
84 bool* was_called, 100 bool* was_called,
85 std::string* data_out, 101 std::string* data_out,
86 ServiceWorkerStatusCode* status_out, 102 ServiceWorkerStatusCode* status_out,
87 const std::string& data, 103 const std::string& data,
88 ServiceWorkerStatusCode status) { 104 ServiceWorkerStatusCode status) {
89 *was_called = true; 105 *was_called = true;
90 *data_out = data; 106 *data_out = data;
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 bool was_called = false; 338 bool was_called = false;
323 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED; 339 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED;
324 storage()->DeleteRegistration( 340 storage()->DeleteRegistration(
325 registration_id, origin, MakeStatusCallback(&was_called, &result)); 341 registration_id, origin, MakeStatusCallback(&was_called, &result));
326 EXPECT_FALSE(was_called); // always async 342 EXPECT_FALSE(was_called); // always async
327 base::RunLoop().RunUntilIdle(); 343 base::RunLoop().RunUntilIdle();
328 EXPECT_TRUE(was_called); 344 EXPECT_TRUE(was_called);
329 return result; 345 return result;
330 } 346 }
331 347
332 void GetAllRegistrations( 348 void GetAllRegistrationsInfos(
333 std::vector<ServiceWorkerRegistrationInfo>* registrations) { 349 std::vector<ServiceWorkerRegistrationInfo>* registrations) {
334 bool was_called = false; 350 bool was_called = false;
335 storage()->GetAllRegistrations( 351 storage()->GetAllRegistrationsInfos(
336 MakeGetRegistrationsCallback(&was_called, registrations)); 352 MakeGetRegistrationsInfosCallback(&was_called, registrations));
337 EXPECT_FALSE(was_called); // always async 353 EXPECT_FALSE(was_called); // always async
338 base::RunLoop().RunUntilIdle(); 354 base::RunLoop().RunUntilIdle();
339 EXPECT_TRUE(was_called); 355 EXPECT_TRUE(was_called);
340 } 356 }
341 357
342 void GetRegistrationsForOrigin( 358 void GetRegistrationsForOrigin(
343 const GURL& origin, 359 const GURL& origin,
344 std::vector<ServiceWorkerRegistrationInfo>* registrations) { 360 std::vector<scoped_refptr<ServiceWorkerRegistration>>* registrations,
361 std::vector<std::vector<ResourceRecord>>* resource_lists) {
345 bool was_called = false; 362 bool was_called = false;
346 storage()->GetRegistrationsForOrigin( 363 storage()->GetRegistrationsForOrigin(
347 origin, 364 origin, MakeGetRegistrationsCallback(&was_called, registrations,
348 MakeGetRegistrationsCallback(&was_called, registrations)); 365 resource_lists));
349 EXPECT_FALSE(was_called); // always async 366 EXPECT_FALSE(was_called); // always async
350 base::RunLoop().RunUntilIdle(); 367 base::RunLoop().RunUntilIdle();
351 EXPECT_TRUE(was_called); 368 EXPECT_TRUE(was_called);
352 } 369 }
353 370
354 ServiceWorkerStatusCode GetUserData( 371 ServiceWorkerStatusCode GetUserData(
355 int64 registration_id, 372 int64 registration_id,
356 const std::string& key, 373 const std::string& key,
357 std::string* data) { 374 std::string* data) {
358 bool was_called = false; 375 bool was_called = false;
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after
572 FindRegistrationForDocument(kDocumentUrl, &found_registration)); 589 FindRegistrationForDocument(kDocumentUrl, &found_registration));
573 ASSERT_TRUE(found_registration.get()); 590 ASSERT_TRUE(found_registration.get());
574 EXPECT_EQ(kRegistrationId, found_registration->id()); 591 EXPECT_EQ(kRegistrationId, found_registration->id());
575 EXPECT_TRUE(found_registration->HasOneRef()); 592 EXPECT_TRUE(found_registration->HasOneRef());
576 593
577 // Check that sizes are populated correctly 594 // Check that sizes are populated correctly
578 EXPECT_EQ(live_version.get(), found_registration->waiting_version()); 595 EXPECT_EQ(live_version.get(), found_registration->waiting_version());
579 EXPECT_EQ(kResource1Size + kResource2Size, 596 EXPECT_EQ(kResource1Size + kResource2Size,
580 found_registration->resources_total_size_bytes()); 597 found_registration->resources_total_size_bytes());
581 std::vector<ServiceWorkerRegistrationInfo> all_registrations; 598 std::vector<ServiceWorkerRegistrationInfo> all_registrations;
582 GetAllRegistrations(&all_registrations); 599 GetAllRegistrationsInfos(&all_registrations);
583 EXPECT_EQ(1u, all_registrations.size()); 600 EXPECT_EQ(1u, all_registrations.size());
584 ServiceWorkerRegistrationInfo info = all_registrations[0]; 601 ServiceWorkerRegistrationInfo info = all_registrations[0];
585 EXPECT_EQ(kResource1Size + kResource2Size, info.stored_version_size_bytes); 602 EXPECT_EQ(kResource1Size + kResource2Size, info.stored_version_size_bytes);
586 all_registrations.clear(); 603 all_registrations.clear();
587 604
588 // Finding by origin should provide the same result iif origin is kScope. 605 // Finding by origin should provide the same result iif origin is kScope.
falken 2015/06/08 04:59:10 nit: not from this patch but s/iif/if
jungkees 2015/06/08 16:00:19 Fixed.
589 std::vector<ServiceWorkerRegistrationInfo> registrations_origin; 606 std::vector<scoped_refptr<ServiceWorkerRegistration>> registrations_origin;
falken 2015/06/08 04:59:10 nit: also not from this patch but registrations_fo
jungkees 2015/06/08 16:00:19 Renamed all the occurrences.
falken 2015/06/08 16:14:58 Sorry, I didn't realize there were multiple occurr
590 GetRegistrationsForOrigin(kScope.GetOrigin(), &registrations_origin); 607 std::vector<std::vector<ResourceRecord>> resource_lists;
608 GetRegistrationsForOrigin(kScope.GetOrigin(), &registrations_origin,
609 &resource_lists);
591 EXPECT_EQ(1u, registrations_origin.size()); 610 EXPECT_EQ(1u, registrations_origin.size());
592 registrations_origin.clear(); 611 registrations_origin.clear();
593 612
594 GetRegistrationsForOrigin( 613 GetRegistrationsForOrigin(GURL("http://example.com/").GetOrigin(),
595 GURL("http://example.com/").GetOrigin(), 614 &registrations_origin, &resource_lists);
596 &registrations_origin);
597 EXPECT_TRUE(registrations_origin.empty()); 615 EXPECT_TRUE(registrations_origin.empty());
598 616
599 found_registration = NULL; 617 found_registration = NULL;
600 618
601 // Drop the live version too. 619 // Drop the live version too.
602 live_version = NULL; 620 live_version = NULL;
603 621
604 // And FindRegistrationForPattern is always async. 622 // And FindRegistrationForPattern is always async.
605 EXPECT_EQ(SERVICE_WORKER_OK, 623 EXPECT_EQ(SERVICE_WORKER_OK,
606 FindRegistrationForPattern(kScope, &found_registration)); 624 FindRegistrationForPattern(kScope, &found_registration));
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
678 // Create an unstored registration. 696 // Create an unstored registration.
679 scoped_refptr<ServiceWorkerRegistration> live_registration = 697 scoped_refptr<ServiceWorkerRegistration> live_registration =
680 new ServiceWorkerRegistration( 698 new ServiceWorkerRegistration(
681 kScope, kRegistrationId, context_ptr_); 699 kScope, kRegistrationId, context_ptr_);
682 scoped_refptr<ServiceWorkerVersion> live_version = 700 scoped_refptr<ServiceWorkerVersion> live_version =
683 new ServiceWorkerVersion( 701 new ServiceWorkerVersion(
684 live_registration.get(), kScript, kVersionId, context_ptr_); 702 live_registration.get(), kScript, kVersionId, context_ptr_);
685 live_version->SetStatus(ServiceWorkerVersion::INSTALLING); 703 live_version->SetStatus(ServiceWorkerVersion::INSTALLING);
686 live_registration->SetWaitingVersion(live_version); 704 live_registration->SetWaitingVersion(live_version);
687 705
688 // Should not be findable, including by GetAllRegistrations. 706 // Should not be findable, including by GetAllRegistrationsInfos.
689 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 707 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
690 FindRegistrationForId( 708 FindRegistrationForId(
691 kRegistrationId, kScope.GetOrigin(), &found_registration)); 709 kRegistrationId, kScope.GetOrigin(), &found_registration));
692 EXPECT_FALSE(found_registration.get()); 710 EXPECT_FALSE(found_registration.get());
693 711
694 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 712 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
695 FindRegistrationForIdOnly(kRegistrationId, &found_registration)); 713 FindRegistrationForIdOnly(kRegistrationId, &found_registration));
696 EXPECT_FALSE(found_registration.get()); 714 EXPECT_FALSE(found_registration.get());
697 715
698 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 716 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
699 FindRegistrationForDocument(kDocumentUrl, &found_registration)); 717 FindRegistrationForDocument(kDocumentUrl, &found_registration));
700 EXPECT_FALSE(found_registration.get()); 718 EXPECT_FALSE(found_registration.get());
701 719
702 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 720 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
703 FindRegistrationForPattern(kScope, &found_registration)); 721 FindRegistrationForPattern(kScope, &found_registration));
704 EXPECT_FALSE(found_registration.get()); 722 EXPECT_FALSE(found_registration.get());
705 723
706 std::vector<ServiceWorkerRegistrationInfo> all_registrations; 724 std::vector<ServiceWorkerRegistrationInfo> all_registrations;
707 GetAllRegistrations(&all_registrations); 725 GetAllRegistrationsInfos(&all_registrations);
708 EXPECT_TRUE(all_registrations.empty()); 726 EXPECT_TRUE(all_registrations.empty());
709 727
710 std::vector<ServiceWorkerRegistrationInfo> registrations_origin; 728 std::vector<scoped_refptr<ServiceWorkerRegistration>> registrations_origin;
711 GetRegistrationsForOrigin(kScope.GetOrigin(), &registrations_origin); 729 std::vector<std::vector<ResourceRecord>> resource_lists;
falken 2015/06/08 04:59:10 Do we ever test the value of this?
jungkees 2015/06/08 16:00:19 Thanks for spotting this. This value is not at all
falken 2015/06/08 16:14:58 I was hoping we could add tests for the new functi
jungkees 2015/06/08 16:27:17 That bit is done in the service_worker_database_un
falken 2015/06/09 07:37:50 Ah of course. Thanks!
730 GetRegistrationsForOrigin(kScope.GetOrigin(), &registrations_origin,
731 &resource_lists);
712 EXPECT_TRUE(registrations_origin.empty()); 732 EXPECT_TRUE(registrations_origin.empty());
713 733
714 GetRegistrationsForOrigin( 734 GetRegistrationsForOrigin(GURL("http://example.com/").GetOrigin(),
715 GURL("http://example.com/").GetOrigin(), 735 &registrations_origin, &resource_lists);
716 &registrations_origin);
717 EXPECT_TRUE(registrations_origin.empty()); 736 EXPECT_TRUE(registrations_origin.empty());
718 737
719 // Notify storage of it being installed. 738 // Notify storage of it being installed.
720 storage()->NotifyInstallingRegistration(live_registration.get()); 739 storage()->NotifyInstallingRegistration(live_registration.get());
721 740
722 // Now should be findable. 741 // Now should be findable.
723 EXPECT_EQ(SERVICE_WORKER_OK, 742 EXPECT_EQ(SERVICE_WORKER_OK,
724 FindRegistrationForId( 743 FindRegistrationForId(
725 kRegistrationId, kScope.GetOrigin(), &found_registration)); 744 kRegistrationId, kScope.GetOrigin(), &found_registration));
726 EXPECT_EQ(live_registration, found_registration); 745 EXPECT_EQ(live_registration, found_registration);
727 found_registration = NULL; 746 found_registration = NULL;
728 747
729 EXPECT_EQ(SERVICE_WORKER_OK, 748 EXPECT_EQ(SERVICE_WORKER_OK,
730 FindRegistrationForIdOnly(kRegistrationId, &found_registration)); 749 FindRegistrationForIdOnly(kRegistrationId, &found_registration));
731 EXPECT_EQ(live_registration, found_registration); 750 EXPECT_EQ(live_registration, found_registration);
732 found_registration = NULL; 751 found_registration = NULL;
733 752
734 EXPECT_EQ(SERVICE_WORKER_OK, 753 EXPECT_EQ(SERVICE_WORKER_OK,
735 FindRegistrationForDocument(kDocumentUrl, &found_registration)); 754 FindRegistrationForDocument(kDocumentUrl, &found_registration));
736 EXPECT_EQ(live_registration, found_registration); 755 EXPECT_EQ(live_registration, found_registration);
737 found_registration = NULL; 756 found_registration = NULL;
738 757
739 EXPECT_EQ(SERVICE_WORKER_OK, 758 EXPECT_EQ(SERVICE_WORKER_OK,
740 FindRegistrationForPattern(kScope, &found_registration)); 759 FindRegistrationForPattern(kScope, &found_registration));
741 EXPECT_EQ(live_registration, found_registration); 760 EXPECT_EQ(live_registration, found_registration);
742 found_registration = NULL; 761 found_registration = NULL;
743 762
744 GetAllRegistrations(&all_registrations); 763 GetAllRegistrationsInfos(&all_registrations);
745 EXPECT_EQ(1u, all_registrations.size()); 764 EXPECT_EQ(1u, all_registrations.size());
746 all_registrations.clear(); 765 all_registrations.clear();
747 766
748 // Finding by origin should provide the same result iif origin is kScope. 767 // Finding by origin should provide the same result iif origin is kScope.
749 GetRegistrationsForOrigin(kScope.GetOrigin(), &registrations_origin); 768 GetRegistrationsForOrigin(kScope.GetOrigin(), &registrations_origin,
769 &resource_lists);
750 EXPECT_EQ(1u, registrations_origin.size()); 770 EXPECT_EQ(1u, registrations_origin.size());
751 registrations_origin.clear(); 771 registrations_origin.clear();
752 772
753 GetRegistrationsForOrigin( 773 GetRegistrationsForOrigin(GURL("http://example.com/").GetOrigin(),
754 GURL("http://example.com/").GetOrigin(), 774 &registrations_origin, &resource_lists);
755 &registrations_origin);
756 EXPECT_TRUE(registrations_origin.empty()); 775 EXPECT_TRUE(registrations_origin.empty());
757 776
758 // Notify storage of installation no longer happening. 777 // Notify storage of installation no longer happening.
759 storage()->NotifyDoneInstallingRegistration( 778 storage()->NotifyDoneInstallingRegistration(
760 live_registration.get(), NULL, SERVICE_WORKER_OK); 779 live_registration.get(), NULL, SERVICE_WORKER_OK);
761 780
762 // Once again, should not be findable. 781 // Once again, should not be findable.
763 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 782 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
764 FindRegistrationForId( 783 FindRegistrationForId(
765 kRegistrationId, kScope.GetOrigin(), &found_registration)); 784 kRegistrationId, kScope.GetOrigin(), &found_registration));
766 EXPECT_FALSE(found_registration.get()); 785 EXPECT_FALSE(found_registration.get());
767 786
768 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 787 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
769 FindRegistrationForIdOnly(kRegistrationId, &found_registration)); 788 FindRegistrationForIdOnly(kRegistrationId, &found_registration));
770 EXPECT_FALSE(found_registration.get()); 789 EXPECT_FALSE(found_registration.get());
771 790
772 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 791 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
773 FindRegistrationForDocument(kDocumentUrl, &found_registration)); 792 FindRegistrationForDocument(kDocumentUrl, &found_registration));
774 EXPECT_FALSE(found_registration.get()); 793 EXPECT_FALSE(found_registration.get());
775 794
776 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 795 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
777 FindRegistrationForPattern(kScope, &found_registration)); 796 FindRegistrationForPattern(kScope, &found_registration));
778 EXPECT_FALSE(found_registration.get()); 797 EXPECT_FALSE(found_registration.get());
779 798
780 GetAllRegistrations(&all_registrations); 799 GetAllRegistrationsInfos(&all_registrations);
781 EXPECT_TRUE(all_registrations.empty()); 800 EXPECT_TRUE(all_registrations.empty());
782 801
783 GetRegistrationsForOrigin(kScope.GetOrigin(), &registrations_origin); 802 GetRegistrationsForOrigin(kScope.GetOrigin(), &registrations_origin,
803 &resource_lists);
784 EXPECT_TRUE(registrations_origin.empty()); 804 EXPECT_TRUE(registrations_origin.empty());
785 805
786 GetRegistrationsForOrigin( 806 GetRegistrationsForOrigin(GURL("http://example.com/").GetOrigin(),
787 GURL("http://example.com/").GetOrigin(), 807 &registrations_origin, &resource_lists);
788 &registrations_origin);
789 EXPECT_TRUE(registrations_origin.empty()); 808 EXPECT_TRUE(registrations_origin.empty());
790 } 809 }
791 810
792 TEST_F(ServiceWorkerStorageTest, StoreUserData) { 811 TEST_F(ServiceWorkerStorageTest, StoreUserData) {
793 const GURL kScope("http://www.test.not/scope/"); 812 const GURL kScope("http://www.test.not/scope/");
794 const GURL kScript("http://www.test.not/script.js"); 813 const GURL kScript("http://www.test.not/script.js");
795 const int64 kRegistrationId = 0; 814 const int64 kRegistrationId = 0;
796 const int64 kVersionId = 0; 815 const int64 kVersionId = 0;
797 816
798 LazyInitialize(); 817 LazyInitialize();
(...skipping 630 matching lines...) Expand 10 before | Expand all | Expand 10 after
1429 are_equal = true; 1448 are_equal = true;
1430 storage()->CompareScriptResources( 1449 storage()->CompareScriptResources(
1431 5, 6, 1450 5, 6,
1432 base::Bind(&OnCompareComplete, &status, &are_equal)); 1451 base::Bind(&OnCompareComplete, &status, &are_equal));
1433 base::RunLoop().RunUntilIdle(); 1452 base::RunLoop().RunUntilIdle();
1434 EXPECT_EQ(SERVICE_WORKER_OK, status); 1453 EXPECT_EQ(SERVICE_WORKER_OK, status);
1435 EXPECT_FALSE(are_equal); 1454 EXPECT_FALSE(are_equal);
1436 } 1455 }
1437 1456
1438 } // namespace content 1457 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698