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