OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/base/cookie_monster_store_test.h" | 5 #include "net/base/cookie_monster_store_test.h" |
6 | 6 |
| 7 #include "base/bind.h" |
7 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
8 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
9 #include "base/time.h" | 10 #include "base/time.h" |
10 #include "googleurl/src/gurl.h" | 11 #include "googleurl/src/gurl.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
12 | 13 |
13 namespace net { | 14 namespace net { |
| 15 LoadedCallbackTask::LoadedCallbackTask(LoadedCallback loaded_callback, |
| 16 std::vector<CookieMonster::CanonicalCookie*> cookies) |
| 17 : loaded_callback_(loaded_callback), |
| 18 cookies_(cookies) { |
| 19 } |
| 20 |
| 21 LoadedCallbackTask::~LoadedCallbackTask() {} |
14 | 22 |
15 MockPersistentCookieStore::MockPersistentCookieStore() | 23 MockPersistentCookieStore::MockPersistentCookieStore() |
16 : load_return_value_(true) { | 24 : load_return_value_(true), |
| 25 loaded_(false) { |
17 } | 26 } |
18 | 27 |
19 MockPersistentCookieStore::~MockPersistentCookieStore() {} | 28 MockPersistentCookieStore::~MockPersistentCookieStore() {} |
20 | 29 |
21 void MockPersistentCookieStore::SetLoadExpectation( | 30 void MockPersistentCookieStore::SetLoadExpectation( |
22 bool return_value, | 31 bool return_value, |
23 const std::vector<CookieMonster::CanonicalCookie*>& result) { | 32 const std::vector<CookieMonster::CanonicalCookie*>& result) { |
24 load_return_value_ = return_value; | 33 load_return_value_ = return_value; |
25 load_result_ = result; | 34 load_result_ = result; |
26 } | 35 } |
27 | 36 |
28 bool MockPersistentCookieStore::Load(const LoadedCallback& loaded_callback) { | 37 void MockPersistentCookieStore::Load(const LoadedCallback& loaded_callback) { |
29 bool ok = load_return_value_; | |
30 std::vector<CookieMonster::CanonicalCookie*> out_cookies; | 38 std::vector<CookieMonster::CanonicalCookie*> out_cookies; |
31 if (ok) { | 39 if (load_return_value_) { |
32 out_cookies = load_result_; | 40 out_cookies = load_result_; |
| 41 loaded_ = true; |
33 } | 42 } |
34 loaded_callback.Run(out_cookies); | 43 MessageLoop::current()->PostTask(FROM_HERE, |
35 return ok; | 44 base::Bind(&LoadedCallbackTask::Run, |
| 45 new LoadedCallbackTask(loaded_callback, out_cookies))); |
| 46 } |
| 47 |
| 48 void MockPersistentCookieStore::LoadCookiesForKey(const std::string& key, |
| 49 const LoadedCallback& loaded_callback) { |
| 50 if (!loaded_) { |
| 51 Load(loaded_callback); |
| 52 } else { |
| 53 MessageLoop::current()->PostTask(FROM_HERE, |
| 54 base::Bind(&LoadedCallbackTask::Run, |
| 55 new LoadedCallbackTask(loaded_callback, |
| 56 std::vector<CookieMonster::CanonicalCookie*>()))); |
| 57 } |
36 } | 58 } |
37 | 59 |
38 void MockPersistentCookieStore::AddCookie( | 60 void MockPersistentCookieStore::AddCookie( |
39 const CookieMonster::CanonicalCookie& cookie) { | 61 const CookieMonster::CanonicalCookie& cookie) { |
40 commands_.push_back( | 62 commands_.push_back( |
41 CookieStoreCommand(CookieStoreCommand::ADD, cookie)); | 63 CookieStoreCommand(CookieStoreCommand::ADD, cookie)); |
42 } | 64 } |
43 | 65 |
44 void MockPersistentCookieStore::UpdateCookieAccessTime( | 66 void MockPersistentCookieStore::UpdateCookieAccessTime( |
45 const CookieMonster::CanonicalCookie& cookie) { | 67 const CookieMonster::CanonicalCookie& cookie) { |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 std::string cookie_path = pc.Path(); | 128 std::string cookie_path = pc.Path(); |
107 | 129 |
108 return CookieMonster::CanonicalCookie( | 130 return CookieMonster::CanonicalCookie( |
109 GURL(), pc.Name(), pc.Value(), key, cookie_path, | 131 GURL(), pc.Name(), pc.Value(), key, cookie_path, |
110 pc.MACKey(), pc.MACAlgorithm(), | 132 pc.MACKey(), pc.MACAlgorithm(), |
111 creation_time, creation_time, cookie_expires, | 133 creation_time, creation_time, cookie_expires, |
112 pc.IsSecure(), pc.IsHttpOnly(), | 134 pc.IsSecure(), pc.IsHttpOnly(), |
113 !cookie_expires.is_null()); | 135 !cookie_expires.is_null()); |
114 } | 136 } |
115 | 137 |
116 MockSimplePersistentCookieStore::MockSimplePersistentCookieStore() {} | 138 MockSimplePersistentCookieStore::MockSimplePersistentCookieStore() |
| 139 : loaded_(false) {} |
117 | 140 |
118 MockSimplePersistentCookieStore::~MockSimplePersistentCookieStore() {} | 141 MockSimplePersistentCookieStore::~MockSimplePersistentCookieStore() {} |
119 | 142 |
120 bool MockSimplePersistentCookieStore::Load( | 143 void MockSimplePersistentCookieStore::Load( |
121 const LoadedCallback& loaded_callback) { | 144 const LoadedCallback& loaded_callback) { |
122 std::vector<CookieMonster::CanonicalCookie*> out_cookies; | 145 std::vector<CookieMonster::CanonicalCookie*> out_cookies; |
| 146 |
123 for (CanonicalCookieMap::const_iterator it = cookies_.begin(); | 147 for (CanonicalCookieMap::const_iterator it = cookies_.begin(); |
124 it != cookies_.end(); it++) | 148 it != cookies_.end(); it++) |
125 out_cookies.push_back( | 149 out_cookies.push_back( |
126 new CookieMonster::CanonicalCookie(it->second)); | 150 new CookieMonster::CanonicalCookie(it->second)); |
127 loaded_callback.Run(out_cookies); | 151 |
128 return true; | 152 MessageLoop::current()->PostTask(FROM_HERE, |
| 153 base::Bind(&LoadedCallbackTask::Run, |
| 154 new LoadedCallbackTask(loaded_callback, out_cookies))); |
| 155 loaded_ = true; |
| 156 } |
| 157 |
| 158 void MockSimplePersistentCookieStore::LoadCookiesForKey(const std::string& key, |
| 159 const LoadedCallback& loaded_callback) { |
| 160 if (!loaded_) { |
| 161 Load(loaded_callback); |
| 162 } else { |
| 163 MessageLoop::current()->PostTask(FROM_HERE, |
| 164 base::Bind(&LoadedCallbackTask::Run, |
| 165 new LoadedCallbackTask(loaded_callback, |
| 166 std::vector<CookieMonster::CanonicalCookie*>()))); |
| 167 } |
129 } | 168 } |
130 | 169 |
131 void MockSimplePersistentCookieStore::AddCookie( | 170 void MockSimplePersistentCookieStore::AddCookie( |
132 const CookieMonster::CanonicalCookie& cookie) { | 171 const CookieMonster::CanonicalCookie& cookie) { |
133 int64 creation_time = cookie.CreationDate().ToInternalValue(); | 172 int64 creation_time = cookie.CreationDate().ToInternalValue(); |
134 EXPECT_TRUE(cookies_.find(creation_time) == cookies_.end()); | 173 EXPECT_TRUE(cookies_.find(creation_time) == cookies_.end()); |
135 cookies_[creation_time] = cookie; | 174 cookies_[creation_time] = cookie; |
136 } | 175 } |
137 | 176 |
138 void MockSimplePersistentCookieStore::UpdateCookieAccessTime( | 177 void MockSimplePersistentCookieStore::UpdateCookieAccessTime( |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 GURL(), "a", "1", base::StringPrintf("h%05d.izzle", i), "/path", | 222 GURL(), "a", "1", base::StringPrintf("h%05d.izzle", i), "/path", |
184 mac_key, mac_algorithm, creation_time, expiration_time, | 223 mac_key, mac_algorithm, creation_time, expiration_time, |
185 last_access_time, false, false, true); | 224 last_access_time, false, false, true); |
186 store->AddCookie(cc); | 225 store->AddCookie(cc); |
187 } | 226 } |
188 | 227 |
189 return new CookieMonster(store, NULL); | 228 return new CookieMonster(store, NULL); |
190 } | 229 } |
191 | 230 |
192 } // namespace net | 231 } // namespace net |
OLD | NEW |