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