OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_BASE_COOKIE_MONSTER_STORE_TEST_H_ | 10 #ifndef NET_BASE_COOKIE_MONSTER_STORE_TEST_H_ |
11 #define NET_BASE_COOKIE_MONSTER_STORE_TEST_H_ | 11 #define NET_BASE_COOKIE_MONSTER_STORE_TEST_H_ |
12 #pragma once | 12 #pragma once |
13 | 13 |
14 #include <map> | 14 #include <map> |
15 #include <string> | 15 #include <string> |
16 #include <utility> | 16 #include <utility> |
17 #include <vector> | 17 #include <vector> |
18 #include "net/base/cookie_monster.h" | 18 #include "net/base/cookie_monster.h" |
19 | 19 |
20 namespace base { | 20 namespace base { |
21 class Time; | 21 class Time; |
22 } | 22 } |
23 | 23 |
24 namespace net { | 24 namespace net { |
25 | 25 |
| 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, |
| 28 // instead we post a LoadedCallbackTask. |
| 29 class LoadedCallbackTask |
| 30 : public base::RefCountedThreadSafe<LoadedCallbackTask> { |
| 31 public: |
| 32 typedef CookieMonster::PersistentCookieStore::LoadedCallback LoadedCallback; |
| 33 |
| 34 LoadedCallbackTask(LoadedCallback loaded_callback, |
| 35 std::vector<CookieMonster::CanonicalCookie*> cookies); |
| 36 ~LoadedCallbackTask(); |
| 37 |
| 38 void Run() { |
| 39 loaded_callback_.Run(cookies_); |
| 40 } |
| 41 |
| 42 private: |
| 43 LoadedCallback loaded_callback_; |
| 44 std::vector<CookieMonster::CanonicalCookie*> cookies_; |
| 45 |
| 46 DISALLOW_COPY_AND_ASSIGN(LoadedCallbackTask); |
| 47 }; // Wrapper class LoadedCallbackTask |
| 48 |
26 // Describes a call to one of the 3 functions of PersistentCookieStore. | 49 // Describes a call to one of the 3 functions of PersistentCookieStore. |
27 struct CookieStoreCommand { | 50 struct CookieStoreCommand { |
28 enum Type { | 51 enum Type { |
29 ADD, | 52 ADD, |
30 UPDATE_ACCESS_TIME, | 53 UPDATE_ACCESS_TIME, |
31 REMOVE, | 54 REMOVE, |
32 }; | 55 }; |
33 | 56 |
34 CookieStoreCommand(Type type, | 57 CookieStoreCommand(Type type, |
35 const CookieMonster::CanonicalCookie& cookie) | 58 const CookieMonster::CanonicalCookie& cookie) |
(...skipping 16 matching lines...) Expand all Loading... |
52 virtual ~MockPersistentCookieStore(); | 75 virtual ~MockPersistentCookieStore(); |
53 | 76 |
54 void SetLoadExpectation( | 77 void SetLoadExpectation( |
55 bool return_value, | 78 bool return_value, |
56 const std::vector<CookieMonster::CanonicalCookie*>& result); | 79 const std::vector<CookieMonster::CanonicalCookie*>& result); |
57 | 80 |
58 const CommandList& commands() const { | 81 const CommandList& commands() const { |
59 return commands_; | 82 return commands_; |
60 } | 83 } |
61 | 84 |
62 virtual bool Load(const LoadedCallback& loaded_callback) OVERRIDE; | 85 virtual void Load(const LoadedCallback& loaded_callback) OVERRIDE; |
| 86 |
| 87 virtual void LoadCookiesForKey(const std::string& key, |
| 88 const LoadedCallback& loaded_callback) OVERRIDE; |
63 | 89 |
64 virtual void AddCookie(const CookieMonster::CanonicalCookie& cookie) OVERRIDE; | 90 virtual void AddCookie(const CookieMonster::CanonicalCookie& cookie) OVERRIDE; |
65 | 91 |
66 virtual void UpdateCookieAccessTime( | 92 virtual void UpdateCookieAccessTime( |
67 const CookieMonster::CanonicalCookie& cookie) OVERRIDE; | 93 const CookieMonster::CanonicalCookie& cookie) OVERRIDE; |
68 | 94 |
69 virtual void DeleteCookie( | 95 virtual void DeleteCookie( |
70 const CookieMonster::CanonicalCookie& cookie) OVERRIDE; | 96 const CookieMonster::CanonicalCookie& cookie) OVERRIDE; |
71 | 97 |
72 virtual void Flush(Task* completion_task) OVERRIDE; | 98 virtual void Flush(Task* completion_task) OVERRIDE; |
73 | 99 |
74 // No files are created so nothing to clear either | 100 // No files are created so nothing to clear either |
75 virtual void SetClearLocalStateOnExit(bool clear_local_state) OVERRIDE; | 101 virtual void SetClearLocalStateOnExit(bool clear_local_state) OVERRIDE; |
76 | 102 |
77 private: | 103 private: |
78 CommandList commands_; | 104 CommandList commands_; |
79 | 105 |
80 // Deferred result to use when Load() is called. | 106 // Deferred result to use when Load() is called. |
81 bool load_return_value_; | 107 bool load_return_value_; |
82 std::vector<CookieMonster::CanonicalCookie*> load_result_; | 108 std::vector<CookieMonster::CanonicalCookie*> load_result_; |
| 109 // Indicates if the store has been fully loaded to avoid returning duplicate |
| 110 // cookies. |
| 111 bool loaded_; |
83 | 112 |
84 DISALLOW_COPY_AND_ASSIGN(MockPersistentCookieStore); | 113 DISALLOW_COPY_AND_ASSIGN(MockPersistentCookieStore); |
85 }; | 114 }; |
86 | 115 |
87 // Mock for CookieMonster::Delegate | 116 // Mock for CookieMonster::Delegate |
88 class MockCookieMonsterDelegate : public CookieMonster::Delegate { | 117 class MockCookieMonsterDelegate : public CookieMonster::Delegate { |
89 public: | 118 public: |
90 typedef std::pair<CookieMonster::CanonicalCookie, bool> | 119 typedef std::pair<CookieMonster::CanonicalCookie, bool> |
91 CookieNotification; | 120 CookieNotification; |
92 | 121 |
(...skipping 30 matching lines...) Expand all Loading... |
123 std::vector<CookieMonster::CanonicalCookie*>* out_list); | 152 std::vector<CookieMonster::CanonicalCookie*>* out_list); |
124 | 153 |
125 // Just act like a backing database. Keep cookie information from | 154 // Just act like a backing database. Keep cookie information from |
126 // Add/Update/Delete and regurgitate it when Load is called. | 155 // Add/Update/Delete and regurgitate it when Load is called. |
127 class MockSimplePersistentCookieStore | 156 class MockSimplePersistentCookieStore |
128 : public CookieMonster::PersistentCookieStore { | 157 : public CookieMonster::PersistentCookieStore { |
129 public: | 158 public: |
130 MockSimplePersistentCookieStore(); | 159 MockSimplePersistentCookieStore(); |
131 virtual ~MockSimplePersistentCookieStore(); | 160 virtual ~MockSimplePersistentCookieStore(); |
132 | 161 |
133 virtual bool Load(const LoadedCallback& loaded_callback); | 162 virtual void Load(const LoadedCallback& loaded_callback) OVERRIDE; |
| 163 |
| 164 virtual void LoadCookiesForKey(const std::string& key, |
| 165 const LoadedCallback& loaded_callback) OVERRIDE; |
134 | 166 |
135 virtual void AddCookie( | 167 virtual void AddCookie( |
136 const CookieMonster::CanonicalCookie& cookie); | 168 const CookieMonster::CanonicalCookie& cookie) OVERRIDE; |
137 | 169 |
138 virtual void UpdateCookieAccessTime( | 170 virtual void UpdateCookieAccessTime( |
139 const CookieMonster::CanonicalCookie& cookie); | 171 const CookieMonster::CanonicalCookie& cookie) OVERRIDE; |
140 | 172 |
141 virtual void DeleteCookie( | 173 virtual void DeleteCookie( |
142 const CookieMonster::CanonicalCookie& cookie); | 174 const CookieMonster::CanonicalCookie& cookie) OVERRIDE; |
143 | 175 |
144 virtual void Flush(Task* completion_task); | 176 virtual void Flush(Task* completion_task) OVERRIDE; |
145 | 177 |
146 virtual void SetClearLocalStateOnExit(bool clear_local_state); | 178 virtual void SetClearLocalStateOnExit(bool clear_local_state) OVERRIDE; |
147 | 179 |
148 private: | 180 private: |
149 typedef std::map<int64, CookieMonster::CanonicalCookie> | 181 typedef std::map<int64, CookieMonster::CanonicalCookie> |
150 CanonicalCookieMap; | 182 CanonicalCookieMap; |
151 | 183 |
152 CanonicalCookieMap cookies_; | 184 CanonicalCookieMap cookies_; |
| 185 |
| 186 // Indicates if the store has been fully loaded to avoid return duplicate |
| 187 // cookies in subsequent load requests |
| 188 bool loaded_; |
153 }; | 189 }; |
154 | 190 |
155 // Helper function for creating a CookieMonster backed by a | 191 // Helper function for creating a CookieMonster backed by a |
156 // MockSimplePersistentCookieStore for garbage collection testing. | 192 // MockSimplePersistentCookieStore for garbage collection testing. |
157 // | 193 // |
158 // Fill the store through import with |num_cookies| cookies, |num_old_cookies| | 194 // Fill the store through import with |num_cookies| cookies, |num_old_cookies| |
159 // with access time Now()-days_old, the rest with access time Now(). | 195 // with access time Now()-days_old, the rest with access time Now(). |
160 // Do two SetCookies(). Return whether each of the two SetCookies() took | 196 // Do two SetCookies(). Return whether each of the two SetCookies() took |
161 // longer than |gc_perf_micros| to complete, and how many cookie were | 197 // longer than |gc_perf_micros| to complete, and how many cookie were |
162 // left in the store afterwards. | 198 // left in the store afterwards. |
163 CookieMonster* CreateMonsterFromStoreForGC( | 199 CookieMonster* CreateMonsterFromStoreForGC( |
164 int num_cookies, | 200 int num_cookies, |
165 int num_old_cookies, | 201 int num_old_cookies, |
166 int days_old); | 202 int days_old); |
167 | 203 |
168 } // namespace net | 204 } // namespace net |
169 | 205 |
170 #endif // NET_BASE_COOKIE_MONSTER_STORE_TEST_H_ | 206 #endif // NET_BASE_COOKIE_MONSTER_STORE_TEST_H_ |
OLD | NEW |