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<AbilityDelegator> commonDelegator_;
78     static std::shared_ptr<AbilityDelegatorArgs> delegatorArgs_;
79 };
80 
81 std::shared_ptr<CJAbilityDelegator> CjAbilityDelegatorTest::cjDelegator = nullptr;
82 std::shared_ptr<AbilityDelegator> 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<AbilityDelegator>(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 
130     AAFwk::Want want;
131     want.SetElementName(VALUE_TEST_BUNDLE_NAME, ABILITY_NAME);
132     auto result = cjDelegator->StartAbility(want);
133 }
134 
135 /**
136  * @tc.name: CjAbilityDelegatorTestExecuteShellCommand_001
137  * @tc.desc: CjAbilityDelegatorTest test for ExecuteShellCommand.
138  * @tc.type: FUNC
139  */
140 HWTEST_F(CjAbilityDelegatorTest, CjAbilityDelegatorTestExecuteShellCommand_001, TestSize.Level1)
141 {
142     const char* cmd = "ls";
143     int64_t timeoutSec = 10;
144     auto shellCmdResult = cjDelegator->ExecuteShellCommand(cmd, timeoutSec);
145     EXPECT_EQ(shellCmdResult, nullptr);
146 }
147 
148 /**
149  * @tc.name: CjAbilityDelegatorTestGetAppContext_001
150  * @tc.desc: CjAbilityDelegatorTest test for GetAppContext.
151  * @tc.type: FUNC
152  */
153 HWTEST_F(CjAbilityDelegatorTest, CjAbilityDelegatorTestGetAppContext_001, TestSize.Level1)
154 {
155     auto appContext = cjDelegator->GetAppContext();
156     EXPECT_EQ(appContext, nullptr);
157 }
158 
159 /**
160  * @tc.name: CjAbilityDelegatorTestGetExitCode_001
161  * @tc.desc: CjAbilityDelegatorTest test for GetExitCode.
162  * @tc.type: FUNC
163  */
164 HWTEST_F(CjAbilityDelegatorTest, CjAbilityDelegatorTestGetExitCode_001, TestSize.Level1)
165 {
166     auto shellResult = std::make_shared<AppExecFwk::ShellCmdResult>();
167     CJShellCmdResult shellCmdResult(shellResult);
168     int32_t exitCode = shellCmdResult.GetExitCode();
169     EXPECT_GE(exitCode, -1);
170 }
171 
172 /**
173  * @tc.name: CjAbilityDelegatorTestGetStdResult_001
174  * @tc.desc: CjAbilityDelegatorTest test for GetStdResult.
175  * @tc.type: FUNC
176  */
177 HWTEST_F(CjAbilityDelegatorTest, CjAbilityDelegatorTestGetStdResult_001, TestSize.Level1)
178 {
179     auto shellResult = std::make_shared<AppExecFwk::ShellCmdResult>();
180     CJShellCmdResult shellCmdResult(shellResult);
181     std::string stdResult = shellCmdResult.GetStdResult();
182     EXPECT_TRUE(stdResult.empty());
183 }
184 
185 /**
186  * @tc.name: CjAbilityDelegatorTestDump_001
187  * @tc.desc: CjAbilityDelegatorTest test for Dump.
188  * @tc.type: FUNC
189  */
190 HWTEST_F(CjAbilityDelegatorTest, CjAbilityDelegatorTestDump_001, TestSize.Level1)
191 {
192     auto shellResult = std::make_shared<AppExecFwk::ShellCmdResult>();
193     CJShellCmdResult shellCmdResult(shellResult);
194     shellCmdResult.Dump();
195     EXPECT_TRUE(shellResult != nullptr);
196 }
197 
198 /**
199  * @tc.name: CJAbilityDelegatorTestFFIAbilityDelegatorRegistryGetAbilityDelegator_001
200  * @tc.desc: CjAbilityDelegatorTest test for FFIAbilityDelegatorRegistryGetAbilityDelegator.
201  * @tc.type: FUNC
202  */
203 HWTEST_F(CjAbilityDelegatorTest,
204     CJAbilityDelegatorTestFFIAbilityDelegatorRegistryGetAbilityDelegator_001, TestSize.Level1)
205 {
206     auto result = FFIAbilityDelegatorRegistryGetAbilityDelegator();
207     EXPECT_NE(result, ERR_OK);
208 }
209 
210 /**
211  * @tc.name: CJAbilityDelegatorTestFFIAbilityDelegatorStartAbility_001
212  * @tc.desc: CjAbilityDelegatorTest test for FFIAbilityDelegatorStartAbility.
213  * @tc.type: FUNC
214  */
215 HWTEST_F(CjAbilityDelegatorTest, CJAbilityDelegatorTestFFIAbilityDelegatorStartAbility_001, TestSize.Level1)
216 {
217     Want want;
218     WantHandle wantHandle = const_cast<AAFwk::Want *>(&want);
219     auto delegator = OHOS::AppExecFwk::AbilityDelegatorRegistry::GetAbilityDelegator();
220     auto cjDelegator = FFI::FFIData::Create<CJAbilityDelegator>(delegator);
221     int64_t id = cjDelegator->GetID();
222     int64_t ret = FFIAbilityDelegatorStartAbility(id, wantHandle);
223     EXPECT_NE(ret, 1);
224 }
225 
226 /**
227  * @tc.name: CJAbilityDelegatorTestFFIAbilityDelegatorExecuteShellCommand_001
228  * @tc.desc: CjAbilityDelegatorTest test for FFIAbilityDelegatorExecuteShellCommand.
229  * @tc.type: FUNC
230  */
231 HWTEST_F(CjAbilityDelegatorTest, CJAbilityDelegatorTestFFIAbilityDelegatorExecuteShellCommand_001, TestSize.Level1)
232 {
233     const char* cmd = "test";
234     int64_t timeoutSec = 1000;
235     auto delegator = OHOS::AppExecFwk::AbilityDelegatorRegistry::GetAbilityDelegator();
236     auto cjDelegator = FFI::FFIData::Create<CJAbilityDelegator>(delegator);
237     int64_t id = cjDelegator->GetID();
238     auto result = FFIAbilityDelegatorExecuteShellCommand(id, cmd, timeoutSec);
239     EXPECT_TRUE(cjDelegator != nullptr);
240 }
241 
242 /**
243  * @tc.name: CJAbilityDelegatorTestFFIGetExitCode_001
244  * @tc.desc: CjAbilityDelegatorTest test for FFIGetExitCode.
245  * @tc.type: FUNC
246  */
247 HWTEST_F(CjAbilityDelegatorTest, CJAbilityDelegatorTestFFIGetExitCode_001, TestSize.Level1)
248 {
249     int64_t timeoutSec = 1000;
250     const char* cmd = "ls";
251 
252     auto shellResult = std::make_shared<AppExecFwk::ShellCmdResult>();
253     auto cJShellCmdResult = FFI::FFIData::Create<CJShellCmdResult>(shellResult);
254     int64_t id = cJShellCmdResult->GetID();
255     auto result = FFIGetExitCode(id);
256     EXPECT_TRUE(cJShellCmdResult != nullptr);
257 }
258 
259 /**
260  * @tc.name: CJAbilityDelegatorTestFFIGetStdResult_001
261  * @tc.desc: CjAbilityDelegatorTest test for FFIGetStdResult.
262  * @tc.type: FUNC
263  */
264 HWTEST_F(CjAbilityDelegatorTest, CJAbilityDelegatorTestFFIGetStdResult_001, TestSize.Level1)
265 {
266     int64_t timeoutSec = 1000;
267     const char* cmd = "ls";
268     auto shellResult = std::make_shared<AppExecFwk::ShellCmdResult>();
269     auto cJShellCmdResult = FFI::FFIData::Create<CJShellCmdResult>(shellResult);
270     int64_t id = cJShellCmdResult->GetID();
271     auto result = FFIGetStdResult(id);
272     EXPECT_TRUE(result == nullptr);
273 }
274 
275 /**
276  * @tc.name: CJAbilityDelegatorTestFFIDump_001
277  * @tc.desc: CjAbilityDelegatorTest test for FFIDump.
278  * @tc.type: FUNC
279  */
280 HWTEST_F(CjAbilityDelegatorTest, CJAbilityDelegatorTestFFIDump_001, TestSize.Level1)
281 {
282     int64_t timeoutSec = 1000;
283     const char* cmd = "ls";
284     auto shellResult = std::make_shared<AppExecFwk::ShellCmdResult>();
285     auto cJShellCmdResult = FFI::FFIData::Create<CJShellCmdResult>(shellResult);
286     int64_t id = cJShellCmdResult->GetID();
287     auto result = FFIDump(id);
288     EXPECT_TRUE(result != nullptr);
289 }
290 
291 /**
292  * @tc.name: CJAbilityDelegatorTestFFIAbilityDelegatorApplicationContext_001
293  * @tc.desc: CjAbilityDelegatorTest test for FFIAbilityDelegatorApplicationContext.
294  * @tc.type: FUNC
295  */
296 HWTEST_F(CjAbilityDelegatorTest, CJAbilityDelegatorTestFFIAbilityDelegatorApplicationContext_001, TestSize.Level1)
297 {
298     auto delegator = OHOS::AppExecFwk::AbilityDelegatorRegistry::GetAbilityDelegator();
299     auto cjDelegator = FFI::FFIData::Create<CJAbilityDelegator>(delegator);
300     int64_t id = cjDelegator->GetID();
301     auto result = FFIAbilityDelegatorApplicationContext(id);
302     EXPECT_TRUE(cjDelegator != nullptr);
303 }
304 
305 }  // namespace AbilityRuntime
306 }  // namespace OHOS