ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / net / cookies / cookie_monster_store_test.cc
blob68abc19dfe5826220d109ca319761cc035c105f2
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 #include "net/cookies/cookie_monster_store_test.h"
7 #include "base/bind.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/time/time.h"
11 #include "net/cookies/cookie_constants.h"
12 #include "net/cookies/cookie_util.h"
13 #include "net/cookies/parsed_cookie.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "url/gurl.h"
17 namespace net {
18 LoadedCallbackTask::LoadedCallbackTask(LoadedCallback loaded_callback,
19 std::vector<CanonicalCookie*> cookies)
20 : loaded_callback_(loaded_callback), cookies_(cookies) {
23 LoadedCallbackTask::~LoadedCallbackTask() {
26 MockPersistentCookieStore::MockPersistentCookieStore()
27 : load_return_value_(true), loaded_(false) {
30 void MockPersistentCookieStore::SetLoadExpectation(
31 bool return_value,
32 const std::vector<CanonicalCookie*>& result) {
33 load_return_value_ = return_value;
34 load_result_ = result;
37 void MockPersistentCookieStore::Load(const LoadedCallback& loaded_callback) {
38 std::vector<CanonicalCookie*> out_cookies;
39 if (load_return_value_) {
40 out_cookies = load_result_;
41 loaded_ = true;
43 base::MessageLoop::current()->PostTask(
44 FROM_HERE,
45 base::Bind(&LoadedCallbackTask::Run,
46 new LoadedCallbackTask(loaded_callback, out_cookies)));
49 void MockPersistentCookieStore::LoadCookiesForKey(
50 const std::string& key,
51 const LoadedCallback& loaded_callback) {
52 if (!loaded_) {
53 Load(loaded_callback);
54 } else {
55 base::MessageLoop::current()->PostTask(
56 FROM_HERE,
57 base::Bind(&LoadedCallbackTask::Run,
58 new LoadedCallbackTask(loaded_callback,
59 std::vector<CanonicalCookie*>())));
63 void MockPersistentCookieStore::AddCookie(const CanonicalCookie& cookie) {
64 commands_.push_back(CookieStoreCommand(CookieStoreCommand::ADD, cookie));
67 void MockPersistentCookieStore::UpdateCookieAccessTime(
68 const CanonicalCookie& cookie) {
69 commands_.push_back(
70 CookieStoreCommand(CookieStoreCommand::UPDATE_ACCESS_TIME, cookie));
73 void MockPersistentCookieStore::DeleteCookie(const CanonicalCookie& cookie) {
74 commands_.push_back(CookieStoreCommand(CookieStoreCommand::REMOVE, cookie));
77 void MockPersistentCookieStore::Flush(const base::Closure& callback) {
78 if (!callback.is_null())
79 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
82 void MockPersistentCookieStore::SetForceKeepSessionState() {
85 MockPersistentCookieStore::~MockPersistentCookieStore() {
88 MockCookieMonsterDelegate::MockCookieMonsterDelegate() {
91 void MockCookieMonsterDelegate::OnCookieChanged(
92 const CanonicalCookie& cookie,
93 bool removed,
94 CookieMonster::Delegate::ChangeCause cause) {
95 CookieNotification notification(cookie, removed);
96 changes_.push_back(notification);
99 void MockCookieMonsterDelegate::OnLoaded() {
102 MockCookieMonsterDelegate::~MockCookieMonsterDelegate() {
105 CanonicalCookie BuildCanonicalCookie(const std::string& key,
106 const std::string& cookie_line,
107 const base::Time& creation_time) {
108 // Parse the cookie line.
109 ParsedCookie pc(cookie_line);
110 EXPECT_TRUE(pc.IsValid());
112 // This helper is simplistic in interpreting a parsed cookie, in order to
113 // avoid duplicated CookieMonster's CanonPath() and CanonExpiration()
114 // functions. Would be nice to export them, and re-use here.
115 EXPECT_FALSE(pc.HasMaxAge());
116 EXPECT_TRUE(pc.HasPath());
117 base::Time cookie_expires = pc.HasExpires()
118 ? cookie_util::ParseCookieTime(pc.Expires())
119 : base::Time();
120 std::string cookie_path = pc.Path();
122 return CanonicalCookie(GURL(), pc.Name(), pc.Value(), key, cookie_path,
123 creation_time, cookie_expires, creation_time,
124 pc.IsSecure(), pc.IsHttpOnly(), pc.IsFirstPartyOnly(),
125 pc.Priority());
128 void AddCookieToList(const std::string& key,
129 const std::string& cookie_line,
130 const base::Time& creation_time,
131 std::vector<CanonicalCookie*>* out_list) {
132 scoped_ptr<CanonicalCookie> cookie(new CanonicalCookie(
133 BuildCanonicalCookie(key, cookie_line, creation_time)));
135 out_list->push_back(cookie.release());
138 MockSimplePersistentCookieStore::MockSimplePersistentCookieStore()
139 : loaded_(false) {
142 void MockSimplePersistentCookieStore::Load(
143 const LoadedCallback& loaded_callback) {
144 std::vector<CanonicalCookie*> out_cookies;
146 for (CanonicalCookieMap::const_iterator it = cookies_.begin();
147 it != cookies_.end(); it++)
148 out_cookies.push_back(new CanonicalCookie(it->second));
150 base::MessageLoop::current()->PostTask(
151 FROM_HERE,
152 base::Bind(&LoadedCallbackTask::Run,
153 new LoadedCallbackTask(loaded_callback, out_cookies)));
154 loaded_ = true;
157 void MockSimplePersistentCookieStore::LoadCookiesForKey(
158 const std::string& key,
159 const LoadedCallback& loaded_callback) {
160 if (!loaded_) {
161 Load(loaded_callback);
162 } else {
163 base::MessageLoop::current()->PostTask(
164 FROM_HERE,
165 base::Bind(&LoadedCallbackTask::Run,
166 new LoadedCallbackTask(loaded_callback,
167 std::vector<CanonicalCookie*>())));
171 void MockSimplePersistentCookieStore::AddCookie(const CanonicalCookie& cookie) {
172 int64 creation_time = cookie.CreationDate().ToInternalValue();
173 EXPECT_TRUE(cookies_.find(creation_time) == cookies_.end());
174 cookies_[creation_time] = cookie;
177 void MockSimplePersistentCookieStore::UpdateCookieAccessTime(
178 const CanonicalCookie& cookie) {
179 int64 creation_time = cookie.CreationDate().ToInternalValue();
180 ASSERT_TRUE(cookies_.find(creation_time) != cookies_.end());
181 cookies_[creation_time].SetLastAccessDate(base::Time::Now());
184 void MockSimplePersistentCookieStore::DeleteCookie(
185 const CanonicalCookie& cookie) {
186 int64 creation_time = cookie.CreationDate().ToInternalValue();
187 CanonicalCookieMap::iterator it = cookies_.find(creation_time);
188 ASSERT_TRUE(it != cookies_.end());
189 cookies_.erase(it);
192 void MockSimplePersistentCookieStore::Flush(const base::Closure& callback) {
193 if (!callback.is_null())
194 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
197 void MockSimplePersistentCookieStore::SetForceKeepSessionState() {
200 CookieMonster* CreateMonsterFromStoreForGC(int num_cookies,
201 int num_old_cookies,
202 int days_old) {
203 base::Time current(base::Time::Now());
204 base::Time past_creation(base::Time::Now() - base::TimeDelta::FromDays(1000));
205 scoped_refptr<MockSimplePersistentCookieStore> store(
206 new MockSimplePersistentCookieStore);
207 // Must expire to be persistent
208 for (int i = 0; i < num_cookies; i++) {
209 base::Time creation_time =
210 past_creation + base::TimeDelta::FromMicroseconds(i);
211 base::Time expiration_time = current + base::TimeDelta::FromDays(30);
212 base::Time last_access_time =
213 (i < num_old_cookies) ? current - base::TimeDelta::FromDays(days_old)
214 : current;
216 CanonicalCookie cc(GURL(), "a", "1", base::StringPrintf("h%05d.izzle", i),
217 "/path", creation_time, expiration_time,
218 last_access_time, false, false, false,
219 COOKIE_PRIORITY_DEFAULT);
220 store->AddCookie(cc);
223 return new CookieMonster(store.get(), NULL);
226 MockSimplePersistentCookieStore::~MockSimplePersistentCookieStore() {
229 } // namespace net