From 3545fab4369917d093b2fec73c78add4a770cde8 Mon Sep 17 00:00:00 2001 From: steven_q Date: Sat, 31 Aug 2024 20:55:23 +0800 Subject: [PATCH] fix account subscribe Signed-off-by: steven_q Change-Id: I85b8155577314a01e834618b4c96fbf2a2b863db --- .../native/libos_account_innerkits.map | 1 + .../os_account_manager_module_test.cpp | 46 +++++++++++++------ .../osaccount/os_account_manager_service.cpp | 8 ++++ 3 files changed, 40 insertions(+), 15 deletions(-) diff --git a/frameworks/osaccount/native/libos_account_innerkits.map b/frameworks/osaccount/native/libos_account_innerkits.map index ece71f7c8..dc4d19e0d 100644 --- a/frameworks/osaccount/native/libos_account_innerkits.map +++ b/frameworks/osaccount/native/libos_account_innerkits.map @@ -25,6 +25,7 @@ "vtable for OHOS::AccountSA::CreateOsAccountForDomainOptions"; "OHOS::AccountSA::OsAccount::GetInstance()"; "OHOS::AccountSA::OsAccount::OsAccount()"; + "OHOS::AccountSA::OsAccount::RestoreListenerRecords()"; "OHOS::AccountSA::OsAccount::CreateOsAccountEventListener(std::__h::shared_ptr const&, OHOS::sptr&)"; "OHOS::AccountSA::OsAccount::CreateOsAccount(std::__h::basic_string, std::__h::allocator> const&, OHOS::AccountSA::OsAccountType const&, OHOS::AccountSA::OsAccountInfo&)"; "OHOS::AccountSA::OsAccount::CreateOsAccount(std::__h::basic_string, std::__h::allocator> const&, std::__h::basic_string, std::__h::allocator> const&, OHOS::AccountSA::OsAccountType const&, OHOS::AccountSA::OsAccountInfo&)"; diff --git a/frameworks/osaccount/native/test/moduletest/os_account_manager_module_test.cpp b/frameworks/osaccount/native/test/moduletest/os_account_manager_module_test.cpp index a88f28a2c..628bde23a 100644 --- a/frameworks/osaccount/native/test/moduletest/os_account_manager_module_test.cpp +++ b/frameworks/osaccount/native/test/moduletest/os_account_manager_module_test.cpp @@ -39,9 +39,11 @@ #include "account_file_operator.h" #undef private #include "os_account_constants.h" -#ifdef BUNDLE_ADAPTER_MOCK #define private public #include "os_account.h" +#undef private +#ifdef BUNDLE_ADAPTER_MOCK +#define private public #include "os_account_manager_service.h" #include "os_account_proxy.h" #include "iinner_os_account_manager.h" @@ -95,6 +97,12 @@ const std::vector CONSTANTS_VECTOR { const std::vector CONSTANTS_VECTOR_TEST { "constraint.private.dns.set", }; + +const std::vector PERMISSION_LIST { + "ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS", + "ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS_EXTENSION" +}; + const std::string CONSTRAINT_PRIVATE_DNS_SET = "constraint.private.dns.set"; const std::string CONSTANT_WIFI = "constraint.wifi"; @@ -350,6 +358,7 @@ private: }; #ifdef ENABLE_MULTIPLE_OS_ACCOUNTS +#ifndef BUNDLE_ADAPTER_MOCK static void Wait(const std::shared_ptr &ptr) { std::unique_lock lock(ptr->mutex); @@ -357,6 +366,7 @@ static void Wait(const std::shared_ptr &ptr) [lockPtr = ptr]() { return lockPtr->stoppingEventReady && lockPtr->stoppedEventReady; }); } #endif +#endif class MockOsAccountSubscriber { public: @@ -2529,14 +2539,13 @@ HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest113, TestSize.Lev HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest114, TestSize.Level1) { AccessTokenID tokenID; - ASSERT_TRUE(AllocPermission( - {"ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS", "ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS_EXTENSION"}, - tokenID)); + ASSERT_TRUE(AllocPermission(PERMISSION_LIST, tokenID)); OsAccountInfo osAccountInfo; EXPECT_EQ(OsAccountManager::CreateOsAccount("ModuleTest114", OsAccountType::NORMAL, osAccountInfo), ERR_OK); EXPECT_EQ(OsAccountManager::ActivateOsAccount(osAccountInfo.GetLocalId()), ERR_OK); +#ifndef BUNDLE_ADAPTER_MOCK EventFwk::MatchingSkills matchingSkills; matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_STOPPING); matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_STOPPED); @@ -2544,42 +2553,42 @@ HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest114, TestSize.Lev auto listener = std::make_shared(); std::shared_ptr subscriberPtr = std::make_shared(subscribeInfo, listener); - ASSERT_NE(subscriberPtr, nullptr); ASSERT_EQ(EventFwk::CommonEventManager::SubscribeCommonEvent(subscriberPtr), true); EXPECT_CALL(*listener, OnReceiveEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_STOPPING)).Times(Exactly(1)); EXPECT_CALL(*listener, OnReceiveEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_STOPPED)).Times(Exactly(1)); +#endif - OsAccountSubscribeInfo subscribeStoppingInfo; - subscribeStoppingInfo.SetOsAccountSubscribeType(OS_ACCOUNT_SUBSCRIBE_TYPE::STOPPING); - subscribeStoppingInfo.SetName("subscribeStopping"); + OsAccountSubscribeInfo subscribeStoppingInfo(OS_ACCOUNT_SUBSCRIBE_TYPE::STOPPING, "subscribeStopping"); auto stoppingPtr = std::make_shared(); - ASSERT_NE(nullptr, stoppingPtr); auto stoppingSubscriber = std::make_shared(subscribeStoppingInfo, stoppingPtr); - ASSERT_NE(nullptr, stoppingSubscriber); EXPECT_EQ(ERR_OK, OsAccountManager::SubscribeOsAccount(stoppingSubscriber)); EXPECT_CALL(*stoppingPtr, OnAccountsChanged(osAccountInfo.GetLocalId())).Times(Exactly(1)); - OsAccountSubscribeInfo subscribeStoppedInfo; - subscribeStoppedInfo.SetOsAccountSubscribeType(OS_ACCOUNT_SUBSCRIBE_TYPE::STOPPED); - subscribeStoppedInfo.SetName("subscribeStopped"); + OsAccountSubscribeInfo subscribeStoppedInfo(OS_ACCOUNT_SUBSCRIBE_TYPE::STOPPED, "subscribeStopped"); auto stoppedPtr = std::make_shared(); - ASSERT_NE(nullptr, stoppedPtr); auto stoppedSubscriber = std::make_shared(subscribeStoppedInfo, stoppedPtr); - ASSERT_NE(nullptr, stoppedSubscriber); EXPECT_EQ(ERR_OK, OsAccountManager::SubscribeOsAccount(stoppedSubscriber)); EXPECT_CALL(*stoppedPtr, OnAccountsChanged(osAccountInfo.GetLocalId())).Times(Exactly(1)); + OsAccount::GetInstance().RestoreListenerRecords(); EXPECT_EQ(OsAccountManager::DeactivateOsAccount(osAccountInfo.GetLocalId()), ERR_OK); +#ifndef BUNDLE_ADAPTER_MOCK Wait(subscriberPtr); +#endif Wait(stoppingSubscriber); Wait(stoppedSubscriber); +#ifndef BUNDLE_ADAPTER_MOCK EXPECT_EQ(EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true); +#endif EXPECT_EQ(ERR_OK, OsAccountManager::UnsubscribeOsAccount(stoppingSubscriber)); EXPECT_EQ(ERR_OK, OsAccountManager::UnsubscribeOsAccount(stoppedSubscriber)); EXPECT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfo.GetLocalId()), ERR_OK); +#ifndef BUNDLE_ADAPTER_MOCK testing::Mock::AllowLeak(listener.get()); +#endif + ASSERT_TRUE(RecoveryPermission(tokenID)); } @@ -2639,8 +2648,10 @@ HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest116, TestSize.Lev auto switchingSubscriber = std::make_shared(subscribeSwitchingInfo); EXPECT_EQ(ERR_OK, OsAccountManager::SubscribeOsAccount(switchingSubscriber)); EXPECT_CALL(*switchingSubscriber, OnAccountsSwitch(account.GetLocalId(), MAIN_ACCOUNT_ID)).Times(Exactly(1)); + OsAccount::GetInstance().RestoreListenerRecords(); // common event: COMMON_EVENT_USER_FOREGROUND 、 COMMON_EVENT_USER_BACKGROUND +#ifndef BUNDLE_ADAPTER_MOCK MatchingSkills matchingSkills; matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_FOREGROUND); matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_BACKGROUND); @@ -2651,6 +2662,7 @@ HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest116, TestSize.Lev ASSERT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true); EXPECT_CALL(*listener, OnReceiveEvent(CommonEventSupport::COMMON_EVENT_USER_BACKGROUND)).Times(Exactly(1)); EXPECT_CALL(*listener, OnReceiveEvent(CommonEventSupport::COMMON_EVENT_USER_FOREGROUND)).Times(Exactly(1)); +#endif EXPECT_EQ(OsAccountManager::ActivateOsAccount(account.GetLocalId()), ERR_OK); @@ -2659,11 +2671,15 @@ HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest116, TestSize.Lev EXPECT_EQ(ERR_OK, OsAccountManager::UnsubscribeOsAccount(activedSubscriber)); EXPECT_EQ(ERR_OK, OsAccountManager::UnsubscribeOsAccount(switchedSubscriber)); EXPECT_EQ(ERR_OK, OsAccountManager::UnsubscribeOsAccount(switchingSubscriber)); +#ifndef BUNDLE_ADAPTER_MOCK EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true); +#endif EXPECT_EQ(OsAccountManager::DeactivateOsAccount(account.GetLocalId()), ERR_OK); EXPECT_EQ(ERR_OK, OsAccountManager::RemoveOsAccount(account.GetLocalId())); +#ifndef BUNDLE_ADAPTER_MOCK testing::Mock::AllowLeak(listener.get()); +#endif ASSERT_TRUE(RecoveryPermission(tokenID)); } diff --git a/services/accountmgr/src/osaccount/os_account_manager_service.cpp b/services/accountmgr/src/osaccount/os_account_manager_service.cpp index 7bd09f43a..1f4998ca1 100644 --- a/services/accountmgr/src/osaccount/os_account_manager_service.cpp +++ b/services/accountmgr/src/osaccount/os_account_manager_service.cpp @@ -709,6 +709,14 @@ ErrCode OsAccountManagerService::SubscribeOsAccount( } } + auto osSubscribeInfo = innerManager_.GetSubscribeRecordInfo(eventListener); + if (osSubscribeInfo != nullptr) { + std::string name; + osSubscribeInfo->GetName(name); + ACCOUNT_LOGI("Event listener %{public}s already exists.", name.c_str()); + return ERR_OK; + } + return innerManager_.SubscribeOsAccount(subscribeInfo, eventListener); }