| 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 "testing/gtest/include/gtest/gtest.h" |
| 8 |
| 9 namespace { |
| 10 |
| 11 const int64 kGlobalQuota = 25 * 1024; |
| 12 |
| 13 } // namespace |
| 14 |
| 15 class TestSiteEngagementService : public SiteEngagementService { |
| 16 public: |
| 17 TestSiteEngagementService() : SiteEngagementService(nullptr) {} |
| 18 |
| 19 ~TestSiteEngagementService() override {} |
| 20 |
| 21 int GetScore(const GURL& url) override { |
| 22 return engagement_score_map_[url]; |
| 23 } |
| 24 |
| 25 int GetTotalEngagementPoints() override { |
| 26 int total = 0; |
| 27 for (const auto& site : engagement_score_map_) |
| 28 total += site.second; |
| 29 return total; |
| 30 } |
| 31 |
| 32 void SetScore(const GURL& origin, int score) { |
| 33 engagement_score_map_[origin] = score; |
| 34 } |
| 35 |
| 36 private: |
| 37 std::map<GURL, int> engagement_score_map_; |
| 38 |
| 39 DISALLOW_COPY_AND_ASSIGN(TestSiteEngagementService); |
| 40 }; |
| 41 |
| 42 class SiteEngagementEvictionPolicyTest : public testing::Test { |
| 43 public: |
| 44 SiteEngagementEvictionPolicyTest() |
| 45 : service_(new TestSiteEngagementService()) {} |
| 46 |
| 47 ~SiteEngagementEvictionPolicyTest() override { |
| 48 } |
| 49 |
| 50 |
| 51 GURL CalculateEvictionOrigin(const std::map<GURL, int64>& usage) { |
| 52 return SiteEngagementEvictionPolicy::CalculateEvictionOrigin( |
| 53 service_, usage, kGlobalQuota); |
| 54 } |
| 55 |
| 56 TestSiteEngagementService* service() { |
| 57 return service_; |
| 58 } |
| 59 |
| 60 private: |
| 61 TestSiteEngagementService* service_; |
| 62 |
| 63 DISALLOW_COPY_AND_ASSIGN(SiteEngagementEvictionPolicyTest); |
| 64 }; |
| 65 |
| 66 TEST_F(SiteEngagementEvictionPolicyTest, GetEvictionOrigin) { |
| 67 GURL url1("http://www.google.com"); |
| 68 GURL url2("http://www.example.com"); |
| 69 GURL url3("http://www.spam.me"); |
| 70 |
| 71 std::map<GURL, int64> usage; |
| 72 usage[url1] = 10 * 1024; |
| 73 usage[url2] = 10 * 1024; |
| 74 usage[url3] = 10 * 1024; |
| 75 |
| 76 service()->SetScore(url1, 50); |
| 77 service()->SetScore(url2, 25); |
| 78 |
| 79 // When 3 sites have equal usage, evict the site with the least engagement. |
| 80 EXPECT_EQ(url3, CalculateEvictionOrigin(usage)); |
| 81 |
| 82 usage[url2] = usage[url3] + 10; |
| 83 |
| 84 // Now |url2| has the most usage but |url3| has the least engagement score so |
| 85 // one of them should be evicted. In this case the heuristic chooses |url3|. |
| 86 EXPECT_EQ(url3, CalculateEvictionOrigin(usage)); |
| 87 |
| 88 // But exceeding allocated usage too much will still result in being evicted |
| 89 // even though the engagement with |url2| is higher. |
| 90 usage[url2] = 15 * 1024; |
| 91 EXPECT_EQ(url2, CalculateEvictionOrigin(usage)); |
| 92 |
| 93 // When all origins have the same engagement, the origin with the highest |
| 94 // usage is evicted. |
| 95 service()->SetScore(url1, 50); |
| 96 service()->SetScore(url2, 50); |
| 97 service()->SetScore(url3, 50); |
| 98 |
| 99 usage[url2] = 10 * 1024; |
| 100 usage[url3] = 20 * 1024; |
| 101 EXPECT_EQ(url3, CalculateEvictionOrigin(usage)); |
| 102 } |
| OLD | NEW |