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/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
382 // - Destruct the WeakPtr on main thread | 382 // - Destruct the WeakPtr on main thread |
383 BackgroundThread background; | 383 BackgroundThread background; |
384 background.Start(); | 384 background.Start(); |
385 | 385 |
386 Target target; | 386 Target target; |
387 Arrow arrow; | 387 Arrow arrow; |
388 arrow.target = target.AsWeakPtr(); | 388 arrow.target = target.AsWeakPtr(); |
389 | 389 |
390 Arrow* arrow_copy; | 390 Arrow* arrow_copy; |
391 background.CreateArrowFromArrow(&arrow_copy, &arrow); | 391 background.CreateArrowFromArrow(&arrow_copy, &arrow); |
392 EXPECT_EQ(arrow_copy->target, &target); | 392 EXPECT_EQ(arrow_copy->target.get(), &target); |
393 background.DeleteArrow(arrow_copy); | 393 background.DeleteArrow(arrow_copy); |
394 } | 394 } |
395 | 395 |
396 TEST(WeakPtrTest, BackgroundThreadRefOutlivesMainThreadRef) { | 396 TEST(WeakPtrTest, BackgroundThreadRefOutlivesMainThreadRef) { |
397 // Originating thread drops all references before another thread. | 397 // Originating thread drops all references before another thread. |
398 // - Main thread creates a WeakPtr and passes copy to background thread | 398 // - Main thread creates a WeakPtr and passes copy to background thread |
399 // - Destruct the pointer on main thread | 399 // - Destruct the pointer on main thread |
400 // - Destruct the pointer on background thread | 400 // - Destruct the pointer on background thread |
401 BackgroundThread background; | 401 BackgroundThread background; |
402 background.Start(); | 402 background.Start(); |
403 | 403 |
404 Target target; | 404 Target target; |
405 Arrow* arrow_copy; | 405 Arrow* arrow_copy; |
406 { | 406 { |
407 Arrow arrow; | 407 Arrow arrow; |
408 arrow.target = target.AsWeakPtr(); | 408 arrow.target = target.AsWeakPtr(); |
409 background.CreateArrowFromArrow(&arrow_copy, &arrow); | 409 background.CreateArrowFromArrow(&arrow_copy, &arrow); |
410 } | 410 } |
411 EXPECT_EQ(arrow_copy->target, &target); | 411 EXPECT_EQ(arrow_copy->target.get(), &target); |
412 background.DeleteArrow(arrow_copy); | 412 background.DeleteArrow(arrow_copy); |
413 } | 413 } |
414 | 414 |
415 TEST(WeakPtrTest, OwnerThreadDeletesObject) { | 415 TEST(WeakPtrTest, OwnerThreadDeletesObject) { |
416 // Originating thread invalidates WeakPtrs while its held by other thread. | 416 // Originating thread invalidates WeakPtrs while its held by other thread. |
417 // - Main thread creates WeakPtr and passes Copy to background thread | 417 // - Main thread creates WeakPtr and passes Copy to background thread |
418 // - Object gets destroyed on main thread | 418 // - Object gets destroyed on main thread |
419 // (invalidates WeakPtr on background thread) | 419 // (invalidates WeakPtr on background thread) |
420 // - WeakPtr gets destroyed on Thread B | 420 // - WeakPtr gets destroyed on Thread B |
421 BackgroundThread background; | 421 BackgroundThread background; |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
579 background.Start(); | 579 background.Start(); |
580 background.DeleteTarget(target.release()); | 580 background.DeleteTarget(target.release()); |
581 | 581 |
582 // Main thread attempts to dereference the target, violating thread binding. | 582 // Main thread attempts to dereference the target, violating thread binding. |
583 ASSERT_DEATH(arrow.target.get(), ""); | 583 ASSERT_DEATH(arrow.target.get(), ""); |
584 } | 584 } |
585 | 585 |
586 #endif | 586 #endif |
587 | 587 |
588 } // namespace base | 588 } // namespace base |
OLD | NEW |