ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / net / cookies / cookie_monster_store_test.h
blob4c4d9df2f52b0c09adb95675e4850aecee920e20
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_
13 #include <map>
14 #include <string>
15 #include <utility>
16 #include <vector>
17 #include "net/cookies/canonical_cookie.h"
18 #include "net/cookies/cookie_monster.h"
20 namespace base {
21 class Time;
24 namespace net {
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;
34 LoadedCallbackTask(LoadedCallback loaded_callback,
35 std::vector<CanonicalCookie*> cookies);
37 void Run() { loaded_callback_.Run(cookies_); }
39 private:
40 friend class base::RefCountedThreadSafe<LoadedCallbackTask>;
41 ~LoadedCallbackTask();
43 LoadedCallback loaded_callback_;
44 std::vector<CanonicalCookie*> cookies_;
46 DISALLOW_COPY_AND_ASSIGN(LoadedCallbackTask);
47 }; // Wrapper class LoadedCallbackTask
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,
57 CookieStoreCommand(Type type, const CanonicalCookie& cookie)
58 : type(type), cookie(cookie) {}
60 Type type;
61 CanonicalCookie cookie;
64 // Implementation of PersistentCookieStore that captures the
65 // received commands and saves them to a list.
66 // The result of calls to Load() can be configured using SetLoadExpectation().
67 class MockPersistentCookieStore : public CookieMonster::PersistentCookieStore {
68 public:
69 typedef std::vector<CookieStoreCommand> CommandList;
71 MockPersistentCookieStore();
73 void SetLoadExpectation(bool return_value,
74 const std::vector<CanonicalCookie*>& result);
76 const CommandList& commands() const { return commands_; }
78 void Load(const LoadedCallback& loaded_callback) override;
80 void LoadCookiesForKey(const std::string& key,
81 const LoadedCallback& loaded_callback) override;
83 void AddCookie(const CanonicalCookie& cookie) override;
85 void UpdateCookieAccessTime(const CanonicalCookie& cookie) override;
87 void DeleteCookie(const CanonicalCookie& cookie) override;
89 void Flush(const base::Closure& callback) override;
91 void SetForceKeepSessionState() override;
93 protected:
94 ~MockPersistentCookieStore() override;
96 private:
97 CommandList commands_;
99 // Deferred result to use when Load() is called.
100 bool load_return_value_;
101 std::vector<CanonicalCookie*> load_result_;
102 // Indicates if the store has been fully loaded to avoid returning duplicate
103 // cookies.
104 bool loaded_;
106 DISALLOW_COPY_AND_ASSIGN(MockPersistentCookieStore);
109 // Mock for CookieMonsterDelegate
110 class MockCookieMonsterDelegate : public CookieMonsterDelegate {
111 public:
112 typedef std::pair<CanonicalCookie, bool> CookieNotification;
114 MockCookieMonsterDelegate();
116 const std::vector<CookieNotification>& changes() const { return changes_; }
118 void reset() { changes_.clear(); }
120 void OnCookieChanged(const CanonicalCookie& cookie,
121 bool removed,
122 CookieMonsterDelegate::ChangeCause cause) override;
124 void OnLoaded() override;
126 private:
127 ~MockCookieMonsterDelegate() override;
129 std::vector<CookieNotification> changes_;
131 DISALLOW_COPY_AND_ASSIGN(MockCookieMonsterDelegate);
134 // Helper to build a single CanonicalCookie.
135 CanonicalCookie BuildCanonicalCookie(const std::string& key,
136 const std::string& cookie_line,
137 const base::Time& creation_time);
139 // Helper to build a list of CanonicalCookie*s.
140 void AddCookieToList(const std::string& key,
141 const std::string& cookie_line,
142 const base::Time& creation_time,
143 std::vector<CanonicalCookie*>* out_list);
145 // Just act like a backing database. Keep cookie information from
146 // Add/Update/Delete and regurgitate it when Load is called.
147 class MockSimplePersistentCookieStore
148 : public CookieMonster::PersistentCookieStore {
149 public:
150 MockSimplePersistentCookieStore();
152 void Load(const LoadedCallback& loaded_callback) override;
154 void LoadCookiesForKey(const std::string& key,
155 const LoadedCallback& loaded_callback) override;
157 void AddCookie(const CanonicalCookie& cookie) override;
159 void UpdateCookieAccessTime(const CanonicalCookie& cookie) override;
161 void DeleteCookie(const CanonicalCookie& cookie) override;
163 void Flush(const base::Closure& callback) override;
165 void SetForceKeepSessionState() override;
167 protected:
168 ~MockSimplePersistentCookieStore() override;
170 private:
171 typedef std::map<int64, CanonicalCookie> CanonicalCookieMap;
173 CanonicalCookieMap cookies_;
175 // Indicates if the store has been fully loaded to avoid return duplicate
176 // cookies in subsequent load requests
177 bool loaded_;
180 // Helper function for creating a CookieMonster backed by a
181 // MockSimplePersistentCookieStore for garbage collection testing.
183 // Fill the store through import with |num_cookies| cookies, |num_old_cookies|
184 // with access time Now()-days_old, the rest with access time Now().
185 // Do two SetCookies(). Return whether each of the two SetCookies() took
186 // longer than |gc_perf_micros| to complete, and how many cookie were
187 // left in the store afterwards.
188 CookieMonster* CreateMonsterFromStoreForGC(int num_cookies,
189 int num_old_cookies,
190 int days_old);
192 } // namespace net
194 #endif // NET_COOKIES_COOKIE_MONSTER_STORE_TEST_H_