Home | History | Annotate | Download | only in appcache
      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.
      4 
      5 #ifndef CONTENT_BROWSER_APPCACHE_MOCK_APPCACHE_STORAGE_H_
      6 #define CONTENT_BROWSER_APPCACHE_MOCK_APPCACHE_STORAGE_H_
      7 
      8 #include <deque>
      9 #include <map>
     10 #include <vector>
     11 
     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"
     22 
     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;
     42 
     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   virtual ~MockAppCacheStorage();
     51 
     52   virtual void GetAllInfo(Delegate* delegate) OVERRIDE;
     53   virtual void LoadCache(int64 id, Delegate* delegate) OVERRIDE;
     54   virtual void LoadOrCreateGroup(const GURL& manifest_url,
     55                                  Delegate* delegate) OVERRIDE;
     56   virtual void StoreGroupAndNewestCache(AppCacheGroup* group,
     57                                         AppCache* newest_cache,
     58                                         Delegate* delegate) OVERRIDE;
     59   virtual void FindResponseForMainRequest(const GURL& url,
     60                                           const GURL& preferred_manifest_url,
     61                                           Delegate* delegate) OVERRIDE;
     62   virtual void FindResponseForSubRequest(
     63       AppCache* cache, const GURL& url,
     64       AppCacheEntry* found_entry, AppCacheEntry* found_fallback_entry,
     65       bool * found_network_namespace) OVERRIDE;
     66   virtual void MarkEntryAsForeign(const GURL& entry_url,
     67                                   int64 cache_id) OVERRIDE;
     68   virtual void MakeGroupObsolete(AppCacheGroup* group,
     69                                  Delegate* delegate,
     70                                  int response_code) OVERRIDE;
     71   virtual AppCacheResponseReader* CreateResponseReader(
     72       const GURL& manifest_url, int64 group_id, int64 response_id) OVERRIDE;
     73   virtual AppCacheResponseWriter* CreateResponseWriter(
     74       const GURL& manifest_url, int64 group_id) OVERRIDE;
     75   virtual void DoomResponses(
     76       const GURL& manifest_url,
     77       const std::vector<int64>& response_ids) OVERRIDE;
     78   virtual void DeleteResponses(
     79       const GURL& manifest_url,
     80       const std::vector<int64>& response_ids) OVERRIDE;
     81 
     82  private:
     83   friend class AppCacheRequestHandlerTest;
     84   friend class AppCacheServiceImplTest;
     85   friend class AppCacheUpdateJobTest;
     86   friend class MockAppCacheStorageTest;
     87 
     88   typedef base::hash_map<int64, scoped_refptr<AppCache> > StoredCacheMap;
     89   typedef std::map<GURL, scoped_refptr<AppCacheGroup> > StoredGroupMap;
     90   typedef std::set<int64> DoomedResponseIds;
     91 
     92   void ProcessGetAllInfo(scoped_refptr<DelegateReference> delegate_ref);
     93   void ProcessLoadCache(
     94       int64 id, scoped_refptr<DelegateReference> delegate_ref);
     95   void ProcessLoadOrCreateGroup(
     96       const GURL& manifest_url, scoped_refptr<DelegateReference> delegate_ref);
     97   void ProcessStoreGroupAndNewestCache(
     98       scoped_refptr<AppCacheGroup> group, scoped_refptr<AppCache> newest_cache,
     99       scoped_refptr<DelegateReference> delegate_ref);
    100   void ProcessMakeGroupObsolete(scoped_refptr<AppCacheGroup> group,
    101                                 scoped_refptr<DelegateReference> delegate_ref,
    102                                 int response_code);
    103   void ProcessFindResponseForMainRequest(
    104       const GURL& url, scoped_refptr<DelegateReference> delegate_ref);
    105 
    106   void ScheduleTask(const base::Closure& task);
    107   void RunOnePendingTask();
    108 
    109   void AddStoredCache(AppCache* cache);
    110   void RemoveStoredCache(AppCache* cache);
    111   void RemoveStoredCaches(const AppCacheGroup::Caches& caches);
    112   bool IsCacheStored(const AppCache* cache) {
    113     return stored_caches_.find(cache->cache_id()) != stored_caches_.end();
    114   }
    115 
    116   void AddStoredGroup(AppCacheGroup* group);
    117   void RemoveStoredGroup(AppCacheGroup* group);
    118   bool IsGroupStored(const AppCacheGroup* group) {
    119     return IsGroupForManifestStored(group->manifest_url());
    120   }
    121   bool IsGroupForManifestStored(const GURL& manifest_url) {
    122     return stored_groups_.find(manifest_url) != stored_groups_.end();
    123   }
    124 
    125   // These helpers determine when certain operations should complete
    126   // asynchronously vs synchronously to faithfully mimic, or mock,
    127   // the behavior of the real implemenation of the AppCacheStorage
    128   // interface.
    129   bool ShouldGroupLoadAppearAsync(const AppCacheGroup* group);
    130   bool ShouldCacheLoadAppearAsync(const AppCache* cache);
    131 
    132   // Lazily constructed in-memory disk cache.
    133   AppCacheDiskCache* disk_cache() {
    134     if (!disk_cache_) {
    135       const int kMaxCacheSize = 10 * 1024 * 1024;
    136       disk_cache_.reset(new AppCacheDiskCache);
    137       disk_cache_->InitWithMemBackend(kMaxCacheSize, net::CompletionCallback());
    138     }
    139     return disk_cache_.get();
    140   }
    141 
    142   // Simulate failures for testing. Once set all subsequent calls
    143   // to MakeGroupObsolete or StorageGroupAndNewestCache will fail.
    144   void SimulateMakeGroupObsoleteFailure() {
    145     simulate_make_group_obsolete_failure_ = true;
    146   }
    147   void SimulateStoreGroupAndNewestCacheFailure() {
    148     simulate_store_group_and_newest_cache_failure_ = true;
    149   }
    150 
    151   // Simulate FindResponseFor results for testing. These
    152   // provided values will be return on the next call to
    153   // the corresponding Find method, subsequent calls are
    154   // unaffected.
    155   void SimulateFindMainResource(
    156       const AppCacheEntry& entry,
    157       const GURL& fallback_url,
    158       const AppCacheEntry& fallback_entry,
    159       int64 cache_id,
    160       int64 group_id,
    161       const GURL& manifest_url) {
    162     simulate_find_main_resource_ = true;
    163     simulate_find_sub_resource_ = false;
    164     simulated_found_entry_ = entry;
    165     simulated_found_fallback_url_ = fallback_url;
    166     simulated_found_fallback_entry_ = fallback_entry;
    167     simulated_found_cache_id_ = cache_id;
    168     simulated_found_group_id_ = group_id;
    169     simulated_found_manifest_url_ = manifest_url,
    170     simulated_found_network_namespace_ = false;  // N/A to main resource loads
    171   }
    172   void SimulateFindSubResource(
    173       const AppCacheEntry& entry,
    174       const AppCacheEntry& fallback_entry,
    175       bool network_namespace) {
    176     simulate_find_main_resource_ = false;
    177     simulate_find_sub_resource_ = true;
    178     simulated_found_entry_ = entry;
    179     simulated_found_fallback_entry_ = fallback_entry;
    180     simulated_found_cache_id_ = kAppCacheNoCacheId; // N/A to sub resource loads
    181     simulated_found_manifest_url_ = GURL();  // N/A to sub resource loads
    182     simulated_found_group_id_ = 0;  // N/A to sub resource loads
    183     simulated_found_network_namespace_ = network_namespace;
    184   }
    185 
    186   void SimulateGetAllInfo(AppCacheInfoCollection* info) {
    187     simulated_appcache_info_ = info;
    188   }
    189 
    190   void SimulateResponseReader(AppCacheResponseReader* reader) {
    191     simulated_reader_.reset(reader);
    192   }
    193 
    194   StoredCacheMap stored_caches_;
    195   StoredGroupMap stored_groups_;
    196   DoomedResponseIds doomed_response_ids_;
    197   scoped_ptr<AppCacheDiskCache> disk_cache_;
    198   std::deque<base::Closure> pending_tasks_;
    199 
    200   bool simulate_make_group_obsolete_failure_;
    201   bool simulate_store_group_and_newest_cache_failure_;
    202 
    203   bool simulate_find_main_resource_;
    204   bool simulate_find_sub_resource_;
    205   AppCacheEntry simulated_found_entry_;
    206   AppCacheEntry simulated_found_fallback_entry_;
    207   int64 simulated_found_cache_id_;
    208   int64 simulated_found_group_id_;
    209   GURL simulated_found_fallback_url_;
    210   GURL simulated_found_manifest_url_;
    211   bool simulated_found_network_namespace_;
    212   scoped_refptr<AppCacheInfoCollection> simulated_appcache_info_;
    213   scoped_ptr<AppCacheResponseReader> simulated_reader_;
    214 
    215   base::WeakPtrFactory<MockAppCacheStorage> weak_factory_;
    216 
    217   FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest,
    218                            BasicFindMainResponse);
    219   FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest,
    220                            BasicFindMainFallbackResponse);
    221   FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest, CreateGroup);
    222   FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest,
    223                            FindMainResponseExclusions);
    224   FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest,
    225                            FindMainResponseWithMultipleCandidates);
    226   FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest, LoadCache_FarHit);
    227   FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest,
    228                            LoadGroupAndCache_FarHit);
    229   FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest, MakeGroupObsolete);
    230   FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest, StoreNewGroup);
    231   FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest,
    232                            StoreExistingGroup);
    233   FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest,
    234                            StoreExistingGroupExistingCache);
    235   FRIEND_TEST_ALL_PREFIXES(AppCacheServiceImplTest,
    236                            DeleteAppCachesForOrigin);
    237 
    238   DISALLOW_COPY_AND_ASSIGN(MockAppCacheStorage);
    239 };
    240 
    241 }  // namespace content
    242 
    243 #endif  // CONTENT_BROWSER_APPCACHE_MOCK_APPCACHE_STORAGE_H_
    244