| 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/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
| (...skipping 1378 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1389 &deleted_version, | 1389 &deleted_version, |
| 1390 &newly_purgeable_resources)); | 1390 &newly_purgeable_resources)); |
| 1391 EXPECT_EQ(data.registration_id, deleted_version.registration_id); | 1391 EXPECT_EQ(data.registration_id, deleted_version.registration_id); |
| 1392 | 1392 |
| 1393 // Should be false because the registration is gone. | 1393 // Should be false because the registration is gone. |
| 1394 EXPECT_EQ(ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND, | 1394 EXPECT_EQ(ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND, |
| 1395 database->UpdateLastCheckTime( | 1395 database->UpdateLastCheckTime( |
| 1396 data.registration_id, origin, base::Time::Now())); | 1396 data.registration_id, origin, base::Time::Now())); |
| 1397 } | 1397 } |
| 1398 | 1398 |
| 1399 TEST(ServiceWorkerDatabaseTest, UncommittedResourceIds) { | 1399 TEST(ServiceWorkerDatabaseTest, UncommittedAndPurgeableResourceIds) { |
| 1400 scoped_ptr<ServiceWorkerDatabase> database(CreateDatabaseInMemory()); | 1400 scoped_ptr<ServiceWorkerDatabase> database(CreateDatabaseInMemory()); |
| 1401 | 1401 |
| 1402 // Write {1, 2, 3}. | 1402 // Write {1, 2, 3} into the uncommitted list. |
| 1403 std::set<int64> ids1; | 1403 std::set<int64> ids1; |
| 1404 ids1.insert(1); | 1404 ids1.insert(1); |
| 1405 ids1.insert(2); | 1405 ids1.insert(2); |
| 1406 ids1.insert(3); | 1406 ids1.insert(3); |
| 1407 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, | 1407 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, |
| 1408 database->WriteUncommittedResourceIds(ids1)); | 1408 database->WriteUncommittedResourceIds(ids1)); |
| 1409 | 1409 |
| 1410 std::set<int64> ids_out; | 1410 std::set<int64> ids_out; |
| 1411 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, | 1411 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, |
| 1412 database->GetUncommittedResourceIds(&ids_out)); | 1412 database->GetUncommittedResourceIds(&ids_out)); |
| 1413 EXPECT_EQ(ids1, ids_out); | 1413 EXPECT_EQ(ids1, ids_out); |
| 1414 | 1414 |
| 1415 // Write {2, 4}. | 1415 // Write {2, 4} into the uncommitted list. |
| 1416 std::set<int64> ids2; | 1416 std::set<int64> ids2; |
| 1417 ids2.insert(2); | 1417 ids2.insert(2); |
| 1418 ids2.insert(4); | 1418 ids2.insert(4); |
| 1419 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, | 1419 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, |
| 1420 database->WriteUncommittedResourceIds(ids2)); | 1420 database->WriteUncommittedResourceIds(ids2)); |
| 1421 | 1421 |
| 1422 ids_out.clear(); | 1422 ids_out.clear(); |
| 1423 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, | 1423 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, |
| 1424 database->GetUncommittedResourceIds(&ids_out)); | 1424 database->GetUncommittedResourceIds(&ids_out)); |
| 1425 std::set<int64> expected = base::STLSetUnion<std::set<int64> >(ids1, ids2); | 1425 std::set<int64> expected = base::STLSetUnion<std::set<int64>>(ids1, ids2); |
| 1426 EXPECT_EQ(expected, ids_out); | 1426 EXPECT_EQ(expected, ids_out); |
| 1427 | 1427 |
| 1428 // Delete {2, 3}. | 1428 // Move {2, 4} from the uncommitted list to the purgeable list. |
| 1429 std::set<int64> ids3; | |
| 1430 ids3.insert(2); | |
| 1431 ids3.insert(3); | |
| 1432 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, | 1429 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, |
| 1433 database->ClearUncommittedResourceIds(ids3)); | 1430 database->PurgeUncommittedResourceIds(ids2)); |
| 1431 ids_out.clear(); |
| 1432 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, |
| 1433 database->GetPurgeableResourceIds(&ids_out)); |
| 1434 EXPECT_EQ(ids2, ids_out); |
| 1434 | 1435 |
| 1436 // Delete {2, 4} from the purgeable list. |
| 1437 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, |
| 1438 database->ClearPurgeableResourceIds(ids2)); |
| 1439 ids_out.clear(); |
| 1440 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, |
| 1441 database->GetPurgeableResourceIds(&ids_out)); |
| 1442 EXPECT_TRUE(ids_out.empty()); |
| 1443 |
| 1444 // {1, 3} should be still in the uncommitted list. |
| 1435 ids_out.clear(); | 1445 ids_out.clear(); |
| 1436 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, | 1446 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, |
| 1437 database->GetUncommittedResourceIds(&ids_out)); | 1447 database->GetUncommittedResourceIds(&ids_out)); |
| 1438 expected = base::STLSetDifference<std::set<int64> >(expected, ids3); | 1448 expected = base::STLSetDifference<std::set<int64>>(ids1, ids2); |
| 1439 EXPECT_EQ(expected, ids_out); | 1449 EXPECT_EQ(expected, ids_out); |
| 1440 } | 1450 } |
| 1441 | 1451 |
| 1442 TEST(ServiceWorkerDatabaseTest, PurgeableResourceIds) { | |
| 1443 scoped_ptr<ServiceWorkerDatabase> database(CreateDatabaseInMemory()); | |
| 1444 | |
| 1445 // Write {1, 2, 3}. | |
| 1446 std::set<int64> ids1; | |
| 1447 ids1.insert(1); | |
| 1448 ids1.insert(2); | |
| 1449 ids1.insert(3); | |
| 1450 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, | |
| 1451 database->WritePurgeableResourceIds(ids1)); | |
| 1452 | |
| 1453 std::set<int64> ids_out; | |
| 1454 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, | |
| 1455 database->GetPurgeableResourceIds(&ids_out)); | |
| 1456 EXPECT_EQ(ids1, ids_out); | |
| 1457 | |
| 1458 // Write {2, 4}. | |
| 1459 std::set<int64> ids2; | |
| 1460 ids2.insert(2); | |
| 1461 ids2.insert(4); | |
| 1462 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, | |
| 1463 database->WritePurgeableResourceIds(ids2)); | |
| 1464 | |
| 1465 ids_out.clear(); | |
| 1466 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, | |
| 1467 database->GetPurgeableResourceIds(&ids_out)); | |
| 1468 std::set<int64> expected = base::STLSetUnion<std::set<int64> >(ids1, ids2); | |
| 1469 EXPECT_EQ(expected, ids_out); | |
| 1470 | |
| 1471 // Delete {2, 3}. | |
| 1472 std::set<int64> ids3; | |
| 1473 ids3.insert(2); | |
| 1474 ids3.insert(3); | |
| 1475 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, | |
| 1476 database->ClearPurgeableResourceIds(ids3)); | |
| 1477 | |
| 1478 ids_out.clear(); | |
| 1479 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, | |
| 1480 database->GetPurgeableResourceIds(&ids_out)); | |
| 1481 expected = base::STLSetDifference<std::set<int64> >(expected, ids3); | |
| 1482 EXPECT_EQ(expected, ids_out); | |
| 1483 } | |
| 1484 | |
| 1485 TEST(ServiceWorkerDatabaseTest, DeleteAllDataForOrigin) { | 1452 TEST(ServiceWorkerDatabaseTest, DeleteAllDataForOrigin) { |
| 1486 scoped_ptr<ServiceWorkerDatabase> database(CreateDatabaseInMemory()); | 1453 scoped_ptr<ServiceWorkerDatabase> database(CreateDatabaseInMemory()); |
| 1487 ServiceWorkerDatabase::RegistrationData deleted_version; | 1454 ServiceWorkerDatabase::RegistrationData deleted_version; |
| 1488 std::vector<int64> newly_purgeable_resources; | 1455 std::vector<int64> newly_purgeable_resources; |
| 1489 | 1456 |
| 1490 // Data associated with |origin1| will be removed. | 1457 // Data associated with |origin1| will be removed. |
| 1491 GURL origin1("http://example.com"); | 1458 GURL origin1("http://example.com"); |
| 1492 GURL origin2("http://example.org"); | 1459 GURL origin2("http://example.org"); |
| 1493 | 1460 |
| 1494 // |origin1| has two registrations (registration1 and registration2). | 1461 // |origin1| has two registrations (registration1 and registration2). |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1641 CreateDatabase(database_dir.path())); | 1608 CreateDatabase(database_dir.path())); |
| 1642 | 1609 |
| 1643 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, database->LazyOpen(true)); | 1610 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, database->LazyOpen(true)); |
| 1644 ASSERT_TRUE(base::DirectoryExists(database_dir.path())); | 1611 ASSERT_TRUE(base::DirectoryExists(database_dir.path())); |
| 1645 | 1612 |
| 1646 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, database->DestroyDatabase()); | 1613 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, database->DestroyDatabase()); |
| 1647 ASSERT_FALSE(base::DirectoryExists(database_dir.path())); | 1614 ASSERT_FALSE(base::DirectoryExists(database_dir.path())); |
| 1648 } | 1615 } |
| 1649 | 1616 |
| 1650 } // namespace content | 1617 } // namespace content |
| OLD | NEW |