OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <string> | 5 #include <string> |
6 | 6 |
7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 bool was_called = false; | 266 bool was_called = false; |
267 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED; | 267 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED; |
268 storage()->UpdateToActiveState( | 268 storage()->UpdateToActiveState( |
269 registration, MakeStatusCallback(&was_called, &result)); | 269 registration, MakeStatusCallback(&was_called, &result)); |
270 EXPECT_FALSE(was_called); // always async | 270 EXPECT_FALSE(was_called); // always async |
271 base::RunLoop().RunUntilIdle(); | 271 base::RunLoop().RunUntilIdle(); |
272 EXPECT_TRUE(was_called); | 272 EXPECT_TRUE(was_called); |
273 return result; | 273 return result; |
274 } | 274 } |
275 | 275 |
| 276 void UpdateLastUpdateCheckTime(ServiceWorkerRegistration* registration) { |
| 277 storage()->UpdateLastUpdateCheckTime(registration); |
| 278 base::RunLoop().RunUntilIdle(); |
| 279 } |
| 280 |
276 ServiceWorkerStatusCode FindRegistrationForDocument( | 281 ServiceWorkerStatusCode FindRegistrationForDocument( |
277 const GURL& document_url, | 282 const GURL& document_url, |
278 scoped_refptr<ServiceWorkerRegistration>* registration) { | 283 scoped_refptr<ServiceWorkerRegistration>* registration) { |
279 bool was_called = false; | 284 bool was_called = false; |
280 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED; | 285 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED; |
281 storage()->FindRegistrationForDocument( | 286 storage()->FindRegistrationForDocument( |
282 document_url, MakeFindCallback(&was_called, &result, registration)); | 287 document_url, MakeFindCallback(&was_called, &result, registration)); |
283 base::RunLoop().RunUntilIdle(); | 288 base::RunLoop().RunUntilIdle(); |
284 EXPECT_TRUE(was_called); | 289 EXPECT_TRUE(was_called); |
285 return result; | 290 return result; |
(...skipping 30 matching lines...) Expand all Loading... |
316 base::WeakPtr<ServiceWorkerContextCore> context_ptr_; | 321 base::WeakPtr<ServiceWorkerContextCore> context_ptr_; |
317 TestBrowserThreadBundle browser_thread_bundle_; | 322 TestBrowserThreadBundle browser_thread_bundle_; |
318 }; | 323 }; |
319 | 324 |
320 TEST_F(ServiceWorkerStorageTest, StoreFindUpdateDeleteRegistration) { | 325 TEST_F(ServiceWorkerStorageTest, StoreFindUpdateDeleteRegistration) { |
321 const GURL kScope("http://www.test.not/scope/"); | 326 const GURL kScope("http://www.test.not/scope/"); |
322 const GURL kScript("http://www.test.not/script.js"); | 327 const GURL kScript("http://www.test.not/script.js"); |
323 const GURL kDocumentUrl("http://www.test.not/scope/document.html"); | 328 const GURL kDocumentUrl("http://www.test.not/scope/document.html"); |
324 const int64 kRegistrationId = 0; | 329 const int64 kRegistrationId = 0; |
325 const int64 kVersionId = 0; | 330 const int64 kVersionId = 0; |
| 331 const base::Time kToday = base::Time::Now(); |
| 332 const base::Time kYesterday = kToday - base::TimeDelta::FromDays(1); |
326 | 333 |
327 scoped_refptr<ServiceWorkerRegistration> found_registration; | 334 scoped_refptr<ServiceWorkerRegistration> found_registration; |
328 | 335 |
329 // We shouldn't find anything without having stored anything. | 336 // We shouldn't find anything without having stored anything. |
330 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, | 337 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, |
331 FindRegistrationForDocument(kDocumentUrl, &found_registration)); | 338 FindRegistrationForDocument(kDocumentUrl, &found_registration)); |
332 EXPECT_FALSE(found_registration); | 339 EXPECT_FALSE(found_registration); |
333 | 340 |
334 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, | 341 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, |
335 FindRegistrationForPattern(kScope, &found_registration)); | 342 FindRegistrationForPattern(kScope, &found_registration)); |
336 EXPECT_FALSE(found_registration); | 343 EXPECT_FALSE(found_registration); |
337 | 344 |
338 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, | 345 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, |
339 FindRegistrationForId( | 346 FindRegistrationForId( |
340 kRegistrationId, kScope.GetOrigin(), &found_registration)); | 347 kRegistrationId, kScope.GetOrigin(), &found_registration)); |
341 EXPECT_FALSE(found_registration); | 348 EXPECT_FALSE(found_registration); |
342 | 349 |
343 // Store something. | 350 // Store something. |
344 scoped_refptr<ServiceWorkerRegistration> live_registration = | 351 scoped_refptr<ServiceWorkerRegistration> live_registration = |
345 new ServiceWorkerRegistration( | 352 new ServiceWorkerRegistration( |
346 kScope, kScript, kRegistrationId, context_ptr_); | 353 kScope, kScript, kRegistrationId, context_ptr_); |
347 scoped_refptr<ServiceWorkerVersion> live_version = | 354 scoped_refptr<ServiceWorkerVersion> live_version = |
348 new ServiceWorkerVersion( | 355 new ServiceWorkerVersion( |
349 live_registration, kVersionId, context_ptr_); | 356 live_registration, kVersionId, context_ptr_); |
350 live_version->SetStatus(ServiceWorkerVersion::INSTALLED); | 357 live_version->SetStatus(ServiceWorkerVersion::INSTALLED); |
351 live_registration->SetWaitingVersion(live_version); | 358 live_registration->SetWaitingVersion(live_version); |
| 359 live_registration->set_last_update_check(kYesterday); |
352 EXPECT_EQ(SERVICE_WORKER_OK, | 360 EXPECT_EQ(SERVICE_WORKER_OK, |
353 StoreRegistration(live_registration, live_version)); | 361 StoreRegistration(live_registration, live_version)); |
354 | 362 |
355 // Now we should find it and get the live ptr back immediately. | 363 // Now we should find it and get the live ptr back immediately. |
356 EXPECT_EQ(SERVICE_WORKER_OK, | 364 EXPECT_EQ(SERVICE_WORKER_OK, |
357 FindRegistrationForDocument(kDocumentUrl, &found_registration)); | 365 FindRegistrationForDocument(kDocumentUrl, &found_registration)); |
358 EXPECT_EQ(live_registration, found_registration); | 366 EXPECT_EQ(live_registration, found_registration); |
359 found_registration = NULL; | 367 found_registration = NULL; |
360 | 368 |
361 // But FindRegistrationForPattern is always async. | 369 // But FindRegistrationForPattern is always async. |
(...skipping 27 matching lines...) Expand all Loading... |
389 live_version = NULL; | 397 live_version = NULL; |
390 | 398 |
391 // And FindRegistrationForPattern is always async. | 399 // And FindRegistrationForPattern is always async. |
392 EXPECT_EQ(SERVICE_WORKER_OK, | 400 EXPECT_EQ(SERVICE_WORKER_OK, |
393 FindRegistrationForPattern(kScope, &found_registration)); | 401 FindRegistrationForPattern(kScope, &found_registration)); |
394 ASSERT_TRUE(found_registration); | 402 ASSERT_TRUE(found_registration); |
395 EXPECT_EQ(kRegistrationId, found_registration->id()); | 403 EXPECT_EQ(kRegistrationId, found_registration->id()); |
396 EXPECT_TRUE(found_registration->HasOneRef()); | 404 EXPECT_TRUE(found_registration->HasOneRef()); |
397 EXPECT_FALSE(found_registration->active_version()); | 405 EXPECT_FALSE(found_registration->active_version()); |
398 ASSERT_TRUE(found_registration->waiting_version()); | 406 ASSERT_TRUE(found_registration->waiting_version()); |
| 407 EXPECT_EQ(kYesterday, found_registration->last_update_check()); |
399 EXPECT_EQ(ServiceWorkerVersion::INSTALLED, | 408 EXPECT_EQ(ServiceWorkerVersion::INSTALLED, |
400 found_registration->waiting_version()->status()); | 409 found_registration->waiting_version()->status()); |
401 | 410 |
402 // Update to active. | 411 // Update to active and update the last check time. |
403 scoped_refptr<ServiceWorkerVersion> temp_version = | 412 scoped_refptr<ServiceWorkerVersion> temp_version = |
404 found_registration->waiting_version(); | 413 found_registration->waiting_version(); |
405 temp_version->SetStatus(ServiceWorkerVersion::ACTIVATED); | 414 temp_version->SetStatus(ServiceWorkerVersion::ACTIVATED); |
406 found_registration->SetActiveVersion(temp_version); | 415 found_registration->SetActiveVersion(temp_version); |
407 temp_version = NULL; | 416 temp_version = NULL; |
408 EXPECT_EQ(SERVICE_WORKER_OK, UpdateToActiveState(found_registration)); | 417 EXPECT_EQ(SERVICE_WORKER_OK, UpdateToActiveState(found_registration)); |
| 418 found_registration->set_last_update_check(kToday); |
| 419 UpdateLastUpdateCheckTime(found_registration); |
| 420 |
409 found_registration = NULL; | 421 found_registration = NULL; |
410 | 422 |
411 // Trying to update a unstored registration to active should fail. | 423 // Trying to update a unstored registration to active should fail. |
412 scoped_refptr<ServiceWorkerRegistration> unstored_registration = | 424 scoped_refptr<ServiceWorkerRegistration> unstored_registration = |
413 new ServiceWorkerRegistration( | 425 new ServiceWorkerRegistration( |
414 kScope, kScript, kRegistrationId + 1, context_ptr_); | 426 kScope, kScript, kRegistrationId + 1, context_ptr_); |
415 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, | 427 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, |
416 UpdateToActiveState(unstored_registration)); | 428 UpdateToActiveState(unstored_registration)); |
417 unstored_registration = NULL; | 429 unstored_registration = NULL; |
418 | 430 |
419 // The Find methods should return a registration with an active version. | 431 // The Find methods should return a registration with an active version |
| 432 // and the expected update time. |
420 EXPECT_EQ(SERVICE_WORKER_OK, | 433 EXPECT_EQ(SERVICE_WORKER_OK, |
421 FindRegistrationForDocument(kDocumentUrl, &found_registration)); | 434 FindRegistrationForDocument(kDocumentUrl, &found_registration)); |
422 ASSERT_TRUE(found_registration); | 435 ASSERT_TRUE(found_registration); |
423 EXPECT_EQ(kRegistrationId, found_registration->id()); | 436 EXPECT_EQ(kRegistrationId, found_registration->id()); |
424 EXPECT_TRUE(found_registration->HasOneRef()); | 437 EXPECT_TRUE(found_registration->HasOneRef()); |
425 EXPECT_FALSE(found_registration->waiting_version()); | 438 EXPECT_FALSE(found_registration->waiting_version()); |
426 ASSERT_TRUE(found_registration->active_version()); | 439 ASSERT_TRUE(found_registration->active_version()); |
427 EXPECT_EQ(ServiceWorkerVersion::ACTIVATED, | 440 EXPECT_EQ(ServiceWorkerVersion::ACTIVATED, |
428 found_registration->active_version()->status()); | 441 found_registration->active_version()->status()); |
| 442 EXPECT_EQ(kToday, found_registration->last_update_check()); |
429 | 443 |
430 // Delete from storage but with a instance still live. | 444 // Delete from storage but with a instance still live. |
431 EXPECT_TRUE(context_->GetLiveVersion(kRegistrationId)); | 445 EXPECT_TRUE(context_->GetLiveVersion(kRegistrationId)); |
432 EXPECT_EQ(SERVICE_WORKER_OK, | 446 EXPECT_EQ(SERVICE_WORKER_OK, |
433 DeleteRegistration(kRegistrationId, kScope.GetOrigin())); | 447 DeleteRegistration(kRegistrationId, kScope.GetOrigin())); |
434 EXPECT_TRUE(context_->GetLiveVersion(kRegistrationId)); | 448 EXPECT_TRUE(context_->GetLiveVersion(kRegistrationId)); |
435 | 449 |
436 // Should no longer be found. | 450 // Should no longer be found. |
437 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, | 451 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, |
438 FindRegistrationForId( | 452 FindRegistrationForId( |
(...skipping 583 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1022 are_equal = true; | 1036 are_equal = true; |
1023 storage()->CompareScriptResources( | 1037 storage()->CompareScriptResources( |
1024 5, 6, | 1038 5, 6, |
1025 base::Bind(&OnCompareComplete, &status, &are_equal)); | 1039 base::Bind(&OnCompareComplete, &status, &are_equal)); |
1026 base::RunLoop().RunUntilIdle(); | 1040 base::RunLoop().RunUntilIdle(); |
1027 EXPECT_EQ(SERVICE_WORKER_OK, status); | 1041 EXPECT_EQ(SERVICE_WORKER_OK, status); |
1028 EXPECT_FALSE(are_equal); | 1042 EXPECT_FALSE(are_equal); |
1029 } | 1043 } |
1030 | 1044 |
1031 } // namespace content | 1045 } // namespace content |
OLD | NEW |