| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/file_path.h" | 5 #include "base/file_path.h" |
| 6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
| 7 #include "base/files/scoped_temp_dir.h" |
| 7 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 9 #include "base/message_loop_proxy.h" | 10 #include "base/message_loop_proxy.h" |
| 10 #include "base/platform_file.h" | 11 #include "base/platform_file.h" |
| 11 #include "base/scoped_temp_dir.h" | |
| 12 #include "base/time.h" | 12 #include "base/time.h" |
| 13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 14 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
| 15 #include "net/base/test_completion_callback.h" | 15 #include "net/base/test_completion_callback.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "third_party/sqlite/sqlite3.h" | 17 #include "third_party/sqlite/sqlite3.h" |
| 18 #include "webkit/database/database_tracker.h" | 18 #include "webkit/database/database_tracker.h" |
| 19 #include "webkit/database/database_util.h" | 19 #include "webkit/database/database_util.h" |
| 20 #include "webkit/quota/mock_special_storage_policy.h" | 20 #include "webkit/quota/mock_special_storage_policy.h" |
| 21 #include "webkit/quota/quota_manager.h" | 21 #include "webkit/quota/quota_manager.h" |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 | 180 |
| 181 // We declare a helper class, and make it a friend of DatabaseTracker using | 181 // We declare a helper class, and make it a friend of DatabaseTracker using |
| 182 // the FRIEND_TEST() macro, and we implement all tests we want to run as | 182 // the FRIEND_TEST() macro, and we implement all tests we want to run as |
| 183 // static methods of this class. Then we make our TEST() targets call these | 183 // static methods of this class. Then we make our TEST() targets call these |
| 184 // static functions. This allows us to run each test in normal mode and | 184 // static functions. This allows us to run each test in normal mode and |
| 185 // incognito mode without writing the same code twice. | 185 // incognito mode without writing the same code twice. |
| 186 class DatabaseTracker_TestHelper_Test { | 186 class DatabaseTracker_TestHelper_Test { |
| 187 public: | 187 public: |
| 188 static void TestDeleteOpenDatabase(bool incognito_mode) { | 188 static void TestDeleteOpenDatabase(bool incognito_mode) { |
| 189 // Initialize the tracker database. | 189 // Initialize the tracker database. |
| 190 ScopedTempDir temp_dir; | 190 base::ScopedTempDir temp_dir; |
| 191 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 191 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 192 scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy = | 192 scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy = |
| 193 new quota::MockSpecialStoragePolicy; | 193 new quota::MockSpecialStoragePolicy; |
| 194 special_storage_policy->AddProtected(GURL(kOrigin2Url)); | 194 special_storage_policy->AddProtected(GURL(kOrigin2Url)); |
| 195 scoped_refptr<DatabaseTracker> tracker( | 195 scoped_refptr<DatabaseTracker> tracker( |
| 196 new DatabaseTracker(temp_dir.path(), incognito_mode, | 196 new DatabaseTracker(temp_dir.path(), incognito_mode, |
| 197 special_storage_policy, NULL, NULL)); | 197 special_storage_policy, NULL, NULL)); |
| 198 | 198 |
| 199 // Create and open three databases. | 199 // Create and open three databases. |
| 200 int64 database_size = 0; | 200 int64 database_size = 0; |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 285 file_util::PathExists(tracker->GetFullDBFilePath(kOrigin2, kDB2))); | 285 file_util::PathExists(tracker->GetFullDBFilePath(kOrigin2, kDB2))); |
| 286 EXPECT_TRUE( | 286 EXPECT_TRUE( |
| 287 file_util::PathExists(tracker->GetFullDBFilePath(kOrigin2, kDB3))); | 287 file_util::PathExists(tracker->GetFullDBFilePath(kOrigin2, kDB3))); |
| 288 | 288 |
| 289 tracker->DatabaseClosed(kOrigin2, kDB3); | 289 tracker->DatabaseClosed(kOrigin2, kDB3); |
| 290 tracker->RemoveObserver(&observer); | 290 tracker->RemoveObserver(&observer); |
| 291 } | 291 } |
| 292 | 292 |
| 293 static void TestDatabaseTracker(bool incognito_mode) { | 293 static void TestDatabaseTracker(bool incognito_mode) { |
| 294 // Initialize the tracker database. | 294 // Initialize the tracker database. |
| 295 ScopedTempDir temp_dir; | 295 base::ScopedTempDir temp_dir; |
| 296 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 296 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 297 scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy = | 297 scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy = |
| 298 new quota::MockSpecialStoragePolicy; | 298 new quota::MockSpecialStoragePolicy; |
| 299 special_storage_policy->AddProtected(GURL(kOrigin2Url)); | 299 special_storage_policy->AddProtected(GURL(kOrigin2Url)); |
| 300 scoped_refptr<DatabaseTracker> tracker( | 300 scoped_refptr<DatabaseTracker> tracker( |
| 301 new DatabaseTracker(temp_dir.path(), incognito_mode, | 301 new DatabaseTracker(temp_dir.path(), incognito_mode, |
| 302 special_storage_policy, NULL, NULL)); | 302 special_storage_policy, NULL, NULL)); |
| 303 | 303 |
| 304 // Add two observers. | 304 // Add two observers. |
| 305 TestObserver observer1; | 305 TestObserver observer1; |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 434 EXPECT_TRUE(origin1_info); | 434 EXPECT_TRUE(origin1_info); |
| 435 EXPECT_EQ(0, origin1_info->TotalSize()); | 435 EXPECT_EQ(0, origin1_info->TotalSize()); |
| 436 } | 436 } |
| 437 | 437 |
| 438 static void DatabaseTrackerQuotaIntegration() { | 438 static void DatabaseTrackerQuotaIntegration() { |
| 439 const GURL kOrigin(kOrigin1Url); | 439 const GURL kOrigin(kOrigin1Url); |
| 440 const string16 kOriginId = DatabaseUtil::GetOriginIdentifier(kOrigin); | 440 const string16 kOriginId = DatabaseUtil::GetOriginIdentifier(kOrigin); |
| 441 const string16 kName = ASCIIToUTF16("name"); | 441 const string16 kName = ASCIIToUTF16("name"); |
| 442 const string16 kDescription = ASCIIToUTF16("description"); | 442 const string16 kDescription = ASCIIToUTF16("description"); |
| 443 | 443 |
| 444 ScopedTempDir temp_dir; | 444 base::ScopedTempDir temp_dir; |
| 445 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 445 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 446 | 446 |
| 447 // Initialize the tracker with a QuotaManagerProxy | 447 // Initialize the tracker with a QuotaManagerProxy |
| 448 scoped_refptr<TestQuotaManagerProxy> test_quota_proxy( | 448 scoped_refptr<TestQuotaManagerProxy> test_quota_proxy( |
| 449 new TestQuotaManagerProxy); | 449 new TestQuotaManagerProxy); |
| 450 scoped_refptr<DatabaseTracker> tracker( | 450 scoped_refptr<DatabaseTracker> tracker( |
| 451 new DatabaseTracker(temp_dir.path(), false /* incognito */, | 451 new DatabaseTracker(temp_dir.path(), false /* incognito */, |
| 452 NULL, test_quota_proxy, NULL)); | 452 NULL, test_quota_proxy, NULL)); |
| 453 EXPECT_TRUE(test_quota_proxy->registered_client_); | 453 EXPECT_TRUE(test_quota_proxy->registered_client_); |
| 454 | 454 |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 534 const string16 kOrigin1 = | 534 const string16 kOrigin1 = |
| 535 DatabaseUtil::GetOriginIdentifier(GURL(kOrigin1Url)); | 535 DatabaseUtil::GetOriginIdentifier(GURL(kOrigin1Url)); |
| 536 const string16 kOrigin2 = | 536 const string16 kOrigin2 = |
| 537 DatabaseUtil::GetOriginIdentifier(GURL(kOrigin2Url)); | 537 DatabaseUtil::GetOriginIdentifier(GURL(kOrigin2Url)); |
| 538 const string16 kDB1 = ASCIIToUTF16("db1"); | 538 const string16 kDB1 = ASCIIToUTF16("db1"); |
| 539 const string16 kDB2 = ASCIIToUTF16("db2"); | 539 const string16 kDB2 = ASCIIToUTF16("db2"); |
| 540 const string16 kDescription = ASCIIToUTF16("database_description"); | 540 const string16 kDescription = ASCIIToUTF16("database_description"); |
| 541 | 541 |
| 542 // Initialize the tracker database. | 542 // Initialize the tracker database. |
| 543 MessageLoop message_loop; | 543 MessageLoop message_loop; |
| 544 ScopedTempDir temp_dir; | 544 base::ScopedTempDir temp_dir; |
| 545 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 545 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 546 FilePath origin1_db_dir; | 546 FilePath origin1_db_dir; |
| 547 FilePath origin2_db_dir; | 547 FilePath origin2_db_dir; |
| 548 { | 548 { |
| 549 scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy = | 549 scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy = |
| 550 new quota::MockSpecialStoragePolicy; | 550 new quota::MockSpecialStoragePolicy; |
| 551 special_storage_policy->AddSessionOnly(GURL(kOrigin2Url)); | 551 special_storage_policy->AddSessionOnly(GURL(kOrigin2Url)); |
| 552 scoped_refptr<DatabaseTracker> tracker( | 552 scoped_refptr<DatabaseTracker> tracker( |
| 553 new DatabaseTracker( | 553 new DatabaseTracker( |
| 554 temp_dir.path(), false, special_storage_policy, NULL, | 554 temp_dir.path(), false, special_storage_policy, NULL, |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 612 const string16 kOrigin1 = | 612 const string16 kOrigin1 = |
| 613 DatabaseUtil::GetOriginIdentifier(GURL(kOrigin1Url)); | 613 DatabaseUtil::GetOriginIdentifier(GURL(kOrigin1Url)); |
| 614 const string16 kOrigin2 = | 614 const string16 kOrigin2 = |
| 615 DatabaseUtil::GetOriginIdentifier(GURL(kOrigin2Url)); | 615 DatabaseUtil::GetOriginIdentifier(GURL(kOrigin2Url)); |
| 616 const string16 kDB1 = ASCIIToUTF16("db1"); | 616 const string16 kDB1 = ASCIIToUTF16("db1"); |
| 617 const string16 kDB2 = ASCIIToUTF16("db2"); | 617 const string16 kDB2 = ASCIIToUTF16("db2"); |
| 618 const string16 kDescription = ASCIIToUTF16("database_description"); | 618 const string16 kDescription = ASCIIToUTF16("database_description"); |
| 619 | 619 |
| 620 // Initialize the tracker database. | 620 // Initialize the tracker database. |
| 621 MessageLoop message_loop; | 621 MessageLoop message_loop; |
| 622 ScopedTempDir temp_dir; | 622 base::ScopedTempDir temp_dir; |
| 623 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 623 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 624 FilePath origin1_db_dir; | 624 FilePath origin1_db_dir; |
| 625 FilePath origin2_db_dir; | 625 FilePath origin2_db_dir; |
| 626 { | 626 { |
| 627 scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy = | 627 scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy = |
| 628 new quota::MockSpecialStoragePolicy; | 628 new quota::MockSpecialStoragePolicy; |
| 629 special_storage_policy->AddSessionOnly(GURL(kOrigin2Url)); | 629 special_storage_policy->AddSessionOnly(GURL(kOrigin2Url)); |
| 630 scoped_refptr<DatabaseTracker> tracker( | 630 scoped_refptr<DatabaseTracker> tracker( |
| 631 new DatabaseTracker( | 631 new DatabaseTracker( |
| 632 temp_dir.path(), false, special_storage_policy, NULL, | 632 temp_dir.path(), false, special_storage_policy, NULL, |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 685 | 685 |
| 686 static void EmptyDatabaseNameIsValid() { | 686 static void EmptyDatabaseNameIsValid() { |
| 687 const GURL kOrigin(kOrigin1Url); | 687 const GURL kOrigin(kOrigin1Url); |
| 688 const string16 kOriginId = DatabaseUtil::GetOriginIdentifier(kOrigin); | 688 const string16 kOriginId = DatabaseUtil::GetOriginIdentifier(kOrigin); |
| 689 const string16 kEmptyName; | 689 const string16 kEmptyName; |
| 690 const string16 kDescription(ASCIIToUTF16("description")); | 690 const string16 kDescription(ASCIIToUTF16("description")); |
| 691 const string16 kChangedDescription(ASCIIToUTF16("changed_description")); | 691 const string16 kChangedDescription(ASCIIToUTF16("changed_description")); |
| 692 | 692 |
| 693 // Initialize a tracker database, no need to put it on disk. | 693 // Initialize a tracker database, no need to put it on disk. |
| 694 const bool kUseInMemoryTrackerDatabase = true; | 694 const bool kUseInMemoryTrackerDatabase = true; |
| 695 ScopedTempDir temp_dir; | 695 base::ScopedTempDir temp_dir; |
| 696 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 696 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 697 scoped_refptr<DatabaseTracker> tracker( | 697 scoped_refptr<DatabaseTracker> tracker( |
| 698 new DatabaseTracker(temp_dir.path(), kUseInMemoryTrackerDatabase, | 698 new DatabaseTracker(temp_dir.path(), kUseInMemoryTrackerDatabase, |
| 699 NULL, NULL, NULL)); | 699 NULL, NULL, NULL)); |
| 700 | 700 |
| 701 // Starts off with no databases. | 701 // Starts off with no databases. |
| 702 std::vector<OriginInfo> infos; | 702 std::vector<OriginInfo> infos; |
| 703 EXPECT_TRUE(tracker->GetAllOriginsInfo(&infos)); | 703 EXPECT_TRUE(tracker->GetAllOriginsInfo(&infos)); |
| 704 EXPECT_TRUE(infos.empty()); | 704 EXPECT_TRUE(infos.empty()); |
| 705 | 705 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 731 } | 731 } |
| 732 | 732 |
| 733 static void HandleSqliteError() { | 733 static void HandleSqliteError() { |
| 734 const GURL kOrigin(kOrigin1Url); | 734 const GURL kOrigin(kOrigin1Url); |
| 735 const string16 kOriginId = DatabaseUtil::GetOriginIdentifier(kOrigin); | 735 const string16 kOriginId = DatabaseUtil::GetOriginIdentifier(kOrigin); |
| 736 const string16 kName(ASCIIToUTF16("name")); | 736 const string16 kName(ASCIIToUTF16("name")); |
| 737 const string16 kDescription(ASCIIToUTF16("description")); | 737 const string16 kDescription(ASCIIToUTF16("description")); |
| 738 | 738 |
| 739 // Initialize a tracker database, no need to put it on disk. | 739 // Initialize a tracker database, no need to put it on disk. |
| 740 const bool kUseInMemoryTrackerDatabase = true; | 740 const bool kUseInMemoryTrackerDatabase = true; |
| 741 ScopedTempDir temp_dir; | 741 base::ScopedTempDir temp_dir; |
| 742 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 742 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 743 scoped_refptr<DatabaseTracker> tracker( | 743 scoped_refptr<DatabaseTracker> tracker( |
| 744 new DatabaseTracker(temp_dir.path(), kUseInMemoryTrackerDatabase, | 744 new DatabaseTracker(temp_dir.path(), kUseInMemoryTrackerDatabase, |
| 745 NULL, NULL, NULL)); | 745 NULL, NULL, NULL)); |
| 746 | 746 |
| 747 // Setup to observe OnScheduledForDelete notifications. | 747 // Setup to observe OnScheduledForDelete notifications. |
| 748 TestObserver observer(false, true); | 748 TestObserver observer(false, true); |
| 749 tracker->AddObserver(&observer); | 749 tracker->AddObserver(&observer); |
| 750 | 750 |
| 751 // Verify does no harm when there is no such database. | 751 // Verify does no harm when there is no such database. |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 839 | 839 |
| 840 TEST(DatabaseTrackerTest, EmptyDatabaseNameIsValid) { | 840 TEST(DatabaseTrackerTest, EmptyDatabaseNameIsValid) { |
| 841 DatabaseTracker_TestHelper_Test::EmptyDatabaseNameIsValid(); | 841 DatabaseTracker_TestHelper_Test::EmptyDatabaseNameIsValid(); |
| 842 } | 842 } |
| 843 | 843 |
| 844 TEST(DatabaseTrackerTest, HandleSqliteError) { | 844 TEST(DatabaseTrackerTest, HandleSqliteError) { |
| 845 DatabaseTracker_TestHelper_Test::HandleSqliteError(); | 845 DatabaseTracker_TestHelper_Test::HandleSqliteError(); |
| 846 } | 846 } |
| 847 | 847 |
| 848 } // namespace webkit_database | 848 } // namespace webkit_database |
| OLD | NEW |