Chromium Code Reviews| Index: webkit/quota/quota_temporary_storage_evictor_unittest.cc |
| diff --git a/webkit/quota/quota_temporary_storage_evictor_unittest.cc b/webkit/quota/quota_temporary_storage_evictor_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..673cc3884bb1473ce1c292cb08b0701530ff357d |
| --- /dev/null |
| +++ b/webkit/quota/quota_temporary_storage_evictor_unittest.cc |
| @@ -0,0 +1,170 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +#include <list> |
| +#include <map> |
| + |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/message_loop.h" |
| +#include "base/message_loop_proxy.h" |
| +#include "webkit/quota/mock_storage_client.h" |
| +#include "webkit/quota/quota_manager.h" |
| +#include "webkit/quota/quota_temporary_storage_evictor.h" |
| + |
| +namespace quota { |
| + |
| +namespace { |
| + |
| +class MockQuotaEvictionHandler : public quota::QuotaEvictionHandler { |
| + public: |
| + MockQuotaEvictionHandler() |
| + : quota_(0), |
| + available_space_(0) {} |
| + virtual ~MockQuotaEvictionHandler() {} |
| + |
| + virtual void EvictOriginData( |
| + const GURL& origin, |
| + StorageType type, |
| + EvictOriginDataCallback* callback) OVERRIDE { |
| + LOG(ERROR) << "MockQM::EvictOriginData"; |
| + remove_origin(origin); |
| + callback->Run(quota::kQuotaStatusOk); |
| + delete callback; |
| + } |
| + |
| + virtual void GetUsageAndQuotaForEviction( |
| + GetUsageAndQuotaForEvictionCallback* callback) OVERRIDE { |
| + LOG(ERROR) << "MockQM::GetUsageAndQuotaForEviction"; |
| + callback->Run(quota::kQuotaStatusOk, usage(), quota_, available_space_); |
| + delete callback; |
| + } |
| + |
| + virtual void GetLRUOrigin( |
| + StorageType type, |
| + GetLRUOriginCallback* callback) OVERRIDE { |
| + if (origin_order_.empty()) |
| + callback->Run(GURL()); |
| + else |
| + callback->Run(origin_order_.back()); |
| + delete callback; |
| + } |
| + |
| + 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.
|
| + int64 total_usage = 0; |
| + for (std::map<GURL, int64>::iterator p = origins_.begin(); |
| + p != origins_.end(); |
| + ++p) |
| + total_usage += p->second; |
| + return total_usage; |
| + } |
| + void set_quota(int64 quota) { |
| + quota_ = quota; |
| + } |
| + void set_available_space(int64 available_space) { |
| + available_space_ = available_space; |
| + } |
| + 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.
|
| + if (remove_origin(origin)) |
| + origin_order_.push_back(origin); |
| + } |
| + 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.
|
| + remove_origin(origin); |
| + origin_order_.push_back(origin); |
| + origins_[origin] = usage; |
| + } |
| + |
| + private: |
| + 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.
|
| + origins_.erase(origin); |
| + for (std::list<GURL>::iterator p = origin_order_.begin(); |
| + p != origin_order_.end(); |
| + ++p) { |
| + if (*p == origin) { |
| + origin_order_.erase(p); |
| + return true; |
| + } |
| + } |
| + return false; |
| + } |
| + |
| + int64 quota_; |
| + int64 available_space_; |
| + std::list<GURL> origin_order_; |
| + std::map<GURL, int64> origins_; |
| +}; |
| + |
| +} // anonymous namespace |
| + |
| +class QuotaTemporaryStorageEvictorTest : public testing::Test { |
| + public: |
| + QuotaTemporaryStorageEvictorTest() {} |
| + |
| + void SetUp() { |
| + quota_eviction_handler_.reset(new MockQuotaEvictionHandler()); |
| + |
| + temporary_storage_evictor_.reset(new QuotaTemporaryStorageEvictor( |
| + quota_eviction_handler_.get(), |
| + 3000, base::MessageLoopProxy::CreateForCurrentThread())); |
| + |
| + additional_callback_count_ = 0; |
| + } |
| + |
| + void TearDown() { |
| + temporary_storage_evictor_.reset(); |
| + quota_eviction_handler_.reset(); |
| + MessageLoop::current()->RunAllPending(); |
| + } |
| + |
| + protected: |
| + MockQuotaEvictionHandler* quota_eviction_handler() const { |
| + return static_cast<MockQuotaEvictionHandler*>( |
| + quota_eviction_handler_.get()); |
| + } |
| + |
| + QuotaTemporaryStorageEvictor* temporary_storage_evictor() const { |
| + return temporary_storage_evictor_.get(); |
| + } |
| + |
| + protected: |
| + scoped_ptr<QuotaEvictionHandler> quota_eviction_handler_; |
| + scoped_ptr<QuotaTemporaryStorageEvictor> temporary_storage_evictor_; |
| + |
| + QuotaStatusCode quota_status_; |
| + std::string host_; |
| + |
| + int additional_callback_count_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(QuotaTemporaryStorageEvictorTest); |
| +}; |
| + |
| +TEST_F(QuotaTemporaryStorageEvictorTest, Simple) { |
| + quota_eviction_handler()->add_pseudo_origin(GURL("http://www.foo.com"), 500); |
| + quota_eviction_handler()->add_pseudo_origin(GURL("http://www.bar.com"), 200); |
| + quota_eviction_handler()->add_pseudo_origin(GURL("http://www.baz.com"), 3000); |
| + quota_eviction_handler()->add_pseudo_origin(GURL("http://www.hoge.com"), 10); |
| + quota_eviction_handler()->set_quota(4000); |
| + quota_eviction_handler()->set_available_space(1000000000); |
| + 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
|
| + temporary_storage_evictor()->Start(); |
| + MessageLoop::current()->RunAllPending(); |
| + LOG(ERROR) << quota_eviction_handler()->usage(); |
| +} |
| + |
| +TEST_F(QuotaTemporaryStorageEvictorTest, Repeated) { |
| + quota_eviction_handler()->add_pseudo_origin(GURL("http://www.foo.com"), 500); |
| + quota_eviction_handler()->add_pseudo_origin(GURL("http://www.bar.com"), 200); |
| + quota_eviction_handler()->add_pseudo_origin(GURL("http://www.baz.com"), 3000); |
| + quota_eviction_handler()->add_pseudo_origin(GURL("http://www.hoge.com"), 10); |
| + quota_eviction_handler()->set_quota(4000); |
| + quota_eviction_handler()->set_available_space(1000000000); |
| + LOG(ERROR) << quota_eviction_handler()->usage(); |
| + temporary_storage_evictor()->set_repeated_eviction(true); |
| + temporary_storage_evictor()->Start(); |
| + MessageLoop::current()->RunAllPending(); |
| + LOG(ERROR) << quota_eviction_handler()->usage(); |
| +} |
| + |
|
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
|
| +} // namespace quota |