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