| 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 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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) { | 435 TEST(WeakPtrTest, NonOwnerThreadCanCopyAndAssignWeakPtr) { |
| 436 // Main thread creates a Target object. | 436 // Main thread creates a Target object. |
| 437 Target target; | 437 Target target; |
| 438 // Main thread creates an arrow referencing the Target. | 438 // Main thread creates an arrow referencing the Target. |
| 439 Arrow *arrow; | 439 Arrow *arrow = new Arrow(); |
| 440 { | 440 arrow->target = target.AsWeakPtr(); |
| 441 ANNOTATE_SCOPED_MEMORY_LEAK; | 441 |
| 442 arrow = new Arrow(); | |
| 443 arrow->target = target.AsWeakPtr(); | |
| 444 } | |
| 445 // Background can copy and assign arrow (as well as the WeakPtr inside). | 442 // Background can copy and assign arrow (as well as the WeakPtr inside). |
| 446 BackgroundThread background; | 443 BackgroundThread background; |
| 447 background.Start(); | 444 background.Start(); |
| 448 background.CopyAndAssignArrow(arrow); | 445 background.CopyAndAssignArrow(arrow); |
| 446 background.DeleteArrow(arrow); |
| 449 } | 447 } |
| 450 | 448 |
| 451 TEST(WeakPtrTest, NonOwnerThreadCanCopyAndAssignWeakPtrBase) { | 449 TEST(WeakPtrTest, NonOwnerThreadCanCopyAndAssignWeakPtrBase) { |
| 452 // Main thread creates a Target object. | 450 // Main thread creates a Target object. |
| 453 Target target; | 451 Target target; |
| 454 // Main thread creates an arrow referencing the Target. | 452 // Main thread creates an arrow referencing the Target. |
| 455 Arrow *arrow; | 453 Arrow *arrow = new Arrow(); |
| 456 { | 454 arrow->target = target.AsWeakPtr(); |
| 457 ANNOTATE_SCOPED_MEMORY_LEAK; | 455 |
| 458 arrow = new Arrow(); | |
| 459 arrow->target = target.AsWeakPtr(); | |
| 460 } | |
| 461 // Background can copy and assign arrow's WeakPtr to a base class WeakPtr. | 456 // Background can copy and assign arrow's WeakPtr to a base class WeakPtr. |
| 462 BackgroundThread background; | 457 BackgroundThread background; |
| 463 background.Start(); | 458 background.Start(); |
| 464 background.CopyAndAssignArrowBase(arrow); | 459 background.CopyAndAssignArrowBase(arrow); |
| 460 background.DeleteArrow(arrow); |
| 465 } | 461 } |
| 466 | 462 |
| 467 TEST(WeakPtrTest, NonOwnerThreadCanDeleteWeakPtr) { | 463 TEST(WeakPtrTest, NonOwnerThreadCanDeleteWeakPtr) { |
| 468 // Main thread creates a Target object. | 464 // Main thread creates a Target object. |
| 469 Target target; | 465 Target target; |
| 470 // Main thread creates an arrow referencing the Target. | 466 // Main thread creates an arrow referencing the Target. |
| 471 Arrow* arrow = new Arrow(); | 467 Arrow* arrow = new Arrow(); |
| 472 arrow->target = target.AsWeakPtr(); | 468 arrow->target = target.AsWeakPtr(); |
| 473 | 469 |
| 474 // Background can delete arrow (as well as the WeakPtr inside). | 470 // Background can delete arrow (as well as the WeakPtr inside). |
| (...skipping 111 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 |