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 #include <gtest/gtest.h>
17 #include "ability_scheduler_proxy.h"
18 #include "ability_scheduler_stub.h"
19 #include "ability_scheduler_mock.h"
20 #include "pac_map.h"
21
22 using namespace testing::ext;
23 using namespace testing;
24
25 namespace OHOS {
26 namespace AAFwk {
27 class AbilitySchedulerProxyTest : public testing::Test {
28 public:
29 static void SetUpTestCase(void);
30 static void TearDownTestCase(void);
31 void SetUp();
32 void TearDown();
33
34 sptr<AbilitySchedulerProxy> abilitySchedulerProxy_ {nullptr};
35 sptr<AbilitySchedulerMock> mock_ {nullptr};
36 sptr<AbilitySchedulerRecipient> abilitySchedulerRecipient_ {nullptr};
37 };
38
SetUpTestCase(void)39 void AbilitySchedulerProxyTest::SetUpTestCase(void)
40 {}
TearDownTestCase(void)41 void AbilitySchedulerProxyTest::TearDownTestCase(void)
42 {}
TearDown(void)43 void AbilitySchedulerProxyTest::TearDown(void)
44 {}
45
SetUp(void)46 void AbilitySchedulerProxyTest::SetUp(void)
47 {
48 mock_ = new AbilitySchedulerMock();
49 abilitySchedulerProxy_ = new AbilitySchedulerProxy(mock_);
50 OHOS::AAFwk::AbilitySchedulerRecipient::RemoteDiedHandler callbake;
51 abilitySchedulerRecipient_ = new AbilitySchedulerRecipient(callbake);
52 }
53
54 /*
55 * Feature: AbilitySchedulerProxy
56 * Function: AbilitySchedulerProxy
57 * SubFunction: NA
58 * FunctionPoints: NA
59 * EnvConditions:NA
60 * CaseDescription: verify AbilitySchedulerProxy is create success
61 */
62 HWTEST_F(AbilitySchedulerProxyTest, ability_scheduler_proxy_operating_001, TestSize.Level1)
63 {
64 EXPECT_NE(abilitySchedulerProxy_, nullptr);
65 }
66
67 /*
68 * Feature: AbilitySchedulerProxy
69 * Function: AbilitySchedulerRecipient
70 * SubFunction: NA
71 * FunctionPoints: NA
72 * EnvConditions:NA
73 * CaseDescription: verify AbilitySchedulerRecipient is create success
74 */
75 HWTEST_F(AbilitySchedulerProxyTest, ability_scheduler_proxy_operating_002, TestSize.Level1)
76 {
77 EXPECT_NE(abilitySchedulerRecipient_, nullptr);
78 }
79
80 /*
81 * Feature: AbilitySchedulerProxy
82 * Function: ScheduleAbilityTransaction
83 * SubFunction: NA
84 * FunctionPoints: AbilitySchedulerProxy ScheduleAbilityTransaction
85 * EnvConditions: NA
86 * CaseDescription: verify ScheduleAbilityTransaction Normal case
87 */
88 HWTEST_F(AbilitySchedulerProxyTest, ability_scheduler_proxy_operating_003, TestSize.Level1)
89 {
90 EXPECT_CALL(*mock_, SendRequest(_, _, _, _))
91 .Times(1)
92 .WillOnce(Invoke(mock_.GetRefPtr(), &AbilitySchedulerMock::InvokeSendRequest));
93 Want want;
94 want.SetFlags(10);
95 LifeCycleStateInfo info;
96 abilitySchedulerProxy_->ScheduleAbilityTransaction(want, info);
97
98 EXPECT_EQ(IAbilityScheduler::SCHEDULE_ABILITY_TRANSACTION, mock_->code_);
99 }
100
101 /*
102 * Feature: AbilitySchedulerProxy
103 * Function: ScheduleAbilityTransaction
104 * SubFunction: NA
105 * FunctionPoints: AbilitySchedulerProxy ScheduleAbilityTransaction
106 * EnvConditions: NA
107 * CaseDescription: verify ScheduleAbilityTransaction Return value exception
108 */
109 HWTEST_F(AbilitySchedulerProxyTest, ability_scheduler_proxy_operating_004, TestSize.Level1)
110 {
111 EXPECT_CALL(*mock_, SendRequest(_, _, _, _))
112 .Times(1)
113 .WillOnce(Invoke(mock_.GetRefPtr(), &AbilitySchedulerMock::InvokeErrorSendRequest));
114 Want want;
115 want.SetFlags(10);
116 LifeCycleStateInfo info;
117 abilitySchedulerProxy_->ScheduleAbilityTransaction(want, info);
118
119 EXPECT_EQ(IAbilityScheduler::SCHEDULE_ABILITY_TRANSACTION, mock_->code_);
120 }
121
122 /*
123 * Feature: AbilitySchedulerProxy
124 * Function: SendResult
125 * SubFunction: NA
126 * FunctionPoints: AbilitySchedulerProxy SendResult
127 * EnvConditions: NA
128 * CaseDescription: verify SendResult Normal case
129 */
130 HWTEST_F(AbilitySchedulerProxyTest, ability_scheduler_proxy_operating_005, TestSize.Level1)
131 {
132 EXPECT_CALL(*mock_, SendRequest(_, _, _, _))
133 .Times(1)
134 .WillOnce(Invoke(mock_.GetRefPtr(), &AbilitySchedulerMock::InvokeSendRequest));
135 Want want;
136 want.SetFlags(10);
137 abilitySchedulerProxy_->SendResult(9, -1, want);
138
139 EXPECT_EQ(IAbilityScheduler::SEND_RESULT, mock_->code_);
140 }
141
142 /*
143 * Feature: AbilitySchedulerProxy
144 * Function: SendResult
145 * SubFunction: NA
146 * FunctionPoints: AbilitySchedulerProxy SendResult
147 * EnvConditions: NA
148 * CaseDescription: verify SendResult Return value exception
149 */
150 HWTEST_F(AbilitySchedulerProxyTest, ability_scheduler_proxy_operating_006, TestSize.Level1)
151 {
152 EXPECT_CALL(*mock_, SendRequest(_, _, _, _))
153 .Times(1)
154 .WillOnce(Invoke(mock_.GetRefPtr(), &AbilitySchedulerMock::InvokeErrorSendRequest));
155 Want want;
156 want.SetFlags(10);
157 abilitySchedulerProxy_->SendResult(9, -1, want);
158
159 EXPECT_EQ(IAbilityScheduler::SEND_RESULT, mock_->code_);
160 }
161
162 /*
163 * Feature: AbilitySchedulerProxy
164 * Function: ScheduleConnectAbility
165 * SubFunction: NA
166 * FunctionPoints: AbilitySchedulerProxy ScheduleConnectAbility
167 * EnvConditions: NA
168 * CaseDescription: verify ScheduleConnectAbility Normal case
169 */
170 HWTEST_F(AbilitySchedulerProxyTest, ability_scheduler_proxy_operating_007, TestSize.Level1)
171 {
172 EXPECT_CALL(*mock_, SendRequest(_, _, _, _))
173 .Times(1)
174 .WillOnce(Invoke(mock_.GetRefPtr(), &AbilitySchedulerMock::InvokeSendRequest));
175 Want want;
176 want.SetFlags(10);
177 abilitySchedulerProxy_->ScheduleConnectAbility(want);
178
179 EXPECT_EQ(IAbilityScheduler::SCHEDULE_ABILITY_CONNECT, mock_->code_);
180 }
181
182 /*
183 * Feature: AbilitySchedulerProxy
184 * Function: ScheduleConnectAbility
185 * SubFunction: NA
186 * FunctionPoints: AbilitySchedulerProxy ScheduleConnectAbility
187 * EnvConditions: NA
188 * CaseDescription: verify ScheduleConnectAbility Return value exception
189 */
190 HWTEST_F(AbilitySchedulerProxyTest, ability_scheduler_proxy_operating_008, TestSize.Level1)
191 {
192 EXPECT_CALL(*mock_, SendRequest(_, _, _, _))
193 .Times(1)
194 .WillOnce(Invoke(mock_.GetRefPtr(), &AbilitySchedulerMock::InvokeErrorSendRequest));
195 Want want;
196 want.SetFlags(10);
197 abilitySchedulerProxy_->ScheduleConnectAbility(want);
198
199 EXPECT_EQ(IAbilityScheduler::SCHEDULE_ABILITY_CONNECT, mock_->code_);
200 }
201
202 /*
203 * Feature: AbilitySchedulerProxy
204 * Function: ScheduleDisconnectAbility
205 * SubFunction: NA
206 * FunctionPoints: AbilitySchedulerProxy ScheduleDisconnectAbility
207 * EnvConditions: NA
208 * CaseDescription: verify ScheduleDisconnectAbility Normal case
209 */
210 HWTEST_F(AbilitySchedulerProxyTest, ability_scheduler_proxy_operating_009, TestSize.Level1)
211 {
212 EXPECT_CALL(*mock_, SendRequest(_, _, _, _))
213 .Times(1)
214 .WillOnce(Invoke(mock_.GetRefPtr(), &AbilitySchedulerMock::InvokeSendRequest));
215 Want want;
216 want.SetFlags(10);
217 abilitySchedulerProxy_->ScheduleDisconnectAbility(want);
218
219 EXPECT_EQ(IAbilityScheduler::SCHEDULE_ABILITY_DISCONNECT, mock_->code_);
220 }
221
222 /*
223 * Feature: AbilitySchedulerProxy
224 * Function: ScheduleDisconnectAbility
225 * SubFunction: NA
226 * FunctionPoints: AbilitySchedulerProxy ScheduleDisconnectAbility
227 * EnvConditions: NA
228 * CaseDescription: verify ScheduleDisconnectAbility Return value exception
229 */
230 HWTEST_F(AbilitySchedulerProxyTest, ability_scheduler_proxy_operating_010, TestSize.Level1)
231 {
232 EXPECT_CALL(*mock_, SendRequest(_, _, _, _))
233 .Times(1)
234 .WillOnce(Invoke(mock_.GetRefPtr(), &AbilitySchedulerMock::InvokeErrorSendRequest));
235 Want want;
236 want.SetFlags(10);
237 abilitySchedulerProxy_->ScheduleDisconnectAbility(want);
238
239 EXPECT_EQ(IAbilityScheduler::SCHEDULE_ABILITY_DISCONNECT, mock_->code_);
240 }
241
242 /*
243 * Feature: AbilitySchedulerProxy
244 * Function: ScheduleCommandAbility
245 * SubFunction: NA
246 * FunctionPoints: AbilitySchedulerProxy ScheduleCommandAbility
247 * EnvConditions: NA
248 * CaseDescription: verify ScheduleCommandAbility Normal case
249 */
250 HWTEST_F(AbilitySchedulerProxyTest, ability_scheduler_proxy_operating_011, TestSize.Level1)
251 {
252 EXPECT_CALL(*mock_, SendRequest(_, _, _, _))
253 .Times(1)
254 .WillOnce(Invoke(mock_.GetRefPtr(), &AbilitySchedulerMock::InvokeSendRequest));
255 Want want;
256 want.SetFlags(10);
257 abilitySchedulerProxy_->ScheduleCommandAbility(want, false, 1);
258
259 EXPECT_EQ(IAbilityScheduler::SCHEDULE_ABILITY_COMMAND, mock_->code_);
260 }
261
262 /*
263 * Feature: AbilitySchedulerProxy
264 * Function: ScheduleCommandAbility
265 * SubFunction: NA
266 * FunctionPoints: AbilitySchedulerProxy ScheduleCommandAbility
267 * EnvConditions: NA
268 * CaseDescription: verify ScheduleCommandAbility Return value exception
269 */
270 HWTEST_F(AbilitySchedulerProxyTest, ability_scheduler_proxy_operating_012, TestSize.Level1)
271 {
272 EXPECT_CALL(*mock_, SendRequest(_, _, _, _))
273 .Times(1)
274 .WillOnce(Invoke(mock_.GetRefPtr(), &AbilitySchedulerMock::InvokeErrorSendRequest));
275 Want want;
276 want.SetFlags(10);
277 abilitySchedulerProxy_->ScheduleCommandAbility(want, false, 1);
278
279 EXPECT_EQ(IAbilityScheduler::SCHEDULE_ABILITY_COMMAND, mock_->code_);
280 }
281
282 /**
283 * @tc.name: ability_scheduler_proxy_operating_013
284 * @tc.desc: test NotifyContinuationResult
285 * @tc.type: FUNC
286 * @tc.require: AR000GI8IJ
287 */
288 HWTEST_F(AbilitySchedulerProxyTest, ability_scheduler_proxy_operating_013, TestSize.Level0)
289 {
290 EXPECT_CALL(*mock_, SendRequest(_, _, _, _))
291 .Times(1)
292 .WillOnce(Invoke(mock_.GetRefPtr(), &AbilitySchedulerMock::InvokeSendRequest));
293 int32_t result = 0;
294 abilitySchedulerProxy_->NotifyContinuationResult(result);
295
296 EXPECT_EQ(IAbilityScheduler::NOTIFY_CONTINUATION_RESULT, mock_->code_);
297 }
298
299 /**
300 * @tc.name: ability_scheduler_proxy_operating_014
301 * @tc.desc: test CallRequest
302 * @tc.type: FUNC
303 * @tc.require:
304 */
305 HWTEST_F(AbilitySchedulerProxyTest, ability_scheduler_proxy_operating_014, TestSize.Level0)
306 {
307 EXPECT_CALL(*mock_, SendRequest(_, _, _, _))
308 .Times(1)
309 .WillOnce(Invoke(mock_.GetRefPtr(), &AbilitySchedulerMock::InvokeSendRequest));
310 abilitySchedulerProxy_->CallRequest();
311
312 EXPECT_EQ(IAbilityScheduler::REQUEST_CALL_REMOTE, mock_->code_);
313 }
314
315 /**
316 * @tc.name: ability_scheduler_proxy_operating_015
317 * @tc.desc: test CallRequest
318 * @tc.type: FUNC
319 * @tc.require:
320 */
321 HWTEST_F(AbilitySchedulerProxyTest, ability_scheduler_proxy_operating_015, TestSize.Level0)
322 {
323 EXPECT_CALL(*mock_, SendRequest(_, _, _, _))
324 .Times(1)
325 .WillOnce(Invoke(mock_.GetRefPtr(), &AbilitySchedulerMock::InvokeErrorSendRequest));
326 abilitySchedulerProxy_->CallRequest();
327
328 EXPECT_EQ(IAbilityScheduler::REQUEST_CALL_REMOTE, mock_->code_);
329 }
330 /**
331 * @tc.name: ability_scheduler_proxy_operating_014
332 * @tc.desc: test DumpAbilityInfo
333 * @tc.type: FUNC
334 * @tc.require: SR000GH1GO
335 */
336 HWTEST_F(AbilitySchedulerProxyTest, ability_scheduler_proxy_operating_016, TestSize.Level1)
337 {
338 EXPECT_CALL(*mock_, SendRequest(_, _, _, _))
339 .Times(1)
340 .WillOnce(Invoke(mock_.GetRefPtr(), &AbilitySchedulerMock::InvokeSendRequest));
341
342 std::vector<std::string> params;
343 std::vector<std::string> info;
344 abilitySchedulerProxy_->DumpAbilityInfo(params, info);
345
346 EXPECT_EQ(IAbilityScheduler::DUMP_ABILITY_RUNNER_INNER, mock_->code_);
347 }
348
349 /*
350 * Feature: AbilitySchedulerProxy
351 * Function: ScheduleSaveAbilityState
352 * SubFunction: NA
353 * FunctionPoints: AbilitySchedulerProxy ScheduleSaveAbilityState
354 * EnvConditions: NA
355 * CaseDescription: verify ScheduleSaveAbilityState Normal case
356 */
357 HWTEST_F(AbilitySchedulerProxyTest, ability_scheduler_proxy_operating_017, TestSize.Level1)
358 {
359 EXPECT_CALL(*mock_, SendRequest(_, _, _, _))
360 .Times(1)
361 .WillOnce(Invoke(mock_.GetRefPtr(), &AbilitySchedulerMock::InvokeSendRequest));
362 abilitySchedulerProxy_->ScheduleSaveAbilityState();
363
364 EXPECT_EQ(IAbilityScheduler::SCHEDULE_SAVE_ABILITY_STATE, mock_->code_);
365 }
366
367 /*
368 * Feature: AbilitySchedulerProxy
369 * Function: ScheduleRestoreAbilityState
370 * SubFunction: NA
371 * FunctionPoints: AbilitySchedulerProxy ScheduleRestoreAbilityState
372 * EnvConditions: NA
373 * CaseDescription: verify ScheduleRestoreAbilityState Normal case
374 */
375 HWTEST_F(AbilitySchedulerProxyTest, ability_scheduler_proxy_operating_018, TestSize.Level1)
376 {
377 EXPECT_CALL(*mock_, SendRequest(_, _, _, _))
378 .Times(1)
379 .WillOnce(Invoke(mock_.GetRefPtr(), &AbilitySchedulerMock::InvokeSendRequest));
380 PacMap pacmap;
381 abilitySchedulerProxy_->ScheduleRestoreAbilityState(pacmap);
382
383 EXPECT_EQ(IAbilityScheduler::SCHEDULE_RESTORE_ABILITY_STATE, mock_->code_);
384 }
385
386 /*
387 * Feature: AbilitySchedulerProxy
388 * Function: Call
389 * SubFunction: NA
390 * FunctionPoints: AbilitySchedulerProxy Call
391 * EnvConditions: NA
392 * CaseDescription: verify Call Normal case
393 */
394 HWTEST_F(AbilitySchedulerProxyTest, ability_scheduler_proxy_operating_019, TestSize.Level1)
395 {
396 EXPECT_CALL(*mock_, SendRequest(_, _, _, _))
397 .Times(1)
398 .WillOnce(Invoke(mock_.GetRefPtr(), &AbilitySchedulerMock::InvokeSendRequest));
399 Uri uri("nullptr");
400 PacMap pacmap;
401 abilitySchedulerProxy_->Call(uri, "", "", pacmap);
402
403 EXPECT_EQ(IAbilityScheduler::SCHEDULE_CALL, mock_->code_);
404 }
405
406 /*
407 * Feature: AbilitySchedulerProxy
408 * Function: ScheduleNotifyChange
409 * SubFunction: NA
410 * FunctionPoints: AbilitySchedulerProxy ScheduleNotifyChange
411 * EnvConditions: NA
412 * CaseDescription: verify ScheduleNotifyChange Return value exception
413 */
414 HWTEST_F(AbilitySchedulerProxyTest, ability_scheduler_proxy_operating_020, TestSize.Level1)
415 {
416 EXPECT_CALL(*mock_, SendRequest(_, _, _, _))
417 .Times(1)
418 .WillOnce(Invoke(mock_.GetRefPtr(), &AbilitySchedulerMock::InvokeSendRequest));
419 Uri uri("");
420
421 abilitySchedulerProxy_->ScheduleNotifyChange(uri);
422 EXPECT_EQ(IAbilityScheduler::SCHEDULE_NOTIFYCHANGE, mock_->code_);
423 }
424
425 /*
426 * Feature: AbilitySchedulerProxy
427 * Function: ExecuteBatch
428 * SubFunction: NA
429 * FunctionPoints: AbilitySchedulerProxy ExecuteBatch
430 * EnvConditions: NA
431 * CaseDescription: verify ExecuteBatch Return value exception
432 */
433 HWTEST_F(AbilitySchedulerProxyTest, ability_scheduler_proxy_operating_021, TestSize.Level1)
434 {
435 EXPECT_CALL(*mock_, SendRequest(_, _, _, _))
436 .Times(1)
437 .WillOnce(Invoke(mock_.GetRefPtr(), &AbilitySchedulerMock::InvokeSendRequest));
438 std::vector<std::shared_ptr<AppExecFwk::DataAbilityOperation>> operations;
439
440 abilitySchedulerProxy_->ExecuteBatch(operations);
441 EXPECT_EQ(IAbilityScheduler::SCHEDULE_EXECUTEBATCH, mock_->code_);
442 }
443
444 /*
445 * Feature: AbilitySchedulerProxy
446 * Function: ContinueAbility
447 * SubFunction: NA
448 * FunctionPoints: AbilitySchedulerProxy ContinueAbility
449 * EnvConditions: NA
450 * CaseDescription: verify ContinueAbility Return value exception
451 */
452 HWTEST_F(AbilitySchedulerProxyTest, ability_scheduler_proxy_operating_022, TestSize.Level1)
453 {
454 EXPECT_CALL(*mock_, SendRequest(_, _, _, _))
455 .Times(1)
456 .WillOnce(Invoke(mock_.GetRefPtr(), &AbilitySchedulerMock::InvokeSendRequest));
457 std::string deviceId = "";
458 uint32_t versionCode = 0;
459
460 abilitySchedulerProxy_->ContinueAbility(deviceId, versionCode);
461 EXPECT_EQ(IAbilityScheduler::CONTINUE_ABILITY, mock_->code_);
462 }
463
464 #ifdef ABILITY_COMMAND_FOR_TEST
465 /*
466 * Feature: AbilitySchedulerProxy
467 * Function: BlockAbility
468 * SubFunction: NA
469 * FunctionPoints: AbilitySchedulerProxy BlockAbility
470 * EnvConditions: NA
471 * CaseDescription: verify BlockAbility Normal case
472 */
473 HWTEST_F(AbilitySchedulerProxyTest, ability_scheduler_proxy_operating_023, TestSize.Level1)
474 {
475 EXPECT_CALL(*mock_, SendRequest(_, _, _, _))
476 .Times(1)
477 .WillOnce(Invoke(mock_.GetRefPtr(), &AbilitySchedulerMock::InvokeSendRequest));
478 abilitySchedulerProxy_->BlockAbility();
479 EXPECT_EQ(IAbilityScheduler::BLOCK_ABILITY_INNER, mock_->code_);
480 }
481 #endif
482 } // namespace AAFwk
483 } // namespace OHOS
484