5#define FML_USED_ON_EMBEDDER
7#include "flutter/fml/memory/weak_ptr.h"
12#include "flutter/fml/message_loop.h"
13#include "flutter/fml/raster_thread_merger.h"
14#include "flutter/fml/synchronization/count_down_latch.h"
15#include "gtest/gtest.h"
20TEST(WeakPtrTest, Basic) {
22 WeakPtrFactory<int> factory(&
data);
23 WeakPtr<int> ptr = factory.GetWeakPtr();
24 EXPECT_EQ(&
data, ptr.get());
27TEST(WeakPtrTest, CopyConstruction) {
29 WeakPtrFactory<int> factory(&
data);
30 WeakPtr<int> ptr = factory.GetWeakPtr();
31 const WeakPtr<int>& ptr2(ptr);
32 EXPECT_EQ(&
data, ptr.get());
33 EXPECT_EQ(&
data, ptr2.get());
36TEST(WeakPtrTest, MoveConstruction) {
38 WeakPtrFactory<int> factory(&
data);
39 WeakPtr<int> ptr = factory.GetWeakPtr();
40 WeakPtr<int> ptr2(std::move(ptr));
43 EXPECT_EQ(
nullptr, ptr.get());
44 EXPECT_EQ(&
data, ptr2.get());
47TEST(WeakPtrTest, CopyAssignment) {
49 WeakPtrFactory<int> factory(&
data);
50 WeakPtr<int> ptr = factory.GetWeakPtr();
52 EXPECT_EQ(
nullptr, ptr2.get());
54 EXPECT_EQ(&
data, ptr.get());
55 EXPECT_EQ(&
data, ptr2.get());
58TEST(WeakPtrTest, MoveAssignment) {
60 WeakPtrFactory<int> factory(&
data);
61 WeakPtr<int> ptr = factory.GetWeakPtr();
63 EXPECT_EQ(
nullptr, ptr2.get());
64 ptr2 = std::move(ptr);
67 EXPECT_EQ(
nullptr, ptr.get());
68 EXPECT_EQ(&
data, ptr2.get());
71TEST(WeakPtrTest, Testable) {
73 WeakPtrFactory<int> factory(&
data);
75 EXPECT_EQ(
nullptr, ptr.get());
77 ptr = factory.GetWeakPtr();
78 EXPECT_EQ(&
data, ptr.get());
82TEST(WeakPtrTest, OutOfScope) {
84 EXPECT_EQ(
nullptr, ptr.get());
87 WeakPtrFactory<int> factory(&
data);
88 ptr = factory.GetWeakPtr();
90 EXPECT_EQ(
nullptr, ptr.get());
93TEST(WeakPtrTest, Multiple) {
98 WeakPtrFactory<int> factory(&
data);
99 a = factory.GetWeakPtr();
100 b = factory.GetWeakPtr();
101 EXPECT_EQ(&
data,
a.get());
102 EXPECT_EQ(&
data,
b.get());
104 EXPECT_EQ(
nullptr,
a.get());
105 EXPECT_EQ(
nullptr,
b.get());
108TEST(WeakPtrTest, MultipleStaged) {
112 WeakPtrFactory<int> factory(&
data);
113 a = factory.GetWeakPtr();
114 { WeakPtr<int>
b = factory.GetWeakPtr(); }
115 EXPECT_NE(
a.get(),
nullptr);
117 EXPECT_EQ(
nullptr,
a.get());
123struct Derived :
public Base {};
125TEST(WeakPtrTest, Dereference) {
127 data.member = 123456.;
128 WeakPtrFactory<Base> factory(&
data);
129 WeakPtr<Base> ptr = factory.GetWeakPtr();
130 EXPECT_EQ(&
data, ptr.get());
131 EXPECT_EQ(
data.member, (*ptr).member);
132 EXPECT_EQ(
data.member, ptr->member);
135TEST(WeakPtrTest, UpcastCopyConstruction) {
137 WeakPtrFactory<Derived> factory(&
data);
138 WeakPtr<Derived> ptr = factory.GetWeakPtr();
139 WeakPtr<Base> ptr2(ptr);
140 EXPECT_EQ(&
data, ptr.get());
141 EXPECT_EQ(&
data, ptr2.get());
144TEST(WeakPtrTest, UpcastMoveConstruction) {
146 WeakPtrFactory<Derived> factory(&
data);
147 WeakPtr<Derived> ptr = factory.GetWeakPtr();
148 WeakPtr<Base> ptr2(std::move(ptr));
151 EXPECT_EQ(
nullptr, ptr.get());
152 EXPECT_EQ(&
data, ptr2.get());
155TEST(WeakPtrTest, UpcastCopyAssignment) {
157 WeakPtrFactory<Derived> factory(&
data);
158 WeakPtr<Derived> ptr = factory.GetWeakPtr();
160 EXPECT_EQ(
nullptr, ptr2.get());
162 EXPECT_EQ(&
data, ptr.get());
163 EXPECT_EQ(&
data, ptr2.get());
166TEST(WeakPtrTest, UpcastMoveAssignment) {
168 WeakPtrFactory<Derived> factory(&
data);
169 WeakPtr<Derived> ptr = factory.GetWeakPtr();
171 EXPECT_EQ(
nullptr, ptr2.get());
172 ptr2 = std::move(ptr);
175 EXPECT_EQ(
nullptr, ptr.get());
176 EXPECT_EQ(&
data, ptr2.get());
179TEST(TaskRunnerAffineWeakPtrTest, ShouldNotCrashIfRunningOnTheSameTaskRunner) {
183 std::thread thread1([&
loop1, &latch1, &term1]() {
195 std::thread thread2([&
loop2, &latch2, &term2, &loop2_task_finish_latch,
196 &loop2_task_start_latch]() {
199 TaskRunnerAffineWeakPtrFactory<int> factory(&
data);
202 loop2->GetTaskRunner()->PostTask([&]() {
204 loop2_task_start_latch.
Wait();
205 TaskRunnerAffineWeakPtr<int> ptr = factory.GetWeakPtr();
206 EXPECT_EQ(*ptr,
data);
207 loop2_task_finish_latch.
Signal();
217 const auto raster_thread_merger =
218 fml::MakeRefCounted<fml::RasterThreadMerger>(qid1, qid2);
219 const size_t kNumFramesMerged = 5;
221 raster_thread_merger->MergeWithLease(kNumFramesMerged);
223 loop2_task_start_latch.
Signal();
224 loop2_task_finish_latch.
Wait();
226 for (
size_t i = 0;
i < kNumFramesMerged;
i++) {
227 ASSERT_TRUE(raster_thread_merger->IsMerged());
228 raster_thread_merger->DecrementLease();
231 ASSERT_FALSE(raster_thread_merger->IsMerged());
static void loop1(skiatest::Reporter *reporter, const char *filename)
static void loop2(skiatest::Reporter *reporter, const char *filename)
static void EnsureInitializedForCurrentThread()
static FML_EMBEDDER_ONLY MessageLoop & GetCurrent()
TEST(MallocMapping, EmptyContructor)
std::shared_ptr< const fml::Mapping > data
#define EXPECT_TRUE(handle)