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, "not registered: %{public}s", name.c_str());
57 return nullptr;
58 }
59 return std::make_shared<CJTestRunnerObject>(id);
60 }
61
~CJTestRunnerObject()62 CJTestRunnerObject::~CJTestRunnerObject()
63 {
64 g_cjTestRunnerFuncs->cjTestRunnerRelease(id_);
65 id_ = 0;
66 }
67
OnRun() const68 void CJTestRunnerObject::OnRun() const
69 {
70 if (g_cjTestRunnerFuncs == nullptr) {
71 TAG_LOGE(AAFwkTag::DELEGATOR, "null g_cjTestRunnerFuncs");
72 return;
73 }
74 g_cjTestRunnerFuncs->cjTestRunnerOnRun(id_);
75 }
76
OnPrepare() const77 void CJTestRunnerObject::OnPrepare() const
78 {
79 if (g_cjTestRunnerFuncs == nullptr) {
80 TAG_LOGE(AAFwkTag::DELEGATOR, "null g_cjTestRunnerFuncs");
81 return;
82 }
83 g_cjTestRunnerFuncs->cjTestRunnerOnPrepare(id_);
84 }
85 } // namespace RunnerRuntime
86 } // namespace OHOS
87