Index: net/disk_cache/disk_cache_test.cc |
diff --git a/net/disk_cache/disk_cache_test.cc b/net/disk_cache/disk_cache_test.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..e5e61ec99a62d8b422f74162bb7ab70afdfdfd27 |
--- /dev/null |
+++ b/net/disk_cache/disk_cache_test.cc |
@@ -0,0 +1,215 @@ |
+// Copyright (c) 2012 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 "net/disk_cache/disk_cache_test.h" |
+ |
+#include "base/file_util.h" |
+#include "base/path_service.h" |
+#include "base/run_loop.h" |
+#include "base/threading/platform_thread.h" |
+#include "net/base/test_completion_callback.h" |
+#include "net/disk_cache/cache_util.h" |
+#include "net/disk_cache/disk_cache.h" |
+#include "net/disk_cache/disk_cache_test_util.h" |
+ |
+namespace disk_cache { |
+ |
+BackendTestTraits::CreateBackendExtraData::~CreateBackendExtraData() { |
+} |
+ |
+bool BackendTestTraits::WritesUpdateLastUsed() const { |
+ return true; |
+} |
+ |
+bool BackendTestTraits::ReadsUpdateLastUsed() const { |
+ return true; |
+} |
+ |
+int BackendTestTraits::SparseRoundingInterval() const { |
+ return 1; |
+} |
+ |
+bool BackendTestTraits::EntryCountIncludesSparseRanges() const { |
+ return false; |
+} |
+ |
+bool BackendTestTraits::ImplementsCouldBeSparse() const { |
+ return true; |
+} |
+ |
+bool BackendTestTraits::DoomedSparseEntriesIOWorks() const { |
+ return true; |
+} |
+ |
+bool BackendTestTraits::EnumerationsAreLexicographicByKey() const { |
+ return true; |
+} |
+ |
+DiskCacheTest::DiskCacheTest() : use_current_thread_(false), |
+ max_size_(0), |
+ traits_(GetParam()), |
+ cache_thread_("CacheThread") { |
+ CHECK(temp_dir_.CreateUniqueTempDir()); |
+ if (!base::MessageLoop::current()) |
+ message_loop_.reset(new base::MessageLoopForIO()); |
+} |
+ |
+DiskCacheTest::~DiskCacheTest() { |
+} |
+ |
+bool DiskCacheTest::CopyTestCache(const std::string& name) { |
+ base::FilePath path; |
+ PathService::Get(base::DIR_SOURCE_ROOT, &path); |
+ path = path.AppendASCII("net"); |
+ path = path.AppendASCII("data"); |
+ path = path.AppendASCII("cache_tests"); |
+ path = path.AppendASCII(name); |
+ |
+ if (!CleanupCacheDir()) |
+ return false; |
+ return base::CopyDirectory(path, cache_path(), false); |
+} |
+ |
+bool DiskCacheTest::CleanupCacheDir() { |
+ return ::DeleteCache(cache_path()); |
+} |
+ |
+void DiskCacheTest::TearDown() { |
+ base::RunLoop().RunUntilIdle(); |
+ if (traits_) { |
+ traits_->PreTearDown(); |
+ base::RunLoop().RunUntilIdle(); |
+ } |
+ cache_.reset(); |
+ if (cache_thread_.IsRunning()) |
+ cache_thread_.Stop(); |
+ |
+ base::RunLoop().RunUntilIdle(); |
+ if (traits_) |
+ traits_->PostTearDown(); |
+} |
+ |
+void DiskCacheTest::InitCacheWithExtraData( |
+ const BackendTestTraits::CreateBackendExtraData* extra_data) { |
+ base::MessageLoopProxy* runner; |
+ if (use_current_thread_) { |
+ runner = base::MessageLoopProxy::current().get(); |
+ } else { |
+ if (!cache_thread_.IsRunning()) { |
+ ASSERT_TRUE(cache_thread_.StartWithOptions( |
+ base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); |
+ } |
+ runner = cache_thread_.message_loop_proxy().get(); |
+ } |
+ |
+ cache_.reset(); |
+ cache_.reset(traits_->CreateBackend(extra_data, cache_path(), max_size_, runner)); |
+} |
+ |
+int DiskCacheTest::OpenEntry(const std::string& key, |
+ disk_cache::Entry** entry) { |
+ net::TestCompletionCallback cb; |
+ int rv = cache_->OpenEntry(key, entry, cb.callback()); |
+ return cb.GetResult(rv); |
+} |
+ |
+int DiskCacheTest::CreateEntry(const std::string& key, |
+ disk_cache::Entry** entry) { |
+ net::TestCompletionCallback cb; |
+ int rv = cache_->CreateEntry(key, entry, cb.callback()); |
+ return cb.GetResult(rv); |
+} |
+ |
+int DiskCacheTest::DoomEntry(const std::string& key) { |
+ net::TestCompletionCallback cb; |
+ int rv = cache_->DoomEntry(key, cb.callback()); |
+ return cb.GetResult(rv); |
+} |
+ |
+int DiskCacheTest::DoomAllEntries() { |
+ net::TestCompletionCallback cb; |
+ int rv = cache_->DoomAllEntries(cb.callback()); |
+ return cb.GetResult(rv); |
+} |
+ |
+int DiskCacheTest::DoomEntriesBetween(const base::Time initial_time, |
+ const base::Time end_time) { |
+ net::TestCompletionCallback cb; |
+ int rv = cache_->DoomEntriesBetween(initial_time, end_time, cb.callback()); |
+ return cb.GetResult(rv); |
+} |
+ |
+int DiskCacheTest::DoomEntriesSince(const base::Time initial_time) { |
+ net::TestCompletionCallback cb; |
+ int rv = cache_->DoomEntriesSince(initial_time, cb.callback()); |
+ return cb.GetResult(rv); |
+} |
+ |
+int DiskCacheTest::OpenNextEntry(void** iter, |
+ disk_cache::Entry** next_entry) { |
+ net::TestCompletionCallback cb; |
+ int rv = cache_->OpenNextEntry(iter, next_entry, cb.callback()); |
+ return cb.GetResult(rv); |
+} |
+ |
+void DiskCacheTest::SetMaxSize(int max_size) { |
+ max_size_ = max_size; |
+ if (cache_) |
+ traits_->SetMaxSize(cache_.get(), max_size); |
+} |
+ |
+void DiskCacheTest::FlushQueueForTest() { |
+ traits_->FlushQueueForTest(cache_.get()); |
+} |
+ |
+int DiskCacheTest::ReadData(disk_cache::Entry* entry, int index, |
+ int offset, net::IOBuffer* buf, int len) { |
+ net::TestCompletionCallback cb; |
+ int rv = entry->ReadData(index, offset, buf, len, cb.callback()); |
+ return cb.GetResult(rv); |
+} |
+ |
+int DiskCacheTest::WriteData(disk_cache::Entry* entry, int index, |
+ int offset, net::IOBuffer* buf, int len, |
+ bool truncate) { |
+ net::TestCompletionCallback cb; |
+ int rv = entry->WriteData(index, offset, buf, len, cb.callback(), truncate); |
+ return cb.GetResult(rv); |
+} |
+ |
+int DiskCacheTest::ReadSparseData(disk_cache::Entry* entry, |
+ int64 offset, net::IOBuffer* buf, |
+ int len) { |
+ net::TestCompletionCallback cb; |
+ int rv = entry->ReadSparseData(offset, buf, len, cb.callback()); |
+ return cb.GetResult(rv); |
+} |
+ |
+int DiskCacheTest::WriteSparseData(disk_cache::Entry* entry, |
+ int64 offset, |
+ net::IOBuffer* buf, int len) { |
+ net::TestCompletionCallback cb; |
+ int rv = entry->WriteSparseData(offset, buf, len, cb.callback()); |
+ return cb.GetResult(rv); |
+} |
+ |
+base::MessageLoopProxy* DiskCacheTest::GetCacheThread() { |
+ if (traits_->UsesCacheThread()) |
+ return cache_thread_.message_loop_proxy(); |
+ |
+ return base::MessageLoopProxy::current(); |
+} |
+ |
+void BackendTestTraits::AddDelay() const { |
+ base::Time initial = base::Time::Now(); |
+ while (base::Time::Now() <= initial) { |
+ base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1)); |
+ }; |
+} |
+ |
+void DiskCacheTest::AddDelay() { |
+ traits()->AddDelay(); |
+} |
+ |
+} // namespace disk_cache |