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