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 OHOS_NAPI_ACE_COMPONENT_TEST_CONFIG_H
17 #define OHOS_NAPI_ACE_COMPONENT_TEST_CONFIG_H
18
19 #include <string_view>
20
21 #include "component_test/component_test_proxy.h"
22 #include "component_test/core/component_test_component_impl.h"
23 #include "component_test/core/component_test_matcher_impl.h"
24 #include "component_test/core/component_test_tester_impl.h"
25 #include "napi/native_api.h"
26 #include "napi/native_node_api.h"
27
28 namespace OHOS::Ace::Napi {
29 using ErrInfo = OHOS::Ace::ComponentTest::ErrInfo;
30 using ErrCode = OHOS::Ace::ComponentTest::ErrCode;
31
32 constexpr char TESTER_NAME[] = "Tester";
33 constexpr char MATCHER_NAME[] = "Matcher";
34 constexpr char COMPONENT_NAME[] = "Component";
35 constexpr char TEST_CASE_TYPE[] = "TestCaseType";
36 constexpr char TEST_CASE_SIZE[] = "TestCaseSize";
37 constexpr char TEST_CASE_LEVEL[] = "TestCaseLevel";
38 constexpr char MATCH_TYPE[] = "MatchType";
39 constexpr char UI_DIRECTION[] = "UiDirection";
40 constexpr napi_status NAPI_ERR = napi_status::napi_generic_failure;
41 constexpr size_t NAPI_MAX_BUF_LEN = 1024;
42 constexpr int32_t ACE_INSTANCE_ID = 0;
43 constexpr size_t ARG_COUNT_ZERO = 0;
44 constexpr size_t ARG_COUNT_ONE = 1;
45 constexpr size_t ARG_COUNT_TWO = 2;
46 constexpr size_t ARG_COUNT_THREE = 3;
47 constexpr size_t ARG_COUNT_FOUR = 4;
48 constexpr size_t ARG_COUNT_FIVE = 5;
49
50 struct ComponentTestAsyncCtx {
51 napi_env env = nullptr;
52 napi_async_work asyncWork = nullptr;
53 napi_deferred deferred = nullptr;
54 napi_ref callbackRef = nullptr;
55 napi_value asyncResult = nullptr;
56 ErrInfo ret = { ErrCode::RET_OK, "" };
57 ComponentTest::ComponentTestMatcherImpl* matcherImpl = nullptr;
58 ComponentTest::ComponentTestTesterImpl* testerImpl = nullptr;
59 ComponentTest::ComponentTestComponentImpl* componentImpl = nullptr;
60 OHOS::MMI::KeyCode keyCode = OHOS::MMI::KeyCode::KEY_UNKNOWN;
61 std::vector<OHOS::MMI::KeyCode> keyCodes = {};
62 uint32_t duration = 0;
63 bool flingP = false;
64 uint32_t delayMs = 0;
65 uint32_t x = 0;
66 uint32_t y = 0;
67 uint32_t endx = 0;
68 uint32_t endy = 0;
69 uint32_t speed = 0;
70 uint32_t stepLen = 0;
71 int32_t deltaX = 0;
72 int32_t deltaY = 0;
73 ComponentTest::UiDirection direction = ComponentTest::UiDirection::TO_LEFT;
74 NG::PointF from;
75 NG::PointF to;
76 float scale = 0;
77 std::string text = "";
78 const char* functionName;
79 };
80
81 ComponentTestAsyncCtx* CreateAsyncContext(napi_env env);
82
83 ComponentTest::ComponentTestMatcherImpl* GetMatcher(napi_env env, napi_value napiMatcher);
84
85 std::string GetComponentTestEtsPosition();
86
87 napi_value CreateBusinessError(napi_env env, const ErrInfo& errInfo);
88
89 #define COMPONENT_TEST_NAPI_THROW(env, errCode) \
90 do { \
91 ErrInfo errInfo = QueryRetMsg(errCode); \
92 napi_value err = CreateBusinessError(env, errInfo); \
93 std::stringstream ss; \
94 ss << __FILE__ << "(" << __FUNCTION__ << ":" << __LINE__ << ")"; \
95 ComponentTest::ComponentTestManagerProxy::Record( \
96 std::string(errInfo.message), ss.str(), OHOS::Ace::ComponentTest::Result::ERROR); \
97 napi_throw(env, err); \
98 } while (0)
99
100 #define COMPONENT_TEST_NAPI_ASSERT(env, cond, errCode) \
101 do { \
102 if (!(cond)) { \
103 COMPONENT_TEST_NAPI_THROW(env, errCode); \
104 return NapiGetUndefined(env); \
105 } \
106 } while (0)
107
108 #define COMPONENT_TEST_NAPI_ASSERT_CUSTOM(env, cond, errCode, errMsg, retValue) \
109 while (!(cond)) { \
110 napi_value err = CreateBusinessError(env, { errCode, errMsg }); \
111 std::stringstream ss; \
112 ss << __FILE__ << "(" << __FUNCTION__ << ":" << __LINE__ << ")"; \
113 ComponentTest::ComponentTestManagerProxy::Record( \
114 std::string(errMsg), ss.str(), OHOS::Ace::ComponentTest::Result::ERROR); \
115 napi_throw(env, err); \
116 return retValue; \
117 }
118
119 #define COMPONENT_TEST_NAPI_JS_ASSERTION_FAIL(env, errCode) \
120 do { \
121 ErrInfo errInfo = QueryRetMsg(errCode); \
122 napi_value err = CreateBusinessError(env, errInfo); \
123 napi_throw(env, err); \
124 return NapiGetUndefined(env); \
125 } while (0)
126
127 #define CHECK_COMPONENT_TEST_ENABLED() \
128 do { \
129 if (!ComponentTest::ComponentTestManagerProxy::IsComponentTestEnabled()) { \
130 LOGW("Component test is disabled, cannot perform this operation"); \
131 return nullptr; \
132 } \
133 } while (0)
134
ComponentTestRecord(bool cond,std::string info)135 inline void ComponentTestRecord(bool cond, std::string info)
136 {
137 if (!cond) {
138 ComponentTest::ComponentTestManagerProxy::Record(
139 info, GetComponentTestEtsPosition(), OHOS::Ace::ComponentTest::Result::FAIL);
140 } else {
141 ComponentTest::ComponentTestManagerProxy::Record(
142 info, GetComponentTestEtsPosition(), OHOS::Ace::ComponentTest::Result::PASS);
143 }
144 }
145
146 struct EnumValueDef {
147 std::string_view name_;
148 uint32_t value_;
149 };
150
NapiValueToString(napi_env env,napi_value value)151 inline std::string NapiValueToString(napi_env env, napi_value value)
152 {
153 size_t bufSize = 0;
154 napi_value napiString;
155 char strBuffer[NAPI_MAX_BUF_LEN] = { 0 };
156 NAPI_CALL(env, napi_coerce_to_string(env, value, &napiString));
157 NAPI_CALL(env, napi_get_value_string_utf8(env, napiString, strBuffer, NAPI_MAX_BUF_LEN, &bufSize));
158 return std::string(strBuffer, bufSize);
159 }
160
161 napi_value NapiGetUndefined(napi_env env);
162
163 bool CheckAndParseStr(napi_env env, napi_value arg, std::string& recv, std::string& errMsg);
164
165 bool CheckAndParseInt32(napi_env env, napi_value arg, int32_t& recv, std::string& errMsg);
166
167 bool CheckAndParseUInt32(napi_env env, napi_value arg, uint32_t& recv, std::string& errMsg);
168
169 bool CheckAndParseBool(napi_env env, napi_value arg, bool& recv, std::string& errMsg);
170
171 struct EnumeratorDef {
172 std::string_view name_;
173 const EnumValueDef* values;
174 size_t valueCount_;
175 };
176
177 constexpr EnumValueDef TEST_CASE_TYPE_VALUES[] = {
178 { "FUNCTION", 0 },
179 { "PERFORMANCE", 1 },
180 { "POWER", 2 },
181 { "RELIABILITY", 3 },
182 { "SECURITY", 4 },
183 { "GLOBAL", 5 },
184 { "COMPATIBILITY", 6 },
185 { "USER", 7 },
186 { "STANDARD", 8 },
187 { "SAFETY", 9 },
188 { "RESILIENCE", 10 },
189 };
190
191 constexpr EnumeratorDef TEST_CASE_TYPE_DEF = {
192 TEST_CASE_TYPE,
193 TEST_CASE_TYPE_VALUES,
194 sizeof(TEST_CASE_TYPE_VALUES) / sizeof(EnumValueDef),
195 };
196
197 constexpr EnumValueDef TEST_CASE_SIZE_VALUES[] = {
198 { "SMALLTEST", 0 },
199 { "MEDIUMTEST", 1 },
200 { "LARGETEST", 2 },
201 };
202
203 constexpr EnumeratorDef TEST_CASE_SIZE_DEF = {
204 TEST_CASE_SIZE,
205 TEST_CASE_SIZE_VALUES,
206 sizeof(TEST_CASE_SIZE_VALUES) / sizeof(EnumValueDef),
207 };
208
209 constexpr EnumValueDef TEST_CASE_LEVEL_VALUES[] = {
210 { "LEVEL0", 0 },
211 { "LEVEL1", 1 },
212 { "LEVEL2", 2 },
213 { "LEVEL3", 3 },
214 { "LEVEL4", 4 },
215 };
216
217 constexpr EnumeratorDef TEST_CASE_LEVEL_DEF = {
218 TEST_CASE_LEVEL,
219 TEST_CASE_LEVEL_VALUES,
220 sizeof(TEST_CASE_LEVEL_VALUES) / sizeof(EnumValueDef),
221 };
222
223 constexpr EnumValueDef MATCH_TYPE_VALUES[] = {
224 { "EQUALS", 0 },
225 { "CONTAINS", 1 },
226 { "STARTS_WITH", 2 },
227 { "ENDS_WITH", 3 },
228 };
229
230 constexpr EnumeratorDef MATCH_TYPE_DEF = {
231 MATCH_TYPE,
232 MATCH_TYPE_VALUES,
233 sizeof(MATCH_TYPE_VALUES) / sizeof(EnumValueDef),
234 };
235
236 constexpr EnumValueDef UI_DIRECTION_VALUES[] = {
237 { "LEFT", 0 },
238 { "RIGHT", 1 },
239 { "UP", 2 },
240 { "DOWN", 3 },
241 };
242
243 constexpr EnumeratorDef UI_DIRECTION_DEF = {
244 UI_DIRECTION,
245 UI_DIRECTION_VALUES,
246 sizeof(UI_DIRECTION_VALUES) / sizeof(EnumValueDef),
247 };
248
249 napi_status ExportEnumerator(napi_env env, napi_value exports, const EnumeratorDef& enumDef);
250
251 } // namespace OHOS::Ace::Napi
252
253 #endif // OHOS_NAPI_ACE_COMPONENT_TEST_CONFIG_H
254