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 DisableFirstCleanup() {
93 first_cleanup_
= false;
96 void DisableIntegrityCheck() {
100 void UseCurrentThread() {
101 use_current_thread_
= true;
104 void SetCacheType(net::CacheType type
) {
108 // Utility methods to access the cache and wait for each operation to finish.
109 int OpenEntry(const std::string
& key
, disk_cache::Entry
** entry
);
110 int CreateEntry(const std::string
& key
, disk_cache::Entry
** entry
);
111 int DoomEntry(const std::string
& key
);
112 int DoomAllEntries();
113 int DoomEntriesBetween(const base::Time initial_time
,
114 const base::Time end_time
);
115 int DoomEntriesSince(const base::Time initial_time
);
116 int OpenNextEntry(void** iter
, disk_cache::Entry
** next_entry
);
117 void FlushQueueForTest();
118 void RunTaskForTest(const base::Closure
& closure
);
119 int ReadData(disk_cache::Entry
* entry
, int index
, int offset
,
120 net::IOBuffer
* buf
, int len
);
121 int WriteData(disk_cache::Entry
* entry
, int index
, int offset
,
122 net::IOBuffer
* buf
, int len
, bool truncate
);
123 int ReadSparseData(disk_cache::Entry
* entry
, int64 offset
, net::IOBuffer
* buf
,
125 int WriteSparseData(disk_cache::Entry
* entry
, int64 offset
,
126 net::IOBuffer
* buf
, int len
);
128 // Asks the cache to trim an entry. If |empty| is true, the whole cache is
130 void TrimForTest(bool empty
);
132 // Asks the cache to trim an entry from the deleted list. If |empty| is
133 // true, the whole list is deleted.
134 void TrimDeletedListForTest(bool empty
);
136 // Makes sure that some time passes before continuing the test. Time::Now()
137 // before and after this method will not be the same.
141 virtual void TearDown() OVERRIDE
;
143 // cache_ will always have a valid object, regardless of how the cache was
144 // initialized. The implementation pointers can be NULL.
145 disk_cache::Backend
* cache_
;
146 disk_cache::BackendImpl
* cache_impl_
;
147 disk_cache::SimpleBackendImpl
* simple_cache_impl_
;
148 disk_cache::MemBackendImpl
* mem_cache_
;
152 net::CacheType type_
;
154 bool simple_cache_mode_
;
155 bool force_creation_
;
159 bool use_current_thread_
;
160 // This is intentionally left uninitialized, to be used by any test.
164 void InitMemoryCache();
165 void InitDiskCache();
167 base::Thread cache_thread_
;
168 DISALLOW_COPY_AND_ASSIGN(DiskCacheTestWithCache
);
171 #endif // NET_DISK_CACHE_DISK_CACHE_TEST_BASE_H_