• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #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_event_handler.h"
25 #include "ability_scheduler_mock.h"
26 #include "mock_ability_connect_callback.h"
27 
28 using namespace testing::ext;
29 
30 
WaitUntilTaskFinished()31 static void WaitUntilTaskFinished()
32 {
33     const uint32_t maxRetryCount = 1000;
34     const uint32_t sleepTime = 1000;
35     uint32_t count = 0;
36     auto handler = OHOS::DelayedSingleton<OHOS::AAFwk::AbilityManagerService>::GetInstance()->GetEventHandler();
37     std::atomic<bool> taskCalled(false);
38     auto f = [&taskCalled]() { taskCalled.store(true); };
39     if (handler->PostTask(f)) {
40         while (!taskCalled.load()) {
41             ++count;
42             if (count >= maxRetryCount) {
43                 std::cout << "max count\n";
44                 break;
45             }
46             usleep(sleepTime);
47         }
48     }
49 }
50 
51 namespace OHOS {
52 namespace AAFwk {
53 class CallContainerTest : public testing::Test {
54 public:
55     static void SetUpTestCase(void);
56     static void TearDownTestCase(void);
57     void SetUp();
58     void TearDown();
59     void OnStartAms();
60     std::shared_ptr<CallContainer> get() const;
61     std::shared_ptr<AbilityRecord> abilityRecord_ {nullptr};
62 private:
63     std::shared_ptr<CallContainer> callContainer_ {nullptr};
64     std::shared_ptr<AbilityManagerService> abilityMgrServ_ {nullptr};
65     int MOCK_MAIN_USER_ID = 100;
66 };
67 
SetUpTestCase(void)68 void CallContainerTest::SetUpTestCase(void)
69 {
70 }
TearDownTestCase(void)71 void CallContainerTest::TearDownTestCase(void)
72 {
73     DelayedSingleton<AbilityManagerService>::DestroyInstance();
74 }
TearDown()75 void CallContainerTest::TearDown()
76 {
77     DelayedSingleton<AbilityManagerService>::DestroyInstance();
78 }
79 
SetUp()80 void CallContainerTest::SetUp()
81 {
82     callContainer_ = std::make_shared<CallContainer>();
83     OHOS::AppExecFwk::AbilityInfo abilityInfo;
84     OHOS::AppExecFwk::ApplicationInfo applicationInfo;
85     Want want;
86     abilityRecord_ = std::make_shared<AbilityRecord>(want, abilityInfo, applicationInfo);
87     abilityMgrServ_ = DelayedSingleton<AbilityManagerService>::GetInstance();
88     OnStartAms();
89 }
90 
OnStartAms()91 void CallContainerTest::OnStartAms()
92 {
93     if (abilityMgrServ_) {
94         if (abilityMgrServ_->state_ == ServiceRunningState::STATE_RUNNING) {
95             return;
96         }
97 
98         abilityMgrServ_->state_ = ServiceRunningState::STATE_RUNNING;
99 
100         abilityMgrServ_->eventLoop_ = AppExecFwk::EventRunner::Create(AbilityConfig::NAME_ABILITY_MGR_SERVICE);
101         EXPECT_TRUE(abilityMgrServ_->eventLoop_);
102 
103         abilityMgrServ_->handler_ = std::make_shared<AbilityEventHandler>(abilityMgrServ_->eventLoop_, abilityMgrServ_);
104         EXPECT_TRUE(abilityMgrServ_->handler_);
105 
106         // init user controller.
107         abilityMgrServ_->userController_ = std::make_shared<UserController>();
108         EXPECT_TRUE(abilityMgrServ_->userController_);
109         abilityMgrServ_->userController_->Init();
110         int userId = MOCK_MAIN_USER_ID;
111         abilityMgrServ_->userController_->SetCurrentUserId(userId);
112         abilityMgrServ_->InitConnectManager(userId, true);
113         abilityMgrServ_->InitDataAbilityManager(userId, true);
114         abilityMgrServ_->InitPendWantManager(userId, true);
115         abilityMgrServ_->systemDataAbilityManager_ = std::make_shared<DataAbilityManager>();
116         EXPECT_TRUE(abilityMgrServ_->systemDataAbilityManager_);
117 
118         abilityMgrServ_->amsConfigResolver_ = std::make_shared<AmsConfigurationParameter>();
119         EXPECT_TRUE(abilityMgrServ_->amsConfigResolver_);
120         abilityMgrServ_->amsConfigResolver_->Parse();
121 
122         abilityMgrServ_->InitMissionListManager(userId, true);
123         abilityMgrServ_->connectManager_->SetEventHandler(abilityMgrServ_->handler_);
124         abilityMgrServ_->eventLoop_->Run();
125         auto topAbility = abilityMgrServ_->GetListManagerByUserId(MOCK_MAIN_USER_ID)->GetCurrentTopAbilityLocked();
126         if (topAbility) {
127             topAbility->SetAbilityState(AAFwk::AbilityState::FOREGROUND);
128         }
129         WaitUntilTaskFinished();
130         return;
131     }
132 
133     GTEST_LOG_(INFO) << "OnStart fail";
134 }
135 
136 
get() const137 std::shared_ptr<CallContainer> CallContainerTest::get() const
138 {
139     return callContainer_;
140 }
141 
142 /*
143  * Feature: CallContainer
144  * Function: AddCallRecord
145  * SubFunction: NA
146  * FunctionPoints: NA
147  * EnvConditions:NA
148  * CaseDescription: Verify funtion call called
149  */
150 HWTEST_F(CallContainerTest, Call_Container_Add_Call_Record_001, TestSize.Level1)
151 {
152     std::shared_ptr<CallContainer> callContainer = get();
153     AbilityRequest abilityRequest;
154     abilityRequest.callerUid = 1;
155     abilityRequest.callType = AbilityCallType::CALL_REQUEST_TYPE;
156     abilityRequest.connect = new AbilityConnectCallback();
157     std::shared_ptr<CallRecord> callRecord = CallRecord::CreateCallRecord(
158         abilityRequest.callerUid, abilityRecord_->shared_from_this(),
159         abilityRequest.connect, abilityRequest.callerToken);
160     callContainer->AddCallRecord(abilityRequest.connect, callRecord);
161 
162     std::shared_ptr<CallRecord> getCallRecord = callContainer->GetCallRecord(abilityRequest.connect);
163     EXPECT_EQ(callRecord, getCallRecord);
164 }
165 
166 /*
167  * Feature: CallContainer
168  * Function: GetCallRecord
169  * SubFunction: NA
170  * FunctionPoints: NA
171  * EnvConditions:NA
172  * CaseDescription: Verify funtion call called
173  */
174 HWTEST_F(CallContainerTest, Call_Container_Get_Call_Record_001, TestSize.Level1)
175 {
176     sptr<IAbilityConnection> connect = new AbilityConnectCallback();
177     std::shared_ptr<CallContainer> callContainer = get();
178     std::shared_ptr<CallRecord> getCallRecord = callContainer->GetCallRecord(connect);
179     EXPECT_EQ(nullptr, getCallRecord);
180 }
181 
182 /*
183  * Feature: CallContainer
184  * Function: RemoveCallRecord
185  * SubFunction: NA
186  * FunctionPoints: NA
187  * EnvConditions:NA
188  * CaseDescription: Verify funtion call called
189  */
190 HWTEST_F(CallContainerTest, Call_Container_Remove_Call_Record_001, TestSize.Level1)
191 {
192     sptr<IAbilityConnection> connect = new AbilityConnectCallback();
193     std::shared_ptr<CallContainer> callContainer = get();
194     bool result = callContainer->RemoveCallRecord(connect);
195     EXPECT_EQ(result, false);
196 }
197 
198 /*
199  * Feature: CallContainer
200  * Function: RemoveCallRecord
201  * SubFunction: NA
202  * FunctionPoints: NA
203  * EnvConditions:NA
204  * CaseDescription: Verify funtion call called
205  */
206 HWTEST_F(CallContainerTest, Call_Container_Remove_Call_Record_002, TestSize.Level1)
207 {
208     std::shared_ptr<CallContainer> callContainer = get();
209     AbilityRequest abilityRequest;
210     abilityRequest.callerUid = 1;
211     abilityRequest.callType = AbilityCallType::CALL_REQUEST_TYPE;
212     abilityRequest.connect = new AbilityConnectCallback();
213     std::shared_ptr<CallRecord> callRecord = CallRecord::CreateCallRecord(
214         abilityRequest.callerUid, abilityRecord_->shared_from_this(),
215         abilityRequest.connect, abilityRequest.callerToken);
216     callContainer->AddCallRecord(abilityRequest.connect, callRecord);
217 
218     bool result = callContainer->RemoveCallRecord(abilityRequest.connect);
219     EXPECT_EQ(result, true);
220 }
221 
222 /*
223  * Feature: CallContainer
224  * Function: CallRequestDone
225  * SubFunction: NA
226  * FunctionPoints: NA
227  * EnvConditions:NA
228  * CaseDescription: Verify funtion call called
229  */
230 HWTEST_F(CallContainerTest, Call_Container_Call_Request_Done_001, TestSize.Level1)
231 {
232     std::shared_ptr<CallContainer> callContainer = get();
233     OHOS::sptr<IAbilityScheduler> scheduler = new AbilitySchedulerMock();
234     abilityRecord_->SetScheduler(scheduler);
235     scheduler->CallRequest();
236     bool result = callContainer->CallRequestDone(nullptr);
237     EXPECT_EQ(result, false);
238 }
239 
240 /*
241  * Feature: CallContainer
242  * Function: CallRequestDone
243  * SubFunction: NA
244  * FunctionPoints: NA
245  * EnvConditions:NA
246  * CaseDescription: Verify funtion call called
247  */
248 HWTEST_F(CallContainerTest, Call_Container_Call_Request_Done_002, TestSize.Level1)
249 {
250     class AbilitySchedulerMockFunction : public AbilitySchedulerMock {
251     public:
CallRequestModify()252         sptr<IRemoteObject> CallRequestModify() { return this; }
253     };
254 
255     std::shared_ptr<CallContainer> callContainer = get();
256     auto scheduler = new AbilitySchedulerMockFunction();
257     sptr<IRemoteObject> object = scheduler->CallRequestModify();
258     bool result = callContainer->CallRequestDone(object);
259     EXPECT_EQ(result, true);
260 }
261 
262 /*
263  * Feature: CallContainer
264  * Function: Dump
265  * SubFunction: NA
266  * FunctionPoints: NA
267  * EnvConditions:NA
268  * CaseDescription: Verify funtion call called
269  */
270 HWTEST_F(CallContainerTest, Call_Container_Dump_001, TestSize.Level1)
271 {
272     std::shared_ptr<CallContainer> callContainer = get();
273     std::vector<std::string> dumpInfo;
274     callContainer->Dump(dumpInfo);
275     EXPECT_EQ(dumpInfo.size(), 0);
276 }
277 
278 /*
279  * Feature: CallContainer
280  * Function: Dump
281  * SubFunction: NA
282  * FunctionPoints: NA
283  * EnvConditions:NA
284  * CaseDescription: Verify funtion call called
285  */
286 HWTEST_F(CallContainerTest, Call_Container_Dump_002, TestSize.Level1)
287 {
288     std::shared_ptr<CallContainer> callContainer = get();
289     AbilityRequest abilityRequest;
290     abilityRequest.callerUid = 1;
291     abilityRequest.callType = AbilityCallType::CALL_REQUEST_TYPE;
292     abilityRequest.connect = new AbilityConnectCallback();
293     std::shared_ptr<CallRecord> callRecord = CallRecord::CreateCallRecord(
294         abilityRequest.callerUid, abilityRecord_->shared_from_this(),
295         abilityRequest.connect, abilityRequest.callerToken);
296     callContainer->AddCallRecord(abilityRequest.connect, callRecord);
297 
298     std::vector<std::string> dumpInfo;
299     callContainer->Dump(dumpInfo);
300     EXPECT_NE(dumpInfo.size(), 0);
301 }
302 
303 /*
304  * Feature: CallContainer
305  * Function: IsNeedToCallRequest
306  * SubFunction: NA
307  * FunctionPoints: NA
308  * EnvConditions:NA
309  * CaseDescription: Verify funtion call called
310  */
311 HWTEST_F(CallContainerTest, Call_Container_Is_Need_To_Call_Request_001, TestSize.Level1)
312 {
313     std::shared_ptr<CallContainer> callContainer = get();
314     EXPECT_EQ(callContainer->IsNeedToCallRequest(), false);
315 }
316 
317 /*
318  * Feature: CallContainer
319  * Function: IsNeedToCallRequest
320  * SubFunction: NA
321  * FunctionPoints: NA
322  * EnvConditions:NA
323  * CaseDescription: Verify funtion call called
324  */
325 HWTEST_F(CallContainerTest, Call_Container_Is_Need_To_Call_Request_002, TestSize.Level1)
326 {
327     std::shared_ptr<CallContainer> callContainer = get();
328     AbilityRequest abilityRequest;
329     abilityRequest.callerUid = 1;
330     abilityRequest.callType = AbilityCallType::CALL_REQUEST_TYPE;
331     abilityRequest.connect = new AbilityConnectCallback();
332     std::shared_ptr<CallRecord> callRecord = CallRecord::CreateCallRecord(
333         abilityRequest.callerUid, abilityRecord_->shared_from_this(),
334         abilityRequest.connect, abilityRequest.callerToken);
335     callRecord->SetCallState(CallState::INIT);
336     callContainer->AddCallRecord(abilityRequest.connect, callRecord);
337     EXPECT_EQ(callContainer->IsNeedToCallRequest(), true);
338 }
339 
340 /*
341  * Feature: CallContainer
342  * Function: IsNeedToCallRequest
343  * SubFunction: NA
344  * FunctionPoints: NA
345  * EnvConditions:NA
346  * CaseDescription: Verify funtion call called
347  */
348 HWTEST_F(CallContainerTest, Call_Container_Is_Need_To_Call_Request_003, TestSize.Level1)
349 {
350     std::shared_ptr<CallContainer> callContainer = get();
351     AbilityRequest abilityRequest;
352     abilityRequest.callerUid = 1;
353     abilityRequest.callType = AbilityCallType::CALL_REQUEST_TYPE;
354     abilityRequest.connect = new AbilityConnectCallback();
355     std::shared_ptr<CallRecord> callRecord = CallRecord::CreateCallRecord(
356         abilityRequest.callerUid, abilityRecord_->shared_from_this(),
357         abilityRequest.connect, abilityRequest.callerToken);
358     callRecord->SetCallState(CallState::REQUESTING);
359     callContainer->AddCallRecord(abilityRequest.connect, callRecord);
360     EXPECT_EQ(callContainer->IsNeedToCallRequest(), true);
361 }
362 
363 /*
364  * Feature: CallContainer
365  * Function: IsNeedToCallRequest
366  * SubFunction: NA
367  * FunctionPoints: NA
368  * EnvConditions:NA
369  * CaseDescription: Verify funtion call called
370  */
371 HWTEST_F(CallContainerTest, Call_Container_Is_Need_To_Call_Request_004, TestSize.Level1)
372 {
373     std::shared_ptr<CallContainer> callContainer = get();
374     AbilityRequest abilityRequest;
375     abilityRequest.callerUid = 1;
376     abilityRequest.callType = AbilityCallType::CALL_REQUEST_TYPE;
377     abilityRequest.connect = new AbilityConnectCallback();
378     std::shared_ptr<CallRecord> callRecord = CallRecord::CreateCallRecord(
379         abilityRequest.callerUid, abilityRecord_->shared_from_this(),
380         abilityRequest.connect, abilityRequest.callerToken);
381     callRecord->SetCallState(CallState::REQUESTED);
382     callContainer->AddCallRecord(abilityRequest.connect, callRecord);
383     EXPECT_EQ(callContainer->IsNeedToCallRequest(), false);
384 }
385 
386 /*
387  * Feature: CallContainer
388  * Function: AddConnectDeathRecipient
389  * SubFunction: NA
390  * FunctionPoints: NA
391  * EnvConditions:NA
392  * CaseDescription: Verify funtion call called
393  */
394 HWTEST_F(CallContainerTest, Call_Container_Add_Connect_Death_Recipient_001, TestSize.Level1)
395 {
396     std::shared_ptr<CallContainer> callContainer = get();
397     sptr<IAbilityConnection> connect = new AbilityConnectCallback();
398     callContainer->AddConnectDeathRecipient(connect);
399     EXPECT_EQ(callContainer->deathRecipientMap_.size(), 1);
400 }
401 
402 /*
403  * Feature: CallContainer
404  * Function: RemoveConnectDeathRecipient
405  * SubFunction: NA
406  * FunctionPoints: NA
407  * EnvConditions:NA
408  * CaseDescription: Verify funtion call called
409  */
410 HWTEST_F(CallContainerTest, Call_Container_Remove_Connect_Death_Recipient_001, TestSize.Level1)
411 {
412     std::shared_ptr<CallContainer> callContainer = get();
413     sptr<IAbilityConnection> connect = new AbilityConnectCallback();
414     callContainer->AddConnectDeathRecipient(connect);
415     callContainer->RemoveConnectDeathRecipient(connect);
416     EXPECT_EQ(callContainer->deathRecipientMap_.size(), 0);
417 }
418 
419 /*
420  * Feature: CallContainer
421  * Function: OnConnectionDied
422  * SubFunction: NA
423  * FunctionPoints: NA
424  * EnvConditions:NA
425  * CaseDescription: Verify funtion call called
426  */
427 HWTEST_F(CallContainerTest, Call_Container_On_Connect_Died_001, TestSize.Level1)
428 {
429     std::shared_ptr<CallContainer> callContainer = get();
430     EXPECT_EQ(callContainer->callRecordMap_.size(), 0);
431 
432     AbilityRequest abilityRequest;
433     abilityRequest.callerUid = 1;
434     abilityRequest.callType = AbilityCallType::CALL_REQUEST_TYPE;
435     abilityRequest.connect = new AbilityConnectCallback();
436     std::shared_ptr<CallRecord> callRecord = CallRecord::CreateCallRecord(
437         abilityRequest.callerUid, abilityRecord_->shared_from_this(),
438         abilityRequest.connect, abilityRequest.callerToken);
439     callRecord->SetCallState(CallState::REQUESTED);
440     callContainer->AddCallRecord(abilityRequest.connect, callRecord);
441     EXPECT_EQ(callContainer->callRecordMap_.size(), 1);
442 
443     auto mission = std::make_shared<Mission>(0, abilityRecord_, "launcher");
444     auto missionList = std::make_shared<MissionList>();
445     missionList->AddMissionToTop(mission);
446     abilityRecord_->callContainer_ = callContainer;
447 
448     std::shared_ptr<MissionListManager> missionListMgr = std::make_shared<MissionListManager>(0);
449     missionListMgr->currentMissionLists_.push_front(missionList);
450     DelayedSingleton<AbilityManagerService>::GetInstance()->currentMissionListManager_ = missionListMgr;
451     callContainer->OnConnectionDied(abilityRequest.connect->AsObject());
452     WaitUntilTaskFinished();
453 
454     EXPECT_EQ(callContainer->callRecordMap_.size(), 0);
455 }
456 }  // namespace AAFwk
457 }  // namespace OHOS
458