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

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

Issue 1411953002: Store foreign fetch scopes in database with other SW information. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@foreign-fetch-interface
Patch Set: address nhiroki's comments Created 5 years, 2 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
OLDNEW
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/file_util.h" 7 #include "base/files/file_util.h"
8 #include "base/files/scoped_temp_dir.h" 8 #include "base/files/scoped_temp_dir.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
(...skipping 480 matching lines...) Expand 10 before | Expand all | Expand 10 after
491 TEST_F(ServiceWorkerStorageTest, StoreFindUpdateDeleteRegistration) { 491 TEST_F(ServiceWorkerStorageTest, StoreFindUpdateDeleteRegistration) {
492 const GURL kScope("http://www.test.not/scope/"); 492 const GURL kScope("http://www.test.not/scope/");
493 const GURL kScript("http://www.test.not/script.js"); 493 const GURL kScript("http://www.test.not/script.js");
494 const GURL kDocumentUrl("http://www.test.not/scope/document.html"); 494 const GURL kDocumentUrl("http://www.test.not/scope/document.html");
495 const GURL kResource1("http://www.test.not/scope/resource1.js"); 495 const GURL kResource1("http://www.test.not/scope/resource1.js");
496 const int64 kResource1Size = 1591234; 496 const int64 kResource1Size = 1591234;
497 const GURL kResource2("http://www.test.not/scope/resource2.js"); 497 const GURL kResource2("http://www.test.not/scope/resource2.js");
498 const int64 kResource2Size = 51; 498 const int64 kResource2Size = 51;
499 const int64 kRegistrationId = 0; 499 const int64 kRegistrationId = 0;
500 const int64 kVersionId = 0; 500 const int64 kVersionId = 0;
501 const GURL kForeignFetchScope("http://www.test.not/scope/ff/");
501 const base::Time kToday = base::Time::Now(); 502 const base::Time kToday = base::Time::Now();
502 const base::Time kYesterday = kToday - base::TimeDelta::FromDays(1); 503 const base::Time kYesterday = kToday - base::TimeDelta::FromDays(1);
503 504
504 scoped_refptr<ServiceWorkerRegistration> found_registration; 505 scoped_refptr<ServiceWorkerRegistration> found_registration;
505 506
506 // We shouldn't find anything without having stored anything. 507 // We shouldn't find anything without having stored anything.
507 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 508 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
508 FindRegistrationForDocument(kDocumentUrl, &found_registration)); 509 FindRegistrationForDocument(kDocumentUrl, &found_registration));
509 EXPECT_FALSE(found_registration.get()); 510 EXPECT_FALSE(found_registration.get());
510 511
(...skipping 14 matching lines...) Expand all
525 526
526 // Store something. 527 // Store something.
527 scoped_refptr<ServiceWorkerRegistration> live_registration = 528 scoped_refptr<ServiceWorkerRegistration> live_registration =
528 new ServiceWorkerRegistration( 529 new ServiceWorkerRegistration(
529 kScope, kRegistrationId, context_ptr_); 530 kScope, kRegistrationId, context_ptr_);
530 scoped_refptr<ServiceWorkerVersion> live_version = 531 scoped_refptr<ServiceWorkerVersion> live_version =
531 new ServiceWorkerVersion( 532 new ServiceWorkerVersion(
532 live_registration.get(), kScript, kVersionId, context_ptr_); 533 live_registration.get(), kScript, kVersionId, context_ptr_);
533 live_version->SetStatus(ServiceWorkerVersion::INSTALLED); 534 live_version->SetStatus(ServiceWorkerVersion::INSTALLED);
534 live_version->script_cache_map()->SetResources(resources); 535 live_version->script_cache_map()->SetResources(resources);
536 live_version->set_foreign_fetch_scopes(
537 std::vector<GURL>(1, kForeignFetchScope));
535 live_registration->SetWaitingVersion(live_version); 538 live_registration->SetWaitingVersion(live_version);
536 live_registration->set_last_update_check(kYesterday); 539 live_registration->set_last_update_check(kYesterday);
537 EXPECT_EQ(SERVICE_WORKER_OK, 540 EXPECT_EQ(SERVICE_WORKER_OK,
538 StoreRegistration(live_registration, live_version)); 541 StoreRegistration(live_registration, live_version));
539 542
540 // Now we should find it and get the live ptr back immediately. 543 // Now we should find it and get the live ptr back immediately.
541 EXPECT_EQ(SERVICE_WORKER_OK, 544 EXPECT_EQ(SERVICE_WORKER_OK,
542 FindRegistrationForDocument(kDocumentUrl, &found_registration)); 545 FindRegistrationForDocument(kDocumentUrl, &found_registration));
543 EXPECT_EQ(live_registration, found_registration); 546 EXPECT_EQ(live_registration, found_registration);
544 EXPECT_EQ(kResource1Size + kResource2Size, 547 EXPECT_EQ(kResource1Size + kResource2Size,
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
611 EXPECT_EQ(SERVICE_WORKER_OK, 614 EXPECT_EQ(SERVICE_WORKER_OK,
612 FindRegistrationForPattern(kScope, &found_registration)); 615 FindRegistrationForPattern(kScope, &found_registration));
613 ASSERT_TRUE(found_registration.get()); 616 ASSERT_TRUE(found_registration.get());
614 EXPECT_EQ(kRegistrationId, found_registration->id()); 617 EXPECT_EQ(kRegistrationId, found_registration->id());
615 EXPECT_TRUE(found_registration->HasOneRef()); 618 EXPECT_TRUE(found_registration->HasOneRef());
616 EXPECT_FALSE(found_registration->active_version()); 619 EXPECT_FALSE(found_registration->active_version());
617 ASSERT_TRUE(found_registration->waiting_version()); 620 ASSERT_TRUE(found_registration->waiting_version());
618 EXPECT_EQ(kYesterday, found_registration->last_update_check()); 621 EXPECT_EQ(kYesterday, found_registration->last_update_check());
619 EXPECT_EQ(ServiceWorkerVersion::INSTALLED, 622 EXPECT_EQ(ServiceWorkerVersion::INSTALLED,
620 found_registration->waiting_version()->status()); 623 found_registration->waiting_version()->status());
624 EXPECT_EQ(
625 1u, found_registration->waiting_version()->foreign_fetch_scopes().size());
626 EXPECT_EQ(kForeignFetchScope,
627 found_registration->waiting_version()->foreign_fetch_scopes()[0]);
621 628
622 // Update to active and update the last check time. 629 // Update to active and update the last check time.
623 scoped_refptr<ServiceWorkerVersion> temp_version = 630 scoped_refptr<ServiceWorkerVersion> temp_version =
624 found_registration->waiting_version(); 631 found_registration->waiting_version();
625 temp_version->SetStatus(ServiceWorkerVersion::ACTIVATED); 632 temp_version->SetStatus(ServiceWorkerVersion::ACTIVATED);
626 found_registration->SetActiveVersion(temp_version); 633 found_registration->SetActiveVersion(temp_version);
627 temp_version = NULL; 634 temp_version = NULL;
628 EXPECT_EQ(SERVICE_WORKER_OK, UpdateToActiveState(found_registration)); 635 EXPECT_EQ(SERVICE_WORKER_OK, UpdateToActiveState(found_registration));
629 found_registration->set_last_update_check(kToday); 636 found_registration->set_last_update_check(kToday);
630 UpdateLastUpdateCheckTime(found_registration.get()); 637 UpdateLastUpdateCheckTime(found_registration.get());
(...skipping 835 matching lines...) Expand 10 before | Expand all | Expand 10 after
1466 live_registration2.get(), NULL, SERVICE_WORKER_OK); 1473 live_registration2.get(), NULL, SERVICE_WORKER_OK);
1467 storage()->NotifyDoneInstallingRegistration( 1474 storage()->NotifyDoneInstallingRegistration(
1468 live_registration3.get(), NULL, SERVICE_WORKER_OK); 1475 live_registration3.get(), NULL, SERVICE_WORKER_OK);
1469 1476
1470 // Find a registration among installed ones. 1477 // Find a registration among installed ones.
1471 EXPECT_EQ(SERVICE_WORKER_OK, 1478 EXPECT_EQ(SERVICE_WORKER_OK,
1472 FindRegistrationForDocument(kDocumentUrl, &found_registration)); 1479 FindRegistrationForDocument(kDocumentUrl, &found_registration));
1473 EXPECT_EQ(live_registration2, found_registration); 1480 EXPECT_EQ(live_registration2, found_registration);
1474 } 1481 }
1475 1482
1483 class ServiceWorkerStorageDiskTest : public ServiceWorkerStorageTest {
1484 public:
1485 void SetUp() override {
1486 ASSERT_TRUE(user_data_directory_.CreateUniqueTempDir());
1487 ServiceWorkerStorageTest::SetUp();
1488 }
1489
1490 base::FilePath GetUserDataDirectory() override {
1491 return user_data_directory_.path();
1492 }
1493
1494 protected:
1495 base::ScopedTempDir user_data_directory_;
1496 };
1497
1498 TEST_F(ServiceWorkerStorageDiskTest, OriginHasForeignFetchRegistrations) {
1499 LazyInitialize();
1500
1501 // Registration 1 for http://www.example.com
1502 const GURL kScope1("http://www.example.com/scope/");
1503 const GURL kScript1("http://www.example.com/script1.js");
1504 const int64 kRegistrationId1 = 1;
1505 const int64 kVersionId1 = 1;
1506 scoped_refptr<ServiceWorkerRegistration> live_registration1 =
1507 new ServiceWorkerRegistration(kScope1, kRegistrationId1, context_ptr_);
1508 scoped_refptr<ServiceWorkerVersion> live_version1 = new ServiceWorkerVersion(
1509 live_registration1.get(), kScript1, kVersionId1, context_ptr_);
1510 std::vector<ServiceWorkerDatabase::ResourceRecord> records1;
1511 records1.push_back(ServiceWorkerDatabase::ResourceRecord(
1512 1, live_version1->script_url(), 100));
1513 live_version1->script_cache_map()->SetResources(records1);
1514 live_version1->SetStatus(ServiceWorkerVersion::INSTALLED);
1515 live_version1->set_foreign_fetch_scopes(std::vector<GURL>(1, kScope1));
1516 live_registration1->SetWaitingVersion(live_version1);
1517
1518 // Registration 2 for http://www.example.com
1519 const GURL kScope2("http://www.example.com/scope/foo");
1520 const GURL kScript2("http://www.example.com/script2.js");
1521 const int64 kRegistrationId2 = 2;
1522 const int64 kVersionId2 = 2;
1523 scoped_refptr<ServiceWorkerRegistration> live_registration2 =
1524 new ServiceWorkerRegistration(kScope2, kRegistrationId2, context_ptr_);
1525 scoped_refptr<ServiceWorkerVersion> live_version2 = new ServiceWorkerVersion(
1526 live_registration2.get(), kScript2, kVersionId2, context_ptr_);
1527 std::vector<ServiceWorkerDatabase::ResourceRecord> records2;
1528 records2.push_back(ServiceWorkerDatabase::ResourceRecord(
1529 2, live_version2->script_url(), 100));
1530 live_version2->script_cache_map()->SetResources(records2);
1531 live_version2->SetStatus(ServiceWorkerVersion::INSTALLED);
1532 live_version2->set_foreign_fetch_scopes(std::vector<GURL>(1, kScope2));
1533 live_registration2->SetWaitingVersion(live_version2);
1534
1535 // Registration for http://www.test.com
1536 const GURL kScope3("http://www.test.com/scope/foobar");
1537 const GURL kScript3("http://www.test.com/script3.js");
1538 const int64 kRegistrationId3 = 3;
1539 const int64 kVersionId3 = 3;
1540 scoped_refptr<ServiceWorkerRegistration> live_registration3 =
1541 new ServiceWorkerRegistration(kScope3, kRegistrationId3, context_ptr_);
1542 scoped_refptr<ServiceWorkerVersion> live_version3 = new ServiceWorkerVersion(
1543 live_registration3.get(), kScript3, kVersionId3, context_ptr_);
1544 std::vector<ServiceWorkerDatabase::ResourceRecord> records3;
1545 records3.push_back(ServiceWorkerDatabase::ResourceRecord(
1546 3, live_version3->script_url(), 100));
1547 live_version3->script_cache_map()->SetResources(records3);
1548 live_version3->SetStatus(ServiceWorkerVersion::INSTALLED);
1549 live_registration3->SetWaitingVersion(live_version3);
1550
1551 // Neither origin should have registrations before they are stored.
1552 const GURL kOrigin1 = kScope1.GetOrigin();
1553 const GURL kOrigin2 = kScope3.GetOrigin();
1554 EXPECT_FALSE(storage()->OriginHasForeignFetchRegistrations(kOrigin1));
1555 EXPECT_FALSE(storage()->OriginHasForeignFetchRegistrations(kOrigin2));
1556
1557 // Store all registrations.
1558 EXPECT_EQ(SERVICE_WORKER_OK,
1559 StoreRegistration(live_registration1, live_version1));
1560 EXPECT_EQ(SERVICE_WORKER_OK,
1561 StoreRegistration(live_registration2, live_version2));
1562 EXPECT_EQ(SERVICE_WORKER_OK,
1563 StoreRegistration(live_registration3, live_version3));
1564
1565 // Now first origin should have foreign fetch registrations, second doesn't.
1566 EXPECT_TRUE(storage()->OriginHasForeignFetchRegistrations(kOrigin1));
1567 EXPECT_FALSE(storage()->OriginHasForeignFetchRegistrations(kOrigin2));
1568
1569 // Remove one registration at first origin.
1570 EXPECT_EQ(SERVICE_WORKER_OK,
1571 DeleteRegistration(kRegistrationId1, kScope1.GetOrigin()));
1572
1573 // First origin should still have a registration left.
1574 EXPECT_TRUE(storage()->OriginHasForeignFetchRegistrations(kOrigin1));
1575 EXPECT_FALSE(storage()->OriginHasForeignFetchRegistrations(kOrigin2));
1576
1577 // Simulate browser shutdown and restart.
1578 live_registration1 = nullptr;
1579 live_version1 = nullptr;
1580 live_registration2 = nullptr;
1581 live_version2 = nullptr;
1582 live_registration3 = nullptr;
1583 live_version3 = nullptr;
1584 context_.reset();
1585 scoped_ptr<ServiceWorkerDatabaseTaskManager> database_task_manager(
1586 new MockServiceWorkerDatabaseTaskManager(
1587 base::ThreadTaskRunnerHandle::Get()));
1588 context_.reset(new ServiceWorkerContextCore(
1589 GetUserDataDirectory(), database_task_manager.Pass(),
1590 base::ThreadTaskRunnerHandle::Get(), nullptr, nullptr, nullptr, nullptr));
1591 LazyInitialize();
1592
1593 // First origin should still have a registration left.
1594 EXPECT_TRUE(storage()->OriginHasForeignFetchRegistrations(kOrigin1));
1595 EXPECT_FALSE(storage()->OriginHasForeignFetchRegistrations(kOrigin2));
1596
1597 // Remove other registration at first origin.
1598 EXPECT_EQ(SERVICE_WORKER_OK,
1599 DeleteRegistration(kRegistrationId2, kScope2.GetOrigin()));
1600
1601 // No foreign fetch registrations remain.
1602 EXPECT_FALSE(storage()->OriginHasForeignFetchRegistrations(kOrigin1));
1603 EXPECT_FALSE(storage()->OriginHasForeignFetchRegistrations(kOrigin2));
1604 }
1605
1476 } // namespace content 1606 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/service_worker/service_worker_storage.cc ('k') | content/browser/service_worker/service_worker_version.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698