• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2025 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 <numeric>
18 #define private public
19 #include "ability_manager_helper.h"
20 #undef private
21 #include "mock_common_event_stub.h"
22 #include "mock_iabilitymanager.h"
23 #include "static_subscriber_connection.h"
24 
25 using namespace testing::ext;
26 using namespace OHOS;
27 using namespace OHOS::EventFwk;
28 using namespace OHOS::AppExecFwk;
29 
30 class AbilityManagerHelperTest : public testing::Test {
31 public:
AbilityManagerHelperTest()32     AbilityManagerHelperTest()
33     {}
~AbilityManagerHelperTest()34     ~AbilityManagerHelperTest()
35     {}
36 
37     static void SetUpTestCase(void);
38     static void TearDownTestCase(void);
39     void SetUp();
40     void TearDown();
41 };
42 
SetUpTestCase(void)43 void AbilityManagerHelperTest::SetUpTestCase(void)
44 {}
45 
TearDownTestCase(void)46 void AbilityManagerHelperTest::TearDownTestCase(void)
47 {}
48 
SetUp(void)49 void AbilityManagerHelperTest::SetUp(void)
50 {}
51 
TearDown(void)52 void AbilityManagerHelperTest::TearDown(void)
53 {}
54 
55 using TestAbilityManager = AAFwk::IAbilityManager;
56 
57 class TestAbilityMgr : public TestAbilityManager {
58 public:
59     TestAbilityMgr() = default;
~TestAbilityMgr()60     virtual ~TestAbilityMgr()
61     {};
AsObject()62     sptr<IRemoteObject> AsObject() override
63     {
64         return nullptr;
65     }
66 
StartAbility(const Want & want,int32_t userId,int requestCode)67     int StartAbility(const Want &want, int32_t userId, int requestCode) override
68     {
69         return 1;
70     }
71 
StartAbility(const Want & want,const sptr<IRemoteObject> & callerToken,int32_t userId,int requestCode)72     int StartAbility(
73         const Want &want, const sptr<IRemoteObject> &callerToken, int32_t userId, int requestCode) override
74     {
75         return 1;
76     }
77 
StartAbility(const Want & want,const AbilityStartSetting & abilityStartSetting,const sptr<IRemoteObject> & callerToken,int32_t userId,int requestCode)78     int StartAbility(const Want &want, const AbilityStartSetting &abilityStartSetting,
79         const sptr<IRemoteObject> &callerToken, int32_t userId, int requestCode) override
80     {
81         return 1;
82     }
83 
StartAbilityByInsightIntent(const Want & want,const sptr<IRemoteObject> & callerToken,uint64_t intentId,int32_t userId=DEFAULT_INVAL_VALUE)84     int32_t StartAbilityByInsightIntent(const Want &want, const sptr<IRemoteObject> &callerToken,
85         uint64_t intentId, int32_t userId = DEFAULT_INVAL_VALUE) override
86     {
87         return 1;
88     }
89 
StartAbility(const Want & want,const StartOptions & startOptions,const sptr<IRemoteObject> & callerToken,int32_t userId,int requestCode)90     int StartAbility(const Want &want, const StartOptions &startOptions,
91         const sptr<IRemoteObject> &callerToken, int32_t userId, int requestCode) override
92     {
93         return 1;
94     }
95 
TerminateAbility(const sptr<IRemoteObject> & token,int resultCode,const Want * resultWant=nullptr)96     int TerminateAbility(
97         const sptr<IRemoteObject> &token, int resultCode, const Want *resultWant = nullptr) override
98     {
99         return 1;
100     }
101 
CloseAbility(const sptr<IRemoteObject> & token,int resultCode=DEFAULT_INVAL_VALUE,const Want * resultWant=nullptr)102     int CloseAbility(const sptr<IRemoteObject> &token, int resultCode = DEFAULT_INVAL_VALUE,
103         const Want *resultWant = nullptr) override
104     {
105         return 1;
106     }
107 
MinimizeAbility(const sptr<IRemoteObject> & token,bool fromUser=false)108     int MinimizeAbility(const sptr<IRemoteObject> &token, bool fromUser = false) override
109     {
110         return 1;
111     }
112 
ConnectAbility(const Want & want,const sptr<IAbilityConnection> & connect,const sptr<IRemoteObject> & callerToken,int32_t userId)113     int ConnectAbility(const Want &want, const sptr<IAbilityConnection> &connect,
114         const sptr<IRemoteObject> &callerToken, int32_t userId) override
115     {
116         return 1;
117     }
118 
DisconnectAbility(sptr<IAbilityConnection> connect)119     int DisconnectAbility(sptr<IAbilityConnection> connect) override
120     {
121         return 1;
122     }
123 
AcquireDataAbility(const Uri & uri,bool tryBind,const sptr<IRemoteObject> & callerToken)124     sptr<IAbilityScheduler> AcquireDataAbility(
125         const Uri &uri, bool tryBind, const sptr<IRemoteObject> &callerToken) override
126     {
127         return nullptr;
128     }
129 
ReleaseDataAbility(sptr<IAbilityScheduler> dataAbilityScheduler,const sptr<IRemoteObject> & callerToken)130     int ReleaseDataAbility(
131         sptr<IAbilityScheduler> dataAbilityScheduler, const sptr<IRemoteObject> &callerToken) override
132     {
133         return 1;
134     }
135 
AttachAbilityThread(const sptr<IAbilityScheduler> & scheduler,const sptr<IRemoteObject> & token)136     int AttachAbilityThread(const sptr<IAbilityScheduler> &scheduler, const sptr<IRemoteObject> &token) override
137     {
138         return 1;
139     }
140 
AbilityTransitionDone(const sptr<IRemoteObject> & token,int state,const PacMap & saveData)141     int AbilityTransitionDone(const sptr<IRemoteObject> &token, int state, const PacMap &saveData)override
142     {
143         return 1;
144     }
145 
ScheduleConnectAbilityDone(const sptr<IRemoteObject> & token,const sptr<IRemoteObject> & remoteObject)146     int ScheduleConnectAbilityDone(
147         const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &remoteObject) override
148     {
149         return 1;
150     }
151 
ScheduleDisconnectAbilityDone(const sptr<IRemoteObject> & token)152     int ScheduleDisconnectAbilityDone(const sptr<IRemoteObject> &token) override
153     {
154         return 1;
155     }
156 
ScheduleCommandAbilityDone(const sptr<IRemoteObject> & token)157     int ScheduleCommandAbilityDone(const sptr<IRemoteObject> &token) override
158     {
159         return 1;
160     }
161 
ScheduleCommandAbilityWindowDone(const sptr<IRemoteObject> & token,const sptr<AAFwk::SessionInfo> & sessionInfo,AAFwk::WindowCommand winCmd,AAFwk::AbilityCommand abilityCmd)162     int ScheduleCommandAbilityWindowDone(
163         const sptr<IRemoteObject> &token,
164         const sptr<AAFwk::SessionInfo> &sessionInfo,
165         AAFwk::WindowCommand winCmd,
166         AAFwk::AbilityCommand abilityCmd) override
167     {
168         return 1;
169     }
170 
DumpState(const std::string & args,std::vector<std::string> & state)171     void DumpState(const std::string &args, std::vector<std::string> &state) override
172     {}
173 
DumpSysState(const std::string & args,std::vector<std::string> & state,bool isClient,bool isUserID,int UserID)174     void DumpSysState(
175         const std::string& args, std::vector<std::string>& state, bool isClient, bool isUserID, int UserID) override
176     {}
177 
StopServiceAbility(const Want & want,int32_t userId,const sptr<IRemoteObject> & token=nullptr)178     int StopServiceAbility(const Want &want, int32_t userId, const sptr<IRemoteObject> &token = nullptr) override
179     {
180         return 1;
181     }
182 
KillProcess(const std::string & bundleName,const bool clearPageStack,int32_t appIndex)183     int KillProcess(const std::string &bundleName, const bool clearPageStack, int32_t appIndex) override
184     {
185         return 1;
186     }
187 
UninstallApp(const std::string & bundleName,int32_t uid)188     int UninstallApp(const std::string &bundleName, int32_t uid) override
189     {
190         return 1;
191     }
192 
GetWantSender(const WantSenderInfo & wantSenderInfo,const sptr<IRemoteObject> & callerToken,int32_t uid)193     sptr<IWantSender> GetWantSender(
194         const WantSenderInfo &wantSenderInfo, const sptr<IRemoteObject> &callerToken, int32_t uid) override
195     {
196         return nullptr;
197     }
198 
SendWantSender(sptr<IWantSender> target,SenderInfo & senderInfo)199     int SendWantSender(sptr<IWantSender> target, SenderInfo &senderInfo) override
200     {
201         return 1;
202     }
203 
CancelWantSender(const sptr<IWantSender> & sender)204     void CancelWantSender(const sptr<IWantSender> &sender) override
205     {}
206 
GetPendingWantUid(const sptr<IWantSender> & target)207     int GetPendingWantUid(const sptr<IWantSender> &target) override
208     {
209         return 1;
210     }
211 
GetPendingWantUserId(const sptr<IWantSender> & target)212     int GetPendingWantUserId(const sptr<IWantSender> &target) override
213     {
214         return 1;
215     }
216 
GetPendingWantBundleName(const sptr<IWantSender> & target)217     std::string GetPendingWantBundleName(const sptr<IWantSender> &target) override
218     {
219         return "";
220     }
221 
GetPendingWantCode(const sptr<IWantSender> & target)222     int GetPendingWantCode(const sptr<IWantSender> &target) override
223     {
224         return 1;
225     }
226 
GetPendingWantType(const sptr<IWantSender> & target)227     int GetPendingWantType(const sptr<IWantSender> &target) override
228     {
229         return 1;
230     }
231 
RegisterCancelListener(const sptr<IWantSender> & sender,const sptr<IWantReceiver> & receiver)232     void RegisterCancelListener(const sptr<IWantSender> &sender, const sptr<IWantReceiver> &receiver) override
233     {}
234 
UnregisterCancelListener(const sptr<IWantSender> & sender,const sptr<IWantReceiver> & receiver)235     void UnregisterCancelListener(const sptr<IWantSender> &sender, const sptr<IWantReceiver> &receiver) override
236     {}
237 
GetPendingRequestWant(const sptr<IWantSender> & target,std::shared_ptr<Want> & want)238     int GetPendingRequestWant(const sptr<IWantSender> &target, std::shared_ptr<Want> &want) override
239     {
240         return 1;
241     }
242 
GetWantSenderInfo(const sptr<IWantSender> & target,std::shared_ptr<WantSenderInfo> & info)243     int GetWantSenderInfo(const sptr<IWantSender> &target, std::shared_ptr<WantSenderInfo> &info) override
244     {
245         return 1;
246     }
247 
ContinueMission(const std::string & srcDeviceId,const std::string & dstDeviceId,int32_t missionId,const sptr<IRemoteObject> & callBack,AAFwk::WantParams & wantParams)248     int ContinueMission(const std::string &srcDeviceId, const std::string &dstDeviceId, int32_t missionId,
249         const sptr<IRemoteObject> &callBack, AAFwk::WantParams &wantParams) override
250     {
251         return 1;
252     }
253 
ContinueAbility(const std::string & deviceId,int32_t missionId,uint32_t versionCode)254     int ContinueAbility(const std::string &deviceId, int32_t missionId, uint32_t versionCode) override
255     {
256         return 1;
257     }
258 
StartContinuation(const Want & want,const sptr<IRemoteObject> & abilityToken,int32_t status)259     int StartContinuation(const Want &want, const sptr<IRemoteObject> &abilityToken, int32_t status) override
260     {
261         return 1;
262     }
263 
NotifyCompleteContinuation(const std::string & deviceId,int32_t sessionId,bool isSuccess)264     void NotifyCompleteContinuation(const std::string &deviceId, int32_t sessionId, bool isSuccess) override
265     {}
266 
NotifyContinuationResult(int32_t missionId,int32_t result)267     int NotifyContinuationResult(int32_t missionId, int32_t result) override
268     {
269         return 1;
270     }
271 
LockMissionForCleanup(int32_t missionId)272     int LockMissionForCleanup(int32_t missionId) override
273     {
274         return 1;
275     }
276 
UnlockMissionForCleanup(int32_t missionId)277     int UnlockMissionForCleanup(int32_t missionId) override
278     {
279         return 1;
280     }
281 
RegisterMissionListener(const sptr<IMissionListener> & listener)282     int RegisterMissionListener(const sptr<IMissionListener> &listener) override
283     {
284         return 1;
285     }
286 
UnRegisterMissionListener(const sptr<IMissionListener> & listener)287     int UnRegisterMissionListener(const sptr<IMissionListener> &listener) override
288     {
289         return 1;
290     }
291 
GetMissionInfos(const std::string & deviceId,int32_t numMax,std::vector<MissionInfo> & missionInfos)292     int GetMissionInfos(
293         const std::string &deviceId, int32_t numMax, std::vector<MissionInfo> &missionInfos) override
294     {
295         return 1;
296     }
297 
GetMissionInfo(const std::string & deviceId,int32_t missionId,MissionInfo & missionInfo)298     int GetMissionInfo(const std::string &deviceId, int32_t missionId, MissionInfo &missionInfo) override
299     {
300         return 1;
301     }
302 
GetMissionSnapshot(const std::string & deviceId,int32_t missionId,MissionSnapshot & snapshot,bool isLowResolution)303     int GetMissionSnapshot(const std::string& deviceId, int32_t missionId,
304         MissionSnapshot& snapshot, bool isLowResolution) override
305     {
306         return 1;
307     }
308 
CleanMission(int32_t missionId)309     int CleanMission(int32_t missionId) override
310     {
311         return 1;
312     }
313 
CleanAllMissions()314     int CleanAllMissions() override
315     {
316         return 1;
317     }
318 
MoveMissionToFront(int32_t missionId)319     int MoveMissionToFront(int32_t missionId) override
320     {
321         return 1;
322     }
323 
MoveMissionToFront(int32_t missionId,const StartOptions & startOptions)324     int MoveMissionToFront(int32_t missionId, const StartOptions &startOptions) override
325     {
326         return 1;
327     }
328 
StartAbilityByCall(const Want & want,const sptr<IAbilityConnection> & connect,const sptr<IRemoteObject> & callerToken,int32_t accountId,bool isSilent)329     int StartAbilityByCall(const Want &want, const sptr<IAbilityConnection> &connect,
330         const sptr<IRemoteObject> &callerToken, int32_t accountId, bool isSilent) override
331     {
332         return 1;
333     }
334 
ReleaseCall(const sptr<IAbilityConnection> & connect,const AppExecFwk::ElementName & element)335     int ReleaseCall(const sptr<IAbilityConnection> &connect, const AppExecFwk::ElementName &element) override
336     {
337         return 1;
338     }
339 
StartUser(int userId,sptr<IUserCallback> callback,bool isAppRecovery)340     int StartUser(int userId, sptr<IUserCallback> callback, bool isAppRecovery) override
341     {
342         return 1;
343     }
344 
StopUser(int userId,const sptr<IUserCallback> & callback)345     int StopUser(int userId, const sptr<IUserCallback> &callback) override
346     {
347         return 1;
348     }
349 
SetMissionLabel(const sptr<IRemoteObject> & abilityToken,const std::string & label)350     int SetMissionLabel(const sptr<IRemoteObject> &abilityToken, const std::string &label) override
351     {
352         return 1;
353     }
354 
SetMissionIcon(const sptr<IRemoteObject> & token,const std::shared_ptr<OHOS::Media::PixelMap> & icon)355     int SetMissionIcon(const sptr<IRemoteObject> &token,
356         const std::shared_ptr<OHOS::Media::PixelMap> &icon) override
357     {
358         return 1;
359     }
360 
RegisterWindowManagerServiceHandler(const sptr<IWindowManagerServiceHandler> & handler,bool animationEnabled)361     int RegisterWindowManagerServiceHandler(const sptr<IWindowManagerServiceHandler>& handler,
362         bool animationEnabled) override
363     {
364         return 1;
365     }
366 
CompleteFirstFrameDrawing(const sptr<IRemoteObject> & abilityToken)367     void CompleteFirstFrameDrawing(const sptr<IRemoteObject> &abilityToken) override
368     {}
369 
GetAbilityRunningInfos(std::vector<AbilityRunningInfo> & info)370     int GetAbilityRunningInfos(std::vector<AbilityRunningInfo> &info) override
371     {
372         return 1;
373     }
374 
GetExtensionRunningInfos(int upperLimit,std::vector<ExtensionRunningInfo> & info)375     int GetExtensionRunningInfos(int upperLimit, std::vector<ExtensionRunningInfo> &info) override
376     {
377         return 1;
378     }
379 
GetProcessRunningInfos(std::vector<AppExecFwk::RunningProcessInfo> & info)380     int GetProcessRunningInfos(std::vector<AppExecFwk::RunningProcessInfo> &info) override
381     {
382         return 1;
383     }
384 
StartSyncRemoteMissions(const std::string & devId,bool fixConflict,int64_t tag)385     int StartSyncRemoteMissions(const std::string &devId, bool fixConflict, int64_t tag) override
386     {
387         return 1;
388     }
389 
StopSyncRemoteMissions(const std::string & devId)390     int StopSyncRemoteMissions(const std::string &devId) override
391     {
392         return 1;
393     }
394 
RegisterMissionListener(const std::string & deviceId,const sptr<IRemoteMissionListener> & listener)395     int RegisterMissionListener(const std::string &deviceId, const sptr<IRemoteMissionListener> &listener) override
396     {
397         return 1;
398     }
399 
UnRegisterMissionListener(const std::string & deviceId,const sptr<IRemoteMissionListener> & listener)400     int UnRegisterMissionListener(const std::string &deviceId,
401         const sptr<IRemoteMissionListener> &listener) override
402     {
403         return 1;
404     }
405 
SetAbilityController(const sptr<AppExecFwk::IAbilityController> & abilityController,bool imAStabilityTest)406     int SetAbilityController(const sptr<AppExecFwk::IAbilityController> &abilityController,
407         bool imAStabilityTest) override
408     {
409         return 1;
410     }
411 
IsRunningInStabilityTest()412     bool IsRunningInStabilityTest() override
413     {
414         return false;
415     }
416 
RegisterSnapshotHandler(const sptr<ISnapshotHandler> & handler)417     int RegisterSnapshotHandler(const sptr<ISnapshotHandler>& handler) override
418     {
419         return 1;
420     }
421 
StartUserTest(const Want & want,const sptr<IRemoteObject> & observer)422     int StartUserTest(const Want &want, const sptr<IRemoteObject> &observer) override
423     {
424         return 1;
425     }
426 
FinishUserTest(const std::string & msg,const int64_t & resultCode,const std::string & bundleName)427     int FinishUserTest(const std::string &msg, const int64_t &resultCode, const std::string &bundleName) override
428     {
429         return 1;
430     }
431 
GetTopAbility(sptr<IRemoteObject> & token)432     int GetTopAbility(sptr<IRemoteObject> &token) override
433     {
434         return 1;
435     }
436 
DelegatorDoAbilityForeground(const sptr<IRemoteObject> & token)437     int DelegatorDoAbilityForeground(const sptr<IRemoteObject> &token) override
438     {
439         return 1;
440     }
441 
DelegatorDoAbilityBackground(const sptr<IRemoteObject> & token)442     int DelegatorDoAbilityBackground(const sptr<IRemoteObject> &token) override
443     {
444         return 1;
445     }
446 
DoAbilityForeground(const sptr<IRemoteObject> & token,uint32_t flag)447     int DoAbilityForeground(const sptr<IRemoteObject> &token, uint32_t flag) override
448     {
449         return 1;
450     }
451 
DoAbilityBackground(const sptr<IRemoteObject> & token,uint32_t flag)452     int DoAbilityBackground(const sptr<IRemoteObject> &token, uint32_t flag) override
453     {
454         return 1;
455     }
456 
GetMissionIdByToken(const sptr<IRemoteObject> & token)457     int32_t GetMissionIdByToken(const sptr<IRemoteObject> &token) override
458     {
459         return 1;
460     }
461 
GetAbilityTokenByCalleeObj(const sptr<IRemoteObject> & callStub,sptr<IRemoteObject> & token)462     void GetAbilityTokenByCalleeObj(const sptr<IRemoteObject> &callStub, sptr<IRemoteObject> &token) override
463     {}
464 
IsValidMissionIds(const std::vector<int32_t> & missionIds,std::vector<MissionValidResult> & results)465     int32_t IsValidMissionIds(
466         const std::vector<int32_t> &missionIds, std::vector<MissionValidResult> &results) override
467     {
468         return 1;
469     }
470 
ReportDrawnCompleted(const sptr<IRemoteObject> & callerToken)471     int32_t ReportDrawnCompleted(const sptr<IRemoteObject> &callerToken) override
472     {
473         return 1;
474     }
475 
RegisterAppDebugListener(sptr<AppExecFwk::IAppDebugListener> listener)476     int32_t RegisterAppDebugListener(sptr<AppExecFwk::IAppDebugListener> listener) override
477     {
478         return 1;
479     }
480 
UnregisterAppDebugListener(sptr<AppExecFwk::IAppDebugListener> listener)481     int32_t UnregisterAppDebugListener(sptr<AppExecFwk::IAppDebugListener> listener) override
482     {
483         return 1;
484     }
485 
AttachAppDebug(const std::string & bundleName,bool isDebugFromLocal)486     int32_t AttachAppDebug(const std::string &bundleName, bool isDebugFromLocal) override
487     {
488         return 1;
489     }
490 
DetachAppDebug(const std::string & bundleName,bool isDebugFromLocal)491     int32_t DetachAppDebug(const std::string &bundleName, bool isDebugFromLocal) override
492     {
493         return 1;
494     }
495 
ExecuteIntent(uint64_t key,const sptr<IRemoteObject> & callerToken,const InsightIntentExecuteParam & param)496     int32_t ExecuteIntent(uint64_t key, const sptr<IRemoteObject> &callerToken,
497         const InsightIntentExecuteParam &param) override
498     {
499         return 1;
500     }
501 
ExecuteInsightIntentDone(const sptr<IRemoteObject> & token,uint64_t intentId,const InsightIntentExecuteResult & result)502     int32_t ExecuteInsightIntentDone(const sptr<IRemoteObject> &token, uint64_t intentId,
503         const InsightIntentExecuteResult &result) override
504     {
505         return 1;
506     }
507 
SetApplicationAutoStartupByEDM(const AutoStartupInfo & info,bool flag)508     int32_t SetApplicationAutoStartupByEDM(const AutoStartupInfo &info, bool flag) override
509     {
510         return 0;
511     }
512 
CancelApplicationAutoStartupByEDM(const AutoStartupInfo & info,bool flag)513     int32_t CancelApplicationAutoStartupByEDM(const AutoStartupInfo &info, bool flag) override
514     {
515         return 0;
516     }
517 
GetForegroundUIAbilities(std::vector<AppExecFwk::AbilityStateData> & list)518     int32_t GetForegroundUIAbilities(std::vector<AppExecFwk::AbilityStateData> &list) override
519     {
520         return 1;
521     }
522 
StartAbilityWithSpecifyTokenId(const Want & want,const sptr<IRemoteObject> & callerToken,uint32_t specifyTokenId,int32_t userId=DEFAULT_INVAL_VALUE,int requestCode=DEFAULT_INVAL_VALUE)523     int32_t StartAbilityWithSpecifyTokenId(const Want& want,
524         const sptr<IRemoteObject>& callerToken,
525         uint32_t specifyTokenId,
526         int32_t userId = DEFAULT_INVAL_VALUE,
527         int requestCode = DEFAULT_INVAL_VALUE) override
528     {
529         return 1;
530     }
531 
UpdateKioskApplicationList(const std::vector<std::string> & appList)532     int32_t UpdateKioskApplicationList(const std::vector<std::string> &appList) override
533     {
534         return 1;
535     }
536 
EnterKioskMode(sptr<IRemoteObject> callerToken)537     int32_t EnterKioskMode(sptr<IRemoteObject> callerToken) override
538     {
539         return 1;
540     }
541 
ExitKioskMode(sptr<IRemoteObject> callerToken)542     int32_t ExitKioskMode(sptr<IRemoteObject> callerToken) override
543     {
544         return 1;
545     }
546 
GetKioskStatus(AAFwk::KioskStatus & kioskStatus)547     int32_t GetKioskStatus(AAFwk::KioskStatus &kioskStatus) override
548     {
549         return 1;
550     }
551 };
552 
553 /**
554  * @tc.name: AbilityManagerHelper_0100
555  * @tc.desc: test GetAbilityMgrProxy function and abilityMgr_ is not nullptr.
556  * @tc.type: FUNC
557  */
HWTEST_F(AbilityManagerHelperTest,AbilityManagerHelper_0100,Level1)558 HWTEST_F(AbilityManagerHelperTest, AbilityManagerHelper_0100, Level1)
559 {
560     GTEST_LOG_(INFO) << "AbilityManagerHelper_0100 start";
561     AbilityManagerHelper abilityManagerHelper;
562     abilityManagerHelper.abilityMgr_ = new (std::nothrow) TestAbilityMgr();
563     EXPECT_EQ(true, abilityManagerHelper.GetAbilityMgrProxy());
564     GTEST_LOG_(INFO) << "AbilityManagerHelper_0100 end";
565 }
566 
567 /**
568  * @tc.name: AbilityManagerHelper_0200
569  * @tc.desc: test GetAbilityMgrProxy function and abilityMgr_ is nullptr.
570  * @tc.type: FUNC
571  */
HWTEST_F(AbilityManagerHelperTest,AbilityManagerHelper_0200,Level1)572 HWTEST_F(AbilityManagerHelperTest, AbilityManagerHelper_0200, Level1)
573 {
574     GTEST_LOG_(INFO) << "AbilityManagerHelper_0200 start";
575     AbilityManagerHelper abilityManagerHelper;
576     abilityManagerHelper.abilityMgr_ = nullptr;
577     EXPECT_EQ(false, abilityManagerHelper.GetAbilityMgrProxy());
578     GTEST_LOG_(INFO) << "AbilityManagerHelper_0200 end";
579 }
580 
581 /**
582  * @tc.name: AbilityManagerHelper_0300
583  * @tc.desc: test ConnectAbility function and GetAbilityMgrProxy is false.
584  * @tc.type: FUNC
585  */
HWTEST_F(AbilityManagerHelperTest,AbilityManagerHelper_0300,Level1)586 HWTEST_F(AbilityManagerHelperTest, AbilityManagerHelper_0300, Level1)
587 {
588     GTEST_LOG_(INFO) << "AbilityManagerHelper_0300 start";
589     AbilityManagerHelper abilityManagerHelper;
590     abilityManagerHelper.abilityMgr_ = nullptr;
591     Want want;
592     CommonEventData event;
593     sptr<IRemoteObject> callerToken = nullptr;
594     const int32_t userId = 1;
595     EXPECT_EQ(-1, abilityManagerHelper.ConnectAbility(want, event, callerToken, userId));
596     GTEST_LOG_(INFO) << "AbilityManagerHelper_0300 end";
597 }
598 
599 /**
600  * @tc.name: AbilityManagerHelper_0400
601  * @tc.desc: test Clear function and abilityMgr_ is nullptr.
602  * @tc.type: FUNC
603  */
HWTEST_F(AbilityManagerHelperTest,AbilityManagerHelper_0400,Level1)604 HWTEST_F(AbilityManagerHelperTest, AbilityManagerHelper_0400, Level1)
605 {
606     GTEST_LOG_(INFO) << "AbilityManagerHelper_0400 start";
607     std::shared_ptr<AbilityManagerHelper> abilityManagerHelper = std::make_shared<AbilityManagerHelper>();
608     ASSERT_NE(nullptr, abilityManagerHelper);
609     abilityManagerHelper->abilityMgr_ = nullptr;
610     abilityManagerHelper->Clear();
611     GTEST_LOG_(INFO) << "AbilityManagerHelper_0400 end";
612 }
613 
614 /**
615  * @tc.name: AbilityManagerHelper_0500
616  * @tc.desc: test Clear function and abilityMgr_ is not nullptr.
617  * @tc.type: FUNC
618  */
HWTEST_F(AbilityManagerHelperTest,AbilityManagerHelper_0500,Level1)619 HWTEST_F(AbilityManagerHelperTest, AbilityManagerHelper_0500, Level1)
620 {
621     GTEST_LOG_(INFO) << "AbilityManagerHelper_0500 start";
622     std::shared_ptr<AbilityManagerHelper> abilityManagerHelper = std::make_shared<AbilityManagerHelper>();
623     ASSERT_NE(nullptr, abilityManagerHelper);
624     sptr<IRemoteObject> remoteObject = sptr<IRemoteObject>(new MockCommonEventStub());
625     abilityManagerHelper->abilityMgr_ = iface_cast<AAFwk::IAbilityManager>(remoteObject);
626     abilityManagerHelper->Clear();
627     GTEST_LOG_(INFO) << "AbilityManagerHelper_0500 end";
628 }
629 
630 /**
631  * @tc.name  : test SetEventHandler
632  * @tc.number: AbilityManagerHelper_0600
633  * @tc.desc  : Test SetEventHandler succeeded.
634  */
HWTEST_F(AbilityManagerHelperTest,AbilityManagerHelper_0600,Level1)635 HWTEST_F(AbilityManagerHelperTest, AbilityManagerHelper_0600, Level1)
636 {
637     GTEST_LOG_(INFO) << "AbilityManagerHelper_0600 start";
638     auto abilityManagerHelper = std::make_shared<AbilityManagerHelper>();
639     CommonEventData data;
640     sptr<StaticSubscriberConnection> connection = new (std::nothrow) StaticSubscriberConnection(data, "");
641     abilityManagerHelper->subscriberConnection_.emplace("test_key", connection);
642     auto handler = std::make_shared<EventHandler>(EventRunner::Create());
643     abilityManagerHelper->SetEventHandler(handler);
644     EXPECT_NE(abilityManagerHelper->subscriberConnection_.size(), 0);
645     GTEST_LOG_(INFO) << "AbilityManagerHelper_0600 end";
646 }
647 
648 /**
649  * @tc.name  : test DisconnectAbility
650  * @tc.number: AbilityManagerHelper_0700
651  * @tc.desc  : Test the DisconnectAbility function when the connection is nullptr.
652  */
HWTEST_F(AbilityManagerHelperTest,AbilityManagerHelper_0700,Level1)653 HWTEST_F(AbilityManagerHelperTest, AbilityManagerHelper_0700, Level1)
654 {
655     GTEST_LOG_(INFO) << "AbilityManagerHelper_0700 start";
656     auto abilityManagerHelper = std::make_shared<AbilityManagerHelper>();
657     CommonEventData data;
658     sptr<StaticSubscriberConnection> connection = new (std::nothrow) StaticSubscriberConnection(data, "");
659     abilityManagerHelper->subscriberConnection_.emplace("test_key", connection);
660     abilityManagerHelper->DisconnectAbility(nullptr, "");
661     EXPECT_NE(abilityManagerHelper->subscriberConnection_.size(), 0);
662     GTEST_LOG_(INFO) << "AbilityManagerHelper_0700 end";
663 }
664 
665 /**
666  * @tc.name  : test DisconnectAbility
667  * @tc.number: AbilityManagerHelper_0800
668  * @tc.desc  : Test the DisconnectAbility function when the connection is not nullptr.
669  */
HWTEST_F(AbilityManagerHelperTest,AbilityManagerHelper_0800,Level1)670 HWTEST_F(AbilityManagerHelperTest, AbilityManagerHelper_0800, Level1)
671 {
672     GTEST_LOG_(INFO) << "AbilityManagerHelper_0800 start";
673     auto abilityManagerHelper = std::make_shared<AbilityManagerHelper>();
674     CommonEventData data;
675     sptr<StaticSubscriberConnection> connection = new (std::nothrow) StaticSubscriberConnection(data, "");
676     abilityManagerHelper->subscriberConnection_.emplace("test_key", connection);
677     sptr<StaticSubscriberConnection> connectionOne = new (std::nothrow) StaticSubscriberConnection(data, "");
678     abilityManagerHelper->DisconnectAbility(connectionOne, "test_action");
679     EXPECT_NE(abilityManagerHelper->subscriberConnection_.size(), 0);
680     GTEST_LOG_(INFO) << "AbilityManagerHelper_0800 end";
681 }
682 
683 /**
684  * @tc.name  : test DisconnectServiceAbilityDelay
685  * @tc.number: AbilityManagerHelper_0900
686  * @tc.desc  : Test the DisconnectServiceAbilityDelay function when the connection is nullptr.
687  */
HWTEST_F(AbilityManagerHelperTest,AbilityManagerHelper_0900,Level1)688 HWTEST_F(AbilityManagerHelperTest, AbilityManagerHelper_0900, Level1)
689 {
690     GTEST_LOG_(INFO) << "AbilityManagerHelper_0900 start";
691     auto abilityManagerHelper = std::make_shared<AbilityManagerHelper>();
692     auto handler = std::make_shared<EventHandler>(EventRunner::Create());
693     abilityManagerHelper->SetEventHandler(handler);
694     CommonEventData data;
695     sptr<StaticSubscriberConnection> connection = new (std::nothrow) StaticSubscriberConnection(data, "");
696     abilityManagerHelper->subscriberConnection_.emplace("test_key", connection);
697     abilityManagerHelper->DisconnectServiceAbilityDelay(nullptr, "");
698     EXPECT_NE(abilityManagerHelper->subscriberConnection_.size(), 0);
699     GTEST_LOG_(INFO) << "AbilityManagerHelper_0900 end";
700 }
701 
702 /**
703  * @tc.name  : test DisconnectServiceAbilityDelay
704  * @tc.number: AbilityManagerHelper_1000
705  * @tc.desc  : Test the DisconnectServiceAbilityDelay function when the eventHandler_ is nullptr.
706  */
HWTEST_F(AbilityManagerHelperTest,AbilityManagerHelper_1000,Level1)707 HWTEST_F(AbilityManagerHelperTest, AbilityManagerHelper_1000, Level1)
708 {
709     GTEST_LOG_(INFO) << "AbilityManagerHelper_1000 start";
710     auto abilityManagerHelper = std::make_shared<AbilityManagerHelper>();
711     CommonEventData data;
712     sptr<StaticSubscriberConnection> connection = new (std::nothrow) StaticSubscriberConnection(data, "");
713     abilityManagerHelper->subscriberConnection_.emplace("test_key", connection);
714     abilityManagerHelper->DisconnectServiceAbilityDelay(connection, "test_action");
715     EXPECT_NE(abilityManagerHelper->subscriberConnection_.size(), 0);
716     GTEST_LOG_(INFO) << "AbilityManagerHelper_1000 end";
717 }
718 
719 /**
720  * @tc.name  : test DisconnectServiceAbilityDelay
721  * @tc.number: AbilityManagerHelper_1100
722  * @tc.desc  : Test the DisconnectServiceAbilityDelay function when the input parameters meet the requirements.
723  */
HWTEST_F(AbilityManagerHelperTest,AbilityManagerHelper_1100,Level1)724 HWTEST_F(AbilityManagerHelperTest, AbilityManagerHelper_1100, Level1)
725 {
726     GTEST_LOG_(INFO) << "AbilityManagerHelper_1100 start";
727     auto abilityManagerHelper = std::make_shared<AbilityManagerHelper>();
728     CommonEventData data;
729     sptr<StaticSubscriberConnection> firstConnection = new (std::nothrow) StaticSubscriberConnection(data, "");
730     abilityManagerHelper->subscriberConnection_.emplace("test_key", firstConnection);
731     auto handler = std::make_shared<EventHandler>(EventRunner::Create());
732     abilityManagerHelper->SetEventHandler(handler);
733     sptr<StaticSubscriberConnection> SecondConnection = new (std::nothrow) StaticSubscriberConnection(data, "");
734     abilityManagerHelper->DisconnectServiceAbilityDelay(SecondConnection, "test_action");
735     EXPECT_NE(abilityManagerHelper->subscriberConnection_.size(), 0);
736     GTEST_LOG_(INFO) << "AbilityManagerHelper_1100 end";
737 }
738 
739 /**
740  * @tc.name  : test ConnectAbility multiple times
741  * @tc.number: AbilityManagerHelper_1200
742  * @tc.desc  : When calling the connection multiple times, the proxy_ is empty
743  */
HWTEST_F(AbilityManagerHelperTest,AbilityManagerHelper_1200,Level1)744 HWTEST_F(AbilityManagerHelperTest, AbilityManagerHelper_1200, Level1)
745 {
746     GTEST_LOG_(INFO) << "AbilityManagerHelper_1200 start";
747     auto abilityManagerHelper = std::make_shared<AbilityManagerHelper>();
748     sptr<IRemoteObject> remoteObject = sptr<IRemoteObject>(new MockCommonEventStub());
749     abilityManagerHelper->abilityMgr_ = new (std::nothrow) TestAbilityMgr();
750 
751     Want want;
752     CommonEventData event;
753     sptr<IRemoteObject> callerToken = nullptr;
754     int32_t userId = 1;
755     CommonEventData data;
756     sptr<StaticSubscriberConnection> connection = new (std::nothrow) StaticSubscriberConnection(data, "");
757     std::string connectionKey =
758         want.GetBundle() + "_" + want.GetElement().GetAbilityName() + "_" + std::to_string(userId);
759     abilityManagerHelper->subscriberConnection_[connectionKey] = connection;
760     abilityManagerHelper->subscriberConnection_[connectionKey]->proxy_ = nullptr;
761 
762     int result1 = abilityManagerHelper->ConnectAbility(want, event, callerToken, userId);
763     EXPECT_EQ(result1, ERR_OK);
764 
765     int result2 = abilityManagerHelper->ConnectAbility(want, event, callerToken, userId);
766     EXPECT_EQ(result2, ERR_OK);
767 
768     int result3 = abilityManagerHelper->ConnectAbility(want, event, callerToken, userId);
769     EXPECT_EQ(result3, ERR_OK);
770 
771     EXPECT_EQ(abilityManagerHelper->subscriberConnection_[connectionKey]->events_.size(), 4);
772 
773     GTEST_LOG_(INFO) << "AbilityManagerHelper_1200 end";
774 }
775 
776 /**
777  * @tc.name  : test ConnectAbility multiple times
778  * @tc.number: AbilityManagerHelper_1300
779  * @tc.desc  : When calling the connection multiple times, the proxy_ is not empty
780  */
HWTEST_F(AbilityManagerHelperTest,AbilityManagerHelper_1300,Level1)781 HWTEST_F(AbilityManagerHelperTest, AbilityManagerHelper_1300, Level1)
782 {
783     GTEST_LOG_(INFO) << "AbilityManagerHelper_1300 start";
784     auto abilityManagerHelper = std::make_shared<AbilityManagerHelper>();
785     sptr<IRemoteObject> remoteObject = sptr<IRemoteObject>(new MockCommonEventStub());
786     abilityManagerHelper->abilityMgr_ = new (std::nothrow) TestAbilityMgr();
787 
788     Want want;
789     CommonEventData event;
790     sptr<IRemoteObject> callerToken = nullptr;
791     int32_t userId = 1;
792     CommonEventData data;
793     sptr<StaticSubscriberConnection> connection = new (std::nothrow) StaticSubscriberConnection(data, "");
794     std::string connectionKey =
795         want.GetBundle() + "_" + want.GetElement().GetAbilityName() + "_" + std::to_string(userId);
796     abilityManagerHelper->subscriberConnection_[connectionKey] = connection;
797     abilityManagerHelper->subscriberConnection_[connectionKey]->GetProxy(callerToken);
798 
799     int result1 = abilityManagerHelper->ConnectAbility(want, event, callerToken, userId);
800     EXPECT_EQ(result1, ERR_OK);
801 
802     int result2 = abilityManagerHelper->ConnectAbility(want, event, callerToken, userId);
803     EXPECT_EQ(result2, ERR_OK);
804 
805     int result3 = abilityManagerHelper->ConnectAbility(want, event, callerToken, userId);
806     EXPECT_EQ(result3, ERR_OK);
807 
808     EXPECT_EQ(abilityManagerHelper->subscriberConnection_[connectionKey]->events_.size(), 1);
809 
810     GTEST_LOG_(INFO) << "AbilityManagerHelper_1300 end";
811 }
812 
813 /**
814  * @tc.name  : test DisconnectAbility when connection is not empty and IsEmptyAction returns false
815  * @tc.number: AbilityManagerHelper_1400
816  * @tc.desc  : Test DisconnectAbility function when action_ has multiple elements.
817  */
HWTEST_F(AbilityManagerHelperTest,AbilityManagerHelper_1400,Level1)818 HWTEST_F(AbilityManagerHelperTest, AbilityManagerHelper_1400, Level1)
819 {
820     GTEST_LOG_(INFO) << "AbilityManagerHelper_1400 start";
821     auto abilityManagerHelper = std::make_shared<AbilityManagerHelper>();
822     CommonEventData data;
823     sptr<StaticSubscriberConnection> connection = new (std::nothrow) StaticSubscriberConnection(data, "");
824 
825     std::string action = "test_action";
826     connection->action_.push_back(action);
827     connection->action_.push_back("other_action");
828     abilityManagerHelper->subscriberConnection_.emplace("test_key", connection);
829 
830     abilityManagerHelper->DisconnectAbility(connection, action);
831 
832     EXPECT_EQ(connection->action_.size(), 1);
833     EXPECT_EQ(connection->action_[0], "other_action");
834     GTEST_LOG_(INFO) << "AbilityManagerHelper_1400 end";
835 }
836 
837 /**
838  * @tc.name  : ConnectAbility_RealDelay_Disconnect_0001
839  * @tc.desc  : Verify that ConnectAbility auto-releases the connection after the delay task.
840  * @tc.type: FUNC
841  */
HWTEST_F(AbilityManagerHelperTest,ConnectAbility_RealDelay_Disconnect_0001,Level1)842 HWTEST_F(AbilityManagerHelperTest, ConnectAbility_RealDelay_Disconnect_0001, Level1)
843 {
844     auto abilityManagerHelper = std::make_shared<AbilityManagerHelper>();
845     auto mockAbilityMgr = new MockIAbilityManager();
846     abilityManagerHelper->abilityMgr_ = mockAbilityMgr;
847 
848     Want want;
849     want.SetBundle("test.bundle");
850     ElementName element;
851     element.SetAbilityName("TestAbility");
852     want.SetElement(element);
853     CommonEventData event;
854     sptr<IRemoteObject> callerToken = nullptr;
855     int32_t userId = 1;
856     CommonEventData data;
857     std::string connectionKey =
858         want.GetBundle() + "_" + want.GetElement().GetAbilityName() + "_" + std::to_string(userId);
859     EXPECT_CALL(*mockAbilityMgr, ConnectAbility(::testing::_, ::testing::_, ::testing::_, ::testing::_))
860         .Times(1)
861         .WillOnce(::testing::Return(ERR_OK));
862 
863     int result1 = abilityManagerHelper->ConnectAbility(want, event, callerToken, userId);
864 
865     auto it = abilityManagerHelper->subscriberConnection_.find(connectionKey);
866     EXPECT_NE(it, abilityManagerHelper->subscriberConnection_.end());
867 
868     ffrt::this_task::sleep_for(std::chrono::seconds(16));
869 
870     it = abilityManagerHelper->subscriberConnection_.find(connectionKey);
871     EXPECT_EQ(it, abilityManagerHelper->subscriberConnection_.end());
872 }