Chromium Code Reviews| 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..555075ef1e901fda11716e6258b31e0b0415c4ed |
| --- /dev/null |
| +++ b/chrome/browser/engagement/site_engagement_eviction_policy_unittest.cc |
| @@ -0,0 +1,91 @@ |
| +// 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; |
| + |
| +} // namespace |
| + |
| +class TestSiteEngagementService : public SiteEngagementService { |
| + public: |
| + TestSiteEngagementService() : SiteEngagementService(nullptr) {} |
| + |
| + ~TestSiteEngagementService() override {} |
| + |
| + int GetScore(const GURL& url) override { |
| + return engagement_score_map_[url]; |
| + } |
| + |
| + int GetTotalEngagementPoints() override { |
| + int total = 0; |
| + for (const auto& site : engagement_score_map_) |
| + total += site.second; |
| + return total; |
| + } |
| + |
| + void SetScore(const GURL& origin, int score) { |
| + engagement_score_map_[origin] = score; |
| + } |
| + |
| + private: |
| + std::map<GURL, int> engagement_score_map_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(TestSiteEngagementService); |
| +}; |
| + |
| +class SiteEngagementEvictionPolicyTest : public testing::Test { |
| + public: |
| + SiteEngagementEvictionPolicyTest() |
| + : service_(new TestSiteEngagementService()) {} |
| + |
| + ~SiteEngagementEvictionPolicyTest() override { |
| + } |
| + |
| + |
| + GURL CalculateEvictionOrigin(const std::map<GURL, int64>& usage) { |
| + return SiteEngagementEvictionPolicy::GetEvictionOriginTask:: |
| + 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.
|
| + } |
| + |
| + TestSiteEngagementService* service() { |
| + return service_; |
| + } |
| + |
| + private: |
| + TestSiteEngagementService* service_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(SiteEngagementEvictionPolicyTest); |
| +}; |
| + |
| +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; |
| + |
| + service()->SetScore(url1, 50); |
| + service()->SetScore(url2, 25); |
| + |
| + // When 3 sites have equal usage, evict the site with the least engagement. |
| + EXPECT_EQ(url3, CalculateEvictionOrigin(usage)); |
| + |
| + usage[url2] = usage[url3] + 10; |
| + |
| + // When a site has more engagement, it may get preference over a site with |
| + // 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.
|
| + EXPECT_EQ(url3, CalculateEvictionOrigin(usage)); |
| + |
| + usage[url2] = 15 * 1024; |
| + // 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.
|
| + EXPECT_EQ(url2, CalculateEvictionOrigin(usage)); |
| +} |
|
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.
|