OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
6 #include "base/memory/weak_ptr.h" | 6 #include "base/memory/weak_ptr.h" |
7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/threading/thread.h" | 9 #include "base/threading/thread.h" |
10 | 10 |
(...skipping 20 matching lines...) Expand all Loading... |
31 *result = new T; | 31 *result = new T; |
32 } | 32 } |
33 }; | 33 }; |
34 | 34 |
35 struct Base {}; | 35 struct Base {}; |
36 struct Derived : Base {}; | 36 struct Derived : Base {}; |
37 | 37 |
38 struct Producer : SupportsWeakPtr<Producer> {}; | 38 struct Producer : SupportsWeakPtr<Producer> {}; |
39 struct Consumer { WeakPtr<Producer> producer; }; | 39 struct Consumer { WeakPtr<Producer> producer; }; |
40 | 40 |
| 41 struct Complex { |
| 42 Complex() { |
| 43 consumer.producer = producer.AsWeakPtr(); |
| 44 } |
| 45 Producer producer; |
| 46 Consumer consumer; |
| 47 }; |
| 48 |
41 } // namespace | 49 } // namespace |
42 | 50 |
43 TEST(WeakPtrTest, Basic) { | 51 TEST(WeakPtrTest, Basic) { |
44 int data; | 52 int data; |
45 WeakPtrFactory<int> factory(&data); | 53 WeakPtrFactory<int> factory(&data); |
46 WeakPtr<int> ptr = factory.GetWeakPtr(); | 54 WeakPtr<int> ptr = factory.GetWeakPtr(); |
47 EXPECT_EQ(&data, ptr.get()); | 55 EXPECT_EQ(&data, ptr.get()); |
48 } | 56 } |
49 | 57 |
50 TEST(WeakPtrTest, Comparison) { | 58 TEST(WeakPtrTest, Comparison) { |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 TEST(WeakPtrTest, SingleThreaded2) { | 149 TEST(WeakPtrTest, SingleThreaded2) { |
142 // Test that it is OK to create a class that has a WeakPtr member on one | 150 // Test that it is OK to create a class that has a WeakPtr member on one |
143 // thread, but use it on another. This tests that we do not trip runtime | 151 // thread, but use it on another. This tests that we do not trip runtime |
144 // checks that ensure that a weak reference is not used by multiple threads. | 152 // checks that ensure that a weak reference is not used by multiple threads. |
145 scoped_ptr<Consumer> consumer(OffThreadObjectCreator<Consumer>::NewObject()); | 153 scoped_ptr<Consumer> consumer(OffThreadObjectCreator<Consumer>::NewObject()); |
146 Producer producer; | 154 Producer producer; |
147 consumer->producer = producer.AsWeakPtr(); | 155 consumer->producer = producer.AsWeakPtr(); |
148 EXPECT_EQ(&producer, consumer->producer.get()); | 156 EXPECT_EQ(&producer, consumer->producer.get()); |
149 } | 157 } |
150 | 158 |
| 159 TEST(WeakPtrTest, ReattachThread) { |
| 160 // Test that we can move a class that supports weak references to another |
| 161 // thread. This should work and not trip any runtime checks as long as |
| 162 // there are no references in existence. |
| 163 scoped_ptr<Complex> complex(OffThreadObjectCreator<Complex>::NewObject()); |
| 164 complex->consumer.producer.reset(); |
| 165 complex->consumer.producer = complex->producer.AsWeakPtr(); |
| 166 EXPECT_EQ(complex->consumer.producer.get(), &complex->producer); |
| 167 } |
| 168 |
151 } // namespace base | 169 } // namespace base |
OLD | NEW |