OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/browser/cache_storage/cache_storage_manager.h" | 5 #include "content/browser/cache_storage/cache_storage_manager.h" |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
10 #include "base/message_loop/message_loop_proxy.h" | 10 #include "base/message_loop/message_loop_proxy.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
23 | 23 |
24 namespace content { | 24 namespace content { |
25 | 25 |
26 class CacheStorageManagerTest : public testing::Test { | 26 class CacheStorageManagerTest : public testing::Test { |
27 public: | 27 public: |
28 CacheStorageManagerTest() | 28 CacheStorageManagerTest() |
29 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), | 29 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), |
30 callback_bool_(false), | 30 callback_bool_(false), |
31 callback_error_(CACHE_STORAGE_OK), | 31 callback_error_(CACHE_STORAGE_OK), |
32 callback_cache_error_(CACHE_STORAGE_OK), | |
33 origin1_("http://example1.com"), | 32 origin1_("http://example1.com"), |
34 origin2_("http://example2.com") {} | 33 origin2_("http://example2.com") {} |
35 | 34 |
36 void SetUp() override { | 35 void SetUp() override { |
37 ChromeBlobStorageContext* blob_storage_context( | 36 ChromeBlobStorageContext* blob_storage_context( |
38 ChromeBlobStorageContext::GetFor(&browser_context_)); | 37 ChromeBlobStorageContext::GetFor(&browser_context_)); |
39 // Wait for ChromeBlobStorageContext to finish initializing. | 38 // Wait for ChromeBlobStorageContext to finish initializing. |
40 base::RunLoop().RunUntilIdle(); | 39 base::RunLoop().RunUntilIdle(); |
41 | 40 |
42 quota_manager_proxy_ = new MockQuotaManagerProxy( | 41 quota_manager_proxy_ = new MockQuotaManagerProxy( |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 | 83 |
85 void StringsAndErrorCallback(base::RunLoop* run_loop, | 84 void StringsAndErrorCallback(base::RunLoop* run_loop, |
86 const std::vector<std::string>& strings, | 85 const std::vector<std::string>& strings, |
87 CacheStorageError error) { | 86 CacheStorageError error) { |
88 callback_strings_ = strings; | 87 callback_strings_ = strings; |
89 callback_error_ = error; | 88 callback_error_ = error; |
90 run_loop->Quit(); | 89 run_loop->Quit(); |
91 } | 90 } |
92 | 91 |
93 void CachePutCallback(base::RunLoop* run_loop, CacheStorageError error) { | 92 void CachePutCallback(base::RunLoop* run_loop, CacheStorageError error) { |
94 callback_cache_error_ = error; | 93 callback_error_ = error; |
95 run_loop->Quit(); | 94 run_loop->Quit(); |
96 } | 95 } |
97 | 96 |
98 void CacheMatchCallback( | 97 void CacheMatchCallback( |
99 base::RunLoop* run_loop, | 98 base::RunLoop* run_loop, |
100 CacheStorageError error, | 99 CacheStorageError error, |
101 scoped_ptr<ServiceWorkerResponse> response, | 100 scoped_ptr<ServiceWorkerResponse> response, |
102 scoped_ptr<storage::BlobDataHandle> blob_data_handle) { | 101 scoped_ptr<storage::BlobDataHandle> blob_data_handle) { |
103 callback_cache_error_ = error; | 102 callback_error_ = error; |
104 callback_cache_response_ = response.Pass(); | 103 callback_cache_response_ = response.Pass(); |
105 // Deliberately drop the data handle as only the url is being tested. | 104 // Deliberately drop the data handle as only the url is being tested. |
106 run_loop->Quit(); | 105 run_loop->Quit(); |
107 } | 106 } |
108 | 107 |
109 bool Open(const GURL& origin, const std::string& cache_name) { | 108 bool Open(const GURL& origin, const std::string& cache_name) { |
110 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | 109 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); |
111 cache_manager_->OpenCache( | 110 cache_manager_->OpenCache( |
112 origin, cache_name, | 111 origin, cache_name, |
113 base::Bind(&CacheStorageManagerTest::CacheAndErrorCallback, | 112 base::Bind(&CacheStorageManagerTest::CacheAndErrorCallback, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 } | 144 } |
146 | 145 |
147 bool Keys(const GURL& origin) { | 146 bool Keys(const GURL& origin) { |
148 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | 147 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); |
149 cache_manager_->EnumerateCaches( | 148 cache_manager_->EnumerateCaches( |
150 origin, | 149 origin, |
151 base::Bind(&CacheStorageManagerTest::StringsAndErrorCallback, | 150 base::Bind(&CacheStorageManagerTest::StringsAndErrorCallback, |
152 base::Unretained(this), base::Unretained(loop.get()))); | 151 base::Unretained(this), base::Unretained(loop.get()))); |
153 loop->Run(); | 152 loop->Run(); |
154 | 153 |
155 bool error = callback_error_ != CACHE_STORAGE_OK; | 154 return callback_error_ == CACHE_STORAGE_OK; |
156 return !error; | |
157 } | 155 } |
158 | 156 |
159 bool StorageMatch(const GURL& origin, | 157 bool StorageMatch(const GURL& origin, |
160 const std::string& cache_name, | 158 const std::string& cache_name, |
161 const GURL& url) { | 159 const GURL& url) { |
162 scoped_ptr<ServiceWorkerFetchRequest> request( | 160 scoped_ptr<ServiceWorkerFetchRequest> request( |
163 new ServiceWorkerFetchRequest()); | 161 new ServiceWorkerFetchRequest()); |
164 request->url = url; | 162 request->url = url; |
165 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | 163 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); |
166 cache_manager_->MatchCache( | 164 cache_manager_->MatchCache( |
167 origin, cache_name, request.Pass(), | 165 origin, cache_name, request.Pass(), |
168 base::Bind(&CacheStorageManagerTest::CacheMatchCallback, | 166 base::Bind(&CacheStorageManagerTest::CacheMatchCallback, |
169 base::Unretained(this), base::Unretained(loop.get()))); | 167 base::Unretained(this), base::Unretained(loop.get()))); |
170 loop->Run(); | 168 loop->Run(); |
171 | 169 |
172 bool error = callback_cache_error_ != CACHE_STORAGE_OK; | 170 return callback_error_ == CACHE_STORAGE_OK; |
173 return !error; | |
174 } | 171 } |
175 | 172 |
176 bool StorageMatchAll(const GURL& origin, const GURL& url) { | 173 bool StorageMatchAll(const GURL& origin, const GURL& url) { |
177 scoped_ptr<ServiceWorkerFetchRequest> request( | 174 scoped_ptr<ServiceWorkerFetchRequest> request( |
178 new ServiceWorkerFetchRequest()); | 175 new ServiceWorkerFetchRequest()); |
179 request->url = url; | 176 request->url = url; |
180 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | 177 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); |
181 cache_manager_->MatchAllCaches( | 178 cache_manager_->MatchAllCaches( |
182 origin, request.Pass(), | 179 origin, request.Pass(), |
183 base::Bind(&CacheStorageManagerTest::CacheMatchCallback, | 180 base::Bind(&CacheStorageManagerTest::CacheMatchCallback, |
184 base::Unretained(this), base::Unretained(loop.get()))); | 181 base::Unretained(this), base::Unretained(loop.get()))); |
185 loop->Run(); | 182 loop->Run(); |
186 | 183 |
187 bool error = callback_cache_error_ != CACHE_STORAGE_OK; | 184 return callback_error_ == CACHE_STORAGE_OK; |
188 return !error; | |
189 } | 185 } |
190 | 186 |
191 bool CachePut(const scoped_refptr<CacheStorageCache>& cache, | 187 bool CachePut(const scoped_refptr<CacheStorageCache>& cache, |
192 const GURL& url) { | 188 const GURL& url) { |
193 ServiceWorkerFetchRequest request; | 189 ServiceWorkerFetchRequest request; |
194 ServiceWorkerResponse response; | 190 ServiceWorkerResponse response; |
195 request.url = url; | 191 request.url = url; |
196 response.url = url; | 192 response.url = url; |
197 | 193 |
198 CacheStorageBatchOperation operation; | 194 CacheStorageBatchOperation operation; |
199 operation.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; | 195 operation.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; |
200 operation.request = request; | 196 operation.request = request; |
201 operation.response = response; | 197 operation.response = response; |
202 | 198 |
203 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | 199 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); |
204 cache->BatchOperation( | 200 cache->BatchOperation( |
205 std::vector<CacheStorageBatchOperation>(1, operation), | 201 std::vector<CacheStorageBatchOperation>(1, operation), |
206 base::Bind(&CacheStorageManagerTest::CachePutCallback, | 202 base::Bind(&CacheStorageManagerTest::CachePutCallback, |
207 base::Unretained(this), base::Unretained(loop.get()))); | 203 base::Unretained(this), base::Unretained(loop.get()))); |
208 loop->Run(); | 204 loop->Run(); |
209 | 205 |
210 bool error = callback_cache_error_ != CACHE_STORAGE_OK; | 206 return callback_error_ == CACHE_STORAGE_OK; |
211 return !error; | |
212 } | 207 } |
213 | 208 |
214 bool CacheMatch(const scoped_refptr<CacheStorageCache>& cache, | 209 bool CacheMatch(const scoped_refptr<CacheStorageCache>& cache, |
215 const GURL& url) { | 210 const GURL& url) { |
216 scoped_ptr<ServiceWorkerFetchRequest> request( | 211 scoped_ptr<ServiceWorkerFetchRequest> request( |
217 new ServiceWorkerFetchRequest()); | 212 new ServiceWorkerFetchRequest()); |
218 request->url = url; | 213 request->url = url; |
219 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | 214 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); |
220 cache->Match( | 215 cache->Match( |
221 request.Pass(), | 216 request.Pass(), |
222 base::Bind(&CacheStorageManagerTest::CacheMatchCallback, | 217 base::Bind(&CacheStorageManagerTest::CacheMatchCallback, |
223 base::Unretained(this), base::Unretained(loop.get()))); | 218 base::Unretained(this), base::Unretained(loop.get()))); |
224 loop->Run(); | 219 loop->Run(); |
225 | 220 |
226 bool error = callback_cache_error_ != CACHE_STORAGE_OK; | 221 return callback_error_ == CACHE_STORAGE_OK; |
227 return !error; | |
228 } | 222 } |
229 | 223 |
230 CacheStorage* CacheStorageForOrigin(const GURL& origin) { | 224 CacheStorage* CacheStorageForOrigin(const GURL& origin) { |
231 return cache_manager_->FindOrCreateCacheStorage(origin); | 225 return cache_manager_->FindOrCreateCacheStorage(origin); |
232 } | 226 } |
233 | 227 |
234 protected: | 228 protected: |
235 TestBrowserContext browser_context_; | 229 TestBrowserContext browser_context_; |
236 TestBrowserThreadBundle browser_thread_bundle_; | 230 TestBrowserThreadBundle browser_thread_bundle_; |
237 | 231 |
238 base::ScopedTempDir temp_dir_; | 232 base::ScopedTempDir temp_dir_; |
239 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; | 233 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; |
240 scoped_ptr<CacheStorageManager> cache_manager_; | 234 scoped_ptr<CacheStorageManager> cache_manager_; |
241 | 235 |
242 scoped_refptr<CacheStorageCache> callback_cache_; | 236 scoped_refptr<CacheStorageCache> callback_cache_; |
243 int callback_bool_; | 237 int callback_bool_; |
244 CacheStorageError callback_error_; | 238 CacheStorageError callback_error_; |
245 CacheStorageError callback_cache_error_; | |
246 scoped_ptr<ServiceWorkerResponse> callback_cache_response_; | 239 scoped_ptr<ServiceWorkerResponse> callback_cache_response_; |
247 std::vector<std::string> callback_strings_; | 240 std::vector<std::string> callback_strings_; |
248 | 241 |
249 const GURL origin1_; | 242 const GURL origin1_; |
250 const GURL origin2_; | 243 const GURL origin2_; |
251 | 244 |
252 private: | 245 private: |
253 DISALLOW_COPY_AND_ASSIGN(CacheStorageManagerTest); | 246 DISALLOW_COPY_AND_ASSIGN(CacheStorageManagerTest); |
254 }; | 247 }; |
255 | 248 |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 TEST_P(CacheStorageManagerTestP, StorageMatchEntryExists) { | 345 TEST_P(CacheStorageManagerTestP, StorageMatchEntryExists) { |
353 EXPECT_TRUE(Open(origin1_, "foo")); | 346 EXPECT_TRUE(Open(origin1_, "foo")); |
354 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo"))); | 347 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo"))); |
355 EXPECT_TRUE(StorageMatch(origin1_, "foo", GURL("http://example.com/foo"))); | 348 EXPECT_TRUE(StorageMatch(origin1_, "foo", GURL("http://example.com/foo"))); |
356 } | 349 } |
357 | 350 |
358 TEST_P(CacheStorageManagerTestP, StorageMatchNoEntry) { | 351 TEST_P(CacheStorageManagerTestP, StorageMatchNoEntry) { |
359 EXPECT_TRUE(Open(origin1_, "foo")); | 352 EXPECT_TRUE(Open(origin1_, "foo")); |
360 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo"))); | 353 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo"))); |
361 EXPECT_FALSE(StorageMatch(origin1_, "foo", GURL("http://example.com/bar"))); | 354 EXPECT_FALSE(StorageMatch(origin1_, "foo", GURL("http://example.com/bar"))); |
362 EXPECT_EQ(CACHE_STORAGE_ERROR_NOT_FOUND, callback_cache_error_); | 355 EXPECT_EQ(CACHE_STORAGE_ERROR_NOT_FOUND, callback_error_); |
363 } | 356 } |
364 | 357 |
365 TEST_P(CacheStorageManagerTestP, StorageMatchNoCache) { | 358 TEST_P(CacheStorageManagerTestP, StorageMatchNoCache) { |
366 EXPECT_TRUE(Open(origin1_, "foo")); | 359 EXPECT_TRUE(Open(origin1_, "foo")); |
367 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo"))); | 360 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo"))); |
368 EXPECT_FALSE(StorageMatch(origin1_, "bar", GURL("http://example.com/foo"))); | 361 EXPECT_FALSE(StorageMatch(origin1_, "bar", GURL("http://example.com/foo"))); |
369 EXPECT_EQ(CACHE_STORAGE_ERROR_NOT_FOUND, callback_cache_error_); | 362 EXPECT_EQ(CACHE_STORAGE_ERROR_NOT_FOUND, callback_error_); |
370 } | 363 } |
371 | 364 |
372 TEST_P(CacheStorageManagerTestP, StorageMatchAllEntryExists) { | 365 TEST_P(CacheStorageManagerTestP, StorageMatchAllEntryExists) { |
373 EXPECT_TRUE(Open(origin1_, "foo")); | 366 EXPECT_TRUE(Open(origin1_, "foo")); |
374 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo"))); | 367 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo"))); |
375 EXPECT_TRUE(StorageMatchAll(origin1_, GURL("http://example.com/foo"))); | 368 EXPECT_TRUE(StorageMatchAll(origin1_, GURL("http://example.com/foo"))); |
376 } | 369 } |
377 | 370 |
378 TEST_P(CacheStorageManagerTestP, StorageMatchAllNoEntry) { | 371 TEST_P(CacheStorageManagerTestP, StorageMatchAllNoEntry) { |
379 EXPECT_TRUE(Open(origin1_, "foo")); | 372 EXPECT_TRUE(Open(origin1_, "foo")); |
380 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo"))); | 373 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo"))); |
381 EXPECT_FALSE(StorageMatchAll(origin1_, GURL("http://example.com/bar"))); | 374 EXPECT_FALSE(StorageMatchAll(origin1_, GURL("http://example.com/bar"))); |
382 EXPECT_EQ(CACHE_STORAGE_ERROR_NOT_FOUND, callback_cache_error_); | 375 EXPECT_EQ(CACHE_STORAGE_ERROR_NOT_FOUND, callback_error_); |
383 } | 376 } |
384 | 377 |
385 TEST_P(CacheStorageManagerTestP, StorageMatchAllNoCaches) { | 378 TEST_P(CacheStorageManagerTestP, StorageMatchAllNoCaches) { |
386 EXPECT_FALSE(StorageMatchAll(origin1_, GURL("http://example.com/foo"))); | 379 EXPECT_FALSE(StorageMatchAll(origin1_, GURL("http://example.com/foo"))); |
387 EXPECT_EQ(CACHE_STORAGE_ERROR_NOT_FOUND, callback_cache_error_); | 380 EXPECT_EQ(CACHE_STORAGE_ERROR_NOT_FOUND, callback_error_); |
388 } | 381 } |
389 | 382 |
390 TEST_P(CacheStorageManagerTestP, StorageMatchAllEntryExistsTwice) { | 383 TEST_P(CacheStorageManagerTestP, StorageMatchAllEntryExistsTwice) { |
391 EXPECT_TRUE(Open(origin1_, "foo")); | 384 EXPECT_TRUE(Open(origin1_, "foo")); |
392 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo"))); | 385 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo"))); |
393 EXPECT_TRUE(Open(origin1_, "bar")); | 386 EXPECT_TRUE(Open(origin1_, "bar")); |
394 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo"))); | 387 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo"))); |
395 | 388 |
396 EXPECT_TRUE(StorageMatchAll(origin1_, GURL("http://example.com/foo"))); | 389 EXPECT_TRUE(StorageMatchAll(origin1_, GURL("http://example.com/foo"))); |
397 } | 390 } |
(...skipping 400 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
798 | 791 |
799 INSTANTIATE_TEST_CASE_P(CacheStorageManagerTests, | 792 INSTANTIATE_TEST_CASE_P(CacheStorageManagerTests, |
800 CacheStorageManagerTestP, | 793 CacheStorageManagerTestP, |
801 ::testing::Values(false, true)); | 794 ::testing::Values(false, true)); |
802 | 795 |
803 INSTANTIATE_TEST_CASE_P(CacheStorageQuotaClientTests, | 796 INSTANTIATE_TEST_CASE_P(CacheStorageQuotaClientTests, |
804 CacheStorageQuotaClientTestP, | 797 CacheStorageQuotaClientTestP, |
805 ::testing::Values(false, true)); | 798 ::testing::Values(false, true)); |
806 | 799 |
807 } // namespace content | 800 } // namespace content |
OLD | NEW |