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

Side by Side Diff: webkit/quota/quota_temporary_storage_evictor_unittest.cc

Issue 7002024: Implement QuotaTemporaryStorageEvictor. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Rebased and nit fix. Created 9 years, 7 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "testing/gtest/include/gtest/gtest.h"
6
7 #include <list>
8 #include <map>
9
10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop.h"
12 #include "base/task.h"
13 #include "webkit/quota/mock_storage_client.h"
14 #include "webkit/quota/quota_temporary_storage_evictor.h"
15
16 namespace quota {
17
18 class QuotaTemporaryStorageEvictorTest;
19
20 namespace {
21
22 class MockQuotaEvictionHandler : public quota::QuotaEvictionHandler {
23 public:
24 MockQuotaEvictionHandler(QuotaTemporaryStorageEvictorTest *test)
25 : quota_(0),
26 available_space_(0),
27 test_(test),
28 task_for_get_usage_and_quota_(NULL) {}
29 virtual ~MockQuotaEvictionHandler() {
30 if (task_for_get_usage_and_quota_)
31 delete task_for_get_usage_and_quota_;
32 }
33
34 virtual void EvictOriginData(
35 const GURL& origin,
36 StorageType type,
37 EvictOriginDataCallback* callback) OVERRIDE {
38 RemoveOrigin(origin);
39 callback->Run(quota::kQuotaStatusOk);
40 delete callback;
41 }
42
43 virtual void GetUsageAndQuotaForEviction(
44 GetUsageAndQuotaForEvictionCallback* callback) OVERRIDE {
45 if (task_for_get_usage_and_quota_)
46 task_for_get_usage_and_quota_->Run();
kinuko 2011/05/23 04:34:53 don't we need to delete the task?
Dai Mikurube (NOT FULLTIME) 2011/05/23 05:53:37 Ah, the task should be deleted, but not here. The
47 callback->Run(quota::kQuotaStatusOk, GetUsage(), quota_, available_space_);
48 delete callback;
49 }
50
51 virtual void GetLRUOrigin(
52 StorageType type,
53 GetLRUOriginCallback* callback) OVERRIDE {
54 if (origin_order_.empty())
55 callback->Run(GURL());
56 else
57 callback->Run(origin_order_.front());
58 delete callback;
59 }
60
61 int64 GetUsage() const {
62 int64 total_usage = 0;
63 for (std::map<GURL, int64>::const_iterator p = origins_.begin();
64 p != origins_.end();
65 ++p)
66 total_usage += p->second;
67 return total_usage;
68 }
69
70 void set_quota(int64 quota) {
71 quota_ = quota;
72 }
73 void set_available_space(int64 available_space) {
74 available_space_ = available_space;
75 }
76 void set_task_for_get_usage_and_quota(CancelableTask* task) {
77 task_for_get_usage_and_quota_ = task;
78 }
79
80 void AccessOrigin(const GURL& origin) {
81 int64 origin_usage = RemoveOrigin(origin);
82 if (origin_usage >= 0)
83 AddPseudoOrigin(origin, origin_usage);
84 }
85
86 void AddPseudoOrigin(const GURL& origin, int64 usage) {
kinuko 2011/05/23 04:34:53 Does this method expect the caller to call RemoveO
Dai Mikurube (NOT FULLTIME) 2011/05/23 05:53:37 Your comment includes two messages... 1) rename th
kinuko 2011/05/23 07:29:51 Keeping 'AccessOrigin' sounds ok to me actually.
Dai Mikurube (NOT FULLTIME) 2011/05/23 09:50:37 Ok, finally, I added such comments at AccessOrigin
kinuko 2011/05/23 12:10:29 Apparently this was wrong, please nvm.
87 RemoveOrigin(origin);
88 origin_order_.push_back(origin);
89 origins_[origin] = usage;
90 }
91
92 private:
93 int64 RemoveOrigin(const GURL& origin) {
94 int64 origin_usage;
95 if (origins_.find(origin) == origins_.end())
kinuko 2011/05/23 04:34:53 EXPECT_TRUE?
Dai Mikurube (NOT FULLTIME) 2011/05/23 05:53:37 As replied above, I designed this method to be cal
96 return -1;
97 else
98 origin_usage = origins_[origin];
99
100 origins_.erase(origin);
101 origin_order_.remove(origin);
102 return origin_usage;
103 }
104
105 int64 quota_;
106 int64 available_space_;
107 std::list<GURL> origin_order_;
108 std::map<GURL, int64> origins_;
109
110 QuotaTemporaryStorageEvictorTest *test_;
111 CancelableTask* task_for_get_usage_and_quota_;
kinuko 2011/05/23 04:34:53 Can this task be scoped_ptr?
Dai Mikurube (NOT FULLTIME) 2011/05/23 05:53:37 Exactly. Done.
112 };
113
114 } // anonymous namespace
115
116 class QuotaTemporaryStorageEvictorTest : public testing::Test {
117 public:
118 QuotaTemporaryStorageEvictorTest()
119 : num_get_usage_and_quota_for_eviction_(0),
120 runnable_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {}
121
122 void SetUp() {
123 quota_eviction_handler_.reset(new MockQuotaEvictionHandler(this));
124
125 // Run repeatedly in the single RunAllPending() when interval_ms == 0
126 temporary_storage_evictor_.reset(new QuotaTemporaryStorageEvictor(
127 quota_eviction_handler_.get(),
128 0, base::MessageLoopProxy::CreateForCurrentThread()));
129 }
130
131 void TearDown() {
132 temporary_storage_evictor_.reset();
133 quota_eviction_handler_.reset();
134 MessageLoop::current()->RunAllPending();
135 }
136
137 void TaskForRepeatTestAfterGetUsageAndQuota() {
138 switch (num_get_usage_and_quota_for_eviction_) {
139 case 1:
140 EXPECT_EQ(962-292, quota_eviction_handler()->GetUsage());
kinuko 2011/05/23 04:34:53 Please separate out these test-dependent constants
Dai Mikurube (NOT FULLTIME) 2011/05/23 05:53:37 Done.
141 quota_eviction_handler()->AddPseudoOrigin(
142 GURL("http://www.hoge.com"), 280);
143 quota_eviction_handler()->AccessOrigin(GURL("http://www.foo.com"));
144 EXPECT_EQ(962-292+280, quota_eviction_handler()->GetUsage());
145 break;
146 case 2:
147 EXPECT_EQ(962-292+280-120, quota_eviction_handler()->GetUsage());
148 temporary_storage_evictor()->set_repeated_eviction(false);
149 break;
150 }
151 ++num_get_usage_and_quota_for_eviction_;
152 }
153
154 protected:
155 MockQuotaEvictionHandler* quota_eviction_handler() const {
156 return static_cast<MockQuotaEvictionHandler*>(
157 quota_eviction_handler_.get());
158 }
159
160 QuotaTemporaryStorageEvictor* temporary_storage_evictor() const {
161 return temporary_storage_evictor_.get();
162 }
163
164 scoped_ptr<QuotaEvictionHandler> quota_eviction_handler_;
165 scoped_ptr<QuotaTemporaryStorageEvictor> temporary_storage_evictor_;
166
167 int num_get_usage_and_quota_for_eviction_;
168
169 ScopedRunnableMethodFactory<QuotaTemporaryStorageEvictorTest>
170 runnable_factory_;
171
172 DISALLOW_COPY_AND_ASSIGN(QuotaTemporaryStorageEvictorTest);
173 };
174
175 TEST_F(QuotaTemporaryStorageEvictorTest, SimpleEvictionTest) {
176 quota_eviction_handler()->AddPseudoOrigin(GURL("http://www.baz.com"), 3000);
177 quota_eviction_handler()->AddPseudoOrigin(GURL("http://www.bar.com"), 200);
178 quota_eviction_handler()->AddPseudoOrigin(GURL("http://www.foo.com"), 500);
179 quota_eviction_handler()->set_quota(4000);
180 quota_eviction_handler()->set_available_space(1000000000);
181 EXPECT_EQ(3700, quota_eviction_handler()->GetUsage());
182 temporary_storage_evictor()->Start();
183 MessageLoop::current()->RunAllPending();
184 EXPECT_EQ(700, quota_eviction_handler()->GetUsage());
185 }
186
187 TEST_F(QuotaTemporaryStorageEvictorTest, MultipleEvictionTest) {
188 quota_eviction_handler()->AddPseudoOrigin(GURL("http://www.hoge.com"), 20);
189 quota_eviction_handler()->AddPseudoOrigin(GURL("http://www.baz.com"), 2900);
190 quota_eviction_handler()->AddPseudoOrigin(GURL("http://www.bar.com"), 450);
191 quota_eviction_handler()->AddPseudoOrigin(GURL("http://www.foo.com"), 400);
192 quota_eviction_handler()->set_quota(4000);
193 quota_eviction_handler()->set_available_space(1000000000);
194 EXPECT_EQ(3770, quota_eviction_handler()->GetUsage());
195 temporary_storage_evictor()->Start();
196 MessageLoop::current()->RunAllPending();
197 EXPECT_EQ(850, quota_eviction_handler()->GetUsage());
198 }
199
200 TEST_F(QuotaTemporaryStorageEvictorTest, RepeatedEvictionTest) {
201 quota_eviction_handler()->AddPseudoOrigin(GURL("http://www.ban.com"), 292);
202 quota_eviction_handler()->AddPseudoOrigin(GURL("http://www.baz.com"), 120);
203 quota_eviction_handler()->AddPseudoOrigin(GURL("http://www.bar.com"), 150);
204 quota_eviction_handler()->AddPseudoOrigin(GURL("http://www.foo.com"), 400);
205 quota_eviction_handler()->set_quota(1000);
206 quota_eviction_handler()->set_available_space(1000000000);
207 quota_eviction_handler()->set_task_for_get_usage_and_quota(
208 runnable_factory_.NewRunnableMethod(&QuotaTemporaryStorageEvictorTest::
209 TaskForRepeatTestAfterGetUsageAndQuota));
210 EXPECT_EQ(962, quota_eviction_handler()->GetUsage());
211 temporary_storage_evictor()->set_repeated_eviction(true);
212 temporary_storage_evictor()->Start();
213 MessageLoop::current()->RunAllPending();
214 EXPECT_EQ(962-292+280-120-150, quota_eviction_handler()->GetUsage());
kinuko 2011/05/23 04:34:53 EXPECT_EQ(2, num_get_usage_and_quota_for_eviction(
Dai Mikurube (NOT FULLTIME) 2011/05/23 05:53:37 Done, but GetUsageAndQuotaForEviction() is called
215 }
216
217 // TODO(dmikurube): Add tests for eviction by available disk space.
218
219 } // namespace quota
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698