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_database.h" | 5 #include "content/browser/service_worker/service_worker_database.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
(...skipping 501 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
512 EXPECT_TRUE(ContainsKey(purgeable_ids_out, resources1[1].resource_id)); | 512 EXPECT_TRUE(ContainsKey(purgeable_ids_out, resources1[1].resource_id)); |
513 | 513 |
514 // Make sure that registration2 is still alive. | 514 // Make sure that registration2 is still alive. |
515 resources_out.clear(); | 515 resources_out.clear(); |
516 EXPECT_TRUE(database->ReadRegistration( | 516 EXPECT_TRUE(database->ReadRegistration( |
517 data2.registration_id, origin, &data_out, &resources_out)); | 517 data2.registration_id, origin, &data_out, &resources_out)); |
518 VerifyRegistrationData(data2, data_out); | 518 VerifyRegistrationData(data2, data_out); |
519 VerifyResourceRecords(resources2, resources_out); | 519 VerifyResourceRecords(resources2, resources_out); |
520 } | 520 } |
521 | 521 |
| 522 TEST(ServiceWorkerDatabaseTest, UpdateVersionToActive) { |
| 523 scoped_ptr<ServiceWorkerDatabase> database(CreateDatabaseInMemory()); |
| 524 GURL origin("http://example.com"); |
| 525 |
| 526 // Should be false because a registration does not exist. |
| 527 EXPECT_FALSE(database->UpdateVersionToActive(0, origin)); |
| 528 |
| 529 // Add a registration. |
| 530 RegistrationData data; |
| 531 data.registration_id = 100; |
| 532 data.scope = URL(origin, "/foo"); |
| 533 data.script = URL(origin, "/script.js"); |
| 534 data.version_id = 200; |
| 535 data.is_active = false; |
| 536 EXPECT_TRUE(database->WriteRegistration(data, std::vector<Resource>())); |
| 537 |
| 538 // Make sure that the registration is stored. |
| 539 RegistrationData data_out; |
| 540 std::vector<Resource> resources_out; |
| 541 EXPECT_TRUE(database->ReadRegistration( |
| 542 data.registration_id, origin, &data_out, &resources_out)); |
| 543 VerifyRegistrationData(data, data_out); |
| 544 EXPECT_TRUE(resources_out.empty()); |
| 545 |
| 546 // Activate the registration. |
| 547 EXPECT_TRUE(database->UpdateVersionToActive(data.registration_id, origin)); |
| 548 |
| 549 // Make sure that the registration is activated. |
| 550 resources_out.clear(); |
| 551 EXPECT_TRUE(database->ReadRegistration( |
| 552 data.registration_id, origin, &data_out, &resources_out)); |
| 553 RegistrationData expected_data = data; |
| 554 expected_data.is_active = true; |
| 555 VerifyRegistrationData(expected_data, data_out); |
| 556 EXPECT_TRUE(resources_out.empty()); |
| 557 |
| 558 // Delete the registration. |
| 559 EXPECT_TRUE(database->DeleteRegistration(data.registration_id, origin)); |
| 560 |
| 561 // Should be false because the registration is gone. |
| 562 EXPECT_FALSE(database->UpdateVersionToActive(data.registration_id, origin)); |
| 563 } |
| 564 |
| 565 TEST(ServiceWorkerDatabaseTest, UpdateLastCheckTime) { |
| 566 scoped_ptr<ServiceWorkerDatabase> database(CreateDatabaseInMemory()); |
| 567 GURL origin("http://example.com"); |
| 568 |
| 569 // Should be false because a registration does not exist. |
| 570 EXPECT_FALSE(database->UpdateLastCheckTime(0, origin, base::Time::Now())); |
| 571 |
| 572 // Add a registration. |
| 573 RegistrationData data; |
| 574 data.registration_id = 100; |
| 575 data.scope = URL(origin, "/foo"); |
| 576 data.script = URL(origin, "/script.js"); |
| 577 data.version_id = 200; |
| 578 data.last_update_check = base::Time::Now(); |
| 579 EXPECT_TRUE(database->WriteRegistration(data, std::vector<Resource>())); |
| 580 |
| 581 // Make sure that the registration is stored. |
| 582 RegistrationData data_out; |
| 583 std::vector<Resource> resources_out; |
| 584 EXPECT_TRUE(database->ReadRegistration( |
| 585 data.registration_id, origin, &data_out, &resources_out)); |
| 586 VerifyRegistrationData(data, data_out); |
| 587 EXPECT_TRUE(resources_out.empty()); |
| 588 |
| 589 // Update the last check time. |
| 590 base::Time updated_time = base::Time::Now(); |
| 591 EXPECT_TRUE(database->UpdateLastCheckTime( |
| 592 data.registration_id, origin, updated_time)); |
| 593 |
| 594 // Make sure that the registration is updated. |
| 595 resources_out.clear(); |
| 596 EXPECT_TRUE(database->ReadRegistration( |
| 597 data.registration_id, origin, &data_out, &resources_out)); |
| 598 RegistrationData expected_data = data; |
| 599 expected_data.last_update_check = updated_time; |
| 600 VerifyRegistrationData(expected_data, data_out); |
| 601 EXPECT_TRUE(resources_out.empty()); |
| 602 |
| 603 // Delete the registration. |
| 604 EXPECT_TRUE(database->DeleteRegistration(data.registration_id, origin)); |
| 605 |
| 606 // Should be false because the registration is gone. |
| 607 EXPECT_FALSE(database->UpdateLastCheckTime( |
| 608 data.registration_id, origin, base::Time::Now())); |
| 609 } |
| 610 |
522 TEST(ServiceWorkerDatabaseTest, UncommittedResourceIds) { | 611 TEST(ServiceWorkerDatabaseTest, UncommittedResourceIds) { |
523 scoped_ptr<ServiceWorkerDatabase> database(CreateDatabaseInMemory()); | 612 scoped_ptr<ServiceWorkerDatabase> database(CreateDatabaseInMemory()); |
524 | 613 |
525 // Write {1, 2, 3}. | 614 // Write {1, 2, 3}. |
526 std::set<int64> ids1; | 615 std::set<int64> ids1; |
527 ids1.insert(1); | 616 ids1.insert(1); |
528 ids1.insert(2); | 617 ids1.insert(2); |
529 ids1.insert(3); | 618 ids1.insert(3); |
530 EXPECT_TRUE(database->WriteUncommittedResourceIds(ids1)); | 619 EXPECT_TRUE(database->WriteUncommittedResourceIds(ids1)); |
531 | 620 |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
661 std::set<int64> purgeable_ids_out; | 750 std::set<int64> purgeable_ids_out; |
662 EXPECT_TRUE(database->GetPurgeableResourceIds(&purgeable_ids_out)); | 751 EXPECT_TRUE(database->GetPurgeableResourceIds(&purgeable_ids_out)); |
663 EXPECT_EQ(4u, purgeable_ids_out.size()); | 752 EXPECT_EQ(4u, purgeable_ids_out.size()); |
664 EXPECT_TRUE(ContainsKey(purgeable_ids_out, 1)); | 753 EXPECT_TRUE(ContainsKey(purgeable_ids_out, 1)); |
665 EXPECT_TRUE(ContainsKey(purgeable_ids_out, 2)); | 754 EXPECT_TRUE(ContainsKey(purgeable_ids_out, 2)); |
666 EXPECT_TRUE(ContainsKey(purgeable_ids_out, 3)); | 755 EXPECT_TRUE(ContainsKey(purgeable_ids_out, 3)); |
667 EXPECT_TRUE(ContainsKey(purgeable_ids_out, 4)); | 756 EXPECT_TRUE(ContainsKey(purgeable_ids_out, 4)); |
668 } | 757 } |
669 | 758 |
670 } // namespace content | 759 } // namespace content |
OLD | NEW |