| 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_ |
| 11 #define NET_HTTP_MOCK_HTTP_CACHE_H_ | 11 #define NET_HTTP_MOCK_HTTP_CACHE_H_ |
| 12 | 12 |
| 13 #include "base/containers/hash_tables.h" | 13 #include "base/containers/hash_tables.h" |
| 14 #include "net/disk_cache/disk_cache.h" | 14 #include "net/disk_cache/disk_cache.h" |
| 15 #include "net/http/http_cache.h" | 15 #include "net/http/http_cache.h" |
| 16 #include "net/http/http_transaction_unittest.h" | 16 #include "net/http/http_transaction_unittest.h" |
| 17 | 17 |
| 18 //----------------------------------------------------------------------------- | 18 //----------------------------------------------------------------------------- |
| 19 // Mock disk cache (a very basic memory cache implementation). | 19 // Mock disk cache (a very basic memory cache implementation). |
| 20 | 20 |
| 21 class MockDiskEntry : public disk_cache::Entry, | 21 class MockDiskEntry : public disk_cache::Entry, |
| 22 public base::RefCounted<MockDiskEntry> { | 22 public base::RefCounted<MockDiskEntry> { |
| 23 public: | 23 public: |
| 24 MockDiskEntry(); | |
| 25 explicit MockDiskEntry(const std::string& key); | 24 explicit MockDiskEntry(const std::string& key); |
| 26 | 25 |
| 27 bool is_doomed() const { return doomed_; } | 26 bool is_doomed() const { return doomed_; } |
| 28 | 27 |
| 29 virtual void Doom() OVERRIDE; | 28 virtual void Doom() OVERRIDE; |
| 30 virtual void Close() OVERRIDE; | 29 virtual void Close() OVERRIDE; |
| 31 virtual std::string GetKey() const OVERRIDE; | 30 virtual std::string GetKey() const OVERRIDE; |
| 32 virtual base::Time GetLastUsed() const OVERRIDE; | 31 virtual base::Time GetLastUsed() const OVERRIDE; |
| 33 virtual base::Time GetLastModified() const OVERRIDE; | 32 virtual base::Time GetLastModified() const OVERRIDE; |
| 34 virtual int32 GetDataSize(int index) const OVERRIDE; | 33 virtual int32 GetDataSize(int index) const OVERRIDE; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 46 int64 offset, int len, int64* start, | 45 int64 offset, int len, int64* start, |
| 47 const net::CompletionCallback& callback) OVERRIDE; | 46 const net::CompletionCallback& callback) OVERRIDE; |
| 48 virtual bool CouldBeSparse() const OVERRIDE; | 47 virtual bool CouldBeSparse() const OVERRIDE; |
| 49 virtual void CancelSparseIO() OVERRIDE; | 48 virtual void CancelSparseIO() OVERRIDE; |
| 50 virtual int ReadyForSparseIO( | 49 virtual int ReadyForSparseIO( |
| 51 const net::CompletionCallback& completion_callback) OVERRIDE; | 50 const net::CompletionCallback& completion_callback) OVERRIDE; |
| 52 | 51 |
| 53 // Fail most subsequent requests. | 52 // Fail most subsequent requests. |
| 54 void set_fail_requests() { fail_requests_ = true; } | 53 void set_fail_requests() { fail_requests_ = true; } |
| 55 | 54 |
| 55 void set_fail_sparse_requests() { fail_sparse_requests_ = true; } |
| 56 |
| 56 // If |value| is true, don't deliver any completion callbacks until called | 57 // If |value| is true, don't deliver any completion callbacks until called |
| 57 // again with |value| set to false. Caution: remember to enable callbacks | 58 // again with |value| set to false. Caution: remember to enable callbacks |
| 58 // again or all subsequent tests will fail. | 59 // again or all subsequent tests will fail. |
| 59 static void IgnoreCallbacks(bool value); | 60 static void IgnoreCallbacks(bool value); |
| 60 | 61 |
| 61 private: | 62 private: |
| 62 friend class base::RefCounted<MockDiskEntry>; | 63 friend class base::RefCounted<MockDiskEntry>; |
| 63 struct CallbackInfo; | 64 struct CallbackInfo; |
| 64 | 65 |
| 65 virtual ~MockDiskEntry(); | 66 virtual ~MockDiskEntry(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 78 int result); | 79 int result); |
| 79 | 80 |
| 80 static const int kNumCacheEntryDataIndices = 3; | 81 static const int kNumCacheEntryDataIndices = 3; |
| 81 | 82 |
| 82 std::string key_; | 83 std::string key_; |
| 83 std::vector<char> data_[kNumCacheEntryDataIndices]; | 84 std::vector<char> data_[kNumCacheEntryDataIndices]; |
| 84 int test_mode_; | 85 int test_mode_; |
| 85 bool doomed_; | 86 bool doomed_; |
| 86 bool sparse_; | 87 bool sparse_; |
| 87 bool fail_requests_; | 88 bool fail_requests_; |
| 89 bool fail_sparse_requests_; |
| 88 bool busy_; | 90 bool busy_; |
| 89 bool delayed_; | 91 bool delayed_; |
| 90 static bool cancel_; | 92 static bool cancel_; |
| 91 static bool ignore_callbacks_; | 93 static bool ignore_callbacks_; |
| 92 }; | 94 }; |
| 93 | 95 |
| 94 class MockDiskCache : public disk_cache::Backend { | 96 class MockDiskCache : public disk_cache::Backend { |
| 95 public: | 97 public: |
| 96 MockDiskCache(); | 98 MockDiskCache(); |
| 97 virtual ~MockDiskCache(); | 99 virtual ~MockDiskCache(); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 127 | 129 |
| 128 // Fail any subsequent CreateEntry and OpenEntry. | 130 // Fail any subsequent CreateEntry and OpenEntry. |
| 129 void set_fail_requests() { fail_requests_ = true; } | 131 void set_fail_requests() { fail_requests_ = true; } |
| 130 | 132 |
| 131 // Return entries that fail some of their requests. | 133 // Return entries that fail some of their requests. |
| 132 void set_soft_failures(bool value) { soft_failures_ = value; } | 134 void set_soft_failures(bool value) { soft_failures_ = value; } |
| 133 | 135 |
| 134 // Makes sure that CreateEntry is not called twice for a given key. | 136 // Makes sure that CreateEntry is not called twice for a given key. |
| 135 void set_double_create_check(bool value) { double_create_check_ = value; } | 137 void set_double_create_check(bool value) { double_create_check_ = value; } |
| 136 | 138 |
| 139 // Makes all requests for data ranges to fail as not implemented. |
| 140 void set_fail_sparse_requests() { fail_sparse_requests_ = true; } |
| 141 |
| 137 void ReleaseAll(); | 142 void ReleaseAll(); |
| 138 | 143 |
| 139 private: | 144 private: |
| 140 typedef base::hash_map<std::string, MockDiskEntry*> EntryMap; | 145 typedef base::hash_map<std::string, MockDiskEntry*> EntryMap; |
| 141 | 146 |
| 142 void CallbackLater(const net::CompletionCallback& callback, int result); | 147 void CallbackLater(const net::CompletionCallback& callback, int result); |
| 143 | 148 |
| 144 EntryMap entries_; | 149 EntryMap entries_; |
| 145 int open_count_; | 150 int open_count_; |
| 146 int create_count_; | 151 int create_count_; |
| 147 bool fail_requests_; | 152 bool fail_requests_; |
| 148 bool soft_failures_; | 153 bool soft_failures_; |
| 149 bool double_create_check_; | 154 bool double_create_check_; |
| 155 bool fail_sparse_requests_; |
| 150 }; | 156 }; |
| 151 | 157 |
| 152 class MockBackendFactory : public net::HttpCache::BackendFactory { | 158 class MockBackendFactory : public net::HttpCache::BackendFactory { |
| 153 public: | 159 public: |
| 154 virtual int CreateBackend(net::NetLog* net_log, | 160 virtual int CreateBackend(net::NetLog* net_log, |
| 155 scoped_ptr<disk_cache::Backend>* backend, | 161 scoped_ptr<disk_cache::Backend>* backend, |
| 156 const net::CompletionCallback& callback) OVERRIDE; | 162 const net::CompletionCallback& callback) OVERRIDE; |
| 157 }; | 163 }; |
| 158 | 164 |
| 159 class MockHttpCache { | 165 class MockHttpCache { |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 private: | 238 private: |
| 233 int Result() { return fail_ ? net::ERR_FAILED : net::OK; } | 239 int Result() { return fail_ ? net::ERR_FAILED : net::OK; } |
| 234 | 240 |
| 235 scoped_ptr<disk_cache::Backend>* backend_; | 241 scoped_ptr<disk_cache::Backend>* backend_; |
| 236 net::CompletionCallback callback_; | 242 net::CompletionCallback callback_; |
| 237 bool block_; | 243 bool block_; |
| 238 bool fail_; | 244 bool fail_; |
| 239 }; | 245 }; |
| 240 | 246 |
| 241 #endif // NET_HTTP_MOCK_HTTP_CACHE_H_ | 247 #endif // NET_HTTP_MOCK_HTTP_CACHE_H_ |
| OLD | NEW |