Fix recording of "Time to error histograms".
[chromium-blink-merge.git] / net / disk_cache / disk_cache_test_base.h
blobef136f4429ffa75e2ac6ac3e66ba328d0955bef5
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 "net/disk_cache/disk_cache.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "testing/platform_test.h"
18 namespace net {
20 class IOBuffer;
22 } // namespace net
24 namespace disk_cache {
26 class Backend;
27 class BackendImpl;
28 class Entry;
29 class MemBackendImpl;
30 class SimpleBackendImpl;
32 } // namespace disk_cache
34 // These tests can use the path service, which uses autoreleased objects on the
35 // Mac, so this needs to be a PlatformTest. Even tests that do not require a
36 // cache (and that do not need to be a DiskCacheTestWithCache) are susceptible
37 // to this problem; all such tests should use TEST_F(DiskCacheTest, ...).
38 class DiskCacheTest : public PlatformTest {
39 protected:
40 DiskCacheTest();
41 ~DiskCacheTest() override;
43 // Copies a set of cache files from the data folder to the test folder.
44 bool CopyTestCache(const std::string& name);
46 // Deletes the contents of |cache_path_|.
47 bool CleanupCacheDir();
49 void TearDown() override;
51 base::FilePath cache_path_;
53 private:
54 base::ScopedTempDir temp_dir_;
55 scoped_ptr<base::MessageLoop> message_loop_;
58 // Provides basic support for cache related tests.
59 class DiskCacheTestWithCache : public DiskCacheTest {
60 protected:
61 class TestIterator {
62 public:
63 explicit TestIterator(scoped_ptr<disk_cache::Backend::Iterator> iterator);
64 ~TestIterator();
66 int OpenNextEntry(disk_cache::Entry** next_entry);
68 private:
69 scoped_ptr<disk_cache::Backend::Iterator> iterator_;
72 DiskCacheTestWithCache();
73 ~DiskCacheTestWithCache() override;
75 void CreateBackend(uint32 flags, base::Thread* thread);
77 void InitCache();
78 void SimulateCrash();
79 void SetTestMode();
81 void SetMemoryOnlyMode() {
82 memory_only_ = true;
85 void SetSimpleCacheMode() {
86 simple_cache_mode_ = true;
89 void SetMask(uint32 mask) {
90 mask_ = mask;
93 void SetMaxSize(int size);
95 // Deletes and re-creates the files on initialization errors.
96 void SetForceCreation() {
97 force_creation_ = true;
100 void SetNewEviction() {
101 new_eviction_ = true;
104 void DisableSimpleCacheWaitForIndex() {
105 simple_cache_wait_for_index_ = false;
108 void DisableFirstCleanup() {
109 first_cleanup_ = false;
112 void DisableIntegrityCheck() {
113 integrity_ = false;
116 void UseCurrentThread() {
117 use_current_thread_ = true;
120 void SetCacheType(net::CacheType type) {
121 type_ = type;
124 // Utility methods to access the cache and wait for each operation to finish.
125 int OpenEntry(const std::string& key, disk_cache::Entry** entry);
126 int CreateEntry(const std::string& key, disk_cache::Entry** entry);
127 int DoomEntry(const std::string& key);
128 int DoomAllEntries();
129 int DoomEntriesBetween(const base::Time initial_time,
130 const base::Time end_time);
131 int DoomEntriesSince(const base::Time initial_time);
132 scoped_ptr<TestIterator> CreateIterator();
133 void FlushQueueForTest();
134 void RunTaskForTest(const base::Closure& closure);
135 int ReadData(disk_cache::Entry* entry, int index, int offset,
136 net::IOBuffer* buf, int len);
137 int WriteData(disk_cache::Entry* entry, int index, int offset,
138 net::IOBuffer* buf, int len, bool truncate);
139 int ReadSparseData(disk_cache::Entry* entry, int64 offset, net::IOBuffer* buf,
140 int len);
141 int WriteSparseData(disk_cache::Entry* entry, int64 offset,
142 net::IOBuffer* buf, int len);
144 // Asks the cache to trim an entry. If |empty| is true, the whole cache is
145 // deleted.
146 void TrimForTest(bool empty);
148 // Asks the cache to trim an entry from the deleted list. If |empty| is
149 // true, the whole list is deleted.
150 void TrimDeletedListForTest(bool empty);
152 // Makes sure that some time passes before continuing the test. Time::Now()
153 // before and after this method will not be the same.
154 void AddDelay();
156 // DiskCacheTest:
157 void TearDown() override;
159 // cache_ will always have a valid object, regardless of how the cache was
160 // initialized. The implementation pointers can be NULL.
161 scoped_ptr<disk_cache::Backend> cache_;
162 disk_cache::BackendImpl* cache_impl_;
163 disk_cache::SimpleBackendImpl* simple_cache_impl_;
164 disk_cache::MemBackendImpl* mem_cache_;
166 uint32 mask_;
167 int size_;
168 net::CacheType type_;
169 bool memory_only_;
170 bool simple_cache_mode_;
171 bool simple_cache_wait_for_index_;
172 bool force_creation_;
173 bool new_eviction_;
174 bool first_cleanup_;
175 bool integrity_;
176 bool use_current_thread_;
177 // This is intentionally left uninitialized, to be used by any test.
178 bool success_;
180 private:
181 void InitMemoryCache();
182 void InitDiskCache();
184 base::Thread cache_thread_;
185 DISALLOW_COPY_AND_ASSIGN(DiskCacheTestWithCache);
188 #endif // NET_DISK_CACHE_DISK_CACHE_TEST_BASE_H_