[Password Autofill] Update bug number for fixing one click popup on Android.
[chromium-blink-merge.git] / net / cookies / cookie_monster_store_test.cc
blob226242a6e503c78d8f6816bd6356928145b53f82
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),
21 cookies_(cookies) {
24 LoadedCallbackTask::~LoadedCallbackTask() {}
26 MockPersistentCookieStore::MockPersistentCookieStore()
27 : load_return_value_(true),
28 loaded_(false) {
31 void MockPersistentCookieStore::SetLoadExpectation(
32 bool return_value,
33 const std::vector<CanonicalCookie*>& result) {
34 load_return_value_ = return_value;
35 load_result_ = result;
38 void MockPersistentCookieStore::Load(const LoadedCallback& loaded_callback) {
39 std::vector<CanonicalCookie*> out_cookies;
40 if (load_return_value_) {
41 out_cookies = load_result_;
42 loaded_ = true;
44 base::MessageLoop::current()->PostTask(
45 FROM_HERE,
46 base::Bind(&LoadedCallbackTask::Run,
47 new LoadedCallbackTask(loaded_callback, out_cookies)));
50 void MockPersistentCookieStore::LoadCookiesForKey(
51 const std::string& key,
52 const LoadedCallback& loaded_callback) {
53 if (!loaded_) {
54 Load(loaded_callback);
55 } else {
56 base::MessageLoop::current()->PostTask(
57 FROM_HERE,
58 base::Bind(&LoadedCallbackTask::Run,
59 new LoadedCallbackTask(loaded_callback,
60 std::vector<CanonicalCookie*>())));
64 void MockPersistentCookieStore::AddCookie(const CanonicalCookie& cookie) {
65 commands_.push_back(
66 CookieStoreCommand(CookieStoreCommand::ADD, cookie));
69 void MockPersistentCookieStore::UpdateCookieAccessTime(
70 const CanonicalCookie& cookie) {
71 commands_.push_back(CookieStoreCommand(
72 CookieStoreCommand::UPDATE_ACCESS_TIME, cookie));
75 void MockPersistentCookieStore::DeleteCookie(const CanonicalCookie& cookie) {
76 commands_.push_back(
77 CookieStoreCommand(CookieStoreCommand::REMOVE, cookie));
80 void MockPersistentCookieStore::Flush(const base::Closure& callback) {
81 if (!callback.is_null())
82 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
85 void MockPersistentCookieStore::SetForceKeepSessionState() {
88 MockPersistentCookieStore::~MockPersistentCookieStore() {}
90 MockCookieMonsterDelegate::MockCookieMonsterDelegate() {}
92 void MockCookieMonsterDelegate::OnCookieChanged(
93 const CanonicalCookie& cookie,
94 bool removed,
95 CookieMonster::Delegate::ChangeCause cause) {
96 CookieNotification notification(cookie, removed);
97 changes_.push_back(notification);
100 void MockCookieMonsterDelegate::OnLoaded() {}
102 MockCookieMonsterDelegate::~MockCookieMonsterDelegate() {}
104 CanonicalCookie BuildCanonicalCookie(const std::string& key,
105 const std::string& cookie_line,
106 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()) : base::Time();
119 std::string cookie_path = pc.Path();
121 return CanonicalCookie(
122 GURL(), pc.Name(), pc.Value(), key, cookie_path,
123 creation_time, cookie_expires, creation_time,
124 pc.IsSecure(), pc.IsHttpOnly(), pc.Priority());
127 void AddCookieToList(
128 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(
133 new CanonicalCookie(
134 BuildCanonicalCookie(key, cookie_line, creation_time)));
136 out_list->push_back(cookie.release());
139 MockSimplePersistentCookieStore::MockSimplePersistentCookieStore()
140 : loaded_(false) {
143 void MockSimplePersistentCookieStore::Load(
144 const LoadedCallback& loaded_callback) {
145 std::vector<CanonicalCookie*> out_cookies;
147 for (CanonicalCookieMap::const_iterator it = cookies_.begin();
148 it != cookies_.end(); it++)
149 out_cookies.push_back(new CanonicalCookie(it->second));
151 base::MessageLoop::current()->PostTask(
152 FROM_HERE,
153 base::Bind(&LoadedCallbackTask::Run,
154 new LoadedCallbackTask(loaded_callback, out_cookies)));
155 loaded_ = true;
158 void MockSimplePersistentCookieStore::LoadCookiesForKey(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(
201 int num_cookies,
202 int num_old_cookies,
203 int days_old) {
204 base::Time current(base::Time::Now());
205 base::Time past_creation(base::Time::Now() - base::TimeDelta::FromDays(1000));
206 scoped_refptr<MockSimplePersistentCookieStore> store(
207 new MockSimplePersistentCookieStore);
208 // Must expire to be persistent
209 for (int i = 0; i < num_cookies; i++) {
210 base::Time creation_time =
211 past_creation + base::TimeDelta::FromMicroseconds(i);
212 base::Time expiration_time = current + base::TimeDelta::FromDays(30);
213 base::Time last_access_time =
214 (i < num_old_cookies) ? current - base::TimeDelta::FromDays(days_old) :
215 current;
217 CanonicalCookie cc(
218 GURL(), "a", "1", base::StringPrintf("h%05d.izzle", i), "/path",
219 creation_time, expiration_time, last_access_time, false, false,
220 COOKIE_PRIORITY_DEFAULT);
221 store->AddCookie(cc);
224 return new CookieMonster(store.get(), NULL);
227 MockSimplePersistentCookieStore::~MockSimplePersistentCookieStore() {}
229 } // namespace net