1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "testing/gtest/include/gtest/gtest.h"
6 #include "base/thread.h"
7 #include "base/weak_ptr.h"
13 class OffThreadObjectCreator
{
15 static T
* NewObject() {
18 Thread
creator_thread("creator_thread");
19 creator_thread
.Start();
20 creator_thread
.message_loop()->PostTask(FROM_HERE
,
21 NewRunnableFunction(OffThreadObjectCreator::CreateObject
, &result
));
23 DCHECK(result
); // We synchronized on thread destruction above.
27 static void CreateObject(T
** result
) {
33 struct Derived
: Base
{};
35 struct Producer
: SupportsWeakPtr
<Producer
> {};
36 struct Consumer
{ WeakPtr
<Producer
> producer
; };
40 TEST(WeakPtrTest
, Basic
) {
42 WeakPtrFactory
<int> factory(&data
);
43 WeakPtr
<int> ptr
= factory
.GetWeakPtr();
44 EXPECT_EQ(&data
, ptr
.get());
47 TEST(WeakPtrTest
, Comparison
) {
49 WeakPtrFactory
<int> factory(&data
);
50 WeakPtr
<int> ptr
= factory
.GetWeakPtr();
51 WeakPtr
<int> ptr2
= ptr
;
52 EXPECT_TRUE(ptr
== ptr2
);
55 TEST(WeakPtrTest
, OutOfScope
) {
57 EXPECT_TRUE(ptr
.get() == NULL
);
60 WeakPtrFactory
<int> factory(&data
);
61 ptr
= factory
.GetWeakPtr();
63 EXPECT_TRUE(ptr
.get() == NULL
);
66 TEST(WeakPtrTest
, Multiple
) {
70 WeakPtrFactory
<int> factory(&data
);
71 a
= factory
.GetWeakPtr();
72 b
= factory
.GetWeakPtr();
73 EXPECT_EQ(&data
, a
.get());
74 EXPECT_EQ(&data
, b
.get());
76 EXPECT_TRUE(a
.get() == NULL
);
77 EXPECT_TRUE(b
.get() == NULL
);
80 TEST(WeakPtrTest
, UpCast
) {
82 WeakPtrFactory
<Derived
> factory(&data
);
83 WeakPtr
<Base
> ptr
= factory
.GetWeakPtr();
84 ptr
= factory
.GetWeakPtr();
85 EXPECT_EQ(ptr
.get(), &data
);
88 TEST(WeakPtrTest
, SupportsWeakPtr
) {
90 WeakPtr
<Producer
> ptr
= f
.AsWeakPtr();
91 EXPECT_EQ(&f
, ptr
.get());
94 TEST(WeakPtrTest
, InvalidateWeakPtrs
) {
96 WeakPtrFactory
<int> factory(&data
);
97 WeakPtr
<int> ptr
= factory
.GetWeakPtr();
98 EXPECT_EQ(&data
, ptr
.get());
99 EXPECT_TRUE(factory
.HasWeakPtrs());
100 factory
.InvalidateWeakPtrs();
101 EXPECT_TRUE(ptr
.get() == NULL
);
102 EXPECT_FALSE(factory
.HasWeakPtrs());
105 TEST(WeakPtrTest
, HasWeakPtrs
) {
107 WeakPtrFactory
<int> factory(&data
);
109 WeakPtr
<int> ptr
= factory
.GetWeakPtr();
110 EXPECT_TRUE(factory
.HasWeakPtrs());
112 EXPECT_FALSE(factory
.HasWeakPtrs());
115 TEST(WeakPtrTest
, SingleThreaded1
) {
116 // Test that it is OK to create a class that supports weak references on one
117 // thread, but use it on another. This tests that we do not trip runtime
118 // checks that ensure that a weak reference is not used by multiple threads.
119 scoped_ptr
<Producer
> producer(OffThreadObjectCreator
<Producer
>::NewObject());
120 WeakPtr
<Producer
> weak_producer
= producer
->AsWeakPtr();
121 EXPECT_EQ(producer
.get(), weak_producer
.get());
124 TEST(WeakPtrTest
, SingleThreaded2
) {
125 // Test that it is OK to create a class that has a WeakPtr member on one
126 // thread, but use it on another. This tests that we do not trip runtime
127 // checks that ensure that a weak reference is not used by multiple threads.
128 scoped_ptr
<Consumer
> consumer(OffThreadObjectCreator
<Consumer
>::NewObject());
130 consumer
->producer
= producer
.AsWeakPtr();
131 EXPECT_EQ(&producer
, consumer
->producer
.get());