1 // Copyright 2013 The Flutter 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.
4
5 #include "flutter/fml/memory/weak_ptr.h"
6
7 #include <utility>
8
9 #include "gtest/gtest.h"
10
11 namespace fml {
12 namespace {
13
TEST(WeakPtrTest,Basic)14 TEST(WeakPtrTest, Basic) {
15 int data = 0;
16 WeakPtrFactory<int> factory(&data);
17 WeakPtr<int> ptr = factory.GetWeakPtr();
18 EXPECT_EQ(&data, ptr.get());
19 }
20
TEST(WeakPtrTest,CopyConstruction)21 TEST(WeakPtrTest, CopyConstruction) {
22 int data = 0;
23 WeakPtrFactory<int> factory(&data);
24 WeakPtr<int> ptr = factory.GetWeakPtr();
25 WeakPtr<int> ptr2(ptr);
26 EXPECT_EQ(&data, ptr.get());
27 EXPECT_EQ(&data, ptr2.get());
28 }
29
TEST(WeakPtrTest,MoveConstruction)30 TEST(WeakPtrTest, MoveConstruction) {
31 int data = 0;
32 WeakPtrFactory<int> factory(&data);
33 WeakPtr<int> ptr = factory.GetWeakPtr();
34 WeakPtr<int> ptr2(std::move(ptr));
35 EXPECT_EQ(nullptr, ptr.get());
36 EXPECT_EQ(&data, ptr2.get());
37 }
38
TEST(WeakPtrTest,CopyAssignment)39 TEST(WeakPtrTest, CopyAssignment) {
40 int data = 0;
41 WeakPtrFactory<int> factory(&data);
42 WeakPtr<int> ptr = factory.GetWeakPtr();
43 WeakPtr<int> ptr2;
44 EXPECT_EQ(nullptr, ptr2.get());
45 ptr2 = ptr;
46 EXPECT_EQ(&data, ptr.get());
47 EXPECT_EQ(&data, ptr2.get());
48 }
49
TEST(WeakPtrTest,MoveAssignment)50 TEST(WeakPtrTest, MoveAssignment) {
51 int data = 0;
52 WeakPtrFactory<int> factory(&data);
53 WeakPtr<int> ptr = factory.GetWeakPtr();
54 WeakPtr<int> ptr2;
55 EXPECT_EQ(nullptr, ptr2.get());
56 ptr2 = std::move(ptr);
57 EXPECT_EQ(nullptr, ptr.get());
58 EXPECT_EQ(&data, ptr2.get());
59 }
60
TEST(WeakPtrTest,Testable)61 TEST(WeakPtrTest, Testable) {
62 int data = 0;
63 WeakPtrFactory<int> factory(&data);
64 WeakPtr<int> ptr;
65 EXPECT_EQ(nullptr, ptr.get());
66 EXPECT_FALSE(ptr);
67 ptr = factory.GetWeakPtr();
68 EXPECT_EQ(&data, ptr.get());
69 EXPECT_TRUE(ptr);
70 }
71
TEST(WeakPtrTest,OutOfScope)72 TEST(WeakPtrTest, OutOfScope) {
73 WeakPtr<int> ptr;
74 EXPECT_EQ(nullptr, ptr.get());
75 {
76 int data = 0;
77 WeakPtrFactory<int> factory(&data);
78 ptr = factory.GetWeakPtr();
79 }
80 EXPECT_EQ(nullptr, ptr.get());
81 }
82
TEST(WeakPtrTest,Multiple)83 TEST(WeakPtrTest, Multiple) {
84 WeakPtr<int> a;
85 WeakPtr<int> b;
86 {
87 int data = 0;
88 WeakPtrFactory<int> factory(&data);
89 a = factory.GetWeakPtr();
90 b = factory.GetWeakPtr();
91 EXPECT_EQ(&data, a.get());
92 EXPECT_EQ(&data, b.get());
93 }
94 EXPECT_EQ(nullptr, a.get());
95 EXPECT_EQ(nullptr, b.get());
96 }
97
TEST(WeakPtrTest,MultipleStaged)98 TEST(WeakPtrTest, MultipleStaged) {
99 WeakPtr<int> a;
100 {
101 int data = 0;
102 WeakPtrFactory<int> factory(&data);
103 a = factory.GetWeakPtr();
104 { WeakPtr<int> b = factory.GetWeakPtr(); }
105 EXPECT_NE(a.get(), nullptr);
106 }
107 EXPECT_EQ(nullptr, a.get());
108 }
109
110 struct Base {
111 double member = 0.;
112 };
113 struct Derived : public Base {};
114
TEST(WeakPtrTest,Dereference)115 TEST(WeakPtrTest, Dereference) {
116 Base data;
117 data.member = 123456.;
118 WeakPtrFactory<Base> factory(&data);
119 WeakPtr<Base> ptr = factory.GetWeakPtr();
120 EXPECT_EQ(&data, ptr.get());
121 EXPECT_EQ(data.member, (*ptr).member);
122 EXPECT_EQ(data.member, ptr->member);
123 }
124
TEST(WeakPtrTest,UpcastCopyConstruction)125 TEST(WeakPtrTest, UpcastCopyConstruction) {
126 Derived data;
127 WeakPtrFactory<Derived> factory(&data);
128 WeakPtr<Derived> ptr = factory.GetWeakPtr();
129 WeakPtr<Base> ptr2(ptr);
130 EXPECT_EQ(&data, ptr.get());
131 EXPECT_EQ(&data, ptr2.get());
132 }
133
TEST(WeakPtrTest,UpcastMoveConstruction)134 TEST(WeakPtrTest, UpcastMoveConstruction) {
135 Derived data;
136 WeakPtrFactory<Derived> factory(&data);
137 WeakPtr<Derived> ptr = factory.GetWeakPtr();
138 WeakPtr<Base> ptr2(std::move(ptr));
139 EXPECT_EQ(nullptr, ptr.get());
140 EXPECT_EQ(&data, ptr2.get());
141 }
142
TEST(WeakPtrTest,UpcastCopyAssignment)143 TEST(WeakPtrTest, UpcastCopyAssignment) {
144 Derived data;
145 WeakPtrFactory<Derived> factory(&data);
146 WeakPtr<Derived> ptr = factory.GetWeakPtr();
147 WeakPtr<Base> ptr2;
148 EXPECT_EQ(nullptr, ptr2.get());
149 ptr2 = ptr;
150 EXPECT_EQ(&data, ptr.get());
151 EXPECT_EQ(&data, ptr2.get());
152 }
153
TEST(WeakPtrTest,UpcastMoveAssignment)154 TEST(WeakPtrTest, UpcastMoveAssignment) {
155 Derived data;
156 WeakPtrFactory<Derived> factory(&data);
157 WeakPtr<Derived> ptr = factory.GetWeakPtr();
158 WeakPtr<Base> ptr2;
159 EXPECT_EQ(nullptr, ptr2.get());
160 ptr2 = std::move(ptr);
161 EXPECT_EQ(nullptr, ptr.get());
162 EXPECT_EQ(&data, ptr2.get());
163 }
164
165 } // namespace
166 } // namespace fml
167