From 12318b088bdcb2adee3452e6c095c861e84bae82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=98=89=E5=AE=9D?= Date: Sat, 23 Nov 2024 10:03:23 +0800 Subject: [PATCH] commit msg MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 黄嘉宝 --- .../system_ability_mgr_stub_unload_test.cpp | 133 ++++++++++-------- 1 file changed, 73 insertions(+), 60 deletions(-) diff --git a/services/samgr/native/test/unittest/src/system_ability_mgr_stub_unload_test.cpp b/services/samgr/native/test/unittest/src/system_ability_mgr_stub_unload_test.cpp index cb2c5f25..e6c6a12b 100644 --- a/services/samgr/native/test/unittest/src/system_ability_mgr_stub_unload_test.cpp +++ b/services/samgr/native/test/unittest/src/system_ability_mgr_stub_unload_test.cpp @@ -24,6 +24,7 @@ #include "string_ex.h" #include "system_ability_definition.h" #include "test_log.h" +#include "ability_death_recipient.h" #define private public #include "sa_status_change_mock.h" @@ -38,12 +39,24 @@ namespace OHOS { namespace { constexpr uint32_t SAID = 1499; constexpr int32_t INVALID_SAID = -1; + +void InitSaMgr(sptr& saMgr) +{ + saMgr->abilityDeath_ = sptr(new AbilityDeathRecipient()); + saMgr->systemProcessDeath_ = sptr(new SystemProcessDeathRecipient()); + saMgr->abilityStatusDeath_ = sptr(new AbilityStatusDeathRecipient()); + saMgr->abilityCallbackDeath_ = sptr(new AbilityCallbackDeathRecipient()); + saMgr->remoteCallbackDeath_ = sptr(new RemoteCallbackDeathRecipient()); + saMgr->workHandler_ = make_shared("workHandler"); + saMgr->collectManager_ = sptr(new DeviceStatusCollectManager()); + saMgr->abilityStateScheduler_ = std::make_shared(); +} } HWTEST_F(SystemAbilityMgrStubTest, UnSubsSystemAbilityInner002, TestSize.Level3) { - sptr saMgr = SystemAbilityManager::GetInstance(); - EXPECT_TRUE(saMgr != nullptr); + sptr saMgr = new SystemAbilityManager; + InitSaMgr(saMgr); MessageParcel data; MessageParcel reply; data.WriteInt32(INVALID_SAID); @@ -53,8 +66,8 @@ HWTEST_F(SystemAbilityMgrStubTest, UnSubsSystemAbilityInner002, TestSize.Level3) HWTEST_F(SystemAbilityMgrStubTest, UnSubsSystemAbilityInner003, TestSize.Level3) { - sptr saMgr = SystemAbilityManager::GetInstance(); - EXPECT_TRUE(saMgr != nullptr); + sptr saMgr = new SystemAbilityManager; + InitSaMgr(saMgr); MessageParcel data; MessageParcel reply; data.WriteInt32(SAID); @@ -64,8 +77,8 @@ HWTEST_F(SystemAbilityMgrStubTest, UnSubsSystemAbilityInner003, TestSize.Level3) HWTEST_F(SystemAbilityMgrStubTest, UnSubsSystemAbilityInner004, TestSize.Level1) { - sptr saMgr = SystemAbilityManager::GetInstance(); - EXPECT_TRUE(saMgr != nullptr); + sptr saMgr = new SystemAbilityManager; + InitSaMgr(saMgr); sptr testAbility(new SaStatusChangeMock()); SAInfo saInfo; saMgr->abilityMap_[SAID] = saInfo; @@ -79,8 +92,8 @@ HWTEST_F(SystemAbilityMgrStubTest, UnSubsSystemAbilityInner004, TestSize.Level1) HWTEST_F(SystemAbilityMgrStubTest, RemoveSystemAbilityInner002, TestSize.Level3) { - sptr saMgr = SystemAbilityManager::GetInstance(); - EXPECT_TRUE(saMgr != nullptr); + sptr saMgr = new SystemAbilityManager; + InitSaMgr(saMgr); MessageParcel data; MessageParcel reply; data.WriteInt32(INVALID_SAID); @@ -90,8 +103,8 @@ HWTEST_F(SystemAbilityMgrStubTest, RemoveSystemAbilityInner002, TestSize.Level3) HWTEST_F(SystemAbilityMgrStubTest, RemoveSystemAbilityInner003, TestSize.Level3) { - sptr saMgr = SystemAbilityManager::GetInstance(); - EXPECT_TRUE(saMgr != nullptr); + sptr saMgr = new SystemAbilityManager; + InitSaMgr(saMgr); MessageParcel data; MessageParcel reply; data.WriteInt32(SAID); @@ -102,24 +115,24 @@ HWTEST_F(SystemAbilityMgrStubTest, RemoveSystemAbilityInner003, TestSize.Level3) HWTEST_F(SystemAbilityMgrStubTest, RemoveSystemAbility001, TestSize.Level3) { - sptr saMgr = SystemAbilityManager::GetInstance(); - EXPECT_TRUE(saMgr != nullptr); + sptr saMgr = new SystemAbilityManager; + InitSaMgr(saMgr); int32_t res = saMgr->RemoveSystemAbility(INVALID_SAID); EXPECT_EQ(res, ERR_INVALID_VALUE); } HWTEST_F(SystemAbilityMgrStubTest, RemoveSystemAbility002, TestSize.Level3) { - sptr saMgr = SystemAbilityManager::GetInstance(); - EXPECT_TRUE(saMgr != nullptr); + sptr saMgr = new SystemAbilityManager; + InitSaMgr(saMgr); int32_t res = saMgr->RemoveSystemAbility(SAID); EXPECT_EQ(res, ERR_INVALID_VALUE); } HWTEST_F(SystemAbilityMgrStubTest, RemoveSystemAbility003, TestSize.Level3) { - sptr saMgr = SystemAbilityManager::GetInstance(); - EXPECT_TRUE(saMgr != nullptr); + sptr saMgr = new SystemAbilityManager; + InitSaMgr(saMgr); SAInfo saInfo; saInfo.remoteObj = nullptr; saMgr->abilityMap_[SAID] = saInfo; @@ -129,8 +142,8 @@ HWTEST_F(SystemAbilityMgrStubTest, RemoveSystemAbility003, TestSize.Level3) HWTEST_F(SystemAbilityMgrStubTest, RemoveSystemAbility004, TestSize.Level3) { - sptr saMgr = SystemAbilityManager::GetInstance(); - EXPECT_TRUE(saMgr != nullptr); + sptr saMgr = new SystemAbilityManager; + InitSaMgr(saMgr); SAInfo saInfo; saInfo.remoteObj = saMgr; saMgr->abilityMap_[SAID] = saInfo; @@ -142,16 +155,16 @@ HWTEST_F(SystemAbilityMgrStubTest, RemoveSystemAbility004, TestSize.Level3) HWTEST_F(SystemAbilityMgrStubTest, RemoveSystemAbility005, TestSize.Level3) { - sptr saMgr = SystemAbilityManager::GetInstance(); - EXPECT_TRUE(saMgr != nullptr); + sptr saMgr = new SystemAbilityManager; + InitSaMgr(saMgr); int32_t res = saMgr->RemoveSystemAbility(nullptr); EXPECT_EQ(res, ERR_INVALID_VALUE); } HWTEST_F(SystemAbilityMgrStubTest, RemoveSystemAbility006, TestSize.Level3) { - sptr saMgr = SystemAbilityManager::GetInstance(); - EXPECT_TRUE(saMgr != nullptr); + sptr saMgr = new SystemAbilityManager; + InitSaMgr(saMgr); SAInfo saInfo; saInfo.remoteObj = saMgr; saMgr->abilityMap_[SAID] = saInfo; @@ -162,8 +175,8 @@ HWTEST_F(SystemAbilityMgrStubTest, RemoveSystemAbility006, TestSize.Level3) HWTEST_F(SystemAbilityMgrStubTest, RemoveSystemAbility007, TestSize.Level3) { - sptr saMgr = SystemAbilityManager::GetInstance(); - EXPECT_TRUE(saMgr != nullptr); + sptr saMgr = new SystemAbilityManager; + InitSaMgr(saMgr); SAInfo saInfo; saInfo.remoteObj = saMgr; uint32_t saId = 0; @@ -175,8 +188,8 @@ HWTEST_F(SystemAbilityMgrStubTest, RemoveSystemAbility007, TestSize.Level3) HWTEST_F(SystemAbilityMgrStubTest, UnSubscribeSystemAbility001, TestSize.Level1) { - sptr saMgr = SystemAbilityManager::GetInstance(); - EXPECT_TRUE(saMgr != nullptr); + sptr saMgr = new SystemAbilityManager; + InitSaMgr(saMgr); sptr listener(new SaStatusChangeMock()); int32_t res = saMgr->UnSubscribeSystemAbility(INVALID_SAID, listener); u16string name = u"device_saname"; @@ -186,8 +199,8 @@ HWTEST_F(SystemAbilityMgrStubTest, UnSubscribeSystemAbility001, TestSize.Level1) HWTEST_F(SystemAbilityMgrStubTest, UnSubscribeSystemAbility002, TestSize.Level1) { - sptr saMgr = SystemAbilityManager::GetInstance(); - EXPECT_TRUE(saMgr != nullptr); + sptr saMgr = new SystemAbilityManager; + InitSaMgr(saMgr); sptr listener(nullptr); int32_t res = saMgr->UnSubscribeSystemAbility(SAID, listener); u16string name = u"deviceSaname"; @@ -199,8 +212,8 @@ HWTEST_F(SystemAbilityMgrStubTest, UnSubscribeSystemAbility002, TestSize.Level1) HWTEST_F(SystemAbilityMgrStubTest, UnSubscribeSystemAbility003, TestSize.Level1) { - sptr saMgr = SystemAbilityManager::GetInstance(); - EXPECT_TRUE(saMgr != nullptr); + sptr saMgr = new SystemAbilityManager; + InitSaMgr(saMgr); sptr listener(nullptr); int32_t res = saMgr->UnSubscribeSystemAbility(INVALID_SAID, listener); string deviceId = "device"; @@ -210,8 +223,8 @@ HWTEST_F(SystemAbilityMgrStubTest, UnSubscribeSystemAbility003, TestSize.Level1) HWTEST_F(SystemAbilityMgrStubTest, UnSubscribeSystemAbility004, TestSize.Level1) { - sptr saMgr = SystemAbilityManager::GetInstance(); - EXPECT_TRUE(saMgr != nullptr); + sptr saMgr = new SystemAbilityManager; + InitSaMgr(saMgr); sptr listener(new SaStatusChangeMock()); saMgr->listenerMap_[SAID].push_back({listener, SAID}); saMgr->abilityStatusDeath_ = nullptr; @@ -226,8 +239,8 @@ HWTEST_F(SystemAbilityMgrStubTest, UnSubscribeSystemAbility004, TestSize.Level1) HWTEST_F(SystemAbilityMgrStubTest, UnSubscribeSystemAbility005, TestSize.Level3) { - sptr saMgr = SystemAbilityManager::GetInstance(); - EXPECT_TRUE(saMgr != nullptr); + sptr saMgr = new SystemAbilityManager; + InitSaMgr(saMgr); sptr listener(new SaStatusChangeMock()); saMgr->listenerMap_[SAID].push_back({listener, SAID}); saMgr->abilityStatusDeath_ = nullptr; @@ -239,8 +252,8 @@ HWTEST_F(SystemAbilityMgrStubTest, UnSubscribeSystemAbility005, TestSize.Level3) HWTEST_F(SystemAbilityMgrStubTest, RemoveSystemProcess001, TestSize.Level1) { - sptr saMgr = SystemAbilityManager::GetInstance(); - EXPECT_TRUE(saMgr != nullptr); + sptr saMgr = new SystemAbilityManager; + InitSaMgr(saMgr); saMgr->workHandler_ = make_shared("workHandler"); sptr testAbility(nullptr); int32_t res = saMgr->RemoveSystemProcess(testAbility); @@ -262,8 +275,8 @@ HWTEST_F(SystemAbilityMgrStubTest, RemoveSystemProcess001, TestSize.Level1) */ HWTEST_F(SystemAbilityMgrStubTest, RemoveSystemProcess002, TestSize.Level3) { - sptr saMgr = SystemAbilityManager::GetInstance(); - EXPECT_TRUE(saMgr != nullptr); + sptr saMgr = new SystemAbilityManager; + InitSaMgr(saMgr); sptr testAbility(new SaStatusChangeMock()); u16string procName = u"proname"; int32_t res = saMgr->AddSystemProcess(procName, testAbility); @@ -282,8 +295,8 @@ HWTEST_F(SystemAbilityMgrStubTest, RemoveSystemProcess002, TestSize.Level3) */ HWTEST_F(SystemAbilityMgrStubTest, RemoveSystemProcess003, TestSize.Level3) { - sptr saMgr = SystemAbilityManager::GetInstance(); - EXPECT_TRUE(saMgr != nullptr); + sptr saMgr = new SystemAbilityManager; + InitSaMgr(saMgr); sptr testAbility(new SaStatusChangeMock()); saMgr->systemProcessMap_.clear(); int32_t res = saMgr->RemoveSystemProcess(testAbility); @@ -332,8 +345,8 @@ HWTEST_F(SystemAbilityMgrStubTest, OnRemoveSystemAbilityInner002, TestSize.Level */ HWTEST_F(SystemAbilityMgrStubTest, UnloadSystemAbilityInner001, TestSize.Level3) { - sptr saMgr = SystemAbilityManager::GetInstance(); - EXPECT_TRUE(saMgr != nullptr); + sptr saMgr = new SystemAbilityManager; + InitSaMgr(saMgr); MessageParcel data; MessageParcel reply; data.WriteInt32(INVALID_SAID); @@ -349,8 +362,8 @@ HWTEST_F(SystemAbilityMgrStubTest, UnloadSystemAbilityInner001, TestSize.Level3) */ HWTEST_F(SystemAbilityMgrStubTest, UnloadSystemAbilityInner002, TestSize.Level3) { - sptr saMgr = SystemAbilityManager::GetInstance(); - EXPECT_TRUE(saMgr != nullptr); + sptr saMgr = new SystemAbilityManager; + InitSaMgr(saMgr); MessageParcel data; MessageParcel reply; data.WriteInt32(SAID); @@ -366,8 +379,8 @@ HWTEST_F(SystemAbilityMgrStubTest, UnloadSystemAbilityInner002, TestSize.Level3) */ HWTEST_F(SystemAbilityMgrStubTest, UnloadSystemAbility001, TestSize.Level3) { - sptr saMgr = SystemAbilityManager::GetInstance(); - EXPECT_TRUE(saMgr != nullptr); + sptr saMgr = new SystemAbilityManager; + InitSaMgr(saMgr); int32_t result = saMgr->UnloadSystemAbility(INVALID_SAID); EXPECT_EQ(result, PROFILE_NOT_EXIST); } @@ -380,8 +393,8 @@ HWTEST_F(SystemAbilityMgrStubTest, UnloadSystemAbility001, TestSize.Level3) */ HWTEST_F(SystemAbilityMgrStubTest, UnloadSystemAbility002, TestSize.Level3) { - sptr saMgr = SystemAbilityManager::GetInstance(); - EXPECT_TRUE(saMgr != nullptr); + sptr saMgr = new SystemAbilityManager; + InitSaMgr(saMgr); int32_t result = saMgr->UnloadSystemAbility(SAID); EXPECT_EQ(result, INVALID_CALL_PROC); } @@ -394,8 +407,8 @@ HWTEST_F(SystemAbilityMgrStubTest, UnloadSystemAbility002, TestSize.Level3) */ HWTEST_F(SystemAbilityMgrStubTest, UnloadSystemAbility003, TestSize.Level3) { - sptr saMgr = SystemAbilityManager::GetInstance(); - EXPECT_TRUE(saMgr != nullptr); + sptr saMgr = new SystemAbilityManager; + InitSaMgr(saMgr); CommonSaProfile saProfile; saMgr->saProfileMap_[SAID] = saProfile; int32_t result = saMgr->UnloadSystemAbility(SAID); @@ -411,8 +424,8 @@ HWTEST_F(SystemAbilityMgrStubTest, UnloadSystemAbility003, TestSize.Level3) HWTEST_F(SystemAbilityMgrStubTest, UnloadSystemAbility004, TestSize.Level3) { SamMockPermission::MockProcess("invalidProcess"); - sptr saMgr = SystemAbilityManager::GetInstance(); - EXPECT_TRUE(saMgr != nullptr); + sptr saMgr = new SystemAbilityManager; + InitSaMgr(saMgr); CommonSaProfile saProfile; saProfile.process = u"testProcess"; saMgr->saProfileMap_[SAID] = saProfile; @@ -429,8 +442,8 @@ HWTEST_F(SystemAbilityMgrStubTest, UnloadSystemAbility004, TestSize.Level3) HWTEST_F(SystemAbilityMgrStubTest, UnloadSystemAbility005, TestSize.Level3) { SamMockPermission::MockProcess("memmgrservice"); - sptr saMgr = SystemAbilityManager::GetInstance(); - EXPECT_TRUE(saMgr != nullptr); + sptr saMgr = new SystemAbilityManager; + InitSaMgr(saMgr); saMgr->abilityStateScheduler_->processContextMap_.clear(); saMgr->abilityStateScheduler_->abilityContextMap_.clear(); CommonSaProfile saProfile; @@ -449,8 +462,8 @@ HWTEST_F(SystemAbilityMgrStubTest, UnloadSystemAbility005, TestSize.Level3) HWTEST_F(SystemAbilityMgrStubTest, UnSubscribeSystemProcessInner002, TestSize.Level3) { DTEST_LOG << "UnSubscribeSystemProcessInner002" << std::endl; - sptr saMgr = SystemAbilityManager::GetInstance(); - EXPECT_TRUE(saMgr != nullptr); + sptr saMgr = new SystemAbilityManager; + InitSaMgr(saMgr); MessageParcel data; data.WriteRemoteObject(nullptr); MessageParcel reply; @@ -461,8 +474,8 @@ HWTEST_F(SystemAbilityMgrStubTest, UnSubscribeSystemProcessInner002, TestSize.Le HWTEST_F(SystemAbilityMgrStubTest, UnSubscribeSystemProcessInner003, TestSize.Level3) { DTEST_LOG << "UnSubscribeSystemProcessInner003" << std::endl; - sptr saMgr = SystemAbilityManager::GetInstance(); - EXPECT_TRUE(saMgr != nullptr); + sptr saMgr = new SystemAbilityManager; + InitSaMgr(saMgr); MessageParcel data; sptr ptr = new SystemProcessStatusChange(); data.WriteRemoteObject(ptr); @@ -473,8 +486,8 @@ HWTEST_F(SystemAbilityMgrStubTest, UnSubscribeSystemProcessInner003, TestSize.Le HWTEST_F(SystemAbilityMgrStubTest, UnloadSystemAbilityInner003, TestSize.Level3) { - sptr saMgr = SystemAbilityManager::GetInstance(); - EXPECT_TRUE(saMgr != nullptr); + sptr saMgr = new SystemAbilityManager; + InitSaMgr(saMgr); MessageParcel data; MessageParcel reply; data.WriteInt32(SAID); -- Gitee