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/service_worker/service_worker_cache.h" | 5 #include "content/browser/cache_storage/cache_storage_cache.h" |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
9 #include "base/message_loop/message_loop_proxy.h" | 9 #include "base/message_loop/message_loop_proxy.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
11 #include "content/browser/fileapi/chrome_blob_storage_context.h" | 11 #include "content/browser/fileapi/chrome_blob_storage_context.h" |
12 #include "content/browser/fileapi/mock_url_request_delegate.h" | 12 #include "content/browser/fileapi/mock_url_request_delegate.h" |
13 #include "content/browser/quota/mock_quota_manager_proxy.h" | 13 #include "content/browser/quota/mock_quota_manager_proxy.h" |
| 14 #include "content/common/cache_storage/cache_storage_types.h" |
14 #include "content/common/service_worker/service_worker_types.h" | 15 #include "content/common/service_worker/service_worker_types.h" |
15 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
16 #include "content/public/common/referrer.h" | 17 #include "content/public/common/referrer.h" |
17 #include "content/public/test/test_browser_context.h" | 18 #include "content/public/test/test_browser_context.h" |
18 #include "content/public/test/test_browser_thread_bundle.h" | 19 #include "content/public/test/test_browser_thread_bundle.h" |
19 #include "net/url_request/url_request_context.h" | 20 #include "net/url_request/url_request_context.h" |
20 #include "net/url_request/url_request_context_getter.h" | 21 #include "net/url_request/url_request_context_getter.h" |
21 #include "net/url_request/url_request_job_factory_impl.h" | 22 #include "net/url_request/url_request_job_factory_impl.h" |
22 #include "storage/browser/blob/blob_data_builder.h" | 23 #include "storage/browser/blob/blob_data_builder.h" |
23 #include "storage/browser/blob/blob_data_handle.h" | 24 #include "storage/browser/blob/blob_data_handle.h" |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 callback.Run(rv); | 115 callback.Run(rv); |
115 } | 116 } |
116 | 117 |
117 scoped_ptr<disk_cache::Backend> backend_; | 118 scoped_ptr<disk_cache::Backend> backend_; |
118 bool delay_open_; | 119 bool delay_open_; |
119 base::Closure open_entry_callback_; | 120 base::Closure open_entry_callback_; |
120 }; | 121 }; |
121 | 122 |
122 } // namespace | 123 } // namespace |
123 | 124 |
124 // A ServiceWorkerCache that can optionally delay during backend creation. | 125 // A CacheStorageCache that can optionally delay during backend creation. |
125 class TestServiceWorkerCache : public ServiceWorkerCache { | 126 class TestCacheStorageCache : public CacheStorageCache { |
126 public: | 127 public: |
127 TestServiceWorkerCache( | 128 TestCacheStorageCache( |
128 const GURL& origin, | 129 const GURL& origin, |
129 const base::FilePath& path, | 130 const base::FilePath& path, |
130 net::URLRequestContext* request_context, | 131 net::URLRequestContext* request_context, |
131 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, | 132 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, |
132 base::WeakPtr<storage::BlobStorageContext> blob_context) | 133 base::WeakPtr<storage::BlobStorageContext> blob_context) |
133 : ServiceWorkerCache(origin, | 134 : CacheStorageCache(origin, |
134 path, | 135 path, |
135 request_context, | 136 request_context, |
136 quota_manager_proxy, | 137 quota_manager_proxy, |
137 blob_context), | 138 blob_context), |
138 delay_backend_creation_(false) {} | 139 delay_backend_creation_(false) {} |
139 | 140 |
140 void CreateBackend(const ErrorCallback& callback) override { | 141 void CreateBackend(const ErrorCallback& callback) override { |
141 backend_creation_callback_ = callback; | 142 backend_creation_callback_ = callback; |
142 if (delay_backend_creation_) | 143 if (delay_backend_creation_) |
143 return; | 144 return; |
144 ContinueCreateBackend(); | 145 ContinueCreateBackend(); |
145 } | 146 } |
146 | 147 |
147 void ContinueCreateBackend() { | 148 void ContinueCreateBackend() { |
148 ServiceWorkerCache::CreateBackend(backend_creation_callback_); | 149 CacheStorageCache::CreateBackend(backend_creation_callback_); |
149 } | 150 } |
150 | 151 |
151 void set_delay_backend_creation(bool delay) { | 152 void set_delay_backend_creation(bool delay) { |
152 delay_backend_creation_ = delay; | 153 delay_backend_creation_ = delay; |
153 } | 154 } |
154 | 155 |
155 // Swap the existing backend with a delayable one. The backend must have been | 156 // Swap the existing backend with a delayable one. The backend must have been |
156 // created before calling this. | 157 // created before calling this. |
157 DelayableBackend* UseDelayableBackend() { | 158 DelayableBackend* UseDelayableBackend() { |
158 EXPECT_TRUE(backend_); | 159 EXPECT_TRUE(backend_); |
159 DelayableBackend* delayable_backend = new DelayableBackend(backend_.Pass()); | 160 DelayableBackend* delayable_backend = new DelayableBackend(backend_.Pass()); |
160 backend_.reset(delayable_backend); | 161 backend_.reset(delayable_backend); |
161 return delayable_backend; | 162 return delayable_backend; |
162 } | 163 } |
163 | 164 |
164 private: | 165 private: |
165 ~TestServiceWorkerCache() override {} | 166 ~TestCacheStorageCache() override {} |
166 | 167 |
167 bool delay_backend_creation_; | 168 bool delay_backend_creation_; |
168 ErrorCallback backend_creation_callback_; | 169 ErrorCallback backend_creation_callback_; |
169 | 170 |
170 DISALLOW_COPY_AND_ASSIGN(TestServiceWorkerCache); | 171 DISALLOW_COPY_AND_ASSIGN(TestCacheStorageCache); |
171 }; | 172 }; |
172 | 173 |
173 class ServiceWorkerCacheTest : public testing::Test { | 174 class CacheStorageCacheTest : public testing::Test { |
174 public: | 175 public: |
175 ServiceWorkerCacheTest() | 176 CacheStorageCacheTest() |
176 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), | 177 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), |
177 callback_error_(ServiceWorkerCache::ERROR_TYPE_OK), | 178 callback_error_(CacheStorageCache::ERROR_TYPE_OK), |
178 callback_closed_(false) {} | 179 callback_closed_(false) {} |
179 | 180 |
180 void SetUp() override { | 181 void SetUp() override { |
181 ChromeBlobStorageContext* blob_storage_context = | 182 ChromeBlobStorageContext* blob_storage_context = |
182 ChromeBlobStorageContext::GetFor(&browser_context_); | 183 ChromeBlobStorageContext::GetFor(&browser_context_); |
183 // Wait for chrome_blob_storage_context to finish initializing. | 184 // Wait for chrome_blob_storage_context to finish initializing. |
184 base::RunLoop().RunUntilIdle(); | 185 base::RunLoop().RunUntilIdle(); |
185 blob_storage_context_ = blob_storage_context->context(); | 186 blob_storage_context_ = blob_storage_context->context(); |
186 | 187 |
187 quota_manager_proxy_ = new MockQuotaManagerProxy( | 188 quota_manager_proxy_ = new MockQuotaManagerProxy( |
188 nullptr, base::MessageLoopProxy::current().get()); | 189 nullptr, base::MessageLoopProxy::current().get()); |
189 | 190 |
190 url_request_job_factory_.reset(new net::URLRequestJobFactoryImpl); | 191 url_request_job_factory_.reset(new net::URLRequestJobFactoryImpl); |
191 url_request_job_factory_->SetProtocolHandler( | 192 url_request_job_factory_->SetProtocolHandler( |
192 "blob", CreateMockBlobProtocolHandler(blob_storage_context->context())); | 193 "blob", CreateMockBlobProtocolHandler(blob_storage_context->context())); |
193 | 194 |
194 net::URLRequestContext* url_request_context = | 195 net::URLRequestContext* url_request_context = |
195 browser_context_.GetRequestContext()->GetURLRequestContext(); | 196 browser_context_.GetRequestContext()->GetURLRequestContext(); |
196 | 197 |
197 url_request_context->set_job_factory(url_request_job_factory_.get()); | 198 url_request_context->set_job_factory(url_request_job_factory_.get()); |
198 | 199 |
199 CreateRequests(blob_storage_context); | 200 CreateRequests(blob_storage_context); |
200 | 201 |
201 if (!MemoryOnly()) | 202 if (!MemoryOnly()) |
202 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 203 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
203 base::FilePath path = MemoryOnly() ? base::FilePath() : temp_dir_.path(); | 204 base::FilePath path = MemoryOnly() ? base::FilePath() : temp_dir_.path(); |
204 | 205 |
205 cache_ = make_scoped_refptr(new TestServiceWorkerCache( | 206 cache_ = make_scoped_refptr(new TestCacheStorageCache( |
206 GURL("http://example.com"), path, url_request_context, | 207 GURL("http://example.com"), path, url_request_context, |
207 quota_manager_proxy_, blob_storage_context->context()->AsWeakPtr())); | 208 quota_manager_proxy_, blob_storage_context->context()->AsWeakPtr())); |
208 } | 209 } |
209 | 210 |
210 void TearDown() override { | 211 void TearDown() override { |
211 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); | 212 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); |
212 base::RunLoop().RunUntilIdle(); | 213 base::RunLoop().RunUntilIdle(); |
213 } | 214 } |
214 | 215 |
215 void CreateRequests(ChromeBlobStorageContext* blob_storage_context) { | 216 void CreateRequests(ChromeBlobStorageContext* blob_storage_context) { |
216 ServiceWorkerHeaderMap headers; | 217 ServiceWorkerHeaderMap headers; |
217 headers.insert(std::make_pair("a", "a")); | 218 headers.insert(std::make_pair("a", "a")); |
218 headers.insert(std::make_pair("b", "b")); | 219 headers.insert(std::make_pair("b", "b")); |
219 body_request_ = | 220 body_request_ = |
220 ServiceWorkerFetchRequest(GURL("http://example.com/body.html"), "GET", | 221 ServiceWorkerFetchRequest(GURL("http://example.com/body.html"), "GET", |
221 headers, Referrer(), false); | 222 headers, Referrer(), false); |
222 no_body_request_ = | 223 no_body_request_ = |
223 ServiceWorkerFetchRequest(GURL("http://example.com/no_body.html"), | 224 ServiceWorkerFetchRequest(GURL("http://example.com/no_body.html"), |
224 "GET", | 225 "GET", headers, Referrer(), false); |
225 headers, | |
226 Referrer(), | |
227 false); | |
228 | 226 |
229 std::string expected_response; | 227 std::string expected_response; |
230 for (int i = 0; i < 100; ++i) | 228 for (int i = 0; i < 100; ++i) |
231 expected_blob_data_ += kTestData; | 229 expected_blob_data_ += kTestData; |
232 | 230 |
233 scoped_ptr<storage::BlobDataBuilder> blob_data( | 231 scoped_ptr<storage::BlobDataBuilder> blob_data( |
234 new storage::BlobDataBuilder("blob-id:myblob")); | 232 new storage::BlobDataBuilder("blob-id:myblob")); |
235 blob_data->AppendData(expected_blob_data_); | 233 blob_data->AppendData(expected_blob_data_); |
236 | 234 |
237 blob_handle_ = | 235 blob_handle_ = |
238 blob_storage_context->context()->AddFinishedBlob(blob_data.get()); | 236 blob_storage_context->context()->AddFinishedBlob(blob_data.get()); |
239 | 237 |
240 body_response_ = | 238 body_response_ = ServiceWorkerResponse( |
241 ServiceWorkerResponse(GURL("http://example.com/body.html"), | 239 GURL("http://example.com/body.html"), 200, "OK", |
242 200, | 240 blink::WebServiceWorkerResponseTypeDefault, headers, |
243 "OK", | 241 blob_handle_->uuid(), expected_blob_data_.size(), GURL()); |
244 blink::WebServiceWorkerResponseTypeDefault, | |
245 headers, | |
246 blob_handle_->uuid(), | |
247 expected_blob_data_.size(), | |
248 GURL()); | |
249 | 242 |
250 no_body_response_ = | 243 no_body_response_ = ServiceWorkerResponse( |
251 ServiceWorkerResponse(GURL("http://example.com/no_body.html"), | 244 GURL("http://example.com/no_body.html"), 200, "OK", |
252 200, | 245 blink::WebServiceWorkerResponseTypeDefault, headers, "", 0, GURL()); |
253 "OK", | |
254 blink::WebServiceWorkerResponseTypeDefault, | |
255 headers, | |
256 "", | |
257 0, | |
258 GURL()); | |
259 } | 246 } |
260 | 247 |
261 scoped_ptr<ServiceWorkerFetchRequest> CopyFetchRequest( | 248 scoped_ptr<ServiceWorkerFetchRequest> CopyFetchRequest( |
262 const ServiceWorkerFetchRequest& request) { | 249 const ServiceWorkerFetchRequest& request) { |
263 return make_scoped_ptr(new ServiceWorkerFetchRequest(request.url, | 250 return make_scoped_ptr(new ServiceWorkerFetchRequest( |
264 request.method, | 251 request.url, request.method, request.headers, request.referrer, |
265 request.headers, | 252 request.is_reload)); |
266 request.referrer, | |
267 request.is_reload)); | |
268 } | 253 } |
269 | 254 |
270 scoped_ptr<ServiceWorkerResponse> CopyFetchResponse( | 255 scoped_ptr<ServiceWorkerResponse> CopyFetchResponse( |
271 const ServiceWorkerResponse& response) { | 256 const ServiceWorkerResponse& response) { |
272 scoped_ptr<ServiceWorkerResponse> sw_response( | 257 scoped_ptr<ServiceWorkerResponse> sw_response(new ServiceWorkerResponse( |
273 new ServiceWorkerResponse(response.url, | 258 response.url, response.status_code, response.status_text, |
274 response.status_code, | 259 response.response_type, response.headers, response.blob_uuid, |
275 response.status_text, | 260 response.blob_size, response.stream_url)); |
276 response.response_type, | |
277 response.headers, | |
278 response.blob_uuid, | |
279 response.blob_size, | |
280 response.stream_url)); | |
281 return sw_response.Pass(); | 261 return sw_response.Pass(); |
282 } | 262 } |
283 | 263 |
284 bool Put(const ServiceWorkerFetchRequest& request, | 264 bool Put(const ServiceWorkerFetchRequest& request, |
285 const ServiceWorkerResponse& response) { | 265 const ServiceWorkerResponse& response) { |
286 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | 266 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); |
287 | 267 |
288 cache_->Put(CopyFetchRequest(request), | 268 cache_->Put( |
289 CopyFetchResponse(response), | 269 CopyFetchRequest(request), CopyFetchResponse(response), |
290 base::Bind(&ServiceWorkerCacheTest::ResponseAndErrorCallback, | 270 base::Bind(&CacheStorageCacheTest::ResponseAndErrorCallback, |
291 base::Unretained(this), | 271 base::Unretained(this), base::Unretained(loop.get()))); |
292 base::Unretained(loop.get()))); | |
293 // TODO(jkarlin): These functions should use base::RunLoop().RunUntilIdle() | 272 // TODO(jkarlin): These functions should use base::RunLoop().RunUntilIdle() |
294 // once the cache uses a passed in MessageLoopProxy instead of the CACHE | 273 // once the cache uses a passed in MessageLoopProxy instead of the CACHE |
295 // thread. | 274 // thread. |
296 loop->Run(); | 275 loop->Run(); |
297 | 276 |
298 return callback_error_ == ServiceWorkerCache::ERROR_TYPE_OK; | 277 return callback_error_ == CacheStorageCache::ERROR_TYPE_OK; |
299 } | 278 } |
300 | 279 |
301 bool Match(const ServiceWorkerFetchRequest& request) { | 280 bool Match(const ServiceWorkerFetchRequest& request) { |
302 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | 281 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); |
303 | 282 |
304 cache_->Match(CopyFetchRequest(request), | 283 cache_->Match( |
305 base::Bind(&ServiceWorkerCacheTest::ResponseAndErrorCallback, | 284 CopyFetchRequest(request), |
306 base::Unretained(this), | 285 base::Bind(&CacheStorageCacheTest::ResponseAndErrorCallback, |
307 base::Unretained(loop.get()))); | 286 base::Unretained(this), base::Unretained(loop.get()))); |
308 loop->Run(); | 287 loop->Run(); |
309 | 288 |
310 return callback_error_ == ServiceWorkerCache::ERROR_TYPE_OK; | 289 return callback_error_ == CacheStorageCache::ERROR_TYPE_OK; |
311 } | 290 } |
312 | 291 |
313 bool Delete(const ServiceWorkerFetchRequest& request) { | 292 bool Delete(const ServiceWorkerFetchRequest& request) { |
314 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | 293 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); |
315 | 294 |
316 cache_->Delete(CopyFetchRequest(request), | 295 cache_->Delete( |
317 base::Bind(&ServiceWorkerCacheTest::ErrorTypeCallback, | 296 CopyFetchRequest(request), |
318 base::Unretained(this), | 297 base::Bind(&CacheStorageCacheTest::ErrorTypeCallback, |
319 base::Unretained(loop.get()))); | 298 base::Unretained(this), base::Unretained(loop.get()))); |
320 loop->Run(); | 299 loop->Run(); |
321 | 300 |
322 return callback_error_ == ServiceWorkerCache::ERROR_TYPE_OK; | 301 return callback_error_ == CacheStorageCache::ERROR_TYPE_OK; |
323 } | 302 } |
324 | 303 |
325 bool Keys() { | 304 bool Keys() { |
326 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | 305 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); |
327 | 306 |
328 cache_->Keys(base::Bind(&ServiceWorkerCacheTest::RequestsCallback, | 307 cache_->Keys(base::Bind(&CacheStorageCacheTest::RequestsCallback, |
329 base::Unretained(this), | 308 base::Unretained(this), |
330 base::Unretained(loop.get()))); | 309 base::Unretained(loop.get()))); |
331 loop->Run(); | 310 loop->Run(); |
332 | 311 |
333 return callback_error_ == ServiceWorkerCache::ERROR_TYPE_OK; | 312 return callback_error_ == CacheStorageCache::ERROR_TYPE_OK; |
334 } | 313 } |
335 | 314 |
336 bool Close() { | 315 bool Close() { |
337 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | 316 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); |
338 | 317 |
339 cache_->Close(base::Bind(&ServiceWorkerCacheTest::CloseCallback, | 318 cache_->Close(base::Bind(&CacheStorageCacheTest::CloseCallback, |
340 base::Unretained(this), | 319 base::Unretained(this), |
341 base::Unretained(loop.get()))); | 320 base::Unretained(loop.get()))); |
342 loop->Run(); | 321 loop->Run(); |
343 return callback_closed_; | 322 return callback_closed_; |
344 } | 323 } |
345 | 324 |
346 void RequestsCallback(base::RunLoop* run_loop, | 325 void RequestsCallback(base::RunLoop* run_loop, |
347 ServiceWorkerCache::ErrorType error, | 326 CacheStorageCache::ErrorType error, |
348 scoped_ptr<ServiceWorkerCache::Requests> requests) { | 327 scoped_ptr<CacheStorageCache::Requests> requests) { |
349 callback_error_ = error; | 328 callback_error_ = error; |
350 callback_strings_.clear(); | 329 callback_strings_.clear(); |
351 if (requests) { | 330 if (requests) { |
352 for (size_t i = 0u; i < requests->size(); ++i) | 331 for (size_t i = 0u; i < requests->size(); ++i) |
353 callback_strings_.push_back(requests->at(i).url.spec()); | 332 callback_strings_.push_back(requests->at(i).url.spec()); |
354 } | 333 } |
355 if (run_loop) | 334 if (run_loop) |
356 run_loop->Quit(); | 335 run_loop->Quit(); |
357 } | 336 } |
358 | 337 |
359 void ErrorTypeCallback(base::RunLoop* run_loop, | 338 void ErrorTypeCallback(base::RunLoop* run_loop, |
360 ServiceWorkerCache::ErrorType error) { | 339 CacheStorageCache::ErrorType error) { |
361 callback_error_ = error; | 340 callback_error_ = error; |
362 if (run_loop) | 341 if (run_loop) |
363 run_loop->Quit(); | 342 run_loop->Quit(); |
364 } | 343 } |
365 | 344 |
366 void ResponseAndErrorCallback( | 345 void ResponseAndErrorCallback( |
367 base::RunLoop* run_loop, | 346 base::RunLoop* run_loop, |
368 ServiceWorkerCache::ErrorType error, | 347 CacheStorageCache::ErrorType error, |
369 scoped_ptr<ServiceWorkerResponse> response, | 348 scoped_ptr<ServiceWorkerResponse> response, |
370 scoped_ptr<storage::BlobDataHandle> body_handle) { | 349 scoped_ptr<storage::BlobDataHandle> body_handle) { |
371 callback_error_ = error; | 350 callback_error_ = error; |
372 callback_response_ = response.Pass(); | 351 callback_response_ = response.Pass(); |
373 callback_response_data_.reset(); | 352 callback_response_data_.reset(); |
374 if (error == ServiceWorkerCache::ERROR_TYPE_OK && | 353 if (error == CacheStorageCache::ERROR_TYPE_OK && |
375 !callback_response_->blob_uuid.empty()) { | 354 !callback_response_->blob_uuid.empty()) { |
376 callback_response_data_ = body_handle.Pass(); | 355 callback_response_data_ = body_handle.Pass(); |
377 } | 356 } |
378 | 357 |
379 if (run_loop) | 358 if (run_loop) |
380 run_loop->Quit(); | 359 run_loop->Quit(); |
381 } | 360 } |
382 | 361 |
383 void CloseCallback(base::RunLoop* run_loop) { | 362 void CloseCallback(base::RunLoop* run_loop) { |
384 EXPECT_FALSE(callback_closed_); | 363 EXPECT_FALSE(callback_closed_); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
428 virtual bool MemoryOnly() { return false; } | 407 virtual bool MemoryOnly() { return false; } |
429 | 408 |
430 protected: | 409 protected: |
431 TestBrowserContext browser_context_; | 410 TestBrowserContext browser_context_; |
432 TestBrowserThreadBundle browser_thread_bundle_; | 411 TestBrowserThreadBundle browser_thread_bundle_; |
433 scoped_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_; | 412 scoped_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_; |
434 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; | 413 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; |
435 storage::BlobStorageContext* blob_storage_context_; | 414 storage::BlobStorageContext* blob_storage_context_; |
436 | 415 |
437 base::ScopedTempDir temp_dir_; | 416 base::ScopedTempDir temp_dir_; |
438 scoped_refptr<TestServiceWorkerCache> cache_; | 417 scoped_refptr<TestCacheStorageCache> cache_; |
439 | 418 |
440 ServiceWorkerFetchRequest body_request_; | 419 ServiceWorkerFetchRequest body_request_; |
441 ServiceWorkerResponse body_response_; | 420 ServiceWorkerResponse body_response_; |
442 ServiceWorkerFetchRequest no_body_request_; | 421 ServiceWorkerFetchRequest no_body_request_; |
443 ServiceWorkerResponse no_body_response_; | 422 ServiceWorkerResponse no_body_response_; |
444 scoped_ptr<storage::BlobDataHandle> blob_handle_; | 423 scoped_ptr<storage::BlobDataHandle> blob_handle_; |
445 std::string expected_blob_data_; | 424 std::string expected_blob_data_; |
446 | 425 |
447 ServiceWorkerCache::ErrorType callback_error_; | 426 CacheStorageCache::ErrorType callback_error_; |
448 scoped_ptr<ServiceWorkerResponse> callback_response_; | 427 scoped_ptr<ServiceWorkerResponse> callback_response_; |
449 scoped_ptr<storage::BlobDataHandle> callback_response_data_; | 428 scoped_ptr<storage::BlobDataHandle> callback_response_data_; |
450 std::vector<std::string> callback_strings_; | 429 std::vector<std::string> callback_strings_; |
451 bool callback_closed_; | 430 bool callback_closed_; |
452 }; | 431 }; |
453 | 432 |
454 class ServiceWorkerCacheTestP : public ServiceWorkerCacheTest, | 433 class CacheStorageCacheTestP : public CacheStorageCacheTest, |
455 public testing::WithParamInterface<bool> { | 434 public testing::WithParamInterface<bool> { |
456 bool MemoryOnly() override { return !GetParam(); } | 435 bool MemoryOnly() override { return !GetParam(); } |
457 }; | 436 }; |
458 | 437 |
459 class ServiceWorkerCacheMemoryOnlyTest | 438 class CacheStorageCacheMemoryOnlyTest |
460 : public ServiceWorkerCacheTest, | 439 : public CacheStorageCacheTest, |
461 public testing::WithParamInterface<bool> { | 440 public testing::WithParamInterface<bool> { |
462 bool MemoryOnly() override { return true; } | 441 bool MemoryOnly() override { return true; } |
463 }; | 442 }; |
464 | 443 |
465 TEST_P(ServiceWorkerCacheTestP, PutNoBody) { | 444 TEST_P(CacheStorageCacheTestP, PutNoBody) { |
466 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); | 445 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); |
467 EXPECT_TRUE(callback_response_); | 446 EXPECT_TRUE(callback_response_); |
468 EXPECT_STREQ(no_body_response_.url.spec().c_str(), | 447 EXPECT_STREQ(no_body_response_.url.spec().c_str(), |
469 callback_response_->url.spec().c_str()); | 448 callback_response_->url.spec().c_str()); |
470 EXPECT_FALSE(callback_response_data_); | 449 EXPECT_FALSE(callback_response_data_); |
471 EXPECT_STREQ("", callback_response_->blob_uuid.c_str()); | 450 EXPECT_STREQ("", callback_response_->blob_uuid.c_str()); |
472 EXPECT_EQ(0u, callback_response_->blob_size); | 451 EXPECT_EQ(0u, callback_response_->blob_size); |
473 } | 452 } |
474 | 453 |
475 TEST_P(ServiceWorkerCacheTestP, PutBody) { | 454 TEST_P(CacheStorageCacheTestP, PutBody) { |
476 EXPECT_TRUE(Put(body_request_, body_response_)); | 455 EXPECT_TRUE(Put(body_request_, body_response_)); |
477 EXPECT_TRUE(callback_response_); | 456 EXPECT_TRUE(callback_response_); |
478 EXPECT_STREQ(body_response_.url.spec().c_str(), | 457 EXPECT_STREQ(body_response_.url.spec().c_str(), |
479 callback_response_->url.spec().c_str()); | 458 callback_response_->url.spec().c_str()); |
480 EXPECT_TRUE(callback_response_data_); | 459 EXPECT_TRUE(callback_response_data_); |
481 EXPECT_STRNE("", callback_response_->blob_uuid.c_str()); | 460 EXPECT_STRNE("", callback_response_->blob_uuid.c_str()); |
482 EXPECT_EQ(expected_blob_data_.size(), callback_response_->blob_size); | 461 EXPECT_EQ(expected_blob_data_.size(), callback_response_->blob_size); |
483 | 462 |
484 std::string response_body; | 463 std::string response_body; |
485 CopyBody(callback_response_data_.get(), &response_body); | 464 CopyBody(callback_response_data_.get(), &response_body); |
486 EXPECT_STREQ(expected_blob_data_.c_str(), response_body.c_str()); | 465 EXPECT_STREQ(expected_blob_data_.c_str(), response_body.c_str()); |
487 } | 466 } |
488 | 467 |
489 TEST_P(ServiceWorkerCacheTestP, ResponseURLDiffersFromRequestURL) { | 468 TEST_P(CacheStorageCacheTestP, ResponseURLDiffersFromRequestURL) { |
490 no_body_response_.url = GURL("http://example.com/foobar"); | 469 no_body_response_.url = GURL("http://example.com/foobar"); |
491 EXPECT_STRNE("http://example.com/foobar", | 470 EXPECT_STRNE("http://example.com/foobar", |
492 no_body_request_.url.spec().c_str()); | 471 no_body_request_.url.spec().c_str()); |
493 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); | 472 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); |
494 EXPECT_TRUE(Match(no_body_request_)); | 473 EXPECT_TRUE(Match(no_body_request_)); |
495 EXPECT_STREQ("http://example.com/foobar", | 474 EXPECT_STREQ("http://example.com/foobar", |
496 callback_response_->url.spec().c_str()); | 475 callback_response_->url.spec().c_str()); |
497 } | 476 } |
498 | 477 |
499 TEST_P(ServiceWorkerCacheTestP, ResponseURLEmpty) { | 478 TEST_P(CacheStorageCacheTestP, ResponseURLEmpty) { |
500 no_body_response_.url = GURL(); | 479 no_body_response_.url = GURL(); |
501 EXPECT_STRNE("", no_body_request_.url.spec().c_str()); | 480 EXPECT_STRNE("", no_body_request_.url.spec().c_str()); |
502 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); | 481 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); |
503 EXPECT_TRUE(Match(no_body_request_)); | 482 EXPECT_TRUE(Match(no_body_request_)); |
504 EXPECT_STREQ("", callback_response_->url.spec().c_str()); | 483 EXPECT_STREQ("", callback_response_->url.spec().c_str()); |
505 } | 484 } |
506 | 485 |
507 TEST_F(ServiceWorkerCacheTest, PutBodyDropBlobRef) { | 486 TEST_F(CacheStorageCacheTest, PutBodyDropBlobRef) { |
508 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | 487 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); |
509 cache_->Put(CopyFetchRequest(body_request_), | 488 cache_->Put(CopyFetchRequest(body_request_), |
510 CopyFetchResponse(body_response_), | 489 CopyFetchResponse(body_response_), |
511 base::Bind(&ServiceWorkerCacheTestP::ResponseAndErrorCallback, | 490 base::Bind(&CacheStorageCacheTestP::ResponseAndErrorCallback, |
512 base::Unretained(this), | 491 base::Unretained(this), base::Unretained(loop.get()))); |
513 base::Unretained(loop.get()))); | |
514 // The handle should be held by the cache now so the deref here should be | 492 // The handle should be held by the cache now so the deref here should be |
515 // okay. | 493 // okay. |
516 blob_handle_.reset(); | 494 blob_handle_.reset(); |
517 loop->Run(); | 495 loop->Run(); |
518 | 496 |
519 EXPECT_EQ(ServiceWorkerCache::ERROR_TYPE_OK, callback_error_); | 497 EXPECT_EQ(CacheStorageCache::ERROR_TYPE_OK, callback_error_); |
520 } | 498 } |
521 | 499 |
522 TEST_P(ServiceWorkerCacheTestP, PutReplace) { | 500 TEST_P(CacheStorageCacheTestP, PutReplace) { |
523 EXPECT_TRUE(Put(body_request_, no_body_response_)); | 501 EXPECT_TRUE(Put(body_request_, no_body_response_)); |
524 EXPECT_TRUE(Match(body_request_)); | 502 EXPECT_TRUE(Match(body_request_)); |
525 EXPECT_FALSE(callback_response_data_); | 503 EXPECT_FALSE(callback_response_data_); |
526 | 504 |
527 EXPECT_TRUE(Put(body_request_, body_response_)); | 505 EXPECT_TRUE(Put(body_request_, body_response_)); |
528 EXPECT_TRUE(Match(body_request_)); | 506 EXPECT_TRUE(Match(body_request_)); |
529 EXPECT_TRUE(callback_response_data_); | 507 EXPECT_TRUE(callback_response_data_); |
530 | 508 |
531 EXPECT_TRUE(Put(body_request_, no_body_response_)); | 509 EXPECT_TRUE(Put(body_request_, no_body_response_)); |
532 EXPECT_TRUE(Match(body_request_)); | 510 EXPECT_TRUE(Match(body_request_)); |
533 EXPECT_FALSE(callback_response_data_); | 511 EXPECT_FALSE(callback_response_data_); |
534 } | 512 } |
535 | 513 |
536 TEST_P(ServiceWorkerCacheTestP, MatchNoBody) { | 514 TEST_P(CacheStorageCacheTestP, MatchNoBody) { |
537 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); | 515 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); |
538 EXPECT_TRUE(Match(no_body_request_)); | 516 EXPECT_TRUE(Match(no_body_request_)); |
539 EXPECT_EQ(200, callback_response_->status_code); | 517 EXPECT_EQ(200, callback_response_->status_code); |
540 EXPECT_STREQ("OK", callback_response_->status_text.c_str()); | 518 EXPECT_STREQ("OK", callback_response_->status_text.c_str()); |
541 EXPECT_STREQ("http://example.com/no_body.html", | 519 EXPECT_STREQ("http://example.com/no_body.html", |
542 callback_response_->url.spec().c_str()); | 520 callback_response_->url.spec().c_str()); |
543 EXPECT_STREQ("", callback_response_->blob_uuid.c_str()); | 521 EXPECT_STREQ("", callback_response_->blob_uuid.c_str()); |
544 EXPECT_EQ(0u, callback_response_->blob_size); | 522 EXPECT_EQ(0u, callback_response_->blob_size); |
545 } | 523 } |
546 | 524 |
547 TEST_P(ServiceWorkerCacheTestP, MatchBody) { | 525 TEST_P(CacheStorageCacheTestP, MatchBody) { |
548 EXPECT_TRUE(Put(body_request_, body_response_)); | 526 EXPECT_TRUE(Put(body_request_, body_response_)); |
549 EXPECT_TRUE(Match(body_request_)); | 527 EXPECT_TRUE(Match(body_request_)); |
550 EXPECT_EQ(200, callback_response_->status_code); | 528 EXPECT_EQ(200, callback_response_->status_code); |
551 EXPECT_STREQ("OK", callback_response_->status_text.c_str()); | 529 EXPECT_STREQ("OK", callback_response_->status_text.c_str()); |
552 EXPECT_STREQ("http://example.com/body.html", | 530 EXPECT_STREQ("http://example.com/body.html", |
553 callback_response_->url.spec().c_str()); | 531 callback_response_->url.spec().c_str()); |
554 EXPECT_STRNE("", callback_response_->blob_uuid.c_str()); | 532 EXPECT_STRNE("", callback_response_->blob_uuid.c_str()); |
555 EXPECT_EQ(expected_blob_data_.size(), callback_response_->blob_size); | 533 EXPECT_EQ(expected_blob_data_.size(), callback_response_->blob_size); |
556 | 534 |
557 std::string response_body; | 535 std::string response_body; |
558 CopyBody(callback_response_data_.get(), &response_body); | 536 CopyBody(callback_response_data_.get(), &response_body); |
559 EXPECT_STREQ(expected_blob_data_.c_str(), response_body.c_str()); | 537 EXPECT_STREQ(expected_blob_data_.c_str(), response_body.c_str()); |
560 } | 538 } |
561 | 539 |
562 TEST_P(ServiceWorkerCacheTestP, Vary) { | 540 TEST_P(CacheStorageCacheTestP, Vary) { |
563 body_request_.headers["vary_foo"] = "foo"; | 541 body_request_.headers["vary_foo"] = "foo"; |
564 body_response_.headers["vary"] = "vary_foo"; | 542 body_response_.headers["vary"] = "vary_foo"; |
565 EXPECT_TRUE(Put(body_request_, body_response_)); | 543 EXPECT_TRUE(Put(body_request_, body_response_)); |
566 EXPECT_TRUE(Match(body_request_)); | 544 EXPECT_TRUE(Match(body_request_)); |
567 | 545 |
568 body_request_.headers["vary_foo"] = "bar"; | 546 body_request_.headers["vary_foo"] = "bar"; |
569 EXPECT_FALSE(Match(body_request_)); | 547 EXPECT_FALSE(Match(body_request_)); |
570 | 548 |
571 body_request_.headers.erase("vary_foo"); | 549 body_request_.headers.erase("vary_foo"); |
572 EXPECT_FALSE(Match(body_request_)); | 550 EXPECT_FALSE(Match(body_request_)); |
573 } | 551 } |
574 | 552 |
575 TEST_P(ServiceWorkerCacheTestP, EmptyVary) { | 553 TEST_P(CacheStorageCacheTestP, EmptyVary) { |
576 body_response_.headers["vary"] = ""; | 554 body_response_.headers["vary"] = ""; |
577 EXPECT_TRUE(Put(body_request_, body_response_)); | 555 EXPECT_TRUE(Put(body_request_, body_response_)); |
578 EXPECT_TRUE(Match(body_request_)); | 556 EXPECT_TRUE(Match(body_request_)); |
579 | 557 |
580 body_request_.headers["zoo"] = "zoo"; | 558 body_request_.headers["zoo"] = "zoo"; |
581 EXPECT_TRUE(Match(body_request_)); | 559 EXPECT_TRUE(Match(body_request_)); |
582 } | 560 } |
583 | 561 |
584 TEST_P(ServiceWorkerCacheTestP, NoVaryButDiffHeaders) { | 562 TEST_P(CacheStorageCacheTestP, NoVaryButDiffHeaders) { |
585 EXPECT_TRUE(Put(body_request_, body_response_)); | 563 EXPECT_TRUE(Put(body_request_, body_response_)); |
586 EXPECT_TRUE(Match(body_request_)); | 564 EXPECT_TRUE(Match(body_request_)); |
587 | 565 |
588 body_request_.headers["zoo"] = "zoo"; | 566 body_request_.headers["zoo"] = "zoo"; |
589 EXPECT_TRUE(Match(body_request_)); | 567 EXPECT_TRUE(Match(body_request_)); |
590 } | 568 } |
591 | 569 |
592 TEST_P(ServiceWorkerCacheTestP, VaryMultiple) { | 570 TEST_P(CacheStorageCacheTestP, VaryMultiple) { |
593 body_request_.headers["vary_foo"] = "foo"; | 571 body_request_.headers["vary_foo"] = "foo"; |
594 body_request_.headers["vary_bar"] = "bar"; | 572 body_request_.headers["vary_bar"] = "bar"; |
595 body_response_.headers["vary"] = " vary_foo , vary_bar"; | 573 body_response_.headers["vary"] = " vary_foo , vary_bar"; |
596 EXPECT_TRUE(Put(body_request_, body_response_)); | 574 EXPECT_TRUE(Put(body_request_, body_response_)); |
597 EXPECT_TRUE(Match(body_request_)); | 575 EXPECT_TRUE(Match(body_request_)); |
598 | 576 |
599 body_request_.headers["vary_bar"] = "foo"; | 577 body_request_.headers["vary_bar"] = "foo"; |
600 EXPECT_FALSE(Match(body_request_)); | 578 EXPECT_FALSE(Match(body_request_)); |
601 | 579 |
602 body_request_.headers.erase("vary_bar"); | 580 body_request_.headers.erase("vary_bar"); |
603 EXPECT_FALSE(Match(body_request_)); | 581 EXPECT_FALSE(Match(body_request_)); |
604 } | 582 } |
605 | 583 |
606 TEST_P(ServiceWorkerCacheTestP, VaryNewHeader) { | 584 TEST_P(CacheStorageCacheTestP, VaryNewHeader) { |
607 body_request_.headers["vary_foo"] = "foo"; | 585 body_request_.headers["vary_foo"] = "foo"; |
608 body_response_.headers["vary"] = " vary_foo, vary_bar"; | 586 body_response_.headers["vary"] = " vary_foo, vary_bar"; |
609 EXPECT_TRUE(Put(body_request_, body_response_)); | 587 EXPECT_TRUE(Put(body_request_, body_response_)); |
610 EXPECT_TRUE(Match(body_request_)); | 588 EXPECT_TRUE(Match(body_request_)); |
611 | 589 |
612 body_request_.headers["vary_bar"] = "bar"; | 590 body_request_.headers["vary_bar"] = "bar"; |
613 EXPECT_FALSE(Match(body_request_)); | 591 EXPECT_FALSE(Match(body_request_)); |
614 } | 592 } |
615 | 593 |
616 TEST_P(ServiceWorkerCacheTestP, VaryStar) { | 594 TEST_P(CacheStorageCacheTestP, VaryStar) { |
617 body_response_.headers["vary"] = "*"; | 595 body_response_.headers["vary"] = "*"; |
618 EXPECT_TRUE(Put(body_request_, body_response_)); | 596 EXPECT_TRUE(Put(body_request_, body_response_)); |
619 EXPECT_FALSE(Match(body_request_)); | 597 EXPECT_FALSE(Match(body_request_)); |
620 } | 598 } |
621 | 599 |
622 TEST_P(ServiceWorkerCacheTestP, EmptyKeys) { | 600 TEST_P(CacheStorageCacheTestP, EmptyKeys) { |
623 EXPECT_TRUE(Keys()); | 601 EXPECT_TRUE(Keys()); |
624 EXPECT_EQ(0u, callback_strings_.size()); | 602 EXPECT_EQ(0u, callback_strings_.size()); |
625 } | 603 } |
626 | 604 |
627 TEST_P(ServiceWorkerCacheTestP, TwoKeys) { | 605 TEST_P(CacheStorageCacheTestP, TwoKeys) { |
628 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); | 606 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); |
629 EXPECT_TRUE(Put(body_request_, body_response_)); | 607 EXPECT_TRUE(Put(body_request_, body_response_)); |
630 EXPECT_TRUE(Keys()); | 608 EXPECT_TRUE(Keys()); |
631 EXPECT_EQ(2u, callback_strings_.size()); | 609 EXPECT_EQ(2u, callback_strings_.size()); |
632 std::vector<std::string> expected_keys; | 610 std::vector<std::string> expected_keys; |
633 expected_keys.push_back(no_body_request_.url.spec()); | 611 expected_keys.push_back(no_body_request_.url.spec()); |
634 expected_keys.push_back(body_request_.url.spec()); | 612 expected_keys.push_back(body_request_.url.spec()); |
635 EXPECT_TRUE(VerifyKeys(expected_keys)); | 613 EXPECT_TRUE(VerifyKeys(expected_keys)); |
636 } | 614 } |
637 | 615 |
638 TEST_P(ServiceWorkerCacheTestP, TwoKeysThenOne) { | 616 TEST_P(CacheStorageCacheTestP, TwoKeysThenOne) { |
639 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); | 617 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); |
640 EXPECT_TRUE(Put(body_request_, body_response_)); | 618 EXPECT_TRUE(Put(body_request_, body_response_)); |
641 EXPECT_TRUE(Keys()); | 619 EXPECT_TRUE(Keys()); |
642 EXPECT_EQ(2u, callback_strings_.size()); | 620 EXPECT_EQ(2u, callback_strings_.size()); |
643 std::vector<std::string> expected_keys; | 621 std::vector<std::string> expected_keys; |
644 expected_keys.push_back(no_body_request_.url.spec()); | 622 expected_keys.push_back(no_body_request_.url.spec()); |
645 expected_keys.push_back(body_request_.url.spec()); | 623 expected_keys.push_back(body_request_.url.spec()); |
646 EXPECT_TRUE(VerifyKeys(expected_keys)); | 624 EXPECT_TRUE(VerifyKeys(expected_keys)); |
647 | 625 |
648 EXPECT_TRUE(Delete(body_request_)); | 626 EXPECT_TRUE(Delete(body_request_)); |
649 EXPECT_TRUE(Keys()); | 627 EXPECT_TRUE(Keys()); |
650 EXPECT_EQ(1u, callback_strings_.size()); | 628 EXPECT_EQ(1u, callback_strings_.size()); |
651 std::vector<std::string> expected_key; | 629 std::vector<std::string> expected_key; |
652 expected_key.push_back(no_body_request_.url.spec()); | 630 expected_key.push_back(no_body_request_.url.spec()); |
653 EXPECT_TRUE(VerifyKeys(expected_key)); | 631 EXPECT_TRUE(VerifyKeys(expected_key)); |
654 } | 632 } |
655 | 633 |
656 TEST_P(ServiceWorkerCacheTestP, DeleteNoBody) { | 634 TEST_P(CacheStorageCacheTestP, DeleteNoBody) { |
657 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); | 635 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); |
658 EXPECT_TRUE(Match(no_body_request_)); | 636 EXPECT_TRUE(Match(no_body_request_)); |
659 EXPECT_TRUE(Delete(no_body_request_)); | 637 EXPECT_TRUE(Delete(no_body_request_)); |
660 EXPECT_FALSE(Match(no_body_request_)); | 638 EXPECT_FALSE(Match(no_body_request_)); |
661 EXPECT_FALSE(Delete(no_body_request_)); | 639 EXPECT_FALSE(Delete(no_body_request_)); |
662 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); | 640 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); |
663 EXPECT_TRUE(Match(no_body_request_)); | 641 EXPECT_TRUE(Match(no_body_request_)); |
664 EXPECT_TRUE(Delete(no_body_request_)); | 642 EXPECT_TRUE(Delete(no_body_request_)); |
665 } | 643 } |
666 | 644 |
667 TEST_P(ServiceWorkerCacheTestP, DeleteBody) { | 645 TEST_P(CacheStorageCacheTestP, DeleteBody) { |
668 EXPECT_TRUE(Put(body_request_, body_response_)); | 646 EXPECT_TRUE(Put(body_request_, body_response_)); |
669 EXPECT_TRUE(Match(body_request_)); | 647 EXPECT_TRUE(Match(body_request_)); |
670 EXPECT_TRUE(Delete(body_request_)); | 648 EXPECT_TRUE(Delete(body_request_)); |
671 EXPECT_FALSE(Match(body_request_)); | 649 EXPECT_FALSE(Match(body_request_)); |
672 EXPECT_FALSE(Delete(body_request_)); | 650 EXPECT_FALSE(Delete(body_request_)); |
673 EXPECT_TRUE(Put(body_request_, body_response_)); | 651 EXPECT_TRUE(Put(body_request_, body_response_)); |
674 EXPECT_TRUE(Match(body_request_)); | 652 EXPECT_TRUE(Match(body_request_)); |
675 EXPECT_TRUE(Delete(body_request_)); | 653 EXPECT_TRUE(Delete(body_request_)); |
676 } | 654 } |
677 | 655 |
678 TEST_P(ServiceWorkerCacheTestP, QuickStressNoBody) { | 656 TEST_P(CacheStorageCacheTestP, QuickStressNoBody) { |
679 for (int i = 0; i < 100; ++i) { | 657 for (int i = 0; i < 100; ++i) { |
680 EXPECT_FALSE(Match(no_body_request_)); | 658 EXPECT_FALSE(Match(no_body_request_)); |
681 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); | 659 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); |
682 EXPECT_TRUE(Match(no_body_request_)); | 660 EXPECT_TRUE(Match(no_body_request_)); |
683 EXPECT_TRUE(Delete(no_body_request_)); | 661 EXPECT_TRUE(Delete(no_body_request_)); |
684 } | 662 } |
685 } | 663 } |
686 | 664 |
687 TEST_P(ServiceWorkerCacheTestP, QuickStressBody) { | 665 TEST_P(CacheStorageCacheTestP, QuickStressBody) { |
688 for (int i = 0; i < 100; ++i) { | 666 for (int i = 0; i < 100; ++i) { |
689 ASSERT_FALSE(Match(body_request_)); | 667 ASSERT_FALSE(Match(body_request_)); |
690 ASSERT_TRUE(Put(body_request_, body_response_)); | 668 ASSERT_TRUE(Put(body_request_, body_response_)); |
691 ASSERT_TRUE(Match(body_request_)); | 669 ASSERT_TRUE(Match(body_request_)); |
692 ASSERT_TRUE(Delete(body_request_)); | 670 ASSERT_TRUE(Delete(body_request_)); |
693 } | 671 } |
694 } | 672 } |
695 | 673 |
696 TEST_P(ServiceWorkerCacheTestP, PutResponseType) { | 674 TEST_P(CacheStorageCacheTestP, PutResponseType) { |
697 EXPECT_TRUE(TestResponseType(blink::WebServiceWorkerResponseTypeBasic)); | 675 EXPECT_TRUE(TestResponseType(blink::WebServiceWorkerResponseTypeBasic)); |
698 EXPECT_TRUE(TestResponseType(blink::WebServiceWorkerResponseTypeCORS)); | 676 EXPECT_TRUE(TestResponseType(blink::WebServiceWorkerResponseTypeCORS)); |
699 EXPECT_TRUE(TestResponseType(blink::WebServiceWorkerResponseTypeDefault)); | 677 EXPECT_TRUE(TestResponseType(blink::WebServiceWorkerResponseTypeDefault)); |
700 EXPECT_TRUE(TestResponseType(blink::WebServiceWorkerResponseTypeError)); | 678 EXPECT_TRUE(TestResponseType(blink::WebServiceWorkerResponseTypeError)); |
701 EXPECT_TRUE(TestResponseType(blink::WebServiceWorkerResponseTypeOpaque)); | 679 EXPECT_TRUE(TestResponseType(blink::WebServiceWorkerResponseTypeOpaque)); |
702 } | 680 } |
703 | 681 |
704 TEST_F(ServiceWorkerCacheTest, CaselessServiceWorkerResponseHeaders) { | 682 TEST_F(CacheStorageCacheTest, CaselessServiceWorkerResponseHeaders) { |
705 // ServiceWorkerCache depends on ServiceWorkerResponse having caseless | 683 // CacheStorageCache depends on ServiceWorkerResponse having caseless |
706 // headers so that it can quickly lookup vary headers. | 684 // headers so that it can quickly lookup vary headers. |
707 ServiceWorkerResponse response(GURL("http://www.example.com"), | 685 ServiceWorkerResponse response(GURL("http://www.example.com"), 200, "OK", |
708 200, | |
709 "OK", | |
710 blink::WebServiceWorkerResponseTypeDefault, | 686 blink::WebServiceWorkerResponseTypeDefault, |
711 ServiceWorkerHeaderMap(), | 687 ServiceWorkerHeaderMap(), "", 0, GURL()); |
712 "", | |
713 0, | |
714 GURL()); | |
715 response.headers["content-type"] = "foo"; | 688 response.headers["content-type"] = "foo"; |
716 response.headers["Content-Type"] = "bar"; | 689 response.headers["Content-Type"] = "bar"; |
717 EXPECT_EQ("bar", response.headers["content-type"]); | 690 EXPECT_EQ("bar", response.headers["content-type"]); |
718 } | 691 } |
719 | 692 |
720 TEST_F(ServiceWorkerCacheTest, CaselessServiceWorkerFetchRequestHeaders) { | 693 TEST_F(CacheStorageCacheTest, CaselessServiceWorkerFetchRequestHeaders) { |
721 // ServiceWorkerCache depends on ServiceWorkerFetchRequest having caseless | 694 // CacheStorageCache depends on ServiceWorkerFetchRequest having caseless |
722 // headers so that it can quickly lookup vary headers. | 695 // headers so that it can quickly lookup vary headers. |
723 ServiceWorkerFetchRequest request(GURL("http://www.example.com"), | 696 ServiceWorkerFetchRequest request(GURL("http://www.example.com"), "GET", |
724 "GET", | 697 ServiceWorkerHeaderMap(), Referrer(), |
725 ServiceWorkerHeaderMap(), | 698 false); |
726 Referrer(), | |
727 false); | |
728 request.headers["content-type"] = "foo"; | 699 request.headers["content-type"] = "foo"; |
729 request.headers["Content-Type"] = "bar"; | 700 request.headers["Content-Type"] = "bar"; |
730 EXPECT_EQ("bar", request.headers["content-type"]); | 701 EXPECT_EQ("bar", request.headers["content-type"]); |
731 } | 702 } |
732 | 703 |
733 TEST_P(ServiceWorkerCacheTestP, QuotaManagerModified) { | 704 TEST_P(CacheStorageCacheTestP, QuotaManagerModified) { |
734 EXPECT_EQ(0, quota_manager_proxy_->notify_storage_modified_count()); | 705 EXPECT_EQ(0, quota_manager_proxy_->notify_storage_modified_count()); |
735 | 706 |
736 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); | 707 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); |
737 EXPECT_EQ(1, quota_manager_proxy_->notify_storage_modified_count()); | 708 EXPECT_EQ(1, quota_manager_proxy_->notify_storage_modified_count()); |
738 EXPECT_LT(0, quota_manager_proxy_->last_notified_delta()); | 709 EXPECT_LT(0, quota_manager_proxy_->last_notified_delta()); |
739 int64 sum_delta = quota_manager_proxy_->last_notified_delta(); | 710 int64 sum_delta = quota_manager_proxy_->last_notified_delta(); |
740 | 711 |
741 EXPECT_TRUE(Put(body_request_, body_response_)); | 712 EXPECT_TRUE(Put(body_request_, body_response_)); |
742 EXPECT_EQ(2, quota_manager_proxy_->notify_storage_modified_count()); | 713 EXPECT_EQ(2, quota_manager_proxy_->notify_storage_modified_count()); |
743 EXPECT_LT(sum_delta, quota_manager_proxy_->last_notified_delta()); | 714 EXPECT_LT(sum_delta, quota_manager_proxy_->last_notified_delta()); |
744 sum_delta += quota_manager_proxy_->last_notified_delta(); | 715 sum_delta += quota_manager_proxy_->last_notified_delta(); |
745 | 716 |
746 EXPECT_TRUE(Delete(body_request_)); | 717 EXPECT_TRUE(Delete(body_request_)); |
747 EXPECT_EQ(3, quota_manager_proxy_->notify_storage_modified_count()); | 718 EXPECT_EQ(3, quota_manager_proxy_->notify_storage_modified_count()); |
748 sum_delta += quota_manager_proxy_->last_notified_delta(); | 719 sum_delta += quota_manager_proxy_->last_notified_delta(); |
749 | 720 |
750 EXPECT_TRUE(Delete(no_body_request_)); | 721 EXPECT_TRUE(Delete(no_body_request_)); |
751 EXPECT_EQ(4, quota_manager_proxy_->notify_storage_modified_count()); | 722 EXPECT_EQ(4, quota_manager_proxy_->notify_storage_modified_count()); |
752 sum_delta += quota_manager_proxy_->last_notified_delta(); | 723 sum_delta += quota_manager_proxy_->last_notified_delta(); |
753 | 724 |
754 EXPECT_EQ(0, sum_delta); | 725 EXPECT_EQ(0, sum_delta); |
755 } | 726 } |
756 | 727 |
757 TEST_F(ServiceWorkerCacheMemoryOnlyTest, MemoryBackedSize) { | 728 TEST_F(CacheStorageCacheMemoryOnlyTest, MemoryBackedSize) { |
758 EXPECT_EQ(0, cache_->MemoryBackedSize()); | 729 EXPECT_EQ(0, cache_->MemoryBackedSize()); |
759 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); | 730 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); |
760 EXPECT_LT(0, cache_->MemoryBackedSize()); | 731 EXPECT_LT(0, cache_->MemoryBackedSize()); |
761 int64 no_body_size = cache_->MemoryBackedSize(); | 732 int64 no_body_size = cache_->MemoryBackedSize(); |
762 | 733 |
763 EXPECT_TRUE(Delete(no_body_request_)); | 734 EXPECT_TRUE(Delete(no_body_request_)); |
764 EXPECT_EQ(0, cache_->MemoryBackedSize()); | 735 EXPECT_EQ(0, cache_->MemoryBackedSize()); |
765 | 736 |
766 EXPECT_TRUE(Put(body_request_, body_response_)); | 737 EXPECT_TRUE(Put(body_request_, body_response_)); |
767 EXPECT_LT(no_body_size, cache_->MemoryBackedSize()); | 738 EXPECT_LT(no_body_size, cache_->MemoryBackedSize()); |
768 | 739 |
769 EXPECT_TRUE(Delete(body_request_)); | 740 EXPECT_TRUE(Delete(body_request_)); |
770 EXPECT_EQ(0, cache_->MemoryBackedSize()); | 741 EXPECT_EQ(0, cache_->MemoryBackedSize()); |
771 } | 742 } |
772 | 743 |
773 TEST_F(ServiceWorkerCacheTest, MemoryBackedSizePersistent) { | 744 TEST_F(CacheStorageCacheTest, MemoryBackedSizePersistent) { |
774 EXPECT_EQ(0, cache_->MemoryBackedSize()); | 745 EXPECT_EQ(0, cache_->MemoryBackedSize()); |
775 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); | 746 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); |
776 EXPECT_EQ(0, cache_->MemoryBackedSize()); | 747 EXPECT_EQ(0, cache_->MemoryBackedSize()); |
777 } | 748 } |
778 | 749 |
779 TEST_P(ServiceWorkerCacheTestP, OpsFailOnClosedBackendNeverCreated) { | 750 TEST_P(CacheStorageCacheTestP, OpsFailOnClosedBackendNeverCreated) { |
780 cache_->set_delay_backend_creation( | 751 cache_->set_delay_backend_creation( |
781 true); // Will hang the test if a backend is created. | 752 true); // Will hang the test if a backend is created. |
782 EXPECT_TRUE(Close()); | 753 EXPECT_TRUE(Close()); |
783 VerifyAllOpsFail(); | 754 VerifyAllOpsFail(); |
784 } | 755 } |
785 | 756 |
786 TEST_P(ServiceWorkerCacheTestP, OpsFailOnClosedBackend) { | 757 TEST_P(CacheStorageCacheTestP, OpsFailOnClosedBackend) { |
787 // Create the backend and put something in it. | 758 // Create the backend and put something in it. |
788 EXPECT_TRUE(Put(body_request_, body_response_)); | 759 EXPECT_TRUE(Put(body_request_, body_response_)); |
789 EXPECT_TRUE(Close()); | 760 EXPECT_TRUE(Close()); |
790 VerifyAllOpsFail(); | 761 VerifyAllOpsFail(); |
791 } | 762 } |
792 | 763 |
793 TEST_P(ServiceWorkerCacheTestP, VerifySerialScheduling) { | 764 TEST_P(CacheStorageCacheTestP, VerifySerialScheduling) { |
794 // Start two operations, the first one is delayed but the second isn't. The | 765 // Start two operations, the first one is delayed but the second isn't. The |
795 // second should wait for the first. | 766 // second should wait for the first. |
796 EXPECT_TRUE(Keys()); // Opens the backend. | 767 EXPECT_TRUE(Keys()); // Opens the backend. |
797 DelayableBackend* delayable_backend = cache_->UseDelayableBackend(); | 768 DelayableBackend* delayable_backend = cache_->UseDelayableBackend(); |
798 delayable_backend->set_delay_open(true); | 769 delayable_backend->set_delay_open(true); |
799 | 770 |
800 scoped_ptr<ServiceWorkerResponse> response1 = | 771 scoped_ptr<ServiceWorkerResponse> response1 = |
801 CopyFetchResponse(body_response_); | 772 CopyFetchResponse(body_response_); |
802 response1->status_code = 1; | 773 response1->status_code = 1; |
803 | 774 |
804 scoped_ptr<base::RunLoop> close_loop1(new base::RunLoop()); | 775 scoped_ptr<base::RunLoop> close_loop1(new base::RunLoop()); |
805 cache_->Put(CopyFetchRequest(body_request_), response1.Pass(), | 776 cache_->Put(CopyFetchRequest(body_request_), response1.Pass(), |
806 base::Bind(&ServiceWorkerCacheTest::ResponseAndErrorCallback, | 777 base::Bind(&CacheStorageCacheTest::ResponseAndErrorCallback, |
807 base::Unretained(this), close_loop1.get())); | 778 base::Unretained(this), close_loop1.get())); |
808 | 779 |
809 // Blocks on opening the cache entry. | 780 // Blocks on opening the cache entry. |
810 base::RunLoop().RunUntilIdle(); | 781 base::RunLoop().RunUntilIdle(); |
811 | 782 |
812 delayable_backend->set_delay_open(false); | 783 delayable_backend->set_delay_open(false); |
813 scoped_ptr<ServiceWorkerResponse> response2 = | 784 scoped_ptr<ServiceWorkerResponse> response2 = |
814 CopyFetchResponse(body_response_); | 785 CopyFetchResponse(body_response_); |
815 response2->status_code = 2; | 786 response2->status_code = 2; |
816 scoped_ptr<base::RunLoop> close_loop2(new base::RunLoop()); | 787 scoped_ptr<base::RunLoop> close_loop2(new base::RunLoop()); |
817 cache_->Put(CopyFetchRequest(body_request_), response2.Pass(), | 788 cache_->Put(CopyFetchRequest(body_request_), response2.Pass(), |
818 base::Bind(&ServiceWorkerCacheTest::ResponseAndErrorCallback, | 789 base::Bind(&CacheStorageCacheTest::ResponseAndErrorCallback, |
819 base::Unretained(this), close_loop2.get())); | 790 base::Unretained(this), close_loop2.get())); |
820 | 791 |
821 // The second put operation should wait for the first to complete. | 792 // The second put operation should wait for the first to complete. |
822 base::RunLoop().RunUntilIdle(); | 793 base::RunLoop().RunUntilIdle(); |
823 EXPECT_FALSE(callback_response_); | 794 EXPECT_FALSE(callback_response_); |
824 | 795 |
825 delayable_backend->OpenEntryContinue(); | 796 delayable_backend->OpenEntryContinue(); |
826 close_loop1->Run(); | 797 close_loop1->Run(); |
827 EXPECT_EQ(1, callback_response_->status_code); | 798 EXPECT_EQ(1, callback_response_->status_code); |
828 close_loop2->Run(); | 799 close_loop2->Run(); |
829 EXPECT_EQ(2, callback_response_->status_code); | 800 EXPECT_EQ(2, callback_response_->status_code); |
830 } | 801 } |
831 | 802 |
832 INSTANTIATE_TEST_CASE_P(ServiceWorkerCacheTest, | 803 INSTANTIATE_TEST_CASE_P(CacheStorageCacheTest, |
833 ServiceWorkerCacheTestP, | 804 CacheStorageCacheTestP, |
834 ::testing::Values(false, true)); | 805 ::testing::Values(false, true)); |
835 | 806 |
836 } // namespace content | 807 } // namespace content |
OLD | NEW |