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 "mock_app_mgr_service.h"
19 #include "app_mgr_proxy.h"
20 #include "hilog_wrapper.h"
21 #include "quick_fix_callback_stub.h"
22
23 using namespace testing;
24 using namespace testing::ext;
25
26 namespace OHOS {
27 namespace AppExecFwk {
28 namespace {
29 const int32_t USER_ID = 100;
30 } // namespace
31
32 class QuickFixCallbackImpl : public AppExecFwk::QuickFixCallbackStub {
33 public:
34 QuickFixCallbackImpl() = default;
35 virtual ~QuickFixCallbackImpl() = default;
36
OnLoadPatchDone(int32_t resultCode,int32_t recordId)37 void OnLoadPatchDone(int32_t resultCode, int32_t recordId) override
38 {
39 HILOG_DEBUG("function called.");
40 }
41
OnUnloadPatchDone(int32_t resultCode,int32_t recordId)42 void OnUnloadPatchDone(int32_t resultCode, int32_t recordId) override
43 {
44 HILOG_DEBUG("function called.");
45 }
46
OnReloadPageDone(int32_t resultCode,int32_t recordId)47 void OnReloadPageDone(int32_t resultCode, int32_t recordId) override
48 {
49 HILOG_DEBUG("function called.");
50 }
51 };
52
53 class AppMgrProxyTest : public testing::Test {
54 public:
55 static void SetUpTestCase();
56 static void TearDownTestCase();
57 void SetUp() override;
58 void TearDown() override;
59
60 sptr<MockAppMgrService> mockAppMgrService_;
61 sptr<AppMgrProxy> appMgrProxy_;
62 };
63
SetUpTestCase(void)64 void AppMgrProxyTest::SetUpTestCase(void)
65 {}
66
TearDownTestCase(void)67 void AppMgrProxyTest::TearDownTestCase(void)
68 {}
69
SetUp()70 void AppMgrProxyTest::SetUp()
71 {
72 GTEST_LOG_(INFO) << "AppMgrProxyTest::SetUp()";
73
74 mockAppMgrService_ = new MockAppMgrService();
75 appMgrProxy_ = new AppMgrProxy(mockAppMgrService_);
76 }
77
TearDown()78 void AppMgrProxyTest::TearDown()
79 {}
80
81 /**
82 * @tc.name: AppMgrProxy_GetProcessRunningInfosByUserId_0100
83 * @tc.desc: GetProcessRunningInfosByUserId
84 * @tc.type: FUNC
85 * @tc.require: SR000GH1GO
86 */
87 HWTEST_F(AppMgrProxyTest, AppMgrProxy_GetProcessRunningInfosByUserId_0100, TestSize.Level0)
88 {
89 GTEST_LOG_(INFO) << "AppMgrProxy_GetProcessRunningInfosByUserId_0100 start";
90
91 EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
92 .Times(1)
93 .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
94
95 std::vector<RunningProcessInfo> info;
96 appMgrProxy_->GetProcessRunningInfosByUserId(info, USER_ID);
97
98 EXPECT_EQ(
99 mockAppMgrService_->code_, static_cast<uint32_t>(AppMgrInterfaceCode::APP_GET_RUNNING_PROCESSES_BY_USER_ID));
100
101 GTEST_LOG_(INFO) << "AppMgrProxy_GetProcessRunningInfosByUserId_0100 end";
102 }
103
104 /**
105 * @tc.name: AppMgrProxy_GetAllRenderProcesses_0100
106 * @tc.desc: GetAllRenderProcesses
107 * @tc.type: FUNC
108 */
109 HWTEST_F(AppMgrProxyTest, AppMgrProxy_GetAllRenderProcesses_0100, TestSize.Level0)
110 {
111 GTEST_LOG_(INFO) << "AppMgrProxy_GetAllRenderProcesses_0100 start";
112
113 EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
114 .Times(1)
115 .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
116
117 std::vector<RenderProcessInfo> info;
118 appMgrProxy_->GetAllRenderProcesses(info);
119
120 EXPECT_EQ(
121 mockAppMgrService_->code_, static_cast<uint32_t>(AppMgrInterfaceCode::APP_GET_ALL_RENDER_PROCESSES));
122
123 GTEST_LOG_(INFO) << "AppMgrProxy_GetAllRenderProcesses_0100 end";
124 }
125
126 /**
127 * @tc.name: GetAppRunningStateByBundleName_0100
128 * @tc.desc: Get app running state by bundle name.
129 * @tc.type: FUNC
130 * @tc.require: issueI581VW
131 */
132 HWTEST_F(AppMgrProxyTest, GetAppRunningStateByBundleName_0100, TestSize.Level0)
133 {
134 HILOG_INFO("%{public}s start.", __func__);
135
136 EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
137 .Times(1)
138 .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
139
140 std::string bundleName = "testBundleName";
141 appMgrProxy_->GetAppRunningStateByBundleName(bundleName);
142
143 EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(AppMgrInterfaceCode::GET_APP_RUNNING_STATE));
144
145 HILOG_INFO("%{public}s end.", __func__);
146 }
147
148 /**
149 * @tc.name: NotifyLoadRepairPatch_0100
150 * @tc.desc: Notify load repair patch.
151 * @tc.type: FUNC
152 * @tc.require: issueI581VW
153 */
154 HWTEST_F(AppMgrProxyTest, NotifyLoadRepairPatch_0100, TestSize.Level0)
155 {
156 HILOG_INFO("%{public}s start.", __func__);
157
158 EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
159 .Times(1)
160 .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
161
162 std::string bundleName = "testBundleName";
163 sptr<IQuickFixCallback> callback = new QuickFixCallbackImpl();
164 appMgrProxy_->NotifyLoadRepairPatch(bundleName, callback);
165
166 EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(AppMgrInterfaceCode::NOTIFY_LOAD_REPAIR_PATCH));
167
168 HILOG_INFO("%{public}s end.", __func__);
169 }
170
171 /**
172 * @tc.name: NotifyHotReloadPage_0100
173 * @tc.desc: Notify ace execute hot reload page.
174 * @tc.type: FUNC
175 * @tc.require: issueI581VW
176 */
177 HWTEST_F(AppMgrProxyTest, NotifyHotReloadPage_0100, TestSize.Level0)
178 {
179 HILOG_INFO("%{public}s start", __func__);
180
181 EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
182 .Times(1)
183 .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
184
185 std::string bundleName = "testBundleName";
186 sptr<IQuickFixCallback> callback = new QuickFixCallbackImpl();
187 appMgrProxy_->NotifyHotReloadPage(bundleName, callback);
188
189 EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(AppMgrInterfaceCode::NOTIFY_HOT_RELOAD_PAGE));
190
191 HILOG_INFO("%{public}s end", __func__);
192 }
193
194 /**
195 * @tc.name: NotifyUnLoadRepairPatch_0100
196 * @tc.desc: Notify unload repair patch.
197 * @tc.type: FUNC
198 * @tc.require: issueI581VW
199 */
200 HWTEST_F(AppMgrProxyTest, NotifyUnLoadRepairPatch_0100, TestSize.Level0)
201 {
202 HILOG_INFO("%{public}s start.", __func__);
203
204 EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
205 .Times(1)
206 .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
207
208 std::string bundleName = "testBundleName";
209 sptr<IQuickFixCallback> callback = new QuickFixCallbackImpl();
210 appMgrProxy_->NotifyUnLoadRepairPatch(bundleName, callback);
211
212 EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(AppMgrInterfaceCode::NOTIFY_UNLOAD_REPAIR_PATCH));
213
214 HILOG_INFO("%{public}s end.", __func__);
215 }
216
217 /**
218 * @tc.name: PreStartNWebSpawnProcess_001
219 * @tc.desc: prestart nwebspawn process.
220 * @tc.type: FUNC
221 * @tc.require: issueI5W4S7
222 */
223 HWTEST_F(AppMgrProxyTest, PreStartNWebSpawnProcess_001, TestSize.Level0)
224 {
225 HILOG_INFO("%{public}s start.", __func__);
226
227 EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
228 .Times(1)
229 .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
230
231 appMgrProxy_->PreStartNWebSpawnProcess();
232 EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(AppMgrInterfaceCode::PRE_START_NWEBSPAWN_PROCESS));
233
234 HILOG_INFO("%{public}s end.", __func__);
235 }
236
237 /**
238 * @tc.name: GetProcessMemoryByPid_001
239 * @tc.desc: Get memorySize by pid.
240 * @tc.type: FUNC
241 * @tc.require: issueI76JBF
242 */
243 HWTEST_F(AppMgrProxyTest, GetProcessMemoryByPid_001, TestSize.Level0)
244 {
245 HILOG_INFO("%{public}s start.", __func__);
246
247 EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
248 .Times(1)
249 .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
250
251 int32_t pid = 0;
252 int32_t memorySize = 0;
253 appMgrProxy_->GetProcessMemoryByPid(pid, memorySize);
254 EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(IAppMgr::Message::GET_PROCESS_MEMORY_BY_PID));
255
256 HILOG_INFO("%{public}s end.", __func__);
257 }
258
259 /**
260 * @tc.name: GetRunningProcessInformation_001
261 * @tc.desc: Get application processes information list by bundleName.
262 * @tc.type: FUNC
263 * @tc.require: issueI76JBF
264 */
265 HWTEST_F(AppMgrProxyTest, GetRunningProcessInformation_001, TestSize.Level0)
266 {
267 HILOG_INFO("%{public}s start.", __func__);
268
269 EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
270 .Times(1)
271 .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
272
273 std::string bundleName = "testBundleName";
274 int32_t userId = USER_ID;
275 std::vector<RunningProcessInfo> info;
276 appMgrProxy_->GetRunningProcessInformation(bundleName, userId, info);
277 EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(IAppMgr::Message::GET_PIDS_BY_BUNDLENAME));
278
279 HILOG_INFO("%{public}s end.", __func__);
280 }
281
282 /**
283 * @tc.name: NotifyAppFault_001
284 * @tc.desc: Notify app fault.
285 * @tc.type: FUNC
286 * @tc.require: issueI79RY8
287 */
288 HWTEST_F(AppMgrProxyTest, NotifyAppFault_001, TestSize.Level1)
289 {
290 EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
291 .Times(1)
292 .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
293 FaultData faultData;
294 appMgrProxy_->NotifyAppFault(faultData);
295 EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(IAppMgr::Message::NOTIFY_APP_FAULT));
296 }
297
298 /**
299 * @tc.name: NotifyAppFaultBySA_001
300 * @tc.desc: Notify app fault by SA.
301 * @tc.type: FUNC
302 * @tc.require: issueI79RY8
303 */
304 HWTEST_F(AppMgrProxyTest, NotifyAppFaultBySA_001, TestSize.Level1)
305 {
306 EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
307 .Times(1)
308 .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
309 AppFaultDataBySA faultData;
310 appMgrProxy_->NotifyAppFaultBySA(faultData);
311 EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(IAppMgr::Message::NOTIFY_APP_FAULT_BY_SA));
312 }
313 } // namespace AppExecFwk
314 } // namespace OHOS
315