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