| Flutter Impeller |

object_unittests.cc
Go to the documentation of this file.
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/testing/testing.h"
6 #include "impeller/toolkit/interop/impeller.h"
7 #include "impeller/toolkit/interop/object.h"
8
9 // NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks)
10
11 namespace impeller::interop::testing {
12
13 IMPELLER_DEFINE_HANDLE(FlagHandle);
14
15 class FlagObject final
16 : public Object<FlagObject, IMPELLER_INTERNAL_HANDLE_NAME(FlagHandle)> {
17 public:
18 explicit FlagObject(bool& destruction_flag)
19 : destruction_flag_(destruction_flag) {
20 FML_CHECK(!destruction_flag_) << "Destruction flag must be cleared.";
21 }
22
23 ~FlagObject() {
24 FML_CHECK(!destruction_flag_) << "Already destructed.";
25 destruction_flag_ = true;
26 }
27
28 private:
29 bool& destruction_flag_;
30 };
31
32 IMPELLER_DEFINE_HANDLE(TestHandle);
33
34 class TestObject final
35 : public Object<TestObject, IMPELLER_INTERNAL_HANDLE_NAME(TestHandle)> {
36 public:
37 TestObject(int arg1, double arg2, char arg3)
38 : arg1_(arg1), arg2_(arg2), arg3_(arg3) {}
39
40 ~TestObject() = default;
41
42 auto GetArg1() const { return arg1_; }
43
44 auto GetArg2() const { return arg2_; }
45
46 auto GetArg3() const { return arg3_; }
47
48 private:
49 int arg1_ = {};
50 double arg2_ = {};
51 char arg3_ = {};
52 };
53
54 TEST(InteropObjectTest, CanCreateScoped) {
55 bool destructed = false;
56 {
57 auto object = Adopt(new FlagObject(destructed)); //
58 }
59 ASSERT_TRUE(destructed);
60
61 destructed = false;
62 {
63 auto object = Ref(new FlagObject(destructed));
64 // New objects start with retain count of 1.
65 object->Release();
66 }
67 ASSERT_TRUE(destructed);
68 }
69
70 TEST(InteropObjectTest, CanCreate) {
71 auto object = Create<TestObject>(1, 1.3, 'c');
72 ASSERT_EQ(object->GetArg1(), 1);
73 ASSERT_EQ(object->GetArg2(), 1.3);
74 ASSERT_EQ(object->GetArg3(), 'c');
75 }
76
77 TEST(InteropObjectTest, CanCopyAssignMove) {
78 auto o = Create<TestObject>(1, 2.3, 'd');
79 ASSERT_EQ(o->GetRefCountForTests(), 1u);
80 {
81 auto o1 = o; // NOLINT(performance-unnecessary-copy-initialization)
82 ASSERT_EQ(o->GetRefCountForTests(), 2u);
83 auto o2 = o; // NOLINT(performance-unnecessary-copy-initialization)
84 ASSERT_EQ(o->GetRefCountForTests(), 3u);
85 auto o3 = o1; // NOLINT(performance-unnecessary-copy-initialization)
86 ASSERT_EQ(o->GetRefCountForTests(), 4u);
87 }
88 ASSERT_EQ(o->GetRefCountForTests(), 1u);
89
90 {
91 auto o1(o); // NOLINT(performance-unnecessary-copy-initialization)
92 ASSERT_EQ(o->GetRefCountForTests(), 2u);
93 ASSERT_EQ(o1->GetRefCountForTests(), 2u);
94 }
95
96 auto move_o = std::move(o);
97 ASSERT_EQ(move_o->GetRefCountForTests(), 1u);
98 }
99
100 } // namespace impeller::interop::testing
101
102 // NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
impeller::interop::Object
Definition: object.h:56
impeller::interop::testing::FlagObject
Definition: object_unittests.cc:16
impeller::interop::testing::FlagObject::FlagObject
FlagObject(bool &destruction_flag)
Definition: object_unittests.cc:18
impeller::interop::testing::FlagObject::~FlagObject
~FlagObject()
Definition: object_unittests.cc:23
impeller::interop::testing::TestObject
Definition: object_unittests.cc:35
impeller::interop::testing::TestObject::GetArg2
auto GetArg2() const
Definition: object_unittests.cc:44
impeller::interop::testing::TestObject::~TestObject
~TestObject()=default
impeller::interop::testing::TestObject::TestObject
TestObject(int arg1, double arg2, char arg3)
Definition: object_unittests.cc:37
impeller::interop::testing::TestObject::GetArg1
auto GetArg1() const
Definition: object_unittests.cc:42
impeller::interop::testing::TestObject::GetArg3
auto GetArg3() const
Definition: object_unittests.cc:46
impeller.h
impeller::interop::testing
Definition: impeller_unittests.cc:25
impeller::interop::testing::IMPELLER_DEFINE_HANDLE
IMPELLER_DEFINE_HANDLE(FlagHandle)
impeller::interop::testing::TEST
TEST(InteropObjectTest, CanCreateScoped)
Definition: object_unittests.cc:54
impeller::interop::Ref
ScopedObject< Object > Ref(Object *object)
Definition: object.h:146
impeller::interop::Adopt
ScopedObject< Object > Adopt(Object *object)
Definition: object.h:151
object.h