OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // This is a mock of the http cache and related testing classes. To be fair, it | 5 // This is a mock of the http cache and related testing classes. To be fair, it |
6 // is not really a mock http cache given that it uses the real implementation of | 6 // is not really a mock http cache given that it uses the real implementation of |
7 // the http cache, but it has fake implementations of all required components, | 7 // the http cache, but it has fake implementations of all required components, |
8 // so it is useful for unit tests at the http layer. | 8 // so it is useful for unit tests at the http layer. |
9 | 9 |
10 #ifndef NET_HTTP_MOCK_HTTP_CACHE_H_ | 10 #ifndef NET_HTTP_MOCK_HTTP_CACHE_H_ |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 net::OldCompletionCallback* callback) OVERRIDE; | 108 net::OldCompletionCallback* callback) OVERRIDE; |
109 virtual int DoomEntriesSince(const base::Time initial_time, | 109 virtual int DoomEntriesSince(const base::Time initial_time, |
110 net::OldCompletionCallback* callback) OVERRIDE; | 110 net::OldCompletionCallback* callback) OVERRIDE; |
111 virtual int OpenNextEntry(void** iter, disk_cache::Entry** next_entry, | 111 virtual int OpenNextEntry(void** iter, disk_cache::Entry** next_entry, |
112 net::OldCompletionCallback* callback) OVERRIDE; | 112 net::OldCompletionCallback* callback) OVERRIDE; |
113 virtual void EndEnumeration(void** iter) OVERRIDE; | 113 virtual void EndEnumeration(void** iter) OVERRIDE; |
114 virtual void GetStats( | 114 virtual void GetStats( |
115 std::vector<std::pair<std::string, std::string> >* stats) OVERRIDE; | 115 std::vector<std::pair<std::string, std::string> >* stats) OVERRIDE; |
116 virtual void OnExternalCacheHit(const std::string& key) OVERRIDE; | 116 virtual void OnExternalCacheHit(const std::string& key) OVERRIDE; |
117 | 117 |
118 // returns number of times a cache entry was successfully opened | 118 // Returns number of times a cache entry was successfully opened. |
119 int open_count() const { return open_count_; } | 119 int open_count() const { return open_count_; } |
120 | 120 |
121 // returns number of times a cache entry was successfully created | 121 // Returns number of times a cache entry was successfully created. |
122 int create_count() const { return create_count_; } | 122 int create_count() const { return create_count_; } |
123 | 123 |
124 // Fail any subsequent CreateEntry and OpenEntry. | 124 // Fail any subsequent CreateEntry and OpenEntry. |
125 void set_fail_requests() { fail_requests_ = true; } | 125 void set_fail_requests() { fail_requests_ = true; } |
126 | 126 |
127 // Return entries that fail some of their requests. | 127 // Return entries that fail some of their requests. |
128 void set_soft_failures(bool value) { soft_failures_ = value; } | 128 void set_soft_failures(bool value) { soft_failures_ = value; } |
129 | 129 |
| 130 // Makes sure that CreateEntry is not called twice for a given key. |
| 131 void set_double_create_check(bool value) { double_create_check_ = value; } |
| 132 |
130 void ReleaseAll(); | 133 void ReleaseAll(); |
131 | 134 |
132 private: | 135 private: |
133 typedef base::hash_map<std::string, MockDiskEntry*> EntryMap; | 136 typedef base::hash_map<std::string, MockDiskEntry*> EntryMap; |
134 class CallbackRunner; | 137 class CallbackRunner; |
135 | 138 |
136 void CallbackLater(net::OldCompletionCallback* callback, int result); | 139 void CallbackLater(net::OldCompletionCallback* callback, int result); |
137 | 140 |
138 EntryMap entries_; | 141 EntryMap entries_; |
139 int open_count_; | 142 int open_count_; |
140 int create_count_; | 143 int create_count_; |
141 bool fail_requests_; | 144 bool fail_requests_; |
142 bool soft_failures_; | 145 bool soft_failures_; |
| 146 bool double_create_check_; |
143 }; | 147 }; |
144 | 148 |
145 class MockBackendFactory : public net::HttpCache::BackendFactory { | 149 class MockBackendFactory : public net::HttpCache::BackendFactory { |
146 public: | 150 public: |
147 virtual int CreateBackend(net::NetLog* net_log, | 151 virtual int CreateBackend(net::NetLog* net_log, |
148 disk_cache::Backend** backend, | 152 disk_cache::Backend** backend, |
149 net::OldCompletionCallback* callback) OVERRIDE; | 153 net::OldCompletionCallback* callback) OVERRIDE; |
150 }; | 154 }; |
151 | 155 |
152 class MockHttpCache { | 156 class MockHttpCache { |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 private: | 228 private: |
225 int Result() { return fail_ ? net::ERR_FAILED : net::OK; } | 229 int Result() { return fail_ ? net::ERR_FAILED : net::OK; } |
226 | 230 |
227 disk_cache::Backend** backend_; | 231 disk_cache::Backend** backend_; |
228 net::OldCompletionCallback* callback_; | 232 net::OldCompletionCallback* callback_; |
229 bool block_; | 233 bool block_; |
230 bool fail_; | 234 bool fail_; |
231 }; | 235 }; |
232 | 236 |
233 #endif // NET_HTTP_MOCK_HTTP_CACHE_H_ | 237 #endif // NET_HTTP_MOCK_HTTP_CACHE_H_ |
OLD | NEW |