OLD | NEW |
---|---|
(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 #include <utility> | |
kinuko
2011/05/24 12:46:48
No need to include this as long as you have <map>.
Dai Mikurube (NOT FULLTIME)
2011/05/25 05:26:31
Done.
| |
10 | |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "base/message_loop.h" | |
13 #include "base/task.h" | |
14 #include "webkit/quota/mock_storage_client.h" | |
15 #include "webkit/quota/quota_temporary_storage_evictor.h" | |
16 | |
17 namespace quota { | |
18 | |
19 class QuotaTemporaryStorageEvictorTest; | |
20 | |
21 namespace { | |
22 | |
23 class MockQuotaEvictionHandler : public quota::QuotaEvictionHandler { | |
24 public: | |
25 MockQuotaEvictionHandler(QuotaTemporaryStorageEvictorTest *test) | |
26 : quota_(0), | |
27 available_space_(0), | |
28 test_(test) {} | |
29 | |
30 virtual void EvictOriginData( | |
31 const GURL& origin, | |
32 StorageType type, | |
33 EvictOriginDataCallback* callback) OVERRIDE { | |
34 int64 origin_usage = EnsureOriginRemoved(origin); | |
35 if (origin_usage >= 0) | |
36 available_space_ += origin_usage; | |
37 callback->Run(quota::kQuotaStatusOk); | |
38 delete callback; | |
39 } | |
40 | |
41 virtual void GetUsageAndQuotaForEviction( | |
42 GetUsageAndQuotaForEvictionCallback* callback) OVERRIDE { | |
43 if (task_for_get_usage_and_quota_.get()) | |
44 task_for_get_usage_and_quota_->Run(); | |
45 callback->Run(quota::kQuotaStatusOk, GetUsage(), quota_, available_space_); | |
46 delete callback; | |
47 } | |
48 | |
49 virtual void GetLRUOrigin( | |
50 StorageType type, | |
51 GetLRUOriginCallback* callback) OVERRIDE { | |
52 if (origin_order_.empty()) | |
53 callback->Run(GURL()); | |
54 else | |
55 callback->Run(origin_order_.front()); | |
56 delete callback; | |
57 } | |
58 | |
59 int64 GetUsage() const { | |
60 int64 total_usage = 0; | |
61 for (std::map<GURL, int64>::const_iterator p = origins_.begin(); | |
62 p != origins_.end(); | |
63 ++p) | |
64 total_usage += p->second; | |
65 return total_usage; | |
66 } | |
67 | |
68 void set_quota(int64 quota) { | |
69 quota_ = quota; | |
70 } | |
71 void set_available_space(int64 available_space) { | |
72 available_space_ = available_space; | |
73 } | |
74 void set_task_for_get_usage_and_quota(CancelableTask* task) { | |
75 task_for_get_usage_and_quota_.reset(task); | |
76 } | |
77 | |
78 // Simulates an access to |origin|. It reorders the internal LRU list. | |
79 // It internally uses AddOrigin(). | |
80 void AccessOrigin(const GURL& origin) { | |
81 std::map<GURL, int64>::iterator found = origins_.find(origin); | |
82 EXPECT_NE(origins_.end(), found); | |
83 AddOrigin(origin, found->second); | |
84 } | |
85 | |
86 // Simulates adding or overwriting the |origin| to the internal origin set | |
87 // with the |usage|. It also adds or moves the |origin| to the end of the | |
88 // LRU list. | |
89 void AddOrigin(const GURL& origin, int64 usage) { | |
90 EnsureOriginRemoved(origin); | |
91 origin_order_.push_back(origin); | |
92 origins_[origin] = usage; | |
93 } | |
94 | |
95 private: | |
96 int64 EnsureOriginRemoved(const GURL& origin) { | |
97 int64 origin_usage; | |
98 if (origins_.find(origin) == origins_.end()) | |
99 return -1; | |
100 else | |
101 origin_usage = origins_[origin]; | |
102 | |
103 origins_.erase(origin); | |
104 origin_order_.remove(origin); | |
105 return origin_usage; | |
106 } | |
107 | |
108 int64 quota_; | |
109 int64 available_space_; | |
110 std::list<GURL> origin_order_; | |
111 std::map<GURL, int64> origins_; | |
112 | |
113 QuotaTemporaryStorageEvictorTest *test_; | |
114 scoped_ptr<CancelableTask> task_for_get_usage_and_quota_; | |
115 }; | |
116 | |
117 } // anonymous namespace | |
118 | |
119 class QuotaTemporaryStorageEvictorTest : public testing::Test { | |
120 public: | |
121 QuotaTemporaryStorageEvictorTest() | |
122 : num_get_usage_and_quota_for_eviction_(0), | |
123 runnable_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {} | |
124 | |
125 void SetUp() { | |
126 quota_eviction_handler_.reset(new MockQuotaEvictionHandler(this)); | |
127 | |
128 // Run multiple evictions in a single RunAllPending() when interval_ms == 0 | |
129 temporary_storage_evictor_.reset(new QuotaTemporaryStorageEvictor( | |
130 quota_eviction_handler_.get(), | |
131 0, base::MessageLoopProxy::CreateForCurrentThread())); | |
132 } | |
133 | |
134 void TearDown() { | |
135 temporary_storage_evictor_.reset(); | |
136 quota_eviction_handler_.reset(); | |
137 MessageLoop::current()->RunAllPending(); | |
138 } | |
139 | |
140 void TaskForRepeatedEvictionTest( | |
141 const std::pair<GURL, int64>& origin_to_be_added, | |
142 int expected_usage_after_first, | |
143 int expected_usage_after_second) { | |
144 switch (num_get_usage_and_quota_for_eviction_) { | |
145 case 1: | |
146 EXPECT_EQ(expected_usage_after_first, | |
147 quota_eviction_handler()->GetUsage()); | |
148 quota_eviction_handler()->AddOrigin(origin_to_be_added.first, | |
149 origin_to_be_added.second); | |
150 break; | |
151 case 2: | |
152 EXPECT_EQ(expected_usage_after_second, | |
153 quota_eviction_handler()->GetUsage()); | |
154 temporary_storage_evictor()->set_repeated_eviction(false); | |
155 break; | |
156 } | |
157 ++num_get_usage_and_quota_for_eviction_; | |
158 } | |
159 | |
160 void TaskForRepeatedEvictionWithAccessOriginTest( | |
kinuko
2011/05/24 12:46:48
Probably this method can be merged into the one ab
Dai Mikurube (NOT FULLTIME)
2011/05/25 05:26:31
Done.
| |
161 const std::pair<GURL, int64>& origin_to_be_added, | |
162 const GURL& origin_to_be_accessed, | |
163 int expected_usage_after_first, | |
164 int expected_usage_after_second) { | |
165 switch (num_get_usage_and_quota_for_eviction_) { | |
166 case 1: | |
167 EXPECT_EQ(expected_usage_after_first, | |
168 quota_eviction_handler()->GetUsage()); | |
169 quota_eviction_handler()->AddOrigin(origin_to_be_added.first, | |
170 origin_to_be_added.second); | |
171 quota_eviction_handler()->AccessOrigin(origin_to_be_accessed); | |
172 break; | |
173 case 2: | |
174 EXPECT_EQ(expected_usage_after_second, | |
175 quota_eviction_handler()->GetUsage()); | |
176 temporary_storage_evictor()->set_repeated_eviction(false); | |
177 break; | |
178 } | |
179 ++num_get_usage_and_quota_for_eviction_; | |
180 } | |
181 | |
182 protected: | |
183 MockQuotaEvictionHandler* quota_eviction_handler() const { | |
184 return static_cast<MockQuotaEvictionHandler*>( | |
185 quota_eviction_handler_.get()); | |
186 } | |
187 | |
188 QuotaTemporaryStorageEvictor* temporary_storage_evictor() const { | |
189 return temporary_storage_evictor_.get(); | |
190 } | |
191 | |
192 int num_get_usage_and_quota_for_eviction() const { | |
193 return num_get_usage_and_quota_for_eviction_; | |
194 } | |
195 | |
196 int64 default_min_available_disk_space_to_start_eviction() const { | |
197 return QuotaTemporaryStorageEvictor:: | |
198 kDefaultMinAvailableDiskSpaceToStartEviction; | |
199 } | |
200 | |
201 scoped_ptr<QuotaEvictionHandler> quota_eviction_handler_; | |
202 scoped_ptr<QuotaTemporaryStorageEvictor> temporary_storage_evictor_; | |
203 | |
204 int num_get_usage_and_quota_for_eviction_; | |
205 | |
206 ScopedRunnableMethodFactory<QuotaTemporaryStorageEvictorTest> | |
207 runnable_factory_; | |
208 | |
209 DISALLOW_COPY_AND_ASSIGN(QuotaTemporaryStorageEvictorTest); | |
210 }; | |
211 | |
212 TEST_F(QuotaTemporaryStorageEvictorTest, SimpleEvictionTest) { | |
213 quota_eviction_handler()->AddOrigin(GURL("http://www.z.com"), 3000); | |
214 quota_eviction_handler()->AddOrigin(GURL("http://www.y.com"), 200); | |
215 quota_eviction_handler()->AddOrigin(GURL("http://www.x.com"), 500); | |
216 quota_eviction_handler()->set_quota(4000); | |
217 quota_eviction_handler()->set_available_space(1000000000); | |
218 EXPECT_EQ(3700, quota_eviction_handler()->GetUsage()); | |
219 temporary_storage_evictor()->Start(); | |
220 MessageLoop::current()->RunAllPending(); | |
221 EXPECT_EQ(700, quota_eviction_handler()->GetUsage()); | |
222 } | |
223 | |
224 TEST_F(QuotaTemporaryStorageEvictorTest, MultipleEvictionTest) { | |
225 quota_eviction_handler()->AddOrigin(GURL("http://www.z.com"), 20); | |
226 quota_eviction_handler()->AddOrigin(GURL("http://www.y.com"), 2900); | |
227 quota_eviction_handler()->AddOrigin(GURL("http://www.x.com"), 450); | |
228 quota_eviction_handler()->AddOrigin(GURL("http://www.w.com"), 400); | |
229 quota_eviction_handler()->set_quota(4000); | |
230 quota_eviction_handler()->set_available_space(1000000000); | |
231 EXPECT_EQ(3770, quota_eviction_handler()->GetUsage()); | |
232 temporary_storage_evictor()->Start(); | |
233 MessageLoop::current()->RunAllPending(); | |
234 EXPECT_EQ(850, quota_eviction_handler()->GetUsage()); | |
235 } | |
236 | |
237 TEST_F(QuotaTemporaryStorageEvictorTest, RepeatedEvictionTest) { | |
238 const int64 a_size = 400; | |
239 const int64 b_size = 150; | |
240 const int64 c_size = 120; | |
241 const int64 d_size = 292; | |
242 const int64 initial_total_size = a_size + b_size + c_size + d_size; | |
243 const int64 e_size = 275; | |
244 | |
245 quota_eviction_handler()->AddOrigin(GURL("http://www.d.com"), d_size); | |
246 quota_eviction_handler()->AddOrigin(GURL("http://www.c.com"), c_size); | |
247 quota_eviction_handler()->AddOrigin(GURL("http://www.b.com"), b_size); | |
248 quota_eviction_handler()->AddOrigin(GURL("http://www.a.com"), a_size); | |
249 quota_eviction_handler()->set_quota(1000); | |
250 quota_eviction_handler()->set_available_space(1000000000); | |
251 quota_eviction_handler()->set_task_for_get_usage_and_quota( | |
252 runnable_factory_.NewRunnableMethod( | |
253 &QuotaTemporaryStorageEvictorTest::TaskForRepeatedEvictionTest, | |
254 std::make_pair(GURL("http://www.e.com"), e_size), | |
255 initial_total_size - d_size, | |
256 initial_total_size - d_size + e_size - c_size)); | |
257 EXPECT_EQ(initial_total_size, quota_eviction_handler()->GetUsage()); | |
258 temporary_storage_evictor()->set_repeated_eviction(true); | |
259 temporary_storage_evictor()->Start(); | |
260 MessageLoop::current()->RunAllPending(); | |
261 EXPECT_EQ(initial_total_size - d_size + e_size - c_size - b_size, | |
262 quota_eviction_handler()->GetUsage()); | |
263 EXPECT_EQ(4, num_get_usage_and_quota_for_eviction()); | |
264 } | |
265 | |
266 TEST_F(QuotaTemporaryStorageEvictorTest, RepeatedEvictionWithAccessOriginTest) { | |
267 const int64 a_size = 400; | |
268 const int64 b_size = 150; | |
269 const int64 c_size = 120; | |
270 const int64 d_size = 292; | |
271 const int64 initial_total_size = a_size + b_size + c_size + d_size; | |
272 const int64 e_size = 275; | |
273 | |
274 quota_eviction_handler()->AddOrigin(GURL("http://www.d.com"), d_size); | |
275 quota_eviction_handler()->AddOrigin(GURL("http://www.c.com"), c_size); | |
276 quota_eviction_handler()->AddOrigin(GURL("http://www.b.com"), b_size); | |
277 quota_eviction_handler()->AddOrigin(GURL("http://www.a.com"), a_size); | |
278 quota_eviction_handler()->set_quota(1000); | |
279 quota_eviction_handler()->set_available_space(1000000000); | |
280 quota_eviction_handler()->set_task_for_get_usage_and_quota( | |
281 runnable_factory_.NewRunnableMethod( | |
282 &QuotaTemporaryStorageEvictorTest:: | |
283 TaskForRepeatedEvictionWithAccessOriginTest, | |
284 std::make_pair(GURL("http://www.e.com"), e_size), | |
285 GURL("http://www.c.com"), | |
286 initial_total_size - d_size, | |
287 initial_total_size - d_size + e_size - b_size)); | |
288 EXPECT_EQ(initial_total_size, quota_eviction_handler()->GetUsage()); | |
289 temporary_storage_evictor()->set_repeated_eviction(true); | |
290 temporary_storage_evictor()->Start(); | |
291 MessageLoop::current()->RunAllPending(); | |
292 EXPECT_EQ(initial_total_size - d_size + e_size - b_size - a_size, | |
293 quota_eviction_handler()->GetUsage()); | |
294 EXPECT_EQ(4, num_get_usage_and_quota_for_eviction()); | |
295 } | |
296 | |
297 TEST_F(QuotaTemporaryStorageEvictorTest, DiskSpaceEvictionTest) { | |
298 quota_eviction_handler()->AddOrigin(GURL("http://www.z.com"), 294); | |
299 quota_eviction_handler()->AddOrigin(GURL("http://www.y.com"), 120); | |
300 quota_eviction_handler()->AddOrigin(GURL("http://www.x.com"), 150); | |
301 quota_eviction_handler()->AddOrigin(GURL("http://www.w.com"), 300); | |
302 quota_eviction_handler()->set_quota(10000); | |
303 quota_eviction_handler()->set_available_space( | |
304 default_min_available_disk_space_to_start_eviction() - 350); | |
305 EXPECT_EQ(864, quota_eviction_handler()->GetUsage()); | |
306 temporary_storage_evictor()->Start(); | |
307 MessageLoop::current()->RunAllPending(); | |
308 EXPECT_EQ(450, quota_eviction_handler()->GetUsage()); | |
309 } | |
310 | |
311 } // namespace quota | |
OLD | NEW |