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

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: address_comments 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 {
michaeln 2015/10/12 23:36:26 The production code looks pretty straight forward
calamity 2015/10/13 08:34:40 Ah yeah, this was an integration test with the Quo
michaeln 2015/10/13 20:13:39 Ok, thnx for getting rid of the global ptr in the
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(),
michaeln 2015/10/12 23:36:26 The trailing .get()s are not needed (or wanted rea
calamity 2015/10/13 08:34:40 N/A.
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_->SetTemporaryStorageEvictionPolicy(
89 make_scoped_ptr(new SiteEngagementEvictionPolicy(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_->GetUsageAndQuotaForEviction(base::Bind(
116 &SiteEngagementEvictionPolicyWithQuotaManagerTest::DoGetEvictionOrigin,
117 base::Unretained(this)));
118 }
119
120 void DoGetEvictionOrigin(storage::QuotaStatusCode status,
121 const storage::UsageAndQuota& usage_and_quota) {
122 quota_manager_->GetEvictionOrigin(
123 storage::kStorageTypeTemporary, kGlobalQuota,
124 base::Bind(&SiteEngagementEvictionPolicyWithQuotaManagerTest::
125 OnDidGetEvictionOrigin,
126 weak_factory_.GetWeakPtr()));
127 }
128
129 void OnDidGetEvictionOrigin(const GURL& origin) { eviction_origin_ = origin; }
130
131 const GURL& eviction_origin() { return eviction_origin_; }
132
133 private:
134 content::TestBrowserThreadBundle thread_bundle_;
135 base::ScopedTempDir data_dir_;
136
137 scoped_refptr<storage::QuotaManager> quota_manager_;
138 scoped_refptr<content::MockSpecialStoragePolicy> mock_special_storage_policy_;
139 base::Callback<SiteEngagementScoreProvider*(content::BrowserContext*)>
140 score_provider_callback_;
141
142 GURL eviction_origin_;
143
144 base::WeakPtrFactory<SiteEngagementEvictionPolicyWithQuotaManagerTest>
145 weak_factory_;
146
147 DISALLOW_COPY_AND_ASSIGN(SiteEngagementEvictionPolicyWithQuotaManagerTest);
148 };
149
150 // Ensure the SiteEngagementEvictionPolicy completes successfully
151 // when used as a QuotaManager's eviction policy.
152 TEST_F(SiteEngagementEvictionPolicyWithQuotaManagerTest, GetEvictionOrigin) {
153 GetEvictionOrigin();
154 base::RunLoop().RunUntilIdle();
155
156 EXPECT_EQ(GURL(kFooOrigin), eviction_origin());
157 }
158
43 class SiteEngagementEvictionPolicyTest : public testing::Test { 159 class SiteEngagementEvictionPolicyTest : public testing::Test {
44 public: 160 public:
45 SiteEngagementEvictionPolicyTest() 161 SiteEngagementEvictionPolicyTest()
46 : score_provider_(new TestSiteEngagementScoreProvider()), 162 : score_provider_(new TestSiteEngagementScoreProvider()),
47 storage_policy_(new content::MockSpecialStoragePolicy()) {} 163 storage_policy_(new content::MockSpecialStoragePolicy()) {}
48 164
49 ~SiteEngagementEvictionPolicyTest() override {} 165 ~SiteEngagementEvictionPolicyTest() override {}
50 166
51 GURL CalculateEvictionOrigin(const std::map<GURL, int64>& usage) { 167 GURL CalculateEvictionOrigin(const std::map<GURL, int64>& usage) {
52 return SiteEngagementEvictionPolicy::CalculateEvictionOrigin( 168 return SiteEngagementEvictionPolicy::CalculateEvictionOriginForTests(
53 storage_policy_, score_provider_.get(), usage, kGlobalQuota); 169 storage_policy_, score_provider_.get(), usage, kGlobalQuota);
54 } 170 }
55 171
56 TestSiteEngagementScoreProvider* score_provider() { 172 TestSiteEngagementScoreProvider* score_provider() {
57 return score_provider_.get(); 173 return score_provider_.get();
58 } 174 }
59 175
60 content::MockSpecialStoragePolicy* storage_policy() { 176 content::MockSpecialStoragePolicy* storage_policy() {
61 return storage_policy_.get(); 177 return storage_policy_.get();
62 } 178 }
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 EXPECT_EQ(url2, CalculateEvictionOrigin(usage)); 237 EXPECT_EQ(url2, CalculateEvictionOrigin(usage));
122 238
123 // Durable storage doesn't get evicted. 239 // Durable storage doesn't get evicted.
124 storage_policy()->AddDurable(url2); 240 storage_policy()->AddDurable(url2);
125 EXPECT_EQ(url1, CalculateEvictionOrigin(usage)); 241 EXPECT_EQ(url1, CalculateEvictionOrigin(usage));
126 242
127 // Unlimited storage doesn't get evicted. 243 // Unlimited storage doesn't get evicted.
128 storage_policy()->AddUnlimited(url1); 244 storage_policy()->AddUnlimited(url1);
129 EXPECT_EQ(GURL(), CalculateEvictionOrigin(usage)); 245 EXPECT_EQ(GURL(), CalculateEvictionOrigin(usage));
130 } 246 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698