| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/engagement/site_engagement_eviction_policy.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 | |
| 9 #include <memory> | |
| 10 | |
| 11 #include "base/files/scoped_temp_dir.h" | |
| 12 #include "base/macros.h" | |
| 13 #include "base/memory/weak_ptr.h" | |
| 14 #include "base/run_loop.h" | |
| 15 #include "base/threading/thread_task_runner_handle.h" | |
| 16 #include "chrome/browser/engagement/site_engagement_service.h" | |
| 17 #include "content/public/test/mock_special_storage_policy.h" | |
| 18 #include "content/public/test/mock_storage_client.h" | |
| 19 #include "content/public/test/test_browser_thread_bundle.h" | |
| 20 #include "storage/browser/quota/quota_manager.h" | |
| 21 #include "storage/browser/quota/quota_manager_proxy.h" | |
| 22 #include "testing/gtest/include/gtest/gtest.h" | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 const int64_t kGlobalQuota = 25 * 1024; | |
| 27 | |
| 28 } // namespace | |
| 29 | |
| 30 class TestSiteEngagementScoreProvider : public SiteEngagementScoreProvider { | |
| 31 public: | |
| 32 TestSiteEngagementScoreProvider() {} | |
| 33 | |
| 34 virtual ~TestSiteEngagementScoreProvider() {} | |
| 35 | |
| 36 double GetScore(const GURL& url) const override { | |
| 37 const auto& it = engagement_score_map_.find(url); | |
| 38 if (it != engagement_score_map_.end()) | |
| 39 return it->second; | |
| 40 return 0.0; | |
| 41 } | |
| 42 | |
| 43 double GetTotalEngagementPoints() const override { | |
| 44 double total = 0; | |
| 45 for (const auto& site : engagement_score_map_) | |
| 46 total += site.second; | |
| 47 return total; | |
| 48 } | |
| 49 | |
| 50 void SetScore(const GURL& origin, double score) { | |
| 51 engagement_score_map_[origin] = score; | |
| 52 } | |
| 53 | |
| 54 private: | |
| 55 std::map<GURL, double> engagement_score_map_; | |
| 56 | |
| 57 DISALLOW_COPY_AND_ASSIGN(TestSiteEngagementScoreProvider); | |
| 58 }; | |
| 59 | |
| 60 class SiteEngagementEvictionPolicyTest : public testing::Test { | |
| 61 public: | |
| 62 SiteEngagementEvictionPolicyTest() | |
| 63 : score_provider_(new TestSiteEngagementScoreProvider()), | |
| 64 storage_policy_(new content::MockSpecialStoragePolicy()) {} | |
| 65 | |
| 66 ~SiteEngagementEvictionPolicyTest() override {} | |
| 67 | |
| 68 GURL CalculateEvictionOriginWithExceptions( | |
| 69 const std::map<GURL, int64_t>& usage, | |
| 70 const std::set<GURL>& exceptions) { | |
| 71 return SiteEngagementEvictionPolicy::CalculateEvictionOriginForTests( | |
| 72 storage_policy_, score_provider_.get(), exceptions, usage, | |
| 73 kGlobalQuota); | |
| 74 } | |
| 75 | |
| 76 GURL CalculateEvictionOrigin(const std::map<GURL, int64_t>& usage) { | |
| 77 return CalculateEvictionOriginWithExceptions(usage, std::set<GURL>()); | |
| 78 } | |
| 79 | |
| 80 TestSiteEngagementScoreProvider* score_provider() { | |
| 81 return score_provider_.get(); | |
| 82 } | |
| 83 | |
| 84 content::MockSpecialStoragePolicy* storage_policy() { | |
| 85 return storage_policy_.get(); | |
| 86 } | |
| 87 | |
| 88 private: | |
| 89 std::unique_ptr<TestSiteEngagementScoreProvider> score_provider_; | |
| 90 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy_; | |
| 91 | |
| 92 DISALLOW_COPY_AND_ASSIGN(SiteEngagementEvictionPolicyTest); | |
| 93 }; | |
| 94 | |
| 95 TEST_F(SiteEngagementEvictionPolicyTest, GetEvictionOrigin) { | |
| 96 GURL url1("http://www.google.com"); | |
| 97 GURL url2("http://www.example.com"); | |
| 98 GURL url3("http://www.spam.me"); | |
| 99 | |
| 100 std::map<GURL, int64_t> usage; | |
| 101 usage[url1] = 10 * 1024; | |
| 102 usage[url2] = 10 * 1024; | |
| 103 usage[url3] = 10 * 1024; | |
| 104 | |
| 105 score_provider()->SetScore(url1, 50); | |
| 106 score_provider()->SetScore(url2, 25); | |
| 107 | |
| 108 // When 3 sites have equal usage, evict the site with the least engagement. | |
| 109 EXPECT_EQ(url3, CalculateEvictionOrigin(usage)); | |
| 110 | |
| 111 usage[url2] = usage[url3] + 10; | |
| 112 | |
| 113 // Now |url2| has the most usage but |url3| has the least engagement score so | |
| 114 // one of them should be evicted. In this case the heuristic chooses |url3|. | |
| 115 EXPECT_EQ(url3, CalculateEvictionOrigin(usage)); | |
| 116 | |
| 117 // But exceeding allocated usage too much will still result in being evicted | |
| 118 // even though the engagement with |url2| is higher. | |
| 119 usage[url2] = 15 * 1024; | |
| 120 EXPECT_EQ(url2, CalculateEvictionOrigin(usage)); | |
| 121 | |
| 122 // When all origins have the same engagement, the origin with the highest | |
| 123 // usage is evicted. | |
| 124 score_provider()->SetScore(url1, 50); | |
| 125 score_provider()->SetScore(url2, 50); | |
| 126 score_provider()->SetScore(url3, 50); | |
| 127 | |
| 128 usage[url2] = 10 * 1024; | |
| 129 usage[url3] = 20 * 1024; | |
| 130 EXPECT_EQ(url3, CalculateEvictionOrigin(usage)); | |
| 131 } | |
| 132 | |
| 133 // Test that durable and unlimited storage origins are exempt from eviction. | |
| 134 TEST_F(SiteEngagementEvictionPolicyTest, SpecialStoragePolicy) { | |
| 135 GURL url1("http://www.google.com"); | |
| 136 GURL url2("http://www.example.com"); | |
| 137 | |
| 138 std::map<GURL, int64_t> usage; | |
| 139 usage[url1] = 10 * 1024; | |
| 140 usage[url2] = 10 * 1024; | |
| 141 | |
| 142 score_provider()->SetScore(url1, 50); | |
| 143 score_provider()->SetScore(url2, 25); | |
| 144 | |
| 145 EXPECT_EQ(url2, CalculateEvictionOrigin(usage)); | |
| 146 | |
| 147 // Durable storage doesn't get evicted. | |
| 148 storage_policy()->AddDurable(url2); | |
| 149 EXPECT_EQ(url1, CalculateEvictionOrigin(usage)); | |
| 150 | |
| 151 // Unlimited storage doesn't get evicted. | |
| 152 storage_policy()->AddUnlimited(url1); | |
| 153 EXPECT_EQ(GURL(), CalculateEvictionOrigin(usage)); | |
| 154 } | |
| 155 | |
| 156 TEST_F(SiteEngagementEvictionPolicyTest, Exceptions) { | |
| 157 GURL url1("http://www.google.com"); | |
| 158 GURL url2("http://www.example.com"); | |
| 159 | |
| 160 std::map<GURL, int64_t> usage; | |
| 161 usage[url1] = 10 * 1024; | |
| 162 usage[url2] = 10 * 1024; | |
| 163 | |
| 164 score_provider()->SetScore(url1, 50); | |
| 165 score_provider()->SetScore(url2, 25); | |
| 166 | |
| 167 EXPECT_EQ(url2, CalculateEvictionOrigin(usage)); | |
| 168 | |
| 169 // The policy should respect exceptions. | |
| 170 std::set<GURL> exceptions; | |
| 171 exceptions.insert(url2); | |
| 172 EXPECT_EQ(url1, CalculateEvictionOriginWithExceptions(usage, exceptions)); | |
| 173 } | |
| OLD | NEW |