OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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 NET_DISK_CACHE_DISK_CACHE_TEST_H_ |
| 6 #define NET_DISK_CACHE_DISK_CACHE_TEST_H_ |
| 7 |
| 8 #include "base/basictypes.h" |
| 9 #include "base/files/file_path.h" |
| 10 #include "base/files/scoped_temp_dir.h" |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/threading/thread.h" |
| 13 #include "net/base/cache_type.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "testing/platform_test.h" |
| 16 |
| 17 namespace base { |
| 18 class MessageLoopProxy; |
| 19 } // namespace base |
| 20 |
| 21 namespace net { |
| 22 class IOBuffer; |
| 23 } // namespace net |
| 24 |
| 25 namespace disk_cache { |
| 26 |
| 27 class Backend; |
| 28 class BackendImpl; |
| 29 class Entry; |
| 30 class MemBackendImpl; |
| 31 class SimpleBackendImpl; |
| 32 |
| 33 class BackendTestTraits { |
| 34 public: |
| 35 class CreateBackendExtraData { |
| 36 protected: |
| 37 ~CreateBackendExtraData(); |
| 38 }; |
| 39 |
| 40 BackendTestTraits() {} |
| 41 virtual ~BackendTestTraits() {} |
| 42 |
| 43 virtual Backend* CreateBackend(const CreateBackendExtraData* extra_data, |
| 44 const base::FilePath& cache_path, |
| 45 int max_size, |
| 46 base::MessageLoopProxy* task_runner) const = 0; |
| 47 |
| 48 virtual bool UsesCacheThread() const = 0; |
| 49 |
| 50 // Backend specific quirks that tests might want to respect: |
| 51 virtual bool WritesUpdateLastUsed() const; |
| 52 virtual bool ReadsUpdateLastUsed() const; |
| 53 virtual int SparseRoundingInterval() const; |
| 54 virtual bool EntryCountIncludesSparseRanges() const; |
| 55 virtual bool ImplementsCouldBeSparse() const; |
| 56 virtual bool DoomedSparseEntriesIOWorks() const; |
| 57 virtual bool EnumerationsAreLexicographicByKey() const; |
| 58 |
| 59 // Events |
| 60 virtual void PreTearDown() const {} |
| 61 virtual void PostTearDown() const {} |
| 62 |
| 63 |
| 64 // Helpers |
| 65 virtual bool SetMaxSize(Backend* backend, int size) const = 0; |
| 66 virtual void FlushQueueForTest(Backend* backend) const {} |
| 67 virtual void AddDelay() const; |
| 68 }; |
| 69 |
| 70 // Provides basic support for cache related tests. |
| 71 class DiskCacheTest : public PlatformTest, |
| 72 public ::testing::WithParamInterface<const BackendTestTrai
ts*> |
| 73 { |
| 74 public: |
| 75 void InitCache() { InitCacheWithExtraData(NULL); } |
| 76 |
| 77 // Utility methods to access the cache and wait for each operation to finish. |
| 78 int OpenEntry(const std::string& key, disk_cache::Entry** entry); |
| 79 int CreateEntry(const std::string& key, disk_cache::Entry** entry); |
| 80 int DoomEntry(const std::string& key); |
| 81 int DoomAllEntries(); |
| 82 int DoomEntriesBetween(const base::Time initial_time, |
| 83 const base::Time end_time); |
| 84 int DoomEntriesSince(const base::Time initial_time); |
| 85 int OpenNextEntry(void** iter, disk_cache::Entry** next_entry); |
| 86 int ReadData(disk_cache::Entry* entry, int index, int offset, |
| 87 net::IOBuffer* buf, int len); |
| 88 int WriteData(disk_cache::Entry* entry, int index, int offset, |
| 89 net::IOBuffer* buf, int len, bool truncate); |
| 90 int ReadSparseData(disk_cache::Entry* entry, int64 offset, net::IOBuffer* buf, |
| 91 int len); |
| 92 int WriteSparseData(disk_cache::Entry* entry, int64 offset, |
| 93 net::IOBuffer* buf, int len); |
| 94 |
| 95 const base::FilePath& cache_path() const { return temp_dir_.path(); } |
| 96 disk_cache::Backend* cache() { return cache_.get(); } |
| 97 |
| 98 // Makes sure that some time passes before continuing the test. Time::Now() |
| 99 // before and after this method will not be the same. |
| 100 void AddDelay(); |
| 101 |
| 102 void FlushQueueForTest(); |
| 103 |
| 104 void SetMaxSize(int max_size); |
| 105 |
| 106 // Copies a set of cache files from the data folder to the test folder. |
| 107 bool CopyTestCache(const std::string& name); |
| 108 |
| 109 protected: |
| 110 DiskCacheTest(); |
| 111 virtual ~DiskCacheTest(); |
| 112 |
| 113 // Do not call this in a test that may run on multiple backends. |
| 114 void InitCacheWithExtraData(const BackendTestTraits::CreateBackendExtraData* e
xtra_data); |
| 115 |
| 116 void DeleteCache() { cache_.reset(); } |
| 117 |
| 118 void CreateBackend(uint32 flags, base::Thread* thread); |
| 119 |
| 120 // Deletes the contents of |cache_path_|. |
| 121 bool CleanupCacheDir(); |
| 122 |
| 123 void UseCurrentThread() { use_current_thread_ = true; } |
| 124 |
| 125 // TODO(implement) |
| 126 void DisableIntegrityCheck() {} |
| 127 |
| 128 |
| 129 base::MessageLoopProxy* GetCacheThread(); |
| 130 |
| 131 |
| 132 // From PlatformTest: |
| 133 virtual void TearDown() OVERRIDE; |
| 134 |
| 135 const BackendTestTraits* traits() const { return traits_; } |
| 136 |
| 137 protected: |
| 138 |
| 139 private: |
| 140 bool use_current_thread_; |
| 141 int max_size_; |
| 142 |
| 143 scoped_ptr<disk_cache::Backend> cache_; |
| 144 |
| 145 base::ScopedTempDir temp_dir_; |
| 146 scoped_ptr<base::MessageLoop> message_loop_; |
| 147 |
| 148 const BackendTestTraits* traits_; |
| 149 |
| 150 base::Thread cache_thread_; |
| 151 DISALLOW_COPY_AND_ASSIGN(DiskCacheTest); |
| 152 }; |
| 153 |
| 154 } // namespace disk_cache |
| 155 |
| 156 #endif // NET_DISK_CACHE_DISK_CACHE_TEST_H_ |
OLD | NEW |