1 /*
2 * Copyright (c) 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, 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 "ani.h"
17 #include <ani_signature_builder.h>
18 #include "hilog/log.h"
19 #include <array>
20 #include <iostream>
21 #include <unistd.h>
22 #include <future>
23 #include <queue>
24 #include <set>
25 #include <string>
26 #include "nlohmann/json.hpp"
27 #include "fcntl.h"
28 #include "common_utilities_hpp.h"
29 #include "frontend_api_handler.h"
30 #include "ipc_transactor.h"
31 #include "test_server_client.h"
32 #include "error_handler.h"
33 #include "ui_event_observer_ani.h"
34 #include <cstring>
35 #include <cstdio>
36
37 using namespace OHOS::uitest;
38 using namespace nlohmann;
39 using namespace std;
40 using namespace arkts::ani_signature;
41 static ApiTransactor g_apiTransactClient(false);
42 static future<void> g_establishConnectionFuture;
43
44 template <typename T>
compareAndReport(const T & param1,const T & param2,const std::string & errorMessage,const std::string & message)45 void compareAndReport(const T ¶m1, const T ¶m2, const std::string &errorMessage, const std::string &message)
46 {
47 if (param1 != param2) {
48 HiLog::Error(LABEL, "compareAndReport: %{public}s", errorMessage.c_str());
49 return;
50 } else {
51 HiLog::Info(LABEL, "compareAndReport: %{public}s", message.c_str());
52 return;
53 }
54 }
55
pushParam(ani_env * env,ani_object input,ApiCallInfo & callInfo_,bool isInt)56 static void pushParam(ani_env *env, ani_object input, ApiCallInfo &callInfo_, bool isInt)
57 {
58 ani_boolean ret;
59 env->Reference_IsUndefined(reinterpret_cast<ani_ref>(input), &ret);
60 if (ret == ANI_FALSE) {
61 if (isInt) {
62 ani_double param;
63 env->Object_CallMethodByName_Double(input, "unboxed", nullptr, ¶m);
64 callInfo_.paramList_.push_back(int(param));
65 } else {
66 ani_double param;
67 arkts::ani_signature::SignatureBuilder rd{};
68 rd.SetReturnDouble();
69 env->Object_CallMethodByName_Double(input, "unboxed", rd.BuildSignatureDescriptor().c_str(), ¶m);
70 callInfo_.paramList_.push_back(param);
71 }
72 }
73 }
74
aniStringToStdString(ani_env * env,ani_string string_object)75 static string aniStringToStdString([[maybe_unused]] ani_env *env, ani_string string_object)
76 {
77 ani_size strSize;
78 env->String_GetUTF8Size(string_object, &strSize);
79 std::vector<char> buffer(strSize + 1);
80 char *utf8_buffer = buffer.data();
81 ani_size bytes_written = 0;
82 env->String_GetUTF8(string_object, utf8_buffer, strSize + 1, &bytes_written);
83 utf8_buffer[bytes_written] = '\0';
84 std::string s = std::string(utf8_buffer);
85 std::cout << s << std::endl;
86 return s;
87 }
88
findCls(ani_env * env,const char * className)89 static ani_class findCls(ani_env *env, const char *className)
90 {
91 ani_class cls;
92 ani_ref nullref;
93 env->GetNull(&nullref);
94 if (ANI_OK != env->FindClass(className, &cls)) {
95 HiLog::Error(LABEL, "Not found className: %{public}s", className);
96 }
97 return cls;
98 }
99
findCtorMethod(ani_env * env,ani_class cls,const char * name)100 static ani_method findCtorMethod(ani_env *env, ani_class cls, const char *name)
101 {
102 ani_method ctor = nullptr;
103 if (ANI_OK != env->Class_FindMethod(cls, Builder::BuildConstructorName().c_str(), name, &ctor)) {
104 HiLog::Error(LABEL, "Not found ctor: %{public}s", name);
105 }
106 return ctor;
107 }
108
waitForConnectionIfNeed()109 static void waitForConnectionIfNeed()
110 {
111 if (g_establishConnectionFuture.valid()) {
112 HiLog::Error(LABEL, "%{public}s. Begin Wait for Connection", __func__);
113 g_establishConnectionFuture.get();
114 }
115 }
116
Transact(ApiCallInfo callInfo_,ApiReplyInfo & reply_)117 static void Transact(ApiCallInfo callInfo_, ApiReplyInfo &reply_)
118 {
119 waitForConnectionIfNeed();
120 g_apiTransactClient.Transact(callInfo_, reply_);
121 }
UnmarshalObject(ani_env * env,nlohmann::json resultValue_)122 static ani_ref UnmarshalObject(ani_env *env, nlohmann::json resultValue_)
123 {
124 const auto resultType = resultValue_.type();
125 ani_ref result = nullptr;
126 if (resultType == nlohmann::detail::value_t::null) {
127 return result;
128 } else if (resultType != nlohmann::detail::value_t::string) {
129 ani_string str;
130 env->String_NewUTF8(resultValue_.dump().c_str(), resultValue_.dump().size(), &str);
131 result = reinterpret_cast<ani_ref>(str);
132 return result;
133 }
134 const auto cppString = resultValue_.get<string>();
135 string frontendTypeName;
136 bool bindJsThis = false;
137 for (const auto &classDef : FRONTEND_CLASS_DEFS) {
138 const auto objRefFormat = string(classDef->name_) + "#";
139 if (cppString.find(objRefFormat) == 0) {
140 frontendTypeName = string(classDef->name_);
141 bindJsThis = classDef->bindUiDriver_;
142 break;
143 }
144 }
145 ani_string str;
146 env->String_NewUTF8(cppString.c_str(), cppString.length(), &str);
147 result = reinterpret_cast<ani_ref>(str);
148 ani_size size;
149 env->String_GetUTF16Size(str, &size);
150 return result;
151 }
152
UnmarshalReply(ani_env * env,const ApiCallInfo callInfo_,const ApiReplyInfo & reply_)153 static ani_ref UnmarshalReply(ani_env *env, const ApiCallInfo callInfo_, const ApiReplyInfo &reply_)
154 {
155 if (callInfo_.fdParamIndex_ >= 0) {
156 auto fd = callInfo_.paramList_.at(INDEX_ZERO).get<int>();
157 (void)close(fd);
158 }
159 HiLog::Info(LABEL, "%{public}s.Start to UnmarshalReply", __func__);
160 const auto &message = reply_.exception_.message_;
161 ErrCode code = reply_.exception_.code_;
162 if (code == INTERNAL_ERROR || code == ERR_INTERNAL) {
163 HiLog::Error(LABEL, "UItest : ERRORINFO: code='%{public}u', message='%{public}s'", code, message.c_str());
164 return nullptr;
165 } else if (reply_.exception_.code_ != NO_ERROR) {
166 HiLog::Error(LABEL, "UItest : ERRORINFO: code='%{public}u', message='%{public}s'", code, message.c_str());
167 ErrorHandler::Throw(env, code, message);
168 return nullptr;
169 }
170 HiLog::Info(LABEL, "UITEST: Start to unmarshall return value:%{public}s", reply_.resultValue_.dump().c_str());
171 const auto resultType = reply_.resultValue_.type();
172 if (resultType == nlohmann::detail::value_t::null) {
173 return nullptr;
174 } else if (resultType == nlohmann::detail::value_t::array) {
175 ani_class arrayCls = nullptr;
176 if (ANI_OK != env->FindClass(Builder::BuildClass({"escompat", "Array"}).Descriptor().c_str(), &arrayCls)) {
177 HiLog::Error(LABEL, "%{public}s FindClass Array Failed", __func__);
178 }
179 ani_ref undefinedRef = nullptr;
180 if (ANI_OK != env->GetUndefined(&undefinedRef)) {
181 HiLog::Error(LABEL, "%{public}s GetUndefined Failed", __func__);
182 }
183 arkts::ani_signature::SignatureBuilder array_ctor{};
184 array_ctor.AddInt();
185 ani_method arrayCtor = findCtorMethod(env, arrayCls, array_ctor.BuildSignatureDescriptor().c_str());
186 ani_object arrayObj;
187 ani_size com_size = reply_.resultValue_.size();
188 if (ANI_OK != env->Object_New(arrayCls, arrayCtor, &arrayObj, com_size)) {
189 HiLog::Error(LABEL, "%{public}s Object New Array Failed", __func__);
190 return reinterpret_cast<ani_ref>(arrayObj);
191 }
192 ani_class cls = findCls(env, Builder::BuildClass({"@ohos", "UiTest", "Component"}).Descriptor().c_str());
193 ani_method com_ctor;
194 ani_object com_obj;
195 if (cls != nullptr) {
196 arkts::ani_signature::SignatureBuilder string_ctor{};
197 string_ctor.AddClass({"std", "core", "String"});
198 com_ctor = findCtorMethod(env, cls, string_ctor.BuildSignatureDescriptor().c_str());
199 }
200 if (cls == nullptr || com_ctor == nullptr) {
201 return nullptr;
202 }
203 for (ani_size index = 0; index < reply_.resultValue_.size(); index++) {
204 ani_ref item = UnmarshalObject(env, reply_.resultValue_.at(index));
205 if (ANI_OK != env->Object_New(cls, com_ctor, &com_obj, reinterpret_cast<ani_object>(item))) {
206 HiLog::Error(LABEL, "%{public}s component Object new failed !!!", __func__);
207 }
208 if (ANI_OK != env->Object_CallMethodByName_Void(arrayObj, "$_set", "ILstd/core/Object;:V", index, com_obj)) {
209 HiLog::Error(LABEL, "%{public}s Object_CallMethodByName_Void set Failed", __func__);
210 break;
211 }
212 }
213 return reinterpret_cast<ani_ref>(arrayObj);
214 } else {
215 return UnmarshalObject(env, reply_.resultValue_);
216 }
217 }
218
ScheduleEstablishConnection(ani_env * env,ani_string connToken)219 static ani_boolean ScheduleEstablishConnection(ani_env *env, ani_string connToken)
220 {
221 auto token = aniStringToStdString(env, connToken);
222 ani_vm *vm = nullptr;
223 if (ANI_OK != env->GetVM(&vm)) {
224 HiLog::Error(LABEL, "%{public}s GetVM failed", __func__);
225 }
226 bool result = false;
227 g_establishConnectionFuture = async(launch::async, [vm, token, &result]() {
228 using namespace std::placeholders;
229 auto &instance = UiEventObserverAni::Get();
230 auto callbackHandler = std::bind(&UiEventObserverAni::HandleEventCallback, &instance, vm, _1, _2);
231 result = g_apiTransactClient.InitAndConnectPeer(token, callbackHandler);
232 HiLog::Error(LABEL, "End setup transaction connection, result=%{public}d", result);
233 });
234 return result;
235 }
236
GetConnectionStat(ani_env * env)237 static ani_double GetConnectionStat(ani_env *env)
238 {
239 return g_apiTransactClient.GetConnectionStat();
240 }
241
unwrapp(ani_env * env,ani_object object,const char * name)242 static ani_string unwrapp(ani_env *env, ani_object object, const char *name)
243 {
244 ani_ref it;
245 if (ANI_OK != env->Object_GetFieldByName_Ref(object, name, &it)) {
246 return nullptr;
247 }
248 return reinterpret_cast<ani_string>(it);
249 }
250
getPoint(ani_env * env,ani_object p)251 static json getPoint(ani_env *env, ani_object p)
252 {
253 auto point = json();
254 string list[] = {"x", "y"};
255 for (int index = 0; index < 2; index++) {
256 char *cstr = new char[list[index].length() + 1];
257 strcpy(cstr, list[index].c_str());
258 ani_double value;
259 compareAndReport(ANI_OK,
260 env->Object_GetPropertyByName_Double(p, cstr, &value),
261 "Object_GetField_Double Failed '" + std::string(cstr) + "'",
262 "Successful!!get double proprty");
263 point[list[index]] = int(value);
264 }
265 return point;
266 }
267
getRect(ani_env * env,ani_object p)268 static json getRect(ani_env *env, ani_object p)
269 {
270 auto rect = json();
271 string list[] = {"left", "right", "top", "bottom"};
272 for (int index = 0; index < 4; index++) {
273 char *cstr = new char[list[index].length() + 1];
274 strcpy(cstr, list[index].c_str());
275 ani_double value;
276 compareAndReport(ANI_OK,
277 env->Object_GetPropertyByName_Double(p, cstr, &value),
278 "Object_GetField_Double Failed '" + std::string(cstr) + "'",
279 "Successful!!get double proprty");
280 rect[list[index]] = int(value);
281 }
282 return rect;
283 }
284
newRect(ani_env * env,ani_object object,nlohmann::json num)285 static ani_object newRect(ani_env *env, ani_object object, nlohmann::json num)
286 {
287 ani_object rect_obj = {};
288 ani_class cls = findCls(env, Builder::BuildClass({"@ohos", "UiTest", "RectInner"}).Descriptor().c_str());
289 ani_method ctor = nullptr;
290 if (cls != nullptr) {
291 static const char *name = nullptr;
292 ctor = findCtorMethod(env, cls, name);
293 }
294 if (cls == nullptr || ctor == nullptr) {
295 return nullptr;
296 }
297 if (ANI_OK != env->Object_New(cls, ctor, &rect_obj)) {
298 HiLog::Error(LABEL, "Create Rect object failed");
299 return rect_obj;
300 }
301 ani_method setter;
302 string direct[] = {"left", "top", "right", "bottom"};
303 for (int index = 0; index < 4; index++) {
304 string tag = direct[index];
305 char *setter_name = strdup((Builder::BuildSetterName(tag)).c_str());
306 if (ANI_OK != env->Class_FindMethod(cls, setter_name, nullptr, &setter)) {
307 HiLog::Error(LABEL, "Find Method <set>tag failed");
308 }
309 if (ANI_OK != env->Object_CallMethod_Void(rect_obj, setter, ani_double(num[tag]))) {
310 HiLog::Error(LABEL, "call setter failed %{public}s", direct[index].c_str());
311 return rect_obj;
312 }
313 }
314 return rect_obj;
315 }
316
newPoint(ani_env * env,ani_object obj,int x,int y)317 static ani_object newPoint(ani_env *env, ani_object obj, int x, int y)
318 {
319 ani_object point_obj = {};
320 ani_class cls = findCls(env, Builder::BuildClass({"@ohos", "UiTest", "PointInner"}).Descriptor().c_str());
321 ani_method ctor = nullptr;
322 if (cls != nullptr) {
323 static const char *name = nullptr;
324 ctor = findCtorMethod(env, cls, name);
325 }
326 if (cls == nullptr || ctor == nullptr) {
327 return nullptr;
328 }
329 if (ANI_OK != env->Object_New(cls, ctor, &point_obj)) {
330 HiLog::Error(LABEL, "Create Point object failed");
331 return point_obj;
332 }
333 ani_method setter;
334 string direct[] = {"x", "y"};
335 int num[2] = {x, y};
336 for (int index = 0; index < 2; index++)
337 {
338 string tag = direct[index];
339 char *method_name = strdup((Builder::BuildSetterName(tag)).c_str());
340 if (ANI_OK != env->Class_FindMethod(cls, method_name, nullptr, &setter)) {
341 HiLog::Error(LABEL, "Find Method <set>tag failed");
342 }
343 if (ANI_OK != env->Object_CallMethod_Void(point_obj, setter, ani_double(num[index]))) {
344 HiLog::Error(LABEL, "call setter failed %{public}s", direct[index].c_str());
345 return point_obj;
346 }
347 }
348 return point_obj;
349 }
350
createMatrix(ani_env * env,ani_object object,ani_double fingers,ani_double steps)351 static ani_ref createMatrix(ani_env *env, ani_object object, ani_double fingers, ani_double steps)
352 {
353 ani_class cls = findCls(env, Builder::BuildClass({"@ohos", "UiTest", "PointerMatrix"}).Descriptor().c_str());
354 ani_ref nullref;
355 env->GetNull(&nullref);
356 ani_method ctor = nullptr;
357 if (cls != nullptr) {
358 arkts::ani_signature::SignatureBuilder string_ctor{};
359 string_ctor.AddClass({"std", "core", "String"});
360 ctor = findCtorMethod(env, cls, string_ctor.BuildSignatureDescriptor().c_str());
361 } else {
362 return nullref;
363 }
364 ApiCallInfo callInfo_;
365 ApiReplyInfo reply_;
366 callInfo_.apiId_ = "PointerMatrix.create";
367 callInfo_.paramList_.push_back(int(fingers));
368 callInfo_.paramList_.push_back(int(steps));
369 Transact(callInfo_, reply_);
370 ani_ref nativePointerMatrix = UnmarshalReply(env, callInfo_, reply_);
371 if (nativePointerMatrix == nullptr) {
372 return nullref;
373 }
374 ani_object pointer_matrix_object;
375 if (ANI_OK != env->Object_New(cls, ctor, &pointer_matrix_object, reinterpret_cast<ani_object>(nativePointerMatrix))) {
376 HiLog::Error(LABEL, "New PointerMatrix Failed %{public}s", __func__);
377 }
378 return pointer_matrix_object;
379 }
380
setPoint(ani_env * env,ani_object object,ani_double finger,ani_double step,ani_object point)381 static void setPoint(ani_env *env, ani_object object, ani_double finger, ani_double step, ani_object point)
382 {
383 ApiCallInfo callInfo_;
384 ApiReplyInfo reply_;
385 callInfo_.apiId_ = "PointerMatrix.setPoint";
386 callInfo_.paramList_.push_back(int(finger));
387 callInfo_.paramList_.push_back(int(step));
388 callInfo_.paramList_.push_back(getPoint(env, point));
389 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, object, "nativePointerMatrix"));
390 Transact(callInfo_, reply_);
391 UnmarshalReply(env, callInfo_, reply_);
392 return;
393 }
394
getInputTextModeOptions(ani_env * env,ani_object f)395 static json getInputTextModeOptions(ani_env *env, ani_object f)
396 {
397 auto options = json();
398 string list[] = {"paste", "addition"};
399 for (int i = 0; i < TWO; i++) {
400 char *cstr = new char[list[i].length() + 1];
401 strcpy(cstr, list[i].c_str());
402 ani_ref value;
403 if (env->Object_GetPropertyByName_Ref(f, cstr, &value) != ANI_OK) {
404 HiLog::Error(LABEL, "GetPropertyByName %{public}s fail", cstr);
405 continue;
406 }
407 ani_boolean ret;
408 env->Reference_IsUndefined(value, &ret);
409 if (ret == ANI_TRUE) {
410 continue;
411 }
412 ani_boolean b;
413 compareAndReport(ANI_OK,
414 env->Object_CallMethodByName_Boolean(static_cast<ani_object>(value), "unboxed", nullptr, &b),
415 "Object_CallMethodByName_Boolean Failed",
416 "get boolean value");
417 HiLog::Info(LABEL, "%{public}d ani_boolean !!!", static_cast<int>(b));
418 options[list[i]] = static_cast<bool>(b);
419 }
420 return options;
421 }
422
BindPointMatrix(ani_env * env)423 static ani_boolean BindPointMatrix(ani_env *env)
424 {
425 ani_class cls = findCls(env, Builder::BuildClass({"@ohos", "UiTest", "PointerMatrix"}).Descriptor().c_str());
426 if (cls == nullptr) {
427 HiLog::Error(LABEL, "%{public}s Not found className !!!", __func__);
428 return false;
429 }
430 std::array methods = {
431 ani_native_function{"create", nullptr, reinterpret_cast<void *>(createMatrix)},
432 ani_native_function{"setPoint", nullptr, reinterpret_cast<void *>(setPoint)},
433 };
434 if (ANI_OK != env->Class_BindNativeMethods(cls, methods.data(), methods.size())) {
435 HiLog::Error(LABEL, "%{public}s Cannot bind native methods to !!!", __func__);
436 return false;
437 }
438 return true;
439 }
440
createOn(ani_env * env,ani_object object,nlohmann::json params,string apiId_)441 static ani_ref createOn(ani_env *env, ani_object object, nlohmann::json params, string apiId_)
442 {
443 ani_class cls = findCls(env, Builder::BuildClass({"@ohos", "UiTest", "On"}).Descriptor().c_str());
444 ani_method ctor = nullptr;
445 if (cls != nullptr) {
446 arkts::ani_signature::SignatureBuilder string_ctor{};
447 string_ctor.AddClass({"std", "core", "String"});
448 ctor = findCtorMethod(env, cls, string_ctor.BuildSignatureDescriptor().c_str());
449 }
450 if (ctor == nullptr || cls == nullptr) {
451 return nullptr;
452 }
453 ApiCallInfo callInfo_;
454 ApiReplyInfo reply_;
455 callInfo_.apiId_ = apiId_;
456 callInfo_.paramList_ = params;
457 string on_obj = aniStringToStdString(env, unwrapp(env, object, "nativeOn"));
458 if (on_obj != "") {
459 callInfo_.callerObjRef_ = on_obj;
460 } else {
461 callInfo_.callerObjRef_ = string(REF_SEED_ON);
462 }
463 Transact(callInfo_, reply_);
464 ani_ref nativeOn = UnmarshalReply(env, callInfo_, reply_);
465 if (nativeOn == nullptr) {
466 return nullptr;
467 }
468 ani_object on_object;
469 if (ANI_OK != env->Object_New(cls, ctor, &on_object, reinterpret_cast<ani_ref>(nativeOn))) {
470 HiLog::Error(LABEL, "%{public}s New ON failed !!!", __func__);
471 }
472 return on_object;
473 }
474
within(ani_env * env,ani_object obj,ani_object on)475 static ani_ref within(ani_env *env, ani_object obj, ani_object on)
476 {
477 nlohmann::json params = nlohmann::json::array();
478 string p = aniStringToStdString(env, unwrapp(env, on, "nativeOn"));
479 params.push_back(p);
480 return createOn(env, obj, params, "On.within");
481 }
482
isBefore(ani_env * env,ani_object obj,ani_object on)483 static ani_ref isBefore(ani_env *env, ani_object obj, ani_object on)
484 {
485 nlohmann::json params = nlohmann::json::array();
486 string p = aniStringToStdString(env, unwrapp(env, on, "nativeOn"));
487 params.push_back(p);
488 return createOn(env, obj, params, "On.isBefore");
489 }
490
isAfter(ani_env * env,ani_object obj,ani_object on)491 static ani_ref isAfter(ani_env *env, ani_object obj, ani_object on)
492 {
493 nlohmann::json params = nlohmann::json::array();
494 string p = aniStringToStdString(env, unwrapp(env, on, "nativeOn"));
495 params.push_back(p);
496 return createOn(env, obj, params, "On.isAfter");
497 }
id(ani_env * env,ani_object obj,ani_string id,ani_enum_item pattern)498 static ani_ref id(ani_env *env, ani_object obj, ani_string id, ani_enum_item pattern)
499 {
500 nlohmann::json params = nlohmann::json::array();
501 params.push_back(aniStringToStdString(env, id));
502 ani_int enumValue;
503 env->EnumItem_GetValue_Int(pattern, &enumValue);
504 params.push_back(enumValue);
505 return createOn(env, obj, params, "On.id");
506 }
text(ani_env * env,ani_object obj,ani_string text,ani_enum_item pattern)507 static ani_ref text(ani_env *env, ani_object obj, ani_string text, ani_enum_item pattern)
508 {
509 nlohmann::json params = nlohmann::json::array();
510 params.push_back(aniStringToStdString(env, text));
511 ani_int enumValue;
512 env->EnumItem_GetValue_Int(pattern, &enumValue);
513 params.push_back(enumValue);
514 return createOn(env, obj, params, "On.text");
515 }
type(ani_env * env,ani_object obj,ani_string type,ani_enum_item pattern)516 static ani_ref type(ani_env *env, ani_object obj, ani_string type, ani_enum_item pattern)
517 {
518 nlohmann::json params = nlohmann::json::array();
519 params.push_back(aniStringToStdString(env, type));
520 ani_int enumValue;
521 env->EnumItem_GetValue_Int(pattern, &enumValue);
522 params.push_back(enumValue);
523 return createOn(env, obj, params, "On.type");
524 }
hint(ani_env * env,ani_object obj,ani_string text,ani_enum_item pattern)525 static ani_ref hint(ani_env *env, ani_object obj, ani_string text, ani_enum_item pattern)
526 {
527 nlohmann::json params = nlohmann::json::array();
528 params.push_back(aniStringToStdString(env, text));
529 ani_int enumValue;
530 env->EnumItem_GetValue_Int(pattern, &enumValue);
531 params.push_back(enumValue);
532 return createOn(env, obj, params, "On.hint");
533 }
description(ani_env * env,ani_object obj,ani_string text,ani_enum_item pattern)534 static ani_ref description(ani_env *env, ani_object obj, ani_string text, ani_enum_item pattern)
535 {
536 nlohmann::json params = nlohmann::json::array();
537 params.push_back(aniStringToStdString(env, text));
538 ani_int enumValue;
539 env->EnumItem_GetValue_Int(pattern, &enumValue);
540 params.push_back(enumValue);
541 return createOn(env, obj, params, "On.description");
542 }
inWindow(ani_env * env,ani_object obj,ani_string bundleName)543 static ani_ref inWindow(ani_env *env, ani_object obj, ani_string bundleName)
544 {
545 nlohmann::json params = nlohmann::json::array();
546 params.push_back(aniStringToStdString(env, bundleName));
547 return createOn(env, obj, params, "On.inWindow");
548 }
pushBool(ani_env * env,ani_object input,nlohmann::json & params)549 static void pushBool(ani_env *env, ani_object input, nlohmann::json ¶ms)
550 {
551 ani_boolean ret;
552 env->Reference_IsUndefined(reinterpret_cast<ani_ref>(input), &ret);
553 if (ret == ANI_FALSE) {
554 ani_boolean param;
555 HiLog::Info(LABEL, "%{public}s ani_boolean !!!", __func__);
556 env->Object_CallMethodByName_Boolean(input, "unboxed", ":Z", ¶m);
557 HiLog::Info(LABEL, "%{public}d ani_boolean !!!", static_cast<int>(param));
558 params.push_back(static_cast<bool>(param));
559 }
560 }
enabled(ani_env * env,ani_object obj,ani_object b)561 static ani_ref enabled(ani_env *env, ani_object obj, ani_object b)
562 {
563 nlohmann::json params = nlohmann::json::array();
564 pushBool(env, b, params);
565 return createOn(env, obj, params, "On.enabled");
566 }
focused(ani_env * env,ani_object obj,ani_object b)567 static ani_ref focused(ani_env *env, ani_object obj, ani_object b)
568 {
569 nlohmann::json params = nlohmann::json::array();
570 pushBool(env, b, params);
571 return createOn(env, obj, params, "On.focused");
572 }
clickable(ani_env * env,ani_object obj,ani_object b)573 static ani_ref clickable(ani_env *env, ani_object obj, ani_object b)
574 {
575 nlohmann::json params = nlohmann::json::array();
576 pushBool(env, b, params);
577 return createOn(env, obj, params, "On.clickable");
578 }
checked(ani_env * env,ani_object obj,ani_object b)579 static ani_ref checked(ani_env *env, ani_object obj, ani_object b)
580 {
581 nlohmann::json params = nlohmann::json::array();
582 pushBool(env, b, params);
583 return createOn(env, obj, params, "On.checked");
584 }
checkable(ani_env * env,ani_object obj,ani_object b)585 static ani_ref checkable(ani_env *env, ani_object obj, ani_object b)
586 {
587 nlohmann::json params = nlohmann::json::array();
588 pushBool(env, b, params);
589 return createOn(env, obj, params, "On.checkable");
590 }
longClickable(ani_env * env,ani_object obj,ani_object b)591 static ani_ref longClickable(ani_env *env, ani_object obj, ani_object b)
592 {
593 nlohmann::json params = nlohmann::json::array();
594 pushBool(env, b, params);
595 return createOn(env, obj, params, "On.longClickable");
596 }
selected(ani_env * env,ani_object obj,ani_object b)597 static ani_ref selected(ani_env *env, ani_object obj, ani_object b)
598 {
599 nlohmann::json params = nlohmann::json::array();
600 pushBool(env, b, params);
601 return createOn(env, obj, params, "On.selected");
602 }
scrollable(ani_env * env,ani_object obj,ani_object b)603 static ani_ref scrollable(ani_env *env, ani_object obj, ani_object b)
604 {
605 nlohmann::json params = nlohmann::json::array();
606 HiLog::Info(LABEL, "%{public}s scrollable !!!", __func__);
607 pushBool(env, b, params);
608 return createOn(env, obj, params, "On.scrollable");
609 }
610
BindOn(ani_env * env)611 static ani_boolean BindOn(ani_env *env)
612 {
613 ani_class cls = findCls(env, Builder::BuildClass({"@ohos", "UiTest", "On"}).Descriptor().c_str());
614 if (cls == nullptr) {
615 HiLog::Error(LABEL, "%{public}s Not found className !!!", __func__);
616 return false;
617 }
618 std::array methods = {
619 ani_native_function{"id", nullptr, reinterpret_cast<void *>(id)},
620 ani_native_function{"text", nullptr, reinterpret_cast<void *>(text)},
621 ani_native_function{"type", nullptr, reinterpret_cast<void *>(type)},
622 ani_native_function{"hint", nullptr, reinterpret_cast<void *>(hint)},
623 ani_native_function{"description", nullptr, reinterpret_cast<void *>(description)},
624 ani_native_function{"inWindow", nullptr, reinterpret_cast<void *>(inWindow)},
625 ani_native_function{"enabled", nullptr, reinterpret_cast<void *>(enabled)},
626 ani_native_function{"within", nullptr, reinterpret_cast<void *>(within)},
627 ani_native_function{"selected", nullptr, reinterpret_cast<void *>(selected)},
628 ani_native_function{"focused", nullptr, reinterpret_cast<void *>(focused)},
629 ani_native_function{"clickable", nullptr, reinterpret_cast<void *>(clickable)},
630 ani_native_function{"checkable", nullptr, reinterpret_cast<void *>(checkable)},
631 ani_native_function{"checked", nullptr, reinterpret_cast<void *>(checked)},
632 ani_native_function{"scrollable", nullptr, reinterpret_cast<void *>(scrollable)},
633 ani_native_function{"isBefore", nullptr, reinterpret_cast<void *>(isBefore)},
634 ani_native_function{"isAfter", nullptr, reinterpret_cast<void *>(isAfter)},
635 ani_native_function{"longClickable", nullptr, reinterpret_cast<void *>(longClickable)},
636 };
637 if (ANI_OK != env->Class_BindNativeMethods(cls, methods.data(), methods.size())) {
638 HiLog::Error(LABEL, "%{public}s Cannot bind native methods to !!!", __func__);
639 return false;
640 }
641 return true;
642 }
643
create(ani_env * env,ani_class clazz)644 static ani_ref create([[maybe_unused]] ani_env *env, [[maybe_unused]] ani_class clazz)
645 {
646 ani_class cls;
647 ani_ref nullref;
648 env->GetNull(&nullref);
649 if (ANI_OK != env->FindClass(Builder::BuildClass({"@ohos", "UiTest", "Driver"}).Descriptor().c_str(), &cls)) {
650 HiLog::Error(LABEL, "@ohos/uitest/Driver Not found !!!");
651 return nullref;
652 }
653 ani_method ctor = nullptr;
654 arkts::ani_signature::SignatureBuilder string_ctor{};
655 string_ctor.AddClass({"std", "core", "String"});
656 if (ANI_OK != env->Class_FindMethod(cls, "<ctor>", string_ctor.BuildSignatureDescriptor().c_str(), &ctor)) {
657 HiLog::Error(LABEL, "Driver Ctor Not found !!!");
658 return nullref;
659 }
660 ApiCallInfo callInfo_;
661 ApiReplyInfo reply_;
662 callInfo_.apiId_ = "Driver.create";
663 Transact(callInfo_, reply_);
664 ani_ref nativeDriver = UnmarshalReply(env, callInfo_, reply_);
665 if (nativeDriver == nullptr) {
666 return nullref;
667 }
668 ani_object driver_object;
669 if (ANI_OK != env->Object_New(cls, ctor, &driver_object, reinterpret_cast<ani_object>(nativeDriver))) {
670 HiLog::Error(LABEL, "%{public}s New Driver Failed!!!", __func__);
671 }
672 return driver_object;
673 }
674
delayMsSync(ani_env * env,ani_object obj,ani_double t)675 static ani_boolean delayMsSync(ani_env *env, ani_object obj, ani_double t)
676 {
677 ApiCallInfo callInfo_;
678 ApiReplyInfo reply_;
679 callInfo_.apiId_ = "Driver.delayMs";
680 callInfo_.paramList_.push_back(int(t));
681 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
682 Transact(callInfo_, reply_);
683 UnmarshalReply(env, callInfo_, reply_);
684 return true;
685 }
686
findComponentSync(ani_env * env,ani_object obj,ani_object on_obj)687 static ani_ref findComponentSync(ani_env *env, ani_object obj, ani_object on_obj)
688 {
689 ApiCallInfo callInfo_;
690 ApiReplyInfo reply_;
691 callInfo_.apiId_ = "Driver.findComponent";
692 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
693 callInfo_.paramList_.push_back(aniStringToStdString(env, unwrapp(env, on_obj, "nativeOn")));
694 Transact(callInfo_, reply_);
695 ani_ref nativeComponent = UnmarshalReply(env, callInfo_, reply_);
696 if (nativeComponent == nullptr) {
697 return nativeComponent;
698 }
699 ani_object com_obj;
700 ani_class cls = findCls(env, Builder::BuildClass({"@ohos", "UiTest", "Component"}).Descriptor().c_str());
701 ani_method ctor = nullptr;
702 if (cls != nullptr) {
703 arkts::ani_signature::SignatureBuilder string_ctor{};
704 string_ctor.AddClass({"std", "core", "String"});
705 ctor = findCtorMethod(env, cls, string_ctor.BuildSignatureDescriptor().c_str());
706 }
707 if (cls == nullptr || ctor == nullptr) {
708 return nullptr;
709 }
710 if (ANI_OK != env->Object_New(cls, ctor, &com_obj, reinterpret_cast<ani_object>(nativeComponent))) {
711 HiLog::Error(LABEL, "%{public}s New Component Failed !!!", __func__);
712 }
713 return com_obj;
714 }
715
findComponentsSync(ani_env * env,ani_object obj,ani_object on_obj)716 static ani_object findComponentsSync(ani_env *env, ani_object obj, ani_object on_obj)
717 {
718 ApiCallInfo callInfo_;
719 ApiReplyInfo reply_;
720 callInfo_.apiId_ = "Driver.findComponents";
721 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
722 callInfo_.paramList_.push_back(aniStringToStdString(env, unwrapp(env, on_obj, "nativeOn")));
723 Transact(callInfo_, reply_);
724 ani_object nativeComponents = reinterpret_cast<ani_object>(UnmarshalReply(env, callInfo_, reply_));
725 return nativeComponents;
726 }
727
assertComponentExistSync(ani_env * env,ani_object obj,ani_object on_obj)728 static ani_boolean assertComponentExistSync(ani_env *env, ani_object obj, ani_object on_obj)
729 {
730 ApiCallInfo callInfo_;
731 ApiReplyInfo reply_;
732 callInfo_.apiId_ = "Driver.assertComponentExist";
733 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
734 callInfo_.paramList_.push_back(aniStringToStdString(env, unwrapp(env, on_obj, "nativeOn")));
735 Transact(callInfo_, reply_);
736 UnmarshalReply(env, callInfo_, reply_);
737 return true;
738 }
739
performClick(ani_env * env,ani_object obj,ani_double x,ani_double y,string api)740 static void performClick(ani_env *env, ani_object obj, ani_double x, ani_double y, string api)
741 {
742 ApiCallInfo callInfo_;
743 ApiReplyInfo reply_;
744 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
745 callInfo_.apiId_ = api;
746 callInfo_.paramList_.push_back(int(x));
747 callInfo_.paramList_.push_back(int(y));
748 Transact(callInfo_, reply_);
749 UnmarshalReply(env, callInfo_, reply_);
750 }
751
clickSync(ani_env * env,ani_object obj,ani_double x,ani_double y)752 static ani_boolean clickSync(ani_env *env, ani_object obj, ani_double x, ani_double y)
753 {
754 string api_name = "Driver.click";
755 performClick(env, obj, x, y, api_name);
756 return true;
757 }
758
doubleClickSync(ani_env * env,ani_object obj,ani_double x,ani_double y)759 static ani_boolean doubleClickSync(ani_env *env, ani_object obj, ani_double x, ani_double y)
760 {
761 string api_name = "Driver.doubleClick";
762 performClick(env, obj, x, y, api_name);
763 return true;
764 }
765
longClickSync(ani_env * env,ani_object obj,ani_double x,ani_double y)766 static ani_boolean longClickSync(ani_env *env, ani_object obj, ani_double x, ani_double y)
767 {
768 string api_name = "Driver.longClick";
769 performClick(env, obj, x, y, api_name);
770 return true;
771 }
772
performDriver(ani_env * env,ani_object obj,string api)773 static ani_ref performDriver(ani_env *env, ani_object obj, string api)
774 {
775 ApiCallInfo callInfo_;
776 ApiReplyInfo reply_;
777 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
778 callInfo_.apiId_ = api;
779 Transact(callInfo_, reply_);
780 return UnmarshalReply(env, callInfo_, reply_);
781 }
782
pressBackSync(ani_env * env,ani_object obj)783 static ani_boolean pressBackSync(ani_env *env, ani_object obj)
784 {
785 performDriver(env, obj, "Driver.pressBack");
786 return true;
787 }
788
pressBackWithDisplayIdSync(ani_env * env,ani_object obj,ani_double displayId)789 static ani_boolean pressBackWithDisplayIdSync(ani_env *env, ani_object obj, ani_double displayId)
790 {
791 ApiCallInfo callInfo_;
792 ApiReplyInfo reply_;
793 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
794 callInfo_.apiId_ = "Driver.pressBack";
795 callInfo_.paramList_.push_back(int(displayId));
796 Transact(callInfo_, reply_);
797 UnmarshalReply(env, callInfo_, reply_);
798 return true;
799 }
800
flingSync(ani_env * env,ani_object obj,ani_object f,ani_object t,ani_double stepLen,ani_double speed)801 static ani_boolean flingSync(ani_env *env, ani_object obj, ani_object f, ani_object t, ani_double stepLen, ani_double speed)
802 {
803 ApiCallInfo callInfo_;
804 ApiReplyInfo reply_;
805 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
806 callInfo_.apiId_ = "Driver.fling";
807 auto from = getPoint(env, f);
808 auto to = getPoint(env, t);
809 callInfo_.paramList_.push_back(from);
810 callInfo_.paramList_.push_back(to);
811 callInfo_.paramList_.push_back(int(stepLen));
812 callInfo_.paramList_.push_back(int(speed));
813 Transact(callInfo_, reply_);
814 UnmarshalReply(env, callInfo_, reply_);
815 return true;
816 }
817
flingSyncDirection(ani_env * env,ani_object obj,ani_enum_item direction,ani_double speed)818 static ani_boolean flingSyncDirection(ani_env *env, ani_object obj, ani_enum_item direction, ani_double speed)
819 {
820 ApiCallInfo callInfo_;
821 ApiReplyInfo reply_;
822 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
823 callInfo_.apiId_ = "Driver.fling";
824 ani_int enumValue;
825 env->EnumItem_GetValue_Int(direction, &enumValue);
826 callInfo_.paramList_.push_back(enumValue);
827 callInfo_.paramList_.push_back(int(speed));
828 Transact(callInfo_, reply_);
829 UnmarshalReply(env, callInfo_, reply_);
830 return true;
831 }
832
flingWithDisplayIdSync(ani_env * env,ani_object obj,ani_enum_item direction,ani_double speed,ani_double displayId)833 static ani_boolean flingWithDisplayIdSync(ani_env *env, ani_object obj, ani_enum_item direction, ani_double speed,
834 ani_double displayId)
835 {
836 ApiCallInfo callInfo_;
837 ApiReplyInfo reply_;
838 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
839 callInfo_.apiId_ = "Driver.fling";
840 ani_int enumValue;
841 env->EnumItem_GetValue_Int(direction, &enumValue);
842 callInfo_.paramList_.push_back(enumValue);
843 callInfo_.paramList_.push_back(int(speed));
844 callInfo_.paramList_.push_back(int(displayId));
845 Transact(callInfo_, reply_);
846 UnmarshalReply(env, callInfo_, reply_);
847 return true;
848 }
849
inputTextSync(ani_env * env,ani_object obj,ani_object p,ani_string text)850 static ani_boolean inputTextSync(ani_env *env, ani_object obj, ani_object p, ani_string text)
851 {
852 ApiCallInfo callInfo_;
853 ApiReplyInfo reply_;
854 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
855 callInfo_.apiId_ = "Driver.inputText";
856 auto point = getPoint(env, p);
857 callInfo_.paramList_.push_back(point);
858 callInfo_.paramList_.push_back(aniStringToStdString(env, text));
859 Transact(callInfo_, reply_);
860 UnmarshalReply(env, callInfo_, reply_);
861 return true;
862 }
863
inputTextWithModeSync(ani_env * env,ani_object obj,ani_object p,ani_string text,ani_object inputMode)864 static ani_boolean inputTextWithModeSync(ani_env *env, ani_object obj, ani_object p, ani_string text,
865 ani_object inputMode)
866 {
867 ApiCallInfo callInfo_;
868 ApiReplyInfo reply_;
869 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
870 callInfo_.apiId_ = "Driver.inputText";
871 auto point = getPoint(env, p);
872 callInfo_.paramList_.push_back(point);
873 callInfo_.paramList_.push_back(aniStringToStdString(env, text));
874 callInfo_.paramList_.push_back(getInputTextModeOptions(env, inputMode));
875 Transact(callInfo_, reply_);
876 UnmarshalReply(env, callInfo_, reply_);
877 return true;
878 }
879
swipeSync(ani_env * env,ani_object obj,ani_double x1,ani_double y1,ani_double x2,ani_double y2,ani_object speed)880 static ani_boolean swipeSync(ani_env *env, ani_object obj, ani_double x1, ani_double y1, ani_double x2, ani_double y2, ani_object speed)
881 {
882 ApiCallInfo callInfo_;
883 ApiReplyInfo reply_;
884 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
885 callInfo_.apiId_ = "Driver.swipe";
886 callInfo_.paramList_.push_back(int(x1));
887 callInfo_.paramList_.push_back(int(y1));
888 callInfo_.paramList_.push_back(int(x2));
889 callInfo_.paramList_.push_back(int(y2));
890 pushParam(env, speed, callInfo_, true);
891 Transact(callInfo_, reply_);
892 UnmarshalReply(env, callInfo_, reply_);
893 return true;
894 }
895
dragSync(ani_env * env,ani_object obj,ani_double x1,ani_double y1,ani_double x2,ani_double y2,ani_object speed)896 static ani_boolean dragSync(ani_env *env, ani_object obj, ani_double x1, ani_double y1, ani_double x2, ani_double y2, ani_object speed)
897 {
898 ApiCallInfo callInfo_;
899 ApiReplyInfo reply_;
900 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
901 callInfo_.apiId_ = "Driver.drag";
902 callInfo_.paramList_.push_back(int(x1));
903 callInfo_.paramList_.push_back(int(y1));
904 callInfo_.paramList_.push_back(int(x2));
905 callInfo_.paramList_.push_back(int(y2));
906 pushParam(env, speed, callInfo_, true);
907 Transact(callInfo_, reply_);
908 UnmarshalReply(env, callInfo_, reply_);
909 return true;
910 }
911
getWindowFilter(ani_env * env,ani_object f)912 static json getWindowFilter(ani_env *env, ani_object f)
913 {
914 auto filter = json();
915 string list[] = {"bundleName", "title", "focused", "active"};
916 for (int i = 0; i < 4; i++) {
917 char *cstr = new char[list[i].length() + 1];
918 strcpy(cstr, list[i].c_str());
919 ani_ref value;
920 if (env->Object_GetPropertyByName_Ref(f, cstr, &value) != ANI_OK) {
921 HiLog::Error(LABEL, "GetPropertyByName %{public}s fail", cstr);
922 continue;
923 }
924 ani_boolean ret;
925 env->Reference_IsUndefined(value, &ret);
926 if (ret == ANI_TRUE) {
927 continue;
928 }
929 if (i < 2) {
930 filter[list[i]] = aniStringToStdString(env, reinterpret_cast<ani_string>(value));
931 } else {
932 ani_boolean b;
933 compareAndReport(ANI_OK,
934 env->Object_CallMethodByName_Boolean(static_cast<ani_object>(value), "unboxed", nullptr, &b),
935 "CallMethodByName_Boolean Failed",
936 "get boolean value");
937 HiLog::Info(LABEL, "%{public}d ani_boolean !!!", static_cast<int>(b));
938 filter[list[i]] = static_cast<bool>(b);
939 }
940 }
941 return filter;
942 }
943
findWindowSync(ani_env * env,ani_object obj,ani_object filter)944 static ani_object findWindowSync(ani_env *env, ani_object obj, ani_object filter)
945 {
946 ApiCallInfo callInfo_;
947 ApiReplyInfo reply_;
948 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
949 callInfo_.apiId_ = "Driver.findWindow";
950 callInfo_.paramList_.push_back(getWindowFilter(env, filter));
951 Transact(callInfo_, reply_);
952 ani_ref nativeWindow = UnmarshalReply(env, callInfo_, reply_);
953 if (nativeWindow == nullptr) {
954 return reinterpret_cast<ani_object>(nativeWindow);
955 }
956 ani_object window_obj;
957 ani_class cls = findCls(env, Builder::BuildClass({"@ohos", "UiTest", "UiWindow"}).Descriptor().c_str());
958 ani_method ctor = nullptr;
959 if (cls != nullptr) {
960 arkts::ani_signature::SignatureBuilder string_ctor{};
961 string_ctor.AddClass({"std", "core", "String"});
962 ctor = findCtorMethod(env, cls, string_ctor.BuildSignatureDescriptor().c_str());
963 }
964 if (cls == nullptr || ctor == nullptr) {
965 return nullptr;
966 }
967 if (ANI_OK != env->Object_New(cls, ctor, &window_obj, reinterpret_cast<ani_object>(nativeWindow))) {
968 HiLog::Error(LABEL, "New UiWindow failed");
969 }
970 return window_obj;
971 }
972
createUIEventObserverSync(ani_env * env,ani_object obj)973 static ani_object createUIEventObserverSync(ani_env *env, ani_object obj)
974 {
975 ApiCallInfo callInfo_;
976 ApiReplyInfo reply_;
977 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
978 callInfo_.apiId_ = "Driver.createUIEventObserver";
979 Transact(callInfo_, reply_);
980 ani_ref nativeUIEventObserver = UnmarshalReply(env, callInfo_, reply_);
981 if (nativeUIEventObserver == nullptr) {
982 return nullptr;
983 }
984 ani_object observer_obj;
985 ani_class cls = findCls(env, Builder::BuildClass({"@ohos", "UiTest", "UIEventObserver"}).Descriptor().c_str());
986 ani_method ctor = nullptr;
987 if (cls != nullptr) {
988 arkts::ani_signature::SignatureBuilder string_ctor{};
989 string_ctor.AddClass({"std", "core", "String"});
990 ctor = findCtorMethod(env, cls, string_ctor.BuildSignatureDescriptor().c_str());
991 }
992 if (cls == nullptr || ctor == nullptr) {
993 HiLog::Error(LABEL, "Not found className/ctor: %{public}s", "UIEventObserver");
994 return nullptr;
995 }
996 if (ANI_OK != env->Object_New(cls, ctor, &observer_obj, reinterpret_cast<ani_object>(nativeUIEventObserver))) {
997 HiLog::Error(LABEL, "New UIEventObserver fail");
998 }
999 return observer_obj;
1000 }
1001
injectMultiPointerActionSync(ani_env * env,ani_object obj,ani_object pointers,ani_object speed)1002 static ani_boolean injectMultiPointerActionSync(ani_env *env, ani_object obj, ani_object pointers, ani_object speed)
1003 {
1004 ApiCallInfo callInfo_;
1005 ApiReplyInfo reply_;
1006 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
1007 callInfo_.apiId_ = "Driver.injectMultiPointerAction";
1008 callInfo_.paramList_.push_back(aniStringToStdString(env, unwrapp(env, pointers, "nativePointerMatrix")));
1009 pushParam(env, speed, callInfo_, true);
1010 Transact(callInfo_, reply_);
1011 UnmarshalReply(env, callInfo_, reply_);
1012 return true;
1013 }
1014
triggerKeySync(ani_env * env,ani_object obj,ani_double keyCode)1015 static ani_boolean triggerKeySync(ani_env *env, ani_object obj, ani_double keyCode)
1016 {
1017 ApiCallInfo callInfo_;
1018 ApiReplyInfo reply_;
1019 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
1020 callInfo_.apiId_ = "Driver.triggerKey";
1021 callInfo_.paramList_.push_back(int(keyCode));
1022 Transact(callInfo_, reply_);
1023 UnmarshalReply(env, callInfo_, reply_);
1024 return true;
1025 }
1026
triggerKeyWithDisplayIdSync(ani_env * env,ani_object obj,ani_double keyCode,ani_double displayId)1027 static ani_boolean triggerKeyWithDisplayIdSync(ani_env *env, ani_object obj, ani_double keyCode, ani_double displayId)
1028 {
1029 ApiCallInfo callInfo_;
1030 ApiReplyInfo reply_;
1031 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
1032 callInfo_.apiId_ = "Driver.triggerKey";
1033 callInfo_.paramList_.push_back(int(keyCode));
1034 callInfo_.paramList_.push_back(int(displayId));
1035 Transact(callInfo_, reply_);
1036 UnmarshalReply(env, callInfo_, reply_);
1037 return true;
1038 }
1039
wakeUpDisplaySync(ani_env * env,ani_object obj)1040 static ani_boolean wakeUpDisplaySync(ani_env *env, ani_object obj)
1041 {
1042 performDriver(env, obj, "Driver.wakeUpDisplay");
1043 return true;
1044 }
1045
pressHomeSync(ani_env * env,ani_object obj)1046 static ani_boolean pressHomeSync(ani_env *env, ani_object obj)
1047 {
1048 performDriver(env, obj, "Driver.pressHome");
1049 return true;
1050 }
1051
pressHomeWithDisplayIdSync(ani_env * env,ani_object obj,ani_double displayId)1052 static ani_boolean pressHomeWithDisplayIdSync(ani_env *env, ani_object obj, ani_double displayId)
1053 {
1054 ApiCallInfo callInfo_;
1055 ApiReplyInfo reply_;
1056 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
1057 callInfo_.apiId_ = "Driver.pressHome";
1058 callInfo_.paramList_.push_back(int(displayId));
1059 Transact(callInfo_, reply_);
1060 UnmarshalReply(env, callInfo_, reply_);
1061 return true;
1062 }
1063
getDisplaySizeSync(ani_env * env,ani_object obj)1064 static ani_ref getDisplaySizeSync(ani_env *env, ani_object obj)
1065 {
1066 ApiCallInfo callInfo_;
1067 ApiReplyInfo reply_;
1068 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
1069 callInfo_.apiId_ = "Driver.getDisplaySize";
1070 Transact(callInfo_, reply_);
1071 ani_ref result = UnmarshalReply(env, callInfo_, reply_);
1072 if (result == nullptr) {
1073 return nullptr;
1074 }
1075 ani_object p = newPoint(env, obj, reply_.resultValue_["x"], reply_.resultValue_["y"]);
1076 return p;
1077 }
1078
getDisplaySizeWithDisplayIdSync(ani_env * env,ani_object obj,ani_double displayId)1079 static ani_ref getDisplaySizeWithDisplayIdSync(ani_env *env, ani_object obj, ani_double displayId)
1080 {
1081 ApiCallInfo callInfo_;
1082 ApiReplyInfo reply_;
1083 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
1084 callInfo_.apiId_ = "Driver.getDisplaySize";
1085 callInfo_.paramList_.push_back(int(displayId));
1086 Transact(callInfo_, reply_);
1087 ani_ref result = UnmarshalReply(env, callInfo_, reply_);
1088 if (result == nullptr) {
1089 return nullptr;
1090 }
1091 ani_object p = newPoint(env, obj, reply_.resultValue_["x"], reply_.resultValue_["y"]);
1092 return p;
1093 }
1094
getDisplayDensitySync(ani_env * env,ani_object obj)1095 static ani_object getDisplayDensitySync(ani_env *env, ani_object obj)
1096 {
1097 ApiCallInfo callInfo_;
1098 ApiReplyInfo reply_;
1099 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
1100 callInfo_.apiId_ = "Driver.getDisplayDensity";
1101 Transact(callInfo_, reply_);
1102 ani_ref result = UnmarshalReply(env, callInfo_, reply_);
1103 if (result == nullptr) {
1104 return nullptr;
1105 }
1106 ani_object p = newPoint(env, obj, reply_.resultValue_["x"], reply_.resultValue_["y"]);
1107 return p;
1108 }
1109
getDisplayRotationSync(ani_env * env,ani_object obj)1110 static ani_object getDisplayRotationSync(ani_env *env, ani_object obj)
1111 {
1112 ApiCallInfo callInfo_;
1113 ApiReplyInfo reply_;
1114 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
1115 callInfo_.apiId_ = "Driver.getDisplayRotation";
1116 Transact(callInfo_, reply_);
1117 ani_ref result = UnmarshalReply(env, callInfo_, reply_);
1118 if (result == nullptr) {
1119 return nullptr;
1120 }
1121 ani_enum enumType;
1122 if (ANI_OK != env->FindEnum(Builder::BuildEnum({"@ohos", "UiTest", "DisplayRotation"}).Descriptor().c_str(), &enumType)) {
1123 HiLog::Error(LABEL, "Find Enum Faild: %{public}s", __func__);
1124 }
1125 ani_enum_item enumItem;
1126 auto index = static_cast<uint8_t>(reply_.resultValue_.get<int>());
1127 env->Enum_GetEnumItemByIndex(enumType, index, &enumItem);
1128 HiLog::Info(LABEL, " DisplayRotation: %{public}d ", index);
1129 return enumItem;
1130 }
1131
waitForIdleSync(ani_env * env,ani_object obj,ani_double idleTime,ani_double timeout)1132 static ani_boolean waitForIdleSync(ani_env *env, ani_object obj, ani_double idleTime, ani_double timeout)
1133 {
1134 ApiCallInfo callInfo_;
1135 ApiReplyInfo reply_;
1136 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
1137 callInfo_.apiId_ = "Driver.waitForIdle";
1138 callInfo_.paramList_.push_back(int(idleTime));
1139 callInfo_.paramList_.push_back(int(timeout));
1140 Transact(callInfo_, reply_);
1141 UnmarshalReply(env, callInfo_, reply_);
1142 return true;
1143 }
1144
waitForComponentSync(ani_env * env,ani_object obj,ani_object on_obj,ani_double time)1145 static ani_object waitForComponentSync(ani_env *env, ani_object obj, ani_object on_obj, ani_double time)
1146 {
1147 ApiCallInfo callInfo_;
1148 ApiReplyInfo reply_;
1149 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
1150 callInfo_.apiId_ = "Driver.waitForComponent";
1151 callInfo_.paramList_.push_back(aniStringToStdString(env, unwrapp(env, on_obj, "nativeOn")));
1152 callInfo_.paramList_.push_back(int(time));
1153 Transact(callInfo_, reply_);
1154 ani_ref nativeComponent = UnmarshalReply(env, callInfo_, reply_);
1155 if (nativeComponent == nullptr) {
1156 return reinterpret_cast<ani_object>(nativeComponent);
1157 }
1158 ani_object component_obj;
1159 ani_class cls = findCls(env, Builder::BuildClass({"@ohos", "UiTest", "Component"}).Descriptor().c_str());
1160 ani_method ctor = nullptr;
1161 if (cls != nullptr) {
1162 arkts::ani_signature::SignatureBuilder string_ctor{};
1163 string_ctor.AddClass({"std", "core", "String"});
1164 ctor = findCtorMethod(env, cls, string_ctor.BuildSignatureDescriptor().c_str());
1165 }
1166 if (cls == nullptr || ctor == nullptr) {
1167 return nullptr;
1168 }
1169 if (ANI_OK != env->Object_New(cls, ctor, &component_obj, reinterpret_cast<ani_object>(nativeComponent))) {
1170 HiLog::Error(LABEL, "New Component fail: %{public}s", __func__);
1171 }
1172 return component_obj;
1173 }
1174
triggerCombineKeysSync(ani_env * env,ani_object obj,ani_double key0,ani_double key1,ani_object key2)1175 static ani_boolean triggerCombineKeysSync(ani_env *env, ani_object obj, ani_double key0, ani_double key1, ani_object key2)
1176 {
1177 ApiCallInfo callInfo_;
1178 ApiReplyInfo reply_;
1179 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
1180 callInfo_.apiId_ = "Driver.triggerCombineKeys";
1181 callInfo_.paramList_.push_back(int(key0));
1182 callInfo_.paramList_.push_back(int(key1));
1183 pushParam(env, key2, callInfo_, true);
1184 Transact(callInfo_, reply_);
1185 UnmarshalReply(env, callInfo_, reply_);
1186 return true;
1187 }
1188
setDisplayRotationSync(ani_env * env,ani_object obj,ani_enum_item rotation)1189 static ani_boolean setDisplayRotationSync(ani_env *env, ani_object obj, ani_enum_item rotation)
1190 {
1191 ApiCallInfo callInfo_;
1192 ApiReplyInfo reply_;
1193 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
1194 callInfo_.apiId_ = "Driver.setDisplayRotation";
1195 ani_int enumValue;
1196 env->EnumItem_GetValue_Int(rotation, &enumValue);
1197 callInfo_.paramList_.push_back(enumValue);
1198 Transact(callInfo_, reply_);
1199 UnmarshalReply(env, callInfo_, reply_);
1200 return true;
1201 }
1202
screenCaptureSync(ani_env * env,ani_object obj,ani_string path,ani_object rect)1203 static ani_boolean screenCaptureSync(ani_env *env, ani_object obj, ani_string path, ani_object rect)
1204 {
1205 ApiCallInfo callInfo_;
1206 ApiReplyInfo reply_;
1207 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
1208 callInfo_.apiId_ = "Driver.screenCapture";
1209 string savePath = aniStringToStdString(env, path);
1210 HiLog::Info(LABEL, "savePath: %{public}s", savePath.c_str());
1211 auto fd = open(savePath.c_str(), O_RDWR | O_CREAT, 0666);
1212 if (fd == -1) {
1213 return false;
1214 }
1215 HiLog::Info(LABEL, "savePath: %{public}d", fd);
1216 callInfo_.paramList_[INDEX_ZERO] = fd;
1217 callInfo_.paramList_[INDEX_ONE] = getRect(env, rect);
1218 callInfo_.fdParamIndex_ = INDEX_ZERO;
1219 Transact(callInfo_, reply_);
1220 ani_ref result = UnmarshalReply(env, callInfo_, reply_);
1221 if (result == nullptr) {
1222 return false;
1223 }
1224 return reply_.resultValue_.get<bool>();
1225 }
1226
screenCapSync(ani_env * env,ani_object obj,ani_string path)1227 static ani_boolean screenCapSync(ani_env *env, ani_object obj, ani_string path)
1228 {
1229 ApiCallInfo callInfo_;
1230 ApiReplyInfo reply_;
1231 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
1232 callInfo_.apiId_ = "Driver.screenCap";
1233 string savePath = aniStringToStdString(env, path);
1234 HiLog::Info(LABEL, "savePath: %{public}s", savePath.c_str());
1235 int32_t fd = open(savePath.c_str(), O_RDWR | O_CREAT, 0666);
1236 if (fd == -1) {
1237 return false;
1238 }
1239 HiLog::Info(LABEL, "savePath: %{public}d", fd);
1240 callInfo_.paramList_[INDEX_ZERO] = fd;
1241 callInfo_.fdParamIndex_ = INDEX_ZERO;
1242 Transact(callInfo_, reply_);
1243 ani_ref result = UnmarshalReply(env, callInfo_, reply_);
1244 if (result == nullptr) {
1245 return false;
1246 }
1247 return reply_.resultValue_.get<bool>();
1248 }
1249
setDisplayRotationEnabledSync(ani_env * env,ani_object obj,ani_boolean enable)1250 static ani_boolean setDisplayRotationEnabledSync(ani_env *env, ani_object obj, ani_boolean enable)
1251 {
1252 ApiCallInfo callInfo_;
1253 ApiReplyInfo reply_;
1254 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
1255 callInfo_.apiId_ = "Driver.setDisplayRotationEnabled";
1256 callInfo_.paramList_.push_back(static_cast<bool>(enable));
1257 Transact(callInfo_, reply_);
1258 UnmarshalReply(env, callInfo_, reply_);
1259 return true;
1260 }
1261
penSwipeSync(ani_env * env,ani_object obj,ani_object f,ani_object t,ani_object speed,ani_object pressure)1262 static ani_boolean penSwipeSync(ani_env *env, ani_object obj, ani_object f, ani_object t, ani_object speed, ani_object pressure)
1263 {
1264 ApiCallInfo callInfo_;
1265 ApiReplyInfo reply_;
1266 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
1267 callInfo_.apiId_ = "Driver.penSwipe";
1268 auto from = getPoint(env, f);
1269 auto to = getPoint(env, t);
1270 callInfo_.paramList_.push_back(from);
1271 callInfo_.paramList_.push_back(to);
1272 pushParam(env, speed, callInfo_, true);
1273 pushParam(env, pressure, callInfo_, false);
1274 Transact(callInfo_, reply_);
1275 UnmarshalReply(env, callInfo_, reply_);
1276 return true;
1277 }
1278
penClickSync(ani_env * env,ani_object obj,ani_object p)1279 static ani_boolean penClickSync(ani_env *env, ani_object obj, ani_object p)
1280 {
1281 ApiCallInfo callInfo_;
1282 ApiReplyInfo reply_;
1283 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
1284 callInfo_.apiId_ = "Driver.penClick";
1285 auto point = getPoint(env, p);
1286 callInfo_.paramList_.push_back(point);
1287 Transact(callInfo_, reply_);
1288 UnmarshalReply(env, callInfo_, reply_);
1289 return true;
1290 }
1291
penDoubleClickSync(ani_env * env,ani_object obj,ani_object p)1292 static ani_boolean penDoubleClickSync(ani_env *env, ani_object obj, ani_object p)
1293 {
1294 ApiCallInfo callInfo_;
1295 ApiReplyInfo reply_;
1296 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
1297 callInfo_.apiId_ = "Driver.penDoubleClick";
1298 auto point = getPoint(env, p);
1299 callInfo_.paramList_.push_back(point);
1300 Transact(callInfo_, reply_);
1301 UnmarshalReply(env, callInfo_, reply_);
1302 return true;
1303 }
1304
penLongClickSync(ani_env * env,ani_object obj,ani_object p,ani_object pressure)1305 static ani_boolean penLongClickSync(ani_env *env, ani_object obj, ani_object p, ani_object pressure)
1306 {
1307 ApiCallInfo callInfo_;
1308 ApiReplyInfo reply_;
1309 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
1310 callInfo_.apiId_ = "Driver.penLongClick";
1311 auto point = getPoint(env, p);
1312 callInfo_.paramList_.push_back(point);
1313 pushParam(env, pressure, callInfo_, false);
1314 Transact(callInfo_, reply_);
1315 UnmarshalReply(env, callInfo_, reply_);
1316 return true;
1317 }
1318
mouseScrollSync(ani_env * env,ani_object obj,ani_object p,ani_boolean down,ani_double dis,ani_object key1,ani_object key2,ani_object speed)1319 static ani_boolean mouseScrollSync(ani_env *env, ani_object obj, ani_object p, ani_boolean down, ani_double dis, ani_object key1, ani_object key2,
1320 ani_object speed)
1321 {
1322 ApiCallInfo callInfo_;
1323 ApiReplyInfo reply_;
1324 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
1325 callInfo_.apiId_ = "Driver.mouseScroll";
1326 auto point = getPoint(env, p);
1327 callInfo_.paramList_.push_back(point);
1328 callInfo_.paramList_.push_back(static_cast<bool>(down));
1329 callInfo_.paramList_.push_back(int(dis));
1330 pushParam(env, key1, callInfo_, true);
1331 pushParam(env, key2, callInfo_, true);
1332 pushParam(env, speed, callInfo_, true);
1333 Transact(callInfo_, reply_);
1334 UnmarshalReply(env, callInfo_, reply_);
1335 return true;
1336 }
1337
mouseMoveWithTrackSync(ani_env * env,ani_object obj,ani_object f,ani_object t,ani_object speed)1338 static ani_boolean mouseMoveWithTrackSync(ani_env *env, ani_object obj, ani_object f, ani_object t, ani_object speed)
1339 {
1340 ApiCallInfo callInfo_;
1341 ApiReplyInfo reply_;
1342 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
1343 callInfo_.apiId_ = "Driver.mouseMoveWithTrack";
1344 auto from = getPoint(env, f);
1345 auto to = getPoint(env, t);
1346 callInfo_.paramList_.push_back(from);
1347 callInfo_.paramList_.push_back(to);
1348 pushParam(env, speed, callInfo_, true);
1349 Transact(callInfo_, reply_);
1350 UnmarshalReply(env, callInfo_, reply_);
1351 return true;
1352 }
1353
mouseDragSync(ani_env * env,ani_object obj,ani_object f,ani_object t,ani_object speed)1354 static ani_boolean mouseDragSync(ani_env *env, ani_object obj, ani_object f, ani_object t, ani_object speed)
1355 {
1356 ApiCallInfo callInfo_;
1357 ApiReplyInfo reply_;
1358 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
1359 callInfo_.apiId_ = "Driver.mouseDrag";
1360 auto from = getPoint(env, f);
1361 auto to = getPoint(env, t);
1362 callInfo_.paramList_.push_back(from);
1363 callInfo_.paramList_.push_back(to);
1364 pushParam(env, speed, callInfo_, true);
1365 Transact(callInfo_, reply_);
1366 UnmarshalReply(env, callInfo_, reply_);
1367 return true;
1368 }
1369
mouseMoveToSync(ani_env * env,ani_object obj,ani_object p)1370 static ani_boolean mouseMoveToSync(ani_env *env, ani_object obj, ani_object p)
1371 {
1372 ApiCallInfo callInfo_;
1373 ApiReplyInfo reply_;
1374 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
1375 callInfo_.apiId_ = "Driver.mouseMoveTo";
1376 auto point = getPoint(env, p);
1377 callInfo_.paramList_.push_back(point);
1378 Transact(callInfo_, reply_);
1379 UnmarshalReply(env, callInfo_, reply_);
1380 return true;
1381 }
1382
mouseClickSync(ani_env * env,ani_object obj,ani_object p,ani_enum_item btnId,ani_object key1,ani_object key2)1383 static ani_boolean mouseClickSync(ani_env *env, ani_object obj, ani_object p, ani_enum_item btnId, ani_object key1, ani_object key2)
1384 {
1385 ApiCallInfo callInfo_;
1386 ApiReplyInfo reply_;
1387 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
1388 callInfo_.apiId_ = "Driver.mouseClick";
1389 auto point = getPoint(env, p);
1390 callInfo_.paramList_.push_back(point);
1391 ani_int enumValue;
1392 env->EnumItem_GetValue_Int(btnId, &enumValue);
1393 callInfo_.paramList_.push_back(enumValue);
1394 pushParam(env, key1, callInfo_, true);
1395 pushParam(env, key2, callInfo_, true);
1396 Transact(callInfo_, reply_);
1397 UnmarshalReply(env, callInfo_, reply_);
1398 return true;
1399 }
1400
mouseDoubleClickSync(ani_env * env,ani_object obj,ani_object p,ani_enum_item btnId,ani_object key1,ani_object key2)1401 static ani_boolean mouseDoubleClickSync(ani_env *env, ani_object obj, ani_object p, ani_enum_item btnId, ani_object key1, ani_object key2)
1402 {
1403 ApiCallInfo callInfo_;
1404 ApiReplyInfo reply_;
1405 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
1406 callInfo_.apiId_ = "Driver.mouseDoubleClick";
1407 auto point = getPoint(env, p);
1408 callInfo_.paramList_.push_back(point);
1409 ani_int enumValue;
1410 env->EnumItem_GetValue_Int(btnId, &enumValue);
1411 callInfo_.paramList_.push_back(enumValue);
1412 pushParam(env, key1, callInfo_, true);
1413 pushParam(env, key2, callInfo_, true);
1414 Transact(callInfo_, reply_);
1415 UnmarshalReply(env, callInfo_, reply_);
1416 return true;
1417 }
1418
mouseLongClickSync(ani_env * env,ani_object obj,ani_object p,ani_enum_item btnId,ani_object key1,ani_object key2)1419 static ani_boolean mouseLongClickSync(ani_env *env, ani_object obj, ani_object p, ani_enum_item btnId, ani_object key1, ani_object key2)
1420 {
1421 ApiCallInfo callInfo_;
1422 ApiReplyInfo reply_;
1423 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
1424 callInfo_.apiId_ = "Driver.mouseLongClick";
1425 auto point = getPoint(env, p);
1426 callInfo_.paramList_.push_back(point);
1427 ani_int enumValue;
1428 env->EnumItem_GetValue_Int(btnId, &enumValue);
1429 callInfo_.paramList_.push_back(enumValue);
1430 pushParam(env, key1, callInfo_, true);
1431 pushParam(env, key2, callInfo_, true);
1432 Transact(callInfo_, reply_);
1433 UnmarshalReply(env, callInfo_, reply_);
1434 return true;
1435 }
1436
injectPenPointerActionSync(ani_env * env,ani_object obj,ani_object pointers,ani_object speed,ani_object pressure)1437 static ani_boolean injectPenPointerActionSync(ani_env *env, ani_object obj, ani_object pointers, ani_object speed, ani_object pressure)
1438 {
1439 ApiCallInfo callInfo_;
1440 ApiReplyInfo reply_;
1441 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
1442 callInfo_.apiId_ = "Driver.injectPenPointerAction";
1443 callInfo_.paramList_.push_back(aniStringToStdString(env, unwrapp(env, pointers, "nativePointerMatrix")));
1444 pushParam(env, speed, callInfo_, true);
1445 pushParam(env, pressure, callInfo_, false);
1446 Transact(callInfo_, reply_);
1447 UnmarshalReply(env, callInfo_, reply_);
1448 return true;
1449 }
1450
getTouchPadSwipeOptions(ani_env * env,ani_object f)1451 static json getTouchPadSwipeOptions(ani_env *env, ani_object f)
1452 {
1453 auto options = json();
1454 string list[] = {"stay", "speed"};
1455 for (int i = 0; i < TWO; i++) {
1456 char *cstr = new char[list[i].length() + 1];
1457 strcpy(cstr, list[i].c_str());
1458 ani_ref value;
1459 if (env->Object_GetPropertyByName_Ref(f, cstr, &value) != ANI_OK) {
1460 HiLog::Error(LABEL, "GetPropertyByName %{public}s fail", cstr);
1461 continue;
1462 }
1463 ani_boolean ret;
1464 env->Reference_IsUndefined(value, &ret);
1465 if (ret == ANI_TRUE) {
1466 continue;
1467 }
1468 if (i == ONE) {
1469 ani_double speed;
1470 compareAndReport(ANI_OK,
1471 env->Object_CallMethodByName_Double(static_cast<ani_object>(value), "unboxed", nullptr, &speed),
1472 "Object_CallMethodByName_Boolean Failed",
1473 "get boolean value");
1474 options[list[i]] = int(speed);
1475 } else {
1476 ani_boolean b;
1477 compareAndReport(ANI_OK,
1478 env->Object_CallMethodByName_Boolean(static_cast<ani_object>(value), "unboxed", nullptr, &b),
1479 "Object_CallMethodByName_Boolean Failed",
1480 "get boolean value");
1481 HiLog::Info(LABEL, "%{public}d ani_boolean !!!", static_cast<int>(b));
1482 options[list[i]] = static_cast<bool>(b);
1483 }
1484 }
1485 return options;
1486 }
1487
touchPadMultiFingerSwipeSync(ani_env * env,ani_object obj,ani_double fingers,ani_enum_item direction,ani_object touchPadOpt)1488 static ani_boolean touchPadMultiFingerSwipeSync(ani_env *env, ani_object obj, ani_double fingers, ani_enum_item direction, ani_object touchPadOpt)
1489 {
1490 ApiCallInfo callInfo_;
1491 ApiReplyInfo reply_;
1492 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeDriver"));
1493 callInfo_.apiId_ = "Driver.touchPadMultiFingerSwipe";
1494 callInfo_.paramList_.push_back(int(fingers));
1495 ani_int enumValue;
1496 env->EnumItem_GetValue_Int(direction, &enumValue);
1497 callInfo_.paramList_.push_back(enumValue);
1498 callInfo_.paramList_.push_back(getTouchPadSwipeOptions(env, touchPadOpt));
1499 Transact(callInfo_, reply_);
1500 UnmarshalReply(env, callInfo_, reply_);
1501 return true;
1502 }
BindDriver(ani_env * env)1503 static ani_boolean BindDriver(ani_env *env)
1504 {
1505 ani_class cls;
1506 if (ANI_OK != env->FindClass(Builder::BuildClass({"@ohos", "UiTest", "Driver"}).Descriptor().c_str(), &cls)) {
1507 HiLog::Error(LABEL, "%{public}s Not found !!!", __func__);
1508 return false;
1509 }
1510
1511 std::array methods = {
1512 ani_native_function{"create", ":L@ohos/UiTest/Driver;", reinterpret_cast<void *>(create)},
1513 ani_native_function{"delayMsSync", nullptr, reinterpret_cast<void *>(delayMsSync)},
1514 ani_native_function{"clickSync", nullptr, reinterpret_cast<void *>(clickSync)},
1515 ani_native_function{"longClickSync", nullptr, reinterpret_cast<void *>(longClickSync)},
1516 ani_native_function{"doubleClickSync", nullptr, reinterpret_cast<void *>(doubleClickSync)},
1517 ani_native_function{"flingSync", nullptr, reinterpret_cast<void *>(flingSync)},
1518 ani_native_function{"flingSyncDirection", nullptr, reinterpret_cast<void *>(flingSyncDirection)},
1519 ani_native_function{"flingWithDisplayIdSync", nullptr, reinterpret_cast<void *>(flingWithDisplayIdSync)},
1520 ani_native_function{"swipeSync", nullptr, reinterpret_cast<void *>(swipeSync)},
1521 ani_native_function{"dragSync", nullptr, reinterpret_cast<void *>(dragSync)},
1522 ani_native_function{"pressBackSync", nullptr, reinterpret_cast<void *>(pressBackSync)},
1523 ani_native_function{"pressBackWithDisplayIdSync", nullptr,
1524 reinterpret_cast<void *>(pressBackWithDisplayIdSync)},
1525 ani_native_function{"assertComponentExistSync", nullptr, reinterpret_cast<void *>(assertComponentExistSync)},
1526 ani_native_function{"triggerKeySync", nullptr, reinterpret_cast<void *>(triggerKeySync)},
1527 ani_native_function{"triggerKeyWithDisplayIdSync", nullptr,
1528 reinterpret_cast<void *>(triggerKeyWithDisplayIdSync)},
1529 ani_native_function{"inputTextSync", nullptr, reinterpret_cast<void *>(inputTextSync)},
1530 ani_native_function{"inputTextWithModeSync", nullptr, reinterpret_cast<void *>(inputTextWithModeSync)},
1531 ani_native_function{"findWindowSync", nullptr, reinterpret_cast<void *>(findWindowSync)},
1532 ani_native_function{"createUIEventObserver", nullptr,
1533 reinterpret_cast<void *>(createUIEventObserverSync)},
1534 ani_native_function{"wakeUpDisplaySync", nullptr, reinterpret_cast<void *>(wakeUpDisplaySync)},
1535 ani_native_function{"pressHomeSync", nullptr, reinterpret_cast<void *>(pressHomeSync)},
1536 ani_native_function{"pressHomeWithDisplayIdSync", nullptr,
1537 reinterpret_cast<void *>(pressHomeWithDisplayIdSync)},
1538 ani_native_function{"getDisplaySizeSync", nullptr, reinterpret_cast<void *>(getDisplaySizeSync)},
1539 ani_native_function{"getDisplaySizeWithDisplayIdSync", nullptr,
1540 reinterpret_cast<void *>(getDisplaySizeWithDisplayIdSync)},
1541 ani_native_function{"getDisplayDensitySync", nullptr, reinterpret_cast<void *>(getDisplayDensitySync)},
1542 ani_native_function{"getDisplayRotationSync", nullptr, reinterpret_cast<void *>(getDisplayRotationSync)},
1543 ani_native_function{"findComponentsSync", nullptr, reinterpret_cast<void *>(findComponentsSync)},
1544 ani_native_function{"findComponentSync", nullptr, reinterpret_cast<void *>(findComponentSync)},
1545 ani_native_function{"waitForIdleSync", nullptr, reinterpret_cast<void *>(waitForIdleSync)},
1546 ani_native_function{"waitForComponentSync", nullptr, reinterpret_cast<void *>(waitForComponentSync)},
1547 ani_native_function{"triggerCombineKeysSync", nullptr, reinterpret_cast<void *>(triggerCombineKeysSync)},
1548 ani_native_function{"setDisplayRotationEnabledSync", nullptr, reinterpret_cast<void *>(setDisplayRotationEnabledSync)},
1549 ani_native_function{"setDisplayRotationSync", nullptr, reinterpret_cast<void *>(setDisplayRotationSync)},
1550 ani_native_function{"screenCaptureSync", nullptr, reinterpret_cast<void *>(screenCaptureSync)},
1551 ani_native_function{"screenCapSync", nullptr, reinterpret_cast<void *>(screenCapSync)},
1552 ani_native_function{"penSwipeSync", nullptr, reinterpret_cast<void *>(penSwipeSync)},
1553 ani_native_function{"penClickSync", nullptr, reinterpret_cast<void *>(penClickSync)},
1554 ani_native_function{"penDoubleClickSync", nullptr, reinterpret_cast<void *>(penDoubleClickSync)},
1555 ani_native_function{"penLongClickSync", "L@ohos/UiTest/Point;Lstd/core/Double;:Z", reinterpret_cast<void *>(penLongClickSync)},
1556 ani_native_function{"mouseScrollSync", nullptr, reinterpret_cast<void *>(mouseScrollSync)},
1557 ani_native_function{"mouseMoveWithTrackSync", nullptr, reinterpret_cast<void *>(mouseMoveWithTrackSync)},
1558 ani_native_function{"mouseMoveToSync", nullptr, reinterpret_cast<void *>(mouseMoveToSync)},
1559 ani_native_function{"mouseDragSync", nullptr, reinterpret_cast<void *>(mouseDragSync)},
1560 ani_native_function{"mouseClickSync", nullptr, reinterpret_cast<void *>(mouseClickSync)},
1561 ani_native_function{"mouseDoubleClickSync", nullptr, reinterpret_cast<void *>(mouseDoubleClickSync)},
1562 ani_native_function{"mouseLongClickSync", nullptr, reinterpret_cast<void *>(mouseLongClickSync)},
1563 ani_native_function{"injectMultiPointerActionSync", nullptr, reinterpret_cast<void *>(injectMultiPointerActionSync)},
1564 ani_native_function{"injectPenPointerActionSync", nullptr, reinterpret_cast<void *>(injectPenPointerActionSync)},
1565 ani_native_function{"touchPadMultiFingerSwipeSync", nullptr, reinterpret_cast<void *>(touchPadMultiFingerSwipeSync)},
1566 };
1567
1568 if (ANI_OK != env->Class_BindNativeMethods(cls, methods.data(), methods.size())) {
1569 HiLog::Error(LABEL, "%{public}s Cannot bind native methods to !!!", __func__);
1570 return false;
1571 }
1572 return true;
1573 }
1574
performWindow(ani_env * env,ani_object obj,string api)1575 static void performWindow(ani_env *env, ani_object obj, string api)
1576 {
1577 ApiCallInfo callInfo_;
1578 ApiReplyInfo reply_;
1579 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeWindow"));
1580 callInfo_.apiId_ = api;
1581 Transact(callInfo_, reply_);
1582 UnmarshalReply(env, callInfo_, reply_);
1583 }
1584
splitSync(ani_env * env,ani_object obj,string api)1585 static ani_boolean splitSync(ani_env *env, ani_object obj, string api)
1586 {
1587 performWindow(env, obj, "UiWindow.split");
1588 return true;
1589 }
1590
resumeSync(ani_env * env,ani_object obj,string api)1591 static ani_boolean resumeSync(ani_env *env, ani_object obj, string api)
1592 {
1593 performWindow(env, obj, "UiWindow.resume");
1594 return true;
1595 }
1596
closeSync(ani_env * env,ani_object obj,string api)1597 static ani_boolean closeSync(ani_env *env, ani_object obj, string api)
1598 {
1599 performWindow(env, obj, "UiWindow.close");
1600 return true;
1601 }
1602
minimizeSync(ani_env * env,ani_object obj,string api)1603 static ani_boolean minimizeSync(ani_env *env, ani_object obj, string api)
1604 {
1605 performWindow(env, obj, "UiWindow.minimize");
1606 return true;
1607 }
1608
maximizeSync(ani_env * env,ani_object obj,string api)1609 static ani_boolean maximizeSync(ani_env *env, ani_object obj, string api)
1610 {
1611 performWindow(env, obj, "UiWindow.maximize");
1612 return true;
1613 }
1614
focusSync(ani_env * env,ani_object obj,string api)1615 static ani_boolean focusSync(ani_env *env, ani_object obj, string api)
1616 {
1617 performWindow(env, obj, "UiWindow.focus");
1618 return true;
1619 }
1620
isFocusedSync(ani_env * env,ani_object obj)1621 static ani_boolean isFocusedSync(ani_env *env, ani_object obj)
1622 {
1623 ApiCallInfo callInfo_;
1624 ApiReplyInfo reply_;
1625 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeWindow"));
1626 callInfo_.apiId_ = "UiWindow.isFocused";
1627 Transact(callInfo_, reply_);
1628 ani_ref ret = UnmarshalReply(env, callInfo_, reply_);
1629 if (ret == nullptr) {
1630 return false;
1631 }
1632 return reply_.resultValue_.get<bool>();
1633 }
1634
isActiveSync(ani_env * env,ani_object obj)1635 static ani_boolean isActiveSync(ani_env *env, ani_object obj)
1636 {
1637 ApiCallInfo callInfo_;
1638 ApiReplyInfo reply_;
1639 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeWindow"));
1640 callInfo_.apiId_ = "UiWindow.isActive";
1641 Transact(callInfo_, reply_);
1642 ani_ref ret = UnmarshalReply(env, callInfo_, reply_);
1643 if (ret == nullptr) {
1644 return false;
1645 }
1646 return reply_.resultValue_.get<bool>();
1647 }
1648
resizeSync(ani_env * env,ani_object obj,ani_double w,ani_double h,ani_enum_item d)1649 static ani_boolean resizeSync(ani_env *env, ani_object obj, ani_double w, ani_double h, ani_enum_item d)
1650 {
1651 ApiCallInfo callInfo_;
1652 ApiReplyInfo reply_;
1653 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeWindow"));
1654 callInfo_.apiId_ = "UiWindow.resize";
1655 callInfo_.paramList_.push_back(int(w));
1656 callInfo_.paramList_.push_back(int(h));
1657 ani_int enumValue;
1658 env->EnumItem_GetValue_Int(d, &enumValue);
1659 callInfo_.paramList_.push_back(enumValue);
1660 Transact(callInfo_, reply_);
1661 UnmarshalReply(env, callInfo_, reply_);
1662 return true;
1663 }
1664
moveToSync(ani_env * env,ani_object obj,ani_double x,ani_double y)1665 static ani_boolean moveToSync(ani_env *env, ani_object obj, ani_double x, ani_double y)
1666 {
1667 ApiCallInfo callInfo_;
1668 ApiReplyInfo reply_;
1669 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeWindow"));
1670 callInfo_.apiId_ = "UiWindow.moveTo";
1671 callInfo_.paramList_.push_back(int(x));
1672 callInfo_.paramList_.push_back(int(y));
1673 Transact(callInfo_, reply_);
1674 UnmarshalReply(env, callInfo_, reply_);
1675 return true;
1676 }
1677
getWindowModeSync(ani_env * env,ani_object obj)1678 static ani_ref getWindowModeSync(ani_env *env, ani_object obj)
1679 {
1680 ApiCallInfo callInfo_;
1681 ApiReplyInfo reply_;
1682 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeWindow"));
1683 callInfo_.apiId_ = "UiWindow.getWindowMode";
1684 Transact(callInfo_, reply_);
1685 ani_ref result = UnmarshalReply(env, callInfo_, reply_);
1686 if (result == nullptr) {
1687 ani_ref nullref;
1688 env->GetNull(&nullref);
1689 return nullref;
1690 }
1691 ani_enum enumType;
1692 if (ANI_OK != env->FindEnum(Builder::BuildEnum({"@ohos", "UiTest", "WindowMode"}).Descriptor().c_str(), &enumType)) {
1693 HiLog::Error(LABEL, "Not found enum item: %{public}s", __func__);
1694 }
1695 ani_enum_item enumItem;
1696 auto index = static_cast<uint8_t>(reply_.resultValue_.get<int>());
1697 env->Enum_GetEnumItemByIndex(enumType, index, &enumItem);
1698 HiLog::Info(LABEL, " getWindowMode: %{public}d ", index);
1699 return enumItem;
1700 }
1701
getBundleNameSync(ani_env * env,ani_object obj)1702 static ani_ref getBundleNameSync(ani_env *env, ani_object obj)
1703 {
1704 ApiCallInfo callInfo_;
1705 ApiReplyInfo reply_;
1706 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeWindow"));
1707 callInfo_.apiId_ = "UiWindow.getBundleName";
1708 Transact(callInfo_, reply_);
1709 ani_ref ret = UnmarshalReply(env, callInfo_, reply_);
1710 return ret;
1711 }
1712
getTitleSync(ani_env * env,ani_object obj)1713 static ani_ref getTitleSync(ani_env *env, ani_object obj)
1714 {
1715 ApiCallInfo callInfo_;
1716 ApiReplyInfo reply_;
1717 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeWindow"));
1718 callInfo_.apiId_ = "UiWindow.getTitle";
1719 Transact(callInfo_, reply_);
1720 ani_ref ret = UnmarshalReply(env, callInfo_, reply_);
1721 return ret;
1722 }
1723
getBoundsSync(ani_env * env,ani_object obj)1724 static ani_ref getBoundsSync(ani_env *env, ani_object obj)
1725 {
1726 ApiCallInfo callInfo_;
1727 ApiReplyInfo reply_;
1728 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeWindow"));
1729 callInfo_.apiId_ = "UiWindow.getBounds";
1730 Transact(callInfo_, reply_);
1731 ani_ref result = UnmarshalReply(env, callInfo_, reply_);
1732 if (result == nullptr) {
1733 ani_ref nullref;
1734 env->GetNull(&nullref);
1735 return nullref;
1736 }
1737 ani_object r = newRect(env, obj, reply_.resultValue_);
1738 return r;
1739 }
1740
BindWindow(ani_env * env)1741 static ani_boolean BindWindow(ani_env *env)
1742 {
1743 ani_class cls;
1744 if (ANI_OK != env->FindClass(Builder::BuildClass({"@ohos", "UiTest", "UiWindow"}).Descriptor().c_str(), &cls)) {
1745 HiLog::Error(LABEL, "%{public}s Not found className !!!", __func__);
1746 return false;
1747 }
1748
1749 std::array methods = {
1750 ani_native_function{"splitSync", nullptr, reinterpret_cast<void *>(splitSync)},
1751 ani_native_function{"resumeSync", nullptr, reinterpret_cast<void *>(resumeSync)},
1752 ani_native_function{"closeSync", nullptr, reinterpret_cast<void *>(closeSync)},
1753 ani_native_function{"minimizeSync", nullptr, reinterpret_cast<void *>(minimizeSync)},
1754 ani_native_function{"maximizeSync", nullptr, reinterpret_cast<void *>(maximizeSync)},
1755 ani_native_function{"focusSync", nullptr, reinterpret_cast<void *>(focusSync)},
1756 ani_native_function{"isFocusedSync", nullptr, reinterpret_cast<void *>(isFocusedSync)},
1757 ani_native_function{"isActiveSync", nullptr, reinterpret_cast<void *>(isActiveSync)},
1758 ani_native_function{"resizeSync", nullptr, reinterpret_cast<void *>(resizeSync)},
1759 ani_native_function{"moveToSync", nullptr, reinterpret_cast<void *>(moveToSync)},
1760 ani_native_function{"getWindowModeSync", nullptr, reinterpret_cast<void *>(getWindowModeSync)},
1761 ani_native_function{"getBundleNameSync", nullptr, reinterpret_cast<void *>(getBundleNameSync)},
1762 ani_native_function{"getTitleSync", nullptr, reinterpret_cast<void *>(getTitleSync)},
1763 ani_native_function{"winGetBoundsSync", nullptr, reinterpret_cast<void *>(getBoundsSync)},
1764 };
1765
1766 if (ANI_OK != env->Class_BindNativeMethods(cls, methods.data(), methods.size())) {
1767 HiLog::Error(LABEL, "%{public}s Cannot bind native methods to !!!", __func__);
1768 return false;
1769 }
1770 return true;
1771 }
1772
getBoundsCenterSync(ani_env * env,ani_object obj)1773 static ani_ref getBoundsCenterSync(ani_env *env, ani_object obj)
1774 {
1775 ApiCallInfo callInfo_;
1776 ApiReplyInfo reply_;
1777 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeComponent"));
1778 callInfo_.apiId_ = "Component.getBoundsCenter";
1779 Transact(callInfo_, reply_);
1780 ani_ref result = UnmarshalReply(env, callInfo_, reply_);
1781 if (result == nullptr) {
1782 ani_ref nullref;
1783 env->GetNull(&nullref);
1784 return nullref;
1785 }
1786 ani_object p = newPoint(env, obj, reply_.resultValue_["x"], reply_.resultValue_["y"]);
1787 HiLog::Info(LABEL, "reply_.resultValue_[x]:%{public}s", reply_.resultValue_["x"].dump().c_str());
1788 HiLog::Info(LABEL, "reply_.resultValue_[y]:%{public}s", reply_.resultValue_["y"].dump().c_str());
1789 return p;
1790 }
comGetBounds(ani_env * env,ani_object obj)1791 static ani_ref comGetBounds(ani_env *env, ani_object obj)
1792 {
1793 ApiCallInfo callInfo_;
1794 ApiReplyInfo reply_;
1795 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeComponent"));
1796 callInfo_.apiId_ = "Component.getBounds";
1797 Transact(callInfo_, reply_);
1798 ani_ref result = UnmarshalReply(env, callInfo_, reply_);
1799 if (result == nullptr) {
1800 ani_ref nullref;
1801 env->GetNull(&nullref);
1802 return nullref;
1803 }
1804 ani_object r = newRect(env, obj, reply_.resultValue_);
1805 return r;
1806 }
1807
performComponentApi(ani_env * env,ani_object obj,string apiId_)1808 static ani_ref performComponentApi(ani_env *env, ani_object obj, string apiId_)
1809 {
1810 ApiCallInfo callInfo_;
1811 ApiReplyInfo reply_;
1812 callInfo_.apiId_ = apiId_;
1813 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeComponent"));
1814 Transact(callInfo_, reply_);
1815 ani_ref result = UnmarshalReply(env, callInfo_, reply_);
1816 return result;
1817 }
1818
comClick(ani_env * env,ani_object obj)1819 static ani_boolean comClick(ani_env *env, ani_object obj)
1820 {
1821 performComponentApi(env, obj, "Component.click");
1822 return true;
1823 }
1824
comDoubleClick(ani_env * env,ani_object obj)1825 static ani_boolean comDoubleClick(ani_env *env, ani_object obj)
1826 {
1827 performComponentApi(env, obj, "Component.doubleClick");
1828 return true;
1829 }
1830
comLongClick(ani_env * env,ani_object obj)1831 static ani_boolean comLongClick(ani_env *env, ani_object obj)
1832 {
1833 performComponentApi(env, obj, "Component.longClick");
1834 return true;
1835 }
1836
comDragToSync(ani_env * env,ani_object obj,ani_object target)1837 static ani_boolean comDragToSync(ani_env *env, ani_object obj, ani_object target)
1838 {
1839 ApiCallInfo callInfo_;
1840 ApiReplyInfo reply_;
1841 callInfo_.apiId_ = "Component.dragTo";
1842 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeComponent"));
1843 callInfo_.paramList_.push_back(aniStringToStdString(env, unwrapp(env, target, "nativeComponent")));
1844 Transact(callInfo_, reply_);
1845 UnmarshalReply(env, callInfo_, reply_);
1846 return true;
1847 }
1848
getText(ani_env * env,ani_object obj)1849 static ani_ref getText(ani_env *env, ani_object obj)
1850 {
1851 return performComponentApi(env, obj, "Component.getText");
1852 }
1853
getType(ani_env * env,ani_object obj)1854 static ani_ref getType(ani_env *env, ani_object obj)
1855 {
1856 return performComponentApi(env, obj, "Component.getType");
1857 }
1858
getId(ani_env * env,ani_object obj)1859 static ani_ref getId(ani_env *env, ani_object obj)
1860 {
1861 return performComponentApi(env, obj, "Component.getId");
1862 }
1863
getHint(ani_env * env,ani_object obj)1864 static ani_ref getHint(ani_env *env, ani_object obj)
1865 {
1866 return performComponentApi(env, obj, "Component.getHint");
1867 }
1868
getDescription(ani_env * env,ani_object obj)1869 static ani_ref getDescription(ani_env *env, ani_object obj)
1870 {
1871 return performComponentApi(env, obj, "Component.getDescription");
1872 }
1873
comInputText(ani_env * env,ani_object obj,ani_string txt)1874 static ani_boolean comInputText(ani_env *env, ani_object obj, ani_string txt)
1875 {
1876 ApiCallInfo callInfo_;
1877 ApiReplyInfo reply_;
1878 callInfo_.apiId_ = "Component.inputText";
1879 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeComponent"));
1880 callInfo_.paramList_.push_back(aniStringToStdString(env, txt));
1881 Transact(callInfo_, reply_);
1882 UnmarshalReply(env, callInfo_, reply_);
1883 return true;
1884 }
1885
comInputTextWithMode(ani_env * env,ani_object obj,ani_string txt,ani_object inputMode)1886 static ani_boolean comInputTextWithMode(ani_env *env, ani_object obj, ani_string txt, ani_object inputMode)
1887 {
1888 ApiCallInfo callInfo_;
1889 ApiReplyInfo reply_;
1890 callInfo_.apiId_ = "Component.inputText";
1891 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeComponent"));
1892 callInfo_.paramList_.push_back(aniStringToStdString(env, txt));
1893 callInfo_.paramList_.push_back(getInputTextModeOptions(env, inputMode));
1894 Transact(callInfo_, reply_);
1895 UnmarshalReply(env, callInfo_, reply_);
1896 return true;
1897 }
1898
clearText(ani_env * env,ani_object obj)1899 static ani_boolean clearText(ani_env *env, ani_object obj)
1900 {
1901 performComponentApi(env, obj, "Component.clearText");
1902 return true;
1903 }
1904
scrollToTop(ani_env * env,ani_object obj,ani_object speed)1905 static ani_boolean scrollToTop(ani_env *env, ani_object obj, ani_object speed)
1906 {
1907 ApiCallInfo callInfo_;
1908 ApiReplyInfo reply_;
1909 callInfo_.apiId_ = "Component.scrollToTop";
1910 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeComponent"));
1911 pushParam(env, speed, callInfo_, true);
1912 Transact(callInfo_, reply_);
1913 UnmarshalReply(env, callInfo_, reply_);
1914 return true;
1915 }
1916
scrollToBottom(ani_env * env,ani_object obj,ani_object speed)1917 static ani_boolean scrollToBottom(ani_env *env, ani_object obj, ani_object speed)
1918 {
1919 ApiCallInfo callInfo_;
1920 ApiReplyInfo reply_;
1921 callInfo_.apiId_ = "Component.scrollToBottom";
1922 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeComponent"));
1923 pushParam(env, speed, callInfo_, true);
1924 Transact(callInfo_, reply_);
1925 UnmarshalReply(env, callInfo_, reply_);
1926 return true;
1927 }
1928
scrollSearch(ani_env * env,ani_object obj,ani_object on,ani_object vertical,ani_object offset)1929 static ani_object scrollSearch(ani_env *env, ani_object obj, ani_object on, ani_object vertical, ani_object offset)
1930 {
1931 ApiCallInfo callInfo_;
1932 ApiReplyInfo reply_;
1933 callInfo_.apiId_ = "Component.scrollSearch";
1934 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeComponent"));
1935 callInfo_.paramList_.push_back(aniStringToStdString(env, unwrapp(env, on, "nativeOn")));
1936 pushBool(env, vertical, callInfo_.paramList_);
1937 pushParam(env, offset, callInfo_, true);
1938 Transact(callInfo_, reply_);
1939 ani_ref nativeComponent = UnmarshalReply(env, callInfo_, reply_);
1940 if (nativeComponent == nullptr) {
1941 return nullptr;
1942 }
1943 ani_object com_obj;
1944 ani_class cls = findCls(env, Builder::BuildClass({"@ohos", "UiTest", "Component"}).Descriptor().c_str());
1945 ani_method ctor = nullptr;
1946 if (cls != nullptr) {
1947 arkts::ani_signature::SignatureBuilder string_ctor{};
1948 string_ctor.AddClass({"std", "core", "String"});
1949 ctor = findCtorMethod(env, cls, string_ctor.BuildSignatureDescriptor().c_str());
1950 }
1951 if (cls == nullptr || ctor == nullptr) {
1952 return nullptr;
1953 }
1954 if (ANI_OK != env->Object_New(cls, ctor, &com_obj, reinterpret_cast<ani_object>(nativeComponent))) {
1955 HiLog::Error(LABEL, "%{public}s New Component Failed !!!", __func__);
1956 }
1957 return com_obj;
1958 }
1959
pinch(ani_env * env,ani_object obj,ani_double scale,string apiId_)1960 static void pinch(ani_env *env, ani_object obj, ani_double scale, string apiId_)
1961 {
1962 ApiCallInfo callInfo_;
1963 ApiReplyInfo reply_;
1964 callInfo_.apiId_ = apiId_;
1965 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeComponent"));
1966 callInfo_.paramList_.push_back(scale);
1967 Transact(callInfo_, reply_);
1968 UnmarshalReply(env, callInfo_, reply_);
1969 return;
1970 }
1971
pinchIn(ani_env * env,ani_object obj,ani_double scale)1972 static ani_boolean pinchIn(ani_env *env, ani_object obj, ani_double scale)
1973 {
1974 pinch(env, obj, scale, "Component.pinchIn");
1975 return true;
1976 }
1977
pinchOut(ani_env * env,ani_object obj,ani_double scale)1978 static ani_boolean pinchOut(ani_env *env, ani_object obj, ani_double scale)
1979 {
1980 pinch(env, obj, scale, "Component.pinchOut");
1981 return true;
1982 }
performComponentApiBool(ani_env * env,ani_object obj,string apiId_)1983 static ani_boolean performComponentApiBool(ani_env *env, ani_object obj, string apiId_)
1984 {
1985 ApiCallInfo callInfo_;
1986 ApiReplyInfo reply_;
1987 callInfo_.apiId_ = apiId_;
1988 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeComponent"));
1989 Transact(callInfo_, reply_);
1990 ani_ref result = UnmarshalReply(env, callInfo_, reply_);
1991 if (result == nullptr) {
1992 return false;
1993 }
1994 return reply_.resultValue_.get<bool>();
1995 }
isSelected(ani_env * env,ani_object obj)1996 static ani_boolean isSelected(ani_env *env, ani_object obj)
1997 {
1998 return performComponentApiBool(env, obj, "Component.isSelected");
1999 }
2000
isClickable(ani_env * env,ani_object obj)2001 static ani_boolean isClickable(ani_env *env, ani_object obj)
2002 {
2003 return performComponentApiBool(env, obj, "Component.isClickable");
2004 }
2005
isLongClickable(ani_env * env,ani_object obj)2006 static ani_boolean isLongClickable(ani_env *env, ani_object obj)
2007 {
2008 return performComponentApiBool(env, obj, "Component.isLongClickable");
2009 }
2010
isScrollable(ani_env * env,ani_object obj)2011 static ani_boolean isScrollable(ani_env *env, ani_object obj)
2012 {
2013 return performComponentApiBool(env, obj, "Component.isScrollable");
2014 }
2015
isEnabled(ani_env * env,ani_object obj)2016 static ani_boolean isEnabled(ani_env *env, ani_object obj)
2017 {
2018 return performComponentApiBool(env, obj, "Component.isEnabled");
2019 }
2020
isFocused(ani_env * env,ani_object obj)2021 static ani_boolean isFocused(ani_env *env, ani_object obj)
2022 {
2023 return performComponentApiBool(env, obj, "Component.isFocused");
2024 }
2025
isChecked(ani_env * env,ani_object obj)2026 static ani_boolean isChecked(ani_env *env, ani_object obj)
2027 {
2028 return performComponentApiBool(env, obj, "Component.isChecked");
2029 }
2030
isCheckable(ani_env * env,ani_object obj)2031 static ani_boolean isCheckable(ani_env *env, ani_object obj)
2032 {
2033 return performComponentApiBool(env, obj, "Component.isCheckable");
2034 }
2035
BindComponent(ani_env * env)2036 static ani_boolean BindComponent(ani_env *env)
2037 {
2038 ani_class cls;
2039 if (ANI_OK != env->FindClass(Builder::BuildClass({"@ohos", "UiTest", "Component"}).Descriptor().c_str(), &cls)) {
2040 HiLog::Error(LABEL, "%{public}s Not found className !!!", __func__);
2041 return false;
2042 }
2043 std::array methods = {
2044 ani_native_function{"comClickSync", nullptr, reinterpret_cast<void *>(comClick)},
2045 ani_native_function{"comLongClickSync", nullptr, reinterpret_cast<void *>(comLongClick)},
2046 ani_native_function{"comDoubleClickSync", nullptr, reinterpret_cast<void *>(comDoubleClick)},
2047 ani_native_function{"comDragToSync", nullptr, reinterpret_cast<void *>(comDragToSync)},
2048 ani_native_function{"comGetBoundsSync", nullptr, reinterpret_cast<void *>(comGetBounds)},
2049 ani_native_function{"getBoundsCenterSync", nullptr, reinterpret_cast<void *>(getBoundsCenterSync)},
2050 ani_native_function{"getTextSync", nullptr, reinterpret_cast<void *>(getText)},
2051 ani_native_function{"getTypeSync", nullptr, reinterpret_cast<void *>(getType)},
2052 ani_native_function{"getIdSync", nullptr, reinterpret_cast<void *>(getId)},
2053 ani_native_function{"getHintSync", nullptr, reinterpret_cast<void *>(getHint)},
2054 ani_native_function{"getDescriptionSync", nullptr, reinterpret_cast<void *>(getDescription)},
2055 ani_native_function{"comInputTextSync", nullptr, reinterpret_cast<void *>(comInputText)},
2056 ani_native_function{"comInputTextWithModeSync", nullptr, reinterpret_cast<void *>(comInputTextWithMode)},
2057 ani_native_function{"clearTextSync", nullptr, reinterpret_cast<void *>(clearText)},
2058 ani_native_function{"scrollToTopSync", nullptr, reinterpret_cast<void *>(scrollToTop)},
2059 ani_native_function{"scrollToBottomSync", nullptr, reinterpret_cast<void *>(scrollToBottom)},
2060 ani_native_function{"scrollSearchSync", nullptr, reinterpret_cast<void *>(scrollSearch)},
2061 ani_native_function{"pinchInSync", nullptr, reinterpret_cast<void *>(pinchIn)},
2062 ani_native_function{"pinchOutSync", nullptr, reinterpret_cast<void *>(pinchOut)},
2063 ani_native_function{"isScrollableSync", nullptr, reinterpret_cast<void *>(isScrollable)},
2064 ani_native_function{"isSelectedSync", nullptr, reinterpret_cast<void *>(isSelected)},
2065 ani_native_function{"isLongClickableSync", nullptr, reinterpret_cast<void *>(isLongClickable)},
2066 ani_native_function{"isClickableSync", nullptr, reinterpret_cast<void *>(isClickable)},
2067 ani_native_function{"isFocusedSync", nullptr, reinterpret_cast<void *>(isFocused)},
2068 ani_native_function{"isEnabledSync", nullptr, reinterpret_cast<void *>(isEnabled)},
2069 ani_native_function{"isCheckedSync", nullptr, reinterpret_cast<void *>(isChecked)},
2070 ani_native_function{"isCheckableSync", nullptr, reinterpret_cast<void *>(isCheckable)},
2071 };
2072
2073 if (ANI_OK != env->Class_BindNativeMethods(cls, methods.data(), methods.size())) {
2074 HiLog::Error(LABEL, "%{public}s Cannot bind native methods to !!!", __func__);
2075 return false;
2076 }
2077 return true;
2078 }
once(ani_env * env,ani_object obj,ani_string type,ani_object callback)2079 static void once(ani_env *env, ani_object obj, ani_string type, ani_object callback)
2080 {
2081 ApiCallInfo callInfo_;
2082 ApiReplyInfo reply_;
2083 callInfo_.callerObjRef_ = aniStringToStdString(env, unwrapp(env, obj, "nativeUIEventObserver"));
2084 callInfo_.apiId_ = "UIEventObserver.once";
2085 callInfo_.paramList_.push_back(aniStringToStdString(env, type));
2086 UiEventObserverAni::Get().PreprocessCallOnce(env, callInfo_, obj, callback, reply_);
2087 Transact(callInfo_, reply_);
2088 UnmarshalReply(env, callInfo_, reply_);
2089 }
BindUiEventObserver(ani_env * env)2090 static ani_boolean BindUiEventObserver(ani_env *env)
2091 {
2092 ani_class cls;
2093 if (ANI_OK != env->FindClass(Builder::BuildClass({"@ohos", "UiTest", "UIEventObserver"}).Descriptor().c_str(), &cls)) {
2094 HiLog::Error(LABEL, "%{public}s Not found className !!!", __func__);
2095 return false;
2096 }
2097 std::array methods = {
2098 ani_native_function{"once", nullptr, reinterpret_cast<void *>(once)},
2099 };
2100
2101 if (ANI_OK != env->Class_BindNativeMethods(cls, methods.data(), methods.size())) {
2102 HiLog::Error(LABEL, "%{public}s Cannot bind native methods to !!!", __func__);
2103 return false;
2104 }
2105 return true;
2106 }
StsUiTestInit(ani_env * env)2107 void StsUiTestInit(ani_env *env)
2108 {
2109 HiLog::Info(LABEL, "%{public}s StsUiTestInit call", __func__);
2110 ani_status status = ANI_ERROR;
2111 if (env->ResetError() != ANI_OK) {
2112 HiLog::Error(LABEL, "%{public}s ResetError failed", __func__);
2113 }
2114 ani_namespace ns;
2115 status = env->FindNamespace(Builder::BuildNamespace({"@ohos", "UiTest", "UiTest"}).Descriptor().c_str(), &ns);
2116 if (status != ANI_OK) {
2117 HiLog::Error(LABEL, "FindNamespace UiTest failed status : %{public}d", status);
2118 return;
2119 }
2120 std::array kitFunctions = {
2121 ani_native_function{"ScheduleEstablishConnection", nullptr, reinterpret_cast<void *>(ScheduleEstablishConnection)},
2122 ani_native_function{"GetConnectionStat", nullptr, reinterpret_cast<void *>(GetConnectionStat)},
2123 };
2124 status = env->Namespace_BindNativeFunctions(ns, kitFunctions.data(), kitFunctions.size());
2125 if (status != ANI_OK) {
2126 HiLog::Error(LABEL, "Namespace_BindNativeFunctions failed status : %{public}d", status);
2127 }
2128 if (env->ResetError() != ANI_OK) {
2129 HiLog::Error(LABEL, "%{public}s ResetError failed", __func__);
2130 }
2131 HiLog::Info(LABEL, "%{public}s StsUiTestInit end", __func__);
2132 }
ANI_Constructor(ani_vm * vm,uint32_t * result)2133 ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result)
2134 {
2135 ani_env *env;
2136 if (ANI_OK != vm->GetEnv(ANI_VERSION_1, &env)) {
2137 HiLog::Error(LABEL, "%{public}s UITest: Unsupported ANI_VERSION_1 !!!", __func__);
2138 return (ani_status)ANI_ERROR;
2139 }
2140 StsUiTestInit(env);
2141 auto status = true;
2142 status &= BindDriver(env);
2143 status &= BindOn(env);
2144 status &= BindComponent(env);
2145 status &= BindWindow(env);
2146 status &= BindPointMatrix(env);
2147 status &= BindUiEventObserver(env);
2148 if (!status) {
2149 HiLog::Error(LABEL, "%{public}s ani_error", __func__);
2150 return ANI_ERROR;
2151 }
2152 HiLog::Info(LABEL, "%{public}s ani_bind success !!!", __func__);
2153 *result = ANI_VERSION_1;
2154 return ANI_OK;
2155 }
2156