From bc85388cfbc7e76e1516c6ed65f602b875c8620a Mon Sep 17 00:00:00 2001 From: zero-cyc Date: Mon, 15 Aug 2022 16:10:08 +0800 Subject: [PATCH] =?UTF-8?q?kvstore=E6=8E=A5=E5=8F=A3=E5=88=87=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zero-cyc Change-Id: I0d0234eed6c70470ca05fced10c754495bbf765a --- services/ans/include/preferences_constant.h | 2 +- .../src/notification_preferences_database.cpp | 23 ++++++++- .../include/distributed_database.h | 2 + .../distributed_preferences_database.h | 1 + .../distributed/src/distributed_database.cpp | 48 ++++++++++++------- .../src/distributed_notification_manager.cpp | 11 ++++- .../src/distributed_preferences_database.cpp | 45 +++++++++-------- .../src/distributed_screen_status_manager.cpp | 28 ++++++----- .../unittest/distributed_database_test.cpp | 2 + .../distributed_notification_manager_test.cpp | 3 +- ...distributed_screen_status_manager_test.cpp | 13 ++--- 11 files changed, 117 insertions(+), 61 deletions(-) diff --git a/services/ans/include/preferences_constant.h b/services/ans/include/preferences_constant.h index c99d818fd..fae1fd77e 100644 --- a/services/ans/include/preferences_constant.h +++ b/services/ans/include/preferences_constant.h @@ -21,7 +21,7 @@ namespace Notification { /** * Indicates distributed database app id. */ -constexpr char APP_ID[] = "advanced_notification_service"; +constexpr char APP_ID[] = "notification_service"; /** * Indicates distributed database store id. diff --git a/services/ans/src/notification_preferences_database.cpp b/services/ans/src/notification_preferences_database.cpp index 842216dbb..0d18ef611 100644 --- a/services/ans/src/notification_preferences_database.cpp +++ b/services/ans/src/notification_preferences_database.cpp @@ -203,6 +203,8 @@ const static std::string KEY_SLOT_ENABLE_BYPASS_DND = "enableBypassDnd"; */ const static std::string KEY_SLOT_ENABLED = "enabled"; +constexpr char KV_STORE_PATH[] = "/data/service/el1/public/database/notification_service"; + const std::map &, std::string &)>> NotificationPreferencesDatabase::slotMap_ = { @@ -338,7 +340,9 @@ DistributedKv::Status NotificationPreferencesDatabase::GetKvStore() .createIfMissing = true, .encrypt = false, .autoSync = false, + .area = DistributedKv::EL1, .kvStoreType = DistributedKv::KvStoreType::SINGLE_VERSION, + .baseDir = KV_STORE_PATH }; auto status = dataManager_.GetSingleKvStore(options, appId_, storeId_, kvStorePtr_); if (status != DistributedKv::Status::SUCCESS) { @@ -392,6 +396,7 @@ bool NotificationPreferencesDatabase::PutSlotsToDisturbeDB( return false; } DistributedKv::Status status = kvStorePtr_->PutBatch(entries); + CloseKvStore(); return (status == DistributedKv::Status::SUCCESS); } @@ -423,6 +428,7 @@ bool NotificationPreferencesDatabase::PutGroupsToDisturbeDB( return false; } DistributedKv::Status status = kvStorePtr_->PutBatch(entries); + CloseKvStore(); return (status == DistributedKv::Status::SUCCESS); } @@ -455,6 +461,7 @@ bool NotificationPreferencesDatabase::PutBundlePropertyToDisturbeDB( break; } }); + CloseKvStore(); return result; } @@ -559,6 +566,7 @@ bool NotificationPreferencesDatabase::PutNotificationsEnabled(const int32_t &use DistributedKv::Key enableKey(typeKey); DistributedKv::Value enableValue(std::to_string(enabled)); DistributedKv::Status status = kvStorePtr_->Put(enableKey, enableValue); + CloseKvStore(); if (status != DistributedKv::Status::SUCCESS) { ANS_LOGE("Store enable notification failed. %{public}d", status); return false; @@ -622,6 +630,7 @@ bool NotificationPreferencesDatabase::PutDoNotDisturbDate( }; DistributedKv::Status status = kvStorePtr_->PutBatch(entries); + CloseKvStore(); if (status != DistributedKv::Status::SUCCESS) { ANS_LOGE("Store DoNotDisturbDate failed. %{public}d", status); return false; @@ -750,6 +759,7 @@ bool NotificationPreferencesDatabase::ParseFromDisturbeDB(NotificationPreference return false; } ParseBundleFromDistureDB(info, entries); + CloseKvStore(); return true; } @@ -760,7 +770,7 @@ bool NotificationPreferencesDatabase::RemoveAllDataFromDisturbeDB() ANS_LOGE("KvStore is nullptr."); return false; } - DistributedKv::Status status = dataManager_.DeleteKvStore(appId_, storeId_); + DistributedKv::Status status = dataManager_.DeleteKvStore(appId_, storeId_, KV_STORE_PATH); return (status == DistributedKv::Status::SUCCESS); } @@ -779,6 +789,7 @@ bool NotificationPreferencesDatabase::RemoveBundleFromDisturbeDB(const std::stri if (status != DistributedKv::Status::SUCCESS) { ANS_LOGE("Get Bundle Info failed."); + CloseKvStore(); return false; } @@ -790,6 +801,7 @@ bool NotificationPreferencesDatabase::RemoveBundleFromDisturbeDB(const std::stri DistributedKv::Key bundleDBKey(KEY_BUNDLE_LABEL + KEY_BUNDLE_NAME + KEY_UNDER_LINE + bundleKey); keys.push_back(bundleDBKey); status = kvStorePtr_->DeleteBatch(keys); + CloseKvStore(); if (status != DistributedKv::Status::SUCCESS) { ANS_LOGE("delete bundle Info failed."); return false; @@ -818,6 +830,7 @@ bool NotificationPreferencesDatabase::RemoveSlotFromDisturbeDB( status = kvStorePtr_->GetEntries(DistributedKv::Key(GenerateSlotKey(bundleKey, slotType) + KEY_UNDER_LINE), slotentries); if (status != DistributedKv::Status::SUCCESS) { + CloseKvStore(); return false; } std::vector keys; @@ -826,6 +839,7 @@ bool NotificationPreferencesDatabase::RemoveSlotFromDisturbeDB( } status = kvStorePtr_->DeleteBatch(keys); + CloseKvStore(); if (status != DistributedKv::Status::SUCCESS) { ANS_LOGE("delete bundle Info failed."); return false; @@ -852,6 +866,7 @@ bool NotificationPreferencesDatabase::RemoveAllSlotsFromDisturbeDB(const std::st std::vector slotsEntries; status = kvStorePtr_->GetEntries(DistributedKv::Key(GenerateSlotKey(bundleKey) + KEY_UNDER_LINE), slotsEntries); if (status != DistributedKv::Status::SUCCESS) { + CloseKvStore(); return false; } std::vector keys; @@ -860,6 +875,7 @@ bool NotificationPreferencesDatabase::RemoveAllSlotsFromDisturbeDB(const std::st } status = kvStorePtr_->DeleteBatch(keys); + CloseKvStore(); ANS_LOGD("%{public}s remove all slots status %{public}d", __FUNCTION__, status); return (status == DistributedKv::Status::SUCCESS); } @@ -887,11 +903,13 @@ bool NotificationPreferencesDatabase::RemoveGroupsFromDisturbeDB( for (auto iter : groupIds) { result = GetRemoveGroupKeysFromDisturbeDB(bundleKey, iter, keys); if (!result) { + CloseKvStore(); return result; } } DistributedKv::Status status = kvStorePtr_->DeleteBatch(keys); + CloseKvStore(); ANS_LOGD("%{public}s remove groups status %{public}d", __FUNCTION__, status); return (status == DistributedKv::Status::SUCCESS); } @@ -961,6 +979,7 @@ DistributedKv::Status NotificationPreferencesDatabase::PutBundlePropertyToDistur return DistributedKv::Status::ERROR; } DistributedKv::Status status = kvStorePtr_->Put(key, value); + CloseKvStore(); return status; } @@ -1650,6 +1669,7 @@ bool NotificationPreferencesDatabase::RemoveNotificationEnable(const int32_t use std::string(KEY_ENABLE_ALL_NOTIFICATION).append(KEY_UNDER_LINE).append(std::to_string(userId)); DistributedKv::Key enableKey(key); DistributedKv::Status status = kvStorePtr_->Delete(enableKey); + CloseKvStore(); if (status != DistributedKv::Status::SUCCESS) { ANS_LOGE("delete bundle Info failed."); return false; @@ -1681,6 +1701,7 @@ bool NotificationPreferencesDatabase::RemoveDoNotDisturbDate(const int32_t userI }; DistributedKv::Status status = kvStorePtr_->DeleteBatch(keys); + CloseKvStore(); if (status != DistributedKv::Status::SUCCESS) { ANS_LOGE("delete DoNotDisturb date failed."); return false; diff --git a/services/distributed/include/distributed_database.h b/services/distributed/include/distributed_database.h index 4bde7adab..dca37c7b1 100644 --- a/services/distributed/include/distributed_database.h +++ b/services/distributed/include/distributed_database.h @@ -123,6 +123,8 @@ public: */ bool RecreateDistributedDB(); + bool OnDeviceConnected(); + private: void GetKvDataManager(void); bool CheckKvDataManager(void); diff --git a/services/distributed/include/distributed_preferences_database.h b/services/distributed/include/distributed_preferences_database.h index 6248c6d0f..1fc06ba15 100644 --- a/services/distributed/include/distributed_preferences_database.h +++ b/services/distributed/include/distributed_preferences_database.h @@ -83,6 +83,7 @@ private: bool CheckKvDataManager(void); void GetKvStore(void); bool CheckKvStore(void); + void CloseKvStore(); private: std::mutex mutex_; diff --git a/services/distributed/src/distributed_database.cpp b/services/distributed/src/distributed_database.cpp index 8dccefc9b..23f3f80a5 100644 --- a/services/distributed/src/distributed_database.cpp +++ b/services/distributed/src/distributed_database.cpp @@ -20,8 +20,9 @@ namespace OHOS { namespace Notification { namespace { -const std::string APP_ID = "advanced_notification_service"; +const std::string APP_ID = "notification_service"; const std::string STORE_ID = "distributed_notification"; +constexpr char KV_STORE_PATH[] = "/data/service/el1/public/database/notification_service"; } // namespace DistributedDatabase::DistributedDatabase( @@ -29,7 +30,6 @@ DistributedDatabase::DistributedDatabase( : DistributedFlowControl(), databaseCb_(databaseCb), deviceCb_(deviceCb) { GetKvDataManager(); - GetKvStore(); } DistributedDatabase::~DistributedDatabase() @@ -66,12 +66,15 @@ void DistributedDatabase::GetKvStore(void) if (!CheckKvDataManager()) { return; } - - DistributedKv::Options options; - options.createIfMissing = true; - options.securityLevel = DistributedKv::SecurityLevel::S1; - options.autoSync = true; - options.kvStoreType = DistributedKv::KvStoreType::SINGLE_VERSION; + DistributedKv::Options options { + .createIfMissing = true, + .securityLevel = DistributedKv::SecurityLevel::S1, + .autoSync = true, + .encrypt = false, + .area = DistributedKv::EL1, + .kvStoreType = DistributedKv::KvStoreType::SINGLE_VERSION, + .baseDir = KV_STORE_PATH + }; DistributedKv::AppId appId = {.appId = APP_ID}; DistributedKv::StoreId storeId = {.storeId = STORE_ID}; DistributedKv::Status status = kvDataManager_->GetSingleKvStore(options, appId, storeId, kvStore_); @@ -106,11 +109,21 @@ bool DistributedDatabase::CheckKvStore(void) return true; } +bool DistributedDatabase::OnDeviceConnected() +{ + std::lock_guard lock(mutex_); + if (!CheckKvStore()) { + return false; + } + return true; +} + bool DistributedDatabase::PutToDistributedDB(const std::string &key, const std::string &value) { std::lock_guard lock(mutex_); - if (!CheckKvStore()) { + if (kvStore_ == nullptr) { + ANS_LOGE("kvStore is nullptr."); return false; } @@ -134,7 +147,8 @@ bool DistributedDatabase::GetFromDistributedDB(const std::string &key, std::stri { std::lock_guard lock(mutex_); - if (!CheckKvStore()) { + if (kvStore_ == nullptr) { + ANS_LOGE("kvStore is nullptr."); return false; } @@ -160,7 +174,8 @@ bool DistributedDatabase::GetEntriesFromDistributedDB(const std::string &prefixK { std::lock_guard lock(mutex_); - if (!CheckKvStore()) { + if (kvStore_ == nullptr) { + ANS_LOGE("kvStore is nullptr."); return false; } @@ -183,7 +198,8 @@ bool DistributedDatabase::DeleteToDistributedDB(const std::string &key) { std::lock_guard lock(mutex_); - if (!CheckKvStore()) { + if (kvStore_ == nullptr) { + ANS_LOGE("kvStore is nullptr."); return false; } @@ -206,7 +222,8 @@ bool DistributedDatabase::ClearDataByDevice(const std::string &deviceId) { std::lock_guard lock(mutex_); - if (!CheckKvStore()) { + if (kvStore_ == nullptr) { + ANS_LOGE("kvStore is nullptr."); return false; } @@ -305,18 +322,15 @@ bool DistributedDatabase::RecreateDistributedDB() ANS_LOGE("KvManager flow control."); return false; } - kvStore_.reset(); - DistributedKv::AppId appId = {.appId = APP_ID}; DistributedKv::StoreId storeId = {.storeId = STORE_ID}; - DistributedKv::Status status = kvDataManager_->DeleteKvStore(appId, storeId); + DistributedKv::Status status = kvDataManager_->DeleteKvStore(appId, storeId, KV_STORE_PATH); if (status != DistributedKv::Status::SUCCESS) { ANS_LOGE("kvDataManager DeleteKvStore() failed ret = 0x%{public}x", status); return false; } - GetKvStore(); return true; } } // namespace Notification diff --git a/services/distributed/src/distributed_notification_manager.cpp b/services/distributed/src/distributed_notification_manager.cpp index 5aa3e53ad..460405aab 100644 --- a/services/distributed/src/distributed_notification_manager.cpp +++ b/services/distributed/src/distributed_notification_manager.cpp @@ -202,8 +202,15 @@ void DistributedNotificationManager::OnDatabaseDelete( void DistributedNotificationManager::OnDeviceConnected(const std::string &deviceId) { ANS_LOGD("%{public}s", __FUNCTION__); - handler_->PostTask(std::bind([=]() {})); - return; + handler_->PostTask(std::bind([=]() { + if (database_ == nullptr) { + ANS_LOGE("OnDeviceConnected failed: database is null"); + return; + } + if (!database_->OnDeviceConnected()) { + ANS_LOGE("OnDeviceConnected failed."); + } + })); } void DistributedNotificationManager::OnDeviceDisconnected(const std::string &deviceId) diff --git a/services/distributed/src/distributed_preferences_database.cpp b/services/distributed/src/distributed_preferences_database.cpp index 535bcda40..4ee3a39c5 100644 --- a/services/distributed/src/distributed_preferences_database.cpp +++ b/services/distributed/src/distributed_preferences_database.cpp @@ -20,14 +20,14 @@ namespace OHOS { namespace Notification { namespace { -const std::string APP_ID = "advanced_notification_service"; +const std::string APP_ID = "notification_service"; const std::string STORE_ID = "distributed_preferences"; +constexpr char KV_STORE_PATH[] = "/data/service/el1/public/database/notification_service"; } // namespace DistributedPreferencesDatabase::DistributedPreferencesDatabase() : DistributedFlowControl() { GetKvDataManager(); - GetKvStore(); } DistributedPreferencesDatabase::~DistributedPreferencesDatabase() @@ -57,15 +57,16 @@ void DistributedPreferencesDatabase::GetKvStore(void) return; } - DistributedKv::Status status; - DistributedKv::Options options; - options.createIfMissing = true; - options.autoSync = false; - options.kvStoreType = DistributedKv::KvStoreType::SINGLE_VERSION; - + DistributedKv::Options options = { + .createIfMissing = true, + .autoSync = false, + .area = DistributedKv::EL1, + .kvStoreType = DistributedKv::KvStoreType::SINGLE_VERSION, + .baseDir = KV_STORE_PATH + }; DistributedKv::AppId appId = {.appId = APP_ID}; DistributedKv::StoreId storeId = {.storeId = STORE_ID}; - status = kvDataManager_->GetSingleKvStore(options, appId, storeId, kvStore_); + DistributedKv::Status status = kvDataManager_->GetSingleKvStore(options, appId, storeId, kvStore_); if (status != DistributedKv::Status::SUCCESS) { ANS_LOGE("kvDataManager GetSingleKvStore failed ret = 0x%{public}x", status); kvStore_.reset(); @@ -96,12 +97,14 @@ bool DistributedPreferencesDatabase::PutToDistributedDB(const std::string &key, if (!KvStoreFlowControl()) { ANS_LOGE("kvStore flow control."); + CloseKvStore(); return false; } DistributedKv::Key kvStoreKey(key); DistributedKv::Value kvStoreValue(value); DistributedKv::Status status = kvStore_->Put(kvStoreKey, kvStoreValue); + CloseKvStore(); if (status != DistributedKv::Status::SUCCESS) { ANS_LOGE("kvStore Put() failed ret = 0x%{public}x", status); return false; @@ -113,26 +116,24 @@ bool DistributedPreferencesDatabase::PutToDistributedDB(const std::string &key, bool DistributedPreferencesDatabase::GetFromDistributedDB(const std::string &key, std::string &value) { std::lock_guard lock(mutex_); - if (!CheckKvStore()) { return false; } if (!KvStoreFlowControl()) { ANS_LOGE("kvStore flow control."); + CloseKvStore(); return false; } - DistributedKv::Key kvStoreKey(key); DistributedKv::Value kvStoreValue; DistributedKv::Status status = kvStore_->Get(kvStoreKey, kvStoreValue); + CloseKvStore(); if (status != DistributedKv::Status::SUCCESS) { ANS_LOGE("kvStore Get() failed ret = 0x%{public}x", status); return false; } - value = kvStoreValue.ToString(); - return true; } @@ -140,23 +141,21 @@ bool DistributedPreferencesDatabase::GetEntriesFromDistributedDB( const std::string &prefixKey, std::vector &entries) { std::lock_guard lock(mutex_); - if (!CheckKvStore()) { return false; } - if (!KvStoreFlowControl()) { ANS_LOGE("kvStore flow control."); + CloseKvStore(); return false; } - DistributedKv::Key kvStoreKey(prefixKey); DistributedKv::Status status = kvStore_->GetEntries(kvStoreKey, entries); + CloseKvStore(); if (status != DistributedKv::Status::SUCCESS) { ANS_LOGE("kvStore GetEntries() failed ret = 0x%{public}x", status); return false; } - return true; } @@ -167,15 +166,15 @@ bool DistributedPreferencesDatabase::DeleteToDistributedDB(const std::string &ke if (!CheckKvStore()) { return false; } - if (!KvStoreFlowControl()) { ANS_LOGE("kvStore flow control."); + CloseKvStore(); return false; } - DistributedKv::Key kvStoreKey(key); DistributedKv::Value kvStoreValue; DistributedKv::Status status = kvStore_->Delete(kvStoreKey); + CloseKvStore(); if (status != DistributedKv::Status::SUCCESS) { ANS_LOGE("kvStore Delete() failed ret = 0x%{public}x", status); return false; @@ -204,12 +203,18 @@ bool DistributedPreferencesDatabase::ClearDatabase(void) return false; } - status = kvDataManager_->DeleteKvStore(appId, storeId); + status = kvDataManager_->DeleteKvStore(appId, storeId, KV_STORE_PATH); if (status != DistributedKv::Status::SUCCESS) { ANS_LOGE("kvDataManager DeleteKvStore() failed ret = 0x%{public}x", status); return false; } return true; } + +void DistributedPreferencesDatabase::CloseKvStore() +{ + DistributedKv::AppId appId = {.appId = APP_ID}; + kvDataManager_->CloseKvStore(appId, kvStore_); +} } // namespace Notification } // namespace OHOS \ No newline at end of file diff --git a/services/distributed/src/distributed_screen_status_manager.cpp b/services/distributed/src/distributed_screen_status_manager.cpp index dfd721e41..d8b0bd5bc 100644 --- a/services/distributed/src/distributed_screen_status_manager.cpp +++ b/services/distributed/src/distributed_screen_status_manager.cpp @@ -21,12 +21,13 @@ namespace OHOS { namespace Notification { namespace { -const std::string APP_ID = "advanced_notification_service"; +const std::string APP_ID = "notification_service"; const std::string STORE_ID = "distributed_screen_status"; const std::string DELIMITER = "|"; const std::string SCREEN_STATUS_LABEL = "screen_status"; const std::string SCREEN_STATUS_VALUE_ON = "on"; const std::string SCREEN_STATUS_VALUE_OFF = "off"; +constexpr char KV_STORE_PATH[] = "/data/service/el1/public/database/notification_service"; } // namespace DistributedScreenStatusManager::DistributedScreenStatusManager() : DistributedFlowControl() @@ -37,7 +38,6 @@ DistributedScreenStatusManager::DistributedScreenStatusManager() : DistributedFl }; deviceCb_ = std::make_shared(callback); GetKvDataManager(); - GetKvStore(); } DistributedScreenStatusManager::~DistributedScreenStatusManager() @@ -46,6 +46,8 @@ DistributedScreenStatusManager::~DistributedScreenStatusManager() void DistributedScreenStatusManager::OnDeviceConnected(const std::string &deviceId) { ANS_LOGI("deviceId:%{public}s", deviceId.c_str()); + std::lock_guard lock(mutex_); + CheckKvStore(); } void DistributedScreenStatusManager::OnDeviceDisconnected(const std::string &deviceId) @@ -72,7 +74,7 @@ void DistributedScreenStatusManager::OnDeviceDisconnected(const std::string &dev DistributedKv::AppId appId = {.appId = APP_ID}; DistributedKv::StoreId storeId = {.storeId = STORE_ID}; - kvDataManager_->DeleteKvStore(appId, storeId); + kvDataManager_->DeleteKvStore(appId, storeId, KV_STORE_PATH); if (!CheckKvStore()) { return; @@ -112,16 +114,16 @@ void DistributedScreenStatusManager::GetKvStore(void) if (!CheckKvDataManager()) { return; } - - DistributedKv::Status status; - DistributedKv::Options options; - options.createIfMissing = true; - options.autoSync = true; - options.kvStoreType = DistributedKv::KvStoreType::SINGLE_VERSION; - + DistributedKv::Options options = { + .createIfMissing = true, + .autoSync = true, + .area = DistributedKv::EL1, + .kvStoreType = DistributedKv::KvStoreType::SINGLE_VERSION, + .baseDir = KV_STORE_PATH + }; DistributedKv::AppId appId = {.appId = APP_ID}; DistributedKv::StoreId storeId = {.storeId = STORE_ID}; - status = kvDataManager_->GetSingleKvStore(options, appId, storeId, kvStore_); + DistributedKv::Status status = kvDataManager_->GetSingleKvStore(options, appId, storeId, kvStore_); if (status != DistributedKv::Status::SUCCESS) { ANS_LOGE("kvDataManager GetSingleKvStore failed ret = 0x%{public}x", status); kvStore_.reset(); @@ -153,7 +155,7 @@ std::string DistributedScreenStatusManager::GenerateDistributedKey(const std::st ErrCode DistributedScreenStatusManager::CheckRemoteDevicesIsUsing(bool &isUsing) { std::lock_guard lock(mutex_); - if (!CheckKvDataManager() || !CheckKvStore()) { + if (!CheckKvDataManager() || kvStore_ == nullptr) { return ERR_ANS_DISTRIBUTED_OPERATION_FAILED; } @@ -204,7 +206,7 @@ ErrCode DistributedScreenStatusManager::SetLocalScreenStatus(bool screenOn) std::lock_guard lock(mutex_); ANS_LOGI("%{public}s, screenOn:%{public}s", __FUNCTION__, screenOn ? "true" : "false"); localScreenOn_ = screenOn; - if (!CheckKvStore()) { + if (kvStore_ == nullptr) { return ERR_ANS_DISTRIBUTED_OPERATION_FAILED; } diff --git a/services/distributed/test/unittest/distributed_database_test.cpp b/services/distributed/test/unittest/distributed_database_test.cpp index 329297a73..c6c6dcfcb 100644 --- a/services/distributed/test/unittest/distributed_database_test.cpp +++ b/services/distributed/test/unittest/distributed_database_test.cpp @@ -17,6 +17,7 @@ #include "gtest/gtest.h" +#define private public #include "distributed_database.h" using namespace testing::ext; @@ -67,6 +68,7 @@ void DistributedDatabaseTest::SetUp() databaseCallback_ = std::make_shared(databaseCallback); deviceCallback_ = std::make_shared(deviceCallback); database_ = std::make_shared(databaseCallback_, deviceCallback_); + database_->OnDeviceConnected(); } void DistributedDatabaseTest::TearDown() diff --git a/services/distributed/test/unittest/distributed_notification_manager_test.cpp b/services/distributed/test/unittest/distributed_notification_manager_test.cpp index e4ab49cf0..70d832fb1 100644 --- a/services/distributed/test/unittest/distributed_notification_manager_test.cpp +++ b/services/distributed/test/unittest/distributed_notification_manager_test.cpp @@ -16,7 +16,7 @@ #include #include "gtest/gtest.h" - +#define private public #include "distributed_notification_manager.h" using namespace testing::ext; @@ -42,6 +42,7 @@ protected: void DistributedNotificationManagerTest::SetUp() { distributedManager_ = DistributedNotificationManager::GetInstance(); + distributedManager_->OnDeviceConnected("test"); } void DistributedNotificationManagerTest::TearDown() diff --git a/services/distributed/test/unittest/distributed_screen_status_manager_test.cpp b/services/distributed/test/unittest/distributed_screen_status_manager_test.cpp index e1b68b00b..5c4b85d63 100644 --- a/services/distributed/test/unittest/distributed_screen_status_manager_test.cpp +++ b/services/distributed/test/unittest/distributed_screen_status_manager_test.cpp @@ -16,7 +16,7 @@ #include #include "gtest/gtest.h" - +#define private public #include "distributed_screen_status_manager.h" using namespace testing::ext; @@ -27,17 +27,18 @@ public: void SetUp() override; void TearDown() override; protected: - std::shared_ptr DistributedScreenStatusManager_; + std::shared_ptr distributedScreenStatusManager_; }; void DistributedScreenStatusManagerTest::SetUp() { - DistributedScreenStatusManager_ = DistributedScreenStatusManager::GetInstance(); + distributedScreenStatusManager_ = DistributedScreenStatusManager::GetInstance(); + distributedScreenStatusManager_->OnDeviceConnected("test"); } void DistributedScreenStatusManagerTest::TearDown() { - DistributedScreenStatusManager_ = nullptr; + distributedScreenStatusManager_ = nullptr; DistributedScreenStatusManager::DestroyInstance(); } @@ -50,7 +51,7 @@ HWTEST_F(DistributedScreenStatusManagerTest, CheckRemoteDevicesIsUsing_00100, Fu { bool isUsing = true; - EXPECT_EQ(DistributedScreenStatusManager_->CheckRemoteDevicesIsUsing(isUsing), ERR_OK); + EXPECT_EQ(distributedScreenStatusManager_->CheckRemoteDevicesIsUsing(isUsing), ERR_OK); } /** @@ -62,7 +63,7 @@ HWTEST_F(DistributedScreenStatusManagerTest, SetLocalScreenStatus_00100, Functio { bool screenOn = false; - EXPECT_EQ(DistributedScreenStatusManager_->SetLocalScreenStatus(screenOn), ERR_OK); + EXPECT_EQ(distributedScreenStatusManager_->SetLocalScreenStatus(screenOn), ERR_OK); } } // namespace Notification } // namespace OHOS \ No newline at end of file -- Gitee