1 /*
2 * Copyright (c) 2022 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 <thread>
17 #include <functional>
18 #include <fstream>
19 #include <nlohmann/json.hpp>
20
21 #include <gtest/gtest.h>
22 #include <gmock/gmock.h>
23
24 #define private public
25 #define protected public
26 #include "sa_mgr_client.h"
27 #include "mock_ability_connect_callback_stub.h"
28 #include "mock_ability_scheduler.h"
29 #include "mock_app_mgr_client.h"
30 #include "mock_bundle_mgr.h"
31 #include "mock_native_token.h"
32 #include "ability_state.h"
33 #include "ability_manager_errors.h"
34 #include "sa_mgr_client.h"
35 #include "system_ability_definition.h"
36 #include "ability_manager_service.h"
37 #include "ability_connect_callback_proxy.h"
38 #include "ability_config.h"
39 #include "pending_want_manager.h"
40 #include "pending_want_record.h"
41 #undef private
42 #undef protected
43 #include "wants_info.h"
44 #include "want_receiver_stub.h"
45 #include "want_sender_stub.h"
46
47 using namespace std::placeholders;
48 using namespace testing::ext;
49 using namespace OHOS::AppExecFwk;
50 using OHOS::iface_cast;
51 using OHOS::IRemoteObject;
52 using OHOS::sptr;
53 using testing::_;
54 using testing::Invoke;
55 using testing::Return;
56
57 namespace OHOS {
58 namespace AAFwk {
59 namespace {
60 const int32_t MOCK_MAIN_USER_ID = 100;
61
WaitUntilTaskFinished()62 static void WaitUntilTaskFinished()
63 {
64 const uint32_t maxRetryCount = 1000;
65 const uint32_t sleepTime = 1000;
66 uint32_t count = 0;
67 auto handler = OHOS::DelayedSingleton<AbilityManagerService>::GetInstance()->GetEventHandler();
68 std::atomic<bool> taskCalled(false);
69 auto f = [&taskCalled]() { taskCalled.store(true); };
70 if (handler->PostTask(f)) {
71 while (!taskCalled.load()) {
72 ++count;
73 if (count >= maxRetryCount) {
74 break;
75 }
76 usleep(sleepTime);
77 }
78 }
79 }
80 } // namespace
81 class RunningInfosModuleTest : public testing::Test {
82 public:
83 static void SetUpTestCase(void);
84 static void TearDownTestCase(void);
85 void SetUp();
86 void TearDown();
87 void OnStartAms();
88 void OnStopAms();
89 Want CreateWant(const std::string &abilityName, const std::string &bundleName);
90
91 inline static std::shared_ptr<AbilityManagerService> abilityMgrServ_ {nullptr};
92 inline static MockAppMgrClient *mockAppMgrClient_ = nullptr;
93 };
94
CreateWant(const std::string & abilityName,const std::string & bundleName)95 Want RunningInfosModuleTest::CreateWant(const std::string &abilityName, const std::string &bundleName)
96 {
97 ElementName element;
98 element.SetDeviceID("device");
99 element.SetAbilityName(abilityName);
100 element.SetBundleName(bundleName);
101 Want want;
102 want.SetElement(element);
103 return want;
104 }
105
OnStartAms()106 void RunningInfosModuleTest::OnStartAms()
107 {
108 if (abilityMgrServ_) {
109 if (abilityMgrServ_->state_ == ServiceRunningState::STATE_RUNNING) {
110 return;
111 }
112
113 abilityMgrServ_->state_ = ServiceRunningState::STATE_RUNNING;
114
115 abilityMgrServ_->eventLoop_ = AppExecFwk::EventRunner::Create(AbilityConfig::NAME_ABILITY_MGR_SERVICE);
116 EXPECT_TRUE(abilityMgrServ_->eventLoop_);
117
118 abilityMgrServ_->handler_ = std::make_shared<AbilityEventHandler>(abilityMgrServ_->eventLoop_, abilityMgrServ_);
119 EXPECT_TRUE(abilityMgrServ_->handler_);
120
121 // init user controller.
122 abilityMgrServ_->userController_ = std::make_shared<UserController>();
123 EXPECT_TRUE(abilityMgrServ_->userController_);
124 abilityMgrServ_->userController_->Init();
125 int userId = MOCK_MAIN_USER_ID;
126 abilityMgrServ_->userController_->SetCurrentUserId(userId);
127 abilityMgrServ_->InitConnectManager(userId, true);
128 abilityMgrServ_->InitDataAbilityManager(userId, true);
129 abilityMgrServ_->InitPendWantManager(userId, true);
130 abilityMgrServ_->systemDataAbilityManager_ = std::make_shared<DataAbilityManager>();
131 EXPECT_TRUE(abilityMgrServ_->systemDataAbilityManager_);
132
133 abilityMgrServ_->amsConfigResolver_ = std::make_shared<AmsConfigurationParameter>();
134 EXPECT_TRUE(abilityMgrServ_->amsConfigResolver_);
135 abilityMgrServ_->amsConfigResolver_->Parse();
136
137 abilityMgrServ_->interceptorExecuter_ = std::make_shared<AbilityInterceptorExecuter>();
138 EXPECT_TRUE(abilityMgrServ_->interceptorExecuter_);
139
140 abilityMgrServ_->InitMissionListManager(userId, true);
141 abilityMgrServ_->connectManager_->SetEventHandler(abilityMgrServ_->handler_);
142 abilityMgrServ_->eventLoop_->Run();
143 auto topAbility = abilityMgrServ_->GetListManagerByUserId(MOCK_MAIN_USER_ID)->GetCurrentTopAbilityLocked();
144 if (topAbility) {
145 topAbility->SetAbilityState(AAFwk::AbilityState::FOREGROUND);
146 }
147 WaitUntilTaskFinished();
148 return;
149 }
150
151 GTEST_LOG_(INFO) << "OnStart fail";
152 }
153
OnStopAms()154 void RunningInfosModuleTest::OnStopAms()
155 {
156 abilityMgrServ_->eventLoop_.reset();
157 abilityMgrServ_->handler_.reset();
158 abilityMgrServ_->state_ = ServiceRunningState::STATE_NOT_START;
159 }
160
SetUpTestCase()161 void RunningInfosModuleTest::SetUpTestCase()
162 {
163 MockNativeToken::SetNativeToken();
164 OHOS::DelayedSingleton<SaMgrClient>::GetInstance()->RegisterSystemAbility(
165 OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, new BundleMgrService());
166 auto appScheduler = DelayedSingleton<AppScheduler>::GetInstance();
167 mockAppMgrClient_ = new MockAppMgrClient();
168 if (mockAppMgrClient_) {
169 appScheduler->appMgrClient_.reset(mockAppMgrClient_);
170 GTEST_LOG_(INFO) << "mock appMgrClient_ ok";
171 }
172 }
173
TearDownTestCase()174 void RunningInfosModuleTest::TearDownTestCase()
175 {
176 delete mockAppMgrClient_;
177 mockAppMgrClient_ = nullptr;
178 OHOS::DelayedSingleton<SaMgrClient>::DestroyInstance();
179 }
180
SetUp()181 void RunningInfosModuleTest::SetUp()
182 {
183 abilityMgrServ_ = OHOS::DelayedSingleton<AbilityManagerService>::GetInstance();
184 OnStartAms();
185 }
186
TearDown()187 void RunningInfosModuleTest::TearDown()
188 {
189 OnStopAms();
190 OHOS::DelayedSingleton<AbilityManagerService>::DestroyInstance();
191 }
192
193 /*
194 * Feature: AbilityManagerService
195 * Function: GetAbilityRunningInfos
196 * SubFunction: NA
197 * FunctionPoints:query ability running infos
198 * EnvConditions: NA
199 * CaseDescription: start page ability, mock object, call query function.
200 */
201 HWTEST_F(RunningInfosModuleTest, GetAbilityRunningInfos_001, TestSize.Level1)
202 {
203 std::string abilityName = "MusicAbility";
204 std::string bundleName = "com.ix.hiMusic";
205 EXPECT_CALL(*mockAppMgrClient_, LoadAbility(_, _, _, _, _)).Times(1);
206 Want want = CreateWant(abilityName, bundleName);
207
208 auto result = abilityMgrServ_->StartAbility(want);
209 EXPECT_EQ(OHOS::ERR_OK, result);
210 EXPECT_CALL(*mockAppMgrClient_, GetRunningProcessInfoByToken(_, _)).Times(1);
211
212 std::vector<AbilityRunningInfo> infos;
213 abilityMgrServ_->GetAbilityRunningInfos(infos);
214 size_t infoCount {1};
215 EXPECT_TRUE(infos.size() == infoCount);
216 if (infos.size() == infoCount) {
217 EXPECT_TRUE(infos[0].ability.GetAbilityName() == abilityName);
218 EXPECT_TRUE(infos[0].abilityState == static_cast<int>(AbilityState::INITIAL));
219 }
220 }
221
222 /*
223 * Feature: AbilityManagerService
224 * Function: GetAbilityRunningInfos
225 * SubFunction: NA
226 * FunctionPoints:query ability running infos
227 * EnvConditions: NA
228 * CaseDescription: start service ability, mock object, call query function.
229 */
230 HWTEST_F(RunningInfosModuleTest, GetAbilityRunningInfos_002, TestSize.Level1)
231 {
232 std::string abilityName = "EnterAbility";
233 std::string bundleName = "com.ohos.camera";
234 EXPECT_CALL(*mockAppMgrClient_, LoadAbility(_, _, _, _, _)).Times(1);
235 Want want = CreateWant(abilityName, bundleName);
236 auto result = abilityMgrServ_->StartAbility(want);
237 EXPECT_EQ(OHOS::ERR_OK, result);
238 EXPECT_CALL(*mockAppMgrClient_, GetRunningProcessInfoByToken(_, _)).Times(1);
239
240 std::vector<AbilityRunningInfo> infos;
241 abilityMgrServ_->GetAbilityRunningInfos(infos);
242 size_t infoCount {1};
243 EXPECT_TRUE(infos.size() == infoCount);
244 if (infos.size() == infoCount) {
245 EXPECT_TRUE(infos[0].ability.GetAbilityName() == abilityName);
246 EXPECT_TRUE(infos[0].abilityState == static_cast<int>(AbilityState::INITIAL));
247 }
248 }
249
250 /*
251 * Feature: AbilityManagerService
252 * Function: GetAbilityRunningInfos
253 * SubFunction: NA
254 * FunctionPoints:query ability running infos
255 * EnvConditions: NA
256 * CaseDescription: start launcher, mock object, call query function.
257 */
258 HWTEST_F(RunningInfosModuleTest, GetAbilityRunningInfos_003, TestSize.Level1)
259 {
260 std::string abilityName = "com.ohos.launcher.MainAbility";
261 std::string bundleName = "com.ohos.launcher";
262 EXPECT_CALL(*mockAppMgrClient_, LoadAbility(_, _, _, _, _)).Times(1);
263 Want want = CreateWant(abilityName, bundleName);
264 auto result = abilityMgrServ_->StartAbility(want);
265 EXPECT_EQ(OHOS::ERR_OK, result);
266 EXPECT_CALL(*mockAppMgrClient_, GetRunningProcessInfoByToken(_, _)).Times(1);
267
268 std::vector<AbilityRunningInfo> infos;
269 abilityMgrServ_->GetAbilityRunningInfos(infos);
270 size_t infoCount {1};
271 EXPECT_TRUE(infos.size() == infoCount);
272 if (infos.size() == infoCount) {
273 EXPECT_TRUE(infos[0].ability.GetAbilityName() == abilityName);
274 EXPECT_TRUE(infos[0].abilityState == static_cast<int>(AbilityState::INITIAL));
275 }
276 }
277
278 /*
279 * Feature: AbilityManagerService
280 * Function: GetAbilityRunningInfos
281 * SubFunction: NA
282 * FunctionPoints:query ability running infos
283 * EnvConditions: NA
284 * CaseDescription: start two page abilities, mock object, call query function.
285 */
286 HWTEST_F(RunningInfosModuleTest, GetAbilityRunningInfos_004, TestSize.Level1)
287 {
288 std::string abilityName = "MusicAbility";
289 std::string bundleName = "com.ix.hiMusic";
290 EXPECT_CALL(*mockAppMgrClient_, LoadAbility(_, _, _, _, _)).Times(2);
291 Want want = CreateWant(abilityName, bundleName);
292 auto result = abilityMgrServ_->StartAbility(want);
293 EXPECT_EQ(OHOS::ERR_OK, result);
294
295 auto topAbility = abilityMgrServ_->currentMissionListManager_->GetCurrentTopAbilityLocked();
296 EXPECT_TRUE(topAbility);
297 topAbility->SetAbilityState(AbilityState::FOREGROUND);
298
299 std::string abilityName2 = "MusicAbilityOther";
300 std::string bundleName2 = "com.ix.hiMusicOther";
301 want = CreateWant(abilityName2, bundleName2);
302 auto result2 = abilityMgrServ_->StartAbility(want);
303 EXPECT_EQ(OHOS::ERR_OK, result2);
304 EXPECT_CALL(*mockAppMgrClient_, GetRunningProcessInfoByToken(_, _)).Times(2);
305
306 std::vector<AbilityRunningInfo> infos;
307 abilityMgrServ_->GetAbilityRunningInfos(infos);
308 size_t infoCount {2};
309 EXPECT_TRUE(infos.size() == infoCount);
310 if (infos.size() == infoCount) {
311 EXPECT_TRUE(infos[0].ability.GetAbilityName() == abilityName2);
312 EXPECT_TRUE(infos[0].abilityState == static_cast<int>(AbilityState::INITIAL));
313 EXPECT_TRUE(infos[1].ability.GetAbilityName() == abilityName);
314 EXPECT_TRUE(infos[1].abilityState == static_cast<int>(AbilityState::FOREGROUND));
315 }
316 }
317
318 /*
319 * Feature: AbilityManagerService
320 * Function: GetAbilityRunningInfos
321 * SubFunction: NA
322 * FunctionPoints:query ability running infos
323 * EnvConditions: NA
324 * CaseDescription: start two service abilities, mock object, call query function.
325 */
326 HWTEST_F(RunningInfosModuleTest, GetAbilityRunningInfos_005, TestSize.Level1)
327 {
328 std::string abilityName = "EnterAbility";
329 std::string bundleName = "com.ohos.photos";
330 EXPECT_CALL(*mockAppMgrClient_, LoadAbility(_, _, _, _, _)).Times(2);
331 Want want = CreateWant(abilityName, bundleName);
332 auto result = abilityMgrServ_->StartAbility(want);
333 EXPECT_EQ(OHOS::ERR_OK, result);
334
335 std::string abilityName2 = "EnterAbility";
336 std::string bundleName2 = "com.ohos.camera";
337 want = CreateWant(abilityName2, bundleName2);
338 auto result2 = abilityMgrServ_->StartAbility(want);
339 EXPECT_EQ(OHOS::ERR_OK, result2);
340 EXPECT_CALL(*mockAppMgrClient_, GetRunningProcessInfoByToken(_, _)).Times(2);
341
342 std::vector<AbilityRunningInfo> infos;
343 abilityMgrServ_->GetAbilityRunningInfos(infos);
344 size_t infoCount {2};
345 EXPECT_TRUE(infos.size() == infoCount);
346 if (infos.size() == infoCount) {
347 EXPECT_TRUE(infos[0].ability.GetAbilityName() == abilityName);
348 EXPECT_TRUE(infos[0].abilityState == static_cast<int>(AbilityState::INITIAL));
349 EXPECT_TRUE(infos[1].ability.GetAbilityName() == abilityName2);
350 EXPECT_TRUE(infos[1].abilityState == static_cast<int>(AbilityState::INITIAL));
351 }
352 }
353
354 /*
355 * Feature: AbilityManagerService
356 * Function: GetAbilityRunningInfos
357 * SubFunction: NA
358 * FunctionPoints:query ability running infos
359 * EnvConditions: NA
360 * CaseDescription: start two launcher abilities, mock object, call query function.
361 */
362 HWTEST_F(RunningInfosModuleTest, GetAbilityRunningInfos_006, TestSize.Level1)
363 {
364 std::string abilityName = "com.ohos.launcher.MainAbility";
365 std::string bundleName = "com.ohos.launcher";
366 EXPECT_CALL(*mockAppMgrClient_, LoadAbility(_, _, _, _, _)).Times(2);
367 Want want = CreateWant(abilityName, bundleName);
368 auto result = abilityMgrServ_->StartAbility(want);
369 EXPECT_EQ(OHOS::ERR_OK, result);
370
371 auto topAbility = abilityMgrServ_->currentMissionListManager_->GetCurrentTopAbilityLocked();
372 EXPECT_TRUE(topAbility);
373 topAbility->SetAbilityState(AbilityState::FOREGROUND);
374
375 std::string abilityName2 = "com.ohos.launcher.MainAbilityOther";
376 std::string bundleName2 = "com.ohos.launcherOther";
377 want = CreateWant(abilityName2, bundleName2);
378 auto result2 = abilityMgrServ_->StartAbility(want);
379 EXPECT_EQ(OHOS::ERR_OK, result2);
380 EXPECT_CALL(*mockAppMgrClient_, GetRunningProcessInfoByToken(_, _)).Times(2);
381
382 std::vector<AbilityRunningInfo> infos;
383 abilityMgrServ_->GetAbilityRunningInfos(infos);
384 size_t infoCount {2};
385 EXPECT_TRUE(infos.size() == infoCount);
386 if (infos.size() == infoCount) {
387 EXPECT_TRUE(infos[0].ability.GetAbilityName() == abilityName2);
388 EXPECT_TRUE(infos[0].abilityState == static_cast<int>(AbilityState::INITIAL));
389 EXPECT_TRUE(infos[1].ability.GetAbilityName() == abilityName);
390 EXPECT_TRUE(infos[1].abilityState == static_cast<int>(AbilityState::FOREGROUND));
391 }
392 }
393
394 /*
395 * Feature: AbilityManagerService
396 * Function: GetExtensionRunningInfos
397 * SubFunction: NA
398 * FunctionPoints:query extension running infos
399 * EnvConditions: NA
400 * CaseDescription: start service ability, mock object, call query function.
401 */
402 HWTEST_F(RunningInfosModuleTest, GetExtensionRunningInfos_001, TestSize.Level1)
403 {
404 std::string abilityName = "hiExtension";
405 std::string bundleName = "com.ix.hiExtension";
406 EXPECT_CALL(*mockAppMgrClient_, LoadAbility(_, _, _, _, _)).Times(1);
407 Want want = CreateWant(abilityName, bundleName);
408 auto result = abilityMgrServ_->StartAbility(want);
409 EXPECT_EQ(OHOS::ERR_OK, result);
410 EXPECT_CALL(*mockAppMgrClient_, GetRunningProcessInfoByToken(_, _)).Times(1);
411
412 std::vector<ExtensionRunningInfo> infos;
413 int upperLimit = 10;
414 abilityMgrServ_->GetExtensionRunningInfos(upperLimit, infos);
415 size_t infoCount {1};
416 EXPECT_TRUE(infos.size() == infoCount);
417 if (infos.size() == infoCount) {
418 EXPECT_TRUE(infos[0].extension.GetAbilityName() == abilityName);
419 }
420 }
421
422 /*
423 * Feature: AbilityManagerService
424 * Function: GetExtensionRunningInfos
425 * SubFunction: NA
426 * FunctionPoints:query extension running infos
427 * EnvConditions: NA
428 * CaseDescription: start service abilities, mock object, call query function.
429 */
430 HWTEST_F(RunningInfosModuleTest, GetExtensionRunningInfos_002, TestSize.Level1)
431 {
432 std::string abilityName = "hiExtension";
433 std::string bundleName = "com.ix.hiExtension";
434 EXPECT_CALL(*mockAppMgrClient_, LoadAbility(_, _, _, _, _)).Times(2);
435 Want want = CreateWant(abilityName, bundleName);
436 auto result = abilityMgrServ_->StartAbility(want);
437 EXPECT_EQ(OHOS::ERR_OK, result);
438
439 std::string abilityName2 = "hiExtensionOther";
440 std::string bundleName2 = "com.ix.hiExtension";
441 want = CreateWant(abilityName2, bundleName2);
442 auto result2 = abilityMgrServ_->StartAbility(want);
443 EXPECT_EQ(OHOS::ERR_OK, result2);
444 EXPECT_CALL(*mockAppMgrClient_, GetRunningProcessInfoByToken(_, _)).Times(2);
445
446 std::vector<ExtensionRunningInfo> infos;
447 int upperLimit = 10;
448 abilityMgrServ_->GetExtensionRunningInfos(upperLimit, infos);
449 size_t infoCount {2};
450 if (infos.size() == infoCount) {
451 EXPECT_TRUE(infos[0].extension.GetAbilityName() == abilityName);
452 EXPECT_TRUE(infos[1].extension.GetAbilityName() == abilityName2);
453 }
454 }
455
456 /*
457 * Feature: AbilityManagerService
458 * Function: GetProcessRunningInfos
459 * SubFunction: NA
460 * FunctionPoints:query process running infos
461 * EnvConditions: NA
462 * CaseDescription: start service ability, mock object, call query function.
463 */
464 HWTEST_F(RunningInfosModuleTest, GetProcessRunningInfos_001, TestSize.Level1)
465 {
466 std::string abilityName = "hiExtension";
467 std::string bundleName = "com.ix.hiExtension";
468 EXPECT_CALL(*mockAppMgrClient_, LoadAbility(_, _, _, _, _)).Times(1);
469 Want want = CreateWant(abilityName, bundleName);
470 auto result = abilityMgrServ_->StartAbility(want);
471 EXPECT_EQ(OHOS::ERR_OK, result);
472 EXPECT_CALL(*mockAppMgrClient_, GetAllRunningProcesses(_)).Times(1);
473
474 std::vector<RunningProcessInfo> infos;
475 auto ret = abilityMgrServ_->GetProcessRunningInfos(infos);
476 EXPECT_EQ(OHOS::ERR_OK, ret);
477 }
478 } // namespace AAFwk
479 } // namespace OHOS
480