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

Side by Side Diff: content/browser/database_tracker_unittest.cc

Issue 2316043002: //content: Change ScopedTempDir::path() to GetPath() (Closed)
Patch Set: Just rebased Created 4 years, 3 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 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 <stddef.h> 5 #include <stddef.h>
6 #include <stdint.h> 6 #include <stdint.h>
7 7
8 #include <memory> 8 #include <memory>
9 9
10 #include "base/files/file.h" 10 #include "base/files/file.h"
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 class DatabaseTracker_TestHelper_Test { 195 class DatabaseTracker_TestHelper_Test {
196 public: 196 public:
197 static void TestDeleteOpenDatabase(bool incognito_mode) { 197 static void TestDeleteOpenDatabase(bool incognito_mode) {
198 // Initialize the tracker database. 198 // Initialize the tracker database.
199 base::ScopedTempDir temp_dir; 199 base::ScopedTempDir temp_dir;
200 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 200 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
201 scoped_refptr<MockSpecialStoragePolicy> special_storage_policy = 201 scoped_refptr<MockSpecialStoragePolicy> special_storage_policy =
202 new MockSpecialStoragePolicy; 202 new MockSpecialStoragePolicy;
203 special_storage_policy->AddProtected(GURL(kOrigin2Url)); 203 special_storage_policy->AddProtected(GURL(kOrigin2Url));
204 scoped_refptr<DatabaseTracker> tracker( 204 scoped_refptr<DatabaseTracker> tracker(
205 new DatabaseTracker(temp_dir.path(), 205 new DatabaseTracker(temp_dir.GetPath(), incognito_mode,
206 incognito_mode, 206 special_storage_policy.get(), NULL, NULL));
207 special_storage_policy.get(),
208 NULL,
209 NULL));
210 207
211 // Create and open three databases. 208 // Create and open three databases.
212 int64_t database_size = 0; 209 int64_t database_size = 0;
213 const std::string kOrigin1 = 210 const std::string kOrigin1 =
214 storage::GetIdentifierFromOrigin(GURL(kOrigin1Url)); 211 storage::GetIdentifierFromOrigin(GURL(kOrigin1Url));
215 const std::string kOrigin2 = 212 const std::string kOrigin2 =
216 storage::GetIdentifierFromOrigin(GURL(kOrigin2Url)); 213 storage::GetIdentifierFromOrigin(GURL(kOrigin2Url));
217 const base::string16 kDB1 = ASCIIToUTF16("db1"); 214 const base::string16 kDB1 = ASCIIToUTF16("db1");
218 const base::string16 kDB2 = ASCIIToUTF16("db2"); 215 const base::string16 kDB2 = ASCIIToUTF16("db2");
219 const base::string16 kDB3 = ASCIIToUTF16("db3"); 216 const base::string16 kDB3 = ASCIIToUTF16("db3");
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 } 300 }
304 301
305 static void TestDatabaseTracker(bool incognito_mode) { 302 static void TestDatabaseTracker(bool incognito_mode) {
306 // Initialize the tracker database. 303 // Initialize the tracker database.
307 base::ScopedTempDir temp_dir; 304 base::ScopedTempDir temp_dir;
308 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 305 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
309 scoped_refptr<MockSpecialStoragePolicy> special_storage_policy = 306 scoped_refptr<MockSpecialStoragePolicy> special_storage_policy =
310 new MockSpecialStoragePolicy; 307 new MockSpecialStoragePolicy;
311 special_storage_policy->AddProtected(GURL(kOrigin2Url)); 308 special_storage_policy->AddProtected(GURL(kOrigin2Url));
312 scoped_refptr<DatabaseTracker> tracker( 309 scoped_refptr<DatabaseTracker> tracker(
313 new DatabaseTracker(temp_dir.path(), 310 new DatabaseTracker(temp_dir.GetPath(), incognito_mode,
314 incognito_mode, 311 special_storage_policy.get(), NULL, NULL));
315 special_storage_policy.get(),
316 NULL,
317 NULL));
318 312
319 // Add two observers. 313 // Add two observers.
320 TestObserver observer1; 314 TestObserver observer1;
321 TestObserver observer2; 315 TestObserver observer2;
322 tracker->AddObserver(&observer1); 316 tracker->AddObserver(&observer1);
323 tracker->AddObserver(&observer2); 317 tracker->AddObserver(&observer2);
324 318
325 // Open three new databases. 319 // Open three new databases.
326 int64_t database_size = 0; 320 int64_t database_size = 0;
327 const std::string kOrigin1 = 321 const std::string kOrigin1 =
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
456 const base::string16 kName = ASCIIToUTF16("name"); 450 const base::string16 kName = ASCIIToUTF16("name");
457 const base::string16 kDescription = ASCIIToUTF16("description"); 451 const base::string16 kDescription = ASCIIToUTF16("description");
458 452
459 base::ScopedTempDir temp_dir; 453 base::ScopedTempDir temp_dir;
460 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 454 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
461 455
462 // Initialize the tracker with a QuotaManagerProxy 456 // Initialize the tracker with a QuotaManagerProxy
463 scoped_refptr<TestQuotaManagerProxy> test_quota_proxy( 457 scoped_refptr<TestQuotaManagerProxy> test_quota_proxy(
464 new TestQuotaManagerProxy); 458 new TestQuotaManagerProxy);
465 scoped_refptr<DatabaseTracker> tracker( 459 scoped_refptr<DatabaseTracker> tracker(
466 new DatabaseTracker(temp_dir.path(), 460 new DatabaseTracker(temp_dir.GetPath(), false /* incognito */, NULL,
467 false /* incognito */, 461 test_quota_proxy.get(), NULL));
468 NULL,
469 test_quota_proxy.get(),
470 NULL));
471 EXPECT_TRUE(test_quota_proxy->registered_client_); 462 EXPECT_TRUE(test_quota_proxy->registered_client_);
472 463
473 // Create a database and modify it a couple of times, close it, 464 // Create a database and modify it a couple of times, close it,
474 // then delete it. Observe the tracker notifies accordingly. 465 // then delete it. Observe the tracker notifies accordingly.
475 466
476 int64_t database_size = 0; 467 int64_t database_size = 0;
477 tracker->DatabaseOpened(kOriginId, kName, kDescription, 0, 468 tracker->DatabaseOpened(kOriginId, kName, kDescription, 0,
478 &database_size); 469 &database_size);
479 EXPECT_TRUE(test_quota_proxy->WasAccessNotified(kOrigin)); 470 EXPECT_TRUE(test_quota_proxy->WasAccessNotified(kOrigin));
480 test_quota_proxy->reset(); 471 test_quota_proxy->reset();
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
561 base::MessageLoop message_loop; 552 base::MessageLoop message_loop;
562 base::ScopedTempDir temp_dir; 553 base::ScopedTempDir temp_dir;
563 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 554 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
564 base::FilePath origin1_db_dir; 555 base::FilePath origin1_db_dir;
565 base::FilePath origin2_db_dir; 556 base::FilePath origin2_db_dir;
566 { 557 {
567 scoped_refptr<MockSpecialStoragePolicy> special_storage_policy = 558 scoped_refptr<MockSpecialStoragePolicy> special_storage_policy =
568 new MockSpecialStoragePolicy; 559 new MockSpecialStoragePolicy;
569 special_storage_policy->AddSessionOnly(GURL(kOrigin2Url)); 560 special_storage_policy->AddSessionOnly(GURL(kOrigin2Url));
570 scoped_refptr<DatabaseTracker> tracker(new DatabaseTracker( 561 scoped_refptr<DatabaseTracker> tracker(new DatabaseTracker(
571 temp_dir.path(), false, special_storage_policy.get(), NULL, 562 temp_dir.GetPath(), false, special_storage_policy.get(), NULL,
572 base::ThreadTaskRunnerHandle::Get().get())); 563 base::ThreadTaskRunnerHandle::Get().get()));
573 564
574 // Open two new databases. 565 // Open two new databases.
575 tracker->DatabaseOpened(kOrigin1, kDB1, kDescription, 0, 566 tracker->DatabaseOpened(kOrigin1, kDB1, kDescription, 0,
576 &database_size); 567 &database_size);
577 EXPECT_EQ(0, database_size); 568 EXPECT_EQ(0, database_size);
578 tracker->DatabaseOpened(kOrigin2, kDB2, kDescription, 0, 569 tracker->DatabaseOpened(kOrigin2, kDB2, kDescription, 0,
579 &database_size); 570 &database_size);
580 EXPECT_EQ(0, database_size); 571 EXPECT_EQ(0, database_size);
581 572
(...skipping 16 matching lines...) Expand all
598 589
599 // Close all databases. 590 // Close all databases.
600 tracker->DatabaseClosed(kOrigin1, kDB1); 591 tracker->DatabaseClosed(kOrigin1, kDB1);
601 tracker->DatabaseClosed(kOrigin2, kDB2); 592 tracker->DatabaseClosed(kOrigin2, kDB2);
602 593
603 tracker->Shutdown(); 594 tracker->Shutdown();
604 } 595 }
605 596
606 // At this point, the database tracker should be gone. Create a new one. 597 // At this point, the database tracker should be gone. Create a new one.
607 scoped_refptr<DatabaseTracker> tracker( 598 scoped_refptr<DatabaseTracker> tracker(
608 new DatabaseTracker(temp_dir.path(), false, NULL, NULL, NULL)); 599 new DatabaseTracker(temp_dir.GetPath(), false, NULL, NULL, NULL));
609 600
610 // Get all data for all origins. 601 // Get all data for all origins.
611 std::vector<OriginInfo> origins_info; 602 std::vector<OriginInfo> origins_info;
612 EXPECT_TRUE(tracker->GetAllOriginsInfo(&origins_info)); 603 EXPECT_TRUE(tracker->GetAllOriginsInfo(&origins_info));
613 // kOrigin1 was not session-only, so it survived. kOrigin2 was session-only 604 // kOrigin1 was not session-only, so it survived. kOrigin2 was session-only
614 // and it got deleted. 605 // and it got deleted.
615 EXPECT_EQ(size_t(1), origins_info.size()); 606 EXPECT_EQ(size_t(1), origins_info.size());
616 EXPECT_EQ(kOrigin1, origins_info[0].GetOriginIdentifier()); 607 EXPECT_EQ(kOrigin1, origins_info[0].GetOriginIdentifier());
617 EXPECT_TRUE( 608 EXPECT_TRUE(
618 base::PathExists(tracker->GetFullDBFilePath(kOrigin1, kDB1))); 609 base::PathExists(tracker->GetFullDBFilePath(kOrigin1, kDB1)));
(...skipping 19 matching lines...) Expand all
638 base::MessageLoop message_loop; 629 base::MessageLoop message_loop;
639 base::ScopedTempDir temp_dir; 630 base::ScopedTempDir temp_dir;
640 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 631 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
641 base::FilePath origin1_db_dir; 632 base::FilePath origin1_db_dir;
642 base::FilePath origin2_db_dir; 633 base::FilePath origin2_db_dir;
643 { 634 {
644 scoped_refptr<MockSpecialStoragePolicy> special_storage_policy = 635 scoped_refptr<MockSpecialStoragePolicy> special_storage_policy =
645 new MockSpecialStoragePolicy; 636 new MockSpecialStoragePolicy;
646 special_storage_policy->AddSessionOnly(GURL(kOrigin2Url)); 637 special_storage_policy->AddSessionOnly(GURL(kOrigin2Url));
647 scoped_refptr<DatabaseTracker> tracker(new DatabaseTracker( 638 scoped_refptr<DatabaseTracker> tracker(new DatabaseTracker(
648 temp_dir.path(), false, special_storage_policy.get(), NULL, 639 temp_dir.GetPath(), false, special_storage_policy.get(), NULL,
649 base::ThreadTaskRunnerHandle::Get().get())); 640 base::ThreadTaskRunnerHandle::Get().get()));
650 tracker->SetForceKeepSessionState(); 641 tracker->SetForceKeepSessionState();
651 642
652 // Open two new databases. 643 // Open two new databases.
653 tracker->DatabaseOpened(kOrigin1, kDB1, kDescription, 0, 644 tracker->DatabaseOpened(kOrigin1, kDB1, kDescription, 0,
654 &database_size); 645 &database_size);
655 EXPECT_EQ(0, database_size); 646 EXPECT_EQ(0, database_size);
656 tracker->DatabaseOpened(kOrigin2, kDB2, kDescription, 0, 647 tracker->DatabaseOpened(kOrigin2, kDB2, kDescription, 0,
657 &database_size); 648 &database_size);
658 EXPECT_EQ(0, database_size); 649 EXPECT_EQ(0, database_size);
(...skipping 17 matching lines...) Expand all
676 667
677 // Close all databases. 668 // Close all databases.
678 tracker->DatabaseClosed(kOrigin1, kDB1); 669 tracker->DatabaseClosed(kOrigin1, kDB1);
679 tracker->DatabaseClosed(kOrigin2, kDB2); 670 tracker->DatabaseClosed(kOrigin2, kDB2);
680 671
681 tracker->Shutdown(); 672 tracker->Shutdown();
682 } 673 }
683 674
684 // At this point, the database tracker should be gone. Create a new one. 675 // At this point, the database tracker should be gone. Create a new one.
685 scoped_refptr<DatabaseTracker> tracker( 676 scoped_refptr<DatabaseTracker> tracker(
686 new DatabaseTracker(temp_dir.path(), false, NULL, NULL, NULL)); 677 new DatabaseTracker(temp_dir.GetPath(), false, NULL, NULL, NULL));
687 678
688 // Get all data for all origins. 679 // Get all data for all origins.
689 std::vector<OriginInfo> origins_info; 680 std::vector<OriginInfo> origins_info;
690 EXPECT_TRUE(tracker->GetAllOriginsInfo(&origins_info)); 681 EXPECT_TRUE(tracker->GetAllOriginsInfo(&origins_info));
691 // No origins were deleted. 682 // No origins were deleted.
692 EXPECT_EQ(size_t(2), origins_info.size()); 683 EXPECT_EQ(size_t(2), origins_info.size());
693 EXPECT_TRUE( 684 EXPECT_TRUE(
694 base::PathExists(tracker->GetFullDBFilePath(kOrigin1, kDB1))); 685 base::PathExists(tracker->GetFullDBFilePath(kOrigin1, kDB1)));
695 EXPECT_TRUE( 686 EXPECT_TRUE(
696 base::PathExists(tracker->GetFullDBFilePath(kOrigin2, kDB2))); 687 base::PathExists(tracker->GetFullDBFilePath(kOrigin2, kDB2)));
697 688
698 EXPECT_TRUE(base::PathExists(origin1_db_dir)); 689 EXPECT_TRUE(base::PathExists(origin1_db_dir));
699 EXPECT_TRUE(base::PathExists(origin2_db_dir)); 690 EXPECT_TRUE(base::PathExists(origin2_db_dir));
700 } 691 }
701 692
702 static void EmptyDatabaseNameIsValid() { 693 static void EmptyDatabaseNameIsValid() {
703 const GURL kOrigin(kOrigin1Url); 694 const GURL kOrigin(kOrigin1Url);
704 const std::string kOriginId = storage::GetIdentifierFromOrigin(kOrigin); 695 const std::string kOriginId = storage::GetIdentifierFromOrigin(kOrigin);
705 const base::string16 kEmptyName; 696 const base::string16 kEmptyName;
706 const base::string16 kDescription(ASCIIToUTF16("description")); 697 const base::string16 kDescription(ASCIIToUTF16("description"));
707 const base::string16 kChangedDescription( 698 const base::string16 kChangedDescription(
708 ASCIIToUTF16("changed_description")); 699 ASCIIToUTF16("changed_description"));
709 700
710 // Initialize a tracker database, no need to put it on disk. 701 // Initialize a tracker database, no need to put it on disk.
711 const bool kUseInMemoryTrackerDatabase = true; 702 const bool kUseInMemoryTrackerDatabase = true;
712 base::ScopedTempDir temp_dir; 703 base::ScopedTempDir temp_dir;
713 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 704 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
714 scoped_refptr<DatabaseTracker> tracker( 705 scoped_refptr<DatabaseTracker> tracker(new DatabaseTracker(
715 new DatabaseTracker(temp_dir.path(), kUseInMemoryTrackerDatabase, 706 temp_dir.GetPath(), kUseInMemoryTrackerDatabase, NULL, NULL, NULL));
716 NULL, NULL, NULL));
717 707
718 // Starts off with no databases. 708 // Starts off with no databases.
719 std::vector<OriginInfo> infos; 709 std::vector<OriginInfo> infos;
720 EXPECT_TRUE(tracker->GetAllOriginsInfo(&infos)); 710 EXPECT_TRUE(tracker->GetAllOriginsInfo(&infos));
721 EXPECT_TRUE(infos.empty()); 711 EXPECT_TRUE(infos.empty());
722 712
723 // Create a db with an empty name. 713 // Create a db with an empty name.
724 int64_t database_size = -1; 714 int64_t database_size = -1;
725 tracker->DatabaseOpened(kOriginId, kEmptyName, kDescription, 0, 715 tracker->DatabaseOpened(kOriginId, kEmptyName, kDescription, 0,
726 &database_size); 716 &database_size);
(...skipping 23 matching lines...) Expand all
750 static void HandleSqliteError() { 740 static void HandleSqliteError() {
751 const GURL kOrigin(kOrigin1Url); 741 const GURL kOrigin(kOrigin1Url);
752 const std::string kOriginId = storage::GetIdentifierFromOrigin(kOrigin); 742 const std::string kOriginId = storage::GetIdentifierFromOrigin(kOrigin);
753 const base::string16 kName(ASCIIToUTF16("name")); 743 const base::string16 kName(ASCIIToUTF16("name"));
754 const base::string16 kDescription(ASCIIToUTF16("description")); 744 const base::string16 kDescription(ASCIIToUTF16("description"));
755 745
756 // Initialize a tracker database, no need to put it on disk. 746 // Initialize a tracker database, no need to put it on disk.
757 const bool kUseInMemoryTrackerDatabase = true; 747 const bool kUseInMemoryTrackerDatabase = true;
758 base::ScopedTempDir temp_dir; 748 base::ScopedTempDir temp_dir;
759 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 749 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
760 scoped_refptr<DatabaseTracker> tracker( 750 scoped_refptr<DatabaseTracker> tracker(new DatabaseTracker(
761 new DatabaseTracker(temp_dir.path(), kUseInMemoryTrackerDatabase, 751 temp_dir.GetPath(), kUseInMemoryTrackerDatabase, NULL, NULL, NULL));
762 NULL, NULL, NULL));
763 752
764 // Setup to observe OnScheduledForDelete notifications. 753 // Setup to observe OnScheduledForDelete notifications.
765 TestObserver observer(false, true); 754 TestObserver observer(false, true);
766 tracker->AddObserver(&observer); 755 tracker->AddObserver(&observer);
767 756
768 // Verify does no harm when there is no such database. 757 // Verify does no harm when there is no such database.
769 tracker->HandleSqliteError(kOriginId, kName, SQLITE_CORRUPT); 758 tracker->HandleSqliteError(kOriginId, kName, SQLITE_CORRUPT);
770 EXPECT_FALSE(tracker->IsDatabaseScheduledForDeletion(kOriginId, kName)); 759 EXPECT_FALSE(tracker->IsDatabaseScheduledForDeletion(kOriginId, kName));
771 EXPECT_FALSE(observer.DidReceiveNewNotification()); 760 EXPECT_FALSE(observer.DidReceiveNewNotification());
772 761
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
857 846
858 TEST(DatabaseTrackerTest, EmptyDatabaseNameIsValid) { 847 TEST(DatabaseTrackerTest, EmptyDatabaseNameIsValid) {
859 DatabaseTracker_TestHelper_Test::EmptyDatabaseNameIsValid(); 848 DatabaseTracker_TestHelper_Test::EmptyDatabaseNameIsValid();
860 } 849 }
861 850
862 TEST(DatabaseTrackerTest, HandleSqliteError) { 851 TEST(DatabaseTrackerTest, HandleSqliteError) {
863 DatabaseTracker_TestHelper_Test::HandleSqliteError(); 852 DatabaseTracker_TestHelper_Test::HandleSqliteError();
864 } 853 }
865 854
866 } // namespace content 855 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/cross_site_transfer_browsertest.cc ('k') | content/browser/devtools/devtools_http_handler_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698