• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/api/array_util.h>
19 #include <meta/api/function.h>
20 #include <meta/api/object.h>
21 #include <meta/api/property/array_element_bind.h>
22 #include <meta/interface/property/array_property.h>
23 #include <meta/interface/property/construct_array_property.h>
24 #include <meta/interface/property/construct_property.h>
25 #include <meta/interface/property/property.h>
26 
27 #include "TestRunner.h"
28 #include "helpers/testing_objects.h"
29 #include "helpers/util.h"
30 
31 using namespace testing;
32 using namespace testing::ext;
33 
34 META_BEGIN_NAMESPACE()
35 
36 class ArrayPropertyTest : public testing::Test {
37 public:
SetUpTestSuite()38     static void SetUpTestSuite()
39     {
40         SetTest();
41     }
TearDownTestSuite()42     static void TearDownTestSuite()
43     {
44         TearDownTest();
45     }
SetUp()46     void SetUp() {}
TearDown()47     void TearDown() {}
48 };
49 
50 /**
51  * @tc.name: Values
52  * @tc.desc: test Values
53  * @tc.type: FUNC
54  */
55 HWTEST_F(ArrayPropertyTest, Values, TestSize.Level1)
56 {
57     auto p = ConstructArrayProperty<int>("test");
58 
59     EXPECT_EQ(p->GetSize(), 0);
60     EXPECT_TRUE(p->SetDefaultValue({ 1, 2, 3 }));
61     EXPECT_EQ(p->GetSize(), 3);
62     EXPECT_EQ(p->GetDefaultValue(), (BASE_NS::vector<int> { 1, 2, 3 }));
63     EXPECT_EQ(p->GetValue(), (BASE_NS::vector<int> { 1, 2, 3 }));
64     EXPECT_TRUE(p->SetValue({ 1, 2 }));
65     EXPECT_EQ(p->GetValue(), (BASE_NS::vector<int> { 1, 2 }));
66     EXPECT_EQ(p->GetValueAt(0), 1);
67     EXPECT_EQ(p->GetValueAt(1), 2);
68     EXPECT_TRUE(p->AddValue(5));
69     EXPECT_EQ(p->GetSize(), 3);
70     EXPECT_EQ(p->GetValueAt(2), 5);
71     EXPECT_TRUE(p->InsertValueAt(1, 7));
72     EXPECT_EQ(p->GetSize(), 4);
73     EXPECT_EQ(p->GetValueAt(1), 7);
74     EXPECT_EQ(p->GetValue(), (BASE_NS::vector<int> { 1, 7, 2, 5 }));
75     EXPECT_TRUE(p->RemoveAt(0));
76     EXPECT_EQ(p->GetSize(), 3);
77     EXPECT_EQ(p->GetValue(), (BASE_NS::vector<int> { 7, 2, 5 }));
78     EXPECT_TRUE(p->SetValueAt(1, 11));
79     EXPECT_EQ(p->GetValue(), (BASE_NS::vector<int> { 7, 11, 5 }));
80 }
81 
82 /**
83  * @tc.name: ValuesTypeless
84  * @tc.desc: test ValuesTypeless
85  * @tc.type: FUNC
86  */
87 HWTEST_F(ArrayPropertyTest, ValuesTypeless, TestSize.Level1)
88 {
89     auto p = ConstructArrayProperty<int>("test");
90 
91     Any<int> value1 { 1 };
92     Any<int> value2 { 2 };
93     Any<int> value3 { 3 };
94     p->AddAny(value1);
95     EXPECT_EQ(p->GetSize(), 1);
96     EXPECT_EQ(p->GetValue(), (BASE_NS::vector<int> { 1 }));
97     p->AddAny(value3);
98     p->InsertAnyAt(1, value2);
99     EXPECT_EQ(p->GetValue(), (BASE_NS::vector<int> { 1, 2, 3 }));
100     Any<int> v;
101     EXPECT_TRUE(p->GetAnyAt(0, v));
102     EXPECT_EQ(GetValue<int>(v), 1);
103 }
104 
105 /**
106  * @tc.name: ArrayElementBind
107  * @tc.desc: test ArrayElementBind
108  * @tc.type: FUNC
109  */
110 HWTEST_F(ArrayPropertyTest, ArrayElementBind, TestSize.Level1)
111 {
112     auto arr = ConstructArrayProperty<int>("test", BASE_NS::vector<int> { 1, 2, 3 });
113     auto p = ConstructProperty<int>("p");
114     AddArrayElementBind(p, arr, 1);
115 
116     int arrCount = 0;
__anon7f36f75b0102null117     arr->OnChanged()->AddHandler(MakeCallback<IOnChanged>([&] { ++arrCount; }));
118 
119     int pCount = 0;
__anon7f36f75b0202null120     p->OnChanged()->AddHandler(MakeCallback<IOnChanged>([&] { ++pCount; }));
121 
122     EXPECT_EQ(arr->GetValue(), (BASE_NS::vector<int> { 1, 2, 3 }));
123     EXPECT_TRUE(p->SetValue(4));
124     EXPECT_EQ(pCount, 1);
125     EXPECT_EQ(arrCount, 1);
126     EXPECT_EQ(arr->GetValue(), (BASE_NS::vector<int> { 1, 4, 3 }));
127     EXPECT_TRUE(arr->SetValue({ 1, 1, 1 }));
128     EXPECT_EQ(pCount, 2);
129     EXPECT_EQ(arrCount, 2);
130     EXPECT_EQ(p->GetValue(), 1);
131     EXPECT_TRUE(arr->SetValue({ 1, 1, 4 }));
132     EXPECT_EQ(p->GetValue(), 1);
133 }
134 
135 /**
136  * @tc.name: ValidArrayPropertyLock
137  * @tc.desc: test ValidArrayPropertyLock
138  * @tc.type: FUNC
139  */
140 HWTEST_F(ArrayPropertyTest, ValidArrayPropertyLock, TestSize.Level1)
141 {
142     {
143         auto p = ConstructArrayProperty<int>("p").GetProperty();
144         ArrayPropertyLock lock { p };
145         EXPECT_TRUE(lock);
146         EXPECT_TRUE(lock.IsValid());
147         EXPECT_TRUE(lock.GetProperty());
148 
149         TypedArrayPropertyLock<int> typedLock { p };
150         EXPECT_TRUE(typedLock);
151         EXPECT_TRUE(typedLock.IsValid());
152         EXPECT_TRUE(typedLock.GetProperty());
153 
154         TypedArrayPropertyLock<float> bad { p };
155         EXPECT_FALSE(bad);
156         EXPECT_FALSE(bad.IsValid());
157         EXPECT_FALSE(bad.GetProperty());
158     }
159     {
160         auto p = ConstructProperty<int>("p").GetProperty();
161         ArrayPropertyLock lock { p };
162         EXPECT_FALSE(lock);
163         EXPECT_FALSE(lock.IsValid());
164         EXPECT_FALSE(lock.GetProperty());
165 
166         TypedArrayPropertyLock<int> typedLock { p };
167         EXPECT_FALSE(typedLock);
168         EXPECT_FALSE(typedLock.IsValid());
169         EXPECT_FALSE(typedLock.GetProperty());
170 
171         TypedArrayPropertyLock<float> bad { p };
172         EXPECT_FALSE(bad);
173         EXPECT_FALSE(bad.IsValid());
174         EXPECT_FALSE(bad.GetProperty());
175     }
176 }
177 
178 /**
179  * @tc.name: TypelessAccess
180  * @tc.desc: test TypelessAccess
181  * @tc.type: FUNC
182  */
183 HWTEST_F(ArrayPropertyTest, TypelessAccess, TestSize.Level1)
184 {
185     auto p = ConstructArrayProperty<int>("p", { 1, 2, 3, 4, 5 });
186     {
187         auto any = p->GetAnyAt(0);
188         ASSERT_TRUE(any);
189         EXPECT_EQ(GetValue<int>(*any), 1);
190         EXPECT_FALSE(p->GetAnyAt(5));
191     }
192     {
193         EXPECT_FALSE(p->SetAnyAt(0, Any<float>(0)));
194         EXPECT_FALSE(p->InsertAnyAt(0, Any<float>(0)));
195         EXPECT_FALSE(p->AddAny(Any<float>(0)));
196         EXPECT_FALSE(p->RemoveAt(5));
197     }
198 }
199 
200 /**
201  * @tc.name: TypelessAccessCompatibility
202  * @tc.desc: test TypelessAccessCompatibility
203  * @tc.type: FUNC
204  */
205 HWTEST_F(ArrayPropertyTest, TypelessAccessCompatibility, TestSize.Level1)
206 {
207     Object a(CreateInstance(ClassId::Object)), b(CreateInstance(ClassId::Object)), c(CreateInstance(ClassId::Object)),
208         d(CreateInstance(ClassId::Object));
209     auto p = ConstructArrayProperty<SharedPtrIInterface>("p", { a.GetPtr(), b.GetPtr(), c.GetPtr() });
210     {
211         ArrayPropertyLock l { p.GetProperty() };
212 
213         EXPECT_TRUE(l->SetAnyAt(0, Any<IObject::Ptr> { d.GetPtr() }));
214 
215         auto any = l->GetAnyAt(0);
216         ASSERT_TRUE(any);
217         EXPECT_EQ(GetValue<SharedPtrIInterface>(*any), interface_pointer_cast<CORE_NS::IInterface>(d));
218         EXPECT_FALSE(p->GetAnyAt(5));
219     }
220 }
221 
222 /**
223  * @tc.name: PropertyHelpers
224  * @tc.desc: test PropertyHelpers
225  * @tc.type: FUNC
226  */
227 HWTEST_F(ArrayPropertyTest, PropertyHelpers, TestSize.Level1)
228 {
229     Object a(CreateInstance(ClassId::Object)), b(CreateInstance(ClassId::Object)), c(CreateInstance(ClassId::Object));
230     auto p = ConstructArrayProperty<SharedPtrIInterface>("p", { a.GetPtr(), b.GetPtr(), c.GetPtr() });
231 
232     EXPECT_FALSE(IsGetPointer(p));
233     EXPECT_FALSE(IsSetPointer(p));
234     EXPECT_TRUE(IsGetPointerArray(p));
235     EXPECT_TRUE(IsSetPointerArray(p));
236     EXPECT_EQ(GetPointerAt(0, p), interface_pointer_cast<CORE_NS::IInterface>(a));
237     EXPECT_EQ(GetPointerAt<IObject>(0, p), a.GetPtr());
238 
239     EXPECT_TRUE(IsArrayProperty(p));
240     EXPECT_FALSE(IsArrayProperty(ConstructProperty<int>("p").GetProperty()));
241 
242     auto other = CreateTestType<IObject>();
243 
244     EXPECT_TRUE(SetPointerAt(0, p, other));
245     EXPECT_EQ(GetPointerAt(0, p), interface_pointer_cast<CORE_NS::IInterface>(other));
246 
247     auto cp = ConstructArrayProperty<IObject::ConstPtr>("p", { a.GetPtr(), b.GetPtr(), c.GetPtr() });
248     EXPECT_TRUE(IsGetPointerArray(cp));
249     EXPECT_TRUE(IsSetPointerArray(cp));
250     EXPECT_TRUE(SetPointerAt(0, cp, other));
251     EXPECT_EQ(GetConstPointerAt(0, cp), interface_pointer_cast<CORE_NS::IInterface>(other));
252 
253     IObject::ConstPtr some = CreateTestType<IObject>();
254     EXPECT_TRUE(SetPointerAt(0, cp, some));
255     EXPECT_EQ(GetConstPointerAt(0, cp), interface_pointer_cast<CORE_NS::IInterface>(some));
256 }
257 
258 META_END_NAMESPACE()
259