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/service_worker/service_worker_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/common/service_worker/service_worker_types.h" | 13 #include "content/common/service_worker/service_worker_types.h" |
14 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
15 #include "content/public/test/test_browser_context.h" | 15 #include "content/public/test/test_browser_context.h" |
16 #include "content/public/test/test_browser_thread_bundle.h" | 16 #include "content/public/test/test_browser_thread_bundle.h" |
| 17 #include "net/disk_cache/simple/simple_backend_impl.h" |
17 #include "net/url_request/url_request_context.h" | 18 #include "net/url_request/url_request_context.h" |
18 #include "net/url_request/url_request_context_getter.h" | 19 #include "net/url_request/url_request_context_getter.h" |
19 #include "net/url_request/url_request_job_factory_impl.h" | 20 #include "net/url_request/url_request_job_factory_impl.h" |
20 #include "storage/browser/blob/blob_data_handle.h" | 21 #include "storage/browser/blob/blob_data_handle.h" |
21 #include "storage/browser/blob/blob_storage_context.h" | 22 #include "storage/browser/blob/blob_storage_context.h" |
22 #include "storage/browser/blob/blob_url_request_job_factory.h" | 23 #include "storage/browser/blob/blob_url_request_job_factory.h" |
23 #include "storage/common/blob/blob_data.h" | 24 #include "storage/common/blob/blob_data.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
25 | 26 |
26 namespace content { | 27 namespace content { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 } else { | 72 } else { |
72 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 73 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
73 cache_ = ServiceWorkerCache::CreatePersistentCache( | 74 cache_ = ServiceWorkerCache::CreatePersistentCache( |
74 temp_dir_.path(), | 75 temp_dir_.path(), |
75 url_request_context, | 76 url_request_context, |
76 blob_storage_context->context()->AsWeakPtr()); | 77 blob_storage_context->context()->AsWeakPtr()); |
77 } | 78 } |
78 CreateBackend(); | 79 CreateBackend(); |
79 } | 80 } |
80 | 81 |
| 82 virtual void TearDown() OVERRIDE { |
| 83 base::RunLoop().RunUntilIdle(); |
| 84 disk_cache::SimpleBackendImpl::FlushWorkerPoolForTesting(); |
| 85 base::RunLoop().RunUntilIdle(); |
| 86 cache_.reset(); |
| 87 base::RunLoop().RunUntilIdle(); |
| 88 disk_cache::SimpleBackendImpl::FlushWorkerPoolForTesting(); |
| 89 base::RunLoop().RunUntilIdle(); |
| 90 } |
| 91 |
81 void CreateRequests(ChromeBlobStorageContext* blob_storage_context) { | 92 void CreateRequests(ChromeBlobStorageContext* blob_storage_context) { |
82 std::map<std::string, std::string> headers; | 93 std::map<std::string, std::string> headers; |
83 headers.insert(std::make_pair("a", "a")); | 94 headers.insert(std::make_pair("a", "a")); |
84 headers.insert(std::make_pair("b", "b")); | 95 headers.insert(std::make_pair("b", "b")); |
85 body_request_.reset(new ServiceWorkerFetchRequest( | 96 body_request_.reset(new ServiceWorkerFetchRequest( |
86 GURL("http://example.com/body.html"), "GET", headers, GURL(""), false)); | 97 GURL("http://example.com/body.html"), "GET", headers, GURL(""), false)); |
87 no_body_request_.reset( | 98 no_body_request_.reset( |
88 new ServiceWorkerFetchRequest(GURL("http://example.com/no_body.html"), | 99 new ServiceWorkerFetchRequest(GURL("http://example.com/no_body.html"), |
89 "GET", | 100 "GET", |
90 headers, | 101 headers, |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 | 167 |
157 cache_->Delete(request, | 168 cache_->Delete(request, |
158 base::Bind(&ServiceWorkerCacheTest::ErrorTypeCallback, | 169 base::Bind(&ServiceWorkerCacheTest::ErrorTypeCallback, |
159 base::Unretained(this), | 170 base::Unretained(this), |
160 base::Unretained(loop.get()))); | 171 base::Unretained(loop.get()))); |
161 loop->Run(); | 172 loop->Run(); |
162 | 173 |
163 return callback_error_ == ServiceWorkerCache::ErrorTypeOK; | 174 return callback_error_ == ServiceWorkerCache::ErrorTypeOK; |
164 } | 175 } |
165 | 176 |
| 177 bool Keys() { |
| 178 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); |
| 179 |
| 180 cache_->Keys(base::Bind(&ServiceWorkerCacheTest::RequestsCallback, |
| 181 base::Unretained(this), |
| 182 base::Unretained(loop.get()))); |
| 183 loop->Run(); |
| 184 |
| 185 return callback_error_ == ServiceWorkerCache::ErrorTypeOK; |
| 186 } |
| 187 |
| 188 void RequestsCallback(base::RunLoop* run_loop, |
| 189 ServiceWorkerCache::ErrorType error, |
| 190 scoped_ptr<ServiceWorkerCache::Requests> requests) { |
| 191 callback_error_ = error; |
| 192 callback_strings_.clear(); |
| 193 for (size_t i = 0u; i < requests->size(); ++i) |
| 194 callback_strings_.push_back(requests->at(i).url.spec()); |
| 195 run_loop->Quit(); |
| 196 } |
| 197 |
166 void ErrorTypeCallback(base::RunLoop* run_loop, | 198 void ErrorTypeCallback(base::RunLoop* run_loop, |
167 ServiceWorkerCache::ErrorType error) { | 199 ServiceWorkerCache::ErrorType error) { |
168 callback_error_ = error; | 200 callback_error_ = error; |
169 run_loop->Quit(); | 201 run_loop->Quit(); |
170 } | 202 } |
171 | 203 |
172 void ResponseAndErrorCallback( | 204 void ResponseAndErrorCallback( |
173 base::RunLoop* run_loop, | 205 base::RunLoop* run_loop, |
174 ServiceWorkerCache::ErrorType error, | 206 ServiceWorkerCache::ErrorType error, |
175 scoped_ptr<ServiceWorkerResponse> response, | 207 scoped_ptr<ServiceWorkerResponse> response, |
176 scoped_ptr<storage::BlobDataHandle> body_handle) { | 208 scoped_ptr<storage::BlobDataHandle> body_handle) { |
177 callback_error_ = error; | 209 callback_error_ = error; |
178 callback_response_ = response.Pass(); | 210 callback_response_ = response.Pass(); |
179 | 211 |
180 if (error == ServiceWorkerCache::ErrorTypeOK && | 212 if (error == ServiceWorkerCache::ErrorTypeOK && |
181 !callback_response_->blob_uuid.empty()) { | 213 !callback_response_->blob_uuid.empty()) { |
182 callback_response_data_ = body_handle.Pass(); | 214 callback_response_data_ = body_handle.Pass(); |
183 } | 215 } |
184 | 216 |
185 run_loop->Quit(); | 217 run_loop->Quit(); |
186 } | 218 } |
187 | 219 |
188 void CopyBody(storage::BlobDataHandle* blob_handle, std::string* output) { | 220 void CopyBody(storage::BlobDataHandle* blob_handle, std::string* output) { |
189 storage::BlobData* data = blob_handle->data(); | 221 storage::BlobData* data = blob_handle->data(); |
190 std::vector<storage::BlobData::Item> items = data->items(); | 222 std::vector<storage::BlobData::Item> items = data->items(); |
191 for (size_t i = 0, max = items.size(); i < max; ++i) | 223 for (size_t i = 0, max = items.size(); i < max; ++i) |
192 output->append(items[i].bytes(), items[i].length()); | 224 output->append(items[i].bytes(), items[i].length()); |
193 } | 225 } |
194 | 226 |
| 227 bool VerifyKeys(const std::vector<std::string>& expected_keys) { |
| 228 if (expected_keys.size() != callback_strings_.size()) |
| 229 return false; |
| 230 |
| 231 std::set<std::string> found_set; |
| 232 for (int i = 0, max = callback_strings_.size(); i < max; ++i) |
| 233 found_set.insert(callback_strings_[i]); |
| 234 |
| 235 for (int i = 0, max = expected_keys.size(); i < max; ++i) { |
| 236 if (found_set.find(expected_keys[i]) == found_set.end()) |
| 237 return false; |
| 238 } |
| 239 return true; |
| 240 } |
| 241 |
195 virtual bool MemoryOnly() { return false; } | 242 virtual bool MemoryOnly() { return false; } |
196 | 243 |
197 protected: | 244 protected: |
198 TestBrowserContext browser_context_; | 245 TestBrowserContext browser_context_; |
199 TestBrowserThreadBundle browser_thread_bundle_; | 246 TestBrowserThreadBundle browser_thread_bundle_; |
200 scoped_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_; | 247 scoped_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_; |
201 storage::BlobStorageContext* blob_storage_context_; | 248 storage::BlobStorageContext* blob_storage_context_; |
202 | 249 |
203 base::ScopedTempDir temp_dir_; | 250 base::ScopedTempDir temp_dir_; |
204 scoped_ptr<ServiceWorkerCache> cache_; | 251 scoped_ptr<ServiceWorkerCache> cache_; |
205 | 252 |
206 scoped_ptr<ServiceWorkerFetchRequest> body_request_; | 253 scoped_ptr<ServiceWorkerFetchRequest> body_request_; |
207 scoped_ptr<ServiceWorkerResponse> body_response_; | 254 scoped_ptr<ServiceWorkerResponse> body_response_; |
208 scoped_ptr<ServiceWorkerFetchRequest> no_body_request_; | 255 scoped_ptr<ServiceWorkerFetchRequest> no_body_request_; |
209 scoped_ptr<ServiceWorkerResponse> no_body_response_; | 256 scoped_ptr<ServiceWorkerResponse> no_body_response_; |
210 scoped_ptr<storage::BlobDataHandle> blob_handle_; | 257 scoped_ptr<storage::BlobDataHandle> blob_handle_; |
211 std::string expected_blob_data_; | 258 std::string expected_blob_data_; |
212 | 259 |
213 ServiceWorkerCache::ErrorType callback_error_; | 260 ServiceWorkerCache::ErrorType callback_error_; |
214 scoped_ptr<ServiceWorkerResponse> callback_response_; | 261 scoped_ptr<ServiceWorkerResponse> callback_response_; |
215 scoped_ptr<storage::BlobDataHandle> callback_response_data_; | 262 scoped_ptr<storage::BlobDataHandle> callback_response_data_; |
| 263 std::vector<std::string> callback_strings_; |
216 }; | 264 }; |
217 | 265 |
218 class ServiceWorkerCacheTestP : public ServiceWorkerCacheTest, | 266 class ServiceWorkerCacheTestP : public ServiceWorkerCacheTest, |
219 public testing::WithParamInterface<bool> { | 267 public testing::WithParamInterface<bool> { |
220 virtual bool MemoryOnly() OVERRIDE { return !GetParam(); } | 268 virtual bool MemoryOnly() OVERRIDE { return !GetParam(); } |
221 }; | 269 }; |
222 | 270 |
223 TEST_P(ServiceWorkerCacheTestP, PutNoBody) { | 271 TEST_P(ServiceWorkerCacheTestP, PutNoBody) { |
224 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); | 272 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); |
225 } | 273 } |
(...skipping 10 matching lines...) Expand all Loading... |
236 base::Unretained(this), | 284 base::Unretained(this), |
237 base::Unretained(loop.get()))); | 285 base::Unretained(loop.get()))); |
238 // The handle should be held by the cache now so the deref here should be | 286 // The handle should be held by the cache now so the deref here should be |
239 // okay. | 287 // okay. |
240 blob_handle_.reset(); | 288 blob_handle_.reset(); |
241 loop->Run(); | 289 loop->Run(); |
242 | 290 |
243 EXPECT_EQ(ServiceWorkerCache::ErrorTypeOK, callback_error_); | 291 EXPECT_EQ(ServiceWorkerCache::ErrorTypeOK, callback_error_); |
244 } | 292 } |
245 | 293 |
| 294 TEST_P(ServiceWorkerCacheTestP, MatchNoBody) { |
| 295 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); |
| 296 EXPECT_TRUE(Match(no_body_request_.get())); |
| 297 EXPECT_EQ(200, callback_response_->status_code); |
| 298 EXPECT_STREQ("OK", callback_response_->status_text.c_str()); |
| 299 EXPECT_STREQ("http://example.com/no_body.html", |
| 300 callback_response_->url.spec().c_str()); |
| 301 } |
| 302 |
| 303 TEST_P(ServiceWorkerCacheTestP, MatchBody) { |
| 304 EXPECT_TRUE(Put(body_request_.get(), body_response_.get())); |
| 305 EXPECT_TRUE(Match(body_request_.get())); |
| 306 EXPECT_EQ(200, callback_response_->status_code); |
| 307 EXPECT_STREQ("OK", callback_response_->status_text.c_str()); |
| 308 EXPECT_STREQ("http://example.com/body.html", |
| 309 callback_response_->url.spec().c_str()); |
| 310 std::string response_body; |
| 311 CopyBody(callback_response_data_.get(), &response_body); |
| 312 EXPECT_STREQ(expected_blob_data_.c_str(), response_body.c_str()); |
| 313 } |
| 314 |
| 315 TEST_P(ServiceWorkerCacheTestP, EmptyKeys) { |
| 316 EXPECT_TRUE(Keys()); |
| 317 EXPECT_EQ(0u, callback_strings_.size()); |
| 318 } |
| 319 |
| 320 TEST_P(ServiceWorkerCacheTestP, TwoKeys) { |
| 321 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); |
| 322 EXPECT_TRUE(Put(body_request_.get(), body_response_.get())); |
| 323 EXPECT_TRUE(Keys()); |
| 324 EXPECT_EQ(2u, callback_strings_.size()); |
| 325 std::vector<std::string> expected_keys; |
| 326 expected_keys.push_back(no_body_request_->url.spec()); |
| 327 expected_keys.push_back(body_request_->url.spec()); |
| 328 EXPECT_TRUE(VerifyKeys(expected_keys)); |
| 329 } |
| 330 |
| 331 TEST_P(ServiceWorkerCacheTestP, TwoKeysThenOne) { |
| 332 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); |
| 333 EXPECT_TRUE(Put(body_request_.get(), body_response_.get())); |
| 334 EXPECT_TRUE(Keys()); |
| 335 EXPECT_EQ(2u, callback_strings_.size()); |
| 336 std::vector<std::string> expected_keys; |
| 337 expected_keys.push_back(no_body_request_->url.spec()); |
| 338 expected_keys.push_back(body_request_->url.spec()); |
| 339 EXPECT_TRUE(VerifyKeys(expected_keys)); |
| 340 |
| 341 EXPECT_TRUE(Delete(body_request_.get())); |
| 342 EXPECT_TRUE(Keys()); |
| 343 EXPECT_EQ(1u, callback_strings_.size()); |
| 344 std::vector<std::string> expected_key; |
| 345 expected_key.push_back(no_body_request_->url.spec()); |
| 346 EXPECT_TRUE(VerifyKeys(expected_key)); |
| 347 } |
| 348 |
| 349 // TODO(jkarlin): Once SimpleCache is working bug-free on Windows reenable these |
| 350 // tests. In the meanwhile we know that Windows operations will be a little |
| 351 // flaky (though not crashy). See https://crbug.com/409109 |
| 352 #ifndef OS_WIN |
246 TEST_P(ServiceWorkerCacheTestP, DeleteNoBody) { | 353 TEST_P(ServiceWorkerCacheTestP, DeleteNoBody) { |
247 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); | 354 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); |
248 EXPECT_TRUE(Match(no_body_request_.get())); | 355 EXPECT_TRUE(Match(no_body_request_.get())); |
249 EXPECT_TRUE(Delete(no_body_request_.get())); | 356 EXPECT_TRUE(Delete(no_body_request_.get())); |
250 EXPECT_FALSE(Match(no_body_request_.get())); | 357 EXPECT_FALSE(Match(no_body_request_.get())); |
251 EXPECT_FALSE(Delete(no_body_request_.get())); | 358 EXPECT_FALSE(Delete(no_body_request_.get())); |
252 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); | 359 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); |
253 EXPECT_TRUE(Match(no_body_request_.get())); | 360 EXPECT_TRUE(Match(no_body_request_.get())); |
254 EXPECT_TRUE(Delete(no_body_request_.get())); | 361 EXPECT_TRUE(Delete(no_body_request_.get())); |
255 } | 362 } |
256 | 363 |
257 TEST_P(ServiceWorkerCacheTestP, DeleteBody) { | 364 TEST_P(ServiceWorkerCacheTestP, DeleteBody) { |
258 EXPECT_TRUE(Put(body_request_.get(), body_response_.get())); | 365 EXPECT_TRUE(Put(body_request_.get(), body_response_.get())); |
259 EXPECT_TRUE(Match(body_request_.get())); | 366 EXPECT_TRUE(Match(body_request_.get())); |
260 EXPECT_TRUE(Delete(body_request_.get())); | 367 EXPECT_TRUE(Delete(body_request_.get())); |
261 EXPECT_FALSE(Match(body_request_.get())); | 368 EXPECT_FALSE(Match(body_request_.get())); |
262 EXPECT_FALSE(Delete(body_request_.get())); | 369 EXPECT_FALSE(Delete(body_request_.get())); |
263 EXPECT_TRUE(Put(body_request_.get(), body_response_.get())); | 370 EXPECT_TRUE(Put(body_request_.get(), body_response_.get())); |
264 EXPECT_TRUE(Match(body_request_.get())); | 371 EXPECT_TRUE(Match(body_request_.get())); |
265 EXPECT_TRUE(Delete(body_request_.get())); | 372 EXPECT_TRUE(Delete(body_request_.get())); |
266 } | 373 } |
267 | 374 |
268 TEST_P(ServiceWorkerCacheTestP, MatchNoBody) { | |
269 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); | |
270 EXPECT_TRUE(Match(no_body_request_.get())); | |
271 EXPECT_EQ(200, callback_response_->status_code); | |
272 EXPECT_STREQ("OK", callback_response_->status_text.c_str()); | |
273 EXPECT_STREQ("http://example.com/no_body.html", | |
274 callback_response_->url.spec().c_str()); | |
275 } | |
276 | |
277 TEST_P(ServiceWorkerCacheTestP, MatchBody) { | |
278 EXPECT_TRUE(Put(body_request_.get(), body_response_.get())); | |
279 EXPECT_TRUE(Match(body_request_.get())); | |
280 EXPECT_EQ(200, callback_response_->status_code); | |
281 EXPECT_STREQ("OK", callback_response_->status_text.c_str()); | |
282 EXPECT_STREQ("http://example.com/body.html", | |
283 callback_response_->url.spec().c_str()); | |
284 std::string response_body; | |
285 CopyBody(callback_response_data_.get(), &response_body); | |
286 EXPECT_STREQ(expected_blob_data_.c_str(), response_body.c_str()); | |
287 } | |
288 | |
289 TEST_P(ServiceWorkerCacheTestP, QuickStressNoBody) { | 375 TEST_P(ServiceWorkerCacheTestP, QuickStressNoBody) { |
290 for (int i = 0; i < 100; ++i) { | 376 for (int i = 0; i < 100; ++i) { |
291 EXPECT_FALSE(Match(no_body_request_.get())); | 377 EXPECT_FALSE(Match(no_body_request_.get())); |
292 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); | 378 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); |
293 EXPECT_TRUE(Match(no_body_request_.get())); | 379 EXPECT_TRUE(Match(no_body_request_.get())); |
294 EXPECT_TRUE(Delete(no_body_request_.get())); | 380 EXPECT_TRUE(Delete(no_body_request_.get())); |
295 } | 381 } |
296 } | 382 } |
297 | 383 |
298 TEST_P(ServiceWorkerCacheTestP, QuickStressBody) { | 384 TEST_P(ServiceWorkerCacheTestP, QuickStressBody) { |
299 for (int i = 0; i < 100; ++i) { | 385 for (int i = 0; i < 100; ++i) { |
300 ASSERT_FALSE(Match(body_request_.get())); | 386 ASSERT_FALSE(Match(body_request_.get())); |
301 ASSERT_TRUE(Put(body_request_.get(), body_response_.get())); | 387 ASSERT_TRUE(Put(body_request_.get(), body_response_.get())); |
302 ASSERT_TRUE(Match(body_request_.get())); | 388 ASSERT_TRUE(Match(body_request_.get())); |
303 ASSERT_TRUE(Delete(body_request_.get())); | 389 ASSERT_TRUE(Delete(body_request_.get())); |
304 } | 390 } |
305 } | 391 } |
| 392 #endif // OS_WIN |
306 | 393 |
307 INSTANTIATE_TEST_CASE_P(ServiceWorkerCacheTest, | 394 INSTANTIATE_TEST_CASE_P(ServiceWorkerCacheTest, |
308 ServiceWorkerCacheTestP, | 395 ServiceWorkerCacheTestP, |
309 ::testing::Values(false, true)); | 396 ::testing::Values(false, true)); |
310 | 397 |
311 } // namespace content | 398 } // namespace content |
OLD | NEW |