1 /*
2  * Copyright (c) 2022-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 
18 #include "ability_foreground_state_observer_interface.h"
19 #include "app_foreground_state_observer_stub.h"
20 #include "app_mgr_proxy.h"
21 #include "hilog_tag_wrapper.h"
22 #include "mock_ability_foreground_state_observer_stub.h"
23 #include "mock_app_mgr_service.h"
24 #include "quick_fix_callback_stub.h"
25 #include "render_state_observer_stub.h"
26 
27 using namespace testing;
28 using namespace testing::ext;
29 
30 namespace OHOS {
31 namespace AppExecFwk {
32 namespace {
33 const int32_t USER_ID = 100;
34 } // namespace
35 
36 class AppForegroundStateObserverMock : public AppForegroundStateObserverStub {
37 public:
38     AppForegroundStateObserverMock() = default;
39     virtual ~AppForegroundStateObserverMock() = default;
40 
OnAppStateChanged(const AppStateData & appStateData)41     void OnAppStateChanged(const AppStateData &appStateData) override
42     {}
43 };
44 
45 class RenderStateObserverMock : public RenderStateObserverStub {
46 public:
47     RenderStateObserverMock() = default;
48     virtual ~RenderStateObserverMock() = default;
OnRenderStateChanged(const RenderStateData & renderStateData)49     void OnRenderStateChanged(const RenderStateData &renderStateData) override
50     {}
51 };
52 
53 class QuickFixCallbackImpl : public AppExecFwk::QuickFixCallbackStub {
54 public:
55     QuickFixCallbackImpl() = default;
56     virtual ~QuickFixCallbackImpl() = default;
57 
OnLoadPatchDone(int32_t resultCode,int32_t recordId)58     void OnLoadPatchDone(int32_t resultCode, int32_t recordId) override
59     {
60         TAG_LOGD(AAFwkTag::TEST, "function called.");
61     }
62 
OnUnloadPatchDone(int32_t resultCode,int32_t recordId)63     void OnUnloadPatchDone(int32_t resultCode, int32_t recordId) override
64     {
65         TAG_LOGD(AAFwkTag::TEST, "function called.");
66     }
67 
OnReloadPageDone(int32_t resultCode,int32_t recordId)68     void OnReloadPageDone(int32_t resultCode, int32_t recordId) override
69     {
70         TAG_LOGD(AAFwkTag::TEST, "function called.");
71     }
72 };
73 
74 class AppMgrProxyTest : public testing::Test {
75 public:
76     static void SetUpTestCase();
77     static void TearDownTestCase();
78     void SetUp() override;
79     void TearDown() override;
80 
81     sptr<MockAppMgrService> mockAppMgrService_;
82     sptr<AppMgrProxy> appMgrProxy_;
83 };
84 
SetUpTestCase(void)85 void AppMgrProxyTest::SetUpTestCase(void)
86 {}
87 
TearDownTestCase(void)88 void AppMgrProxyTest::TearDownTestCase(void)
89 {}
90 
SetUp()91 void AppMgrProxyTest::SetUp()
92 {
93     GTEST_LOG_(INFO) << "AppMgrProxyTest::SetUp()";
94 
95     mockAppMgrService_ = new MockAppMgrService();
96     appMgrProxy_ = new AppMgrProxy(mockAppMgrService_);
97 }
98 
TearDown()99 void AppMgrProxyTest::TearDown()
100 {}
101 
102 /**
103  * @tc.name: AppMgrProxy_GetProcessRunningInfosByUserId_0100
104  * @tc.desc: GetProcessRunningInfosByUserId
105  * @tc.type: FUNC
106  * @tc.require: SR000GH1GO
107  */
108 HWTEST_F(AppMgrProxyTest, AppMgrProxy_GetProcessRunningInfosByUserId_0100, TestSize.Level0)
109 {
110     GTEST_LOG_(INFO) << "AppMgrProxy_GetProcessRunningInfosByUserId_0100 start";
111 
112     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
113         .Times(1)
114         .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
115 
116     std::vector<RunningProcessInfo> info;
117     appMgrProxy_->GetProcessRunningInfosByUserId(info, USER_ID);
118 
119     EXPECT_EQ(
120         mockAppMgrService_->code_, static_cast<uint32_t>(AppMgrInterfaceCode::APP_GET_RUNNING_PROCESSES_BY_USER_ID));
121 
122     GTEST_LOG_(INFO) << "AppMgrProxy_GetProcessRunningInfosByUserId_0100 end";
123 }
124 
125 /**
126  * @tc.name: AppMgrProxy_GetAllRenderProcesses_0100
127  * @tc.desc: GetAllRenderProcesses
128  * @tc.type: FUNC
129  */
130 HWTEST_F(AppMgrProxyTest, AppMgrProxy_GetAllRenderProcesses_0100, TestSize.Level0)
131 {
132     GTEST_LOG_(INFO) << "AppMgrProxy_GetAllRenderProcesses_0100 start";
133 
134     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
135         .Times(1)
136         .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
137 
138     std::vector<RenderProcessInfo> info;
139     appMgrProxy_->GetAllRenderProcesses(info);
140 
141     EXPECT_EQ(
142         mockAppMgrService_->code_, static_cast<uint32_t>(AppMgrInterfaceCode::APP_GET_ALL_RENDER_PROCESSES));
143 
144     GTEST_LOG_(INFO) << "AppMgrProxy_GetAllRenderProcesses_0100 end";
145 }
146 
147 /**
148  * @tc.name: AppMgrProxy_GetAllChildrenProcesses_0100
149  * @tc.desc: GetAllChildrenProcesses
150  * @tc.type: FUNC
151  */
152 HWTEST_F(AppMgrProxyTest, AppMgrProxy_GetAllChildrenProcesses_0100, TestSize.Level0)
153 {
154     GTEST_LOG_(INFO) << "AppMgrProxy_GetAllChildrenProcesses_0100 start";
155 
156     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
157         .Times(1)
158         .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
159 
160     std::vector<ChildProcessInfo> info;
161     appMgrProxy_->GetAllChildrenProcesses(info);
162 
163     EXPECT_EQ(
164         mockAppMgrService_->code_, static_cast<uint32_t>(AppMgrInterfaceCode::GET_ALL_CHILDREN_PROCESSES));
165 
166     GTEST_LOG_(INFO) << "AppMgrProxy_GetAllChildrenProcesses_0100 end";
167 }
168 
169 /**
170  * @tc.name: GetAppRunningStateByBundleName_0100
171  * @tc.desc: Get app running state by bundle name.
172  * @tc.type: FUNC
173  * @tc.require: issueI581VW
174  */
175 HWTEST_F(AppMgrProxyTest, GetAppRunningStateByBundleName_0100, TestSize.Level0)
176 {
177     TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__);
178 
179     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
180         .Times(1)
181         .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
182 
183     std::string bundleName = "testBundleName";
184     appMgrProxy_->GetAppRunningStateByBundleName(bundleName);
185 
186     EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(AppMgrInterfaceCode::GET_APP_RUNNING_STATE));
187 
188     TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__);
189 }
190 
191 /**
192  * @tc.name: NotifyLoadRepairPatch_0100
193  * @tc.desc: Notify load repair patch.
194  * @tc.type: FUNC
195  * @tc.require: issueI581VW
196  */
197 HWTEST_F(AppMgrProxyTest, NotifyLoadRepairPatch_0100, TestSize.Level0)
198 {
199     TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__);
200 
201     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
202         .Times(1)
203         .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
204 
205     std::string bundleName = "testBundleName";
206     sptr<IQuickFixCallback> callback = new QuickFixCallbackImpl();
207     appMgrProxy_->NotifyLoadRepairPatch(bundleName, callback);
208 
209     EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(AppMgrInterfaceCode::NOTIFY_LOAD_REPAIR_PATCH));
210 
211     TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__);
212 }
213 
214 /**
215  * @tc.name: NotifyHotReloadPage_0100
216  * @tc.desc: Notify ace execute hot reload page.
217  * @tc.type: FUNC
218  * @tc.require: issueI581VW
219  */
220 HWTEST_F(AppMgrProxyTest, NotifyHotReloadPage_0100, TestSize.Level0)
221 {
222     TAG_LOGI(AAFwkTag::TEST, "%{public}s start", __func__);
223 
224     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
225         .Times(1)
226         .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
227 
228     std::string bundleName = "testBundleName";
229     sptr<IQuickFixCallback> callback = new QuickFixCallbackImpl();
230     appMgrProxy_->NotifyHotReloadPage(bundleName, callback);
231 
232     EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(AppMgrInterfaceCode::NOTIFY_HOT_RELOAD_PAGE));
233 
234     TAG_LOGI(AAFwkTag::TEST, "%{public}s end", __func__);
235 }
236 
237 /**
238  * @tc.name: NotifyUnLoadRepairPatch_0100
239  * @tc.desc: Notify unload repair patch.
240  * @tc.type: FUNC
241  * @tc.require: issueI581VW
242  */
243 HWTEST_F(AppMgrProxyTest, NotifyUnLoadRepairPatch_0100, TestSize.Level0)
244 {
245     TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__);
246 
247     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
248         .Times(1)
249         .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
250 
251     std::string bundleName = "testBundleName";
252     sptr<IQuickFixCallback> callback = new QuickFixCallbackImpl();
253     appMgrProxy_->NotifyUnLoadRepairPatch(bundleName, callback);
254 
255     EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(AppMgrInterfaceCode::NOTIFY_UNLOAD_REPAIR_PATCH));
256 
257     TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__);
258 }
259 
260 /**
261  * @tc.name: PreStartNWebSpawnProcess_001
262  * @tc.desc: prestart nwebspawn process.
263  * @tc.type: FUNC
264  * @tc.require: issueI5W4S7
265  */
266 HWTEST_F(AppMgrProxyTest, PreStartNWebSpawnProcess_001, TestSize.Level0)
267 {
268     TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__);
269 
270     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
271         .Times(1)
272         .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
273 
274     appMgrProxy_->PreStartNWebSpawnProcess();
275     EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(AppMgrInterfaceCode::PRE_START_NWEBSPAWN_PROCESS));
276 
277     TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__);
278 }
279 
280 /**
281  * @tc.name: GetProcessMemoryByPid_001
282  * @tc.desc: Get memorySize by pid.
283  * @tc.type: FUNC
284  * @tc.require: issueI76JBF
285  */
286 HWTEST_F(AppMgrProxyTest, GetProcessMemoryByPid_001, TestSize.Level0)
287 {
288     TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__);
289 
290     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
291         .Times(1)
292         .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
293 
294     int32_t pid = 0;
295     int32_t memorySize = 0;
296     appMgrProxy_->GetProcessMemoryByPid(pid, memorySize);
297     EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(AppMgrInterfaceCode::GET_PROCESS_MEMORY_BY_PID));
298 
299     TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__);
300 }
301 
302 /**
303  * @tc.name: GetRunningProcessInformation_001
304  * @tc.desc: Get application processes information list by bundleName.
305  * @tc.type: FUNC
306  * @tc.require: issueI76JBF
307  */
308 HWTEST_F(AppMgrProxyTest, GetRunningProcessInformation_001, TestSize.Level0)
309 {
310     TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__);
311 
312     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
313         .Times(1)
314         .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
315 
316     std::string bundleName = "testBundleName";
317     int32_t userId = USER_ID;
318     std::vector<RunningProcessInfo> info;
319     appMgrProxy_->GetRunningProcessInformation(bundleName, userId, info);
320     EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(AppMgrInterfaceCode::GET_PIDS_BY_BUNDLENAME));
321 
322     TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__);
323 }
324 
325 /**
326  * @tc.name: NotifyAppFault_001
327  * @tc.desc: Notify app fault.
328  * @tc.type: FUNC
329  * @tc.require: issueI79RY8
330  */
331 HWTEST_F(AppMgrProxyTest, NotifyAppFault_001, TestSize.Level1)
332 {
333     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
334         .Times(1)
335         .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
336     FaultData faultData;
337     appMgrProxy_->NotifyAppFault(faultData);
338     EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(AppMgrInterfaceCode::NOTIFY_APP_FAULT));
339 }
340 
341 /**
342  * @tc.name: NotifyAppFaultBySA_001
343  * @tc.desc: Notify app fault by SA.
344  * @tc.type: FUNC
345  * @tc.require: issueI79RY8
346  */
347 HWTEST_F(AppMgrProxyTest, NotifyAppFaultBySA_001, TestSize.Level1)
348 {
349     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
350         .Times(1)
351         .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
352     AppFaultDataBySA faultData;
353     appMgrProxy_->NotifyAppFaultBySA(faultData);
354     EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(AppMgrInterfaceCode::NOTIFY_APP_FAULT_BY_SA));
355 }
356 
357 /**
358  * @tc.name: SetAppFreezeFilter_001
359  * @tc.desc: Set appfreeze filter.
360  * @tc.type: FUNC
361  */
362 HWTEST_F(AppMgrProxyTest, SetAppFreezeFilter_001, TestSize.Level1)
363 {
364     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
365         .Times(1)
366         .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
367     int32_t pid = 0; // test value
368     appMgrProxy_->SetAppFreezeFilter(pid);
369     EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(AppMgrInterfaceCode::SET_APPFREEZE_FILTER));
370 }
371 
372 /**
373  * @tc.name: ChangeAppGcState_001
374  * @tc.desc: Change app Gc state.
375  * @tc.type: FUNC
376  * @tc.require: issuesI85VVU
377  */
378 HWTEST_F(AppMgrProxyTest, ChangeAppGcState_001, TestSize.Level1)
379 {
380     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
381         .Times(1)
382         .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
383     int32_t pid = 0;
384     int32_t state = 0;
385     appMgrProxy_->ChangeAppGcState(pid, state);
386     EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(AppMgrInterfaceCode::CHANGE_APP_GC_STATE));
387 }
388 
389 /**
390  * @tc.name: IsApplicationRunning_001
391  * @tc.desc: Send request to query the running status of the application.
392  * @tc.type: FUNC
393  */
394 HWTEST_F(AppMgrProxyTest, IsApplicationRunning_001, TestSize.Level1)
395 {
396     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
397         .Times(1)
398         .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
399 
400     std::string bundleName = "testBundleName";
401     bool isRunning = false;
402     appMgrProxy_->IsApplicationRunning(bundleName, isRunning);
403     EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(AppMgrInterfaceCode::IS_APPLICATION_RUNNING));
404 }
405 
406 /**
407  * @tc.name: IsAppRunning_001
408  * @tc.desc: Send request to query the running status of the application.
409  * @tc.type: FUNC
410  */
411 HWTEST_F(AppMgrProxyTest, IsAppRunning_001, TestSize.Level1)
412 {
413     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
414         .Times(1)
415         .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
416 
417     std::string bundleName = "testBundleName";
418     int32_t appCloneIndex = 0;
419     bool isRunning = false;
420 
421     appMgrProxy_->IsAppRunning(bundleName, appCloneIndex, isRunning);
422     EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(AppMgrInterfaceCode::IS_APP_RUNNING));
423 }
424 
425 /**
426  * @tc.number: RegisterAbilityForegroundStateObserver_0100
427  * @tc.desc: Verify that the RegisterAbilityForegroundStateObserver function is called normally.
428  * @tc.type: FUNC
429  */
430 HWTEST_F(AppMgrProxyTest, RegisterAbilityForegroundStateObserver_0100, TestSize.Level1)
431 {
432     sptr<IAbilityForegroundStateObserver> observer = new MockAbilityForegroundStateObserverStub();
433     EXPECT_NE(observer->AsObject(), nullptr);
434     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
435         .Times(1)
436         .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
437     appMgrProxy_->RegisterAbilityForegroundStateObserver(observer);
438     EXPECT_EQ(mockAppMgrService_->code_,
439         static_cast<uint32_t>(AppMgrInterfaceCode::REGISTER_ABILITY_FOREGROUND_STATE_OBSERVER));
440 }
441 
442 /**
443  * @tc.number: RegisterAbilityForegroundStateObserver_0200
444  * @tc.desc: Verify that the RegisterAbilityForegroundStateObserver parameter of the function is null.
445  * @tc.type: FUNC
446  */
447 HWTEST_F(AppMgrProxyTest, RegisterAbilityForegroundStateObserver_0200, TestSize.Level1)
448 {
449     sptr<IAbilityForegroundStateObserver> observer = nullptr;
450     auto result = appMgrProxy_->RegisterAbilityForegroundStateObserver(observer);
451     EXPECT_EQ(result, OHOS::ERR_INVALID_VALUE);
452 }
453 
454 /**
455  * @tc.number: UnregisterAbilityForegroundStateObserver_0100
456  * @tc.desc: Verify that the UnregisterAbilityForegroundStateObserver function is called normally.
457  * @tc.type: FUNC
458  */
459 HWTEST_F(AppMgrProxyTest, UnregisterAbilityForegroundStateObserver_0100, TestSize.Level1)
460 {
461     sptr<IAbilityForegroundStateObserver> observer = new MockAbilityForegroundStateObserverStub();
462     EXPECT_NE(observer->AsObject(), nullptr);
463     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
464         .Times(1)
465         .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
466     appMgrProxy_->UnregisterAbilityForegroundStateObserver(observer);
467     EXPECT_EQ(mockAppMgrService_->code_,
468         static_cast<uint32_t>(AppMgrInterfaceCode::UNREGISTER_ABILITY_FOREGROUND_STATE_OBSERVER));
469 }
470 
471 /**
472  * @tc.number: RegisterAbilityForegroundStateObserver_0200
473  * @tc.desc: Verify that the UnregisterAbilityForegroundStateObserver parameter of the function is null.
474  * @tc.type: FUNC
475  */
476 HWTEST_F(AppMgrProxyTest, UnregisterAbilityForegroundStateObserver_0200, TestSize.Level1)
477 {
478     sptr<IAbilityForegroundStateObserver> observer = nullptr;
479     auto result = appMgrProxy_->UnregisterAbilityForegroundStateObserver(observer);
480     EXPECT_EQ(result, OHOS::ERR_INVALID_VALUE);
481 }
482 
483 /**
484  * @tc.name: RegisterAppForegroundStateObserver_0100
485  * @tc.desc: Test when all condition not met.
486  * @tc.type: FUNC
487  */
488 HWTEST_F(AppMgrProxyTest, RegisterAppForegroundStateObserver_0100, TestSize.Level1)
489 {
490     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _)).Times(1);
491     sptr<IAppForegroundStateObserver> observer = new (std::nothrow) AppForegroundStateObserverMock();
492     auto res = appMgrProxy_->RegisterAppForegroundStateObserver(observer);
493     EXPECT_EQ(res, NO_ERROR);
494 }
495 
496 /**
497  * @tc.name: UnregisterAppForegroundStateObserver_0100
498  * @tc.desc: Test when all condition not met.
499  * @tc.type: FUNC
500  */
501 HWTEST_F(AppMgrProxyTest, UnregisterAppForegroundStateObserver_0100, TestSize.Level1)
502 {
503     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _)).Times(1);
504     sptr<IAppForegroundStateObserver> observer = new (std::nothrow) AppForegroundStateObserverMock();
505     auto res = appMgrProxy_->RegisterAppForegroundStateObserver(observer);
506     EXPECT_EQ(res, NO_ERROR);
507 }
508 
509 /**
510  * @tc.name: RegisterRenderStateObserver_0100
511  * @tc.desc: Test registerRenderStateObserversendRequest.
512  * @tc.type: FUNC
513  */
514 HWTEST_F(AppMgrProxyTest, RegisterRenderStateObserver_0100, TestSize.Level1)
515 {
516     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _)).Times(1);
517     sptr<IRenderStateObserver> observer = new (std::nothrow) RenderStateObserverMock();
518     auto res = appMgrProxy_->RegisterRenderStateObserver(observer);
519     EXPECT_EQ(res, NO_ERROR);
520 }
521 
522 /**
523  * @tc.name: UnregisterRenderStateObserver_0100
524  * @tc.desc: Test unregisterRenderStateObserversendRequest.
525  * @tc.type: FUNC
526  */
527 HWTEST_F(AppMgrProxyTest, UnregisterRenderStateObserver_0100, TestSize.Level1)
528 {
529     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _)).Times(1);
530     sptr<IRenderStateObserver> observer = new (std::nothrow) RenderStateObserverMock();
531     auto res = appMgrProxy_->UnregisterRenderStateObserver(observer);
532     EXPECT_EQ(res, NO_ERROR);
533 }
534 
535 /**
536  * @tc.name: UpdateRenderState_0100
537  * @tc.desc: Test updateRenderState sendRequest.
538  * @tc.type: FUNC
539  */
540 HWTEST_F(AppMgrProxyTest, UpdateRenderState_0100, TestSize.Level1)
541 {
542     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _)).Times(1);
543     pid_t renderPid = 0;
544     int32_t state = 0;
545     auto res = appMgrProxy_->UpdateRenderState(renderPid, state);
546     EXPECT_EQ(res, NO_ERROR);
547 }
548 
549 /**
550  * @tc.name: SignRestartAppFlag_0100
551  * @tc.desc: Test SignRestartAppFlag.
552  * @tc.type: FUNC
553  */
554 HWTEST_F(AppMgrProxyTest, SignRestartAppFlag_0100, TestSize.Level1)
555 {
556     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _)).Times(1);
557     int32_t uid = 0;
558     auto res = appMgrProxy_->SignRestartAppFlag(uid);
559     EXPECT_EQ(res, NO_ERROR);
560 }
561 
562 /**
563  * @tc.name: NotifyMemorySizeStateChanged_0100
564  * @tc.desc: Test NotifyMemorySizeStateChanged.
565  * @tc.type: FUNC
566  */
567 HWTEST_F(AppMgrProxyTest, NotifyMemorySizeStateChanged_0100, TestSize.Level1)
568 {
569     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _)).Times(1);
570     auto res = appMgrProxy_->NotifyMemorySizeStateChanged(true);
571     EXPECT_EQ(res, NO_ERROR);
572 }
573 
574 /**
575  * @tc.name: NotifyMemorySizeStateChanged_0200
576  * @tc.desc: Test NotifyMemorySizeStateChanged.
577  * @tc.type: FUNC
578  */
579 HWTEST_F(AppMgrProxyTest, NotifyMemorySizeStateChanged_0200, TestSize.Level1)
580 {
581     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _)).Times(1);
582     auto res = appMgrProxy_->NotifyMemorySizeStateChanged(false);
583     EXPECT_EQ(res, NO_ERROR);
584 }
585 
586 /**
587  * @tc.name: GetAllUIExtensionRootHostPid_0100
588  * @tc.desc: Get all ui extension root host pid.
589  * @tc.type: FUNC
590  */
591 HWTEST_F(AppMgrProxyTest, GetAllUIExtensionRootHostPid_0100, TestSize.Level1)
592 {
593     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
594         .Times(1)
595         .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
596     pid_t pid = 0;
597     std::vector<pid_t> hostPids;
598     auto res = appMgrProxy_->GetAllUIExtensionRootHostPid(pid, hostPids);
599     EXPECT_EQ(res, NO_ERROR);
600     EXPECT_EQ(mockAppMgrService_->code_,
601         static_cast<uint32_t>(AppMgrInterfaceCode::GET_ALL_UI_EXTENSION_ROOT_HOST_PID));
602 }
603 
604 /**
605  * @tc.name: GetAllUIExtensionProviderPid_0100
606  * @tc.desc: Get all ui extension provider pid.
607  * @tc.type: FUNC
608  */
609 HWTEST_F(AppMgrProxyTest, GetAllUIExtensionProviderPid_0100, TestSize.Level1)
610 {
611     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
612         .Times(1)
613         .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
614     pid_t hostPid = 0;
615     std::vector<pid_t> providerPids;
616     auto res = appMgrProxy_->GetAllUIExtensionProviderPid(hostPid, providerPids);
617     EXPECT_EQ(res, NO_ERROR);
618     EXPECT_EQ(mockAppMgrService_->code_,
619         static_cast<uint32_t>(AppMgrInterfaceCode::GET_ALL_UI_EXTENSION_PROVIDER_PID));
620 }
621 
622 /**
623  * @tc.name: PreloadApplication_0100
624  * @tc.desc: Preload application.
625  * @tc.type: FUNC
626  */
627 HWTEST_F(AppMgrProxyTest, PreloadApplication_0100, TestSize.Level1)
628 {
629     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
630         .Times(1)
631         .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
632 
633     std::string bundleName = "com.acts.preloadtest";
634     int32_t userId = 100;
635     PreloadMode preloadMode = PreloadMode::PRE_MAKE;
636     int32_t appIndex = 0;
637     auto ret = appMgrProxy_->PreloadApplication(bundleName, userId, preloadMode, appIndex);
638     EXPECT_EQ(ret, NO_ERROR);
639     EXPECT_EQ(mockAppMgrService_->code_,
640         static_cast<uint32_t>(AppMgrInterfaceCode::PRELOAD_APPLICATION));
641 }
642 
643 /**
644  * @tc.name: SetSupportedProcessCacheSelf_001
645  * @tc.desc: The application sets itself whether or not to support process cache.
646  * @tc.type: FUNC
647  */
648 HWTEST_F(AppMgrProxyTest, SetSupportedProcessCacheSelf_001, TestSize.Level0)
649 {
650     TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__);
651 
652     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
653         .Times(1)
654         .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
655     bool isSupported = false;
656     appMgrProxy_->SetSupportedProcessCacheSelf(isSupported);
657     EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(AppMgrInterfaceCode::SET_SUPPORTED_PROCESS_CACHE_SELF));
658 
659     TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__);
660 }
661 
662 /**
663  * @tc.name: GetRunningMultiAppInfoByBundleName_001
664  * @tc.desc: Get multiApp information by bundleName.
665  * @tc.type: FUNC
666  * @tc.require: issueI9HMAO
667  */
668 HWTEST_F(AppMgrProxyTest, GetRunningMultiAppInfoByBundleName_001, TestSize.Level1)
669 {
670     TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__);
671 
672     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
673         .Times(1)
674         .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
675 
676     std::string bundleName = "testBundleName";
677     RunningMultiAppInfo info;
678     appMgrProxy_->GetRunningMultiAppInfoByBundleName(bundleName, info);
679     EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>
680         (AppMgrInterfaceCode::GET_RUNNING_MULTIAPP_INFO_BY_BUNDLENAME));
681 
682     TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__);
683 }
684 
685 /**
686  * @tc.name: GetAllRunningInstanceKeysBySelf_001
687  * @tc.desc: GetAllRunningInstanceKeysBySelf.
688  * @tc.type: FUNC
689  * @tc.require: issueI9HMAO
690  */
691 HWTEST_F(AppMgrProxyTest, GetAllRunningInstanceKeysBySelf_001, TestSize.Level1)
692 {
693     TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__);
694 
695     MessageParcel data;
696     MessageParcel reply;
697     MessageOption option;
698 
699     data.WriteInterfaceToken(AppMgrStub::GetDescriptor());
700 
701     EXPECT_CALL(*mockAppMgrService_, GetAllRunningInstanceKeysBySelf(_)).Times(1);
702 
703     auto result = mockAppMgrService_->OnRemoteRequest(
704         static_cast<uint32_t>(AppMgrInterfaceCode::GET_All_RUNNING_INSTANCE_KEYS_BY_SELF), data, reply, option);
705     EXPECT_EQ(result, NO_ERROR);
706 
707     TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__);
708 }
709 
710 /**
711  * @tc.name: GetAllRunningInstanceKeysByBundleName_001
712  * @tc.desc: GetAllRunningInstanceKeysByBundleName.
713  * @tc.type: FUNC
714  * @tc.require: issueI9HMAO
715  */
716 HWTEST_F(AppMgrProxyTest, GetAllRunningInstanceKeysByBundleName_001, TestSize.Level1)
717 {
718     TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__);
719 
720     MessageParcel data;
721     MessageParcel reply;
722     MessageOption option;
723 
724     data.WriteInterfaceToken(AppMgrStub::GetDescriptor());
725     std::string bundleName = "testBundleName";
726     data.WriteString(bundleName);
727     int32_t userId = -1;
728     data.WriteInt32(userId);
729 
730     EXPECT_CALL(*mockAppMgrService_, GetAllRunningInstanceKeysByBundleName(_, _, _)).Times(1);
731 
732     auto result = mockAppMgrService_->OnRemoteRequest(
733         static_cast<uint32_t>(AppMgrInterfaceCode::GET_All_RUNNING_INSTANCE_KEYS_BY_BUNDLENAME), data, reply, option);
734     EXPECT_EQ(result, NO_ERROR);
735 
736     TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__);
737 }
738 
739 /**
740  * @tc.name: GetAppIndexByPid_001
741  * @tc.desc: Get app index of pid.
742  * @tc.type: FUNC
743  * @tc.require:
744  */
745 HWTEST_F(AppMgrProxyTest, GetAppIndexByPid_001, TestSize.Level1)
746 {
747     TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__);
748 
749     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
750         .Times(1)
751         .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
752 
753     pid_t pid = 1;
754     int32_t appIndex = -1;
755     appMgrProxy_->GetAppIndexByPid(pid, appIndex);
756     EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(AppMgrInterfaceCode::GET_APP_INDEX_BY_PID));
757 
758     TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__);
759 }
760 
761 /**
762  * @tc.name: GetSupportedProcessCachePids_001
763  * @tc.desc: Get pids of processes which belong to specific bundle name and support process cache feature.
764  * @tc.type: FUNC
765  * @tc.require: issueIAGZ7H
766  */
767 HWTEST_F(AppMgrProxyTest, GetSupportedProcessCachePids_001, TestSize.Level0)
768 {
769     TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__);
770 
771     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
772         .Times(1)
773         .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
774 
775     std::string bundleName = "testBundleName";
776     std::vector<int32_t> pidList;
777     appMgrProxy_->GetSupportedProcessCachePids(bundleName, pidList);
778     EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(AppMgrInterfaceCode::GET_SUPPORTED_PROCESS_CACHE_PIDS));
779 
780     TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__);
781 }
782 } // namespace AppExecFwk
783 } // namespace OHOS
784