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 | |
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 | |
29 virtual void EvictOriginData( | |
30 const GURL& origin, | |
31 StorageType type, | |
32 EvictOriginDataCallback* callback) OVERRIDE { | |
33 int64 origin_usage = EnsureOriginRemoved(origin); | |
34 if (origin_usage >= 0) | |
35 available_space_ += origin_usage; | |
36 callback->Run(quota::kQuotaStatusOk); | |
37 delete callback; | |
38 } | |
39 | |
40 virtual void GetUsageAndQuotaForEviction( | |
41 GetUsageAndQuotaForEvictionCallback* callback) OVERRIDE { | |
42 if (task_for_get_usage_and_quota_.get()) | |
43 task_for_get_usage_and_quota_->Run(); | |
44 callback->Run(quota::kQuotaStatusOk, GetUsage(), quota_, available_space_); | |
45 delete callback; | |
46 } | |
47 | |
48 virtual void GetLRUOrigin( | |
49 StorageType type, | |
50 GetLRUOriginCallback* callback) OVERRIDE { | |
51 if (origin_order_.empty()) | |
52 callback->Run(GURL()); | |
53 else | |
54 callback->Run(origin_order_.front()); | |
55 delete callback; | |
56 } | |
57 | |
58 int64 GetUsage() const { | |
59 int64 total_usage = 0; | |
60 for (std::map<GURL, int64>::const_iterator p = origins_.begin(); | |
61 p != origins_.end(); | |
62 ++p) | |
63 total_usage += p->second; | |
64 return total_usage; | |
65 } | |
66 | |
67 void set_quota(int64 quota) { | |
68 quota_ = quota; | |
69 } | |
70 void set_available_space(int64 available_space) { | |
71 available_space_ = available_space; | |
72 } | |
73 void set_task_for_get_usage_and_quota(CancelableTask* task) { | |
74 task_for_get_usage_and_quota_.reset(task); | |
75 } | |
76 | |
77 // Simulates an access to |origin|. It reorders the internal LRU list. | |
78 // It internally uses AddOrigin(). | |
79 void AccessOrigin(const GURL& origin) { | |
80 std::map<GURL, int64>::iterator found = origins_.find(origin); | |
81 EXPECT_NE(origins_.end(), found); | |
82 AddOrigin(origin, found->second); | |
83 } | |
84 | |
85 // Simulates adding or overwriting the |origin| to the internal origin set | |
86 // with the |usage|. It also adds or moves the |origin| to the end of the | |
87 // LRU list. | |
88 void AddOrigin(const GURL& origin, int64 usage) { | |
89 EnsureOriginRemoved(origin); | |
90 origin_order_.push_back(origin); | |
91 origins_[origin] = usage; | |
92 } | |
93 | |
94 private: | |
95 int64 EnsureOriginRemoved(const GURL& origin) { | |
96 int64 origin_usage; | |
97 if (origins_.find(origin) == origins_.end()) | |
98 return -1; | |
99 else | |
100 origin_usage = origins_[origin]; | |
101 | |
102 origins_.erase(origin); | |
103 origin_order_.remove(origin); | |
104 return origin_usage; | |
105 } | |
106 | |
107 int64 quota_; | |
108 int64 available_space_; | |
109 std::list<GURL> origin_order_; | |
110 std::map<GURL, int64> origins_; | |
111 | |
112 QuotaTemporaryStorageEvictorTest *test_; | |
113 scoped_ptr<CancelableTask> task_for_get_usage_and_quota_; | |
114 }; | |
115 | |
116 } // anonymous namespace | |
117 | |
118 class QuotaTemporaryStorageEvictorTest : public testing::Test { | |
119 public: | |
120 QuotaTemporaryStorageEvictorTest() | |
121 : num_get_usage_and_quota_for_eviction_(0), | |
122 runnable_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {} | |
123 | |
124 void SetUp() { | |
125 quota_eviction_handler_.reset(new MockQuotaEvictionHandler(this)); | |
126 | |
127 // Run repeatedly in the single RunAllPending() when interval_ms == 0 | |
kinuko
2011/05/24 09:20:53
nit: in the single -> in a single
nit: Run repeate
Dai Mikurube (NOT FULLTIME)
2011/05/24 11:30:52
Done.
| |
128 temporary_storage_evictor_.reset(new QuotaTemporaryStorageEvictor( | |
129 quota_eviction_handler_.get(), | |
130 0, base::MessageLoopProxy::CreateForCurrentThread())); | |
131 } | |
132 | |
133 void TearDown() { | |
134 temporary_storage_evictor_.reset(); | |
135 quota_eviction_handler_.reset(); | |
136 MessageLoop::current()->RunAllPending(); | |
137 } | |
138 | |
139 void TaskForRepeatTestAfterGetUsageAndQuota( | |
140 int e_size, | |
kinuko
2011/05/24 09:20:53
Thanks for parameterizing the expected values.
Th
Dai Mikurube (NOT FULLTIME)
2011/05/24 11:30:52
Make sense. Done 2.
| |
141 int expected_usage_after_first, | |
142 int expected_usage_after_second) { | |
143 switch (num_get_usage_and_quota_for_eviction_) { | |
144 case 1: | |
145 EXPECT_EQ(expected_usage_after_first, | |
146 quota_eviction_handler()->GetUsage()); | |
147 quota_eviction_handler()->AddOrigin(GURL("http://www.e.com"), e_size); | |
148 quota_eviction_handler()->AccessOrigin(GURL("http://www.c.com")); | |
kinuko
2011/05/24 09:20:53
Here I'd wonder why we need to mix 'eviction-with-
Dai Mikurube (NOT FULLTIME)
2011/05/24 11:30:52
Ah, I understand.
1) I've tried separating them.
kinuko
2011/05/24 12:46:48
I imagined a separate test doing AccessOrigin betw
| |
149 break; | |
150 case 2: | |
151 EXPECT_EQ(expected_usage_after_second, | |
152 quota_eviction_handler()->GetUsage()); | |
153 temporary_storage_evictor()->set_repeated_eviction(false); | |
154 break; | |
155 } | |
156 ++num_get_usage_and_quota_for_eviction_; | |
157 } | |
158 | |
159 protected: | |
160 MockQuotaEvictionHandler* quota_eviction_handler() const { | |
161 return static_cast<MockQuotaEvictionHandler*>( | |
162 quota_eviction_handler_.get()); | |
163 } | |
164 | |
165 QuotaTemporaryStorageEvictor* temporary_storage_evictor() const { | |
166 return temporary_storage_evictor_.get(); | |
167 } | |
168 | |
169 int num_get_usage_and_quota_for_eviction() const { | |
170 return num_get_usage_and_quota_for_eviction_; | |
171 } | |
172 | |
173 int64 default_min_available_disk_space_to_start_eviction() const { | |
174 return QuotaTemporaryStorageEvictor:: | |
175 kDefaultMinAvailableDiskSpaceToStartEviction; | |
176 } | |
177 | |
178 scoped_ptr<QuotaEvictionHandler> quota_eviction_handler_; | |
179 scoped_ptr<QuotaTemporaryStorageEvictor> temporary_storage_evictor_; | |
180 | |
181 int num_get_usage_and_quota_for_eviction_; | |
182 | |
183 ScopedRunnableMethodFactory<QuotaTemporaryStorageEvictorTest> | |
184 runnable_factory_; | |
185 | |
186 DISALLOW_COPY_AND_ASSIGN(QuotaTemporaryStorageEvictorTest); | |
187 }; | |
188 | |
189 TEST_F(QuotaTemporaryStorageEvictorTest, SimpleEvictionTest) { | |
190 quota_eviction_handler()->AddOrigin(GURL("http://www.z.com"), 3000); | |
191 quota_eviction_handler()->AddOrigin(GURL("http://www.y.com"), 200); | |
192 quota_eviction_handler()->AddOrigin(GURL("http://www.x.com"), 500); | |
193 quota_eviction_handler()->set_quota(4000); | |
194 quota_eviction_handler()->set_available_space(1000000000); | |
195 EXPECT_EQ(3700, quota_eviction_handler()->GetUsage()); | |
196 temporary_storage_evictor()->Start(); | |
197 MessageLoop::current()->RunAllPending(); | |
198 EXPECT_EQ(700, quota_eviction_handler()->GetUsage()); | |
199 } | |
200 | |
201 TEST_F(QuotaTemporaryStorageEvictorTest, MultipleEvictionTest) { | |
202 quota_eviction_handler()->AddOrigin(GURL("http://www.z.com"), 20); | |
203 quota_eviction_handler()->AddOrigin(GURL("http://www.y.com"), 2900); | |
204 quota_eviction_handler()->AddOrigin(GURL("http://www.x.com"), 450); | |
205 quota_eviction_handler()->AddOrigin(GURL("http://www.w.com"), 400); | |
206 quota_eviction_handler()->set_quota(4000); | |
207 quota_eviction_handler()->set_available_space(1000000000); | |
208 EXPECT_EQ(3770, quota_eviction_handler()->GetUsage()); | |
209 temporary_storage_evictor()->Start(); | |
210 MessageLoop::current()->RunAllPending(); | |
211 EXPECT_EQ(850, quota_eviction_handler()->GetUsage()); | |
212 } | |
213 | |
214 TEST_F(QuotaTemporaryStorageEvictorTest, RepeatedEvictionTest) { | |
215 const int64 a_size = 400; | |
216 const int64 b_size = 150; | |
217 const int64 c_size = 120; | |
218 const int64 d_size = 292; | |
219 const int64 initial_total_size = a_size + b_size + c_size + d_size; | |
220 const int64 e_size = 275; | |
221 | |
222 quota_eviction_handler()->AddOrigin(GURL("http://www.d.com"), d_size); | |
223 quota_eviction_handler()->AddOrigin(GURL("http://www.c.com"), c_size); | |
224 quota_eviction_handler()->AddOrigin(GURL("http://www.b.com"), b_size); | |
225 quota_eviction_handler()->AddOrigin(GURL("http://www.a.com"), a_size); | |
226 quota_eviction_handler()->set_quota(1000); | |
227 quota_eviction_handler()->set_available_space(1000000000); | |
228 quota_eviction_handler()->set_task_for_get_usage_and_quota( | |
229 runnable_factory_.NewRunnableMethod(&QuotaTemporaryStorageEvictorTest:: | |
230 TaskForRepeatTestAfterGetUsageAndQuota, | |
231 e_size, | |
232 initial_total_size - d_size, | |
233 initial_total_size - d_size + e_size - b_size)); | |
234 EXPECT_EQ(initial_total_size, quota_eviction_handler()->GetUsage()); | |
235 temporary_storage_evictor()->set_repeated_eviction(true); | |
236 temporary_storage_evictor()->Start(); | |
237 MessageLoop::current()->RunAllPending(); | |
238 EXPECT_EQ(initial_total_size - d_size + e_size - b_size - a_size, | |
239 quota_eviction_handler()->GetUsage()); | |
240 EXPECT_EQ(4, num_get_usage_and_quota_for_eviction()); | |
241 } | |
242 | |
243 TEST_F(QuotaTemporaryStorageEvictorTest, DiskSpaceEvictionTest) { | |
244 quota_eviction_handler()->AddOrigin(GURL("http://www.z.com"), 294); | |
245 quota_eviction_handler()->AddOrigin(GURL("http://www.y.com"), 120); | |
246 quota_eviction_handler()->AddOrigin(GURL("http://www.x.com"), 150); | |
247 quota_eviction_handler()->AddOrigin(GURL("http://www.w.com"), 300); | |
248 quota_eviction_handler()->set_quota(10000); | |
249 quota_eviction_handler()->set_available_space( | |
250 default_min_available_disk_space_to_start_eviction() - 350); | |
251 EXPECT_EQ(864, quota_eviction_handler()->GetUsage()); | |
252 temporary_storage_evictor()->Start(); | |
253 MessageLoop::current()->RunAllPending(); | |
254 EXPECT_EQ(450, quota_eviction_handler()->GetUsage()); | |
255 } | |
256 | |
257 } // namespace quota | |
OLD | NEW |