18bf80f4bSopenharmony_ci/* 28bf80f4bSopenharmony_ci * Copyright (C) 2024 Huawei Device Co., Ltd. 38bf80f4bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 48bf80f4bSopenharmony_ci * you may not use this file except in compliance with the License. 58bf80f4bSopenharmony_ci * You may obtain a copy of the License at 68bf80f4bSopenharmony_ci * 78bf80f4bSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 88bf80f4bSopenharmony_ci * 98bf80f4bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 108bf80f4bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 118bf80f4bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 128bf80f4bSopenharmony_ci * See the License for the specific language governing permissions and 138bf80f4bSopenharmony_ci * limitations under the License. 148bf80f4bSopenharmony_ci */ 158bf80f4bSopenharmony_ci 168bf80f4bSopenharmony_ci#include <gtest/gtest.h> 178bf80f4bSopenharmony_ci 188bf80f4bSopenharmony_ci#include <meta/api/animation/animation.h> 198bf80f4bSopenharmony_ci#include <meta/api/connector.h> 208bf80f4bSopenharmony_ci#include <meta/api/function.h> 218bf80f4bSopenharmony_ci#include <meta/ext/any_builder.h> 228bf80f4bSopenharmony_ci#include <meta/ext/attachment/attachment.h> 238bf80f4bSopenharmony_ci#include <meta/ext/object.h> 248bf80f4bSopenharmony_ci#include <meta/interface/intf_connector.h> 258bf80f4bSopenharmony_ci#include <meta/interface/property/property.h> 268bf80f4bSopenharmony_ci 278bf80f4bSopenharmony_ci#include "src/serialisation_utils.h" 288bf80f4bSopenharmony_ci#include "src/testing_objects.h" 298bf80f4bSopenharmony_ci#include "src/util.h" 308bf80f4bSopenharmony_ci#include "src/test_runner.h" 318bf80f4bSopenharmony_ci 328bf80f4bSopenharmony_ciusing namespace testing::ext; 338bf80f4bSopenharmony_ci 348bf80f4bSopenharmony_ciMETA_BEGIN_NAMESPACE() 358bf80f4bSopenharmony_ci 368bf80f4bSopenharmony_ciclass FunctionTest : public testing::Test { 378bf80f4bSopenharmony_cipublic: 388bf80f4bSopenharmony_ci static void SetUpTestSuite() 398bf80f4bSopenharmony_ci { 408bf80f4bSopenharmony_ci SetTest(); 418bf80f4bSopenharmony_ci UnregisterTestTypes(); // Unregister TestType 428bf80f4bSopenharmony_ci } 438bf80f4bSopenharmony_ci static void TearDownTestSuite() 448bf80f4bSopenharmony_ci { 458bf80f4bSopenharmony_ci ResetTest(); 468bf80f4bSopenharmony_ci } 478bf80f4bSopenharmony_ci void SetUp() override {} 488bf80f4bSopenharmony_ci void TearDown() override {} 498bf80f4bSopenharmony_ci}; 508bf80f4bSopenharmony_ci 518bf80f4bSopenharmony_cinamespace { 528bf80f4bSopenharmony_ci 538bf80f4bSopenharmony_ciclass IFuncTestType : public CORE_NS::IInterface { 548bf80f4bSopenharmony_ci META_INTERFACE(CORE_NS::IInterface, IFuncTestType, "5c79a9ab-f673-49b5-b18a-f16d0db50c96") 558bf80f4bSopenharmony_cipublic: 568bf80f4bSopenharmony_ci virtual void MyFunc() = 0; 578bf80f4bSopenharmony_ci virtual int MyFunc2(int) = 0; 588bf80f4bSopenharmony_ci virtual void OnStart() = 0; 598bf80f4bSopenharmony_ci 608bf80f4bSopenharmony_ci META_EVENT(IOnChanged, OnTest) 618bf80f4bSopenharmony_ci META_PROPERTY(int, TestProperty) 628bf80f4bSopenharmony_ci}; 638bf80f4bSopenharmony_ci 648bf80f4bSopenharmony_ciMETA_REGISTER_CLASS(TestType, "abcd6e8b-36d4-44e4-802d-b2d80e50c612", META_NS::ObjectCategoryBits::APPLICATION) 658bf80f4bSopenharmony_ci 668bf80f4bSopenharmony_ciclass TestType final : public META_NS::ObjectFwd< 678bf80f4bSopenharmony_ci TestType, Meta::ClassId::TestType, META_NS::ClassId::Object, IFuncTestType> { 688bf80f4bSopenharmony_ci using Super = META_NS::ObjectFwd<TestType, Meta::ClassId::TestType, META_NS::ClassId::Object, IFuncTestType>; 698bf80f4bSopenharmony_ci using Super::Super; 708bf80f4bSopenharmony_ci 718bf80f4bSopenharmony_cipublic: 728bf80f4bSopenharmony_ci META_IMPLEMENT_FUNCTION(TestType, MyFunc); 738bf80f4bSopenharmony_ci META_IMPLEMENT_FUNCTION(TestType, MyFunc2, "value"); 748bf80f4bSopenharmony_ci META_IMPLEMENT_FUNCTION(TestType, OnStart, "state"); 758bf80f4bSopenharmony_ci 768bf80f4bSopenharmony_ci META_IMPLEMENT_PROPERTY(int, TestProperty); 778bf80f4bSopenharmony_ci 788bf80f4bSopenharmony_ci void MyFunc() override 798bf80f4bSopenharmony_ci { 808bf80f4bSopenharmony_ci myFuncCalled_ = true; 818bf80f4bSopenharmony_ci } 828bf80f4bSopenharmony_ci 838bf80f4bSopenharmony_ci int MyFunc2(int v) override 848bf80f4bSopenharmony_ci { 858bf80f4bSopenharmony_ci myFunc2Called_ = v; 868bf80f4bSopenharmony_ci return v; 878bf80f4bSopenharmony_ci } 888bf80f4bSopenharmony_ci 898bf80f4bSopenharmony_ci void OnStart() override 908bf80f4bSopenharmony_ci { 918bf80f4bSopenharmony_ci onStartCalled_ = true; 928bf80f4bSopenharmony_ci } 938bf80f4bSopenharmony_ci META_IMPLEMENT_EVENT(IOnChanged, OnTest); 948bf80f4bSopenharmony_ci 958bf80f4bSopenharmony_cipublic: 968bf80f4bSopenharmony_ci bool myFuncCalled_ {}; 978bf80f4bSopenharmony_ci int myFunc2Called_ {}; 988bf80f4bSopenharmony_ci bool onStartCalled_ {}; 998bf80f4bSopenharmony_ci}; 1008bf80f4bSopenharmony_ci 1018bf80f4bSopenharmony_ci} // namespace 1028bf80f4bSopenharmony_ci 1038bf80f4bSopenharmony_ci 1048bf80f4bSopenharmony_ci/** 1058bf80f4bSopenharmony_ci * @tc.name: FunctionTest 1068bf80f4bSopenharmony_ci * @tc.desc: test EventConnect function 1078bf80f4bSopenharmony_ci * @tc.type: FUNC 1088bf80f4bSopenharmony_ci * @tc.require: I7DMS1 1098bf80f4bSopenharmony_ci */ 1108bf80f4bSopenharmony_ciHWTEST_F(FunctionTest, EventConnect, TestSize.Level1) 1118bf80f4bSopenharmony_ci{ 1128bf80f4bSopenharmony_ci RegisterObjectType<TestType>(); 1138bf80f4bSopenharmony_ci auto& registry = META_NS::GetObjectRegistry(); 1148bf80f4bSopenharmony_ci auto p = ConstructProperty<int>(registry, "prop", 0); 1158bf80f4bSopenharmony_ci 1168bf80f4bSopenharmony_ci auto object = registry.Create(Meta::ClassId::TestType); 1178bf80f4bSopenharmony_ci ASSERT_TRUE(object); 1188bf80f4bSopenharmony_ci 1198bf80f4bSopenharmony_ci auto f = interface_cast<IMetadata>(object)->GetFunctionByName("MyFunc"); 1208bf80f4bSopenharmony_ci ASSERT_TRUE(f); 1218bf80f4bSopenharmony_ci 1228bf80f4bSopenharmony_ci auto f2 = interface_cast<IMetadata>(object)->GetFunctionByName("MyFunc2"); 1238bf80f4bSopenharmony_ci ASSERT_TRUE(f2); 1248bf80f4bSopenharmony_ci 1258bf80f4bSopenharmony_ci EXPECT_TRUE(p->OnChanged()->IsCompatibleWith(f)); 1268bf80f4bSopenharmony_ci EXPECT_TRUE(p->OnChanged()->IsCompatibleWith(f2)); 1278bf80f4bSopenharmony_ci 1288bf80f4bSopenharmony_ci p->OnChanged()->AddHandler(f); 1298bf80f4bSopenharmony_ci p->OnChanged()->AddHandler(f2); 1308bf80f4bSopenharmony_ci p->SetValue(2); 1318bf80f4bSopenharmony_ci 1328bf80f4bSopenharmony_ci auto* t = reinterpret_cast<TestType*>(object.get()); 1338bf80f4bSopenharmony_ci EXPECT_TRUE(t->myFuncCalled_); 1348bf80f4bSopenharmony_ci EXPECT_TRUE(t->myFunc2Called_ == 0); 1358bf80f4bSopenharmony_ci 1368bf80f4bSopenharmony_ci UnregisterObjectType<TestType>(); 1378bf80f4bSopenharmony_ci} 1388bf80f4bSopenharmony_ci 1398bf80f4bSopenharmony_ci/** 1408bf80f4bSopenharmony_ci * @tc.name: FunctionTest 1418bf80f4bSopenharmony_ci * @tc.desc: test Connector function 1428bf80f4bSopenharmony_ci * @tc.type: FUNC 1438bf80f4bSopenharmony_ci * @tc.require: I7DMS1 1448bf80f4bSopenharmony_ci */ 1458bf80f4bSopenharmony_ciHWTEST_F(FunctionTest, Connector, TestSize.Level1) 1468bf80f4bSopenharmony_ci{ 1478bf80f4bSopenharmony_ci RegisterObjectType<TestType>(); 1488bf80f4bSopenharmony_ci 1498bf80f4bSopenharmony_ci auto& registry = META_NS::GetObjectRegistry(); 1508bf80f4bSopenharmony_ci 1518bf80f4bSopenharmony_ci auto object = registry.Create(Meta::ClassId::TestType); 1528bf80f4bSopenharmony_ci ASSERT_TRUE(object); 1538bf80f4bSopenharmony_ci 1548bf80f4bSopenharmony_ci ASSERT_TRUE(Connect(object, "OnTest", object, "MyFunc")); 1558bf80f4bSopenharmony_ci 1568bf80f4bSopenharmony_ci auto* t = reinterpret_cast<TestType*>(object.get()); 1578bf80f4bSopenharmony_ci 1588bf80f4bSopenharmony_ci EXPECT_FALSE(t->myFuncCalled_); 1598bf80f4bSopenharmony_ci Invoke<IOnChanged>(t->OnTest()); 1608bf80f4bSopenharmony_ci EXPECT_TRUE(t->myFuncCalled_); 1618bf80f4bSopenharmony_ci 1628bf80f4bSopenharmony_ci t->myFuncCalled_ = false; 1638bf80f4bSopenharmony_ci ASSERT_TRUE(Disconnect(object, "OnTest", object, "MyFunc")); 1648bf80f4bSopenharmony_ci Invoke<IOnChanged>(t->OnTest()); 1658bf80f4bSopenharmony_ci EXPECT_FALSE(t->myFuncCalled_); 1668bf80f4bSopenharmony_ci 1678bf80f4bSopenharmony_ci UnregisterObjectType<TestType>(); 1688bf80f4bSopenharmony_ci} 1698bf80f4bSopenharmony_ci 1708bf80f4bSopenharmony_ci 1718bf80f4bSopenharmony_ci/** 1728bf80f4bSopenharmony_ci * @tc.name: FunctionTest 1738bf80f4bSopenharmony_ci * @tc.desc: test Forwarding function 1748bf80f4bSopenharmony_ci * @tc.type: FUNC 1758bf80f4bSopenharmony_ci * @tc.require: I7DMS1 1768bf80f4bSopenharmony_ci */ 1778bf80f4bSopenharmony_ciHWTEST_F(FunctionTest, Forwarding, TestSize.Level1) 1788bf80f4bSopenharmony_ci{ 1798bf80f4bSopenharmony_ci RegisterObjectType<TestType>(); 1808bf80f4bSopenharmony_ci auto& registry = META_NS::GetObjectRegistry(); 1818bf80f4bSopenharmony_ci auto object = registry.Create(Meta::ClassId::TestType); 1828bf80f4bSopenharmony_ci ASSERT_TRUE(object); 1838bf80f4bSopenharmony_ci auto func = CreateFunction(object, "MyFunc"); 1848bf80f4bSopenharmony_ci ASSERT_TRUE(func); 1858bf80f4bSopenharmony_ci 1868bf80f4bSopenharmony_ci ASSERT_TRUE(CallMetaFunction<void>(func)); 1878bf80f4bSopenharmony_ci auto* t = reinterpret_cast<TestType*>(object.get()); 1888bf80f4bSopenharmony_ci EXPECT_TRUE(t->myFuncCalled_); 1898bf80f4bSopenharmony_ci 1908bf80f4bSopenharmony_ci auto func2 = CreateFunction(object, "MyFunc2"); 1918bf80f4bSopenharmony_ci ASSERT_TRUE(func2); 1928bf80f4bSopenharmony_ci 1938bf80f4bSopenharmony_ci EXPECT_EQ(CallMetaFunction<int>(func2, 2).value, 2); 1948bf80f4bSopenharmony_ci EXPECT_EQ(t->myFunc2Called_, 2); 1958bf80f4bSopenharmony_ci 1968bf80f4bSopenharmony_ci UnregisterObjectType<TestType>(); 1978bf80f4bSopenharmony_ci} 1988bf80f4bSopenharmony_ci 1998bf80f4bSopenharmony_ci/** 2008bf80f4bSopenharmony_ci * @tc.name: FunctionTest 2018bf80f4bSopenharmony_ci * @tc.desc: test SerialiseConnector function 2028bf80f4bSopenharmony_ci * @tc.type: FUNC 2038bf80f4bSopenharmony_ci * @tc.require: I7DMS1 2048bf80f4bSopenharmony_ci */ 2058bf80f4bSopenharmony_ciHWTEST_F(FunctionTest, SerialiseConnector, TestSize.Level1) 2068bf80f4bSopenharmony_ci{ 2078bf80f4bSopenharmony_ci RegisterObjectType<TestType>(); 2088bf80f4bSopenharmony_ci auto& registry = META_NS::GetObjectRegistry(); 2098bf80f4bSopenharmony_ci TestSerialiser ser; 2108bf80f4bSopenharmony_ci 2118bf80f4bSopenharmony_ci { 2128bf80f4bSopenharmony_ci auto object = registry.Create(Meta::ClassId::TestType); 2138bf80f4bSopenharmony_ci ASSERT_TRUE(object); 2148bf80f4bSopenharmony_ci ASSERT_TRUE(Connect(object, "OnTest", object, "MyFunc")); 2158bf80f4bSopenharmony_ci ASSERT_TRUE(ser.Export(object)); 2168bf80f4bSopenharmony_ci } 2178bf80f4bSopenharmony_ci 2188bf80f4bSopenharmony_ci ser.Dump("file://./connector.json"); 2198bf80f4bSopenharmony_ci 2208bf80f4bSopenharmony_ci auto object = ser.Import(); 2218bf80f4bSopenharmony_ci ASSERT_TRUE(object); 2228bf80f4bSopenharmony_ci 2238bf80f4bSopenharmony_ci auto* t = reinterpret_cast<TestType*>(object.get()); 2248bf80f4bSopenharmony_ci 2258bf80f4bSopenharmony_ci EXPECT_FALSE(t->myFuncCalled_); 2268bf80f4bSopenharmony_ci Invoke<IOnChanged>(t->OnTest()); 2278bf80f4bSopenharmony_ci EXPECT_TRUE(t->myFuncCalled_); 2288bf80f4bSopenharmony_ci 2298bf80f4bSopenharmony_ci UnregisterObjectType<TestType>(); 2308bf80f4bSopenharmony_ci} 2318bf80f4bSopenharmony_ci 2328bf80f4bSopenharmony_ci 2338bf80f4bSopenharmony_ci/** 2348bf80f4bSopenharmony_ci * @tc.name: FunctionTest 2358bf80f4bSopenharmony_ci * @tc.desc: test Serialization function 2368bf80f4bSopenharmony_ci * @tc.type: FUNC 2378bf80f4bSopenharmony_ci * @tc.require: I7DMS1 2388bf80f4bSopenharmony_ci */ 2398bf80f4bSopenharmony_ciHWTEST_F(FunctionTest, Serialization, TestSize.Level1) 2408bf80f4bSopenharmony_ci{ 2418bf80f4bSopenharmony_ci RegisterObjectType<TestType>(); 2428bf80f4bSopenharmony_ci auto& registry = META_NS::GetObjectRegistry(); 2438bf80f4bSopenharmony_ci TestSerialiser ser; 2448bf80f4bSopenharmony_ci 2458bf80f4bSopenharmony_ci { 2468bf80f4bSopenharmony_ci auto object = registry.Create(Meta::ClassId::TestType); 2478bf80f4bSopenharmony_ci ASSERT_TRUE(object); 2488bf80f4bSopenharmony_ci 2498bf80f4bSopenharmony_ci auto func = CreateFunction(object, "MyFunc"); 2508bf80f4bSopenharmony_ci ASSERT_TRUE(func); 2518bf80f4bSopenharmony_ci 2528bf80f4bSopenharmony_ci auto func2 = CreateFunction(object, "MyFunc2"); 2538bf80f4bSopenharmony_ci ASSERT_TRUE(func2); 2548bf80f4bSopenharmony_ci 2558bf80f4bSopenharmony_ci auto p = ConstructArrayProperty<IFunction::Ptr>("functions"); 2568bf80f4bSopenharmony_ci p->AddValue(interface_pointer_cast<IFunction>(func)); 2578bf80f4bSopenharmony_ci p->AddValue(interface_pointer_cast<IFunction>(func2)); 2588bf80f4bSopenharmony_ci 2598bf80f4bSopenharmony_ci interface_cast<IMetadata>(object)->AddProperty(p); 2608bf80f4bSopenharmony_ci ASSERT_TRUE(ser.Export(object)); 2618bf80f4bSopenharmony_ci } 2628bf80f4bSopenharmony_ci 2638bf80f4bSopenharmony_ci ser.Dump("file://./func_ser.json"); 2648bf80f4bSopenharmony_ci 2658bf80f4bSopenharmony_ci auto obj = ser.Import(); 2668bf80f4bSopenharmony_ci ASSERT_TRUE(obj); 2678bf80f4bSopenharmony_ci 2688bf80f4bSopenharmony_ci auto metad = interface_pointer_cast<IMetadata>(obj); 2698bf80f4bSopenharmony_ci ASSERT_TRUE(metad); 2708bf80f4bSopenharmony_ci 2718bf80f4bSopenharmony_ci auto p = ArrayProperty<IFunction::Ptr>(metad->GetPropertyByName("functions")); 2728bf80f4bSopenharmony_ci ASSERT_TRUE(p); 2738bf80f4bSopenharmony_ci ASSERT_EQ(p->GetSize(), 2); 2748bf80f4bSopenharmony_ci 2758bf80f4bSopenharmony_ci ASSERT_TRUE(CallMetaFunction<void>(p->GetValueAt(0))); 2768bf80f4bSopenharmony_ci EXPECT_EQ(CallMetaFunction<int>(p->GetValueAt(1), 2).value, 2); 2778bf80f4bSopenharmony_ci 2788bf80f4bSopenharmony_ci auto* t = reinterpret_cast<TestType*>(obj.get()); 2798bf80f4bSopenharmony_ci EXPECT_TRUE(t->myFuncCalled_); 2808bf80f4bSopenharmony_ci EXPECT_EQ(t->myFunc2Called_, 2); 2818bf80f4bSopenharmony_ci 2828bf80f4bSopenharmony_ci UnregisterObjectType<TestType>(); 2838bf80f4bSopenharmony_ci} 2848bf80f4bSopenharmony_ciMETA_END_NAMESPACE()