Don't send a SHChangeNotify for creating an app icon when creating a shortcut.
[chromium-blink-merge.git] / net / cookies / cookie_monster_store_test.cc
blobede98ecc33b682c52db3f9d17203bc8a1a877e77
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 MockCookieMonsterDelegate::~MockCookieMonsterDelegate() {
102 CanonicalCookie BuildCanonicalCookie(const std::string& key,
103 const std::string& cookie_line,
104 const base::Time& creation_time) {
105 // Parse the cookie line.
106 ParsedCookie pc(cookie_line);
107 EXPECT_TRUE(pc.IsValid());
109 // This helper is simplistic in interpreting a parsed cookie, in order to
110 // avoid duplicated CookieMonster's CanonPath() and CanonExpiration()
111 // functions. Would be nice to export them, and re-use here.
112 EXPECT_FALSE(pc.HasMaxAge());
113 EXPECT_TRUE(pc.HasPath());
114 base::Time cookie_expires = pc.HasExpires()
115 ? cookie_util::ParseCookieTime(pc.Expires())
116 : base::Time();
117 std::string cookie_path = pc.Path();
119 return CanonicalCookie(GURL(), pc.Name(), pc.Value(), key, cookie_path,
120 creation_time, cookie_expires, creation_time,
121 pc.IsSecure(), pc.IsHttpOnly(), pc.IsFirstPartyOnly(),
122 pc.Priority());
125 void AddCookieToList(const std::string& key,
126 const std::string& cookie_line,
127 const base::Time& creation_time,
128 std::vector<CanonicalCookie*>* out_list) {
129 scoped_ptr<CanonicalCookie> cookie(new CanonicalCookie(
130 BuildCanonicalCookie(key, cookie_line, creation_time)));
132 out_list->push_back(cookie.release());
135 MockSimplePersistentCookieStore::MockSimplePersistentCookieStore()
136 : loaded_(false) {
139 void MockSimplePersistentCookieStore::Load(
140 const LoadedCallback& loaded_callback) {
141 std::vector<CanonicalCookie*> out_cookies;
143 for (CanonicalCookieMap::const_iterator it = cookies_.begin();
144 it != cookies_.end(); it++)
145 out_cookies.push_back(new CanonicalCookie(it->second));
147 base::MessageLoop::current()->PostTask(
148 FROM_HERE,
149 base::Bind(&LoadedCallbackTask::Run,
150 new LoadedCallbackTask(loaded_callback, out_cookies)));
151 loaded_ = true;
154 void MockSimplePersistentCookieStore::LoadCookiesForKey(
155 const std::string& key,
156 const LoadedCallback& loaded_callback) {
157 if (!loaded_) {
158 Load(loaded_callback);
159 } else {
160 base::MessageLoop::current()->PostTask(
161 FROM_HERE,
162 base::Bind(&LoadedCallbackTask::Run,
163 new LoadedCallbackTask(loaded_callback,
164 std::vector<CanonicalCookie*>())));
168 void MockSimplePersistentCookieStore::AddCookie(const CanonicalCookie& cookie) {
169 int64 creation_time = cookie.CreationDate().ToInternalValue();
170 EXPECT_TRUE(cookies_.find(creation_time) == cookies_.end());
171 cookies_[creation_time] = cookie;
174 void MockSimplePersistentCookieStore::UpdateCookieAccessTime(
175 const CanonicalCookie& cookie) {
176 int64 creation_time = cookie.CreationDate().ToInternalValue();
177 ASSERT_TRUE(cookies_.find(creation_time) != cookies_.end());
178 cookies_[creation_time].SetLastAccessDate(base::Time::Now());
181 void MockSimplePersistentCookieStore::DeleteCookie(
182 const CanonicalCookie& cookie) {
183 int64 creation_time = cookie.CreationDate().ToInternalValue();
184 CanonicalCookieMap::iterator it = cookies_.find(creation_time);
185 ASSERT_TRUE(it != cookies_.end());
186 cookies_.erase(it);
189 void MockSimplePersistentCookieStore::Flush(const base::Closure& callback) {
190 if (!callback.is_null())
191 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
194 void MockSimplePersistentCookieStore::SetForceKeepSessionState() {
197 CookieMonster* CreateMonsterFromStoreForGC(int num_cookies,
198 int num_old_cookies,
199 int days_old) {
200 base::Time current(base::Time::Now());
201 base::Time past_creation(base::Time::Now() - base::TimeDelta::FromDays(1000));
202 scoped_refptr<MockSimplePersistentCookieStore> store(
203 new MockSimplePersistentCookieStore);
204 // Must expire to be persistent
205 for (int i = 0; i < num_cookies; i++) {
206 base::Time creation_time =
207 past_creation + base::TimeDelta::FromMicroseconds(i);
208 base::Time expiration_time = current + base::TimeDelta::FromDays(30);
209 base::Time last_access_time =
210 (i < num_old_cookies) ? current - base::TimeDelta::FromDays(days_old)
211 : current;
213 CanonicalCookie cc(GURL(), "a", "1", base::StringPrintf("h%05d.izzle", i),
214 "/path", creation_time, expiration_time,
215 last_access_time, false, false, false,
216 COOKIE_PRIORITY_DEFAULT);
217 store->AddCookie(cc);
220 return new CookieMonster(store.get(), NULL);
223 MockSimplePersistentCookieStore::~MockSimplePersistentCookieStore() {
226 } // namespace net