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