• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #ifndef ARKUI_CAPI_XTS_COMMON_TEST_H
17 #define ARKUI_CAPI_XTS_COMMON_TEST_H
18 
19 #include <napi/native_api.h>
20 #include <hilog/log.h>
21 #include <cstdio>
22 #include <arkui/native_interface.h>
23 #include <arkui/native_node.h>
24 #include <string>
25 
26 namespace ArkUICapiTest {
27 
28 #define PARAM_0_POINT_5 0.5
29 #define PARAM_1_POINT_5 1.5
30 #define PARAM_0_POINT_6 0.6
31 #define PARAM_1_POINT_1 1.1
32 #define PARAM_NEGATIVE_0_POINT_1 (-0.1)
33 #define PARAM_NEGATIVE_0_POINT_8 (-0.8)
34 #define PARAM_0 0
35 #define PARAM_1 1
36 #define PARAM_2 2
37 #define PARAM_3 3
38 #define PARAM_4 4
39 #define PARAM_5 5
40 #define PARAM_6 6
41 #define PARAM_7 7
42 #define PARAM_8 8
43 #define PARAM_9 9
44 #define PARAM_10 10
45 #define PARAM_11 11
46 #define PARAM_12 12
47 #define PARAM_13 13
48 #define PARAM_14 14
49 #define PARAM_15 15
50 #define PARAM_16 16
51 #define PARAM_17 17
52 #define PARAM_18 18
53 #define PARAM_19 19
54 #define PARAM_20 20
55 #define PARAM_80 80
56 #define PARAM_50_POINT_5 50.5
57 #define PARAM_80_POINT_5 80.5
58 #define PARAM_101_POINT_5 101.5
59 #define PARAM_90 90
60 #define PARAM_100 100
61 #define PARAM_200 200
62 #define PARAM_10000 10000
63 #define PARAM_NEGATIVE_1 (-1)
64 #define PARAM_NEGATIVE_2 (-2)
65 #define PARAM_NEGATIVE_10 (-10)
66 #define PARAM_NEGATIVE_30 (-30)
67 #define PARAM_NEGATIVE_50 (-50)
68 #define PARAM_NEGATIVE_50_POINT_5 (-50.5)
69 #define PARAM_NEGATIVE_100 (-100)
70 #define PARAM_64 64
71 #define DEFAULT_MARGIN 10
72 #define SIZE_2 2
73 #define SIZE_3 3
74 #define SIZE_5 5
75 #define SIZE_10 10
76 #define SIZE_20 20
77 #define SIZE_30 30
78 #define SIZE_40 40
79 #define SIZE_50 50
80 #define SIZE_56 56
81 #define SIZE_60 60
82 #define SIZE_70 70
83 #define SIZE_80 80
84 #define SIZE_90 90
85 #define SIZE_100 100
86 #define SIZE_110 110
87 #define SIZE_120 120
88 #define SIZE_180 180
89 #define SIZE_150 150
90 #define SIZE_200 200
91 #define SIZE_250 250
92 #define SIZE_300 300
93 #define SIZE_350 350
94 #define SIZE_400 400
95 #define SIZE_500 500
96 #define SIZE_600 600
97 #define SIZE_10000 10000
98 #define SUCCESS 0
99 #define INVALID_PARAM 401
100 #define NULL_PARAM 404
101 #define COLOR_BLACK 0xFF000000
102 #define COLOR_RED 0xFFFF0000
103 #define COLOR_GREEN 0xFF00FF00
104 #define COLOR_BLUE 0xFF0000FF
105 #define COLOR_YELLOW 0xFFFFFF00
106 #define COLOR_PURPLE 0xFFFF11FF
107 #define COLOR_GRAY 0xFFDDDDDD
108 #define COLOR_CYAN 0xFF00FFFF
109 #define COLOR_WHITE 0xFFFFFFFF
110 #define COLOR_PINK 0xFFFFC0CB
111 #define COLOR_ILLEGAL 0x00000000
112 #define ON_CLICK_EVENT_ID 6001
113 #define ON_FOCUS_EVENT_ID 6002
114 #define ON_TOUCH_EVENT_ID 6003
115 #define ON_AREA_CHANGE_EVENT_ID 6004
116 #define ON_APPEAR_EVENT_ID 6005
117 #define ON_BLUR_EVENT_ID 6006
118 #define ON_CHANGE_EVENT_ID 6007
119 #define ON_SUBMIT_EVENT_ID 6008
120 #define ON_CUT_EVENT_ID 6009
121 #define ON_PASTE_EVENT_ID 6010
122 #define ON_MEASURE_EVENT_ID 6011
123 #define ON_LAYOUT_EVENT_ID 6012
124 #define ON_DRAW_EVENT_ID 6013
125 #define ON_CUSTOM_EVENT_1_ID 9001
126 #define ON_CUSTOM_EVENT_2_ID 9002
127 #define ON_CUSTOM_EVENT_3_ID 9003
128 #define ON_CUSTOM_EVENT_4_ID 9004
129 #define ON_CUSTOM_EVENT_5_ID 9005
130 #define ON_CUSTOM_EVENT_6_ID 9006
131 #define ON_CUSTOM_EVENT_11_ID 9011
132 #define ON_CUSTOM_EVENT_12_ID 9012
133 #define ON_CUSTOM_EVENT_13_ID 9013
134 #define ON_CUSTOM_EVENT_14_ID 9014
135 #define ON_CUSTOM_EVENT_15_ID 9015
136 #define ON_CUSTOM_EVENT_16_ID 9016
137 #define PARAM_NEGATIVE_1 (-1)
138 #define PARAM_NEGATIVE_100 (-100)
139 #define PARAM_NEGATIVE_5 (-5)
140 
141 /**
142  * Log print domain.
143  */
144 const unsigned int LOG_PRINT_DOMAIN = 0xFF00;
145 
146 #define ASSERT_EQ(result, expect)                                                                                      \
147     do {                                                                                                               \
148         if ((result) != (expect)) {                                                                                    \
149             std::string assertStr = "assert equal failed, expect is " + std::to_string(expect) + " and result is " +   \
150                                     std::to_string(result);                                                            \
151             char assertChars[assertStr.size() + PARAM_1];                                                              \
152             strcpy(assertChars, assertStr.c_str());                                                                    \
153             napi_throw_error((env), nullptr, assertChars);                                                             \
154             napi_value errorResult = nullptr;                                                                          \
155             napi_create_int32(env, PARAM_NEGATIVE_1, &errorResult);                                                    \
156             return errorResult;                                                                                        \
157         }                                                                                                              \
158     } while (0)
159 
160 #define ASSERT_NE(result, expect)                                                                                      \
161     do {                                                                                                               \
162         if ((result) == (expect)) {                                                                                    \
163             std::string assertStr = "assert not equal failed, expect is " + std::to_string(expect) +                   \
164                                     " and result is " + std::to_string(result);                                        \
165             char assertChars[assertStr.size() + PARAM_1];                                                              \
166             strcpy(assertChars, assertStr.c_str());                                                                    \
167             napi_throw_error((env), nullptr, assertChars);                                                             \
168             napi_value errorResult = nullptr;                                                                          \
169             napi_create_int32(env, PARAM_NEGATIVE_1, &errorResult);                                                    \
170             return errorResult;                                                                                        \
171         }                                                                                                              \
172     } while (0)
173 
174 #define ASSERT_STREQ(result, expect)                                                                                   \
175     do {                                                                                                               \
176         auto *res1 = (result);                                                                                         \
177         decltype(res1) res2 = (expect);                                                                                \
178         std::string str1 = res1;                                                                                       \
179         std::string str2 = res2;                                                                                       \
180         if (::strcmp(res1, res2) != 0) {                                                                               \
181             OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "ASSERT_STREQ",                                         \
182                          "assert equal failed, expect is %{public}s and result is %{public}s", res2, res1);            \
183             napi_value errorResult = nullptr;                                                                          \
184             napi_create_int32(env, PARAM_NEGATIVE_1, &errorResult);                                                    \
185             return errorResult;                                                                                        \
186         }                                                                                                              \
187     } while (0)
188 
189 #define ASSERT_STRNE(result, expect)                                                                                   \
190     do {                                                                                                               \
191         auto *res1 = (result);                                                                                         \
192         decltype(res1) res2 = (expect);                                                                                \
193         std::string str1 = res1;                                                                                       \
194         std::string str2 = res2;                                                                                       \
195         if (::strcmp(res1, res2) == 0) {                                                                               \
196             OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "ASSERT_STRNE",                                         \
197                          "assert not equal failed, expect is %{public}s and result is %{public}s", res2, res1);        \
198             napi_value errorResult = nullptr;                                                                          \
199             napi_create_int32(env, PARAM_NEGATIVE_1, &errorResult);                                                    \
200             return errorResult;                                                                                        \
201         }                                                                                                              \
202     } while (0)
203 
204 #define ASSERT_OBJ(result, expect)                                                                                     \
205     do {                                                                                                               \
206         if ((result) != (expect)) {                                                                                    \
207             napi_value errorResult = nullptr;                                                                          \
208             return errorResult;                                                                                        \
209         }                                                                                                              \
210     } while (0)
211 
212 #define NAPI_END                                                                                                       \
213     do {                                                                                                               \
214         napi_value result = nullptr;                                                                                   \
215         napi_create_int32(env, SUCCESS, &result);                                                                      \
216         return result;                                                                                                 \
217     } while (0)
218 
219 #define NAPI_START(componentName, componentType)                                                                       \
220     auto nodeAPI = reinterpret_cast<ArkUI_NativeNodeAPI_1 *>(                                                          \
221         OH_ArkUI_QueryModuleInterfaceByName(ARKUI_NATIVE_NODE, "ArkUI_NativeNodeAPI_1"));                              \
222     auto(componentName) = nodeAPI->createNode(componentType);                                                          \
223     do {                                                                                                               \
224         if ((env == nullptr) || (info == nullptr)) {                                                                   \
225             OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NAPI_CHECK_PARAMS", "GetContext env or info is null"); \
226             return nullptr;                                                                                            \
227         }                                                                                                              \
228     } while (0)
229 
230 } // namespace ArkUICapiTest
231 #endif // ARKUI_CAPI_XTS_COMMON_TEST_H
232