1 /*
2  * Copyright (c) 2021-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 
18 #define private public
19 #define protected public
20 #include "bundle_manager_helper.h"
21 #include "common_event_subscriber_manager.h"
22 #include "static_subscriber_manager.h"
23 #include "ability_manager_helper.h"
24 #undef private
25 #undef protected
26 
27 #include "ces_inner_error_code.h"
28 #include "common_event_listener.h"
29 #include "common_event_stub.h"
30 #include "common_event_subscriber.h"
31 #include "inner_common_event_manager.h"
32 #include "mock_bundle_manager.h"
33 #include "bundle_manager_helper.h"
34 #include "common_event_manager_service.h"
35 
36 using namespace testing::ext;
37 using namespace OHOS;
38 using namespace OHOS::EventFwk;
39 using namespace OHOS::AppExecFwk;
40 
41 namespace {
42 const std::string EVENT = "com.ces.test.event";
43 const std::string ENTITY = "com.ces.test.entity";
44 const std::string SCHEME = "com.ces.test.scheme";
45 const std::string PERMISSION = "com.ces.test.permission";
46 const std::string DEVICEDID = "deviceId";
47 constexpr int32_t ERR_COMMON = -1;
48 }  // namespace
49 
50 static OHOS::sptr<OHOS::IRemoteObject> bundleObject = nullptr;
51 class CommonEventSubscribeUnitTest : public testing::Test {
52 public:
CommonEventSubscribeUnitTest()53     CommonEventSubscribeUnitTest()
54     {}
~CommonEventSubscribeUnitTest()55     ~CommonEventSubscribeUnitTest()
56     {}
57     static void SetUpTestCase(void);
58     static void TearDownTestCase(void);
59     void SetUp();
60     void TearDown();
61 };
62 
63 class SubscriberTest : public CommonEventSubscriber {
64 public:
SubscriberTest(const CommonEventSubscribeInfo & sp)65     SubscriberTest(const CommonEventSubscribeInfo &sp) : CommonEventSubscriber(sp)
66     {}
67 
~SubscriberTest()68     ~SubscriberTest()
69     {}
70 
OnReceiveEvent(const CommonEventData & data)71     virtual void OnReceiveEvent(const CommonEventData &data)
72     {}
73 };
74 
75 class CommonEventStubTest : public CommonEventStub {
76 public:
CommonEventStubTest()77     CommonEventStubTest()
78     {}
79 
PublishCommonEvent(const CommonEventData & event,const CommonEventPublishInfo & publishinfo,const OHOS::sptr<OHOS::IRemoteObject> & commonEventListener,const int32_t & userId)80     virtual int32_t PublishCommonEvent(const CommonEventData &event, const CommonEventPublishInfo &publishinfo,
81         const OHOS::sptr<OHOS::IRemoteObject> &commonEventListener, const int32_t &userId)
82     {
83         return ERR_COMMON;
84     }
85 
SubscribeCommonEvent(const CommonEventSubscribeInfo & subscribeInfo,const OHOS::sptr<OHOS::IRemoteObject> & commonEventListener,const int32_t instanceKey)86     virtual int32_t SubscribeCommonEvent(
87         const CommonEventSubscribeInfo &subscribeInfo, const OHOS::sptr<OHOS::IRemoteObject> &commonEventListener,
88         const int32_t instanceKey)
89     {
90         return ERR_COMMON;
91     }
92 
UnsubscribeCommonEvent(const OHOS::sptr<OHOS::IRemoteObject> & commonEventListener)93     virtual int32_t UnsubscribeCommonEvent(const OHOS::sptr<OHOS::IRemoteObject> &commonEventListener)
94     {
95         return ERR_COMMON;
96     }
97 
DumpState(const uint8_t & dumpType,const std::string & event,const int32_t & userId,std::vector<std::string> & state)98     virtual bool DumpState(const uint8_t &dumpType, const std::string &event, const int32_t &userId,
99         std::vector<std::string> &state)
100     {
101         return false;
102     }
103 
~CommonEventStubTest()104     virtual ~CommonEventStubTest()
105     {}
106 
FinishReceiver(const OHOS::sptr<OHOS::IRemoteObject> & proxy,const int & code,const std::string & receiverData,const bool & abortEvent)107     virtual bool FinishReceiver(const OHOS::sptr<OHOS::IRemoteObject> &proxy, const int &code,
108         const std::string &receiverData, const bool &abortEvent)
109     {
110         return false;
111     }
112 };
113 
114 using TestAbilityManager = AAFwk::IAbilityManager;
115 
116 class TestAbilityMgr : public TestAbilityManager {
117 public:
118     TestAbilityMgr() = default;
~TestAbilityMgr()119     virtual ~TestAbilityMgr()
120     {};
AsObject()121     sptr<IRemoteObject> AsObject() override
122     {
123         return nullptr;
124     }
125 
StartAbility(const Want & want,int32_t userId,int requestCode)126     int StartAbility(const Want &want, int32_t userId, int requestCode) override
127     {
128         return 1;
129     }
130 
StartAbility(const Want & want,const sptr<IRemoteObject> & callerToken,int32_t userId,int requestCode)131     int StartAbility(
132         const Want &want, const sptr<IRemoteObject> &callerToken, int32_t userId, int requestCode) override
133     {
134         return 1;
135     }
136 
StartAbility(const Want & want,const AbilityStartSetting & abilityStartSetting,const sptr<IRemoteObject> & callerToken,int32_t userId,int requestCode)137     int StartAbility(const Want &want, const AbilityStartSetting &abilityStartSetting,
138         const sptr<IRemoteObject> &callerToken, int32_t userId, int requestCode) override
139     {
140         return 1;
141     }
142 
StartAbilityByInsightIntent(const Want & want,const sptr<IRemoteObject> & callerToken,uint64_t intentId,int32_t userId=DEFAULT_INVAL_VALUE)143     int32_t StartAbilityByInsightIntent(const Want &want, const sptr<IRemoteObject> &callerToken,
144         uint64_t intentId, int32_t userId = DEFAULT_INVAL_VALUE) override
145     {
146         return 1;
147     }
148 
StartAbility(const Want & want,const StartOptions & startOptions,const sptr<IRemoteObject> & callerToken,int32_t userId,int requestCode)149     int StartAbility(const Want &want, const StartOptions &startOptions,
150         const sptr<IRemoteObject> &callerToken, int32_t userId, int requestCode) override
151     {
152         return 1;
153     }
154 
TerminateAbility(const sptr<IRemoteObject> & token,int resultCode,const Want * resultWant=nullptr)155     int TerminateAbility(
156         const sptr<IRemoteObject> &token, int resultCode, const Want *resultWant = nullptr) override
157     {
158         return 1;
159     }
160 
CloseAbility(const sptr<IRemoteObject> & token,int resultCode=DEFAULT_INVAL_VALUE,const Want * resultWant=nullptr)161     int CloseAbility(const sptr<IRemoteObject> &token, int resultCode = DEFAULT_INVAL_VALUE,
162         const Want *resultWant = nullptr) override
163     {
164         return 1;
165     }
166 
MinimizeAbility(const sptr<IRemoteObject> & token,bool fromUser=false)167     int MinimizeAbility(const sptr<IRemoteObject> &token, bool fromUser = false) override
168     {
169         return 1;
170     }
171 
ConnectAbility(const Want & want,const sptr<IAbilityConnection> & connect,const sptr<IRemoteObject> & callerToken,int32_t userId)172     int ConnectAbility(const Want &want, const sptr<IAbilityConnection> &connect,
173         const sptr<IRemoteObject> &callerToken, int32_t userId) override
174     {
175         return 2097177;
176     }
177 
DisconnectAbility(sptr<IAbilityConnection> connect)178     int DisconnectAbility(sptr<IAbilityConnection> connect) override
179     {
180         return 1;
181     }
182 
AcquireDataAbility(const Uri & uri,bool tryBind,const sptr<IRemoteObject> & callerToken)183     sptr<IAbilityScheduler> AcquireDataAbility(
184         const Uri &uri, bool tryBind, const sptr<IRemoteObject> &callerToken) override
185     {
186         return nullptr;
187     }
188 
ReleaseDataAbility(sptr<IAbilityScheduler> dataAbilityScheduler,const sptr<IRemoteObject> & callerToken)189     int ReleaseDataAbility(
190         sptr<IAbilityScheduler> dataAbilityScheduler, const sptr<IRemoteObject> &callerToken) override
191     {
192         return 1;
193     }
194 
AttachAbilityThread(const sptr<IAbilityScheduler> & scheduler,const sptr<IRemoteObject> & token)195     int AttachAbilityThread(const sptr<IAbilityScheduler> &scheduler, const sptr<IRemoteObject> &token) override
196     {
197         return 1;
198     }
199 
AbilityTransitionDone(const sptr<IRemoteObject> & token,int state,const PacMap & saveData)200     int AbilityTransitionDone(const sptr<IRemoteObject> &token, int state, const PacMap &saveData)override
201     {
202         return 1;
203     }
204 
ScheduleConnectAbilityDone(const sptr<IRemoteObject> & token,const sptr<IRemoteObject> & remoteObject)205     int ScheduleConnectAbilityDone(
206         const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &remoteObject) override
207     {
208         return 1;
209     }
210 
ScheduleDisconnectAbilityDone(const sptr<IRemoteObject> & token)211     int ScheduleDisconnectAbilityDone(const sptr<IRemoteObject> &token) override
212     {
213         return 1;
214     }
215 
ScheduleCommandAbilityDone(const sptr<IRemoteObject> & token)216     int ScheduleCommandAbilityDone(const sptr<IRemoteObject> &token) override
217     {
218         return 1;
219     }
220 
ScheduleCommandAbilityWindowDone(const sptr<IRemoteObject> & token,const sptr<AAFwk::SessionInfo> & sessionInfo,AAFwk::WindowCommand winCmd,AAFwk::AbilityCommand abilityCmd)221     int ScheduleCommandAbilityWindowDone(
222         const sptr<IRemoteObject> &token,
223         const sptr<AAFwk::SessionInfo> &sessionInfo,
224         AAFwk::WindowCommand winCmd,
225         AAFwk::AbilityCommand abilityCmd) override
226     {
227         return 1;
228     }
229 
DumpState(const std::string & args,std::vector<std::string> & state)230     void DumpState(const std::string &args, std::vector<std::string> &state) override
231     {}
232 
DumpSysState(const std::string & args,std::vector<std::string> & state,bool isClient,bool isUserID,int UserID)233     void DumpSysState(
234         const std::string& args, std::vector<std::string>& state, bool isClient, bool isUserID, int UserID) override
235     {}
236 
StopServiceAbility(const Want & want,int32_t userId,const sptr<IRemoteObject> & token=nullptr)237     int StopServiceAbility(const Want &want, int32_t userId, const sptr<IRemoteObject> &token = nullptr) override
238     {
239         return 1;
240     }
241 
KillProcess(const std::string & bundleName,const bool clearPageStack)242     int KillProcess(const std::string &bundleName, const bool clearPageStack) override
243     {
244         return 1;
245     }
246 
UninstallApp(const std::string & bundleName,int32_t uid)247     int UninstallApp(const std::string &bundleName, int32_t uid) override
248     {
249         return 1;
250     }
251 
GetWantSender(const WantSenderInfo & wantSenderInfo,const sptr<IRemoteObject> & callerToken,int32_t uid)252     sptr<IWantSender> GetWantSender(
253         const WantSenderInfo &wantSenderInfo, const sptr<IRemoteObject> &callerToken, int32_t uid) override
254     {
255         return nullptr;
256     }
257 
SendWantSender(sptr<IWantSender> target,const SenderInfo & senderInfo)258     int SendWantSender(sptr<IWantSender> target, const SenderInfo &senderInfo) override
259     {
260         return 1;
261     }
262 
CancelWantSender(const sptr<IWantSender> & sender)263     void CancelWantSender(const sptr<IWantSender> &sender) override
264     {}
265 
GetPendingWantUid(const sptr<IWantSender> & target)266     int GetPendingWantUid(const sptr<IWantSender> &target) override
267     {
268         return 1;
269     }
270 
GetPendingWantUserId(const sptr<IWantSender> & target)271     int GetPendingWantUserId(const sptr<IWantSender> &target) override
272     {
273         return 1;
274     }
275 
GetPendingWantBundleName(const sptr<IWantSender> & target)276     std::string GetPendingWantBundleName(const sptr<IWantSender> &target) override
277     {
278         return "";
279     }
280 
GetPendingWantCode(const sptr<IWantSender> & target)281     int GetPendingWantCode(const sptr<IWantSender> &target) override
282     {
283         return 1;
284     }
285 
GetPendingWantType(const sptr<IWantSender> & target)286     int GetPendingWantType(const sptr<IWantSender> &target) override
287     {
288         return 1;
289     }
290 
RegisterCancelListener(const sptr<IWantSender> & sender,const sptr<IWantReceiver> & receiver)291     void RegisterCancelListener(const sptr<IWantSender> &sender, const sptr<IWantReceiver> &receiver) override
292     {}
293 
UnregisterCancelListener(const sptr<IWantSender> & sender,const sptr<IWantReceiver> & receiver)294     void UnregisterCancelListener(const sptr<IWantSender> &sender, const sptr<IWantReceiver> &receiver) override
295     {}
296 
GetPendingRequestWant(const sptr<IWantSender> & target,std::shared_ptr<Want> & want)297     int GetPendingRequestWant(const sptr<IWantSender> &target, std::shared_ptr<Want> &want) override
298     {
299         return 1;
300     }
301 
GetWantSenderInfo(const sptr<IWantSender> & target,std::shared_ptr<WantSenderInfo> & info)302     int GetWantSenderInfo(const sptr<IWantSender> &target, std::shared_ptr<WantSenderInfo> &info) override
303     {
304         return 1;
305     }
306 
ContinueMission(const std::string & srcDeviceId,const std::string & dstDeviceId,int32_t missionId,const sptr<IRemoteObject> & callBack,AAFwk::WantParams & wantParams)307     int ContinueMission(const std::string &srcDeviceId, const std::string &dstDeviceId, int32_t missionId,
308         const sptr<IRemoteObject> &callBack, AAFwk::WantParams &wantParams) override
309     {
310         return 1;
311     }
312 
ContinueAbility(const std::string & deviceId,int32_t missionId,uint32_t versionCode)313     int ContinueAbility(const std::string &deviceId, int32_t missionId, uint32_t versionCode) override
314     {
315         return 1;
316     }
317 
StartContinuation(const Want & want,const sptr<IRemoteObject> & abilityToken,int32_t status)318     int StartContinuation(const Want &want, const sptr<IRemoteObject> &abilityToken, int32_t status) override
319     {
320         return 1;
321     }
322 
NotifyCompleteContinuation(const std::string & deviceId,int32_t sessionId,bool isSuccess)323     void NotifyCompleteContinuation(const std::string &deviceId, int32_t sessionId, bool isSuccess) override
324     {}
325 
NotifyContinuationResult(int32_t missionId,int32_t result)326     int NotifyContinuationResult(int32_t missionId, int32_t result) override
327     {
328         return 1;
329     }
330 
LockMissionForCleanup(int32_t missionId)331     int LockMissionForCleanup(int32_t missionId) override
332     {
333         return 1;
334     }
335 
UnlockMissionForCleanup(int32_t missionId)336     int UnlockMissionForCleanup(int32_t missionId) override
337     {
338         return 1;
339     }
340 
RegisterMissionListener(const sptr<IMissionListener> & listener)341     int RegisterMissionListener(const sptr<IMissionListener> &listener) override
342     {
343         return 1;
344     }
345 
UnRegisterMissionListener(const sptr<IMissionListener> & listener)346     int UnRegisterMissionListener(const sptr<IMissionListener> &listener) override
347     {
348         return 1;
349     }
350 
GetMissionInfos(const std::string & deviceId,int32_t numMax,std::vector<MissionInfo> & missionInfos)351     int GetMissionInfos(
352         const std::string &deviceId, int32_t numMax, std::vector<MissionInfo> &missionInfos) override
353     {
354         return 1;
355     }
356 
GetMissionInfo(const std::string & deviceId,int32_t missionId,MissionInfo & missionInfo)357     int GetMissionInfo(const std::string &deviceId, int32_t missionId, MissionInfo &missionInfo) override
358     {
359         return 1;
360     }
361 
GetMissionSnapshot(const std::string & deviceId,int32_t missionId,MissionSnapshot & snapshot,bool isLowResolution)362     int GetMissionSnapshot(const std::string& deviceId, int32_t missionId,
363         MissionSnapshot& snapshot, bool isLowResolution) override
364     {
365         return 1;
366     }
367 
CleanMission(int32_t missionId)368     int CleanMission(int32_t missionId) override
369     {
370         return 1;
371     }
372 
CleanAllMissions()373     int CleanAllMissions() override
374     {
375         return 1;
376     }
377 
MoveMissionToFront(int32_t missionId)378     int MoveMissionToFront(int32_t missionId) override
379     {
380         return 1;
381     }
382 
MoveMissionToFront(int32_t missionId,const StartOptions & startOptions)383     int MoveMissionToFront(int32_t missionId, const StartOptions &startOptions) override
384     {
385         return 1;
386     }
387 
StartAbilityByCall(const Want & want,const sptr<IAbilityConnection> & connect,const sptr<IRemoteObject> & callerToken,int32_t accountId)388     int StartAbilityByCall(const Want &want, const sptr<IAbilityConnection> &connect,
389         const sptr<IRemoteObject> &callerToken, int32_t accountId) override
390     {
391         return 1;
392     }
393 
ReleaseCall(const sptr<IAbilityConnection> & connect,const AppExecFwk::ElementName & element)394     int ReleaseCall(const sptr<IAbilityConnection> &connect, const AppExecFwk::ElementName &element) override
395     {
396         return 1;
397     }
398 
StartUser(int userId,sptr<IUserCallback> callback)399     int StartUser(int userId, sptr<IUserCallback> callback) override
400     {
401         return 1;
402     }
403 
StopUser(int userId,const sptr<IUserCallback> & callback)404     int StopUser(int userId, const sptr<IUserCallback> &callback) override
405     {
406         return 1;
407     }
408 
SetMissionLabel(const sptr<IRemoteObject> & abilityToken,const std::string & label)409     int SetMissionLabel(const sptr<IRemoteObject> &abilityToken, const std::string &label) override
410     {
411         return 1;
412     }
413 
SetMissionIcon(const sptr<IRemoteObject> & token,const std::shared_ptr<OHOS::Media::PixelMap> & icon)414     int SetMissionIcon(const sptr<IRemoteObject> &token,
415         const std::shared_ptr<OHOS::Media::PixelMap> &icon) override
416     {
417         return 1;
418     }
419 
RegisterWindowManagerServiceHandler(const sptr<IWindowManagerServiceHandler> & handler,bool animationEnabled)420     int RegisterWindowManagerServiceHandler(const sptr<IWindowManagerServiceHandler>& handler,
421         bool animationEnabled) override
422     {
423         return 1;
424     }
425 
CompleteFirstFrameDrawing(const sptr<IRemoteObject> & abilityToken)426     void CompleteFirstFrameDrawing(const sptr<IRemoteObject> &abilityToken) override
427     {}
428 
GetAbilityRunningInfos(std::vector<AbilityRunningInfo> & info)429     int GetAbilityRunningInfos(std::vector<AbilityRunningInfo> &info) override
430     {
431         return 1;
432     }
433 
GetExtensionRunningInfos(int upperLimit,std::vector<ExtensionRunningInfo> & info)434     int GetExtensionRunningInfos(int upperLimit, std::vector<ExtensionRunningInfo> &info) override
435     {
436         return 1;
437     }
438 
GetProcessRunningInfos(std::vector<AppExecFwk::RunningProcessInfo> & info)439     int GetProcessRunningInfos(std::vector<AppExecFwk::RunningProcessInfo> &info) override
440     {
441         return 1;
442     }
443 
StartSyncRemoteMissions(const std::string & devId,bool fixConflict,int64_t tag)444     int StartSyncRemoteMissions(const std::string &devId, bool fixConflict, int64_t tag) override
445     {
446         return 1;
447     }
448 
StopSyncRemoteMissions(const std::string & devId)449     int StopSyncRemoteMissions(const std::string &devId) override
450     {
451         return 1;
452     }
453 
RegisterMissionListener(const std::string & deviceId,const sptr<IRemoteMissionListener> & listener)454     int RegisterMissionListener(const std::string &deviceId, const sptr<IRemoteMissionListener> &listener) override
455     {
456         return 1;
457     }
458 
UnRegisterMissionListener(const std::string & deviceId,const sptr<IRemoteMissionListener> & listener)459     int UnRegisterMissionListener(const std::string &deviceId,
460         const sptr<IRemoteMissionListener> &listener) override
461     {
462         return 1;
463     }
464 
SetAbilityController(const sptr<AppExecFwk::IAbilityController> & abilityController,bool imAStabilityTest)465     int SetAbilityController(const sptr<AppExecFwk::IAbilityController> &abilityController,
466         bool imAStabilityTest) override
467     {
468         return 1;
469     }
470 
IsRunningInStabilityTest()471     bool IsRunningInStabilityTest() override
472     {
473         return false;
474     }
475 
RegisterSnapshotHandler(const sptr<ISnapshotHandler> & handler)476     int RegisterSnapshotHandler(const sptr<ISnapshotHandler>& handler) override
477     {
478         return 1;
479     }
480 
StartUserTest(const Want & want,const sptr<IRemoteObject> & observer)481     int StartUserTest(const Want &want, const sptr<IRemoteObject> &observer) override
482     {
483         return 1;
484     }
485 
FinishUserTest(const std::string & msg,const int64_t & resultCode,const std::string & bundleName)486     int FinishUserTest(const std::string &msg, const int64_t &resultCode, const std::string &bundleName) override
487     {
488         return 1;
489     }
490 
GetTopAbility(sptr<IRemoteObject> & token)491     int GetTopAbility(sptr<IRemoteObject> &token) override
492     {
493         return 1;
494     }
495 
DelegatorDoAbilityForeground(const sptr<IRemoteObject> & token)496     int DelegatorDoAbilityForeground(const sptr<IRemoteObject> &token) override
497     {
498         return 1;
499     }
500 
DelegatorDoAbilityBackground(const sptr<IRemoteObject> & token)501     int DelegatorDoAbilityBackground(const sptr<IRemoteObject> &token) override
502     {
503         return 1;
504     }
505 
DoAbilityForeground(const sptr<IRemoteObject> & token,uint32_t flag)506     int DoAbilityForeground(const sptr<IRemoteObject> &token, uint32_t flag) override
507     {
508         return 1;
509     }
510 
DoAbilityBackground(const sptr<IRemoteObject> & token,uint32_t flag)511     int DoAbilityBackground(const sptr<IRemoteObject> &token, uint32_t flag) override
512     {
513         return 1;
514     }
515 
GetMissionIdByToken(const sptr<IRemoteObject> & token)516     int32_t GetMissionIdByToken(const sptr<IRemoteObject> &token) override
517     {
518         return 1;
519     }
520 
GetAbilityTokenByCalleeObj(const sptr<IRemoteObject> & callStub,sptr<IRemoteObject> & token)521     void GetAbilityTokenByCalleeObj(const sptr<IRemoteObject> &callStub, sptr<IRemoteObject> &token) override
522     {}
523 
IsValidMissionIds(const std::vector<int32_t> & missionIds,std::vector<MissionValidResult> & results)524     int32_t IsValidMissionIds(
525         const std::vector<int32_t> &missionIds, std::vector<MissionValidResult> &results) override
526     {
527         return 1;
528     }
529 
ReportDrawnCompleted(const sptr<IRemoteObject> & callerToken)530     int32_t ReportDrawnCompleted(const sptr<IRemoteObject> &callerToken) override
531     {
532         return 1;
533     }
534 
RegisterAppDebugListener(sptr<AppExecFwk::IAppDebugListener> listener)535     int32_t RegisterAppDebugListener(sptr<AppExecFwk::IAppDebugListener> listener) override
536     {
537         return 1;
538     }
539 
UnregisterAppDebugListener(sptr<AppExecFwk::IAppDebugListener> listener)540     int32_t UnregisterAppDebugListener(sptr<AppExecFwk::IAppDebugListener> listener) override
541     {
542         return 1;
543     }
544 
AttachAppDebug(const std::string & bundleName)545     int32_t AttachAppDebug(const std::string &bundleName) override
546     {
547         return 1;
548     }
549 
DetachAppDebug(const std::string & bundleName)550     int32_t DetachAppDebug(const std::string &bundleName) override
551     {
552         return 1;
553     }
554 
ExecuteIntent(uint64_t key,const sptr<IRemoteObject> & callerToken,const InsightIntentExecuteParam & param)555     int32_t ExecuteIntent(uint64_t key, const sptr<IRemoteObject> &callerToken,
556         const InsightIntentExecuteParam &param) override
557     {
558         return 1;
559     }
560 
ExecuteInsightIntentDone(const sptr<IRemoteObject> & token,uint64_t intentId,const InsightIntentExecuteResult & result)561     int32_t ExecuteInsightIntentDone(const sptr<IRemoteObject> &token, uint64_t intentId,
562         const InsightIntentExecuteResult &result) override
563     {
564         return 1;
565     }
566 
SetApplicationAutoStartupByEDM(const AutoStartupInfo & info,bool flag)567     int32_t SetApplicationAutoStartupByEDM(const AutoStartupInfo &info, bool flag) override
568     {
569         return 0;
570     }
571 
CancelApplicationAutoStartupByEDM(const AutoStartupInfo & info,bool flag)572     int32_t CancelApplicationAutoStartupByEDM(const AutoStartupInfo &info, bool flag) override
573     {
574         return 0;
575     }
576 
GetForegroundUIAbilities(std::vector<AppExecFwk::AbilityStateData> & list)577     int32_t GetForegroundUIAbilities(std::vector<AppExecFwk::AbilityStateData> &list) override
578     {
579         return 1;
580     }
581 
StartAbilityWithSpecifyTokenId(const Want & want,const sptr<IRemoteObject> & callerToken,uint32_t specifyTokenId,int32_t userId=DEFAULT_INVAL_VALUE,int requestCode=DEFAULT_INVAL_VALUE)582     int32_t StartAbilityWithSpecifyTokenId(const Want& want,
583         const sptr<IRemoteObject>& callerToken,
584         uint32_t specifyTokenId,
585         int32_t userId = DEFAULT_INVAL_VALUE,
586         int requestCode = DEFAULT_INVAL_VALUE) override
587     {
588         return 1;
589     }
590 };
591 
592 class EventReceiveStubTest : public EventReceiveStub {
593 public:
EventReceiveStubTest()594     EventReceiveStubTest()
595     {}
596 
~EventReceiveStubTest()597     ~EventReceiveStubTest()
598     {}
599 
NotifyEvent(const CommonEventData & commonEventData,const bool & ordered,const bool & sticky)600     virtual void NotifyEvent(const CommonEventData &commonEventData, const bool &ordered, const bool &sticky)
601     {}
602 };
603 
SetUpTestCase(void)604 void CommonEventSubscribeUnitTest::SetUpTestCase(void)
605 {
606     bundleObject = new MockBundleMgrService();
607     OHOS::DelayedSingleton<BundleManagerHelper>::GetInstance()->sptrBundleMgr_ =
608         OHOS::iface_cast<OHOS::AppExecFwk::IBundleMgr>(bundleObject);
609 }
610 
TearDownTestCase(void)611 void CommonEventSubscribeUnitTest::TearDownTestCase(void)
612 {}
613 
SetUp(void)614 void CommonEventSubscribeUnitTest::SetUp(void)
615 {}
616 
TearDown(void)617 void CommonEventSubscribeUnitTest::TearDown(void)
618 {}
619 
620 /*
621  * @tc.number: CommonEventSubscribeUnitTest_0300
622  * @tc.name: test subscribe event
623  * @tc.desc: InnerCommonEventManager subscribe common event success
624  */
625 HWTEST_F(CommonEventSubscribeUnitTest, CommonEventSubscribeUnitTest_0300, Function | MediumTest | Level1)
626 {
627     // make subscriber info
628     MatchingSkills matchingSkills;
629     matchingSkills.AddEvent(EVENT);
630     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
631 
632     // make subscriber
633     std::shared_ptr<SubscriberTest> subscriber = std::make_shared<SubscriberTest>(subscribeInfo);
634 
635     // make common event listener
636     OHOS::sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriber);
637     OHOS::sptr<OHOS::IRemoteObject> commonEventListenerPtr(commonEventListener);
638 
639     // SubscribeCommonEvent
640     struct tm curTime {0};
641     OHOS::Security::AccessToken::AccessTokenID tokenID = 0;
642     InnerCommonEventManager innerCommonEventManager;
643     const pid_t pid = 0;
644     const uid_t uid = 0;
645     EXPECT_TRUE(innerCommonEventManager.SubscribeCommonEvent(
646         subscribeInfo, commonEventListenerPtr, curTime, pid, uid, tokenID, ""));
647 }
648 
649 /*
650  * @tc.number: CommonEventSubscribeUnitTest_0400
651  * @tc.name: test subscribe event
652  * @tc.desc: InnerCommonEventManager subscribe common event fail because subscribeInfo has not event
653  */
654 HWTEST_F(CommonEventSubscribeUnitTest, CommonEventSubscribeUnitTest_0400, Function | MediumTest | Level1)
655 {
656     // make subscriber info
657     MatchingSkills matchingSkills;
658     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
659 
660     // make subscriber
661     std::shared_ptr<SubscriberTest> subscriber = std::make_shared<SubscriberTest>(subscribeInfo);
662 
663     // make common event listener
664     OHOS::sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriber);
665     OHOS::sptr<OHOS::IRemoteObject> commonEventListenerPtr(commonEventListener);
666 
667     // SubscribeCommonEvent
668     struct tm curTime {0};
669     OHOS::Security::AccessToken::AccessTokenID tokenID = 0;
670     InnerCommonEventManager innerCommonEventManager;
671     const pid_t pid = 0;
672     const uid_t uid = 0;
673     EXPECT_FALSE(innerCommonEventManager.SubscribeCommonEvent(
674         subscribeInfo, commonEventListenerPtr, curTime, pid, uid, tokenID, ""));
675 }
676 
677 /*
678  * @tc.number: CommonEventSubscribeUnitTest_0500
679  * @tc.name: test subscribe event
680  * @tc.desc: 1. subscribe common event
681  *                      2. fail subscribe common event, inner common event manager
682  *                          common event listener is null
683  */
684 HWTEST_F(CommonEventSubscribeUnitTest, CommonEventSubscribeUnitTest_0500, Function | MediumTest | Level1)
685 {
686     // make subscriber info
687     MatchingSkills matchingSkills;
688     matchingSkills.AddEvent(EVENT);
689     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
690 
691     // SubscribeCommonEvent
692     OHOS::sptr<OHOS::IRemoteObject> sp(nullptr);
693     struct tm curTime {0};
694     OHOS::Security::AccessToken::AccessTokenID tokenID = 0;
695     InnerCommonEventManager innerCommonEventManager;
696     const pid_t pid = 0;
697     const uid_t uid = 0;
698     EXPECT_FALSE(innerCommonEventManager.SubscribeCommonEvent(subscribeInfo, sp, curTime, pid, uid, tokenID, ""));
699 }
700 
701 /*
702  * @tc.number: CommonEventSubscribeUnitTest_0600
703  * @tc.name: test subscribe event
704  * @tc.desc: subscribe common event success
705  */
706 HWTEST_F(CommonEventSubscribeUnitTest, CommonEventSubscribeUnitTest_0600, Function | MediumTest | Level1)
707 {
708     // make subscriber info
709     MatchingSkills matchingSkills;
710     matchingSkills.AddEvent(EVENT);
711     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
712     std::shared_ptr<CommonEventSubscribeInfo> subscribeInfoPtr =
713         std::make_shared<CommonEventSubscribeInfo>(subscribeInfo);
714 
715     // make subscriber
716     std::shared_ptr<SubscriberTest> subscriber = std::make_shared<SubscriberTest>(subscribeInfo);
717 
718     // make common event listener
719     OHOS::sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriber);
720     OHOS::sptr<OHOS::IRemoteObject> commonEventListenerPtr(commonEventListener);
721 
722     // InsertSubscriber
723     struct tm curTime {0};
724     EventRecordInfo eventRecordInfo;
725     eventRecordInfo.pid = 0;
726     eventRecordInfo.uid = 0;
727     eventRecordInfo.bundleName = "bundleName";
728     CommonEventSubscriberManager commonEventSubscriberManager;
729     auto result = commonEventSubscriberManager.InsertSubscriber(
730         subscribeInfoPtr, commonEventListener, curTime, eventRecordInfo);
731     EXPECT_NE(nullptr, result);
732 }
733 
734 /*
735  * @tc.number: CommonEventSubscribeUnitTest_0700
736  * @tc.name: test subscribe event
737  * @tc.desc: 1. subscribe common event
738  *                      2. fail subscribe common event , common event subscriber manager
739  *                          event subscriber info is null
740  */
741 HWTEST_F(CommonEventSubscribeUnitTest, CommonEventSubscribeUnitTest_0700, Function | MediumTest | Level1)
742 {
743     // make subscriber info
744     MatchingSkills matchingSkills;
745     matchingSkills.AddEvent(EVENT);
746     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
747 
748     // make subscriber
749     std::shared_ptr<SubscriberTest> subscriber = std::make_shared<SubscriberTest>(subscribeInfo);
750 
751     // make common event listener
752     OHOS::sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriber);
753     OHOS::sptr<OHOS::IRemoteObject> commonEventListenerPtr(commonEventListener);
754 
755     // InsertSubscriber
756     struct tm curTime {0};
757     EventRecordInfo eventRecordInfo;
758     eventRecordInfo.pid = 0;
759     eventRecordInfo.uid = 0;
760     eventRecordInfo.bundleName = "bundleName";
761     CommonEventSubscriberManager commonEventSubscriberManager;
762     auto result =
763         commonEventSubscriberManager.InsertSubscriber(nullptr, commonEventListenerPtr, curTime, eventRecordInfo);
764     EXPECT_EQ(nullptr, result);
765 }
766 
767 /*
768  * @tc.number: CommonEventSubscribeUnitTest_0800
769  * @tc.name: test subscribe event
770  * @tc.desc: 1. subscribe common event
771  *                      2. fail subscribe common event , common event subscriber manager
772  *                          event common event listener is null
773  */
774 HWTEST_F(CommonEventSubscribeUnitTest, CommonEventSubscribeUnitTest_0800, Function | MediumTest | Level1)
775 {
776     // make subscriber info
777     MatchingSkills matchingSkills;
778     matchingSkills.AddEvent(EVENT);
779     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
780     std::shared_ptr<CommonEventSubscribeInfo> subscribeInfoPtr =
781         std::make_shared<CommonEventSubscribeInfo>(subscribeInfo);
782 
783     // make subscriber
784     std::shared_ptr<SubscriberTest> subscriber = std::make_shared<SubscriberTest>(subscribeInfo);
785 
786     // InsertSubscriber
787     struct tm curTime {0};
788     EventRecordInfo eventRecordInfo;
789     eventRecordInfo.pid = 0;
790     eventRecordInfo.uid = 0;
791     eventRecordInfo.bundleName = "bundleName";
792     CommonEventSubscriberManager commonEventSubscriberManager;
793     auto result = commonEventSubscriberManager.InsertSubscriber(subscribeInfoPtr, nullptr, curTime, eventRecordInfo);
794     EXPECT_EQ(nullptr, result);
795 }
796 
797 /*
798  * @tc.number: CommonEventSubscribeUnitTest_0900
799  * @tc.name: test subscribe event
800  * @tc.desc: 1. subscribe common event
801  *                      2. fail subscribe common event , common event subscriber manager
802  *                          event size is null
803  */
804 HWTEST_F(CommonEventSubscribeUnitTest, CommonEventSubscribeUnitTest_0900, Function | MediumTest | Level1)
805 {
806     // make subscriber info
807     MatchingSkills matchingSkills;
808     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
809     std::shared_ptr<CommonEventSubscribeInfo> subscribeInfoPtr =
810         std::make_shared<CommonEventSubscribeInfo>(subscribeInfo);
811 
812     // make subscriber
813     CommonEventStubTest CommonEventStubTest;
814     std::shared_ptr<SubscriberTest> subscriber = std::make_shared<SubscriberTest>(subscribeInfo);
815 
816     // make common event listener
817     OHOS::sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriber);
818     std::shared_ptr<InnerCommonEventManager> innerCommonEventManager = std::make_shared<InnerCommonEventManager>();
819     OHOS::sptr<OHOS::IRemoteObject> commonEventListenerPtr(commonEventListener);
820 
821     // InsertSubscriber
822     struct tm curTime {0};
823     EventRecordInfo eventRecordInfo;
824     eventRecordInfo.pid = 0;
825     eventRecordInfo.uid = 0;
826     eventRecordInfo.bundleName = "bundleName";
827     CommonEventSubscriberManager commonEventSubscriberManager;
828     auto result = commonEventSubscriberManager.InsertSubscriber(
829         subscribeInfoPtr, commonEventListenerPtr, curTime, eventRecordInfo);
830     EXPECT_EQ(nullptr, result);
831 }
832 
833 /*
834  * @tc.number: CommonEventSubscribeUnitTest_1000
835  * @tc.name: ttest subscribe event
836  * @tc.desc:  insert subscriber record locked success
837  */
838 HWTEST_F(CommonEventSubscribeUnitTest, CommonEventSubscribeUnitTest_1000, Function | MediumTest | Level1)
839 {
840     // make subscriber info
841     MatchingSkills matchingSkills;
842     matchingSkills.AddEvent(EVENT);
843     matchingSkills.AddEvent(ENTITY);
844     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
845 
846     // make subscriber
847     std::shared_ptr<SubscriberTest> subscriber = std::make_shared<SubscriberTest>(subscribeInfo);
848 
849     // make common event listener
850     OHOS::sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriber);
851     OHOS::sptr<OHOS::IRemoteObject> commonEventListenerPtr(commonEventListener);
852 
853     // make events
854     std::vector<std::string> events = subscribeInfo.GetMatchingSkills().GetEvents();
855 
856     // make record
857     struct tm recordTime {0};
858     auto record = std::make_shared<EventSubscriberRecord>();
859     record->eventSubscribeInfo = std::make_shared<CommonEventSubscribeInfo>(subscribeInfo);
860     record->commonEventListener = commonEventListenerPtr;
861     record->recordTime = recordTime;
862     record->eventRecordInfo.pid = 0;
863     record->eventRecordInfo.uid = 0;
864     record->eventRecordInfo.bundleName = "bundleName";
865 
866     // InsertSubscriberRecordLocked
867     CommonEventSubscriberManager commonEventSubscriberManager;
868     EXPECT_TRUE(commonEventSubscriberManager.InsertSubscriberRecordLocked(events, record));
869 }
870 
871 /*
872  * @tc.number: CommonEventSubscribeUnitTest_1100
873  * @tc.name: test subscribe event
874  * @tc.desc:  insert subscriber record locked fail because events size is 0
875  */
876 HWTEST_F(CommonEventSubscribeUnitTest, CommonEventSubscribeUnitTest_1100, Function | MediumTest | Level1)
877 {
878     // make subscriber info
879     MatchingSkills matchingSkills;
880     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
881 
882     // make subscriber
883     std::shared_ptr<SubscriberTest> subscriber = std::make_shared<SubscriberTest>(subscribeInfo);
884 
885     // make common event listener
886     OHOS::sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriber);
887     OHOS::sptr<OHOS::IRemoteObject> commonEventListenerPtr(commonEventListener);
888 
889     // make events
890     std::vector<std::string> events = subscribeInfo.GetMatchingSkills().GetEvents();
891 
892     // make record
893     struct tm recordTime {0};
894     auto record = std::make_shared<EventSubscriberRecord>();
895     record->eventSubscribeInfo = std::make_shared<CommonEventSubscribeInfo>(subscribeInfo);
896     record->commonEventListener = commonEventListenerPtr;
897     record->recordTime = recordTime;
898     record->eventRecordInfo.pid = 0;
899     record->eventRecordInfo.uid = 0;
900     record->eventRecordInfo.bundleName = "bundleName";
901 
902     // InsertSubscriberRecordLocked
903     CommonEventSubscriberManager commonEventSubscriberManager;
904     EXPECT_FALSE(commonEventSubscriberManager.InsertSubscriberRecordLocked(events, record));
905 }
906 
907 /*
908  * @tc.number: CommonEventSubscribeUnitTest_1200
909  * @tc.name: test subscribe event
910  * @tc.desc:  insert subscriber record locked fail because record is nullptr
911  */
912 HWTEST_F(CommonEventSubscribeUnitTest, CommonEventSubscribeUnitTest_1200, Function | MediumTest | Level1)
913 {
914     // make subscriber info
915     MatchingSkills matchingSkills;
916     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
917 
918     // make events
919     std::vector<std::string> events = subscribeInfo.GetMatchingSkills().GetEvents();
920     CommonEventSubscriberManager commonEventSubscriberManager;
921 
922     // InsertSubscriberRecordLocked
923     EXPECT_FALSE(commonEventSubscriberManager.InsertSubscriberRecordLocked(events, nullptr));
924 }
925 
926 /**
927  * @tc.name: AbilityManagerHelper_0001
928  * @tc.desc: GetAbilityMgrProxy
929  * @tc.type: FUNC
930  * @tc.require: I5R11Y
931  */
932 HWTEST_F(CommonEventSubscribeUnitTest, AbilityManagerHelper_0001, Function | MediumTest | Level1)
933 {
934     AbilityManagerHelper abilityManagerHelper;
935     abilityManagerHelper.abilityMgr_ = new (std::nothrow) TestAbilityMgr();
936     bool result = abilityManagerHelper.GetAbilityMgrProxy();
937     abilityManagerHelper.Clear();
938     EXPECT_EQ(result, true);
939 }
940 
941 /**
942  * @tc.name: AbilityManagerHelper_0002
943  * @tc.desc: ConnectAbility
944  * @tc.type: FUNC
945  * @tc.require: I5R11Y
946  */
947 HWTEST_F(CommonEventSubscribeUnitTest, AbilityManagerHelper_0002, Function | MediumTest | Level1)
948 {
949     // make a want
950     Want want;
951     want.SetAction(EVENT);
952 
953     // make common event data
954     CommonEventData event;
955     OHOS::sptr<OHOS::IRemoteObject> callerToken(nullptr);
956     const int32_t userId = 1;
957     AbilityManagerHelper abilityManagerHelper;
958     abilityManagerHelper.abilityMgr_ = new (std::nothrow) TestAbilityMgr();
959     int result = abilityManagerHelper.ConnectAbility(want, event, callerToken, userId);
960     EXPECT_EQ(result, 2097177);
961 }
962 
963 /**
964  * @tc.name: BundleManagerHelper_0001
965  * @tc.desc: QueryExtensionInfos
966  * @tc.type: FUNC
967  * @tc.require: I5R11Y
968  */
969 HWTEST_F(CommonEventSubscribeUnitTest, BundleManagerHelper_0001, Function | MediumTest | Level1)
970 {
971     std::vector<OHOS::AppExecFwk::ExtensionAbilityInfo> extensionInfos;
972     const int32_t userId = 3;
973     BundleManagerHelper bundleManagerHelper;
974     bool result = bundleManagerHelper.QueryExtensionInfos(extensionInfos, userId);
975     EXPECT_EQ(result, false);
976 }
977 
978 /**
979  * @tc.name: BundleManagerHelper_0002
980  * @tc.desc: GetResConfigFile
981  * @tc.type: FUNC
982  * @tc.require: I5R11Y
983  */
984 HWTEST_F(CommonEventSubscribeUnitTest, BundleManagerHelper_0002, Function | MediumTest | Level1)
985 {
986     OHOS::AppExecFwk::ExtensionAbilityInfo extension;
987     std::vector<std::string> profileInfos;
988     BundleManagerHelper bundleManagerHelper;
989     bool result = bundleManagerHelper.GetResConfigFile(extension, profileInfos);
990     EXPECT_EQ(result, false);
991 }
992 
993 /**
994  * @tc.name: CommonEventManagerService_0001
995  * @tc.desc: PublishCommonEvent
996  * @tc.type: FUNC
997  * @tc.require: I5R11Y
998  */
999 HWTEST_F(CommonEventSubscribeUnitTest, CommonEventManagerService_0001, Function | MediumTest | Level1)
1000 {
1001     // make a want
1002     Want want;
1003     want.SetAction(EVENT);
1004     // make common event data
1005     CommonEventData event;
1006     // make publish info
1007     CommonEventPublishInfo publishinfo;
1008     OHOS::sptr<OHOS::IRemoteObject> commonEventListener(nullptr);
1009     const uid_t uid = 1;
1010     const int32_t userId = 1;
1011     const int32_t tokenId = 0;
1012     CommonEventManagerService commonEventManagerService;
1013     bool result = commonEventManagerService.PublishCommonEvent(event, publishinfo, commonEventListener, uid,
1014         tokenId, userId);
1015     EXPECT_EQ(result, false);
1016 }
1017 
1018 /**
1019  * @tc.name: CommonEventManagerService_0002
1020  * @tc.desc: DumpState
1021  * @tc.type: FUNC
1022  * @tc.require: I5R11Y
1023  */
1024 HWTEST_F(CommonEventSubscribeUnitTest, CommonEventManagerService_0002, Function | MediumTest | Level1)
1025 {
1026     uint8_t dumpType = 1;
1027     std::string event = "Event";
1028     const int32_t userId =2;
1029     std::vector<std::string> state;
1030     CommonEventManagerService commonEventManagerService;
1031     bool result = commonEventManagerService.DumpState(dumpType, event, userId, state);
1032     EXPECT_EQ(result, false);
1033 }
1034 
1035 /**
1036  * @tc.name: CommonEventManagerService_0003
1037  * @tc.desc: FinishReceiver
1038  * @tc.type: FUNC
1039  * @tc.require: I5R11Y
1040  */
1041 HWTEST_F(CommonEventSubscribeUnitTest, CommonEventManagerService_0003, Function | MediumTest | Level1)
1042 {
1043     OHOS::sptr<OHOS::IRemoteObject> proxy(nullptr);
1044     int32_t code = 0;
1045     std::string receiverData = "receiverData";
1046     CommonEventManagerService commonEventManagerService;
1047     bool result = commonEventManagerService.FinishReceiver(proxy, code, receiverData, false);
1048     EXPECT_EQ(result, false);
1049 }
1050 
1051 /**
1052  * @tc.name: CommonEventManagerService_0004
1053  * @tc.desc: Dump
1054  * @tc.type: FUNC
1055  * @tc.require: I5R11Y
1056  */
1057 HWTEST_F(CommonEventSubscribeUnitTest, CommonEventManagerService_0004, Function | MediumTest | Level1)
1058 {
1059     int fd = 1;
1060     std::vector<std::u16string> args;
1061     CommonEventManagerService commonEventManagerService;
1062     int result = commonEventManagerService.Dump(fd, args);
1063     EXPECT_EQ(result, int(OHOS::ERR_INVALID_VALUE));
1064 }
1065