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