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, Hardware
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 "utils/matrix.h"
19 #include "utils/point.h"
20 #include "utils/scalar.h"
21
22 using namespace testing;
23 using namespace testing::ext;
24
25 namespace OHOS {
26 namespace Rosen {
27 namespace Drawing {
28 class MatrixTest : public testing::Test {
29 public:
30 static void SetUpTestCase();
31 static void TearDownTestCase();
32 void SetUp() override;
33 void TearDown() override;
34 };
35
SetUpTestCase()36 void MatrixTest::SetUpTestCase() {}
TearDownTestCase()37 void MatrixTest::TearDownTestCase() {}
SetUp()38 void MatrixTest::SetUp() {}
TearDown()39 void MatrixTest::TearDown() {}
40
41 /**
42 * @tc.name: CreateAndDestroy001
43 * @tc.desc:
44 * @tc.type: FUNC
45 * @tc.require:AR000GGNV3
46 * @tc.author:
47 */
48 HWTEST_F(MatrixTest, CreateAndDestroy001, TestSize.Level1)
49 {
50 // The best way to create Matrix.
51 std::unique_ptr<Matrix> matrix = std::make_unique<Matrix>();
52 ASSERT_TRUE(matrix != nullptr);
53 }
54
55 /**
56 * @tc.name: CreateAndDestroy002
57 * @tc.desc:
58 * @tc.type: FUNC
59 * @tc.require:AR000GGNV3
60 * @tc.author:
61 */
62 HWTEST_F(MatrixTest, CreateAndDestroy002, TestSize.Level1)
63 {
64 // The best way to create Matrix.
65 Matrix matrix1;
66 Matrix matrix2;
67 ASSERT_TRUE(matrix1.Invert(matrix2));
68 }
69
70 /**
71 * @tc.name: MatrixRotateTest001
72 * @tc.desc:
73 * @tc.type: FUNC
74 * @tc.require:AR000GGNV3
75 * @tc.author:
76 */
77 HWTEST_F(MatrixTest, MatrixRotateTest001, TestSize.Level1)
78 {
79 std::unique_ptr<Matrix> matrix = std::make_unique<Matrix>();
80 ASSERT_TRUE(matrix != nullptr);
81 matrix->Rotate(0.1f, 150.1f, 650.9f);
82 }
83
84 /**
85 * @tc.name: MatrixRotateTest002
86 * @tc.desc:
87 * @tc.type: FUNC
88 * @tc.require:AR000GGNV3
89 * @tc.author:
90 */
91 HWTEST_F(MatrixTest, MatrixRotateTest002, TestSize.Level1)
92 {
93 std::unique_ptr<Matrix> matrix = std::make_unique<Matrix>();
94 ASSERT_TRUE(matrix != nullptr);
95 matrix->Rotate(20.8f, 133.0f, 100);
96 }
97
98 /**
99 * @tc.name: MatrixTranslateTest001
100 * @tc.desc:
101 * @tc.type: FUNC
102 * @tc.require:AR000GGNV3
103 * @tc.author:
104 */
105 HWTEST_F(MatrixTest, MatrixTranslateTest001, TestSize.Level1)
106 {
107 std::unique_ptr<Matrix> matrix = std::make_unique<Matrix>();
108 ASSERT_TRUE(matrix != nullptr);
109 matrix->Translate(20.8f, 100);
110 }
111
112 /**
113 * @tc.name: MatrixTranslateTest002
114 * @tc.desc:
115 * @tc.type: FUNC
116 * @tc.require:AR000GGNV3
117 * @tc.author:
118 */
119 HWTEST_F(MatrixTest, MatrixTranslateTest002, TestSize.Level1)
120 {
121 std::unique_ptr<Matrix> matrix = std::make_unique<Matrix>();
122 ASSERT_TRUE(matrix != nullptr);
123 matrix->Translate(77.7f, 190.2f);
124 }
125
126 /**
127 * @tc.name: MatrixScaleTest001
128 * @tc.desc:
129 * @tc.type: FUNC
130 * @tc.require:AR000GGNV3
131 * @tc.author:
132 */
133 HWTEST_F(MatrixTest, MatrixScaleTest001, TestSize.Level1)
134 {
135 std::unique_ptr<Matrix> matrix = std::make_unique<Matrix>();
136 ASSERT_TRUE(matrix != nullptr);
137 matrix->Scale(32.1f, 10.6f, 800, 90.1f);
138 }
139
140 /**
141 * @tc.name: MatrixScaleTest002
142 * @tc.desc:
143 * @tc.type: FUNC
144 * @tc.require:AR000GGNV3
145 * @tc.author:
146 */
147 HWTEST_F(MatrixTest, MatrixScaleTest002, TestSize.Level1)
148 {
149 std::unique_ptr<Matrix> matrix = std::make_unique<Matrix>();
150 ASSERT_TRUE(matrix != nullptr);
151 matrix->Scale(16.5f, 50.6f, 150.8f, 560.9f);
152 }
153
154 /**
155 * @tc.name: MatrixPreRotateTest001
156 * @tc.desc: test for seting Matrix to Matrix multiplied by Matrix constructed from rotating by degrees.
157 * @tc.type: FUNC
158 * @tc.require: I73WU0
159 */
160 HWTEST_F(MatrixTest, MatrixPreRotateTest001, TestSize.Level1)
161 {
162 std::unique_ptr<Matrix> matrix = std::make_unique<Matrix>();
163 ASSERT_TRUE(matrix != nullptr);
164 matrix->PreRotate(0.1f);
165 }
166
167 /**
168 * @tc.name: MatrixPreTranslateTest001
169 * @tc.desc: test for seting Matrix to Matrix constructed from translation (dx, dy) multiplied by Matrix.
170 * @tc.type: FUNC
171 * @tc.require: I73WU0
172 */
173 HWTEST_F(MatrixTest, MatrixPreTranslateTest001, TestSize.Level1)
174 {
175 std::unique_ptr<Matrix> matrix = std::make_unique<Matrix>();
176 ASSERT_TRUE(matrix != nullptr);
177 matrix->PreTranslate(20.8f, 100);
178 }
179
180 /**
181 * @tc.name: MatrixPreTranslateTest002
182 * @tc.desc: test for sets Matrix to Matrix constructed from translation (dx, dy) multiplied by Matrix.
183 * @tc.type: FUNC
184 * @tc.require: I73WU0
185 */
186 HWTEST_F(MatrixTest, MatrixPreTranslateTest002, TestSize.Level1)
187 {
188 std::unique_ptr<Matrix> matrix = std::make_unique<Matrix>();
189 ASSERT_TRUE(matrix != nullptr);
190 matrix->PreTranslate(77.7f, 190.2f);
191 }
192
193 /**
194 * @tc.name: MatrixPreScaleTest001
195 * @tc.desc: test for seting Matrix to Matrix multiplied by Matrix constructed from scaling by (sx, sy).
196 * @tc.type: FUNC
197 * @tc.require: I73WU0
198 */
199 HWTEST_F(MatrixTest, MatrixPreScaleTest001, TestSize.Level1)
200 {
201 std::unique_ptr<Matrix> matrix = std::make_unique<Matrix>();
202 ASSERT_TRUE(matrix != nullptr);
203 matrix->PreScale(32.1f, 10.6f);
204 }
205
206 /**
207 * @tc.name: MatrixPreScaleTest002
208 * @tc.desc: test for seting Matrix to Matrix multiplied by Matrix constructed from scaling by (sx, sy).
209 * @tc.type: FUNC
210 * @tc.require: I73WU0
211 */
212 HWTEST_F(MatrixTest, MatrixPreScaleTest002, TestSize.Level1)
213 {
214 std::unique_ptr<Matrix> matrix = std::make_unique<Matrix>();
215 ASSERT_TRUE(matrix != nullptr);
216 matrix->PreScale(16.5f, 50.6f);
217 }
218
219 /**
220 * @tc.name: MatrixPreConcatTest001
221 * @tc.desc: test for seting Matrix to Matrix other multiplied by Matrix.
222 * @tc.type: FUNC
223 * @tc.require: I73WU0
224 */
225 HWTEST_F(MatrixTest, MatrixPreConcatTest001, TestSize.Level1)
226 {
227 std::unique_ptr<Matrix> matrix1 = std::make_unique<Matrix>();
228 std::unique_ptr<Matrix> matrix2 = std::make_unique<Matrix>();
229 ASSERT_TRUE(matrix1 != nullptr);
230 ASSERT_TRUE(matrix2 != nullptr);
231 matrix1->PreConcat(*matrix2.get());
232 }
233
234 /**
235 * @tc.name: MatrixMultiplyTest001
236 * @tc.desc:
237 * @tc.type: FUNC
238 * @tc.require:AR000GGNV3
239 * @tc.author:
240 */
241 HWTEST_F(MatrixTest, MatrixMultiplyTest001, TestSize.Level1)
242 {
243 Matrix matrix1;
244 Matrix matrix2;
245 matrix1.Rotate(3.0f, 2.0f, 1.0f);
246 matrix2.Rotate(1.5f, 2.5f, 3.5f);
247 Matrix matrix3 = matrix1 * matrix2;
248 EXPECT_TRUE(matrix3 == matrix1);
249 EXPECT_FALSE(matrix3 == matrix2);
250 }
251
252 /**
253 * @tc.name: MatrixMultiplyTest002
254 * @tc.desc:
255 * @tc.type: FUNC
256 * @tc.require:AR000GGNV3
257 * @tc.author:
258 */
259 HWTEST_F(MatrixTest, MatrixMultiplyTest002, TestSize.Level1)
260 {
261 Matrix matrix1;
262 Matrix matrix2;
263 matrix1.Rotate(3.7f, 6.0f, 1.9f);
264 matrix2.Rotate(5.5f, 12.5f, 30.5f);
265 Matrix matrix3 = matrix1 * matrix2;
266 EXPECT_TRUE(matrix3 == matrix1);
267 EXPECT_FALSE(matrix3 == matrix2);
268 }
269
270 /**
271 * @tc.name: MatrixEqualTest001
272 * @tc.desc:
273 * @tc.type: FUNC
274 * @tc.require:AR000GGNV3
275 * @tc.author:
276 */
277 HWTEST_F(MatrixTest, MatrixEqualTest001, TestSize.Level1)
278 {
279 Matrix matrix1;
280 Matrix matrix2 = matrix1;
281 EXPECT_TRUE(matrix1 == matrix2);
282 }
283
284 /**
285 * @tc.name: MatrixEqualTest002
286 * @tc.desc:
287 * @tc.type: FUNC
288 * @tc.require:AR000GGNV3
289 * @tc.author:
290 */
291 HWTEST_F(MatrixTest, MatrixEqualTest002, TestSize.Level1)
292 {
293 Matrix matrix1;
294 matrix1.Rotate(1.0f, 2.0f, 3.0f);
295 Matrix matrix2;
296 EXPECT_FALSE(matrix1 == matrix2);
297 }
298
299 /**
300 * @tc.name: MatrixSetMatrixTest001
301 * @tc.desc:
302 * @tc.type: FUNC
303 * @tc.require:AR000GGNV3
304 * @tc.author:
305 */
306 HWTEST_F(MatrixTest, MatrixSetMatrixTest001, TestSize.Level1)
307 {
308 std::unique_ptr<Matrix> matrix = std::make_unique<Matrix>();
309 ASSERT_TRUE(matrix != nullptr);
310 matrix->SetMatrix(200, 150, 800, 60, 200, 150, 800, 60, 90);
311 }
312
313 /**
314 * @tc.name: MatrixSetMatrixTest002
315 * @tc.desc:
316 * @tc.type: FUNC
317 * @tc.require:AR000GGNV3
318 * @tc.author:
319 */
320 HWTEST_F(MatrixTest, MatrixSetMatrixTest002, TestSize.Level1)
321 {
322 std::unique_ptr<Matrix> matrix = std::make_unique<Matrix>();
323 ASSERT_TRUE(matrix != nullptr);
324 matrix->SetMatrix(20.9f, 15.8f, 80.8f, 60, 2.4f, 99.9f, 60, 60, 900);
325 }
326
327 /**
328 * @tc.name: MatrixSetMatrixTest003
329 * @tc.desc:
330 * @tc.type: FUNC
331 * @tc.require:AR000GGNV3
332 * @tc.author:
333 */
334 HWTEST_F(MatrixTest, MatrixSetMatrixTest003, TestSize.Level1)
335 {
336 std::unique_ptr<Matrix> matrix = std::make_unique<Matrix>();
337 ASSERT_TRUE(matrix != nullptr);
338 matrix->SetMatrix(20.9f, 15.8f, 80.8f, 60.6f, 2.4f, 99.9f, 60.5f, 60.1f, 90.5f);
339 }
340
341 /**
342 * @tc.name: MatrixMapPointsTest001
343 * @tc.desc:
344 * @tc.type: FUNC
345 * @tc.require:AR000GGNV3
346 * @tc.author:
347 */
348 HWTEST_F(MatrixTest, MatrixMapPointsTest001, TestSize.Level1)
349 {
350 std::unique_ptr<Matrix> matrix = std::make_unique<Matrix>();
351 ASSERT_TRUE(matrix != nullptr);
352 std::vector<Point> dst = { { 1, 2 } };
353 std::vector<Point> src = { { 2, 3 } };
354 matrix->MapPoints(dst, src, 1);
355 }
356
357 /**
358 * @tc.name: MatrixMapPointsTest002
359 * @tc.desc:
360 * @tc.type: FUNC
361 * @tc.require:AR000GGNV3
362 * @tc.author:
363 */
364 HWTEST_F(MatrixTest, MatrixMapPointsTest002, TestSize.Level1)
365 {
366 std::unique_ptr<Matrix> matrix = std::make_unique<Matrix>();
367 ASSERT_TRUE(matrix != nullptr);
368 std::vector<Point> dst = { { 3, 2 } };
369 std::vector<Point> src = { { 1, 3 } };
370 matrix->MapPoints(dst, src, 1);
371 }
372
373 /**
374 * @tc.name: MatrixMapRectTest001
375 * @tc.desc: test for seting dst to bounds of src corners mapped by Matrix.
376 * @tc.type: FUNC
377 * @tc.require: I73WU0
378 */
379 HWTEST_F(MatrixTest, MatrixMapRectTest001, TestSize.Level1)
380 {
381 std::unique_ptr<Matrix> matrix = std::make_unique<Matrix>();
382 ASSERT_TRUE(matrix != nullptr);
383 RectF dst;
384 RectF src(40, 50, 190, 200);
385 EXPECT_TRUE(matrix->MapRect(dst, src));
386 }
387
388 /**
389 * @tc.name: MatrixMapRectTest002
390 * @tc.desc: test for seting dst to bounds of src corners mapped by Matrix.
391 * @tc.type: FUNC
392 * @tc.require: I73WU0
393 */
394 HWTEST_F(MatrixTest, MatrixMapRectTest002, TestSize.Level1)
395 {
396 std::unique_ptr<Matrix> matrix = std::make_unique<Matrix>();
397 ASSERT_TRUE(matrix != nullptr);
398 RectF dst;
399 RectF src(80, 100, 190, 200);
400 EXPECT_TRUE(matrix->MapRect(dst, src));
401 }
402
403 /**
404 * @tc.name: MatrixSetTest001
405 * @tc.desc: test for seting Matrix value.
406 * @tc.type: FUNC
407 * @tc.require: I73WU0
408 */
409 HWTEST_F(MatrixTest, MatrixSetTest001, TestSize.Level1)
410 {
411 std::unique_ptr<Matrix> matrix = std::make_unique<Matrix>();
412 ASSERT_TRUE(matrix != nullptr);
413 matrix->Set(Matrix::SCALE_X, 20.9f);
414 EXPECT_EQ(matrix->Get(Matrix::SCALE_X), 20.9f);
415 matrix->Set(Matrix::SKEW_X, 15.8f);
416 EXPECT_EQ(matrix->Get(Matrix::SKEW_X), 15.8f);
417 matrix->Set(Matrix::TRANS_X, 80.8f);
418 EXPECT_EQ(matrix->Get(Matrix::TRANS_X), 80.8f);
419 matrix->Set(Matrix::SKEW_Y, 60.6f);
420 EXPECT_EQ(matrix->Get(Matrix::SKEW_Y), 60.6f);
421 matrix->Set(Matrix::SCALE_Y, 2.4f);
422 EXPECT_EQ(matrix->Get(Matrix::SCALE_Y), 2.4f);
423 matrix->Set(Matrix::TRANS_Y, 99.9f);
424 EXPECT_EQ(matrix->Get(Matrix::TRANS_Y), 99.9f);
425 matrix->Set(Matrix::PERSP_0, 60.5f);
426 EXPECT_EQ(matrix->Get(Matrix::PERSP_0), 60.5f);
427 matrix->Set(Matrix::PERSP_1, 60.1f);
428 EXPECT_EQ(matrix->Get(Matrix::PERSP_1), 60.1f);
429 matrix->Set(Matrix::PERSP_2, 90.5f);
430 EXPECT_EQ(matrix->Get(Matrix::PERSP_2), 90.5f);
431 }
432
433 /**
434 * @tc.name: MatrixGetTest001
435 * @tc.desc:
436 * @tc.type: FUNC
437 * @tc.require:AR000GGNV3
438 * @tc.author:
439 */
440 HWTEST_F(MatrixTest, MatrixGetTest001, TestSize.Level1)
441 {
442 std::unique_ptr<Matrix> matrix = std::make_unique<Matrix>();
443 ASSERT_TRUE(matrix != nullptr);
444 ASSERT_EQ(1, matrix->Get(0));
445 }
446
447 /**
448 * @tc.name: GetAll001
449 * @tc.desc: the test copies the nine scalar values contained in the matrix into a buffer.
450 * @tc.type: FUNC
451 * @tc.require: I73WU0
452 */
453 HWTEST_F(MatrixTest, GetAll001, TestSize.Level1)
454 {
455 std::unique_ptr<Matrix> matrix = std::make_unique<Matrix>();
456 ASSERT_TRUE(matrix != nullptr);
457 matrix->SetMatrix(20.9f, 15.8f, 80.8f, 60.6f, 2.4f, 99.9f, 60.5f, 60.1f, 90.5f);
458 Matrix::Buffer buffer;
459 matrix->GetAll(buffer);
460 }
461 } // namespace Drawing
462 } // namespace Rosen
463 } // namespace OHOS
464
465