1 /*
2 * Copyright (c) 2021-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 #define private public
17 #include "app_mgr_client.h"
18 #undef private
19 #include <gtest/gtest.h>
20 #include "ability_info.h"
21 #include "application_info.h"
22 #include "hilog_tag_wrapper.h"
23 #include "iapp_state_callback.h"
24 #include "mock_ability_token.h"
25 #include "mock_ams_mgr_scheduler.h"
26 #include "mock_app_mgr_service.h"
27 #include "mock_app_service_mgr.h"
28 #include "mock_iapp_state_callback.h"
29 #include "mock_native_token.h"
30 #include "param.h"
31 #include "running_process_info.h"
32
33 using namespace testing::ext;
34 using testing::_;
35 using testing::Invoke;
36 using testing::InvokeWithoutArgs;
37 using testing::Return;
38 using testing::SetArgReferee;
39
40 namespace OHOS {
41 namespace AppExecFwk {
42 namespace {
43 const int32_t USER_ID = 100;
44 } // namespace
45
46 class AmsAppMgrClientTest : public testing::Test {
47 public:
48 static void SetUpTestCase();
49 static void TearDownTestCase();
50 void SetUp();
51 void TearDown();
52
53 protected:
54 sptr<MockAbilityToken> token_;
55 sptr<MockAbilityToken> preToken_;
56 std::unique_ptr<AppMgrClient> client_;
57 };
58
SetUpTestCase()59 void AmsAppMgrClientTest::SetUpTestCase()
60 {
61 MockNativeToken::SetNativeToken();
62 }
63
TearDownTestCase()64 void AmsAppMgrClientTest::TearDownTestCase()
65 {}
66
SetUp()67 void AmsAppMgrClientTest::SetUp()
68 {
69 client_.reset(new (std::nothrow) AppMgrClient());
70 client_->SetServiceManager(std::make_unique<MockAppServiceMgr>());
71 token_ = new (std::nothrow) MockAbilityToken();
72 }
73
TearDown()74 void AmsAppMgrClientTest::TearDown()
75 {}
76
77 /**
78 * @tc.name: AppMgrClient_GetProcessRunningInfosByUserId_0100
79 * @tc.desc: GetProcessRunningInfosByUserId
80 * @tc.type: FUNC
81 * @tc.require: SR000GH1GO
82 */
83 HWTEST_F(AmsAppMgrClientTest, AppMgrClient_GetProcessRunningInfosByUserId_0100, TestSize.Level1)
84 {
85 TAG_LOGI(AAFwkTag::TEST, "AppMgrClient_GetProcessRunningInfosByUserId_0100 start");
86
87 EXPECT_EQ(AppMgrResultCode::RESULT_OK, client_->ConnectAppMgrService());
88
89 EXPECT_CALL(
90 *(static_cast<MockAppMgrService*>((iface_cast<IAppMgr>(client_->GetRemoteObject())).GetRefPtr())),
91 GetProcessRunningInfosByUserId(_, _))
92 .Times(1)
93 .WillOnce(Return(AppMgrResultCode::RESULT_OK));
94
95 std::vector<RunningProcessInfo> info;
96 int32_t userId = USER_ID;
97 auto result = client_->GetProcessRunningInfosByUserId(info, userId);
98 TAG_LOGI(AAFwkTag::TEST, "result = %{public}d", result);
99
100 EXPECT_EQ(result, AppMgrResultCode::RESULT_OK);
101
102 TAG_LOGI(AAFwkTag::TEST, "AppMgrClient_GetProcessRunningInfosByUserId_0100 end");
103 }
104
105 /*
106 * Feature: AppMgrService
107 * Function: AppMgrClient
108 * SubFunction: LoadAbility Function
109 * FunctionPoints: AppMgrClient LoadAbility interface
110 * EnvConditions: Mobile that can run ohos test framework
111 * CaseDescription: Verify if AppMgrClient invoke LoadAbility works.
112 */
113 HWTEST_F(AmsAppMgrClientTest, AppMgrClient_001, TestSize.Level1)
114 {
115 TAG_LOGI(AAFwkTag::TEST, "ams_app_mgr_client_test_001 start");
116 EXPECT_EQ(AppMgrResultCode::RESULT_OK, client_->ConnectAppMgrService());
117
118 AbilityInfo abilityInfo;
119 ApplicationInfo appInfo;
120 Want want;
121
122 sptr<IAmsMgr> amsMgrScheduler(new MockAmsMgrScheduler());
123
124 EXPECT_CALL(*(static_cast<MockAmsMgrScheduler*>(amsMgrScheduler.GetRefPtr())),
125 LoadAbility(_, _, _, _)).Times(1);
126
127 EXPECT_CALL(*(static_cast<MockAppMgrService*>((iface_cast<IAppMgr>(client_->GetRemoteObject())).GetRefPtr())),
128 GetAmsMgr())
129 .Times(1)
130 .WillOnce(Return(amsMgrScheduler));
131 AbilityRuntime::LoadParam loadParam;
132 loadParam.token = token_;
133 loadParam.preToken = preToken_;
134 EXPECT_EQ(AppMgrResultCode::RESULT_OK, client_->LoadAbility(abilityInfo, appInfo, want, loadParam));
135 TAG_LOGI(AAFwkTag::TEST, "ams_app_mgr_client_test_001 end");
136 }
137
138 /*
139 * Feature: AppMgrService
140 * Function: AppMgrClient
141 * SubFunction: LoadAbility Function
142 * FunctionPoints: AppMgrClient LoadAbility interface
143 * EnvConditions: Mobile that can run ohos test framework
144 * CaseDescription: Verify if AppMgrClient invoke LoadAbility act normal without connect to service.
145 */
146 HWTEST_F(AmsAppMgrClientTest, AppMgrClient_002, TestSize.Level1)
147 {
148 TAG_LOGI(AAFwkTag::TEST, "ams_app_mgr_client_test_002 start");
149 AbilityInfo abilityInfo;
150 ApplicationInfo appInfo;
151 Want want;
152 AbilityRuntime::LoadParam loadParam;
153 loadParam.token = token_;
154 loadParam.preToken = preToken_;
155 EXPECT_EQ(
156 AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED, client_->LoadAbility(abilityInfo, appInfo, want, loadParam));
157 TAG_LOGI(AAFwkTag::TEST, "ams_app_mgr_client_test_002 end");
158 }
159
160 /*
161 * Feature: AppMgrService
162 * Function: AppMgrClient
163 * SubFunction: TerminateAbility Function
164 * FunctionPoints: AppMgrClient TerminateAbility interface
165 * EnvConditions: Mobile that can run ohos test framework
166 * CaseDescription: Verify if AppMgrClient invoke TerminateAbility works.
167 */
168 HWTEST_F(AmsAppMgrClientTest, AppMgrClient_003, TestSize.Level1)
169 {
170 TAG_LOGI(AAFwkTag::TEST, "ams_app_mgr_client_test_003 start");
171 EXPECT_EQ(AppMgrResultCode::RESULT_OK, client_->ConnectAppMgrService());
172
173 sptr<IAmsMgr> amsMgrScheduler(new MockAmsMgrScheduler());
174 EXPECT_CALL(*(static_cast<MockAppMgrService*>((iface_cast<IAppMgr>(client_->GetRemoteObject())).GetRefPtr())),
175 GetAmsMgr())
176 .Times(1)
177 .WillOnce(Return(amsMgrScheduler));
178 EXPECT_CALL(*(static_cast<MockAmsMgrScheduler*>(amsMgrScheduler.GetRefPtr())), TerminateAbility(_, _)).Times(1);
179
180 EXPECT_EQ(AppMgrResultCode::RESULT_OK, client_->TerminateAbility(token_, false));
181 TAG_LOGI(AAFwkTag::TEST, "ams_app_mgr_client_test_003 end");
182 }
183
184 /*
185 * Feature: AppMgrService
186 * Function: AppMgrClient
187 * SubFunction: TerminateAbility Function
188 * FunctionPoints: AppMgrClient TerminateAbility interface
189 * EnvConditions: Mobile that can run ohos test framework
190 * CaseDescription: Verify if AppMgrClient invoke TerminateAbility act normal without connect to service.
191 */
192 HWTEST_F(AmsAppMgrClientTest, AppMgrClient_004, TestSize.Level1)
193 {
194 TAG_LOGI(AAFwkTag::TEST, "ams_app_mgr_client_test_004 start");
195 EXPECT_EQ(AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED, client_->TerminateAbility(token_, false));
196 TAG_LOGI(AAFwkTag::TEST, "ams_app_mgr_client_test_004 end");
197 }
198
199 /*
200 * Feature: AppMgrService
201 * Function: AppMgrClient
202 * SubFunction: UpdateAbilityState Function
203 * FunctionPoints: AppMgrClient UpdateAbilityState interface
204 * EnvConditions: Mobile that can run ohos test framework
205 * CaseDescription: Verify if AppMgrClient invoke UpdateAbilityState works.
206 */
207 HWTEST_F(AmsAppMgrClientTest, AppMgrClient_005, TestSize.Level1)
208 {
209 TAG_LOGI(AAFwkTag::TEST, "ams_app_mgr_client_test_005 start");
210 EXPECT_EQ(AppMgrResultCode::RESULT_OK, client_->ConnectAppMgrService());
211
212 sptr<IAmsMgr> amsMgrScheduler(new MockAmsMgrScheduler());
213 EXPECT_CALL(*(static_cast<MockAmsMgrScheduler*>(amsMgrScheduler.GetRefPtr())), UpdateAbilityState(_, _)).Times(1);
214 EXPECT_CALL(*(static_cast<MockAppMgrService*>((iface_cast<IAppMgr>(client_->GetRemoteObject())).GetRefPtr())),
215 GetAmsMgr())
216 .Times(1)
217 .WillOnce(Return(amsMgrScheduler));
218
219 AbilityState state = AbilityState::ABILITY_STATE_CREATE;
220 EXPECT_EQ(AppMgrResultCode::RESULT_OK, client_->UpdateAbilityState(token_, state));
221 TAG_LOGI(AAFwkTag::TEST, "ams_app_mgr_client_test_005 end");
222 }
223
224 /*
225 * Feature: AppMgrService
226 * Function: AppMgrClient
227 * SubFunction: UpdateAbilityState Function
228 * FunctionPoints: AppMgrClient UpdateAbilityState interface
229 * EnvConditions: Mobile that can run ohos test framework
230 * CaseDescription: Verify if AppMgrClient invoke UpdateAbilityState act normal without connect to service.
231 */
232 HWTEST_F(AmsAppMgrClientTest, AppMgrClient_006, TestSize.Level1)
233 {
234 TAG_LOGI(AAFwkTag::TEST, "ams_app_mgr_client_test_006 start");
235 AbilityState state = AbilityState::ABILITY_STATE_CREATE;
236 EXPECT_EQ(AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED, client_->UpdateAbilityState(token_, state));
237 TAG_LOGI(AAFwkTag::TEST, "ams_app_mgr_client_test_006 end");
238 }
239
240 /*
241 * Feature: AppMgrService
242 * Function: AppMgrClient
243 * SubFunction: RegisterAppStateCallback Function
244 * FunctionPoints: AppMgrClient RegisterAppStateCallback interface
245 * EnvConditions: Mobile that can run ohos test framework
246 * CaseDescription: Verify if AppMgrClient invoke RegisterAppStateCallback works.
247 */
248 HWTEST_F(AmsAppMgrClientTest, AppMgrClient_007, TestSize.Level1)
249 {
250 TAG_LOGI(AAFwkTag::TEST, "ams_app_mgr_client_test_007 start");
251 EXPECT_EQ(AppMgrResultCode::RESULT_OK, client_->ConnectAppMgrService());
252 sptr<MockAppStateCallback> mockCallback(new MockAppStateCallback());
253 sptr<IAppStateCallback> callback = iface_cast<IAppStateCallback>(mockCallback);
254
255 sptr<IAmsMgr> amsMgrScheduler(new MockAmsMgrScheduler());
256 EXPECT_CALL(*(static_cast<MockAppMgrService*>((iface_cast<IAppMgr>(client_->GetRemoteObject())).GetRefPtr())),
257 GetAmsMgr())
258 .Times(1)
259 .WillOnce(Return(amsMgrScheduler));
260
261 EXPECT_CALL(*mockCallback, OnAbilityRequestDone(_, _)).Times(1);
262 EXPECT_CALL(*mockCallback, OnAppStateChanged(_)).Times(1);
263
264 EXPECT_EQ(AppMgrResultCode::RESULT_OK, client_->RegisterAppStateCallback(callback));
265 TAG_LOGI(AAFwkTag::TEST, "ams_app_mgr_client_test_007 end");
266 }
267
268 /*
269 * Feature: AppMgrService
270 * Function: AppMgrClient
271 * SubFunction: RegisterAppStateCallback Function
272 * FunctionPoints: AppMgrClient RegisterAppStateCallback interface
273 * EnvConditions: Mobile that can run ohos test framework
274 * CaseDescription: Verify if AppMgrClient invoke RegisterAppStateCallback act normal without connect to service.
275 */
276 HWTEST_F(AmsAppMgrClientTest, AppMgrClient_008, TestSize.Level1)
277 {
278 TAG_LOGI(AAFwkTag::TEST, "ams_app_mgr_client_test_008 start");
279 sptr<IAppStateCallback> callback;
280 EXPECT_EQ(AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED, client_->RegisterAppStateCallback(callback));
281 TAG_LOGI(AAFwkTag::TEST, "ams_app_mgr_client_test_008 end");
282 }
283
284 /*
285 * Feature: AppMgrService
286 * Function: AppMgrClient::KillApplication
287 * SubFunction: RegisterAppStateCallback Function
288 * FunctionPoints: AppMgrClient KillApplication interface
289 * EnvConditions: Mobile that can run ohos test framework
290 * CaseDescription: Notify app of death
291 */
292 HWTEST_F(AmsAppMgrClientTest, AppMgrClient_010, TestSize.Level1)
293 {
294 TAG_LOGI(AAFwkTag::TEST, "ams_app_mgr_client_test_008 start");
295 EXPECT_EQ(AppMgrResultCode::RESULT_OK, client_->ConnectAppMgrService());
296 sptr<IRemoteObject> token;
297 sptr<IRemoteObject> preToken;
298
299 sptr<IAmsMgr> amsMgrScheduler(new MockAmsMgrScheduler());
300 EXPECT_CALL(*(static_cast<MockAmsMgrScheduler*>(amsMgrScheduler.GetRefPtr())), KillApplication(_, _, _))
301 .Times(1)
302 .WillOnce(Return(ERR_OK));
303 EXPECT_CALL(*(static_cast<MockAppMgrService*>((iface_cast<IAppMgr>(client_->GetRemoteObject())).GetRefPtr())),
304 GetAmsMgr())
305 .Times(1)
306 .WillOnce(Return(amsMgrScheduler));
307
308 EXPECT_EQ(AppMgrResultCode::RESULT_OK, client_->KillApplication(""));
309 TAG_LOGI(AAFwkTag::TEST, "ams_app_mgr_client_test_008 end");
310 }
311
312 /*
313 * Feature: AppMgrService
314 * Function: AppMgrClient::KillApplication
315 * SubFunction: RegisterAppStateCallback Function
316 * FunctionPoints: AppMgrClient KillApplication interface
317 * EnvConditions: Mobile that can run ohos test framework
318 * CaseDescription: Notify app of death
319 */
320 HWTEST_F(AmsAppMgrClientTest, AppMgrClient_011, TestSize.Level1)
321 {
322 TAG_LOGI(AAFwkTag::TEST, "ams_app_mgr_client_test_011 start");
323 sptr<IRemoteObject> token;
324 sptr<IRemoteObject> preToken;
325 EXPECT_EQ(AppMgrResultCode::RESULT_OK, client_->ConnectAppMgrService());
326 sptr<IAmsMgr> amsMgrScheduler(new MockAmsMgrScheduler());
327 EXPECT_CALL(*(static_cast<MockAmsMgrScheduler*>(amsMgrScheduler.GetRefPtr())), KillApplication(_, _, _))
328 .Times(1)
329 .WillOnce(Return(ERR_NO_MEMORY));
330 EXPECT_CALL(*(static_cast<MockAppMgrService*>((iface_cast<IAppMgr>(client_->GetRemoteObject())).GetRefPtr())),
331 GetAmsMgr())
332 .Times(1)
333 .WillOnce(Return(amsMgrScheduler));
334
335 EXPECT_EQ(AppMgrResultCode::ERROR_SERVICE_NOT_READY, client_->KillApplication(""));
336 TAG_LOGI(AAFwkTag::TEST, "ams_app_mgr_client_test_011 end");
337 }
338
339 /*
340 * Feature: AppMgrService
341 * Function: AppMgrClient::KillProcessByAbilityToken
342 * SubFunction: RegisterAppStateCallback Function
343 * FunctionPoints: AppMgrClient KillApplication interface
344 * EnvConditions: Mobile that can run ohos test framework
345 * CaseDescription: Notify app of death
346 */
347 HWTEST_F(AmsAppMgrClientTest, AppMgrClient_012, TestSize.Level1)
348 {
349 TAG_LOGI(AAFwkTag::TEST, "ams_app_mgr_client_test_012 start");
350 sptr<IRemoteObject> token;
351 EXPECT_EQ(AppMgrResultCode::RESULT_OK, client_->ConnectAppMgrService());
352 sptr<IAmsMgr> amsMgrScheduler(new MockAmsMgrScheduler());
353 EXPECT_CALL(*(static_cast<MockAmsMgrScheduler*>(amsMgrScheduler.GetRefPtr())), KillProcessByAbilityToken(_))
354 .Times(1);
355
356 EXPECT_CALL(*(static_cast<MockAppMgrService*>((iface_cast<IAppMgr>(client_->GetRemoteObject())).GetRefPtr())),
357 GetAmsMgr())
358 .Times(1)
359 .WillOnce(Return(amsMgrScheduler));
360
361 EXPECT_EQ(AppMgrResultCode::RESULT_OK, client_->KillProcessByAbilityToken(token));
362 TAG_LOGI(AAFwkTag::TEST, "ams_app_mgr_client_test_012 end");
363 }
364
365 /*
366 * Feature: AppMgrService
367 * Function: AppMgrClient::KillProcessByAbilityToken
368 * SubFunction: RegisterAppStateCallback Function
369 * FunctionPoints: AppMgrClient KillApplication interface
370 * EnvConditions: Mobile that can run ohos test framework
371 * CaseDescription: Notify app of death
372 */
373 HWTEST_F(AmsAppMgrClientTest, AppMgrClient_013, TestSize.Level1)
374 {
375 TAG_LOGI(AAFwkTag::TEST, "ams_app_mgr_client_test_013 start");
376 sptr<IRemoteObject> token;
377 EXPECT_EQ(AppMgrResultCode::RESULT_OK, client_->ConnectAppMgrService());
378 sptr<IAmsMgr> amsMgrScheduler(new MockAmsMgrScheduler());
379 EXPECT_CALL(*(static_cast<MockAmsMgrScheduler*>(amsMgrScheduler.GetRefPtr())), KillProcessByAbilityToken(_))
380 .Times(0);
381
382 EXPECT_CALL(*(static_cast<MockAppMgrService*>((iface_cast<IAppMgr>(client_->GetRemoteObject())).GetRefPtr())),
383 GetAmsMgr())
384 .Times(1)
385 .WillOnce(Return(nullptr));
386
387 EXPECT_EQ(AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED, client_->KillProcessByAbilityToken(token));
388 TAG_LOGI(AAFwkTag::TEST, "ams_app_mgr_client_test_013 end");
389 }
390
391 /*
392 * Feature: AppMgrService
393 * Function: AppMgrClient::ClearUpApplicationData
394 * SubFunction: ClearUpApplicationData
395 * FunctionPoints: AppMgrClient ClearUpApplicationData interface
396 * EnvConditions: Mobile that can run ohos test framework
397 * CaseDescription: clear the application data.
398 */
399 HWTEST_F(AmsAppMgrClientTest, AppMgrClient_014, TestSize.Level1)
400 {
401 sptr<IRemoteObject> token;
402 EXPECT_EQ(AppMgrResultCode::RESULT_OK, client_->ConnectAppMgrService());
403 EXPECT_CALL(*(static_cast<MockAppMgrService*>((iface_cast<IAppMgr>(client_->GetRemoteObject())).GetRefPtr())),
404 ClearUpApplicationData(_, _, _))
405 .Times(1)
406 .WillOnce(Return(ERR_NO_MEMORY));
407 EXPECT_EQ(AppMgrResultCode::ERROR_SERVICE_NOT_READY, client_->ClearUpApplicationData("com.test", 0));
408 }
409
410 /*
411 * Feature: AppMgrService
412 * Function: AppMgrClient::ClearUpApplicationData
413 * SubFunction: ClearUpApplicationData
414 * FunctionPoints: AppMgrClient ClearUpApplicationData interface
415 * EnvConditions: Mobile that can run ohos test framework
416 * CaseDescription: clear the application data.
417 */
418 HWTEST_F(AmsAppMgrClientTest, AppMgrClient_015, TestSize.Level1)
419 {
420 sptr<IRemoteObject> token;
421 EXPECT_EQ(AppMgrResultCode::RESULT_OK, client_->ConnectAppMgrService());
422 sptr<IAppMgr> appMgr(new MockAppMgrService());
423 EXPECT_CALL(*(static_cast<MockAppMgrService*>((iface_cast<IAppMgr>(client_->GetRemoteObject())).GetRefPtr())),
424 ClearUpApplicationData(_, _, _))
425 .Times(1)
426 .WillOnce(Return(ERR_OK));
427 EXPECT_EQ(AppMgrResultCode::RESULT_OK, client_->ClearUpApplicationData("com.test", 0));
428 }
429
430 /**
431 * @tc.name: AppMgrClient_016
432 * @tc.desc: Verify the function when parameter is 2
433 * @tc.type: FUNC
434 * @tc.require: issueI5823X
435 */
436 HWTEST_F(AmsAppMgrClientTest, AppMgrClient_016, TestSize.Level1)
437 {
438 TAG_LOGI(AAFwkTag::TEST, "ams_app_mgr_client_test_016 start");
439 AppMgrClient* ret = new AppMgrClient();
440 auto ans = ret->NotifyMemoryLevel(MemoryLevel::MEMORY_LEVEL_CRITICAL);
441 EXPECT_EQ(ans, ERR_INVALID_VALUE);
442 TAG_LOGI(AAFwkTag::TEST, "ams_app_mgr_client_test_016 end");
443 }
444
445 /**
446 * @tc.name: AppMgrClient_017
447 * @tc.desc: Verify the function when parameter is 1
448 * @tc.type: FUNC
449 * @tc.require: issueI5823X
450 */
451 HWTEST_F(AmsAppMgrClientTest, AppMgrClient_017, TestSize.Level1)
452 {
453 TAG_LOGI(AAFwkTag::TEST, "ams_app_mgr_client_test_017 start");
454 AppMgrClient* ret = new AppMgrClient();
455 auto ans = ret->NotifyMemoryLevel(MemoryLevel::MEMORY_LEVEL_LOW);
456 EXPECT_EQ(ans, ERR_INVALID_VALUE);
457 TAG_LOGI(AAFwkTag::TEST, "ams_app_mgr_client_test_017 end");
458 }
459
460 /**
461 * @tc.name: AppMgrClient_018
462 * @tc.desc: Verify the function when parameter is 0
463 * @tc.type: FUNC
464 * @tc.require: issueI5823X
465 */
466 HWTEST_F(AmsAppMgrClientTest, AppMgrClient_018, TestSize.Level1)
467 {
468 TAG_LOGI(AAFwkTag::TEST, "ams_app_mgr_client_test_018 start");
469 AppMgrClient* ret = new AppMgrClient();
470 auto ans = ret->NotifyMemoryLevel(MemoryLevel::MEMORY_LEVEL_MODERATE);
471 EXPECT_EQ(ans, ERR_INVALID_VALUE);
472 TAG_LOGI(AAFwkTag::TEST, "ams_app_mgr_client_test_018 end");
473 }
474
475 /**
476 * @tc.name: AppMgrClient_019
477 * @tc.desc: Verify the function when parameter is 2
478 * @tc.type: FUNC
479 * @tc.require: issueI581UZ
480 */
481 HWTEST_F(AmsAppMgrClientTest, AppMgrClient_019, TestSize.Level1)
482 {
483 TAG_LOGI(AAFwkTag::TEST, "ams_app_mgr_client_test_019 start");
484 AppMgrClient* ret = new AppMgrClient();
485 auto ans = ret->NotifyMemoryLevel(MemoryLevel::MEMORY_LEVEL_CRITICAL);
486 EXPECT_EQ(ans, ERR_INVALID_VALUE);
487 TAG_LOGI(AAFwkTag::TEST, "ams_app_mgr_client_test_019 end");
488 }
489
490 /**
491 * @tc.name: AppMgrClient_020
492 * @tc.desc: Verify the function when parameter is 1
493 * @tc.type: FUNC
494 * @tc.require: issueI581UZ
495 */
496 HWTEST_F(AmsAppMgrClientTest, AppMgrClient_020, TestSize.Level1)
497 {
498 TAG_LOGI(AAFwkTag::TEST, "ams_app_mgr_client_test_020 start");
499 AppMgrClient* ret = new AppMgrClient();
500 auto ans = ret->NotifyMemoryLevel(MemoryLevel::MEMORY_LEVEL_LOW);
501 EXPECT_EQ(ans, ERR_INVALID_VALUE);
502 TAG_LOGI(AAFwkTag::TEST, "ams_app_mgr_client_test_020 end");
503 }
504
505 /**
506 * @tc.name: AppMgrClient_021
507 * @tc.desc: Verify the function when parameter is 0
508 * @tc.type: FUNC
509 * @tc.require: issueI581UZ
510 */
511 HWTEST_F(AmsAppMgrClientTest, AppMgrClient_021, TestSize.Level1)
512 {
513 TAG_LOGI(AAFwkTag::TEST, "ams_app_mgr_client_test_021 start");
514 AppMgrClient* ret = new AppMgrClient();
515 auto ans = ret->NotifyMemoryLevel(MemoryLevel::MEMORY_LEVEL_MODERATE);
516 EXPECT_EQ(ans, ERR_INVALID_VALUE);
517 TAG_LOGI(AAFwkTag::TEST, "ams_app_mgr_client_test_021 end");
518 }
519
520 /*
521 * Feature: AppMgrService
522 * Function: AppMgrClient::PreloadApplicationByPhase
523 * SubFunction: PreloadApplicationByPhase
524 * FunctionPoints: AppMgrClient PreloadApplicationByPhase interface
525 * EnvConditions: Mobile that can run ohos test framework
526 * CaseDescription: Test PreloadApplicationByPhase when mgrHolder_ is nullptr.
527 */
528 HWTEST_F(AmsAppMgrClientTest, PreloadApplicationByPhase_001, TestSize.Level1)
529 {
530 TAG_LOGI(AAFwkTag::TEST, "PreloadApplicationByPhase_001 start");
531 client_->mgrHolder_ = nullptr;
532
533 std::string bundleName = "com.example.test";
534 int32_t userId = 100;
535 int32_t appIndex = 0;
536 AppExecFwk::PreloadPhase preloadPhase = AppExecFwk::PreloadPhase::PROCESS_CREATED;
537
538 int32_t result = client_->PreloadApplicationByPhase(bundleName, userId, appIndex, preloadPhase);
539 EXPECT_EQ(result, AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED);
540 TAG_LOGI(AAFwkTag::TEST, "PreloadApplicationByPhase_001 end");
541 }
542
543 /*
544 * Feature: AppMgrService
545 * Function: AppMgrClient::PreloadApplicationByPhase
546 * SubFunction: PreloadApplicationByPhase
547 * FunctionPoints: AppMgrClient PreloadApplicationByPhase interface
548 * EnvConditions: Mobile that can run ohos test framework
549 * CaseDescription: Test PreloadApplicationByPhase when service is nullptr.
550 */
551 HWTEST_F(AmsAppMgrClientTest, PreloadApplicationByPhase_002, TestSize.Level1)
552 {
553 TAG_LOGI(AAFwkTag::TEST, "PreloadApplicationByPhase_002 start");
554 client_->SetServiceManager(nullptr);
555
556 std::string bundleName = "com.example.test";
557 int32_t userId = 100;
558 int32_t appIndex = 0;
559 AppExecFwk::PreloadPhase preloadPhase = AppExecFwk::PreloadPhase::PROCESS_CREATED;
560
561 int32_t result = client_->PreloadApplicationByPhase(bundleName, userId, appIndex, preloadPhase);
562 EXPECT_EQ(result, AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED);
563 TAG_LOGI(AAFwkTag::TEST, "PreloadApplicationByPhase_002 end");
564 }
565
566 /*
567 * Feature: AppMgrService
568 * Function: AppMgrClient::PreloadApplicationByPhase
569 * SubFunction: PreloadApplicationByPhase
570 * FunctionPoints: AppMgrClient PreloadApplicationByPhase interface
571 * EnvConditions: Mobile that can run ohos test framework
572 * CaseDescription: Test PreloadApplicationByPhase when amsService is nullptr.
573 */
574 HWTEST_F(AmsAppMgrClientTest, PreloadApplicationByPhase_003, TestSize.Level1)
575 {
576 TAG_LOGI(AAFwkTag::TEST, "PreloadApplicationByPhase_003 start");
577 EXPECT_EQ(AppMgrResultCode::RESULT_OK, client_->ConnectAppMgrService());
578
579 sptr<IAmsMgr> amsMgrScheduler = nullptr;
580 EXPECT_CALL(*(static_cast<MockAppMgrService*>((iface_cast<IAppMgr>(client_->GetRemoteObject())).GetRefPtr())),
581 GetAmsMgr())
582 .Times(1)
583 .WillOnce(Return(amsMgrScheduler));
584
585 std::string bundleName = "com.example.test";
586 int32_t userId = 100;
587 int32_t appIndex = 0;
588 AppExecFwk::PreloadPhase preloadPhase = AppExecFwk::PreloadPhase::PROCESS_CREATED;
589
590 int32_t result = client_->PreloadApplicationByPhase(bundleName, userId, appIndex, preloadPhase);
591 EXPECT_EQ(result, AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED);
592 TAG_LOGI(AAFwkTag::TEST, "PreloadApplicationByPhase_003 end");
593 }
594
595 /*
596 * Feature: AppMgrService
597 * Function: AppMgrClient::PreloadApplicationByPhase
598 * SubFunction: PreloadApplicationByPhase
599 * FunctionPoints: AppMgrClient PreloadApplicationByPhase interface
600 * EnvConditions: Mobile that can run ohos test framework
601 * CaseDescription: Test PreloadApplicationByPhase with valid parameters.
602 */
603 HWTEST_F(AmsAppMgrClientTest, PreloadApplicationByPhase_004, TestSize.Level1)
604 {
605 TAG_LOGI(AAFwkTag::TEST, "PreloadApplicationByPhase_004 start");
606 EXPECT_EQ(AppMgrResultCode::RESULT_OK, client_->ConnectAppMgrService());
607
608 sptr<MockAmsMgrScheduler> mockAmsMgrScheduler(new MockAmsMgrScheduler());
609 EXPECT_CALL(*(static_cast<MockAppMgrService*>((iface_cast<IAppMgr>(client_->GetRemoteObject())).GetRefPtr())),
610 GetAmsMgr())
611 .Times(1)
612 .WillOnce(Return(mockAmsMgrScheduler));
613
614 EXPECT_CALL(*mockAmsMgrScheduler, PreloadApplicationByPhase(_, _, _, _))
615 .Times(1)
616 .WillOnce(Return(AppMgrResultCode::RESULT_OK));
617
618 std::string bundleName = "com.example.test";
619 int32_t userId = 100;
620 int32_t appIndex = 0;
621 AppExecFwk::PreloadPhase preloadPhase = AppExecFwk::PreloadPhase::PROCESS_CREATED;
622
623 int32_t result = client_->PreloadApplicationByPhase(bundleName, userId, appIndex, preloadPhase);
624 EXPECT_EQ(result, AppMgrResultCode::RESULT_OK);
625 TAG_LOGI(AAFwkTag::TEST, "PreloadApplicationByPhase_004 end");
626 }
627
628 /*
629 * Feature: AppMgrService
630 * Function: AppMgrClient::NotifyPreloadAbilityStateChanged
631 * SubFunction: NotifyPreloadAbilityStateChanged
632 * FunctionPoints: AppMgrClient NotifyPreloadAbilityStateChanged interface
633 * EnvConditions: Mobile that can run ohos test framework
634 * CaseDescription: Test NotifyPreloadAbilityStateChanged with valid parameters.
635 */
636 HWTEST_F(AmsAppMgrClientTest, NotifyPreloadAbilityStateChanged_001, TestSize.Level1)
637 {
638 TAG_LOGI(AAFwkTag::TEST, "NotifyPreloadAbilityStateChanged_001 start");
639 client_->mgrHolder_ = nullptr;
640
641 int32_t result = client_->NotifyPreloadAbilityStateChanged(token_, true);
642 EXPECT_EQ(result, AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED);
643 TAG_LOGI(AAFwkTag::TEST, "NotifyPreloadAbilityStateChanged_001 end");
644 }
645
646 /*
647 * Feature: AppMgrService
648 * Function: AppMgrClient::NotifyPreloadAbilityStateChanged
649 * SubFunction: NotifyPreloadAbilityStateChanged
650 * FunctionPoints: AppMgrClient NotifyPreloadAbilityStateChanged interface
651 * EnvConditions: Mobile that can run ohos test framework
652 * CaseDescription: Test NotifyPreloadAbilityStateChanged with valid parameters.
653 */
654 HWTEST_F(AmsAppMgrClientTest, NotifyPreloadAbilityStateChanged_002, TestSize.Level1)
655 {
656 TAG_LOGI(AAFwkTag::TEST, "NotifyPreloadAbilityStateChanged_002 start");
657 client_->SetServiceManager(nullptr);
658
659 int32_t result = client_->NotifyPreloadAbilityStateChanged(token_, true);
660 EXPECT_EQ(result, AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED);
661 TAG_LOGI(AAFwkTag::TEST, "NotifyPreloadAbilityStateChanged_002 end");
662 }
663
664 /*
665 * Feature: AppMgrService
666 * Function: AppMgrClient::NotifyPreloadAbilityStateChanged
667 * SubFunction: NotifyPreloadAbilityStateChanged
668 * FunctionPoints: AppMgrClient NotifyPreloadAbilityStateChanged interface
669 * EnvConditions: Mobile that can run ohos test framework
670 * CaseDescription: Test NotifyPreloadAbilityStateChanged with valid parameters.
671 */
672 HWTEST_F(AmsAppMgrClientTest, NotifyPreloadAbilityStateChanged_003, TestSize.Level1)
673 {
674 TAG_LOGI(AAFwkTag::TEST, "NotifyPreloadAbilityStateChanged_003 start");
675 EXPECT_EQ(AppMgrResultCode::RESULT_OK, client_->ConnectAppMgrService());
676
677 sptr<IAmsMgr> amsMgrScheduler = nullptr;
678 EXPECT_CALL(*(static_cast<MockAppMgrService*>((iface_cast<IAppMgr>(client_->GetRemoteObject())).GetRefPtr())),
679 GetAmsMgr())
680 .Times(1)
681 .WillOnce(Return(amsMgrScheduler));
682
683 int32_t result = client_->NotifyPreloadAbilityStateChanged(token_, true);
684 EXPECT_EQ(result, AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED);
685 TAG_LOGI(AAFwkTag::TEST, "NotifyPreloadAbilityStateChanged_003 end");
686 }
687
688 /*
689 * Feature: AppMgrService
690 * Function: AppMgrClient::NotifyPreloadAbilityStateChanged
691 * SubFunction: NotifyPreloadAbilityStateChanged
692 * FunctionPoints: AppMgrClient NotifyPreloadAbilityStateChanged interface
693 * EnvConditions: Mobile that can run ohos test framework
694 * CaseDescription: Test NotifyPreloadAbilityStateChanged with valid parameters.
695 */
696 HWTEST_F(AmsAppMgrClientTest, NotifyPreloadAbilityStateChanged_004, TestSize.Level1)
697 {
698 TAG_LOGI(AAFwkTag::TEST, "NotifyPreloadAbilityStateChanged_004 start");
699 EXPECT_EQ(AppMgrResultCode::RESULT_OK, client_->ConnectAppMgrService());
700
701 sptr<MockAmsMgrScheduler> mockAmsMgrScheduler(new MockAmsMgrScheduler());
702 EXPECT_CALL(*(static_cast<MockAppMgrService*>((iface_cast<IAppMgr>(client_->GetRemoteObject())).GetRefPtr())),
703 GetAmsMgr())
704 .Times(1)
705 .WillOnce(Return(mockAmsMgrScheduler));
706
707 EXPECT_CALL(*mockAmsMgrScheduler, NotifyPreloadAbilityStateChanged(_, _))
708 .Times(1)
709 .WillOnce(Return(AppMgrResultCode::RESULT_OK));
710
711 int32_t result = client_->NotifyPreloadAbilityStateChanged(token_, true);
712 EXPECT_EQ(result, ERR_OK);
713 TAG_LOGI(AAFwkTag::TEST, "NotifyPreloadAbilityStateChanged_004 end");
714 }
715
716 HWTEST_F(AmsAppMgrClientTest, CheckPreloadAppRecordExist_001, TestSize.Level1)
717 {
718 TAG_LOGI(AAFwkTag::TEST, "CheckPreloadAppRecordExist_001 start");
719 client_->mgrHolder_ = nullptr;
720
721 std::string bundleName = "com.example.test";
722 int32_t userId = 100;
723 int32_t appIndex = 0;
724 bool isExist = false;
725
726 int32_t result = client_->CheckPreloadAppRecordExist(bundleName, userId, appIndex, isExist);
727 EXPECT_EQ(result, AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED);
728 TAG_LOGI(AAFwkTag::TEST, "CheckPreloadAppRecordExist_001 end");
729 }
730
731 HWTEST_F(AmsAppMgrClientTest, CheckPreloadAppRecordExist_002, TestSize.Level1)
732 {
733 TAG_LOGI(AAFwkTag::TEST, "CheckPreloadAppRecordExist_002 start");
734 client_->SetServiceManager(nullptr);
735
736 std::string bundleName = "com.example.test";
737 int32_t userId = 100;
738 int32_t appIndex = 0;
739 bool isExist = false;
740
741 int32_t result = client_->CheckPreloadAppRecordExist(bundleName, userId, appIndex, isExist);
742 EXPECT_EQ(result, AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED);
743 TAG_LOGI(AAFwkTag::TEST, "CheckPreloadAppRecordExist_002 end");
744 }
745
746 HWTEST_F(AmsAppMgrClientTest, CheckPreloadAppRecordExist_003, TestSize.Level1)
747 {
748 TAG_LOGI(AAFwkTag::TEST, "CheckPreloadAppRecordExist_003 start");
749 EXPECT_EQ(AppMgrResultCode::RESULT_OK, client_->ConnectAppMgrService());
750
751 sptr<IAmsMgr> amsMgrScheduler = nullptr;
752 EXPECT_CALL(*(static_cast<MockAppMgrService*>((iface_cast<IAppMgr>(client_->GetRemoteObject())).GetRefPtr())),
753 GetAmsMgr())
754 .Times(1)
755 .WillOnce(Return(amsMgrScheduler));
756
757 std::string bundleName = "com.example.test";
758 int32_t userId = 100;
759 int32_t appIndex = 0;
760 bool isExist = false;
761
762 int32_t result = client_->CheckPreloadAppRecordExist(bundleName, userId, appIndex, isExist);
763 EXPECT_EQ(result, AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED);
764 TAG_LOGI(AAFwkTag::TEST, "CheckPreloadAppRecordExist_003 end");
765 }
766
767 HWTEST_F(AmsAppMgrClientTest, CheckPreloadAppRecordExist_004, TestSize.Level1)
768 {
769 TAG_LOGI(AAFwkTag::TEST, "CheckPreloadAppRecordExist_004 start");
770 EXPECT_EQ(AppMgrResultCode::RESULT_OK, client_->ConnectAppMgrService());
771
772 sptr<MockAmsMgrScheduler> mockAmsMgrScheduler(new MockAmsMgrScheduler());
773 EXPECT_CALL(*(static_cast<MockAppMgrService*>((iface_cast<IAppMgr>(client_->GetRemoteObject())).GetRefPtr())),
774 GetAmsMgr())
775 .Times(1)
776 .WillOnce(Return(mockAmsMgrScheduler));
777
778 EXPECT_CALL(*mockAmsMgrScheduler, CheckPreloadAppRecordExist(_, _, _, _))
779 .Times(1)
780 .WillOnce(Return(AppMgrResultCode::RESULT_OK));
781
782 std::string bundleName = "com.example.test";
783 int32_t userId = 100;
784 int32_t appIndex = 0;
785 bool isExist = false;
786
787 int32_t result = client_->CheckPreloadAppRecordExist(bundleName, userId, appIndex, isExist);
788 EXPECT_EQ(result, ERR_OK);
789 TAG_LOGI(AAFwkTag::TEST, "CheckPreloadAppRecordExist_004 end");
790 }
791
792 /*
793 * Feature: AppMgrService
794 * Function: AppMgrClient::QueryRunningSharedBundles
795 * SubFunction: QueryRunningSharedBundles
796 * FunctionPoints: AppMgrClient QueryRunningSharedBundles interface
797 * EnvConditions: Mobile that can run ohos test framework
798 * CaseDescription: Test QueryRunningSharedBundles with valid parameters.
799 */
800 HWTEST_F(AmsAppMgrClientTest, QueryRunningSharedBundles_001, TestSize.Level1)
801 {
802 TAG_LOGI(AAFwkTag::TEST, "QueryRunningSharedBundles_001 start");
803 client_->mgrHolder_ = nullptr;
804
805 pid_t pid = 1;
806 std::map<std::string, uint32_t> sharedBundles;
807 int32_t result = client_->QueryRunningSharedBundles(pid, sharedBundles);
808 EXPECT_EQ(result, AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED);
809 TAG_LOGI(AAFwkTag::TEST, "QueryRunningSharedBundles_001 end");
810 }
811
812 /*
813 * Feature: AppMgrService
814 * Function: AppMgrClient::QueryRunningSharedBundles
815 * SubFunction: QueryRunningSharedBundles
816 * FunctionPoints: AppMgrClient QueryRunningSharedBundles interface
817 * EnvConditions: Mobile that can run ohos test framework
818 * CaseDescription: Test QueryRunningSharedBundles with valid parameters.
819 */
820 HWTEST_F(AmsAppMgrClientTest, QueryRunningSharedBundles_002, TestSize.Level1)
821 {
822 TAG_LOGI(AAFwkTag::TEST, "QueryRunningSharedBundles_002 start");
823 client_->SetServiceManager(nullptr);
824
825 pid_t pid = 1;
826 std::map<std::string, uint32_t> sharedBundles;
827 int32_t result = client_->QueryRunningSharedBundles(pid, sharedBundles);
828 EXPECT_EQ(result, AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED);
829 TAG_LOGI(AAFwkTag::TEST, "QueryRunningSharedBundles_002 end");
830 }
831
832 /*
833 * Feature: AppMgrService
834 * Function: AppMgrClient::QueryRunningSharedBundles
835 * SubFunction: QueryRunningSharedBundles
836 * FunctionPoints: AppMgrClient QueryRunningSharedBundles interface
837 * EnvConditions: Mobile that can run ohos test framework
838 * CaseDescription: Test QueryRunningSharedBundles with valid parameters.
839 */
840 HWTEST_F(AmsAppMgrClientTest, QueryRunningSharedBundles_003, TestSize.Level1)
841 {
842 TAG_LOGI(AAFwkTag::TEST, "QueryRunningSharedBundles_003 start");
843 EXPECT_EQ(AppMgrResultCode::RESULT_OK, client_->ConnectAppMgrService());
844
845 EXPECT_CALL(*(static_cast<MockAppMgrService*>((iface_cast<IAppMgr>(client_->GetRemoteObject())).GetRefPtr())),
846 QueryRunningSharedBundles(_, _))
847 .Times(1)
848 .WillOnce(Return(ERR_OK));
849
850 pid_t pid = 1;
851 std::map<std::string, uint32_t> sharedBundles;
852 int32_t result = client_->QueryRunningSharedBundles(pid, sharedBundles);
853 EXPECT_EQ(result, ERR_OK);
854 TAG_LOGI(AAFwkTag::TEST, "QueryRunningSharedBundles_003 end");
855 }
856 } // namespace AppExecFwk
857 } // namespace OHOS
858