| 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 <set> | 5 #include <set> |
| 6 #include <sstream> | 6 #include <sstream> |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 using base::MessageLoopProxy; | 26 using base::MessageLoopProxy; |
| 27 | 27 |
| 28 namespace quota { | 28 namespace quota { |
| 29 | 29 |
| 30 // For shorter names. | 30 // For shorter names. |
| 31 const StorageType kTemp = kStorageTypeTemporary; | 31 const StorageType kTemp = kStorageTypeTemporary; |
| 32 const StorageType kPerm = kStorageTypePersistent; | 32 const StorageType kPerm = kStorageTypePersistent; |
| 33 | 33 |
| 34 const int kAllClients = QuotaClient::kAllClientsMask; | 34 const int kAllClients = QuotaClient::kAllClientsMask; |
| 35 | 35 |
| 36 // |
| 37 class TestQuotaManager : public QuotaManager { |
| 38 public: |
| 39 TestQuotaManager(bool is_incognito, |
| 40 const FilePath& profile_path, |
| 41 base::SingleThreadTaskRunner* io_thread, |
| 42 base::SequencedTaskRunner* db_thread, |
| 43 SpecialStoragePolicy* special_storage_policy) |
| 44 : QuotaManager(is_incognito, profile_path, io_thread, db_thread, |
| 45 special_storage_policy) { |
| 46 } |
| 47 |
| 48 // QuotaManager implementation. |
| 49 virtual int64 GetAvailableDiskSpace() const { |
| 50 return 13377331; |
| 51 } |
| 52 |
| 53 private: |
| 54 ~TestQuotaManager() { printf("~TestQuotaManager\n"); }; |
| 55 }; |
| 56 |
| 36 class QuotaManagerTest : public testing::Test { | 57 class QuotaManagerTest : public testing::Test { |
| 37 protected: | 58 protected: |
| 38 typedef QuotaManager::QuotaTableEntry QuotaTableEntry; | 59 typedef QuotaManager::QuotaTableEntry QuotaTableEntry; |
| 39 typedef QuotaManager::QuotaTableEntries QuotaTableEntries; | 60 typedef QuotaManager::QuotaTableEntries QuotaTableEntries; |
| 40 typedef QuotaManager::OriginInfoTableEntries OriginInfoTableEntries; | 61 typedef QuotaManager::OriginInfoTableEntries OriginInfoTableEntries; |
| 41 | 62 |
| 42 public: | 63 public: |
| 43 QuotaManagerTest() | 64 QuotaManagerTest() |
| 44 : weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | 65 : weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 66 quota_status_(kQuotaStatusUnknown), |
| 67 type_(kStorageTypeUnknown), |
| 68 usage_(0), |
| 69 unlimited_usage_(0), |
| 70 quota_(0), |
| 71 available_space_(0), |
| 72 modified_origins_type_(kStorageTypeUnknown), |
| 73 status_callback_count_(0), |
| 74 additional_callback_count_(0), |
| 45 mock_time_counter_(0) { | 75 mock_time_counter_(0) { |
| 46 } | 76 } |
| 47 | 77 |
| 48 void SetUp() { | 78 void SetUp() { |
| 49 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); | 79 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); |
| 50 mock_special_storage_policy_ = new MockSpecialStoragePolicy; | 80 mock_special_storage_policy_ = new MockSpecialStoragePolicy; |
| 51 quota_manager_ = new QuotaManager( | 81 quota_manager_ = new TestQuotaManager( |
| 52 false /* is_incognito */, | 82 false /* is_incognito */, |
| 53 data_dir_.path(), | 83 data_dir_.path(), |
| 54 MessageLoopProxy::current(), | 84 MessageLoopProxy::current(), |
| 55 MessageLoopProxy::current(), | 85 MessageLoopProxy::current(), |
| 56 mock_special_storage_policy_); | 86 mock_special_storage_policy_); |
| 57 // Don't (automatically) start the eviction for testing. | 87 // Don't (automatically) start the eviction for testing. |
| 58 quota_manager_->eviction_disabled_ = true; | 88 quota_manager_->eviction_disabled_ = true; |
| 59 additional_callback_count_ = 0; | 89 additional_callback_count_ = 0; |
| 60 } | 90 } |
| 61 | 91 |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 quota_manager_->DumpOriginInfoTable( | 305 quota_manager_->DumpOriginInfoTable( |
| 276 base::Bind(&QuotaManagerTest::DidDumpOriginInfoTable, | 306 base::Bind(&QuotaManagerTest::DidDumpOriginInfoTable, |
| 277 weak_factory_.GetWeakPtr())); | 307 weak_factory_.GetWeakPtr())); |
| 278 } | 308 } |
| 279 | 309 |
| 280 void DidGetUsageInfo(const UsageInfoEntries& entries) { | 310 void DidGetUsageInfo(const UsageInfoEntries& entries) { |
| 281 usage_info_.insert(usage_info_.begin(), entries.begin(), entries.end()); | 311 usage_info_.insert(usage_info_.begin(), entries.begin(), entries.end()); |
| 282 } | 312 } |
| 283 | 313 |
| 284 void DidGetUsageAndQuota(QuotaStatusCode status, int64 usage, int64 quota) { | 314 void DidGetUsageAndQuota(QuotaStatusCode status, int64 usage, int64 quota) { |
| 315 printf("DidGetUsageAndQuota\n"); |
| 285 quota_status_ = status; | 316 quota_status_ = status; |
| 286 usage_ = usage; | 317 usage_ = usage; |
| 287 quota_ = quota; | 318 quota_ = quota; |
| 288 } | 319 } |
| 289 | 320 |
| 290 void DidGetQuota(QuotaStatusCode status, | 321 void DidGetQuota(QuotaStatusCode status, |
| 291 StorageType type, | 322 StorageType type, |
| 292 int64 quota) { | 323 int64 quota) { |
| 293 quota_status_ = status; | 324 quota_status_ = status; |
| 294 type_ = type; | 325 type_ = type; |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 358 | 389 |
| 359 void GetUsage_WithModifyTestBody(const StorageType type); | 390 void GetUsage_WithModifyTestBody(const StorageType type); |
| 360 | 391 |
| 361 void set_additional_callback_count(int c) { additional_callback_count_ = c; } | 392 void set_additional_callback_count(int c) { additional_callback_count_ = c; } |
| 362 int additional_callback_count() const { return additional_callback_count_; } | 393 int additional_callback_count() const { return additional_callback_count_; } |
| 363 void DidGetUsageAndQuotaAdditional( | 394 void DidGetUsageAndQuotaAdditional( |
| 364 QuotaStatusCode status, int64 usage, int64 quota) { | 395 QuotaStatusCode status, int64 usage, int64 quota) { |
| 365 ++additional_callback_count_; | 396 ++additional_callback_count_; |
| 366 } | 397 } |
| 367 | 398 |
| 368 QuotaManager* quota_manager() const { return quota_manager_.get(); } | 399 TestQuotaManager* quota_manager() const { return /*quota_manager_.get();*/NULL
; } |
| 369 void set_quota_manager(QuotaManager* quota_manager) { | 400 void set_quota_manager(TestQuotaManager* quota_manager) { |
| 401 printf("set_quota_manager: %p\n", quota_manager); |
| 370 quota_manager_ = quota_manager; | 402 quota_manager_ = quota_manager; |
| 371 } | 403 } |
| 372 | 404 |
| 373 MockSpecialStoragePolicy* mock_special_storage_policy() const { | 405 MockSpecialStoragePolicy* mock_special_storage_policy() const { |
| 374 return mock_special_storage_policy_.get(); | 406 return mock_special_storage_policy_.get(); |
| 375 } | 407 } |
| 376 | 408 |
| 377 QuotaStatusCode status() const { return quota_status_; } | 409 QuotaStatusCode status() const { return quota_status_; } |
| 378 const UsageInfoEntries& usage_info() const { return usage_info_; } | 410 const UsageInfoEntries& usage_info() const { return usage_info_; } |
| 379 const std::string& host() const { return host_; } | 411 const std::string& host() const { return host_; } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 396 private: | 428 private: |
| 397 base::Time IncrementMockTime() { | 429 base::Time IncrementMockTime() { |
| 398 ++mock_time_counter_; | 430 ++mock_time_counter_; |
| 399 return base::Time::FromDoubleT(mock_time_counter_ * 10.0); | 431 return base::Time::FromDoubleT(mock_time_counter_ * 10.0); |
| 400 } | 432 } |
| 401 | 433 |
| 402 MessageLoop message_loop_; | 434 MessageLoop message_loop_; |
| 403 ScopedTempDir data_dir_; | 435 ScopedTempDir data_dir_; |
| 404 base::WeakPtrFactory<QuotaManagerTest> weak_factory_; | 436 base::WeakPtrFactory<QuotaManagerTest> weak_factory_; |
| 405 | 437 |
| 406 scoped_refptr<QuotaManager> quota_manager_; | 438 scoped_refptr<TestQuotaManager> quota_manager_; |
| 407 scoped_refptr<MockSpecialStoragePolicy> mock_special_storage_policy_; | 439 scoped_refptr<MockSpecialStoragePolicy> mock_special_storage_policy_; |
| 408 | 440 |
| 409 QuotaStatusCode quota_status_; | 441 QuotaStatusCode quota_status_; |
| 410 UsageInfoEntries usage_info_; | 442 UsageInfoEntries usage_info_; |
| 411 std::string host_; | 443 std::string host_; |
| 412 StorageType type_; | 444 StorageType type_; |
| 413 int64 usage_; | 445 int64 usage_; |
| 414 int64 unlimited_usage_; | 446 int64 unlimited_usage_; |
| 415 int64 quota_; | 447 int64 quota_; |
| 416 int64 available_space_; | 448 int64 available_space_; |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 614 GetUsageAndQuota(GURL("http://foo.com/"), kTemp); | 646 GetUsageAndQuota(GURL("http://foo.com/"), kTemp); |
| 615 MessageLoop::current()->RunAllPending(); | 647 MessageLoop::current()->RunAllPending(); |
| 616 EXPECT_EQ(kQuotaStatusOk, status()); | 648 EXPECT_EQ(kQuotaStatusOk, status()); |
| 617 EXPECT_EQ(10 + 30, usage()); | 649 EXPECT_EQ(10 + 30, usage()); |
| 618 | 650 |
| 619 GetUsageAndQuota(GURL("http://bar.com/"), kPerm); | 651 GetUsageAndQuota(GURL("http://bar.com/"), kPerm); |
| 620 MessageLoop::current()->RunAllPending(); | 652 MessageLoop::current()->RunAllPending(); |
| 621 EXPECT_EQ(kQuotaStatusOk, status()); | 653 EXPECT_EQ(kQuotaStatusOk, status()); |
| 622 EXPECT_EQ(50, usage()); | 654 EXPECT_EQ(50, usage()); |
| 623 | 655 |
| 656 GetAvailableSpace(); |
| 657 MessageLoop::current()->RunAllPending(); |
| 658 EXPECT_EQ(kQuotaStatusOk, status()); |
| 659 EXPECT_LE(0, available_space()); |
| 660 |
| 624 GetUsageAndQuota(GURL("http://unlimited/"), kTemp); | 661 GetUsageAndQuota(GURL("http://unlimited/"), kTemp); |
| 625 MessageLoop::current()->RunAllPending(); | 662 MessageLoop::current()->RunAllPending(); |
| 626 EXPECT_EQ(kQuotaStatusOk, status()); | 663 EXPECT_EQ(kQuotaStatusOk, status()); |
| 627 EXPECT_EQ(1, usage()); | 664 EXPECT_EQ(1, usage()); |
| 628 EXPECT_EQ(kint64max, quota()); | 665 EXPECT_EQ(available_space(), quota()); |
| 629 | 666 |
| 667 printf("before\n"); |
| 630 GetUsageAndQuota(GURL("http://unlimited/"), kPerm); | 668 GetUsageAndQuota(GURL("http://unlimited/"), kPerm); |
| 631 MessageLoop::current()->RunAllPending(); | 669 MessageLoop::current()->RunAllPending(); |
| 670 printf("after\n"); |
| 632 EXPECT_EQ(kQuotaStatusOk, status()); | 671 EXPECT_EQ(kQuotaStatusOk, status()); |
| 633 EXPECT_EQ(1, usage()); | 672 EXPECT_EQ(1, usage()); |
| 634 EXPECT_EQ(kint64max, quota()); | 673 EXPECT_EQ(available_space(), quota()); |
| 635 | 674 |
| 636 GetGlobalUsage(kTemp); | 675 GetGlobalUsage(kTemp); |
| 637 MessageLoop::current()->RunAllPending(); | 676 MessageLoop::current()->RunAllPending(); |
| 638 EXPECT_EQ(kQuotaStatusOk, status()); | 677 EXPECT_EQ(kQuotaStatusOk, status()); |
| 639 EXPECT_EQ(10 + 20 + 30 + 1, usage()); | 678 EXPECT_EQ(10 + 20 + 30 + 1, usage()); |
| 640 EXPECT_EQ(1, unlimited_usage()); | 679 EXPECT_EQ(1, unlimited_usage()); |
| 641 | 680 |
| 642 GetGlobalUsage(kPerm); | 681 GetGlobalUsage(kPerm); |
| 643 MessageLoop::current()->RunAllPending(); | 682 MessageLoop::current()->RunAllPending(); |
| 644 EXPECT_EQ(kQuotaStatusOk, status()); | 683 EXPECT_EQ(kQuotaStatusOk, status()); |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 785 static const MockOriginData kData[] = { | 824 static const MockOriginData kData[] = { |
| 786 { "http://usage10/", kTemp, 10 }, | 825 { "http://usage10/", kTemp, 10 }, |
| 787 { "http://usage50/", kTemp, 50 }, | 826 { "http://usage50/", kTemp, 50 }, |
| 788 { "http://unlimited/", kTemp, 4000 }, | 827 { "http://unlimited/", kTemp, 4000 }, |
| 789 }; | 828 }; |
| 790 mock_special_storage_policy()->AddUnlimited(GURL("http://unlimited/")); | 829 mock_special_storage_policy()->AddUnlimited(GURL("http://unlimited/")); |
| 791 MockStorageClient* client = CreateClient(kData, ARRAYSIZE_UNSAFE(kData), | 830 MockStorageClient* client = CreateClient(kData, ARRAYSIZE_UNSAFE(kData), |
| 792 QuotaClient::kFileSystem); | 831 QuotaClient::kFileSystem); |
| 793 RegisterClient(client); | 832 RegisterClient(client); |
| 794 | 833 |
| 795 // Test when not overbugdet. | 834 // Test when not over budget. |
| 796 SetTemporaryGlobalQuota(1000); | 835 SetTemporaryGlobalQuota(1000); |
| 797 MessageLoop::current()->RunAllPending(); | 836 MessageLoop::current()->RunAllPending(); |
| 798 | 837 |
| 799 GetGlobalUsage(kTemp); | 838 GetGlobalUsage(kTemp); |
| 800 MessageLoop::current()->RunAllPending(); | 839 MessageLoop::current()->RunAllPending(); |
| 801 EXPECT_EQ(10 + 50 + 4000, usage()); | 840 EXPECT_EQ(10 + 50 + 4000, usage()); |
| 802 EXPECT_EQ(4000, unlimited_usage()); | 841 EXPECT_EQ(4000, unlimited_usage()); |
| 803 | 842 |
| 804 const int kPerHostQuotaFor1000 = | 843 const int kPerHostQuotaFor1000 = |
| 805 1000 / QuotaManager::kPerHostTemporaryPortion; | 844 1000 / QuotaManager::kPerHostTemporaryPortion; |
| 806 | 845 |
| 807 GetUsageAndQuota(GURL("http://usage10/"), kTemp); | 846 GetUsageAndQuota(GURL("http://usage10/"), kTemp); |
| 808 MessageLoop::current()->RunAllPending(); | 847 MessageLoop::current()->RunAllPending(); |
| 809 EXPECT_EQ(kQuotaStatusOk, status()); | 848 EXPECT_EQ(kQuotaStatusOk, status()); |
| 810 EXPECT_EQ(10, usage()); | 849 EXPECT_EQ(10, usage()); |
| 811 EXPECT_EQ(kPerHostQuotaFor1000, quota()); | 850 EXPECT_EQ(kPerHostQuotaFor1000, quota()); |
| 812 | 851 |
| 813 GetUsageAndQuota(GURL("http://usage50/"), kTemp); | 852 GetUsageAndQuota(GURL("http://usage50/"), kTemp); |
| 814 MessageLoop::current()->RunAllPending(); | 853 MessageLoop::current()->RunAllPending(); |
| 815 EXPECT_EQ(kQuotaStatusOk, status()); | 854 EXPECT_EQ(kQuotaStatusOk, status()); |
| 816 EXPECT_EQ(50, usage()); | 855 EXPECT_EQ(50, usage()); |
| 817 EXPECT_EQ(kPerHostQuotaFor1000, quota()); | 856 EXPECT_EQ(kPerHostQuotaFor1000, quota()); |
| 818 | 857 |
| 858 GetAvailableSpace(); |
| 859 MessageLoop::current()->RunAllPending(); |
| 860 EXPECT_EQ(kQuotaStatusOk, status()); |
| 861 EXPECT_LE(0, available_space()); |
| 862 |
| 819 GetUsageAndQuota(GURL("http://unlimited/"), kTemp); | 863 GetUsageAndQuota(GURL("http://unlimited/"), kTemp); |
| 820 MessageLoop::current()->RunAllPending(); | 864 MessageLoop::current()->RunAllPending(); |
| 821 EXPECT_EQ(kQuotaStatusOk, status()); | 865 EXPECT_EQ(kQuotaStatusOk, status()); |
| 822 EXPECT_EQ(4000, usage()); | 866 EXPECT_EQ(4000, usage()); |
| 823 EXPECT_EQ(kint64max, quota()); | 867 EXPECT_EQ(available_space(), quota()); |
| 824 | 868 |
| 825 // Test when overbugdet. | 869 // Test when over budget. |
| 826 SetTemporaryGlobalQuota(100); | 870 SetTemporaryGlobalQuota(100); |
| 827 MessageLoop::current()->RunAllPending(); | 871 MessageLoop::current()->RunAllPending(); |
| 828 | 872 |
| 829 const int kPerHostQuotaFor100 = | 873 const int kPerHostQuotaFor100 = |
| 830 100 / QuotaManager::kPerHostTemporaryPortion; | 874 100 / QuotaManager::kPerHostTemporaryPortion; |
| 831 | 875 |
| 832 GetUsageAndQuota(GURL("http://usage10/"), kTemp); | 876 GetUsageAndQuota(GURL("http://usage10/"), kTemp); |
| 833 MessageLoop::current()->RunAllPending(); | 877 MessageLoop::current()->RunAllPending(); |
| 834 EXPECT_EQ(kQuotaStatusOk, status()); | 878 EXPECT_EQ(kQuotaStatusOk, status()); |
| 835 EXPECT_EQ(10, usage()); | 879 EXPECT_EQ(10, usage()); |
| 836 EXPECT_EQ(kPerHostQuotaFor100, quota()); | 880 EXPECT_EQ(kPerHostQuotaFor100, quota()); |
| 837 | 881 |
| 838 GetUsageAndQuota(GURL("http://usage50/"), kTemp); | 882 GetUsageAndQuota(GURL("http://usage50/"), kTemp); |
| 839 MessageLoop::current()->RunAllPending(); | 883 MessageLoop::current()->RunAllPending(); |
| 840 EXPECT_EQ(kQuotaStatusOk, status()); | 884 EXPECT_EQ(kQuotaStatusOk, status()); |
| 841 EXPECT_EQ(50, usage()); | 885 EXPECT_EQ(50, usage()); |
| 842 EXPECT_EQ(kPerHostQuotaFor100, quota()); | 886 EXPECT_EQ(kPerHostQuotaFor100, quota()); |
| 843 | 887 |
| 888 GetAvailableSpace(); |
| 889 MessageLoop::current()->RunAllPending(); |
| 890 EXPECT_EQ(kQuotaStatusOk, status()); |
| 891 EXPECT_LE(0, available_space()); |
| 892 |
| 844 GetUsageAndQuota(GURL("http://unlimited/"), kTemp); | 893 GetUsageAndQuota(GURL("http://unlimited/"), kTemp); |
| 845 MessageLoop::current()->RunAllPending(); | 894 MessageLoop::current()->RunAllPending(); |
| 846 EXPECT_EQ(kQuotaStatusOk, status()); | 895 EXPECT_EQ(kQuotaStatusOk, status()); |
| 847 EXPECT_EQ(4000, usage()); | 896 EXPECT_EQ(4000, usage()); |
| 848 EXPECT_EQ(kint64max, quota()); | 897 EXPECT_EQ(available_space(), quota()); |
| 849 | 898 |
| 850 // Revoke the unlimited rights and make sure the change is noticed. | 899 // Revoke the unlimited rights and make sure the change is noticed. |
| 851 mock_special_storage_policy()->Reset(); | 900 mock_special_storage_policy()->Reset(); |
| 852 mock_special_storage_policy()->NotifyChanged(); | 901 mock_special_storage_policy()->NotifyChanged(); |
| 853 | 902 |
| 854 GetGlobalUsage(kTemp); | 903 GetGlobalUsage(kTemp); |
| 855 MessageLoop::current()->RunAllPending(); | 904 MessageLoop::current()->RunAllPending(); |
| 856 EXPECT_EQ(10 + 50 + 4000, usage()); | 905 EXPECT_EQ(10 + 50 + 4000, usage()); |
| 857 EXPECT_EQ(0, unlimited_usage()); | 906 EXPECT_EQ(0, unlimited_usage()); |
| 858 | 907 |
| (...skipping 1169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2028 EXPECT_EQ(predelete_foo_tmp - 2 - 1, usage()); | 2077 EXPECT_EQ(predelete_foo_tmp - 2 - 1, usage()); |
| 2029 | 2078 |
| 2030 DeleteHostData("foo.com", kTemp, | 2079 DeleteHostData("foo.com", kTemp, |
| 2031 QuotaClient::kDatabase | QuotaClient::kIndexedDatabase); | 2080 QuotaClient::kDatabase | QuotaClient::kIndexedDatabase); |
| 2032 MessageLoop::current()->RunAllPending(); | 2081 MessageLoop::current()->RunAllPending(); |
| 2033 GetHostUsage("foo.com", kTemp); | 2082 GetHostUsage("foo.com", kTemp); |
| 2034 MessageLoop::current()->RunAllPending(); | 2083 MessageLoop::current()->RunAllPending(); |
| 2035 EXPECT_EQ(predelete_foo_tmp - 8 - 4 - 2 - 1, usage()); | 2084 EXPECT_EQ(predelete_foo_tmp - 8 - 4 - 2 - 1, usage()); |
| 2036 } | 2085 } |
| 2037 } // namespace quota | 2086 } // namespace quota |
| OLD | NEW |