diff --git a/services/reminder/BUILD.gn b/services/reminder/BUILD.gn index 182e3404d6d4964514d046c09b321b9fd3bbbc5d..146d6e1bf47d6bad9dbe616ff3fee9ff85eae8a6 100644 --- a/services/reminder/BUILD.gn +++ b/services/reminder/BUILD.gn @@ -54,7 +54,6 @@ ohos_source_set("reminder_service_sources") { "src/reminder_agent_service.cpp", "src/reminder_agent_service_ability.cpp", "src/reminder_bundle_manager_helper.cpp", - "src/reminder_config_change_observer.cpp", "src/reminder_data_manager.cpp", "src/reminder_data_manager_inner.cpp", "src/reminder_datashare_helper.cpp", diff --git a/services/reminder/include/reminder_config_change_observer.h b/services/reminder/include/reminder_config_change_observer.h deleted file mode 100644 index 982b7ddb1fffef61c9ae26bd9daf4fa2ffdc938c..0000000000000000000000000000000000000000 --- a/services/reminder/include/reminder_config_change_observer.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (c) 2023 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_REMINDER_INCLUDE_REMINDER_CONFIG_CHANGE_OBSERVER_H -#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_REMINDER_INCLUDE_REMINDER_CONFIG_CHANGE_OBSERVER_H - -#include "configuration_observer_stub.h" - -namespace OHOS { -namespace Notification { - -/** - * @brief Listening system language change, when the system language changes, - * notify ReminderDataManager. -*/ -class ReminderConfigChangeObserver final : public AppExecFwk::ConfigurationObserverStub { -public: - ReminderConfigChangeObserver() = default; - ~ReminderConfigChangeObserver() = default; - -public: - void OnConfigurationUpdated(const AppExecFwk::Configuration &configuration) override; - -private: - std::string languageInfo_; -}; -} // namespace Notification -} // namespace OHOS - -#endif \ No newline at end of file diff --git a/services/reminder/include/reminder_data_manager.h b/services/reminder/include/reminder_data_manager.h index efe1cfeb484ba18738226f1e7400c376685bd7cf..e5f859dbb653fed209f54ae1c1bb5c3c2ffa7ba7 100644 --- a/services/reminder/include/reminder_data_manager.h +++ b/services/reminder/include/reminder_data_manager.h @@ -30,7 +30,6 @@ #include "reminder_request_adaptation.h" #include "reminder_store.h" #include "reminder_timer_info.h" -#include "reminder_config_change_observer.h" #include "datashare_predicates.h" #include "datashare_values_bucket.h" #include "app_mgr_interface.h" diff --git a/services/reminder/include/reminder_event_manager.h b/services/reminder/include/reminder_event_manager.h index 4f1cae6276fa8a9fb6c81c265ba2bb4423f08d28..04021384628e6cd92e4cfa05bb4ebc8d6e60271b 100644 --- a/services/reminder/include/reminder_event_manager.h +++ b/services/reminder/include/reminder_event_manager.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -17,85 +17,84 @@ #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_REMINDER_INCLUDE_REMINDER_EVENT_MANAGER_H #include "common_event_subscriber.h" -#include "reminder_data_manager.h" -#include "system_ability_status_change_stub.h" #include "notification_subscriber.h" +#include "configuration_observer_stub.h" +#include "system_ability_status_change_stub.h" -#include - -namespace OHOS { -namespace Notification { +namespace OHOS::Notification { class ReminderEventManager { public: - explicit ReminderEventManager(std::shared_ptr &reminderDataManager); - virtual ~ReminderEventManager() {}; - ReminderEventManager(ReminderEventManager &other) = delete; - ReminderEventManager& operator = (const ReminderEventManager &other) = delete; + ReminderEventManager() = default; + ~ReminderEventManager() = default; + ReminderEventManager(ReminderEventManager& other) = delete; + ReminderEventManager& operator = (const ReminderEventManager& other) = delete; + +public: + void Init(); private: - void init(std::shared_ptr &reminderDataManager) const; - void SubscribeSystemAbility(std::shared_ptr &reminderDataManager) const; + void SubscribeEvent(); + void SubscribeSystemAbility(const int32_t systemAbilityId); class ReminderEventSubscriber : public EventFwk::CommonEventSubscriber { public: - ReminderEventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscriberInfo, - std::shared_ptr &reminderDataManager); - virtual void OnReceiveEvent(const EventFwk::CommonEventData &data); - -private: - sptr GetBundleOption(const OHOS::EventFwk::Want &want) const; - int32_t GetUid(const OHOS::EventFwk::Want &want) const; - void HandlePackageRemove(const EventFwk::Want &want) const; - void HandleProcessDied(const EventFwk::Want &want) const; - std::shared_ptr reminderDataManager_ = nullptr; + ReminderEventSubscriber(const EventFwk::CommonEventSubscribeInfo& subscriberInfo); + void OnReceiveEvent(const EventFwk::CommonEventData& data) override; }; -class ReminderEventCustomSubscriber : public EventFwk::CommonEventSubscriber { +class ReminderCustomEventSubscriber : public EventFwk::CommonEventSubscriber { public: - ReminderEventCustomSubscriber(const EventFwk::CommonEventSubscribeInfo &subscriberInfo, - std::shared_ptr &reminderDataManager); - virtual void OnReceiveEvent(const EventFwk::CommonEventData &data); - -private: - std::shared_ptr reminderDataManager_ = nullptr; + ReminderCustomEventSubscriber(const EventFwk::CommonEventSubscribeInfo& subscriberInfo); + void OnReceiveEvent(const EventFwk::CommonEventData& data) override; }; -class SystemAbilityStatusChangeListener : public OHOS::SystemAbilityStatusChangeStub { +class SystemAbilityStatusChangeListener : public SystemAbilityStatusChangeStub { public: - explicit SystemAbilityStatusChangeListener(std::shared_ptr &reminderDataManager); - ~SystemAbilityStatusChangeListener() {}; - virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; - virtual void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; -private: - std::shared_ptr reminderDataManager_ = nullptr; + SystemAbilityStatusChangeListener() = default; + ~SystemAbilityStatusChangeListener() = default; + + void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; + void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; }; class ReminderNotificationSubscriber : public NotificationSubscriber { public: - explicit ReminderNotificationSubscriber(std::shared_ptr &reminderDataManager); - ~ReminderNotificationSubscriber() override; - void OnConnected() override; - void OnDisconnected() override; - void OnCanceled(const std::shared_ptr &request, - const std::shared_ptr &sortingMap, int deleteReason) override; - void OnConsumed(const std::shared_ptr &request, - const std::shared_ptr &sortingMap) override; - void OnUpdate(const std::shared_ptr &sortingMap) override; - void OnDied() override; + ReminderNotificationSubscriber() = default; + ~ReminderNotificationSubscriber() override = default; + + void OnConnected() override {} + void OnDisconnected() override {} + void OnCanceled(const std::shared_ptr& request, + const std::shared_ptr& sortingMap, int deleteReason) override; + void OnConsumed(const std::shared_ptr& request, + const std::shared_ptr& sortingMap) override {} + void OnUpdate(const std::shared_ptr& sortingMap) override {} + void OnDied() override {} void OnDoNotDisturbDateChange( - const std::shared_ptr &date) override; + const std::shared_ptr& date) override {} void OnEnabledNotificationChanged( - const std::shared_ptr &callbackData) override; - void OnBadgeChanged(const std::shared_ptr &badgeData) override; - void OnBadgeEnabledChanged(const sptr &callbackData) override; - void OnBatchCanceled(const std::vector> &requestList, - const std::shared_ptr &sortingMap, int32_t deleteReason) override; -private: - std::shared_ptr reminderDataManager_ = nullptr; + const std::shared_ptr& callbackData) override {} + void OnBadgeChanged(const std::shared_ptr& badgeData) override {} + void OnBadgeEnabledChanged(const sptr& callbackData) override {} + void OnBatchCanceled(const std::vector>& requestList, + const std::shared_ptr& sortingMap, int32_t deleteReason) override {} }; +}; + +/** + * @brief Listening system language change, when the system language changes, + * notify ReminderDataManager. + */ +class ReminderConfigChangeObserver final : public AppExecFwk::ConfigurationObserverStub { +public: + ReminderConfigChangeObserver() = default; + ~ReminderConfigChangeObserver() = default; - static std::shared_ptr subscriber_; +public: + void OnConfigurationUpdated(const AppExecFwk::Configuration& configuration) override; + +private: + std::string languageInfo_; }; -} // namespace OHOS -} // namespace Notification +} // namespace OHOS::Notification #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_REMINDER_INCLUDE_REMINDER_EVENT_MANAGER_H diff --git a/services/reminder/src/reminder_config_change_observer.cpp b/services/reminder/src/reminder_config_change_observer.cpp deleted file mode 100644 index 81b6257d7c0a30f80d12a63e36dae1489005a17b..0000000000000000000000000000000000000000 --- a/services/reminder/src/reminder_config_change_observer.cpp +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2023 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "reminder_config_change_observer.h" - -#include "ans_log_wrapper.h" -#include "reminder_data_manager.h" - -namespace OHOS { -namespace Notification { - -void ReminderConfigChangeObserver::OnConfigurationUpdated(const AppExecFwk::Configuration &configuration) -{ - ANSR_LOGD("called"); - auto reminderDataMgr = ReminderDataManager::GetInstance(); - if (reminderDataMgr == nullptr) { - ANSR_LOGE("null reminderDataMgr"); - return; - } - std::string newLanguageInfo = configuration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE); - if (!newLanguageInfo.empty() && newLanguageInfo != languageInfo_) { - ANSR_LOGD("language change: %{public}s -> %{public}s", languageInfo_.c_str(), newLanguageInfo.c_str()); - reminderDataMgr->OnLanguageChanged(); - languageInfo_ = newLanguageInfo; - } -} - -} // namespace Notification -} // namespace OHOS diff --git a/services/reminder/src/reminder_data_manager.cpp b/services/reminder/src/reminder_data_manager.cpp index d3b0509f1aa79747250ebb9f39cef92d5c2e70d3..66ccade9fcd97c9afd6155c9a6b047674d1154dc 100644 --- a/services/reminder/src/reminder_data_manager.cpp +++ b/services/reminder/src/reminder_data_manager.cpp @@ -622,7 +622,8 @@ std::shared_ptr ReminderDataManager::InitInstance() if (REMINDER_DATA_MANAGER == nullptr) { REMINDER_DATA_MANAGER = std::make_shared(); REMINDER_DATA_MANAGER->Init(); - ReminderEventManager reminderEventManager(REMINDER_DATA_MANAGER); + ReminderEventManager reminderEventManager; + reminderEventManager.Init(); } return REMINDER_DATA_MANAGER; } diff --git a/services/reminder/src/reminder_event_manager.cpp b/services/reminder/src/reminder_event_manager.cpp index ec486dc45b3bb7e441e4c39a6b6435fb0a97924b..a261de8f4908ef0f38fb3165f0707ae691b4f985 100644 --- a/services/reminder/src/reminder_event_manager.cpp +++ b/services/reminder/src/reminder_event_manager.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -16,34 +16,30 @@ #include "reminder_event_manager.h" #include "ans_log_wrapper.h" -#include "bundle_constants.h" -#include "bundle_mgr_interface.h" -#include "common_event_manager.h" -#include "common_event_support.h" +#include "reminder_data_manager.h" #include "reminder_bundle_manager_helper.h" -#include "if_system_ability_manager.h" + #include "ipc_skeleton.h" -#include "iservice_registry.h" -#include "system_ability_definition.h" #include "notification_helper.h" -#include "string_ex.h" +#include "common_event_manager.h" +#include "common_event_support.h" +#include "system_ability_definition.h" +#include "if_system_ability_manager.h" -using namespace OHOS::EventFwk; -namespace OHOS { -namespace Notification { -static const std::string NOTIFICATION_LABEL = "REMINDER_AGENT"; +namespace OHOS::Notification { static constexpr const char* UNLOCK_SCREEN_EVENT = "common.event.UNLOCK_SCREEN"; -std::shared_ptr ReminderEventManager::subscriber_ - = nullptr; -ReminderEventManager::ReminderEventManager(std::shared_ptr &reminderDataManager) +void ReminderEventManager::Init() { - init(reminderDataManager); + SubscribeEvent(); + SubscribeSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + SubscribeSystemAbility(APP_MGR_SERVICE_ID); + SubscribeSystemAbility(ABILITY_MGR_SERVICE_ID); } -void ReminderEventManager::init(std::shared_ptr &reminderDataManager) const +void ReminderEventManager::SubscribeEvent() { - MatchingSkills customMatchingSkills; + EventFwk::MatchingSkills customMatchingSkills; customMatchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_ALARM_ALERT); customMatchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_ALERT_TIMEOUT); customMatchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_CLOSE_ALERT); @@ -51,223 +47,177 @@ void ReminderEventManager::init(std::shared_ptr &reminderDa customMatchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_REMOVE_NOTIFICATION); customMatchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_CUSTOM_ALERT); customMatchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_CLICK_ALERT); - CommonEventSubscribeInfo customSubscriberInfo(customMatchingSkills); + EventFwk::CommonEventSubscribeInfo customSubscriberInfo(customMatchingSkills); customSubscriberInfo.SetPermission("ohos.permission.GRANT_SENSITIVE_PERMISSIONS"); customSubscriberInfo.SetThreadMode(EventFwk::CommonEventSubscribeInfo::COMMON); - auto customSubscriber = std::make_shared(customSubscriberInfo, reminderDataManager); - - MatchingSkills matchingSkills; - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED); - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED); - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_RESTARTED); - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED); - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_TIME_CHANGED); - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED); - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_REMOVED); - CommonEventSubscribeInfo subscriberInfo(matchingSkills); + auto customSubscriber = std::make_shared(customSubscriberInfo); + + EventFwk::MatchingSkills matchingSkills; + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_RESTARTED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_TIME_CHANGED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED); + EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills); subscriberInfo.SetThreadMode(EventFwk::CommonEventSubscribeInfo::COMMON); - auto subscriber = std::make_shared(subscriberInfo, reminderDataManager); + auto subscriber = std::make_shared(subscriberInfo); - MatchingSkills screenMatchingSkills; + EventFwk::MatchingSkills screenMatchingSkills; screenMatchingSkills.AddEvent(UNLOCK_SCREEN_EVENT); - CommonEventSubscribeInfo screenSubscriberInfo(screenMatchingSkills); + EventFwk::CommonEventSubscribeInfo screenSubscriberInfo(screenMatchingSkills); screenSubscriberInfo.SetPublisherBundleName(AppExecFwk::Constants::SCENE_BOARD_BUNDLE_NAME); - auto screenSubscriber = std::make_shared(screenSubscriberInfo, reminderDataManager); + auto screenSubscriber = std::make_shared(screenSubscriberInfo); std::string identity = IPCSkeleton::ResetCallingIdentity(); - if (CommonEventManager::SubscribeCommonEvent(subscriber) && - CommonEventManager::SubscribeCommonEvent(customSubscriber) && - CommonEventManager::SubscribeCommonEvent(screenSubscriber)) { - ANSR_LOGD("SubscribeCommonEvent ok"); + if (EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber) && + EventFwk::CommonEventManager::SubscribeCommonEvent(customSubscriber) && + EventFwk::CommonEventManager::SubscribeCommonEvent(screenSubscriber)) { + ANSR_LOGD("SubscribeCommonEvent ok."); } else { - ANSR_LOGD("SubscribeCommonEvent fail"); + ANSR_LOGW("SubscribeCommonEvent failed."); } IPCSkeleton::SetCallingIdentity(identity); - subscriber_ = std::make_shared(reminderDataManager); - if (NotificationHelper::SubscribeNotification(*subscriber_) != ERR_OK) { - ANSR_LOGD("SubscribeNotification failed"); + auto notificationSubscriber = std::make_shared(); + if (NotificationHelper::SubscribeNotification(*notificationSubscriber) != ERR_OK) { + ANSR_LOGW("SubscribeNotification failed."); } - - SubscribeSystemAbility(reminderDataManager); } -void ReminderEventManager::SubscribeSystemAbility(std::shared_ptr &reminderDataManager) const +void ReminderEventManager::SubscribeSystemAbility(const int32_t systemAbilityId) { sptr statusChangeListener - = new (std::nothrow) SystemAbilityStatusChangeListener(reminderDataManager); + = new (std::nothrow) SystemAbilityStatusChangeListener(); if (statusChangeListener == nullptr) { - ANSR_LOGE("Failed to create statusChangeListener due to no memory."); + ANSR_LOGW("Failed to create SystemAbilityStatusChangeListener."); return; } - // app mgr - sptr appMgrStatusChangeListener - = new (std::nothrow) SystemAbilityStatusChangeListener(reminderDataManager); - if (appMgrStatusChangeListener == nullptr) { - ANSR_LOGE("Failed to create appMgrStatusChangeListener due to no memory."); - return; - } - // ability mgr - sptr abilityMgrStatusListener - = new (std::nothrow) SystemAbilityStatusChangeListener(reminderDataManager); - if (abilityMgrStatusListener == nullptr) { - ANSR_LOGE("Failed to create abilityMgrStatusListener due to no memory."); - return; - } - sptr samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (samgrProxy == nullptr) { - ANSR_LOGD("samgrProxy is null"); + ANSR_LOGW("GetSystemAbilityManager failed."); return; } - int32_t ret = samgrProxy->SubscribeSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, statusChangeListener); + int32_t ret = samgrProxy->SubscribeSystemAbility(systemAbilityId, statusChangeListener); if (ret != ERR_OK) { - ANSR_LOGE("subscribe system ability id: %{public}d failed", BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); - } - ret = samgrProxy->SubscribeSystemAbility(APP_MGR_SERVICE_ID, appMgrStatusChangeListener); - if (ret != ERR_OK) { - ANSR_LOGE("subscribe system ability id: %{public}d failed", APP_MGR_SERVICE_ID); - } - ret = samgrProxy->SubscribeSystemAbility(ABILITY_MGR_SERVICE_ID, abilityMgrStatusListener); - if (ret != ERR_OK) { - ANSR_LOGE("subscribe system ability id: %{public}d failed", ABILITY_MGR_SERVICE_ID); + ANSR_LOGW("SubscribeSystemAbility id: %{public}d failed.", systemAbilityId); } } ReminderEventManager::ReminderEventSubscriber::ReminderEventSubscriber( - const CommonEventSubscribeInfo &subscriberInfo, - std::shared_ptr &reminderDataManager) : CommonEventSubscriber(subscriberInfo) -{ - reminderDataManager_ = reminderDataManager; -} + const EventFwk::CommonEventSubscribeInfo& subscriberInfo) : CommonEventSubscriber(subscriberInfo) +{} -ReminderEventManager::ReminderEventCustomSubscriber::ReminderEventCustomSubscriber( - const CommonEventSubscribeInfo &subscriberInfo, - std::shared_ptr &reminderDataManager) : CommonEventSubscriber(subscriberInfo) +void ReminderEventManager::ReminderEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData& data) { - reminderDataManager_ = reminderDataManager; -} - -void ReminderEventManager::ReminderEventCustomSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data) -{ - Want want = data.GetWant(); + auto manager = ReminderDataManager::GetInstance(); + if (manager == nullptr) { + return; + } + EventFwk::Want want = data.GetWant(); std::string action = want.GetAction(); - ANSR_LOGI("Recieved common event:%{public}s", action.c_str()); - if (action == ReminderRequest::REMINDER_EVENT_ALARM_ALERT) { - reminderDataManager_->ShowActiveReminder(want); + ANSR_LOGD("Recieved common event:%{public}s.", action.c_str()); + if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED || + action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED) { + AppExecFwk::ElementName ele = want.GetElement(); + std::string bundleName = ele.GetBundleName(); + int32_t userId = want.GetIntParam(AppExecFwk::Constants::USER_ID, -1); + int32_t uid = want.GetIntParam(AppExecFwk::Constants::UID, -1); + manager->CancelAllReminders(bundleName, userId, uid); return; } - if (action == ReminderRequest::REMINDER_EVENT_ALERT_TIMEOUT) { - reminderDataManager_->TerminateAlerting(want); + if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_RESTARTED) { + AppExecFwk::ElementName ele = want.GetElement(); + std::string bundleName = ele.GetBundleName(); + int32_t userId = want.GetIntParam(AppExecFwk::Constants::USER_ID, -1); + int32_t uid = ReminderBundleManagerHelper::GetInstance().GetDefaultUidByBundleName(bundleName, userId); + manager->OnProcessDiedLocked(uid); return; } - if (action == ReminderRequest::REMINDER_EVENT_CLOSE_ALERT) { - reminderDataManager_->CloseReminder(want, true); + if (action == EventFwk::CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED) { + manager->RefreshRemindersDueToSysTimeChange(ReminderDataManager::TIME_ZONE_CHANGE); return; } - if (action == ReminderRequest::REMINDER_EVENT_SNOOZE_ALERT) { - reminderDataManager_->SnoozeReminder(want); + if (action == EventFwk::CommonEventSupport::COMMON_EVENT_TIME_CHANGED) { + manager->RefreshRemindersDueToSysTimeChange(ReminderDataManager::DATE_TIME_CHANGE); return; } - if (action == ReminderRequest::REMINDER_EVENT_CUSTOM_ALERT) { - reminderDataManager_->HandleCustomButtonClick(want); + if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) { + manager->OnUserSwitch(data.GetCode()); return; } - if (action == ReminderRequest::REMINDER_EVENT_REMOVE_NOTIFICATION) { - reminderDataManager_->CloseReminder(want, false, false); + if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED) { + manager->OnUserRemove(data.GetCode()); return; } - if (action == ReminderRequest::REMINDER_EVENT_CLICK_ALERT) { - reminderDataManager_->ClickReminder(want); + if (action.compare(UNLOCK_SCREEN_EVENT) == 0) { + manager->OnUnlockScreen(); return; } } -void ReminderEventManager::ReminderEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data) +ReminderEventManager::ReminderCustomEventSubscriber::ReminderCustomEventSubscriber( + const EventFwk::CommonEventSubscribeInfo& subscriberInfo) : CommonEventSubscriber(subscriberInfo) +{} + +void ReminderEventManager::ReminderCustomEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData& data) { - Want want = data.GetWant(); - std::string action = want.GetAction(); - ANSR_LOGD("Recieved common event:%{public}s", action.c_str()); - if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) { - HandlePackageRemove(want); + auto manager = ReminderDataManager::GetInstance(); + if (manager == nullptr) { return; } - if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED) { - HandlePackageRemove(want); + EventFwk::Want want = data.GetWant(); + std::string action = want.GetAction(); + ANSR_LOGI("Recieved common event:%{public}s.", action.c_str()); + if (action == ReminderRequest::REMINDER_EVENT_ALARM_ALERT) { + manager->ShowActiveReminder(want); return; } - if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_RESTARTED) { - HandleProcessDied(want); + if (action == ReminderRequest::REMINDER_EVENT_ALERT_TIMEOUT) { + manager->TerminateAlerting(want); return; } - if (action == CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED) { - reminderDataManager_->RefreshRemindersDueToSysTimeChange(ReminderDataManager::TIME_ZONE_CHANGE); + if (action == ReminderRequest::REMINDER_EVENT_CLOSE_ALERT) { + manager->CloseReminder(want, true); return; } - if (action == CommonEventSupport::COMMON_EVENT_TIME_CHANGED) { - reminderDataManager_->RefreshRemindersDueToSysTimeChange(ReminderDataManager::DATE_TIME_CHANGE); + if (action == ReminderRequest::REMINDER_EVENT_SNOOZE_ALERT) { + manager->SnoozeReminder(want); return; } - if (action == CommonEventSupport::COMMON_EVENT_USER_SWITCHED) { - reminderDataManager_->OnUserSwitch(data.GetCode()); + if (action == ReminderRequest::REMINDER_EVENT_CUSTOM_ALERT) { + manager->HandleCustomButtonClick(want); return; } - if (action == CommonEventSupport::COMMON_EVENT_USER_REMOVED) { - reminderDataManager_->OnUserRemove(data.GetCode()); + if (action == ReminderRequest::REMINDER_EVENT_REMOVE_NOTIFICATION) { + manager->CloseReminder(want, false, false); return; } - if (action.compare(UNLOCK_SCREEN_EVENT) == 0) { - reminderDataManager_->OnUnlockScreen(); + if (action == ReminderRequest::REMINDER_EVENT_CLICK_ALERT) { + manager->ClickReminder(want); + return; } } -void ReminderEventManager::ReminderEventSubscriber::HandlePackageRemove(const EventFwk::Want &want) const -{ - OHOS::AppExecFwk::ElementName ele = want.GetElement(); - std::string bundleName = ele.GetBundleName(); - int32_t userId = want.GetIntParam(OHOS::AppExecFwk::Constants::USER_ID, -1); - int32_t uid = want.GetIntParam(OHOS::AppExecFwk::Constants::UID, -1); - reminderDataManager_->CancelAllReminders(bundleName, userId, uid); -} - -void ReminderEventManager::ReminderEventSubscriber::HandleProcessDied(const EventFwk::Want &want) const -{ - int32_t uid = GetUid(want); - reminderDataManager_->OnProcessDiedLocked(uid); -} - -int32_t ReminderEventManager::ReminderEventSubscriber::GetUid( - const OHOS::EventFwk::Want &want) const -{ - OHOS::AppExecFwk::ElementName ele = want.GetElement(); - std::string bundleName = ele.GetBundleName(); - int32_t userId = want.GetIntParam(OHOS::AppExecFwk::Constants::USER_ID, -1); - int32_t uid = ReminderBundleManagerHelper::GetInstance().GetDefaultUidByBundleName(bundleName, userId); - ANSR_LOGD("bundleName=%{public}s, userId=%{private}d, uid=%{public}d", bundleName.c_str(), userId, uid); - return uid; -} - -ReminderEventManager::SystemAbilityStatusChangeListener::SystemAbilityStatusChangeListener( - std::shared_ptr &reminderDataManager) -{ - reminderDataManager_ = reminderDataManager; -} - void ReminderEventManager::SystemAbilityStatusChangeListener::OnAddSystemAbility( int32_t systemAbilityId, const std::string& deviceId) { - ANSR_LOGD("OnAddSystemAbilityInner"); + auto manager = ReminderDataManager::GetInstance(); + if (manager == nullptr) { + return; + } switch (systemAbilityId) { case BUNDLE_MGR_SERVICE_SYS_ABILITY_ID: - ANSR_LOGD("OnAddSystemAbilityInner: BUNDLE_MGR_SERVICE_SYS_ABILITY"); - reminderDataManager_->OnBundleMgrServiceStart(); + ANSR_LOGD("AddSystemAbility: BUNDLE_MGR_SERVICE_SYS_ABILITY_ID."); + manager->OnBundleMgrServiceStart(); break; case APP_MGR_SERVICE_ID: - ANSR_LOGD("OnAddSystemAbilityInner: APP_MGR_SERVICE"); + ANSR_LOGD("AddSystemAbility: APP_MGR_SERVICE_ID."); break; case ABILITY_MGR_SERVICE_ID: - ANSR_LOGD("OnAddSystemAbilityInner ABILITY_MGR_SERVICE_ID"); - reminderDataManager_->OnAbilityMgrServiceStart(); + ANSR_LOGD("AddSystemAbility: ABILITY_MGR_SERVICE_ID."); + manager->OnAbilityMgrServiceStart(); break; default: break; @@ -277,38 +227,29 @@ void ReminderEventManager::SystemAbilityStatusChangeListener::OnAddSystemAbility void ReminderEventManager::SystemAbilityStatusChangeListener::OnRemoveSystemAbility( int32_t systemAbilityId, const std::string& deviceId) { - ANSR_LOGD("OnRemoveSystemAbilityInner"); + auto manager = ReminderDataManager::GetInstance(); + if (manager == nullptr) { + return; + } switch (systemAbilityId) { case BUNDLE_MGR_SERVICE_SYS_ABILITY_ID: - ANSR_LOGD("OnRemoveSystemAbilityInner: BUNDLE_MGR_SERVICE_SYS_ABILITY"); + ANSR_LOGD("RemoveSystemAbility: BUNDLE_MGR_SERVICE_SYS_ABILITY_ID."); break; case APP_MGR_SERVICE_ID: - ANSR_LOGD("OnRemoveSystemAbilityInner: APP_MGR_SERVICE"); - reminderDataManager_->OnRemoveAppMgr(); + ANSR_LOGD("RemoveSystemAbility: APP_MGR_SERVICE_ID."); + manager->OnRemoveAppMgr(); break; case ABILITY_MGR_SERVICE_ID: - ANSR_LOGD("OnRemoveSystemAbilityInner ABILITY_MGR_SERVICE_ID"); + ANSR_LOGD("RemoveSystemAbility: ABILITY_MGR_SERVICE_ID."); break; default: break; } } -ReminderEventManager::ReminderNotificationSubscriber::ReminderNotificationSubscriber( - std::shared_ptr &reminderDataManager) -{ - reminderDataManager_ = reminderDataManager; -} - -ReminderEventManager::ReminderNotificationSubscriber::~ReminderNotificationSubscriber() {} - -void ReminderEventManager::ReminderNotificationSubscriber::OnConnected() {} - -void ReminderEventManager::ReminderNotificationSubscriber::OnDisconnected() {} - void ReminderEventManager::ReminderNotificationSubscriber::OnCanceled( - const std::shared_ptr ¬ification, - const std::shared_ptr &sortingMap, int deleteReason) + const std::shared_ptr& notification, + const std::shared_ptr& sortingMap, int deleteReason) { // Note: Don't modify param notification if (deleteReason != NotificationConstant::TRIGGER_AUTO_DELETE_REASON_DELETE) { @@ -320,40 +261,30 @@ void ReminderEventManager::ReminderNotificationSubscriber::OnCanceled( NotificationRequest request = notification->GetNotificationRequest(); std::string label = request.GetLabel(); int64_t autoDeletedTime = request.GetAutoDeletedTime(); - if (autoDeletedTime <= 0 || label != NOTIFICATION_LABEL) { + if (autoDeletedTime <= 0 || label.compare(ReminderRequest::NOTIFICATION_LABEL) != 0) { return; } - if (reminderDataManager_ == nullptr) { + auto manager = ReminderDataManager::GetInstance(); + if (manager == nullptr) { return; } int32_t notificationId = request.GetNotificationId(); int32_t uid = request.GetOwnerUid() == 0 ? request.GetCreatorUid() : request.GetOwnerUid(); - reminderDataManager_->HandleAutoDeleteReminder(notificationId, uid, autoDeletedTime); + manager->HandleAutoDeleteReminder(notificationId, uid, autoDeletedTime); } -void ReminderEventManager::ReminderNotificationSubscriber::OnConsumed(const std::shared_ptr ¬ification, - const std::shared_ptr &sortingMap) {} - -void ReminderEventManager::ReminderNotificationSubscriber::OnUpdate( - const std::shared_ptr &sortingMap) {} - -void ReminderEventManager::ReminderNotificationSubscriber::OnDied() {} - -void ReminderEventManager::ReminderNotificationSubscriber::OnDoNotDisturbDateChange( - const std::shared_ptr &date) {} - -void ReminderEventManager::ReminderNotificationSubscriber::OnEnabledNotificationChanged( - const std::shared_ptr &callbackData) {} - -void ReminderEventManager::ReminderNotificationSubscriber::OnBadgeChanged( - const std::shared_ptr &badgeData) {} - -void ReminderEventManager::ReminderNotificationSubscriber::OnBadgeEnabledChanged( - const sptr &callbackData) {} - -void ReminderEventManager::ReminderNotificationSubscriber::OnBatchCanceled( - const std::vector> &requestList, - const std::shared_ptr &sortingMap, int32_t deleteReason) {} -} // namespace OHOS -} // namespace Notification +void ReminderConfigChangeObserver::OnConfigurationUpdated(const AppExecFwk::Configuration& configuration) +{ + auto manager = ReminderDataManager::GetInstance(); + if (manager == nullptr) { + return; + } + std::string newLanguageInfo = configuration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE); + if (!newLanguageInfo.empty() && newLanguageInfo != languageInfo_) { + ANSR_LOGD("Language change: %{public}s -> %{public}s.", languageInfo_.c_str(), newLanguageInfo.c_str()); + manager->OnLanguageChanged(); + languageInfo_ = newLanguageInfo; + } +} +} // namespace OHOS::Notification diff --git a/services/reminder/test/unittest/BUILD.gn b/services/reminder/test/unittest/BUILD.gn index 3419ce493809dd60058b157f23823eadd281dfe0..8f95afd0bc079a09cfc8ec4f22259dd777752665 100644 --- a/services/reminder/test/unittest/BUILD.gn +++ b/services/reminder/test/unittest/BUILD.gn @@ -403,12 +403,77 @@ ohos_unittest("reminder_agent_service_ability_test") { part_name = "${component_name}" } +ohos_unittest("reminder_event_manager_test") { + sanitize = { + integer_overflow = true + ubsan = true + boundary_sanitize = true + cfi = true + cfi_cross_dso = true + debug = false + } + module_out_path = module_output_path + include_dirs = [ + ".", + "include", + "/${services_path}/reminder/include", + "${services_path}/reminder/test/unittest/mock/include", + ] + sources = [ + "${services_path}/reminder/src/reminder_event_manager.cpp", + "mock/mock_notification_helper.cpp", + "mock/mock_reminder_bundle_manager_helper.cpp", + "mock/mock_reminder_data_manager.cpp", + "reminder_event_manager_test.cpp", + ] + deps = [ + "${frameworks_module_ans_path}:ans_innerkits", + "${frameworks_module_reminder_path}:reminder_innerkits", + ] + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "ability_runtime:abilitykit_native", + "ability_runtime:app_manager", + "ability_runtime:wantagent_innerkits", + "access_token:libaccesstoken_sdk", + "access_token:libtokenid_sdk", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "common_event_service:cesfwk_innerkits", + "data_share:datashare_common", + "eventhandler:libeventhandler", + "ffrt:libffrt", + "hilog:libhilog", + "hitrace:hitrace_meter", + "hitrace:libhitracechain", + "image_framework:image_native", + "init:libbegetutil", + "ipc:ipc_core", + "kv_store:distributeddata_inner", + "os_account:os_account_innerkits", + "relational_store:native_rdb", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + "time_service:time_client", + ] + + cflags = [ + "-Dprivate=public", + "-Dprotected=public", + ] + subsystem_name = "${subsystem_name}" + part_name = "${component_name}" +} + group("unittest") { testonly = true deps = [ ":reminder_agent_service_ability_test", ":reminder_agent_service_test", ":reminder_bundle_manager_helper_test", + ":reminder_event_manager_test", ":reminder_service_test", ":reminder_unit_test", ] diff --git a/services/reminder/test/unittest/mock/include/mock_notification_helper.h b/services/reminder/test/unittest/mock/include/mock_notification_helper.h index 802fe8c38ab376c613a1b823e1269f6e052f74eb..bb1e992d14b49552ba197013225c5e4aa3c19c17 100644 --- a/services/reminder/test/unittest/mock/include/mock_notification_helper.h +++ b/services/reminder/test/unittest/mock/include/mock_notification_helper.h @@ -23,6 +23,8 @@ class MockNotificationHelper { public: static void MockIsAllowUseReminder(const bool isAllowUseReminder); static void MockIsAllowedNotify(const bool isAllowedNotify, const int32_t ret); + static void MockSubscribeNotification(const int32_t ret); + static void MockSubscribeCommonEvent(const bool ret); }; } // namespace OHOS::Notification diff --git a/services/reminder/test/unittest/mock/include/mock_reminder_data_manager.h b/services/reminder/test/unittest/mock/include/mock_reminder_data_manager.h index 73a0742e147ce591e39fdad43fcdd04a76ec91cd..5cab506794cc33d552688dde2d7e8f403ee8e25a 100644 --- a/services/reminder/test/unittest/mock/include/mock_reminder_data_manager.h +++ b/services/reminder/test/unittest/mock/include/mock_reminder_data_manager.h @@ -29,6 +29,26 @@ public: static void MockDelExcludeDates(const int32_t ret); static void MockGetExcludeDates(const int32_t ret); static void MockQueryActiveReminderCount(const int32_t ret); + + static void ResetFlag(); + + static bool callCancelAllReminders_; + static bool callOnProcessDiedLocked_; + static bool callRefreshRemindersDueToSysTimeChange_; + static bool callOnUserSwitch_; + static bool callOnUserRemove_; + static bool callOnUnlockScreen_; + static bool callShowActiveReminder_; + static bool callTerminateAlerting_; + static bool callCloseReminder_; + static bool callSnoozeReminder_; + static bool callHandleCustomButtonClick_; + static bool callClickReminder_; + static bool callOnBundleMgrServiceStart_; + static bool callOnAbilityMgrServiceStart_; + static bool callOnRemoveAppMgr_; + static bool callHandleAutoDeleteReminder_; + static bool callOnLanguageChanged_; }; } // namespace OHOS::Notification diff --git a/services/reminder/test/unittest/mock/mock_notification_helper.cpp b/services/reminder/test/unittest/mock/mock_notification_helper.cpp index 3058d8f269bb985cb9a4b16c331b04cac40f6f52..754fcc29721098dc00a822a712e0bc39bd8fe3ed 100644 --- a/services/reminder/test/unittest/mock/mock_notification_helper.cpp +++ b/services/reminder/test/unittest/mock/mock_notification_helper.cpp @@ -16,14 +16,18 @@ #include "mock_notification_helper.h" #include "notification_helper.h" +#include "common_event_manager.h" -namespace OHOS::Notification { +namespace OHOS { namespace { bool g_mockIsAllowUseReminder = true; bool g_mockIsAllowedNotify = true; +bool g_mockSubscribeCommonEventRet = true; int32_t g_mockIsAllowedNotifyRet = 0; +int32_t g_mockSubscribeNotificationRet = 0; } +namespace Notification { void MockNotificationHelper::MockIsAllowUseReminder(const bool isAllowUseReminder) { g_mockIsAllowUseReminder = isAllowUseReminder; @@ -35,6 +39,16 @@ void MockNotificationHelper::MockIsAllowedNotify(const bool isAllowedNotify, con g_mockIsAllowedNotifyRet = ret; } +void MockNotificationHelper::MockSubscribeNotification(const int32_t ret) +{ + g_mockSubscribeNotificationRet = ret; +} + +void MockNotificationHelper::MockSubscribeCommonEvent(const bool ret) +{ + g_mockSubscribeCommonEventRet = ret; +} + ErrCode NotificationHelper::AllowUseReminder(const std::string& bundleName, bool& isAllowUseReminder) { isAllowUseReminder = g_mockIsAllowUseReminder; @@ -46,4 +60,17 @@ ErrCode NotificationHelper::IsAllowedNotify(const NotificationBundleOption& bund allowed = g_mockIsAllowedNotify; return g_mockIsAllowedNotifyRet; } -} // namespace OHOS::Notification \ No newline at end of file + +ErrCode NotificationHelper::SubscribeNotification(const NotificationSubscriber& subscriber) +{ + return g_mockSubscribeNotificationRet; +} +} + +namespace EventFwk { +bool CommonEventManager::SubscribeCommonEvent(const std::shared_ptr& subscriber) +{ + return g_mockSubscribeCommonEventRet; +} +} +} // namespace OHOS \ No newline at end of file diff --git a/services/reminder/test/unittest/mock/mock_reminder_data_manager.cpp b/services/reminder/test/unittest/mock/mock_reminder_data_manager.cpp index 490b4c7375c32ebeda162db256ea0e10225ce107..f83c9f04fceeff5b617bf2651793b43a90c33d28 100644 --- a/services/reminder/test/unittest/mock/mock_reminder_data_manager.cpp +++ b/services/reminder/test/unittest/mock/mock_reminder_data_manager.cpp @@ -29,6 +29,24 @@ int32_t g_mockGetExcludeDatesRet = 0; int32_t g_mockQueryActiveReminderCountRet = 0; } +bool MockReminderDataManager::callCancelAllReminders_ = false; +bool MockReminderDataManager::callOnProcessDiedLocked_ = false; +bool MockReminderDataManager::callRefreshRemindersDueToSysTimeChange_ = false; +bool MockReminderDataManager::callOnUserSwitch_ = false; +bool MockReminderDataManager::callOnUserRemove_ = false; +bool MockReminderDataManager::callOnUnlockScreen_ = false; +bool MockReminderDataManager::callShowActiveReminder_ = false; +bool MockReminderDataManager::callTerminateAlerting_ = false; +bool MockReminderDataManager::callCloseReminder_ = false; +bool MockReminderDataManager::callSnoozeReminder_ = false; +bool MockReminderDataManager::callHandleCustomButtonClick_ = false; +bool MockReminderDataManager::callClickReminder_ = false; +bool MockReminderDataManager::callOnBundleMgrServiceStart_ = false; +bool MockReminderDataManager::callOnAbilityMgrServiceStart_ = false; +bool MockReminderDataManager::callOnRemoveAppMgr_ = false; +bool MockReminderDataManager::callHandleAutoDeleteReminder_ = false; +bool MockReminderDataManager::callOnLanguageChanged_ = false; + void MockReminderDataManager::MockPublishReminder(const int32_t ret) { g_mockPublishReminderRet = ret; @@ -69,6 +87,27 @@ void MockReminderDataManager::MockQueryActiveReminderCount(const int32_t ret) g_mockQueryActiveReminderCountRet = ret; } +void MockReminderDataManager::ResetFlag() +{ + callCancelAllReminders_ = false; + callOnProcessDiedLocked_ = false; + callRefreshRemindersDueToSysTimeChange_ = false; + callOnUserSwitch_ = false; + callOnUserRemove_ = false; + callOnUnlockScreen_ = false; + callShowActiveReminder_ = false; + callTerminateAlerting_ = false; + callCloseReminder_ = false; + callSnoozeReminder_ = false; + callHandleCustomButtonClick_ = false; + callClickReminder_ = false; + callOnBundleMgrServiceStart_ = false; + callOnAbilityMgrServiceStart_ = false; + callOnRemoveAppMgr_ = false; + callHandleAutoDeleteReminder_ = false; + callOnLanguageChanged_ = false; +} + std::shared_ptr ReminderDataManager::REMINDER_DATA_MANAGER = nullptr; ReminderDataManager::ReminderDataManager() {} @@ -109,6 +148,7 @@ ErrCode ReminderDataManager::CancelReminder(const int32_t& reminderId, const int ErrCode ReminderDataManager::CancelAllReminders(const std::string& packageName, const int32_t userId, const int32_t uid) { + MockReminderDataManager::callCancelAllReminders_ = true; return g_mockCancelAllRemindersRet; } @@ -137,4 +177,85 @@ int32_t ReminderDataManager::QueryActiveReminderCount() { return g_mockQueryActiveReminderCountRet; } + +void ReminderDataManager::OnProcessDiedLocked(const int32_t callingUid) +{ + MockReminderDataManager::callOnProcessDiedLocked_ = true; +} + +void ReminderDataManager::RefreshRemindersDueToSysTimeChange(uint8_t type) +{ + MockReminderDataManager::callRefreshRemindersDueToSysTimeChange_ = true; +} + +void ReminderDataManager::OnUserSwitch(const int32_t& userId) +{ + MockReminderDataManager::callOnUserSwitch_ = true; +} + +void ReminderDataManager::OnUserRemove(const int32_t& userId) +{ + MockReminderDataManager::callOnUserRemove_ = true; +} + +void ReminderDataManager::OnUnlockScreen() +{ + MockReminderDataManager::callOnUnlockScreen_ = true; +} + +void ReminderDataManager::ShowActiveReminder(const EventFwk::Want& want) +{ + MockReminderDataManager::callShowActiveReminder_ = true; +} + +void ReminderDataManager::TerminateAlerting(const EventFwk::Want& want) +{ + MockReminderDataManager::callTerminateAlerting_ = true; +} + +void ReminderDataManager::CloseReminder(const EventFwk::Want& want, bool cancelNotification, bool isButtonClick) +{ + MockReminderDataManager::callCloseReminder_ = true; +} + +void ReminderDataManager::SnoozeReminder(const EventFwk::Want& want) +{ + MockReminderDataManager::callSnoozeReminder_ = true; +} + +void ReminderDataManager::HandleCustomButtonClick(const EventFwk::Want& want) +{ + MockReminderDataManager::callHandleCustomButtonClick_ = true; +} + +void ReminderDataManager::ClickReminder(const EventFwk::Want& want) +{ + MockReminderDataManager::callClickReminder_ = true; +} + +void ReminderDataManager::OnBundleMgrServiceStart() +{ + MockReminderDataManager::callOnBundleMgrServiceStart_ = true; +} + +void ReminderDataManager::OnAbilityMgrServiceStart() +{ + MockReminderDataManager::callOnAbilityMgrServiceStart_ = true; +} + +void ReminderDataManager::OnRemoveAppMgr() +{ + MockReminderDataManager::callOnRemoveAppMgr_ = true; +} + +void ReminderDataManager::HandleAutoDeleteReminder(const int32_t notificationId, const int32_t uid, + const int64_t autoDeletedTime) +{ + MockReminderDataManager::callHandleAutoDeleteReminder_ = true; +} + +void ReminderDataManager::OnLanguageChanged() +{ + MockReminderDataManager::callOnLanguageChanged_ = true; +} } // namespace OHOS::Notification \ No newline at end of file diff --git a/services/reminder/test/unittest/reminder_agent_service_test.cpp b/services/reminder/test/unittest/reminder_agent_service_test.cpp index d0323d7a15d64b49ab96098c5fdfa7a029ff9a2a..77fa313a028032c6576ca80ec83aab98b10163bc 100644 --- a/services/reminder/test/unittest/reminder_agent_service_test.cpp +++ b/services/reminder/test/unittest/reminder_agent_service_test.cpp @@ -425,12 +425,12 @@ HWTEST_F(ReminderAgentServiceTest, ReminderAgentServiceTest_015, Function | Smal } /** - * @tc.name: ReminderAgentServiceTest_017 + * @tc.name: ReminderAgentServiceTest_016 * @tc.desc: Test UpdateReminder function * @tc.type: FUNC * @tc.require: issueI5S4VP */ -HWTEST_F(ReminderAgentServiceTest, ReminderAgentServiceTest_017, Function | SmallTest | Level1) +HWTEST_F(ReminderAgentServiceTest, ReminderAgentServiceTest_016, Function | SmallTest | Level1) { // test CreateReminderRequest int32_t reminderId = 0; diff --git a/services/reminder/test/unittest/reminder_data_manager_test.cpp b/services/reminder/test/unittest/reminder_data_manager_test.cpp index cf6e80a3473d887c2867a8fa9c6baf13a44a1b93..45e658dcbc542637d73990e8d095300b4ad03704 100644 --- a/services/reminder/test/unittest/reminder_data_manager_test.cpp +++ b/services/reminder/test/unittest/reminder_data_manager_test.cpp @@ -22,7 +22,6 @@ #include "common_event_support.h" #include "matching_skills.h" #include "reminder_data_manager.h" -#include "reminder_event_manager.h" #include "reminder_request_timer.h" #include "reminder_request_alarm.h" #include "reminder_request.h" @@ -31,7 +30,6 @@ #include "ability_manager_client.h" #include "mock_ipc_skeleton.h" #include "reminder_datashare_helper.h" -#include "reminder_config_change_observer.h" #include "reminder_calendar_share_table.h" #include "reminder_timer_info.h" #include "reminder_utils.h" @@ -538,90 +536,6 @@ HWTEST_F(ReminderDataManagerTest, ReminderDataManagerTest_018, Level1) ASSERT_EQ(ringUri, getRingUri); } -/** - * @tc.name: ReminderEventManagerTest_001 - * @tc.desc: Reminder data manager test - * @tc.type: FUNC - * @tc.require: issueI5YTF3 - */ -HWTEST_F(ReminderDataManagerTest, ReminderEventManagerTest_001, Level1) -{ - MatchingSkills matchingSkills; - matchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_ALARM_ALERT); - matchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_ALERT_TIMEOUT); - matchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_CLOSE_ALERT); - matchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_SNOOZE_ALERT); - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED); - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED); - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED); - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_RESTARTED); - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED); - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_TIME_CHANGED); - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED); - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_REMOVED); - CommonEventSubscribeInfo subscriberInfo(matchingSkills); - auto subscriber = std::make_shared(subscriberInfo, manager); - EventFwk::CommonEventData data; - Want want; - want.SetAction(ReminderRequest::REMINDER_EVENT_ALARM_ALERT); - data.SetWant(want); - subscriber->OnReceiveEvent(data); - want.SetAction(ReminderRequest::REMINDER_EVENT_ALERT_TIMEOUT); - data.SetWant(want); - subscriber->OnReceiveEvent(data); - want.SetAction(ReminderRequest::REMINDER_EVENT_CLOSE_ALERT); - data.SetWant(want); - subscriber->OnReceiveEvent(data); - want.SetAction(ReminderRequest::REMINDER_EVENT_SNOOZE_ALERT); - data.SetWant(want); - subscriber->OnReceiveEvent(data); - want.SetAction(ReminderRequest::REMINDER_EVENT_REMOVE_NOTIFICATION); - data.SetWant(want); - subscriber->OnReceiveEvent(data); - want.SetAction(CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED); - data.SetWant(want); - subscriber->OnReceiveEvent(data); - want.SetAction(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED); - data.SetWant(want); - subscriber->OnReceiveEvent(data); - want.SetAction(CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED); - data.SetWant(want); - subscriber->OnReceiveEvent(data); - want.SetAction(CommonEventSupport::COMMON_EVENT_PACKAGE_RESTARTED); - data.SetWant(want); - subscriber->OnReceiveEvent(data); - want.SetAction(CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED); - data.SetWant(want); - subscriber->OnReceiveEvent(data); - want.SetAction(CommonEventSupport::COMMON_EVENT_TIME_CHANGED); - data.SetWant(want); - subscriber->OnReceiveEvent(data); - want.SetAction(CommonEventSupport::COMMON_EVENT_USER_SWITCHED); - data.SetWant(want); - subscriber->OnReceiveEvent(data); - want.SetAction(CommonEventSupport::COMMON_EVENT_USER_REMOVED); - data.SetWant(want); - subscriber->OnReceiveEvent(data); - remove("/data/service/el1/public/notification/notification.db"); - EXPECT_TRUE(manager != nullptr); -} - -/** - * @tc.name: ReminderEventManagerTest_002 - * @tc.desc: Reminder data manager test - * @tc.type: FUNC - * @tc.require: issueI5YTF3 - */ -HWTEST_F(ReminderDataManagerTest, ReminderEventManagerTest_002, Level1) -{ - auto statusChangeListener - = std::make_shared(manager); - statusChangeListener->OnAddSystemAbility(0, ""); - statusChangeListener->OnRemoveSystemAbility(0, ""); - remove("/data/service/el1/public/notification/notification.db"); - EXPECT_TRUE(manager != nullptr); -} - /** * @tc.name: ReminderEventManagerTest_003 * @tc.desc: Reminder data manager test @@ -866,125 +780,6 @@ HWTEST_F(ReminderDataManagerTest, IsMatched_00001, Level1) EXPECT_EQ(ret, true); } -/** - * @tc.name: ReminderEventManager_00001 - * @tc.desc: Reminder data manager test - * @tc.type: FUNC - * @tc.require: issue#I9IIDE - */ -HWTEST_F(ReminderDataManagerTest, ReminderEventManager_001, Level1) -{ - MatchingSkills matchingSkills; - matchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_ALARM_ALERT); - matchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_ALERT_TIMEOUT); - matchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_CLOSE_ALERT); - matchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_SNOOZE_ALERT); - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED); - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED); - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED); - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_RESTARTED); - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED); - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_TIME_CHANGED); - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED); - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_REMOVED); - CommonEventSubscribeInfo subscriberInfo(matchingSkills); - auto subscriber = std::make_shared(subscriberInfo, manager); - - EventFwk::Want want; - want.SetParam(ReminderRequest::PARAM_REMINDER_ID, 0); - AppExecFwk::ElementName element("", "test", "EntryAbility"); - want.SetElement(element); - subscriber->HandlePackageRemove(want); - EXPECT_TRUE(manager != nullptr); -} - -/** - * @tc.name: IsMatched_00001 - * @tc.desc: Reminder data manager test - * @tc.type: FUNC - * @tc.require: issue#I9IIDE - */ -HWTEST_F(ReminderDataManagerTest, ReminderEventManager_002, Level1) -{ - auto statusChangeListener - = std::make_shared(manager); - statusChangeListener->OnAddSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, ""); - statusChangeListener->OnAddSystemAbility(APP_MGR_SERVICE_ID, ""); - statusChangeListener->OnAddSystemAbility(ABILITY_MGR_SERVICE_ID, ""); - statusChangeListener->OnAddSystemAbility(-1, ""); - statusChangeListener->OnRemoveSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, ""); - statusChangeListener->OnRemoveSystemAbility(APP_MGR_SERVICE_ID, ""); - statusChangeListener->OnRemoveSystemAbility(ABILITY_MGR_SERVICE_ID, ""); - statusChangeListener->OnRemoveSystemAbility(-1, ""); - EXPECT_TRUE(manager != nullptr); -} - -/** - * @tc.name: ReminderEventManagerTest_005 - * @tc.desc: Reminder data manager test - * @tc.type: FUNC - * @tc.require: issueI5YTF3 - */ -HWTEST_F(ReminderDataManagerTest, ReminderEventManagerTest_005, Level1) -{ - MatchingSkills customMatchingSkills; - customMatchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_ALARM_ALERT); - customMatchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_ALERT_TIMEOUT); - customMatchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_CLOSE_ALERT); - customMatchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_SNOOZE_ALERT); - customMatchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_REMOVE_NOTIFICATION); - customMatchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_CUSTOM_ALERT); - customMatchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_CLICK_ALERT); - CommonEventSubscribeInfo subscriberInfo(customMatchingSkills); - auto subscriber = std::make_shared(subscriberInfo, manager); - EventFwk::CommonEventData data; - Want want; - want.SetAction(ReminderRequest::REMINDER_EVENT_ALARM_ALERT); - data.SetWant(want); - subscriber->OnReceiveEvent(data); - want.SetAction(ReminderRequest::REMINDER_EVENT_ALERT_TIMEOUT); - data.SetWant(want); - subscriber->OnReceiveEvent(data); - want.SetAction(ReminderRequest::REMINDER_EVENT_CLOSE_ALERT); - data.SetWant(want); - subscriber->OnReceiveEvent(data); - want.SetAction(ReminderRequest::REMINDER_EVENT_SNOOZE_ALERT); - data.SetWant(want); - subscriber->OnReceiveEvent(data); - want.SetAction(ReminderRequest::REMINDER_EVENT_REMOVE_NOTIFICATION); - data.SetWant(want); - subscriber->OnReceiveEvent(data); - want.SetAction(ReminderRequest::REMINDER_EVENT_CUSTOM_ALERT); - data.SetWant(want); - subscriber->OnReceiveEvent(data); - want.SetAction(ReminderRequest::REMINDER_EVENT_CLICK_ALERT); - data.SetWant(want); - subscriber->OnReceiveEvent(data); - EXPECT_TRUE(manager != nullptr); -} - -/** - * @tc.name: ReminderEventManagerTest_006 - * @tc.desc: Reminder data manager test - * @tc.type: FUNC - * @tc.require: issueI5YTF3 - */ -HWTEST_F(ReminderDataManagerTest, ReminderEventManagerTest_006, Level1) -{ - std::shared_ptr data; - ReminderEventManager::ReminderNotificationSubscriber subscriber(data); - subscriber.OnCanceled(nullptr, nullptr, NotificationConstant::PACKAGE_REMOVE_REASON_DELETE); - subscriber.OnCanceled(nullptr, nullptr, NotificationConstant::TRIGGER_AUTO_DELETE_REASON_DELETE); - sptr request = new NotificationRequest(); - std::shared_ptr notification = std::make_shared(request); - subscriber.OnCanceled(notification, nullptr, NotificationConstant::TRIGGER_AUTO_DELETE_REASON_DELETE); - request->SetAutoDeletedTime(100); - subscriber.OnCanceled(notification, nullptr, NotificationConstant::TRIGGER_AUTO_DELETE_REASON_DELETE); - request->SetLabel("REMINDER_AGENT"); - subscriber.OnCanceled(notification, nullptr, NotificationConstant::TRIGGER_AUTO_DELETE_REASON_DELETE); - EXPECT_TRUE(manager != nullptr); -} - /** * @tc.name: ReminderDataManagerTest_020 * @tc.desc: Reminder data manager test diff --git a/services/reminder/test/unittest/reminder_event_manager_test.cpp b/services/reminder/test/unittest/reminder_event_manager_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b03a4dc00f6da7824c2c9d8d812da938d7aa8d6d --- /dev/null +++ b/services/reminder/test/unittest/reminder_event_manager_test.cpp @@ -0,0 +1,370 @@ +/* + * Copyright (c) 2024-2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "reminder_data_manager.h" +#include "reminder_event_manager.h" + +#include "common_event_support.h" +#include "mock_service_registry.h" +#include "mock_notification_helper.h" +#include "mock_reminder_data_manager.h" + +using namespace testing::ext; +namespace OHOS::Notification { +class ReminderEventManagerTest : public testing::Test { +public: + static void SetUpTestCase() {} + static void TearDownTestCase() {} + void SetUp() {} + void TearDown() {} +}; + +/** + * @tc.name: ReminderEventManagerTest_001 + * @tc.desc: test ReminderEventManager::Init function + * @tc.type: FUNC + * @tc.require: issueI5YTF3 + */ +HWTEST_F(ReminderEventManagerTest, ReminderEventManagerTest_001, Level1) +{ + ReminderDataManager::InitInstance(); + auto manager = std::make_shared(); + manager->Init(); + EXPECT_TRUE(manager != nullptr); +} + +/** + * @tc.name: ReminderEventManagerTest_002 + * @tc.desc: test ReminderEventManager::SubscribeEvent function + * @tc.type: FUNC + * @tc.require: issueI5YTF3 + */ +HWTEST_F(ReminderEventManagerTest, ReminderEventManagerTest_002, Level1) +{ + auto manager = std::make_shared(); + MockNotificationHelper::MockSubscribeCommonEvent(false); + manager->SubscribeEvent(); + MockNotificationHelper::MockSubscribeCommonEvent(true); + manager->SubscribeEvent(); + MockNotificationHelper::MockSubscribeNotification(-1); + manager->SubscribeEvent(); + MockNotificationHelper::MockSubscribeNotification(ERR_OK); + manager->SubscribeEvent(); + EXPECT_TRUE(manager != nullptr); +} + +/** + * @tc.name: ReminderEventManagerTest_003 + * @tc.desc: test ReminderEventManager::SubscribeSystemAbility function + * @tc.type: FUNC + * @tc.require: issueI5YTF3 + */ +HWTEST_F(ReminderEventManagerTest, ReminderEventManagerTest_003, Level1) +{ + auto manager = std::make_shared(); + MockServiceRegistry::MockGetSystemAbilityManager(true); + manager->SubscribeSystemAbility(APP_MGR_SERVICE_ID); + MockServiceRegistry::MockGetSystemAbilityManager(false); + manager->SubscribeSystemAbility(APP_MGR_SERVICE_ID); + EXPECT_TRUE(manager != nullptr); +} + +/** + * @tc.name: ReminderEventManagerTest_004 + * @tc.desc: test ReminderEventSubscriber::OnReceiveEvent function + * @tc.type: FUNC + * @tc.require: issueI5YTF3 + */ +HWTEST_F(ReminderEventManagerTest, ReminderEventManagerTest_004, Level1) +{ + MockReminderDataManager::ResetFlag(); + EventFwk::MatchingSkills matchingSkills; + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_RESTARTED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_TIME_CHANGED); + EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills); + auto subscriber = std::make_shared(subscriberInfo); + + EventFwk::CommonEventData data; + EventFwk::Want want; + want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED); + data.SetWant(want); + auto manager = std::move(ReminderDataManager::REMINDER_DATA_MANAGER); + subscriber->OnReceiveEvent(data); + EXPECT_TRUE(MockReminderDataManager::callCancelAllReminders_ == false); + + ReminderDataManager::REMINDER_DATA_MANAGER = std::move(manager); + subscriber->OnReceiveEvent(data); + EXPECT_TRUE(MockReminderDataManager::callCancelAllReminders_ == true); + + want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + EXPECT_TRUE(MockReminderDataManager::callRefreshRemindersDueToSysTimeChange_ == true); + + MockReminderDataManager::ResetFlag(); + want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + EXPECT_TRUE(MockReminderDataManager::callCancelAllReminders_ == true); + + want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_RESTARTED); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + EXPECT_TRUE(MockReminderDataManager::callOnProcessDiedLocked_ == true); + + want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_TIME_CHANGED); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + EXPECT_TRUE(MockReminderDataManager::callRefreshRemindersDueToSysTimeChange_ == true); +} + +/** + * @tc.name: ReminderEventManagerTest_005 + * @tc.desc: test ReminderEventSubscriber::OnReceiveEvent function + * @tc.type: FUNC + * @tc.require: issueI5YTF3 + */ +HWTEST_F(ReminderEventManagerTest, ReminderEventManagerTest_005, Level1) +{ + MockReminderDataManager::ResetFlag(); + EventFwk::MatchingSkills matchingSkills; + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED); + matchingSkills.AddEvent("common.event.UNLOCK_SCREEN"); + EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills); + auto subscriber = std::make_shared(subscriberInfo); + + EventFwk::CommonEventData data; + EventFwk::Want want; + want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + EXPECT_TRUE(MockReminderDataManager::callOnUserSwitch_ == true); + + want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + EXPECT_TRUE(MockReminderDataManager::callOnUserRemove_ == true); + + want.SetAction("common.event.UNLOCK_SCREEN"); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + EXPECT_TRUE(MockReminderDataManager::callOnUnlockScreen_ == true); + + want.SetAction(ReminderRequest::REMINDER_EVENT_ALARM_ALERT); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + EXPECT_TRUE(MockReminderDataManager::callCancelAllReminders_ == false); +} + +/** + * @tc.name: ReminderEventManagerTest_006 + * @tc.desc: test ReminderCustomEventSubscriber::OnReceiveEvent function + * @tc.type: FUNC + * @tc.require: issueI5YTF3 + */ +HWTEST_F(ReminderEventManagerTest, ReminderEventManagerTest_006, Level1) +{ + MockReminderDataManager::ResetFlag(); + EventFwk::MatchingSkills matchingSkills; + matchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_ALARM_ALERT); + matchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_ALERT_TIMEOUT); + matchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_CLOSE_ALERT); + matchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_SNOOZE_ALERT); + EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills); + auto subscriber = std::make_shared(subscriberInfo); + + EventFwk::CommonEventData data; + EventFwk::Want want; + want.SetAction(ReminderRequest::REMINDER_EVENT_ALARM_ALERT); + data.SetWant(want); + auto manager = std::move(ReminderDataManager::REMINDER_DATA_MANAGER); + subscriber->OnReceiveEvent(data); + EXPECT_TRUE(MockReminderDataManager::callShowActiveReminder_ == false); + + ReminderDataManager::REMINDER_DATA_MANAGER = std::move(manager); + subscriber->OnReceiveEvent(data); + EXPECT_TRUE(MockReminderDataManager::callShowActiveReminder_ == true); + + want.SetAction(ReminderRequest::REMINDER_EVENT_ALERT_TIMEOUT); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + EXPECT_TRUE(MockReminderDataManager::callTerminateAlerting_ == true); + + want.SetAction(ReminderRequest::REMINDER_EVENT_CLOSE_ALERT); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + EXPECT_TRUE(MockReminderDataManager::callCloseReminder_ == true); + + want.SetAction(ReminderRequest::REMINDER_EVENT_SNOOZE_ALERT); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + EXPECT_TRUE(MockReminderDataManager::callSnoozeReminder_ == true); +} + +/** + * @tc.name: ReminderEventManagerTest_007 + * @tc.desc: test ReminderCustomEventSubscriber::OnReceiveEvent function + * @tc.type: FUNC + * @tc.require: issueI5YTF3 + */ +HWTEST_F(ReminderEventManagerTest, ReminderEventManagerTest_007, Level1) +{ + MockReminderDataManager::ResetFlag(); + EventFwk::MatchingSkills matchingSkills; + matchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_CUSTOM_ALERT); + matchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_REMOVE_NOTIFICATION); + matchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_CLICK_ALERT); + EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills); + auto subscriber = std::make_shared(subscriberInfo); + + EventFwk::CommonEventData data; + EventFwk::Want want; + want.SetAction(ReminderRequest::REMINDER_EVENT_CUSTOM_ALERT); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + EXPECT_TRUE(MockReminderDataManager::callHandleCustomButtonClick_ == false); + + want.SetAction(ReminderRequest::REMINDER_EVENT_REMOVE_NOTIFICATION); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + EXPECT_TRUE(MockReminderDataManager::callCloseReminder_ == true); + + want.SetAction(ReminderRequest::REMINDER_EVENT_CLICK_ALERT); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + EXPECT_TRUE(MockReminderDataManager::callClickReminder_ == true); + + want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + EXPECT_TRUE(MockReminderDataManager::callShowActiveReminder_ == false); +} + +/** + * @tc.name: ReminderEventManagerTest_008 + * @tc.desc: test SystemAbilityStatusChangeListener + * @tc.type: FUNC + * @tc.require: issue#I9IIDE + */ +HWTEST_F(ReminderEventManagerTest, ReminderEventManagerTest_008, Level1) +{ + MockReminderDataManager::ResetFlag(); + auto listener = std::make_shared(); + // test OnAddSystemAbility + auto manager = std::move(ReminderDataManager::REMINDER_DATA_MANAGER); + listener->OnAddSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, ""); + EXPECT_TRUE(MockReminderDataManager::callOnBundleMgrServiceStart_ == false); + + ReminderDataManager::REMINDER_DATA_MANAGER = std::move(manager); + listener->OnAddSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, ""); + EXPECT_TRUE(MockReminderDataManager::callOnBundleMgrServiceStart_ == true); + + listener->OnAddSystemAbility(APP_MGR_SERVICE_ID, ""); + listener->OnAddSystemAbility(ABILITY_MGR_SERVICE_ID, ""); + listener->OnAddSystemAbility(-1, ""); + EXPECT_TRUE(MockReminderDataManager::callOnAbilityMgrServiceStart_ == true); + + // test OnRemoveSystemAbility + manager = std::move(ReminderDataManager::REMINDER_DATA_MANAGER); + listener->OnRemoveSystemAbility(APP_MGR_SERVICE_ID, ""); + EXPECT_TRUE(MockReminderDataManager::callOnRemoveAppMgr_ == false); + + ReminderDataManager::REMINDER_DATA_MANAGER = std::move(manager); + listener->OnRemoveSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, ""); + listener->OnRemoveSystemAbility(ABILITY_MGR_SERVICE_ID, ""); + listener->OnRemoveSystemAbility(APP_MGR_SERVICE_ID, ""); + listener->OnRemoveSystemAbility(-1, ""); + EXPECT_TRUE(MockReminderDataManager::callOnRemoveAppMgr_ == true); +} + +/** + * @tc.name: ReminderEventManagerTest_009 + * @tc.desc: test ReminderNotificationSubscriber::OnCanceled test + * @tc.type: FUNC + * @tc.require: issueI5YTF3 + */ +HWTEST_F(ReminderEventManagerTest, ReminderEventManagerTest_009, Level1) +{ + MockReminderDataManager::ResetFlag(); + ReminderEventManager::ReminderNotificationSubscriber subscriber; + // test deleteReason + subscriber.OnCanceled(nullptr, nullptr, NotificationConstant::PACKAGE_REMOVE_REASON_DELETE); + EXPECT_TRUE(MockReminderDataManager::callHandleAutoDeleteReminder_ == false); + + // test notification + subscriber.OnCanceled(nullptr, nullptr, NotificationConstant::TRIGGER_AUTO_DELETE_REASON_DELETE); + EXPECT_TRUE(MockReminderDataManager::callHandleAutoDeleteReminder_ == false); + + // test autoDeletedTime + sptr request = new NotificationRequest(); + std::shared_ptr notification = std::make_shared(request); + subscriber.OnCanceled(notification, nullptr, NotificationConstant::TRIGGER_AUTO_DELETE_REASON_DELETE); + EXPECT_TRUE(MockReminderDataManager::callHandleAutoDeleteReminder_ == false); + + request->SetAutoDeletedTime(100); + // test label + subscriber.OnCanceled(notification, nullptr, NotificationConstant::TRIGGER_AUTO_DELETE_REASON_DELETE); + EXPECT_TRUE(MockReminderDataManager::callHandleAutoDeleteReminder_ == false); + + request->SetLabel("REMINDER_AGENT"); + // test manager + auto manager = std::move(ReminderDataManager::REMINDER_DATA_MANAGER); + subscriber.OnCanceled(notification, nullptr, NotificationConstant::TRIGGER_AUTO_DELETE_REASON_DELETE); + EXPECT_TRUE(MockReminderDataManager::callHandleAutoDeleteReminder_ == false); + + ReminderDataManager::REMINDER_DATA_MANAGER = std::move(manager); + subscriber.OnCanceled(notification, nullptr, NotificationConstant::TRIGGER_AUTO_DELETE_REASON_DELETE); + EXPECT_TRUE(MockReminderDataManager::callHandleAutoDeleteReminder_ == true); +} + +/** + * @tc.name: ReminderEventManagerTest_010 + * @tc.desc: test ReminderConfigChangeObserver::OnConfigurationUpdated function + * @tc.type: FUNC + * @tc.require: issueI5YTF3 + */ +HWTEST_F(ReminderEventManagerTest, ReminderEventManagerTest_010, Level1) +{ + MockReminderDataManager::ResetFlag(); + ReminderConfigChangeObserver observer; + AppExecFwk::Configuration config; + // test manager == nullptr + auto manager = std::move(ReminderDataManager::REMINDER_DATA_MANAGER); + observer.OnConfigurationUpdated(config); + EXPECT_TRUE(MockReminderDataManager::callOnLanguageChanged_ == false); + + ReminderDataManager::REMINDER_DATA_MANAGER = std::move(manager); + // test languageInfo + config.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE, ""); + observer.OnConfigurationUpdated(config); + EXPECT_TRUE(MockReminderDataManager::callOnLanguageChanged_ == false); + + config.RemoveItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE); + config.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE, "test"); + observer.languageInfo_ = "test"; + observer.OnConfigurationUpdated(config); + EXPECT_TRUE(MockReminderDataManager::callOnLanguageChanged_ == false); + + observer.languageInfo_ = "1111"; + observer.OnConfigurationUpdated(config); + EXPECT_TRUE(MockReminderDataManager::callOnLanguageChanged_ == true); +} +} // namespace OHOS::Notification \ No newline at end of file