| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // This file contains test infrastructure for multiple files | 5 // This file contains test infrastructure for multiple files |
| 6 // (current cookie_monster_unittest.cc and cookie_monster_perftest.cc) | 6 // (current cookie_monster_unittest.cc and cookie_monster_perftest.cc) |
| 7 // that need to test out CookieMonster interactions with the backing store. | 7 // that need to test out CookieMonster interactions with the backing store. |
| 8 // It should only be included by test code. | 8 // It should only be included by test code. |
| 9 | 9 |
| 10 #ifndef NET_COOKIES_COOKIE_MONSTER_STORE_TEST_H_ | 10 #ifndef NET_COOKIES_COOKIE_MONSTER_STORE_TEST_H_ |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 // Wrapper class for posting a loaded callback. Since the Callback class is not | 26 // Wrapper class for posting a loaded callback. Since the Callback class is not |
| 27 // reference counted, we cannot post a callback to the message loop directly, | 27 // reference counted, we cannot post a callback to the message loop directly, |
| 28 // instead we post a LoadedCallbackTask. | 28 // instead we post a LoadedCallbackTask. |
| 29 class LoadedCallbackTask | 29 class LoadedCallbackTask |
| 30 : public base::RefCountedThreadSafe<LoadedCallbackTask> { | 30 : public base::RefCountedThreadSafe<LoadedCallbackTask> { |
| 31 public: | 31 public: |
| 32 typedef CookieMonster::PersistentCookieStore::LoadedCallback LoadedCallback; | 32 typedef CookieMonster::PersistentCookieStore::LoadedCallback LoadedCallback; |
| 33 | 33 |
| 34 LoadedCallbackTask(LoadedCallback loaded_callback, | 34 LoadedCallbackTask(LoadedCallback loaded_callback, |
| 35 std::vector<CookieMonster::CanonicalCookie*> cookies); | 35 std::vector<CookieMonster::CanonicalCookie*> cookies); |
| 36 ~LoadedCallbackTask(); | |
| 37 | 36 |
| 38 void Run() { | 37 void Run() { |
| 39 loaded_callback_.Run(cookies_); | 38 loaded_callback_.Run(cookies_); |
| 40 } | 39 } |
| 41 | 40 |
| 42 private: | 41 private: |
| 42 friend class base::RefCountedThreadSafe<LoadedCallbackTask>; |
| 43 ~LoadedCallbackTask(); |
| 44 |
| 43 LoadedCallback loaded_callback_; | 45 LoadedCallback loaded_callback_; |
| 44 std::vector<CookieMonster::CanonicalCookie*> cookies_; | 46 std::vector<CookieMonster::CanonicalCookie*> cookies_; |
| 45 | 47 |
| 46 DISALLOW_COPY_AND_ASSIGN(LoadedCallbackTask); | 48 DISALLOW_COPY_AND_ASSIGN(LoadedCallbackTask); |
| 47 }; // Wrapper class LoadedCallbackTask | 49 }; // Wrapper class LoadedCallbackTask |
| 48 | 50 |
| 49 // Describes a call to one of the 3 functions of PersistentCookieStore. | 51 // Describes a call to one of the 3 functions of PersistentCookieStore. |
| 50 struct CookieStoreCommand { | 52 struct CookieStoreCommand { |
| 51 enum Type { | 53 enum Type { |
| 52 ADD, | 54 ADD, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 65 | 67 |
| 66 // Implementation of PersistentCookieStore that captures the | 68 // Implementation of PersistentCookieStore that captures the |
| 67 // received commands and saves them to a list. | 69 // received commands and saves them to a list. |
| 68 // The result of calls to Load() can be configured using SetLoadExpectation(). | 70 // The result of calls to Load() can be configured using SetLoadExpectation(). |
| 69 class MockPersistentCookieStore | 71 class MockPersistentCookieStore |
| 70 : public CookieMonster::PersistentCookieStore { | 72 : public CookieMonster::PersistentCookieStore { |
| 71 public: | 73 public: |
| 72 typedef std::vector<CookieStoreCommand> CommandList; | 74 typedef std::vector<CookieStoreCommand> CommandList; |
| 73 | 75 |
| 74 MockPersistentCookieStore(); | 76 MockPersistentCookieStore(); |
| 75 virtual ~MockPersistentCookieStore(); | |
| 76 | 77 |
| 77 void SetLoadExpectation( | 78 void SetLoadExpectation( |
| 78 bool return_value, | 79 bool return_value, |
| 79 const std::vector<CookieMonster::CanonicalCookie*>& result); | 80 const std::vector<CookieMonster::CanonicalCookie*>& result); |
| 80 | 81 |
| 81 const CommandList& commands() const { | 82 const CommandList& commands() const { |
| 82 return commands_; | 83 return commands_; |
| 83 } | 84 } |
| 84 | 85 |
| 85 virtual void Load(const LoadedCallback& loaded_callback) OVERRIDE; | 86 virtual void Load(const LoadedCallback& loaded_callback) OVERRIDE; |
| 86 | 87 |
| 87 virtual void LoadCookiesForKey(const std::string& key, | 88 virtual void LoadCookiesForKey(const std::string& key, |
| 88 const LoadedCallback& loaded_callback) OVERRIDE; | 89 const LoadedCallback& loaded_callback) OVERRIDE; |
| 89 | 90 |
| 90 virtual void AddCookie(const CookieMonster::CanonicalCookie& cookie) OVERRIDE; | 91 virtual void AddCookie(const CookieMonster::CanonicalCookie& cookie) OVERRIDE; |
| 91 | 92 |
| 92 virtual void UpdateCookieAccessTime( | 93 virtual void UpdateCookieAccessTime( |
| 93 const CookieMonster::CanonicalCookie& cookie) OVERRIDE; | 94 const CookieMonster::CanonicalCookie& cookie) OVERRIDE; |
| 94 | 95 |
| 95 virtual void DeleteCookie( | 96 virtual void DeleteCookie( |
| 96 const CookieMonster::CanonicalCookie& cookie) OVERRIDE; | 97 const CookieMonster::CanonicalCookie& cookie) OVERRIDE; |
| 97 | 98 |
| 98 virtual void Flush(const base::Closure& callback) OVERRIDE; | 99 virtual void Flush(const base::Closure& callback) OVERRIDE; |
| 99 | 100 |
| 100 // No files are created so nothing to clear either | 101 // No files are created so nothing to clear either |
| 101 virtual void SetClearLocalStateOnExit(bool clear_local_state) OVERRIDE; | 102 virtual void SetClearLocalStateOnExit(bool clear_local_state) OVERRIDE; |
| 102 | 103 |
| 103 private: | 104 private: |
| 105 virtual ~MockPersistentCookieStore(); |
| 106 |
| 104 CommandList commands_; | 107 CommandList commands_; |
| 105 | 108 |
| 106 // Deferred result to use when Load() is called. | 109 // Deferred result to use when Load() is called. |
| 107 bool load_return_value_; | 110 bool load_return_value_; |
| 108 std::vector<CookieMonster::CanonicalCookie*> load_result_; | 111 std::vector<CookieMonster::CanonicalCookie*> load_result_; |
| 109 // Indicates if the store has been fully loaded to avoid returning duplicate | 112 // Indicates if the store has been fully loaded to avoid returning duplicate |
| 110 // cookies. | 113 // cookies. |
| 111 bool loaded_; | 114 bool loaded_; |
| 112 | 115 |
| 113 DISALLOW_COPY_AND_ASSIGN(MockPersistentCookieStore); | 116 DISALLOW_COPY_AND_ASSIGN(MockPersistentCookieStore); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 const std::string& cookie_line, | 153 const std::string& cookie_line, |
| 151 const base::Time& creation_time, | 154 const base::Time& creation_time, |
| 152 std::vector<CookieMonster::CanonicalCookie*>* out_list); | 155 std::vector<CookieMonster::CanonicalCookie*>* out_list); |
| 153 | 156 |
| 154 // Just act like a backing database. Keep cookie information from | 157 // Just act like a backing database. Keep cookie information from |
| 155 // Add/Update/Delete and regurgitate it when Load is called. | 158 // Add/Update/Delete and regurgitate it when Load is called. |
| 156 class MockSimplePersistentCookieStore | 159 class MockSimplePersistentCookieStore |
| 157 : public CookieMonster::PersistentCookieStore { | 160 : public CookieMonster::PersistentCookieStore { |
| 158 public: | 161 public: |
| 159 MockSimplePersistentCookieStore(); | 162 MockSimplePersistentCookieStore(); |
| 160 virtual ~MockSimplePersistentCookieStore(); | |
| 161 | 163 |
| 162 virtual void Load(const LoadedCallback& loaded_callback) OVERRIDE; | 164 virtual void Load(const LoadedCallback& loaded_callback) OVERRIDE; |
| 163 | 165 |
| 164 virtual void LoadCookiesForKey(const std::string& key, | 166 virtual void LoadCookiesForKey(const std::string& key, |
| 165 const LoadedCallback& loaded_callback) OVERRIDE; | 167 const LoadedCallback& loaded_callback) OVERRIDE; |
| 166 | 168 |
| 167 virtual void AddCookie( | 169 virtual void AddCookie( |
| 168 const CookieMonster::CanonicalCookie& cookie) OVERRIDE; | 170 const CookieMonster::CanonicalCookie& cookie) OVERRIDE; |
| 169 | 171 |
| 170 virtual void UpdateCookieAccessTime( | 172 virtual void UpdateCookieAccessTime( |
| 171 const CookieMonster::CanonicalCookie& cookie) OVERRIDE; | 173 const CookieMonster::CanonicalCookie& cookie) OVERRIDE; |
| 172 | 174 |
| 173 virtual void DeleteCookie( | 175 virtual void DeleteCookie( |
| 174 const CookieMonster::CanonicalCookie& cookie) OVERRIDE; | 176 const CookieMonster::CanonicalCookie& cookie) OVERRIDE; |
| 175 | 177 |
| 176 virtual void Flush(const base::Closure& callback) OVERRIDE; | 178 virtual void Flush(const base::Closure& callback) OVERRIDE; |
| 177 | 179 |
| 178 virtual void SetClearLocalStateOnExit(bool clear_local_state) OVERRIDE; | 180 virtual void SetClearLocalStateOnExit(bool clear_local_state) OVERRIDE; |
| 179 | 181 |
| 180 private: | 182 private: |
| 183 virtual ~MockSimplePersistentCookieStore(); |
| 184 |
| 181 typedef std::map<int64, CookieMonster::CanonicalCookie> | 185 typedef std::map<int64, CookieMonster::CanonicalCookie> |
| 182 CanonicalCookieMap; | 186 CanonicalCookieMap; |
| 183 | 187 |
| 184 CanonicalCookieMap cookies_; | 188 CanonicalCookieMap cookies_; |
| 185 | 189 |
| 186 // Indicates if the store has been fully loaded to avoid return duplicate | 190 // Indicates if the store has been fully loaded to avoid return duplicate |
| 187 // cookies in subsequent load requests | 191 // cookies in subsequent load requests |
| 188 bool loaded_; | 192 bool loaded_; |
| 189 }; | 193 }; |
| 190 | 194 |
| 191 // Helper function for creating a CookieMonster backed by a | 195 // Helper function for creating a CookieMonster backed by a |
| 192 // MockSimplePersistentCookieStore for garbage collection testing. | 196 // MockSimplePersistentCookieStore for garbage collection testing. |
| 193 // | 197 // |
| 194 // Fill the store through import with |num_cookies| cookies, |num_old_cookies| | 198 // Fill the store through import with |num_cookies| cookies, |num_old_cookies| |
| 195 // with access time Now()-days_old, the rest with access time Now(). | 199 // with access time Now()-days_old, the rest with access time Now(). |
| 196 // Do two SetCookies(). Return whether each of the two SetCookies() took | 200 // Do two SetCookies(). Return whether each of the two SetCookies() took |
| 197 // longer than |gc_perf_micros| to complete, and how many cookie were | 201 // longer than |gc_perf_micros| to complete, and how many cookie were |
| 198 // left in the store afterwards. | 202 // left in the store afterwards. |
| 199 CookieMonster* CreateMonsterFromStoreForGC( | 203 CookieMonster* CreateMonsterFromStoreForGC( |
| 200 int num_cookies, | 204 int num_cookies, |
| 201 int num_old_cookies, | 205 int num_old_cookies, |
| 202 int days_old); | 206 int days_old); |
| 203 | 207 |
| 204 } // namespace net | 208 } // namespace net |
| 205 | 209 |
| 206 #endif // NET_COOKIES_COOKIE_MONSTER_STORE_TEST_H_ | 210 #endif // NET_COOKIES_COOKIE_MONSTER_STORE_TEST_H_ |
| OLD | NEW |