From 82c14b31bae49ebc62d90056929abe00b95cb1be Mon Sep 17 00:00:00 2001 From: zhengzhuolan Date: Mon, 30 Jun 2025 20:51:53 +0800 Subject: [PATCH] Add code Signed-off-by: zhengzhuolan --- frameworks/ans/IAnsManager.idl | 2 + frameworks/ans/src/notification_helper.cpp | 5 + .../unittest/notification_helper_test.cpp | 13 ++ frameworks/core/include/ans_notification.h | 8 + frameworks/core/src/ans_notification.cpp | 10 + .../ans_notification_branch_test.cpp | 5 + .../ans_notification_unit_test.cpp | 24 +++ .../unittest/mock/mock_ans_manager_proxy.h | 1 + frameworks/js/napi/include/distributed.h | 11 + .../napi/include/manager/napi_distributed.h | 2 + frameworks/js/napi/src/distributed.cpp | 86 ++++++++ .../js/napi/src/manager/init_module.cpp | 4 + .../js/napi/src/manager/napi_distributed.cpp | 160 +++++++++++++- interfaces/inner_api/notification_helper.h | 8 + .../include/advanced_notification_service.h | 8 + .../ans/include/notification_preferences.h | 17 ++ .../notification_preferences_database.h | 17 ++ ...tification_distributed_manager_service.cpp | 25 ++- services/ans/src/notification_preferences.cpp | 42 ++++ .../src/notification_preferences_database.cpp | 44 ++++ services/ans/test/unittest/BUILD.gn | 32 +++ ...ation_distributed_manager_service_test.cpp | 48 +++++ ...notification_preferences_database_test.cpp | 81 ++++++++ .../notification_preferences_second_test.cpp | 196 ++++++++++++++++++ tools/test/mock/mock_ans_manager_stub.h | 5 + 25 files changed, 850 insertions(+), 4 deletions(-) create mode 100644 services/ans/test/unittest/notification_preferences_second_test.cpp diff --git a/frameworks/ans/IAnsManager.idl b/frameworks/ans/IAnsManager.idl index 88f585c97..9d447d582 100644 --- a/frameworks/ans/IAnsManager.idl +++ b/frameworks/ans/IAnsManager.idl @@ -182,6 +182,8 @@ interface OHOS.Notification.IAnsManager { void IsDistributedEnabled([out] boolean enabled); + void GetDistributedDevicelist([out] String[] deviceTypes); + void SetDistributedEnabledBySlot( [in] int slotTypeInt, [in] String deviceType, [in] boolean enabled); diff --git a/frameworks/ans/src/notification_helper.cpp b/frameworks/ans/src/notification_helper.cpp index c88bf75d4..6f548000e 100644 --- a/frameworks/ans/src/notification_helper.cpp +++ b/frameworks/ans/src/notification_helper.cpp @@ -747,5 +747,10 @@ ErrCode NotificationHelper::SetHashCodeRule(const uint32_t type) { return DelayedSingleton::GetInstance()->SetHashCodeRule(type); } + +ErrCode NotificationHelper::GetDistributedDevicelist(std::vector &deviceTypes) +{ + return DelayedSingleton::GetInstance()->GetDistributedDevicelist(deviceTypes); +} } // namespace Notification } // namespace OHOS diff --git a/frameworks/ans/test/unittest/notification_helper_test.cpp b/frameworks/ans/test/unittest/notification_helper_test.cpp index d3c25258c..4d998ec1f 100644 --- a/frameworks/ans/test/unittest/notification_helper_test.cpp +++ b/frameworks/ans/test/unittest/notification_helper_test.cpp @@ -1363,5 +1363,18 @@ HWTEST_F(NotificationHelperTest, IsSilentReminderEnabled_00001, Function | Small ErrCode ret = notificationHelper.IsSilentReminderEnabled(bo, enableStatus); EXPECT_EQ(ret, ERR_ANS_INVALID_PARAM); } + +/** + * @tc.name: GetDistributedDevicelist_0100 + * @tc.desc: Test GetDistributedDevicelist. + * @tc.type: FUNC + */ +HWTEST_F(NotificationHelperTest, GetDistributedDevicelist_0100, Function | SmallTest | Level1) +{ + std::vector deviceTypes; + NotificationHelper notificationHelper; + ErrCode ret = notificationHelper.GetDistributedDevicelist(deviceTypes); + EXPECT_EQ(ret, ERR_OK); +} } } diff --git a/frameworks/core/include/ans_notification.h b/frameworks/core/include/ans_notification.h index b5d177b43..a57ccb8e4 100644 --- a/frameworks/core/include/ans_notification.h +++ b/frameworks/core/include/ans_notification.h @@ -1377,6 +1377,14 @@ public: */ ErrCode SetHashCodeRule(const uint32_t type); + /** + * @brief get distributed device list. + * + * @param deviceTypes Indicates device types. + * @return Returns ERR_OK on success, others on failure. + */ + ErrCode GetDistributedDevicelist(std::vector &deviceTypes); + private: /** * @brief Gets Ans Manager proxy. diff --git a/frameworks/core/src/ans_notification.cpp b/frameworks/core/src/ans_notification.cpp index 2cec6965f..7eda75b94 100644 --- a/frameworks/core/src/ans_notification.cpp +++ b/frameworks/core/src/ans_notification.cpp @@ -2485,5 +2485,15 @@ ErrCode AnsNotification::GetAllNotificationsBySlotType(std::vectorGetAllNotificationsBySlotType(notifications, slotType); } + +ErrCode AnsNotification::GetDistributedDevicelist(std::vector &deviceTypes) +{ + sptr proxy = GetAnsManagerProxy(); + if (!proxy) { + ANS_LOGE("GetAnsManagerProxy fail."); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + return proxy->GetDistributedDevicelist(deviceTypes); +} } // namespace Notification } // namespace OHOS diff --git a/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp b/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp index 10fd8864d..f0a4de4b7 100644 --- a/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp +++ b/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp @@ -456,6 +456,11 @@ public: return ERR_ANS_INVALID_PARAM; } + ErrCode GetDistributedDevicelist(std::vector &deviceTypes) override + { + return ERR_ANS_INVALID_PARAM; + } + ErrCode GetDeviceRemindType(int32_t& remindTypeInt) override { return ERR_ANS_INVALID_PARAM; diff --git a/frameworks/core/test/unittest/ans_notification_test/ans_notification_unit_test.cpp b/frameworks/core/test/unittest/ans_notification_test/ans_notification_unit_test.cpp index e0e86f6cb..2be6089ea 100644 --- a/frameworks/core/test/unittest/ans_notification_test/ans_notification_unit_test.cpp +++ b/frameworks/core/test/unittest/ans_notification_test/ans_notification_unit_test.cpp @@ -1768,6 +1768,18 @@ HWTEST_F(AnsNotificationUnitTest, CancelAsBundle_0100, Function | MediumTest | L EXPECT_EQ(res, ERR_ANS_SERVICE_NOT_CONNECTED); } +/* + * @tc.name: GetDistributedDevicelist_0100 + * @tc.desc: test GetDistributedDevicelist. + * @tc.type: FUNC + */ +HWTEST_F(AnsNotificationUnitTest, GetDistributedDevicelist_0100, Function | MediumTest | Level1) +{ + std::vector deviceTypes; + ErrCode ret = ans_->GetDistributedDevicelist(deviceTypes); + EXPECT_EQ(ret, ERR_ANS_SERVICE_NOT_CONNECTED); +} + /* * @tc.name: GetNotificationSettings_0100 * @tc.desc: test GetNotificationSetting. @@ -2320,5 +2332,17 @@ HWTEST_F(AnsNotificationUnitTest, UnSubscribeNotification_0800, Function | Mediu ErrCode ret = ans_->UnSubscribeNotification(subscriberPtr, nullptr); EXPECT_EQ(ret, ERR_ANS_INVALID_PARAM); } + +/* + * @tc.name: GetDistributedDevicelist_0200 + * @tc.desc: test GetDistributedDevicelist. + * @tc.type: FUNC + */ +HWTEST_F(AnsNotificationUnitTest, GetDistributedDevicelist_0200, Function | MediumTest | Level1) +{ + std::vector deviceTypes; + ErrCode ret = ans_->GetDistributedDevicelist(deviceTypes); + EXPECT_EQ(ret, ERR_OK); +} } // namespace Notification } // namespace OHOS diff --git a/frameworks/core/test/unittest/mock/mock_ans_manager_proxy.h b/frameworks/core/test/unittest/mock/mock_ans_manager_proxy.h index c435ba9a4..9dc390c8e 100644 --- a/frameworks/core/test/unittest/mock/mock_ans_manager_proxy.h +++ b/frameworks/core/test/unittest/mock/mock_ans_manager_proxy.h @@ -188,6 +188,7 @@ public: const bool enabled)); MOCK_METHOD2(IsSilentReminderEnabled, ErrCode(const sptr &bundleOption, int32_t &enableStatusInt)); + MOCK_METHOD1(GetDistributedDevicelist, ErrCode(std::vector& deviceList)); #ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED MOCK_METHOD1(RegisterSwingCallback, ErrCode(const sptr&)); #endif diff --git a/frameworks/js/napi/include/distributed.h b/frameworks/js/napi/include/distributed.h index b6894c0fc..2fb2ec1d9 100644 --- a/frameworks/js/napi/include/distributed.h +++ b/frameworks/js/napi/include/distributed.h @@ -28,11 +28,13 @@ struct AsyncCallbackInfoIsEnabled { napi_async_work asyncWork = nullptr; CallbackPromiseInfo info; bool enable = false; + std::string deviceType; }; struct EnabledParams { napi_ref callback = nullptr; bool enable = false; + std::string deviceType; }; struct AsyncCallbackInfoEnabled { @@ -116,6 +118,13 @@ struct AsynDeviceStatusConfig { CallbackPromiseInfo info; }; +struct AsynCallbackInfoGetDistributedDeviceList { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + CallbackPromiseInfo info; + std::vector deviceList; +}; + napi_value IsDistributedEnabled(napi_env env, napi_callback_info info); napi_value EnableDistributed(napi_env env, napi_callback_info info); napi_value EnableDistributedByBundle(napi_env env, napi_callback_info info); @@ -131,6 +140,8 @@ napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, IsEnabledByBundleParams ¶ms); napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, EnabledWithoutAppParams ¶ms); napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, GetEnabledWithoutAppParams ¶ms); +napi_value ParseSetDistributedEnabledParams(const napi_env &env, const napi_callback_info &info, EnabledParams ¶ms); +napi_value ParseIsDistributedEnabledParams(const napi_env &env, const napi_callback_info &info, EnabledParams ¶ms); } // namespace NotificationNapi } // namespace OHOS diff --git a/frameworks/js/napi/include/manager/napi_distributed.h b/frameworks/js/napi/include/manager/napi_distributed.h index 0395dcc93..4f077dfc0 100644 --- a/frameworks/js/napi/include/manager/napi_distributed.h +++ b/frameworks/js/napi/include/manager/napi_distributed.h @@ -31,6 +31,8 @@ napi_value NapiGetDeviceRemindType(napi_env env, napi_callback_info info); napi_value NapiSetSyncNotificationEnabledWithoutApp(napi_env env, napi_callback_info info); napi_value NapiGetSyncNotificationEnabledWithoutApp(napi_env env, napi_callback_info info); napi_value NapiSetTargetDeviceStatus(napi_env env, napi_callback_info info); +napi_value NapiSetDistributedEnabled(napi_env env, napi_callback_info info); +napi_value NapiGetDistributedDeviceList(napi_env env, napi_callback_info info); } // namespace NotificationNapi } // namespace OHOS diff --git a/frameworks/js/napi/src/distributed.cpp b/frameworks/js/napi/src/distributed.cpp index 359298665..96618180c 100644 --- a/frameworks/js/napi/src/distributed.cpp +++ b/frameworks/js/napi/src/distributed.cpp @@ -20,6 +20,9 @@ namespace OHOS { namespace NotificationNapi { const int ENABLED_MAX_PARA = 2; const int ENABLED_MIN_PARA = 1; +const int ARGC_ZERO = 0; +const int ARGC_ONE = 1; +const int ARGC_TWO = 2; const int ENABLED_BUNDLE_MAX_PARA = 3; const int ENABLED_BUNDLE_MIN_PARA = 2; const int IS_ENABLED_BUNDLE_MAX_PARA = 2; @@ -121,6 +124,89 @@ napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, return Common::NapiGetNull(env); } +napi_value ParseSetDistributedEnabledParams(const napi_env &env, const napi_callback_info &info, EnabledParams ¶ms) +{ + ANS_LOGD("called"); + size_t argc = ARGC_TWO; + napi_value argv[ARGC_TWO] = { nullptr }; + napi_value thisVar = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); + if (argc < ARGC_TWO) { + ANS_LOGE("Wrong number of arguments."); + Common::NapiThrow(env, ERROR_PARAM_INVALID, MANDATORY_PARAMETER_ARE_LEFT_UNSPECIFIED); + return nullptr; + } + napi_valuetype valuetype = napi_undefined; + + // argv[0]: enable + NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype)); + if (valuetype != napi_boolean) { + ANS_LOGE("Wrong argument type. Bool expected."); + std::string msg = "Incorrect parameter types.The type of enable must be boolean."; + Common::NapiThrow(env, ERROR_PARAM_INVALID, msg); + return nullptr; + } + napi_get_value_bool(env, argv[PARAM0], ¶ms.enable); + + // argv[1]: deviceType + NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype)); + if (valuetype != napi_string) { + ANS_LOGE("Wrong argument type. String expected."); + std::string msg = "Incorrect parameter types.The type of deviceType must be string."; + Common::NapiThrow(env, ERROR_PARAM_INVALID, msg); + return nullptr; + } + + char str[STR_MAX_SIZE] = { 0 }; + size_t strLen = 0; + NAPI_CALL(env, napi_get_value_string_utf8(env, argv[PARAM1], str, STR_MAX_SIZE - 1, &strLen)); + if (std::strlen(str) == 0) { + ANS_LOGE("Property deviceType is empty"); + Common::NapiThrow(env, ERROR_PARAM_INVALID, MANDATORY_PARAMETER_ARE_LEFT_UNSPECIFIED); + return nullptr; + } + params.deviceType = str; + return Common::NapiGetNull(env); +} + +napi_value ParseIsDistributedEnabledParams(const napi_env &env, const napi_callback_info &info, EnabledParams ¶ms) +{ + ANS_LOGD("called"); + size_t argc = ARGC_ONE; + napi_value argv[ARGC_ONE] = { nullptr }; + napi_value thisVar = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); + if (argc < ARGC_ZERO) { + ANS_LOGE("Wrong number of arguments"); + Common::NapiThrow(env, ERROR_PARAM_INVALID, MANDATORY_PARAMETER_ARE_LEFT_UNSPECIFIED); + return nullptr; + } + + // argv[0]: callback or deviceType + napi_valuetype valuetype = napi_undefined; + if (argc >= ARGC_ONE) { + NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype)); + if (valuetype == napi_function) { + napi_create_reference(env, argv[PARAM0], 1, ¶ms.callback); + } else if (valuetype == napi_string) { + char str[STR_MAX_SIZE] = { 0 }; + size_t strLen = 0; + NAPI_CALL(env, napi_get_value_string_utf8(env, argv[PARAM0], str, STR_MAX_SIZE - 1, &strLen)); + if (std::strlen(str) == 0) { + ANS_LOGE("Property deviceType is empty"); + Common::NapiThrow(env, ERROR_PARAM_INVALID, MANDATORY_PARAMETER_ARE_LEFT_UNSPECIFIED); + return nullptr; + } + params.deviceType = str; + } else { + ANS_LOGE("Wrong argument type. Callback or String expected."); + Common::NapiThrow(env, ERROR_PARAM_INVALID, INCORRECT_PARAMETER_TYPES); + return nullptr; + } + } + return Common::NapiGetNull(env); +} + napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, EnabledByBundleParams ¶ms) { ANS_LOGD("called"); diff --git a/frameworks/js/napi/src/manager/init_module.cpp b/frameworks/js/napi/src/manager/init_module.cpp index 0912bc380..60275785c 100644 --- a/frameworks/js/napi/src/manager/init_module.cpp +++ b/frameworks/js/napi/src/manager/init_module.cpp @@ -127,6 +127,8 @@ napi_value NotificationManagerInit(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("getDeviceRemindType", NapiGetDeviceRemindType), DECLARE_NAPI_FUNCTION("setSyncNotificationEnabledWithoutApp", NapiSetSyncNotificationEnabledWithoutApp), DECLARE_NAPI_FUNCTION("isSmartReminderEnabled", NapiIsSmartReminderEnabled), + DECLARE_NAPI_FUNCTION("setDistributedEnabled", NapiSetDistributedEnabled), + DECLARE_NAPI_FUNCTION("getDistributedDeviceList", NapiGetDistributedDeviceList), #else DECLARE_NAPI_FUNCTION("isDistributedEnabled", Common::NapiReturnFalseCbNewType), DECLARE_NAPI_FUNCTION("setDistributedEnable", Common::NapiReturnCapErrCb), @@ -138,6 +140,8 @@ napi_value NotificationManagerInit(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("getDeviceRemindType", Common::NapiReturnCapErrCb), DECLARE_NAPI_FUNCTION("setSyncNotificationEnabledWithoutApp", Common::NapiReturnCapErrCb), DECLARE_NAPI_FUNCTION("isSmartReminderEnabled", Common::NapiReturnCapErr), + DECLARE_NAPI_FUNCTION("setDistributedEnabled", Common::NapiReturnCapErr), + DECLARE_NAPI_FUNCTION("getDistributedDeviceList", Common::NapiReturnCapErr), #endif #ifdef ANS_FEATURE_DISTURB_MANAGER diff --git a/frameworks/js/napi/src/manager/napi_distributed.cpp b/frameworks/js/napi/src/manager/napi_distributed.cpp index c771a73bf..bbffb1bf8 100644 --- a/frameworks/js/napi/src/manager/napi_distributed.cpp +++ b/frameworks/js/napi/src/manager/napi_distributed.cpp @@ -47,16 +47,51 @@ void AsyncCompleteCallbackNapiIsDistributedEnabled(napi_env env, napi_status sta } } +napi_value DoIsDistributedEnabledWithDeviceType(napi_env env, napi_callback_info info, EnabledParams ¶ms) +{ + auto asynccallbackinfo = new (std::nothrow) + AsyncCallbackInfoIsEnabled{ .env = env, .asyncWork = nullptr, .deviceType = params.deviceType }; + if (!asynccallbackinfo) { + return Common::JSParaError(env, nullptr); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, nullptr, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "isDistributedEnabled", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work( + env, nullptr, resourceName, + [](napi_env env, void *data) { + ANS_LOGD("NapiIsDistributedEnabled work excute."); + AsyncCallbackInfoIsEnabled *asynccallbackinfo = static_cast(data); + + if (asynccallbackinfo) { + asynccallbackinfo->info.errorCode = + NotificationHelper::IsDistributedEnabled(asynccallbackinfo->deviceType, asynccallbackinfo->enable); + ANS_LOGI("IsDistributedEnabled enable = %{public}d", asynccallbackinfo->enable); + } + }, + AsyncCompleteCallbackNapiIsDistributedEnabled, (void *)asynccallbackinfo, &asynccallbackinfo->asyncWork); + + napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated); + return promise; +} + napi_value NapiIsDistributedEnabled(napi_env env, napi_callback_info info) { ANS_LOGD("called"); - - napi_ref callback = nullptr; - if (Common::ParseParaOnlyCallback(env, info, callback) == nullptr) { + EnabledParams params{}; + if (ParseIsDistributedEnabledParams(env, info, params) == nullptr) { Common::NapiThrow(env, ERROR_PARAM_INVALID); return Common::NapiGetUndefined(env); } + if (!params.deviceType.empty()) { + return DoIsDistributedEnabledWithDeviceType(env, info, params); + } + + napi_ref callback = params.callback; auto asynccallbackinfo = new (std::nothrow) AsyncCallbackInfoIsEnabled {.env = env, .asyncWork = nullptr}; if (!asynccallbackinfo) { return Common::JSParaError(env, callback); @@ -162,6 +197,58 @@ napi_value NapiEnableDistributed(napi_env env, napi_callback_info info) } } +napi_value NapiSetDistributedEnabled(napi_env env, napi_callback_info info) +{ + ANS_LOGD("called"); + EnabledParams params{}; + if (ParseSetDistributedEnabledParams(env, info, params) == nullptr) { + ANS_LOGD("null ParseSetDistributedEnabledParams"); + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + AsyncCallbackInfoEnabled *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoEnabled{ .env = env, .asyncWork = nullptr, .params = params }; + if (!asynccallbackinfo) { + ANS_LOGD("Create asyncCallbackinfo fail."); + Common::NapiThrow(env, ERROR_INTERNAL_ERROR); + return Common::JSParaError(env, nullptr); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, nullptr, asynccallbackinfo->info, promise); + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "setDistributedEnabled", NAPI_AUTO_LENGTH, &resourceName); + // Async function call + napi_create_async_work( + env, nullptr, resourceName, + [](napi_env env, void *data) { + ANS_LOGD("NapiSetDistributedEnabled work excute."); + AsyncCallbackInfoEnabled *asynccallbackinfo = static_cast(data); + if (asynccallbackinfo) { + asynccallbackinfo->info.errorCode = NotificationHelper::SetDistributedEnabled( + asynccallbackinfo->params.deviceType, asynccallbackinfo->params.enable); + ANS_LOGD("errorCode = %{public}d", asynccallbackinfo->info.errorCode); + } + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGD("NapiSetDistributedEnabled work complete."); + AsyncCallbackInfoEnabled *asynccallbackinfo = static_cast(data); + if (asynccallbackinfo) { + Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env)); + if (asynccallbackinfo->info.callback != nullptr) { + ANS_LOGD("Delete napiSetDistributedEnabled callback reference."); + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + ANS_LOGD("NapiSetDistributedEnabled work complete end."); + }, + (void *)asynccallbackinfo, &asynccallbackinfo->asyncWork); + napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated); + return promise; +} + napi_value NapiEnableDistributedByBundle(napi_env env, napi_callback_info info) { ANS_LOGD("called"); @@ -641,5 +728,72 @@ napi_value NapiSetTargetDeviceStatus(napi_env env, napi_callback_info info) return promise; } } + +void AsyncCompleteCallbackNapiGetDistributedDeviceList(napi_env env, napi_status status, void *data) +{ + ANS_LOGD("called"); + if (!data) { + ANS_LOGE("null data"); + return; + } + napi_value result = nullptr; + auto asynccallbackinfo = reinterpret_cast(data); + if (asynccallbackinfo) { + ANS_LOGD("Asynccallbackinfo conversion is success."); + if (asynccallbackinfo->info.errorCode != ERR_OK) { + result = Common::NapiGetNull(env); + } else { + napi_value arr = nullptr; + napi_create_array(env, &arr); + size_t count = 0; + for (auto vec : asynccallbackinfo->deviceList) { + napi_value vecValue = nullptr; + ANS_LOGI("deviceType = %{public}s", vec.c_str()); + napi_create_string_utf8(env, vec.c_str(), NAPI_AUTO_LENGTH, &vecValue); + napi_set_element(env, arr, count, vecValue); + count++; + } + result = arr; + } + Common::CreateReturnValue(env, asynccallbackinfo->info, result); + if (asynccallbackinfo->info.callback != nullptr) { + ANS_LOGD("Delete napiGetDistributedDeviceList callback reference."); + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } +} + +napi_value NapiGetDistributedDeviceList(napi_env env, napi_callback_info info) +{ + ANS_LOGD("called"); + AsynCallbackInfoGetDistributedDeviceList *asynccallbackinfo = + new (std::nothrow) AsynCallbackInfoGetDistributedDeviceList{ .env = env, .asyncWork = nullptr }; + if (!asynccallbackinfo) { + Common::NapiThrow(env, ERROR_INTERNAL_ERROR); + return Common::JSParaError(env, nullptr); + } + + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, nullptr, asynccallbackinfo->info, promise); + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "getDistributedDeviceList", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work( + env, nullptr, resourceName, + [](napi_env env, void *data) { + ANS_LOGD("NapiGetDistributedDeviceList work excute."); + auto asynccallbackinfo = reinterpret_cast(data); + if (asynccallbackinfo) { + asynccallbackinfo->info.errorCode = + NotificationHelper::GetDistributedDevicelist(asynccallbackinfo->deviceList); + } + }, + AsyncCompleteCallbackNapiGetDistributedDeviceList, (void *)asynccallbackinfo, &asynccallbackinfo->asyncWork); + napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated); + return promise; +} } // namespace NotificationNapi } // namespace OHOS diff --git a/interfaces/inner_api/notification_helper.h b/interfaces/inner_api/notification_helper.h index 2b7cba8ff..2034bf837 100644 --- a/interfaces/inner_api/notification_helper.h +++ b/interfaces/inner_api/notification_helper.h @@ -1362,6 +1362,14 @@ public: * @return Returns ERR_OK on success, others on failure. */ static ErrCode SetHashCodeRule(const uint32_t type); + + /** + * @brief get distributed device list. + * + * @param deviceTypes Indicates device types. + * @return Returns ERR_OK on success, others on failure. + */ + static ErrCode GetDistributedDevicelist(std::vector &deviceTypes); }; } // namespace Notification } // namespace OHOS diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index 4e143b977..db430a161 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -1134,6 +1134,14 @@ public: ErrCode SetDistributedAuthStatus( const std::string &deviceType, const std::string &deviceId, int32_t userId, bool isAuth) override; + /** + * @brief get distributed device list. + * + * @param deviceTypes Indicates device types. + * @return Returns ERR_OK on success, others on failure. + */ + ErrCode GetDistributedDevicelist(std::vector &deviceTypes) override; + /** * @brief Get Enable smartphone to collaborate with other devices for intelligent reminders * diff --git a/services/ans/include/notification_preferences.h b/services/ans/include/notification_preferences.h index 783f66a13..107e1c7c1 100644 --- a/services/ans/include/notification_preferences.h +++ b/services/ans/include/notification_preferences.h @@ -535,6 +535,23 @@ public: bool GetkioskAppTrustList(std::vector &kioskAppTrustList); + /** + * @brief Set distributed device list. + * + * @param deviceTypes Indicates device types. + * @param userId Indicates userId + * @return Returns ERR_OK on success, others on failure. + */ + ErrCode SetDistributedDevicelist(std::vector &deviceTypes, const int32_t &userId); + + /** + * @brief Get distributed device list. + * + * @param deviceTypes Indicates device types. + * @return Returns ERR_OK on success, others on failure. + */ + ErrCode GetDistributedDevicelist(std::vector &deviceTypes); + private: bool GetBundleInfo(NotificationPreferencesInfo &preferencesInfo, const sptr &bundleOption, NotificationPreferencesInfo::BundleInfo &info) const; diff --git a/services/ans/include/notification_preferences_database.h b/services/ans/include/notification_preferences_database.h index 42cf20fd1..9f3b5b23e 100644 --- a/services/ans/include/notification_preferences_database.h +++ b/services/ans/include/notification_preferences_database.h @@ -392,6 +392,23 @@ public: void ParseBundleFromDistureDB(NotificationPreferencesInfo &info, const std::unordered_map &entries, const int32_t &userId); + /** + * @brief Put distributed device list into disturbe DB. + * + * @param deviceTypes Indicates device types. + * @param userId Indicates userId + * @return Return true on success, false on failure. + */ + bool PutDistributedDevicelist(const std::string &deviceTypes, const int32_t &userId); + + /** + * @brief get distributed device list from disturbe DB. + * + * @param deviceTypes Indicates device types. + * @return Return true on success, false on failure. + */ + bool GetDistributedDevicelist(std::string &deviceTypes); + private: bool CheckRdbStore(); diff --git a/services/ans/src/distributed_manager/advanced_notification_distributed_manager_service.cpp b/services/ans/src/distributed_manager/advanced_notification_distributed_manager_service.cpp index 8ba566960..8ffe94edc 100644 --- a/services/ans/src/distributed_manager/advanced_notification_distributed_manager_service.cpp +++ b/services/ans/src/distributed_manager/advanced_notification_distributed_manager_service.cpp @@ -728,7 +728,30 @@ ErrCode AdvancedNotificationService::SetDistributedAuthStatus( return ERR_ANS_NON_SYSTEM_APP; } - return NotificationPreferences::GetInstance()->SetDistributedAuthStatus(deviceType, deviceId, userId, isAuth); + auto result = + NotificationPreferences::GetInstance()->SetDistributedAuthStatus(deviceType, deviceId, userId, isAuth); + if (result == ERR_OK && isAuth) { + std::vector deviceTypes; + if (NotificationPreferences::GetInstance()->GetDistributedDevicelist(deviceTypes) == ERR_OK) { + auto it = std::find(deviceTypes.begin(), deviceTypes.end(), deviceType); + if (it == deviceTypes.end()) { + deviceTypes.push_back(deviceType); + NotificationPreferences::GetInstance()->SetDistributedDevicelist(deviceTypes, userId); + } + } + } + return result; +} + +ErrCode AdvancedNotificationService::GetDistributedDevicelist(std::vector &deviceTypes) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + bool isSubSystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubSystem && !AccessTokenHelper::IsSystemApp()) { + ANS_LOGW("Not system app or SA!"); + return ERR_ANS_NON_SYSTEM_APP; + } + return NotificationPreferences::GetInstance()->GetDistributedDevicelist(deviceTypes); } ErrCode AdvancedNotificationService::GetDeviceRemindType(int32_t& remindTypeInt) diff --git a/services/ans/src/notification_preferences.cpp b/services/ans/src/notification_preferences.cpp index d23d6d397..fe38cf6c4 100644 --- a/services/ans/src/notification_preferences.cpp +++ b/services/ans/src/notification_preferences.cpp @@ -1567,6 +1567,48 @@ bool NotificationPreferences::GetkioskAppTrustList(std::vector &kio return true; } +ErrCode NotificationPreferences::SetDistributedDevicelist(std::vector &deviceTypes, const int32_t &userId) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + std::lock_guard lock(preferenceMutex_); + bool storeDBResult = true; + nlohmann::json deviceTypesJson = deviceTypes; + std::string deviceTypesjsonString = deviceTypesJson.dump(); + storeDBResult = preferncesDB_->PutDistributedDevicelist(deviceTypesjsonString, userId); + return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED; +} + +ErrCode NotificationPreferences::GetDistributedDevicelist(std::vector &deviceTypes) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + std::lock_guard lock(preferenceMutex_); + std::string value = ""; + auto storeDBResult = preferncesDB_->GetDistributedDevicelist(value); + if (!storeDBResult) { + return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED; + } + if (value.empty()) { + ANS_LOGE("Empty json"); + return ERR_OK; + } + + if (!nlohmann::json::accept(value)) { + ANS_LOGE("Invalid json string"); + return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED; + } + nlohmann::json jsonObject = nlohmann::json::parse(value, nullptr, false); + if (jsonObject.is_null() || jsonObject.empty()) { + ANS_LOGE("Invalid JSON object"); + return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED; + } + if (jsonObject.is_discarded() || !jsonObject.is_array()) { + ANS_LOGE("Parse device type list failed due to data is discarded or not array"); + return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED; + } + deviceTypes = jsonObject.get>(); + return ERR_OK; +} + ErrCode NotificationPreferences::SetSubscriberExistFlag(const std::string& deviceType, bool existFlag) { ANS_LOGD("%{public}s", __FUNCTION__); diff --git a/services/ans/src/notification_preferences_database.cpp b/services/ans/src/notification_preferences_database.cpp index 07fb36696..87e47d781 100644 --- a/services/ans/src/notification_preferences_database.cpp +++ b/services/ans/src/notification_preferences_database.cpp @@ -268,6 +268,11 @@ static const char* const KEY_DISTRIBUTED_NOTIFICATION_SWITCH = "distributedNotif */ static const char* const KEY_ENABLE_DISTRIBUTED_AUTH_STATUS = "enabledDistributedAuthStatus"; +/** + * Indicates that distributed device list. + */ +static const char* const KEY_DISTRIBUTED_DEVICE_LIST = "distributedDeviceList"; + NotificationPreferencesDatabase::NotificationPreferencesDatabase() { NotificationRdbConfig notificationRdbConfig; @@ -2305,6 +2310,45 @@ ErrCode NotificationPreferencesDatabase::SetDistributedAuthStatus( return (result == NativeRdb::E_OK); } +bool NotificationPreferencesDatabase::PutDistributedDevicelist(const std::string &deviceTypes, const int32_t &userId) +{ + ANS_LOGD("%{public}s, deviceTypes: %{public}s, userId: %{public}d", __FUNCTION__, deviceTypes.c_str(), userId); + if (!CheckRdbStore()) { + ANS_LOGE("null RdbStore"); + return false; + } + int32_t result = rdbDataManager_->InsertData(KEY_DISTRIBUTED_DEVICE_LIST, deviceTypes, userId); + return (result == NativeRdb::E_OK); +} + +bool NotificationPreferencesDatabase::GetDistributedDevicelist(std::string &deviceTypes) +{ + int32_t userId = SUBSCRIBE_USER_INIT; + OHOS::AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(userId); + if (userId == SUBSCRIBE_USER_INIT) { + ANS_LOGE("Current user acquisition failed"); + return false; + } + bool result = false; + GetValueFromDisturbeDB(KEY_DISTRIBUTED_DEVICE_LIST, userId, [&](const int32_t &status, std::string &value) { + switch (status) { + case NativeRdb::E_EMPTY_VALUES_BUCKET: { + result = true; + break; + } + case NativeRdb::E_OK: { + result = true; + deviceTypes = value; + break; + } + default: + result = false; + break; + } + }); + return result; +} + bool NotificationPreferencesDatabase::GetDistributedEnabledForBundle(const std::string deviceType, const NotificationPreferencesInfo::BundleInfo &bundleInfo, bool &enabled) { diff --git a/services/ans/test/unittest/BUILD.gn b/services/ans/test/unittest/BUILD.gn index 5c368267f..4b1e38b42 100644 --- a/services/ans/test/unittest/BUILD.gn +++ b/services/ans/test/unittest/BUILD.gn @@ -597,6 +597,37 @@ ohos_unittest("notification_service_publish_test") { part_name = "${component_name}" } +ohos_unittest("notification_preferences_second_test") { + module_out_path = module_output_path + include_dirs = [ + ".", + "/${services_path}/ans/include", + ] + + defines = [] + + sources = [ "notification_preferences_second_test.cpp" ] + + deps = [ + "${frameworks_module_ans_path}:ans_innerkits", + "${services_path}/ans:libans", + ] + + external_deps = [ + "c_utils:utils", + "data_share:datashare_consumer", + "ffrt:libffrt", + "googletest:gtest_main", + "hilog:libhilog", + "hitrace:hitrace_meter", + "kv_store:distributeddata_inner", + "relational_store:native_rdb", + ] + + subsystem_name = "${subsystem_name}" + part_name = "${component_name}" +} + ohos_unittest("notification_preferences_test") { module_out_path = module_output_path include_dirs = [ @@ -1612,6 +1643,7 @@ group("unittest") { ":notification_extension_wrapper_unit_test", ":notification_preferences_database_branch_test", ":notification_preferences_database_test", + ":notification_preferences_second_test", ":notification_preferences_test", ":notification_publish_service_test", ":notification_rdb_data_mgr_test", diff --git a/services/ans/test/unittest/advanced_notification_service_test/advanced_notification_distributed_manager_service_test.cpp b/services/ans/test/unittest/advanced_notification_service_test/advanced_notification_distributed_manager_service_test.cpp index 93d96699e..f5112acb9 100644 --- a/services/ans/test/unittest/advanced_notification_service_test/advanced_notification_distributed_manager_service_test.cpp +++ b/services/ans/test/unittest/advanced_notification_service_test/advanced_notification_distributed_manager_service_test.cpp @@ -784,5 +784,53 @@ HWTEST_F(AdvancedNotificationDistMgrServiceTest, SetDistributedAuthStatus_200, F ASSERT_EQ(ret, (int)ERR_OK); } +/** + * @tc.name: GetDistributedDevicelist_0100 + * @tc.desc: Test GetDistributedDevicelist. + * @tc.type: FUNC + */ +HWTEST_F(AdvancedNotificationDistMgrServiceTest, GetDistributedDevicelist_0100, Function | SmallTest | Level1) +{ + MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP); + MockIsSystemApp(false); + std::vector deviceTypes; + auto ret = advancedNotificationService_->GetDistributedDevicelist(deviceTypes); + ASSERT_EQ(ret, (int)ERR_ANS_NON_SYSTEM_APP); +} + +/** + * @tc.name: GetDistributedDevicelist_0200 + * @tc.desc: Test GetDistributedDevicelist. + * @tc.type: FUNC + */ +HWTEST_F(AdvancedNotificationDistMgrServiceTest, GetDistributedDevicelist_0200, Function | SmallTest | Level1) +{ + MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP); + MockIsSystemApp(true); + std::vector deviceTypes; + auto ret = advancedNotificationService_->GetDistributedDevicelist(deviceTypes); + ASSERT_EQ(ret, (int)ERR_OK); +} + +/** + * @tc.name: GetDistributedDevicelist_0300 + * @tc.desc: Test GetDistributedDevicelist. + * @tc.type: FUNC + */ +HWTEST_F(AdvancedNotificationDistMgrServiceTest, GetDistributedDevicelist_0300, Function | SmallTest | Level1) +{ + MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP); + MockIsSystemApp(true); + const std::string deviceType = "testDeviceType"; + const std::string deviceId = "testDeviceId"; + int32_t userId = 100; + bool isAuth = true; + auto ret = advancedNotificationService_->SetDistributedAuthStatus(deviceType, deviceId, userId, isAuth); + ASSERT_EQ(ret, (int)ERR_OK); + std::vector deviceTypes; + ret = advancedNotificationService_->GetDistributedDevicelist(deviceTypes); + ASSERT_EQ(ret, (int)ERR_OK); + ASSERT_NE(deviceTypes.size(), 0); +} } // namespace Notification } // namespace OHOS \ No newline at end of file diff --git a/services/ans/test/unittest/notification_preferences_database_test.cpp b/services/ans/test/unittest/notification_preferences_database_test.cpp index 1548a81c9..92d639a89 100644 --- a/services/ans/test/unittest/notification_preferences_database_test.cpp +++ b/services/ans/test/unittest/notification_preferences_database_test.cpp @@ -1723,5 +1723,86 @@ HWTEST_F(NotificationPreferencesDatabaseTest, DelCloneProfileInfo_0205, TestSize result = preferncesDB_->DelBatchCloneProfileInfo(100, deleteProfileInfo); ASSERT_EQ(result, true); } + +/** + * @tc.name: PutDistributedDevicelist_0100 + * @tc.desc: Test PutDistributedDevicelist + * @tc.type: FUNC + */ +HWTEST_F(NotificationPreferencesDatabaseTest, PutDistributedDevicelist_0100, TestSize.Level1) +{ + preferncesDB_->rdbDataManager_ = nullptr; + std::string deviceTypes = "deviceTypes"; + int32_t userId = 100; + auto ret = preferncesDB_->PutDistributedDevicelist(deviceTypes, userId); + ASSERT_EQ(ret, false); +} + +/** + * @tc.name: PutDistributedDevicelist_0200 + * @tc.desc: Test PutDistributedDevicelist + * @tc.type: FUNC + */ +HWTEST_F(NotificationPreferencesDatabaseTest, PutDistributedDevicelist_0200, TestSize.Level1) +{ + preferncesDB_ = std::make_unique(); + ASSERT_NE(preferncesDB_, nullptr); + std::string deviceTypes = "deviceTypes"; + int32_t userId = 100; + auto ret = preferncesDB_->PutDistributedDevicelist(deviceTypes, userId); + ASSERT_EQ(ret, true); +} + +/** + * @tc.name: GetDistributedDevicelist_0100 + * @tc.desc: Test GetDistributedDevicelist + * @tc.type: FUNC + */ +HWTEST_F(NotificationPreferencesDatabaseTest, GetDistributedDevicelist_0100, TestSize.Level1) +{ + std::string deviceTypes; + int32_t userId = 100; + auto ret = preferncesDB_->PutDistributedDevicelist(deviceTypes, userId); + ASSERT_EQ(ret, true); + ret = preferncesDB_->GetDistributedDevicelist(deviceTypes); + ASSERT_EQ(ret, true); +} + +/** + * @tc.name: GetDistributedDevicelist_0200 + * @tc.desc: Test GetDistributedDevicelist + * @tc.type: FUNC + */ +HWTEST_F(NotificationPreferencesDatabaseTest, GetDistributedDevicelist_0200, TestSize.Level1) +{ + std::string deviceTypes = "deviceTypes"; + int32_t userId = 100; + auto ret = preferncesDB_->PutDistributedDevicelist(deviceTypes, userId); + ASSERT_EQ(ret, true); + ret = preferncesDB_->GetDistributedDevicelist(deviceTypes); + ASSERT_EQ(ret, true); + ASSERT_EQ(deviceTypes.empty(), false); +} + +/** + * @tc.name: GetDistributedDevicelist_0300 + * @tc.desc: Test GetDistributedDevicelist + * @tc.type: FUNC + */ +HWTEST_F(NotificationPreferencesDatabaseTest, GetDistributedDevicelist_0300, TestSize.Level1) +{ + std::string deviceTypes1 = "deviceTypes1"; + int32_t userId1 = 100; + auto ret = preferncesDB_->PutDistributedDevicelist(deviceTypes1, userId1); + ASSERT_EQ(ret, true); + std::string deviceTypes2 = "deviceTypes2"; + int32_t userId2 = 101; + ret = preferncesDB_->PutDistributedDevicelist(deviceTypes2, userId2); + ASSERT_EQ(ret, true); + std::string deviceTypes; + ret = preferncesDB_->GetDistributedDevicelist(deviceTypes); + ASSERT_EQ(ret, true); + ASSERT_EQ(deviceTypes, deviceTypes1); +} } // namespace Notification } // namespace OHOS diff --git a/services/ans/test/unittest/notification_preferences_second_test.cpp b/services/ans/test/unittest/notification_preferences_second_test.cpp new file mode 100644 index 000000000..5b4f69185 --- /dev/null +++ b/services/ans/test/unittest/notification_preferences_second_test.cpp @@ -0,0 +1,196 @@ +/* + * Copyright (c) 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 "ans_inner_errors.h" +#define private public +#define protected public +#include "notification_preferences.h" +#include "notification_preferences_database.h" +#undef private +#undef protected + +using namespace testing::ext; +namespace OHOS { +namespace Notification { +class NotificationPreferencesTest : public testing::Test { +public: + static void SetUpTestCase(){}; + static void TearDownTestCase() {} + void SetUp(){}; + void TearDown(){}; +}; + +/** + * @tc.name: SetDistributedDevicelist_0100 + * @tc.desc: Test SetDistributedDevicelist + * @tc.type: FUNC + */ +HWTEST_F(NotificationPreferencesTest, SetDistributedDevicelist_0100, Function | SmallTest | Level1) +{ + NotificationPreferences notificationPreferences; + std::vector deviceTypes; + int32_t userId = 100; + auto ret = notificationPreferences.SetDistributedDevicelist(deviceTypes, userId); + ASSERT_EQ(ret, ERR_OK); +} + +/** + * @tc.name: SetDistributedDevicelist_0200 + * @tc.desc: Test SetDistributedDevicelist + * @tc.type: FUNC + */ +HWTEST_F(NotificationPreferencesTest, SetDistributedDevicelist_0200, Function | SmallTest | Level1) +{ + NotificationPreferences notificationPreferences; + notificationPreferences.preferncesDB_->rdbDataManager_ = nullptr; + std::vector deviceTypes; + int32_t userId = 100; + auto ret = notificationPreferences.SetDistributedDevicelist(deviceTypes, userId); + ASSERT_EQ(ret, ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED); +} + +/** + * @tc.name: GetDistributedDevicelist_0100 + * @tc.desc: Test SetDistributedDevicelist + * @tc.type: FUNC + */ +HWTEST_F(NotificationPreferencesTest, GetDistributedDevicelist_0100, Function | SmallTest | Level1) +{ + NotificationPreferences notificationPreferences; + notificationPreferences.preferncesDB_->rdbDataManager_ = nullptr; + std::vector deviceTypes; + auto ret = notificationPreferences.GetDistributedDevicelist(deviceTypes); + ASSERT_EQ(ret, ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED); +} + +/** + * @tc.name: GetDistributedDevicelist_0200 + * @tc.desc: Test SetDistributedDevicelist + * @tc.type: FUNC + */ +HWTEST_F(NotificationPreferencesTest, GetDistributedDevicelist_0200, Function | SmallTest | Level1) +{ + NotificationPreferences notificationPreferences; + int32_t userId = 100; + std::string deviceTypesjsonString = ""; + notificationPreferences.preferncesDB_->PutDistributedDevicelist(deviceTypesjsonString, userId); + std::vector deviceTypes; + auto ret = notificationPreferences.GetDistributedDevicelist(deviceTypes); + ASSERT_EQ(ret, ERR_OK); +} + +/** + * @tc.name: GetDistributedDevicelist_0300 + * @tc.desc: Test SetDistributedDevicelist + * @tc.type: FUNC + */ +HWTEST_F(NotificationPreferencesTest, GetDistributedDevicelist_0300, Function | SmallTest | Level1) +{ + NotificationPreferences notificationPreferences; + int32_t userId = 100; + std::string deviceTypesjsonString = "invalid deviceTypes"; + notificationPreferences.preferncesDB_->PutDistributedDevicelist(deviceTypesjsonString, userId); + std::vector deviceTypes; + auto ret = notificationPreferences.GetDistributedDevicelist(deviceTypes); + ASSERT_EQ(ret, ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED); +} + +/** + * @tc.name: GetDistributedDevicelist_0400 + * @tc.desc: Test SetDistributedDevicelist + * @tc.type: FUNC + */ +HWTEST_F(NotificationPreferencesTest, GetDistributedDevicelist_0400, Function | SmallTest | Level1) +{ + NotificationPreferences notificationPreferences; + int32_t userId = 100; + std::string deviceTypesjsonString = "null"; + notificationPreferences.preferncesDB_->PutDistributedDevicelist(deviceTypesjsonString, userId); + std::vector deviceTypes; + auto ret = notificationPreferences.GetDistributedDevicelist(deviceTypes); + ASSERT_EQ(ret, ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED); +} + +/** + * @tc.name: GetDistributedDevicelist_0500 + * @tc.desc: Test SetDistributedDevicelist + * @tc.type: FUNC + */ +HWTEST_F(NotificationPreferencesTest, GetDistributedDevicelist_0500, Function | SmallTest | Level1) +{ + NotificationPreferences notificationPreferences; + int32_t userId = 100; + std::string deviceTypesjsonString = "[]"; + notificationPreferences.preferncesDB_->PutDistributedDevicelist(deviceTypesjsonString, userId); + std::vector deviceTypes; + auto ret = notificationPreferences.GetDistributedDevicelist(deviceTypes); + ASSERT_EQ(ret, ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED); +} + +/** + * @tc.name: GetDistributedDevicelist_0600 + * @tc.desc: Test SetDistributedDevicelist + * @tc.type: FUNC + */ +HWTEST_F(NotificationPreferencesTest, GetDistributedDevicelist_0600, Function | SmallTest | Level1) +{ + NotificationPreferences notificationPreferences; + int32_t userId = 100; + std::string deviceTypesjsonString = "[1, 2, 3,]"; + notificationPreferences.preferncesDB_->PutDistributedDevicelist(deviceTypesjsonString, userId); + std::vector deviceTypes; + auto ret = notificationPreferences.GetDistributedDevicelist(deviceTypes); + ASSERT_EQ(ret, ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED); +} + +/** + * @tc.name: GetDistributedDevicelist_0700 + * @tc.desc: Test SetDistributedDevicelist + * @tc.type: FUNC + */ +HWTEST_F(NotificationPreferencesTest, GetDistributedDevicelist_0700, Function | SmallTest | Level1) +{ + NotificationPreferences notificationPreferences; + int32_t userId = 100; + std::string deviceTypesjsonString = R"({"key": "value"})"; + notificationPreferences.preferncesDB_->PutDistributedDevicelist(deviceTypesjsonString, userId); + std::vector deviceTypes; + auto ret = notificationPreferences.GetDistributedDevicelist(deviceTypes); + ASSERT_EQ(ret, ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED); +} + +/** + * @tc.name: GetDistributedDevicelist_0800 + * @tc.desc: Test SetDistributedDevicelist + * @tc.type: FUNC + */ +HWTEST_F(NotificationPreferencesTest, GetDistributedDevicelist_0800, Function | SmallTest | Level1) +{ + NotificationPreferences notificationPreferences; + std::vector deviceTypes; + deviceTypes.push_back("deviceType1"); + int32_t userId = 100; + auto ret = notificationPreferences.SetDistributedDevicelist(deviceTypes, userId); + ASSERT_EQ(ret, ERR_OK); + deviceTypes.clear(); + ASSERT_EQ(deviceTypes.size(), 0); + ret = notificationPreferences.GetDistributedDevicelist(deviceTypes); + ASSERT_EQ(ret, ERR_OK); + ASSERT_EQ(deviceTypes.size(), 1); +} +} // namespace Notification +} // namespace OHOS diff --git a/tools/test/mock/mock_ans_manager_stub.h b/tools/test/mock/mock_ans_manager_stub.h index 1545c603c..e32f22365 100644 --- a/tools/test/mock/mock_ans_manager_stub.h +++ b/tools/test/mock/mock_ans_manager_stub.h @@ -587,6 +587,11 @@ public: return ERR_ANS_INVALID_PARAM; } + ErrCode GetDistributedDevicelist(std::vector &deviceTypes) override + { + return ERR_ANS_INVALID_PARAM; + } + ErrCode IsSmartReminderEnabled(const std::string& deviceType, bool& enabled) override { return ERR_ANS_INVALID_PARAM; -- Gitee