• 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 #include "js_component_test_utils.h"
17 
18 #include "interfaces/napi/kits/utils/napi_utils.h"
19 
20 #include "base/log/log.h"
21 #include "bridge/common/utils/engine_helper.h"
22 
23 namespace OHOS::Ace::Napi {
24 
CreateAsyncContext(napi_env env)25 ComponentTestAsyncCtx* CreateAsyncContext(napi_env env)
26 {
27     ComponentTestAsyncCtx* asyncContext = new (std::nothrow) ComponentTestAsyncCtx();
28     if (asyncContext == nullptr) {
29         LOGE("%{public}s", "Failed to create asyncContext.");
30         COMPONENT_TEST_NAPI_THROW(env, ErrCode::RET_ERR_FAILED);
31         return nullptr;
32     }
33     asyncContext->env = env;
34     asyncContext->ret = ErrInfo { ErrCode::RET_OK, "" };
35     return asyncContext;
36 }
37 
GetMatcher(napi_env env,napi_value napiMatcher)38 ComponentTest::ComponentTestMatcherImpl* GetMatcher(napi_env env, napi_value napiMatcher)
39 {
40     ComponentTest::ComponentTestMatcherImpl* matcherImpl = nullptr;
41     napi_status status = napi_unwrap(env, napiMatcher, reinterpret_cast<void**>(&matcherImpl));
42     if (status != napi_ok || !matcherImpl) {
43         LOGE("%{public}s", "Failed to get unwrap ComponentTestMatcherImpl");
44         COMPONENT_TEST_NAPI_THROW(env, ErrCode::RET_ERR_FAILED);
45         return nullptr;
46     }
47     return matcherImpl;
48 }
49 
GetComponentTestEtsPosition()50 std::string GetComponentTestEtsPosition()
51 {
52     auto jsEngine = EngineHelper::GetCurrentEngine();
53     std::string stack;
54     CHECK_NULL_RETURN(jsEngine, "_ets_position");
55     jsEngine->GetStackTrace(stack);
56     LOGI("stack: %{public}s", stack.c_str());
57     std::regex reg("\\((.*)\\)");
58     std::smatch match;
59     if (std::regex_search(stack, match, reg)) {
60         LOGI("position %{public}s", match[1].str().c_str());
61         return (match[1].str());
62     }
63     return "_ets_position";
64 }
65 
CreateBusinessError(napi_env env,const ErrInfo & errInfo)66 napi_value CreateBusinessError(napi_env env, const ErrInfo& errInfo)
67 {
68     napi_value result = nullptr;
69     if (errInfo.errCode == ErrCode::RET_OK) {
70         napi_get_undefined(env, &result);
71     } else {
72         napi_value errCode = nullptr;
73         napi_create_int32(env, static_cast<int32_t>(errInfo.errCode), &errCode);
74         napi_value eMsg = nullptr;
75         napi_create_string_utf8(env, errInfo.message.data(), NAPI_AUTO_LENGTH, &eMsg);
76         napi_create_error(env, nullptr, eMsg, &result);
77         napi_set_named_property(env, result, "code", errCode);
78     }
79     return result;
80 }
81 
NapiGetUndefined(napi_env env)82 napi_value NapiGetUndefined(napi_env env)
83 {
84     napi_value result = nullptr;
85     napi_get_undefined(env, &result);
86     return result;
87 }
88 
CheckAndParseStr(napi_env env,napi_value arg,std::string & recv,std::string & errMsg)89 bool CheckAndParseStr(napi_env env, napi_value arg, std::string& recv, std::string& errMsg)
90 {
91     if (arg == nullptr) {
92         errMsg = "Parameter is null.";
93         return false;
94     }
95 
96     napi_valuetype valueType = napi_undefined;
97     napi_typeof(env, arg, &valueType);
98     if (valueType != napi_string) {
99         errMsg = "The type of parameter is not string";
100         return false;
101     }
102 
103     size_t msgLen = GetParamLen(env, arg) + 1;
104     std::unique_ptr<char[]> message = std::make_unique<char[]>(msgLen);
105     size_t ret = 0;
106     if (napi_get_value_string_utf8(env, arg, message.get(), msgLen, &ret) != napi_ok) {
107         errMsg = "Get string parameter value failed.";
108         return false;
109     }
110 
111     recv = message.get();
112     return true;
113 }
114 
CheckAndParseInt32(napi_env env,napi_value arg,int32_t & recv,std::string & errMsg)115 bool CheckAndParseInt32(napi_env env, napi_value arg, int32_t& recv, std::string& errMsg)
116 {
117     if (arg == nullptr) {
118         errMsg = "Parameter is null.";
119         return false;
120     }
121 
122     napi_valuetype valueType = napi_undefined;
123     napi_typeof(env, arg, &valueType);
124     if (valueType != napi_number) {
125         errMsg = "The type of parameter is not number";
126         return false;
127     }
128 
129     napi_get_value_int32(env, arg, &recv);
130     return true;
131 }
132 
CheckAndParseUInt32(napi_env env,napi_value arg,uint32_t & recv,std::string & errMsg)133 bool CheckAndParseUInt32(napi_env env, napi_value arg, uint32_t& recv, std::string& errMsg)
134 {
135     if (arg == nullptr) {
136         errMsg = "Parameter is null.";
137         return false;
138     }
139 
140     napi_valuetype valueType = napi_undefined;
141     napi_typeof(env, arg, &valueType);
142     if (valueType != napi_number) {
143         errMsg = "The type of parameter is not number";
144         return false;
145     }
146 
147     napi_get_value_uint32(env, arg, &recv);
148     return true;
149 }
150 
CheckAndParseBool(napi_env env,napi_value arg,bool & recv,std::string & errMsg)151 bool CheckAndParseBool(napi_env env, napi_value arg, bool& recv, std::string& errMsg)
152 {
153     if (arg == nullptr) {
154         errMsg = "Parameter is null.";
155         return false;
156     }
157 
158     napi_valuetype valueType = napi_undefined;
159     napi_typeof(env, arg, &valueType);
160     if (valueType != napi_boolean) {
161         errMsg = "The type of parameter is not boolean";
162         return false;
163     }
164 
165     napi_get_value_bool(env, arg, &recv);
166     return true;
167 }
168 
ExportEnumerator(napi_env env,napi_value exports,const EnumeratorDef & enumDef)169 napi_status ExportEnumerator(napi_env env, napi_value exports, const EnumeratorDef& enumDef)
170 {
171     NAPI_ASSERT_BASE(env, exports != nullptr, "Illegal export params", NAPI_ERR);
172     napi_value enumerator;
173     NAPI_CALL_BASE(env, napi_create_object(env, &enumerator), NAPI_ERR);
174     for (size_t index = 0; index < enumDef.valueCount_; index++) {
175         const auto& def = enumDef.values[index];
176         napi_value prop = nullptr;
177         NAPI_CALL_BASE(env, napi_create_uint32(env, def.value_, &prop), NAPI_ERR);
178         NAPI_CALL_BASE(env, napi_set_named_property(env, enumerator, def.name_.data(), prop), NAPI_ERR);
179     }
180     NAPI_CALL_BASE(env, napi_set_named_property(env, exports, enumDef.name_.data(), enumerator), NAPI_ERR);
181     return napi_ok;
182 }
183 
184 } // namespace OHOS::Ace::Napi
185