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 "webkit/quota/mock_storage_client.h" | |
| 14 #include "webkit/quota/quota_manager.h" | |
| 15 #include "webkit/quota/quota_temporary_storage_evictor.h" | |
| 16 | |
| 17 namespace quota { | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 class MockQuotaEvictionHandler : public quota::QuotaEvictionHandler { | |
| 22 public: | |
| 23 MockQuotaEvictionHandler() | |
| 24 : quota_(0), | |
| 25 available_space_(0) {} | |
| 26 virtual ~MockQuotaEvictionHandler() {} | |
| 27 | |
| 28 virtual void EvictOriginData( | |
| 29 const GURL& origin, | |
| 30 StorageType type, | |
| 31 EvictOriginDataCallback* callback) OVERRIDE { | |
| 32 LOG(ERROR) << "MockQM::EvictOriginData"; | |
| 33 remove_origin(origin); | |
| 34 callback->Run(quota::kQuotaStatusOk); | |
| 35 delete callback; | |
| 36 } | |
| 37 | |
| 38 virtual void GetUsageAndQuotaForEviction( | |
| 39 GetUsageAndQuotaForEvictionCallback* callback) OVERRIDE { | |
| 40 LOG(ERROR) << "MockQM::GetUsageAndQuotaForEviction"; | |
| 41 callback->Run(quota::kQuotaStatusOk, usage(), quota_, available_space_); | |
| 42 delete callback; | |
| 43 } | |
| 44 | |
| 45 virtual void GetLRUOrigin( | |
| 46 StorageType type, | |
| 47 GetLRUOriginCallback* callback) OVERRIDE { | |
| 48 if (origin_order_.empty()) | |
| 49 callback->Run(GURL()); | |
| 50 else | |
| 51 callback->Run(origin_order_.back()); | |
| 52 delete callback; | |
| 53 } | |
| 54 | |
| 55 int64 usage() { | |
|
kinuko
2011/05/20 06:41:14
For this I may expect the function name to be GetU
Dai Mikurube (NOT FULLTIME)
2011/05/20 07:36:54
Done.
| |
| 56 int64 total_usage = 0; | |
| 57 for (std::map<GURL, int64>::iterator p = origins_.begin(); | |
| 58 p != origins_.end(); | |
| 59 ++p) | |
| 60 total_usage += p->second; | |
| 61 return total_usage; | |
| 62 } | |
| 63 void set_quota(int64 quota) { | |
| 64 quota_ = quota; | |
| 65 } | |
| 66 void set_available_space(int64 available_space) { | |
| 67 available_space_ = available_space; | |
| 68 } | |
| 69 void access_origin(const GURL& origin) { | |
|
kinuko
2011/05/20 06:41:14
AccessOrigin ? (Since it's not a simple getter/se
Dai Mikurube (NOT FULLTIME)
2011/05/20 07:36:54
Done.
| |
| 70 if (remove_origin(origin)) | |
| 71 origin_order_.push_back(origin); | |
| 72 } | |
| 73 void add_pseudo_origin(const GURL& origin, int64 usage) { | |
|
kinuko
2011/05/20 06:41:14
ditto.
Dai Mikurube (NOT FULLTIME)
2011/05/20 07:36:54
Done.
| |
| 74 remove_origin(origin); | |
| 75 origin_order_.push_back(origin); | |
| 76 origins_[origin] = usage; | |
| 77 } | |
| 78 | |
| 79 private: | |
| 80 bool remove_origin(const GURL& origin) { | |
|
kinuko
2011/05/20 06:41:14
ditto.
Dai Mikurube (NOT FULLTIME)
2011/05/20 07:36:54
Done.
| |
| 81 origins_.erase(origin); | |
| 82 for (std::list<GURL>::iterator p = origin_order_.begin(); | |
| 83 p != origin_order_.end(); | |
| 84 ++p) { | |
| 85 if (*p == origin) { | |
| 86 origin_order_.erase(p); | |
| 87 return true; | |
| 88 } | |
| 89 } | |
| 90 return false; | |
| 91 } | |
| 92 | |
| 93 int64 quota_; | |
| 94 int64 available_space_; | |
| 95 std::list<GURL> origin_order_; | |
| 96 std::map<GURL, int64> origins_; | |
| 97 }; | |
| 98 | |
| 99 } // anonymous namespace | |
| 100 | |
| 101 class QuotaTemporaryStorageEvictorTest : public testing::Test { | |
| 102 public: | |
| 103 QuotaTemporaryStorageEvictorTest() {} | |
| 104 | |
| 105 void SetUp() { | |
| 106 quota_eviction_handler_.reset(new MockQuotaEvictionHandler()); | |
| 107 | |
| 108 temporary_storage_evictor_.reset(new QuotaTemporaryStorageEvictor( | |
| 109 quota_eviction_handler_.get(), | |
| 110 3000, base::MessageLoopProxy::CreateForCurrentThread())); | |
| 111 | |
| 112 additional_callback_count_ = 0; | |
| 113 } | |
| 114 | |
| 115 void TearDown() { | |
| 116 temporary_storage_evictor_.reset(); | |
| 117 quota_eviction_handler_.reset(); | |
| 118 MessageLoop::current()->RunAllPending(); | |
| 119 } | |
| 120 | |
| 121 protected: | |
| 122 MockQuotaEvictionHandler* quota_eviction_handler() const { | |
| 123 return static_cast<MockQuotaEvictionHandler*>( | |
| 124 quota_eviction_handler_.get()); | |
| 125 } | |
| 126 | |
| 127 QuotaTemporaryStorageEvictor* temporary_storage_evictor() const { | |
| 128 return temporary_storage_evictor_.get(); | |
| 129 } | |
| 130 | |
| 131 protected: | |
| 132 scoped_ptr<QuotaEvictionHandler> quota_eviction_handler_; | |
| 133 scoped_ptr<QuotaTemporaryStorageEvictor> temporary_storage_evictor_; | |
| 134 | |
| 135 QuotaStatusCode quota_status_; | |
| 136 std::string host_; | |
| 137 | |
| 138 int additional_callback_count_; | |
| 139 | |
| 140 DISALLOW_COPY_AND_ASSIGN(QuotaTemporaryStorageEvictorTest); | |
| 141 }; | |
| 142 | |
| 143 TEST_F(QuotaTemporaryStorageEvictorTest, Simple) { | |
| 144 quota_eviction_handler()->add_pseudo_origin(GURL("http://www.foo.com"), 500); | |
| 145 quota_eviction_handler()->add_pseudo_origin(GURL("http://www.bar.com"), 200); | |
| 146 quota_eviction_handler()->add_pseudo_origin(GURL("http://www.baz.com"), 3000); | |
| 147 quota_eviction_handler()->add_pseudo_origin(GURL("http://www.hoge.com"), 10); | |
| 148 quota_eviction_handler()->set_quota(4000); | |
| 149 quota_eviction_handler()->set_available_space(1000000000); | |
| 150 LOG(ERROR) << quota_eviction_handler()->usage(); | |
|
kinuko
2011/05/20 06:41:14
These LOG(ERROR) will eventually be replaced by so
Dai Mikurube (NOT FULLTIME)
2011/05/20 07:36:54
Of course, they're just for local execution. Just
| |
| 151 temporary_storage_evictor()->Start(); | |
| 152 MessageLoop::current()->RunAllPending(); | |
| 153 LOG(ERROR) << quota_eviction_handler()->usage(); | |
| 154 } | |
| 155 | |
| 156 TEST_F(QuotaTemporaryStorageEvictorTest, Repeated) { | |
| 157 quota_eviction_handler()->add_pseudo_origin(GURL("http://www.foo.com"), 500); | |
| 158 quota_eviction_handler()->add_pseudo_origin(GURL("http://www.bar.com"), 200); | |
| 159 quota_eviction_handler()->add_pseudo_origin(GURL("http://www.baz.com"), 3000); | |
| 160 quota_eviction_handler()->add_pseudo_origin(GURL("http://www.hoge.com"), 10); | |
| 161 quota_eviction_handler()->set_quota(4000); | |
| 162 quota_eviction_handler()->set_available_space(1000000000); | |
| 163 LOG(ERROR) << quota_eviction_handler()->usage(); | |
| 164 temporary_storage_evictor()->set_repeated_eviction(true); | |
| 165 temporary_storage_evictor()->Start(); | |
| 166 MessageLoop::current()->RunAllPending(); | |
| 167 LOG(ERROR) << quota_eviction_handler()->usage(); | |
| 168 } | |
| 169 | |
|
kinuko
2011/05/20 06:41:14
Maybe later we could add the same set of tests usi
Dai Mikurube (NOT FULLTIME)
2011/05/20 07:36:54
I guess such test cases should be run in another t
kinuko
2011/05/20 08:27:02
Yes of course we could add them as a new test (nei
| |
| 170 } // namespace quota | |
| OLD | NEW |