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

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: 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 const int64 kGlobalQuota = 25 * 1024;
11 }
12
13 class TestSiteEngagementEvictionPolicy : public SiteEngagementEvictionPolicy {
14 public:
15 TestSiteEngagementEvictionPolicy() : SiteEngagementEvictionPolicy(nullptr) {}
16
17 ~TestSiteEngagementEvictionPolicy() override {}
18
19 void SetScore(const GURL& origin, int score) {
20 engagment_score_map_[origin] = score;
21 }
22
23 protected:
24 // Overridden in tests.
25 int GetScore(const GURL& origin) override {
26 return engagment_score_map_[origin];
27 }
28
29 int GetTotalEngagementPoints() override {
30 int total = 0;
31 for (const auto& site : engagment_score_map_)
32 total += site.second;
33
34 return total;
35 }
36
37 private:
38 std::map<GURL, int> engagment_score_map_;
39
40 DISALLOW_COPY_AND_ASSIGN(TestSiteEngagementEvictionPolicy);
41 };
42
43 using SiteEngagementEvictionPolicyTest = testing::Test;
44
45 TEST_F(SiteEngagementEvictionPolicyTest, GetEvictionOrigin) {
46 GURL url1("http://www.google.com");
47 GURL url2("http://www.example.com");
48 GURL url3("http://www.spam.me");
49
50 std::map<GURL, int64> usage;
51 usage[url1] = 10 * 1024;
52 usage[url2] = 10 * 1024;
53 usage[url3] = 10 * 1024;
54
55 TestSiteEngagementEvictionPolicy policy;
56 policy.SetScore(url1, 50);
57 policy.SetScore(url2, 25);
58
59 // When 3 sites have equal usage, evict the site with the least engagement.
60 EXPECT_EQ(url3, policy.GetEvictionOriginOnUIThread(usage, kGlobalQuota));
61
62 usage[url2] = usage[url3] + 10;
63
64 // When a site has more engagement, it may get preference over a site with
65 // less usage.
66 EXPECT_EQ(url3, policy.GetEvictionOriginOnUIThread(usage, kGlobalQuota));
67
68 usage[url2] = 15 * 1024;
69 // But exceeding allocated usage too much will still result in being evicted.
70 EXPECT_EQ(url2, policy.GetEvictionOriginOnUIThread(usage, kGlobalQuota));
71 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698