• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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_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 
26 using namespace testing;
27 using namespace testing::ext;
28 
29 namespace OHOS {
30 namespace AppExecFwk {
31 namespace {
32 const int32_t USER_ID = 100;
33 } // namespace
34 
35 class AppForegroundStateObserverMock : public AppForegroundStateObserverStub {
36 public:
37     AppForegroundStateObserverMock() = default;
38     virtual ~AppForegroundStateObserverMock() = default;
39 
OnAppStateChanged(const AppStateData & appStateData)40     void OnAppStateChanged(const AppStateData &appStateData) override
41     {}
42 };
43 
44 class QuickFixCallbackImpl : public AppExecFwk::QuickFixCallbackStub {
45 public:
46     QuickFixCallbackImpl() = default;
47     virtual ~QuickFixCallbackImpl() = default;
48 
OnLoadPatchDone(int32_t resultCode,int32_t recordId)49     void OnLoadPatchDone(int32_t resultCode, int32_t recordId) override
50     {
51         HILOG_DEBUG("function called.");
52     }
53 
OnUnloadPatchDone(int32_t resultCode,int32_t recordId)54     void OnUnloadPatchDone(int32_t resultCode, int32_t recordId) override
55     {
56         HILOG_DEBUG("function called.");
57     }
58 
OnReloadPageDone(int32_t resultCode,int32_t recordId)59     void OnReloadPageDone(int32_t resultCode, int32_t recordId) override
60     {
61         HILOG_DEBUG("function called.");
62     }
63 };
64 
65 class AppMgrProxyTest : public testing::Test {
66 public:
67     static void SetUpTestCase();
68     static void TearDownTestCase();
69     void SetUp() override;
70     void TearDown() override;
71 
72     sptr<MockAppMgrService> mockAppMgrService_;
73     sptr<AppMgrProxy> appMgrProxy_;
74 };
75 
SetUpTestCase(void)76 void AppMgrProxyTest::SetUpTestCase(void)
77 {}
78 
TearDownTestCase(void)79 void AppMgrProxyTest::TearDownTestCase(void)
80 {}
81 
SetUp()82 void AppMgrProxyTest::SetUp()
83 {
84     GTEST_LOG_(INFO) << "AppMgrProxyTest::SetUp()";
85 
86     mockAppMgrService_ = new MockAppMgrService();
87     appMgrProxy_ = new AppMgrProxy(mockAppMgrService_);
88 }
89 
TearDown()90 void AppMgrProxyTest::TearDown()
91 {}
92 
93 /**
94  * @tc.name: AppMgrProxy_GetProcessRunningInfosByUserId_0100
95  * @tc.desc: GetProcessRunningInfosByUserId
96  * @tc.type: FUNC
97  * @tc.require: SR000GH1GO
98  */
99 HWTEST_F(AppMgrProxyTest, AppMgrProxy_GetProcessRunningInfosByUserId_0100, TestSize.Level0)
100 {
101     GTEST_LOG_(INFO) << "AppMgrProxy_GetProcessRunningInfosByUserId_0100 start";
102 
103     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
104         .Times(1)
105         .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
106 
107     std::vector<RunningProcessInfo> info;
108     appMgrProxy_->GetProcessRunningInfosByUserId(info, USER_ID);
109 
110     EXPECT_EQ(
111         mockAppMgrService_->code_, static_cast<uint32_t>(AppMgrInterfaceCode::APP_GET_RUNNING_PROCESSES_BY_USER_ID));
112 
113     GTEST_LOG_(INFO) << "AppMgrProxy_GetProcessRunningInfosByUserId_0100 end";
114 }
115 
116 /**
117  * @tc.name: AppMgrProxy_GetAllRenderProcesses_0100
118  * @tc.desc: GetAllRenderProcesses
119  * @tc.type: FUNC
120  */
121 HWTEST_F(AppMgrProxyTest, AppMgrProxy_GetAllRenderProcesses_0100, TestSize.Level0)
122 {
123     GTEST_LOG_(INFO) << "AppMgrProxy_GetAllRenderProcesses_0100 start";
124 
125     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
126         .Times(1)
127         .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
128 
129     std::vector<RenderProcessInfo> info;
130     appMgrProxy_->GetAllRenderProcesses(info);
131 
132     EXPECT_EQ(
133         mockAppMgrService_->code_, static_cast<uint32_t>(AppMgrInterfaceCode::APP_GET_ALL_RENDER_PROCESSES));
134 
135     GTEST_LOG_(INFO) << "AppMgrProxy_GetAllRenderProcesses_0100 end";
136 }
137 
138 /**
139  * @tc.name: GetAppRunningStateByBundleName_0100
140  * @tc.desc: Get app running state by bundle name.
141  * @tc.type: FUNC
142  * @tc.require: issueI581VW
143  */
144 HWTEST_F(AppMgrProxyTest, GetAppRunningStateByBundleName_0100, TestSize.Level0)
145 {
146     HILOG_INFO("%{public}s start.", __func__);
147 
148     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
149         .Times(1)
150         .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
151 
152     std::string bundleName = "testBundleName";
153     appMgrProxy_->GetAppRunningStateByBundleName(bundleName);
154 
155     EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(AppMgrInterfaceCode::GET_APP_RUNNING_STATE));
156 
157     HILOG_INFO("%{public}s end.", __func__);
158 }
159 
160 /**
161  * @tc.name: NotifyLoadRepairPatch_0100
162  * @tc.desc: Notify load repair patch.
163  * @tc.type: FUNC
164  * @tc.require: issueI581VW
165  */
166 HWTEST_F(AppMgrProxyTest, NotifyLoadRepairPatch_0100, TestSize.Level0)
167 {
168     HILOG_INFO("%{public}s start.", __func__);
169 
170     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
171         .Times(1)
172         .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
173 
174     std::string bundleName = "testBundleName";
175     sptr<IQuickFixCallback> callback = new QuickFixCallbackImpl();
176     appMgrProxy_->NotifyLoadRepairPatch(bundleName, callback);
177 
178     EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(AppMgrInterfaceCode::NOTIFY_LOAD_REPAIR_PATCH));
179 
180     HILOG_INFO("%{public}s end.", __func__);
181 }
182 
183 /**
184  * @tc.name: NotifyHotReloadPage_0100
185  * @tc.desc: Notify ace execute hot reload page.
186  * @tc.type: FUNC
187  * @tc.require: issueI581VW
188  */
189 HWTEST_F(AppMgrProxyTest, NotifyHotReloadPage_0100, TestSize.Level0)
190 {
191     HILOG_INFO("%{public}s start", __func__);
192 
193     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
194         .Times(1)
195         .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
196 
197     std::string bundleName = "testBundleName";
198     sptr<IQuickFixCallback> callback = new QuickFixCallbackImpl();
199     appMgrProxy_->NotifyHotReloadPage(bundleName, callback);
200 
201     EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(AppMgrInterfaceCode::NOTIFY_HOT_RELOAD_PAGE));
202 
203     HILOG_INFO("%{public}s end", __func__);
204 }
205 
206 /**
207  * @tc.name: NotifyUnLoadRepairPatch_0100
208  * @tc.desc: Notify unload repair patch.
209  * @tc.type: FUNC
210  * @tc.require: issueI581VW
211  */
212 HWTEST_F(AppMgrProxyTest, NotifyUnLoadRepairPatch_0100, TestSize.Level0)
213 {
214     HILOG_INFO("%{public}s start.", __func__);
215 
216     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
217         .Times(1)
218         .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
219 
220     std::string bundleName = "testBundleName";
221     sptr<IQuickFixCallback> callback = new QuickFixCallbackImpl();
222     appMgrProxy_->NotifyUnLoadRepairPatch(bundleName, callback);
223 
224     EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(AppMgrInterfaceCode::NOTIFY_UNLOAD_REPAIR_PATCH));
225 
226     HILOG_INFO("%{public}s end.", __func__);
227 }
228 
229 /**
230  * @tc.name: PreStartNWebSpawnProcess_001
231  * @tc.desc: prestart nwebspawn process.
232  * @tc.type: FUNC
233  * @tc.require: issueI5W4S7
234  */
235 HWTEST_F(AppMgrProxyTest, PreStartNWebSpawnProcess_001, TestSize.Level0)
236 {
237     HILOG_INFO("%{public}s start.", __func__);
238 
239     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
240         .Times(1)
241         .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
242 
243     appMgrProxy_->PreStartNWebSpawnProcess();
244     EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(AppMgrInterfaceCode::PRE_START_NWEBSPAWN_PROCESS));
245 
246     HILOG_INFO("%{public}s end.", __func__);
247 }
248 
249 /**
250  * @tc.name: GetProcessMemoryByPid_001
251  * @tc.desc: Get memorySize by pid.
252  * @tc.type: FUNC
253  * @tc.require: issueI76JBF
254  */
255 HWTEST_F(AppMgrProxyTest, GetProcessMemoryByPid_001, TestSize.Level0)
256 {
257     HILOG_INFO("%{public}s start.", __func__);
258 
259     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
260         .Times(1)
261         .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
262 
263     int32_t pid = 0;
264     int32_t memorySize = 0;
265     appMgrProxy_->GetProcessMemoryByPid(pid, memorySize);
266     EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(AppMgrInterfaceCode::GET_PROCESS_MEMORY_BY_PID));
267 
268     HILOG_INFO("%{public}s end.", __func__);
269 }
270 
271 /**
272  * @tc.name: GetRunningProcessInformation_001
273  * @tc.desc: Get application processes information list by bundleName.
274  * @tc.type: FUNC
275  * @tc.require: issueI76JBF
276  */
277 HWTEST_F(AppMgrProxyTest, GetRunningProcessInformation_001, TestSize.Level0)
278 {
279     HILOG_INFO("%{public}s start.", __func__);
280 
281     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
282         .Times(1)
283         .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
284 
285     std::string bundleName = "testBundleName";
286     int32_t userId = USER_ID;
287     std::vector<RunningProcessInfo> info;
288     appMgrProxy_->GetRunningProcessInformation(bundleName, userId, info);
289     EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(AppMgrInterfaceCode::GET_PIDS_BY_BUNDLENAME));
290 
291     HILOG_INFO("%{public}s end.", __func__);
292 }
293 
294 /**
295  * @tc.name: NotifyAppFault_001
296  * @tc.desc: Notify app fault.
297  * @tc.type: FUNC
298  * @tc.require: issueI79RY8
299  */
300 HWTEST_F(AppMgrProxyTest, NotifyAppFault_001, TestSize.Level1)
301 {
302     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
303         .Times(1)
304         .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
305     FaultData faultData;
306     appMgrProxy_->NotifyAppFault(faultData);
307     EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(AppMgrInterfaceCode::NOTIFY_APP_FAULT));
308 }
309 
310 /**
311  * @tc.name: NotifyAppFaultBySA_001
312  * @tc.desc: Notify app fault by SA.
313  * @tc.type: FUNC
314  * @tc.require: issueI79RY8
315  */
316 HWTEST_F(AppMgrProxyTest, NotifyAppFaultBySA_001, TestSize.Level1)
317 {
318     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
319         .Times(1)
320         .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
321     AppFaultDataBySA faultData;
322     appMgrProxy_->NotifyAppFaultBySA(faultData);
323     EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(AppMgrInterfaceCode::NOTIFY_APP_FAULT_BY_SA));
324 }
325 
326 /**
327  * @tc.name: ChangeAppGcState_001
328  * @tc.desc: Change app Gc state.
329  * @tc.type: FUNC
330  * @tc.require: issuesI85VVU
331  */
332 HWTEST_F(AppMgrProxyTest, ChangeAppGcState_001, TestSize.Level1)
333 {
334     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
335         .Times(1)
336         .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
337     int32_t pid = 0;
338     int32_t state = 0;
339     appMgrProxy_->ChangeAppGcState(pid, state);
340     EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(AppMgrInterfaceCode::CHANGE_APP_GC_STATE));
341 }
342 
343 /**
344  * @tc.name: IsApplicationRunning_001
345  * @tc.desc: Send request to query the running status of the application.
346  * @tc.type: FUNC
347  */
348 HWTEST_F(AppMgrProxyTest, IsApplicationRunning_001, TestSize.Level1)
349 {
350     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
351         .Times(1)
352         .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
353 
354     std::string bundleName = "testBundleName";
355     bool isRunning = false;
356     appMgrProxy_->IsApplicationRunning(bundleName, isRunning);
357     EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(AppMgrInterfaceCode::IS_APPLICATION_RUNNING));
358 }
359 
360 /**
361  * @tc.number: RegisterAbilityForegroundStateObserver_0100
362  * @tc.desc: Verify that the RegisterAbilityForegroundStateObserver function is called normally.
363  * @tc.type: FUNC
364  */
365 HWTEST_F(AppMgrProxyTest, RegisterAbilityForegroundStateObserver_0100, TestSize.Level1)
366 {
367     sptr<IAbilityForegroundStateObserver> observer = new MockAbilityForegroundStateObserverStub();
368     EXPECT_NE(observer->AsObject(), nullptr);
369     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
370         .Times(1)
371         .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
372     appMgrProxy_->RegisterAbilityForegroundStateObserver(observer);
373     EXPECT_EQ(mockAppMgrService_->code_,
374         static_cast<uint32_t>(AppMgrInterfaceCode::REGISTER_ABILITY_FOREGROUND_STATE_OBSERVER));
375 }
376 
377 /**
378  * @tc.number: RegisterAbilityForegroundStateObserver_0200
379  * @tc.desc: Verify that the RegisterAbilityForegroundStateObserver parameter of the function is null.
380  * @tc.type: FUNC
381  */
382 HWTEST_F(AppMgrProxyTest, RegisterAbilityForegroundStateObserver_0200, TestSize.Level1)
383 {
384     sptr<IAbilityForegroundStateObserver> observer = nullptr;
385     auto result = appMgrProxy_->RegisterAbilityForegroundStateObserver(observer);
386     EXPECT_EQ(result, OHOS::ERR_INVALID_VALUE);
387 }
388 
389 /**
390  * @tc.number: UnregisterAbilityForegroundStateObserver_0100
391  * @tc.desc: Verify that the UnregisterAbilityForegroundStateObserver function is called normally.
392  * @tc.type: FUNC
393  */
394 HWTEST_F(AppMgrProxyTest, UnregisterAbilityForegroundStateObserver_0100, TestSize.Level1)
395 {
396     sptr<IAbilityForegroundStateObserver> observer = new MockAbilityForegroundStateObserverStub();
397     EXPECT_NE(observer->AsObject(), nullptr);
398     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
399         .Times(1)
400         .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
401     appMgrProxy_->UnregisterAbilityForegroundStateObserver(observer);
402     EXPECT_EQ(mockAppMgrService_->code_,
403         static_cast<uint32_t>(AppMgrInterfaceCode::UNREGISTER_ABILITY_FOREGROUND_STATE_OBSERVER));
404 }
405 
406 /**
407  * @tc.number: RegisterAbilityForegroundStateObserver_0200
408  * @tc.desc: Verify that the UnregisterAbilityForegroundStateObserver parameter of the function is null.
409  * @tc.type: FUNC
410  */
411 HWTEST_F(AppMgrProxyTest, UnregisterAbilityForegroundStateObserver_0200, TestSize.Level1)
412 {
413     sptr<IAbilityForegroundStateObserver> observer = nullptr;
414     auto result = appMgrProxy_->UnregisterAbilityForegroundStateObserver(observer);
415     EXPECT_EQ(result, OHOS::ERR_INVALID_VALUE);
416 }
417 
418 /**
419  * @tc.name: RegisterAppForegroundStateObserver_0100
420  * @tc.desc: Test when all condition not met.
421  * @tc.type: FUNC
422  */
423 HWTEST_F(AppMgrProxyTest, RegisterAppForegroundStateObserver_0100, TestSize.Level1)
424 {
425     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _)).Times(1);
426     sptr<IAppForegroundStateObserver> observer = new (std::nothrow) AppForegroundStateObserverMock();
427     auto res = appMgrProxy_->RegisterAppForegroundStateObserver(observer);
428     EXPECT_EQ(res, NO_ERROR);
429 }
430 
431 /**
432  * @tc.name: UnregisterAppForegroundStateObserver_0100
433  * @tc.desc: Test when all condition not met.
434  * @tc.type: FUNC
435  */
436 HWTEST_F(AppMgrProxyTest, UnregisterAppForegroundStateObserver_0100, TestSize.Level1)
437 {
438     EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _)).Times(1);
439     sptr<IAppForegroundStateObserver> observer = new (std::nothrow) AppForegroundStateObserverMock();
440     auto res = appMgrProxy_->RegisterAppForegroundStateObserver(observer);
441     EXPECT_EQ(res, NO_ERROR);
442 }
443 } // namespace AppExecFwk
444 } // namespace OHOS
445