Index: base/memory/weak_ptr_unittest.cc |
diff --git a/base/memory/weak_ptr_unittest.cc b/base/memory/weak_ptr_unittest.cc |
index be3b6dbf6d1e6104435bc8a29e1cbda42621e946..ab3480c8057ec278ed15aa698f319b2e4d17dfc9 100644 |
--- a/base/memory/weak_ptr_unittest.cc |
+++ b/base/memory/weak_ptr_unittest.cc |
@@ -336,45 +336,63 @@ TEST(WeakPtrTest, MoveOwnershipImplicitly) { |
background.DeleteArrow(arrow); |
} |
-TEST(WeakPtrTest, MoveOwnershipExplicitlyObjectNotReferenced) { |
- // Case 1: The target is not bound to any thread yet. So calling |
- // DetachFromThread() is a no-op. |
- Target target; |
- target.DetachFromThreadHack(); |
- |
- // Case 2: The target is bound to main thread but no WeakPtr is pointing to |
- // it. In this case, it will be re-bound to any thread trying to get a |
- // WeakPtr pointing to it. So detach function call is again no-op. |
- { |
- WeakPtr<Target> weak_ptr = target.AsWeakPtr(); |
- } |
- target.DetachFromThreadHack(); |
-} |
- |
-TEST(WeakPtrTest, MoveOwnershipExplicitly) { |
+TEST(WeakPtrTest, MoveOwnershipOfUnreferencedObject) { |
BackgroundThread background; |
background.Start(); |
Arrow* arrow; |
{ |
Target target; |
- // Background thread creates WeakPtr(and implicitly owns the object). |
+ // Background thread creates WeakPtr. |
background.CreateArrowFromTarget(&arrow, &target); |
+ |
+ // Bind to background thread. |
EXPECT_EQ(&target, background.DeRef(arrow)); |
- // Detach from background thread. |
- target.DetachFromThreadHack(); |
+ // Release the only WeakPtr. |
+ arrow->target.reset(); |
+ |
+ // Now we should be able to create a new reference from this thread. |
+ arrow->target = target.AsWeakPtr(); |
// Re-bind to main thread. |
EXPECT_EQ(&target, arrow->target.get()); |
- // Main thread can now delete the target. |
+ // And the main thread can now delete the target. |
} |
// WeakPtr can be deleted on non-owner thread. |
background.DeleteArrow(arrow); |
Wez
2013/08/01 21:09:10
nit: This doesn't feel like it belongs in this tes
no sievers
2013/08/02 22:24:09
Done.
NonOwnerThreadCanDeleteWeakPtr already cover
|
} |
+TEST(WeakPtrTest, MoveOwnershipAfterInvalidate) { |
+ BackgroundThread background; |
+ background.Start(); |
+ |
+ Arrow arrow; |
+ { |
+ Target* target = new Target; |
no sievers
2013/08/01 20:57:59
I could add some plumbing in Background to avoid i
Wez
2013/08/01 21:09:10
You can create a Target, wrap it with a WeakPtrFac
Wez
2013/08/01 21:09:10
nit: scoped_ptr<Target> target = new Target, and t
no sievers
2013/08/02 22:24:09
Done.
|
+ WeakPtrFactory<Target> factory(target); |
+ |
+ // Bind to main thread. |
+ arrow.target = factory.GetWeakPtr(); |
+ EXPECT_EQ(target, arrow.target.get()); |
+ |
+ factory.InvalidateWeakPtrs(); |
+ EXPECT_EQ(NULL, arrow.target.get()); |
+ |
+ arrow.target = factory.GetWeakPtr(); |
+ // Re-bind to background thread. |
+ EXPECT_EQ(target, background.DeRef(&arrow)); |
+ |
+ // And the background thread can now delete the target. |
+ background.DeleteTarget(target); |
+ |
+ // Make sure we can delete the factory on the main thread. |
Wez
2013/08/01 21:09:10
This code's not deleting the factory, it's droppin
|
+ arrow.target.reset(); |
no sievers
2013/08/01 20:57:59
This wouldn't be needed if I made the test the opp
Wez
2013/08/01 21:09:10
Why is it needed at all? It's just clearing the We
no sievers
2013/08/01 21:15:12
Because the factory's flag is now bound to the bac
|
+ } |
+} |
+ |
TEST(WeakPtrTest, MainThreadRefOutlivesBackgroundThreadRef) { |
// Originating thread has a WeakPtr that outlives others. |
// - Main thread creates a WeakPtr |