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" |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 *was_called = true; | 72 *was_called = true; |
73 *all_out = all; | 73 *all_out = all; |
74 } | 74 } |
75 | 75 |
76 ServiceWorkerStorage::GetAllRegistrationInfosCallback MakeGetAllCallback( | 76 ServiceWorkerStorage::GetAllRegistrationInfosCallback MakeGetAllCallback( |
77 bool* was_called, | 77 bool* was_called, |
78 std::vector<ServiceWorkerRegistrationInfo>* all) { | 78 std::vector<ServiceWorkerRegistrationInfo>* all) { |
79 return base::Bind(&GetAllCallback, was_called, all); | 79 return base::Bind(&GetAllCallback, was_called, all); |
80 } | 80 } |
81 | 81 |
82 void OnIOComplete(int* rv_out, int rv) { | |
83 *rv_out = rv; | |
84 } | |
85 | |
86 void OnCompareComplete( | 82 void OnCompareComplete( |
87 ServiceWorkerStatusCode* status_out, bool* are_equal_out, | 83 ServiceWorkerStatusCode* status_out, bool* are_equal_out, |
88 ServiceWorkerStatusCode status, bool are_equal) { | 84 ServiceWorkerStatusCode status, bool are_equal) { |
89 *status_out = status; | 85 *status_out = status; |
90 *are_equal_out = are_equal; | 86 *are_equal_out = are_equal; |
91 } | 87 } |
92 | 88 |
93 void WriteResponse( | 89 void WriteResponse( |
94 ServiceWorkerStorage* storage, int64 id, | 90 ServiceWorkerStorage* storage, int64 id, |
95 const std::string& headers, | 91 const std::string& headers, |
96 IOBuffer* body, int length) { | 92 IOBuffer* body, int length) { |
97 scoped_ptr<ServiceWorkerResponseWriter> writer = | 93 scoped_ptr<ServiceWorkerResponseWriter> writer = |
98 storage->CreateResponseWriter(id); | 94 storage->CreateResponseWriter(id); |
99 | 95 |
100 scoped_ptr<net::HttpResponseInfo> info(new net::HttpResponseInfo); | 96 scoped_ptr<net::HttpResponseInfo> info(new net::HttpResponseInfo); |
101 info->request_time = base::Time::Now(); | 97 info->request_time = base::Time::Now(); |
102 info->response_time = base::Time::Now(); | 98 info->response_time = base::Time::Now(); |
103 info->was_cached = false; | 99 info->was_cached = false; |
104 info->headers = new net::HttpResponseHeaders(headers); | 100 info->headers = new net::HttpResponseHeaders(headers); |
105 scoped_refptr<HttpResponseInfoIOBuffer> info_buffer = | 101 scoped_refptr<HttpResponseInfoIOBuffer> info_buffer = |
106 new HttpResponseInfoIOBuffer(info.release()); | 102 new HttpResponseInfoIOBuffer(info.release()); |
107 | 103 { |
108 int rv = -1234; | 104 TestCompletionCallback cb; |
109 writer->WriteInfo(info_buffer.get(), base::Bind(&OnIOComplete, &rv)); | 105 writer->WriteInfo(info_buffer.get(), cb.callback()); |
110 base::RunLoop().RunUntilIdle(); | 106 int rv = cb.WaitForResult(); |
111 EXPECT_LT(0, rv); | 107 EXPECT_LT(0, rv); |
112 | 108 } |
113 rv = -1234; | 109 { |
114 writer->WriteData(body, length, base::Bind(&OnIOComplete, &rv)); | 110 TestCompletionCallback cb; |
115 base::RunLoop().RunUntilIdle(); | 111 writer->WriteData(body, length, cb.callback()); |
116 EXPECT_EQ(length, rv); | 112 int rv = cb.WaitForResult(); |
| 113 EXPECT_EQ(length, rv); |
| 114 } |
117 } | 115 } |
118 | 116 |
119 void WriteStringResponse( | 117 void WriteStringResponse( |
120 ServiceWorkerStorage* storage, int64 id, | 118 ServiceWorkerStorage* storage, int64 id, |
121 const std::string& headers, | 119 const std::string& headers, |
122 const std::string& body) { | 120 const std::string& body) { |
123 scoped_refptr<IOBuffer> body_buffer(new WrappedIOBuffer(body.data())); | 121 scoped_refptr<IOBuffer> body_buffer(new WrappedIOBuffer(body.data())); |
124 WriteResponse(storage, id, headers, body_buffer.get(), body.length()); | 122 WriteResponse(storage, id, headers, body_buffer.get(), body.length()); |
125 } | 123 } |
126 | 124 |
(...skipping 616 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
743 base::RunLoop().RunUntilIdle(); | 741 base::RunLoop().RunUntilIdle(); |
744 verify_ids.clear(); | 742 verify_ids.clear(); |
745 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, | 743 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, |
746 storage()->database_->GetPurgeableResourceIds(&verify_ids)); | 744 storage()->database_->GetPurgeableResourceIds(&verify_ids)); |
747 EXPECT_TRUE(verify_ids.empty()); | 745 EXPECT_TRUE(verify_ids.empty()); |
748 | 746 |
749 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id1_, false)); | 747 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id1_, false)); |
750 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id2_, false)); | 748 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id2_, false)); |
751 } | 749 } |
752 | 750 |
753 // Android has flaky IO error: http://crbug.com/387045 | 751 TEST_F(ServiceWorkerResourceStorageDiskTest, CleanupOnRestart) { |
754 #if defined(OS_ANDROID) | |
755 #define MAYBE_CleanupOnRestart DISABLED_CleanupOnRestart | |
756 #else | |
757 #define MAYBE_CleanupOnRestart CleanupOnRestart | |
758 #endif | |
759 TEST_F(ServiceWorkerResourceStorageDiskTest, MAYBE_CleanupOnRestart) { | |
760 // Promote the worker to active and add a controllee. | 752 // Promote the worker to active and add a controllee. |
761 registration_->SetActiveVersion(registration_->waiting_version()); | 753 registration_->SetActiveVersion(registration_->waiting_version()); |
762 registration_->SetWaitingVersion(NULL); | 754 registration_->SetWaitingVersion(NULL); |
763 storage()->UpdateToActiveState( | 755 storage()->UpdateToActiveState( |
764 registration_.get(), base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 756 registration_.get(), base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
765 scoped_ptr<ServiceWorkerProviderHost> host( | 757 scoped_ptr<ServiceWorkerProviderHost> host( |
766 new ServiceWorkerProviderHost(33 /* dummy render process id */, | 758 new ServiceWorkerProviderHost(33 /* dummy render process id */, |
767 1 /* dummy provider_id */, | 759 1 /* dummy provider_id */, |
768 context_->AsWeakPtr(), | 760 context_->AsWeakPtr(), |
769 NULL)); | 761 NULL)); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
827 storage()->StoreUncommittedResponseId(kNewResourceId); | 819 storage()->StoreUncommittedResponseId(kNewResourceId); |
828 base::RunLoop().RunUntilIdle(); | 820 base::RunLoop().RunUntilIdle(); |
829 | 821 |
830 // The stale resources should be purged, but the new resource should persist. | 822 // The stale resources should be purged, but the new resource should persist. |
831 verify_ids.clear(); | 823 verify_ids.clear(); |
832 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, | 824 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, |
833 storage()->database_->GetUncommittedResourceIds(&verify_ids)); | 825 storage()->database_->GetUncommittedResourceIds(&verify_ids)); |
834 ASSERT_EQ(1u, verify_ids.size()); | 826 ASSERT_EQ(1u, verify_ids.size()); |
835 EXPECT_EQ(kNewResourceId, *verify_ids.begin()); | 827 EXPECT_EQ(kNewResourceId, *verify_ids.begin()); |
836 | 828 |
| 829 // Purging resources needs interactions with SimpleCache's worker thread, |
| 830 // so single RunUntilIdle() call may not be sufficient. |
| 831 while (storage()->is_purge_pending_) |
| 832 base::RunLoop().RunUntilIdle(); |
| 833 |
837 verify_ids.clear(); | 834 verify_ids.clear(); |
838 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, | 835 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, |
839 storage()->database_->GetPurgeableResourceIds(&verify_ids)); | 836 storage()->database_->GetPurgeableResourceIds(&verify_ids)); |
840 EXPECT_TRUE(verify_ids.empty()); | 837 EXPECT_TRUE(verify_ids.empty()); |
841 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id1_, false)); | 838 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id1_, false)); |
842 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id2_, false)); | 839 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id2_, false)); |
843 EXPECT_FALSE( | 840 EXPECT_FALSE( |
844 VerifyBasicResponse(storage(), kStaleUncommittedResourceId, false)); | 841 VerifyBasicResponse(storage(), kStaleUncommittedResourceId, false)); |
845 EXPECT_TRUE(VerifyBasicResponse(storage(), kNewResourceId, true)); | 842 EXPECT_TRUE(VerifyBasicResponse(storage(), kNewResourceId, true)); |
846 } | 843 } |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1038 are_equal = true; | 1035 are_equal = true; |
1039 storage()->CompareScriptResources( | 1036 storage()->CompareScriptResources( |
1040 5, 6, | 1037 5, 6, |
1041 base::Bind(&OnCompareComplete, &status, &are_equal)); | 1038 base::Bind(&OnCompareComplete, &status, &are_equal)); |
1042 base::RunLoop().RunUntilIdle(); | 1039 base::RunLoop().RunUntilIdle(); |
1043 EXPECT_EQ(SERVICE_WORKER_OK, status); | 1040 EXPECT_EQ(SERVICE_WORKER_OK, status); |
1044 EXPECT_FALSE(are_equal); | 1041 EXPECT_FALSE(are_equal); |
1045 } | 1042 } |
1046 | 1043 |
1047 } // namespace content | 1044 } // namespace content |
OLD | NEW |