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

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: Update tools/metrics/histograms/histograms.xml. 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 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<scoped_refptr<ServiceWorkerRegistration>>* registrations) {
345 bool was_called = false; 360 bool was_called = false;
346 storage()->GetRegistrationsForOrigin( 361 storage()->GetRegistrationsForOrigin(
347 origin, 362 origin, MakeGetRegistrationsCallback(&was_called, registrations));
348 MakeGetRegistrationsCallback(&was_called, registrations));
349 EXPECT_FALSE(was_called); // always async 363 EXPECT_FALSE(was_called); // always async
350 base::RunLoop().RunUntilIdle(); 364 base::RunLoop().RunUntilIdle();
351 EXPECT_TRUE(was_called); 365 EXPECT_TRUE(was_called);
352 } 366 }
353 367
354 ServiceWorkerStatusCode GetUserData( 368 ServiceWorkerStatusCode GetUserData(
355 int64 registration_id, 369 int64 registration_id,
356 const std::string& key, 370 const std::string& key,
357 std::string* data) { 371 std::string* data) {
358 bool was_called = false; 372 bool was_called = false;
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after
572 FindRegistrationForDocument(kDocumentUrl, &found_registration)); 586 FindRegistrationForDocument(kDocumentUrl, &found_registration));
573 ASSERT_TRUE(found_registration.get()); 587 ASSERT_TRUE(found_registration.get());
574 EXPECT_EQ(kRegistrationId, found_registration->id()); 588 EXPECT_EQ(kRegistrationId, found_registration->id());
575 EXPECT_TRUE(found_registration->HasOneRef()); 589 EXPECT_TRUE(found_registration->HasOneRef());
576 590
577 // Check that sizes are populated correctly 591 // Check that sizes are populated correctly
578 EXPECT_EQ(live_version.get(), found_registration->waiting_version()); 592 EXPECT_EQ(live_version.get(), found_registration->waiting_version());
579 EXPECT_EQ(kResource1Size + kResource2Size, 593 EXPECT_EQ(kResource1Size + kResource2Size,
580 found_registration->resources_total_size_bytes()); 594 found_registration->resources_total_size_bytes());
581 std::vector<ServiceWorkerRegistrationInfo> all_registrations; 595 std::vector<ServiceWorkerRegistrationInfo> all_registrations;
582 GetAllRegistrations(&all_registrations); 596 GetAllRegistrationsInfos(&all_registrations);
583 EXPECT_EQ(1u, all_registrations.size()); 597 EXPECT_EQ(1u, all_registrations.size());
584 ServiceWorkerRegistrationInfo info = all_registrations[0]; 598 ServiceWorkerRegistrationInfo info = all_registrations[0];
585 EXPECT_EQ(kResource1Size + kResource2Size, info.stored_version_size_bytes); 599 EXPECT_EQ(kResource1Size + kResource2Size, info.stored_version_size_bytes);
586 all_registrations.clear(); 600 all_registrations.clear();
587 601
588 // Finding by origin should provide the same result iif origin is kScope. 602 // Finding by origin should provide the same result if origin is kScope.
589 std::vector<ServiceWorkerRegistrationInfo> registrations_origin; 603 std::vector<scoped_refptr<ServiceWorkerRegistration>>
590 GetRegistrationsForOrigin(kScope.GetOrigin(), &registrations_origin); 604 registrations_for_origin;
591 EXPECT_EQ(1u, registrations_origin.size()); 605 GetRegistrationsForOrigin(kScope.GetOrigin(), &registrations_for_origin);
592 registrations_origin.clear(); 606 EXPECT_EQ(1u, registrations_for_origin.size());
607 registrations_for_origin.clear();
593 608
594 GetRegistrationsForOrigin( 609 GetRegistrationsForOrigin(GURL("http://example.com/").GetOrigin(),
595 GURL("http://example.com/").GetOrigin(), 610 &registrations_for_origin);
596 &registrations_origin); 611 EXPECT_TRUE(registrations_for_origin.empty());
597 EXPECT_TRUE(registrations_origin.empty());
598 612
599 found_registration = NULL; 613 found_registration = NULL;
600 614
601 // Drop the live version too. 615 // Drop the live version too.
602 live_version = NULL; 616 live_version = NULL;
603 617
604 // And FindRegistrationForPattern is always async. 618 // And FindRegistrationForPattern is always async.
605 EXPECT_EQ(SERVICE_WORKER_OK, 619 EXPECT_EQ(SERVICE_WORKER_OK,
606 FindRegistrationForPattern(kScope, &found_registration)); 620 FindRegistrationForPattern(kScope, &found_registration));
607 ASSERT_TRUE(found_registration.get()); 621 ASSERT_TRUE(found_registration.get());
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
678 // Create an unstored registration. 692 // Create an unstored registration.
679 scoped_refptr<ServiceWorkerRegistration> live_registration = 693 scoped_refptr<ServiceWorkerRegistration> live_registration =
680 new ServiceWorkerRegistration( 694 new ServiceWorkerRegistration(
681 kScope, kRegistrationId, context_ptr_); 695 kScope, kRegistrationId, context_ptr_);
682 scoped_refptr<ServiceWorkerVersion> live_version = 696 scoped_refptr<ServiceWorkerVersion> live_version =
683 new ServiceWorkerVersion( 697 new ServiceWorkerVersion(
684 live_registration.get(), kScript, kVersionId, context_ptr_); 698 live_registration.get(), kScript, kVersionId, context_ptr_);
685 live_version->SetStatus(ServiceWorkerVersion::INSTALLING); 699 live_version->SetStatus(ServiceWorkerVersion::INSTALLING);
686 live_registration->SetWaitingVersion(live_version); 700 live_registration->SetWaitingVersion(live_version);
687 701
688 // Should not be findable, including by GetAllRegistrations. 702 // Should not be findable, including by GetAllRegistrationsInfos.
689 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 703 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
690 FindRegistrationForId( 704 FindRegistrationForId(
691 kRegistrationId, kScope.GetOrigin(), &found_registration)); 705 kRegistrationId, kScope.GetOrigin(), &found_registration));
692 EXPECT_FALSE(found_registration.get()); 706 EXPECT_FALSE(found_registration.get());
693 707
694 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 708 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
695 FindRegistrationForIdOnly(kRegistrationId, &found_registration)); 709 FindRegistrationForIdOnly(kRegistrationId, &found_registration));
696 EXPECT_FALSE(found_registration.get()); 710 EXPECT_FALSE(found_registration.get());
697 711
698 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 712 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
699 FindRegistrationForDocument(kDocumentUrl, &found_registration)); 713 FindRegistrationForDocument(kDocumentUrl, &found_registration));
700 EXPECT_FALSE(found_registration.get()); 714 EXPECT_FALSE(found_registration.get());
701 715
702 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 716 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
703 FindRegistrationForPattern(kScope, &found_registration)); 717 FindRegistrationForPattern(kScope, &found_registration));
704 EXPECT_FALSE(found_registration.get()); 718 EXPECT_FALSE(found_registration.get());
705 719
706 std::vector<ServiceWorkerRegistrationInfo> all_registrations; 720 std::vector<ServiceWorkerRegistrationInfo> all_registrations;
707 GetAllRegistrations(&all_registrations); 721 GetAllRegistrationsInfos(&all_registrations);
708 EXPECT_TRUE(all_registrations.empty()); 722 EXPECT_TRUE(all_registrations.empty());
709 723
710 std::vector<ServiceWorkerRegistrationInfo> registrations_origin; 724 std::vector<scoped_refptr<ServiceWorkerRegistration>>
711 GetRegistrationsForOrigin(kScope.GetOrigin(), &registrations_origin); 725 registrations_for_origin;
712 EXPECT_TRUE(registrations_origin.empty()); 726 GetRegistrationsForOrigin(kScope.GetOrigin(), &registrations_for_origin);
727 EXPECT_TRUE(registrations_for_origin.empty());
713 728
714 GetRegistrationsForOrigin( 729 GetRegistrationsForOrigin(GURL("http://example.com/").GetOrigin(),
715 GURL("http://example.com/").GetOrigin(), 730 &registrations_for_origin);
716 &registrations_origin); 731 EXPECT_TRUE(registrations_for_origin.empty());
717 EXPECT_TRUE(registrations_origin.empty());
718 732
719 // Notify storage of it being installed. 733 // Notify storage of it being installed.
720 storage()->NotifyInstallingRegistration(live_registration.get()); 734 storage()->NotifyInstallingRegistration(live_registration.get());
721 735
722 // Now should be findable. 736 // Now should be findable.
723 EXPECT_EQ(SERVICE_WORKER_OK, 737 EXPECT_EQ(SERVICE_WORKER_OK,
724 FindRegistrationForId( 738 FindRegistrationForId(
725 kRegistrationId, kScope.GetOrigin(), &found_registration)); 739 kRegistrationId, kScope.GetOrigin(), &found_registration));
726 EXPECT_EQ(live_registration, found_registration); 740 EXPECT_EQ(live_registration, found_registration);
727 found_registration = NULL; 741 found_registration = NULL;
728 742
729 EXPECT_EQ(SERVICE_WORKER_OK, 743 EXPECT_EQ(SERVICE_WORKER_OK,
730 FindRegistrationForIdOnly(kRegistrationId, &found_registration)); 744 FindRegistrationForIdOnly(kRegistrationId, &found_registration));
731 EXPECT_EQ(live_registration, found_registration); 745 EXPECT_EQ(live_registration, found_registration);
732 found_registration = NULL; 746 found_registration = NULL;
733 747
734 EXPECT_EQ(SERVICE_WORKER_OK, 748 EXPECT_EQ(SERVICE_WORKER_OK,
735 FindRegistrationForDocument(kDocumentUrl, &found_registration)); 749 FindRegistrationForDocument(kDocumentUrl, &found_registration));
736 EXPECT_EQ(live_registration, found_registration); 750 EXPECT_EQ(live_registration, found_registration);
737 found_registration = NULL; 751 found_registration = NULL;
738 752
739 EXPECT_EQ(SERVICE_WORKER_OK, 753 EXPECT_EQ(SERVICE_WORKER_OK,
740 FindRegistrationForPattern(kScope, &found_registration)); 754 FindRegistrationForPattern(kScope, &found_registration));
741 EXPECT_EQ(live_registration, found_registration); 755 EXPECT_EQ(live_registration, found_registration);
742 found_registration = NULL; 756 found_registration = NULL;
743 757
744 GetAllRegistrations(&all_registrations); 758 GetAllRegistrationsInfos(&all_registrations);
745 EXPECT_EQ(1u, all_registrations.size()); 759 EXPECT_EQ(1u, all_registrations.size());
746 all_registrations.clear(); 760 all_registrations.clear();
747 761
748 // Finding by origin should provide the same result iif origin is kScope. 762 // Finding by origin should provide the same result if origin is kScope.
749 GetRegistrationsForOrigin(kScope.GetOrigin(), &registrations_origin); 763 GetRegistrationsForOrigin(kScope.GetOrigin(), &registrations_for_origin);
750 EXPECT_EQ(1u, registrations_origin.size()); 764 EXPECT_EQ(1u, registrations_for_origin.size());
751 registrations_origin.clear(); 765 registrations_for_origin.clear();
752 766
753 GetRegistrationsForOrigin( 767 GetRegistrationsForOrigin(GURL("http://example.com/").GetOrigin(),
754 GURL("http://example.com/").GetOrigin(), 768 &registrations_for_origin);
755 &registrations_origin); 769 EXPECT_TRUE(registrations_for_origin.empty());
756 EXPECT_TRUE(registrations_origin.empty());
757 770
758 // Notify storage of installation no longer happening. 771 // Notify storage of installation no longer happening.
759 storage()->NotifyDoneInstallingRegistration( 772 storage()->NotifyDoneInstallingRegistration(
760 live_registration.get(), NULL, SERVICE_WORKER_OK); 773 live_registration.get(), NULL, SERVICE_WORKER_OK);
761 774
762 // Once again, should not be findable. 775 // Once again, should not be findable.
763 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 776 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
764 FindRegistrationForId( 777 FindRegistrationForId(
765 kRegistrationId, kScope.GetOrigin(), &found_registration)); 778 kRegistrationId, kScope.GetOrigin(), &found_registration));
766 EXPECT_FALSE(found_registration.get()); 779 EXPECT_FALSE(found_registration.get());
767 780
768 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 781 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
769 FindRegistrationForIdOnly(kRegistrationId, &found_registration)); 782 FindRegistrationForIdOnly(kRegistrationId, &found_registration));
770 EXPECT_FALSE(found_registration.get()); 783 EXPECT_FALSE(found_registration.get());
771 784
772 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 785 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
773 FindRegistrationForDocument(kDocumentUrl, &found_registration)); 786 FindRegistrationForDocument(kDocumentUrl, &found_registration));
774 EXPECT_FALSE(found_registration.get()); 787 EXPECT_FALSE(found_registration.get());
775 788
776 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 789 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
777 FindRegistrationForPattern(kScope, &found_registration)); 790 FindRegistrationForPattern(kScope, &found_registration));
778 EXPECT_FALSE(found_registration.get()); 791 EXPECT_FALSE(found_registration.get());
779 792
780 GetAllRegistrations(&all_registrations); 793 GetAllRegistrationsInfos(&all_registrations);
781 EXPECT_TRUE(all_registrations.empty()); 794 EXPECT_TRUE(all_registrations.empty());
782 795
783 GetRegistrationsForOrigin(kScope.GetOrigin(), &registrations_origin); 796 GetRegistrationsForOrigin(kScope.GetOrigin(), &registrations_for_origin);
784 EXPECT_TRUE(registrations_origin.empty()); 797 EXPECT_TRUE(registrations_for_origin.empty());
785 798
786 GetRegistrationsForOrigin( 799 GetRegistrationsForOrigin(GURL("http://example.com/").GetOrigin(),
787 GURL("http://example.com/").GetOrigin(), 800 &registrations_for_origin);
788 &registrations_origin); 801 EXPECT_TRUE(registrations_for_origin.empty());
789 EXPECT_TRUE(registrations_origin.empty());
790 } 802 }
791 803
792 TEST_F(ServiceWorkerStorageTest, StoreUserData) { 804 TEST_F(ServiceWorkerStorageTest, StoreUserData) {
793 const GURL kScope("http://www.test.not/scope/"); 805 const GURL kScope("http://www.test.not/scope/");
794 const GURL kScript("http://www.test.not/script.js"); 806 const GURL kScript("http://www.test.not/script.js");
795 const int64 kRegistrationId = 0; 807 const int64 kRegistrationId = 0;
796 const int64 kVersionId = 0; 808 const int64 kVersionId = 0;
797 809
798 LazyInitialize(); 810 LazyInitialize();
799 811
(...skipping 649 matching lines...) Expand 10 before | Expand all | Expand 10 after
1449 are_equal = true; 1461 are_equal = true;
1450 storage()->CompareScriptResources( 1462 storage()->CompareScriptResources(
1451 5, 6, 1463 5, 6,
1452 base::Bind(&OnCompareComplete, &status, &are_equal)); 1464 base::Bind(&OnCompareComplete, &status, &are_equal));
1453 base::RunLoop().RunUntilIdle(); 1465 base::RunLoop().RunUntilIdle();
1454 EXPECT_EQ(SERVICE_WORKER_OK, status); 1466 EXPECT_EQ(SERVICE_WORKER_OK, status);
1455 EXPECT_FALSE(are_equal); 1467 EXPECT_FALSE(are_equal);
1456 } 1468 }
1457 1469
1458 } // namespace content 1470 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698