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.
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"
12 const int64 kGlobalQuota
= 25 * 1024;
16 class TestSiteEngagementScoreProvider
: public SiteEngagementScoreProvider
{
18 TestSiteEngagementScoreProvider() {}
20 virtual ~TestSiteEngagementScoreProvider() {}
22 int GetScore(const GURL
& url
) override
{ return engagement_score_map_
[url
]; }
24 int GetTotalEngagementPoints() override
{
26 for (const auto& site
: engagement_score_map_
)
31 void SetScore(const GURL
& origin
, int score
) {
32 engagement_score_map_
[origin
] = score
;
36 std::map
<GURL
, int> engagement_score_map_
;
38 DISALLOW_COPY_AND_ASSIGN(TestSiteEngagementScoreProvider
);
41 class SiteEngagementEvictionPolicyTest
: public testing::Test
{
43 SiteEngagementEvictionPolicyTest()
44 : score_provider_(new TestSiteEngagementScoreProvider()),
45 storage_policy_(new content::MockSpecialStoragePolicy()) {}
47 ~SiteEngagementEvictionPolicyTest() override
{}
49 GURL
CalculateEvictionOrigin(const std::map
<GURL
, int64
>& usage
) {
50 return SiteEngagementEvictionPolicy::CalculateEvictionOrigin(
51 storage_policy_
, score_provider_
.get(), usage
, kGlobalQuota
);
54 TestSiteEngagementScoreProvider
* score_provider() {
55 return score_provider_
.get();
58 content::MockSpecialStoragePolicy
* storage_policy() {
59 return storage_policy_
.get();
63 scoped_ptr
<TestSiteEngagementScoreProvider
> score_provider_
;
64 scoped_refptr
<content::MockSpecialStoragePolicy
> storage_policy_
;
66 DISALLOW_COPY_AND_ASSIGN(SiteEngagementEvictionPolicyTest
);
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");
74 std::map
<GURL
, int64
> usage
;
75 usage
[url1
] = 10 * 1024;
76 usage
[url2
] = 10 * 1024;
77 usage
[url3
] = 10 * 1024;
79 score_provider()->SetScore(url1
, 50);
80 score_provider()->SetScore(url2
, 25);
82 // When 3 sites have equal usage, evict the site with the least engagement.
83 EXPECT_EQ(url3
, CalculateEvictionOrigin(usage
));
85 usage
[url2
] = usage
[url3
] + 10;
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
));
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
));
96 // When all origins have the same engagement, the origin with the highest
98 score_provider()->SetScore(url1
, 50);
99 score_provider()->SetScore(url2
, 50);
100 score_provider()->SetScore(url3
, 50);
102 usage
[url2
] = 10 * 1024;
103 usage
[url3
] = 20 * 1024;
104 EXPECT_EQ(url3
, CalculateEvictionOrigin(usage
));
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");
112 std::map
<GURL
, int64
> usage
;
113 usage
[url1
] = 10 * 1024;
114 usage
[url2
] = 10 * 1024;
116 score_provider()->SetScore(url1
, 50);
117 score_provider()->SetScore(url2
, 25);
119 EXPECT_EQ(url2
, CalculateEvictionOrigin(usage
));
121 // Durable storage doesn't get evicted.
122 storage_policy()->AddDurable(url2
);
123 EXPECT_EQ(url1
, CalculateEvictionOrigin(usage
));
125 // Unlimited storage doesn't get evicted.
126 storage_policy()->AddUnlimited(url1
);
127 EXPECT_EQ(GURL(), CalculateEvictionOrigin(usage
));