| 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 <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 409 | 409 |
| 410 // The new WeakPtr is owned by background thread. | 410 // The new WeakPtr is owned by background thread. |
| 411 EXPECT_EQ(target, background.DeRef(&arrow)); | 411 EXPECT_EQ(target, background.DeRef(&arrow)); |
| 412 } | 412 } |
| 413 | 413 |
| 414 // Target can only be deleted on background thread. | 414 // Target can only be deleted on background thread. |
| 415 background.DeleteTarget(target); | 415 background.DeleteTarget(target); |
| 416 background.DeleteArrow(arrow); | 416 background.DeleteArrow(arrow); |
| 417 } | 417 } |
| 418 | 418 |
| 419 TEST(WeakPtrTest, MoveOwnershipOfUnreferencedObject) { | |
| 420 BackgroundThread background; | |
| 421 background.Start(); | |
| 422 | |
| 423 Arrow* arrow; | |
| 424 { | |
| 425 Target target; | |
| 426 // Background thread creates WeakPtr. | |
| 427 background.CreateArrowFromTarget(&arrow, &target); | |
| 428 | |
| 429 // Bind to background thread. | |
| 430 EXPECT_EQ(&target, background.DeRef(arrow)); | |
| 431 | |
| 432 // Release the only WeakPtr. | |
| 433 arrow->target.reset(); | |
| 434 | |
| 435 // Now we should be able to create a new reference from this thread. | |
| 436 arrow->target = target.AsWeakPtr(); | |
| 437 | |
| 438 // Re-bind to main thread. | |
| 439 EXPECT_EQ(&target, arrow->target.get()); | |
| 440 | |
| 441 // And the main thread can now delete the target. | |
| 442 } | |
| 443 | |
| 444 delete arrow; | |
| 445 } | |
| 446 | |
| 447 TEST(WeakPtrTest, MoveOwnershipAfterInvalidate) { | 419 TEST(WeakPtrTest, MoveOwnershipAfterInvalidate) { |
| 448 BackgroundThread background; | 420 BackgroundThread background; |
| 449 background.Start(); | 421 background.Start(); |
| 450 | 422 |
| 451 Arrow arrow; | 423 Arrow arrow; |
| 452 std::unique_ptr<TargetWithFactory> target(new TargetWithFactory); | 424 std::unique_ptr<TargetWithFactory> target(new TargetWithFactory); |
| 453 | 425 |
| 454 // Bind to main thread. | 426 // Bind to main thread. |
| 455 arrow.target = target->factory.GetWeakPtr(); | 427 arrow.target = target->factory.GetWeakPtr(); |
| 456 EXPECT_EQ(target.get(), arrow.target.get()); | 428 EXPECT_EQ(target.get(), arrow.target.get()); |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 669 // Background thread tries to delete target, binding the object to the thread. | 641 // Background thread tries to delete target, binding the object to the thread. |
| 670 BackgroundThread background; | 642 BackgroundThread background; |
| 671 background.Start(); | 643 background.Start(); |
| 672 background.DeleteTarget(target.release()); | 644 background.DeleteTarget(target.release()); |
| 673 | 645 |
| 674 // Main thread attempts to dereference the target, violating thread binding. | 646 // Main thread attempts to dereference the target, violating thread binding. |
| 675 ASSERT_DCHECK_DEATH(arrow.target.get()); | 647 ASSERT_DCHECK_DEATH(arrow.target.get()); |
| 676 } | 648 } |
| 677 | 649 |
| 678 } // namespace base | 650 } // namespace base |
| OLD | NEW |