Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(308)

Side by Side Diff: chrome/browser/engagement/site_engagement_eviction_policy_unittest.cc

Issue 1221523003: Add a SiteEngagementEvictionPolicy. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@get_total_engagement_points
Patch Set: refactor again Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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::GetEvictionOriginTask::
53 CalculateEvictionOrigin(service_, usage, kGlobalQuota);
raymes 2015/07/21 03:16:00 Ohh I see, this one is static for testing. That se
calamity 2015/07/21 04:57:09 Done.
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 // When a site has more engagement, it may get preference over a site with
85 // less usage.
raymes 2015/07/21 03:16:00 This comment is a bit hard to read in the context
calamity 2015/07/21 04:57:09 Done.
86 EXPECT_EQ(url3, CalculateEvictionOrigin(usage));
87
88 usage[url2] = 15 * 1024;
89 // But exceeding allocated usage too much will still result in being evicted.
raymes 2015/07/21 03:16:00 result in being evicted even though the engagement
calamity 2015/07/21 04:57:09 Done.
90 EXPECT_EQ(url2, CalculateEvictionOrigin(usage));
91 }
raymes 2015/07/21 03:16:00 I can think of at least one more test to add: All
calamity 2015/07/21 04:57:09 Done.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698