| 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 #include "chrome/browser/engagement/site_engagement_service.h" |
| 7 #include "content/public/test/mock_special_storage_policy.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 |
| 10 namespace { |
| 11 |
| 12 const int64 kGlobalQuota = 25 * 1024; |
| 13 |
| 14 } // namespace |
| 15 |
| 16 class TestSiteEngagementScoreProvider : public SiteEngagementScoreProvider { |
| 17 public: |
| 18 TestSiteEngagementScoreProvider() {} |
| 19 |
| 20 virtual ~TestSiteEngagementScoreProvider() {} |
| 21 |
| 22 int GetScore(const GURL& url) override { return engagement_score_map_[url]; } |
| 23 |
| 24 int GetTotalEngagementPoints() override { |
| 25 int total = 0; |
| 26 for (const auto& site : engagement_score_map_) |
| 27 total += site.second; |
| 28 return total; |
| 29 } |
| 30 |
| 31 void SetScore(const GURL& origin, int score) { |
| 32 engagement_score_map_[origin] = score; |
| 33 } |
| 34 |
| 35 private: |
| 36 std::map<GURL, int> engagement_score_map_; |
| 37 |
| 38 DISALLOW_COPY_AND_ASSIGN(TestSiteEngagementScoreProvider); |
| 39 }; |
| 40 |
| 41 class SiteEngagementEvictionPolicyTest : public testing::Test { |
| 42 public: |
| 43 SiteEngagementEvictionPolicyTest() |
| 44 : score_provider_(new TestSiteEngagementScoreProvider()), |
| 45 storage_policy_(new content::MockSpecialStoragePolicy()) {} |
| 46 |
| 47 ~SiteEngagementEvictionPolicyTest() override {} |
| 48 |
| 49 GURL CalculateEvictionOrigin(const std::map<GURL, int64>& usage) { |
| 50 return SiteEngagementEvictionPolicy::CalculateEvictionOrigin( |
| 51 storage_policy_, score_provider_.get(), usage, kGlobalQuota); |
| 52 } |
| 53 |
| 54 TestSiteEngagementScoreProvider* score_provider() { |
| 55 return score_provider_.get(); |
| 56 } |
| 57 |
| 58 content::MockSpecialStoragePolicy* storage_policy() { |
| 59 return storage_policy_.get(); |
| 60 } |
| 61 |
| 62 private: |
| 63 scoped_ptr<TestSiteEngagementScoreProvider> score_provider_; |
| 64 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy_; |
| 65 |
| 66 DISALLOW_COPY_AND_ASSIGN(SiteEngagementEvictionPolicyTest); |
| 67 }; |
| 68 |
| 69 TEST_F(SiteEngagementEvictionPolicyTest, GetEvictionOrigin) { |
| 70 GURL url1("http://www.google.com"); |
| 71 GURL url2("http://www.example.com"); |
| 72 GURL url3("http://www.spam.me"); |
| 73 |
| 74 std::map<GURL, int64> usage; |
| 75 usage[url1] = 10 * 1024; |
| 76 usage[url2] = 10 * 1024; |
| 77 usage[url3] = 10 * 1024; |
| 78 |
| 79 score_provider()->SetScore(url1, 50); |
| 80 score_provider()->SetScore(url2, 25); |
| 81 |
| 82 // When 3 sites have equal usage, evict the site with the least engagement. |
| 83 EXPECT_EQ(url3, CalculateEvictionOrigin(usage)); |
| 84 |
| 85 usage[url2] = usage[url3] + 10; |
| 86 |
| 87 // Now |url2| has the most usage but |url3| has the least engagement score so |
| 88 // one of them should be evicted. In this case the heuristic chooses |url3|. |
| 89 EXPECT_EQ(url3, CalculateEvictionOrigin(usage)); |
| 90 |
| 91 // But exceeding allocated usage too much will still result in being evicted |
| 92 // even though the engagement with |url2| is higher. |
| 93 usage[url2] = 15 * 1024; |
| 94 EXPECT_EQ(url2, CalculateEvictionOrigin(usage)); |
| 95 |
| 96 // When all origins have the same engagement, the origin with the highest |
| 97 // usage is evicted. |
| 98 score_provider()->SetScore(url1, 50); |
| 99 score_provider()->SetScore(url2, 50); |
| 100 score_provider()->SetScore(url3, 50); |
| 101 |
| 102 usage[url2] = 10 * 1024; |
| 103 usage[url3] = 20 * 1024; |
| 104 EXPECT_EQ(url3, CalculateEvictionOrigin(usage)); |
| 105 } |
| 106 |
| 107 // Test that durable and unlimited storage origins are exempt from eviction. |
| 108 TEST_F(SiteEngagementEvictionPolicyTest, SpecialStoragePolicy) { |
| 109 GURL url1("http://www.google.com"); |
| 110 GURL url2("http://www.example.com"); |
| 111 |
| 112 std::map<GURL, int64> usage; |
| 113 usage[url1] = 10 * 1024; |
| 114 usage[url2] = 10 * 1024; |
| 115 |
| 116 score_provider()->SetScore(url1, 50); |
| 117 score_provider()->SetScore(url2, 25); |
| 118 |
| 119 EXPECT_EQ(url2, CalculateEvictionOrigin(usage)); |
| 120 |
| 121 // Durable storage doesn't get evicted. |
| 122 storage_policy()->AddDurable(url2); |
| 123 EXPECT_EQ(url1, CalculateEvictionOrigin(usage)); |
| 124 |
| 125 // Unlimited storage doesn't get evicted. |
| 126 storage_policy()->AddUnlimited(url1); |
| 127 EXPECT_EQ(GURL(), CalculateEvictionOrigin(usage)); |
| 128 } |
| OLD | NEW |