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