/* * Copyright (c) 2022-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #define private public #include "ability_manager_helper.h" #undef private #include "mock_common_event_stub.h" #include "mock_iabilitymanager.h" #include "static_subscriber_connection.h" using namespace testing::ext; using namespace OHOS; using namespace OHOS::EventFwk; using namespace OHOS::AppExecFwk; class AbilityManagerHelperTest : public testing::Test { public: AbilityManagerHelperTest() {} ~AbilityManagerHelperTest() {} static void SetUpTestCase(void); static void TearDownTestCase(void); void SetUp(); void TearDown(); }; void AbilityManagerHelperTest::SetUpTestCase(void) {} void AbilityManagerHelperTest::TearDownTestCase(void) {} void AbilityManagerHelperTest::SetUp(void) {} void AbilityManagerHelperTest::TearDown(void) {} using TestAbilityManager = AAFwk::IAbilityManager; class TestAbilityMgr : public TestAbilityManager { public: TestAbilityMgr() = default; virtual ~TestAbilityMgr() {}; sptr AsObject() override { return nullptr; } int StartAbility(const Want &want, int32_t userId, int requestCode) override { return 1; } int StartAbility( const Want &want, const sptr &callerToken, int32_t userId, int requestCode) override { return 1; } int StartAbility(const Want &want, const AbilityStartSetting &abilityStartSetting, const sptr &callerToken, int32_t userId, int requestCode) override { return 1; } int32_t StartAbilityByInsightIntent(const Want &want, const sptr &callerToken, uint64_t intentId, int32_t userId = DEFAULT_INVAL_VALUE) override { return 1; } int StartAbility(const Want &want, const StartOptions &startOptions, const sptr &callerToken, int32_t userId, int requestCode) override { return 1; } int TerminateAbility( const sptr &token, int resultCode, const Want *resultWant = nullptr) override { return 1; } int CloseAbility(const sptr &token, int resultCode = DEFAULT_INVAL_VALUE, const Want *resultWant = nullptr) override { return 1; } int MinimizeAbility(const sptr &token, bool fromUser = false) override { return 1; } int ConnectAbility(const Want &want, const sptr &connect, const sptr &callerToken, int32_t userId) override { return 1; } int DisconnectAbility(sptr connect) override { return 1; } sptr AcquireDataAbility( const Uri &uri, bool tryBind, const sptr &callerToken) override { return nullptr; } int ReleaseDataAbility( sptr dataAbilityScheduler, const sptr &callerToken) override { return 1; } int AttachAbilityThread(const sptr &scheduler, const sptr &token) override { return 1; } int AbilityTransitionDone(const sptr &token, int state, const PacMap &saveData)override { return 1; } int ScheduleConnectAbilityDone( const sptr &token, const sptr &remoteObject) override { return 1; } int ScheduleDisconnectAbilityDone(const sptr &token) override { return 1; } int ScheduleCommandAbilityDone(const sptr &token) override { return 1; } int ScheduleCommandAbilityWindowDone( const sptr &token, const sptr &sessionInfo, AAFwk::WindowCommand winCmd, AAFwk::AbilityCommand abilityCmd) override { return 1; } void DumpState(const std::string &args, std::vector &state) override {} void DumpSysState( const std::string& args, std::vector& state, bool isClient, bool isUserID, int UserID) override {} int StopServiceAbility(const Want &want, int32_t userId, const sptr &token = nullptr) override { return 1; } int KillProcess(const std::string &bundleName, const bool clearPageStack, int32_t appIndex) override { return 1; } int UninstallApp(const std::string &bundleName, int32_t uid) override { return 1; } sptr GetWantSender( const WantSenderInfo &wantSenderInfo, const sptr &callerToken, int32_t uid) override { return nullptr; } int SendWantSender(sptr target, SenderInfo &senderInfo) override { return 1; } void CancelWantSender(const sptr &sender) override {} int GetPendingWantUid(const sptr &target) override { return 1; } int GetPendingWantUserId(const sptr &target) override { return 1; } std::string GetPendingWantBundleName(const sptr &target) override { return ""; } int GetPendingWantCode(const sptr &target) override { return 1; } int GetPendingWantType(const sptr &target) override { return 1; } void RegisterCancelListener(const sptr &sender, const sptr &receiver) override {} void UnregisterCancelListener(const sptr &sender, const sptr &receiver) override {} int GetPendingRequestWant(const sptr &target, std::shared_ptr &want) override { return 1; } int GetWantSenderInfo(const sptr &target, std::shared_ptr &info) override { return 1; } int ContinueMission(const std::string &srcDeviceId, const std::string &dstDeviceId, int32_t missionId, const sptr &callBack, AAFwk::WantParams &wantParams) override { return 1; } int ContinueAbility(const std::string &deviceId, int32_t missionId, uint32_t versionCode) override { return 1; } int StartContinuation(const Want &want, const sptr &abilityToken, int32_t status) override { return 1; } void NotifyCompleteContinuation(const std::string &deviceId, int32_t sessionId, bool isSuccess) override {} int NotifyContinuationResult(int32_t missionId, int32_t result) override { return 1; } int LockMissionForCleanup(int32_t missionId) override { return 1; } int UnlockMissionForCleanup(int32_t missionId) override { return 1; } int RegisterMissionListener(const sptr &listener) override { return 1; } int UnRegisterMissionListener(const sptr &listener) override { return 1; } int GetMissionInfos( const std::string &deviceId, int32_t numMax, std::vector &missionInfos) override { return 1; } int GetMissionInfo(const std::string &deviceId, int32_t missionId, MissionInfo &missionInfo) override { return 1; } int GetMissionSnapshot(const std::string& deviceId, int32_t missionId, MissionSnapshot& snapshot, bool isLowResolution) override { return 1; } int CleanMission(int32_t missionId) override { return 1; } int CleanAllMissions() override { return 1; } int MoveMissionToFront(int32_t missionId) override { return 1; } int MoveMissionToFront(int32_t missionId, const StartOptions &startOptions) override { return 1; } int StartAbilityByCall(const Want &want, const sptr &connect, const sptr &callerToken, int32_t accountId, bool isSilent) override { return 1; } int ReleaseCall(const sptr &connect, const AppExecFwk::ElementName &element) override { return 1; } int StartUser(int userId, sptr callback, bool isAppRecovery) override { return 1; } int StopUser(int userId, const sptr &callback) override { return 1; } int SetMissionLabel(const sptr &abilityToken, const std::string &label) override { return 1; } int SetMissionIcon(const sptr &token, const std::shared_ptr &icon) override { return 1; } int RegisterWindowManagerServiceHandler(const sptr& handler, bool animationEnabled) override { return 1; } void CompleteFirstFrameDrawing(const sptr &abilityToken) override {} int GetAbilityRunningInfos(std::vector &info) override { return 1; } int GetExtensionRunningInfos(int upperLimit, std::vector &info) override { return 1; } int GetProcessRunningInfos(std::vector &info) override { return 1; } int StartSyncRemoteMissions(const std::string &devId, bool fixConflict, int64_t tag) override { return 1; } int StopSyncRemoteMissions(const std::string &devId) override { return 1; } int RegisterMissionListener(const std::string &deviceId, const sptr &listener) override { return 1; } int UnRegisterMissionListener(const std::string &deviceId, const sptr &listener) override { return 1; } int SetAbilityController(const sptr &abilityController, bool imAStabilityTest) override { return 1; } bool IsRunningInStabilityTest() override { return false; } int RegisterSnapshotHandler(const sptr& handler) override { return 1; } int StartUserTest(const Want &want, const sptr &observer) override { return 1; } int FinishUserTest(const std::string &msg, const int64_t &resultCode, const std::string &bundleName) override { return 1; } int GetTopAbility(sptr &token) override { return 1; } int DelegatorDoAbilityForeground(const sptr &token) override { return 1; } int DelegatorDoAbilityBackground(const sptr &token) override { return 1; } int DoAbilityForeground(const sptr &token, uint32_t flag) override { return 1; } int DoAbilityBackground(const sptr &token, uint32_t flag) override { return 1; } int32_t GetMissionIdByToken(const sptr &token) override { return 1; } void GetAbilityTokenByCalleeObj(const sptr &callStub, sptr &token) override {} int32_t IsValidMissionIds( const std::vector &missionIds, std::vector &results) override { return 1; } int32_t ReportDrawnCompleted(const sptr &callerToken) override { return 1; } int32_t RegisterAppDebugListener(sptr listener) override { return 1; } int32_t UnregisterAppDebugListener(sptr listener) override { return 1; } int32_t AttachAppDebug(const std::string &bundleName, bool isDebugFromLocal) override { return 1; } int32_t DetachAppDebug(const std::string &bundleName, bool isDebugFromLocal) override { return 1; } int32_t ExecuteIntent(uint64_t key, const sptr &callerToken, const InsightIntentExecuteParam ¶m) override { return 1; } int32_t ExecuteInsightIntentDone(const sptr &token, uint64_t intentId, const InsightIntentExecuteResult &result) override { return 1; } int32_t SetApplicationAutoStartupByEDM(const AutoStartupInfo &info, bool flag) override { return 0; } int32_t CancelApplicationAutoStartupByEDM(const AutoStartupInfo &info, bool flag) override { return 0; } int32_t GetForegroundUIAbilities(std::vector &list) override { return 1; } int32_t StartAbilityWithSpecifyTokenId(const Want& want, const sptr& callerToken, uint32_t specifyTokenId, int32_t userId = DEFAULT_INVAL_VALUE, int requestCode = DEFAULT_INVAL_VALUE) override { return 1; } int32_t UpdateKioskApplicationList(const std::vector &appList) override { return 1; } int32_t EnterKioskMode(sptr callerToken) override { return 1; } int32_t ExitKioskMode(sptr callerToken) override { return 1; } int32_t GetKioskStatus(AAFwk::KioskStatus &kioskStatus) override { return 1; } }; /** * @tc.name: AbilityManagerHelper_0100 * @tc.desc: test GetAbilityMgrProxy function and abilityMgr_ is not nullptr. * @tc.type: FUNC */ HWTEST_F(AbilityManagerHelperTest, AbilityManagerHelper_0100, Level1) { GTEST_LOG_(INFO) << "AbilityManagerHelper_0100 start"; AbilityManagerHelper abilityManagerHelper; abilityManagerHelper.abilityMgr_ = new (std::nothrow) TestAbilityMgr(); EXPECT_EQ(true, abilityManagerHelper.GetAbilityMgrProxy()); GTEST_LOG_(INFO) << "AbilityManagerHelper_0100 end"; } /** * @tc.name: AbilityManagerHelper_0200 * @tc.desc: test GetAbilityMgrProxy function and abilityMgr_ is nullptr. * @tc.type: FUNC */ HWTEST_F(AbilityManagerHelperTest, AbilityManagerHelper_0200, Level1) { GTEST_LOG_(INFO) << "AbilityManagerHelper_0200 start"; AbilityManagerHelper abilityManagerHelper; abilityManagerHelper.abilityMgr_ = nullptr; EXPECT_EQ(false, abilityManagerHelper.GetAbilityMgrProxy()); GTEST_LOG_(INFO) << "AbilityManagerHelper_0200 end"; } /** * @tc.name: AbilityManagerHelper_0300 * @tc.desc: test ConnectAbility function and GetAbilityMgrProxy is false. * @tc.type: FUNC */ HWTEST_F(AbilityManagerHelperTest, AbilityManagerHelper_0300, Level1) { GTEST_LOG_(INFO) << "AbilityManagerHelper_0300 start"; AbilityManagerHelper abilityManagerHelper; abilityManagerHelper.abilityMgr_ = nullptr; Want want; CommonEventData event; sptr callerToken = nullptr; const int32_t userId = 1; EXPECT_EQ(-1, abilityManagerHelper.ConnectAbility(want, event, callerToken, userId)); GTEST_LOG_(INFO) << "AbilityManagerHelper_0300 end"; } /** * @tc.name: AbilityManagerHelper_0400 * @tc.desc: test Clear function and abilityMgr_ is nullptr. * @tc.type: FUNC */ HWTEST_F(AbilityManagerHelperTest, AbilityManagerHelper_0400, Level1) { GTEST_LOG_(INFO) << "AbilityManagerHelper_0400 start"; std::shared_ptr abilityManagerHelper = std::make_shared(); ASSERT_NE(nullptr, abilityManagerHelper); abilityManagerHelper->abilityMgr_ = nullptr; abilityManagerHelper->Clear(); GTEST_LOG_(INFO) << "AbilityManagerHelper_0400 end"; } /** * @tc.name: AbilityManagerHelper_0500 * @tc.desc: test Clear function and abilityMgr_ is not nullptr. * @tc.type: FUNC */ HWTEST_F(AbilityManagerHelperTest, AbilityManagerHelper_0500, Level1) { GTEST_LOG_(INFO) << "AbilityManagerHelper_0500 start"; std::shared_ptr abilityManagerHelper = std::make_shared(); ASSERT_NE(nullptr, abilityManagerHelper); sptr remoteObject = sptr(new MockCommonEventStub()); abilityManagerHelper->abilityMgr_ = iface_cast(remoteObject); abilityManagerHelper->Clear(); GTEST_LOG_(INFO) << "AbilityManagerHelper_0500 end"; } /** * @tc.name : test SetEventHandler * @tc.number: AbilityManagerHelper_0600 * @tc.desc : Test SetEventHandler succeeded. */ HWTEST_F(AbilityManagerHelperTest, AbilityManagerHelper_0600, Level1) { GTEST_LOG_(INFO) << "AbilityManagerHelper_0600 start"; auto abilityManagerHelper = std::make_shared(); CommonEventData data; sptr connection = new (std::nothrow) StaticSubscriberConnection(data, ""); abilityManagerHelper->subscriberConnection_.emplace("test_key", connection); auto handler = std::make_shared(EventRunner::Create()); abilityManagerHelper->SetEventHandler(handler); EXPECT_NE(abilityManagerHelper->subscriberConnection_.size(), 0); GTEST_LOG_(INFO) << "AbilityManagerHelper_0600 end"; } /** * @tc.name : test DisconnectAbility * @tc.number: AbilityManagerHelper_0700 * @tc.desc : Test the DisconnectAbility function when the connection is nullptr. */ HWTEST_F(AbilityManagerHelperTest, AbilityManagerHelper_0700, Level1) { GTEST_LOG_(INFO) << "AbilityManagerHelper_0700 start"; auto abilityManagerHelper = std::make_shared(); CommonEventData data; sptr connection = new (std::nothrow) StaticSubscriberConnection(data, ""); abilityManagerHelper->subscriberConnection_.emplace("test_key", connection); abilityManagerHelper->DisconnectAbility(nullptr, ""); EXPECT_NE(abilityManagerHelper->subscriberConnection_.size(), 0); GTEST_LOG_(INFO) << "AbilityManagerHelper_0700 end"; } /** * @tc.name : test DisconnectAbility * @tc.number: AbilityManagerHelper_0800 * @tc.desc : Test the DisconnectAbility function when the connection is not nullptr. */ HWTEST_F(AbilityManagerHelperTest, AbilityManagerHelper_0800, Level1) { GTEST_LOG_(INFO) << "AbilityManagerHelper_0800 start"; auto abilityManagerHelper = std::make_shared(); CommonEventData data; sptr connection = new (std::nothrow) StaticSubscriberConnection(data, ""); abilityManagerHelper->subscriberConnection_.emplace("test_key", connection); sptr connectionOne = new (std::nothrow) StaticSubscriberConnection(data, ""); abilityManagerHelper->DisconnectAbility(connectionOne, "test_action"); EXPECT_NE(abilityManagerHelper->subscriberConnection_.size(), 0); GTEST_LOG_(INFO) << "AbilityManagerHelper_0800 end"; } /** * @tc.name : test DisconnectServiceAbilityDelay * @tc.number: AbilityManagerHelper_0900 * @tc.desc : Test the DisconnectServiceAbilityDelay function when the connection is nullptr. */ HWTEST_F(AbilityManagerHelperTest, AbilityManagerHelper_0900, Level1) { GTEST_LOG_(INFO) << "AbilityManagerHelper_0900 start"; auto abilityManagerHelper = std::make_shared(); auto handler = std::make_shared(EventRunner::Create()); abilityManagerHelper->SetEventHandler(handler); CommonEventData data; sptr connection = new (std::nothrow) StaticSubscriberConnection(data, ""); abilityManagerHelper->subscriberConnection_.emplace("test_key", connection); abilityManagerHelper->DisconnectServiceAbilityDelay(nullptr, ""); EXPECT_NE(abilityManagerHelper->subscriberConnection_.size(), 0); GTEST_LOG_(INFO) << "AbilityManagerHelper_0900 end"; } /** * @tc.name : test DisconnectServiceAbilityDelay * @tc.number: AbilityManagerHelper_1000 * @tc.desc : Test the DisconnectServiceAbilityDelay function when the eventHandler_ is nullptr. */ HWTEST_F(AbilityManagerHelperTest, AbilityManagerHelper_1000, Level1) { GTEST_LOG_(INFO) << "AbilityManagerHelper_1000 start"; auto abilityManagerHelper = std::make_shared(); CommonEventData data; sptr connection = new (std::nothrow) StaticSubscriberConnection(data, ""); abilityManagerHelper->subscriberConnection_.emplace("test_key", connection); abilityManagerHelper->DisconnectServiceAbilityDelay(connection, "test_action"); EXPECT_NE(abilityManagerHelper->subscriberConnection_.size(), 0); GTEST_LOG_(INFO) << "AbilityManagerHelper_1000 end"; } /** * @tc.name : test DisconnectServiceAbilityDelay * @tc.number: AbilityManagerHelper_1100 * @tc.desc : Test the DisconnectServiceAbilityDelay function when the input parameters meet the requirements. */ HWTEST_F(AbilityManagerHelperTest, AbilityManagerHelper_1100, Level1) { GTEST_LOG_(INFO) << "AbilityManagerHelper_1100 start"; auto abilityManagerHelper = std::make_shared(); CommonEventData data; sptr firstConnection = new (std::nothrow) StaticSubscriberConnection(data, ""); abilityManagerHelper->subscriberConnection_.emplace("test_key", firstConnection); auto handler = std::make_shared(EventRunner::Create()); abilityManagerHelper->SetEventHandler(handler); sptr SecondConnection = new (std::nothrow) StaticSubscriberConnection(data, ""); abilityManagerHelper->DisconnectServiceAbilityDelay(SecondConnection, "test_action"); EXPECT_NE(abilityManagerHelper->subscriberConnection_.size(), 0); GTEST_LOG_(INFO) << "AbilityManagerHelper_1100 end"; } /** * @tc.name : test ConnectAbility multiple times * @tc.number: AbilityManagerHelper_1200 * @tc.desc : When calling the connection multiple times, the proxy_ is empty */ HWTEST_F(AbilityManagerHelperTest, AbilityManagerHelper_1200, Level1) { GTEST_LOG_(INFO) << "AbilityManagerHelper_1200 start"; auto abilityManagerHelper = std::make_shared(); sptr remoteObject = sptr(new MockCommonEventStub()); abilityManagerHelper->abilityMgr_ = new (std::nothrow) TestAbilityMgr(); Want want; CommonEventData event; sptr callerToken = nullptr; int32_t userId = 1; CommonEventData data; sptr connection = new (std::nothrow) StaticSubscriberConnection(data, ""); std::string connectionKey = want.GetBundle() + "_" + want.GetElement().GetAbilityName() + "_" + std::to_string(userId); abilityManagerHelper->subscriberConnection_[connectionKey] = connection; abilityManagerHelper->subscriberConnection_[connectionKey]->proxy_ = nullptr; int result1 = abilityManagerHelper->ConnectAbility(want, event, callerToken, userId); EXPECT_EQ(result1, ERR_OK); int result2 = abilityManagerHelper->ConnectAbility(want, event, callerToken, userId); EXPECT_EQ(result2, ERR_OK); int result3 = abilityManagerHelper->ConnectAbility(want, event, callerToken, userId); EXPECT_EQ(result3, ERR_OK); EXPECT_EQ(abilityManagerHelper->subscriberConnection_[connectionKey]->events_.size(), 4); GTEST_LOG_(INFO) << "AbilityManagerHelper_1200 end"; } /** * @tc.name : test ConnectAbility multiple times * @tc.number: AbilityManagerHelper_1300 * @tc.desc : When calling the connection multiple times, the proxy_ is not empty */ HWTEST_F(AbilityManagerHelperTest, AbilityManagerHelper_1300, Level1) { GTEST_LOG_(INFO) << "AbilityManagerHelper_1300 start"; auto abilityManagerHelper = std::make_shared(); sptr remoteObject = sptr(new MockCommonEventStub()); abilityManagerHelper->abilityMgr_ = new (std::nothrow) TestAbilityMgr(); Want want; CommonEventData event; sptr callerToken = nullptr; int32_t userId = 1; CommonEventData data; sptr connection = new (std::nothrow) StaticSubscriberConnection(data, ""); std::string connectionKey = want.GetBundle() + "_" + want.GetElement().GetAbilityName() + "_" + std::to_string(userId); abilityManagerHelper->subscriberConnection_[connectionKey] = connection; abilityManagerHelper->subscriberConnection_[connectionKey]->GetProxy(callerToken); int result1 = abilityManagerHelper->ConnectAbility(want, event, callerToken, userId); EXPECT_EQ(result1, ERR_OK); int result2 = abilityManagerHelper->ConnectAbility(want, event, callerToken, userId); EXPECT_EQ(result2, ERR_OK); int result3 = abilityManagerHelper->ConnectAbility(want, event, callerToken, userId); EXPECT_EQ(result3, ERR_OK); EXPECT_EQ(abilityManagerHelper->subscriberConnection_[connectionKey]->events_.size(), 1); GTEST_LOG_(INFO) << "AbilityManagerHelper_1300 end"; } /** * @tc.name : test DisconnectAbility when connection is not empty and IsEmptyAction returns false * @tc.number: AbilityManagerHelper_1400 * @tc.desc : Test DisconnectAbility function when action_ has multiple elements. */ HWTEST_F(AbilityManagerHelperTest, AbilityManagerHelper_1400, Level1) { GTEST_LOG_(INFO) << "AbilityManagerHelper_1400 start"; auto abilityManagerHelper = std::make_shared(); CommonEventData data; sptr connection = new (std::nothrow) StaticSubscriberConnection(data, ""); std::string action = "test_action"; connection->action_.push_back(action); connection->action_.push_back("other_action"); abilityManagerHelper->subscriberConnection_.emplace("test_key", connection); abilityManagerHelper->DisconnectAbility(connection, action); EXPECT_EQ(connection->action_.size(), 1); EXPECT_EQ(connection->action_[0], "other_action"); GTEST_LOG_(INFO) << "AbilityManagerHelper_1400 end"; } /** * @tc.name : ConnectAbility_RealDelay_Disconnect_0001 * @tc.desc : Verify that ConnectAbility auto-releases the connection after the delay task. * @tc.type: FUNC */ HWTEST_F(AbilityManagerHelperTest, ConnectAbility_RealDelay_Disconnect_0001, Level1) { auto abilityManagerHelper = std::make_shared(); auto mockAbilityMgr = new MockIAbilityManager(); abilityManagerHelper->abilityMgr_ = mockAbilityMgr; Want want; want.SetBundle("test.bundle"); ElementName element; element.SetAbilityName("TestAbility"); want.SetElement(element); CommonEventData event; sptr callerToken = nullptr; int32_t userId = 1; CommonEventData data; std::string connectionKey = want.GetBundle() + "_" + want.GetElement().GetAbilityName() + "_" + std::to_string(userId); EXPECT_CALL(*mockAbilityMgr, ConnectAbility(::testing::_, ::testing::_, ::testing::_, ::testing::_)) .Times(1) .WillOnce(::testing::Return(ERR_OK)); int result1 = abilityManagerHelper->ConnectAbility(want, event, callerToken, userId); auto it = abilityManagerHelper->subscriberConnection_.find(connectionKey); EXPECT_NE(it, abilityManagerHelper->subscriberConnection_.end()); ffrt::this_task::sleep_for(std::chrono::seconds(16)); it = abilityManagerHelper->subscriberConnection_.find(connectionKey); EXPECT_EQ(it, abilityManagerHelper->subscriberConnection_.end()); }