| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stdint.h> |
| 6 |
| 5 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/macros.h" |
| 6 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 7 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
| 8 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 9 #include "base/thread_task_runner_handle.h" | 12 #include "base/thread_task_runner_handle.h" |
| 10 #include "chrome/browser/engagement/site_engagement_eviction_policy.h" | 13 #include "chrome/browser/engagement/site_engagement_eviction_policy.h" |
| 11 #include "chrome/browser/engagement/site_engagement_service.h" | 14 #include "chrome/browser/engagement/site_engagement_service.h" |
| 12 #include "content/public/test/mock_special_storage_policy.h" | 15 #include "content/public/test/mock_special_storage_policy.h" |
| 13 #include "content/public/test/mock_storage_client.h" | 16 #include "content/public/test/mock_storage_client.h" |
| 14 #include "content/public/test/test_browser_thread_bundle.h" | 17 #include "content/public/test/test_browser_thread_bundle.h" |
| 15 #include "storage/browser/quota/quota_manager.h" | 18 #include "storage/browser/quota/quota_manager.h" |
| 16 #include "storage/browser/quota/quota_manager_proxy.h" | 19 #include "storage/browser/quota/quota_manager_proxy.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 21 |
| 19 namespace { | 22 namespace { |
| 20 | 23 |
| 21 const int64 kGlobalQuota = 25 * 1024; | 24 const int64_t kGlobalQuota = 25 * 1024; |
| 22 | 25 |
| 23 } // namespace | 26 } // namespace |
| 24 | 27 |
| 25 class TestSiteEngagementScoreProvider : public SiteEngagementScoreProvider { | 28 class TestSiteEngagementScoreProvider : public SiteEngagementScoreProvider { |
| 26 public: | 29 public: |
| 27 TestSiteEngagementScoreProvider() {} | 30 TestSiteEngagementScoreProvider() {} |
| 28 | 31 |
| 29 virtual ~TestSiteEngagementScoreProvider() {} | 32 virtual ~TestSiteEngagementScoreProvider() {} |
| 30 | 33 |
| 31 double GetScore(const GURL& url) override { | 34 double GetScore(const GURL& url) override { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 50 }; | 53 }; |
| 51 | 54 |
| 52 class SiteEngagementEvictionPolicyTest : public testing::Test { | 55 class SiteEngagementEvictionPolicyTest : public testing::Test { |
| 53 public: | 56 public: |
| 54 SiteEngagementEvictionPolicyTest() | 57 SiteEngagementEvictionPolicyTest() |
| 55 : score_provider_(new TestSiteEngagementScoreProvider()), | 58 : score_provider_(new TestSiteEngagementScoreProvider()), |
| 56 storage_policy_(new content::MockSpecialStoragePolicy()) {} | 59 storage_policy_(new content::MockSpecialStoragePolicy()) {} |
| 57 | 60 |
| 58 ~SiteEngagementEvictionPolicyTest() override {} | 61 ~SiteEngagementEvictionPolicyTest() override {} |
| 59 | 62 |
| 60 GURL CalculateEvictionOriginWithExceptions(const std::map<GURL, int64>& usage, | 63 GURL CalculateEvictionOriginWithExceptions( |
| 61 const std::set<GURL>& exceptions) { | 64 const std::map<GURL, int64_t>& usage, |
| 65 const std::set<GURL>& exceptions) { |
| 62 return SiteEngagementEvictionPolicy::CalculateEvictionOriginForTests( | 66 return SiteEngagementEvictionPolicy::CalculateEvictionOriginForTests( |
| 63 storage_policy_, score_provider_.get(), exceptions, usage, | 67 storage_policy_, score_provider_.get(), exceptions, usage, |
| 64 kGlobalQuota); | 68 kGlobalQuota); |
| 65 } | 69 } |
| 66 | 70 |
| 67 GURL CalculateEvictionOrigin(const std::map<GURL, int64>& usage) { | 71 GURL CalculateEvictionOrigin(const std::map<GURL, int64_t>& usage) { |
| 68 return CalculateEvictionOriginWithExceptions(usage, std::set<GURL>()); | 72 return CalculateEvictionOriginWithExceptions(usage, std::set<GURL>()); |
| 69 } | 73 } |
| 70 | 74 |
| 71 TestSiteEngagementScoreProvider* score_provider() { | 75 TestSiteEngagementScoreProvider* score_provider() { |
| 72 return score_provider_.get(); | 76 return score_provider_.get(); |
| 73 } | 77 } |
| 74 | 78 |
| 75 content::MockSpecialStoragePolicy* storage_policy() { | 79 content::MockSpecialStoragePolicy* storage_policy() { |
| 76 return storage_policy_.get(); | 80 return storage_policy_.get(); |
| 77 } | 81 } |
| 78 | 82 |
| 79 private: | 83 private: |
| 80 scoped_ptr<TestSiteEngagementScoreProvider> score_provider_; | 84 scoped_ptr<TestSiteEngagementScoreProvider> score_provider_; |
| 81 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy_; | 85 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy_; |
| 82 | 86 |
| 83 DISALLOW_COPY_AND_ASSIGN(SiteEngagementEvictionPolicyTest); | 87 DISALLOW_COPY_AND_ASSIGN(SiteEngagementEvictionPolicyTest); |
| 84 }; | 88 }; |
| 85 | 89 |
| 86 TEST_F(SiteEngagementEvictionPolicyTest, GetEvictionOrigin) { | 90 TEST_F(SiteEngagementEvictionPolicyTest, GetEvictionOrigin) { |
| 87 GURL url1("http://www.google.com"); | 91 GURL url1("http://www.google.com"); |
| 88 GURL url2("http://www.example.com"); | 92 GURL url2("http://www.example.com"); |
| 89 GURL url3("http://www.spam.me"); | 93 GURL url3("http://www.spam.me"); |
| 90 | 94 |
| 91 std::map<GURL, int64> usage; | 95 std::map<GURL, int64_t> usage; |
| 92 usage[url1] = 10 * 1024; | 96 usage[url1] = 10 * 1024; |
| 93 usage[url2] = 10 * 1024; | 97 usage[url2] = 10 * 1024; |
| 94 usage[url3] = 10 * 1024; | 98 usage[url3] = 10 * 1024; |
| 95 | 99 |
| 96 score_provider()->SetScore(url1, 50); | 100 score_provider()->SetScore(url1, 50); |
| 97 score_provider()->SetScore(url2, 25); | 101 score_provider()->SetScore(url2, 25); |
| 98 | 102 |
| 99 // When 3 sites have equal usage, evict the site with the least engagement. | 103 // When 3 sites have equal usage, evict the site with the least engagement. |
| 100 EXPECT_EQ(url3, CalculateEvictionOrigin(usage)); | 104 EXPECT_EQ(url3, CalculateEvictionOrigin(usage)); |
| 101 | 105 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 119 usage[url2] = 10 * 1024; | 123 usage[url2] = 10 * 1024; |
| 120 usage[url3] = 20 * 1024; | 124 usage[url3] = 20 * 1024; |
| 121 EXPECT_EQ(url3, CalculateEvictionOrigin(usage)); | 125 EXPECT_EQ(url3, CalculateEvictionOrigin(usage)); |
| 122 } | 126 } |
| 123 | 127 |
| 124 // Test that durable and unlimited storage origins are exempt from eviction. | 128 // Test that durable and unlimited storage origins are exempt from eviction. |
| 125 TEST_F(SiteEngagementEvictionPolicyTest, SpecialStoragePolicy) { | 129 TEST_F(SiteEngagementEvictionPolicyTest, SpecialStoragePolicy) { |
| 126 GURL url1("http://www.google.com"); | 130 GURL url1("http://www.google.com"); |
| 127 GURL url2("http://www.example.com"); | 131 GURL url2("http://www.example.com"); |
| 128 | 132 |
| 129 std::map<GURL, int64> usage; | 133 std::map<GURL, int64_t> usage; |
| 130 usage[url1] = 10 * 1024; | 134 usage[url1] = 10 * 1024; |
| 131 usage[url2] = 10 * 1024; | 135 usage[url2] = 10 * 1024; |
| 132 | 136 |
| 133 score_provider()->SetScore(url1, 50); | 137 score_provider()->SetScore(url1, 50); |
| 134 score_provider()->SetScore(url2, 25); | 138 score_provider()->SetScore(url2, 25); |
| 135 | 139 |
| 136 EXPECT_EQ(url2, CalculateEvictionOrigin(usage)); | 140 EXPECT_EQ(url2, CalculateEvictionOrigin(usage)); |
| 137 | 141 |
| 138 // Durable storage doesn't get evicted. | 142 // Durable storage doesn't get evicted. |
| 139 storage_policy()->AddDurable(url2); | 143 storage_policy()->AddDurable(url2); |
| 140 EXPECT_EQ(url1, CalculateEvictionOrigin(usage)); | 144 EXPECT_EQ(url1, CalculateEvictionOrigin(usage)); |
| 141 | 145 |
| 142 // Unlimited storage doesn't get evicted. | 146 // Unlimited storage doesn't get evicted. |
| 143 storage_policy()->AddUnlimited(url1); | 147 storage_policy()->AddUnlimited(url1); |
| 144 EXPECT_EQ(GURL(), CalculateEvictionOrigin(usage)); | 148 EXPECT_EQ(GURL(), CalculateEvictionOrigin(usage)); |
| 145 } | 149 } |
| 146 | 150 |
| 147 TEST_F(SiteEngagementEvictionPolicyTest, Exceptions) { | 151 TEST_F(SiteEngagementEvictionPolicyTest, Exceptions) { |
| 148 GURL url1("http://www.google.com"); | 152 GURL url1("http://www.google.com"); |
| 149 GURL url2("http://www.example.com"); | 153 GURL url2("http://www.example.com"); |
| 150 | 154 |
| 151 std::map<GURL, int64> usage; | 155 std::map<GURL, int64_t> usage; |
| 152 usage[url1] = 10 * 1024; | 156 usage[url1] = 10 * 1024; |
| 153 usage[url2] = 10 * 1024; | 157 usage[url2] = 10 * 1024; |
| 154 | 158 |
| 155 score_provider()->SetScore(url1, 50); | 159 score_provider()->SetScore(url1, 50); |
| 156 score_provider()->SetScore(url2, 25); | 160 score_provider()->SetScore(url2, 25); |
| 157 | 161 |
| 158 EXPECT_EQ(url2, CalculateEvictionOrigin(usage)); | 162 EXPECT_EQ(url2, CalculateEvictionOrigin(usage)); |
| 159 | 163 |
| 160 // The policy should respect exceptions. | 164 // The policy should respect exceptions. |
| 161 std::set<GURL> exceptions; | 165 std::set<GURL> exceptions; |
| 162 exceptions.insert(url2); | 166 exceptions.insert(url2); |
| 163 EXPECT_EQ(url1, CalculateEvictionOriginWithExceptions(usage, exceptions)); | 167 EXPECT_EQ(url1, CalculateEvictionOriginWithExceptions(usage, exceptions)); |
| 164 } | 168 } |
| OLD | NEW |