OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/bind.h" | 6 #include "base/bind.h" |
7 #include "base/ios/weak_nsobject.h" | 7 #include "base/ios/weak_nsobject.h" |
8 #include "base/mac/scoped_nsobject.h" | 8 #include "base/mac/scoped_nsobject.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 void TouchWeakData(const WeakNSObject<NSMutableData>& weak_data) { | 102 void TouchWeakData(const WeakNSObject<NSMutableData>& weak_data) { |
103 if (!weak_data) | 103 if (!weak_data) |
104 return; | 104 return; |
105 [weak_data increaseLengthBy:1]; | 105 [weak_data increaseLengthBy:1]; |
106 } | 106 } |
107 | 107 |
108 // Makes a copy of |weak_object| on the current thread and posts a task to touch | 108 // Makes a copy of |weak_object| on the current thread and posts a task to touch |
109 // the weak object on its original thread. | 109 // the weak object on its original thread. |
110 void CopyWeakNSObjectAndPost(const WeakNSObject<NSMutableData>& weak_object, | 110 void CopyWeakNSObjectAndPost(const WeakNSObject<NSMutableData>& weak_object, |
111 scoped_refptr<SingleThreadTaskRunner> runner) { | 111 scoped_refptr<SingleThreadTaskRunner> runner) { |
112 WeakNSObject<NSMutableData> weak_copy(weak_object); | 112 // Copy using constructor. |
113 runner->PostTask(FROM_HERE, Bind(&TouchWeakData, weak_copy)); | 113 WeakNSObject<NSMutableData> weak_copy1(weak_object); |
| 114 runner->PostTask(FROM_HERE, Bind(&TouchWeakData, weak_copy1)); |
| 115 // Copy using assignment operator. |
| 116 WeakNSObject<NSMutableData> weak_copy2 = weak_object; |
| 117 runner->PostTask(FROM_HERE, Bind(&TouchWeakData, weak_copy2)); |
114 } | 118 } |
115 | 119 |
116 // Tests that the weak object can be copied on a different thread. | 120 // Tests that the weak object can be copied on a different thread. |
117 TEST(WeakNSObjectTest, WeakNSObjectCopyOnOtherThread) { | 121 TEST(WeakNSObjectTest, WeakNSObjectCopyOnOtherThread) { |
118 MessageLoop loop; | 122 MessageLoop loop; |
119 Thread other_thread("WeakNSObjectCopyOnOtherThread"); | 123 Thread other_thread("WeakNSObjectCopyOnOtherThread"); |
120 other_thread.Start(); | 124 other_thread.Start(); |
121 | 125 |
122 scoped_nsobject<NSMutableData> data([[NSMutableData alloc] init]); | 126 scoped_nsobject<NSMutableData> data([[NSMutableData alloc] init]); |
123 WeakNSObject<NSMutableData> weak(data); | 127 WeakNSObject<NSMutableData> weak(data); |
124 | 128 |
125 scoped_refptr<SingleThreadTaskRunner> runner = loop.task_runner(); | 129 scoped_refptr<SingleThreadTaskRunner> runner = loop.task_runner(); |
126 other_thread.task_runner()->PostTask( | 130 other_thread.task_runner()->PostTask( |
127 FROM_HERE, Bind(&CopyWeakNSObjectAndPost, weak, runner)); | 131 FROM_HERE, Bind(&CopyWeakNSObjectAndPost, weak, runner)); |
128 other_thread.Stop(); | 132 other_thread.Stop(); |
129 loop.RunUntilIdle(); | 133 loop.RunUntilIdle(); |
130 | 134 |
131 // Check that TouchWeakData was called. | 135 // Check that TouchWeakData was called and the object touched twice. |
132 EXPECT_EQ(1u, [data length]); | 136 EXPECT_EQ(2u, [data length]); |
133 } | 137 } |
134 | 138 |
135 } // namespace | 139 } // namespace |
136 } // namespace base | 140 } // namespace base |
OLD | NEW |