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 // Wrapper class for posting a loaded callback. Since the Callback class is not | |
16 // reference counted, we cannot post a callback to the message loop directly, | |
17 // instead we post a LoadCallbackTask. | |
18 class LoadedCallbackTask | |
19 : public base::RefCountedThreadSafe<LoadedCallbackTask> { | |
20 public: | |
21 typedef CookieMonster::PersistentCookieStore::LoadedCallback LoadedCallback; | |
22 LoadedCallbackTask(LoadedCallback loaded_callback, | |
23 std::vector<CookieMonster::CanonicalCookie*> cookies) | |
24 : loaded_callback_(loaded_callback), cookies_(cookies) {} | |
25 | |
26 void Run() { | |
27 loaded_callback_.Run(cookies_); | |
28 } | |
29 | |
30 private: | |
31 LoadedCallback loaded_callback_; | |
32 std::vector<CookieMonster::CanonicalCookie*> cookies_; | |
33 | |
34 DISALLOW_COPY_AND_ASSIGN(LoadedCallbackTask); | |
35 }; // Wrapper class LoadedCallbackTask | |
14 | 36 |
15 MockPersistentCookieStore::MockPersistentCookieStore() | 37 MockPersistentCookieStore::MockPersistentCookieStore() |
16 : load_return_value_(true) { | 38 : load_return_value_(true), |
39 loaded_(false) { | |
17 } | 40 } |
18 | 41 |
19 MockPersistentCookieStore::~MockPersistentCookieStore() {} | 42 MockPersistentCookieStore::~MockPersistentCookieStore() {} |
20 | 43 |
21 void MockPersistentCookieStore::SetLoadExpectation( | 44 void MockPersistentCookieStore::SetLoadExpectation( |
22 bool return_value, | 45 bool return_value, |
23 const std::vector<CookieMonster::CanonicalCookie*>& result) { | 46 const std::vector<CookieMonster::CanonicalCookie*>& result) { |
24 load_return_value_ = return_value; | 47 load_return_value_ = return_value; |
25 load_result_ = result; | 48 load_result_ = result; |
26 } | 49 } |
27 | 50 |
28 bool MockPersistentCookieStore::Load(const LoadedCallback& loaded_callback) { | 51 void MockPersistentCookieStore::Load(const LoadedCallback& loaded_callback) { |
29 bool ok = load_return_value_; | |
30 std::vector<CookieMonster::CanonicalCookie*> out_cookies; | 52 std::vector<CookieMonster::CanonicalCookie*> out_cookies; |
31 if (ok) { | 53 if (load_return_value_) { |
32 out_cookies = load_result_; | 54 out_cookies = load_result_; |
55 loaded_ = true; | |
33 } | 56 } |
34 loaded_callback.Run(out_cookies); | 57 MessageLoop::current()->PostTask(FROM_HERE, |
35 return ok; | 58 base::Bind(&LoadedCallbackTask::Run, |
59 new LoadedCallbackTask(loaded_callback, out_cookies))); | |
60 } | |
61 | |
62 void MockPersistentCookieStore::LoadCookiesForKey(const std::string& key, | |
63 const LoadedCallback& loaded_callback) { | |
64 if (!loaded_) { | |
65 Load(loaded_callback); | |
66 } else { | |
67 MessageLoop::current()->PostTask(FROM_HERE, | |
68 base::Bind(&LoadedCallbackTask::Run, | |
69 new LoadedCallbackTask(loaded_callback, | |
70 std::vector<CookieMonster::CanonicalCookie*>()))); | |
71 loaded_ = true; | |
erikwright (departed)
2011/10/04 19:55:02
line 71 seems redundant (loaded_ must equal true h
guohui
2011/10/06 15:40:00
Done.
| |
72 } | |
36 } | 73 } |
37 | 74 |
38 void MockPersistentCookieStore::AddCookie( | 75 void MockPersistentCookieStore::AddCookie( |
39 const CookieMonster::CanonicalCookie& cookie) { | 76 const CookieMonster::CanonicalCookie& cookie) { |
40 commands_.push_back( | 77 commands_.push_back( |
41 CookieStoreCommand(CookieStoreCommand::ADD, cookie)); | 78 CookieStoreCommand(CookieStoreCommand::ADD, cookie)); |
42 } | 79 } |
43 | 80 |
44 void MockPersistentCookieStore::UpdateCookieAccessTime( | 81 void MockPersistentCookieStore::UpdateCookieAccessTime( |
45 const CookieMonster::CanonicalCookie& cookie) { | 82 const CookieMonster::CanonicalCookie& cookie) { |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
106 std::string cookie_path = pc.Path(); | 143 std::string cookie_path = pc.Path(); |
107 | 144 |
108 return CookieMonster::CanonicalCookie( | 145 return CookieMonster::CanonicalCookie( |
109 GURL(), pc.Name(), pc.Value(), key, cookie_path, | 146 GURL(), pc.Name(), pc.Value(), key, cookie_path, |
110 pc.MACKey(), pc.MACAlgorithm(), | 147 pc.MACKey(), pc.MACAlgorithm(), |
111 creation_time, creation_time, cookie_expires, | 148 creation_time, creation_time, cookie_expires, |
112 pc.IsSecure(), pc.IsHttpOnly(), | 149 pc.IsSecure(), pc.IsHttpOnly(), |
113 !cookie_expires.is_null()); | 150 !cookie_expires.is_null()); |
114 } | 151 } |
115 | 152 |
116 MockSimplePersistentCookieStore::MockSimplePersistentCookieStore() {} | 153 MockSimplePersistentCookieStore::MockSimplePersistentCookieStore() |
154 : loaded_(false) {} | |
117 | 155 |
118 MockSimplePersistentCookieStore::~MockSimplePersistentCookieStore() {} | 156 MockSimplePersistentCookieStore::~MockSimplePersistentCookieStore() {} |
119 | 157 |
120 bool MockSimplePersistentCookieStore::Load( | 158 void MockSimplePersistentCookieStore::Load( |
121 const LoadedCallback& loaded_callback) { | 159 const LoadedCallback& loaded_callback) { |
122 std::vector<CookieMonster::CanonicalCookie*> out_cookies; | 160 std::vector<CookieMonster::CanonicalCookie*> out_cookies; |
161 | |
123 for (CanonicalCookieMap::const_iterator it = cookies_.begin(); | 162 for (CanonicalCookieMap::const_iterator it = cookies_.begin(); |
124 it != cookies_.end(); it++) | 163 it != cookies_.end(); it++) |
125 out_cookies.push_back( | 164 out_cookies.push_back( |
126 new CookieMonster::CanonicalCookie(it->second)); | 165 new CookieMonster::CanonicalCookie(it->second)); |
127 loaded_callback.Run(out_cookies); | 166 |
128 return true; | 167 MessageLoop::current()->PostTask(FROM_HERE, |
168 base::Bind(&LoadedCallbackTask::Run, | |
169 new LoadedCallbackTask(loaded_callback, out_cookies))); | |
170 loaded_ = true; | |
171 } | |
172 | |
173 void MockSimplePersistentCookieStore::LoadCookiesForKey(const std::string& key, | |
174 const LoadedCallback& loaded_callback) { | |
175 if (!loaded_) { | |
176 Load(loaded_callback); | |
177 } else { | |
178 MessageLoop::current()->PostTask(FROM_HERE, | |
179 base::Bind(&LoadedCallbackTask::Run, | |
180 new LoadedCallbackTask(loaded_callback, | |
181 std::vector<CookieMonster::CanonicalCookie*>()))); | |
182 loaded_ = true; | |
erikwright (departed)
2011/10/04 19:55:02
ditto (line 182 seems redundant).
guohui
2011/10/06 15:40:00
Done.
| |
183 } | |
129 } | 184 } |
130 | 185 |
131 void MockSimplePersistentCookieStore::AddCookie( | 186 void MockSimplePersistentCookieStore::AddCookie( |
132 const CookieMonster::CanonicalCookie& cookie) { | 187 const CookieMonster::CanonicalCookie& cookie) { |
133 int64 creation_time = cookie.CreationDate().ToInternalValue(); | 188 int64 creation_time = cookie.CreationDate().ToInternalValue(); |
134 EXPECT_TRUE(cookies_.find(creation_time) == cookies_.end()); | 189 EXPECT_TRUE(cookies_.find(creation_time) == cookies_.end()); |
135 cookies_[creation_time] = cookie; | 190 cookies_[creation_time] = cookie; |
136 } | 191 } |
137 | 192 |
138 void MockSimplePersistentCookieStore::UpdateCookieAccessTime( | 193 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", | 238 GURL(), "a", "1", base::StringPrintf("h%05d.izzle", i), "/path", |
184 mac_key, mac_algorithm, creation_time, expiration_time, | 239 mac_key, mac_algorithm, creation_time, expiration_time, |
185 last_access_time, false, false, true); | 240 last_access_time, false, false, true); |
186 store->AddCookie(cc); | 241 store->AddCookie(cc); |
187 } | 242 } |
188 | 243 |
189 return new CookieMonster(store, NULL); | 244 return new CookieMonster(store, NULL); |
190 } | 245 } |
191 | 246 |
192 } // namespace net | 247 } // namespace net |
OLD | NEW |