OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef NET_DISK_CACHE_DISK_CACHE_TEST_BASE_H_ | |
6 #define NET_DISK_CACHE_DISK_CACHE_TEST_BASE_H_ | |
7 | |
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" | |
16 | |
17 namespace net { | |
18 | |
19 class IOBuffer; | |
20 | |
21 } // namespace net | |
22 | |
23 namespace disk_cache { | |
24 | |
25 class Backend; | |
26 class BackendImpl; | |
27 class Entry; | |
28 class MemBackendImpl; | |
29 class SimpleBackendImpl; | |
30 | |
31 } // namespace disk_cache | |
32 | |
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(); | |
41 | |
42 // Copies a set of cache files from the data folder to the test folder. | |
43 bool CopyTestCache(const std::string& name); | |
44 | |
45 // Deletes the contents of |cache_path_|. | |
46 bool CleanupCacheDir(); | |
47 | |
48 virtual void TearDown() OVERRIDE; | |
49 | |
50 base::FilePath cache_path_; | |
51 | |
52 private: | |
53 base::ScopedTempDir temp_dir_; | |
54 scoped_ptr<base::MessageLoop> message_loop_; | |
55 }; | |
56 | |
57 // Provides basic support for cache related tests. | |
58 class DiskCacheTestWithCache : public DiskCacheTest { | |
59 protected: | |
60 DiskCacheTestWithCache(); | |
61 virtual ~DiskCacheTestWithCache(); | |
62 | |
63 void CreateBackend(uint32 flags, base::Thread* thread); | |
64 | |
65 void InitCache(); | |
66 void SimulateCrash(); | |
67 void SetTestMode(); | |
68 | |
69 void SetMemoryOnlyMode() { | |
70 memory_only_ = true; | |
71 } | |
72 | |
73 void SetSimpleCacheMode() { | |
74 simple_cache_mode_ = true; | |
75 } | |
76 | |
77 void SetMask(uint32 mask) { | |
78 mask_ = mask; | |
79 } | |
80 | |
81 void SetMaxSize(int size); | |
82 | |
83 // Deletes and re-creates the files on initialization errors. | |
84 void SetForceCreation() { | |
85 force_creation_ = true; | |
86 } | |
87 | |
88 void SetNewEviction() { | |
89 new_eviction_ = true; | |
90 } | |
91 | |
92 void DisableSimpleCacheWaitForIndex() { | |
93 simple_cache_wait_for_index_ = false; | |
94 } | |
95 | |
96 void DisableFirstCleanup() { | |
97 first_cleanup_ = false; | |
98 } | |
99 | |
100 void DisableIntegrityCheck() { | |
101 integrity_ = false; | |
102 } | |
103 | |
104 void UseCurrentThread() { | |
105 use_current_thread_ = true; | |
106 } | |
107 | |
108 void SetCacheType(net::CacheType type) { | |
109 type_ = type; | |
110 } | |
111 | |
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, | |
128 int len); | |
129 int WriteSparseData(disk_cache::Entry* entry, int64 offset, | |
130 net::IOBuffer* buf, int len); | |
131 | |
132 // Asks the cache to trim an entry. If |empty| is true, the whole cache is | |
133 // deleted. | |
134 void TrimForTest(bool empty); | |
135 | |
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); | |
139 | |
140 // Makes sure that some time passes before continuing the test. Time::Now() | |
141 // before and after this method will not be the same. | |
142 void AddDelay(); | |
143 | |
144 // DiskCacheTest: | |
145 virtual void TearDown() OVERRIDE; | |
146 | |
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_; | |
153 | |
154 uint32 mask_; | |
155 int size_; | |
156 net::CacheType type_; | |
157 bool memory_only_; | |
158 bool simple_cache_mode_; | |
159 bool simple_cache_wait_for_index_; | |
160 bool force_creation_; | |
161 bool new_eviction_; | |
162 bool first_cleanup_; | |
163 bool integrity_; | |
164 bool use_current_thread_; | |
165 // This is intentionally left uninitialized, to be used by any test. | |
166 bool success_; | |
167 | |
168 private: | |
169 void InitMemoryCache(); | |
170 void InitDiskCache(); | |
171 | |
172 base::Thread cache_thread_; | |
173 DISALLOW_COPY_AND_ASSIGN(DiskCacheTestWithCache); | |
174 }; | |
175 | |
176 #endif // NET_DISK_CACHE_DISK_CACHE_TEST_BASE_H_ | |
OLD | NEW |