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_
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"
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
{
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
,
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
,
69 int response_code
) override
;
70 void StoreEvictionTimes(AppCacheGroup
* group
) override
;
71 AppCacheResponseReader
* CreateResponseReader(const GURL
& manifest_url
,
73 int64 response_id
) override
;
74 AppCacheResponseWriter
* CreateResponseWriter(const GURL
& manifest_url
,
75 int64 group_id
) override
;
76 AppCacheResponseMetadataWriter
* CreateResponseMetadataWriter(
78 int64 response_id
) override
;
79 void DoomResponses(const GURL
& manifest_url
,
80 const std::vector
<int64
>& response_ids
) override
;
81 void DeleteResponses(const GURL
& manifest_url
,
82 const std::vector
<int64
>& response_ids
) override
;
85 friend class AppCacheRequestHandlerTest
;
86 friend class AppCacheServiceImplTest
;
87 friend class AppCacheUpdateJobTest
;
88 friend class MockAppCacheStorageTest
;
90 typedef base::hash_map
<int64
, scoped_refptr
<AppCache
> > StoredCacheMap
;
91 typedef std::map
<GURL
, scoped_refptr
<AppCacheGroup
> > StoredGroupMap
;
92 typedef std::set
<int64
> DoomedResponseIds
;
93 typedef std::map
<int64
, std::pair
<base::Time
, base::Time
>>
94 StoredEvictionTimesMap
;
96 void ProcessGetAllInfo(scoped_refptr
<DelegateReference
> delegate_ref
);
97 void ProcessLoadCache(
98 int64 id
, scoped_refptr
<DelegateReference
> delegate_ref
);
99 void ProcessLoadOrCreateGroup(
100 const GURL
& manifest_url
, scoped_refptr
<DelegateReference
> delegate_ref
);
101 void ProcessStoreGroupAndNewestCache(
102 scoped_refptr
<AppCacheGroup
> group
, scoped_refptr
<AppCache
> newest_cache
,
103 scoped_refptr
<DelegateReference
> delegate_ref
);
104 void ProcessMakeGroupObsolete(scoped_refptr
<AppCacheGroup
> group
,
105 scoped_refptr
<DelegateReference
> delegate_ref
,
107 void ProcessFindResponseForMainRequest(
108 const GURL
& url
, scoped_refptr
<DelegateReference
> delegate_ref
);
110 void ScheduleTask(const base::Closure
& task
);
111 void RunOnePendingTask();
113 void AddStoredCache(AppCache
* cache
);
114 void RemoveStoredCache(AppCache
* cache
);
115 void RemoveStoredCaches(const AppCacheGroup::Caches
& caches
);
116 bool IsCacheStored(const AppCache
* cache
) {
117 return stored_caches_
.find(cache
->cache_id()) != stored_caches_
.end();
120 void AddStoredGroup(AppCacheGroup
* group
);
121 void RemoveStoredGroup(AppCacheGroup
* group
);
122 bool IsGroupStored(const AppCacheGroup
* group
) {
123 return IsGroupForManifestStored(group
->manifest_url());
125 bool IsGroupForManifestStored(const GURL
& manifest_url
) {
126 return stored_groups_
.find(manifest_url
) != stored_groups_
.end();
129 // These helpers determine when certain operations should complete
130 // asynchronously vs synchronously to faithfully mimic, or mock,
131 // the behavior of the real implemenation of the AppCacheStorage
133 bool ShouldGroupLoadAppearAsync(const AppCacheGroup
* group
);
134 bool ShouldCacheLoadAppearAsync(const AppCache
* cache
);
136 // Lazily constructed in-memory disk cache.
137 AppCacheDiskCache
* disk_cache() {
139 const int kMaxCacheSize
= 10 * 1024 * 1024;
140 disk_cache_
.reset(new AppCacheDiskCache
);
141 disk_cache_
->InitWithMemBackend(kMaxCacheSize
, net::CompletionCallback());
143 return disk_cache_
.get();
146 // Simulate failures for testing. Once set all subsequent calls
147 // to MakeGroupObsolete or StorageGroupAndNewestCache will fail.
148 void SimulateMakeGroupObsoleteFailure() {
149 simulate_make_group_obsolete_failure_
= true;
151 void SimulateStoreGroupAndNewestCacheFailure() {
152 simulate_store_group_and_newest_cache_failure_
= true;
155 // Simulate FindResponseFor results for testing. These
156 // provided values will be return on the next call to
157 // the corresponding Find method, subsequent calls are
159 void SimulateFindMainResource(
160 const AppCacheEntry
& entry
,
161 const GURL
& fallback_url
,
162 const AppCacheEntry
& fallback_entry
,
165 const GURL
& manifest_url
) {
166 simulate_find_main_resource_
= true;
167 simulate_find_sub_resource_
= false;
168 simulated_found_entry_
= entry
;
169 simulated_found_fallback_url_
= fallback_url
;
170 simulated_found_fallback_entry_
= fallback_entry
;
171 simulated_found_cache_id_
= cache_id
;
172 simulated_found_group_id_
= group_id
;
173 simulated_found_manifest_url_
= manifest_url
,
174 simulated_found_network_namespace_
= false; // N/A to main resource loads
176 void SimulateFindSubResource(
177 const AppCacheEntry
& entry
,
178 const AppCacheEntry
& fallback_entry
,
179 bool network_namespace
) {
180 simulate_find_main_resource_
= false;
181 simulate_find_sub_resource_
= true;
182 simulated_found_entry_
= entry
;
183 simulated_found_fallback_entry_
= fallback_entry
;
184 simulated_found_cache_id_
= kAppCacheNoCacheId
; // N/A to sub resource loads
185 simulated_found_manifest_url_
= GURL(); // N/A to sub resource loads
186 simulated_found_group_id_
= 0; // N/A to sub resource loads
187 simulated_found_network_namespace_
= network_namespace
;
190 void SimulateGetAllInfo(AppCacheInfoCollection
* info
) {
191 simulated_appcache_info_
= info
;
194 void SimulateResponseReader(AppCacheResponseReader
* reader
) {
195 simulated_reader_
.reset(reader
);
198 StoredCacheMap stored_caches_
;
199 StoredGroupMap stored_groups_
;
200 StoredEvictionTimesMap stored_eviction_times_
;
201 DoomedResponseIds doomed_response_ids_
;
202 scoped_ptr
<AppCacheDiskCache
> disk_cache_
;
203 std::deque
<base::Closure
> pending_tasks_
;
205 bool simulate_make_group_obsolete_failure_
;
206 bool simulate_store_group_and_newest_cache_failure_
;
208 bool simulate_find_main_resource_
;
209 bool simulate_find_sub_resource_
;
210 AppCacheEntry simulated_found_entry_
;
211 AppCacheEntry simulated_found_fallback_entry_
;
212 int64 simulated_found_cache_id_
;
213 int64 simulated_found_group_id_
;
214 GURL simulated_found_fallback_url_
;
215 GURL simulated_found_manifest_url_
;
216 bool simulated_found_network_namespace_
;
217 scoped_refptr
<AppCacheInfoCollection
> simulated_appcache_info_
;
218 scoped_ptr
<AppCacheResponseReader
> simulated_reader_
;
220 base::WeakPtrFactory
<MockAppCacheStorage
> weak_factory_
;
222 FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest
,
223 BasicFindMainResponse
);
224 FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest
,
225 BasicFindMainFallbackResponse
);
226 FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest
, CreateGroup
);
227 FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest
,
228 FindMainResponseExclusions
);
229 FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest
,
230 FindMainResponseWithMultipleCandidates
);
231 FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest
, LoadCache_FarHit
);
232 FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest
,
233 LoadGroupAndCache_FarHit
);
234 FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest
, MakeGroupObsolete
);
235 FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest
, StoreNewGroup
);
236 FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest
,
238 FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest
,
239 StoreExistingGroupExistingCache
);
240 FRIEND_TEST_ALL_PREFIXES(AppCacheServiceImplTest
,
241 DeleteAppCachesForOrigin
);
243 DISALLOW_COPY_AND_ASSIGN(MockAppCacheStorage
);
246 } // namespace content
248 #endif // CONTENT_BROWSER_APPCACHE_MOCK_APPCACHE_STORAGE_H_