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 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 MockPersistentCookieStore(); | 75 MockPersistentCookieStore(); |
76 | 76 |
77 void SetLoadExpectation( | 77 void SetLoadExpectation( |
78 bool return_value, | 78 bool return_value, |
79 const std::vector<CanonicalCookie*>& result); | 79 const std::vector<CanonicalCookie*>& result); |
80 | 80 |
81 const CommandList& commands() const { | 81 const CommandList& commands() const { |
82 return commands_; | 82 return commands_; |
83 } | 83 } |
84 | 84 |
85 virtual void Load(const LoadedCallback& loaded_callback) override; | 85 void Load(const LoadedCallback& loaded_callback) override; |
86 | 86 |
87 virtual void LoadCookiesForKey(const std::string& key, | 87 void LoadCookiesForKey(const std::string& key, |
88 const LoadedCallback& loaded_callback) override; | 88 const LoadedCallback& loaded_callback) override; |
89 | 89 |
90 virtual void AddCookie(const CanonicalCookie& cookie) override; | 90 void AddCookie(const CanonicalCookie& cookie) override; |
91 | 91 |
92 virtual void UpdateCookieAccessTime( | 92 void UpdateCookieAccessTime(const CanonicalCookie& cookie) override; |
93 const CanonicalCookie& cookie) override; | |
94 | 93 |
95 virtual void DeleteCookie( | 94 void DeleteCookie(const CanonicalCookie& cookie) override; |
96 const CanonicalCookie& cookie) override; | |
97 | 95 |
98 virtual void Flush(const base::Closure& callback) override; | 96 void Flush(const base::Closure& callback) override; |
99 | 97 |
100 virtual void SetForceKeepSessionState() override; | 98 void SetForceKeepSessionState() override; |
101 | 99 |
102 protected: | 100 protected: |
103 virtual ~MockPersistentCookieStore(); | 101 ~MockPersistentCookieStore() override; |
104 | 102 |
105 private: | 103 private: |
106 CommandList commands_; | 104 CommandList commands_; |
107 | 105 |
108 // Deferred result to use when Load() is called. | 106 // Deferred result to use when Load() is called. |
109 bool load_return_value_; | 107 bool load_return_value_; |
110 std::vector<CanonicalCookie*> load_result_; | 108 std::vector<CanonicalCookie*> load_result_; |
111 // Indicates if the store has been fully loaded to avoid returning duplicate | 109 // Indicates if the store has been fully loaded to avoid returning duplicate |
112 // cookies. | 110 // cookies. |
113 bool loaded_; | 111 bool loaded_; |
114 | 112 |
115 DISALLOW_COPY_AND_ASSIGN(MockPersistentCookieStore); | 113 DISALLOW_COPY_AND_ASSIGN(MockPersistentCookieStore); |
116 }; | 114 }; |
117 | 115 |
118 // Mock for CookieMonsterDelegate | 116 // Mock for CookieMonsterDelegate |
119 class MockCookieMonsterDelegate : public CookieMonsterDelegate { | 117 class MockCookieMonsterDelegate : public CookieMonsterDelegate { |
120 public: | 118 public: |
121 typedef std::pair<CanonicalCookie, bool> | 119 typedef std::pair<CanonicalCookie, bool> |
122 CookieNotification; | 120 CookieNotification; |
123 | 121 |
124 MockCookieMonsterDelegate(); | 122 MockCookieMonsterDelegate(); |
125 | 123 |
126 const std::vector<CookieNotification>& changes() const { return changes_; } | 124 const std::vector<CookieNotification>& changes() const { return changes_; } |
127 | 125 |
128 void reset() { changes_.clear(); } | 126 void reset() { changes_.clear(); } |
129 | 127 |
130 virtual void OnCookieChanged( | 128 void OnCookieChanged(const CanonicalCookie& cookie, |
131 const CanonicalCookie& cookie, | 129 bool removed, |
132 bool removed, | 130 CookieMonsterDelegate::ChangeCause cause) override; |
133 CookieMonsterDelegate::ChangeCause cause) override; | |
134 | 131 |
135 virtual void OnLoaded() override; | 132 void OnLoaded() override; |
136 | 133 |
137 private: | 134 private: |
138 virtual ~MockCookieMonsterDelegate(); | 135 ~MockCookieMonsterDelegate() override; |
139 | 136 |
140 std::vector<CookieNotification> changes_; | 137 std::vector<CookieNotification> changes_; |
141 | 138 |
142 DISALLOW_COPY_AND_ASSIGN(MockCookieMonsterDelegate); | 139 DISALLOW_COPY_AND_ASSIGN(MockCookieMonsterDelegate); |
143 }; | 140 }; |
144 | 141 |
145 // Helper to build a single CanonicalCookie. | 142 // Helper to build a single CanonicalCookie. |
146 CanonicalCookie BuildCanonicalCookie(const std::string& key, | 143 CanonicalCookie BuildCanonicalCookie(const std::string& key, |
147 const std::string& cookie_line, | 144 const std::string& cookie_line, |
148 const base::Time& creation_time); | 145 const base::Time& creation_time); |
149 | 146 |
150 // Helper to build a list of CanonicalCookie*s. | 147 // Helper to build a list of CanonicalCookie*s. |
151 void AddCookieToList( | 148 void AddCookieToList( |
152 const std::string& key, | 149 const std::string& key, |
153 const std::string& cookie_line, | 150 const std::string& cookie_line, |
154 const base::Time& creation_time, | 151 const base::Time& creation_time, |
155 std::vector<CanonicalCookie*>* out_list); | 152 std::vector<CanonicalCookie*>* out_list); |
156 | 153 |
157 // Just act like a backing database. Keep cookie information from | 154 // Just act like a backing database. Keep cookie information from |
158 // Add/Update/Delete and regurgitate it when Load is called. | 155 // Add/Update/Delete and regurgitate it when Load is called. |
159 class MockSimplePersistentCookieStore | 156 class MockSimplePersistentCookieStore |
160 : public CookieMonster::PersistentCookieStore { | 157 : public CookieMonster::PersistentCookieStore { |
161 public: | 158 public: |
162 MockSimplePersistentCookieStore(); | 159 MockSimplePersistentCookieStore(); |
163 | 160 |
164 virtual void Load(const LoadedCallback& loaded_callback) override; | 161 void Load(const LoadedCallback& loaded_callback) override; |
165 | 162 |
166 virtual void LoadCookiesForKey(const std::string& key, | 163 void LoadCookiesForKey(const std::string& key, |
167 const LoadedCallback& loaded_callback) override; | 164 const LoadedCallback& loaded_callback) override; |
168 | 165 |
169 virtual void AddCookie(const CanonicalCookie& cookie) override; | 166 void AddCookie(const CanonicalCookie& cookie) override; |
170 | 167 |
171 virtual void UpdateCookieAccessTime(const CanonicalCookie& cookie) override; | 168 void UpdateCookieAccessTime(const CanonicalCookie& cookie) override; |
172 | 169 |
173 virtual void DeleteCookie(const CanonicalCookie& cookie) override; | 170 void DeleteCookie(const CanonicalCookie& cookie) override; |
174 | 171 |
175 virtual void Flush(const base::Closure& callback) override; | 172 void Flush(const base::Closure& callback) override; |
176 | 173 |
177 virtual void SetForceKeepSessionState() override; | 174 void SetForceKeepSessionState() override; |
178 | 175 |
179 protected: | 176 protected: |
180 virtual ~MockSimplePersistentCookieStore(); | 177 ~MockSimplePersistentCookieStore() override; |
181 | 178 |
182 private: | 179 private: |
183 typedef std::map<int64, CanonicalCookie> CanonicalCookieMap; | 180 typedef std::map<int64, CanonicalCookie> CanonicalCookieMap; |
184 | 181 |
185 CanonicalCookieMap cookies_; | 182 CanonicalCookieMap cookies_; |
186 | 183 |
187 // Indicates if the store has been fully loaded to avoid return duplicate | 184 // Indicates if the store has been fully loaded to avoid return duplicate |
188 // cookies in subsequent load requests | 185 // cookies in subsequent load requests |
189 bool loaded_; | 186 bool loaded_; |
190 }; | 187 }; |
191 | 188 |
192 // Helper function for creating a CookieMonster backed by a | 189 // Helper function for creating a CookieMonster backed by a |
193 // MockSimplePersistentCookieStore for garbage collection testing. | 190 // MockSimplePersistentCookieStore for garbage collection testing. |
194 // | 191 // |
195 // Fill the store through import with |num_cookies| cookies, |num_old_cookies| | 192 // Fill the store through import with |num_cookies| cookies, |num_old_cookies| |
196 // with access time Now()-days_old, the rest with access time Now(). | 193 // with access time Now()-days_old, the rest with access time Now(). |
197 // Do two SetCookies(). Return whether each of the two SetCookies() took | 194 // Do two SetCookies(). Return whether each of the two SetCookies() took |
198 // longer than |gc_perf_micros| to complete, and how many cookie were | 195 // longer than |gc_perf_micros| to complete, and how many cookie were |
199 // left in the store afterwards. | 196 // left in the store afterwards. |
200 CookieMonster* CreateMonsterFromStoreForGC( | 197 CookieMonster* CreateMonsterFromStoreForGC( |
201 int num_cookies, | 198 int num_cookies, |
202 int num_old_cookies, | 199 int num_old_cookies, |
203 int days_old); | 200 int days_old); |
204 | 201 |
205 } // namespace net | 202 } // namespace net |
206 | 203 |
207 #endif // NET_COOKIES_COOKIE_MONSTER_STORE_TEST_H_ | 204 #endif // NET_COOKIES_COOKIE_MONSTER_STORE_TEST_H_ |
OLD | NEW |