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 <gtest/gtest.h>
17 #include "cj_runtime.h"
18 #include "runtime.h"
19 #include "cj_mock_runtime.h"
20 
21 #include "event_runner.h"
22 #include "hilog_wrapper.h"
23 #include "cj_runtime.h"
24 
25 using namespace testing;
26 using namespace testing::ext;
27 
28 namespace OHOS {
29 namespace AbilityRuntime {
30 namespace {
31 const std::string TEST_BUNDLE_NAME = "com.ohos.contactsdataability";
32 const std::string TEST_MODULE_NAME = ".ContactsDataAbility";
33 const std::string TEST_ABILITY_NAME = "ContactsDataAbility";
34 const std::string TEST_CODE_PATH = "/data/storage/el1/bundle";
35 const std::string TEST_HAP_PATH = "/system/app/com.ohos.contactsdataability/Contacts_DataAbility.hap";
36 const std::string TEST_LIB_PATH = "/data/storage/el1/bundle/lib/";
37 const std::string TEST_MODULE_PATH = "/data/storage/el1/bundle/curCJModulePath";
38 }  // namespace
39 class CjRuntimeTest : public testing::Test {
40 public:
CjRuntimeTest()41     CjRuntimeTest()
42     {}
~CjRuntimeTest()43     ~CjRuntimeTest()
44     {}
45     static void SetUpTestCase(void);
46     static void TearDownTestCase(void);
47     void SetUp() override;
48     void TearDown() override;
49     Runtime::Options options_;
50 };
51 
SetUpTestCase(void)52 void CjRuntimeTest::SetUpTestCase(void)
53 {}
54 
TearDownTestCase(void)55 void CjRuntimeTest::TearDownTestCase(void)
56 {}
57 
SetUp(void)58 void CjRuntimeTest::SetUp(void)
59 {
60     options_.bundleName = TEST_BUNDLE_NAME;
61     options_.codePath = TEST_CODE_PATH;
62     options_.loadAce = false;
63     options_.isBundle = true;
64     options_.preload = false;
65     std::shared_ptr<AppExecFwk::EventRunner> eventRunner = AppExecFwk::EventRunner::Create(TEST_ABILITY_NAME);
66     options_.eventRunner = eventRunner;
67 }
68 
TearDown(void)69 void CjRuntimeTest::TearDown(void)
70 {}
71 
72 /**
73  * @tc.name: CjRuntimeCreate_001
74  * @tc.desc: Interface Create Test
75  * @tc.type: FUNC
76  */
77 HWTEST_F(CjRuntimeTest, CjRuntimeCreate_001, TestSize.Level1)
78 {
79     options_.preload = true;
80     options_.lang = CJRuntime::Language::CJ;
81     std::unique_ptr<CJRuntime> runtime = std::make_unique<cjMockRuntime>();
82     AppLibPathMap appLibPaths{};
83     CJRuntime::SetAppLibPath(appLibPaths);
84     auto cjRuntime = runtime->Create(options_);
85     EXPECT_EQ(cjRuntime, nullptr);
86     std::vector<std::string> paths = {"/data/test/"};
87     appLibPaths.emplace("", paths);
88     CJRuntime::SetAppLibPath(appLibPaths);
89     cjRuntime = runtime->Create(options_);
90 }
91 
92 /**
93  * @tc.name: CjRuntimeCreate_002
94  * @tc.desc: Interface Create Test for Fail Situation
95  * @tc.type: FUNC
96  */
97 HWTEST_F(CjRuntimeTest, CjRuntimeCreate_002, TestSize.Level1)
98 {
99     options_.preload = true;
100     options_.lang = CJRuntime::Language::JS;
101     std::unique_ptr<CJRuntime> runtime = std::make_unique<cjMockRuntime>();
102     auto cjRuntime = runtime->Create(options_);
103     EXPECT_TRUE(cjRuntime == nullptr);
104 }
105 
106 /**
107  * @tc.name: CjRuntimeSetAppLibPath_001
108  * @tc.desc: Interface SetAppLibPath Test
109  * @tc.type: FUNC
110  */
111 HWTEST_F(CjRuntimeTest, CjRuntimeSetAppLibPath_001, TestSize.Level0)
112 {
113     std::string appLibPathKey = TEST_BUNDLE_NAME + TEST_MODULE_NAME;
114     std::string libPath = TEST_LIB_PATH;
115 
116     AppLibPathMap appLibPaths{};
117     CJRuntime::SetAppLibPath(appLibPaths);
118 
119     appLibPaths[appLibPathKey].emplace_back(libPath);
120     EXPECT_NE(appLibPaths.size(), 0);
121     CJRuntime::SetAppLibPath(appLibPaths);
122 }
123 
124 /**
125  * @tc.name: CjRuntimeGetLanguageTest_001
126  * @tc.desc: CjRuntime Test for GetLanguage
127  * @tc.type: FUNC
128  */
129 HWTEST_F(CjRuntimeTest, CjRuntimeGetLanguageTest_001, TestSize.Level0)
130 {
131     auto instance = std::make_unique<CJRuntime>();
132 
133     CJRuntime::Language language = instance->GetLanguage();
134     EXPECT_TRUE(language == CJRuntime::Language::CJ);
135     instance->UnLoadCJAppLibrary();
136 }
137 
138 /**
139  * @tc.name: CjRuntimeStartDebuggerMode_001
140  * @tc.desc: CjRuntime test for StartDebuggerMode.
141  * @tc.type: FUNC
142  */
143 HWTEST_F(CjRuntimeTest, CjRuntimeStartDebuggerMode_001, TestSize.Level0)
144 {
145     auto instance = std::make_unique<CJRuntime>();
146 
147     bool needBreakPoint = true;
148     bool debugApp = true;
149     const std::string processName = "test";
150 
151     CJRuntime::DebugOption debugOption;
152     debugOption.isStartWithDebug = needBreakPoint;
153     debugOption.isDebugApp = debugApp;
154     debugOption.processName = processName;
155 
156     instance->StartDebugMode(debugOption);
157     instance->StartDebugMode(debugOption);
158 }
159 
160 }  // namespace Runtime
161 }  // namespace OHOS
162