OLD | NEW |
---|---|
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" |
11 #include "content/browser/browser_thread_impl.h" | 11 #include "content/browser/browser_thread_impl.h" |
12 #include "content/browser/service_worker/service_worker_context_core.h" | 12 #include "content/browser/service_worker/service_worker_context_core.h" |
13 #include "content/browser/service_worker/service_worker_disk_cache.h" | 13 #include "content/browser/service_worker/service_worker_disk_cache.h" |
14 #include "content/browser/service_worker/service_worker_registration.h" | 14 #include "content/browser/service_worker/service_worker_registration.h" |
15 #include "content/browser/service_worker/service_worker_storage.h" | 15 #include "content/browser/service_worker/service_worker_storage.h" |
16 #include "content/browser/service_worker/service_worker_utils.h" | 16 #include "content/browser/service_worker/service_worker_utils.h" |
17 #include "content/browser/service_worker/service_worker_version.h" | 17 #include "content/browser/service_worker/service_worker_version.h" |
18 #include "content/common/service_worker/service_worker_status_code.h" | 18 #include "content/common/service_worker/service_worker_status_code.h" |
19 #include "content/public/test/mock_special_storage_policy.h" | |
19 #include "content/public/test/test_browser_thread_bundle.h" | 20 #include "content/public/test/test_browser_thread_bundle.h" |
20 #include "net/base/io_buffer.h" | 21 #include "net/base/io_buffer.h" |
21 #include "net/base/net_errors.h" | 22 #include "net/base/net_errors.h" |
22 #include "net/base/test_completion_callback.h" | 23 #include "net/base/test_completion_callback.h" |
23 #include "net/http/http_response_headers.h" | 24 #include "net/http/http_response_headers.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
25 | 26 |
26 using net::IOBuffer; | 27 using net::IOBuffer; |
27 using net::TestCompletionCallback; | 28 using net::TestCompletionCallback; |
28 using net::WrappedIOBuffer; | 29 using net::WrappedIOBuffer; |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
185 | 186 |
186 } // namespace | 187 } // namespace |
187 | 188 |
188 class ServiceWorkerStorageTest : public testing::Test { | 189 class ServiceWorkerStorageTest : public testing::Test { |
189 public: | 190 public: |
190 ServiceWorkerStorageTest() | 191 ServiceWorkerStorageTest() |
191 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) { | 192 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) { |
192 } | 193 } |
193 | 194 |
194 virtual void SetUp() OVERRIDE { | 195 virtual void SetUp() OVERRIDE { |
196 special_storage_policy_ = new MockSpecialStoragePolicy(); | |
195 context_.reset( | 197 context_.reset( |
196 new ServiceWorkerContextCore(GetUserDataDirectory(), | 198 new ServiceWorkerContextCore(GetUserDataDirectory(), |
197 base::ThreadTaskRunnerHandle::Get(), | 199 base::ThreadTaskRunnerHandle::Get(), |
198 base::ThreadTaskRunnerHandle::Get(), | 200 base::ThreadTaskRunnerHandle::Get(), |
199 base::ThreadTaskRunnerHandle::Get(), | 201 base::ThreadTaskRunnerHandle::Get(), |
200 NULL, | 202 NULL, |
203 special_storage_policy_.get(), | |
201 NULL, | 204 NULL, |
202 NULL)); | 205 NULL)); |
203 context_ptr_ = context_->AsWeakPtr(); | 206 context_ptr_ = context_->AsWeakPtr(); |
204 } | 207 } |
205 | 208 |
206 virtual void TearDown() OVERRIDE { | 209 virtual void TearDown() OVERRIDE { |
207 context_.reset(); | 210 context_.reset(); |
208 } | 211 } |
209 | 212 |
210 virtual base::FilePath GetUserDataDirectory() { return base::FilePath(); } | 213 virtual base::FilePath GetUserDataDirectory() { return base::FilePath(); } |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
313 storage()->FindRegistrationForId( | 316 storage()->FindRegistrationForId( |
314 registration_id, origin, | 317 registration_id, origin, |
315 MakeFindCallback(&was_called, &result, registration)); | 318 MakeFindCallback(&was_called, &result, registration)); |
316 base::RunLoop().RunUntilIdle(); | 319 base::RunLoop().RunUntilIdle(); |
317 EXPECT_TRUE(was_called); | 320 EXPECT_TRUE(was_called); |
318 return result; | 321 return result; |
319 } | 322 } |
320 | 323 |
321 scoped_ptr<ServiceWorkerContextCore> context_; | 324 scoped_ptr<ServiceWorkerContextCore> context_; |
322 base::WeakPtr<ServiceWorkerContextCore> context_ptr_; | 325 base::WeakPtr<ServiceWorkerContextCore> context_ptr_; |
326 scoped_refptr<MockSpecialStoragePolicy> special_storage_policy_; | |
323 TestBrowserThreadBundle browser_thread_bundle_; | 327 TestBrowserThreadBundle browser_thread_bundle_; |
324 }; | 328 }; |
325 | 329 |
326 TEST_F(ServiceWorkerStorageTest, StoreFindUpdateDeleteRegistration) { | 330 TEST_F(ServiceWorkerStorageTest, StoreFindUpdateDeleteRegistration) { |
327 const GURL kScope("http://www.test.not/scope/"); | 331 const GURL kScope("http://www.test.not/scope/"); |
328 const GURL kScript("http://www.test.not/script.js"); | 332 const GURL kScript("http://www.test.not/script.js"); |
329 const GURL kDocumentUrl("http://www.test.not/scope/document.html"); | 333 const GURL kDocumentUrl("http://www.test.not/scope/document.html"); |
330 const int64 kRegistrationId = 0; | 334 const int64 kRegistrationId = 0; |
331 const int64 kVersionId = 0; | 335 const int64 kVersionId = 0; |
332 const base::Time kToday = base::Time::Now(); | 336 const base::Time kToday = base::Time::Now(); |
(...skipping 476 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
809 | 813 |
810 // Simulate browser shutdown. The purgeable and uncommitted resources are now | 814 // Simulate browser shutdown. The purgeable and uncommitted resources are now |
811 // stale. | 815 // stale. |
812 context_.reset(); | 816 context_.reset(); |
813 context_.reset( | 817 context_.reset( |
814 new ServiceWorkerContextCore(GetUserDataDirectory(), | 818 new ServiceWorkerContextCore(GetUserDataDirectory(), |
815 base::ThreadTaskRunnerHandle::Get(), | 819 base::ThreadTaskRunnerHandle::Get(), |
816 base::ThreadTaskRunnerHandle::Get(), | 820 base::ThreadTaskRunnerHandle::Get(), |
817 base::ThreadTaskRunnerHandle::Get(), | 821 base::ThreadTaskRunnerHandle::Get(), |
818 NULL, | 822 NULL, |
823 special_storage_policy_.get(), | |
819 NULL, | 824 NULL, |
820 NULL)); | 825 NULL)); |
821 storage()->LazyInitialize(base::Bind(&base::DoNothing)); | 826 storage()->LazyInitialize(base::Bind(&base::DoNothing)); |
822 base::RunLoop().RunUntilIdle(); | 827 base::RunLoop().RunUntilIdle(); |
823 | 828 |
824 // Store a new uncommitted resource. This triggers stale resource cleanup. | 829 // Store a new uncommitted resource. This triggers stale resource cleanup. |
825 int64 kNewResourceId = storage()->NewResourceId(); | 830 int64 kNewResourceId = storage()->NewResourceId(); |
826 WriteBasicResponse(storage(), kNewResourceId); | 831 WriteBasicResponse(storage(), kNewResourceId); |
827 storage()->StoreUncommittedResponseId(kNewResourceId); | 832 storage()->StoreUncommittedResponseId(kNewResourceId); |
828 base::RunLoop().RunUntilIdle(); | 833 base::RunLoop().RunUntilIdle(); |
829 | 834 |
830 // The stale resources should be purged, but the new resource should persist. | 835 // The stale resources should be purged, but the new resource should persist. |
831 verify_ids.clear(); | 836 verify_ids.clear(); |
832 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, | 837 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, |
833 storage()->database_->GetUncommittedResourceIds(&verify_ids)); | 838 storage()->database_->GetUncommittedResourceIds(&verify_ids)); |
834 ASSERT_EQ(1u, verify_ids.size()); | 839 ASSERT_EQ(1u, verify_ids.size()); |
835 EXPECT_EQ(kNewResourceId, *verify_ids.begin()); | 840 EXPECT_EQ(kNewResourceId, *verify_ids.begin()); |
836 | 841 |
837 verify_ids.clear(); | 842 verify_ids.clear(); |
838 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, | 843 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, |
839 storage()->database_->GetPurgeableResourceIds(&verify_ids)); | 844 storage()->database_->GetPurgeableResourceIds(&verify_ids)); |
840 EXPECT_TRUE(verify_ids.empty()); | 845 EXPECT_TRUE(verify_ids.empty()); |
841 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id1_, false)); | 846 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id1_, false)); |
842 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id2_, false)); | 847 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id2_, false)); |
843 EXPECT_FALSE( | 848 EXPECT_FALSE( |
844 VerifyBasicResponse(storage(), kStaleUncommittedResourceId, false)); | 849 VerifyBasicResponse(storage(), kStaleUncommittedResourceId, false)); |
845 EXPECT_TRUE(VerifyBasicResponse(storage(), kNewResourceId, true)); | 850 EXPECT_TRUE(VerifyBasicResponse(storage(), kNewResourceId, true)); |
846 } | 851 } |
847 | 852 |
853 TEST_F(ServiceWorkerResourceStorageDiskTest, ClearOnExit) { | |
854 // Store a new registration. | |
855 GURL cleared_scope("http://www.test.clear/scope/"); | |
856 GURL cleared_script("http://www.test.clear/script.js"); | |
857 int64 cleared_registration_id = storage()->NewRegistrationId(); | |
858 int64 cleared_version_id = storage()->NewVersionId(); | |
859 | |
860 RegistrationData data; | |
861 data.registration_id = cleared_registration_id; | |
862 data.scope = cleared_scope; | |
863 data.script = cleared_script; | |
864 data.version_id = cleared_version_id; | |
865 data.is_active = false; | |
866 | |
867 scoped_refptr<ServiceWorkerRegistration> cleared_registration = | |
868 storage()->GetOrCreateRegistration(data, std::vector<ResourceRecord>()); | |
869 cleared_registration->waiting_version()->SetStatus(ServiceWorkerVersion::NEW); | |
michaeln1
2014/10/08 19:28:57
to more closely mimic actual usage, i think the wa
| |
870 EXPECT_EQ(SERVICE_WORKER_OK, | |
871 StoreRegistration(cleared_registration, | |
872 cleared_registration->waiting_version())); | |
873 | |
874 // Set its origin as session-only. | |
875 special_storage_policy_->AddSessionOnly(cleared_scope.GetOrigin()); | |
876 | |
877 // Simulate browser restart. | |
878 context_.reset(); | |
879 context_.reset( | |
880 new ServiceWorkerContextCore(GetUserDataDirectory(), | |
881 base::ThreadTaskRunnerHandle::Get(), | |
882 base::ThreadTaskRunnerHandle::Get(), | |
883 base::ThreadTaskRunnerHandle::Get(), | |
884 NULL, | |
885 special_storage_policy_.get(), | |
886 NULL, | |
887 NULL)); | |
888 storage()->LazyInitialize(base::Bind(&base::DoNothing)); | |
889 base::RunLoop().RunUntilIdle(); | |
890 | |
891 // Verify that only the session-only origin is gone. | |
892 scoped_refptr<ServiceWorkerRegistration> found_registration; | |
893 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, | |
894 FindRegistrationForId(cleared_registration_id, | |
michaeln1
2014/10/08 19:28:57
maybe test that it actually can be found prior to
| |
895 cleared_scope.GetOrigin(), | |
896 &found_registration)); | |
897 EXPECT_EQ(SERVICE_WORKER_OK, | |
898 FindRegistrationForId( | |
899 registration_id_, scope_.GetOrigin(), &found_registration)); | |
900 } | |
901 | |
848 TEST_F(ServiceWorkerResourceStorageTest, UpdateRegistration) { | 902 TEST_F(ServiceWorkerResourceStorageTest, UpdateRegistration) { |
849 // Promote the worker to active worker and add a controllee. | 903 // Promote the worker to active worker and add a controllee. |
850 registration_->SetActiveVersion(registration_->waiting_version()); | 904 registration_->SetActiveVersion(registration_->waiting_version()); |
851 storage()->UpdateToActiveState( | 905 storage()->UpdateToActiveState( |
852 registration_.get(), base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 906 registration_.get(), base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
853 scoped_ptr<ServiceWorkerProviderHost> host( | 907 scoped_ptr<ServiceWorkerProviderHost> host( |
854 new ServiceWorkerProviderHost(33 /* dummy render process id */, | 908 new ServiceWorkerProviderHost(33 /* dummy render process id */, |
855 1 /* dummy provider_id */, | 909 1 /* dummy provider_id */, |
856 context_->AsWeakPtr(), | 910 context_->AsWeakPtr(), |
857 NULL)); | 911 NULL)); |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1038 are_equal = true; | 1092 are_equal = true; |
1039 storage()->CompareScriptResources( | 1093 storage()->CompareScriptResources( |
1040 5, 6, | 1094 5, 6, |
1041 base::Bind(&OnCompareComplete, &status, &are_equal)); | 1095 base::Bind(&OnCompareComplete, &status, &are_equal)); |
1042 base::RunLoop().RunUntilIdle(); | 1096 base::RunLoop().RunUntilIdle(); |
1043 EXPECT_EQ(SERVICE_WORKER_OK, status); | 1097 EXPECT_EQ(SERVICE_WORKER_OK, status); |
1044 EXPECT_FALSE(are_equal); | 1098 EXPECT_FALSE(are_equal); |
1045 } | 1099 } |
1046 | 1100 |
1047 } // namespace content | 1101 } // namespace content |
OLD | NEW |