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

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

Powered by Google App Engine
This is Rietveld 408576698