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 #include <src/testing_objects.h> 18 19 #include <gtest/gtest.h> 20 21 #include <meta/api/property/custom_value.h> 22 #include <meta/ext/implementation_macros.h> 23 #include <meta/ext/object.h> 24 25 using namespace testing; 26 using namespace testing::ext; 27 28 META_BEGIN_NAMESPACE() 29 30 class IGetSetTestInterface : public CORE_NS::IInterface { 31 META_INTERFACE(CORE_NS::IInterface, IGetSetTestInterface, "a952bbc2-c352-4134-bb09-95be0b983c0b") 32 public: 33 META_PROPERTY(int, Value) 34 35 virtual int GetCount() const = 0; 36 virtual int SetCount() const = 0; 37 }; 38 39 META_REGISTER_CLASS(GetSetTestType, "35ed312a-6195-4f0d-959f-2e21a47584d6", ObjectCategory::NO_CATEGORY) 40 41 class GetSetTestType : public IntroduceInterfaces<MetaObject, IGetSetTestInterface> { 42 META_OBJECT(GetSetTestType, ClassId::GetSetTestType, IntroduceInterfaces) 43 public: 44 META_BEGIN_STATIC_DATA() META_STATIC_PROPERTY_DATA(IGetSetTestInterface,int,Value)45 META_STATIC_PROPERTY_DATA(IGetSetTestInterface, int, Value) 46 META_END_STATIC_DATA() 47 48 META_IMPLEMENT_PROPERTY(int, Value) 49 50 int GetF() const 51 { 52 ++getCalled_; 53 return value_; 54 } SetF(const int & value)55 bool SetF(const int& value) 56 { 57 ++setCalled_; 58 value_ = value; 59 return true; 60 } 61 Build(const IMetadata::Ptr & d)62 bool Build(const IMetadata::Ptr& d) override 63 { 64 bool res = Super::Build(d); 65 if (res) { 66 AddGetSetCustomValue(Value(), this, &GetSetTestType::GetF, &GetSetTestType::SetF); 67 } 68 return res; 69 } 70 GetCount() const71 int GetCount() const override 72 { 73 return getCalled_; 74 } SetCount() const75 int SetCount() const override 76 { 77 return setCalled_; 78 } 79 80 private: 81 int value_ {}; 82 mutable int getCalled_ {}; 83 mutable int setCalled_ {}; 84 }; 85 86 class CustomValueTest : public testing::Test { 87 public: SetUpTestSuite()88 static void SetUpTestSuite() 89 { 90 SetTest(); 91 } TearDownTestSuite()92 static void TearDownTestSuite() 93 { 94 TearDownTest(); 95 } SetUp()96 void SetUp() {} TearDown()97 void TearDown() {} 98 }; 99 100 /** 101 * @tc.name: GetSet 102 * @tc.desc: test Get Set 103 * @tc.type:FUNC 104 * @tc.require: 105 */ 106 HWTEST_F(CustomValueTest, GetSet, TestSize.Level1) 107 { 108 auto adapter = std::make_shared<OHOS::Render3D::SceneAdapter>(); 109 adapter->LoadPluginsAndInit(); 110 111 RegisterObjectType<GetSetTestType>(); 112 auto obj = GetObjectRegistry().Create<IGetSetTestInterface>(ClassId::GetSetTestType); 113 114 EXPECT_EQ(obj->Value()->GetValue(), 0); 115 EXPECT_EQ(obj->Value()->GetValue(), 0); 116 obj->Value()->SetValue(2); 117 EXPECT_EQ(obj->Value()->GetValue(), 2); 118 119 EXPECT_EQ(obj->GetCount(), 2); 120 EXPECT_EQ(obj->SetCount(), 1); 121 122 UnregisterObjectType<GetSetTestType>(); 123 } 124 125 META_END_NAMESPACE() 126