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 <fstream> 17 18 #include <gtest/gtest.h> 19 20 #include "event_monitor_handler.h" 21 #include "input_event_data_transformation.h" 22 #include "input_event_handler.h" 23 #include "mmi_log.h" 24 25 namespace OHOS { 26 namespace MMI { 27 namespace { 28 using namespace testing::ext; 29 constexpr int32_t UID_ROOT { 0 }; 30 static constexpr char PROGRAM_NAME[] = "uds_sesion_test"; 31 int32_t g_moduleType = 3; 32 int32_t g_pid = 0; 33 int32_t g_writeFd = -1; 34 constexpr size_t MAX_EVENTIDS_SIZE = 1001; 35 } // namespace 36 37 class EventMonitorHandlerTest : public testing::Test { 38 public: SetUpTestCase(void)39 static void SetUpTestCase(void) {} TearDownTestCase(void)40 static void TearDownTestCase(void) {} 41 }; 42 43 class MyInputEventConsumer : public IInputEventHandler::IInputEventConsumer { 44 public: OnInputEvent(InputHandlerType type,std::shared_ptr<KeyEvent> event) const45 void OnInputEvent(InputHandlerType type, std::shared_ptr<KeyEvent> event) const override {} OnInputEvent(InputHandlerType type,std::shared_ptr<PointerEvent> event) const46 void OnInputEvent(InputHandlerType type, std::shared_ptr<PointerEvent> event) const override {} OnInputEvent(InputHandlerType type,std::shared_ptr<AxisEvent> event) const47 void OnInputEvent(InputHandlerType type, std::shared_ptr<AxisEvent> event) const override {} 48 }; 49 50 /** 51 * @tc.name: EventMonitorHandlerTest_AddInputHandler_002 52 * @tc.desc: Verify the invalid and valid event type of AddInputHandler 53 * @tc.type: FUNC 54 * @tc.require: 55 */ 56 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_AddInputHandler_002, TestSize.Level1) 57 { 58 CALL_TEST_DEBUG; 59 EventMonitorHandler eventMonitorHandler; 60 InputHandlerType handlerType = InputHandlerType::NONE; 61 HandleEventType eventType = HANDLE_EVENT_TYPE_KEY; 62 std::shared_ptr<IInputEventHandler::IInputEventConsumer> callback = std::make_shared<MyInputEventConsumer>(); 63 int32_t ret = eventMonitorHandler.AddInputHandler(handlerType, eventType, callback); 64 EXPECT_EQ(ret, RET_OK); 65 eventType = HANDLE_EVENT_TYPE_FINGERPRINT; 66 ret = eventMonitorHandler.AddInputHandler(handlerType, eventType, callback); 67 EXPECT_EQ(ret, RET_OK); 68 } 69 70 /** 71 * @tc.name: EventMonitorHandlerTest_AddInputHandler_003 72 * @tc.desc: Verify the invalid and valid event type of AddInputHandler 73 * @tc.type: FUNC 74 * @tc.require: 75 */ 76 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_AddInputHandler_003, TestSize.Level1) 77 { 78 CALL_TEST_DEBUG; 79 EventMonitorHandler eventMonitorHandler; 80 InputHandlerType handlerType = InputHandlerType::NONE; 81 HandleEventType eventType = HANDLE_EVENT_TYPE_KEY; 82 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 83 int32_t ret = eventMonitorHandler.AddInputHandler(handlerType, eventType, session); 84 EXPECT_EQ(ret, RET_OK); 85 eventType = HANDLE_EVENT_TYPE_FINGERPRINT; 86 ret = eventMonitorHandler.AddInputHandler(handlerType, eventType, session); 87 EXPECT_EQ(ret, RET_OK); 88 } 89 90 /** 91 * @tc.name: EventMonitorHandlerTest_HandlePointerEvent 92 * @tc.desc: Test Overrides the if (OnHandleEvent(pointerEvent)) branch of the HandlePointerEvent function 93 * @tc.type: FUNC 94 * @tc.require: 95 */ 96 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_HandlePointerEvent, TestSize.Level1) 97 { 98 CALL_TEST_DEBUG; 99 EventMonitorHandler eventMonitorHandler; 100 std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create(); 101 ASSERT_NE(pointerEvent, nullptr); 102 int32_t deviceId = 1; 103 EventMonitorHandler::MonitorCollection::ConsumptionState consumptionState; 104 pointerEvent->bitwise_ = PointerEvent::EVENT_FLAG_NONE; 105 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN); 106 pointerEvent->SetDeviceId(deviceId); 107 consumptionState.isMonitorConsumed_ = true; 108 eventMonitorHandler.monitors_.states_.insert(std::make_pair(deviceId, consumptionState)); 109 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.HandlePointerEvent(pointerEvent)); 110 } 111 112 /** 113 * @tc.name: EventMonitorHandlerTest_HandleTouchEvent 114 * @tc.desc: Test Test Overrides the if (OnHandleEvent(pointerEvent)) branch of the HandleTouchEvent function 115 * @tc.type: FUNC 116 * @tc.require: 117 */ 118 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_HandleTouchEvent, TestSize.Level1) 119 { 120 CALL_TEST_DEBUG; 121 EventMonitorHandler eventMonitorHandler; 122 std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create(); 123 ASSERT_NE(pointerEvent, nullptr); 124 int32_t deviceId = 1; 125 EventMonitorHandler::MonitorCollection::ConsumptionState consumptionState; 126 pointerEvent->bitwise_ = PointerEvent::EVENT_FLAG_NONE; 127 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN); 128 pointerEvent->SetDeviceId(deviceId); 129 consumptionState.isMonitorConsumed_ = true; 130 eventMonitorHandler.monitors_.states_.insert(std::make_pair(deviceId, consumptionState)); 131 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.HandleTouchEvent(pointerEvent)); 132 } 133 134 /** 135 * @tc.name: EventMonitorHandlerTest_HandleTouchEvent_001 136 * @tc.desc: Test Overrides the if (item.GetToolType() == PointerEvent::TOOL_TYPE_KNUCKLE) branch 137 * <br> of the HandleTouchEvent function 138 * @tc.type: FUNC 139 * @tc.require: 140 */ 141 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_HandleTouchEvent_001, TestSize.Level1) 142 { 143 CALL_TEST_DEBUG; 144 EventMonitorHandler eventMonitorHandler; 145 std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create(); 146 ASSERT_NE(pointerEvent, nullptr); 147 pointerEvent->bitwise_ = PointerEvent::EVENT_FLAG_NONE; 148 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE); 149 pointerEvent->SetPointerId(1); 150 PointerEvent::PointerItem item; 151 item.SetPointerId(1); 152 item.SetToolType(PointerEvent::TOOL_TYPE_KNUCKLE); 153 pointerEvent->AddPointerItem(item); 154 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.HandleTouchEvent(pointerEvent)); 155 } 156 157 /** 158 * @tc.name: EventMonitorHandlerTest_OnHandleEvent_Key 159 * @tc.desc: Test Overrides the if (keyEvent->HasFlag(InputEvent::EVENT_FLAG_NO_MONITOR)) branch 160 * @tc.type: FUNC 161 * @tc.require: 162 */ 163 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_OnHandleEvent_Key, TestSize.Level1) 164 { 165 CALL_TEST_DEBUG; 166 EventMonitorHandler eventMonitorHandler; 167 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create(); 168 ASSERT_NE(keyEvent, nullptr); 169 keyEvent->bitwise_ = InputEvent::EVENT_FLAG_NO_MONITOR; 170 ASSERT_FALSE(eventMonitorHandler.OnHandleEvent(keyEvent)); 171 keyEvent->bitwise_ = InputEvent::EVENT_FLAG_NONE; 172 ASSERT_FALSE(eventMonitorHandler.OnHandleEvent(keyEvent)); 173 } 174 175 /** 176 * @tc.name: EventMonitorHandlerTest_OnHandleEvent_Pointer 177 * @tc.desc: Test Overrides the if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_NO_MONITOR)) branch 178 * @tc.type: FUNC 179 * @tc.require: 180 */ 181 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_OnHandleEvent_Pointer, TestSize.Level1) 182 { 183 CALL_TEST_DEBUG; 184 EventMonitorHandler eventMonitorHandler; 185 std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create(); 186 ASSERT_NE(pointerEvent, nullptr); 187 pointerEvent->bitwise_ = InputEvent::EVENT_FLAG_NO_MONITOR; 188 ASSERT_FALSE(eventMonitorHandler.OnHandleEvent(pointerEvent)); 189 } 190 191 /** 192 * @tc.name: EventMonitorHandlerTest_OnHandleEvent_Pointer_001 193 * @tc.desc: Test Overrides the if (monitors_.HandleEvent(pointerEvent)) branch 194 * @tc.type: FUNC 195 * @tc.require: 196 */ 197 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_OnHandleEvent_Pointer_001, TestSize.Level1) 198 { 199 CALL_TEST_DEBUG; 200 EventMonitorHandler eventMonitorHandler; 201 std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create(); 202 ASSERT_NE(pointerEvent, nullptr); 203 pointerEvent->bitwise_ = InputEvent::EVENT_FLAG_NONE; 204 int32_t deviceId = 1; 205 EventMonitorHandler::MonitorCollection::ConsumptionState consumptionState; 206 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN); 207 pointerEvent->SetDeviceId(deviceId); 208 consumptionState.isMonitorConsumed_ = true; 209 eventMonitorHandler.monitors_.states_.insert(std::make_pair(deviceId, consumptionState)); 210 ASSERT_TRUE(eventMonitorHandler.OnHandleEvent(pointerEvent)); 211 } 212 213 /** 214 * @tc.name: EventMonitorHandlerTest_MarkConsumed_001 215 * @tc.desc: Test Overrides the if (eventIds.find(eventId) != eventIds.cend()) branch 216 * @tc.type: FUNC 217 * @tc.require: 218 */ 219 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_MarkConsumed_001, TestSize.Level1) 220 { 221 CALL_TEST_DEBUG; 222 EventMonitorHandler eventMonitorHandler; 223 int32_t deviceId = 1; 224 int32_t eventId = 20; 225 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 226 EventMonitorHandler::SessionHandler sessionHandler { InputHandlerType::MONITOR, HANDLE_EVENT_TYPE_ALL, session }; 227 eventMonitorHandler.monitors_.monitors_.insert(sessionHandler); 228 EventMonitorHandler::MonitorCollection::ConsumptionState consumptionState; 229 consumptionState.eventIds_.insert(20); 230 consumptionState.isMonitorConsumed_ = false; 231 eventMonitorHandler.monitors_.states_.insert(std::make_pair(deviceId, consumptionState)); 232 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.monitors_.MarkConsumed(eventId, session)); 233 } 234 235 /** 236 * @tc.name: EventMonitorHandlerTest_MarkConsumed_002 237 * @tc.desc: Test Overrides the if (tIter == states_.end()) branch 238 * @tc.type: FUNC 239 * @tc.require: 240 */ 241 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_MarkConsumed_002, TestSize.Level1) 242 { 243 CALL_TEST_DEBUG; 244 EventMonitorHandler eventMonitorHandler; 245 int32_t deviceId = 1; 246 int32_t eventId = 20; 247 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 248 EventMonitorHandler::SessionHandler sessionHandler { InputHandlerType::MONITOR, HANDLE_EVENT_TYPE_ALL, session }; 249 eventMonitorHandler.monitors_.monitors_.insert(sessionHandler); 250 EventMonitorHandler::MonitorCollection::ConsumptionState consumptionState; 251 consumptionState.eventIds_.insert(10); 252 eventMonitorHandler.monitors_.states_.insert(std::make_pair(deviceId, consumptionState)); 253 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.monitors_.MarkConsumed(eventId, session)); 254 } 255 256 /** 257 * @tc.name: EventMonitorHandlerTest_MarkConsumed_003 258 * @tc.desc: Test Overrides the if (state.isMonitorConsumed_) branch 259 * @tc.type: FUNC 260 * @tc.require: 261 */ 262 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_MarkConsumed_003, TestSize.Level1) 263 { 264 CALL_TEST_DEBUG; 265 EventMonitorHandler eventMonitorHandler; 266 int32_t deviceId = 1; 267 int32_t eventId = 10; 268 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 269 EventMonitorHandler::SessionHandler sessionHandler { InputHandlerType::MONITOR, HANDLE_EVENT_TYPE_ALL, session }; 270 eventMonitorHandler.monitors_.monitors_.insert(sessionHandler); 271 EventMonitorHandler::MonitorCollection::ConsumptionState consumptionState; 272 consumptionState.eventIds_.insert(10); 273 consumptionState.isMonitorConsumed_ = true; 274 eventMonitorHandler.monitors_.states_.insert(std::make_pair(deviceId, consumptionState)); 275 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.monitors_.MarkConsumed(eventId, session)); 276 } 277 278 /** 279 * @tc.name: EventMonitorHandlerTest_HandleEvent 280 * @tc.desc: Test Overrides the if ((mon.eventType_ & HANDLE_EVENT_TYPE_KEY) == HANDLE_EVENT_TYPE_KEY) branch 281 * @tc.type: FUNC 282 * @tc.require: 283 */ 284 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_HandleEvent, TestSize.Level1) 285 { 286 CALL_TEST_DEBUG; 287 EventMonitorHandler eventMonitorHandler; 288 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create(); 289 ASSERT_NE(keyEvent, nullptr); 290 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 291 EventMonitorHandler::SessionHandler sessionHandler { InputHandlerType::MONITOR, HANDLE_EVENT_TYPE_KEY, session }; 292 eventMonitorHandler.monitors_.monitors_.insert(sessionHandler); 293 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.monitors_.HandleEvent(keyEvent)); 294 } 295 296 /** 297 * @tc.name: EventMonitorHandlerTest_HandleEvent_001 298 * @tc.desc: Test Overwrites the else branch of if ((mon.eventType_ & HANDLE_EVENT_TYPE_KEY) == HANDLE_EVENT_TYPE_KEY) 299 * @tc.type: FUNC 300 * @tc.require: 301 */ 302 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_HandleEvent_001, TestSize.Level1) 303 { 304 CALL_TEST_DEBUG; 305 EventMonitorHandler eventMonitorHandler; 306 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create(); 307 ASSERT_NE(keyEvent, nullptr); 308 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 309 EventMonitorHandler::SessionHandler sessionHandler { InputHandlerType::MONITOR, HANDLE_EVENT_TYPE_NONE, session }; 310 eventMonitorHandler.monitors_.monitors_.insert(sessionHandler); 311 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.monitors_.HandleEvent(keyEvent)); 312 } 313 314 /** 315 * @tc.name: EventMonitorHandlerTest_Monitor 316 * @tc.desc: Test Monitor 317 * @tc.type: FUNC 318 * @tc.require: 319 */ 320 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_Monitor, TestSize.Level1) 321 { 322 CALL_TEST_DEBUG; 323 EventMonitorHandler eventMonitorHandler; 324 std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create(); 325 ASSERT_NE(pointerEvent, nullptr); 326 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_UP); 327 pointerEvent->SetPointerId(1); 328 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN); 329 pointerEvent->SetButtonId(1); 330 pointerEvent->SetFingerCount(2); 331 pointerEvent->SetZOrder(100); 332 pointerEvent->SetDispatchTimes(1000); 333 PointerEvent::PointerItem item; 334 item.SetPointerId(1); 335 pointerEvent->AddPointerItem(item); 336 pointerEvent->SetHandlerEventType(HANDLE_EVENT_TYPE_POINTER); 337 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 338 EventMonitorHandler::SessionHandler sess { InputHandlerType::MONITOR, HANDLE_EVENT_TYPE_POINTER, session }; 339 eventMonitorHandler.monitors_.monitors_.insert(sess); 340 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.monitors_.Monitor(pointerEvent)); 341 342 pointerEvent->SetHandlerEventType(HANDLE_EVENT_TYPE_FINGERPRINT); 343 EventMonitorHandler::SessionHandler sesshdl { InputHandlerType::MONITOR, HANDLE_EVENT_TYPE_NONE, session }; 344 eventMonitorHandler.monitors_.monitors_.insert(sesshdl); 345 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.monitors_.Monitor(pointerEvent)); 346 } 347 348 /** 349 * @tc.name: EventMonitorHandlerTest_OnHandleEvent_001 350 * @tc.desc: Test OnHandleEvent 351 * @tc.type: FUNC 352 * @tc.require: 353 */ 354 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_OnHandleEvent_001, TestSize.Level1) 355 { 356 CALL_TEST_DEBUG; 357 EventMonitorHandler eventMonitorHandler; 358 auto keyEvent = KeyEvent::Create(); 359 eventMonitorHandler.HandleKeyEvent(keyEvent); 360 ASSERT_EQ(eventMonitorHandler.OnHandleEvent(keyEvent), false); 361 auto pointerEvent = PointerEvent::Create(); 362 eventMonitorHandler.HandlePointerEvent(pointerEvent); 363 ASSERT_EQ(eventMonitorHandler.OnHandleEvent(pointerEvent), false); 364 365 eventMonitorHandler.HandleTouchEvent(pointerEvent); 366 PointerEvent::PointerItem item; 367 item.SetDeviceId(1); 368 item.SetPointerId(0); 369 item.SetDisplayX(523); 370 item.SetDisplayY(723); 371 item.SetPressure(5); 372 pointerEvent->AddPointerItem(item); 373 item.SetDisplayY(610); 374 item.SetPointerId(1); 375 item.SetDeviceId(1); 376 item.SetPressure(7); 377 item.SetDisplayX(600); 378 pointerEvent->AddPointerItem(item); 379 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_MOVE); 380 pointerEvent->SetPointerId(1); 381 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN); 382 383 keyEvent->SetKeyCode(KeyEvent::KEYCODE_BACK); 384 keyEvent->SetActionTime(100); 385 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN); 386 keyEvent->ActionToString(KeyEvent::KEY_ACTION_DOWN); 387 keyEvent->KeyCodeToString(KeyEvent::KEYCODE_BACK); 388 KeyEvent::KeyItem part; 389 part.SetKeyCode(KeyEvent::KEYCODE_BACK); 390 part.SetDownTime(100); 391 part.SetPressed(true); 392 part.SetUnicode(0); 393 keyEvent->AddKeyItem(part); 394 395 eventMonitorHandler.HandlePointerEvent(pointerEvent); 396 eventMonitorHandler.HandleTouchEvent(pointerEvent); 397 ASSERT_EQ(eventMonitorHandler.OnHandleEvent(keyEvent), false); 398 ASSERT_EQ(eventMonitorHandler.OnHandleEvent(pointerEvent), false); 399 } 400 401 /** 402 * @tc.name: EventMonitorHandlerTest_InitSessionLostCallback_001 403 * @tc.desc: Verify the invalid and valid event type of InitSessionLostCallback 404 * @tc.type: FUNC 405 * @tc.require: 406 */ 407 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_InitSessionLostCallback_001, TestSize.Level1) 408 { 409 CALL_TEST_DEBUG; 410 EventMonitorHandler eventMonitorHandler; 411 eventMonitorHandler.sessionLostCallbackInitialized_ = true; 412 eventMonitorHandler.InitSessionLostCallback(); 413 eventMonitorHandler.sessionLostCallbackInitialized_ = false; 414 UDSServer udSever; 415 InputHandler->udsServer_ = &udSever; 416 auto udsServerPtr = InputHandler->GetUDSServer(); 417 EXPECT_NE(udsServerPtr, nullptr); 418 eventMonitorHandler.InitSessionLostCallback(); 419 InputHandler->udsServer_ = nullptr; 420 } 421 422 /** 423 * @tc.name: EventMonitorHandlerTest_AddInputHandler_001 424 * @tc.desc: Verify the invalid and valid event type of AddInputHandler 425 * @tc.type: FUNC 426 * @tc.require: 427 */ 428 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_AddInputHandler_001, TestSize.Level1) 429 { 430 CALL_TEST_DEBUG; 431 EventMonitorHandler eventMonitorHandler; 432 InputHandlerType handlerType = InputHandlerType::NONE; 433 HandleEventType eventType = 0; 434 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 435 int32_t ret = eventMonitorHandler.AddInputHandler(handlerType, eventType, session); 436 EXPECT_EQ(ret, RET_ERR); 437 eventType = 1; 438 ret = eventMonitorHandler.AddInputHandler(handlerType, eventType, session); 439 EXPECT_EQ(ret, RET_OK); 440 } 441 442 /** 443 * @tc.name: EventMonitorHandlerTest_RemoveInputHandler_001 444 * @tc.desc: Verify the invalid and valid event type of RemoveInputHandler 445 * @tc.type: FUNC 446 * @tc.require: 447 */ 448 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_RemoveInputHandler_001, TestSize.Level1) 449 { 450 CALL_TEST_DEBUG; 451 EventMonitorHandler eventMonitorHandler; 452 InputHandlerType handlerType = InputHandlerType::NONE; 453 HandleEventType eventType = 1; 454 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 455 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.RemoveInputHandler(handlerType, eventType, session)); 456 handlerType = InputHandlerType::MONITOR; 457 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.RemoveInputHandler(handlerType, eventType, session)); 458 } 459 460 /** 461 * @tc.name: EventMonitorHandlerTest_SendToClient_001 462 * @tc.desc: Verify the keyEvent and pointerEvent of SendToClient 463 * @tc.type: FUNC 464 * @tc.require: 465 */ 466 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_SendToClient_001, TestSize.Level1) 467 { 468 CALL_TEST_DEBUG; 469 InputHandlerType handlerType = InputHandlerType::NONE; 470 HandleEventType eventType = 0; 471 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 472 EventMonitorHandler::SessionHandler sessionHandler { handlerType, eventType, session }; 473 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create(); 474 NetPacket keyEventPkt(MmiMessageId::REPORT_KEY_EVENT); 475 keyEventPkt << InputHandlerType::MONITOR << static_cast<uint32_t>(evdev_device_udev_tags::EVDEV_UDEV_TAG_INPUT); 476 ASSERT_NO_FATAL_FAILURE(sessionHandler.SendToClient(keyEvent, keyEventPkt)); 477 478 NetPacket pointerEventPkt(MmiMessageId::REPORT_POINTER_EVENT); 479 std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create(); 480 pointerEventPkt << InputHandlerType::MONITOR << static_cast<uint32_t>(evdev_device_udev_tags::EVDEV_UDEV_TAG_INPUT); 481 InputEventDataTransformation::Marshalling(pointerEvent, pointerEventPkt); 482 ASSERT_NO_FATAL_FAILURE(sessionHandler.SendToClient(pointerEvent, pointerEventPkt)); 483 } 484 485 /** 486 * @tc.name: EventMonitorHandlerTest_AddMonitor_001 487 * @tc.desc: Verify the invalid and valid event type of AddMonitor 488 * @tc.type: FUNC 489 * @tc.require: 490 */ 491 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_AddMonitor_001, TestSize.Level1) 492 { 493 CALL_TEST_DEBUG; 494 EventMonitorHandler::MonitorCollection monitorCollection; 495 InputHandlerType handlerType = InputHandlerType::NONE; 496 HandleEventType eventType = 0; 497 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 498 EventMonitorHandler::SessionHandler sessionHandler { handlerType, eventType, session }; 499 for (int i = 0; i < MAX_N_INPUT_MONITORS - 1; i++) { 500 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 501 EventMonitorHandler::SessionHandler sessionHandler = { handlerType, eventType, session }; 502 monitorCollection.monitors_.insert(sessionHandler); 503 } 504 int32_t ret = monitorCollection.AddMonitor(sessionHandler); 505 EXPECT_EQ(ret, RET_OK); 506 SessionPtr session2 = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 507 EventMonitorHandler::SessionHandler sessionHandler2 { handlerType, eventType, session2 }; 508 monitorCollection.monitors_.insert(sessionHandler2); 509 ret = monitorCollection.AddMonitor(sessionHandler2); 510 EXPECT_EQ(ret, RET_ERR); 511 } 512 513 /** 514 * @tc.name: EventMonitorHandlerTest_RemoveMonitor_001 515 * @tc.desc: Verify the invalid and valid event type of RemoveMonitor 516 * @tc.type: FUNC 517 * @tc.require: 518 */ 519 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_RemoveMonitor_001, TestSize.Level1) 520 { 521 CALL_TEST_DEBUG; 522 EventMonitorHandler::MonitorCollection monitorCollection; 523 InputHandlerType handlerType = InputHandlerType::NONE; 524 HandleEventType eventType = 0; 525 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 526 EventMonitorHandler::SessionHandler sessionHandler { handlerType, eventType, session }; 527 ASSERT_NO_FATAL_FAILURE(monitorCollection.RemoveMonitor(sessionHandler)); 528 monitorCollection.monitors_.insert(sessionHandler); 529 ASSERT_NO_FATAL_FAILURE(monitorCollection.RemoveMonitor(sessionHandler)); 530 SessionPtr session2 = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 531 eventType = 1; 532 sessionHandler = { handlerType, eventType, session2 }; 533 monitorCollection.monitors_.insert(sessionHandler); 534 ASSERT_NO_FATAL_FAILURE(monitorCollection.RemoveMonitor(sessionHandler)); 535 } 536 537 /** 538 * @tc.name: EventMonitorHandlerTest_MarkConsumed 539 * @tc.desc: Test MarkConsumed 540 * @tc.type: FUNC 541 * @tc.require: 542 */ 543 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_MarkConsumed, TestSize.Level1) 544 { 545 CALL_TEST_DEBUG; 546 EventMonitorHandler eventMonitorHandler; 547 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 548 int32_t eventId = 100; 549 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.MarkConsumed(eventId, session)); 550 } 551 552 /** 553 * @tc.name: EventMonitorHandlerTest_OnSessionLost 554 * @tc.desc: Test OnSessionLost 555 * @tc.type: FUNC 556 * @tc.require: 557 */ 558 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_OnSessionLost, TestSize.Level1) 559 { 560 CALL_TEST_DEBUG; 561 EventMonitorHandler eventMonitorHandler; 562 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 563 EventMonitorHandler::SessionHandler sessionHandler { InputHandlerType::MONITOR, HANDLE_EVENT_TYPE_KEY, session }; 564 eventMonitorHandler.monitors_.monitors_.insert(sessionHandler); 565 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.OnSessionLost(session)); 566 567 eventMonitorHandler.monitors_.monitors_.insert(sessionHandler); 568 session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 569 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.OnSessionLost(session)); 570 } 571 572 /** 573 * @tc.name: EventMonitorHandlerTest_HasMonitor 574 * @tc.desc: Test HasMonitor 575 * @tc.type: FUNC 576 * @tc.require: 577 */ 578 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_HasMonitor, TestSize.Level1) 579 { 580 CALL_TEST_DEBUG; 581 EventMonitorHandler::MonitorCollection monitorCollection; 582 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 583 EventMonitorHandler::SessionHandler monitor { InputHandlerType::MONITOR, HANDLE_EVENT_TYPE_ALL, session }; 584 monitorCollection.monitors_.insert(monitor); 585 ASSERT_TRUE(monitorCollection.HasMonitor(session)); 586 } 587 588 /** 589 * @tc.name: EventMonitorHandlerTest_UpdateConsumptionState 590 * @tc.desc: Test UpdateConsumptionState 591 * @tc.type: FUNC 592 * @tc.require: 593 */ 594 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_UpdateConsumptionState, TestSize.Level1) 595 { 596 CALL_TEST_DEBUG; 597 int32_t deviceId = 6; 598 EventMonitorHandler::MonitorCollection monitorCollection; 599 std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create(); 600 ASSERT_NE(pointerEvent, nullptr); 601 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHPAD); 602 pointerEvent->SetDeviceId(deviceId); 603 EventMonitorHandler::MonitorCollection::ConsumptionState state; 604 for (int32_t i = 0; i <= MAX_EVENTIDS_SIZE; ++i) { 605 state.eventIds_.insert(i); 606 } 607 monitorCollection.states_.insert(std::make_pair(deviceId, state)); 608 PointerEvent::PointerItem item; 609 item.SetDeviceId(1); 610 pointerEvent->AddPointerItem(item); 611 pointerEvent->SetId(1); 612 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_SWIPE_BEGIN); 613 ASSERT_NO_FATAL_FAILURE(monitorCollection.UpdateConsumptionState(pointerEvent)); 614 615 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_SWIPE_END); 616 ASSERT_NO_FATAL_FAILURE(monitorCollection.UpdateConsumptionState(pointerEvent)); 617 } 618 619 /** 620 * @tc.name: EventMonitorHandlerTest_OnScreenCaptureFinished_001 621 * @tc.desc: Test OnScreenCaptureFinished 622 * @tc.type: FUNC 623 * @tc.require: 624 */ 625 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_OnScreenCaptureFinished_001, TestSize.Level1) 626 { 627 CALL_TEST_DEBUG; 628 EventMonitorHandler eventMonitorHandler; 629 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 630 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.OnScreenCaptureFinished(session)); 631 EventMonitorHandler::MonitorCollection monitorCollection; 632 EventMonitorHandler::SessionHandler monitor { InputHandlerType::MONITOR, HANDLE_EVENT_TYPE_ALL, session }; 633 monitorCollection.monitors_.insert(monitor); 634 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.OnScreenCaptureFinished(session)); 635 } 636 637 /** 638 * @tc.name: EventMonitorHandlerTest_OnScreenCaptureStarted_001 639 * @tc.desc: Test OnScreenCaptureStarted 640 * @tc.type: FUNC 641 * @tc.require: 642 */ 643 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_OnScreenCaptureStarted_001, TestSize.Level1) 644 { 645 CALL_TEST_DEBUG; 646 EventMonitorHandler eventMonitorHandler; 647 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 648 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.OnScreenCaptureStarted(session)); 649 EventMonitorHandler::MonitorCollection monitorCollection; 650 EventMonitorHandler::SessionHandler monitor { InputHandlerType::MONITOR, HANDLE_EVENT_TYPE_ALL, session }; 651 monitorCollection.monitors_.insert(monitor); 652 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.OnScreenCaptureStarted(session)); 653 } 654 655 /** 656 * @tc.name: EventMonitorHandlerTest_OnScreenCaptureStarted_002 657 * @tc.desc: Test OnScreenCaptureStarted 658 * @tc.type: FUNC 659 * @tc.require: 660 */ 661 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_OnScreenCaptureStarted_002, TestSize.Level1) 662 { 663 CALL_TEST_DEBUG; 664 EventMonitorHandler eventMonitorHandler; 665 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 666 EventMonitorHandler::MonitorCollection monitorCollection; 667 EventMonitorHandler::SessionHandler handler { InputHandlerType::MONITOR, HANDLE_EVENT_TYPE_ALL, session }; 668 std::set<EventMonitorHandler::SessionHandler> handlerSet; 669 handlerSet.insert(handler); 670 monitorCollection.endScreenCaptureMonitors_[-1] = handlerSet; 671 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.OnScreenCaptureStarted(session)); 672 } 673 674 /** 675 * @tc.name: EventMonitorHandlerTest_Dump_001 676 * @tc.desc: Test Dump 677 * @tc.type: FUNC 678 * @tc.require: 679 */ 680 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_Dump_001, TestSize.Level1) 681 { 682 CALL_TEST_DEBUG; 683 EventMonitorHandler eventMonitorHandler; 684 int32_t fd = 1; 685 std::vector<std::string> args; 686 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.Dump(fd, args)); 687 } 688 689 /** 690 * @tc.name: EventMonitorHandlerTest_OnSessionLost_001 691 * @tc.desc: Test OnSessionLost 692 * @tc.type: FUNC 693 * @tc.require: 694 */ 695 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_OnSessionLost_001, TestSize.Level1) 696 { 697 CALL_TEST_DEBUG; 698 EventMonitorHandler eventMonitorHandler; 699 EventMonitorHandler::MonitorCollection monitorCollection; 700 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 701 EventMonitorHandler::SessionHandler sessionHandler { InputHandlerType::MONITOR, HANDLE_EVENT_TYPE_KEY, session }; 702 eventMonitorHandler.monitors_.monitors_.insert(sessionHandler); 703 std::set<EventMonitorHandler::SessionHandler> handlerSet; 704 handlerSet.insert(sessionHandler); 705 monitorCollection.endScreenCaptureMonitors_[-1] = handlerSet; 706 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.OnSessionLost(session)); 707 eventMonitorHandler.monitors_.monitors_.insert(sessionHandler); 708 session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 709 handlerSet.insert(sessionHandler); 710 monitorCollection.endScreenCaptureMonitors_[-1] = handlerSet; 711 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.OnSessionLost(session)); 712 } 713 714 /** 715 * @tc.name: EventMonitorHandlerTest_RecoveryScreenCaptureMonitor_001 716 * @tc.desc: Test RecoveryScreenCaptureMonitor 717 * @tc.type: FUNC 718 * @tc.require: 719 */ 720 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_RecoveryScreenCaptureMonitor_001, TestSize.Level1) 721 { 722 CALL_TEST_DEBUG; 723 EventMonitorHandler::MonitorCollection monitorCollection; 724 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 725 session->tokenType_ = TokenType::TOKEN_SHELL; 726 ASSERT_NO_FATAL_FAILURE(monitorCollection.RecoveryScreenCaptureMonitor(session)); 727 session->tokenType_ = TokenType::TOKEN_HAP; 728 ASSERT_NO_FATAL_FAILURE(monitorCollection.RecoveryScreenCaptureMonitor(session)); 729 730 EventMonitorHandler::SessionHandler handler { InputHandlerType::MONITOR, HANDLE_EVENT_TYPE_ALL, session }; 731 std::set<EventMonitorHandler::SessionHandler> handlerSet; 732 handlerSet.insert(handler); 733 monitorCollection.endScreenCaptureMonitors_[-1] = handlerSet; 734 ASSERT_NO_FATAL_FAILURE(monitorCollection.RecoveryScreenCaptureMonitor(session)); 735 } 736 737 /** 738 * @tc.name: EventMonitorHandlerTest_RemoveScreenCaptureMonitor_001 739 * @tc.desc: Test RemoveScreenCaptureMonitor 740 * @tc.type: FUNC 741 * @tc.require: 742 */ 743 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_RemoveScreenCaptureMonitor_001, TestSize.Level1) 744 { 745 CALL_TEST_DEBUG; 746 EventMonitorHandler::MonitorCollection monitorCollection; 747 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 748 session->tokenType_ = TokenType::TOKEN_SHELL; 749 ASSERT_NO_FATAL_FAILURE(monitorCollection.RemoveScreenCaptureMonitor(session)); 750 session->tokenType_ = TokenType::TOKEN_HAP; 751 ASSERT_NO_FATAL_FAILURE(monitorCollection.RemoveScreenCaptureMonitor(session)); 752 EventMonitorHandler::SessionHandler handler { InputHandlerType::MONITOR, HANDLE_EVENT_TYPE_ALL, session }; 753 std::set<EventMonitorHandler::SessionHandler> handlerSet; 754 handlerSet.insert(handler); 755 monitorCollection.endScreenCaptureMonitors_[-1] = handlerSet; 756 ASSERT_NO_FATAL_FAILURE(monitorCollection.RemoveScreenCaptureMonitor(session)); 757 } 758 } // namespace MMI 759 } // namespace OHOS 760