Pass CreateDirectory errors up to IndexedDB.
[chromium-blink-merge.git] / net / disk_cache / disk_cache_test_base.h
blob69e0920df4f4a01036657b55f77224c59abf53ac
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"
17 namespace net {
19 class IOBuffer;
21 } // namespace net
23 namespace disk_cache {
25 class Backend;
26 class BackendImpl;
27 class Entry;
28 class MemBackendImpl;
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 {
38 protected:
39 DiskCacheTest();
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_;
52 private:
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 {
59 protected:
60 DiskCacheTestWithCache();
61 virtual ~DiskCacheTestWithCache();
63 void CreateBackend(uint32 flags, base::Thread* thread);
65 void InitCache();
66 void SimulateCrash();
67 void SetTestMode();
69 void SetMemoryOnlyMode() {
70 memory_only_ = true;
73 void SetSimpleCacheMode() {
74 simple_cache_mode_ = true;
77 void SetMask(uint32 mask) {
78 mask_ = 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() {
89 new_eviction_ = true;
92 void DisableFirstCleanup() {
93 first_cleanup_ = false;
96 void DisableIntegrityCheck() {
97 integrity_ = false;
100 void UseCurrentThread() {
101 use_current_thread_ = true;
104 void SetCacheType(net::CacheType type) {
105 type_ = 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,
124 int len);
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
129 // deleted.
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.
138 void AddDelay();
140 // DiskCacheTest:
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_;
150 uint32 mask_;
151 int size_;
152 net::CacheType type_;
153 bool memory_only_;
154 bool simple_cache_mode_;
155 bool force_creation_;
156 bool new_eviction_;
157 bool first_cleanup_;
158 bool integrity_;
159 bool use_current_thread_;
160 // This is intentionally left uninitialized, to be used by any test.
161 bool success_;
163 private:
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_