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/function.h> 198bf80f4bSopenharmony_ci#include <meta/interface/intf_metadata.h> 208bf80f4bSopenharmony_ci 218bf80f4bSopenharmony_ci#include "src/test_runner.h"s 228bf80f4bSopenharmony_ci#include "src/testing_objects.h" 238bf80f4bSopenharmony_ci#include "src/util.h" 248bf80f4bSopenharmony_ci 258bf80f4bSopenharmony_ciusing namespace testing; 268bf80f4bSopenharmony_ciusing namespace testing::ext; 278bf80f4bSopenharmony_ci 288bf80f4bSopenharmony_cinamespace { 298bf80f4bSopenharmony_ci const int ZERO = 0; // test value for Set/Get function 308bf80f4bSopenharmony_ci const int ONE = 1; // test value for Set/Get function 318bf80f4bSopenharmony_ci const int TWO = 2; // test value for Set/Get function 328bf80f4bSopenharmony_ci const int THREE = 3; // test value for Set/Get function 338bf80f4bSopenharmony_ci} 348bf80f4bSopenharmony_ci 358bf80f4bSopenharmony_ciMETA_BEGIN_NAMESPACE() 368bf80f4bSopenharmony_ci 378bf80f4bSopenharmony_ciclass IntfMetadataTest : public testing::Test { 388bf80f4bSopenharmony_cipublic: 398bf80f4bSopenharmony_ci static void SetUpTestSuite() 408bf80f4bSopenharmony_ci { 418bf80f4bSopenharmony_ci SetTest(); 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_ci/** 528bf80f4bSopenharmony_ci * @tc.name: IntfMetadataTest 538bf80f4bSopenharmony_ci * @tc.desc: test Functions function 548bf80f4bSopenharmony_ci * @tc.type: FUNC 558bf80f4bSopenharmony_ci * @tc.require: I7DMS1 568bf80f4bSopenharmony_ci */ 578bf80f4bSopenharmony_ciHWTEST_F(IntfMetadataTest, Functions, TestSize.Level1) 588bf80f4bSopenharmony_ci{ 598bf80f4bSopenharmony_ci auto p = CreateTestType(); 608bf80f4bSopenharmony_ci ITestType::ConstPtr cp = p; 618bf80f4bSopenharmony_ci 628bf80f4bSopenharmony_ci auto m = interface_cast<IMetadata>(p); 638bf80f4bSopenharmony_ci ASSERT_TRUE(m); 648bf80f4bSopenharmony_ci 658bf80f4bSopenharmony_ci { 668bf80f4bSopenharmony_ci auto f = m->GetFunctionByName("MyTestFunc"); 678bf80f4bSopenharmony_ci ASSERT_TRUE(f); 688bf80f4bSopenharmony_ci EXPECT_FALSE(CallMetaFunction<float>(f, 1)); 698bf80f4bSopenharmony_ci EXPECT_FALSE(CallMetaFunction<int>(f, 0.0)); 708bf80f4bSopenharmony_ci EXPECT_FALSE(CallMetaFunction<int>(f, 1, 1)); 718bf80f4bSopenharmony_ci auto res = CallMetaFunction<int>(f, 1); 728bf80f4bSopenharmony_ci ASSERT_TRUE(res); 738bf80f4bSopenharmony_ci EXPECT_EQ(res.value, 1); 748bf80f4bSopenharmony_ci EXPECT_EQ(f->GetDestination(), interface_pointer_cast<IObject>(p)); 758bf80f4bSopenharmony_ci } 768bf80f4bSopenharmony_ci { 778bf80f4bSopenharmony_ci auto f = m->GetFunctionByName("MyConstTestFunc"); 788bf80f4bSopenharmony_ci ASSERT_TRUE(f); 798bf80f4bSopenharmony_ci auto res = CallMetaFunction<int>(f, 2); 808bf80f4bSopenharmony_ci ASSERT_TRUE(res); 818bf80f4bSopenharmony_ci EXPECT_EQ(res.value, 2); 828bf80f4bSopenharmony_ci } 838bf80f4bSopenharmony_ci { 848bf80f4bSopenharmony_ci auto f = m->GetFunctionByName("NormalMember"); 858bf80f4bSopenharmony_ci ASSERT_TRUE(f); 868bf80f4bSopenharmony_ci auto res = CallMetaFunction<void>(f); 878bf80f4bSopenharmony_ci ASSERT_TRUE(res); 888bf80f4bSopenharmony_ci } 898bf80f4bSopenharmony_ci { 908bf80f4bSopenharmony_ci auto f = m->GetFunctionByName("OtherNormalMember"); 918bf80f4bSopenharmony_ci ASSERT_TRUE(f); 928bf80f4bSopenharmony_ci 938bf80f4bSopenharmony_ci auto c = f->CreateCallContext(); 948bf80f4bSopenharmony_ci ASSERT_TRUE(c); 958bf80f4bSopenharmony_ci ASSERT_TRUE(Get<int>(c, "value")); 968bf80f4bSopenharmony_ci ASSERT_TRUE(Get<int>(c, "some")); 978bf80f4bSopenharmony_ci 988bf80f4bSopenharmony_ci auto res = CallMetaFunction<int>(f, 2, 3); 998bf80f4bSopenharmony_ci ASSERT_TRUE(res); 1008bf80f4bSopenharmony_ci EXPECT_EQ(res.value, 5); 1018bf80f4bSopenharmony_ci } 1028bf80f4bSopenharmony_ci} 1038bf80f4bSopenharmony_ci 1048bf80f4bSopenharmony_ci/** 1058bf80f4bSopenharmony_ci * @tc.name: IntfMetadataTest 1068bf80f4bSopenharmony_ci * @tc.desc: test Events function 1078bf80f4bSopenharmony_ci * @tc.type: FUNC 1088bf80f4bSopenharmony_ci * @tc.require: I7DMS1 1098bf80f4bSopenharmony_ci */ 1108bf80f4bSopenharmony_ciHWTEST_F(IntfMetadataTest, Events, TestSize.Level1) 1118bf80f4bSopenharmony_ci{ 1128bf80f4bSopenharmony_ci auto p = CreateTestType(); 1138bf80f4bSopenharmony_ci auto m = interface_cast<IMetadata>(p); 1148bf80f4bSopenharmony_ci ASSERT_TRUE(m); 1158bf80f4bSopenharmony_ci 1168bf80f4bSopenharmony_ci auto e = m->GetEventByName("OnTest"); 1178bf80f4bSopenharmony_ci ASSERT_TRUE(e); 1188bf80f4bSopenharmony_ci} 1198bf80f4bSopenharmony_ci 1208bf80f4bSopenharmony_citemplate<typename Set, typename Get> 1218bf80f4bSopenharmony_civoid TestGetSetValue(Set sp, Get gp) 1228bf80f4bSopenharmony_ci{ 1238bf80f4bSopenharmony_ci ASSERT_TRUE(SetValue(sp, "First", ONE)); 1248bf80f4bSopenharmony_ci ASSERT_EQ(GetValue(gp, "First", TWO), ONE); 1258bf80f4bSopenharmony_ci 1268bf80f4bSopenharmony_ci ASSERT_TRUE(SetValue<int>(sp, "First", TWO)); 1278bf80f4bSopenharmony_ci ASSERT_EQ(GetValue<int>(gp, "First"), TWO); 1288bf80f4bSopenharmony_ci ASSERT_EQ(GetValue<int>(gp, "First", ONE), TWO); 1298bf80f4bSopenharmony_ci 1308bf80f4bSopenharmony_ci ASSERT_FALSE(SetValue<float>(sp, "First", TWO)); 1318bf80f4bSopenharmony_ci ASSERT_EQ(GetValue<float>(gp, "First"), ZERO); 1328bf80f4bSopenharmony_ci ASSERT_EQ(GetValue<float>(gp, "First", ONE), ONE); 1338bf80f4bSopenharmony_ci 1348bf80f4bSopenharmony_ci ASSERT_FALSE(SetValue(Set {}, "First", THREE)); 1358bf80f4bSopenharmony_ci Get nil {}; 1368bf80f4bSopenharmony_ci ASSERT_EQ(GetValue(nil, "First", THREE), THREE); 1378bf80f4bSopenharmony_ci ASSERT_EQ(GetValue<int>(nil, "First", THREE), THREE); 1388bf80f4bSopenharmony_ci} 1398bf80f4bSopenharmony_ci 1408bf80f4bSopenharmony_citemplate<typename Type> 1418bf80f4bSopenharmony_civoid TestGetSetValue(BASE_NS::shared_ptr<Type> p) 1428bf80f4bSopenharmony_ci{ 1438bf80f4bSopenharmony_ci BASE_NS::shared_ptr<const Type> c_p = p; 1448bf80f4bSopenharmony_ci BASE_NS::weak_ptr<Type> w_p = p; 1458bf80f4bSopenharmony_ci BASE_NS::weak_ptr<const Type> cw_p = p; 1468bf80f4bSopenharmony_ci 1478bf80f4bSopenharmony_ci TestGetSetValue(p, p); 1488bf80f4bSopenharmony_ci TestGetSetValue(p, c_p); 1498bf80f4bSopenharmony_ci TestGetSetValue(p, w_p); 1508bf80f4bSopenharmony_ci TestGetSetValue(p, cw_p); 1518bf80f4bSopenharmony_ci TestGetSetValue(w_p, p); 1528bf80f4bSopenharmony_ci} 1538bf80f4bSopenharmony_ci 1548bf80f4bSopenharmony_ci/** 1558bf80f4bSopenharmony_ci * @tc.name: IntfMetadataTest 1568bf80f4bSopenharmony_ci * @tc.desc: test GetSetValue function 1578bf80f4bSopenharmony_ci * @tc.type: FUNC 1588bf80f4bSopenharmony_ci * @tc.require: I7DMS1 1598bf80f4bSopenharmony_ci */ 1608bf80f4bSopenharmony_ciHWTEST_F(IntfMetadataTest, GetSetValue, TestSize.Level1) 1618bf80f4bSopenharmony_ci{ 1628bf80f4bSopenharmony_ci auto p = CreateTestType(); 1638bf80f4bSopenharmony_ci 1648bf80f4bSopenharmony_ci TestGetSetValue<ITestType>(p); 1658bf80f4bSopenharmony_ci TestGetSetValue<IMetadata>(interface_pointer_cast<IMetadata>(p)); 1668bf80f4bSopenharmony_ci TestGetSetValue<CORE_NS::IInterface>(interface_pointer_cast<CORE_NS::IInterface>(p)); 1678bf80f4bSopenharmony_ci} 1688bf80f4bSopenharmony_ci 1698bf80f4bSopenharmony_citemplate<typename Type> 1708bf80f4bSopenharmony_cistatic auto* GetMeta(const BASE_NS::vector<Type>& vec, const BASE_NS::string& name) 1718bf80f4bSopenharmony_ci{ 1728bf80f4bSopenharmony_ci for (size_t i = 0; i != vec.size(); ++i) { 1738bf80f4bSopenharmony_ci if (vec[i].name == name) { 1748bf80f4bSopenharmony_ci return &vec[i]; 1758bf80f4bSopenharmony_ci } 1768bf80f4bSopenharmony_ci } 1778bf80f4bSopenharmony_ci return decltype(&vec[0])(nullptr); 1788bf80f4bSopenharmony_ci} 1798bf80f4bSopenharmony_ci 1808bf80f4bSopenharmony_ci/** 1818bf80f4bSopenharmony_ci * @tc.name: IntfMetadataTest 1828bf80f4bSopenharmony_ci * @tc.desc: test StaticMetadata function 1838bf80f4bSopenharmony_ci * @tc.type: FUNC 1848bf80f4bSopenharmony_ci * @tc.require: I7DMS1 1858bf80f4bSopenharmony_ci */ 1868bf80f4bSopenharmony_ciHWTEST_F(IntfMetadataTest, StaticMetadata, TestSize.Level1) 1878bf80f4bSopenharmony_ci{ 1888bf80f4bSopenharmony_ci EXPECT_EQ(META_NS::InterfaceId::ITestType.ReadableName(), "This is test type"); 1898bf80f4bSopenharmony_ci EXPECT_EQ(META_NS::InterfaceId::ITestContainer.ReadableName(), "ITestContainer"); 1908bf80f4bSopenharmony_ci 1918bf80f4bSopenharmony_ci auto fac = GetObjectRegistry().GetObjectFactory(ClassId::TestType); 1928bf80f4bSopenharmony_ci ASSERT_TRUE(fac); 1938bf80f4bSopenharmony_ci auto sm = fac->GetClassStaticMetadata(); 1948bf80f4bSopenharmony_ci 1958bf80f4bSopenharmony_ci auto p = GetMeta(sm.properties, "First"); 1968bf80f4bSopenharmony_ci ASSERT_TRUE(p); 1978bf80f4bSopenharmony_ci ASSERT_TRUE(p->interfaceInfo.IsValid()); 1988bf80f4bSopenharmony_ci EXPECT_EQ(p->interfaceInfo.ReadableName(), "This is test type"); 1998bf80f4bSopenharmony_ci 2008bf80f4bSopenharmony_ci auto e = GetMeta(sm.events, "OnTest"); 2018bf80f4bSopenharmony_ci ASSERT_TRUE(e); 2028bf80f4bSopenharmony_ci ASSERT_TRUE(e->interfaceInfo.IsValid()); 2038bf80f4bSopenharmony_ci EXPECT_EQ(e->interfaceInfo.ReadableName(), "This is test type"); 2048bf80f4bSopenharmony_ci 2058bf80f4bSopenharmony_ci auto f = GetMeta(sm.functions, "NormalMember"); 2068bf80f4bSopenharmony_ci ASSERT_TRUE(f); 2078bf80f4bSopenharmony_ci ASSERT_TRUE(f->interfaceInfo.IsValid()); 2088bf80f4bSopenharmony_ci EXPECT_EQ(f->interfaceInfo.ReadableName(), "This is test type"); 2098bf80f4bSopenharmony_ci} 2108bf80f4bSopenharmony_ciMETA_END_NAMESPACE()