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 #include <gmock/gmock-matchers.h> 16 #include <gtest/gtest.h> 17 18 #include <meta/ext/implementation_macros.h> 19 #include <meta/ext/minimal_object.h> 20 #include <meta/interface/property/construct_property.h> 21 #include <meta/interface/property/intf_modifier.h> 22 #include <meta/interface/property/intf_stack_resetable.h> 23 #include <meta/interface/property/property.h> 24 25 #include "TestRunner.h" 26 #include "helpers/testing_objects.h" 27 28 using namespace testing; 29 using namespace testing::ext; 30 31 META_BEGIN_NAMESPACE() 32 33 class ModifierTest : public testing::Test { 34 public: SetUpTestSuite()35 static void SetUpTestSuite() 36 { 37 SetTest(); 38 } TearDownTestSuite()39 static void TearDownTestSuite() 40 { 41 TearDownTest(); 42 } SetUp()43 void SetUp() {} TearDown()44 void TearDown() {} 45 }; 46 47 META_REGISTER_CLASS(ValidRangeEvaluator, "bf310567-a76c-41c3-af08-81c1adf682ef", ObjectCategory::NO_CATEGORY) 48 49 class ValidRangeEvaluator : public IntroduceInterfaces<MinimalObject, IModifier> { 50 META_IMPLEMENT_OBJECT_TYPE_INTERFACE(ClassId::ValidRangeEvaluator) 51 public: ValidRangeEvaluator(int min,int max)52 ValidRangeEvaluator(int min, int max) : min_(min), max_(max) {} 53 ProcessOnGet(IAny & value)54 EvaluationResult ProcessOnGet(IAny& value) override 55 { 56 int v = GetValue<int>(value); 57 if (v < min_) { 58 value.CopyFrom(Any<int>(min_)); 59 return EVAL_VALUE_CHANGED; 60 } else if (v > max_) { 61 value.CopyFrom(Any<int>(max_)); 62 return EVAL_VALUE_CHANGED; 63 } 64 return EVAL_CONTINUE; 65 } ProcessOnSet(IAny & value,const IAny & current)66 EvaluationResult ProcessOnSet(IAny& value, const IAny& current) override 67 { 68 return EVAL_CONTINUE; 69 } IsCompatible(const TypeId & id) const70 bool IsCompatible(const TypeId& id) const override 71 { 72 return id == TypeId(UidFromType<int>()); 73 } 74 75 private: 76 int min_, max_; 77 }; 78 79 HWTEST_F(ModifierTest, ValidRange, TestSize.Level1) 80 { 81 auto p = ConstructProperty<int>("test"); 82 auto stack = interface_cast<IStackProperty>(p->GetProperty()); 83 ASSERT_TRUE(stack); 84 85 ASSERT_TRUE(stack->AddModifier(IModifier::Ptr(new ValidRangeEvaluator(1, 10)))); 86 p->SetValue(13); 87 EXPECT_EQ(p->GetValue(), 10); 88 p->SetValue(0); 89 EXPECT_EQ(p->GetValue(), 1); 90 p->SetValue(6); 91 EXPECT_EQ(p->GetValue(), 6); 92 93 auto mods = stack->GetModifiers(BASE_NS::vector<TypeId>({ IModifier::UID }), false); 94 EXPECT_EQ(mods.size(), 1); 95 } 96 97 HWTEST_F(ModifierTest, ValidRangePropertyBind, TestSize.Level1) 98 { 99 auto p = ConstructProperty<int>("test"); 100 auto modifier = IModifier::Ptr(new ValidRangeEvaluator(1, 10)); 101 auto stack = interface_cast<IStackProperty>(p->GetProperty()); 102 ASSERT_TRUE(stack); 103 stack->AddModifier(modifier); 104 105 auto source = ConstructProperty<int>("source"); 106 source->SetValue(13); 107 108 EXPECT_TRUE(p->SetBind(source)); 109 EXPECT_EQ(p->GetValue(), 10); 110 111 source->SetValue(0); 112 EXPECT_EQ(p->GetValue(), 1); 113 114 source->SetValue(7); 115 EXPECT_EQ(p->GetValue(), 7); 116 117 source->Reset(); 118 EXPECT_EQ(p->GetValue(), 1); 119 120 auto vals = stack->GetValues(BASE_NS::vector<TypeId>({ IBind::UID }), false); 121 EXPECT_EQ(vals.size(), 1); 122 123 auto mods = stack->GetModifiers(BASE_NS::vector<TypeId>({ IModifier::UID }), false); 124 ASSERT_EQ(mods.size(), 1); 125 EXPECT_EQ(mods[0], modifier); 126 127 p->ResetBind(); 128 EXPECT_EQ(p->GetValue(), 1); 129 130 stack->RemoveModifier(modifier); 131 132 EXPECT_EQ(p->GetValue(), 0); 133 } 134 135 HWTEST_F(ModifierTest, IncompatibleModifier, TestSize.Level1) 136 { 137 auto modifier = IModifier::Ptr(new ValidRangeEvaluator(1, 10)); 138 auto p = ConstructProperty<BASE_NS::string>("test"); 139 EXPECT_EQ(p->AddModifier(modifier).GetError(), GenericError::INCOMPATIBLE_TYPES); 140 } 141 142 META_REGISTER_CLASS(StickyValidator, "11310567-a76c-41c3-af08-81c1adf682ef", ObjectCategory::NO_CATEGORY) 143 144 class StickyValidator : public IntroduceInterfaces<MinimalObject, IModifier, IStackResetable> { 145 META_IMPLEMENT_OBJECT_TYPE_INTERFACE(ClassId::StickyValidator) 146 public: StickyValidator(int value)147 StickyValidator(int value) : allowed_(value) {} 148 ProcessOnGet(IAny & value)149 EvaluationResult ProcessOnGet(IAny& value) override 150 { 151 return EVAL_CONTINUE; 152 } ProcessOnSet(IAny & value,const IAny & current)153 EvaluationResult ProcessOnSet(IAny& value, const IAny& current) override 154 { 155 if (GetValue<int>(value) != allowed_) { 156 return EVAL_ERROR; 157 } 158 return EVAL_CONTINUE; 159 } IsCompatible(const TypeId & id) const160 bool IsCompatible(const TypeId& id) const override 161 { 162 return id == TypeId(UidFromType<int>()); 163 } ProcessOnReset(const IAny & defaultValue)164 ResetResult ProcessOnReset(const IAny& defaultValue) override 165 { 166 return RESET_CONTINUE; 167 } 168 169 private: 170 int allowed_; 171 }; 172 173 HWTEST_F(ModifierTest, StickyValidator, TestSize.Level1) 174 { 175 auto p = ConstructProperty<int>("test"); 176 auto stack = p->GetStackProperty(); 177 ASSERT_TRUE(stack); 178 179 p->SetValue(3); 180 181 ASSERT_TRUE(stack->AddModifier(IModifier::Ptr(new StickyValidator(2)))); 182 EXPECT_FALSE(p->SetValue(1)); 183 EXPECT_EQ(p->GetValue(), 3); 184 EXPECT_TRUE(p->SetValue(2)); 185 EXPECT_EQ(p->GetValue(), 2); 186 187 EXPECT_FALSE(p->IsDefaultValue()); 188 p->ResetValue(); 189 EXPECT_EQ(p->GetValue(), 0); 190 EXPECT_FALSE(p->SetValue(1)); 191 EXPECT_EQ(p->GetValue(), 0); 192 193 EXPECT_TRUE(p->IsDefaultValue()); 194 auto mods = stack->GetModifiers(BASE_NS::vector<TypeId>({ IModifier::UID }), false); 195 EXPECT_EQ(mods.size(), 1); 196 } 197 198 META_END_NAMESPACE() 199