1 /* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include <gtest/gtest.h> 17 18 #include "ans_inner_errors.h" 19 #define private public 20 #define protected public 21 #include "notification_preferences.h" 22 #include "notification_preferences_database.h" 23 #undef private 24 #undef protected 25 26 using namespace testing::ext; 27 namespace OHOS { 28 namespace Notification { 29 class NotificationPreferencesTest : public testing::Test { 30 public: SetUpTestCase()31 static void SetUpTestCase(){}; TearDownTestCase()32 static void TearDownTestCase() {} SetUp()33 void SetUp(){}; TearDown()34 void TearDown(){}; 35 }; 36 37 /** 38 * @tc.name: SetDistributedDevicelist_0100 39 * @tc.desc: Test SetDistributedDevicelist 40 * @tc.type: FUNC 41 */ 42 HWTEST_F(NotificationPreferencesTest, SetDistributedDevicelist_0100, Function | SmallTest | Level1) 43 { 44 NotificationPreferences notificationPreferences; 45 std::vector<std::string> deviceTypes; 46 int32_t userId = 100; 47 auto ret = notificationPreferences.SetDistributedDevicelist(deviceTypes, userId); 48 ASSERT_EQ(ret, ERR_OK); 49 } 50 51 /** 52 * @tc.name: SetDistributedDevicelist_0200 53 * @tc.desc: Test SetDistributedDevicelist 54 * @tc.type: FUNC 55 */ 56 HWTEST_F(NotificationPreferencesTest, SetDistributedDevicelist_0200, Function | SmallTest | Level1) 57 { 58 NotificationPreferences notificationPreferences; 59 notificationPreferences.preferncesDB_->rdbDataManager_ = nullptr; 60 std::vector<std::string> deviceTypes; 61 int32_t userId = 100; 62 auto ret = notificationPreferences.SetDistributedDevicelist(deviceTypes, userId); 63 ASSERT_EQ(ret, ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED); 64 } 65 66 /** 67 * @tc.name: GetDistributedDevicelist_0100 68 * @tc.desc: Test SetDistributedDevicelist 69 * @tc.type: FUNC 70 */ 71 HWTEST_F(NotificationPreferencesTest, GetDistributedDevicelist_0100, Function | SmallTest | Level1) 72 { 73 NotificationPreferences notificationPreferences; 74 notificationPreferences.preferncesDB_->rdbDataManager_ = nullptr; 75 std::vector<std::string> deviceTypes; 76 auto ret = notificationPreferences.GetDistributedDevicelist(deviceTypes); 77 ASSERT_EQ(ret, ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED); 78 } 79 80 /** 81 * @tc.name: GetDistributedDevicelist_0200 82 * @tc.desc: Test SetDistributedDevicelist 83 * @tc.type: FUNC 84 */ 85 HWTEST_F(NotificationPreferencesTest, GetDistributedDevicelist_0200, Function | SmallTest | Level1) 86 { 87 NotificationPreferences notificationPreferences; 88 int32_t userId = 100; 89 std::string deviceTypesjsonString = ""; 90 notificationPreferences.preferncesDB_->PutDistributedDevicelist(deviceTypesjsonString, userId); 91 std::vector<std::string> deviceTypes; 92 auto ret = notificationPreferences.GetDistributedDevicelist(deviceTypes); 93 ASSERT_EQ(ret, ERR_OK); 94 } 95 96 /** 97 * @tc.name: GetDistributedDevicelist_0300 98 * @tc.desc: Test SetDistributedDevicelist 99 * @tc.type: FUNC 100 */ 101 HWTEST_F(NotificationPreferencesTest, GetDistributedDevicelist_0300, Function | SmallTest | Level1) 102 { 103 NotificationPreferences notificationPreferences; 104 int32_t userId = 100; 105 std::string deviceTypesjsonString = "invalid deviceTypes"; 106 notificationPreferences.preferncesDB_->PutDistributedDevicelist(deviceTypesjsonString, userId); 107 std::vector<std::string> deviceTypes; 108 auto ret = notificationPreferences.GetDistributedDevicelist(deviceTypes); 109 ASSERT_EQ(ret, ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED); 110 } 111 112 /** 113 * @tc.name: GetDistributedDevicelist_0400 114 * @tc.desc: Test SetDistributedDevicelist 115 * @tc.type: FUNC 116 */ 117 HWTEST_F(NotificationPreferencesTest, GetDistributedDevicelist_0400, Function | SmallTest | Level1) 118 { 119 NotificationPreferences notificationPreferences; 120 int32_t userId = 100; 121 std::string deviceTypesjsonString = "null"; 122 notificationPreferences.preferncesDB_->PutDistributedDevicelist(deviceTypesjsonString, userId); 123 std::vector<std::string> deviceTypes; 124 auto ret = notificationPreferences.GetDistributedDevicelist(deviceTypes); 125 ASSERT_EQ(ret, ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED); 126 } 127 128 /** 129 * @tc.name: GetDistributedDevicelist_0500 130 * @tc.desc: Test SetDistributedDevicelist 131 * @tc.type: FUNC 132 */ 133 HWTEST_F(NotificationPreferencesTest, GetDistributedDevicelist_0500, Function | SmallTest | Level1) 134 { 135 NotificationPreferences notificationPreferences; 136 int32_t userId = 100; 137 std::string deviceTypesjsonString = "[]"; 138 notificationPreferences.preferncesDB_->PutDistributedDevicelist(deviceTypesjsonString, userId); 139 std::vector<std::string> deviceTypes; 140 auto ret = notificationPreferences.GetDistributedDevicelist(deviceTypes); 141 ASSERT_EQ(ret, ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED); 142 } 143 144 /** 145 * @tc.name: GetDistributedDevicelist_0600 146 * @tc.desc: Test SetDistributedDevicelist 147 * @tc.type: FUNC 148 */ 149 HWTEST_F(NotificationPreferencesTest, GetDistributedDevicelist_0600, Function | SmallTest | Level1) 150 { 151 NotificationPreferences notificationPreferences; 152 int32_t userId = 100; 153 std::string deviceTypesjsonString = "[1, 2, 3,]"; 154 notificationPreferences.preferncesDB_->PutDistributedDevicelist(deviceTypesjsonString, userId); 155 std::vector<std::string> deviceTypes; 156 auto ret = notificationPreferences.GetDistributedDevicelist(deviceTypes); 157 ASSERT_EQ(ret, ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED); 158 } 159 160 /** 161 * @tc.name: GetDistributedDevicelist_0700 162 * @tc.desc: Test SetDistributedDevicelist 163 * @tc.type: FUNC 164 */ 165 HWTEST_F(NotificationPreferencesTest, GetDistributedDevicelist_0700, Function | SmallTest | Level1) 166 { 167 NotificationPreferences notificationPreferences; 168 int32_t userId = 100; 169 std::string deviceTypesjsonString = R"({"key": "value"})"; 170 notificationPreferences.preferncesDB_->PutDistributedDevicelist(deviceTypesjsonString, userId); 171 std::vector<std::string> deviceTypes; 172 auto ret = notificationPreferences.GetDistributedDevicelist(deviceTypes); 173 ASSERT_EQ(ret, ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED); 174 } 175 176 /** 177 * @tc.name: GetDistributedDevicelist_0800 178 * @tc.desc: Test SetDistributedDevicelist 179 * @tc.type: FUNC 180 */ 181 HWTEST_F(NotificationPreferencesTest, GetDistributedDevicelist_0800, Function | SmallTest | Level1) 182 { 183 NotificationPreferences notificationPreferences; 184 std::vector<std::string> deviceTypes; 185 deviceTypes.push_back("deviceType1"); 186 int32_t userId = 100; 187 auto ret = notificationPreferences.SetDistributedDevicelist(deviceTypes, userId); 188 ASSERT_EQ(ret, ERR_OK); 189 deviceTypes.clear(); 190 ASSERT_EQ(deviceTypes.size(), 0); 191 ret = notificationPreferences.GetDistributedDevicelist(deviceTypes); 192 ASSERT_EQ(ret, ERR_OK); 193 ASSERT_EQ(deviceTypes.size(), 1); 194 } 195 } // namespace Notification 196 } // namespace OHOS 197