1 /* 2 * Copyright (c) 2021-2024 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 #define private public 19 20 #include "notification_analytics_util.h" 21 #include "mock_common_event_manager.h" 22 #include "string_wrapper.h" 23 #include "want_params_wrapper.h" 24 #include "distributed_device_status.h" 25 26 using namespace testing::ext; 27 28 namespace OHOS { 29 namespace Notification { 30 31 class NotificationAnalyticsUtilTest : public testing::Test { 32 public: SetUpTestCase()33 static void SetUpTestCase() {}; TearDownTestCase()34 static void TearDownTestCase() {}; SetUp()35 void SetUp() {}; TearDown()36 void TearDown() {}; 37 }; 38 39 /** 40 * @tc.name: NeedReport_100 41 * @tc.desc: Test NeedReport when error code is ok and checkfailed is true. 42 * @tc.type: FUNC 43 */ 44 HWTEST_F(NotificationAnalyticsUtilTest, NeedReport_100, Function | SmallTest | Level1) 45 { 46 HaMetaMessage message; 47 message.errorCode_ = ERR_OK; 48 49 auto ret = message.NeedReport(); 50 51 ASSERT_FALSE(ret); 52 } 53 54 /** 55 * @tc.name: NeedReport_200 56 * @tc.desc: Test NeedReport. 57 * @tc.type: FUNC 58 */ 59 HWTEST_F(NotificationAnalyticsUtilTest, NeedReport_200, Function | SmallTest | Level1) 60 { 61 HaMetaMessage message; 62 message.checkfailed_ = false; 63 64 auto ret = message.NeedReport(); 65 66 ASSERT_TRUE(ret); 67 } 68 69 /** 70 * @tc.name: Checkfailed_100 71 * @tc.desc: Test Checkfailed when default is true. 72 * @tc.type: FUNC 73 */ 74 HWTEST_F(NotificationAnalyticsUtilTest, Checkfailed_100, Function | SmallTest | Level1) 75 { 76 HaMetaMessage message; 77 bool checkfailed = false; 78 79 message.Checkfailed(checkfailed); 80 81 ASSERT_FALSE(message.checkfailed_); 82 } 83 84 /** 85 * @tc.name: TypeCode_100 86 * @tc.desc: Test TypeCode when default is -1. 87 * @tc.type: FUNC 88 */ 89 HWTEST_F(NotificationAnalyticsUtilTest, TypeCode_100, Function | SmallTest | Level1) 90 { 91 HaMetaMessage message; 92 int32_t typeCode = 0; 93 94 message.TypeCode(typeCode); 95 96 ASSERT_EQ(message.typeCode_, 0); 97 } 98 99 /** 100 * @tc.name: GetMessage_100 101 * @tc.desc: Test GetMessage. 102 * @tc.type: FUNC 103 */ 104 HWTEST_F(NotificationAnalyticsUtilTest, GetMessage_100, Function | SmallTest | Level1) 105 { 106 std::string expect = "test"; 107 HaMetaMessage message; 108 message.message_ = expect; 109 110 auto ret = message.GetMessage(); 111 112 ASSERT_EQ(ret, expect); 113 } 114 115 /** 116 * @tc.name: DeleteReason_100 117 * @tc.desc: Test DeleteReason when default is -1. 118 * @tc.type: FUNC 119 */ 120 HWTEST_F(NotificationAnalyticsUtilTest, DeleteReason_100, Function | SmallTest | Level1) 121 { 122 HaMetaMessage message; 123 int32_t deleteReason = 0; 124 125 message.DeleteReason(deleteReason); 126 127 ASSERT_EQ(message.deleteReason_, deleteReason); 128 } 129 130 /** 131 * @tc.name: ReportDeleteFailedEvent_100 132 * @tc.desc: Test ReportDeleteFailedEvent. 133 * @tc.type: FUNC 134 */ 135 HWTEST_F(NotificationAnalyticsUtilTest, ReportDeleteFailedEvent_100, Function | SmallTest | Level1) 136 { 137 HaMetaMessage message; 138 message.errorCode_ = ERR_OK; 139 message.checkfailed_ = false; 140 std::string bundle = "bundle"; 141 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest(); 142 auto agentBundleOption = std::make_shared<NotificationBundleOption>(bundle, DEFAULT_UID); 143 request->SetAgentBundle(agentBundleOption); 144 145 NotificationAnalyticsUtil::ReportDeleteFailedEvent(request, message); 146 147 ASSERT_EQ(message.agentBundleName_, bundle); 148 } 149 150 /** 151 * @tc.name: ReportDeleteFailedEvent_200 152 * @tc.desc: Test ReportDeleteFailedEvent when no need to report. 153 * @tc.type: FUNC 154 */ 155 HWTEST_F(NotificationAnalyticsUtilTest, ReportDeleteFailedEvent_200, Function | SmallTest | Level1) 156 { 157 HaMetaMessage message; 158 message.errorCode_ = ERR_OK; 159 message.checkfailed_ = true; 160 std::string bundle = "bundle"; 161 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest(); 162 auto agentBundleOption = std::make_shared<NotificationBundleOption>(bundle, DEFAULT_UID); 163 request->SetAgentBundle(agentBundleOption); 164 165 NotificationAnalyticsUtil::ReportDeleteFailedEvent(request, message); 166 167 ASSERT_NE(message.agentBundleName_, bundle); 168 } 169 170 /** 171 * @tc.name: Operation_100 172 * @tc.desc: Test Operation. 173 * @tc.type: FUNC 174 */ 175 HWTEST_F(NotificationAnalyticsUtilTest, Operation_100, Function | SmallTest | Level1) 176 { 177 HaOperationMessage operationMessage = HaOperationMessage(false); 178 std::vector<std::string> deviceTypes; 179 deviceTypes.push_back("abc"); 180 deviceTypes.push_back("2in1"); 181 deviceTypes.push_back("tablet"); 182 operationMessage.KeyNode(true).SyncPublish(deviceTypes); 183 operationMessage.ToJson(); 184 ASSERT_EQ(operationMessage.notificationData.countTime, 2); 185 operationMessage.ResetData(); 186 187 operationMessage = HaOperationMessage(true); 188 deviceTypes.clear(); 189 deviceTypes.push_back("abc"); 190 deviceTypes.push_back("wearable"); 191 deviceTypes.push_back("headset"); 192 operationMessage.KeyNode(false).SyncPublish(deviceTypes); 193 operationMessage.ToJson(); 194 ASSERT_EQ(operationMessage.liveViewData.countTime, 2); 195 ASSERT_EQ(operationMessage.liveViewData.syncWatchHead, 1); 196 } 197 198 /** 199 * @tc.name: Operation_200 200 * @tc.desc: Test Operation. 201 * @tc.type: FUNC 202 */ 203 HWTEST_F(NotificationAnalyticsUtilTest, Operation_200, Function | SmallTest | Level1) 204 { 205 HaOperationMessage operationMessage = HaOperationMessage(false); 206 operationMessage.SyncDelete("2in1", std::string()).SyncClick("2in1").SyncReply("2in1"); 207 operationMessage.SyncDelete("pcb", std::string()).SyncClick("pcb").SyncReply("pcb"); 208 operationMessage.ToJson(); 209 210 ASSERT_EQ(operationMessage.notificationData.countTime, 3); 211 operationMessage.ResetData(); 212 213 operationMessage = HaOperationMessage(true); 214 operationMessage.ResetData(); 215 operationMessage.SyncDelete("2in1", std::string()).SyncClick("2in1").SyncReply("2in1"); 216 operationMessage.SyncDelete("pcb", std::string()).SyncClick("pcb").SyncReply("pcb"); 217 operationMessage.ToJson(); 218 ASSERT_EQ(operationMessage.liveViewData.countTime, 3); 219 } 220 221 /** 222 * @tc.name: Operation_300 223 * @tc.desc: Test Operation. 224 * @tc.type: FUNC 225 */ 226 HWTEST_F(NotificationAnalyticsUtilTest, Operation_300, Function | SmallTest | Level1) 227 { 228 HaOperationMessage operationMessage = HaOperationMessage(false); 229 operationMessage.ResetData(); 230 ASSERT_EQ(operationMessage.DetermineWhetherToSend(), false); 231 232 operationMessage.isLiveView_ = true; 233 ASSERT_EQ(operationMessage.DetermineWhetherToSend(), false); 234 235 operationMessage.isLiveView_ = false; 236 operationMessage.liveViewData.keyNode++; 237 ASSERT_EQ(operationMessage.DetermineWhetherToSend(), false); 238 239 operationMessage.isLiveView_ = true; 240 operationMessage.liveViewData.countTime = 10000; 241 ASSERT_EQ(operationMessage.DetermineWhetherToSend(), true); 242 243 operationMessage.liveViewData.countTime = 10; 244 operationMessage.liveViewData.time = 0; 245 ASSERT_EQ(operationMessage.DetermineWhetherToSend(), true); 246 247 operationMessage.isLiveView_ = false; 248 operationMessage.notificationData.countTime = 10000; 249 ASSERT_EQ(operationMessage.DetermineWhetherToSend(), true); 250 251 operationMessage.notificationData.countTime = 10; 252 operationMessage.notificationData.time = 0; 253 ASSERT_EQ(operationMessage.DetermineWhetherToSend(), true); 254 operationMessage.ResetData(); 255 } 256 257 /** 258 * @tc.name: Operation_400 259 * @tc.desc: Test Operation. 260 * @tc.type: FUNC 261 */ 262 HWTEST_F(NotificationAnalyticsUtilTest, Operation_400, Function | SmallTest | Level1) 263 { 264 HaOperationMessage operationMessage = HaOperationMessage(true); 265 operationMessage.liveViewData.keyNode++; 266 NotificationAnalyticsUtil::ReportOperationsDotEvent(operationMessage); 267 ASSERT_EQ(operationMessage.liveViewData.keyNode, 0); 268 } 269 270 /** 271 * @tc.name: BuildAnsData_100 272 * @tc.desc: Test BuildAnsData when including . 273 * @tc.type: FUNC 274 */ 275 HWTEST_F(NotificationAnalyticsUtilTest, BuildAnsData_100, Function | SmallTest | Level1) 276 { 277 HaMetaMessage message; 278 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest(); 279 auto unifiedGroupInfo = std::make_shared<NotificationUnifiedGroupInfo>(); 280 auto extraInfo = std::make_shared<AAFwk::WantParams>(); 281 AAFwk::WantParams pushData; 282 pushData.SetParam("msgId", AAFwk::String::Box("msgId")); 283 pushData.SetParam("mcMsgId", AAFwk::String::Box("mcMsgId")); 284 extraInfo->SetParam("pushData", AAFwk::WantParamWrapper::Box(pushData)); 285 unifiedGroupInfo->SetExtraInfo(extraInfo); 286 request->SetUnifiedGroupInfo(unifiedGroupInfo); 287 auto flags = std::make_shared<NotificationFlags>(); 288 request->SetFlags(flags); 289 290 auto ret = NotificationAnalyticsUtil::BuildAnsData(request, message); 291 292 ASSERT_TRUE(ret.find("msgId") != std::string::npos); 293 ASSERT_TRUE(ret.find("mcMsgId") != std::string::npos); 294 } 295 296 /** 297 * @tc.name: GetDeviceStatus_100 298 * @tc.desc: Test GetDeviceStatus. 299 * @tc.type: FUNC 300 */ 301 HWTEST_F(NotificationAnalyticsUtilTest, GetDeviceStatus_100, Function | SmallTest | Level1) 302 { 303 HaMetaMessage message; 304 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest(); 305 std::string deviceType = "phone"; 306 std::bitset<DistributedDeviceStatus::STATUS_SIZE> bitStatus; 307 request->AdddeviceStatu(deviceType, bitStatus.bitset<DistributedDeviceStatus::STATUS_SIZE>::to_string()); 308 309 auto ret = NotificationAnalyticsUtil::GetDeviceStatus(request); 310 311 ASSERT_TRUE(ret.find("phone") != std::string::npos); 312 } 313 314 /** 315 * @tc.name: SetControlFlags_100 316 * @tc.desc: Test SetControlFlags when set controlFlags. 317 * @tc.type: FUNC 318 */ 319 HWTEST_F(NotificationAnalyticsUtilTest, SetControlFlags_100, Function | SmallTest | Level1) 320 { 321 auto flags = std::make_shared<NotificationFlags>(); 322 uint32_t controlFlags = 0; 323 flags->SetSoundEnabled(NotificationConstant::FlagStatus::OPEN); 324 flags->SetVibrationEnabled(NotificationConstant::FlagStatus::OPEN); 325 flags->SetLockScreenVisblenessEnabled(true); 326 flags->SetBannerEnabled(true); 327 328 auto ret = NotificationAnalyticsUtil::SetControlFlags(flags, controlFlags); 329 330 ASSERT_EQ(ret, (0b1111 << 10)); 331 } 332 333 /** 334 * @tc.name: SetControlFlags_200 335 * @tc.desc: Test SetControlFlags when clear controlFlags. 336 * @tc.type: FUNC 337 */ 338 HWTEST_F(NotificationAnalyticsUtilTest, SetControlFlags_200, Function | SmallTest | Level1) 339 { 340 auto flags = std::make_shared<NotificationFlags>(); 341 uint32_t controlFlags = 0b1111 << 10; 342 flags->SetSoundEnabled(NotificationConstant::FlagStatus::CLOSE); 343 flags->SetVibrationEnabled(NotificationConstant::FlagStatus::CLOSE); 344 flags->SetLockScreenVisblenessEnabled(false); 345 flags->SetBannerEnabled(false); 346 347 auto ret = NotificationAnalyticsUtil::SetControlFlags(flags, controlFlags); 348 349 ASSERT_EQ(ret, 0); 350 } 351 352 /** 353 * @tc.name: AggregateLiveView_001 354 * @tc.desc: Test AggregateLiveView 355 * @tc.type: FUNC 356 */ 357 HWTEST_F(NotificationAnalyticsUtilTest, AggregateLiveView_001, Function | SmallTest | Level1) 358 { 359 std::string bundle = "com.example.app#TAXI"; 360 int32_t status = 0; 361 362 NotificationAnalyticsUtil::AddLiveViewFailedNum(bundle, status); 363 NotificationAnalyticsUtil::AddLiveViewSuccessNum(bundle, status); 364 bundle = "com.example.app2#-99"; 365 NotificationAnalyticsUtil::AddLocalLiveViewFailedNum(bundle); 366 NotificationAnalyticsUtil::AddLocalLiveViewSuccessNum(bundle); 367 ReportCache reportCache = NotificationAnalyticsUtil::AggregateLiveView(); 368 369 EXPECT_EQ(reportCache.eventCode, 7); 370 std::string ansData = reportCache.want.GetStringParam("ansData"); 371 nlohmann::json jsonData = nlohmann::json::parse(ansData); 372 EXPECT_TRUE(jsonData["data"].is_string()); 373 EXPECT_TRUE(jsonData["startTime"].is_number_integer()); 374 EXPECT_TRUE(jsonData["endTime"].is_number_integer()); 375 } 376 377 /** 378 * @tc.name: ReportFlowControl_001 379 * @tc.desc: Test ReportFlowControl_001 380 * @tc.type: FUNC 381 */ 382 HWTEST_F(NotificationAnalyticsUtilTest, ReportFlowControl_001, Function | SmallTest | Level1) 383 { 384 auto res = NotificationAnalyticsUtil::ReportFlowControl(0); 385 EXPECT_TRUE(res); 386 } 387 388 /** 389 * @tc.name: RemoveExpired_001 390 * @tc.desc: Test RemoveExpired_001 391 * @tc.type: FUNC 392 */ 393 HWTEST_F(NotificationAnalyticsUtilTest, RemoveExpired_001, Function | SmallTest | Level1) 394 { 395 std::list<std::chrono::system_clock::time_point> list; 396 auto now = std::chrono::system_clock::now(); 397 list.push_back(now); 398 EXPECT_EQ(list.size(), 1); 399 400 NotificationAnalyticsUtil::RemoveExpired(list, now, 100); 401 EXPECT_EQ(list.size(), 1); 402 403 NotificationAnalyticsUtil::RemoveExpired(list, now, -1); 404 EXPECT_EQ(list.size(), 0); 405 } 406 407 /** 408 * @tc.name: ReportSlotEnable_001 409 * @tc.desc: Test ReportSlotEnable_001 410 * @tc.type: FUNC 411 */ 412 HWTEST_F(NotificationAnalyticsUtilTest, ReportSlotEnable_001, Function | SmallTest | Level1) 413 { 414 NotificationAnalyticsUtil::GetAllSlotMessageCache(100); 415 for (auto i = 0; i <= 20; ++i) { 416 NotificationAnalyticsUtil::ReportSlotEnable(); 417 } 418 auto res = NotificationAnalyticsUtil::ReportSlotEnable(); 419 EXPECT_FALSE(res); 420 } 421 422 /** 423 * @tc.name: GetReportSlotMessage_001 424 * @tc.desc: Test GetReportSlotMessage_001 425 * @tc.type: FUNC 426 */ 427 HWTEST_F(NotificationAnalyticsUtilTest, GetReportSlotMessage_001, Function | SmallTest | Level1) 428 { 429 std::string budleEntryKey = "1_1_1_1_1_1_1"; 430 std::string budleEntryValue = "1"; 431 ReportSlotMessage reportSlotMessage; 432 int32_t userId = 1; 433 auto res = NotificationAnalyticsUtil::GetReportSlotMessage( 434 budleEntryKey, budleEntryValue, reportSlotMessage, userId); 435 EXPECT_FALSE(res); 436 } 437 } 438 } 439