Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(233)

Side by Side Diff: content/browser/service_worker/service_worker_database_unittest.cc

Issue 355163003: Don't prematurely delete script resources when registration is deleted (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/file_util.h" 9 #include "base/file_util.h"
10 #include "base/files/scoped_temp_dir.h" 10 #include "base/files/scoped_temp_dir.h"
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, 247 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK,
248 database->GetOriginsWithRegistrations(&origins)); 248 database->GetOriginsWithRegistrations(&origins));
249 EXPECT_EQ(3U, origins.size()); 249 EXPECT_EQ(3U, origins.size());
250 EXPECT_TRUE(ContainsKey(origins, origin1)); 250 EXPECT_TRUE(ContainsKey(origins, origin1));
251 EXPECT_TRUE(ContainsKey(origins, origin2)); 251 EXPECT_TRUE(ContainsKey(origins, origin2));
252 EXPECT_TRUE(ContainsKey(origins, origin3)); 252 EXPECT_TRUE(ContainsKey(origins, origin3));
253 253
254 // |origin3| has another registration, so should not remove it from the 254 // |origin3| has another registration, so should not remove it from the
255 // unique origin list. 255 // unique origin list.
256 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK, 256 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK,
257 database->DeleteRegistration(data4.registration_id, origin3, 257 database->DeleteRegistration(data4.registration_id, origin3));
258 &newly_purgeable_resources));
259 258
260 origins.clear(); 259 origins.clear();
261 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, 260 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK,
262 database->GetOriginsWithRegistrations(&origins)); 261 database->GetOriginsWithRegistrations(&origins));
263 EXPECT_EQ(3U, origins.size()); 262 EXPECT_EQ(3U, origins.size());
264 EXPECT_TRUE(ContainsKey(origins, origin1)); 263 EXPECT_TRUE(ContainsKey(origins, origin1));
265 EXPECT_TRUE(ContainsKey(origins, origin2)); 264 EXPECT_TRUE(ContainsKey(origins, origin2));
266 EXPECT_TRUE(ContainsKey(origins, origin3)); 265 EXPECT_TRUE(ContainsKey(origins, origin3));
267 266
268 // |origin3| should be removed from the unique origin list. 267 // |origin3| should be removed from the unique origin list.
269 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK, 268 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK,
270 database->DeleteRegistration(data3.registration_id, origin3, 269 database->DeleteRegistration(data3.registration_id, origin3));
271 &newly_purgeable_resources));
272 270
273 origins.clear(); 271 origins.clear();
274 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, 272 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK,
275 database->GetOriginsWithRegistrations(&origins)); 273 database->GetOriginsWithRegistrations(&origins));
276 EXPECT_EQ(2U, origins.size()); 274 EXPECT_EQ(2U, origins.size());
277 EXPECT_TRUE(ContainsKey(origins, origin1)); 275 EXPECT_TRUE(ContainsKey(origins, origin1));
278 EXPECT_TRUE(ContainsKey(origins, origin2)); 276 EXPECT_TRUE(ContainsKey(origins, origin2));
279 } 277 }
280 278
281 TEST(ServiceWorkerDatabaseTest, GetRegistrationsForOrigin) { 279 TEST(ServiceWorkerDatabaseTest, GetRegistrationsForOrigin) {
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
439 VerifyRegistrationData(data, data_out); 437 VerifyRegistrationData(data, data_out);
440 VerifyResourceRecords(resources, resources_out); 438 VerifyResourceRecords(resources, resources_out);
441 439
442 // Make sure that the resource is removed from the uncommitted list. 440 // Make sure that the resource is removed from the uncommitted list.
443 uncommitted_ids_out.clear(); 441 uncommitted_ids_out.clear();
444 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, 442 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK,
445 database->GetUncommittedResourceIds(&uncommitted_ids_out)); 443 database->GetUncommittedResourceIds(&uncommitted_ids_out));
446 EXPECT_TRUE(uncommitted_ids_out.empty()); 444 EXPECT_TRUE(uncommitted_ids_out.empty());
447 445
448 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, 446 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK,
449 database->DeleteRegistration(data.registration_id, origin, 447 database->DeleteRegistration(data.registration_id, origin));
450 &newly_purgeable_resources));
451 ASSERT_EQ(resources.size(), newly_purgeable_resources.size());
452 for (size_t i = 0; i < resources.size(); ++i)
453 EXPECT_EQ(newly_purgeable_resources[i], resources[i].resource_id);
454 448
455 // Make sure that the registration and resource records are gone. 449 // Make sure that the registration and resource records are gone.
456 resources_out.clear(); 450 resources_out.clear();
457 EXPECT_EQ(ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND, 451 EXPECT_EQ(ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND,
458 database->ReadRegistration( 452 database->ReadRegistration(
459 data.registration_id, origin, &data_out, &resources_out)); 453 data.registration_id, origin, &data_out, &resources_out));
460 EXPECT_TRUE(resources_out.empty()); 454 EXPECT_TRUE(resources_out.empty());
461 455
462 // Resources should be purgeable because these are no longer referred. 456 // Resources should be purgeable because these are no longer referred.
463 std::set<int64> purgeable_ids_out; 457 std::set<int64> purgeable_ids_out;
464 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, 458 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK,
465 database->GetPurgeableResourceIds(&purgeable_ids_out)); 459 database->GetPurgeableResourceIds(&purgeable_ids_out));
466 EXPECT_EQ(2u, purgeable_ids_out.size()); 460 EXPECT_EQ(0U, purgeable_ids_out.size());
467 EXPECT_TRUE(ContainsKey(purgeable_ids_out, resources[0].resource_id));
468 EXPECT_TRUE(ContainsKey(purgeable_ids_out, resources[1].resource_id));
469 } 461 }
470 462
471 TEST(ServiceWorkerDatabaseTest, Registration_Overwrite) { 463 TEST(ServiceWorkerDatabaseTest, Registration_Overwrite) {
472 scoped_ptr<ServiceWorkerDatabase> database(CreateDatabaseInMemory()); 464 scoped_ptr<ServiceWorkerDatabase> database(CreateDatabaseInMemory());
473 465
474 GURL origin("http://example.com"); 466 GURL origin("http://example.com");
475 RegistrationData data; 467 RegistrationData data;
476 data.registration_id = 100; 468 data.registration_id = 100;
477 data.scope = URL(origin, "/foo"); 469 data.scope = URL(origin, "/foo");
478 data.script = URL(origin, "/script.js"); 470 data.script = URL(origin, "/script.js");
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
575 VerifyRegistrationData(data2, data_out); 567 VerifyRegistrationData(data2, data_out);
576 VerifyResourceRecords(resources2, resources_out); 568 VerifyResourceRecords(resources2, resources_out);
577 569
578 std::set<int64> purgeable_ids_out; 570 std::set<int64> purgeable_ids_out;
579 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, 571 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK,
580 database->GetPurgeableResourceIds(&purgeable_ids_out)); 572 database->GetPurgeableResourceIds(&purgeable_ids_out));
581 EXPECT_TRUE(purgeable_ids_out.empty()); 573 EXPECT_TRUE(purgeable_ids_out.empty());
582 574
583 // Delete registration1. 575 // Delete registration1.
584 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, 576 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK,
585 database->DeleteRegistration(data1.registration_id, origin, 577 database->DeleteRegistration(data1.registration_id, origin));
586 &newly_purgeable_resources));
587 578
588 // Make sure that registration1 is gone. 579 // Make sure that registration1 is gone.
589 resources_out.clear(); 580 resources_out.clear();
590 EXPECT_EQ(ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND, 581 EXPECT_EQ(ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND,
591 database->ReadRegistration( 582 database->ReadRegistration(
592 data1.registration_id, origin, &data_out, &resources_out)); 583 data1.registration_id, origin, &data_out, &resources_out));
593 EXPECT_TRUE(resources_out.empty()); 584 EXPECT_TRUE(resources_out.empty());
594 585
595 purgeable_ids_out.clear(); 586 purgeable_ids_out.clear();
596 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, 587 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK,
597 database->GetPurgeableResourceIds(&purgeable_ids_out)); 588 database->GetPurgeableResourceIds(&purgeable_ids_out));
598 EXPECT_EQ(2u, purgeable_ids_out.size()); 589 EXPECT_EQ(0u, purgeable_ids_out.size());
599 EXPECT_TRUE(ContainsKey(purgeable_ids_out, resources1[0].resource_id));
600 EXPECT_TRUE(ContainsKey(purgeable_ids_out, resources1[1].resource_id));
601 590
602 // Make sure that registration2 is still alive. 591 // Make sure that registration2 is still alive.
603 resources_out.clear(); 592 resources_out.clear();
604 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, database->ReadRegistration( 593 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, database->ReadRegistration(
605 data2.registration_id, origin, &data_out, &resources_out)); 594 data2.registration_id, origin, &data_out, &resources_out));
606 VerifyRegistrationData(data2, data_out); 595 VerifyRegistrationData(data2, data_out);
607 VerifyResourceRecords(resources2, resources_out); 596 VerifyResourceRecords(resources2, resources_out);
608 } 597 }
609 598
610 TEST(ServiceWorkerDatabaseTest, UpdateVersionToActive) { 599 TEST(ServiceWorkerDatabaseTest, UpdateVersionToActive) {
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
646 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, 635 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK,
647 database->ReadRegistration( 636 database->ReadRegistration(
648 data.registration_id, origin, &data_out, &resources_out)); 637 data.registration_id, origin, &data_out, &resources_out));
649 RegistrationData expected_data = data; 638 RegistrationData expected_data = data;
650 expected_data.is_active = true; 639 expected_data.is_active = true;
651 VerifyRegistrationData(expected_data, data_out); 640 VerifyRegistrationData(expected_data, data_out);
652 EXPECT_TRUE(resources_out.empty()); 641 EXPECT_TRUE(resources_out.empty());
653 642
654 // Delete the registration. 643 // Delete the registration.
655 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, 644 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK,
656 database->DeleteRegistration(data.registration_id, origin, 645 database->DeleteRegistration(data.registration_id, origin));
657 &newly_purgeable_resources));
658 646
659 // Should be false because the registration is gone. 647 // Should be false because the registration is gone.
660 EXPECT_EQ(ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND, 648 EXPECT_EQ(ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND,
661 database->UpdateVersionToActive(data.registration_id, origin)); 649 database->UpdateVersionToActive(data.registration_id, origin));
662 } 650 }
663 651
664 TEST(ServiceWorkerDatabaseTest, UpdateLastCheckTime) { 652 TEST(ServiceWorkerDatabaseTest, UpdateLastCheckTime) {
665 scoped_ptr<ServiceWorkerDatabase> database(CreateDatabaseInMemory()); 653 scoped_ptr<ServiceWorkerDatabase> database(CreateDatabaseInMemory());
666 GURL origin("http://example.com"); 654 GURL origin("http://example.com");
667 std::vector<int64> newly_purgeable_resources; 655 std::vector<int64> newly_purgeable_resources;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
701 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, 689 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK,
702 database->ReadRegistration( 690 database->ReadRegistration(
703 data.registration_id, origin, &data_out, &resources_out)); 691 data.registration_id, origin, &data_out, &resources_out));
704 RegistrationData expected_data = data; 692 RegistrationData expected_data = data;
705 expected_data.last_update_check = updated_time; 693 expected_data.last_update_check = updated_time;
706 VerifyRegistrationData(expected_data, data_out); 694 VerifyRegistrationData(expected_data, data_out);
707 EXPECT_TRUE(resources_out.empty()); 695 EXPECT_TRUE(resources_out.empty());
708 696
709 // Delete the registration. 697 // Delete the registration.
710 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, 698 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK,
711 database->DeleteRegistration(data.registration_id, origin, 699 database->DeleteRegistration(data.registration_id, origin));
712 &newly_purgeable_resources));
713 700
714 // Should be false because the registration is gone. 701 // Should be false because the registration is gone.
715 EXPECT_EQ(ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND, 702 EXPECT_EQ(ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND,
716 database->UpdateLastCheckTime( 703 database->UpdateLastCheckTime(
717 data.registration_id, origin, base::Time::Now())); 704 data.registration_id, origin, base::Time::Now()));
718 } 705 }
719 706
720 TEST(ServiceWorkerDatabaseTest, UncommittedResourceIds) { 707 TEST(ServiceWorkerDatabaseTest, UncommittedResourceIds) {
721 scoped_ptr<ServiceWorkerDatabase> database(CreateDatabaseInMemory()); 708 scoped_ptr<ServiceWorkerDatabase> database(CreateDatabaseInMemory());
722 709
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
895 CreateDatabase(database_dir.path())); 882 CreateDatabase(database_dir.path()));
896 883
897 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, database->LazyOpen(true)); 884 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, database->LazyOpen(true));
898 ASSERT_TRUE(base::DirectoryExists(database_dir.path())); 885 ASSERT_TRUE(base::DirectoryExists(database_dir.path()));
899 886
900 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, database->DestroyDatabase()); 887 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, database->DestroyDatabase());
901 ASSERT_FALSE(base::DirectoryExists(database_dir.path())); 888 ASSERT_FALSE(base::DirectoryExists(database_dir.path()));
902 } 889 }
903 890
904 } // namespace content 891 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698