1 /*
2 * Copyright (C) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <bitset>
17 #include <limits>
18 #include <type_traits>
19
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22
23 #include <base/util/uid_util.h>
24
25 #include <meta/api/make_callback.h>
26 #include <meta/ext/object.h>
27 #include <meta/interface/animation/builtin_animations.h>
28 #include <meta/interface/animation/intf_animation.h>
29 #include <meta/interface/intf_class_registry.h>
30 #include <meta/interface/intf_object_registry.h>
31
32 #include "src/test_runner.h"
33
34 using namespace testing;
35 using namespace testing::ext;
36
37 META_BEGIN_NAMESPACE()
38
39 class IntfClassRegistryTest : public testing::Test {
40 protected:
IntfClassRegistryTest()41 IntfClassRegistryTest() : classRegistry_(META_NS::GetClassRegistry()) {}
42
43 void SetUp() override {}
44 void TearDown() override {}
SetUpTestSuite()45 static void SetUpTestSuite()
46 {
47 SetTest();
48 }
TearDownTestSuite()49 static void TearDownTestSuite()
50 {
51 ResetTest();
52 }
53
54 protected:
55 IClassRegistry& classRegistry_;
56 };
57
MATCHER_P(ContainsClass, uid, �)58 MATCHER_P(ContainsClass, uid, "")
59 {
60 for (const auto& info : arg) {
61 if (info && info->GetClassInfo().Id() == uid) {
62 return true;
63 }
64 }
65 return false;
66 }
67
68 /**
69 * @tc.name: IntfClassRegistryTest
70 * @tc.desc: test GetAllClasses function
71 * @tc.type: FUNC
72 * @tc.require: I7DMS1
73 */
HWTEST_F(IntfClassRegistryTest, GetAllClasses, TestSize.Level1)74 HWTEST_F(IntfClassRegistryTest, GetAllClasses, TestSize.Level1)
75 {
76 auto all = classRegistry_.GetAllTypes({});
77 EXPECT_THAT(all, Not(IsEmpty()));
78 }
79
80 /**
81 * @tc.name: IntfClassRegistryTest
82 * @tc.desc: test GetAllTypesUis function
83 * @tc.type: FUNC
84 * @tc.require: I7DMS1
85 */
HWTEST_F(IntfClassRegistryTest, GetAllTypesUis, TestSize.Level1)86 HWTEST_F(IntfClassRegistryTest, GetAllTypesUis, TestSize.Level1)
87 {
88 auto animations = classRegistry_.GetAllTypes({ IAnimation::UID });
89 EXPECT_THAT(animations, Not(IsEmpty()));
90
91 EXPECT_THAT(animations, ContainsClass(ClassId::KeyframeAnimation));
92
93 auto staggered = classRegistry_.GetAllTypes({ IStaggeredAnimation::UID });
94
95 EXPECT_THAT(staggered, Not(ContainsClass(ClassId::KeyframeAnimation)));
96 }
97
98 META_REGISTER_CLASS(MyObject1, "440d971e-afae-497a-95c3-97c3ba7f5d18", META_NS::ObjectCategoryBits::APPLICATION)
99 META_REGISTER_CLASS(MyObject2, "f8d418d7-99aa-4b19-92b7-319959cc98f1", META_NS::ObjectCategoryBits::APPLICATION)
100
101 class MyObject1 : public META_NS::ObjectFwd<MyObject1, ClassId::MyObject1, META_NS::ClassId::Object> {
102 public:
103 };
104 class MyObject2 : public META_NS::ObjectFwd<MyObject2, ClassId::MyObject2, META_NS::ClassId::Object> {
105 public:
106 };
107
108 /**
109 * @tc.name: IntfClassRegistryTest
110 * @tc.desc: test Events function
111 * @tc.type: FUNC
112 * @tc.require: I7DMS1
113 */
HWTEST_F(IntfClassRegistryTest, Events, TestSize.Level1)114 HWTEST_F(IntfClassRegistryTest, Events, TestSize.Level1)
115 {
116 auto& objReg = META_NS::GetObjectRegistry();
117 auto& classReg = objReg.GetClassRegistry();
118
119 int registered = 0;
120 int unregistered = 0;
121 ClassRegistrationInfo lastRegistered;
122 ClassRegistrationInfo lastUnregistered;
123
124 auto token1 = classReg.OnClassRegistered()->AddHandler(META_NS::MakeCallback<META_NS::IOnClassRegistrationChanged>(
125 [®istered, &lastRegistered](const ClassRegistrationInfo& info) {
126 registered++;
127 lastRegistered = info;
128 }));
129
130 auto token2 =
131 classReg.OnClassUnregistered()->AddHandler(META_NS::MakeCallback<META_NS::IOnClassRegistrationChanged>(
132 [&unregistered, &lastUnregistered](const ClassRegistrationInfo& info) {
133 unregistered++;
134 lastUnregistered = info;
135 }));
136
137 EXPECT_TRUE(objReg.RegisterObjectType(MyObject1::GetFactory()));
138 EXPECT_EQ(registered, 1);
139 EXPECT_EQ(unregistered, 0);
140 EXPECT_EQ(lastRegistered.classInfo, MyObject1::GetFactory());
141 EXPECT_EQ(lastUnregistered.classInfo, nullptr);
142 lastRegistered.classInfo.reset();
143
144 EXPECT_FALSE(objReg.RegisterObjectType(MyObject1::GetFactory()));
145 EXPECT_EQ(registered, 1);
146 EXPECT_EQ(unregistered, 0);
147 EXPECT_EQ(lastRegistered.classInfo, nullptr);
148 EXPECT_EQ(lastUnregistered.classInfo, nullptr);
149
150 EXPECT_TRUE(objReg.RegisterObjectType(MyObject2::GetFactory()));
151 EXPECT_EQ(registered, 2);
152 EXPECT_EQ(unregistered, 0);
153 EXPECT_EQ(lastRegistered.classInfo, MyObject2::GetFactory());
154 EXPECT_EQ(lastUnregistered.classInfo, nullptr);
155 lastRegistered.classInfo.reset();
156
157 EXPECT_TRUE(objReg.UnregisterObjectType(MyObject1::GetFactory()));
158 EXPECT_EQ(registered, 2);
159 EXPECT_EQ(unregistered, 1);
160 EXPECT_EQ(lastRegistered.classInfo, nullptr);
161 EXPECT_EQ(lastUnregistered.classInfo, MyObject1::GetFactory());
162 lastUnregistered.classInfo.reset();
163
164 EXPECT_FALSE(objReg.UnregisterObjectType(MyObject1::GetFactory()));
165 EXPECT_EQ(registered, 2);
166 EXPECT_EQ(unregistered, 1);
167 EXPECT_EQ(lastRegistered.classInfo, nullptr);
168 EXPECT_EQ(lastUnregistered.classInfo, nullptr);
169
170 EXPECT_TRUE(objReg.UnregisterObjectType(MyObject2::GetFactory()));
171 EXPECT_EQ(registered, 2);
172 EXPECT_EQ(unregistered, 2);
173 EXPECT_EQ(lastRegistered.classInfo, nullptr);
174 EXPECT_EQ(lastUnregistered.classInfo, MyObject2::GetFactory());
175
176 EXPECT_TRUE(classReg.OnClassRegistered()->RemoveHandler(token1));
177 EXPECT_TRUE(classReg.OnClassUnregistered()->RemoveHandler(token2));
178 }
179
180 META_END_NAMESPACE()