[Storage] Blob Storage Refactoring pt 1:
[chromium-blink-merge.git] / content / browser / appcache / mock_appcache_storage.h
bloba6ece438ef1f1eba664227be955be4a784e4bffe
1 // Copyright 2014 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 CONTENT_BROWSER_APPCACHE_MOCK_APPCACHE_STORAGE_H_
6 #define CONTENT_BROWSER_APPCACHE_MOCK_APPCACHE_STORAGE_H_
8 #include <deque>
9 #include <map>
10 #include <vector>
12 #include "base/callback.h"
13 #include "base/containers/hash_tables.h"
14 #include "base/gtest_prod_util.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "content/browser/appcache/appcache.h"
18 #include "content/browser/appcache/appcache_disk_cache.h"
19 #include "content/browser/appcache/appcache_group.h"
20 #include "content/browser/appcache/appcache_response.h"
21 #include "content/browser/appcache/appcache_storage.h"
23 namespace content {
24 FORWARD_DECLARE_TEST(AppCacheServiceImplTest, DeleteAppCachesForOrigin);
25 FORWARD_DECLARE_TEST(MockAppCacheStorageTest, BasicFindMainResponse);
26 FORWARD_DECLARE_TEST(MockAppCacheStorageTest,
27 BasicFindMainFallbackResponse);
28 FORWARD_DECLARE_TEST(MockAppCacheStorageTest, CreateGroup);
29 FORWARD_DECLARE_TEST(MockAppCacheStorageTest, FindMainResponseExclusions);
30 FORWARD_DECLARE_TEST(MockAppCacheStorageTest,
31 FindMainResponseWithMultipleCandidates);
32 FORWARD_DECLARE_TEST(MockAppCacheStorageTest, LoadCache_FarHit);
33 FORWARD_DECLARE_TEST(MockAppCacheStorageTest, LoadGroupAndCache_FarHit);
34 FORWARD_DECLARE_TEST(MockAppCacheStorageTest, MakeGroupObsolete);
35 FORWARD_DECLARE_TEST(MockAppCacheStorageTest, StoreNewGroup);
36 FORWARD_DECLARE_TEST(MockAppCacheStorageTest, StoreExistingGroup);
37 FORWARD_DECLARE_TEST(MockAppCacheStorageTest,
38 StoreExistingGroupExistingCache);
39 class AppCacheRequestHandlerTest;
40 class AppCacheServiceImplTest;
41 class MockAppCacheStorageTest;
43 // For use in unit tests.
44 // Note: This class is also being used to bootstrap our development efforts.
45 // We can get layout tests up and running, and back fill with real storage
46 // somewhat in parallel.
47 class MockAppCacheStorage : public AppCacheStorage {
48 public:
49 explicit MockAppCacheStorage(AppCacheServiceImpl* service);
50 ~MockAppCacheStorage() override;
52 void GetAllInfo(Delegate* delegate) override;
53 void LoadCache(int64 id, Delegate* delegate) override;
54 void LoadOrCreateGroup(const GURL& manifest_url, Delegate* delegate) override;
55 void StoreGroupAndNewestCache(AppCacheGroup* group,
56 AppCache* newest_cache,
57 Delegate* delegate) override;
58 void FindResponseForMainRequest(const GURL& url,
59 const GURL& preferred_manifest_url,
60 Delegate* delegate) override;
61 void FindResponseForSubRequest(AppCache* cache,
62 const GURL& url,
63 AppCacheEntry* found_entry,
64 AppCacheEntry* found_fallback_entry,
65 bool* found_network_namespace) override;
66 void MarkEntryAsForeign(const GURL& entry_url, int64 cache_id) override;
67 void MakeGroupObsolete(AppCacheGroup* group,
68 Delegate* delegate,
69 int response_code) override;
70 AppCacheResponseReader* CreateResponseReader(const GURL& manifest_url,
71 int64 group_id,
72 int64 response_id) override;
73 AppCacheResponseWriter* CreateResponseWriter(const GURL& manifest_url,
74 int64 group_id) override;
75 void DoomResponses(const GURL& manifest_url,
76 const std::vector<int64>& response_ids) override;
77 void DeleteResponses(const GURL& manifest_url,
78 const std::vector<int64>& response_ids) override;
80 private:
81 friend class AppCacheRequestHandlerTest;
82 friend class AppCacheServiceImplTest;
83 friend class AppCacheUpdateJobTest;
84 friend class MockAppCacheStorageTest;
86 typedef base::hash_map<int64, scoped_refptr<AppCache> > StoredCacheMap;
87 typedef std::map<GURL, scoped_refptr<AppCacheGroup> > StoredGroupMap;
88 typedef std::set<int64> DoomedResponseIds;
90 void ProcessGetAllInfo(scoped_refptr<DelegateReference> delegate_ref);
91 void ProcessLoadCache(
92 int64 id, scoped_refptr<DelegateReference> delegate_ref);
93 void ProcessLoadOrCreateGroup(
94 const GURL& manifest_url, scoped_refptr<DelegateReference> delegate_ref);
95 void ProcessStoreGroupAndNewestCache(
96 scoped_refptr<AppCacheGroup> group, scoped_refptr<AppCache> newest_cache,
97 scoped_refptr<DelegateReference> delegate_ref);
98 void ProcessMakeGroupObsolete(scoped_refptr<AppCacheGroup> group,
99 scoped_refptr<DelegateReference> delegate_ref,
100 int response_code);
101 void ProcessFindResponseForMainRequest(
102 const GURL& url, scoped_refptr<DelegateReference> delegate_ref);
104 void ScheduleTask(const base::Closure& task);
105 void RunOnePendingTask();
107 void AddStoredCache(AppCache* cache);
108 void RemoveStoredCache(AppCache* cache);
109 void RemoveStoredCaches(const AppCacheGroup::Caches& caches);
110 bool IsCacheStored(const AppCache* cache) {
111 return stored_caches_.find(cache->cache_id()) != stored_caches_.end();
114 void AddStoredGroup(AppCacheGroup* group);
115 void RemoveStoredGroup(AppCacheGroup* group);
116 bool IsGroupStored(const AppCacheGroup* group) {
117 return IsGroupForManifestStored(group->manifest_url());
119 bool IsGroupForManifestStored(const GURL& manifest_url) {
120 return stored_groups_.find(manifest_url) != stored_groups_.end();
123 // These helpers determine when certain operations should complete
124 // asynchronously vs synchronously to faithfully mimic, or mock,
125 // the behavior of the real implemenation of the AppCacheStorage
126 // interface.
127 bool ShouldGroupLoadAppearAsync(const AppCacheGroup* group);
128 bool ShouldCacheLoadAppearAsync(const AppCache* cache);
130 // Lazily constructed in-memory disk cache.
131 AppCacheDiskCache* disk_cache() {
132 if (!disk_cache_) {
133 const int kMaxCacheSize = 10 * 1024 * 1024;
134 disk_cache_.reset(new AppCacheDiskCache);
135 disk_cache_->InitWithMemBackend(kMaxCacheSize, net::CompletionCallback());
137 return disk_cache_.get();
140 // Simulate failures for testing. Once set all subsequent calls
141 // to MakeGroupObsolete or StorageGroupAndNewestCache will fail.
142 void SimulateMakeGroupObsoleteFailure() {
143 simulate_make_group_obsolete_failure_ = true;
145 void SimulateStoreGroupAndNewestCacheFailure() {
146 simulate_store_group_and_newest_cache_failure_ = true;
149 // Simulate FindResponseFor results for testing. These
150 // provided values will be return on the next call to
151 // the corresponding Find method, subsequent calls are
152 // unaffected.
153 void SimulateFindMainResource(
154 const AppCacheEntry& entry,
155 const GURL& fallback_url,
156 const AppCacheEntry& fallback_entry,
157 int64 cache_id,
158 int64 group_id,
159 const GURL& manifest_url) {
160 simulate_find_main_resource_ = true;
161 simulate_find_sub_resource_ = false;
162 simulated_found_entry_ = entry;
163 simulated_found_fallback_url_ = fallback_url;
164 simulated_found_fallback_entry_ = fallback_entry;
165 simulated_found_cache_id_ = cache_id;
166 simulated_found_group_id_ = group_id;
167 simulated_found_manifest_url_ = manifest_url,
168 simulated_found_network_namespace_ = false; // N/A to main resource loads
170 void SimulateFindSubResource(
171 const AppCacheEntry& entry,
172 const AppCacheEntry& fallback_entry,
173 bool network_namespace) {
174 simulate_find_main_resource_ = false;
175 simulate_find_sub_resource_ = true;
176 simulated_found_entry_ = entry;
177 simulated_found_fallback_entry_ = fallback_entry;
178 simulated_found_cache_id_ = kAppCacheNoCacheId; // N/A to sub resource loads
179 simulated_found_manifest_url_ = GURL(); // N/A to sub resource loads
180 simulated_found_group_id_ = 0; // N/A to sub resource loads
181 simulated_found_network_namespace_ = network_namespace;
184 void SimulateGetAllInfo(AppCacheInfoCollection* info) {
185 simulated_appcache_info_ = info;
188 void SimulateResponseReader(AppCacheResponseReader* reader) {
189 simulated_reader_.reset(reader);
192 StoredCacheMap stored_caches_;
193 StoredGroupMap stored_groups_;
194 DoomedResponseIds doomed_response_ids_;
195 scoped_ptr<AppCacheDiskCache> disk_cache_;
196 std::deque<base::Closure> pending_tasks_;
198 bool simulate_make_group_obsolete_failure_;
199 bool simulate_store_group_and_newest_cache_failure_;
201 bool simulate_find_main_resource_;
202 bool simulate_find_sub_resource_;
203 AppCacheEntry simulated_found_entry_;
204 AppCacheEntry simulated_found_fallback_entry_;
205 int64 simulated_found_cache_id_;
206 int64 simulated_found_group_id_;
207 GURL simulated_found_fallback_url_;
208 GURL simulated_found_manifest_url_;
209 bool simulated_found_network_namespace_;
210 scoped_refptr<AppCacheInfoCollection> simulated_appcache_info_;
211 scoped_ptr<AppCacheResponseReader> simulated_reader_;
213 base::WeakPtrFactory<MockAppCacheStorage> weak_factory_;
215 FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest,
216 BasicFindMainResponse);
217 FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest,
218 BasicFindMainFallbackResponse);
219 FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest, CreateGroup);
220 FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest,
221 FindMainResponseExclusions);
222 FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest,
223 FindMainResponseWithMultipleCandidates);
224 FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest, LoadCache_FarHit);
225 FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest,
226 LoadGroupAndCache_FarHit);
227 FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest, MakeGroupObsolete);
228 FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest, StoreNewGroup);
229 FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest,
230 StoreExistingGroup);
231 FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest,
232 StoreExistingGroupExistingCache);
233 FRIEND_TEST_ALL_PREFIXES(AppCacheServiceImplTest,
234 DeleteAppCachesForOrigin);
236 DISALLOW_COPY_AND_ASSIGN(MockAppCacheStorage);
239 } // namespace content
241 #endif // CONTENT_BROWSER_APPCACHE_MOCK_APPCACHE_STORAGE_H_