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 #define private public
18 #define protected public
19 #include "call_container.h"
20 #include "ability_record.h"
21 #include "ability_manager_service.h"
22 #include "mission_list_manager.h"
23 #undef private
24 #undef protected
25 #include "ability_scheduler_mock.h"
26 #include "mock_ability_connect_callback.h"
27 
28 using namespace testing::ext;
29 
30 namespace OHOS {
31 namespace AAFwk {
32 class CallContainerTest : public testing::Test {
33 public:
34     static void SetUpTestCase(void);
35     static void TearDownTestCase(void);
36     void SetUp();
37     void TearDown();
38     std::shared_ptr<CallContainer> get() const;
39     std::shared_ptr<AbilityRecord> abilityRecord_{ nullptr };
40 private:
41     std::shared_ptr<CallContainer> callContainer_{ nullptr };
42 
43     int MOCK_MAIN_USER_ID = 100;
44 };
45 
SetUpTestCase(void)46 void CallContainerTest::SetUpTestCase(void) {}
TearDownTestCase(void)47 void CallContainerTest::TearDownTestCase(void) {}
TearDown()48 void CallContainerTest::TearDown() {}
49 
SetUp()50 void CallContainerTest::SetUp()
51 {
52     callContainer_ = std::make_shared<CallContainer>();
53     OHOS::AppExecFwk::AbilityInfo abilityInfo;
54     OHOS::AppExecFwk::ApplicationInfo applicationInfo;
55     Want want;
56     abilityRecord_ = std::make_shared<AbilityRecord>(want, abilityInfo, applicationInfo);
57 }
58 
get() const59 std::shared_ptr<CallContainer> CallContainerTest::get() const
60 {
61     return callContainer_;
62 }
63 
64 /*
65  * Feature: CallContainer
66  * Function: AddCallRecord
67  * SubFunction: NA
68  * FunctionPoints: NA
69  * EnvConditions:NA
70  * CaseDescription: Verify funtion call called
71  */
72 HWTEST_F(CallContainerTest, Call_Container_Add_Call_Record_001, TestSize.Level1)
73 {
74     std::shared_ptr<CallContainer> callContainer = get();
75     AbilityRequest abilityRequest;
76     abilityRequest.callerUid = 1;
77     abilityRequest.callType = AbilityCallType::CALL_REQUEST_TYPE;
78     abilityRequest.connect = new AbilityConnectCallback();
79     std::shared_ptr<CallRecord> callRecord = CallRecord::CreateCallRecord(
80         abilityRequest.callerUid, abilityRecord_->shared_from_this(),
81         abilityRequest.connect, abilityRequest.callerToken);
82     callContainer->AddCallRecord(abilityRequest.connect, callRecord);
83 
84     std::shared_ptr<CallRecord> getCallRecord = callContainer->GetCallRecord(abilityRequest.connect);
85     EXPECT_EQ(callRecord, getCallRecord);
86 }
87 
88 /*
89  * Feature: CallContainer
90  * Function: GetCallRecord
91  * SubFunction: NA
92  * FunctionPoints: NA
93  * EnvConditions:NA
94  * CaseDescription: Verify funtion call called
95  */
96 HWTEST_F(CallContainerTest, Call_Container_Get_Call_Record_001, TestSize.Level1)
97 {
98     sptr<IAbilityConnection> connect = new AbilityConnectCallback();
99     std::shared_ptr<CallContainer> callContainer = get();
100     std::shared_ptr<CallRecord> getCallRecord = callContainer->GetCallRecord(connect);
101     EXPECT_EQ(nullptr, getCallRecord);
102 }
103 
104 /*
105  * Feature: CallContainer
106  * Function: RemoveCallRecord
107  * SubFunction: NA
108  * FunctionPoints: NA
109  * EnvConditions:NA
110  * CaseDescription: Verify funtion call called
111  */
112 HWTEST_F(CallContainerTest, Call_Container_Remove_Call_Record_001, TestSize.Level1)
113 {
114     sptr<IAbilityConnection> connect = new AbilityConnectCallback();
115     std::shared_ptr<CallContainer> callContainer = get();
116     bool result = callContainer->RemoveCallRecord(connect);
117     EXPECT_EQ(result, false);
118 }
119 
120 /*
121  * Feature: CallContainer
122  * Function: RemoveCallRecord
123  * SubFunction: NA
124  * FunctionPoints: NA
125  * EnvConditions:NA
126  * CaseDescription: Verify funtion call called
127  */
128 HWTEST_F(CallContainerTest, Call_Container_Remove_Call_Record_002, TestSize.Level1)
129 {
130     std::shared_ptr<CallContainer> callContainer = get();
131     AbilityRequest abilityRequest;
132     abilityRequest.callerUid = 1;
133     abilityRequest.callType = AbilityCallType::CALL_REQUEST_TYPE;
134     abilityRequest.connect = new AbilityConnectCallback();
135     std::shared_ptr<CallRecord> callRecord = CallRecord::CreateCallRecord(
136         abilityRequest.callerUid, abilityRecord_->shared_from_this(),
137         abilityRequest.connect, abilityRequest.callerToken);
138     callContainer->AddCallRecord(abilityRequest.connect, callRecord);
139 
140     bool result = callContainer->RemoveCallRecord(abilityRequest.connect);
141     EXPECT_EQ(result, true);
142 }
143 
144 /*
145  * Feature: CallContainer
146  * Function: CallRequestDone
147  * SubFunction: NA
148  * FunctionPoints: NA
149  * EnvConditions:NA
150  * CaseDescription: Verify funtion call called
151  */
152 HWTEST_F(CallContainerTest, Call_Container_Call_Request_Done_001, TestSize.Level1)
153 {
154     std::shared_ptr<CallContainer> callContainer = get();
155     OHOS::sptr<IAbilityScheduler> scheduler = new AbilitySchedulerMock();
156     abilityRecord_->SetScheduler(scheduler);
157     scheduler->CallRequest();
158     bool result = callContainer->CallRequestDone(nullptr);
159     EXPECT_EQ(result, false);
160 }
161 
162 /*
163  * Feature: CallContainer
164  * Function: CallRequestDone
165  * SubFunction: NA
166  * FunctionPoints: NA
167  * EnvConditions:NA
168  * CaseDescription: Verify funtion call called
169  */
170 HWTEST_F(CallContainerTest, Call_Container_Call_Request_Done_002, TestSize.Level1)
171 {
172     class AbilitySchedulerMockFunction : public AbilitySchedulerMock {
173     public:
CallRequestModify()174         sptr<IRemoteObject> CallRequestModify() { return this; }
175     };
176 
177     std::shared_ptr<CallContainer> callContainer = get();
178     auto scheduler = new AbilitySchedulerMockFunction();
179     sptr<IRemoteObject> object = scheduler->CallRequestModify();
180     bool result = callContainer->CallRequestDone(object);
181     EXPECT_EQ(result, true);
182 }
183 
184 /*
185  * Feature: CallContainer
186  * Function: Dump
187  * SubFunction: NA
188  * FunctionPoints: NA
189  * EnvConditions:NA
190  * CaseDescription: Verify funtion call called
191  */
192 HWTEST_F(CallContainerTest, Call_Container_Dump_001, TestSize.Level1)
193 {
194     std::shared_ptr<CallContainer> callContainer = get();
195     std::vector<std::string> dumpInfo;
196     callContainer->Dump(dumpInfo);
197     EXPECT_EQ(dumpInfo.size(), 0);
198 }
199 
200 /*
201  * Feature: CallContainer
202  * Function: Dump
203  * SubFunction: NA
204  * FunctionPoints: NA
205  * EnvConditions:NA
206  * CaseDescription: Verify funtion call called
207  */
208 HWTEST_F(CallContainerTest, Call_Container_Dump_002, TestSize.Level1)
209 {
210     std::shared_ptr<CallContainer> callContainer = get();
211     AbilityRequest abilityRequest;
212     abilityRequest.callerUid = 1;
213     abilityRequest.callType = AbilityCallType::CALL_REQUEST_TYPE;
214     abilityRequest.connect = new AbilityConnectCallback();
215     std::shared_ptr<CallRecord> callRecord = CallRecord::CreateCallRecord(
216         abilityRequest.callerUid, abilityRecord_->shared_from_this(),
217         abilityRequest.connect, abilityRequest.callerToken);
218     callContainer->AddCallRecord(abilityRequest.connect, callRecord);
219 
220     std::vector<std::string> dumpInfo;
221     callContainer->Dump(dumpInfo);
222     EXPECT_NE(dumpInfo.size(), 0);
223 }
224 
225 /*
226  * Feature: CallContainer
227  * Function: IsNeedToCallRequest
228  * SubFunction: NA
229  * FunctionPoints: NA
230  * EnvConditions:NA
231  * CaseDescription: Verify funtion call called
232  */
233 HWTEST_F(CallContainerTest, Call_Container_Is_Need_To_Call_Request_001, TestSize.Level1)
234 {
235     std::shared_ptr<CallContainer> callContainer = get();
236     EXPECT_EQ(callContainer->IsNeedToCallRequest(), false);
237 }
238 
239 /*
240  * Feature: CallContainer
241  * Function: IsNeedToCallRequest
242  * SubFunction: NA
243  * FunctionPoints: NA
244  * EnvConditions:NA
245  * CaseDescription: Verify funtion call called
246  */
247 HWTEST_F(CallContainerTest, Call_Container_Is_Need_To_Call_Request_002, TestSize.Level1)
248 {
249     std::shared_ptr<CallContainer> callContainer = get();
250     AbilityRequest abilityRequest;
251     abilityRequest.callerUid = 1;
252     abilityRequest.callType = AbilityCallType::CALL_REQUEST_TYPE;
253     abilityRequest.connect = new AbilityConnectCallback();
254     std::shared_ptr<CallRecord> callRecord = CallRecord::CreateCallRecord(
255         abilityRequest.callerUid, abilityRecord_->shared_from_this(),
256         abilityRequest.connect, abilityRequest.callerToken);
257     callRecord->SetCallState(CallState::INIT);
258     callContainer->AddCallRecord(abilityRequest.connect, callRecord);
259     EXPECT_EQ(callContainer->IsNeedToCallRequest(), true);
260 }
261 
262 /*
263  * Feature: CallContainer
264  * Function: IsNeedToCallRequest
265  * SubFunction: NA
266  * FunctionPoints: NA
267  * EnvConditions:NA
268  * CaseDescription: Verify funtion call called
269  */
270 HWTEST_F(CallContainerTest, Call_Container_Is_Need_To_Call_Request_003, TestSize.Level1)
271 {
272     std::shared_ptr<CallContainer> callContainer = get();
273     AbilityRequest abilityRequest;
274     abilityRequest.callerUid = 1;
275     abilityRequest.callType = AbilityCallType::CALL_REQUEST_TYPE;
276     abilityRequest.connect = new AbilityConnectCallback();
277     std::shared_ptr<CallRecord> callRecord = CallRecord::CreateCallRecord(
278         abilityRequest.callerUid, abilityRecord_->shared_from_this(),
279         abilityRequest.connect, abilityRequest.callerToken);
280     callRecord->SetCallState(CallState::REQUESTING);
281     callContainer->AddCallRecord(abilityRequest.connect, callRecord);
282     EXPECT_EQ(callContainer->IsNeedToCallRequest(), true);
283 }
284 
285 /*
286  * Feature: CallContainer
287  * Function: IsNeedToCallRequest
288  * SubFunction: NA
289  * FunctionPoints: NA
290  * EnvConditions:NA
291  * CaseDescription: Verify funtion call called
292  */
293 HWTEST_F(CallContainerTest, Call_Container_Is_Need_To_Call_Request_004, TestSize.Level1)
294 {
295     std::shared_ptr<CallContainer> callContainer = get();
296     AbilityRequest abilityRequest;
297     abilityRequest.callerUid = 1;
298     abilityRequest.callType = AbilityCallType::CALL_REQUEST_TYPE;
299     abilityRequest.connect = new AbilityConnectCallback();
300     std::shared_ptr<CallRecord> callRecord = CallRecord::CreateCallRecord(
301         abilityRequest.callerUid, abilityRecord_->shared_from_this(),
302         abilityRequest.connect, abilityRequest.callerToken);
303     callRecord->SetCallState(CallState::REQUESTED);
304     callContainer->AddCallRecord(abilityRequest.connect, callRecord);
305     EXPECT_EQ(callContainer->IsNeedToCallRequest(), false);
306 }
307 
308 /*
309  * Feature: CallContainer
310  * Function: AddConnectDeathRecipient
311  * SubFunction: NA
312  * FunctionPoints: NA
313  * EnvConditions:NA
314  * CaseDescription: Verify funtion call called
315  */
316 HWTEST_F(CallContainerTest, Call_Container_Add_Connect_Death_Recipient_001, TestSize.Level1)
317 {
318     std::shared_ptr<CallContainer> callContainer = get();
319     sptr<IAbilityConnection> connect = new AbilityConnectCallback();
320     callContainer->AddConnectDeathRecipient(connect);
321     EXPECT_EQ(callContainer->deathRecipientMap_.size(), 1);
322 }
323 
324 /*
325  * Feature: CallContainer
326  * Function: RemoveConnectDeathRecipient
327  * SubFunction: NA
328  * FunctionPoints: NA
329  * EnvConditions:NA
330  * CaseDescription: Verify funtion call called
331  */
332 HWTEST_F(CallContainerTest, Call_Container_Remove_Connect_Death_Recipient_001, TestSize.Level1)
333 {
334     std::shared_ptr<CallContainer> callContainer = get();
335     sptr<IAbilityConnection> connect = new AbilityConnectCallback();
336     callContainer->AddConnectDeathRecipient(connect);
337     callContainer->RemoveConnectDeathRecipient(connect);
338     EXPECT_EQ(callContainer->deathRecipientMap_.size(), 0);
339 }
340 
341 /*
342  * Feature: CallContainer
343  * Function: OnConnectionDied
344  * SubFunction: NA
345  * FunctionPoints: NA
346  * EnvConditions:NA
347  * CaseDescription: Verify funtion call called
348  */
349 HWTEST_F(CallContainerTest, Call_Container_On_Connect_Died_001, TestSize.Level1)
350 {
351     std::shared_ptr<CallContainer> callContainer = get();
352     EXPECT_EQ(callContainer->callRecordMap_.size(), 0);
353 
354     AbilityRequest abilityRequest;
355     abilityRequest.callerUid = 1;
356     abilityRequest.callType = AbilityCallType::CALL_REQUEST_TYPE;
357     abilityRequest.connect = new AbilityConnectCallback();
358     std::shared_ptr<CallRecord> callRecord = CallRecord::CreateCallRecord(
359         abilityRequest.callerUid, abilityRecord_->shared_from_this(),
360         abilityRequest.connect, abilityRequest.callerToken);
361     callRecord->SetCallState(CallState::REQUESTED);
362     callContainer->AddCallRecord(abilityRequest.connect, callRecord);
363     EXPECT_EQ(callContainer->callRecordMap_.size(), 1);
364 
365     auto mission = std::make_shared<Mission>(0, abilityRecord_, "launcher");
366     auto missionList = std::make_shared<MissionList>();
367     missionList->AddMissionToTop(mission);
368     abilityRecord_->callContainer_ = callContainer;
369 
370     std::shared_ptr<MissionListManager> missionListMgr = std::make_shared<MissionListManager>(0);
371     missionListMgr->currentMissionLists_.push_front(missionList);
372     DelayedSingleton<AbilityManagerService>::GetInstance()->subManagersHelper_ =
373         std::make_shared<SubManagersHelper>(nullptr, nullptr);
374     DelayedSingleton<AbilityManagerService>::GetInstance()->subManagersHelper_->currentMissionListManager_ =
375         missionListMgr;
376     callContainer->OnConnectionDied(abilityRequest.connect->AsObject());
377 
378     EXPECT_EQ(callContainer->callRecordMap_.size(), 1);
379 }
380 
381 /*
382  * Feature: CallContainer
383  * Function: IsExistConnection
384  * SubFunction: NA
385  * FunctionPoints: NA
386  * EnvConditions:NA
387  * CaseDescription: Verify IsExistConnection funtion call called
388  */
389 HWTEST_F(CallContainerTest, Call_Container_Is_Exist_Connection_001, TestSize.Level1)
390 {
391     std::shared_ptr<CallContainer> callContainer = get();
392     sptr<IAbilityConnection> connect = new AbilityConnectCallback();
393     EXPECT_FALSE(callContainer->IsExistConnection(connect));
394 }
395 }  // namespace AAFwk
396 }  // namespace OHOS
397