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