1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "runner_runtime/cj_test_runner_object.h"
17
18 #include <string>
19
20 #include "hilog_tag_wrapper.h"
21
22 namespace {
23 // g_cjTestRunnerFuncs is used to save cj functions.
24 // It is assigned by the global variable REGISTER_ABILITY on the cj side which invokes RegisterCJTestRunnerFuncs.
25 // And it is never released.
26 CJTestRunnerFuncs* g_cjTestRunnerFuncs = nullptr;
27 } // namespace
28
RegisterCJTestRunnerFuncs(void (* registerFunc)(CJTestRunnerFuncs *))29 void RegisterCJTestRunnerFuncs(void (*registerFunc)(CJTestRunnerFuncs*))
30 {
31 TAG_LOGI(AAFwkTag::DELEGATOR, "called");
32 if (g_cjTestRunnerFuncs != nullptr) {
33 TAG_LOGE(AAFwkTag::DELEGATOR, "repeated registration");
34 return;
35 }
36
37 if (registerFunc == nullptr) {
38 TAG_LOGE(AAFwkTag::DELEGATOR, "null registerFunc");
39 return;
40 }
41
42 g_cjTestRunnerFuncs = new CJTestRunnerFuncs();
43 registerFunc(g_cjTestRunnerFuncs);
44 }
45
46 namespace OHOS {
47 namespace RunnerRuntime {
LoadModule(const std::string & name)48 std::shared_ptr<CJTestRunnerObject> CJTestRunnerObject::LoadModule(const std::string& name)
49 {
50 if (g_cjTestRunnerFuncs == nullptr) {
51 TAG_LOGE(AAFwkTag::DELEGATOR, "null g_cjTestRunnerFuncs");
52 return nullptr;
53 }
54 auto id = g_cjTestRunnerFuncs->cjTestRunnerCreate(name.c_str());
55 if (id == 0) {
56 TAG_LOGE(AAFwkTag::DELEGATOR,
57 "failed to invoke, ability: %{public}s is not registered", name.c_str());
58 return nullptr;
59 }
60 return std::make_shared<CJTestRunnerObject>(id);
61 }
62
~CJTestRunnerObject()63 CJTestRunnerObject::~CJTestRunnerObject()
64 {
65 g_cjTestRunnerFuncs->cjTestRunnerRelease(id_);
66 id_ = 0;
67 }
68
OnRun() const69 void CJTestRunnerObject::OnRun() const
70 {
71 if (g_cjTestRunnerFuncs == nullptr) {
72 TAG_LOGE(AAFwkTag::DELEGATOR, "null g_cjTestRunnerFuncs");
73 return;
74 }
75 g_cjTestRunnerFuncs->cjTestRunnerOnRun(id_);
76 }
77
OnPrepare() const78 void CJTestRunnerObject::OnPrepare() const
79 {
80 if (g_cjTestRunnerFuncs == nullptr) {
81 TAG_LOGE(AAFwkTag::DELEGATOR, "null g_cjTestRunnerFuncs");
82 return;
83 }
84 g_cjTestRunnerFuncs->cjTestRunnerOnPrepare(id_);
85 }
86 } // namespace RunnerRuntime
87 } // namespace OHOS
88