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