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 <gtest/gtest.h>
17
18 #include <meta/api/animation/animation.h>
19 #include <meta/api/connector.h>
20 #include <meta/api/function.h>
21 #include <meta/ext/any_builder.h>
22 #include <meta/ext/attachment/attachment.h>
23 #include <meta/ext/object.h>
24 #include <meta/interface/intf_connector.h>
25 #include <meta/interface/property/property.h>
26
27 #include "src/serialisation_utils.h"
28 #include "src/testing_objects.h"
29 #include "src/util.h"
30 #include "src/test_runner.h"
31
32 using namespace testing::ext;
33
34 META_BEGIN_NAMESPACE()
35
36 class FunctionTest : public testing::Test {
37 public:
SetUpTestSuite()38 static void SetUpTestSuite()
39 {
40 SetTest();
41 UnregisterTestTypes(); // Unregister TestType
42 }
TearDownTestSuite()43 static void TearDownTestSuite()
44 {
45 ResetTest();
46 }
47 void SetUp() override {}
48 void TearDown() override {}
49 };
50
51 namespace {
52
53 class IFuncTestType : public CORE_NS::IInterface {
54 META_INTERFACE(CORE_NS::IInterface, IFuncTestType, "5c79a9ab-f673-49b5-b18a-f16d0db50c96")
55 public:
56 virtual void MyFunc() = 0;
57 virtual int MyFunc2(int) = 0;
58 virtual void OnStart() = 0;
59
60 META_EVENT(IOnChanged, OnTest)
61 META_PROPERTY(int, TestProperty)
62 };
63
64 META_REGISTER_CLASS(TestType, "abcd6e8b-36d4-44e4-802d-b2d80e50c612", META_NS::ObjectCategoryBits::APPLICATION)
65
66 class TestType final : public META_NS::ObjectFwd<
67 TestType, Meta::ClassId::TestType, META_NS::ClassId::Object, IFuncTestType> {
68 using Super = META_NS::ObjectFwd<TestType, Meta::ClassId::TestType, META_NS::ClassId::Object, IFuncTestType>;
69 using Super::Super;
70
71 public:
72 META_IMPLEMENT_FUNCTION(TestType, MyFunc);
73 META_IMPLEMENT_FUNCTION(TestType, MyFunc2, "value");
74 META_IMPLEMENT_FUNCTION(TestType, OnStart, "state");
75
76 META_IMPLEMENT_PROPERTY(int, TestProperty);
77
78 void MyFunc() override
79 {
80 myFuncCalled_ = true;
81 }
82
83 int MyFunc2(int v) override
84 {
85 myFunc2Called_ = v;
86 return v;
87 }
88
89 void OnStart() override
90 {
91 onStartCalled_ = true;
92 }
93 META_IMPLEMENT_EVENT(IOnChanged, OnTest);
94
95 public:
96 bool myFuncCalled_ {};
97 int myFunc2Called_ {};
98 bool onStartCalled_ {};
99 };
100
101 } // namespace
102
103
104 /**
105 * @tc.name: FunctionTest
106 * @tc.desc: test EventConnect function
107 * @tc.type: FUNC
108 * @tc.require: I7DMS1
109 */
HWTEST_F(FunctionTest, EventConnect, TestSize.Level1)110 HWTEST_F(FunctionTest, EventConnect, TestSize.Level1)
111 {
112 RegisterObjectType<TestType>();
113 auto& registry = META_NS::GetObjectRegistry();
114 auto p = ConstructProperty<int>(registry, "prop", 0);
115
116 auto object = registry.Create(Meta::ClassId::TestType);
117 ASSERT_TRUE(object);
118
119 auto f = interface_cast<IMetadata>(object)->GetFunctionByName("MyFunc");
120 ASSERT_TRUE(f);
121
122 auto f2 = interface_cast<IMetadata>(object)->GetFunctionByName("MyFunc2");
123 ASSERT_TRUE(f2);
124
125 EXPECT_TRUE(p->OnChanged()->IsCompatibleWith(f));
126 EXPECT_TRUE(p->OnChanged()->IsCompatibleWith(f2));
127
128 p->OnChanged()->AddHandler(f);
129 p->OnChanged()->AddHandler(f2);
130 p->SetValue(2);
131
132 auto* t = reinterpret_cast<TestType*>(object.get());
133 EXPECT_TRUE(t->myFuncCalled_);
134 EXPECT_TRUE(t->myFunc2Called_ == 0);
135
136 UnregisterObjectType<TestType>();
137 }
138
139 /**
140 * @tc.name: FunctionTest
141 * @tc.desc: test Connector function
142 * @tc.type: FUNC
143 * @tc.require: I7DMS1
144 */
HWTEST_F(FunctionTest, Connector, TestSize.Level1)145 HWTEST_F(FunctionTest, Connector, TestSize.Level1)
146 {
147 RegisterObjectType<TestType>();
148
149 auto& registry = META_NS::GetObjectRegistry();
150
151 auto object = registry.Create(Meta::ClassId::TestType);
152 ASSERT_TRUE(object);
153
154 ASSERT_TRUE(Connect(object, "OnTest", object, "MyFunc"));
155
156 auto* t = reinterpret_cast<TestType*>(object.get());
157
158 EXPECT_FALSE(t->myFuncCalled_);
159 Invoke<IOnChanged>(t->OnTest());
160 EXPECT_TRUE(t->myFuncCalled_);
161
162 t->myFuncCalled_ = false;
163 ASSERT_TRUE(Disconnect(object, "OnTest", object, "MyFunc"));
164 Invoke<IOnChanged>(t->OnTest());
165 EXPECT_FALSE(t->myFuncCalled_);
166
167 UnregisterObjectType<TestType>();
168 }
169
170
171 /**
172 * @tc.name: FunctionTest
173 * @tc.desc: test Forwarding function
174 * @tc.type: FUNC
175 * @tc.require: I7DMS1
176 */
HWTEST_F(FunctionTest, Forwarding, TestSize.Level1)177 HWTEST_F(FunctionTest, Forwarding, TestSize.Level1)
178 {
179 RegisterObjectType<TestType>();
180 auto& registry = META_NS::GetObjectRegistry();
181 auto object = registry.Create(Meta::ClassId::TestType);
182 ASSERT_TRUE(object);
183 auto func = CreateFunction(object, "MyFunc");
184 ASSERT_TRUE(func);
185
186 ASSERT_TRUE(CallMetaFunction<void>(func));
187 auto* t = reinterpret_cast<TestType*>(object.get());
188 EXPECT_TRUE(t->myFuncCalled_);
189
190 auto func2 = CreateFunction(object, "MyFunc2");
191 ASSERT_TRUE(func2);
192
193 EXPECT_EQ(CallMetaFunction<int>(func2, 2).value, 2);
194 EXPECT_EQ(t->myFunc2Called_, 2);
195
196 UnregisterObjectType<TestType>();
197 }
198
199 /**
200 * @tc.name: FunctionTest
201 * @tc.desc: test SerialiseConnector function
202 * @tc.type: FUNC
203 * @tc.require: I7DMS1
204 */
HWTEST_F(FunctionTest, SerialiseConnector, TestSize.Level1)205 HWTEST_F(FunctionTest, SerialiseConnector, TestSize.Level1)
206 {
207 RegisterObjectType<TestType>();
208 auto& registry = META_NS::GetObjectRegistry();
209 TestSerialiser ser;
210
211 {
212 auto object = registry.Create(Meta::ClassId::TestType);
213 ASSERT_TRUE(object);
214 ASSERT_TRUE(Connect(object, "OnTest", object, "MyFunc"));
215 ASSERT_TRUE(ser.Export(object));
216 }
217
218 ser.Dump("file://./connector.json");
219
220 auto object = ser.Import();
221 ASSERT_TRUE(object);
222
223 auto* t = reinterpret_cast<TestType*>(object.get());
224
225 EXPECT_FALSE(t->myFuncCalled_);
226 Invoke<IOnChanged>(t->OnTest());
227 EXPECT_TRUE(t->myFuncCalled_);
228
229 UnregisterObjectType<TestType>();
230 }
231
232
233 /**
234 * @tc.name: FunctionTest
235 * @tc.desc: test Serialization function
236 * @tc.type: FUNC
237 * @tc.require: I7DMS1
238 */
HWTEST_F(FunctionTest, Serialization, TestSize.Level1)239 HWTEST_F(FunctionTest, Serialization, TestSize.Level1)
240 {
241 RegisterObjectType<TestType>();
242 auto& registry = META_NS::GetObjectRegistry();
243 TestSerialiser ser;
244
245 {
246 auto object = registry.Create(Meta::ClassId::TestType);
247 ASSERT_TRUE(object);
248
249 auto func = CreateFunction(object, "MyFunc");
250 ASSERT_TRUE(func);
251
252 auto func2 = CreateFunction(object, "MyFunc2");
253 ASSERT_TRUE(func2);
254
255 auto p = ConstructArrayProperty<IFunction::Ptr>("functions");
256 p->AddValue(interface_pointer_cast<IFunction>(func));
257 p->AddValue(interface_pointer_cast<IFunction>(func2));
258
259 interface_cast<IMetadata>(object)->AddProperty(p);
260 ASSERT_TRUE(ser.Export(object));
261 }
262
263 ser.Dump("file://./func_ser.json");
264
265 auto obj = ser.Import();
266 ASSERT_TRUE(obj);
267
268 auto metad = interface_pointer_cast<IMetadata>(obj);
269 ASSERT_TRUE(metad);
270
271 auto p = ArrayProperty<IFunction::Ptr>(metad->GetPropertyByName("functions"));
272 ASSERT_TRUE(p);
273 ASSERT_EQ(p->GetSize(), 2);
274
275 ASSERT_TRUE(CallMetaFunction<void>(p->GetValueAt(0)));
276 EXPECT_EQ(CallMetaFunction<int>(p->GetValueAt(1), 2).value, 2);
277
278 auto* t = reinterpret_cast<TestType*>(obj.get());
279 EXPECT_TRUE(t->myFuncCalled_);
280 EXPECT_EQ(t->myFunc2Called_, 2);
281
282 UnregisterObjectType<TestType>();
283 }
284 META_END_NAMESPACE()