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 #include <gtest/gtest.h>
16 #include <memory>
17 #include <string>
18 #include <regex>
19 
20 #include "ability_delegator_registry.h"
21 #include "hilog_wrapper.h"
22 #include "cj_runtime.h"
23 #include "runner_runtime/cj_test_runner.h"
24 #include "runner_runtime/cj_test_runner_object.h"
25 
26 #include "cj_mock_runtime.h"
27 #include "constants.h"
28 #include "app_loader.h"
29 #include "event_runner.h"
30 #include "hilog_tag_wrapper.h"
31 #include "napi/native_common.h"
32 #include "ohos_application.h"
33 
34 using namespace OHOS;
35 using namespace RunnerRuntime;
36 using namespace OHOS::AppExecFwk;
37 using namespace OHOS::AAFwk;
38 using namespace OHOS::AbilityBase::Constants;
39 using namespace testing;
40 using namespace testing::ext;
41 
42 namespace {
43 const std::string KEY_TEST_BUNDLE_NAME = "-p";
44 const std::string VALUE_TEST_BUNDLE_NAME = "com.example.myapplicationjs";
45 const std::string KEY_TEST_RUNNER_CLASS = "-s unittest";
46 const std::string VALUE_TEST_RUNNER_CLASS = "CjUserTestRunnerCj";
47 const std::string KEY_TEST_CASE = "-s class";
48 const std::string VALUE_TEST_CASE =
49 "ohos.acts.aafwk.ability.test.ConstructorTest#testDataAbilityOtherFunction0010_js";
50 const std::string KEY_TEST_WAIT_TIMEOUT = "-w";
51 const std::string VALUE_TEST_WAIT_TIMEOUT = "35";
52 const std::string REPORT_FINISH_MSG = "report finish message";
53 const std::string TEST_BUNDLE_NAME = "com.ohos.contactsdataability";
54 const std::string TEST_MODULE_NAME = ".ContactsDataAbility";
55 const std::string TEST_ABILITY_NAME = "ContactsDataAbility";
56 const std::string TEST_CODE_PATH = "/data/storage/el1/bundle";
57 const std::string TEST_HAP_PATH = "/system/app/com.ohos.contactsdataability/Contacts_DataAbility.hap";
58 const std::string TEST_LIB_PATH = "/data/storage/el1/bundle/lib/";
59 const std::string TEST_MODULE_PATH = "/data/storage/el1/bundle/curCJModulePath";
60 }
61 
62 class CjTestRunnerTest : public Test {
63 public:
CjTestRunnerTest()64     CjTestRunnerTest()
65     {}
~CjTestRunnerTest()66     ~CjTestRunnerTest()
67     {}
68     static void SetUpTestCase(void);
69     static void TearDownTestCase(void);
70     void SetUp() override;
71     void TearDown() override;
72     Runtime::Options options_;
73 
74 protected:
75     std::unique_ptr<CJTestRunner> testRunner_;
76     std::shared_ptr<AbilityDelegatorArgs> delegator_;
77     std::unique_ptr<CJRuntime> runtime_;
78     AppExecFwk::BundleInfo bundleInfo_;
79 };
80 
SetUpTestCase()81 void CjTestRunnerTest::SetUpTestCase()
82 {}
83 
TearDownTestCase()84 void CjTestRunnerTest::TearDownTestCase()
85 {}
86 
SetUp()87 void CjTestRunnerTest::SetUp()
88 {
89     options_.bundleName = TEST_BUNDLE_NAME;
90     options_.codePath = TEST_CODE_PATH;
91     options_.loadAce = false;
92     options_.isBundle = true;
93     options_.preload = false;
94     std::shared_ptr<AppExecFwk::EventRunner> eventRunner = AppExecFwk::EventRunner::Create(TEST_ABILITY_NAME);
95     options_.eventRunner = eventRunner;
96     options_.preload = true;
97     options_.lang = CJRuntime::Language::CJ;
98     std::unique_ptr<CJRuntime> runtime = std::make_unique<cjMockRuntime>();
99     AppLibPathMap appLibPaths{};
100     std::vector<std::string> paths = {"/data/test/"};
101     appLibPaths.emplace("", paths);
102     CJRuntime::SetAppLibPath(appLibPaths);
103     runtime_ = runtime->Create(options_);
104 }
105 
TearDown()106 void CjTestRunnerTest::TearDown()
107 {
108 }
109 
110 /**
111  * @tc.name: CjTestRunnerTestCreate_Failed_RuntimeNull_001
112  * @tc.desc: CjTestRunnerTest test for OnMemoryLevel.
113  * @tc.type: FUNC
114  */
115 HWTEST_F(CjTestRunnerTest, CjTestRunnerTestCreate_Failed_RuntimeNull_001, TestSize.Level0)
116 {
117     TAG_LOGI(AAFwkTag::TEST, "CjTestRunnerTestInitialize_Success_001 is called");
118     std::map<std::string, std::string> paras;
119     paras.emplace(KEY_TEST_BUNDLE_NAME, VALUE_TEST_BUNDLE_NAME);
120     paras.emplace(KEY_TEST_RUNNER_CLASS, VALUE_TEST_RUNNER_CLASS);
121     paras.emplace(KEY_TEST_CASE, VALUE_TEST_CASE);
122     paras.emplace(KEY_TEST_WAIT_TIMEOUT, VALUE_TEST_WAIT_TIMEOUT);
123 
124     Want want;
125     for (auto para : paras) {
126         want.SetParam(para.first, para.second);
127     }
128     std::shared_ptr<AbilityDelegatorArgs> abilityArgs = std::make_shared<AbilityDelegatorArgs>(want);
129 
130     std::unique_ptr<Runtime> runtime = static_cast<std::unique_ptr<Runtime>>(std::move(runtime_));
131     std::unique_ptr<TestRunner> testRunner = TestRunner::Create(
132         runtime,
133         abilityArgs,
134         true);
135     EXPECT_TRUE(runtime == nullptr);
136 }
137