1 /*
2  * Copyright (c) 2022-2023 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 <memory>
17 #include "gtest/gtest.h"
18 #include "gtest/hwext/gtest-tag.h"
19 #include "include/modifier/rs_render_modifier.h"
20 #include "message_parcel.h"
21 #include "common/rs_vector4.h"
22 #include "pipeline/rs_render_node.h"
23 
24 using namespace testing;
25 using namespace testing::ext;
26 
27 namespace OHOS::Rosen {
28 class RSRenderPropertyTest : public testing::Test {
29 public:
30     constexpr static float floatData[] = {
31         0.0f, 485.44f, 2.0f,
32         std::numeric_limits<float>::max(), std::numeric_limits<float>::min(),
33         };
34     PropertyId id = 100;
35     static void SetUpTestCase();
36     static void TearDownTestCase();
37     void SetUp() override;
38     void TearDown() override;
39 };
40 
SetUpTestCase()41 void RSRenderPropertyTest::SetUpTestCase() {}
TearDownTestCase()42 void RSRenderPropertyTest::TearDownTestCase() {}
SetUp()43 void RSRenderPropertyTest::SetUp() {}
TearDown()44 void RSRenderPropertyTest::TearDown() {}
45 
46 /**
47  * @tc.name: LifeCycle001
48  * @tc.desc:
49  * @tc.type:FUNC
50  */
51 HWTEST_F(RSRenderPropertyTest, LifeCycle001, TestSize.Level1)
52 {
53     auto prop = std::make_shared<RSRenderProperty<float>>();
54     ASSERT_TRUE(prop != nullptr);
55     ASSERT_TRUE(prop->GetId() == 0);
56 
57     auto prop2 = std::make_shared<RSRenderProperty<float>>(floatData[0], id);
58     ASSERT_TRUE(prop2 != nullptr);
59     ASSERT_EQ(prop2->GetId(), id);
60 }
61 
62 /**
63  * @tc.name: Property001
64  * @tc.desc:
65  * @tc.type:FUNC
66  */
67 HWTEST_F(RSRenderPropertyTest, Property001, TestSize.Level1)
68 {
69     auto prop = std::make_shared<RSRenderProperty<float>>(floatData[0], id);
70     ASSERT_TRUE(prop != nullptr);
71     ASSERT_EQ(prop->Get(), floatData[0]);
72 
73     prop->Set(floatData[1]);
74     ASSERT_EQ(prop->Get(), floatData[1]);
75 }
76 
77 /**
78  * @tc.name: PropertyOp001
79  * @tc.desc:
80  * @tc.type:FUNC
81  */
82 HWTEST_F(RSRenderPropertyTest, PropertyOp001, TestSize.Level1)
83 {
84     std::shared_ptr<RSRenderAnimatableProperty<float>> prop1 = nullptr;
85     auto prop2 = std::make_shared<RSRenderAnimatableProperty<float>>(floatData[0], id);
86     auto prop3 = std::make_shared<RSRenderAnimatableProperty<float>>(floatData[1], id + 1);
87 
88     prop1 += prop3;
89     ASSERT_EQ(prop1, nullptr);
90 
91     prop2 += prop3;
92     ASSERT_TRUE(ROSEN_EQ(prop2->Get(), floatData[0] + floatData[1]));
93 
94     prop1 -= prop3;
95     ASSERT_EQ(prop1, nullptr);
96 
97     prop2 -= prop3;
98     ASSERT_TRUE(ROSEN_EQ(prop2->Get(), floatData[0]));
99 
100     ASSERT_EQ(prop1 + prop3, nullptr);
101     ASSERT_TRUE(ROSEN_EQ(
102         std::static_pointer_cast<RSRenderProperty<float>>(prop2 + prop3)->Get(), floatData[0] + floatData[1]));
103 
104     ASSERT_EQ(prop1 - prop3, nullptr);
105     ASSERT_TRUE(ROSEN_EQ(
106         std::static_pointer_cast<RSRenderProperty<float>>(prop2 - prop3)->Get(), floatData[0] - floatData[1]));
107 
108     ASSERT_EQ(prop1 * floatData[1], nullptr);
109     ASSERT_TRUE(ROSEN_EQ(
110         std::static_pointer_cast<RSRenderProperty<float>>(prop2 * floatData[1])->Get(), floatData[0] * floatData[1]));
111 
112     ASSERT_FALSE(prop1 == prop3);
113     ASSERT_TRUE(prop1 != prop3);
114 
115     ASSERT_FALSE(prop2 == prop3);
116     ASSERT_TRUE(prop2 != prop3);
117 }
118 
119 template<typename T>
120 class MockRSRenderProperty : public RSRenderProperty<T> {
121 public:
MockRSRenderProperty(const RSRenderPropertyType type)122     explicit MockRSRenderProperty(const RSRenderPropertyType type) : RSRenderProperty<T>(), type_(type) {}
123     virtual ~MockRSRenderProperty() = default;
124 protected:
125     RSRenderPropertyType type_;
126 };
127 
128 template<typename T>
129 class MockRSRenderAnimatableProperty : public RSRenderAnimatableProperty<T> {
130 public:
MockRSRenderAnimatableProperty(const T & value)131     explicit MockRSRenderAnimatableProperty(const T& value) : RSRenderAnimatableProperty<T>(value) {}
MockRSRenderAnimatableProperty(const RSRenderPropertyType type)132     explicit MockRSRenderAnimatableProperty(const RSRenderPropertyType type) : RSRenderAnimatableProperty<T>()
133     {
134         RSRenderAnimatableProperty<T>::SetPropertyType(type);
135     }
136     virtual ~MockRSRenderAnimatableProperty() = default;
ToFloat() const137     float ToFloat() const
138     {
139         return RSRenderAnimatableProperty<T>::ToFloat();
140     }
141 };
142 /**
143  * @tc.name: PropertyOp002
144  * @tc.desc:
145  * @tc.type:FUNC
146  */
147 HWTEST_F(RSRenderPropertyTest, PropertyOp002, TestSize.Level1)
148 {
149     auto prop0 = std::make_shared<MockRSRenderAnimatableProperty<float>>(floatData[0]);
150     ASSERT_TRUE(ROSEN_EQ(prop0->ToFloat(), floatData[0]));
151 
152     auto prop1 = std::make_shared<MockRSRenderAnimatableProperty<Vector4f>>(Vector4f(floatData[1]));
153     ASSERT_TRUE(ROSEN_EQ(prop1->ToFloat(), 2 * floatData[1]));
154 
155     auto prop2 = std::make_shared<MockRSRenderAnimatableProperty<Vector2f>>(Vector2f(floatData[2], 0.f));
156     ASSERT_TRUE(ROSEN_EQ(prop2->ToFloat(), floatData[2]));
157 
158     auto prop3 = std::make_shared<MockRSRenderAnimatableProperty<Quaternion>>(Quaternion());
159     ASSERT_TRUE(ROSEN_EQ(prop3->ToFloat(), 1.f));
160 }
161 
162 /**
163  * @tc.name: PropertyIPC001
164  * @tc.desc:
165  * @tc.type:FUNC
166  */
167 HWTEST_F(RSRenderPropertyTest, PropertyIPC001, TestSize.Level1)
168 {
169     std::vector<std::shared_ptr<RSRenderPropertyBase>> props;
170     props.push_back(std::make_shared<MockRSRenderAnimatableProperty<float>>(
171         RSRenderPropertyType::PROPERTY_FLOAT));
172     props.push_back(std::make_shared<MockRSRenderAnimatableProperty<Color>>(
173         RSRenderPropertyType::PROPERTY_COLOR));
174     props.push_back(std::make_shared<MockRSRenderAnimatableProperty<Matrix3f>>(
175         RSRenderPropertyType::PROPERTY_MATRIX3F));
176     props.push_back(std::make_shared<MockRSRenderAnimatableProperty<Quaternion>>(
177         RSRenderPropertyType::PROPERTY_QUATERNION));
178     props.push_back(std::make_shared<MockRSRenderAnimatableProperty<std::shared_ptr<RSFilter>>>(
179         RSRenderPropertyType::PROPERTY_FILTER));
180     props.push_back(std::make_shared<MockRSRenderAnimatableProperty<Vector2f>>(
181         RSRenderPropertyType::PROPERTY_VECTOR2F));
182     props.push_back(std::make_shared<MockRSRenderAnimatableProperty<Vector4f>>(
183         RSRenderPropertyType::PROPERTY_VECTOR4F));
184     props.push_back(std::make_shared<MockRSRenderAnimatableProperty<Vector4<Color>>>(
185         RSRenderPropertyType::PROPERTY_VECTOR4_COLOR));
186 
187     for (auto& prop : props) {
188         MessageParcel parcel;
189         ASSERT_TRUE(RSRenderPropertyBase::Marshalling(parcel, prop));
190         ASSERT_TRUE(RSRenderPropertyBase::Unmarshalling(parcel, prop));
191     }
192 
193     MessageParcel parcel1;
194     auto intProp = std::make_shared<RSRenderAnimatableProperty<int>>();
195     std::shared_ptr<RSRenderPropertyBase> tmpProp;
196     ASSERT_FALSE(RSRenderPropertyBase::Marshalling(parcel1, intProp));
197     ASSERT_TRUE(RSRenderPropertyBase::Unmarshalling(parcel1, tmpProp));
198 
199     MessageParcel parcel2;
200     int data = 0;
201     parcel2.ParseFrom(data, sizeof(int));
202     ASSERT_FALSE(RSRenderPropertyBase::Marshalling(parcel2, intProp));
203 }
204 
205 /**
206  * @tc.name: PropertyIPC002
207  * @tc.desc:
208  * @tc.type:FUNC
209  */
210 HWTEST_F(RSRenderPropertyTest, PropertyIPC002, TestSize.Level1)
211 {
212     std::vector<std::shared_ptr<RSRenderPropertyBase>> props;
213     props.push_back(std::make_shared<MockRSRenderProperty<float>>(
214         RSRenderPropertyType::PROPERTY_MATRIX3F));
215     props.push_back(std::make_shared<MockRSRenderProperty<Color>>(
216         RSRenderPropertyType::PROPERTY_FLOAT));
217     props.push_back(std::make_shared<MockRSRenderProperty<Matrix3f>>(
218         RSRenderPropertyType::PROPERTY_FLOAT));
219     props.push_back(std::make_shared<MockRSRenderProperty<Quaternion>>(
220         RSRenderPropertyType::PROPERTY_FLOAT));
221     props.push_back(std::make_shared<MockRSRenderProperty<std::shared_ptr<RSFilter>>>(
222         RSRenderPropertyType::PROPERTY_FLOAT));
223     props.push_back(std::make_shared<MockRSRenderProperty<Vector2f>>(
224         RSRenderPropertyType::PROPERTY_FLOAT));
225     props.push_back(std::make_shared<MockRSRenderProperty<Vector4f>>(
226         RSRenderPropertyType::PROPERTY_FLOAT));
227     props.push_back(std::make_shared<MockRSRenderProperty<Vector4<Color>>>(
228         RSRenderPropertyType::PROPERTY_FLOAT));
229 
230     for (auto& prop : props) {
231         MessageParcel parcel;
232         ASSERT_FALSE(RSRenderPropertyBase::Marshalling(parcel, prop));
233         ASSERT_TRUE(RSRenderPropertyBase::Unmarshalling(parcel, prop));
234     }
235 }
236 
237 /**
238  * @tc.name: OnChange
239  * @tc.desc: Test OnChange and Marshalling
240  * @tc.type:FUNC
241  * @tc.require: issueI9QIQO
242  */
243 HWTEST_F(RSRenderPropertyTest, OnChange, TestSize.Level1)
244 {
245     std::shared_ptr<RSRenderPropertyBase> base = std::make_shared<RSRenderPropertyBase>();
246     base->OnChange();
247     base->Attach(std::make_shared<RSRenderNode>(1));
248     base->modifierType_ = RSModifierType::FOREGROUND_COLOR;
249     base->OnChange();
250     base->modifierType_ = RSModifierType::POSITION_Z;
251     base->OnChange();
252     base->modifierType_ = RSModifierType::FOREGROUND_STYLE;
253     base->OnChange();
254 
255     base->UpdatePropertyUnit(RSModifierType::FRAME);
256     base->UpdatePropertyUnit(RSModifierType::SCALE);
257     base->UpdatePropertyUnit(RSModifierType::ROTATION_X);
258     base->UpdatePropertyUnit(RSModifierType::CLIP_BOUNDS);
259     ASSERT_EQ(base->node_.lock(), nullptr);
260 }
261 
262 /**
263  * @tc.name: IsNearEqual
264  * @tc.desc: Test IsNearEqual
265  * @tc.type:FUNC
266  * @tc.require: issueI9QIQO
267  */
268 HWTEST_F(RSRenderPropertyTest, IsNearEqual, TestSize.Level1)
269 {
270     RSRenderAnimatableProperty<float> property;
271     Vector2f vector2f(0, 0);
272     RSRenderAnimatableProperty<Vector2f> propertyTwo(vector2f);
273     Matrix3f matrix3f;
274     RSRenderAnimatableProperty<Matrix3f> propertyMatrix3f(matrix3f);
275     Color color;
276     RSRenderAnimatableProperty<Color> propertyColor(color);
277     std::shared_ptr<RSFilter> rsFilter = std::make_shared<RSFilter>();
278     RSRenderAnimatableProperty<std::shared_ptr<RSFilter>> propertyRSFilter(rsFilter);
279     Vector4<Color> vector4;
280     RSRenderAnimatableProperty<Vector4<Color>> propertyVector4Color(vector4);
281     RRect rect;
282     RSRenderAnimatableProperty<RRect> propertyRect(rect);
283 
284     std::shared_ptr<RSRenderPropertyBase> value;
285     EXPECT_TRUE(property.IsNearEqual(value, 1.f));
286     EXPECT_TRUE(propertyTwo.IsNearEqual(value, 1.f));
287     EXPECT_TRUE(propertyMatrix3f.IsNearEqual(value, 1.f));
288     EXPECT_TRUE(propertyColor.IsNearEqual(value, 1.f));
289     EXPECT_TRUE(propertyRSFilter.IsNearEqual(value, 1.f));
290     EXPECT_TRUE(propertyVector4Color.IsNearEqual(value, 1.f));
291     EXPECT_TRUE(propertyRect.IsNearEqual(value, 1.f));
292 
293     std::shared_ptr<RSRenderProperty<float>> floatValue = std::make_shared<RSRenderProperty<float>>();
294     float float1 = 0.0;
295     floatValue->Set(float1);
296     std::shared_ptr<RSRenderProperty<Vector2f>> vector2fValue = std::make_shared<RSRenderProperty<Vector2f>>();
297     Vector2f vector2f1(0, 0);
298     vector2fValue->Set(vector2f1);
299     std::shared_ptr<RSRenderProperty<Matrix3f>> matrix3fValue = std::make_shared<RSRenderProperty<Matrix3f>>();
300     Matrix3f matrix3f1;
301     matrix3fValue->Set(matrix3f1);
302     std::shared_ptr<RSRenderProperty<Color>> colorValue = std::make_shared<RSRenderProperty<Color>>();
303     Color color1;
304     colorValue->Set(color1);
305     std::shared_ptr<RSRenderProperty<Vector4<Color>>> vector4Value
306     = std::make_shared<RSRenderProperty<Vector4<Color>>>();
307     Vector4<Color> vector41;
308     vector4Value->Set(vector41);
309     std::shared_ptr<RSRenderProperty<RRect>> rectValue = std::make_shared<RSRenderProperty<RRect>>();
310     RRect rect1;
311     rectValue->Set(rect1);
312     EXPECT_TRUE(property.IsNearEqual(floatValue, 1.f));
313     EXPECT_TRUE(propertyTwo.IsNearEqual(vector2fValue, 1.f));
314     EXPECT_TRUE(propertyMatrix3f.IsNearEqual(matrix3fValue, 1.f));
315     EXPECT_TRUE(propertyColor.IsNearEqual(colorValue, 1.f));
316     EXPECT_TRUE(propertyVector4Color.IsNearEqual(vector4Value, 1.f));
317     EXPECT_TRUE(propertyRect.IsNearEqual(rectValue, 1.f));
318 }
319 
320 /**
321  * @tc.name: OnChange001
322  * @tc.desc: Test OnChange
323  * @tc.type:FUNC
324  * @tc.require: issueI9QIQO
325  */
326 HWTEST_F(RSRenderPropertyTest, OnChange001, TestSize.Level1)
327 {
328     std::shared_ptr<RSRenderPropertyBase> base = std::make_shared<RSRenderPropertyBase>();
329     Parcel parcel;
330     std::vector<std::shared_ptr<RSRenderPropertyBase>> props;
331     props.push_back(std::make_shared<MockRSRenderProperty<Color>>(
332         RSRenderPropertyType::PROPERTY_FLOAT));
333 
334     const std::shared_ptr<RSRenderPropertyBase> val = nullptr;
335     bool ret = base->Marshalling(parcel, val);
336     ASSERT_TRUE(ret);
337 }
338 
339 /**
340  * @tc.name: OnChange002
341  * @tc.desc: Test OnChange
342  * @tc.type:FUNC
343  * @tc.require: issueI9QIQO
344  */
345 HWTEST_F(RSRenderPropertyTest, OnChange002, TestSize.Level1)
346 {
347     std::shared_ptr<RSRenderPropertyBase> base = std::make_shared<RSRenderPropertyBase>();
348     Parcel parcel;
349     std::vector<std::shared_ptr<RSRenderPropertyBase>> props;
350     props.push_back(std::make_shared<MockRSRenderProperty<Color>>(
351         RSRenderPropertyType::PROPERTY_RRECT));
352     for (auto& prop : props) {
353         MessageParcel parcel;
354         ASSERT_FALSE(RSRenderPropertyBase::Marshalling(parcel, prop));
355         ASSERT_TRUE(RSRenderPropertyBase::Unmarshalling(parcel, prop));
356     }
357 }
358 
359 /**
360  * @tc.name: IsNearEqual001
361  * @tc.desc: Test IsNearEqual
362  * @tc.type:FUNC
363  * @tc.require: issueI9QIQO
364  */
365 HWTEST_F(RSRenderPropertyTest, IsNearEqual001, TestSize.Level1)
366 {
367     RSRenderAnimatableProperty<float> property1;
368     RSRenderAnimatableProperty<Vector2f> property2;
369     RSRenderAnimatableProperty<Quaternion> property3;
370     RSRenderAnimatableProperty<Vector4f> property4;
371     RSRenderAnimatableProperty<Matrix3f> property5;
372     RSRenderAnimatableProperty<Color> property6;
373     RSRenderAnimatableProperty<std::shared_ptr<RSFilter>> property7;
374     RSRenderAnimatableProperty<Vector4<Color>> property8;
375     RSRenderAnimatableProperty<RRect> property9;
376     RSRenderAnimatableProperty<std::shared_ptr<RSFilter>> property10;
377     const std::shared_ptr<RSRenderPropertyBase> value = nullptr;
378     float zeroThreshold = 0.0;
379     ASSERT_TRUE(property1.IsNearEqual(value, zeroThreshold));
380     ASSERT_TRUE(property2.IsNearEqual(value, zeroThreshold));
381     ASSERT_TRUE(property3.IsNearEqual(value, zeroThreshold));
382     ASSERT_TRUE(property4.IsNearEqual(value, zeroThreshold));
383     ASSERT_TRUE(property5.IsNearEqual(value, zeroThreshold));
384     ASSERT_TRUE(property6.IsNearEqual(value, zeroThreshold));
385     ASSERT_TRUE(property7.IsNearEqual(value, zeroThreshold));
386     ASSERT_TRUE(property8.IsNearEqual(value, zeroThreshold));
387     ASSERT_TRUE(property9.IsNearEqual(value, zeroThreshold));
388     ASSERT_TRUE(property10.IsEqual(value));
389 }
390 
391 /**
392  * @tc.name: IsNearEqual002
393  * @tc.desc: Test IsNearEqual
394  * @tc.type:FUNC
395  * @tc.require: issueI9QIQO
396  */
397 HWTEST_F(RSRenderPropertyTest, IsNearEqual002, TestSize.Level1)
398 {
399     RSRenderAnimatableProperty<float> property1;
400     Vector2f vector2f;
401     RSRenderAnimatableProperty<Vector2f> property2(vector2f);
402     Quaternion quaternion;
403     RSRenderAnimatableProperty<Quaternion> property3(quaternion);
404     Vector4f vector4;
405     RSRenderAnimatableProperty<Vector4f> property4(vector4);
406     Matrix3f matrix3f;
407     RSRenderAnimatableProperty<Matrix3f> property5(matrix3f);
408     Color color;
409     RSRenderAnimatableProperty<Color> property6(color);
410     std::shared_ptr<RSFilter> pFilter = std::make_shared<RSFilter>();
411     RSRenderAnimatableProperty<std::shared_ptr<RSFilter>> property7(pFilter);
412     Vector4<Color> vectorColor;
413     RSRenderAnimatableProperty<Vector4<Color>> property8(vectorColor);
414     RRect rect;
415     RSRenderAnimatableProperty<RRect> property9(rect);
416     float zeroThreshold = 0.0001;
417 
418     std::shared_ptr<RSRenderProperty<float>> floatValue = std::make_shared<RSRenderProperty<float>>();
419     std::shared_ptr<RSRenderProperty<Vector2f>> vector2fValue = std::make_shared<RSRenderProperty<Vector2f>>();
420     vector2fValue->Set(vector2f);
421     std::shared_ptr<RSRenderProperty<Quaternion>> quaternionValue = std::make_shared<RSRenderProperty<Quaternion>>();
422     quaternionValue->Set(quaternion);
423     std::shared_ptr<RSRenderProperty<Vector4f>> vector4Value = std::make_shared<RSRenderProperty<Vector4f>>();
424     vector4Value->Set(vector4);
425     std::shared_ptr<RSRenderProperty<Matrix3f>> matrix3fValue = std::make_shared<RSRenderProperty<Matrix3f>>();
426     matrix3fValue->Set(matrix3f);
427     std::shared_ptr<RSRenderProperty<std::shared_ptr<RSFilter>>> pFilterValue
428     = std::make_shared<RSRenderProperty<std::shared_ptr<RSFilter>>>();
429     pFilterValue->Set(pFilter);
430     std::shared_ptr<RSRenderProperty<Color>> colorValue = std::make_shared<RSRenderProperty<Color>>();
431     colorValue->Set(color);
432     std::shared_ptr<RSRenderProperty<Vector4<Color>>> vector4ColorValue
433     = std::make_shared<RSRenderProperty<Vector4<Color>>>();
434     vector4ColorValue->Set(vectorColor);
435     std::shared_ptr<RSRenderProperty<RRect>> rectValue = std::make_shared<RSRenderProperty<RRect>>();
436     rectValue->Set(rect);
437 
438     EXPECT_TRUE(property1.IsNearEqual(floatValue, zeroThreshold));
439     EXPECT_TRUE(property2.IsNearEqual(vector2fValue, zeroThreshold));
440     EXPECT_TRUE(property3.IsNearEqual(quaternionValue, zeroThreshold));
441     EXPECT_TRUE(property4.IsNearEqual(vector4Value, zeroThreshold));
442     EXPECT_TRUE(property5.IsNearEqual(matrix3fValue, zeroThreshold));
443     EXPECT_TRUE(property6.IsNearEqual(colorValue, zeroThreshold));
444     EXPECT_TRUE(property7.IsNearEqual(pFilterValue, zeroThreshold));
445     EXPECT_TRUE(property8.IsNearEqual(vector4ColorValue, zeroThreshold));
446     EXPECT_TRUE(property9.IsNearEqual(rectValue, zeroThreshold));
447     EXPECT_TRUE(property7.IsEqual(pFilterValue));
448 }
449 }
450