Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(255)

Side by Side Diff: net/cookies/cookie_store_unittest.h

Issue 1634803004: Convert CookieMonster tests to use base::RunLoop (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Comments Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/cookies/cookie_store_test_callbacks.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 #ifndef NET_COOKIES_COOKIE_STORE_UNITTEST_H_ 5 #ifndef NET_COOKIES_COOKIE_STORE_UNITTEST_H_
6 #define NET_COOKIES_COOKIE_STORE_UNITTEST_H_ 6 #define NET_COOKIES_COOKIE_STORE_UNITTEST_H_
7 7
8 #include <set> 8 #include <set>
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 std::string GetCookies(CookieStore* cs, const GURL& url) { 102 std::string GetCookies(CookieStore* cs, const GURL& url) {
103 DCHECK(cs); 103 DCHECK(cs);
104 CookieOptions options; 104 CookieOptions options;
105 if (!CookieStoreTestTraits::supports_http_only) 105 if (!CookieStoreTestTraits::supports_http_only)
106 options.set_include_httponly(); 106 options.set_include_httponly();
107 StringResultCookieCallback callback; 107 StringResultCookieCallback callback;
108 cs->GetCookiesWithOptionsAsync( 108 cs->GetCookiesWithOptionsAsync(
109 url, options, 109 url, options,
110 base::Bind(&StringResultCookieCallback::Run, 110 base::Bind(&StringResultCookieCallback::Run,
111 base::Unretained(&callback))); 111 base::Unretained(&callback)));
112 RunFor(kTimeout); 112 callback.WaitUntilDone();
113 EXPECT_TRUE(callback.did_run());
114 return callback.result(); 113 return callback.result();
115 } 114 }
116 115
117 std::string GetCookiesWithOptions(CookieStore* cs, 116 std::string GetCookiesWithOptions(CookieStore* cs,
118 const GURL& url, 117 const GURL& url,
119 const CookieOptions& options) { 118 const CookieOptions& options) {
120 DCHECK(cs); 119 DCHECK(cs);
121 StringResultCookieCallback callback; 120 StringResultCookieCallback callback;
122 cs->GetCookiesWithOptionsAsync( 121 cs->GetCookiesWithOptionsAsync(
123 url, options, base::Bind(&StringResultCookieCallback::Run, 122 url, options, base::Bind(&StringResultCookieCallback::Run,
124 base::Unretained(&callback))); 123 base::Unretained(&callback)));
125 RunFor(kTimeout); 124 callback.WaitUntilDone();
126 EXPECT_TRUE(callback.did_run());
127 return callback.result(); 125 return callback.result();
128 } 126 }
129 127
130 CookieList GetAllCookies(CookieStore* cs) { 128 CookieList GetAllCookies(CookieStore* cs) {
131 DCHECK(cs); 129 DCHECK(cs);
132 GetCookieListCallback callback; 130 GetCookieListCallback callback;
133 cs->GetAllCookiesAsync( 131 cs->GetAllCookiesAsync(
134 base::Bind(&GetCookieListCallback::Run, base::Unretained(&callback))); 132 base::Bind(&GetCookieListCallback::Run, base::Unretained(&callback)));
135 RunFor(kTimeout); 133 callback.WaitUntilDone();
136 EXPECT_TRUE(callback.did_run());
137 return callback.cookies(); 134 return callback.cookies();
138 } 135 }
139 136
140 bool SetCookieWithOptions(CookieStore* cs, 137 bool SetCookieWithOptions(CookieStore* cs,
141 const GURL& url, 138 const GURL& url,
142 const std::string& cookie_line, 139 const std::string& cookie_line,
143 const CookieOptions& options) { 140 const CookieOptions& options) {
144 DCHECK(cs); 141 DCHECK(cs);
145 ResultSavingCookieCallback<bool> callback; 142 ResultSavingCookieCallback<bool> callback;
146 cs->SetCookieWithOptionsAsync( 143 cs->SetCookieWithOptionsAsync(
147 url, cookie_line, options, 144 url, cookie_line, options,
148 base::Bind( 145 base::Bind(
149 &ResultSavingCookieCallback<bool>::Run, 146 &ResultSavingCookieCallback<bool>::Run,
150 base::Unretained(&callback))); 147 base::Unretained(&callback)));
151 RunFor(kTimeout); 148 callback.WaitUntilDone();
152 EXPECT_TRUE(callback.did_run());
153 return callback.result(); 149 return callback.result();
154 } 150 }
155 151
156 bool SetCookieWithServerTime(CookieStore* cs, 152 bool SetCookieWithServerTime(CookieStore* cs,
157 const GURL& url, 153 const GURL& url,
158 const std::string& cookie_line, 154 const std::string& cookie_line,
159 const base::Time& server_time) { 155 const base::Time& server_time) {
160 CookieOptions options; 156 CookieOptions options;
161 if (!CookieStoreTestTraits::supports_http_only) 157 if (!CookieStoreTestTraits::supports_http_only)
162 options.set_include_httponly(); 158 options.set_include_httponly();
(...skipping 13 matching lines...) Expand all
176 } 172 }
177 173
178 void DeleteCookie(CookieStore* cs, 174 void DeleteCookie(CookieStore* cs,
179 const GURL& url, 175 const GURL& url,
180 const std::string& cookie_name) { 176 const std::string& cookie_name) {
181 DCHECK(cs); 177 DCHECK(cs);
182 NoResultCookieCallback callback; 178 NoResultCookieCallback callback;
183 cs->DeleteCookieAsync( 179 cs->DeleteCookieAsync(
184 url, cookie_name, 180 url, cookie_name,
185 base::Bind(&NoResultCookieCallback::Run, base::Unretained(&callback))); 181 base::Bind(&NoResultCookieCallback::Run, base::Unretained(&callback)));
186 RunFor(kTimeout); 182 callback.WaitUntilDone();
187 EXPECT_TRUE(callback.did_run());
188 } 183 }
189 184
190 int DeleteCreatedBetween(CookieStore* cs, 185 int DeleteCreatedBetween(CookieStore* cs,
191 const base::Time& delete_begin, 186 const base::Time& delete_begin,
192 const base::Time& delete_end) { 187 const base::Time& delete_end) {
193 DCHECK(cs); 188 DCHECK(cs);
194 ResultSavingCookieCallback<int> callback; 189 ResultSavingCookieCallback<int> callback;
195 cs->DeleteAllCreatedBetweenAsync( 190 cs->DeleteAllCreatedBetweenAsync(
196 delete_begin, delete_end, 191 delete_begin, delete_end,
197 base::Bind( 192 base::Bind(
198 &ResultSavingCookieCallback<int>::Run, 193 &ResultSavingCookieCallback<int>::Run,
199 base::Unretained(&callback))); 194 base::Unretained(&callback)));
200 RunFor(kTimeout); 195 callback.WaitUntilDone();
201 EXPECT_TRUE(callback.did_run());
202 return callback.result(); 196 return callback.result();
203 } 197 }
204 198
205 int DeleteAllCreatedBetweenForHost(CookieStore* cs, 199 int DeleteAllCreatedBetweenForHost(CookieStore* cs,
206 const base::Time delete_begin, 200 const base::Time delete_begin,
207 const base::Time delete_end, 201 const base::Time delete_end,
208 const GURL& url) { 202 const GURL& url) {
209 DCHECK(cs); 203 DCHECK(cs);
210 ResultSavingCookieCallback<int> callback; 204 ResultSavingCookieCallback<int> callback;
211 cs->DeleteAllCreatedBetweenForHostAsync( 205 cs->DeleteAllCreatedBetweenForHostAsync(
212 delete_begin, delete_end, url, 206 delete_begin, delete_end, url,
213 base::Bind( 207 base::Bind(
214 &ResultSavingCookieCallback<int>::Run, 208 &ResultSavingCookieCallback<int>::Run,
215 base::Unretained(&callback))); 209 base::Unretained(&callback)));
216 RunFor(kTimeout); 210 callback.WaitUntilDone();
217 EXPECT_TRUE(callback.did_run());
218 return callback.result(); 211 return callback.result();
219 } 212 }
220 213
221 int DeleteSessionCookies(CookieStore* cs) { 214 int DeleteSessionCookies(CookieStore* cs) {
222 DCHECK(cs); 215 DCHECK(cs);
223 ResultSavingCookieCallback<int> callback; 216 ResultSavingCookieCallback<int> callback;
224 cs->DeleteSessionCookiesAsync( 217 cs->DeleteSessionCookiesAsync(
225 base::Bind( 218 base::Bind(
226 &ResultSavingCookieCallback<int>::Run, 219 &ResultSavingCookieCallback<int>::Run,
227 base::Unretained(&callback))); 220 base::Unretained(&callback)));
228 RunFor(kTimeout); 221 callback.WaitUntilDone();
229 EXPECT_TRUE(callback.did_run());
230 return callback.result(); 222 return callback.result();
231 } 223 }
232 224
233 void RunFor(int ms) {
234 // Runs the test thread message loop for up to |ms| milliseconds.
235 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
236 FROM_HERE, base::Bind(&base::MessageLoop::QuitWhenIdle,
237 weak_factory_->GetWeakPtr()),
238 base::TimeDelta::FromMilliseconds(ms));
239 base::MessageLoop::current()->Run();
240 weak_factory_->InvalidateWeakPtrs();
241 }
242
243 scoped_refptr<CookieStore> GetCookieStore() { 225 scoped_refptr<CookieStore> GetCookieStore() {
244 return CookieStoreTestTraits::Create(); 226 return CookieStoreTestTraits::Create();
245 } 227 }
246 228
247 // Compares two cookie lines. 229 // Compares two cookie lines.
248 void MatchCookieLines(const std::string& line1, const std::string& line2) { 230 void MatchCookieLines(const std::string& line1, const std::string& line2) {
249 EXPECT_EQ(TokenizeCookieLine(line1), TokenizeCookieLine(line2)); 231 EXPECT_EQ(TokenizeCookieLine(line1), TokenizeCookieLine(line2));
250 } 232 }
251 233
252 // Check the cookie line by polling until equality or a timeout is reached. 234 // Check the cookie line by polling until equality or a timeout is reached.
(...skipping 958 matching lines...) Expand 10 before | Expand all | Expand 10 after
1211 cs->DeleteSessionCookiesAsync( 1193 cs->DeleteSessionCookiesAsync(
1212 base::Bind( 1194 base::Bind(
1213 &ResultSavingCookieCallback<int>::Run, 1195 &ResultSavingCookieCallback<int>::Run,
1214 base::Unretained(callback))); 1196 base::Unretained(callback)));
1215 } 1197 }
1216 1198
1217 protected: 1199 protected:
1218 void RunOnOtherThread(const base::Closure& task) { 1200 void RunOnOtherThread(const base::Closure& task) {
1219 other_thread_.Start(); 1201 other_thread_.Start();
1220 other_thread_.task_runner()->PostTask(FROM_HERE, task); 1202 other_thread_.task_runner()->PostTask(FROM_HERE, task);
1221 CookieStoreTest<CookieStoreTestTraits>::RunFor(kTimeout);
1222 other_thread_.Stop(); 1203 other_thread_.Stop();
1223 } 1204 }
1224 1205
1225 Thread other_thread_; 1206 Thread other_thread_;
1226 }; 1207 };
1227 1208
1228 TYPED_TEST_CASE_P(MultiThreadedCookieStoreTest); 1209 TYPED_TEST_CASE_P(MultiThreadedCookieStoreTest);
1229 1210
1230 // TODO(ycxiao): Eventually, we will need to create a separate thread, create 1211 // TODO(ycxiao): Eventually, we will need to create a separate thread, create
1231 // the cookie store on that thread (or at least its store, i.e., the DB 1212 // the cookie store on that thread (or at least its store, i.e., the DB
1232 // thread). 1213 // thread).
1233 TYPED_TEST_P(MultiThreadedCookieStoreTest, ThreadCheckGetCookies) { 1214 TYPED_TEST_P(MultiThreadedCookieStoreTest, ThreadCheckGetCookies) {
1234 scoped_refptr<CookieStore> cs(this->GetCookieStore()); 1215 scoped_refptr<CookieStore> cs(this->GetCookieStore());
1235 EXPECT_TRUE(this->SetCookie(cs.get(), this->http_www_google_.url(), "A=B")); 1216 EXPECT_TRUE(this->SetCookie(cs.get(), this->http_www_google_.url(), "A=B"));
1236 this->MatchCookieLines( 1217 this->MatchCookieLines(
1237 "A=B", this->GetCookies(cs.get(), this->http_www_google_.url())); 1218 "A=B", this->GetCookies(cs.get(), this->http_www_google_.url()));
1238 StringResultCookieCallback callback(&this->other_thread_); 1219 StringResultCookieCallback callback(&this->other_thread_);
1239 base::Closure task = base::Bind( 1220 base::Closure task = base::Bind(
1240 &MultiThreadedCookieStoreTest<TypeParam>::GetCookiesTask, 1221 &MultiThreadedCookieStoreTest<TypeParam>::GetCookiesTask,
1241 base::Unretained(this), cs, this->http_www_google_.url(), &callback); 1222 base::Unretained(this), cs, this->http_www_google_.url(), &callback);
1242 this->RunOnOtherThread(task); 1223 this->RunOnOtherThread(task);
1243 EXPECT_TRUE(callback.did_run()); 1224 callback.WaitUntilDone();
1244 EXPECT_EQ("A=B", callback.result()); 1225 EXPECT_EQ("A=B", callback.result());
1245 } 1226 }
1246 1227
1247 TYPED_TEST_P(MultiThreadedCookieStoreTest, ThreadCheckGetCookiesWithOptions) { 1228 TYPED_TEST_P(MultiThreadedCookieStoreTest, ThreadCheckGetCookiesWithOptions) {
1248 scoped_refptr<CookieStore> cs(this->GetCookieStore()); 1229 scoped_refptr<CookieStore> cs(this->GetCookieStore());
1249 CookieOptions options; 1230 CookieOptions options;
1250 if (!TypeParam::supports_http_only) 1231 if (!TypeParam::supports_http_only)
1251 options.set_include_httponly(); 1232 options.set_include_httponly();
1252 EXPECT_TRUE(this->SetCookie(cs.get(), this->http_www_google_.url(), "A=B")); 1233 EXPECT_TRUE(this->SetCookie(cs.get(), this->http_www_google_.url(), "A=B"));
1253 this->MatchCookieLines( 1234 this->MatchCookieLines(
1254 "A=B", this->GetCookiesWithOptions(cs.get(), this->http_www_google_.url(), 1235 "A=B", this->GetCookiesWithOptions(cs.get(), this->http_www_google_.url(),
1255 options)); 1236 options));
1256 StringResultCookieCallback callback(&this->other_thread_); 1237 StringResultCookieCallback callback(&this->other_thread_);
1257 base::Closure task = base::Bind( 1238 base::Closure task = base::Bind(
1258 &MultiThreadedCookieStoreTest<TypeParam>::GetCookiesWithOptionsTask, 1239 &MultiThreadedCookieStoreTest<TypeParam>::GetCookiesWithOptionsTask,
1259 base::Unretained(this), cs, this->http_www_google_.url(), options, 1240 base::Unretained(this), cs, this->http_www_google_.url(), options,
1260 &callback); 1241 &callback);
1261 this->RunOnOtherThread(task); 1242 this->RunOnOtherThread(task);
1262 EXPECT_TRUE(callback.did_run()); 1243 callback.WaitUntilDone();
1263 EXPECT_EQ("A=B", callback.result()); 1244 EXPECT_EQ("A=B", callback.result());
1264 } 1245 }
1265 1246
1266 TYPED_TEST_P(MultiThreadedCookieStoreTest, ThreadCheckSetCookieWithOptions) { 1247 TYPED_TEST_P(MultiThreadedCookieStoreTest, ThreadCheckSetCookieWithOptions) {
1267 scoped_refptr<CookieStore> cs(this->GetCookieStore()); 1248 scoped_refptr<CookieStore> cs(this->GetCookieStore());
1268 CookieOptions options; 1249 CookieOptions options;
1269 if (!TypeParam::supports_http_only) 1250 if (!TypeParam::supports_http_only)
1270 options.set_include_httponly(); 1251 options.set_include_httponly();
1271 EXPECT_TRUE(this->SetCookieWithOptions(cs.get(), this->http_www_google_.url(), 1252 EXPECT_TRUE(this->SetCookieWithOptions(cs.get(), this->http_www_google_.url(),
1272 "A=B", options)); 1253 "A=B", options));
1273 ResultSavingCookieCallback<bool> callback(&this->other_thread_); 1254 ResultSavingCookieCallback<bool> callback(&this->other_thread_);
1274 base::Closure task = base::Bind( 1255 base::Closure task = base::Bind(
1275 &MultiThreadedCookieStoreTest<TypeParam>::SetCookieWithOptionsTask, 1256 &MultiThreadedCookieStoreTest<TypeParam>::SetCookieWithOptionsTask,
1276 base::Unretained(this), cs, this->http_www_google_.url(), "A=B", options, 1257 base::Unretained(this), cs, this->http_www_google_.url(), "A=B", options,
1277 &callback); 1258 &callback);
1278 this->RunOnOtherThread(task); 1259 this->RunOnOtherThread(task);
1279 EXPECT_TRUE(callback.did_run()); 1260 callback.WaitUntilDone();
1280 EXPECT_TRUE(callback.result()); 1261 EXPECT_TRUE(callback.result());
1281 } 1262 }
1282 1263
1283 TYPED_TEST_P(MultiThreadedCookieStoreTest, ThreadCheckDeleteCookie) { 1264 TYPED_TEST_P(MultiThreadedCookieStoreTest, ThreadCheckDeleteCookie) {
1284 scoped_refptr<CookieStore> cs(this->GetCookieStore()); 1265 scoped_refptr<CookieStore> cs(this->GetCookieStore());
1285 CookieOptions options; 1266 CookieOptions options;
1286 if (!TypeParam::supports_http_only) 1267 if (!TypeParam::supports_http_only)
1287 options.set_include_httponly(); 1268 options.set_include_httponly();
1288 EXPECT_TRUE(this->SetCookieWithOptions(cs.get(), this->http_www_google_.url(), 1269 EXPECT_TRUE(this->SetCookieWithOptions(cs.get(), this->http_www_google_.url(),
1289 "A=B", options)); 1270 "A=B", options));
1290 this->DeleteCookie(cs.get(), this->http_www_google_.url(), "A"); 1271 this->DeleteCookie(cs.get(), this->http_www_google_.url(), "A");
1291 EXPECT_TRUE(this->SetCookieWithOptions(cs.get(), this->http_www_google_.url(), 1272 EXPECT_TRUE(this->SetCookieWithOptions(cs.get(), this->http_www_google_.url(),
1292 "A=B", options)); 1273 "A=B", options));
1293 NoResultCookieCallback callback(&this->other_thread_); 1274 NoResultCookieCallback callback(&this->other_thread_);
1294 base::Closure task = base::Bind( 1275 base::Closure task = base::Bind(
1295 &MultiThreadedCookieStoreTest<TypeParam>::DeleteCookieTask, 1276 &MultiThreadedCookieStoreTest<TypeParam>::DeleteCookieTask,
1296 base::Unretained(this), cs, this->http_www_google_.url(), "A", &callback); 1277 base::Unretained(this), cs, this->http_www_google_.url(), "A", &callback);
1297 this->RunOnOtherThread(task); 1278 this->RunOnOtherThread(task);
1298 EXPECT_TRUE(callback.did_run()); 1279 callback.WaitUntilDone();
1299 } 1280 }
1300 1281
1301 TYPED_TEST_P(MultiThreadedCookieStoreTest, ThreadCheckDeleteSessionCookies) { 1282 TYPED_TEST_P(MultiThreadedCookieStoreTest, ThreadCheckDeleteSessionCookies) {
1302 scoped_refptr<CookieStore> cs(this->GetCookieStore()); 1283 scoped_refptr<CookieStore> cs(this->GetCookieStore());
1303 CookieOptions options; 1284 CookieOptions options;
1304 if (!TypeParam::supports_http_only) 1285 if (!TypeParam::supports_http_only)
1305 options.set_include_httponly(); 1286 options.set_include_httponly();
1306 EXPECT_TRUE(this->SetCookieWithOptions(cs.get(), this->http_www_google_.url(), 1287 EXPECT_TRUE(this->SetCookieWithOptions(cs.get(), this->http_www_google_.url(),
1307 "A=B", options)); 1288 "A=B", options));
1308 EXPECT_TRUE(this->SetCookieWithOptions( 1289 EXPECT_TRUE(this->SetCookieWithOptions(
1309 cs.get(), this->http_www_google_.url(), 1290 cs.get(), this->http_www_google_.url(),
1310 "B=C; expires=Mon, 18-Apr-22 22:50:13 GMT", options)); 1291 "B=C; expires=Mon, 18-Apr-22 22:50:13 GMT", options));
1311 EXPECT_EQ(1, this->DeleteSessionCookies(cs.get())); 1292 EXPECT_EQ(1, this->DeleteSessionCookies(cs.get()));
1312 EXPECT_EQ(0, this->DeleteSessionCookies(cs.get())); 1293 EXPECT_EQ(0, this->DeleteSessionCookies(cs.get()));
1313 EXPECT_TRUE(this->SetCookieWithOptions(cs.get(), this->http_www_google_.url(), 1294 EXPECT_TRUE(this->SetCookieWithOptions(cs.get(), this->http_www_google_.url(),
1314 "A=B", options)); 1295 "A=B", options));
1315 ResultSavingCookieCallback<int> callback(&this->other_thread_); 1296 ResultSavingCookieCallback<int> callback(&this->other_thread_);
1316 base::Closure task = base::Bind( 1297 base::Closure task = base::Bind(
1317 &MultiThreadedCookieStoreTest<TypeParam>::DeleteSessionCookiesTask, 1298 &MultiThreadedCookieStoreTest<TypeParam>::DeleteSessionCookiesTask,
1318 base::Unretained(this), cs, &callback); 1299 base::Unretained(this), cs, &callback);
1319 this->RunOnOtherThread(task); 1300 this->RunOnOtherThread(task);
1320 EXPECT_TRUE(callback.did_run()); 1301 callback.WaitUntilDone();
1321 EXPECT_EQ(1, callback.result()); 1302 EXPECT_EQ(1, callback.result());
1322 } 1303 }
1323 1304
1324 REGISTER_TYPED_TEST_CASE_P(MultiThreadedCookieStoreTest, 1305 REGISTER_TYPED_TEST_CASE_P(MultiThreadedCookieStoreTest,
1325 ThreadCheckGetCookies, 1306 ThreadCheckGetCookies,
1326 ThreadCheckGetCookiesWithOptions, 1307 ThreadCheckGetCookiesWithOptions,
1327 ThreadCheckSetCookieWithOptions, 1308 ThreadCheckSetCookieWithOptions,
1328 ThreadCheckDeleteCookie, 1309 ThreadCheckDeleteCookie,
1329 ThreadCheckDeleteSessionCookies); 1310 ThreadCheckDeleteSessionCookies);
1330 1311
1331 } // namespace net 1312 } // namespace net
1332 1313
1333 #endif // NET_COOKIES_COOKIE_STORE_UNITTEST_H_ 1314 #endif // NET_COOKIES_COOKIE_STORE_UNITTEST_H_
OLDNEW
« no previous file with comments | « net/cookies/cookie_store_test_callbacks.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698