Use multiline attribute to check for IA2_STATE_MULTILINE.
[chromium-blink-merge.git] / content / browser / appcache / mock_appcache_storage.h
blob7803ff7cc5ad7c9f4b52a051cab06aa35ee9eab0
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 AppCacheResponseMetadataWriter* CreateResponseMetadataWriter(
76 int64 group_id,
77 int64 response_id) override;
78 void DoomResponses(const GURL& manifest_url,
79 const std::vector<int64>& response_ids) override;
80 void DeleteResponses(const GURL& manifest_url,
81 const std::vector<int64>& response_ids) override;
83 private:
84 friend class AppCacheRequestHandlerTest;
85 friend class AppCacheServiceImplTest;
86 friend class AppCacheUpdateJobTest;
87 friend class MockAppCacheStorageTest;
89 typedef base::hash_map<int64, scoped_refptr<AppCache> > StoredCacheMap;
90 typedef std::map<GURL, scoped_refptr<AppCacheGroup> > StoredGroupMap;
91 typedef std::set<int64> DoomedResponseIds;
93 void ProcessGetAllInfo(scoped_refptr<DelegateReference> delegate_ref);
94 void ProcessLoadCache(
95 int64 id, scoped_refptr<DelegateReference> delegate_ref);
96 void ProcessLoadOrCreateGroup(
97 const GURL& manifest_url, scoped_refptr<DelegateReference> delegate_ref);
98 void ProcessStoreGroupAndNewestCache(
99 scoped_refptr<AppCacheGroup> group, scoped_refptr<AppCache> newest_cache,
100 scoped_refptr<DelegateReference> delegate_ref);
101 void ProcessMakeGroupObsolete(scoped_refptr<AppCacheGroup> group,
102 scoped_refptr<DelegateReference> delegate_ref,
103 int response_code);
104 void ProcessFindResponseForMainRequest(
105 const GURL& url, scoped_refptr<DelegateReference> delegate_ref);
107 void ScheduleTask(const base::Closure& task);
108 void RunOnePendingTask();
110 void AddStoredCache(AppCache* cache);
111 void RemoveStoredCache(AppCache* cache);
112 void RemoveStoredCaches(const AppCacheGroup::Caches& caches);
113 bool IsCacheStored(const AppCache* cache) {
114 return stored_caches_.find(cache->cache_id()) != stored_caches_.end();
117 void AddStoredGroup(AppCacheGroup* group);
118 void RemoveStoredGroup(AppCacheGroup* group);
119 bool IsGroupStored(const AppCacheGroup* group) {
120 return IsGroupForManifestStored(group->manifest_url());
122 bool IsGroupForManifestStored(const GURL& manifest_url) {
123 return stored_groups_.find(manifest_url) != stored_groups_.end();
126 // These helpers determine when certain operations should complete
127 // asynchronously vs synchronously to faithfully mimic, or mock,
128 // the behavior of the real implemenation of the AppCacheStorage
129 // interface.
130 bool ShouldGroupLoadAppearAsync(const AppCacheGroup* group);
131 bool ShouldCacheLoadAppearAsync(const AppCache* cache);
133 // Lazily constructed in-memory disk cache.
134 AppCacheDiskCache* disk_cache() {
135 if (!disk_cache_) {
136 const int kMaxCacheSize = 10 * 1024 * 1024;
137 disk_cache_.reset(new AppCacheDiskCache);
138 disk_cache_->InitWithMemBackend(kMaxCacheSize, net::CompletionCallback());
140 return disk_cache_.get();
143 // Simulate failures for testing. Once set all subsequent calls
144 // to MakeGroupObsolete or StorageGroupAndNewestCache will fail.
145 void SimulateMakeGroupObsoleteFailure() {
146 simulate_make_group_obsolete_failure_ = true;
148 void SimulateStoreGroupAndNewestCacheFailure() {
149 simulate_store_group_and_newest_cache_failure_ = true;
152 // Simulate FindResponseFor results for testing. These
153 // provided values will be return on the next call to
154 // the corresponding Find method, subsequent calls are
155 // unaffected.
156 void SimulateFindMainResource(
157 const AppCacheEntry& entry,
158 const GURL& fallback_url,
159 const AppCacheEntry& fallback_entry,
160 int64 cache_id,
161 int64 group_id,
162 const GURL& manifest_url) {
163 simulate_find_main_resource_ = true;
164 simulate_find_sub_resource_ = false;
165 simulated_found_entry_ = entry;
166 simulated_found_fallback_url_ = fallback_url;
167 simulated_found_fallback_entry_ = fallback_entry;
168 simulated_found_cache_id_ = cache_id;
169 simulated_found_group_id_ = group_id;
170 simulated_found_manifest_url_ = manifest_url,
171 simulated_found_network_namespace_ = false; // N/A to main resource loads
173 void SimulateFindSubResource(
174 const AppCacheEntry& entry,
175 const AppCacheEntry& fallback_entry,
176 bool network_namespace) {
177 simulate_find_main_resource_ = false;
178 simulate_find_sub_resource_ = true;
179 simulated_found_entry_ = entry;
180 simulated_found_fallback_entry_ = fallback_entry;
181 simulated_found_cache_id_ = kAppCacheNoCacheId; // N/A to sub resource loads
182 simulated_found_manifest_url_ = GURL(); // N/A to sub resource loads
183 simulated_found_group_id_ = 0; // N/A to sub resource loads
184 simulated_found_network_namespace_ = network_namespace;
187 void SimulateGetAllInfo(AppCacheInfoCollection* info) {
188 simulated_appcache_info_ = info;
191 void SimulateResponseReader(AppCacheResponseReader* reader) {
192 simulated_reader_.reset(reader);
195 StoredCacheMap stored_caches_;
196 StoredGroupMap stored_groups_;
197 DoomedResponseIds doomed_response_ids_;
198 scoped_ptr<AppCacheDiskCache> disk_cache_;
199 std::deque<base::Closure> pending_tasks_;
201 bool simulate_make_group_obsolete_failure_;
202 bool simulate_store_group_and_newest_cache_failure_;
204 bool simulate_find_main_resource_;
205 bool simulate_find_sub_resource_;
206 AppCacheEntry simulated_found_entry_;
207 AppCacheEntry simulated_found_fallback_entry_;
208 int64 simulated_found_cache_id_;
209 int64 simulated_found_group_id_;
210 GURL simulated_found_fallback_url_;
211 GURL simulated_found_manifest_url_;
212 bool simulated_found_network_namespace_;
213 scoped_refptr<AppCacheInfoCollection> simulated_appcache_info_;
214 scoped_ptr<AppCacheResponseReader> simulated_reader_;
216 base::WeakPtrFactory<MockAppCacheStorage> weak_factory_;
218 FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest,
219 BasicFindMainResponse);
220 FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest,
221 BasicFindMainFallbackResponse);
222 FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest, CreateGroup);
223 FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest,
224 FindMainResponseExclusions);
225 FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest,
226 FindMainResponseWithMultipleCandidates);
227 FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest, LoadCache_FarHit);
228 FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest,
229 LoadGroupAndCache_FarHit);
230 FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest, MakeGroupObsolete);
231 FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest, StoreNewGroup);
232 FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest,
233 StoreExistingGroup);
234 FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest,
235 StoreExistingGroupExistingCache);
236 FRIEND_TEST_ALL_PREFIXES(AppCacheServiceImplTest,
237 DeleteAppCachesForOrigin);
239 DISALLOW_COPY_AND_ASSIGN(MockAppCacheStorage);
242 } // namespace content
244 #endif // CONTENT_BROWSER_APPCACHE_MOCK_APPCACHE_STORAGE_H_