Home | History | Annotate | Download | only in disk_cache
      1 // Copyright (c) 2006-2008 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 "net/disk_cache/disk_cache_test_base.h"
      6 
      7 #include "net/disk_cache/backend_impl.h"
      8 #include "net/disk_cache/disk_cache_test_util.h"
      9 #include "net/disk_cache/mem_backend_impl.h"
     10 
     11 void DiskCacheTest::TearDown() {
     12   MessageLoop::current()->RunAllPending();
     13 }
     14 
     15 void DiskCacheTestWithCache::SetMaxSize(int size) {
     16   size_ = size;
     17   if (cache_impl_)
     18     EXPECT_TRUE(cache_impl_->SetMaxSize(size));
     19 
     20   if (mem_cache_)
     21     EXPECT_TRUE(mem_cache_->SetMaxSize(size));
     22 }
     23 
     24 void DiskCacheTestWithCache::InitCache() {
     25   if (mask_ || new_eviction_)
     26     implementation_ = true;
     27 
     28   if (memory_only_)
     29     InitMemoryCache();
     30   else
     31     InitDiskCache();
     32 
     33   ASSERT_TRUE(NULL != cache_);
     34   if (first_cleanup_)
     35     ASSERT_EQ(0, cache_->GetEntryCount());
     36 }
     37 
     38 void DiskCacheTestWithCache::InitMemoryCache() {
     39   if (!implementation_) {
     40     cache_ = disk_cache::CreateInMemoryCacheBackend(size_);
     41     return;
     42   }
     43 
     44   mem_cache_ = new disk_cache::MemBackendImpl();
     45   cache_ = mem_cache_;
     46   ASSERT_TRUE(NULL != cache_);
     47 
     48   if (size_)
     49     EXPECT_TRUE(mem_cache_->SetMaxSize(size_));
     50 
     51   ASSERT_TRUE(mem_cache_->Init());
     52 }
     53 
     54 void DiskCacheTestWithCache::InitDiskCache() {
     55   FilePath path = GetCacheFilePath();
     56   if (first_cleanup_)
     57     ASSERT_TRUE(DeleteCache(path));
     58 
     59   if (implementation_)
     60     return InitDiskCacheImpl(path);
     61 
     62   cache_ = disk_cache::BackendImpl::CreateBackend(path, force_creation_, size_,
     63                                                   net::DISK_CACHE,
     64                                                   disk_cache::kNoRandom);
     65 }
     66 
     67 void DiskCacheTestWithCache::InitDiskCacheImpl(const FilePath& path) {
     68   if (mask_)
     69     cache_impl_ = new disk_cache::BackendImpl(path, mask_);
     70   else
     71     cache_impl_ = new disk_cache::BackendImpl(path);
     72 
     73   cache_ = cache_impl_;
     74   ASSERT_TRUE(NULL != cache_);
     75 
     76   if (size_)
     77     EXPECT_TRUE(cache_impl_->SetMaxSize(size_));
     78 
     79   if (new_eviction_)
     80     cache_impl_->SetNewEviction();
     81 
     82   cache_impl_->SetFlags(disk_cache::kNoRandom);
     83   ASSERT_TRUE(cache_impl_->Init());
     84 }
     85 
     86 void DiskCacheTestWithCache::TearDown() {
     87   MessageLoop::current()->RunAllPending();
     88   delete cache_;
     89 
     90   if (!memory_only_ && integrity_) {
     91     FilePath path = GetCacheFilePath();
     92     EXPECT_TRUE(CheckCacheIntegrity(path, new_eviction_));
     93   }
     94 
     95   PlatformTest::TearDown();
     96 }
     97 
     98 // We are expected to leak memory when simulating crashes.
     99 void DiskCacheTestWithCache::SimulateCrash() {
    100   ASSERT_TRUE(implementation_ && !memory_only_);
    101   cache_impl_->ClearRefCountForTest();
    102 
    103   delete cache_impl_;
    104   FilePath path = GetCacheFilePath();
    105   EXPECT_TRUE(CheckCacheIntegrity(path, new_eviction_));
    106 
    107   InitDiskCacheImpl(path);
    108 }
    109 
    110 void DiskCacheTestWithCache::SetTestMode() {
    111   ASSERT_TRUE(implementation_ && !memory_only_);
    112   cache_impl_->SetUnitTestMode();
    113 }
    114