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 constexpr int32_t REMOVE_OBSERVER { -2 }; 36 constexpr int32_t UNOBSERVED { -1 }; 37 constexpr int32_t ACTIVE_EVENT { 2 }; 38 constexpr int32_t THREE_FINGERS { 3 }; 39 constexpr int32_t FOUR_FINGERS { 4 }; 40 } // namespace 41 42 class EventMonitorHandlerTest : public testing::Test { 43 public: SetUpTestCase(void)44 static void SetUpTestCase(void) {} TearDownTestCase(void)45 static void TearDownTestCase(void) {} 46 }; 47 48 class MyInputEventConsumer : public IInputEventHandler::IInputEventConsumer { 49 public: OnInputEvent(InputHandlerType type,std::shared_ptr<KeyEvent> event) const50 void OnInputEvent(InputHandlerType type, std::shared_ptr<KeyEvent> event) const override {} OnInputEvent(InputHandlerType type,std::shared_ptr<PointerEvent> event) const51 void OnInputEvent(InputHandlerType type, std::shared_ptr<PointerEvent> event) const override {} OnInputEvent(InputHandlerType type,std::shared_ptr<AxisEvent> event) const52 void OnInputEvent(InputHandlerType type, std::shared_ptr<AxisEvent> event) const override {} 53 }; 54 55 /** 56 * @tc.name: EventMonitorHandlerTest_AddInputHandler_002 57 * @tc.desc: Verify the invalid and valid event type of AddInputHandler 58 * @tc.type: FUNC 59 * @tc.require: 60 */ 61 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_AddInputHandler_002, TestSize.Level1) 62 { 63 CALL_TEST_DEBUG; 64 EventMonitorHandler eventMonitorHandler; 65 InputHandlerType handlerType = InputHandlerType::NONE; 66 HandleEventType eventType = HANDLE_EVENT_TYPE_KEY; 67 std::shared_ptr<IInputEventHandler::IInputEventConsumer> callback = std::make_shared<MyInputEventConsumer>(); 68 int32_t ret = eventMonitorHandler.AddInputHandler(handlerType, eventType, callback); 69 EXPECT_EQ(ret, RET_OK); 70 eventType = HANDLE_EVENT_TYPE_NONE; 71 ret = eventMonitorHandler.AddInputHandler(handlerType, eventType, callback); 72 EXPECT_NE(ret, RET_OK); 73 } 74 75 /** 76 * @tc.name: EventMonitorHandlerTest_AddInputHandler_003 77 * @tc.desc: Verify the invalid and valid event type of AddInputHandler 78 * @tc.type: FUNC 79 * @tc.require: 80 */ 81 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_AddInputHandler_003, TestSize.Level1) 82 { 83 CALL_TEST_DEBUG; 84 EventMonitorHandler eventMonitorHandler; 85 InputHandlerType handlerType = InputHandlerType::NONE; 86 HandleEventType eventType = HANDLE_EVENT_TYPE_KEY; 87 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 88 int32_t ret = eventMonitorHandler.AddInputHandler(handlerType, eventType, session); 89 EXPECT_EQ(ret, RET_OK); 90 eventType = HANDLE_EVENT_TYPE_FINGERPRINT; 91 ret = eventMonitorHandler.AddInputHandler(handlerType, eventType, session); 92 EXPECT_EQ(ret, RET_OK); 93 } 94 95 /** 96 * @tc.name: EventMonitorHandlerTest_HandlePointerEvent 97 * @tc.desc: Test Overrides the if (OnHandleEvent(pointerEvent)) branch of the HandlePointerEvent function 98 * @tc.type: FUNC 99 * @tc.require: 100 */ 101 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_HandlePointerEvent, TestSize.Level1) 102 { 103 CALL_TEST_DEBUG; 104 EventMonitorHandler eventMonitorHandler; 105 std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create(); 106 ASSERT_NE(pointerEvent, nullptr); 107 int32_t deviceId = 1; 108 EventMonitorHandler::MonitorCollection::ConsumptionState consumptionState; 109 pointerEvent->bitwise_ = PointerEvent::EVENT_FLAG_NONE; 110 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN); 111 pointerEvent->SetDeviceId(deviceId); 112 consumptionState.isMonitorConsumed_ = true; 113 eventMonitorHandler.monitors_.states_.insert(std::make_pair(deviceId, consumptionState)); 114 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.HandlePointerEvent(pointerEvent)); 115 } 116 117 /** 118 * @tc.name: EventMonitorHandlerTest_HandleTouchEvent 119 * @tc.desc: Test Test Overrides the if (OnHandleEvent(pointerEvent)) branch of the HandleTouchEvent function 120 * @tc.type: FUNC 121 * @tc.require: 122 */ 123 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_HandleTouchEvent, TestSize.Level1) 124 { 125 CALL_TEST_DEBUG; 126 EventMonitorHandler eventMonitorHandler; 127 std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create(); 128 ASSERT_NE(pointerEvent, nullptr); 129 int32_t deviceId = 1; 130 EventMonitorHandler::MonitorCollection::ConsumptionState consumptionState; 131 pointerEvent->bitwise_ = PointerEvent::EVENT_FLAG_NONE; 132 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN); 133 pointerEvent->SetDeviceId(deviceId); 134 consumptionState.isMonitorConsumed_ = true; 135 eventMonitorHandler.monitors_.states_.insert(std::make_pair(deviceId, consumptionState)); 136 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.HandleTouchEvent(pointerEvent)); 137 } 138 139 /** 140 * @tc.name: EventMonitorHandlerTest_HandleTouchEvent_001 141 * @tc.desc: Test Overrides the if (item.GetToolType() == PointerEvent::TOOL_TYPE_KNUCKLE) branch 142 * <br> of the HandleTouchEvent function 143 * @tc.type: FUNC 144 * @tc.require: 145 */ 146 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_HandleTouchEvent_001, TestSize.Level1) 147 { 148 CALL_TEST_DEBUG; 149 EventMonitorHandler eventMonitorHandler; 150 std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create(); 151 ASSERT_NE(pointerEvent, nullptr); 152 pointerEvent->bitwise_ = PointerEvent::EVENT_FLAG_NONE; 153 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE); 154 pointerEvent->SetPointerId(1); 155 PointerEvent::PointerItem item; 156 item.SetPointerId(1); 157 item.SetToolType(PointerEvent::TOOL_TYPE_KNUCKLE); 158 pointerEvent->AddPointerItem(item); 159 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.HandleTouchEvent(pointerEvent)); 160 } 161 162 /** 163 * @tc.name: EventMonitorHandlerTest_OnHandleEvent_Key 164 * @tc.desc: Test Overrides the if (keyEvent->HasFlag(InputEvent::EVENT_FLAG_NO_MONITOR)) branch 165 * @tc.type: FUNC 166 * @tc.require: 167 */ 168 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_OnHandleEvent_Key, TestSize.Level1) 169 { 170 CALL_TEST_DEBUG; 171 EventMonitorHandler eventMonitorHandler; 172 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create(); 173 ASSERT_NE(keyEvent, nullptr); 174 keyEvent->bitwise_ = InputEvent::EVENT_FLAG_NO_MONITOR; 175 ASSERT_FALSE(eventMonitorHandler.OnHandleEvent(keyEvent)); 176 keyEvent->bitwise_ = InputEvent::EVENT_FLAG_NONE; 177 ASSERT_FALSE(eventMonitorHandler.OnHandleEvent(keyEvent)); 178 } 179 180 /** 181 * @tc.name: EventMonitorHandlerTest_OnHandleEvent_Pointer 182 * @tc.desc: Test Overrides the if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_NO_MONITOR)) branch 183 * @tc.type: FUNC 184 * @tc.require: 185 */ 186 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_OnHandleEvent_Pointer, TestSize.Level1) 187 { 188 CALL_TEST_DEBUG; 189 EventMonitorHandler eventMonitorHandler; 190 std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create(); 191 ASSERT_NE(pointerEvent, nullptr); 192 pointerEvent->bitwise_ = InputEvent::EVENT_FLAG_NO_MONITOR; 193 ASSERT_FALSE(eventMonitorHandler.OnHandleEvent(pointerEvent)); 194 } 195 196 /** 197 * @tc.name: EventMonitorHandlerTest_OnHandleEvent_Pointer_001 198 * @tc.desc: Test Overrides the if (monitors_.HandleEvent(pointerEvent)) branch 199 * @tc.type: FUNC 200 * @tc.require: 201 */ 202 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_OnHandleEvent_Pointer_001, TestSize.Level1) 203 { 204 CALL_TEST_DEBUG; 205 EventMonitorHandler eventMonitorHandler; 206 std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create(); 207 ASSERT_NE(pointerEvent, nullptr); 208 pointerEvent->bitwise_ = InputEvent::EVENT_FLAG_NONE; 209 int32_t deviceId = 1; 210 EventMonitorHandler::MonitorCollection::ConsumptionState consumptionState; 211 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN); 212 pointerEvent->SetDeviceId(deviceId); 213 consumptionState.isMonitorConsumed_ = true; 214 eventMonitorHandler.monitors_.states_.insert(std::make_pair(deviceId, consumptionState)); 215 ASSERT_TRUE(eventMonitorHandler.OnHandleEvent(pointerEvent)); 216 } 217 218 /** 219 * @tc.name: EventMonitorHandlerTest_MarkConsumed_001 220 * @tc.desc: Test Overrides the if (eventIds.find(eventId) != eventIds.cend()) branch 221 * @tc.type: FUNC 222 * @tc.require: 223 */ 224 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_MarkConsumed_001, TestSize.Level1) 225 { 226 CALL_TEST_DEBUG; 227 EventMonitorHandler eventMonitorHandler; 228 int32_t deviceId = 1; 229 int32_t eventId = 20; 230 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 231 EventMonitorHandler::SessionHandler sessionHandler { InputHandlerType::MONITOR, HANDLE_EVENT_TYPE_ALL, session }; 232 eventMonitorHandler.monitors_.monitors_.insert(sessionHandler); 233 EventMonitorHandler::MonitorCollection::ConsumptionState consumptionState; 234 consumptionState.eventIds_.insert(20); 235 consumptionState.isMonitorConsumed_ = false; 236 eventMonitorHandler.monitors_.states_.insert(std::make_pair(deviceId, consumptionState)); 237 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.monitors_.MarkConsumed(eventId, session)); 238 } 239 240 /** 241 * @tc.name: EventMonitorHandlerTest_MarkConsumed_002 242 * @tc.desc: Test Overrides the if (tIter == states_.end()) branch 243 * @tc.type: FUNC 244 * @tc.require: 245 */ 246 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_MarkConsumed_002, TestSize.Level1) 247 { 248 CALL_TEST_DEBUG; 249 EventMonitorHandler eventMonitorHandler; 250 int32_t deviceId = 1; 251 int32_t eventId = 20; 252 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 253 EventMonitorHandler::SessionHandler sessionHandler { InputHandlerType::MONITOR, HANDLE_EVENT_TYPE_ALL, session }; 254 eventMonitorHandler.monitors_.monitors_.insert(sessionHandler); 255 EventMonitorHandler::MonitorCollection::ConsumptionState consumptionState; 256 consumptionState.eventIds_.insert(10); 257 eventMonitorHandler.monitors_.states_.insert(std::make_pair(deviceId, consumptionState)); 258 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.monitors_.MarkConsumed(eventId, session)); 259 } 260 261 /** 262 * @tc.name: EventMonitorHandlerTest_MarkConsumed_003 263 * @tc.desc: Test Overrides the if (state.isMonitorConsumed_) branch 264 * @tc.type: FUNC 265 * @tc.require: 266 */ 267 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_MarkConsumed_003, TestSize.Level1) 268 { 269 CALL_TEST_DEBUG; 270 EventMonitorHandler eventMonitorHandler; 271 int32_t deviceId = 1; 272 int32_t eventId = 10; 273 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 274 EventMonitorHandler::SessionHandler sessionHandler { InputHandlerType::MONITOR, HANDLE_EVENT_TYPE_ALL, session }; 275 eventMonitorHandler.monitors_.monitors_.insert(sessionHandler); 276 EventMonitorHandler::MonitorCollection::ConsumptionState consumptionState; 277 consumptionState.eventIds_.insert(10); 278 consumptionState.isMonitorConsumed_ = true; 279 eventMonitorHandler.monitors_.states_.insert(std::make_pair(deviceId, consumptionState)); 280 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.monitors_.MarkConsumed(eventId, session)); 281 } 282 283 /** 284 * @tc.name: EventMonitorHandlerTest_HandleEvent 285 * @tc.desc: Test Overrides the if ((mon.eventType_ & HANDLE_EVENT_TYPE_KEY) == HANDLE_EVENT_TYPE_KEY) branch 286 * @tc.type: FUNC 287 * @tc.require: 288 */ 289 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_HandleEvent, TestSize.Level1) 290 { 291 CALL_TEST_DEBUG; 292 EventMonitorHandler eventMonitorHandler; 293 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create(); 294 ASSERT_NE(keyEvent, nullptr); 295 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 296 EventMonitorHandler::SessionHandler sessionHandler { InputHandlerType::MONITOR, HANDLE_EVENT_TYPE_KEY, session }; 297 eventMonitorHandler.monitors_.monitors_.insert(sessionHandler); 298 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.monitors_.HandleEvent(keyEvent)); 299 } 300 301 /** 302 * @tc.name: EventMonitorHandlerTest_HandleEvent_001 303 * @tc.desc: Test Overwrites the else branch of if ((mon.eventType_ & HANDLE_EVENT_TYPE_KEY) == HANDLE_EVENT_TYPE_KEY) 304 * @tc.type: FUNC 305 * @tc.require: 306 */ 307 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_HandleEvent_001, TestSize.Level1) 308 { 309 CALL_TEST_DEBUG; 310 EventMonitorHandler eventMonitorHandler; 311 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create(); 312 ASSERT_NE(keyEvent, nullptr); 313 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 314 EventMonitorHandler::SessionHandler sessionHandler { InputHandlerType::MONITOR, HANDLE_EVENT_TYPE_NONE, session }; 315 eventMonitorHandler.monitors_.monitors_.insert(sessionHandler); 316 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.monitors_.HandleEvent(keyEvent)); 317 } 318 319 /** 320 * @tc.name: EventMonitorHandlerTest_HandleEvent_002 321 * @tc.desc: Test HandleEvent 322 * @tc.type: FUNC 323 * @tc.require: 324 */ 325 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_HandleEvent_002, TestSize.Level1) 326 { 327 CALL_TEST_DEBUG; 328 EventMonitorHandler eventMonitorHandler; 329 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create(); 330 ASSERT_NE(keyEvent, nullptr); 331 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 332 EventMonitorHandler::SessionHandler sessionHandler { InputHandlerType::MONITOR, HANDLE_EVENT_TYPE_NONE, session }; 333 eventMonitorHandler.monitors_.monitors_.insert(sessionHandler); 334 335 NapProcess::GetInstance()->napClientPid_ = ACTIVE_EVENT; 336 OHOS::MMI::NapProcess::NapStatusData napData; 337 napData.pid = 2; 338 napData.uid = 3; 339 napData.bundleName = "programName"; 340 EXPECT_FALSE(NapProcess::GetInstance()->IsNeedNotify(napData)); 341 bool ret = eventMonitorHandler.monitors_.HandleEvent(keyEvent); 342 EXPECT_FALSE(ret); 343 } 344 345 /** 346 * @tc.name: EventMonitorHandlerTest_HandleEvent_003 347 * @tc.desc: Test HandleEvent 348 * @tc.type: FUNC 349 * @tc.require: 350 */ 351 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_HandleEvent_003, TestSize.Level1) 352 { 353 CALL_TEST_DEBUG; 354 EventMonitorHandler eventMonitorHandler; 355 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create(); 356 ASSERT_NE(keyEvent, nullptr); 357 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 358 EventMonitorHandler::SessionHandler sessionHandler { InputHandlerType::MONITOR, HANDLE_EVENT_TYPE_NONE, session }; 359 eventMonitorHandler.monitors_.monitors_.insert(sessionHandler); 360 361 NapProcess::GetInstance()->napClientPid_ = REMOVE_OBSERVER; 362 bool ret = eventMonitorHandler.monitors_.HandleEvent(keyEvent); 363 EXPECT_FALSE(ret); 364 } 365 366 /** 367 * @tc.name: EventMonitorHandlerTest_HandleEvent_004 368 * @tc.desc: Test HandleEvent 369 * @tc.type: FUNC 370 * @tc.require: 371 */ 372 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_HandleEvent_004, TestSize.Level1) 373 { 374 CALL_TEST_DEBUG; 375 EventMonitorHandler eventMonitorHandler; 376 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create(); 377 ASSERT_NE(keyEvent, nullptr); 378 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 379 EventMonitorHandler::SessionHandler sessionHandler { InputHandlerType::MONITOR, HANDLE_EVENT_TYPE_NONE, session }; 380 eventMonitorHandler.monitors_.monitors_.insert(sessionHandler); 381 382 NapProcess::GetInstance()->napClientPid_ = UNOBSERVED; 383 bool ret = eventMonitorHandler.monitors_.HandleEvent(keyEvent); 384 EXPECT_FALSE(ret); 385 } 386 387 /** 388 * @tc.name: EventMonitorHandlerTest_Monitor 389 * @tc.desc: Test Monitor 390 * @tc.type: FUNC 391 * @tc.require: 392 */ 393 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_Monitor, TestSize.Level1) 394 { 395 CALL_TEST_DEBUG; 396 EventMonitorHandler eventMonitorHandler; 397 std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create(); 398 ASSERT_NE(pointerEvent, nullptr); 399 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_UP); 400 pointerEvent->SetPointerId(1); 401 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN); 402 pointerEvent->SetButtonId(1); 403 pointerEvent->SetFingerCount(2); 404 pointerEvent->SetZOrder(100); 405 pointerEvent->SetDispatchTimes(1000); 406 PointerEvent::PointerItem item; 407 item.SetPointerId(1); 408 pointerEvent->AddPointerItem(item); 409 pointerEvent->SetHandlerEventType(HANDLE_EVENT_TYPE_POINTER); 410 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 411 EventMonitorHandler::SessionHandler sess { InputHandlerType::MONITOR, HANDLE_EVENT_TYPE_POINTER, session }; 412 eventMonitorHandler.monitors_.monitors_.insert(sess); 413 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.monitors_.Monitor(pointerEvent)); 414 415 pointerEvent->SetHandlerEventType(HANDLE_EVENT_TYPE_FINGERPRINT); 416 EventMonitorHandler::SessionHandler sesshdl { InputHandlerType::MONITOR, HANDLE_EVENT_TYPE_NONE, session }; 417 eventMonitorHandler.monitors_.monitors_.insert(sesshdl); 418 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.monitors_.Monitor(pointerEvent)); 419 } 420 421 /** 422 * @tc.name: EventMonitorHandlerTest_Monitor_01 423 * @tc.desc: Test Monitor 424 * @tc.type: FUNC 425 * @tc.require: 426 */ 427 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_Monitor_01, TestSize.Level1) 428 { 429 CALL_TEST_DEBUG; 430 EventMonitorHandler eventMonitorHandler; 431 std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create(); 432 ASSERT_NE(pointerEvent, nullptr); 433 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_UP); 434 pointerEvent->SetPointerId(1); 435 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN); 436 pointerEvent->SetButtonId(1); 437 pointerEvent->SetFingerCount(2); 438 pointerEvent->SetZOrder(100); 439 pointerEvent->SetDispatchTimes(1000); 440 PointerEvent::PointerItem item; 441 item.SetPointerId(1); 442 pointerEvent->AddPointerItem(item); 443 444 pointerEvent->SetHandlerEventType(HANDLE_EVENT_TYPE_FINGERPRINT); 445 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 446 EventMonitorHandler::SessionHandler sess { InputHandlerType::MONITOR, HANDLE_EVENT_TYPE_NONE, session }; 447 eventMonitorHandler.monitors_.monitors_.insert(sess); 448 449 NapProcess::GetInstance()->napClientPid_ = ACTIVE_EVENT; 450 OHOS::MMI::NapProcess::NapStatusData napData; 451 napData.pid = 2; 452 napData.uid = 3; 453 napData.bundleName = "programName"; 454 EXPECT_FALSE(NapProcess::GetInstance()->IsNeedNotify(napData)); 455 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.monitors_.Monitor(pointerEvent)); 456 457 NapProcess::GetInstance()->napClientPid_ = REMOVE_OBSERVER; 458 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.monitors_.Monitor(pointerEvent)); 459 460 NapProcess::GetInstance()->napClientPid_ = UNOBSERVED; 461 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.monitors_.Monitor(pointerEvent)); 462 } 463 464 /** 465 * @tc.name: EventMonitorHandlerTest_OnHandleEvent_001 466 * @tc.desc: Test OnHandleEvent 467 * @tc.type: FUNC 468 * @tc.require: 469 */ 470 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_OnHandleEvent_001, TestSize.Level1) 471 { 472 CALL_TEST_DEBUG; 473 EventMonitorHandler eventMonitorHandler; 474 auto keyEvent = KeyEvent::Create(); 475 eventMonitorHandler.HandleKeyEvent(keyEvent); 476 ASSERT_EQ(eventMonitorHandler.OnHandleEvent(keyEvent), false); 477 auto pointerEvent = PointerEvent::Create(); 478 eventMonitorHandler.HandlePointerEvent(pointerEvent); 479 ASSERT_EQ(eventMonitorHandler.OnHandleEvent(pointerEvent), false); 480 481 eventMonitorHandler.HandleTouchEvent(pointerEvent); 482 PointerEvent::PointerItem item; 483 item.SetDeviceId(1); 484 item.SetPointerId(0); 485 item.SetDisplayX(523); 486 item.SetDisplayY(723); 487 item.SetPressure(5); 488 pointerEvent->AddPointerItem(item); 489 item.SetDisplayY(610); 490 item.SetPointerId(1); 491 item.SetDeviceId(1); 492 item.SetPressure(7); 493 item.SetDisplayX(600); 494 pointerEvent->AddPointerItem(item); 495 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_MOVE); 496 pointerEvent->SetPointerId(1); 497 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN); 498 499 keyEvent->SetKeyCode(KeyEvent::KEYCODE_BACK); 500 keyEvent->SetActionTime(100); 501 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN); 502 keyEvent->ActionToString(KeyEvent::KEY_ACTION_DOWN); 503 keyEvent->KeyCodeToString(KeyEvent::KEYCODE_BACK); 504 KeyEvent::KeyItem part; 505 part.SetKeyCode(KeyEvent::KEYCODE_BACK); 506 part.SetDownTime(100); 507 part.SetPressed(true); 508 part.SetUnicode(0); 509 keyEvent->AddKeyItem(part); 510 511 eventMonitorHandler.HandlePointerEvent(pointerEvent); 512 eventMonitorHandler.HandleTouchEvent(pointerEvent); 513 ASSERT_EQ(eventMonitorHandler.OnHandleEvent(keyEvent), false); 514 ASSERT_EQ(eventMonitorHandler.OnHandleEvent(pointerEvent), false); 515 } 516 517 /** 518 * @tc.name: EventMonitorHandlerTest_InitSessionLostCallback_001 519 * @tc.desc: Verify the invalid and valid event type of InitSessionLostCallback 520 * @tc.type: FUNC 521 * @tc.require: 522 */ 523 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_InitSessionLostCallback_001, TestSize.Level1) 524 { 525 CALL_TEST_DEBUG; 526 EventMonitorHandler eventMonitorHandler; 527 eventMonitorHandler.sessionLostCallbackInitialized_ = true; 528 eventMonitorHandler.InitSessionLostCallback(); 529 eventMonitorHandler.sessionLostCallbackInitialized_ = false; 530 UDSServer udSever; 531 InputHandler->udsServer_ = &udSever; 532 auto udsServerPtr = InputHandler->GetUDSServer(); 533 EXPECT_NE(udsServerPtr, nullptr); 534 eventMonitorHandler.InitSessionLostCallback(); 535 InputHandler->udsServer_ = nullptr; 536 } 537 538 /** 539 * @tc.name: EventMonitorHandlerTest_AddInputHandler_001 540 * @tc.desc: Verify the invalid and valid event type of AddInputHandler 541 * @tc.type: FUNC 542 * @tc.require: 543 */ 544 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_AddInputHandler_001, TestSize.Level1) 545 { 546 CALL_TEST_DEBUG; 547 EventMonitorHandler eventMonitorHandler; 548 InputHandlerType handlerType = InputHandlerType::NONE; 549 HandleEventType eventType = 0; 550 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 551 int32_t ret = eventMonitorHandler.AddInputHandler(handlerType, eventType, session); 552 EXPECT_EQ(ret, RET_ERR); 553 eventType = 1; 554 ret = eventMonitorHandler.AddInputHandler(handlerType, eventType, session); 555 EXPECT_EQ(ret, RET_OK); 556 } 557 558 /** 559 * @tc.name: EventMonitorHandlerTest_RemoveInputHandler_001 560 * @tc.desc: Verify the invalid and valid event type of RemoveInputHandler 561 * @tc.type: FUNC 562 * @tc.require: 563 */ 564 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_RemoveInputHandler_001, TestSize.Level1) 565 { 566 CALL_TEST_DEBUG; 567 EventMonitorHandler eventMonitorHandler; 568 InputHandlerType handlerType = InputHandlerType::NONE; 569 HandleEventType eventType = 1; 570 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 571 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.RemoveInputHandler(handlerType, eventType, session)); 572 handlerType = InputHandlerType::MONITOR; 573 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.RemoveInputHandler(handlerType, eventType, session)); 574 } 575 576 /** 577 * @tc.name: EventMonitorHandlerTest_SendToClient_001 578 * @tc.desc: Verify the keyEvent and pointerEvent of SendToClient 579 * @tc.type: FUNC 580 * @tc.require: 581 */ 582 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_SendToClient_001, TestSize.Level1) 583 { 584 CALL_TEST_DEBUG; 585 InputHandlerType handlerType = InputHandlerType::NONE; 586 HandleEventType eventType = 0; 587 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 588 EventMonitorHandler::SessionHandler sessionHandler { handlerType, eventType, session }; 589 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create(); 590 NetPacket keyEventPkt(MmiMessageId::REPORT_KEY_EVENT); 591 keyEventPkt << InputHandlerType::MONITOR << static_cast<uint32_t>(evdev_device_udev_tags::EVDEV_UDEV_TAG_INPUT); 592 ASSERT_NO_FATAL_FAILURE(sessionHandler.SendToClient(keyEvent, keyEventPkt)); 593 594 NetPacket pointerEventPkt(MmiMessageId::REPORT_POINTER_EVENT); 595 std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create(); 596 pointerEventPkt << InputHandlerType::MONITOR << static_cast<uint32_t>(evdev_device_udev_tags::EVDEV_UDEV_TAG_INPUT); 597 InputEventDataTransformation::Marshalling(pointerEvent, pointerEventPkt); 598 ASSERT_NO_FATAL_FAILURE(sessionHandler.SendToClient(pointerEvent, pointerEventPkt)); 599 } 600 601 /** 602 * @tc.name: EventMonitorHandlerTest_AddMonitor_001 603 * @tc.desc: Verify the invalid and valid event type of AddMonitor 604 * @tc.type: FUNC 605 * @tc.require: 606 */ 607 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_AddMonitor_001, TestSize.Level1) 608 { 609 CALL_TEST_DEBUG; 610 EventMonitorHandler::MonitorCollection monitorCollection; 611 InputHandlerType handlerType = InputHandlerType::NONE; 612 HandleEventType eventType = 0; 613 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 614 EventMonitorHandler::SessionHandler sessionHandler { handlerType, eventType, session }; 615 sessionHandler.eventType_ = HANDLE_EVENT_TYPE_TOUCH_GESTURE; 616 monitorCollection.monitors_.insert(sessionHandler); 617 for (int i = 0; i < MAX_N_INPUT_MONITORS - 2; i++) { 618 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 619 EventMonitorHandler::SessionHandler sessionHandler = { handlerType, eventType, session }; 620 sessionHandler.eventType_ = HANDLE_EVENT_TYPE_NONE; 621 monitorCollection.monitors_.insert(sessionHandler); 622 } 623 int32_t ret = monitorCollection.AddMonitor(sessionHandler); 624 EXPECT_EQ(ret, RET_OK); 625 626 monitorCollection.monitors_.erase(sessionHandler); 627 SessionPtr session2 = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 628 EventMonitorHandler::SessionHandler sessionHandler2 { handlerType, eventType, session2 }; 629 monitorCollection.monitors_.insert(sessionHandler2); 630 ret = monitorCollection.AddMonitor(sessionHandler2); 631 EXPECT_EQ(ret, RET_OK); 632 633 SessionPtr session3 = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 634 EventMonitorHandler::SessionHandler sessionHandler3 { handlerType, eventType, session3 }; 635 monitorCollection.monitors_.insert(sessionHandler3); 636 ret = monitorCollection.AddMonitor(sessionHandler3); 637 EXPECT_EQ(ret, RET_ERR); 638 } 639 640 /** 641 * @tc.name: EventMonitorHandlerTest_RemoveMonitor_001 642 * @tc.desc: Verify the invalid and valid event type of RemoveMonitor 643 * @tc.type: FUNC 644 * @tc.require: 645 */ 646 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_RemoveMonitor_001, TestSize.Level1) 647 { 648 CALL_TEST_DEBUG; 649 EventMonitorHandler::MonitorCollection monitorCollection; 650 InputHandlerType handlerType = InputHandlerType::NONE; 651 HandleEventType eventType = 0; 652 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 653 EventMonitorHandler::SessionHandler sessionHandler { handlerType, eventType, session }; 654 ASSERT_NO_FATAL_FAILURE(monitorCollection.RemoveMonitor(sessionHandler)); 655 sessionHandler.eventType_ = HANDLE_EVENT_TYPE_TOUCH_GESTURE; 656 monitorCollection.monitors_.insert(sessionHandler); 657 ASSERT_NO_FATAL_FAILURE(monitorCollection.RemoveMonitor(sessionHandler)); 658 SessionPtr session2 = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 659 eventType = 1; 660 sessionHandler = { handlerType, eventType, session2 }; 661 monitorCollection.monitors_.insert(sessionHandler); 662 ASSERT_NO_FATAL_FAILURE(monitorCollection.RemoveMonitor(sessionHandler)); 663 } 664 665 /** 666 * @tc.name: EventMonitorHandlerTest_MarkConsumed 667 * @tc.desc: Test MarkConsumed 668 * @tc.type: FUNC 669 * @tc.require: 670 */ 671 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_MarkConsumed, TestSize.Level1) 672 { 673 CALL_TEST_DEBUG; 674 EventMonitorHandler eventMonitorHandler; 675 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 676 int32_t eventId = 100; 677 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.MarkConsumed(eventId, session)); 678 } 679 680 /** 681 * @tc.name: EventMonitorHandlerTest_OnSessionLost 682 * @tc.desc: Test OnSessionLost 683 * @tc.type: FUNC 684 * @tc.require: 685 */ 686 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_OnSessionLost, TestSize.Level1) 687 { 688 CALL_TEST_DEBUG; 689 EventMonitorHandler eventMonitorHandler; 690 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 691 EventMonitorHandler::SessionHandler sessionHandler { InputHandlerType::MONITOR, HANDLE_EVENT_TYPE_KEY, session }; 692 eventMonitorHandler.monitors_.monitors_.insert(sessionHandler); 693 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.OnSessionLost(session)); 694 695 eventMonitorHandler.monitors_.monitors_.insert(sessionHandler); 696 session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 697 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.OnSessionLost(session)); 698 } 699 700 /** 701 * @tc.name: EventMonitorHandlerTest_HasMonitor 702 * @tc.desc: Test HasMonitor 703 * @tc.type: FUNC 704 * @tc.require: 705 */ 706 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_HasMonitor, TestSize.Level1) 707 { 708 CALL_TEST_DEBUG; 709 EventMonitorHandler::MonitorCollection monitorCollection; 710 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 711 EventMonitorHandler::SessionHandler monitor { InputHandlerType::MONITOR, HANDLE_EVENT_TYPE_ALL, session }; 712 monitorCollection.monitors_.insert(monitor); 713 ASSERT_TRUE(monitorCollection.HasMonitor(session)); 714 } 715 716 /** 717 * @tc.name: EventMonitorHandlerTest_UpdateConsumptionState 718 * @tc.desc: Test UpdateConsumptionState 719 * @tc.type: FUNC 720 * @tc.require: 721 */ 722 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_UpdateConsumptionState, TestSize.Level1) 723 { 724 CALL_TEST_DEBUG; 725 int32_t deviceId = 6; 726 EventMonitorHandler::MonitorCollection monitorCollection; 727 std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create(); 728 ASSERT_NE(pointerEvent, nullptr); 729 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHPAD); 730 pointerEvent->SetDeviceId(deviceId); 731 EventMonitorHandler::MonitorCollection::ConsumptionState state; 732 for (int32_t i = 0; i <= MAX_EVENTIDS_SIZE; ++i) { 733 state.eventIds_.insert(i); 734 } 735 monitorCollection.states_.insert(std::make_pair(deviceId, state)); 736 PointerEvent::PointerItem item; 737 item.SetDeviceId(1); 738 pointerEvent->AddPointerItem(item); 739 pointerEvent->SetId(1); 740 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_SWIPE_BEGIN); 741 ASSERT_NO_FATAL_FAILURE(monitorCollection.UpdateConsumptionState(pointerEvent)); 742 743 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_SWIPE_END); 744 ASSERT_NO_FATAL_FAILURE(monitorCollection.UpdateConsumptionState(pointerEvent)); 745 } 746 747 /** 748 * @tc.name: EventMonitorHandlerTest_ProcessScreenCapture_001 749 * @tc.desc: Test ProcessScreenCapture 750 * @tc.type: FUNC 751 * @tc.require: 752 */ 753 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_ProcessScreenCapture_001, TestSize.Level1) 754 { 755 CALL_TEST_DEBUG; 756 EventMonitorHandler eventMonitorHandler; 757 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 758 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.ProcessScreenCapture(g_pid, false)); 759 EventMonitorHandler::MonitorCollection monitorCollection; 760 EventMonitorHandler::SessionHandler monitor { InputHandlerType::MONITOR, HANDLE_EVENT_TYPE_ALL, session }; 761 monitorCollection.monitors_.insert(monitor); 762 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.ProcessScreenCapture(g_pid, false)); 763 } 764 765 /** 766 * @tc.name: EventMonitorHandlerTest_ProcessScreenCapture_002 767 * @tc.desc: Test ProcessScreenCapture 768 * @tc.type: FUNC 769 * @tc.require: 770 */ 771 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_ProcessScreenCapture_002, TestSize.Level1) 772 { 773 CALL_TEST_DEBUG; 774 EventMonitorHandler eventMonitorHandler; 775 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 776 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.ProcessScreenCapture(g_pid, true)); 777 EventMonitorHandler::MonitorCollection monitorCollection; 778 EventMonitorHandler::SessionHandler monitor { InputHandlerType::MONITOR, HANDLE_EVENT_TYPE_ALL, session }; 779 monitorCollection.monitors_.insert(monitor); 780 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.ProcessScreenCapture(g_pid, true)); 781 } 782 783 /** 784 * @tc.name: EventMonitorHandlerTest_ProcessScreenCapture_003 785 * @tc.desc: Test ProcessScreenCapture 786 * @tc.type: FUNC 787 * @tc.require: 788 */ 789 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_ProcessScreenCapture_003, TestSize.Level1) 790 { 791 CALL_TEST_DEBUG; 792 EventMonitorHandler eventMonitorHandler; 793 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 794 EventMonitorHandler::MonitorCollection monitorCollection; 795 EventMonitorHandler::SessionHandler handler { InputHandlerType::MONITOR, HANDLE_EVENT_TYPE_ALL, session }; 796 std::set<EventMonitorHandler::SessionHandler> handlerSet; 797 handlerSet.insert(handler); 798 monitorCollection.endScreenCaptureMonitors_[-1] = handlerSet; 799 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.ProcessScreenCapture(g_pid, true)); 800 } 801 802 /** 803 * @tc.name: EventMonitorHandlerTest_Dump_001 804 * @tc.desc: Test Dump 805 * @tc.type: FUNC 806 * @tc.require: 807 */ 808 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_Dump_001, TestSize.Level1) 809 { 810 CALL_TEST_DEBUG; 811 EventMonitorHandler eventMonitorHandler; 812 int32_t fd = 1; 813 std::vector<std::string> args; 814 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.Dump(fd, args)); 815 } 816 817 /** 818 * @tc.name: EventMonitorHandlerTest_OnSessionLost_001 819 * @tc.desc: Test OnSessionLost 820 * @tc.type: FUNC 821 * @tc.require: 822 */ 823 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_OnSessionLost_001, TestSize.Level1) 824 { 825 CALL_TEST_DEBUG; 826 EventMonitorHandler eventMonitorHandler; 827 EventMonitorHandler::MonitorCollection monitorCollection; 828 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 829 EventMonitorHandler::SessionHandler sessionHandler { InputHandlerType::MONITOR, HANDLE_EVENT_TYPE_KEY, session }; 830 eventMonitorHandler.monitors_.monitors_.insert(sessionHandler); 831 std::set<EventMonitorHandler::SessionHandler> handlerSet; 832 handlerSet.insert(sessionHandler); 833 monitorCollection.endScreenCaptureMonitors_[-1] = handlerSet; 834 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.OnSessionLost(session)); 835 eventMonitorHandler.monitors_.monitors_.insert(sessionHandler); 836 session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 837 handlerSet.insert(sessionHandler); 838 monitorCollection.endScreenCaptureMonitors_[-1] = handlerSet; 839 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.OnSessionLost(session)); 840 } 841 842 /** 843 * @tc.name: EventMonitorHandlerTest_RecoveryScreenCaptureMonitor_001 844 * @tc.desc: Test RecoveryScreenCaptureMonitor 845 * @tc.type: FUNC 846 * @tc.require: 847 */ 848 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_RecoveryScreenCaptureMonitor_001, TestSize.Level1) 849 { 850 CALL_TEST_DEBUG; 851 EventMonitorHandler::MonitorCollection monitorCollection; 852 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 853 session->tokenType_ = TokenType::TOKEN_SHELL; 854 ASSERT_NO_FATAL_FAILURE(monitorCollection.RecoveryScreenCaptureMonitor(session)); 855 session->tokenType_ = TokenType::TOKEN_HAP; 856 ASSERT_NO_FATAL_FAILURE(monitorCollection.RecoveryScreenCaptureMonitor(session)); 857 858 EventMonitorHandler::SessionHandler handler { InputHandlerType::MONITOR, HANDLE_EVENT_TYPE_ALL, session }; 859 std::set<EventMonitorHandler::SessionHandler> handlerSet; 860 handlerSet.insert(handler); 861 monitorCollection.endScreenCaptureMonitors_[-1] = handlerSet; 862 ASSERT_NO_FATAL_FAILURE(monitorCollection.RecoveryScreenCaptureMonitor(session)); 863 } 864 865 /** 866 * @tc.name: EventMonitorHandlerTest_RemoveScreenCaptureMonitor_001 867 * @tc.desc: Test RemoveScreenCaptureMonitor 868 * @tc.type: FUNC 869 * @tc.require: 870 */ 871 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_RemoveScreenCaptureMonitor_001, TestSize.Level1) 872 { 873 CALL_TEST_DEBUG; 874 EventMonitorHandler::MonitorCollection monitorCollection; 875 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 876 session->tokenType_ = TokenType::TOKEN_SHELL; 877 ASSERT_NO_FATAL_FAILURE(monitorCollection.RemoveScreenCaptureMonitor(session)); 878 session->tokenType_ = TokenType::TOKEN_HAP; 879 ASSERT_NO_FATAL_FAILURE(monitorCollection.RemoveScreenCaptureMonitor(session)); 880 EventMonitorHandler::SessionHandler handler { InputHandlerType::MONITOR, HANDLE_EVENT_TYPE_ALL, session }; 881 std::set<EventMonitorHandler::SessionHandler> handlerSet; 882 handlerSet.insert(handler); 883 monitorCollection.endScreenCaptureMonitors_[-1] = handlerSet; 884 ASSERT_NO_FATAL_FAILURE(monitorCollection.RemoveScreenCaptureMonitor(session)); 885 } 886 887 /** 888 * @tc.name: EventMonitorHandlerTest_ProcessScreenCapture_004 889 * @tc.desc: Test ProcessScreenCapture 890 * @tc.type: FUNC 891 * @tc.require: 892 */ 893 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_ProcessScreenCapture_004, TestSize.Level1) 894 { 895 CALL_TEST_DEBUG; 896 EventMonitorHandler eventMonitorHandler; 897 int32_t pid = 2; 898 bool isStart = true; 899 UDSServer udSever; 900 InputHandler->udsServer_ = &udSever; 901 udSever.idxPidMap_.insert(std::make_pair(pid, 2)); 902 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 903 udSever.sessionsMap_.insert(std::make_pair(pid, session)); 904 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.ProcessScreenCapture(pid, isStart)); 905 isStart = false; 906 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.ProcessScreenCapture(pid, isStart)); 907 } 908 909 /** 910 * @tc.name: EventMonitorHandlerTest_Dump_002 911 * @tc.desc: Test Dump 912 * @tc.type: FUNC 913 * @tc.require: 914 */ 915 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_Dump_002, TestSize.Level1) 916 { 917 CALL_TEST_DEBUG; 918 EventMonitorHandler eventMonitorHandler; 919 int32_t fd = 1; 920 std::vector<std::string> args; 921 SessionPtr session = nullptr; 922 EventMonitorHandler::MonitorCollection monitorCollection; 923 EventMonitorHandler::SessionHandler monitor { InputHandlerType::INTERCEPTOR, 2, session }; 924 monitorCollection.monitors_.insert(monitor); 925 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.Dump(fd, args)); 926 } 927 928 /** 929 * @tc.name: EventMonitorHandlerTest_Dump_003 930 * @tc.desc: Test Dump 931 * @tc.type: FUNC 932 * @tc.require: 933 */ 934 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_Dump_003, TestSize.Level1) 935 { 936 CALL_TEST_DEBUG; 937 EventMonitorHandler eventMonitorHandler; 938 int32_t fd = 1; 939 std::vector<std::string> args; 940 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 941 EventMonitorHandler::MonitorCollection monitorCollection; 942 EventMonitorHandler::SessionHandler monitorone { InputHandlerType::INTERCEPTOR, 2, session }; 943 monitorCollection.monitors_.insert(monitorone); 944 EventMonitorHandler::SessionHandler monitortwo { InputHandlerType::MONITOR, 3, session }; 945 monitorCollection.monitors_.insert(monitortwo); 946 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.Dump(fd, args)); 947 } 948 949 /** 950 * @tc.name: EventMonitorHandlerTest_CheckHasInputHandler_001 951 * @tc.desc: Test CheckHasInputHandler 952 * @tc.type: FUNC 953 * @tc.require: 954 */ 955 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_CheckHasInputHandler_001, TestSize.Level1) 956 { 957 CALL_TEST_DEBUG; 958 EventMonitorHandler eventMonitorHandler; 959 HandleEventType eventType = 1; 960 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.CheckHasInputHandler(eventType)); 961 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 962 EventMonitorHandler::MonitorCollection monitorCollection; 963 EventMonitorHandler::SessionHandler monitorone { InputHandlerType::INTERCEPTOR, 1, session }; 964 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.CheckHasInputHandler(eventType)); 965 monitorCollection.monitors_.insert(monitorone); 966 EventMonitorHandler::SessionHandler monitortwo { InputHandlerType::MONITOR, 2, session }; 967 monitorCollection.monitors_.insert(monitortwo); 968 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.CheckHasInputHandler(eventType)); 969 EventMonitorHandler::SessionHandler monitorthere { InputHandlerType::MONITOR, 3, session }; 970 monitorCollection.monitors_.insert(monitorthere); 971 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.CheckHasInputHandler(eventType)); 972 } 973 974 /** 975 * @tc.name: EventMonitorHandlerTest_RemoveInputHandler_002 976 * @tc.desc: Verify the invalid and valid event type of RemoveInputHandler 977 * @tc.type: FUNC 978 * @tc.require: 979 */ 980 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_RemoveInputHandler_002, TestSize.Level1) 981 { 982 CALL_TEST_DEBUG; 983 EventMonitorHandler eventMonitorHandler; 984 InputHandlerType handlerType = InputHandlerType::MONITOR; 985 HandleEventType eventType = 2; 986 std::shared_ptr<IInputEventHandler::IInputEventConsumer> callback = std::make_shared<MyInputEventConsumer>(); 987 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.RemoveInputHandler(handlerType, eventType, callback)); 988 handlerType = InputHandlerType::NONE; 989 ASSERT_NO_FATAL_FAILURE(eventMonitorHandler.RemoveInputHandler(handlerType, eventType, callback)); 990 } 991 992 /** 993 * @tc.name: EventMonitorHandlerTest_IsPinch 994 * @tc.desc: Test IsPinch 995 * @tc.type: FUNC 996 * @tc.require: 997 */ 998 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_IsPinch, TestSize.Level1) 999 { 1000 CALL_TEST_DEBUG; 1001 EventMonitorHandler::MonitorCollection monitorCollection; 1002 std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create(); 1003 ASSERT_NE(pointerEvent, nullptr); 1004 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_UNKNOWN); 1005 bool ret = false; 1006 ret = monitorCollection.IsPinch(pointerEvent); 1007 ASSERT_FALSE(ret); 1008 1009 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE); 1010 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_UP); 1011 ret = monitorCollection.IsPinch(pointerEvent); 1012 ASSERT_FALSE(ret); 1013 1014 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_AXIS_UPDATE); 1015 ret = monitorCollection.IsPinch(pointerEvent); 1016 ASSERT_TRUE(ret); 1017 } 1018 1019 /** 1020 * @tc.name: EventMonitorHandlerTest_IsRotate 1021 * @tc.desc: Test IsRotate 1022 * @tc.type: FUNC 1023 * @tc.require: 1024 */ 1025 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_IsRotate, TestSize.Level1) 1026 { 1027 CALL_TEST_DEBUG; 1028 EventMonitorHandler::MonitorCollection monitorCollection; 1029 std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create(); 1030 ASSERT_NE(pointerEvent, nullptr); 1031 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_UNKNOWN); 1032 bool ret = false; 1033 ret = monitorCollection.IsRotate(pointerEvent); 1034 ASSERT_FALSE(ret); 1035 1036 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE); 1037 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_ROTATE_UPDATE); 1038 ret = monitorCollection.IsRotate(pointerEvent); 1039 ASSERT_TRUE(ret); 1040 } 1041 1042 /** 1043 * @tc.name: EventMonitorHandlerTest_IsThreeFingersSwipe 1044 * @tc.desc: Test IsThreeFingersSwipe 1045 * @tc.type: FUNC 1046 * @tc.require: 1047 */ 1048 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_IsThreeFingersSwipe, TestSize.Level1) 1049 { 1050 CALL_TEST_DEBUG; 1051 EventMonitorHandler::MonitorCollection monitorCollection; 1052 std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create(); 1053 ASSERT_NE(pointerEvent, nullptr); 1054 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN); 1055 bool ret = false; 1056 ret = monitorCollection.IsThreeFingersSwipe(pointerEvent); 1057 ASSERT_FALSE(ret); 1058 1059 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHPAD); 1060 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_SWIPE_UPDATE); 1061 pointerEvent->SetFingerCount(THREE_FINGERS); 1062 ret = monitorCollection.IsThreeFingersSwipe(pointerEvent); 1063 ASSERT_TRUE(ret); 1064 } 1065 1066 /** 1067 * @tc.name: EventMonitorHandlerTest_IsFourFingersSwipe 1068 * @tc.desc: Test IsFourFingersSwipe 1069 * @tc.type: FUNC 1070 * @tc.require: 1071 */ 1072 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_IsFourFingersSwipe, TestSize.Level1) 1073 { 1074 CALL_TEST_DEBUG; 1075 EventMonitorHandler::MonitorCollection monitorCollection; 1076 std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create(); 1077 ASSERT_NE(pointerEvent, nullptr); 1078 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN); 1079 bool ret = false; 1080 ret = monitorCollection.IsFourFingersSwipe(pointerEvent); 1081 ASSERT_FALSE(ret); 1082 1083 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHPAD); 1084 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_SWIPE_UPDATE); 1085 pointerEvent->SetFingerCount(FOUR_FINGERS); 1086 ret = monitorCollection.IsFourFingersSwipe(pointerEvent); 1087 ASSERT_TRUE(ret); 1088 } 1089 1090 /** 1091 * @tc.name: EventMonitorHandlerTest_IsThreeFingersTap 1092 * @tc.desc: Test IsThreeFingersTap 1093 * @tc.type: FUNC 1094 * @tc.require: 1095 */ 1096 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_IsThreeFingersTap, TestSize.Level1) 1097 { 1098 CALL_TEST_DEBUG; 1099 EventMonitorHandler::MonitorCollection monitorCollection; 1100 std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create(); 1101 ASSERT_NE(pointerEvent, nullptr); 1102 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN); 1103 bool ret = false; 1104 ret = monitorCollection.IsThreeFingersTap(pointerEvent); 1105 ASSERT_FALSE(ret); 1106 1107 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHPAD); 1108 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_TRIPTAP); 1109 pointerEvent->SetFingerCount(THREE_FINGERS); 1110 ret = monitorCollection.IsThreeFingersTap(pointerEvent); 1111 ASSERT_TRUE(ret); 1112 } 1113 1114 #ifdef OHOS_BUILD_ENABLE_FINGERPRINT 1115 /** 1116 * @tc.name: EventMonitorHandlerTest_IsFingerprint 1117 * @tc.desc: Test IsFingerprint 1118 * @tc.type: FUNC 1119 * @tc.require: 1120 */ 1121 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_IsFingerprint, TestSize.Level1) 1122 { 1123 CALL_TEST_DEBUG; 1124 EventMonitorHandler::MonitorCollection monitorCollection; 1125 std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create(); 1126 ASSERT_NE(pointerEvent, nullptr); 1127 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN); 1128 bool ret = false; 1129 ret = monitorCollection.IsFingerprint(pointerEvent); 1130 ASSERT_FALSE(ret); 1131 1132 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_FINGERPRINT); 1133 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_FINGERPRINT_SLIDE); 1134 ret = monitorCollection.IsFingerprint(pointerEvent); 1135 ASSERT_TRUE(ret); 1136 } 1137 #endif // OHOS_BUILD_ENABLE_FINGERPRINT 1138 /** 1139 * @tc.name: EventMonitorHandlerTest_CheckIfNeedSendToClient_01 1140 * @tc.desc: Test CheckIfNeedSendToClient 1141 * @tc.type: FUNC 1142 * @tc.require: 1143 */ 1144 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_CheckIfNeedSendToClient_01, TestSize.Level1) 1145 { 1146 CALL_TEST_DEBUG; 1147 EventMonitorHandler::MonitorCollection monitorCollection; 1148 std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create(); 1149 ASSERT_NE(pointerEvent, nullptr); 1150 InputHandlerType handlerType = InputHandlerType::NONE; 1151 HandleEventType eventType = 0; 1152 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 1153 EventMonitorHandler::SessionHandler sessionHandler { handlerType, eventType, session }; 1154 sessionHandler.eventType_ = HANDLE_EVENT_TYPE_FINGERPRINT; 1155 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_FINGERPRINT); 1156 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_FINGERPRINT_SLIDE); 1157 bool ret = false; 1158 ret = monitorCollection.CheckIfNeedSendToClient(sessionHandler, pointerEvent); 1159 ASSERT_TRUE(ret); 1160 1161 sessionHandler.eventType_ = HANDLE_EVENT_TYPE_TOUCH_GESTURE; 1162 ret = monitorCollection.CheckIfNeedSendToClient(sessionHandler, pointerEvent); 1163 ASSERT_TRUE(ret); 1164 1165 sessionHandler.eventType_ = HANDLE_EVENT_TYPE_SWIPEINWARD; 1166 ret = monitorCollection.CheckIfNeedSendToClient(sessionHandler, pointerEvent); 1167 ASSERT_TRUE(ret); 1168 1169 sessionHandler.eventType_ = HANDLE_EVENT_TYPE_TOUCH; 1170 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN); 1171 ret = monitorCollection.CheckIfNeedSendToClient(sessionHandler, pointerEvent); 1172 ASSERT_TRUE(ret); 1173 1174 sessionHandler.eventType_ = HANDLE_EVENT_TYPE_MOUSE; 1175 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE); 1176 ret = monitorCollection.CheckIfNeedSendToClient(sessionHandler, pointerEvent); 1177 ASSERT_TRUE(ret); 1178 1179 sessionHandler.eventType_ = HANDLE_EVENT_TYPE_PINCH; 1180 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE); 1181 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_AXIS_UPDATE); 1182 ret = monitorCollection.CheckIfNeedSendToClient(sessionHandler, pointerEvent); 1183 ASSERT_TRUE(ret); 1184 1185 sessionHandler.eventType_ = HANDLE_EVENT_TYPE_THREEFINGERSSWIP; 1186 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHPAD); 1187 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_SWIPE_UPDATE); 1188 pointerEvent->SetFingerCount(THREE_FINGERS); 1189 ret = monitorCollection.CheckIfNeedSendToClient(sessionHandler, pointerEvent); 1190 ASSERT_TRUE(ret); 1191 } 1192 1193 /** 1194 * @tc.name: EventMonitorHandlerTest_CheckIfNeedSendToClient_02 1195 * @tc.desc: Test CheckIfNeedSendToClient 1196 * @tc.type: FUNC 1197 * @tc.require: 1198 */ 1199 HWTEST_F(EventMonitorHandlerTest, EventMonitorHandlerTest_CheckIfNeedSendToClient_02, TestSize.Level1) 1200 { 1201 CALL_TEST_DEBUG; 1202 EventMonitorHandler::MonitorCollection monitorCollection; 1203 std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create(); 1204 ASSERT_NE(pointerEvent, nullptr); 1205 InputHandlerType handlerType = InputHandlerType::NONE; 1206 HandleEventType eventType = 0; 1207 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 1208 EventMonitorHandler::SessionHandler sessionHandler { handlerType, eventType, session }; 1209 1210 sessionHandler.eventType_ = HANDLE_EVENT_TYPE_FOURFINGERSSWIP; 1211 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHPAD); 1212 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_SWIPE_UPDATE); 1213 pointerEvent->SetFingerCount(FOUR_FINGERS); 1214 bool ret = false; 1215 ret = monitorCollection.CheckIfNeedSendToClient(sessionHandler, pointerEvent); 1216 ASSERT_TRUE(ret); 1217 1218 sessionHandler.eventType_ = HANDLE_EVENT_TYPE_ROTATE; 1219 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE); 1220 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_ROTATE_UPDATE); 1221 ret = monitorCollection.CheckIfNeedSendToClient(sessionHandler, pointerEvent); 1222 ASSERT_TRUE(ret); 1223 1224 sessionHandler.eventType_ = HANDLE_EVENT_TYPE_THREEFINGERSTAP; 1225 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHPAD); 1226 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_TRIPTAP); 1227 pointerEvent->SetFingerCount(THREE_FINGERS); 1228 ret = monitorCollection.CheckIfNeedSendToClient(sessionHandler, pointerEvent); 1229 ASSERT_TRUE(ret); 1230 } 1231 } // namespace MMI 1232 } // namespace OHOS 1233