Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/files/scoped_temp_dir.h" | |
| 6 #include "base/memory/scoped_ptr.h" | |
| 7 #include "base/memory/weak_ptr.h" | |
| 8 #include "base/run_loop.h" | |
| 9 #include "base/thread_task_runner_handle.h" | |
| 5 #include "chrome/browser/engagement/site_engagement_eviction_policy.h" | 10 #include "chrome/browser/engagement/site_engagement_eviction_policy.h" |
| 6 #include "chrome/browser/engagement/site_engagement_service.h" | 11 #include "chrome/browser/engagement/site_engagement_service.h" |
| 7 #include "content/public/test/mock_special_storage_policy.h" | 12 #include "content/public/test/mock_special_storage_policy.h" |
| 13 #include "content/public/test/mock_storage_client.h" | |
| 14 #include "content/public/test/test_browser_thread.h" | |
| 15 #include "storage/browser/quota/quota_manager.h" | |
| 16 #include "storage/browser/quota/quota_manager_proxy.h" | |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 9 | 18 |
| 10 namespace { | 19 namespace { |
| 11 | 20 |
| 12 const int64 kGlobalQuota = 25 * 1024; | 21 const int64 kGlobalQuota = 25 * 1024; |
| 13 | 22 |
| 14 } // namespace | 23 // Returns a deterministic value for the amount of available disk space. |
| 24 int64 GetAvailableDiskSpaceForTest(const base::FilePath&) { | |
| 25 return kGlobalQuota; | |
| 26 } | |
| 15 | 27 |
| 16 class TestSiteEngagementScoreProvider : public SiteEngagementScoreProvider { | 28 class TestSiteEngagementScoreProvider : public SiteEngagementScoreProvider { |
| 17 public: | 29 public: |
| 18 TestSiteEngagementScoreProvider() {} | 30 TestSiteEngagementScoreProvider() {} |
| 19 | 31 |
| 20 virtual ~TestSiteEngagementScoreProvider() {} | 32 virtual ~TestSiteEngagementScoreProvider() {} |
| 21 | 33 |
| 22 int GetScore(const GURL& url) override { return engagement_score_map_[url]; } | 34 int GetScore(const GURL& url) override { return engagement_score_map_[url]; } |
| 23 | 35 |
| 24 int GetTotalEngagementPoints() override { | 36 int GetTotalEngagementPoints() override { |
| 25 int total = 0; | 37 int total = 0; |
| 26 for (const auto& site : engagement_score_map_) | 38 for (const auto& site : engagement_score_map_) |
| 27 total += site.second; | 39 total += site.second; |
| 28 return total; | 40 return total; |
| 29 } | 41 } |
| 30 | 42 |
| 31 void SetScore(const GURL& origin, int score) { | 43 void SetScore(const GURL& origin, int score) { |
| 32 engagement_score_map_[origin] = score; | 44 engagement_score_map_[origin] = score; |
| 33 } | 45 } |
| 34 | 46 |
| 35 private: | 47 private: |
| 36 std::map<GURL, int> engagement_score_map_; | 48 std::map<GURL, int> engagement_score_map_; |
| 37 | 49 |
| 38 DISALLOW_COPY_AND_ASSIGN(TestSiteEngagementScoreProvider); | 50 DISALLOW_COPY_AND_ASSIGN(TestSiteEngagementScoreProvider); |
| 39 }; | 51 }; |
| 40 | 52 |
| 53 SiteEngagementScoreProvider* GetTestSiteEngagementScoreProvider( | |
| 54 content::BrowserContext*) { | |
| 55 CR_DEFINE_STATIC_LOCAL(TestSiteEngagementScoreProvider, | |
| 56 kGlobalTestScoreProvider, ()); | |
| 57 return &kGlobalTestScoreProvider; | |
| 58 } | |
| 59 | |
| 60 } // namespace | |
| 61 | |
| 62 class SiteEngagementEvictionPolicyWithQuotaManagerTest : public testing::Test { | |
| 63 public: | |
| 64 SiteEngagementEvictionPolicyWithQuotaManagerTest() | |
| 65 : ui_thread_(content::BrowserThread::UI, &message_loop_), | |
| 66 weak_factory_(this) {} | |
| 67 | |
| 68 void SetUp() override { | |
| 69 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); | |
| 70 mock_special_storage_policy_ = new content::MockSpecialStoragePolicy; | |
| 71 | |
| 72 quota_manager_ = | |
| 73 new storage::QuotaManager(false, data_dir_.path(), | |
| 74 base::ThreadTaskRunnerHandle::Get().get(), | |
| 75 base::ThreadTaskRunnerHandle::Get().get(), | |
| 76 mock_special_storage_policy_.get()); | |
| 77 | |
| 78 // Populate the QuotaManager with some data. | |
| 79 static const content::MockOriginData kData[] = { | |
| 80 {"http://foo.com/", storage::kStorageTypeTemporary, 1}, | |
| 81 {"http://foo.com:1/", storage::kStorageTypeTemporary, 20}, | |
| 82 {"https://foo.com/", storage::kStorageTypeTemporary, 300}, | |
| 83 }; | |
| 84 quota_manager_->proxy()->RegisterClient(new content::MockStorageClient( | |
| 85 quota_manager_->proxy(), kData, storage::QuotaClient::kFileSystem, | |
| 86 arraysize(kData))); | |
| 87 | |
| 88 // Set the eviction policy. | |
| 89 quota_manager_->SetQuotaEvictionPolicy( | |
| 90 storage::kStorageTypeTemporary, | |
| 91 make_scoped_ptr(new SiteEngagementEvictionPolicy( | |
| 92 storage::kStorageTypeTemporary, quota_manager_.get(), nullptr))); | |
| 93 | |
| 94 // Don't (automatically) start the eviction for testing. | |
| 95 quota_manager_->eviction_disabled_ = true; | |
| 96 // Don't query the hard disk for remaining capacity. | |
| 97 quota_manager_->get_disk_space_fn_ = &GetAvailableDiskSpaceForTest; | |
| 98 | |
| 99 // Use a TestSiteEngagementScoreProvider when retrieving the engagement | |
| 100 // scores. | |
| 101 SiteEngagementEvictionPolicy::SetSiteEngagementScoreProviderCallback( | |
| 102 base::Bind(&GetTestSiteEngagementScoreProvider)); | |
| 103 } | |
| 104 | |
| 105 void TearDown() override { | |
| 106 // Make sure the quota manager cleans up correctly. | |
| 107 quota_manager_ = nullptr; | |
| 108 base::RunLoop().RunUntilIdle(); | |
| 109 } | |
| 110 | |
| 111 protected: | |
| 112 void GetEvictionOrigin() { | |
| 113 quota_manager_->GetEvictionOrigin( | |
| 114 storage::kStorageTypeTemporary, | |
| 115 base::Bind(&SiteEngagementEvictionPolicyWithQuotaManagerTest:: | |
| 116 OnDidGetEvictionOrigin, | |
| 117 weak_factory_.GetWeakPtr())); | |
| 118 } | |
| 119 | |
| 120 void OnDidGetEvictionOrigin(const GURL& origin) { eviction_origin_ = origin; } | |
| 121 | |
| 122 const GURL& eviction_origin() { return eviction_origin_; } | |
| 123 | |
| 124 private: | |
| 125 base::MessageLoop message_loop_; | |
| 126 base::ScopedTempDir data_dir_; | |
| 127 | |
| 128 scoped_refptr<storage::QuotaManager> quota_manager_; | |
| 129 scoped_refptr<content::MockSpecialStoragePolicy> mock_special_storage_policy_; | |
| 130 | |
| 131 content::TestBrowserThread ui_thread_; | |
| 132 | |
| 133 GURL eviction_origin_; | |
| 134 | |
| 135 base::WeakPtrFactory<SiteEngagementEvictionPolicyWithQuotaManagerTest> | |
| 136 weak_factory_; | |
| 137 | |
| 138 DISALLOW_COPY_AND_ASSIGN(SiteEngagementEvictionPolicyWithQuotaManagerTest); | |
| 139 }; | |
| 140 | |
| 141 // Ensure the SiteEngagementEvictionPolicy completes completes successfully | |
|
raymes
2015/09/17 04:14:31
nit: completes completes
calamity
2015/09/17 06:06:46
Done.
| |
| 142 // when used as a QuotaManager's eviction policy. | |
| 143 TEST_F(SiteEngagementEvictionPolicyWithQuotaManagerTest, GetEvictionOrigin) { | |
| 144 GetEvictionOrigin(); | |
| 145 base::RunLoop().RunUntilIdle(); | |
| 146 | |
| 147 EXPECT_FALSE(eviction_origin().is_empty()); | |
|
raymes
2015/09/17 04:14:31
Can we actually verify what the expected origin sh
calamity
2015/09/17 06:06:46
I think that's the job of the unit tests. This tes
raymes
2015/09/17 06:58:21
I think it would be nice to verify that one of the
calamity
2015/09/21 03:27:51
Done.
| |
| 148 } | |
| 149 | |
| 41 class SiteEngagementEvictionPolicyTest : public testing::Test { | 150 class SiteEngagementEvictionPolicyTest : public testing::Test { |
| 42 public: | 151 public: |
| 43 SiteEngagementEvictionPolicyTest() | 152 SiteEngagementEvictionPolicyTest() |
| 44 : score_provider_(new TestSiteEngagementScoreProvider()), | 153 : score_provider_(new TestSiteEngagementScoreProvider()), |
| 45 storage_policy_(new content::MockSpecialStoragePolicy()) {} | 154 storage_policy_(new content::MockSpecialStoragePolicy()) {} |
| 46 | 155 |
| 47 ~SiteEngagementEvictionPolicyTest() override {} | 156 ~SiteEngagementEvictionPolicyTest() override {} |
| 48 | 157 |
| 49 GURL CalculateEvictionOrigin(const std::map<GURL, int64>& usage) { | 158 GURL CalculateEvictionOrigin(const std::map<GURL, int64>& usage) { |
| 50 return SiteEngagementEvictionPolicy::CalculateEvictionOrigin( | 159 return SiteEngagementEvictionPolicy::CalculateEvictionOrigin( |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 119 EXPECT_EQ(url2, CalculateEvictionOrigin(usage)); | 228 EXPECT_EQ(url2, CalculateEvictionOrigin(usage)); |
| 120 | 229 |
| 121 // Durable storage doesn't get evicted. | 230 // Durable storage doesn't get evicted. |
| 122 storage_policy()->AddDurable(url2); | 231 storage_policy()->AddDurable(url2); |
| 123 EXPECT_EQ(url1, CalculateEvictionOrigin(usage)); | 232 EXPECT_EQ(url1, CalculateEvictionOrigin(usage)); |
| 124 | 233 |
| 125 // Unlimited storage doesn't get evicted. | 234 // Unlimited storage doesn't get evicted. |
| 126 storage_policy()->AddUnlimited(url1); | 235 storage_policy()->AddUnlimited(url1); |
| 127 EXPECT_EQ(GURL(), CalculateEvictionOrigin(usage)); | 236 EXPECT_EQ(GURL(), CalculateEvictionOrigin(usage)); |
| 128 } | 237 } |
| OLD | NEW |