| 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" | |
| 8 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 9 #include "base/stringprintf.h" | 8 #include "base/stringprintf.h" |
| 10 #include "base/time.h" | 9 #include "base/time.h" |
| 11 #include "googleurl/src/gurl.h" | 10 #include "googleurl/src/gurl.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 12 |
| 14 namespace net { | 13 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() {} | |
| 22 | 14 |
| 23 MockPersistentCookieStore::MockPersistentCookieStore() | 15 MockPersistentCookieStore::MockPersistentCookieStore() |
| 24 : load_return_value_(true), | 16 : load_return_value_(true) { |
| 25 loaded_(false) { | |
| 26 } | 17 } |
| 27 | 18 |
| 28 MockPersistentCookieStore::~MockPersistentCookieStore() {} | 19 MockPersistentCookieStore::~MockPersistentCookieStore() {} |
| 29 | 20 |
| 30 void MockPersistentCookieStore::SetLoadExpectation( | 21 void MockPersistentCookieStore::SetLoadExpectation( |
| 31 bool return_value, | 22 bool return_value, |
| 32 const std::vector<CookieMonster::CanonicalCookie*>& result) { | 23 const std::vector<CookieMonster::CanonicalCookie*>& result) { |
| 33 load_return_value_ = return_value; | 24 load_return_value_ = return_value; |
| 34 load_result_ = result; | 25 load_result_ = result; |
| 35 } | 26 } |
| 36 | 27 |
| 37 void MockPersistentCookieStore::Load(const LoadedCallback& loaded_callback) { | 28 bool MockPersistentCookieStore::Load(const LoadedCallback& loaded_callback) { |
| 29 bool ok = load_return_value_; |
| 38 std::vector<CookieMonster::CanonicalCookie*> out_cookies; | 30 std::vector<CookieMonster::CanonicalCookie*> out_cookies; |
| 39 if (load_return_value_) { | 31 if (ok) { |
| 40 out_cookies = load_result_; | 32 out_cookies = load_result_; |
| 41 loaded_ = true; | |
| 42 } | 33 } |
| 43 MessageLoop::current()->PostTask(FROM_HERE, | 34 loaded_callback.Run(out_cookies); |
| 44 base::Bind(&LoadedCallbackTask::Run, | 35 return ok; |
| 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 } | |
| 58 } | 36 } |
| 59 | 37 |
| 60 void MockPersistentCookieStore::AddCookie( | 38 void MockPersistentCookieStore::AddCookie( |
| 61 const CookieMonster::CanonicalCookie& cookie) { | 39 const CookieMonster::CanonicalCookie& cookie) { |
| 62 commands_.push_back( | 40 commands_.push_back( |
| 63 CookieStoreCommand(CookieStoreCommand::ADD, cookie)); | 41 CookieStoreCommand(CookieStoreCommand::ADD, cookie)); |
| 64 } | 42 } |
| 65 | 43 |
| 66 void MockPersistentCookieStore::UpdateCookieAccessTime( | 44 void MockPersistentCookieStore::UpdateCookieAccessTime( |
| 67 const CookieMonster::CanonicalCookie& cookie) { | 45 const CookieMonster::CanonicalCookie& cookie) { |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 std::string cookie_path = pc.Path(); | 106 std::string cookie_path = pc.Path(); |
| 129 | 107 |
| 130 return CookieMonster::CanonicalCookie( | 108 return CookieMonster::CanonicalCookie( |
| 131 GURL(), pc.Name(), pc.Value(), key, cookie_path, | 109 GURL(), pc.Name(), pc.Value(), key, cookie_path, |
| 132 pc.MACKey(), pc.MACAlgorithm(), | 110 pc.MACKey(), pc.MACAlgorithm(), |
| 133 creation_time, creation_time, cookie_expires, | 111 creation_time, creation_time, cookie_expires, |
| 134 pc.IsSecure(), pc.IsHttpOnly(), | 112 pc.IsSecure(), pc.IsHttpOnly(), |
| 135 !cookie_expires.is_null()); | 113 !cookie_expires.is_null()); |
| 136 } | 114 } |
| 137 | 115 |
| 138 MockSimplePersistentCookieStore::MockSimplePersistentCookieStore() | 116 MockSimplePersistentCookieStore::MockSimplePersistentCookieStore() {} |
| 139 : loaded_(false) {} | |
| 140 | 117 |
| 141 MockSimplePersistentCookieStore::~MockSimplePersistentCookieStore() {} | 118 MockSimplePersistentCookieStore::~MockSimplePersistentCookieStore() {} |
| 142 | 119 |
| 143 void MockSimplePersistentCookieStore::Load( | 120 bool MockSimplePersistentCookieStore::Load( |
| 144 const LoadedCallback& loaded_callback) { | 121 const LoadedCallback& loaded_callback) { |
| 145 std::vector<CookieMonster::CanonicalCookie*> out_cookies; | 122 std::vector<CookieMonster::CanonicalCookie*> out_cookies; |
| 146 | |
| 147 for (CanonicalCookieMap::const_iterator it = cookies_.begin(); | 123 for (CanonicalCookieMap::const_iterator it = cookies_.begin(); |
| 148 it != cookies_.end(); it++) | 124 it != cookies_.end(); it++) |
| 149 out_cookies.push_back( | 125 out_cookies.push_back( |
| 150 new CookieMonster::CanonicalCookie(it->second)); | 126 new CookieMonster::CanonicalCookie(it->second)); |
| 151 | 127 loaded_callback.Run(out_cookies); |
| 152 MessageLoop::current()->PostTask(FROM_HERE, | 128 return true; |
| 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 } | |
| 168 } | 129 } |
| 169 | 130 |
| 170 void MockSimplePersistentCookieStore::AddCookie( | 131 void MockSimplePersistentCookieStore::AddCookie( |
| 171 const CookieMonster::CanonicalCookie& cookie) { | 132 const CookieMonster::CanonicalCookie& cookie) { |
| 172 int64 creation_time = cookie.CreationDate().ToInternalValue(); | 133 int64 creation_time = cookie.CreationDate().ToInternalValue(); |
| 173 EXPECT_TRUE(cookies_.find(creation_time) == cookies_.end()); | 134 EXPECT_TRUE(cookies_.find(creation_time) == cookies_.end()); |
| 174 cookies_[creation_time] = cookie; | 135 cookies_[creation_time] = cookie; |
| 175 } | 136 } |
| 176 | 137 |
| 177 void MockSimplePersistentCookieStore::UpdateCookieAccessTime( | 138 void MockSimplePersistentCookieStore::UpdateCookieAccessTime( |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 222 GURL(), "a", "1", base::StringPrintf("h%05d.izzle", i), "/path", | 183 GURL(), "a", "1", base::StringPrintf("h%05d.izzle", i), "/path", |
| 223 mac_key, mac_algorithm, creation_time, expiration_time, | 184 mac_key, mac_algorithm, creation_time, expiration_time, |
| 224 last_access_time, false, false, true); | 185 last_access_time, false, false, true); |
| 225 store->AddCookie(cc); | 186 store->AddCookie(cc); |
| 226 } | 187 } |
| 227 | 188 |
| 228 return new CookieMonster(store, NULL); | 189 return new CookieMonster(store, NULL); |
| 229 } | 190 } |
| 230 | 191 |
| 231 } // namespace net | 192 } // namespace net |
| OLD | NEW |