• 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 
16 #include "test/unittest/core/base/view_abstract_test_ng.h"
17 
18 using namespace testing;
19 using namespace testing::ext;
20 
21 namespace OHOS::Ace::NG {
22 /**
23  * @tc.name: SetTranslateTest001
24  * @tc.desc: Test the SetTranslate function of View_Abstract
25  * @tc.type: FUNC
26  */
27 HWTEST_F(ViewAbstractTestNg, SetTranslateTest001, TestSize.Level1)
28 {
29     /**
30      * @tc.steps: step1. Check ViewStackProcessor and construct TranslateOptions value.
31      */
32     TranslateOptions options1 { Dimension(100), Dimension(-200), Dimension(1) };
33     auto stack = ViewStackProcessor::GetInstance();
34     ASSERT_NE(stack, nullptr);
35     stack->ClearVisualState();
36     auto node = stack->GetMainFrameNode();
37     ASSERT_NE(node, nullptr);
38     /**
39      * @tc.steps: step2. Set a none zero TranslateOptions value and get.
40      * @tc.expected: step2. Translate value is same with the value set.
41      */
42     ViewAbstract::SetTranslate(options1);
43     auto translateValue = ViewAbstract::GetTranslate(node);
44     EXPECT_EQ(options1, translateValue);
45     /**
46      * @tc.steps: step3. Set a zero TranslateOptions value and get.
47      * @tc.expected: step3. Translate value is zero.
48      */
49     NG::TranslateOptions options2(0.0f, 0.0f, 0.0f);
50     ViewAbstract::SetTranslate(options2);
51     translateValue = ViewAbstract::GetTranslate(node);
52     EXPECT_EQ(translateValue.x.Value(), 0.0f);
53     EXPECT_EQ(translateValue.y.Value(), 0.0f);
54     EXPECT_EQ(translateValue.z.Value(), 0.0f);
55 }
56 
57 /**
58  * @tc.name: SetRotateTest001
59  * @tc.desc: Test the SetRotate function of View_Abstract
60  * @tc.type: FUNC
61  */
62 HWTEST_F(ViewAbstractTestNg, SetRotateTest001, TestSize.Level1)
63 {
64     /**
65      * @tc.steps: step1. Check ViewStackProcessor.
66      */
67     auto stack = ViewStackProcessor::GetInstance();
68     ASSERT_NE(stack, nullptr);
69     stack->ClearVisualState();
70     auto node = stack->GetMainFrameNode();
71     ASSERT_NE(node, nullptr);
72     /**
73      * @tc.steps: step2. Set a none zero angle value and get.
74      * @tc.expected: step2. Angle value is same with the value set.
75      */
76     constexpr float angle = 90.0f;
77     Vector5F options1 { 0.0f, 0.0f, 1.0f, angle, 0.0f };
78     ViewAbstract::SetRotate(options1);
79     auto rotateValue = ViewAbstract::GetRotate(node);
80     EXPECT_EQ(options1, rotateValue);
81     /**
82      * @tc.steps: step3. Set a zero angle value and get. Restore the rotate value in node.
83      * @tc.expected: step3. Angle value is same with the value set.
84      */
85     constexpr float angleZero = 0.0f;
86     Vector5F options2 { 0.0f, 0.0f, 1.0f, angleZero, 0.0f };
87     // the last step should be restore the rotation value on node to default value.
88     ViewAbstract::SetRotate(options2);
89     rotateValue = ViewAbstract::GetRotate(node);
90     EXPECT_TRUE(NearEqual(rotateValue.w, angleZero));
91 }
92 
93 /**
94  * @tc.name: SetScaleTest001
95  * @tc.desc: Test the SetScale function of View_Abstract
96  * @tc.type: FUNC
97  */
98 HWTEST_F(ViewAbstractTestNg, SetScaleTest001, TestSize.Level1)
99 {
100     /**
101      * @tc.steps: step1. Check ViewStackProcessor.
102      */
103     auto stack = ViewStackProcessor::GetInstance();
104     ASSERT_NE(stack, nullptr);
105     stack->ClearVisualState();
106     auto node = stack->GetMainFrameNode();
107     ASSERT_NE(node, nullptr);
108     /**
109      * @tc.steps: step2. Set a positive scale value and get.
110      * @tc.expected: step2. Scale value is same with the value set.
111      */
112     constexpr float scaleValue1 = 2.0f;
113     VectorF scale1 { scaleValue1, scaleValue1 };
114     ViewAbstract::SetScale(scale1);
115     auto scaleValue = ViewAbstract::GetScale(node);
116     EXPECT_EQ(scale1, scaleValue);
117     /**
118      * @tc.steps: step3. Set a negative angle value and get.
119      * @tc.expected: step3. Scale value is same with the value set.
120      */
121     constexpr float scaleValue2 = -2.0f;
122     VectorF scale2 { scaleValue2, scaleValue2 };
123     ViewAbstract::SetScale(scale2);
124     scaleValue = ViewAbstract::GetScale(node);
125     EXPECT_EQ(scale2, scaleValue);
126     /**
127      * @tc.steps: step3. Set a zero angle value and get. Restore the rotate value in node.
128      * @tc.expected: step3. Angle value is same with the value set.
129      */
130     VectorF scale3 { 1.0f, 1.0f };
131     ViewAbstract::SetScale(scale3);
132     scaleValue = ViewAbstract::GetScale(node);
133     EXPECT_EQ(scale3, scaleValue);
134 }
135 
136 /**
137  * @tc.name: SetPivotTest001
138  * @tc.desc: Test the SetPivot function of View_Abstract
139  * @tc.type: FUNC
140  */
141 HWTEST_F(ViewAbstractTestNg, SetPivotTest001, TestSize.Level1)
142 {
143     /**
144      * @tc.steps: step1. Check ViewStackProcessor.
145      */
146     auto stack = ViewStackProcessor::GetInstance();
147     ASSERT_NE(stack, nullptr);
148     stack->ClearVisualState();
149     auto node = stack->GetMainFrameNode();
150     ASSERT_NE(node, nullptr);
151     /**
152      * @tc.steps: step2. Set a pivot value with x and y value and get.
153      * @tc.expected: step2. Pivot value is same with the value set.
154      */
155     constexpr float centerX = 50.0f;
156     constexpr float centerY = -50.0f;
157     DimensionOffset offset1 { Dimension(centerX), Dimension(centerY) };
158     ViewAbstract::SetPivot(offset1);
159     auto pivotProperty = node->GetRenderContext()->GetTransformCenter();
160     ASSERT_TRUE(pivotProperty.has_value());
161     EXPECT_TRUE(NearEqual(offset1, pivotProperty.value()));
162     /**
163      * @tc.steps: step3. Set a pivot value with x, y and z value and get.
164      * @tc.expected: step3. Pivot value is same with the value set.
165      */
166     DimensionOffset offset2 { Dimension(centerX), Dimension(0.0f) };
167     constexpr float centerZ = 50.0f;
168     offset2.SetZ(Dimension(centerZ));
169     ViewAbstract::SetPivot(offset2);
170     pivotProperty = node->GetRenderContext()->GetTransformCenter();
171     ASSERT_TRUE(pivotProperty.has_value());
172     EXPECT_TRUE(NearEqual(offset2, pivotProperty.value()));
173     /**
174      * @tc.steps: step4. Set a default pivot value and get. Restore the pivot value in node.
175      * @tc.expected: step4. Pivot value is same with the value set.
176      */
177     constexpr Dimension defaultDim(0.5f, DimensionUnit::PERCENT);
178     DimensionOffset offset3 { defaultDim, defaultDim };
179     ViewAbstract::SetPivot(offset3);
180     pivotProperty = node->GetRenderContext()->GetTransformCenter();
181     ASSERT_TRUE(pivotProperty.has_value());
182     EXPECT_TRUE(NearEqual(offset3, pivotProperty.value()));
183 }
184 } // namespace OHOS::Ace::NG
185