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 |
| 104 protected: |
| 105 virtual ~MockPersistentCookieStore(); |
| 106 |
103 private: | 107 private: |
104 CommandList commands_; | 108 CommandList commands_; |
105 | 109 |
106 // Deferred result to use when Load() is called. | 110 // Deferred result to use when Load() is called. |
107 bool load_return_value_; | 111 bool load_return_value_; |
108 std::vector<CookieMonster::CanonicalCookie*> load_result_; | 112 std::vector<CookieMonster::CanonicalCookie*> load_result_; |
109 // Indicates if the store has been fully loaded to avoid returning duplicate | 113 // Indicates if the store has been fully loaded to avoid returning duplicate |
110 // cookies. | 114 // cookies. |
111 bool loaded_; | 115 bool loaded_; |
112 | 116 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 const std::string& cookie_line, | 154 const std::string& cookie_line, |
151 const base::Time& creation_time, | 155 const base::Time& creation_time, |
152 std::vector<CookieMonster::CanonicalCookie*>* out_list); | 156 std::vector<CookieMonster::CanonicalCookie*>* out_list); |
153 | 157 |
154 // Just act like a backing database. Keep cookie information from | 158 // Just act like a backing database. Keep cookie information from |
155 // Add/Update/Delete and regurgitate it when Load is called. | 159 // Add/Update/Delete and regurgitate it when Load is called. |
156 class MockSimplePersistentCookieStore | 160 class MockSimplePersistentCookieStore |
157 : public CookieMonster::PersistentCookieStore { | 161 : public CookieMonster::PersistentCookieStore { |
158 public: | 162 public: |
159 MockSimplePersistentCookieStore(); | 163 MockSimplePersistentCookieStore(); |
160 virtual ~MockSimplePersistentCookieStore(); | |
161 | 164 |
162 virtual void Load(const LoadedCallback& loaded_callback) OVERRIDE; | 165 virtual void Load(const LoadedCallback& loaded_callback) OVERRIDE; |
163 | 166 |
164 virtual void LoadCookiesForKey(const std::string& key, | 167 virtual void LoadCookiesForKey(const std::string& key, |
165 const LoadedCallback& loaded_callback) OVERRIDE; | 168 const LoadedCallback& loaded_callback) OVERRIDE; |
166 | 169 |
167 virtual void AddCookie( | 170 virtual void AddCookie( |
168 const CookieMonster::CanonicalCookie& cookie) OVERRIDE; | 171 const CookieMonster::CanonicalCookie& cookie) OVERRIDE; |
169 | 172 |
170 virtual void UpdateCookieAccessTime( | 173 virtual void UpdateCookieAccessTime( |
171 const CookieMonster::CanonicalCookie& cookie) OVERRIDE; | 174 const CookieMonster::CanonicalCookie& cookie) OVERRIDE; |
172 | 175 |
173 virtual void DeleteCookie( | 176 virtual void DeleteCookie( |
174 const CookieMonster::CanonicalCookie& cookie) OVERRIDE; | 177 const CookieMonster::CanonicalCookie& cookie) OVERRIDE; |
175 | 178 |
176 virtual void Flush(const base::Closure& callback) OVERRIDE; | 179 virtual void Flush(const base::Closure& callback) OVERRIDE; |
177 | 180 |
178 virtual void SetClearLocalStateOnExit(bool clear_local_state) OVERRIDE; | 181 virtual void SetClearLocalStateOnExit(bool clear_local_state) OVERRIDE; |
179 | 182 |
| 183 protected: |
| 184 virtual ~MockSimplePersistentCookieStore(); |
| 185 |
180 private: | 186 private: |
181 typedef std::map<int64, CookieMonster::CanonicalCookie> | 187 typedef std::map<int64, CookieMonster::CanonicalCookie> |
182 CanonicalCookieMap; | 188 CanonicalCookieMap; |
183 | 189 |
184 CanonicalCookieMap cookies_; | 190 CanonicalCookieMap cookies_; |
185 | 191 |
186 // Indicates if the store has been fully loaded to avoid return duplicate | 192 // Indicates if the store has been fully loaded to avoid return duplicate |
187 // cookies in subsequent load requests | 193 // cookies in subsequent load requests |
188 bool loaded_; | 194 bool loaded_; |
189 }; | 195 }; |
190 | 196 |
191 // Helper function for creating a CookieMonster backed by a | 197 // Helper function for creating a CookieMonster backed by a |
192 // MockSimplePersistentCookieStore for garbage collection testing. | 198 // MockSimplePersistentCookieStore for garbage collection testing. |
193 // | 199 // |
194 // Fill the store through import with |num_cookies| cookies, |num_old_cookies| | 200 // 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(). | 201 // with access time Now()-days_old, the rest with access time Now(). |
196 // Do two SetCookies(). Return whether each of the two SetCookies() took | 202 // Do two SetCookies(). Return whether each of the two SetCookies() took |
197 // longer than |gc_perf_micros| to complete, and how many cookie were | 203 // longer than |gc_perf_micros| to complete, and how many cookie were |
198 // left in the store afterwards. | 204 // left in the store afterwards. |
199 CookieMonster* CreateMonsterFromStoreForGC( | 205 CookieMonster* CreateMonsterFromStoreForGC( |
200 int num_cookies, | 206 int num_cookies, |
201 int num_old_cookies, | 207 int num_old_cookies, |
202 int days_old); | 208 int days_old); |
203 | 209 |
204 } // namespace net | 210 } // namespace net |
205 | 211 |
206 #endif // NET_COOKIES_COOKIE_MONSTER_STORE_TEST_H_ | 212 #endif // NET_COOKIES_COOKIE_MONSTER_STORE_TEST_H_ |
OLD | NEW |