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