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

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

Issue 1343273003: Integrate SiteEngagementEvictionPolicy with QuotaManager. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@add_eviction_policy
Patch Set: export Created 5 years, 2 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
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 "base/files/scoped_temp_dir.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "base/memory/weak_ptr.h"
8 #include "base/run_loop.h"
9 #include "base/thread_task_runner_handle.h"
5 #include "chrome/browser/engagement/site_engagement_eviction_policy.h" 10 #include "chrome/browser/engagement/site_engagement_eviction_policy.h"
6 #include "chrome/browser/engagement/site_engagement_service.h" 11 #include "chrome/browser/engagement/site_engagement_service.h"
7 #include "content/public/test/mock_special_storage_policy.h" 12 #include "content/public/test/mock_special_storage_policy.h"
13 #include "content/public/test/mock_storage_client.h"
14 #include "content/public/test/test_browser_thread_bundle.h"
15 #include "storage/browser/quota/quota_manager.h"
16 #include "storage/browser/quota/quota_manager_proxy.h"
8 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
9 18
10 namespace { 19 namespace {
11 20
21 const char* kFooOrigin = "http://foo.com/";
22
12 const int64 kGlobalQuota = 25 * 1024; 23 const int64 kGlobalQuota = 25 * 1024;
13 24
14 } // namespace 25 // Returns a deterministic value for the amount of available disk space.
26 int64 GetAvailableDiskSpaceForTest(const base::FilePath&) {
27 return kGlobalQuota;
28 }
15 29
16 class TestSiteEngagementScoreProvider : public SiteEngagementScoreProvider { 30 class TestSiteEngagementScoreProvider : public SiteEngagementScoreProvider {
17 public: 31 public:
18 TestSiteEngagementScoreProvider() {} 32 TestSiteEngagementScoreProvider() {}
19 33
20 virtual ~TestSiteEngagementScoreProvider() {} 34 virtual ~TestSiteEngagementScoreProvider() {}
21 35
22 double GetScore(const GURL& url) override { 36 double GetScore(const GURL& url) override {
23 return engagement_score_map_[url]; 37 return engagement_score_map_[url];
24 } 38 }
25 39
26 double GetTotalEngagementPoints() override { 40 double GetTotalEngagementPoints() override {
27 double total = 0; 41 double total = 0;
28 for (const auto& site : engagement_score_map_) 42 for (const auto& site : engagement_score_map_)
29 total += site.second; 43 total += site.second;
30 return total; 44 return total;
31 } 45 }
32 46
33 void SetScore(const GURL& origin, double score) { 47 void SetScore(const GURL& origin, double score) {
34 engagement_score_map_[origin] = score; 48 engagement_score_map_[origin] = score;
35 } 49 }
36 50
37 private: 51 private:
38 std::map<GURL, double> engagement_score_map_; 52 std::map<GURL, double> engagement_score_map_;
39 53
40 DISALLOW_COPY_AND_ASSIGN(TestSiteEngagementScoreProvider); 54 DISALLOW_COPY_AND_ASSIGN(TestSiteEngagementScoreProvider);
41 }; 55 };
42 56
57 SiteEngagementScoreProvider* GetTestSiteEngagementScoreProvider(
58 content::BrowserContext*) {
59 CR_DEFINE_STATIC_LOCAL(TestSiteEngagementScoreProvider,
60 kGlobalTestScoreProvider, ());
61 return &kGlobalTestScoreProvider;
62 }
63
64 } // namespace
65
66 class SiteEngagementEvictionPolicyWithQuotaManagerTest : public testing::Test {
67 public:
68 SiteEngagementEvictionPolicyWithQuotaManagerTest() : weak_factory_(this) {}
69
70 void SetUp() override {
71 ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
72 mock_special_storage_policy_ = new content::MockSpecialStoragePolicy;
73
74 quota_manager_ = new storage::QuotaManager(
75 false, data_dir_.path(), base::ThreadTaskRunnerHandle::Get().get(),
76 base::ThreadTaskRunnerHandle::Get().get(),
77 mock_special_storage_policy_.get());
78
79 // Populate the QuotaManager with some data.
80 static const content::MockOriginData kData[] = {
81 {kFooOrigin, storage::kStorageTypeTemporary, 1},
82 };
83 quota_manager_->proxy()->RegisterClient(new content::MockStorageClient(
84 quota_manager_->proxy(), kData, storage::QuotaClient::kFileSystem,
85 arraysize(kData)));
86
87 // Set the eviction policy.
88 quota_manager_->SetQuotaEvictionPolicy(
89 storage::kStorageTypeTemporary,
90 make_scoped_ptr(new SiteEngagementEvictionPolicy(nullptr)));
91
92 // Don't (automatically) start the eviction for testing.
93 quota_manager_->eviction_disabled_ = true;
94 // Don't query the hard disk for remaining capacity.
95 quota_manager_->get_disk_space_fn_ = &GetAvailableDiskSpaceForTest;
96
97 score_provider_callback_ = base::Bind(&GetTestSiteEngagementScoreProvider);
98
99 // Use a TestSiteEngagementScoreProvider when retrieving the engagement
100 // scores.
101 SiteEngagementEvictionPolicy::
102 SetSiteEngagementScoreProviderCallbackForTests(
103 &score_provider_callback_);
104 }
105
106 void TearDown() override {
107 SiteEngagementEvictionPolicy::
108 SetSiteEngagementScoreProviderCallbackForTests(nullptr);
109 // Make sure the quota manager cleans up correctly.
110 quota_manager_ = nullptr;
111 base::RunLoop().RunUntilIdle();
112 }
113
114 protected:
115 void GetEvictionOrigin() {
116 quota_manager_->GetEvictionOrigin(
117 storage::kStorageTypeTemporary,
118 base::Bind(&SiteEngagementEvictionPolicyWithQuotaManagerTest::
119 OnDidGetEvictionOrigin,
120 weak_factory_.GetWeakPtr()));
121 }
122
123 void OnDidGetEvictionOrigin(const GURL& origin) { eviction_origin_ = origin; }
124
125 const GURL& eviction_origin() { return eviction_origin_; }
126
127 private:
128 content::TestBrowserThreadBundle thread_bundle_;
129 base::ScopedTempDir data_dir_;
130
131 scoped_refptr<storage::QuotaManager> quota_manager_;
132 scoped_refptr<content::MockSpecialStoragePolicy> mock_special_storage_policy_;
133 base::Callback<SiteEngagementScoreProvider*(content::BrowserContext*)>
134 score_provider_callback_;
135
136 GURL eviction_origin_;
137
138 base::WeakPtrFactory<SiteEngagementEvictionPolicyWithQuotaManagerTest>
139 weak_factory_;
140
141 DISALLOW_COPY_AND_ASSIGN(SiteEngagementEvictionPolicyWithQuotaManagerTest);
142 };
143
144 // Ensure the SiteEngagementEvictionPolicy completes successfully
145 // when used as a QuotaManager's eviction policy.
146 TEST_F(SiteEngagementEvictionPolicyWithQuotaManagerTest, GetEvictionOrigin) {
147 GetEvictionOrigin();
148 base::RunLoop().RunUntilIdle();
149
150 EXPECT_EQ(GURL(kFooOrigin), eviction_origin());
151 }
152
43 class SiteEngagementEvictionPolicyTest : public testing::Test { 153 class SiteEngagementEvictionPolicyTest : public testing::Test {
44 public: 154 public:
45 SiteEngagementEvictionPolicyTest() 155 SiteEngagementEvictionPolicyTest()
46 : score_provider_(new TestSiteEngagementScoreProvider()), 156 : score_provider_(new TestSiteEngagementScoreProvider()),
47 storage_policy_(new content::MockSpecialStoragePolicy()) {} 157 storage_policy_(new content::MockSpecialStoragePolicy()) {}
48 158
49 ~SiteEngagementEvictionPolicyTest() override {} 159 ~SiteEngagementEvictionPolicyTest() override {}
50 160
51 GURL CalculateEvictionOrigin(const std::map<GURL, int64>& usage) { 161 GURL CalculateEvictionOrigin(const std::map<GURL, int64>& usage) {
52 return SiteEngagementEvictionPolicy::CalculateEvictionOrigin( 162 return SiteEngagementEvictionPolicy::CalculateEvictionOriginForTests(
53 storage_policy_, score_provider_.get(), usage, kGlobalQuota); 163 storage_policy_, score_provider_.get(), usage, kGlobalQuota);
54 } 164 }
55 165
56 TestSiteEngagementScoreProvider* score_provider() { 166 TestSiteEngagementScoreProvider* score_provider() {
57 return score_provider_.get(); 167 return score_provider_.get();
58 } 168 }
59 169
60 content::MockSpecialStoragePolicy* storage_policy() { 170 content::MockSpecialStoragePolicy* storage_policy() {
61 return storage_policy_.get(); 171 return storage_policy_.get();
62 } 172 }
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 EXPECT_EQ(url2, CalculateEvictionOrigin(usage)); 231 EXPECT_EQ(url2, CalculateEvictionOrigin(usage));
122 232
123 // Durable storage doesn't get evicted. 233 // Durable storage doesn't get evicted.
124 storage_policy()->AddDurable(url2); 234 storage_policy()->AddDurable(url2);
125 EXPECT_EQ(url1, CalculateEvictionOrigin(usage)); 235 EXPECT_EQ(url1, CalculateEvictionOrigin(usage));
126 236
127 // Unlimited storage doesn't get evicted. 237 // Unlimited storage doesn't get evicted.
128 storage_policy()->AddUnlimited(url1); 238 storage_policy()->AddUnlimited(url1);
129 EXPECT_EQ(GURL(), CalculateEvictionOrigin(usage)); 239 EXPECT_EQ(GURL(), CalculateEvictionOrigin(usage));
130 } 240 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698