diff --git a/frameworks/ets/ets/@ohos.application.testRunner.ets b/frameworks/ets/ets/@ohos.application.testRunner.ets index 84c032547f16d29e8caa00658a1a8f8f4e6225c6..f19cb90d0627367def8285bf8fd31d2a58c67ca4 100644 --- a/frameworks/ets/ets/@ohos.application.testRunner.ets +++ b/frameworks/ets/ets/@ohos.application.testRunner.ets @@ -14,6 +14,6 @@ */ export default interface TestRunner { - onPrepare(): void; - onRun(): void; + onPrepare: () => void; + onRun: () => void; } diff --git a/frameworks/native/appkit/BUILD.gn b/frameworks/native/appkit/BUILD.gn index 314f54e138c2e346b77aa7936d56be2ff24bf80b..d67a976afb234bf28f47911dce9ed4b02fbc9c66 100644 --- a/frameworks/native/appkit/BUILD.gn +++ b/frameworks/native/appkit/BUILD.gn @@ -693,6 +693,7 @@ ohos_shared_library("test_runner_ani") { "${ability_runtime_innerkits_path}/runtime:runtime", "${ability_runtime_native_path}/appkit:app_context", "${ability_runtime_native_path}/appkit:delegator_mgmt", + "${ability_runtime_path}/frameworks/ets/ani/ani_common:ani_common", ] external_deps = [ diff --git a/frameworks/native/appkit/ability_delegator/runner_runtime/ets_test_runner.cpp b/frameworks/native/appkit/ability_delegator/runner_runtime/ets_test_runner.cpp index 70f26da60968b5cda488dd608f49275bcf4bc44b..8e05cc768e12d5cd2312a8af1fc8bc43df961970 100644 --- a/frameworks/native/appkit/ability_delegator/runner_runtime/ets_test_runner.cpp +++ b/frameworks/native/appkit/ability_delegator/runner_runtime/ets_test_runner.cpp @@ -16,6 +16,7 @@ #include #include "ability_delegator_registry.h" +#include "ani_common_util.h" #include "hilog_tag_wrapper.h" #include "runner_runtime/ets_test_runner.h" @@ -27,6 +28,9 @@ namespace OHOS { namespace RunnerRuntime { +namespace { + constexpr int32_t ARGC_ZERO = 0; +} TestRunner *ETSTestRunner::Create(const std::unique_ptr &runtime, const std::shared_ptr &args, const AppExecFwk::BundleInfo &bundleInfo) @@ -134,17 +138,21 @@ void ETSTestRunner::Prepare() if (env->ResetError() != ANI_OK) { TAG_LOGE(AAFwkTag::ETSRUNTIME, "ResetError failed"); } - ani_method method; + ani_ref funRef; ani_status status = ANI_ERROR; - status = env->Class_FindMethod(etsTestRunnerObj_->aniCls, "onPrepare", ":V", &method); + status = env->Object_GetPropertyByName_Ref(etsTestRunnerObj_->aniObj, "onPrepare", &funRef); if (status != ANI_OK) { TAG_LOGE(AAFwkTag::DELEGATOR, "get onPrepare failed status : %{public}d", status); return; } TAG_LOGI(AAFwkTag::DELEGATOR, "get onPrepare success"); + if (!IsValidProperty(env, funRef)) { + TAG_LOGI(AAFwkTag::DELEGATOR, "invalid onPrepare property"); + return; + } - ani_int result; - status = env->Object_CallMethod_Void(etsTestRunnerObj_->aniObj, method, &result); + ani_ref result; + status = env->FunctionalObject_Call(reinterpret_cast(funRef), ARGC_ZERO, nullptr, &result); if (status != ANI_OK) { TAG_LOGE(AAFwkTag::DELEGATOR, "Object_CallMethod_Void onPrepare failed status : %{public}d", status); } else { @@ -166,17 +174,21 @@ void ETSTestRunner::Run() if (env->ResetError() != ANI_OK) { TAG_LOGE(AAFwkTag::ETSRUNTIME, "ResetError failed"); } - ani_method method; + ani_ref funRef; ani_status status = ANI_ERROR; - status = env->Class_FindMethod(etsTestRunnerObj_->aniCls, "onRun", ":V", &method); + status = env->Object_GetPropertyByName_Ref(etsTestRunnerObj_->aniObj, "onRun", &funRef); if (status != ANI_OK) { TAG_LOGE(AAFwkTag::DELEGATOR, "get onRun failed status : %{public}d", status); return; } TAG_LOGI(AAFwkTag::DELEGATOR, "get onRun success"); + if (!IsValidProperty(env, funRef)) { + TAG_LOGI(AAFwkTag::DELEGATOR, "invalid onRun property"); + return; + } - ani_int result; - status = env->Object_CallMethod_Void(etsTestRunnerObj_->aniObj, method, &result); + ani_ref result; + status = env->FunctionalObject_Call(reinterpret_cast(funRef), ARGC_ZERO, nullptr, &result); if (status != ANI_OK) { TAG_LOGE(AAFwkTag::DELEGATOR, "Object_CallMethod_Void onRun failed status : %{public}d", status); } else {