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