1/*
2 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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 "DrawingNativeMatrixCommon.h"
17#include "drawing_error_code.h"
18#include "drawing_matrix.h"
19#include "drawing_rect.h"
20#include "utils/scalar.h"
21#include "gtest/gtest.h"
22#include <iostream>
23#include <random>
24
25using namespace testing;
26using namespace testing::ext;
27
28namespace OHOS {
29namespace Rosen {
30namespace Drawing {
31
32/*
33 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_MATRIX_1500
34 * @tc.name: testMatrixPostTranslateNormal
35 * @tc.desc: test for testMatrixPostTranslateNormal.
36 * @tc.size  : SmallTest
37 * @tc.type  : Function
38 * @tc.level : Level 0
39 */
40HWTEST_F(DrawingNativeMatrixTest, testMatrixPostTranslateNormal, TestSize.Level0) {
41    // 1. OH_Drawing_MatrixCreate
42    OH_Drawing_Matrix *matrix = OH_Drawing_MatrixCreate();
43    EXPECT_NE(matrix, nullptr);
44    // 2. OH_Drawing_MatrixPostTranslate, passing decimal numbers
45    OH_Drawing_MatrixPostTranslate(matrix, 1.5, 2.5);
46    // 3. OH_Drawing_MatrixPostTranslate, passing integers
47    OH_Drawing_MatrixPostTranslate(matrix, 3, 4);
48    // 4. Free memory
49    OH_Drawing_MatrixDestroy(matrix);
50}
51
52/*
53 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_MATRIX_1501
54 * @tc.name: testMatrixPostTranslateNull
55 * @tc.desc: test for testMatrixPostTranslateNull.
56 * @tc.size  : SmallTest
57 * @tc.type  : Function
58 * @tc.level : Level 3
59 */
60HWTEST_F(DrawingNativeMatrixTest, testMatrixPostTranslateNull, TestSize.Level3) {
61    // 1. OH_Drawing_MatrixCreate
62    OH_Drawing_Matrix *matrix = OH_Drawing_MatrixCreate();
63    EXPECT_NE(matrix, nullptr);
64    // 2. OH_Drawing_MatrixPostTranslate, passing nullptr as the first parameter, check the error code with
65    // OH_Drawing_ErrorCodeGet, no crash, error code returns OH_DRAWING_ERROR_INVALID_PARAMETER
66    OH_Drawing_MatrixPostTranslate(nullptr, 1.5, 2.5);
67    EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_Drawing_ErrorCode::OH_DRAWING_ERROR_INVALID_PARAMETER);
68    // 3. OH_Drawing_MatrixPostTranslate, passing 0 as the second parameter
69    OH_Drawing_MatrixPostTranslate(matrix, 0, 2.5);
70    // 4. OH_Drawing_MatrixPostTranslate, passing 0 as the third parameter
71    OH_Drawing_MatrixPostTranslate(matrix, 1.5, 0);
72    // 5. Free memory
73    OH_Drawing_MatrixDestroy(matrix);
74}
75
76/*
77 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_MATRIX_1502
78 * @tc.name: testMatrixPostTranslateAbnormal
79 * @tc.desc: test for testMatrixPostTranslateAbnormal.
80 * @tc.size  : SmallTest
81 * @tc.type  : Function
82 * @tc.level : Level 3
83 */
84HWTEST_F(DrawingNativeMatrixTest, testMatrixPostTranslateAbnormal, TestSize.Level3) {
85    // 1. OH_Drawing_MatrixCreate
86    OH_Drawing_Matrix *matrix = OH_Drawing_MatrixCreate();
87    EXPECT_NE(matrix, nullptr);
88    // 2. OH_Drawing_MatrixPostTranslate with a negative value as the second parameter
89    OH_Drawing_MatrixPostTranslate(matrix, -1.5, 2.5);
90    // 3. OH_Drawing_MatrixPostTranslate with a negative value as the third parameter
91    OH_Drawing_MatrixPostTranslate(matrix, 1.5, -2.5);
92    // 4. Free memory
93    OH_Drawing_MatrixDestroy(matrix);
94}
95
96/*
97 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_MATRIX_1503
98 * @tc.name: testMatrixPostTranslateMaximum
99 * @tc.desc: test for testMatrixPostTranslateMaximum.
100 * @tc.size  : SmallTest
101 * @tc.type  : Function
102 * @tc.level : Level 3
103 */
104HWTEST_F(DrawingNativeMatrixTest, testMatrixPostTranslateMaximum, TestSize.Level3) {
105    // 1. OH_Drawing_MatrixCreate
106    OH_Drawing_Matrix *matrix = OH_Drawing_MatrixCreate();
107    EXPECT_NE(matrix, nullptr);
108    // 2. OH_Drawing_MatrixPostTranslate with the second parameter as the maximum value
109    OH_Drawing_MatrixPostTranslate(matrix, FLT_MAX, 2.5);
110    // 3. OH_Drawing_MatrixPostTranslate with the third parameter as the maximum value
111    OH_Drawing_MatrixPostTranslate(matrix, 1.5, FLT_MAX);
112    // 4. Free memory
113    OH_Drawing_MatrixDestroy(matrix);
114}
115
116/*
117 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_MATRIX_1504
118 * @tc.name: testMatrixPostTranslateMultipleCalls
119 * @tc.desc: test for testMatrixPostTranslateMultipleCalls.
120 * @tc.size  : SmallTest
121 * @tc.type  : Function
122 * @tc.level : Level 3
123 */
124HWTEST_F(DrawingNativeMatrixTest, testMatrixPostTranslateMultipleCalls, TestSize.Level3) {
125    // 1. OH_Drawing_MatrixCreate
126    OH_Drawing_Matrix *matrix = OH_Drawing_MatrixCreate();
127    EXPECT_NE(matrix, nullptr);
128    // 2. Call OH_Drawing_MatrixPostTranslate 10 times, with dx and dy as random numbers
129    std::random_device rd;
130    std::mt19937 gen(rd());
131    std::uniform_real_distribution<float> dis(-100, 100);
132    for (int i = 0; i < 10; i++) {
133        OH_Drawing_MatrixPostTranslate(matrix, dis(gen), dis(gen));
134    }
135    // 3. Free memory
136    OH_Drawing_MatrixDestroy(matrix);
137}
138
139/*
140 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_MATRIX_1600
141 * @tc.name: testMatrixResetNormal
142 * @tc.desc: test for testMatrixResetNormal.
143 * @tc.size  : SmallTest
144 * @tc.type  : Function
145 * @tc.level : Level 0
146 */
147HWTEST_F(DrawingNativeMatrixTest, testMatrixResetNormal, TestSize.Level0) {
148    // 1. OH_Drawing_MatrixCreate
149    OH_Drawing_Matrix *matrix = OH_Drawing_MatrixCreate();
150    // 2. OH_Drawing_MatrixReset with the identity matrix
151    OH_Drawing_MatrixReset(matrix);
152    // 3. OH_Drawing_MatrixReset with a non-identity matrix
153    OH_Drawing_MatrixSetMatrix(matrix, 2, 1, 3, 1, 2, 2, 3, 1, 1);
154    OH_Drawing_MatrixReset(matrix);
155    // 4. Free memory
156    OH_Drawing_MatrixDestroy(matrix);
157}
158
159/*
160 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_MATRIX_1601
161 * @tc.name: testMatrixResetNull
162 * @tc.desc: test for testMatrixResetNull.
163 * @tc.size  : SmallTest
164 * @tc.type  : Function
165 * @tc.level : Level 3
166 */
167HWTEST_F(DrawingNativeMatrixTest, testMatrixResetNull, TestSize.Level3) {
168    // 1. OH_Drawing_MatrixReset with nullptr as the parameter, check the error code with OH_Drawing_ErrorCodeGet, no
169    // crash, error code returns OH_DRAWING_ERROR_INVALID_PARAMETER
170    OH_Drawing_MatrixReset(nullptr);
171    EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_Drawing_ErrorCode::OH_DRAWING_ERROR_INVALID_PARAMETER);
172}
173
174/*
175 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_MATRIX_1602
176 * @tc.name: testMatrixResetMultipleCalls
177 * @tc.desc: test for testMatrixResetMultipleCalls.
178 * @tc.size  : SmallTest
179 * @tc.type  : Function
180 * @tc.level : Level 3
181 */
182HWTEST_F(DrawingNativeMatrixTest, testMatrixResetMultipleCalls, TestSize.Level3) {
183    // 1. OH_Drawing_MatrixCreate
184    OH_Drawing_Matrix *matrix = OH_Drawing_MatrixCreate();
185    // 2. Call OH_Drawing_MatrixSetMatrix 10 times
186    for (int i = 0; i < 10; i++) {
187        OH_Drawing_MatrixSetMatrix(matrix, 2, 1, 3, 1, 2, 2, 3, 1, 1);
188    }
189    // 3. Call OH_Drawing_MatrixReset 10 times
190    for (int i = 0; i < 10; i++) {
191        OH_Drawing_MatrixReset(matrix);
192    }
193    // 4. Call OH_Drawing_MatrixSetMatrix and OH_Drawing_MatrixReset alternately 10 times
194    for (int i = 0; i < 10; i++) {
195        OH_Drawing_MatrixSetMatrix(matrix, 2, 1, 3, 1, 2, 2, 3, 1, 1);
196        OH_Drawing_MatrixReset(matrix);
197    }
198    // 5. Free memory
199    OH_Drawing_MatrixDestroy(matrix);
200}
201
202/*
203 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_MATRIX_1700
204 * @tc.name: testMatrixConcatNormal
205 * @tc.desc: test for testMatrixConcatNormal.
206 * @tc.size  : SmallTest
207 * @tc.type  : Function
208 * @tc.level : Level 0
209 */
210HWTEST_F(DrawingNativeMatrixTest, testMatrixConcatNormal, TestSize.Level0) {
211    // Define matrices a and b
212    OH_Drawing_Matrix *a = OH_Drawing_MatrixCreate();
213    OH_Drawing_MatrixSetMatrix(a, 1, 0, 0, 0, -1, 0, 0, 0, 1);
214    OH_Drawing_Matrix *b = OH_Drawing_MatrixCreate();
215    OH_Drawing_MatrixSetMatrix(b, 1, 0, 0, 0, 1, 0, 0, 0, 1);
216    OH_Drawing_Matrix *c = OH_Drawing_MatrixCreate();
217    // 1. Call OH_Drawing_MatrixConcat with matrices a and b of different sizes,
218    // and use OH_Drawing_MatrixGetAll to get the result of matrix a multiplied by matrix b
219    OH_Drawing_MatrixConcat(c, b, a);
220    float values[9];
221    OH_Drawing_MatrixGetAll(c, values);
222    EXPECT_EQ(values[0], 1);
223    // 2. Free memory
224    OH_Drawing_MatrixDestroy(a);
225    OH_Drawing_MatrixDestroy(b);
226    OH_Drawing_MatrixDestroy(c);
227}
228
229/*
230 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_MATRIX_1701
231 * @tc.name: testMatrixConcatNull
232 * @tc.desc: test for testMatrixConcatNull.
233 * @tc.size  : SmallTest
234 * @tc.type  : Function
235 * @tc.level : Level 3
236 */
237HWTEST_F(DrawingNativeMatrixTest, testMatrixConcatNull, TestSize.Level3) {
238    // 1. OH_Drawing_MatrixCreate
239    OH_Drawing_Matrix *matrix = OH_Drawing_MatrixCreate();
240    // 2. OH_Drawing_MatrixConcat, passing nullptr as the first parameter, check the error code with
241    // OH_Drawing_ErrorCodeGet
242    OH_Drawing_MatrixConcat(nullptr, matrix, matrix);
243    EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_Drawing_ErrorCode::OH_DRAWING_ERROR_INVALID_PARAMETER);
244    // 3. OH_Drawing_MatrixConcat, passing nullptr as the second parameter, check the error code with
245    // OH_Drawing_ErrorCodeGet
246    OH_Drawing_MatrixConcat(matrix, nullptr, matrix);
247    EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_Drawing_ErrorCode::OH_DRAWING_ERROR_INVALID_PARAMETER);
248    // 4. OH_Drawing_MatrixConcat, passing nullptr as the third parameter, check the error code with
249    // OH_Drawing_ErrorCodeGet
250    OH_Drawing_MatrixConcat(matrix, matrix, nullptr);
251    EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_Drawing_ErrorCode::OH_DRAWING_ERROR_INVALID_PARAMETER);
252    // 5. Free memory
253    OH_Drawing_MatrixDestroy(matrix);
254}
255
256/*
257 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_MATRIX_1702
258 * @tc.name: testMatrixConcatMultipleCalls
259 * @tc.desc: test for testMatrixConcatMultipleCalls.
260 * @tc.size  : SmallTest
261 * @tc.type  : Function
262 * @tc.level : Level 3
263 */
264HWTEST_F(DrawingNativeMatrixTest, testMatrixConcatMultipleCalls, TestSize.Level3) {
265    // 1. Call OH_Drawing_MatrixConcat 10 times with matrices a and b of different sizes,
266    // and use OH_Drawing_MatrixGetAll to get the result of matrix a multiplied by matrix b
267    OH_Drawing_Matrix *a = OH_Drawing_MatrixCreate();
268    OH_Drawing_MatrixSetMatrix(a, 1, 0, 0, 0, -1, 0, 0, 0, 1);
269    OH_Drawing_Matrix *b = OH_Drawing_MatrixCreate();
270    OH_Drawing_MatrixSetMatrix(b, 1, 0, 0, 0, 1, 0, 0, 0, 1);
271    OH_Drawing_Matrix *c = OH_Drawing_MatrixCreate();
272    for (int i = 0; i < 10; i++) {
273        OH_Drawing_MatrixConcat(c, b, a);
274        float values[9];
275        OH_Drawing_MatrixGetAll(c, values);
276        EXPECT_EQ(values[0], 1);
277        EXPECT_EQ(values[1], 0);
278        EXPECT_EQ(values[2], 0);
279        EXPECT_EQ(values[3], 0);
280        EXPECT_EQ(values[4], -1);
281        EXPECT_EQ(values[5], 0);
282        EXPECT_EQ(values[6], 0);
283        EXPECT_EQ(values[7], 0);
284        EXPECT_EQ(values[8], 1);
285    }
286    // 2. Free memory
287    OH_Drawing_MatrixDestroy(a);
288    OH_Drawing_MatrixDestroy(b);
289    OH_Drawing_MatrixDestroy(c);
290}
291
292/*
293 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_MATRIX_1800
294 * @tc.name: testMatrixInvertNormal
295 * @tc.desc: test for testMatrixInvertNormal.
296 * @tc.size  : SmallTest
297 * @tc.type  : Function
298 * @tc.level : Level 0
299 */
300HWTEST_F(DrawingNativeMatrixTest, testMatrixInvertNormal, TestSize.Level0) {
301    OH_Drawing_Matrix *matrix = OH_Drawing_MatrixCreate();
302    OH_Drawing_Matrix *inverse = OH_Drawing_MatrixCreate();
303    OH_Drawing_MatrixInvert(matrix, inverse);
304    OH_Drawing_MatrixInvert(inverse, matrix);
305    OH_Drawing_MatrixDestroy(matrix);
306    OH_Drawing_MatrixDestroy(inverse);
307}
308
309/*
310 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_MATRIX_1801
311 * @tc.name: testMatrixInvertNull
312 * @tc.desc: test for testMatrixInvertNull.
313 * @tc.size  : SmallTest
314 * @tc.type  : Function
315 * @tc.level : Level 3
316 */
317HWTEST_F(DrawingNativeMatrixTest, testMatrixInvertNull, TestSize.Level3) {
318    // 1. OH_Drawing_MatrixCreate
319    OH_Drawing_Matrix *matrix = OH_Drawing_MatrixCreate();
320    // 2. OH_Drawing_MatrixInvert with the first parameter as nullptr, check the error code with OH_Drawing_ErrorCodeGet
321    OH_Drawing_MatrixInvert(nullptr, matrix);
322    EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_Drawing_ErrorCode::OH_DRAWING_ERROR_INVALID_PARAMETER);
323    // 3. OH_Drawing_MatrixInvert with the second parameter as nullptr, check the error code with
324    // OH_Drawing_ErrorCodeGet
325    OH_Drawing_MatrixInvert(matrix, nullptr);
326    EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_Drawing_ErrorCode::OH_DRAWING_ERROR_INVALID_PARAMETER);
327    // 4. Free memory
328    OH_Drawing_MatrixDestroy(matrix);
329}
330
331/*
332 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_MATRIX_1802
333 * @tc.name: testMatrixInvertMultipleCalls
334 * @tc.desc: test for testMatrixInvertMultipleCalls.
335 * @tc.size  : SmallTest
336 * @tc.type  : Function
337 * @tc.level : Level 3
338 */
339HWTEST_F(DrawingNativeMatrixTest, testMatrixInvertMultipleCalls, TestSize.Level3) {
340    // 1. Call OH_Drawing_MatrixInvert 10 times with matrices of different sizes
341    OH_Drawing_Matrix *matrix = OH_Drawing_MatrixCreate();
342    OH_Drawing_Matrix *inverse = OH_Drawing_MatrixCreate();
343    OH_Drawing_MatrixSetMatrix(matrix, 1, 0, 0, 0, -1, 0, 0, 0, 1);
344    for (int i = 0; i < 10; i++) {
345        OH_Drawing_MatrixInvert(matrix, inverse);
346        OH_Drawing_MatrixInvert(inverse, matrix);
347        float values[9];
348        OH_Drawing_MatrixGetAll(matrix, values);
349        EXPECT_EQ(values[0], 1);
350        EXPECT_EQ(values[1], 0);
351        EXPECT_EQ(values[2], 0);
352        EXPECT_EQ(values[3], 0);
353        EXPECT_EQ(values[4], -1);
354        EXPECT_EQ(values[5], 0);
355        EXPECT_EQ(values[6], 0);
356        EXPECT_EQ(values[7], 0);
357        EXPECT_EQ(values[8], 1);
358    }
359    // 2. Free memory
360    OH_Drawing_MatrixDestroy(matrix);
361    OH_Drawing_MatrixDestroy(inverse);
362}
363
364/*
365 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_MATRIX_1900
366 * @tc.name: testMatrixSetPolyToPolyNormal
367 * @tc.desc: test for testMatrixSetPolyToPolyNormal.
368 * @tc.size  : SmallTest
369 * @tc.type  : Function
370 * @tc.level : Level 0
371 */
372HWTEST_F(DrawingNativeMatrixTest, testMatrixSetPolyToPolyNormal, TestSize.Level0) {
373    // 1. OH_Drawing_MatrixCreate
374    OH_Drawing_Matrix *matrix = OH_Drawing_MatrixCreate();
375    EXPECT_NE(matrix, nullptr);
376    // 2. OH_Drawing_MatrixSetPolyToPoly
377    OH_Drawing_MatrixSetMatrix(matrix, 1, 0, 0, 0, -1, 0, 0, 0, 1);
378    OH_Drawing_Point2D src[] = {{0, 0}, {100, 0}, {100, 100}, {0, 100}, {0, 100}};
379    OH_Drawing_Point2D dst[] = {{0, 0}, {100, 30}, {100, 70}, {0, 100}, {0, 100}};
380    OH_Drawing_MatrixSetPolyToPoly(matrix, src, dst, 1);
381    // 3. OH_Drawing_MatrixSetPolyToPoly, iterate count from 0 to 4, keeping the length of the array consistent with
382    // count
383    std::random_device rd;
384    std::mt19937 gen(rd());
385    std::uniform_real_distribution<float> dis(0, 100);
386    for (int i = 0; i < 5; i++) {
387        OH_Drawing_Point2D src[i];
388        OH_Drawing_Point2D dst[i];
389        for (int j = 0; j < i; j++) {
390            // Generate random numbers
391            src[j] = {dis(gen), dis(gen)};
392            dst[j] = {dis(gen), dis(gen)};
393        }
394        OH_Drawing_MatrixSetPolyToPoly(matrix, src, dst, i);
395    }
396    // 4. Free memory
397    OH_Drawing_MatrixDestroy(matrix);
398}
399
400/*
401 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_MATRIX_1901
402 * @tc.name: testMatrixSetPolyToPolyNull
403 * @tc.desc: test for testMatrixSetPolyToPolyNull.
404 * @tc.size  : SmallTest
405 * @tc.type  : Function
406 * @tc.level : Level 3
407 */
408HWTEST_F(DrawingNativeMatrixTest, testMatrixSetPolyToPolyNull, TestSize.Level3) {
409    // 1. OH_Drawing_MatrixCreate
410    OH_Drawing_Matrix *matrix = OH_Drawing_MatrixCreate();
411    // 2. OH_Drawing_MatrixSetPolyToPoly, the first parameter is nullptr, check the error code with
412    // OH_Drawing_ErrorCodeGet
413    OH_Drawing_Point2D src[] = {{0, 0}, {100, 0}, {100, 100}, {0, 100}, {0, 100}};
414    OH_Drawing_Point2D dst[] = {{0, 0}, {100, 30}, {100, 70}, {0, 100}, {0, 100}};
415    OH_Drawing_MatrixSetPolyToPoly(nullptr, src, dst, 5);
416    EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_Drawing_ErrorCode::OH_DRAWING_ERROR_INVALID_PARAMETER);
417    // 3. OH_Drawing_MatrixSetPolyToPoly, the second parameter is nullptr, check the error code with
418    // OH_Drawing_ErrorCodeGet
419    OH_Drawing_MatrixSetPolyToPoly(matrix, nullptr, dst, 5);
420    EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_Drawing_ErrorCode::OH_DRAWING_ERROR_PARAMETER_OUT_OF_RANGE);
421    // 4. OH_Drawing_MatrixSetPolyToPoly, the third parameter is nullptr, check the error code with
422    // OH_Drawing_ErrorCodeGet
423    OH_Drawing_MatrixSetPolyToPoly(matrix, src, nullptr, 5);
424    EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_Drawing_ErrorCode::OH_DRAWING_ERROR_PARAMETER_OUT_OF_RANGE);
425    // 5. Free memory
426    OH_Drawing_MatrixDestroy(matrix);
427}
428
429/*
430 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_MATRIX_1902
431 * @tc.name: testMatrixSetPolyToPolyAbnormal
432 * @tc.desc: test for testMatrixSetPolyToPolyAbnormal.
433 * @tc.size  : SmallTest
434 * @tc.type  : Function
435 * @tc.level : Level 3
436 */
437HWTEST_F(DrawingNativeMatrixTest, testMatrixSetPolyToPolyAbnormal, TestSize.Level3) {
438    // 1. OH_Drawing_MatrixCreate
439    OH_Drawing_Matrix *matrix = OH_Drawing_MatrixCreate();
440    // 2. OH_Drawing_MatrixSetPolyToPoly, pass -1 as count, check the error code with OH_Drawing_ErrorCodeGet
441    OH_Drawing_Point2D src[] = {{0, 0}, {100, 0}, {100, 100}, {0, 100}};
442    OH_Drawing_Point2D dst[] = {{0, 0}, {100, 30}, {100, 70}, {0, 100}};
443    OH_Drawing_MatrixSetPolyToPoly(matrix, src, dst, -1);
444    EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_Drawing_ErrorCode::OH_DRAWING_ERROR_PARAMETER_OUT_OF_RANGE);
445    // 3. OH_Drawing_MatrixSetPolyToPoly, pass 5 as count, check the error code with OH_Drawing_ErrorCodeGet
446    OH_Drawing_MatrixSetPolyToPoly(matrix, src, dst, 5);
447    EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_Drawing_ErrorCode::OH_DRAWING_ERROR_PARAMETER_OUT_OF_RANGE);
448    // 4. Free memory
449    OH_Drawing_MatrixDestroy(matrix);
450}
451
452/*
453 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_MATRIX_1904
454 * @tc.name: testMatrixSetPolyToPolyMultipleCalls
455 * @tc.desc: test for testMatrixSetPolyToPolyMultipleCalls.
456 * @tc.size  : SmallTest
457 * @tc.type  : Function
458 * @tc.level : Level 3
459 */
460HWTEST_F(DrawingNativeMatrixTest, testMatrixSetPolyToPolyMultipleCalls, TestSize.Level3) {
461    // 1. OH_Drawing_MatrixCreate
462    OH_Drawing_Matrix *matrix = OH_Drawing_MatrixCreate();
463    // 2. Call OH_Drawing_MatrixSetPolyToPoly 10 times
464    std::random_device rd;
465    std::mt19937 gen(rd());
466    std::uniform_real_distribution<float> dis(0, 100);
467    for (int i = 0; i < 10; i++) {
468        OH_Drawing_Point2D src[2] = {{dis(gen), dis(gen)}, {dis(gen), dis(gen)}};
469        OH_Drawing_Point2D dst[2] = {{dis(gen), dis(gen)}, {dis(gen), dis(gen)}};
470        OH_Drawing_MatrixSetPolyToPoly(matrix, src, dst, 2);
471    }
472    // 3. Free memory
473    OH_Drawing_MatrixDestroy(matrix);
474}
475
476/*
477 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_MATRIX_2000
478 * @tc.name: testMatrixMapPointsNormal
479 * @tc.desc: test for testMatrixMapPointsNormal.
480 * @tc.size  : SmallTest
481 * @tc.type  : Function
482 * @tc.level : Level 0
483 */
484HWTEST_F(DrawingNativeMatrixTest, testMatrixMapPointsNormal, TestSize.Level0) {
485    // 1. OH_Drawing_MatrixCreate
486    OH_Drawing_Matrix *matrix = OH_Drawing_MatrixCreate();
487    OH_Drawing_Point2D src[] = {{0, 0}, {100, 0}, {100, 100}, {0, 100}, {0, 100}};
488    OH_Drawing_Point2D dst[] = {{0, 0}, {100, 30}, {100, 70}, {0, 100}, {0, 100}};
489    // 2. OH_Drawing_MatrixMapPoints, pass the float value 1.52 as count
490    double value = 1.52;
491    uint32_t count = static_cast<uint32_t>(value);
492    OH_Drawing_MatrixSetPolyToPoly(matrix, src, dst, count);
493    // 3. OH_Drawing_MatrixMapPoints, pass integer 5 as count
494    OH_Drawing_MatrixSetPolyToPoly(matrix, src, dst, 5);
495    // 4. Free memory
496    OH_Drawing_MatrixDestroy(matrix);
497}
498
499/*
500 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_MATRIX_2001
501 * @tc.name: testMatrixMapPointsNull
502 * @tc.desc: test for testMatrixMapPointsNull.
503 * @tc.size  : SmallTest
504 * @tc.type  : Function
505 * @tc.level : Level 3
506 */
507HWTEST_F(DrawingNativeMatrixTest, testMatrixMapPointsNull, TestSize.Level3) {
508    // 1. OH_Drawing_MatrixCreate
509    OH_Drawing_Matrix *matrix = OH_Drawing_MatrixCreate();
510    OH_Drawing_Point2D src[] = {{0, 0}, {100, 0}, {100, 100}, {0, 100}, {0, 100}};
511    OH_Drawing_Point2D dst[] = {{0, 0}, {100, 30}, {100, 70}, {0, 100}, {0, 100}};
512    // 2. OH_Drawing_MatrixMapPoints, the first parameter is nullptr, check the error code with OH_Drawing_ErrorCodeGet
513    OH_Drawing_MatrixMapPoints(nullptr, src, dst, 5);
514    EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_Drawing_ErrorCode::OH_DRAWING_ERROR_INVALID_PARAMETER);
515    // 3. OH_Drawing_MatrixMapPoints, the second parameter is nullptr, check the error code with OH_Drawing_ErrorCodeGet
516    OH_Drawing_MatrixMapPoints(matrix, nullptr, dst, 5);
517    EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_Drawing_ErrorCode::OH_DRAWING_ERROR_INVALID_PARAMETER);
518    // 4. OH_Drawing_MatrixMapPoints, the third parameter is nullptr, check the error code with OH_Drawing_ErrorCodeGet
519    OH_Drawing_MatrixMapPoints(matrix, src, nullptr, 5);
520    EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_Drawing_ErrorCode::OH_DRAWING_ERROR_INVALID_PARAMETER);
521    // 5. OH_Drawing_MatrixMapPoints, the fourth parameter is nullptr, check the error code with OH_Drawing_ErrorCodeGet
522    OH_Drawing_MatrixMapPoints(matrix, src, dst, 0);
523    EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_Drawing_ErrorCode::OH_DRAWING_ERROR_INVALID_PARAMETER);
524    // 6. Free memory
525    OH_Drawing_MatrixDestroy(matrix);
526}
527
528/*
529 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_MATRIX_2002
530 * @tc.name: testMatrixMapPointsAbnormal
531 * @tc.desc: test for testMatrixMapPointsAbnormal.
532 * @tc.size  : SmallTest
533 * @tc.type  : Function
534 * @tc.level : Level 3
535 */
536HWTEST_F(DrawingNativeMatrixTest, testMatrixMapPointsAbnormal, TestSize.Level3) {
537    // 1. OH_Drawing_MatrixCreate
538    OH_Drawing_Matrix *matrix = OH_Drawing_MatrixCreate();
539    OH_Drawing_Point2D src[] = {{0, 0}, {100, 0}, {100, 100}, {0, 100}, {0, 100}};
540    OH_Drawing_Point2D dst[] = {{0, 0}, {100, 30}, {100, 70}, {0, 100}, {0, 100}};
541    // 2. OH_Drawing_MatrixMapPoints, pass -1 as count, check the error code with OH_Drawing_ErrorCodeGet
542    OH_Drawing_MatrixMapPoints(matrix, src, dst, -1);
543    EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_Drawing_ErrorCode::OH_DRAWING_ERROR_INVALID_PARAMETER);
544    // 3. Free memory
545    OH_Drawing_MatrixDestroy(matrix);
546}
547
548/*
549 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_MATRIX_2003
550 * @tc.name: testMatrixMapPointsMultipleCalls
551 * @tc.desc: test for testMatrixMapPointsMultipleCalls.
552 * @tc.size  : SmallTest
553 * @tc.type  : Function
554 * @tc.level : Level 3
555 */
556HWTEST_F(DrawingNativeMatrixTest, testMatrixMapPointsMultipleCalls, TestSize.Level3) {
557    // 1. OH_Drawing_MatrixCreate
558    OH_Drawing_Matrix *matrix = OH_Drawing_MatrixCreate();
559    OH_Drawing_Point2D src[] = {{0, 0}, {100, 0}, {100, 100}, {0, 100}, {0, 100}};
560    OH_Drawing_Point2D dst[] = {{0, 0}, {100, 30}, {100, 70}, {0, 100}, {0, 100}};
561    // 2. Call OH_Drawing_MatrixMapPoints 10 times
562    for (int i = 0; i < 10; i++) {
563        OH_Drawing_MatrixMapPoints(matrix, src, dst, 5);
564    }
565    // 3. Free memory
566    OH_Drawing_MatrixDestroy(matrix);
567}
568
569/*
570 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_MATRIX_2100
571 * @tc.name: testMatrixMapRectNormal
572 * @tc.desc: test for testMatrixMapRectNormal.
573 * @tc.size  : SmallTest
574 * @tc.type  : Function
575 * @tc.level : Level 0
576 */
577HWTEST_F(DrawingNativeMatrixTest, testMatrixMapRectNormal, TestSize.Level0) {
578    // 1. OH_Drawing_MatrixCreate
579    OH_Drawing_Matrix *matrix = OH_Drawing_MatrixCreate();
580    // 2. OH_Drawing_MatrixMapRect, src and dst are the same
581    OH_Drawing_Rect *src = OH_Drawing_RectCreate(0, 0, 100, 100);
582    OH_Drawing_Rect *dst = OH_Drawing_RectCreate(0, 0, 100, 100);
583    OH_Drawing_MatrixMapRect(matrix, src, dst);
584    // 3. OH_Drawing_MatrixMapRect, src and dst are different
585    OH_Drawing_Rect *src2 = OH_Drawing_RectCreate(0, 0, 100, 100);
586    OH_Drawing_Rect *dst2 = OH_Drawing_RectCreate(0, 0, 200, 200);
587    OH_Drawing_MatrixMapRect(matrix, src2, dst2);
588    // 4. Free memory
589    OH_Drawing_MatrixDestroy(matrix);
590    OH_Drawing_RectDestroy(src);
591    OH_Drawing_RectDestroy(dst);
592    OH_Drawing_RectDestroy(src2);
593    OH_Drawing_RectDestroy(dst2);
594}
595
596/*
597 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_MATRIX_2101
598 * @tc.name: testMatrixMapRectNull
599 * @tc.desc: test for testMatrixMapRectNull.
600 * @tc.size  : SmallTest
601 * @tc.type  : Function
602 * @tc.level : Level 3
603 */
604HWTEST_F(DrawingNativeMatrixTest, testMatrixMapRectNull, TestSize.Level3) {
605    // 1. OH_Drawing_MatrixCreate
606    OH_Drawing_Matrix *matrix = OH_Drawing_MatrixCreate();
607    // 2. OH_Drawing_MatrixMapRect, the first parameter is nullptr, check the error code with OH_Drawing_ErrorCodeGet
608    OH_Drawing_Rect *src = OH_Drawing_RectCreate(0, 0, 100, 100);
609    OH_Drawing_Rect *dst = OH_Drawing_RectCreate(0, 0, 100, 100);
610    OH_Drawing_MatrixMapRect(nullptr, src, dst);
611    EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
612    // 3. OH_Drawing_MatrixMapRect, the second parameter is nullptr, check the error code with OH_Drawing_ErrorCodeGet
613    OH_Drawing_MatrixMapRect(matrix, nullptr, dst);
614    EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
615    // 4. OH_Drawing_MatrixMapRect, the third parameter is nullptr, check the error code with OH_Drawing_ErrorCodeGet
616    OH_Drawing_MatrixMapRect(matrix, src, nullptr);
617    EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
618    // 5. Free memory
619    OH_Drawing_MatrixDestroy(matrix);
620    OH_Drawing_RectDestroy(src);
621    OH_Drawing_RectDestroy(dst);
622}
623
624/*
625 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_MATRIX_2103
626 * @tc.name: testMatrixMapRectMultipleCalls
627 * @tc.desc: test for testMatrixMapRectMultipleCalls.
628 * @tc.size  : SmallTest
629 * @tc.type  : Function
630 * @tc.level : Level 3
631 */
632HWTEST_F(DrawingNativeMatrixTest, testMatrixMapRectMultipleCalls, TestSize.Level3) {
633    // 1. OH_Drawing_MatrixCreate
634    OH_Drawing_Matrix *matrix = OH_Drawing_MatrixCreate();
635    // 2. Call OH_Drawing_MatrixMapRect 10 times with different src and dst
636    std::random_device rd;
637    std::mt19937 gen(rd());
638    std::uniform_real_distribution<float> dis(100, 200);
639    for (int i = 0; i < 10; i++) {
640        OH_Drawing_Rect *src = OH_Drawing_RectCreate(0, 0, dis(gen), dis(gen));
641        OH_Drawing_Rect *dst = OH_Drawing_RectCreate(0, 0, dis(gen), dis(gen));
642        OH_Drawing_MatrixMapRect(matrix, src, dst);
643        OH_Drawing_RectDestroy(src);
644        OH_Drawing_RectDestroy(dst);
645    }
646    // 3. Free memory
647    OH_Drawing_MatrixDestroy(matrix);
648}
649
650/*
651 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_MATRIX_2200
652 * @tc.name: testMatrixIsEqualNormal
653 * @tc.desc: test for testMatrixIsEqualNormal.
654 * @tc.size  : SmallTest
655 * @tc.type  : Function
656 * @tc.level : Level 0
657 */
658HWTEST_F(DrawingNativeMatrixTest, testMatrixIsEqualNormal, TestSize.Level0) {
659    // 1. OH_Drawing_MatrixIsEqual with the same matrix
660    OH_Drawing_Matrix *matrix = OH_Drawing_MatrixCreate();
661    OH_Drawing_MatrixSetMatrix(matrix, 1, 2, 3, 4, 5, 6, 7, 8, 9);
662    OH_Drawing_Matrix *matrix2 = OH_Drawing_MatrixCreate();
663    OH_Drawing_MatrixSetMatrix(matrix2, 1, 2, 3, 4, 5, 6, 7, 8, 9);
664    bool ret = OH_Drawing_MatrixIsEqual(matrix, matrix2);
665    EXPECT_EQ(ret, true);
666    // 2. OH_Drawing_MatrixIsEqual with different matrices
667    OH_Drawing_Matrix *matrix3 = OH_Drawing_MatrixCreate();
668    OH_Drawing_MatrixSetMatrix(matrix3, 2, 2, 3, 4, 5, 6, 7, 8, 9);
669    ret = OH_Drawing_MatrixIsEqual(matrix, matrix3);
670    EXPECT_EQ(ret, false);
671    // 3. Free memory
672    OH_Drawing_MatrixDestroy(matrix);
673    OH_Drawing_MatrixDestroy(matrix2);
674    OH_Drawing_MatrixDestroy(matrix3);
675}
676
677/*
678 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_MATRIX_2201
679 * @tc.name: testMatrixIsEqualNull
680 * @tc.desc: test for testMatrixIsEqualNull.
681 * @tc.size  : SmallTest
682 * @tc.type  : Function
683 * @tc.level : Level 3
684 */
685HWTEST_F(DrawingNativeMatrixTest, testMatrixIsEqualNull, TestSize.Level3) {
686    // 1. OH_Drawing_MatrixCreate
687    OH_Drawing_Matrix *matrix = OH_Drawing_MatrixCreate();
688    OH_Drawing_MatrixSetMatrix(matrix, 1, 2, 3, 4, 5, 6, 7, 8, 9);
689    // 2. OH_Drawing_MatrixIsEqual, the first parameter is nullptr, check the error code with OH_Drawing_ErrorCodeGet
690    OH_Drawing_MatrixIsEqual(nullptr, matrix);
691    EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
692    // 3. OH_Drawing_MatrixIsEqual, the second parameter is nullptr, check the error code with OH_Drawing_ErrorCodeGet
693    OH_Drawing_MatrixIsEqual(matrix, nullptr);
694    EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
695    // 4. Free memory
696    OH_Drawing_MatrixDestroy(matrix);
697}
698
699/*
700 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_MATRIX_2202
701 * @tc.name: testMatrixIsEqualMultipleCalls
702 * @tc.desc: test for testMatrixIsEqualMultipleCalls.
703 * @tc.size  : SmallTest
704 * @tc.type  : Function
705 * @tc.level : Level 3
706 */
707HWTEST_F(DrawingNativeMatrixTest, testMatrixIsEqualMultipleCalls, TestSize.Level3) {
708    // 1. OH_Drawing_MatrixCreate
709    OH_Drawing_Matrix *matrix = OH_Drawing_MatrixCreate();
710    OH_Drawing_Matrix *matrix2 = OH_Drawing_MatrixCreate();
711    // 2. Call OH_Drawing_MatrixIsEqual 10 times with alternating different or same matrices
712    for (int i = 0; i < 10; i++) {
713        if (i % 2 == 0) {
714            OH_Drawing_MatrixSetMatrix(matrix, 1, 2, 3, 4, 5, 6, 7, 8, 9);
715            OH_Drawing_MatrixSetMatrix(matrix2, 1, 2, 3, 4, 5, 6, 7, 8, 9);
716            bool ret = OH_Drawing_MatrixIsEqual(matrix, matrix2);
717            EXPECT_EQ(ret, true);
718        } else {
719            OH_Drawing_MatrixSetMatrix(matrix, 1, 2, 3, 4, 5, 6, 7, 8, 9);
720            OH_Drawing_MatrixSetMatrix(matrix2, 2, 2, 3, 4, 5, 6, 7, 8, 9);
721            bool ret = OH_Drawing_MatrixIsEqual(matrix, matrix2);
722            EXPECT_EQ(ret, false);
723        }
724    }
725    // 3. Free memory
726    OH_Drawing_MatrixDestroy(matrix);
727    OH_Drawing_MatrixDestroy(matrix2);
728}
729
730/*
731 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_MATRIX_2300
732 * @tc.name: testMatrixIsIdentityNormal
733 * @tc.desc: test for testMatrixIsIdentityNormal.
734 * @tc.size  : SmallTest
735 * @tc.type  : Function
736 * @tc.level : Level 0
737 */
738HWTEST_F(DrawingNativeMatrixTest, testMatrixIsIdentityNormal, TestSize.Level0) {
739    // 1. OH_Drawing_MatrixIsIdentity with an identity matrix
740    OH_Drawing_Matrix *matrix = OH_Drawing_MatrixCreate();
741    bool ret = OH_Drawing_MatrixIsIdentity(matrix);
742    EXPECT_EQ(ret, true);
743    // 2. OH_Drawing_MatrixIsIdentity with a non-identity matrix
744    OH_Drawing_MatrixSetMatrix(matrix, 1, 2, 3, 4, 5, 6, 7, 8, 9);
745    ret = OH_Drawing_MatrixIsIdentity(matrix);
746    EXPECT_EQ(ret, false);
747    // 3. Free memory
748    OH_Drawing_MatrixDestroy(matrix);
749}
750
751/*
752 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_MATRIX_2301
753 * @tc.name: testMatrixIsIdentityNull
754 * @tc.desc: test for testMatrixIsIdentityNull.
755 * @tc.size  : SmallTest
756 * @tc.type  : Function
757 * @tc.level : Level 3
758 */
759HWTEST_F(DrawingNativeMatrixTest, testMatrixIsIdentityNull, TestSize.Level3) {
760    // 1. OH_Drawing_MatrixCreate
761    OH_Drawing_Matrix *matrix = OH_Drawing_MatrixCreate();
762    // 2. OH_Drawing_MatrixIsIdentity with nullptr as parameter, check the error code with OH_Drawing_ErrorCodeGet
763    OH_Drawing_MatrixIsIdentity(nullptr);
764    EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
765    // 3. Free memory
766    OH_Drawing_MatrixDestroy(matrix);
767}
768
769/*
770 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_MATRIX_2302
771 * @tc.name: testMatrixIsIdentityMultipleCalls
772 * @tc.desc: test for testMatrixIsIdentityMultipleCalls.
773 * @tc.size  : SmallTest
774 * @tc.type  : Function
775 * @tc.level : Level 3
776 */
777HWTEST_F(DrawingNativeMatrixTest, testMatrixIsIdentityMultipleCalls, TestSize.Level3) {
778    // Call OH_Drawing_MatrixIsIdentity 10 times with alternating identity or non-identity matrices
779    for (int i = 0; i < 10; i++) {
780        if (i % 2 == 0) {
781            OH_Drawing_Matrix *matrix = OH_Drawing_MatrixCreate();
782            bool ret = OH_Drawing_MatrixIsIdentity(matrix);
783            EXPECT_EQ(ret, true);
784            OH_Drawing_MatrixDestroy(matrix);
785        } else {
786            OH_Drawing_Matrix *matrix = OH_Drawing_MatrixCreate();
787            OH_Drawing_MatrixSetMatrix(matrix, 1, 2, 3, 4, 5, 6, 7, 8, 9);
788            bool ret = OH_Drawing_MatrixIsIdentity(matrix);
789            EXPECT_EQ(ret, false);
790            OH_Drawing_MatrixDestroy(matrix);
791        }
792    }
793}
794
795} // namespace Drawing
796} // namespace Rosen
797} // namespace OHOS