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