Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 #include "base/memory/weak_ptr.h" | 5 #include "base/memory/weak_ptr.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/debug/leak_annotations.h" | 10 #include "base/debug/leak_annotations.h" |
| (...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 425 { | 425 { |
| 426 Target target; | 426 Target target; |
| 427 Arrow arrow; | 427 Arrow arrow; |
| 428 arrow.target = target.AsWeakPtr(); | 428 arrow.target = target.AsWeakPtr(); |
| 429 background.CreateArrowFromArrow(&arrow_copy, &arrow); | 429 background.CreateArrowFromArrow(&arrow_copy, &arrow); |
| 430 } | 430 } |
| 431 EXPECT_EQ(NULL, arrow_copy->target.get()); | 431 EXPECT_EQ(NULL, arrow_copy->target.get()); |
| 432 background.DeleteArrow(arrow_copy); | 432 background.DeleteArrow(arrow_copy); |
| 433 } | 433 } |
| 434 | 434 |
| 435 TEST(WeakPtrTest, NonOwnerThreadCanCopyAndAssignWeakPtr) { | |
| 436 // Main thread creates a Target object. | |
| 437 Target target; | |
| 438 // Main thread creates an arrow referencing the Target. | |
| 439 Arrow *arrow; | |
| 440 { | |
| 441 ANNOTATE_SCOPED_MEMORY_LEAK; | |
| 442 arrow = new Arrow(); | |
| 443 arrow->target = target.AsWeakPtr(); | |
| 444 } | |
| 445 // Background can copy and assign arrow (as well as the WeakPtr inside). | |
| 446 BackgroundThread background; | |
| 447 background.Start(); | |
| 448 background.CopyAndAssignArrow(arrow); | |
| 449 } | |
| 450 | |
| 451 TEST(WeakPtrTest, NonOwnerThreadCanCopyAndAssignWeakPtrBase) { | |
| 452 // Main thread creates a Target object. | |
| 453 Target target; | |
| 454 // Main thread creates an arrow referencing the Target. | |
| 455 Arrow *arrow; | |
| 456 { | |
| 457 ANNOTATE_SCOPED_MEMORY_LEAK; | |
| 458 arrow = new Arrow(); | |
| 459 arrow->target = target.AsWeakPtr(); | |
| 460 } | |
| 461 // Background can copy and assign arrow's WeakPtr to a base class WeakPtr. | |
| 462 BackgroundThread background; | |
| 463 background.Start(); | |
| 464 background.CopyAndAssignArrowBase(arrow); | |
| 465 } | |
| 466 | |
| 467 TEST(WeakPtrTest, NonOwnerThreadCanDeleteWeakPtr) { | 435 TEST(WeakPtrTest, NonOwnerThreadCanDeleteWeakPtr) { |
| 468 // Main thread creates a Target object. | 436 // Main thread creates a Target object. |
| 469 Target target; | 437 Target target; |
| 470 // Main thread creates an arrow referencing the Target. | 438 // Main thread creates an arrow referencing the Target. |
| 471 Arrow* arrow = new Arrow(); | 439 Arrow* arrow = new Arrow(); |
| 472 arrow->target = target.AsWeakPtr(); | 440 arrow->target = target.AsWeakPtr(); |
| 473 | 441 |
| 474 // Background can delete arrow (as well as the WeakPtr inside). | 442 // Background can delete arrow (as well as the WeakPtr inside). |
| 475 BackgroundThread background; | 443 BackgroundThread background; |
| 476 background.Start(); | 444 background.Start(); |
| 477 background.DeleteArrow(arrow); | 445 background.DeleteArrow(arrow); |
| 478 } | 446 } |
| 479 | 447 |
| 448 TEST(WeakPtrTest, NonOwnerThreadCanCopyAndAssignWeakPtr) { | |
|
jar (doing other things)
2013/06/10 03:32:41
Why did you move the two test methods down (below
akalin
2013/06/10 17:58:21
I think that's the logical order of the tests, sin
| |
| 449 // Main thread creates a Target object. | |
| 450 Target target; | |
| 451 // Main thread creates an arrow referencing the Target. | |
| 452 Arrow *arrow = new Arrow(); | |
| 453 arrow->target = target.AsWeakPtr(); | |
| 454 | |
| 455 // Background can copy and assign arrow (as well as the WeakPtr inside). | |
| 456 BackgroundThread background; | |
| 457 background.Start(); | |
| 458 background.CopyAndAssignArrow(arrow); | |
| 459 background.DeleteArrow(arrow); | |
| 460 } | |
| 461 | |
| 462 TEST(WeakPtrTest, NonOwnerThreadCanCopyAndAssignWeakPtrBase) { | |
| 463 // Main thread creates a Target object. | |
| 464 Target target; | |
| 465 // Main thread creates an arrow referencing the Target. | |
| 466 Arrow *arrow = new Arrow(); | |
| 467 arrow->target = target.AsWeakPtr(); | |
| 468 | |
| 469 // Background can copy and assign arrow's WeakPtr to a base class WeakPtr. | |
| 470 BackgroundThread background; | |
| 471 background.Start(); | |
| 472 background.CopyAndAssignArrowBase(arrow); | |
| 473 background.DeleteArrow(arrow); | |
| 474 } | |
| 475 | |
| 480 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST | 476 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST |
| 481 | 477 |
| 482 TEST(WeakPtrDeathTest, WeakPtrCopyDoesNotChangeThreadBinding) { | 478 TEST(WeakPtrDeathTest, WeakPtrCopyDoesNotChangeThreadBinding) { |
| 483 // The default style "fast" does not support multi-threaded tests | 479 // The default style "fast" does not support multi-threaded tests |
| 484 // (introduces deadlock on Linux). | 480 // (introduces deadlock on Linux). |
| 485 ::testing::FLAGS_gtest_death_test_style = "threadsafe"; | 481 ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 486 | 482 |
| 487 BackgroundThread background; | 483 BackgroundThread background; |
| 488 background.Start(); | 484 background.Start(); |
| 489 | 485 |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 586 background.Start(); | 582 background.Start(); |
| 587 background.DeleteTarget(target.release()); | 583 background.DeleteTarget(target.release()); |
| 588 | 584 |
| 589 // Main thread attempts to dereference the target, violating thread binding. | 585 // Main thread attempts to dereference the target, violating thread binding. |
| 590 ASSERT_DEATH(arrow.target.get(), ""); | 586 ASSERT_DEATH(arrow.target.get(), ""); |
| 591 } | 587 } |
| 592 | 588 |
| 593 #endif | 589 #endif |
| 594 | 590 |
| 595 } // namespace base | 591 } // namespace base |
| OLD | NEW |