1 /*
2 * Copyright (c) 2022 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 "gtest/gtest.h"
17
18 #include "base/geometry/transform_util.h"
19
20 using namespace testing;
21 using namespace testing::ext;
22
23 namespace OHOS::Ace {
24 namespace {
25 const double NUM_D1 = 1.0;
26 const double NUM_D2 = 0.0;
27 const float PROGRESS = 0.5f;
28 }
29
30 class TransformUtilTest : public testing::Test {
31 public:
32 static void SetUpTestCase();
33 static void TearDownTestCase();
34 void SetUp();
35 void TearDown();
36 };
37
SetUpTestCase()38 void TransformUtilTest::SetUpTestCase()
39 {
40 GTEST_LOG_(INFO) << "TransformUtilTest SetUpTestCase";
41 }
42
TearDownTestCase()43 void TransformUtilTest::TearDownTestCase()
44 {
45 GTEST_LOG_(INFO) << "TransformUtilTest TearDownTestCase";
46 }
47
SetUp()48 void TransformUtilTest::SetUp()
49 {
50 GTEST_LOG_(INFO) << "TransformUtilTest SetUp";
51 }
52
TearDown()53 void TransformUtilTest::TearDown()
54 {
55 GTEST_LOG_(INFO) << "TransformUtilTest TearDown";
56 }
57
58 /**
59 * @tc.name: TransformUtilTest001
60 * @tc.desc: Test the functions of the class TranslateOperation.
61 * @tc.type: FUNC
62 */
63 HWTEST_F(TransformUtilTest, TransformUtilTest001, TestSize.Level1)
64 {
65 Dimension dimension(NUM_D1);
66 TranslateOperation to(dimension, dimension, dimension);
67 TranslateOperation from(dimension, dimension, dimension);
68 TranslateOperation result = TranslateOperation::Blend(to, from, PROGRESS);
69 EXPECT_EQ(result, to);
70 }
71
72 /**
73 * @tc.name: TransformUtilTest002
74 * @tc.desc: Test the functions of the class ScaleOperation.
75 * @tc.type: FUNC
76 */
77 HWTEST_F(TransformUtilTest, TransformUtilTest002, TestSize.Level1)
78 {
79 ScaleOperation to(NUM_D1, NUM_D1, NUM_D1);
80 ScaleOperation from(NUM_D1, NUM_D1, NUM_D1);
81 ScaleOperation result = ScaleOperation::Blend(to, from, PROGRESS);
82 EXPECT_EQ(result, to);
83 }
84
85 /**
86 * @tc.name: TransformUtilTest003
87 * @tc.desc: Test the functions of the class SkewOperation.
88 * @tc.type: FUNC
89 */
90 HWTEST_F(TransformUtilTest, TransformUtilTest003, TestSize.Level1)
91 {
92 SkewOperation to(NUM_D1, NUM_D1);
93 SkewOperation from(NUM_D1, NUM_D1);
94 SkewOperation result = SkewOperation::Blend(to, from, PROGRESS);
95 EXPECT_EQ(result, to);
96 }
97
98 /**
99 * @tc.name: TransformUtilTest004
100 * @tc.desc: Test the functions of the class RotateOperation.
101 * @tc.type: FUNC
102 */
103 HWTEST_F(TransformUtilTest, TransformUtilTest004, TestSize.Level1)
104 {
105 RotateOperation to(NUM_D1, NUM_D1, NUM_D1, NUM_D1);
106 RotateOperation from(NUM_D1, NUM_D1, NUM_D1, NUM_D1);
107 RotateOperation result = RotateOperation::Blend(to, from, PROGRESS);
108 EXPECT_EQ(result, to);
109
110 to = RotateOperation(NUM_D2, NUM_D2, NUM_D2, NUM_D1);
111 from = RotateOperation(NUM_D1, NUM_D1, NUM_D1, NUM_D1);
112 result = RotateOperation::Blend(to, from, PROGRESS);
113 EXPECT_EQ(result, from);
114 }
115
116 /**
117 * @tc.name: TransformUtilTest005
118 * @tc.desc: Test the functions of the class PerspectiveOperation.
119 * @tc.type: FUNC
120 */
121 HWTEST_F(TransformUtilTest, TransformUtilTest005, TestSize.Level1)
122 {
123 Dimension dimension(NUM_D1);
124 PerspectiveOperation to(dimension);
125 PerspectiveOperation from(dimension);
126 PerspectiveOperation result = PerspectiveOperation::Blend(to, from, PROGRESS);
127 EXPECT_EQ(result, to);
128 }
129
130 /**
131 * @tc.name: TransformUtilTest006
132 * @tc.desc: Test the functions of the class TransformOperation.
133 * @tc.type: FUNC
134 */
135 HWTEST_F(TransformUtilTest, TransformUtilTest006, TestSize.Level1)
136 {
137 TransformOperation result;
138 TransformOperation to = TransformOperation::Create(TransformOperationType::UNDEFINED);
139 TransformOperation from = TransformOperation::Create(TransformOperationType::UNDEFINED);
140 result = TransformOperation::Blend(to, from, PROGRESS);
141 EXPECT_EQ(result.matrix4_, Matrix4::CreateIdentity());
142 from = TransformOperation::Create(TransformOperationType::TRANSLATE);
143 result = TransformOperation::Blend(to, from, PROGRESS);
144 EXPECT_EQ(result.type_, TransformOperationType::TRANSLATE);
145 EXPECT_EQ(result.translateOperation_, TranslateOperation());
146 result = TransformOperation::Blend(from, to, PROGRESS);
147 EXPECT_EQ(result.type_, TransformOperationType::TRANSLATE);
148 EXPECT_EQ(result.translateOperation_, TranslateOperation());
149 result = TransformOperation::Blend(from, from, PROGRESS);
150 EXPECT_EQ(result.type_, TransformOperationType::TRANSLATE);
151 EXPECT_EQ(result.translateOperation_, TranslateOperation());
152
153 from = TransformOperation::Create(TransformOperationType::SCALE);
154 result = TransformOperation::Blend(to, from, PROGRESS);
155 EXPECT_EQ(result.type_, TransformOperationType::SCALE);
156 EXPECT_EQ(result.scaleOperation_, ScaleOperation());
157
158 from = TransformOperation::Create(TransformOperationType::SKEW);
159 result = TransformOperation::Blend(to, from, PROGRESS);
160 EXPECT_EQ(result.type_, TransformOperationType::SKEW);
161 EXPECT_EQ(result.skewOperation_, SkewOperation());
162
163 from = TransformOperation::Create(TransformOperationType::ROTATE);
164 result = TransformOperation::Blend(to, from, PROGRESS);
165 EXPECT_EQ(result.type_, TransformOperationType::ROTATE);
166 EXPECT_EQ(result.rotateOperation_, RotateOperation());
167
168 from = TransformOperation::Create(TransformOperationType::MATRIX);
169 result = TransformOperation::Blend(to, from, PROGRESS);
170 EXPECT_EQ(result.type_, TransformOperationType::MATRIX);
171 EXPECT_EQ(result.matrix4_, Matrix4::CreateIdentity());
172
173 from = TransformOperation::Create(TransformOperationType::PERSPECTIVE);
174 result = TransformOperation::Blend(to, from, PROGRESS);
175 EXPECT_EQ(result.type_, TransformOperationType::PERSPECTIVE);
176 EXPECT_EQ(result.perspectiveOperation_, PerspectiveOperation());
177 }
178
179 /**
180 * @tc.name: TransformUtilTest007
181 * @tc.desc: Test the function ToString of the class DecomposedTransform.
182 * @tc.type: FUNC
183 */
184 HWTEST_F(TransformUtilTest, TransformUtilTest007, TestSize.Level1)
185 {
186 DecomposedTransform decomposedTransform;
187 std::string resStr;
188 resStr.append("translate: ")
189 .append(std::to_string(0.0f))
190 .append(" ")
191 .append(std::to_string(0.0f))
192 .append(" ")
193 .append(std::to_string(0.0f))
194 .append("\n")
195 .append("scale: ")
196 .append(std::to_string(1.0f))
197 .append(" ")
198 .append(std::to_string(1.0f))
199 .append(" ")
200 .append(std::to_string(1.0f))
201 .append("\n")
202 .append("skew: ")
203 .append(std::to_string(0.0f))
204 .append(" ")
205 .append(std::to_string(0.0f))
206 .append(" ")
207 .append(std::to_string(0.0f))
208 .append("\n")
209 .append("perspective: ")
210 .append(std::to_string(0.0f))
211 .append(" ")
212 .append(std::to_string(0.0f))
213 .append(" ")
214 .append(std::to_string(0.0f))
215 .append(" ")
216 .append(std::to_string(1.0f))
217 .append("\n")
218 .append("quaternion: ")
219 .append(std::to_string(0.0))
220 .append(" ")
221 .append(std::to_string(0.0))
222 .append(" ")
223 .append(std::to_string(0.0))
224 .append(" ")
225 .append(std::to_string(0.0))
226 .append("\n");
227 EXPECT_EQ(decomposedTransform.ToString(), resStr);
228 }
229
230 /**
231 * @tc.name: TransformUtilTest008
232 * @tc.desc: Test the functions of the class TransformOperations.
233 * @tc.type: FUNC
234 */
235 HWTEST_F(TransformUtilTest, TransformUtilTest008, TestSize.Level1)
236 {
237 std::vector<TransformOperation> operations;
238 operations.push_back(TransformOperation::Create(TransformOperationType::TRANSLATE));
239 operations.push_back(TransformOperation::Create(TransformOperationType::SCALE));
240 operations.push_back(TransformOperation::Create(TransformOperationType::SKEW));
241 operations.push_back(TransformOperation::Create(TransformOperationType::ROTATE));
242 operations.push_back(TransformOperation::Create(TransformOperationType::PERSPECTIVE));
243 operations.push_back(TransformOperation::Create(TransformOperationType::MATRIX));
244 operations.push_back(TransformOperation::Create(TransformOperationType::UNDEFINED));
245
246 TransformOperations::ParseOperationsToMatrix(operations);
247 EXPECT_EQ(operations[0].type_, TransformOperationType::TRANSLATE);
248 EXPECT_EQ(operations[0].translateOperation_, TranslateOperation());
249 EXPECT_EQ(operations[0].matrix4_, Matrix4::CreateTranslate(NUM_D2, NUM_D2, NUM_D2));
250 EXPECT_EQ(operations[1].type_, TransformOperationType::SCALE);
251 EXPECT_EQ(operations[1].scaleOperation_, ScaleOperation());
252 EXPECT_EQ(operations[1].matrix4_, Matrix4::CreateScale(NUM_D1, NUM_D1, NUM_D1));
253 EXPECT_EQ(operations[2].type_, TransformOperationType::SKEW);
254 EXPECT_EQ(operations[2].skewOperation_, SkewOperation());
255 EXPECT_EQ(operations[2].matrix4_, Matrix4::CreateSkew(NUM_D2, NUM_D2));
256 EXPECT_EQ(operations[3].type_, TransformOperationType::ROTATE);
257 EXPECT_EQ(operations[3].rotateOperation_, RotateOperation());
258 EXPECT_EQ(operations[3].matrix4_, Matrix4::CreateRotate(NUM_D2, NUM_D2, NUM_D2, NUM_D2));
259 EXPECT_EQ(operations[4].type_, TransformOperationType::PERSPECTIVE);
260 EXPECT_EQ(operations[4].perspectiveOperation_, PerspectiveOperation());
261 EXPECT_EQ(operations[4].matrix4_, Matrix4::CreatePerspective(NUM_D2));
262 EXPECT_EQ(operations[5].type_, TransformOperationType::MATRIX);
263 EXPECT_EQ(operations[5].matrix4_, Matrix4::CreateIdentity());
264 EXPECT_EQ(operations[6].type_, TransformOperationType::UNDEFINED);
265 EXPECT_EQ(operations[6].matrix4_, Matrix4::CreateIdentity());
266
267 TransformOperations to(operations);
268 TransformOperations from(operations);
269 TransformOperations result = TransformOperations::Blend(to, from, PROGRESS);
270 ASSERT_EQ(result.GetOperations().size(), operations.size());
271 EXPECT_EQ(result.GetOperations()[0].translateOperation_, TranslateOperation());
272 EXPECT_EQ(result.GetOperations()[1].scaleOperation_, ScaleOperation());
273 EXPECT_EQ(result.GetOperations()[2].skewOperation_, SkewOperation());
274 EXPECT_EQ(result.GetOperations()[3].rotateOperation_, RotateOperation());
275 EXPECT_EQ(result.GetOperations()[4].perspectiveOperation_, PerspectiveOperation());
276 }
277 } // namespace OHOS::Ace