| 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 // Provides a temporary redirection while clients are updated to use the new |
| 6 // (current cookie_monster_unittest.cc and cookie_monster_perftest.cc) | 6 // path. |
| 7 // that need to test out CookieMonster interactions with the backing store. | |
| 8 // It should only be included by test code. | |
| 9 | 7 |
| 10 #ifndef NET_BASE_COOKIE_MONSTER_STORE_TEST_H_ | 8 #ifndef NET_BASE_COOKIE_MONSTER_STORE_TEST_H_ |
| 11 #define NET_BASE_COOKIE_MONSTER_STORE_TEST_H_ | 9 #define NET_BASE_COOKIE_MONSTER_STORE_TEST_H_ |
| 12 #pragma once | 10 #pragma once |
| 13 | 11 |
| 14 #include <map> | 12 #include "net/cookies/cookie_monster_store_test.h" |
| 15 #include <string> | |
| 16 #include <utility> | |
| 17 #include <vector> | |
| 18 #include "net/base/cookie_monster.h" | |
| 19 | |
| 20 namespace base { | |
| 21 class Time; | |
| 22 } | |
| 23 | |
| 24 namespace net { | |
| 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 | |
| 49 // Describes a call to one of the 3 functions of PersistentCookieStore. | |
| 50 struct CookieStoreCommand { | |
| 51 enum Type { | |
| 52 ADD, | |
| 53 UPDATE_ACCESS_TIME, | |
| 54 REMOVE, | |
| 55 }; | |
| 56 | |
| 57 CookieStoreCommand(Type type, | |
| 58 const CookieMonster::CanonicalCookie& cookie) | |
| 59 : type(type), | |
| 60 cookie(cookie) {} | |
| 61 | |
| 62 Type type; | |
| 63 CookieMonster::CanonicalCookie cookie; | |
| 64 }; | |
| 65 | |
| 66 // Implementation of PersistentCookieStore that captures the | |
| 67 // received commands and saves them to a list. | |
| 68 // The result of calls to Load() can be configured using SetLoadExpectation(). | |
| 69 class MockPersistentCookieStore | |
| 70 : public CookieMonster::PersistentCookieStore { | |
| 71 public: | |
| 72 typedef std::vector<CookieStoreCommand> CommandList; | |
| 73 | |
| 74 MockPersistentCookieStore(); | |
| 75 virtual ~MockPersistentCookieStore(); | |
| 76 | |
| 77 void SetLoadExpectation( | |
| 78 bool return_value, | |
| 79 const std::vector<CookieMonster::CanonicalCookie*>& result); | |
| 80 | |
| 81 const CommandList& commands() const { | |
| 82 return commands_; | |
| 83 } | |
| 84 | |
| 85 virtual void Load(const LoadedCallback& loaded_callback) OVERRIDE; | |
| 86 | |
| 87 virtual void LoadCookiesForKey(const std::string& key, | |
| 88 const LoadedCallback& loaded_callback) OVERRIDE; | |
| 89 | |
| 90 virtual void AddCookie(const CookieMonster::CanonicalCookie& cookie) OVERRIDE; | |
| 91 | |
| 92 virtual void UpdateCookieAccessTime( | |
| 93 const CookieMonster::CanonicalCookie& cookie) OVERRIDE; | |
| 94 | |
| 95 virtual void DeleteCookie( | |
| 96 const CookieMonster::CanonicalCookie& cookie) OVERRIDE; | |
| 97 | |
| 98 virtual void Flush(const base::Closure& callback) OVERRIDE; | |
| 99 | |
| 100 // No files are created so nothing to clear either | |
| 101 virtual void SetClearLocalStateOnExit(bool clear_local_state) OVERRIDE; | |
| 102 | |
| 103 private: | |
| 104 CommandList commands_; | |
| 105 | |
| 106 // Deferred result to use when Load() is called. | |
| 107 bool load_return_value_; | |
| 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_; | |
| 112 | |
| 113 DISALLOW_COPY_AND_ASSIGN(MockPersistentCookieStore); | |
| 114 }; | |
| 115 | |
| 116 // Mock for CookieMonster::Delegate | |
| 117 class MockCookieMonsterDelegate : public CookieMonster::Delegate { | |
| 118 public: | |
| 119 typedef std::pair<CookieMonster::CanonicalCookie, bool> | |
| 120 CookieNotification; | |
| 121 | |
| 122 MockCookieMonsterDelegate(); | |
| 123 | |
| 124 const std::vector<CookieNotification>& changes() const { return changes_; } | |
| 125 | |
| 126 void reset() { changes_.clear(); } | |
| 127 | |
| 128 virtual void OnCookieChanged( | |
| 129 const CookieMonster::CanonicalCookie& cookie, | |
| 130 bool removed, | |
| 131 CookieMonster::Delegate::ChangeCause cause) OVERRIDE; | |
| 132 | |
| 133 private: | |
| 134 virtual ~MockCookieMonsterDelegate(); | |
| 135 | |
| 136 std::vector<CookieNotification> changes_; | |
| 137 | |
| 138 DISALLOW_COPY_AND_ASSIGN(MockCookieMonsterDelegate); | |
| 139 }; | |
| 140 | |
| 141 // Helper to build a single CanonicalCookie. | |
| 142 CookieMonster::CanonicalCookie BuildCanonicalCookie( | |
| 143 const std::string& key, | |
| 144 const std::string& cookie_line, | |
| 145 const base::Time& creation_time); | |
| 146 | |
| 147 // Helper to build a list of CanonicalCookie*s. | |
| 148 void AddCookieToList( | |
| 149 const std::string& key, | |
| 150 const std::string& cookie_line, | |
| 151 const base::Time& creation_time, | |
| 152 std::vector<CookieMonster::CanonicalCookie*>* out_list); | |
| 153 | |
| 154 // Just act like a backing database. Keep cookie information from | |
| 155 // Add/Update/Delete and regurgitate it when Load is called. | |
| 156 class MockSimplePersistentCookieStore | |
| 157 : public CookieMonster::PersistentCookieStore { | |
| 158 public: | |
| 159 MockSimplePersistentCookieStore(); | |
| 160 virtual ~MockSimplePersistentCookieStore(); | |
| 161 | |
| 162 virtual void Load(const LoadedCallback& loaded_callback) OVERRIDE; | |
| 163 | |
| 164 virtual void LoadCookiesForKey(const std::string& key, | |
| 165 const LoadedCallback& loaded_callback) OVERRIDE; | |
| 166 | |
| 167 virtual void AddCookie( | |
| 168 const CookieMonster::CanonicalCookie& cookie) OVERRIDE; | |
| 169 | |
| 170 virtual void UpdateCookieAccessTime( | |
| 171 const CookieMonster::CanonicalCookie& cookie) OVERRIDE; | |
| 172 | |
| 173 virtual void DeleteCookie( | |
| 174 const CookieMonster::CanonicalCookie& cookie) OVERRIDE; | |
| 175 | |
| 176 virtual void Flush(const base::Closure& callback) OVERRIDE; | |
| 177 | |
| 178 virtual void SetClearLocalStateOnExit(bool clear_local_state) OVERRIDE; | |
| 179 | |
| 180 private: | |
| 181 typedef std::map<int64, CookieMonster::CanonicalCookie> | |
| 182 CanonicalCookieMap; | |
| 183 | |
| 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_; | |
| 189 }; | |
| 190 | |
| 191 // Helper function for creating a CookieMonster backed by a | |
| 192 // MockSimplePersistentCookieStore for garbage collection testing. | |
| 193 // | |
| 194 // 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(). | |
| 196 // Do two SetCookies(). Return whether each of the two SetCookies() took | |
| 197 // longer than |gc_perf_micros| to complete, and how many cookie were | |
| 198 // left in the store afterwards. | |
| 199 CookieMonster* CreateMonsterFromStoreForGC( | |
| 200 int num_cookies, | |
| 201 int num_old_cookies, | |
| 202 int days_old); | |
| 203 | |
| 204 } // namespace net | |
| 205 | 13 |
| 206 #endif // NET_BASE_COOKIE_MONSTER_STORE_TEST_H_ | 14 #endif // NET_BASE_COOKIE_MONSTER_STORE_TEST_H_ |
| OLD | NEW |