!2049 fix account subscribe

Merge pull request !2049 from steven_Q/0830
This commit is contained in:
openharmony_ci 2024-08-31 14:06:06 +00:00 committed by Gitee
commit d2fa28ac43
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 40 additions and 15 deletions

View File

@ -25,6 +25,7 @@
"vtable for OHOS::AccountSA::CreateOsAccountForDomainOptions"; "vtable for OHOS::AccountSA::CreateOsAccountForDomainOptions";
"OHOS::AccountSA::OsAccount::GetInstance()"; "OHOS::AccountSA::OsAccount::GetInstance()";
"OHOS::AccountSA::OsAccount::OsAccount()"; "OHOS::AccountSA::OsAccount::OsAccount()";
"OHOS::AccountSA::OsAccount::RestoreListenerRecords()";
"OHOS::AccountSA::OsAccount::CreateOsAccountEventListener(std::__h::shared_ptr<OHOS::AccountSA::OsAccountSubscriber> const&, OHOS::sptr<OHOS::IRemoteObject>&)"; "OHOS::AccountSA::OsAccount::CreateOsAccountEventListener(std::__h::shared_ptr<OHOS::AccountSA::OsAccountSubscriber> const&, OHOS::sptr<OHOS::IRemoteObject>&)";
"OHOS::AccountSA::OsAccount::CreateOsAccount(std::__h::basic_string<char, std::__h::char_traits<char>, std::__h::allocator<char>> const&, OHOS::AccountSA::OsAccountType const&, OHOS::AccountSA::OsAccountInfo&)"; "OHOS::AccountSA::OsAccount::CreateOsAccount(std::__h::basic_string<char, std::__h::char_traits<char>, std::__h::allocator<char>> const&, OHOS::AccountSA::OsAccountType const&, OHOS::AccountSA::OsAccountInfo&)";
"OHOS::AccountSA::OsAccount::CreateOsAccount(std::__h::basic_string<char, std::__h::char_traits<char>, std::__h::allocator<char>> const&, std::__h::basic_string<char, std::__h::char_traits<char>, std::__h::allocator<char>> const&, OHOS::AccountSA::OsAccountType const&, OHOS::AccountSA::OsAccountInfo&)"; "OHOS::AccountSA::OsAccount::CreateOsAccount(std::__h::basic_string<char, std::__h::char_traits<char>, std::__h::allocator<char>> const&, std::__h::basic_string<char, std::__h::char_traits<char>, std::__h::allocator<char>> const&, OHOS::AccountSA::OsAccountType const&, OHOS::AccountSA::OsAccountInfo&)";

View File

@ -39,9 +39,11 @@
#include "account_file_operator.h" #include "account_file_operator.h"
#undef private #undef private
#include "os_account_constants.h" #include "os_account_constants.h"
#ifdef BUNDLE_ADAPTER_MOCK
#define private public #define private public
#include "os_account.h" #include "os_account.h"
#undef private
#ifdef BUNDLE_ADAPTER_MOCK
#define private public
#include "os_account_manager_service.h" #include "os_account_manager_service.h"
#include "os_account_proxy.h" #include "os_account_proxy.h"
#include "iinner_os_account_manager.h" #include "iinner_os_account_manager.h"
@ -95,6 +97,12 @@ const std::vector<std::string> CONSTANTS_VECTOR {
const std::vector<std::string> CONSTANTS_VECTOR_TEST { const std::vector<std::string> CONSTANTS_VECTOR_TEST {
"constraint.private.dns.set", "constraint.private.dns.set",
}; };
const std::vector<std::string> 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 CONSTRAINT_PRIVATE_DNS_SET = "constraint.private.dns.set";
const std::string CONSTANT_WIFI = "constraint.wifi"; const std::string CONSTANT_WIFI = "constraint.wifi";
@ -350,6 +358,7 @@ private:
}; };
#ifdef ENABLE_MULTIPLE_OS_ACCOUNTS #ifdef ENABLE_MULTIPLE_OS_ACCOUNTS
#ifndef BUNDLE_ADAPTER_MOCK
static void Wait(const std::shared_ptr<AccountTestEventSubscriber> &ptr) static void Wait(const std::shared_ptr<AccountTestEventSubscriber> &ptr)
{ {
std::unique_lock<std::mutex> lock(ptr->mutex); std::unique_lock<std::mutex> lock(ptr->mutex);
@ -357,6 +366,7 @@ static void Wait(const std::shared_ptr<AccountTestEventSubscriber> &ptr)
[lockPtr = ptr]() { return lockPtr->stoppingEventReady && lockPtr->stoppedEventReady; }); [lockPtr = ptr]() { return lockPtr->stoppingEventReady && lockPtr->stoppedEventReady; });
} }
#endif #endif
#endif
class MockOsAccountSubscriber { class MockOsAccountSubscriber {
public: public:
@ -2529,14 +2539,13 @@ HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest113, TestSize.Lev
HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest114, TestSize.Level1) HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest114, TestSize.Level1)
{ {
AccessTokenID tokenID; AccessTokenID tokenID;
ASSERT_TRUE(AllocPermission( ASSERT_TRUE(AllocPermission(PERMISSION_LIST, tokenID));
{"ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS", "ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS_EXTENSION"},
tokenID));
OsAccountInfo osAccountInfo; OsAccountInfo osAccountInfo;
EXPECT_EQ(OsAccountManager::CreateOsAccount("ModuleTest114", OsAccountType::NORMAL, osAccountInfo), ERR_OK); EXPECT_EQ(OsAccountManager::CreateOsAccount("ModuleTest114", OsAccountType::NORMAL, osAccountInfo), ERR_OK);
EXPECT_EQ(OsAccountManager::ActivateOsAccount(osAccountInfo.GetLocalId()), ERR_OK); EXPECT_EQ(OsAccountManager::ActivateOsAccount(osAccountInfo.GetLocalId()), ERR_OK);
#ifndef BUNDLE_ADAPTER_MOCK
EventFwk::MatchingSkills matchingSkills; EventFwk::MatchingSkills matchingSkills;
matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_STOPPING); matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_STOPPING);
matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_STOPPED); matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_STOPPED);
@ -2544,42 +2553,42 @@ HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest114, TestSize.Lev
auto listener = std::make_shared<MockSubscriberListener>(); auto listener = std::make_shared<MockSubscriberListener>();
std::shared_ptr<AccountTestEventSubscriber> subscriberPtr = std::shared_ptr<AccountTestEventSubscriber> subscriberPtr =
std::make_shared<AccountTestEventSubscriber>(subscribeInfo, listener); std::make_shared<AccountTestEventSubscriber>(subscribeInfo, listener);
ASSERT_NE(subscriberPtr, nullptr);
ASSERT_EQ(EventFwk::CommonEventManager::SubscribeCommonEvent(subscriberPtr), true); 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_STOPPING)).Times(Exactly(1));
EXPECT_CALL(*listener, OnReceiveEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_STOPPED)).Times(Exactly(1)); EXPECT_CALL(*listener, OnReceiveEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_STOPPED)).Times(Exactly(1));
#endif
OsAccountSubscribeInfo subscribeStoppingInfo; OsAccountSubscribeInfo subscribeStoppingInfo(OS_ACCOUNT_SUBSCRIBE_TYPE::STOPPING, "subscribeStopping");
subscribeStoppingInfo.SetOsAccountSubscribeType(OS_ACCOUNT_SUBSCRIBE_TYPE::STOPPING);
subscribeStoppingInfo.SetName("subscribeStopping");
auto stoppingPtr = std::make_shared<MockOsAccountSubscriber>(); auto stoppingPtr = std::make_shared<MockOsAccountSubscriber>();
ASSERT_NE(nullptr, stoppingPtr);
auto stoppingSubscriber = std::make_shared<DeactivateOsAccountSubscriber>(subscribeStoppingInfo, stoppingPtr); auto stoppingSubscriber = std::make_shared<DeactivateOsAccountSubscriber>(subscribeStoppingInfo, stoppingPtr);
ASSERT_NE(nullptr, stoppingSubscriber);
EXPECT_EQ(ERR_OK, OsAccountManager::SubscribeOsAccount(stoppingSubscriber)); EXPECT_EQ(ERR_OK, OsAccountManager::SubscribeOsAccount(stoppingSubscriber));
EXPECT_CALL(*stoppingPtr, OnAccountsChanged(osAccountInfo.GetLocalId())).Times(Exactly(1)); EXPECT_CALL(*stoppingPtr, OnAccountsChanged(osAccountInfo.GetLocalId())).Times(Exactly(1));
OsAccountSubscribeInfo subscribeStoppedInfo; OsAccountSubscribeInfo subscribeStoppedInfo(OS_ACCOUNT_SUBSCRIBE_TYPE::STOPPED, "subscribeStopped");
subscribeStoppedInfo.SetOsAccountSubscribeType(OS_ACCOUNT_SUBSCRIBE_TYPE::STOPPED);
subscribeStoppedInfo.SetName("subscribeStopped");
auto stoppedPtr = std::make_shared<MockOsAccountSubscriber>(); auto stoppedPtr = std::make_shared<MockOsAccountSubscriber>();
ASSERT_NE(nullptr, stoppedPtr);
auto stoppedSubscriber = std::make_shared<DeactivateOsAccountSubscriber>(subscribeStoppedInfo, stoppedPtr); auto stoppedSubscriber = std::make_shared<DeactivateOsAccountSubscriber>(subscribeStoppedInfo, stoppedPtr);
ASSERT_NE(nullptr, stoppedSubscriber);
EXPECT_EQ(ERR_OK, OsAccountManager::SubscribeOsAccount(stoppedSubscriber)); EXPECT_EQ(ERR_OK, OsAccountManager::SubscribeOsAccount(stoppedSubscriber));
EXPECT_CALL(*stoppedPtr, OnAccountsChanged(osAccountInfo.GetLocalId())).Times(Exactly(1)); EXPECT_CALL(*stoppedPtr, OnAccountsChanged(osAccountInfo.GetLocalId())).Times(Exactly(1));
OsAccount::GetInstance().RestoreListenerRecords();
EXPECT_EQ(OsAccountManager::DeactivateOsAccount(osAccountInfo.GetLocalId()), ERR_OK); EXPECT_EQ(OsAccountManager::DeactivateOsAccount(osAccountInfo.GetLocalId()), ERR_OK);
#ifndef BUNDLE_ADAPTER_MOCK
Wait(subscriberPtr); Wait(subscriberPtr);
#endif
Wait(stoppingSubscriber); Wait(stoppingSubscriber);
Wait(stoppedSubscriber); Wait(stoppedSubscriber);
#ifndef BUNDLE_ADAPTER_MOCK
EXPECT_EQ(EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true); EXPECT_EQ(EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
#endif
EXPECT_EQ(ERR_OK, OsAccountManager::UnsubscribeOsAccount(stoppingSubscriber)); EXPECT_EQ(ERR_OK, OsAccountManager::UnsubscribeOsAccount(stoppingSubscriber));
EXPECT_EQ(ERR_OK, OsAccountManager::UnsubscribeOsAccount(stoppedSubscriber)); EXPECT_EQ(ERR_OK, OsAccountManager::UnsubscribeOsAccount(stoppedSubscriber));
EXPECT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfo.GetLocalId()), ERR_OK); EXPECT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfo.GetLocalId()), ERR_OK);
#ifndef BUNDLE_ADAPTER_MOCK
testing::Mock::AllowLeak(listener.get()); testing::Mock::AllowLeak(listener.get());
#endif
ASSERT_TRUE(RecoveryPermission(tokenID)); ASSERT_TRUE(RecoveryPermission(tokenID));
} }
@ -2639,8 +2648,10 @@ HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest116, TestSize.Lev
auto switchingSubscriber = std::make_shared<ActiveOsAccountSubscriber>(subscribeSwitchingInfo); auto switchingSubscriber = std::make_shared<ActiveOsAccountSubscriber>(subscribeSwitchingInfo);
EXPECT_EQ(ERR_OK, OsAccountManager::SubscribeOsAccount(switchingSubscriber)); EXPECT_EQ(ERR_OK, OsAccountManager::SubscribeOsAccount(switchingSubscriber));
EXPECT_CALL(*switchingSubscriber, OnAccountsSwitch(account.GetLocalId(), MAIN_ACCOUNT_ID)).Times(Exactly(1)); 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 // common event: COMMON_EVENT_USER_FOREGROUND 、 COMMON_EVENT_USER_BACKGROUND
#ifndef BUNDLE_ADAPTER_MOCK
MatchingSkills matchingSkills; MatchingSkills matchingSkills;
matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_FOREGROUND); matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_FOREGROUND);
matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_BACKGROUND); matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_BACKGROUND);
@ -2651,6 +2662,7 @@ HWTEST_F(OsAccountManagerModuleTest, OsAccountManagerModuleTest116, TestSize.Lev
ASSERT_EQ(CommonEventManager::SubscribeCommonEvent(subscriberPtr), true); 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_BACKGROUND)).Times(Exactly(1));
EXPECT_CALL(*listener, OnReceiveEvent(CommonEventSupport::COMMON_EVENT_USER_FOREGROUND)).Times(Exactly(1)); EXPECT_CALL(*listener, OnReceiveEvent(CommonEventSupport::COMMON_EVENT_USER_FOREGROUND)).Times(Exactly(1));
#endif
EXPECT_EQ(OsAccountManager::ActivateOsAccount(account.GetLocalId()), ERR_OK); 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(activedSubscriber));
EXPECT_EQ(ERR_OK, OsAccountManager::UnsubscribeOsAccount(switchedSubscriber)); EXPECT_EQ(ERR_OK, OsAccountManager::UnsubscribeOsAccount(switchedSubscriber));
EXPECT_EQ(ERR_OK, OsAccountManager::UnsubscribeOsAccount(switchingSubscriber)); EXPECT_EQ(ERR_OK, OsAccountManager::UnsubscribeOsAccount(switchingSubscriber));
#ifndef BUNDLE_ADAPTER_MOCK
EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true); EXPECT_EQ(CommonEventManager::UnSubscribeCommonEvent(subscriberPtr), true);
#endif
EXPECT_EQ(OsAccountManager::DeactivateOsAccount(account.GetLocalId()), ERR_OK); EXPECT_EQ(OsAccountManager::DeactivateOsAccount(account.GetLocalId()), ERR_OK);
EXPECT_EQ(ERR_OK, OsAccountManager::RemoveOsAccount(account.GetLocalId())); EXPECT_EQ(ERR_OK, OsAccountManager::RemoveOsAccount(account.GetLocalId()));
#ifndef BUNDLE_ADAPTER_MOCK
testing::Mock::AllowLeak(listener.get()); testing::Mock::AllowLeak(listener.get());
#endif
ASSERT_TRUE(RecoveryPermission(tokenID)); ASSERT_TRUE(RecoveryPermission(tokenID));
} }

View File

@ -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); return innerManager_.SubscribeOsAccount(subscribeInfo, eventListener);
} }