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

Side by Side Diff: net/log/net_log_unittest.cc

Issue 1122483004: Remove NetLog::GetCaptureMode() and NetLogCaptureMode::None() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@mmenke_refactor
Patch Set: rename HasObservers --> IsCapturing Created 5 years, 7 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
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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/log/net_log.h" 5 #include "net/log/net_log.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/memory/scoped_vector.h" 8 #include "base/memory/scoped_vector.h"
9 #include "base/synchronization/waitable_event.h" 9 #include "base/synchronization/waitable_event.h"
10 #include "base/threading/simple_thread.h" 10 #include "base/threading/simple_thread.h"
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 } 254 }
255 255
256 // Test adding and removing a single observer. 256 // Test adding and removing a single observer.
257 TEST(NetLogTest, NetLogAddRemoveObserver) { 257 TEST(NetLogTest, NetLogAddRemoveObserver) {
258 NetLog net_log; 258 NetLog net_log;
259 CountingObserver observer; 259 CountingObserver observer;
260 260
261 AddEvent(&net_log); 261 AddEvent(&net_log);
262 EXPECT_EQ(0, observer.count()); 262 EXPECT_EQ(0, observer.count());
263 EXPECT_EQ(NULL, observer.net_log()); 263 EXPECT_EQ(NULL, observer.net_log());
264 EXPECT_FALSE(net_log.GetCaptureMode().enabled()); 264 EXPECT_FALSE(net_log.IsCapturing());
265 265
266 // Add the observer and add an event. 266 // Add the observer and add an event.
267 net_log.DeprecatedAddObserver( 267 net_log.DeprecatedAddObserver(
268 &observer, NetLogCaptureMode::IncludeCookiesAndCredentials()); 268 &observer, NetLogCaptureMode::IncludeCookiesAndCredentials());
269 EXPECT_TRUE(net_log.IsCapturing());
269 EXPECT_EQ(&net_log, observer.net_log()); 270 EXPECT_EQ(&net_log, observer.net_log());
270 EXPECT_EQ(NetLogCaptureMode::IncludeCookiesAndCredentials(), 271 EXPECT_EQ(NetLogCaptureMode::IncludeCookiesAndCredentials(),
271 observer.capture_mode()); 272 observer.capture_mode());
272 EXPECT_EQ(NetLogCaptureMode::IncludeCookiesAndCredentials(),
273 net_log.GetCaptureMode());
274 273
275 AddEvent(&net_log); 274 AddEvent(&net_log);
276 EXPECT_EQ(1, observer.count()); 275 EXPECT_EQ(1, observer.count());
277 276
278 // Change the observer's logging level and add an event. 277 // Change the observer's logging level and add an event.
279 net_log.SetObserverCaptureMode(&observer, 278 net_log.SetObserverCaptureMode(&observer,
280 NetLogCaptureMode::IncludeSocketBytes()); 279 NetLogCaptureMode::IncludeSocketBytes());
281 EXPECT_EQ(&net_log, observer.net_log()); 280 EXPECT_EQ(&net_log, observer.net_log());
282 EXPECT_EQ(NetLogCaptureMode::IncludeSocketBytes(), observer.capture_mode()); 281 EXPECT_EQ(NetLogCaptureMode::IncludeSocketBytes(), observer.capture_mode());
283 EXPECT_EQ(NetLogCaptureMode::IncludeSocketBytes(), net_log.GetCaptureMode());
284 282
285 AddEvent(&net_log); 283 AddEvent(&net_log);
286 EXPECT_EQ(2, observer.count()); 284 EXPECT_EQ(2, observer.count());
287 285
288 // Remove observer and add an event. 286 // Remove observer and add an event.
289 net_log.DeprecatedRemoveObserver(&observer); 287 net_log.DeprecatedRemoveObserver(&observer);
290 EXPECT_EQ(NULL, observer.net_log()); 288 EXPECT_EQ(NULL, observer.net_log());
291 EXPECT_FALSE(net_log.GetCaptureMode().enabled()); 289 EXPECT_FALSE(net_log.IsCapturing());
292 290
293 AddEvent(&net_log); 291 AddEvent(&net_log);
294 EXPECT_EQ(2, observer.count()); 292 EXPECT_EQ(2, observer.count());
295 293
296 // Add the observer a final time, and add an event. 294 // Add the observer a final time, and add an event.
297 net_log.DeprecatedAddObserver(&observer, 295 net_log.DeprecatedAddObserver(&observer,
298 NetLogCaptureMode::IncludeSocketBytes()); 296 NetLogCaptureMode::IncludeSocketBytes());
299 EXPECT_EQ(&net_log, observer.net_log()); 297 EXPECT_EQ(&net_log, observer.net_log());
300 EXPECT_EQ(NetLogCaptureMode::IncludeSocketBytes(), observer.capture_mode()); 298 EXPECT_EQ(NetLogCaptureMode::IncludeSocketBytes(), observer.capture_mode());
301 EXPECT_EQ(NetLogCaptureMode::IncludeSocketBytes(), net_log.GetCaptureMode());
302 299
303 AddEvent(&net_log); 300 AddEvent(&net_log);
304 EXPECT_EQ(3, observer.count()); 301 EXPECT_EQ(3, observer.count());
305 } 302 }
306 303
307 // Test adding and removing two observers at different log levels. 304 // Test adding and removing two observers at different log levels.
308 TEST(NetLogTest, NetLogTwoObservers) { 305 TEST(NetLogTest, NetLogTwoObservers) {
309 NetLog net_log; 306 NetLog net_log;
310 LoggingObserver observer[2]; 307 LoggingObserver observer[2];
311 308
312 // Add first observer. 309 // Add first observer.
313 net_log.DeprecatedAddObserver( 310 net_log.DeprecatedAddObserver(
314 &observer[0], NetLogCaptureMode::IncludeCookiesAndCredentials()); 311 &observer[0], NetLogCaptureMode::IncludeCookiesAndCredentials());
315 EXPECT_EQ(&net_log, observer[0].net_log()); 312 EXPECT_EQ(&net_log, observer[0].net_log());
316 EXPECT_EQ(NULL, observer[1].net_log()); 313 EXPECT_EQ(NULL, observer[1].net_log());
317 EXPECT_EQ(NetLogCaptureMode::IncludeCookiesAndCredentials(), 314 EXPECT_EQ(NetLogCaptureMode::IncludeCookiesAndCredentials(),
318 observer[0].capture_mode()); 315 observer[0].capture_mode());
319 EXPECT_EQ(NetLogCaptureMode::IncludeCookiesAndCredentials(),
320 net_log.GetCaptureMode());
321 316
322 // Add second observer observer. 317 // Add second observer observer.
323 net_log.DeprecatedAddObserver(&observer[1], 318 net_log.DeprecatedAddObserver(&observer[1],
324 NetLogCaptureMode::IncludeSocketBytes()); 319 NetLogCaptureMode::IncludeSocketBytes());
325 EXPECT_EQ(&net_log, observer[0].net_log()); 320 EXPECT_EQ(&net_log, observer[0].net_log());
326 EXPECT_EQ(&net_log, observer[1].net_log()); 321 EXPECT_EQ(&net_log, observer[1].net_log());
327 EXPECT_EQ(NetLogCaptureMode::IncludeCookiesAndCredentials(), 322 EXPECT_EQ(NetLogCaptureMode::IncludeCookiesAndCredentials(),
328 observer[0].capture_mode()); 323 observer[0].capture_mode());
329 EXPECT_EQ(NetLogCaptureMode::IncludeSocketBytes(), 324 EXPECT_EQ(NetLogCaptureMode::IncludeSocketBytes(),
330 observer[1].capture_mode()); 325 observer[1].capture_mode());
331 EXPECT_EQ(NetLogCaptureMode::IncludeSocketBytes(), net_log.GetCaptureMode());
332 326
333 // Add event and make sure both observers receive it at their respective log 327 // Add event and make sure both observers receive it at their respective log
334 // levels. 328 // levels.
335 int param; 329 int param;
336 AddEvent(&net_log); 330 AddEvent(&net_log);
337 ASSERT_EQ(1U, observer[0].GetNumValues()); 331 ASSERT_EQ(1U, observer[0].GetNumValues());
338 ASSERT_TRUE(observer[0].GetValue(0)->GetInteger("params", &param)); 332 ASSERT_TRUE(observer[0].GetValue(0)->GetInteger("params", &param));
339 EXPECT_EQ(observer[0].capture_mode().ToInternalValueForTesting(), param); 333 EXPECT_EQ(observer[0].capture_mode().ToInternalValueForTesting(), param);
340 ASSERT_EQ(1U, observer[1].GetNumValues()); 334 ASSERT_EQ(1U, observer[1].GetNumValues());
341 ASSERT_TRUE(observer[1].GetValue(0)->GetInteger("params", &param)); 335 ASSERT_TRUE(observer[1].GetValue(0)->GetInteger("params", &param));
342 EXPECT_EQ(observer[1].capture_mode().ToInternalValueForTesting(), param); 336 EXPECT_EQ(observer[1].capture_mode().ToInternalValueForTesting(), param);
343 337
344 // Remove second observer. 338 // Remove second observer.
345 net_log.DeprecatedRemoveObserver(&observer[1]); 339 net_log.DeprecatedRemoveObserver(&observer[1]);
346 EXPECT_EQ(&net_log, observer[0].net_log()); 340 EXPECT_EQ(&net_log, observer[0].net_log());
347 EXPECT_EQ(NULL, observer[1].net_log()); 341 EXPECT_EQ(NULL, observer[1].net_log());
348 EXPECT_EQ(NetLogCaptureMode::IncludeCookiesAndCredentials(), 342 EXPECT_EQ(NetLogCaptureMode::IncludeCookiesAndCredentials(),
349 observer[0].capture_mode()); 343 observer[0].capture_mode());
350 EXPECT_EQ(NetLogCaptureMode::IncludeCookiesAndCredentials(),
351 net_log.GetCaptureMode());
mmenke 2015/05/06 15:14:42 optoinal: May want to replace all of these with I
eroman 2015/05/06 17:01:04 Done.
352 344
353 // Add event and make sure only second observer gets it. 345 // Add event and make sure only second observer gets it.
354 AddEvent(&net_log); 346 AddEvent(&net_log);
355 EXPECT_EQ(2U, observer[0].GetNumValues()); 347 EXPECT_EQ(2U, observer[0].GetNumValues());
356 EXPECT_EQ(1U, observer[1].GetNumValues()); 348 EXPECT_EQ(1U, observer[1].GetNumValues());
357 349
358 // Remove first observer. 350 // Remove first observer.
359 net_log.DeprecatedRemoveObserver(&observer[0]); 351 net_log.DeprecatedRemoveObserver(&observer[0]);
360 EXPECT_EQ(NULL, observer[0].net_log()); 352 EXPECT_EQ(NULL, observer[0].net_log());
361 EXPECT_EQ(NULL, observer[1].net_log()); 353 EXPECT_EQ(NULL, observer[1].net_log());
362 EXPECT_FALSE(net_log.GetCaptureMode().enabled()); 354 EXPECT_FALSE(net_log.IsCapturing());
363 355
364 // Add event and make sure neither observer gets it. 356 // Add event and make sure neither observer gets it.
365 AddEvent(&net_log); 357 AddEvent(&net_log);
366 EXPECT_EQ(2U, observer[0].GetNumValues()); 358 EXPECT_EQ(2U, observer[0].GetNumValues());
367 EXPECT_EQ(1U, observer[1].GetNumValues()); 359 EXPECT_EQ(1U, observer[1].GetNumValues());
368 } 360 }
369 361
370 // Makes sure that adding and removing observers simultaneously on different 362 // Makes sure that adding and removing observers simultaneously on different
371 // threads works. 363 // threads works.
372 TEST(NetLogTest, NetLogAddRemoveObserverThreads) { 364 TEST(NetLogTest, NetLogAddRemoveObserverThreads) {
373 NetLog net_log; 365 NetLog net_log;
374 366
375 // Run a bunch of threads to completion, each of which will repeatedly add 367 // Run a bunch of threads to completion, each of which will repeatedly add
376 // and remove an observer, and set its logging level. 368 // and remove an observer, and set its logging level.
377 RunTestThreads<AddRemoveObserverTestThread>(&net_log); 369 RunTestThreads<AddRemoveObserverTestThread>(&net_log);
378 } 370 }
379 371
380 } // namespace 372 } // namespace
381 373
382 } // namespace net 374 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698