summaryrefslogtreecommitdiff
path: root/net/disk_cache/disk_cache_test_base.cc
diff options
context:
space:
mode:
authorKristian Monsen <kristianm@google.com>2011-05-31 20:30:28 +0100
committerKristian Monsen <kristianm@google.com>2011-06-14 20:31:41 -0700
commit72a454cd3513ac24fbdd0e0cb9ad70b86a99b801 (patch)
tree382278a54ce7a744d62fa510a9a80688cc12434b /net/disk_cache/disk_cache_test_base.cc
parentc4becdd46e31d261b930e4b5a539cbc1d45c23a6 (diff)
downloadchromium-72a454cd3513ac24fbdd0e0cb9ad70b86a99b801.tar.gz
Merge Chromium.org at r11.0.672.0: Initial merge by git.
Change-Id: I8b4aaf611a2a405fe3fe10e8a94ea7658645c192
Diffstat (limited to 'net/disk_cache/disk_cache_test_base.cc')
-rw-r--r--net/disk_cache/disk_cache_test_base.cc222
1 files changed, 135 insertions, 87 deletions
diff --git a/net/disk_cache/disk_cache_test_base.cc b/net/disk_cache/disk_cache_test_base.cc
index 6c9b91c6..41d1caf5 100644
--- a/net/disk_cache/disk_cache_test_base.cc
+++ b/net/disk_cache/disk_cache_test_base.cc
@@ -15,15 +15,25 @@ void DiskCacheTest::TearDown() {
MessageLoop::current()->RunAllPending();
}
-void DiskCacheTestWithCache::SetMaxSize(int size) {
- size_ = size;
- if (cache_impl_)
- EXPECT_TRUE(cache_impl_->SetMaxSize(size));
-
- if (mem_cache_)
- EXPECT_TRUE(mem_cache_->SetMaxSize(size));
+DiskCacheTestWithCache::DiskCacheTestWithCache()
+ : cache_(NULL),
+ cache_impl_(NULL),
+ mem_cache_(NULL),
+ mask_(0),
+ size_(0),
+ type_(net::DISK_CACHE),
+ memory_only_(false),
+ implementation_(false),
+ force_creation_(false),
+ new_eviction_(false),
+ first_cleanup_(true),
+ integrity_(true),
+ use_current_thread_(false),
+ cache_thread_("CacheThread") {
}
+DiskCacheTestWithCache::~DiskCacheTestWithCache() {}
+
void DiskCacheTestWithCache::InitCache() {
if (mask_ || new_eviction_)
implementation_ = true;
@@ -38,86 +48,6 @@ void DiskCacheTestWithCache::InitCache() {
ASSERT_EQ(0, cache_->GetEntryCount());
}
-void DiskCacheTestWithCache::InitMemoryCache() {
- if (!implementation_) {
- cache_ = disk_cache::MemBackendImpl::CreateBackend(size_);
- return;
- }
-
- mem_cache_ = new disk_cache::MemBackendImpl();
- cache_ = mem_cache_;
- ASSERT_TRUE(NULL != cache_);
-
- if (size_)
- EXPECT_TRUE(mem_cache_->SetMaxSize(size_));
-
- ASSERT_TRUE(mem_cache_->Init());
-}
-
-void DiskCacheTestWithCache::InitDiskCache() {
- FilePath path = GetCacheFilePath();
- if (first_cleanup_)
- ASSERT_TRUE(DeleteCache(path));
-
- if (!cache_thread_.IsRunning()) {
- EXPECT_TRUE(cache_thread_.StartWithOptions(
- base::Thread::Options(MessageLoop::TYPE_IO, 0)));
- }
- ASSERT_TRUE(cache_thread_.message_loop() != NULL);
-
- if (implementation_)
- return InitDiskCacheImpl(path);
-
- scoped_refptr<base::MessageLoopProxy> thread =
- use_current_thread_ ? base::MessageLoopProxy::CreateForCurrentThread() :
- cache_thread_.message_loop_proxy();
-
- TestCompletionCallback cb;
- int rv = disk_cache::BackendImpl::CreateBackend(
- path, force_creation_, size_, type_,
- disk_cache::kNoRandom, thread, NULL, &cache_, &cb);
- ASSERT_EQ(net::OK, cb.GetResult(rv));
-}
-
-void DiskCacheTestWithCache::InitDiskCacheImpl(const FilePath& path) {
- scoped_refptr<base::MessageLoopProxy> thread =
- use_current_thread_ ? base::MessageLoopProxy::CreateForCurrentThread() :
- cache_thread_.message_loop_proxy();
- if (mask_)
- cache_impl_ = new disk_cache::BackendImpl(path, mask_, thread, NULL);
- else
- cache_impl_ = new disk_cache::BackendImpl(path, thread, NULL);
-
- cache_ = cache_impl_;
- ASSERT_TRUE(NULL != cache_);
-
- if (size_)
- EXPECT_TRUE(cache_impl_->SetMaxSize(size_));
-
- if (new_eviction_)
- cache_impl_->SetNewEviction();
-
- cache_impl_->SetType(type_);
- cache_impl_->SetFlags(disk_cache::kNoRandom);
- TestCompletionCallback cb;
- int rv = cache_impl_->Init(&cb);
- ASSERT_EQ(net::OK, cb.GetResult(rv));
-}
-
-void DiskCacheTestWithCache::TearDown() {
- MessageLoop::current()->RunAllPending();
- delete cache_;
- if (cache_thread_.IsRunning())
- cache_thread_.Stop();
-
- if (!memory_only_ && integrity_) {
- FilePath path = GetCacheFilePath();
- EXPECT_TRUE(CheckCacheIntegrity(path, new_eviction_));
- }
-
- PlatformTest::TearDown();
-}
-
// We are expected to leak memory when simulating crashes.
void DiskCacheTestWithCache::SimulateCrash() {
ASSERT_TRUE(implementation_ && !memory_only_);
@@ -138,6 +68,15 @@ void DiskCacheTestWithCache::SetTestMode() {
cache_impl_->SetUnitTestMode();
}
+void DiskCacheTestWithCache::SetMaxSize(int size) {
+ size_ = size;
+ if (cache_impl_)
+ EXPECT_TRUE(cache_impl_->SetMaxSize(size));
+
+ if (mem_cache_)
+ EXPECT_TRUE(mem_cache_->SetMaxSize(size));
+}
+
int DiskCacheTestWithCache::OpenEntry(const std::string& key,
disk_cache::Entry** entry) {
TestCompletionCallback cb;
@@ -236,3 +175,112 @@ int DiskCacheTestWithCache::WriteSparseData(disk_cache::Entry* entry,
int rv = entry->WriteSparseData(offset, buf, len, &cb);
return cb.GetResult(rv);
}
+
+// Simple task to run part of a test from the cache thread.
+class TrimTask : public Task {
+ public:
+ TrimTask(disk_cache::BackendImpl* backend, bool deleted, bool empty)
+ : backend_(backend),
+ deleted_(deleted),
+ empty_(empty) {}
+
+ virtual void Run() {
+ if (deleted_)
+ backend_->TrimDeletedListForTest(empty_);
+ else
+ backend_->TrimForTest(empty_);
+ }
+
+ protected:
+ disk_cache::BackendImpl* backend_;
+ bool deleted_;
+ bool empty_;
+};
+
+void DiskCacheTestWithCache::TrimForTest(bool empty) {
+ RunTaskForTest(new TrimTask(cache_impl_, false, empty));
+}
+
+void DiskCacheTestWithCache::TrimDeletedListForTest(bool empty) {
+ RunTaskForTest(new TrimTask(cache_impl_, true, empty));
+}
+
+void DiskCacheTestWithCache::TearDown() {
+ MessageLoop::current()->RunAllPending();
+ delete cache_;
+ if (cache_thread_.IsRunning())
+ cache_thread_.Stop();
+
+ if (!memory_only_ && integrity_) {
+ FilePath path = GetCacheFilePath();
+ EXPECT_TRUE(CheckCacheIntegrity(path, new_eviction_));
+ }
+
+ PlatformTest::TearDown();
+}
+
+void DiskCacheTestWithCache::InitMemoryCache() {
+ if (!implementation_) {
+ cache_ = disk_cache::MemBackendImpl::CreateBackend(size_);
+ return;
+ }
+
+ mem_cache_ = new disk_cache::MemBackendImpl();
+ cache_ = mem_cache_;
+ ASSERT_TRUE(NULL != cache_);
+
+ if (size_)
+ EXPECT_TRUE(mem_cache_->SetMaxSize(size_));
+
+ ASSERT_TRUE(mem_cache_->Init());
+}
+
+void DiskCacheTestWithCache::InitDiskCache() {
+ FilePath path = GetCacheFilePath();
+ if (first_cleanup_)
+ ASSERT_TRUE(DeleteCache(path));
+
+ if (!cache_thread_.IsRunning()) {
+ EXPECT_TRUE(cache_thread_.StartWithOptions(
+ base::Thread::Options(MessageLoop::TYPE_IO, 0)));
+ }
+ ASSERT_TRUE(cache_thread_.message_loop() != NULL);
+
+ if (implementation_)
+ return InitDiskCacheImpl(path);
+
+ scoped_refptr<base::MessageLoopProxy> thread =
+ use_current_thread_ ? base::MessageLoopProxy::CreateForCurrentThread() :
+ cache_thread_.message_loop_proxy();
+
+ TestCompletionCallback cb;
+ int rv = disk_cache::BackendImpl::CreateBackend(
+ path, force_creation_, size_, type_,
+ disk_cache::kNoRandom, thread, NULL, &cache_, &cb);
+ ASSERT_EQ(net::OK, cb.GetResult(rv));
+}
+
+void DiskCacheTestWithCache::InitDiskCacheImpl(const FilePath& path) {
+ scoped_refptr<base::MessageLoopProxy> thread =
+ use_current_thread_ ? base::MessageLoopProxy::CreateForCurrentThread() :
+ cache_thread_.message_loop_proxy();
+ if (mask_)
+ cache_impl_ = new disk_cache::BackendImpl(path, mask_, thread, NULL);
+ else
+ cache_impl_ = new disk_cache::BackendImpl(path, thread, NULL);
+
+ cache_ = cache_impl_;
+ ASSERT_TRUE(NULL != cache_);
+
+ if (size_)
+ EXPECT_TRUE(cache_impl_->SetMaxSize(size_));
+
+ if (new_eviction_)
+ cache_impl_->SetNewEviction();
+
+ cache_impl_->SetType(type_);
+ cache_impl_->SetFlags(disk_cache::kNoRandom);
+ TestCompletionCallback cb;
+ int rv = cache_impl_->Init(&cb);
+ ASSERT_EQ(net::OK, cb.GetResult(rv));
+}