1 /* 2 * Copyright (c) 2023 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 #include "gmock/gmock.h" 18 #define private public 19 #define protected public 20 #include "notification_clone_disturb_service.h" 21 #include "notification_preferences_info.h" 22 #include "notification_do_not_disturb_profile.h" 23 #include "ans_inner_errors.h" 24 #include "notification_preferences.h" 25 #include "notification_clone_util.h" 26 #include "mock/mock_notification_clone_util.h" 27 #undef private 28 #undef protected 29 30 using namespace testing::ext; 31 using namespace testing; 32 using ::testing::_; 33 using ::testing::SetArgPointee; 34 using ::testing::Return; 35 using ::testing::DoAll; 36 using namespace OHOS; 37 using namespace Notification; 38 39 // Test suite class 40 class NotificationCloneDisturbTest : public ::testing::Test { 41 protected: SetUp()42 void SetUp() override 43 { 44 // Initialize objects and dependencies 45 notificationCloneDisturb = new NotificationCloneDisturb(); 46 } 47 TearDown()48 void TearDown() override 49 { 50 // Clean up resources 51 delete notificationCloneDisturb; 52 notificationCloneDisturb = nullptr; 53 } 54 55 NotificationCloneDisturb* notificationCloneDisturb; 56 }; 57 58 /** 59 * @tc.name: OnBackUp_00001 60 * @tc.desc: Test clone OnBackUp. 61 * @tc.type: FUNC 62 * @tc.require: issue 63 */ 64 HWTEST_F(NotificationCloneDisturbTest, OnBackUp_00001, Function | SmallTest | Level1) 65 { 66 nlohmann::json jsonObject; 67 int32_t userId = 100; 68 auto advancedNotificationService_ = AdvancedNotificationService::GetInstance(); 69 70 sptr<NotificationDoNotDisturbProfile> date = nullptr; 71 std::vector<sptr<NotificationDoNotDisturbProfile>> profiles = { date }; 72 auto ret = advancedNotificationService_->AddDoNotDisturbProfiles(profiles); 73 74 ErrCode result = notificationCloneDisturb->OnBackup(jsonObject); 75 notificationCloneDisturb->OnRestore(jsonObject); 76 EXPECT_EQ(result, ERR_OK); 77 } 78 79 80 /** 81 * @tc.name: OnRestore_00001 82 * @tc.desc: Test clone OnRestore jsonObject is null. 83 * @tc.type: FUNC 84 * @tc.require: issue 85 */ 86 HWTEST_F(NotificationCloneDisturbTest, OnRestore_00001, Function | SmallTest | Level1) 87 { 88 nlohmann::json jsonObject = nullptr; 89 notificationCloneDisturb->OnRestore(jsonObject); 90 EXPECT_EQ(jsonObject, nullptr); 91 } 92 93 /** 94 * @tc.name: GetProfileUid_Test_001 95 * @tc.desc: Test that the function sets the UID from uidMap when the key exists in uidMap. 96 * @tc.type: FUNC 97 * @tc.require: issue 98 */ 99 HWTEST_F(NotificationCloneDisturbTest, GetProfileUid_Test_001, Function | SmallTest | Level1) 100 { 101 int32_t userId = 1; 102 std::map<std::string, int32_t> uidMap; 103 std::vector<NotificationBundleOption> trustList; 104 std::vector<NotificationBundleOption> exitBunldleList; 105 std::vector<NotificationBundleOption> notExitBunldleList; 106 107 // Create a bundle with a known key 108 NotificationBundleOption bundle; 109 bundle.SetBundleName("com.example.app"); 110 bundle.SetAppIndex(1); 111 std::string key = "com.example.app1"; 112 uidMap[key] = 12345; 113 114 trustList.push_back(bundle); 115 116 notificationCloneDisturb->GetProfileUid(userId, uidMap, trustList, exitBunldleList, notExitBunldleList); 117 118 EXPECT_EQ(exitBunldleList.size(), 1); 119 EXPECT_EQ(notExitBunldleList.size(), 0); 120 } 121 122 /** 123 * @tc.name: GetProfileUid_Test_002 124 * @tc.desc: Test that the function sets the UID from uidMap when the key exists in uidMap. 125 * @tc.type: FUNC 126 * @tc.require: issue 127 */ 128 HWTEST_F(NotificationCloneDisturbTest, GetProfileUid_Test_002, Function | SmallTest | Level1) 129 { 130 int32_t userId = 1; 131 std::map<std::string, int32_t> uidMap; 132 std::vector<NotificationBundleOption> trustList; 133 std::vector<NotificationBundleOption> exitBunldleList; 134 std::vector<NotificationBundleOption> notExitBunldleList; 135 136 // Create a bundle with an unknown key 137 NotificationBundleOption bundle; 138 bundle.SetBundleName("com.example.app"); 139 bundle.SetAppIndex(1); 140 141 trustList.push_back(bundle); 142 notificationCloneDisturb->GetProfileUid(userId, uidMap, trustList, exitBunldleList, notExitBunldleList); 143 144 EXPECT_EQ(exitBunldleList.size(), 0); 145 EXPECT_EQ(notExitBunldleList.size(), 1); 146 } 147 148 /** 149 * @tc.name: OnRestoreStart_Test_001 150 * @tc.desc: Test OnRestoreStart function when profiles_ is empty 151 * @tc.type: FUNC 152 * @tc.require: issue 153 */ 154 HWTEST_F(NotificationCloneDisturbTest, OnRestoreStart_Test_001, Function | SmallTest | Level1) 155 { 156 std::string bundleName = "com.example.app"; 157 int32_t appIndex = 1; 158 int32_t userId = 100; 159 int32_t uid = 12345; 160 161 // Ensure profiles_ is empty 162 notificationCloneDisturb->profiles_.clear(); 163 164 // Call the function 165 notificationCloneDisturb->OnRestoreStart(bundleName, appIndex, userId, uid); 166 167 // Verify that the function returned without any action 168 EXPECT_TRUE(notificationCloneDisturb->profiles_.empty()); 169 } 170 171 /** 172 * @tc.name: OnRestoreStart_Test_002 173 * @tc.desc: Test OnRestoreStart function when cloneDisturbQueue_ is null 174 * @tc.type: FUNC 175 * @tc.require: issue 176 */ 177 HWTEST_F(NotificationCloneDisturbTest, OnRestoreStart_Test_002, Function | SmallTest | Level1) 178 { 179 std::string bundleName = "com.example.app"; 180 int32_t appIndex = 1; 181 int32_t userId = 100; 182 int32_t uid = 12345; 183 184 // Ensure cloneDisturbQueue_ is null 185 notificationCloneDisturb->cloneDisturbQueue_ = nullptr; 186 187 // Call the function 188 notificationCloneDisturb->OnRestoreStart(bundleName, appIndex, userId, uid); 189 190 // Verify that the function returned without any action 191 EXPECT_EQ(notificationCloneDisturb->cloneDisturbQueue_, nullptr); 192 } 193 194 /** 195 * @tc.name: OnRestoreStart_Test_004 196 * @tc.desc: Test OnRestoreStart function when trustList is empty 197 * @tc.type: FUNC 198 * @tc.require: issue 199 */ 200 HWTEST_F(NotificationCloneDisturbTest, OnRestoreStart_Test_004, Function | SmallTest | Level1) 201 { 202 std::string bundleName = "com.example.app"; 203 int32_t appIndex = 1; 204 int32_t userId = 100; 205 int32_t uid = 12345; 206 207 // Add a profile to profiles_ with an empty trust list 208 auto profile = NotificationDoNotDisturbProfile(1, "name", {}); 209 210 // Ensure cloneDisturbQueue_ is not null 211 notificationCloneDisturb->cloneDisturbQueue_ = std::make_shared<ffrt::queue>("NotificationCloneDisturbQueue"); 212 213 // Call the function 214 notificationCloneDisturb->OnRestoreStart(bundleName, appIndex, userId, uid); 215 216 // Verify that the profile is deleted 217 EXPECT_TRUE(notificationCloneDisturb->profiles_.empty()); 218 } 219 220 /** 221 * @tc.name: OnUserSwitch_Test_001 222 * @tc.desc: Test OnUserSwitch function when cloneDisturbQueue_ is empty 223 * @tc.type: FUNC 224 * @tc.require: issue 225 */ 226 HWTEST_F(NotificationCloneDisturbTest, OnUserSwitch_Test_001, Function | SmallTest | Level1) 227 { 228 // Ensure cloneDisturbQueue_ is not null 229 notificationCloneDisturb->cloneDisturbQueue_ = nullptr; 230 231 // Call the function 232 notificationCloneDisturb->OnUserSwitch(100); 233 234 // Verify that the profile is deleted 235 EXPECT_EQ(notificationCloneDisturb->cloneDisturbQueue_, nullptr); 236 } 237 238 /** 239 * @tc.name: CheckBundleInfo_Test_001 240 * @tc.desc: Test that when a matching bundle is found in trustList, it is added to bundleList and 241 * removed from trustList. 242 * @tc.type: FUNC 243 * @tc.require: issue 244 */ 245 HWTEST_F(NotificationCloneDisturbTest, CheckBundleInfo_Test_001, Function | SmallTest | Level1) 246 { 247 std::vector<NotificationBundleOption> trustList; 248 std::vector<NotificationBundleOption> bundleList; 249 NotificationBundleOption bundle; 250 bundle.SetBundleName("com.example.app"); 251 bundle.SetAppIndex(1); 252 253 NotificationBundleOption matchingBundle; 254 matchingBundle.SetBundleName("com.example.app"); 255 matchingBundle.SetAppIndex(1); 256 trustList.push_back(matchingBundle); 257 258 notificationCloneDisturb->CheckBundleInfo(trustList, bundleList, bundle); 259 260 EXPECT_EQ(bundleList.size(), 1); 261 EXPECT_EQ(trustList.size(), 0); 262 EXPECT_EQ(bundleList[0].GetBundleName(), "com.example.app"); 263 EXPECT_EQ(bundleList[0].GetAppIndex(), 1); 264 } 265 266 /** 267 * @tc.name: CheckBundleInfo_Test_002 268 * @tc.desc: Test that when no matching bundle is found in trustList, the bundle is not added to bundleList. 269 * @tc.type: FUNC 270 * @tc.require: issue 271 */ 272 HWTEST_F(NotificationCloneDisturbTest, CheckBundleInfo_Test_002, Function | SmallTest | Level1) 273 { 274 std::vector<NotificationBundleOption> trustList; 275 std::vector<NotificationBundleOption> bundleList; 276 NotificationBundleOption bundle; 277 bundle.SetBundleName("com.example.app"); 278 bundle.SetAppIndex(1); 279 280 NotificationBundleOption nonMatchingBundle; 281 nonMatchingBundle.SetBundleName("com.example.other"); 282 nonMatchingBundle.SetAppIndex(2); 283 trustList.push_back(nonMatchingBundle); 284 285 notificationCloneDisturb->CheckBundleInfo(trustList, bundleList, bundle); 286 287 EXPECT_EQ(bundleList.size(), 0); 288 EXPECT_EQ(trustList.size(), 1); 289 } 290 291 /** 292 * @tc.name: CheckBundleInfo_Test_003 293 * @tc.desc: Test that when trustList is empty, the bundle is not added to bundleList. 294 * @tc.type: FUNC 295 * @tc.require: issue 296 */ 297 HWTEST_F(NotificationCloneDisturbTest, CheckBundleInfo_Test_003, Function | SmallTest | Level1) 298 { 299 std::vector<NotificationBundleOption> trustList; 300 std::vector<NotificationBundleOption> bundleList; 301 NotificationBundleOption bundle; 302 bundle.SetBundleName("com.example.app"); 303 bundle.SetAppIndex(1); 304 305 notificationCloneDisturb->CheckBundleInfo(trustList, bundleList, bundle); 306 307 EXPECT_EQ(bundleList.size(), 0); 308 EXPECT_EQ(trustList.size(), 0); 309 }