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 17 matching lines...) Expand all Loading... |
28 bool is_doomed() const { return doomed_; } | 28 bool is_doomed() const { return doomed_; } |
29 | 29 |
30 virtual void Doom() OVERRIDE; | 30 virtual void Doom() OVERRIDE; |
31 virtual void Close() OVERRIDE; | 31 virtual void Close() OVERRIDE; |
32 virtual std::string GetKey() const OVERRIDE; | 32 virtual std::string GetKey() const OVERRIDE; |
33 virtual base::Time GetLastUsed() const OVERRIDE; | 33 virtual base::Time GetLastUsed() const OVERRIDE; |
34 virtual base::Time GetLastModified() const OVERRIDE; | 34 virtual base::Time GetLastModified() const OVERRIDE; |
35 virtual int32 GetDataSize(int index) const OVERRIDE; | 35 virtual int32 GetDataSize(int index) const OVERRIDE; |
36 virtual int ReadData(int index, int offset, net::IOBuffer* buf, int buf_len, | 36 virtual int ReadData(int index, int offset, net::IOBuffer* buf, int buf_len, |
37 net::OldCompletionCallback* callback) OVERRIDE; | 37 net::OldCompletionCallback* callback) OVERRIDE; |
| 38 virtual int ReadData(int index, int offset, net::IOBuffer* buf, int buf_len, |
| 39 const net::CompletionCallback& callback) OVERRIDE; |
38 virtual int WriteData(int index, int offset, net::IOBuffer* buf, int buf_len, | 40 virtual int WriteData(int index, int offset, net::IOBuffer* buf, int buf_len, |
39 net::OldCompletionCallback* callback, | 41 net::OldCompletionCallback* callback, |
40 bool truncate) OVERRIDE; | 42 bool truncate) OVERRIDE; |
| 43 virtual int WriteData(int index, int offset, net::IOBuffer* buf, int buf_len, |
| 44 const net::CompletionCallback& callback, |
| 45 bool truncate) OVERRIDE; |
41 virtual int ReadSparseData(int64 offset, net::IOBuffer* buf, int buf_len, | 46 virtual int ReadSparseData(int64 offset, net::IOBuffer* buf, int buf_len, |
42 net::OldCompletionCallback* callback) OVERRIDE; | 47 net::OldCompletionCallback* callback) OVERRIDE; |
43 virtual int WriteSparseData(int64 offset, net::IOBuffer* buf, int buf_len, | 48 virtual int WriteSparseData(int64 offset, net::IOBuffer* buf, int buf_len, |
44 net::OldCompletionCallback* callback) OVERRIDE; | 49 net::OldCompletionCallback* callback) OVERRIDE; |
45 virtual int GetAvailableRange(int64 offset, int len, int64* start, | 50 virtual int GetAvailableRange(int64 offset, int len, int64* start, |
46 net::OldCompletionCallback* callback) OVERRIDE; | 51 net::OldCompletionCallback* callback) OVERRIDE; |
47 virtual bool CouldBeSparse() const OVERRIDE; | 52 virtual bool CouldBeSparse() const OVERRIDE; |
48 virtual void CancelSparseIO() OVERRIDE; | 53 virtual void CancelSparseIO() OVERRIDE; |
49 virtual int ReadyForSparseIO( | 54 virtual int ReadyForSparseIO( |
50 net::OldCompletionCallback* completion_callback) OVERRIDE; | 55 net::OldCompletionCallback* completion_callback) OVERRIDE; |
51 | 56 |
52 // Fail most subsequent requests. | 57 // Fail most subsequent requests. |
53 void set_fail_requests() { fail_requests_ = true; } | 58 void set_fail_requests() { fail_requests_ = true; } |
54 | 59 |
55 // If |value| is true, don't deliver any completion callbacks until called | 60 // If |value| is true, don't deliver any completion callbacks until called |
56 // again with |value| set to false. Caution: remember to enable callbacks | 61 // again with |value| set to false. Caution: remember to enable callbacks |
57 // again or all subsequent tests will fail. | 62 // again or all subsequent tests will fail. |
58 static void IgnoreCallbacks(bool value); | 63 static void IgnoreCallbacks(bool value); |
59 | 64 |
60 private: | 65 private: |
61 friend class base::RefCounted<MockDiskEntry>; | 66 friend class base::RefCounted<MockDiskEntry>; |
62 struct CallbackInfo; | 67 struct CallbackInfo; |
63 | 68 |
64 virtual ~MockDiskEntry(); | 69 virtual ~MockDiskEntry(); |
65 | 70 |
66 // Unlike the callbacks for MockHttpTransaction, we want this one to run even | 71 // Unlike the callbacks for MockHttpTransaction, we want this one to run even |
67 // if the consumer called Close on the MockDiskEntry. We achieve that by | 72 // if the consumer called Close on the MockDiskEntry. We achieve that by |
68 // leveraging the fact that this class is reference counted. | 73 // leveraging the fact that this class is reference counted. |
69 void CallbackLater(net::OldCompletionCallback* callback, int result); | 74 void CallbackLater(net::OldCompletionCallback* callback, int result); |
| 75 void CallbackLater(const net::CompletionCallback& callback, int result); |
70 | 76 |
71 void RunCallback(net::OldCompletionCallback* callback, int result); | 77 void RunOldCallback(net::OldCompletionCallback* callback, int result); |
| 78 void RunCallback(const net::CompletionCallback& callback, int result); |
72 | 79 |
73 // When |store| is true, stores the callback to be delivered later; otherwise | 80 // When |store| is true, stores the callback to be delivered later; otherwise |
74 // delivers any callback previously stored. | 81 // delivers any callback previously stored. |
75 static void StoreAndDeliverCallbacks(bool store, MockDiskEntry* entry, | 82 static void StoreAndDeliverCallbacks(bool store, MockDiskEntry* entry, |
76 net::OldCompletionCallback* callback, | 83 net::OldCompletionCallback* old_callback, |
| 84 const net::CompletionCallback& callback, |
77 int result); | 85 int result); |
78 | 86 |
79 static const int kNumCacheEntryDataIndices = 3; | 87 static const int kNumCacheEntryDataIndices = 3; |
80 | 88 |
81 std::string key_; | 89 std::string key_; |
82 std::vector<char> data_[kNumCacheEntryDataIndices]; | 90 std::vector<char> data_[kNumCacheEntryDataIndices]; |
83 int test_mode_; | 91 int test_mode_; |
84 bool doomed_; | 92 bool doomed_; |
85 bool sparse_; | 93 bool sparse_; |
86 bool fail_requests_; | 94 bool fail_requests_; |
87 bool busy_; | 95 bool busy_; |
88 bool delayed_; | 96 bool delayed_; |
89 static bool cancel_; | 97 static bool cancel_; |
90 static bool ignore_callbacks_; | 98 static bool ignore_callbacks_; |
91 }; | 99 }; |
92 | 100 |
93 class MockDiskCache : public disk_cache::Backend { | 101 class MockDiskCache : public disk_cache::Backend { |
94 public: | 102 public: |
95 MockDiskCache(); | 103 MockDiskCache(); |
96 virtual ~MockDiskCache(); | 104 virtual ~MockDiskCache(); |
97 | 105 |
98 virtual int32 GetEntryCount() const OVERRIDE; | 106 virtual int32 GetEntryCount() const OVERRIDE; |
99 virtual int OpenEntry(const std::string& key, disk_cache::Entry** entry, | 107 virtual int OpenEntry(const std::string& key, disk_cache::Entry** entry, |
100 net::OldCompletionCallback* callback) OVERRIDE; | 108 net::OldCompletionCallback* callback) OVERRIDE; |
| 109 virtual int OpenEntry(const std::string& key, disk_cache::Entry** entry, |
| 110 const net::CompletionCallback& callback) OVERRIDE; |
101 virtual int CreateEntry(const std::string& key, disk_cache::Entry** entry, | 111 virtual int CreateEntry(const std::string& key, disk_cache::Entry** entry, |
102 net::OldCompletionCallback* callback) OVERRIDE; | 112 net::OldCompletionCallback* callback) OVERRIDE; |
| 113 virtual int CreateEntry(const std::string& key, disk_cache::Entry** entry, |
| 114 const net::CompletionCallback& callback) OVERRIDE; |
103 virtual int DoomEntry(const std::string& key, | 115 virtual int DoomEntry(const std::string& key, |
104 net::OldCompletionCallback* callback) OVERRIDE; | 116 net::OldCompletionCallback* callback) OVERRIDE; |
105 virtual int DoomAllEntries(net::OldCompletionCallback* callback) OVERRIDE; | 117 virtual int DoomAllEntries(net::OldCompletionCallback* callback) OVERRIDE; |
106 virtual int DoomEntriesBetween(const base::Time initial_time, | 118 virtual int DoomEntriesBetween(const base::Time initial_time, |
107 const base::Time end_time, | 119 const base::Time end_time, |
108 net::OldCompletionCallback* callback) OVERRIDE; | 120 net::OldCompletionCallback* callback) OVERRIDE; |
109 virtual int DoomEntriesSince(const base::Time initial_time, | 121 virtual int DoomEntriesSince(const base::Time initial_time, |
110 net::OldCompletionCallback* callback) OVERRIDE; | 122 net::OldCompletionCallback* callback) OVERRIDE; |
111 virtual int OpenNextEntry(void** iter, disk_cache::Entry** next_entry, | 123 virtual int OpenNextEntry(void** iter, disk_cache::Entry** next_entry, |
112 net::OldCompletionCallback* callback) OVERRIDE; | 124 net::OldCompletionCallback* callback) OVERRIDE; |
(...skipping 14 matching lines...) Expand all Loading... |
127 // Return entries that fail some of their requests. | 139 // Return entries that fail some of their requests. |
128 void set_soft_failures(bool value) { soft_failures_ = value; } | 140 void set_soft_failures(bool value) { soft_failures_ = value; } |
129 | 141 |
130 // Makes sure that CreateEntry is not called twice for a given key. | 142 // Makes sure that CreateEntry is not called twice for a given key. |
131 void set_double_create_check(bool value) { double_create_check_ = value; } | 143 void set_double_create_check(bool value) { double_create_check_ = value; } |
132 | 144 |
133 void ReleaseAll(); | 145 void ReleaseAll(); |
134 | 146 |
135 private: | 147 private: |
136 typedef base::hash_map<std::string, MockDiskEntry*> EntryMap; | 148 typedef base::hash_map<std::string, MockDiskEntry*> EntryMap; |
137 class CallbackRunner; | |
138 | 149 |
139 void CallbackLater(net::OldCompletionCallback* callback, int result); | 150 void CallbackLater(net::OldCompletionCallback* callback, int result); |
| 151 void CallbackLater(const net::CompletionCallback& callback, int result); |
140 | 152 |
141 EntryMap entries_; | 153 EntryMap entries_; |
142 int open_count_; | 154 int open_count_; |
143 int create_count_; | 155 int create_count_; |
144 bool fail_requests_; | 156 bool fail_requests_; |
145 bool soft_failures_; | 157 bool soft_failures_; |
146 bool double_create_check_; | 158 bool double_create_check_; |
147 }; | 159 }; |
148 | 160 |
149 class MockBackendFactory : public net::HttpCache::BackendFactory { | 161 class MockBackendFactory : public net::HttpCache::BackendFactory { |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 private: | 240 private: |
229 int Result() { return fail_ ? net::ERR_FAILED : net::OK; } | 241 int Result() { return fail_ ? net::ERR_FAILED : net::OK; } |
230 | 242 |
231 disk_cache::Backend** backend_; | 243 disk_cache::Backend** backend_; |
232 net::OldCompletionCallback* callback_; | 244 net::OldCompletionCallback* callback_; |
233 bool block_; | 245 bool block_; |
234 bool fail_; | 246 bool fail_; |
235 }; | 247 }; |
236 | 248 |
237 #endif // NET_HTTP_MOCK_HTTP_CACHE_H_ | 249 #endif // NET_HTTP_MOCK_HTTP_CACHE_H_ |
OLD | NEW |