From c0520679321a0986db9201f07d910c4855b27596 Mon Sep 17 00:00:00 2001 From: cheerful_ricky Date: Wed, 16 Apr 2025 16:41:41 +0800 Subject: [PATCH] refactor rdb cache and refactor some init action Signed-off-by: cheerful_ricky --- .../ans/include/notification_rdb_data_mgr.h | 5 +- .../advanced_notification_service_ability.cpp | 6 - ...veview_all_scenarios_extension_wrapper.cpp | 5 +- .../src/notification_extension_wrapper.cpp | 5 +- .../ans/src/notification_rdb_data_mgr.cpp | 185 +++++++++--------- .../notification_extension_wrapper_test.cpp | 3 + .../mock_rdb_helper.cpp | 14 ++ .../notification_rdb_data_mgr_test.cpp | 44 +++-- 8 files changed, 144 insertions(+), 123 deletions(-) diff --git a/services/ans/include/notification_rdb_data_mgr.h b/services/ans/include/notification_rdb_data_mgr.h index e8e980312..9e55df71d 100644 --- a/services/ans/include/notification_rdb_data_mgr.h +++ b/services/ans/include/notification_rdb_data_mgr.h @@ -149,6 +149,7 @@ public: int32_t DropUserTable(const int32_t userId); private: + std::shared_ptr GetConnection(); int32_t GetUserTableName(const int32_t &userId, std::string &tableName); std::vector GenerateOperatedTables(const int32_t &userId); int32_t DeleteData(const std::string tableName, const std::string key, int32_t &rowId); @@ -157,13 +158,11 @@ private: int32_t QueryDataBeginWithKey(const std::string tableName, const std::string key, std::unordered_map &values); int32_t QueryAllData(const std::string tableName, std::unordered_map &datas); - int32_t InitCreatedTables(); + int32_t InitCreatedTables(std::shared_ptr rdbConnection); int32_t RestoreForMasterSlaver(); private: NotificationRdbConfig notificationRdbConfig_; - std::shared_ptr rdbStore_; - mutable std::mutex rdbStorePtrMutex_; std::set createdTables_; mutable std::mutex createdTableMutex_; }; diff --git a/services/ans/src/advanced_notification_service_ability.cpp b/services/ans/src/advanced_notification_service_ability.cpp index b0e410cb3..a1d06afd4 100644 --- a/services/ans/src/advanced_notification_service_ability.cpp +++ b/services/ans/src/advanced_notification_service_ability.cpp @@ -51,11 +51,6 @@ void AdvancedNotificationServiceAbility::OnStart() return; } -#ifdef ENABLE_ANS_EXT_WRAPPER - EXTENTION_WRAPPER->InitExtentionWrapper(); -#else - ANS_LOGI("Not enabled ans_ext"); -#endif AddSystemAbilityListener(DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID); AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID); AddSystemAbilityListener(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); @@ -64,7 +59,6 @@ void AdvancedNotificationServiceAbility::OnStart() TEL_EXTENTION_WRAPPER->InitTelExtentionWrapper(); #endif AddSystemAbilityListener(DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID); - LIVEVIEW_ALL_SCENARIOS_EXTENTION_WRAPPER->InitExtentionWrapper(); } void AdvancedNotificationServiceAbility::OnStop() diff --git a/services/ans/src/liveview_all_scenarios_extension_wrapper.cpp b/services/ans/src/liveview_all_scenarios_extension_wrapper.cpp index b5cb419d4..335098997 100644 --- a/services/ans/src/liveview_all_scenarios_extension_wrapper.cpp +++ b/services/ans/src/liveview_all_scenarios_extension_wrapper.cpp @@ -20,7 +20,10 @@ namespace OHOS::Notification { const std::string EXTENTION_LIVEVIEW_ALL_SCENARIOS_PATH = "libliveview.z.so"; -LiveviewAllScenariosExtensionWrapper::LiveviewAllScenariosExtensionWrapper() = default; +LiveviewAllScenariosExtensionWrapper::LiveviewAllScenariosExtensionWrapper() +{ + InitExtentionWrapper(); +} LiveviewAllScenariosExtensionWrapper::~LiveviewAllScenariosExtensionWrapper() = default; void LiveviewAllScenariosExtensionWrapper::InitExtentionWrapper() diff --git a/services/ans/src/notification_extension_wrapper.cpp b/services/ans/src/notification_extension_wrapper.cpp index ac27c03ec..228d15d35 100644 --- a/services/ans/src/notification_extension_wrapper.cpp +++ b/services/ans/src/notification_extension_wrapper.cpp @@ -32,7 +32,10 @@ const int32_t PASSITIVE_DELETE = 1; static constexpr const char *SETTINGS_DATA_UNIFIED_GROUP_ENABLE_URI = "datashare:///com.ohos.settingsdata/entry/settingsdata/" "USER_SETTINGSDATA_SECURE_100?Proxy=true&key=unified_group_enable"; -ExtensionWrapper::ExtensionWrapper() = default; +ExtensionWrapper::ExtensionWrapper() +{ + InitExtentionWrapper(); +} ExtensionWrapper::~ExtensionWrapper() = default; diff --git a/services/ans/src/notification_rdb_data_mgr.cpp b/services/ans/src/notification_rdb_data_mgr.cpp index 3dd75d052..7ea2e77fe 100644 --- a/services/ans/src/notification_rdb_data_mgr.cpp +++ b/services/ans/src/notification_rdb_data_mgr.cpp @@ -96,14 +96,17 @@ NotificationDataMgr::NotificationDataMgr(const NotificationRdbConfig ¬ificati int32_t NotificationDataMgr::Init() { - ANS_LOGD("Create rdbStore"); - { - std::lock_guard lock(rdbStorePtrMutex_); - if (rdbStore_ != nullptr) { - ANS_LOGD("notification rdb has existed"); - return NativeRdb::E_OK; - } + std::shared_ptr rdbConnection = GetConnection(); + if (rdbConnection == nullptr) { + ANS_LOGE("notification rdb is null"); + return NativeRdb::E_ERROR; } + return NativeRdb::E_OK; +} + +std::shared_ptr NotificationDataMgr::GetConnection() +{ + ANS_LOGD("Create rdbStore"); NativeRdb::RdbStoreConfig rdbStoreConfig( notificationRdbConfig_.dbPath + notificationRdbConfig_.dbName, NativeRdb::StorageMode::MODE_DISK, @@ -114,39 +117,45 @@ int32_t NotificationDataMgr::Init() rdbStoreConfig.SetSecurityLevel(NativeRdb::SecurityLevel::S1); rdbStoreConfig.SetHaMode(NativeRdb::HAMode::MAIN_REPLICA); RdbStoreDataCallBackNotificationStorage rdbDataCallBack_(notificationRdbConfig_); - std::lock_guard lock(createdTableMutex_); - { - std::lock_guard lock(rdbStorePtrMutex_); - int32_t ret = NativeRdb::E_OK; - rdbStore_ = NativeRdb::RdbHelper::GetRdbStore(rdbStoreConfig, notificationRdbConfig_.version, - rdbDataCallBack_, ret); - if (rdbStore_ == nullptr) { - ANS_LOGE("notification rdb init fail"); - return NativeRdb::E_ERROR; - } - return InitCreatedTables(); - } + + int32_t ret = NativeRdb::E_OK; + std::shared_ptr rdbStore = NativeRdb::RdbHelper::GetRdbStore(rdbStoreConfig, + notificationRdbConfig_.version, rdbDataCallBack_, ret); + if (rdbStore == nullptr) { + ANS_LOGE("notification rdb init fail"); + return nullptr; + } + InitCreatedTables(rdbStore); + return rdbStore; } -int32_t NotificationDataMgr::InitCreatedTables() +int32_t NotificationDataMgr::InitCreatedTables(std::shared_ptr rdbConnection) { + { + std::lock_guard lock(createdTableMutex_); + if (!createdTables_.empty()) { + return NativeRdb::E_OK; + } + } std::string queryTableSql = "SELECT name FROM sqlite_master WHERE type='table'"; - auto absSharedResultSet = rdbStore_->QuerySql(queryTableSql); + auto absSharedResultSet = rdbConnection->QuerySql(queryTableSql); int32_t ret = absSharedResultSet->GoToFirstRow(); if (ret != NativeRdb::E_OK) { ANS_LOGE("Query tableName failed. It's empty!"); return NativeRdb::E_EMPTY_VALUES_BUCKET; } - - do { - std::string tableName; - ret = absSharedResultSet->GetString(0, tableName); - if (ret != NativeRdb::E_OK) { - ANS_LOGE("GetString string failed from sqlite_master table."); - return NativeRdb::E_ERROR; - } - createdTables_.insert(tableName); - } while (absSharedResultSet->GoToNextRow() == NativeRdb::E_OK); + { + std::lock_guard lock(createdTableMutex_); + do { + std::string tableName; + ret = absSharedResultSet->GetString(0, tableName); + if (ret != NativeRdb::E_OK) { + ANS_LOGE("GetString string failed from sqlite_master table."); + return NativeRdb::E_ERROR; + } + createdTables_.insert(tableName); + } while (absSharedResultSet->GoToNextRow() == NativeRdb::E_OK); + } absSharedResultSet->Close(); return NativeRdb::E_OK; } @@ -156,15 +165,6 @@ int32_t NotificationDataMgr::Destroy() ANS_LOGD("Destory rdbStore"); std::lock_guard lock(createdTableMutex_); createdTables_.clear(); - { - std::lock_guard lock(rdbStorePtrMutex_); - if (rdbStore_ == nullptr) { - ANS_LOGE("notification rdb is null"); - return NativeRdb::E_ERROR; - } - - rdbStore_ = nullptr; - } int32_t ret = NativeRdb::RdbHelper::DeleteRdbStore(notificationRdbConfig_.dbPath + notificationRdbConfig_.dbName); if (ret != NativeRdb::E_OK) { ANS_LOGE("failed to destroy db store"); @@ -185,8 +185,8 @@ int32_t NotificationDataMgr::InsertData(const std::string &key, const std::strin ANS_LOGE("Get user table name failed."); return NativeRdb::E_ERROR; } - std::lock_guard lock(rdbStorePtrMutex_); - if (rdbStore_ == nullptr) { + std::shared_ptr rdbConnection = GetConnection(); + if (rdbConnection == nullptr) { ANS_LOGE("notification rdb is null"); return NativeRdb::E_ERROR; } @@ -194,7 +194,7 @@ int32_t NotificationDataMgr::InsertData(const std::string &key, const std::strin NativeRdb::ValuesBucket valuesBucket; valuesBucket.PutString(NOTIFICATION_KEY, key); valuesBucket.PutString(NOTIFICATION_VALUE, value); - ret = rdbStore_->InsertWithConflictResolution(rowId, tableName, valuesBucket, + ret = rdbConnection->InsertWithConflictResolution(rowId, tableName, valuesBucket, NativeRdb::ConflictResolution::ON_CONFLICT_REPLACE); if (ret == NativeRdb::E_SQLITE_CORRUPT) { RestoreForMasterSlaver(); @@ -219,8 +219,8 @@ int32_t NotificationDataMgr::InsertData(const std::string &key, const std::vecto ANS_LOGE("Get user table name failed."); return NativeRdb::E_ERROR; } - std::lock_guard lock(rdbStorePtrMutex_); - if (rdbStore_ == nullptr) { + std::shared_ptr rdbConnection = GetConnection(); + if (rdbConnection == nullptr) { ANS_LOGE("notification rdb is null"); return NativeRdb::E_ERROR; } @@ -228,7 +228,7 @@ int32_t NotificationDataMgr::InsertData(const std::string &key, const std::vecto NativeRdb::ValuesBucket valuesBucket; valuesBucket.PutString(NOTIFICATION_KEY, key); valuesBucket.PutBlob(NOTIFICATION_VALUE, value); - ret = rdbStore_->InsertWithConflictResolution(rowId, tableName, valuesBucket, + ret = rdbConnection->InsertWithConflictResolution(rowId, tableName, valuesBucket, NativeRdb::ConflictResolution::ON_CONFLICT_REPLACE); if (ret == NativeRdb::E_SQLITE_CORRUPT) { RestoreForMasterSlaver(); @@ -254,8 +254,8 @@ int32_t NotificationDataMgr::InsertBatchData(const std::unordered_map lock(rdbStorePtrMutex_); - if (rdbStore_ == nullptr) { + std::shared_ptr rdbConnection = GetConnection(); + if (rdbConnection == nullptr) { ANS_LOGE("notification rdb is null"); return NativeRdb::E_ERROR; } @@ -267,7 +267,7 @@ int32_t NotificationDataMgr::InsertBatchData(const std::unordered_mapBatchInsert(rowId, tableName, buckets); + ret = rdbConnection->BatchInsert(rowId, tableName, buckets); if (ret == NativeRdb::E_SQLITE_CORRUPT) { RestoreForMasterSlaver(); } @@ -286,11 +286,7 @@ int32_t NotificationDataMgr::DeleteData(const std::string &key, const int32_t &u ANS_LOGD("DeleteData start"); std::vector operatedTables = GenerateOperatedTables(userId); std::reverse(operatedTables.begin(), operatedTables.end()); - std::lock_guard lock(rdbStorePtrMutex_); - if (rdbStore_ == nullptr) { - ANS_LOGE("notification rdb is null"); - return NativeRdb::E_ERROR; - } + int32_t ret = NativeRdb::E_OK; int32_t rowId = -1; for (auto tableName : operatedTables) { @@ -307,7 +303,12 @@ int32_t NotificationDataMgr::DeleteData(const std::string tableName, const std:: NativeRdb::AbsRdbPredicates absRdbPredicates(tableName); HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_10, EventBranchId::BRANCH_6); absRdbPredicates.EqualTo(NOTIFICATION_KEY, key); - int32_t ret = rdbStore_->Delete(rowId, absRdbPredicates); + std::shared_ptr rdbConnection = GetConnection(); + if (rdbConnection == nullptr) { + ANS_LOGE("notification rdb is null"); + return NativeRdb::E_ERROR; + } + int32_t ret = rdbConnection->Delete(rowId, absRdbPredicates); if (ret == NativeRdb::E_SQLITE_CORRUPT) { RestoreForMasterSlaver(); } @@ -327,8 +328,8 @@ int32_t NotificationDataMgr::DeleteBatchData(const std::vector &key { std::vector operatedTables = GenerateOperatedTables(userId); std::reverse(operatedTables.begin(), operatedTables.end()); - std::lock_guard lock(rdbStorePtrMutex_); - if (rdbStore_ == nullptr) { + std::shared_ptr rdbConnection = GetConnection(); + if (rdbConnection == nullptr) { ANS_LOGE("notification rdb is null"); return NativeRdb::E_ERROR; } @@ -345,7 +346,7 @@ int32_t NotificationDataMgr::DeleteBatchData(const std::vector &key NativeRdb::AbsRdbPredicates absRdbPredicates(tableName); for (const auto &batchKey : batchKeys) { absRdbPredicates.In(NOTIFICATION_KEY, batchKey); - int32_t ret = rdbStore_->Delete(rowId, absRdbPredicates); + int32_t ret = rdbConnection->Delete(rowId, absRdbPredicates); if (ret == NativeRdb::E_SQLITE_CORRUPT) { RestoreForMasterSlaver(); } @@ -364,11 +365,6 @@ int32_t NotificationDataMgr::QueryData(const std::string &key, std::string &valu { ANS_LOGD("QueryData start"); std::vector operatedTables = GenerateOperatedTables(userId); - std::lock_guard lock(rdbStorePtrMutex_); - if (rdbStore_ == nullptr) { - ANS_LOGE("notification rdb is null"); - return NativeRdb::E_ERROR; - } int32_t ret = NativeRdb::E_OK; for (auto tableName : operatedTables) { ret = QueryData(tableName, key, value); @@ -384,7 +380,12 @@ int32_t NotificationDataMgr::QueryData(const std::string tableName, const std::s NativeRdb::AbsRdbPredicates absRdbPredicates(tableName); HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_10, EventBranchId::BRANCH_2); absRdbPredicates.EqualTo(NOTIFICATION_KEY, key); - auto absSharedResultSet = rdbStore_->Query(absRdbPredicates, std::vector()); + std::shared_ptr rdbConnection = GetConnection(); + if (rdbConnection == nullptr) { + ANS_LOGE("notification rdb is null"); + return NativeRdb::E_ERROR; + } + auto absSharedResultSet = rdbConnection->Query(absRdbPredicates, std::vector()); if (absSharedResultSet == nullptr) { ANS_LOGE("absSharedResultSet failed from %{public}s table.", tableName.c_str()); return NativeRdb::E_ERROR; @@ -421,11 +422,6 @@ int32_t NotificationDataMgr::QueryData(const std::string tableName, const std::s int32_t NotificationDataMgr::QueryData(const std::string &key, std::vector &values, const int32_t &userId) { std::vector operatedTables = GenerateOperatedTables(userId); - std::lock_guard lock(rdbStorePtrMutex_); - if (rdbStore_ == nullptr) { - ANS_LOGE("notification rdb is null"); - return NativeRdb::E_ERROR; - } int32_t ret = NativeRdb::E_OK; for (auto tableName : operatedTables) { ret = QueryData(tableName, key, values); @@ -441,7 +437,12 @@ int32_t NotificationDataMgr::QueryData(const std::string tableName, const std::s NativeRdb::AbsRdbPredicates absRdbPredicates(tableName); HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_10, EventBranchId::BRANCH_3); absRdbPredicates.EqualTo(NOTIFICATION_KEY, key); - auto absSharedResultSet = rdbStore_->Query(absRdbPredicates, std::vector()); + std::shared_ptr rdbConnection = GetConnection(); + if (rdbConnection == nullptr) { + ANS_LOGE("notification rdb is null"); + return NativeRdb::E_ERROR; + } + auto absSharedResultSet = rdbConnection->Query(absRdbPredicates, std::vector()); if (absSharedResultSet == nullptr) { ANS_LOGE("absSharedResultSet failed from %{public}s table.", tableName.c_str()); return NativeRdb::E_ERROR; @@ -478,11 +479,7 @@ int32_t NotificationDataMgr::QueryDataBeginWithKey( { ANS_LOGD("QueryData BeginWithKey start"); std::vector operatedTables = GenerateOperatedTables(userId); - std::lock_guard lock(rdbStorePtrMutex_); - if (rdbStore_ == nullptr) { - ANS_LOGE("notification rdb is null"); - return NativeRdb::E_ERROR; - } + int32_t ret = NativeRdb::E_OK; for (auto tableName : operatedTables) { ret = QueryDataBeginWithKey(tableName, key, values); @@ -502,7 +499,12 @@ int32_t NotificationDataMgr::QueryDataBeginWithKey( NativeRdb::AbsRdbPredicates absRdbPredicates(tableName); HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_10, EventBranchId::BRANCH_5); absRdbPredicates.BeginsWith(NOTIFICATION_KEY, key); - auto absSharedResultSet = rdbStore_->Query(absRdbPredicates, std::vector()); + std::shared_ptr rdbConnection = GetConnection(); + if (rdbConnection == nullptr) { + ANS_LOGE("notification rdb is null"); + return NativeRdb::E_ERROR; + } + auto absSharedResultSet = rdbConnection->Query(absRdbPredicates, std::vector()); if (absSharedResultSet == nullptr) { ANS_LOGE("absSharedResultSet failed from %{public}s table.", tableName.c_str()); return NativeRdb::E_ERROR; @@ -553,11 +555,6 @@ int32_t NotificationDataMgr::QueryAllData(std::unordered_map operatedTables = GenerateOperatedTables(userId); - std::lock_guard lock(rdbStorePtrMutex_); - if (rdbStore_ == nullptr) { - ANS_LOGE("notification rdb is null"); - return NativeRdb::E_ERROR; - } int32_t ret = NativeRdb::E_OK; for (auto tableName : operatedTables) { ret = QueryAllData(tableName, datas); @@ -576,7 +573,12 @@ int32_t NotificationDataMgr::QueryAllData( { NativeRdb::AbsRdbPredicates absRdbPredicates(tableName); HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_10, EventBranchId::BRANCH_4); - auto absSharedResultSet = rdbStore_->Query(absRdbPredicates, std::vector()); + std::shared_ptr rdbConnection = GetConnection(); + if (rdbConnection == nullptr) { + ANS_LOGE("notification rdb is null"); + return NativeRdb::E_ERROR; + } + auto absSharedResultSet = rdbConnection->Query(absRdbPredicates, std::vector()); if (absSharedResultSet == nullptr) { ANS_LOGE("absSharedResultSet failed from %{public}s table.", tableName.c_str()); return NativeRdb::E_ERROR; @@ -631,12 +633,12 @@ int32_t NotificationDataMgr::DropUserTable(const int32_t userId) std::lock_guard lock(createdTableMutex_); int32_t ret = NativeRdb::E_OK; { - std::lock_guard lock(rdbStorePtrMutex_); - if (rdbStore_ == nullptr) { + std::shared_ptr rdbConnection = GetConnection(); + if (rdbConnection == nullptr) { return NativeRdb::E_ERROR; } std::string dropTableSql = "DROP TABLE IF EXISTS " + tableName; - ret = rdbStore_->ExecuteSql(dropTableSql); + ret = rdbConnection->ExecuteSql(dropTableSql); } if (ret == NativeRdb::E_OK) { createdTables_.erase(tableName); @@ -653,6 +655,10 @@ int32_t NotificationDataMgr::GetUserTableName(const int32_t &userId, std::string tableName = notificationRdbConfig_.tableName; return NativeRdb::E_OK; } + std::shared_ptr rdbConnection = GetConnection(); + if (rdbConnection == nullptr) { + return NativeRdb::E_ERROR; + } const char *keySpliter = "_"; std::stringstream stream; @@ -661,13 +667,9 @@ int32_t NotificationDataMgr::GetUserTableName(const int32_t &userId, std::string if (createdTables_.find(tableName) == createdTables_.end()) { std::lock_guard lock(createdTableMutex_); if (createdTables_.find(tableName) == createdTables_.end()) { - std::lock_guard lock(rdbStorePtrMutex_); - if (rdbStore_ == nullptr) { - return NativeRdb::E_ERROR; - } std::string createTableSql = "CREATE TABLE IF NOT EXISTS " + tableName + " (KEY TEXT NOT NULL PRIMARY KEY, VALUE TEXT NOT NULL);"; - int32_t ret = rdbStore_->ExecuteSql(createTableSql); + int32_t ret = rdbConnection->ExecuteSql(createTableSql); if (ret != NativeRdb::E_OK) { ANS_LOGW("createTable %{public}s failed, code: %{public}d", tableName.c_str(), ret); message.ErrorCode(ret).Message("create table failed."); @@ -705,7 +707,8 @@ int32_t NotificationDataMgr::RestoreForMasterSlaver() .ErrorCode(NativeRdb::E_SQLITE_CORRUPT).Message("Rdb is corruped."); NotificationAnalyticsUtil::ReportModifyEvent(message); ANS_LOGI("RestoreForMasterSlaver start"); - int32_t result = rdbStore_->Restore(""); + std::shared_ptr rdbConnection = GetConnection(); + int32_t result = rdbConnection->Restore(""); ANS_LOGI("RestoreForMasterSlaver result = %{public}d", result); return result; } diff --git a/services/ans/test/unittest/notification_extension_wrapper_test.cpp b/services/ans/test/unittest/notification_extension_wrapper_test.cpp index 9c6fe14e5..0274c7972 100644 --- a/services/ans/test/unittest/notification_extension_wrapper_test.cpp +++ b/services/ans/test/unittest/notification_extension_wrapper_test.cpp @@ -155,6 +155,7 @@ HWTEST_F(NotificationExtensionWrapperTest, SetlocalSwitch_True_Test, TestSize.Le HWTEST_F(NotificationExtensionWrapperTest, SyncAdditionConfig_NullSyncAdditionConfig, TestSize.Level0) { ExtensionWrapper extensionWrapper; + extensionWrapper.syncAdditionConfig_ = nullptr; ErrCode result = extensionWrapper.SyncAdditionConfig("key", "value"); EXPECT_EQ(result, 0); } @@ -217,6 +218,7 @@ HWTEST_F(NotificationExtensionWrapperTest, ReminderControl_NullReminderControl, { OHOS::Notification::ExtensionWrapper extensionWrapper; std::string bundleName = "testBundle"; + extensionWrapper.reminderControl_ = nullptr; int32_t result = extensionWrapper.ReminderControl(bundleName); EXPECT_EQ(result, 0); } @@ -235,6 +237,7 @@ HWTEST_F(NotificationExtensionWrapperTest, BannerControl_NullBannerControl, Test // Arrange ExtensionWrapper wrapper; std::string bundleName = "testBundle"; + wrapper.bannerControl_ = nullptr; // Act int32_t result = wrapper.BannerControl(bundleName); diff --git a/services/ans/test/unittest/notification_rdb_data_mgr_test/mock_rdb_helper.cpp b/services/ans/test/unittest/notification_rdb_data_mgr_test/mock_rdb_helper.cpp index d9dc91209..786637533 100755 --- a/services/ans/test/unittest/notification_rdb_data_mgr_test/mock_rdb_helper.cpp +++ b/services/ans/test/unittest/notification_rdb_data_mgr_test/mock_rdb_helper.cpp @@ -15,12 +15,26 @@ #include "rdb_errno.h" #include "rdb_helper.h" +#include "rdb_store.h" +using namespace OHOS::NativeRdb; + +namespace { + std::shared_ptr g_mockRdbStore = nullptr; +} + +void MockRdbStore(std::shared_ptr rdbStore) +{ + g_mockRdbStore = rdbStore; +} namespace OHOS { namespace NativeRdb { std::shared_ptr RdbHelper::GetRdbStore( const RdbStoreConfig &config, int version, RdbOpenCallback &openCallback, int &errCode) { + if (g_mockRdbStore) { + return g_mockRdbStore; + } return nullptr; } diff --git a/services/ans/test/unittest/notification_rdb_data_mgr_test/notification_rdb_data_mgr_test.cpp b/services/ans/test/unittest/notification_rdb_data_mgr_test/notification_rdb_data_mgr_test.cpp index fe194ca70..745d171bc 100755 --- a/services/ans/test/unittest/notification_rdb_data_mgr_test/notification_rdb_data_mgr_test.cpp +++ b/services/ans/test/unittest/notification_rdb_data_mgr_test/notification_rdb_data_mgr_test.cpp @@ -37,6 +37,8 @@ extern void MockGetString(bool mockRet); using namespace testing::ext; using namespace OHOS::NativeRdb; +extern void MockRdbStore(std::shared_ptr rdbStore); + namespace OHOS { namespace Notification { class RdbStoreDataCallBackNotificationStorageTest : public testing::Test { @@ -402,7 +404,7 @@ HWTEST_F(RdbStoreDataCallBackNotificationStorageTest, RdbStoreDataCallBack_00500 NotificationRdbConfig notificationRdbConfig; std::unique_ptr notificationDataMgr = std::make_unique(notificationRdbConfig); - notificationDataMgr->rdbStore_ = std::make_shared(); + MockRdbStore(std::make_shared()); ASSERT_EQ(notificationDataMgr->Destroy(), NativeRdb::E_ERROR); } @@ -416,7 +418,7 @@ HWTEST_F(RdbStoreDataCallBackNotificationStorageTest, RdbStoreDataCallBack_00600 NotificationRdbConfig notificationRdbConfig; std::unique_ptr notificationDataMgr = std::make_unique(notificationRdbConfig); - notificationDataMgr->rdbStore_ = nullptr; + std::string key = ""; std::string value = ""; ASSERT_EQ(notificationDataMgr->InsertData(key, value, -1), NativeRdb::E_ERROR); @@ -432,7 +434,7 @@ HWTEST_F(RdbStoreDataCallBackNotificationStorageTest, RdbStoreDataCallBack_00700 NotificationRdbConfig notificationRdbConfig; std::unique_ptr notificationDataMgr = std::make_unique(notificationRdbConfig); - notificationDataMgr->rdbStore_ = std::make_shared(); + MockRdbStore(std::make_shared()); std::string key = ""; std::string value = ""; ASSERT_EQ(notificationDataMgr->InsertData(key, value, -1), NativeRdb::E_ERROR); @@ -448,7 +450,7 @@ HWTEST_F(RdbStoreDataCallBackNotificationStorageTest, RdbStoreDataCallBack_00800 NotificationRdbConfig notificationRdbConfig; std::unique_ptr notificationDataMgr = std::make_unique(notificationRdbConfig); - notificationDataMgr->rdbStore_ = nullptr; + std::unordered_map values; ASSERT_EQ(notificationDataMgr->InsertBatchData(values, -1), NativeRdb::E_ERROR); } @@ -463,7 +465,7 @@ HWTEST_F(RdbStoreDataCallBackNotificationStorageTest, RdbStoreDataCallBack_00900 NotificationRdbConfig notificationRdbConfig; std::unique_ptr notificationDataMgr = std::make_unique(notificationRdbConfig); - notificationDataMgr->rdbStore_ = std::make_shared(); + MockRdbStore(std::make_shared()); std::unordered_map values = { { "--help", "--help"}, { "--all", "--all"}, @@ -485,7 +487,7 @@ HWTEST_F(RdbStoreDataCallBackNotificationStorageTest, RdbStoreDataCallBack_01000 NotificationRdbConfig notificationRdbConfig; std::unique_ptr notificationDataMgr = std::make_unique(notificationRdbConfig); - notificationDataMgr->rdbStore_ = nullptr; + std::string key = ""; ASSERT_EQ(notificationDataMgr->DeleteData(key, -1), NativeRdb::E_ERROR); } @@ -500,7 +502,7 @@ HWTEST_F(RdbStoreDataCallBackNotificationStorageTest, RdbStoreDataCallBack_01100 NotificationRdbConfig notificationRdbConfig; std::unique_ptr notificationDataMgr = std::make_unique(notificationRdbConfig); - notificationDataMgr->rdbStore_ = std::make_shared(); + MockRdbStore(std::make_shared()); std::string key = ""; ASSERT_EQ(notificationDataMgr->DeleteData(key, -1), NativeRdb::E_ERROR); } @@ -515,7 +517,7 @@ HWTEST_F(RdbStoreDataCallBackNotificationStorageTest, RdbStoreDataCallBack_01200 NotificationRdbConfig notificationRdbConfig; std::unique_ptr notificationDataMgr = std::make_unique(notificationRdbConfig); - notificationDataMgr->rdbStore_ = nullptr; + std::vector keys; ASSERT_EQ(notificationDataMgr->DeleteBatchData(keys, -1), NativeRdb::E_ERROR); } @@ -530,7 +532,7 @@ HWTEST_F(RdbStoreDataCallBackNotificationStorageTest, RdbStoreDataCallBack_01300 NotificationRdbConfig notificationRdbConfig; std::unique_ptr notificationDataMgr = std::make_unique(notificationRdbConfig); - notificationDataMgr->rdbStore_ = std::make_shared(); + MockRdbStore(std::make_shared()); std::vector keys; std::string key = ""; keys.emplace_back(key); @@ -547,7 +549,7 @@ HWTEST_F(RdbStoreDataCallBackNotificationStorageTest, RdbStoreDataCallBack_01400 NotificationRdbConfig notificationRdbConfig; std::unique_ptr notificationDataMgr = std::make_unique(notificationRdbConfig); - notificationDataMgr->rdbStore_ = nullptr; + std::string key = ""; std::string value = ""; ASSERT_EQ(notificationDataMgr->QueryData(key, value, -1), NativeRdb::E_ERROR); @@ -563,7 +565,7 @@ HWTEST_F(RdbStoreDataCallBackNotificationStorageTest, RdbStoreDataCallBack_01500 NotificationRdbConfig notificationRdbConfig; std::unique_ptr notificationDataMgr = std::make_unique(notificationRdbConfig); - notificationDataMgr->rdbStore_ = std::make_shared(); + MockRdbStore(std::make_shared()); std::string key = ""; std::string value = ""; g_mockQueryRet = true; @@ -580,7 +582,7 @@ HWTEST_F(RdbStoreDataCallBackNotificationStorageTest, RdbStoreDataCallBack_01600 NotificationRdbConfig notificationRdbConfig; std::unique_ptr notificationDataMgr = std::make_unique(notificationRdbConfig); - notificationDataMgr->rdbStore_ = std::make_shared(); + MockRdbStore(std::make_shared()); std::string key = ""; std::string value = ""; g_mockQueryRet = false; @@ -600,7 +602,7 @@ HWTEST_F(RdbStoreDataCallBackNotificationStorageTest, RdbStoreDataCallBack_01700 NotificationRdbConfig notificationRdbConfig; std::unique_ptr notificationDataMgr = std::make_unique(notificationRdbConfig); - notificationDataMgr->rdbStore_ = nullptr; + std::string key = ""; std::unordered_map values; ASSERT_EQ(notificationDataMgr->QueryDataBeginWithKey(key, values, -1), NativeRdb::E_ERROR); @@ -616,7 +618,7 @@ HWTEST_F(RdbStoreDataCallBackNotificationStorageTest, RdbStoreDataCallBack_01800 NotificationRdbConfig notificationRdbConfig; std::unique_ptr notificationDataMgr = std::make_unique(notificationRdbConfig); - notificationDataMgr->rdbStore_ = std::make_shared(); + MockRdbStore(std::make_shared()); g_mockQueryRet = true; std::string key = ""; std::unordered_map values; @@ -633,7 +635,7 @@ HWTEST_F(RdbStoreDataCallBackNotificationStorageTest, RdbStoreDataCallBack_01900 NotificationRdbConfig notificationRdbConfig; std::unique_ptr notificationDataMgr = std::make_unique(notificationRdbConfig); - notificationDataMgr->rdbStore_ = std::make_shared(); + MockRdbStore(std::make_shared()); g_mockQueryRet = false; MockHasBlock(true); MockGoToFirstRow(true); @@ -653,7 +655,7 @@ HWTEST_F(RdbStoreDataCallBackNotificationStorageTest, RdbStoreDataCallBack_02000 NotificationRdbConfig notificationRdbConfig; std::unique_ptr notificationDataMgr = std::make_unique(notificationRdbConfig); - notificationDataMgr->rdbStore_ = nullptr; + std::unordered_map datas; ASSERT_EQ(notificationDataMgr->QueryAllData(datas, -1), NativeRdb::E_ERROR); } @@ -668,7 +670,7 @@ HWTEST_F(RdbStoreDataCallBackNotificationStorageTest, RdbStoreDataCallBack_02100 NotificationRdbConfig notificationRdbConfig; std::unique_ptr notificationDataMgr = std::make_unique(notificationRdbConfig); - notificationDataMgr->rdbStore_ = std::make_shared(); + MockRdbStore(std::make_shared()); g_mockQueryRet = true; std::unordered_map datas; ASSERT_EQ(notificationDataMgr->QueryAllData(datas, -1), NativeRdb::E_ERROR); @@ -684,7 +686,7 @@ HWTEST_F(RdbStoreDataCallBackNotificationStorageTest, RdbStoreDataCallBack_02200 NotificationRdbConfig notificationRdbConfig; std::unique_ptr notificationDataMgr = std::make_unique(notificationRdbConfig); - notificationDataMgr->rdbStore_ = std::make_shared(); + MockRdbStore(std::make_shared()); g_mockQueryRet = false; MockHasBlock(true); MockGoToFirstRow(false); @@ -702,7 +704,7 @@ HWTEST_F(RdbStoreDataCallBackNotificationStorageTest, RdbStoreDataCallBack_02300 NotificationRdbConfig notificationRdbConfig; std::unique_ptr notificationDataMgr = std::make_unique(notificationRdbConfig); - notificationDataMgr->rdbStore_ = std::make_shared(); + MockRdbStore(std::make_shared()); g_mockQueryRet = false; MockHasBlock(true); MockGoToFirstRow(true); @@ -721,7 +723,7 @@ HWTEST_F(RdbStoreDataCallBackNotificationStorageTest, RdbStoreDataCallBack_02400 NotificationRdbConfig notificationRdbConfig; std::unique_ptr notificationDataMgr = std::make_unique(notificationRdbConfig); - notificationDataMgr->rdbStore_ = std::make_shared(); + MockRdbStore(std::make_shared()); g_mockQueryRet = false; MockHasBlock(true); MockGoToFirstRow(true); @@ -740,7 +742,7 @@ HWTEST_F(RdbStoreDataCallBackNotificationStorageTest, RdbStoWreDataCallBack_0250 NotificationRdbConfig notificationRdbConfig; std::unique_ptr notificationDataMgr = std::make_unique(notificationRdbConfig); - notificationDataMgr->rdbStore_ = std::make_shared(); + MockRdbStore(std::make_shared()); ASSERT_EQ(notificationDataMgr->DropUserTable(-1), NativeRdb::E_OK); } -- Gitee