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 <iostream>
17 #include <string>
18 #include "gtest/gtest.h"
19 #include "cj_ability_delegator.h"
20 #include "ability_delegator_registry.h"
21 #include "cj_application_context.h"
22 #include "application_context.h"
23 #include "runner_runtime/cj_test_runner.h"
24
25 using namespace testing;
26 using namespace testing::ext;
27 using namespace OHOS::FFI;
28 using namespace OHOS::AbilityRuntime;
29 using namespace OHOS::AppExecFwk;
30 using namespace OHOS::AAFwk;
31 using namespace OHOS::RunnerRuntime;
32
33 namespace OHOS {
34 namespace AbilityDelegatorCJ {
35 namespace {
36 const std::string KEY_TEST_BUNDLE_NAME = "-p";
37 const std::string VALUE_TEST_BUNDLE_NAME = "com.example.myapplication";
38 const std::string CHANGE_VALUE_TEST_BUNDLE_NAME = "com.example.myapplication1";
39 const std::string KEY_TEST_RUNNER_CLASS = "-s unittest";
40 const std::string VALUE_TEST_RUNNER_CLASS = "JSUserTestRunner";
41 const std::string CHANGE_VALUE_TEST_RUNNER_CLASS = "JSUserTestRunner1";
42 const std::string KEY_TEST_CASE = "-s class";
43 const std::string VALUE_TEST_CASE = "ohos.acts.aafwk.ability.test.ConstructorTest#testDataAbilityOtherFunction0010";
44 const std::string CHANGE_VALUE_TEST_CASE =
45 "ohos.acts.aafwk.ability.test.ConstructorTest#testDataAbilityOtherFunction00101";
46 const std::string KEY_TEST_WAIT_TIMEOUT = "-w";
47 const std::string VALUE_TEST_WAIT_TIMEOUT = "50";
48 const std::string CHANGE_VALUE_TEST_WAIT_TIMEOUT = "80";
49 const std::string SET_VALUE_TEST_BUNDLE_NAME = "com.example.myapplicationset";
50 const std::string ABILITY_NAME = "com.example.myapplication.MainAbility";
51 const std::string FINISH_MSG = "finish message";
52 const int32_t FINISH_RESULT_CODE = 144;
53 const std::string PRINT_MSG = "print aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
54 const int ZERO = 0;
55 const int ONE = 1;
56 const int TWO = 2;
57 const int64_t TIMEOUT = 50;
58 const std::string CMD = "ls -l";
59 const std::string KEY_TEST_DEBUG {"-D"};
60 const std::string VALUE_TEST_DEBUG {"true"};
61 const std::string ABILITY_STAGE_MONITOR_MODULE_NAME {"entry"};
62 const std::string ABILITY_STAGE_MONITOR_SRC_ENTRANCE {"MainAbility"};
63 } // namespace
64
65 class CjAbilityDelegatorTest : public testing::Test {
66 public:
CjAbilityDelegatorTest()67 CjAbilityDelegatorTest()
68 {}
~CjAbilityDelegatorTest()69 ~CjAbilityDelegatorTest()
70 {}
71 static void SetUpTestCase();
72 static void TearDownTestCase();
73 void SetUp() override;
74 void TearDown() override;
75 protected:
76 static std::shared_ptr<CJAbilityDelegator> cjDelegator;
77 static std::shared_ptr<CJAbilityDelegatorImpl> commonDelegator_;
78 static std::shared_ptr<AbilityDelegatorArgs> delegatorArgs_;
79 };
80
81 std::shared_ptr<CJAbilityDelegator> CjAbilityDelegatorTest::cjDelegator = nullptr;
82 std::shared_ptr<CJAbilityDelegatorImpl> CjAbilityDelegatorTest::commonDelegator_ = nullptr;
83 std::shared_ptr<AbilityDelegatorArgs> CjAbilityDelegatorTest::delegatorArgs_ = nullptr;
84
SetUpTestCase()85 void CjAbilityDelegatorTest::SetUpTestCase()
86 {
87 // Construct a common ability delegator firstly.
88 std::map<std::string, std::string> paras;
89 paras.emplace(KEY_TEST_BUNDLE_NAME, VALUE_TEST_BUNDLE_NAME);
90 paras.emplace(KEY_TEST_RUNNER_CLASS, VALUE_TEST_RUNNER_CLASS);
91 paras.emplace(KEY_TEST_CASE, VALUE_TEST_CASE);
92 paras.emplace(KEY_TEST_WAIT_TIMEOUT, VALUE_TEST_WAIT_TIMEOUT);
93 paras.emplace(KEY_TEST_DEBUG, VALUE_TEST_DEBUG);
94
95 Want want;
96 for (auto para : paras) {
97 want.SetParam(para.first, para.second);
98 }
99
100 delegatorArgs_ = std::make_shared<AbilityDelegatorArgs>(want);
101 AbilityRuntime::Runtime::Options options;
102 BundleInfo bundleInfo;
103 auto testRunner = CJTestRunner::Create(AbilityRuntime::Runtime::Create(options), delegatorArgs_, bundleInfo);
104 commonDelegator_ = std::make_shared<CJAbilityDelegatorImpl>(std::make_shared<AbilityRuntime::ContextImpl>(),
105 std::move(testRunner), nullptr);
106
107 // 创建一个 CJAbilityDelegator 对象
108 cjDelegator = std::make_shared<CJAbilityDelegator>(commonDelegator_);
109 }
110
TearDownTestCase()111 void CjAbilityDelegatorTest::TearDownTestCase()
112 {}
113
SetUp()114 void CjAbilityDelegatorTest::SetUp()
115 {}
116
TearDown()117 void CjAbilityDelegatorTest::TearDown()
118 {}
119
120 /**
121 * @tc.name: CjAbilityDelegatorTestStartAbility_001
122 * @tc.desc: CjAbilityDelegatorTest test for FFICJWantAddEntity.
123 * @tc.type: FUNC
124 */
125 HWTEST_F(CjAbilityDelegatorTest, CjAbilityDelegatorTestStartAbility_001, TestSize.Level1)
126 {
127 EXPECT_NE(commonDelegator_, nullptr);
128 AbilityDelegatorRegistry::RegisterInstance(commonDelegator_, delegatorArgs_,
129 OHOS::AbilityRuntime::Runtime::Language::CJ);
130
131 AAFwk::Want want;
132 want.SetElementName(VALUE_TEST_BUNDLE_NAME, ABILITY_NAME);
133 auto result = cjDelegator->StartAbility(want);
134 }
135
136 /**
137 * @tc.name: CjAbilityDelegatorTestExecuteShellCommand_001
138 * @tc.desc: CjAbilityDelegatorTest test for ExecuteShellCommand.
139 * @tc.type: FUNC
140 */
141 HWTEST_F(CjAbilityDelegatorTest, CjAbilityDelegatorTestExecuteShellCommand_001, TestSize.Level1)
142 {
143 const char* cmd = "ls";
144 int64_t timeoutSec = 10;
145 auto shellCmdResult = cjDelegator->ExecuteShellCommand(cmd, timeoutSec);
146 EXPECT_EQ(shellCmdResult, nullptr);
147 }
148
149 /**
150 * @tc.name: CjAbilityDelegatorTestGetAppContext_001
151 * @tc.desc: CjAbilityDelegatorTest test for GetAppContext.
152 * @tc.type: FUNC
153 */
154 HWTEST_F(CjAbilityDelegatorTest, CjAbilityDelegatorTestGetAppContext_001, TestSize.Level1)
155 {
156 auto appContext = cjDelegator->GetAppContext();
157 EXPECT_EQ(appContext, nullptr);
158 }
159
160 /**
161 * @tc.name: CjAbilityDelegatorTestGetExitCode_001
162 * @tc.desc: CjAbilityDelegatorTest test for GetExitCode.
163 * @tc.type: FUNC
164 */
165 HWTEST_F(CjAbilityDelegatorTest, CjAbilityDelegatorTestGetExitCode_001, TestSize.Level1)
166 {
167 auto shellResult = std::make_shared<AppExecFwk::ShellCmdResult>();
168 CJShellCmdResult shellCmdResult(shellResult);
169 int32_t exitCode = shellCmdResult.GetExitCode();
170 EXPECT_GE(exitCode, -1);
171 }
172
173 /**
174 * @tc.name: CjAbilityDelegatorTestGetStdResult_001
175 * @tc.desc: CjAbilityDelegatorTest test for GetStdResult.
176 * @tc.type: FUNC
177 */
178 HWTEST_F(CjAbilityDelegatorTest, CjAbilityDelegatorTestGetStdResult_001, TestSize.Level1)
179 {
180 auto shellResult = std::make_shared<AppExecFwk::ShellCmdResult>();
181 CJShellCmdResult shellCmdResult(shellResult);
182 std::string stdResult = shellCmdResult.GetStdResult();
183 EXPECT_TRUE(stdResult.empty());
184 }
185
186 /**
187 * @tc.name: CjAbilityDelegatorTestDump_001
188 * @tc.desc: CjAbilityDelegatorTest test for Dump.
189 * @tc.type: FUNC
190 */
191 HWTEST_F(CjAbilityDelegatorTest, CjAbilityDelegatorTestDump_001, TestSize.Level1)
192 {
193 auto shellResult = std::make_shared<AppExecFwk::ShellCmdResult>();
194 CJShellCmdResult shellCmdResult(shellResult);
195 shellCmdResult.Dump();
196 EXPECT_TRUE(shellResult != nullptr);
197 }
198
199 /**
200 * @tc.name: CJAbilityDelegatorTestFFIAbilityDelegatorRegistryGetAbilityDelegator_001
201 * @tc.desc: CjAbilityDelegatorTest test for FFIAbilityDelegatorRegistryGetAbilityDelegator.
202 * @tc.type: FUNC
203 */
204 HWTEST_F(CjAbilityDelegatorTest,
205 CJAbilityDelegatorTestFFIAbilityDelegatorRegistryGetAbilityDelegator_001, TestSize.Level1)
206 {
207 auto result = FFIAbilityDelegatorRegistryGetAbilityDelegator();
208 EXPECT_NE(result, ERR_OK);
209 }
210
211 /**
212 * @tc.name: CJAbilityDelegatorTestFFIAbilityDelegatorStartAbility_001
213 * @tc.desc: CjAbilityDelegatorTest test for FFIAbilityDelegatorStartAbility.
214 * @tc.type: FUNC
215 */
216 HWTEST_F(CjAbilityDelegatorTest, CJAbilityDelegatorTestFFIAbilityDelegatorStartAbility_001, TestSize.Level1)
217 {
218 Want want;
219 WantHandle wantHandle = const_cast<AAFwk::Want *>(&want);
220 auto delegator = OHOS::AppExecFwk::AbilityDelegatorRegistry::GetCJAbilityDelegator();
221 auto cjDelegator = FFI::FFIData::Create<CJAbilityDelegator>(delegator);
222 int64_t id = cjDelegator->GetID();
223 int64_t ret = FFIAbilityDelegatorStartAbility(id, wantHandle);
224 EXPECT_NE(ret, 1);
225 }
226
227 /**
228 * @tc.name: CJAbilityDelegatorTestFFIAbilityDelegatorExecuteShellCommand_001
229 * @tc.desc: CjAbilityDelegatorTest test for FFIAbilityDelegatorExecuteShellCommand.
230 * @tc.type: FUNC
231 */
232 HWTEST_F(CjAbilityDelegatorTest, CJAbilityDelegatorTestFFIAbilityDelegatorExecuteShellCommand_001, TestSize.Level1)
233 {
234 const char* cmd = "test";
235 int64_t timeoutSec = 1000;
236 auto delegator = OHOS::AppExecFwk::AbilityDelegatorRegistry::GetCJAbilityDelegator();
237 auto cjDelegator = FFI::FFIData::Create<CJAbilityDelegator>(delegator);
238 int64_t id = cjDelegator->GetID();
239 auto result = FFIAbilityDelegatorExecuteShellCommand(id, cmd, timeoutSec);
240 EXPECT_TRUE(cjDelegator != nullptr);
241 }
242
243 /**
244 * @tc.name: CJAbilityDelegatorTestFFIGetExitCode_001
245 * @tc.desc: CjAbilityDelegatorTest test for FFIGetExitCode.
246 * @tc.type: FUNC
247 */
248 HWTEST_F(CjAbilityDelegatorTest, CJAbilityDelegatorTestFFIGetExitCode_001, TestSize.Level1)
249 {
250 int64_t timeoutSec = 1000;
251 const char* cmd = "ls";
252
253 auto shellResult = std::make_shared<AppExecFwk::ShellCmdResult>();
254 auto cJShellCmdResult = FFI::FFIData::Create<CJShellCmdResult>(shellResult);
255 int64_t id = cJShellCmdResult->GetID();
256 auto result = FFIGetExitCode(id);
257 EXPECT_TRUE(cJShellCmdResult != nullptr);
258 }
259
260 /**
261 * @tc.name: CJAbilityDelegatorTestFFIGetStdResult_001
262 * @tc.desc: CjAbilityDelegatorTest test for FFIGetStdResult.
263 * @tc.type: FUNC
264 */
265 HWTEST_F(CjAbilityDelegatorTest, CJAbilityDelegatorTestFFIGetStdResult_001, TestSize.Level1)
266 {
267 int64_t timeoutSec = 1000;
268 const char* cmd = "ls";
269 auto shellResult = std::make_shared<AppExecFwk::ShellCmdResult>();
270 auto cJShellCmdResult = FFI::FFIData::Create<CJShellCmdResult>(shellResult);
271 int64_t id = cJShellCmdResult->GetID();
272 auto result = FFIGetStdResult(id);
273 EXPECT_TRUE(result == nullptr);
274 }
275
276 /**
277 * @tc.name: CJAbilityDelegatorTestFFIDump_001
278 * @tc.desc: CjAbilityDelegatorTest test for FFIDump.
279 * @tc.type: FUNC
280 */
281 HWTEST_F(CjAbilityDelegatorTest, CJAbilityDelegatorTestFFIDump_001, TestSize.Level1)
282 {
283 int64_t timeoutSec = 1000;
284 const char* cmd = "ls";
285 auto shellResult = std::make_shared<AppExecFwk::ShellCmdResult>();
286 auto cJShellCmdResult = FFI::FFIData::Create<CJShellCmdResult>(shellResult);
287 int64_t id = cJShellCmdResult->GetID();
288 auto result = FFIDump(id);
289 EXPECT_TRUE(result != nullptr);
290 }
291
292 /**
293 * @tc.name: CJAbilityDelegatorTestFFIAbilityDelegatorApplicationContext_001
294 * @tc.desc: CjAbilityDelegatorTest test for FFIAbilityDelegatorApplicationContext.
295 * @tc.type: FUNC
296 */
297 HWTEST_F(CjAbilityDelegatorTest, CJAbilityDelegatorTestFFIAbilityDelegatorApplicationContext_001, TestSize.Level1)
298 {
299 auto delegator = OHOS::AppExecFwk::AbilityDelegatorRegistry::GetCJAbilityDelegator();
300 auto cjDelegator = FFI::FFIData::Create<CJAbilityDelegator>(delegator);
301 int64_t id = cjDelegator->GetID();
302 auto result = FFIAbilityDelegatorApplicationContext(id);
303 EXPECT_TRUE(cjDelegator != nullptr);
304 }
305
306 } // namespace AbilityRuntime
307 } // namespace OHOS