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