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