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
3 // found in the LICENSE file.
5 // This file contains test infrastructure for multiple files
6 // (current cookie_monster_unittest.cc and cookie_monster_perftest.cc)
7 // that need to test out CookieMonster interactions with the backing store.
8 // It should only be included by test code.
10 #ifndef NET_COOKIES_COOKIE_MONSTER_STORE_TEST_H_
11 #define NET_COOKIES_COOKIE_MONSTER_STORE_TEST_H_
17 #include "net/cookies/canonical_cookie.h"
18 #include "net/cookies/cookie_monster.h"
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
> {
32 typedef CookieMonster::PersistentCookieStore::LoadedCallback LoadedCallback
;
34 LoadedCallbackTask(LoadedCallback loaded_callback
,
35 std::vector
<CanonicalCookie
*> cookies
);
38 loaded_callback_
.Run(cookies_
);
42 friend class base::RefCountedThreadSafe
<LoadedCallbackTask
>;
43 ~LoadedCallbackTask();
45 LoadedCallback loaded_callback_
;
46 std::vector
<CanonicalCookie
*> cookies_
;
48 DISALLOW_COPY_AND_ASSIGN(LoadedCallbackTask
);
49 }; // Wrapper class LoadedCallbackTask
51 // Describes a call to one of the 3 functions of PersistentCookieStore.
52 struct CookieStoreCommand
{
59 CookieStoreCommand(Type type
, const CanonicalCookie
& cookie
)
64 CanonicalCookie cookie
;
67 // Implementation of PersistentCookieStore that captures the
68 // received commands and saves them to a list.
69 // The result of calls to Load() can be configured using SetLoadExpectation().
70 class MockPersistentCookieStore
71 : public CookieMonster::PersistentCookieStore
{
73 typedef std::vector
<CookieStoreCommand
> CommandList
;
75 MockPersistentCookieStore();
77 void SetLoadExpectation(
79 const std::vector
<CanonicalCookie
*>& result
);
81 const CommandList
& commands() const {
85 virtual void Load(const LoadedCallback
& loaded_callback
) OVERRIDE
;
87 virtual void LoadCookiesForKey(const std::string
& key
,
88 const LoadedCallback
& loaded_callback
) OVERRIDE
;
90 virtual void AddCookie(const CanonicalCookie
& cookie
) OVERRIDE
;
92 virtual void UpdateCookieAccessTime(
93 const CanonicalCookie
& cookie
) OVERRIDE
;
95 virtual void DeleteCookie(
96 const CanonicalCookie
& cookie
) OVERRIDE
;
98 virtual void Flush(const base::Closure
& callback
) OVERRIDE
;
100 virtual void SetForceKeepSessionState() OVERRIDE
;
103 virtual ~MockPersistentCookieStore();
106 CommandList commands_
;
108 // Deferred result to use when Load() is called.
109 bool load_return_value_
;
110 std::vector
<CanonicalCookie
*> load_result_
;
111 // Indicates if the store has been fully loaded to avoid returning duplicate
115 DISALLOW_COPY_AND_ASSIGN(MockPersistentCookieStore
);
118 // Mock for CookieMonster::Delegate
119 class MockCookieMonsterDelegate
: public CookieMonster::Delegate
{
121 typedef std::pair
<CanonicalCookie
, bool>
124 MockCookieMonsterDelegate();
126 const std::vector
<CookieNotification
>& changes() const { return changes_
; }
128 void reset() { changes_
.clear(); }
130 virtual void OnCookieChanged(
131 const CanonicalCookie
& cookie
,
133 CookieMonster::Delegate::ChangeCause cause
) OVERRIDE
;
136 virtual ~MockCookieMonsterDelegate();
138 std::vector
<CookieNotification
> changes_
;
140 DISALLOW_COPY_AND_ASSIGN(MockCookieMonsterDelegate
);
143 // Helper to build a single CanonicalCookie.
144 CanonicalCookie
BuildCanonicalCookie(const std::string
& key
,
145 const std::string
& cookie_line
,
146 const base::Time
& creation_time
);
148 // Helper to build a list of CanonicalCookie*s.
149 void AddCookieToList(
150 const std::string
& key
,
151 const std::string
& cookie_line
,
152 const base::Time
& creation_time
,
153 std::vector
<CanonicalCookie
*>* out_list
);
155 // Just act like a backing database. Keep cookie information from
156 // Add/Update/Delete and regurgitate it when Load is called.
157 class MockSimplePersistentCookieStore
158 : public CookieMonster::PersistentCookieStore
{
160 MockSimplePersistentCookieStore();
162 virtual void Load(const LoadedCallback
& loaded_callback
) OVERRIDE
;
164 virtual void LoadCookiesForKey(const std::string
& key
,
165 const LoadedCallback
& loaded_callback
) OVERRIDE
;
167 virtual void AddCookie(const CanonicalCookie
& cookie
) OVERRIDE
;
169 virtual void UpdateCookieAccessTime(const CanonicalCookie
& cookie
) OVERRIDE
;
171 virtual void DeleteCookie(const CanonicalCookie
& cookie
) OVERRIDE
;
173 virtual void Flush(const base::Closure
& callback
) OVERRIDE
;
175 virtual void SetForceKeepSessionState() OVERRIDE
;
178 virtual ~MockSimplePersistentCookieStore();
181 typedef std::map
<int64
, CanonicalCookie
> CanonicalCookieMap
;
183 CanonicalCookieMap cookies_
;
185 // Indicates if the store has been fully loaded to avoid return duplicate
186 // cookies in subsequent load requests
190 // Helper function for creating a CookieMonster backed by a
191 // MockSimplePersistentCookieStore for garbage collection testing.
193 // 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().
195 // Do two SetCookies(). Return whether each of the two SetCookies() took
196 // longer than |gc_perf_micros| to complete, and how many cookie were
197 // left in the store afterwards.
198 CookieMonster
* CreateMonsterFromStoreForGC(
205 #endif // NET_COOKIES_COOKIE_MONSTER_STORE_TEST_H_