1 // Copyright (c) 2012 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.
5 #ifndef NET_DISK_CACHE_DISK_CACHE_TEST_BASE_H_
6 #define NET_DISK_CACHE_DISK_CACHE_TEST_BASE_H_
8 #include "base/basictypes.h"
9 #include "base/files/file_path.h"
10 #include "base/files/scoped_temp_dir.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/threading/thread.h"
13 #include "net/base/cache_type.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "testing/platform_test.h"
23 namespace disk_cache
{
29 class SimpleBackendImpl
;
31 } // namespace disk_cache
33 // These tests can use the path service, which uses autoreleased objects on the
34 // Mac, so this needs to be a PlatformTest. Even tests that do not require a
35 // cache (and that do not need to be a DiskCacheTestWithCache) are susceptible
36 // to this problem; all such tests should use TEST_F(DiskCacheTest, ...).
37 class DiskCacheTest
: public PlatformTest
{
40 virtual ~DiskCacheTest();
42 // Copies a set of cache files from the data folder to the test folder.
43 bool CopyTestCache(const std::string
& name
);
45 // Deletes the contents of |cache_path_|.
46 bool CleanupCacheDir();
48 virtual void TearDown() OVERRIDE
;
50 base::FilePath cache_path_
;
53 base::ScopedTempDir temp_dir_
;
54 scoped_ptr
<base::MessageLoop
> message_loop_
;
57 // Provides basic support for cache related tests.
58 class DiskCacheTestWithCache
: public DiskCacheTest
{
60 DiskCacheTestWithCache();
61 virtual ~DiskCacheTestWithCache();
63 void CreateBackend(uint32 flags
, base::Thread
* thread
);
69 void SetMemoryOnlyMode() {
73 void SetSimpleCacheMode() {
74 simple_cache_mode_
= true;
77 void SetMask(uint32 mask
) {
81 void SetMaxSize(int size
);
83 // Deletes and re-creates the files on initialization errors.
84 void SetForceCreation() {
85 force_creation_
= true;
88 void SetNewEviction() {
92 void DisableSimpleCacheWaitForIndex() {
93 simple_cache_wait_for_index_
= false;
96 void DisableFirstCleanup() {
97 first_cleanup_
= false;
100 void DisableIntegrityCheck() {
104 void UseCurrentThread() {
105 use_current_thread_
= true;
108 void SetCacheType(net::CacheType type
) {
112 // Utility methods to access the cache and wait for each operation to finish.
113 int OpenEntry(const std::string
& key
, disk_cache::Entry
** entry
);
114 int CreateEntry(const std::string
& key
, disk_cache::Entry
** entry
);
115 int DoomEntry(const std::string
& key
);
116 int DoomAllEntries();
117 int DoomEntriesBetween(const base::Time initial_time
,
118 const base::Time end_time
);
119 int DoomEntriesSince(const base::Time initial_time
);
120 int OpenNextEntry(void** iter
, disk_cache::Entry
** next_entry
);
121 void FlushQueueForTest();
122 void RunTaskForTest(const base::Closure
& closure
);
123 int ReadData(disk_cache::Entry
* entry
, int index
, int offset
,
124 net::IOBuffer
* buf
, int len
);
125 int WriteData(disk_cache::Entry
* entry
, int index
, int offset
,
126 net::IOBuffer
* buf
, int len
, bool truncate
);
127 int ReadSparseData(disk_cache::Entry
* entry
, int64 offset
, net::IOBuffer
* buf
,
129 int WriteSparseData(disk_cache::Entry
* entry
, int64 offset
,
130 net::IOBuffer
* buf
, int len
);
132 // Asks the cache to trim an entry. If |empty| is true, the whole cache is
134 void TrimForTest(bool empty
);
136 // Asks the cache to trim an entry from the deleted list. If |empty| is
137 // true, the whole list is deleted.
138 void TrimDeletedListForTest(bool empty
);
140 // Makes sure that some time passes before continuing the test. Time::Now()
141 // before and after this method will not be the same.
145 virtual void TearDown() OVERRIDE
;
147 // cache_ will always have a valid object, regardless of how the cache was
148 // initialized. The implementation pointers can be NULL.
149 scoped_ptr
<disk_cache::Backend
> cache_
;
150 disk_cache::BackendImpl
* cache_impl_
;
151 disk_cache::SimpleBackendImpl
* simple_cache_impl_
;
152 disk_cache::MemBackendImpl
* mem_cache_
;
156 net::CacheType type_
;
158 bool simple_cache_mode_
;
159 bool simple_cache_wait_for_index_
;
160 bool force_creation_
;
164 bool use_current_thread_
;
165 // This is intentionally left uninitialized, to be used by any test.
169 void InitMemoryCache();
170 void InitDiskCache();
172 base::Thread cache_thread_
;
173 DISALLOW_COPY_AND_ASSIGN(DiskCacheTestWithCache
);
176 #endif // NET_DISK_CACHE_DISK_CACHE_TEST_BASE_H_