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.h"
17
18 #include <regex>
19
20 #include "ability_delegator_registry.h"
21 #include "hilog_tag_wrapper.h"
22 #include "runner_runtime/cj_test_runner_object.h"
23
24 namespace OHOS {
25 namespace RunnerRuntime {
26
Create(const std::unique_ptr<Runtime> & runtime,const std::shared_ptr<AbilityDelegatorArgs> & args,const AppExecFwk::BundleInfo & bundleInfo)27 std::unique_ptr<TestRunner> CJTestRunner::Create(const std::unique_ptr<Runtime> &runtime,
28 const std::shared_ptr<AbilityDelegatorArgs> &args, const AppExecFwk::BundleInfo &bundleInfo)
29 {
30 TAG_LOGI(AAFwkTag::DELEGATOR, "called");
31 if (!runtime) {
32 TAG_LOGE(AAFwkTag::DELEGATOR, "invalid runtime");
33 return nullptr;
34 }
35
36 auto cjRuntime = static_cast<CJRuntime*>(runtime.get());
37 if (!cjRuntime->IsAppLibLoaded()) {
38 TAG_LOGE(AAFwkTag::DELEGATOR, "appLib not loaded");
39 return nullptr;
40 }
41
42 if (!args) {
43 TAG_LOGE(AAFwkTag::DELEGATOR, "invalid args");
44 return nullptr;
45 }
46
47 auto pTestRunner = new (std::nothrow) CJTestRunner(*cjRuntime, args, bundleInfo);
48 if (!pTestRunner) {
49 TAG_LOGE(AAFwkTag::DELEGATOR, "create testrunner failed");
50 return nullptr;
51 }
52
53 return std::unique_ptr<CJTestRunner>(pTestRunner);
54 }
55
CJTestRunner(CJRuntime & cjRuntime,const std::shared_ptr<AbilityDelegatorArgs> & args,const AppExecFwk::BundleInfo & bundleInfo)56 CJTestRunner::CJTestRunner(CJRuntime &cjRuntime, const std::shared_ptr<AbilityDelegatorArgs> &args,
57 const AppExecFwk::BundleInfo &bundleInfo) : cjRuntime_(cjRuntime)
58 {
59 std::string moduleName = args->GetTestRunnerClassName();
60 cjTestRunnerObj_ = CJTestRunnerObject::LoadModule(moduleName);
61 }
62
63 CJTestRunner::~CJTestRunner() = default;
64
Initialize()65 bool CJTestRunner::Initialize()
66 {
67 if (!cjRuntime_.IsAppLibLoaded()) {
68 TAG_LOGE(AAFwkTag::DELEGATOR, "appLib not loaded");
69 return false;
70 }
71 if (!cjTestRunnerObj_) {
72 TAG_LOGE(AAFwkTag::DELEGATOR, "null cjTestRunnerObj_, initialize failed");
73 return false;
74 }
75 return true;
76 }
77
Prepare()78 void CJTestRunner::Prepare()
79 {
80 TAG_LOGI(AAFwkTag::DELEGATOR, "called");
81 TestRunner::Prepare();
82 if (!cjTestRunnerObj_) {
83 TAG_LOGE(AAFwkTag::DELEGATOR, "null cjTestRunnerObj_, initialize failed");
84 return;
85 }
86 cjTestRunnerObj_->OnPrepare();
87 }
88
Run()89 void CJTestRunner::Run()
90 {
91 TAG_LOGI(AAFwkTag::DELEGATOR, "Enter");
92 TestRunner::Run();
93 if (!cjTestRunnerObj_) {
94 TAG_LOGE(AAFwkTag::DELEGATOR, "null cjTestRunnerObj_, initialize failed");
95 return;
96 }
97 cjTestRunnerObj_->OnRun();
98 }
99
ReportFinished(const std::string & msg)100 void CJTestRunner::ReportFinished(const std::string &msg)
101 {
102 TAG_LOGI(AAFwkTag::DELEGATOR, "Enter");
103 auto delegator = AbilityDelegatorRegistry::GetAbilityDelegator();
104 if (!delegator) {
105 TAG_LOGE(AAFwkTag::DELEGATOR, "null delegator");
106 return;
107 }
108
109 delegator->FinishUserTest(msg, -1);
110 }
111
ReportStatus(const std::string & msg)112 void CJTestRunner::ReportStatus(const std::string &msg)
113 {
114 TAG_LOGD(AAFwkTag::DELEGATOR, "Enter");
115 auto delegator = AbilityDelegatorRegistry::GetAbilityDelegator();
116 if (!delegator) {
117 TAG_LOGE(AAFwkTag::DELEGATOR, "null delegator");
118 return;
119 }
120
121 delegator->Print(msg);
122 }
123 } // namespace RunnerRuntime
124 } // namespace OHOS
125