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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/engagement/site_engagement_eviction_policy_unittest.cc
diff --git a/chrome/browser/engagement/site_engagement_eviction_policy_unittest.cc b/chrome/browser/engagement/site_engagement_eviction_policy_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..fa4a2ef7d28d9bd76bf305849418480e05c1e7a3
--- /dev/null
+++ b/chrome/browser/engagement/site_engagement_eviction_policy_unittest.cc
@@ -0,0 +1,71 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/engagement/site_engagement_eviction_policy.h"
+#include "chrome/browser/engagement/site_engagement_service.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+const int64 kGlobalQuota = 25 * 1024;
+}
+
+class TestSiteEngagementEvictionPolicy : public SiteEngagementEvictionPolicy {
+ public:
+ TestSiteEngagementEvictionPolicy() : SiteEngagementEvictionPolicy(nullptr) {}
+
+ ~TestSiteEngagementEvictionPolicy() override {}
+
+ void SetScore(const GURL& origin, int score) {
+ engagment_score_map_[origin] = score;
+ }
+
+ protected:
+ // Overridden in tests.
+ int GetScore(const GURL& origin) override {
+ return engagment_score_map_[origin];
+ }
+
+ int GetTotalEngagementPoints() override {
+ int total = 0;
+ for (const auto& site : engagment_score_map_)
+ total += site.second;
+
+ return total;
+ }
+
+ private:
+ std::map<GURL, int> engagment_score_map_;
+
+ DISALLOW_COPY_AND_ASSIGN(TestSiteEngagementEvictionPolicy);
+};
+
+using SiteEngagementEvictionPolicyTest = testing::Test;
+
+TEST_F(SiteEngagementEvictionPolicyTest, GetEvictionOrigin) {
+ GURL url1("http://www.google.com");
+ GURL url2("http://www.example.com");
+ GURL url3("http://www.spam.me");
+
+ std::map<GURL, int64> usage;
+ usage[url1] = 10 * 1024;
+ usage[url2] = 10 * 1024;
+ usage[url3] = 10 * 1024;
+
+ TestSiteEngagementEvictionPolicy policy;
+ policy.SetScore(url1, 50);
+ policy.SetScore(url2, 25);
+
+ // When 3 sites have equal usage, evict the site with the least engagement.
+ EXPECT_EQ(url3, policy.GetEvictionOriginOnUIThread(usage, kGlobalQuota));
+
+ usage[url2] = usage[url3] + 10;
+
+ // When a site has more engagement, it may get preference over a site with
+ // less usage.
+ EXPECT_EQ(url3, policy.GetEvictionOriginOnUIThread(usage, kGlobalQuota));
+
+ usage[url2] = 15 * 1024;
+ // But exceeding allocated usage too much will still result in being evicted.
+ EXPECT_EQ(url2, policy.GetEvictionOriginOnUIThread(usage, kGlobalQuota));
+}

Powered by Google App Engine
This is Rietveld 408576698