1 /* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include <fstream> 17 18 #include <gtest/gtest.h> 19 20 #include "event_pre_monitor_handler.h" 21 #include "input_event_handler.h" 22 #include "mmi_log.h" 23 24 namespace OHOS { 25 namespace MMI { 26 namespace { 27 using namespace testing::ext; 28 constexpr int32_t UID_ROOT { 0 }; 29 static constexpr char PROGRAM_NAME[] = "uds_sesion_test"; 30 int32_t g_handlerId = 1; 31 int32_t g_moduleType = 3; 32 int32_t g_pid = 0; 33 int32_t g_writeFd = -1; 34 } // namespace 35 36 class EventPreMonitorHandlerTest : public testing::Test { 37 public: SetUpTestCase(void)38 static void SetUpTestCase(void) {} TearDownTestCase(void)39 static void TearDownTestCase(void) {} 40 }; 41 42 class MyInputEventConsumer : public IInputEventHandler::IInputEventConsumer { 43 public: OnInputEvent(InputHandlerType type,std::shared_ptr<KeyEvent> event) const44 void OnInputEvent(InputHandlerType type, std::shared_ptr<KeyEvent> event) const override {} OnInputEvent(InputHandlerType type,std::shared_ptr<PointerEvent> event) const45 void OnInputEvent(InputHandlerType type, std::shared_ptr<PointerEvent> event) const override {} OnInputEvent(InputHandlerType type,std::shared_ptr<AxisEvent> event) const46 void OnInputEvent(InputHandlerType type, std::shared_ptr<AxisEvent> event) const override {} 47 }; 48 49 /** 50 * @tc.name: EventPreMonitorHandlerTest_HandleKeyEvent_001 51 * @tc.desc: Test Overrides the if (HandleKeyEvent(keyEvent)) branch of the HandleKeyEvent function 52 * @tc.type: FUNC 53 * @tc.require: 54 */ 55 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_HandleKeyEvent_001, TestSize.Level1) 56 { 57 CALL_TEST_DEBUG; 58 EventPreMonitorHandler eventPreMonitorHandler; 59 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create(); 60 ASSERT_NE(keyEvent, nullptr); 61 ASSERT_NO_FATAL_FAILURE(eventPreMonitorHandler.HandleKeyEvent(keyEvent)); 62 } 63 64 /** 65 * @tc.name: EventPreMonitorHandlerTest_HandlePointerEvent_001 66 * @tc.desc: Test Overrides the if (HandlePointerEvent(pointerEvent)) branch of the HandlePointerEvent function 67 * @tc.type: FUNC 68 * @tc.require: 69 */ 70 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_HandlePointerEvent_001, TestSize.Level1) 71 { 72 CALL_TEST_DEBUG; 73 EventPreMonitorHandler eventPreMonitorHandler; 74 std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create(); 75 ASSERT_NE(pointerEvent, nullptr); 76 ASSERT_NO_FATAL_FAILURE(eventPreMonitorHandler.HandlePointerEvent(pointerEvent)); 77 } 78 79 /** 80 * @tc.name: EventPreMonitorHandlerTest_HandlePointerEvent_002 81 * @tc.desc: Test Overrides the if (HandlePointerEvent(pointerEvent)) branch of the HandlePointerEvent function 82 * @tc.type: FUNC 83 * @tc.require: 84 */ 85 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_HandlePointerEvent_002, TestSize.Level1) 86 { 87 CALL_TEST_DEBUG; 88 EventPreMonitorHandler eventPreMonitorHandler; 89 std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create(); 90 ASSERT_NE(pointerEvent, nullptr); 91 int32_t deviceId = 1; 92 pointerEvent->bitwise_ = PointerEvent::EVENT_FLAG_NONE; 93 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN); 94 pointerEvent->SetDeviceId(deviceId); 95 PointerEvent::PointerItem item; 96 item.SetPointerId(deviceId); 97 item.SetToolType(PointerEvent::TOOL_TYPE_KNUCKLE); 98 pointerEvent->AddPointerItem(item); 99 ASSERT_NO_FATAL_FAILURE(eventPreMonitorHandler.HandlePointerEvent(pointerEvent)); 100 } 101 102 /** 103 * @tc.name: EventPreMonitorHandlerTest_HandleTouchEvent_001 104 * @tc.desc: Test Overrides the if (HandleTouchEvent(pointerEvent)) branch of the HandleTouchEvent function 105 * @tc.type: FUNC 106 * @tc.require: 107 */ 108 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_HandleTouchEvent_001, TestSize.Level1) 109 { 110 CALL_TEST_DEBUG; 111 EventPreMonitorHandler eventPreMonitorHandler; 112 std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create(); 113 ASSERT_NE(pointerEvent, nullptr); 114 ASSERT_NO_FATAL_FAILURE(eventPreMonitorHandler.HandleTouchEvent(pointerEvent)); 115 } 116 117 /** 118 * @tc.name: EventPreMonitorHandlerTest_HandleTouchEvent_002 119 * @tc.desc: Test Overrides the if (HandleTouchEvent(pointerEvent)) branch of the HandleTouchEvent function 120 * @tc.type: FUNC 121 * @tc.require: 122 */ 123 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_HandleTouchEvent_002, TestSize.Level1) 124 { 125 CALL_TEST_DEBUG; 126 EventPreMonitorHandler eventPreMonitorHandler; 127 std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create(); 128 ASSERT_NE(pointerEvent, nullptr); 129 int32_t deviceId = 1; 130 pointerEvent->bitwise_ = PointerEvent::EVENT_FLAG_NONE; 131 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN); 132 pointerEvent->SetDeviceId(deviceId); 133 PointerEvent::PointerItem item; 134 item.SetPointerId(deviceId); 135 item.SetToolType(PointerEvent::TOOL_TYPE_KNUCKLE); 136 pointerEvent->AddPointerItem(item); 137 ASSERT_NO_FATAL_FAILURE(eventPreMonitorHandler.HandleTouchEvent(pointerEvent)); 138 } 139 140 /** 141 * @tc.name: EventPreMonitorHandlerTest_OnHandleEvent_001 142 * @tc.desc: Test Overrides the if (OnHandleEvent(keyEvent)) branch of the OnHandleEvent function 143 * @tc.type: FUNC 144 * @tc.require: 145 */ 146 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_OnHandleEvent_001, TestSize.Level1) 147 { 148 CALL_TEST_DEBUG; 149 EventPreMonitorHandler eventPreMonitorHandler; 150 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create(); 151 ASSERT_NE(keyEvent, nullptr); 152 keyEvent->SetKeyCode(KeyEvent::KEYCODE_VOLUME_UP); 153 bool ret = eventPreMonitorHandler.OnHandleEvent(keyEvent); 154 ASSERT_FALSE(ret); 155 keyEvent->bitwise_ = InputEvent::EVENT_FLAG_NO_MONITOR; 156 ASSERT_FALSE(eventPreMonitorHandler.OnHandleEvent(keyEvent)); 157 keyEvent->bitwise_ = InputEvent::EVENT_FLAG_NONE; 158 ASSERT_FALSE(eventPreMonitorHandler.OnHandleEvent(keyEvent)); 159 } 160 161 /** 162 * @tc.name: EventPreMonitorHandlerTest_InitSessionLostCallback_001 163 * @tc.desc: Verify the invalid and valid event type of InitSessionLostCallback 164 * @tc.type: FUNC 165 * @tc.require: 166 */ 167 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_InitSessionLostCallback_001, TestSize.Level1) 168 { 169 CALL_TEST_DEBUG; 170 EventPreMonitorHandler eventPreMonitorHandler; 171 eventPreMonitorHandler.sessionLostCallbackInitialized_ = true; 172 eventPreMonitorHandler.InitSessionLostCallback(); 173 eventPreMonitorHandler.sessionLostCallbackInitialized_ = false; 174 UDSServer udSever; 175 InputHandler->udsServer_ = &udSever; 176 auto udsServerPtr = InputHandler->GetUDSServer(); 177 EXPECT_NE(udsServerPtr, nullptr); 178 eventPreMonitorHandler.InitSessionLostCallback(); 179 InputHandler->udsServer_ = nullptr; 180 } 181 182 /** 183 * @tc.name: EventPreMonitorHandlerTest_AddInputHandler_001 184 * @tc.desc: Verify the invalid and valid event type of AddInputHandler 185 * @tc.type: FUNC 186 * @tc.require: 187 */ 188 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_AddInputHandler_001, TestSize.Level1) 189 { 190 CALL_TEST_DEBUG; 191 SessionPtr sess; 192 EventPreMonitorHandler eventPreMonitorHandler; 193 HandleEventType eventType = HANDLE_EVENT_TYPE_PRE_KEY; 194 std::vector<int32_t> keys = {1, 2, 3}; 195 int32_t ret = eventPreMonitorHandler.AddInputHandler(sess, 1, eventType, keys); 196 EXPECT_EQ(ret, RET_ERR); 197 eventType = HANDLE_EVENT_TYPE_NONE; 198 ret = eventPreMonitorHandler.AddInputHandler(sess, 1, eventType, keys); 199 EXPECT_EQ(ret, -1); 200 } 201 202 /** 203 * @tc.name: EventPreMonitorHandlerTest_RemoveInputHandler_001 204 * @tc.desc: Verify the invalid and valid event type of RemoveInputHandler 205 * @tc.type: FUNC 206 * @tc.require: 207 */ 208 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_RemoveInputHandler_001, TestSize.Level1) 209 { 210 CALL_TEST_DEBUG; 211 EventPreMonitorHandler eventPreMonitorHandler; 212 SessionPtr sess; 213 ASSERT_NO_FATAL_FAILURE(eventPreMonitorHandler.RemoveInputHandler(sess, 1)); 214 } 215 216 /** 217 * @tc.name: EventPreMonitorHandlerTest_OnSessionLost 218 * @tc.desc: Test OnSessionLost 219 * @tc.type: FUNC 220 * @tc.require: 221 */ 222 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_OnSessionLost, TestSize.Level1) 223 { 224 CALL_TEST_DEBUG; 225 EventPreMonitorHandler eventPreMonitorHandler; 226 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 227 HandleEventType eventType = HANDLE_EVENT_TYPE_PRE_KEY; 228 std::vector<int32_t> keys = {1, 2, 3}; 229 auto sessionHandler = std::make_shared<EventPreMonitorHandler::SessionHandler>(session, 1, eventType, keys); 230 eventPreMonitorHandler.monitors_.sessionHandlers_[keys] = 231 std::list<std::shared_ptr<EventPreMonitorHandler::SessionHandler>>(); 232 eventPreMonitorHandler.monitors_.sessionHandlers_[keys].push_back(sessionHandler); 233 ASSERT_NO_FATAL_FAILURE(eventPreMonitorHandler.OnSessionLost(session)); 234 } 235 236 /** 237 * @tc.name: EventPreMonitorHandlerTest_OnSessionLost_001 238 * @tc.desc: Test OnSessionLost 239 * @tc.type: FUNC 240 * @tc.require: 241 */ 242 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_OnSessionLost_001, TestSize.Level1) 243 { 244 CALL_TEST_DEBUG; 245 EventPreMonitorHandler eventPreMonitorHandler; 246 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 247 ASSERT_NO_FATAL_FAILURE(eventPreMonitorHandler.OnSessionLost(session)); 248 } 249 250 /** 251 * @tc.name: EventPreMonitorHandlerTest_OnSessionLost_002 252 * @tc.desc: Test OnSessionLost 253 * @tc.type: FUNC 254 * @tc.require: 255 */ 256 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_OnSessionLost_002, TestSize.Level1) 257 { 258 CALL_TEST_DEBUG; 259 EventPreMonitorHandler eventPreMonitorHandler; 260 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 261 std::vector<int32_t> keys = {2, 3, 4}; 262 eventPreMonitorHandler.monitors_.sessionHandlers_[keys] = 263 std::list<std::shared_ptr<EventPreMonitorHandler::SessionHandler>>(); 264 ASSERT_NO_FATAL_FAILURE(eventPreMonitorHandler.OnSessionLost(session)); 265 } 266 267 /** 268 * @tc.name: EventPreMonitorHandlerTest_OnSessionLost_003 269 * @tc.desc: Test OnSessionLost 270 * @tc.type: FUNC 271 * @tc.require: 272 */ 273 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_OnSessionLost_003, TestSize.Level1) 274 { 275 CALL_TEST_DEBUG; 276 EventPreMonitorHandler eventPreMonitorHandler; 277 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 278 HandleEventType eventType = HANDLE_EVENT_TYPE_PRE_KEY; 279 std::vector<int32_t> keys = {2, 3, 4}; 280 auto sessionHandler = 281 std::make_shared<EventPreMonitorHandler::SessionHandler>(session, 1, eventType, keys); 282 eventPreMonitorHandler.monitors_.sessionHandlers_[keys] = 283 std::list<std::shared_ptr<EventPreMonitorHandler::SessionHandler>>(); 284 eventPreMonitorHandler.monitors_.sessionHandlers_[keys].push_back(sessionHandler); 285 char testProgramName[] = "uds_sesion_test_2"; 286 SessionPtr testSession = std::make_shared<UDSSession>(testProgramName, g_moduleType, g_writeFd, UID_ROOT, g_pid); 287 ASSERT_NO_FATAL_FAILURE(eventPreMonitorHandler.OnSessionLost(testSession)); 288 } 289 290 /** 291 * @tc.name: EventPreMonitorHandlerTest_AddMonitor_001 292 * @tc.desc: Verify the invalid and valid event type of AddMonitor 293 * @tc.type: FUNC 294 * @tc.require: 295 */ 296 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_AddMonitor_001, TestSize.Level1) 297 { 298 CALL_TEST_DEBUG; 299 EventPreMonitorHandler::MonitorCollection monitorCollection; 300 HandleEventType eventType = HANDLE_EVENT_TYPE_PRE_KEY; 301 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 302 std::vector<int32_t> keys = {1, 2, 3}; 303 auto sessionHandler = std::make_shared<EventPreMonitorHandler::SessionHandler>(session, 1, eventType, keys); 304 monitorCollection.sessionHandlers_[keys] = std::list<std::shared_ptr<EventPreMonitorHandler::SessionHandler>>(); 305 monitorCollection.sessionHandlers_[keys].push_back(sessionHandler); 306 for (int i = 0; i < MAX_N_INPUT_MONITORS - 2; i++) { 307 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 308 auto sessionHandler = std::make_shared<EventPreMonitorHandler::SessionHandler>(session, 1, eventType, keys); 309 monitorCollection.sessionHandlers_[keys].push_back(sessionHandler); 310 } 311 int32_t ret = monitorCollection.AddMonitor(sessionHandler, keys); 312 EXPECT_EQ(ret, RET_OK); 313 SessionPtr session2 = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 314 auto sessionHandler2 = std::make_shared<EventPreMonitorHandler::SessionHandler>(session2, 1, eventType, keys); 315 monitorCollection.sessionHandlers_[keys].push_back(sessionHandler2); 316 ret = monitorCollection.AddMonitor(sessionHandler2, keys); 317 EXPECT_EQ(ret, RET_OK); 318 } 319 320 /** 321 * @tc.name: EventPreMonitorHandlerTest_RemoveMonitor_002 322 * @tc.desc: Verify the invalid and valid event type of RemoveMonitor 323 * @tc.type: FUNC 324 * @tc.require: 325 */ 326 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_RemoveMonitor_001, TestSize.Level1) 327 { 328 CALL_TEST_DEBUG; 329 EventPreMonitorHandler::MonitorCollection monitorCollection; 330 HandleEventType eventType = HANDLE_EVENT_TYPE_PRE_KEY; 331 std::vector<int32_t> keys = {1, 2, 3}; 332 monitorCollection.sessionHandlers_[keys] = std::list<std::shared_ptr<EventPreMonitorHandler::SessionHandler>>(); 333 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 334 auto sessionHandler = std::make_shared<EventPreMonitorHandler::SessionHandler>(session, 1, eventType, keys); 335 ASSERT_NO_FATAL_FAILURE(monitorCollection.RemoveMonitor(session, 1)); 336 ASSERT_NO_FATAL_FAILURE(monitorCollection.RemoveMonitor(session, 2)); 337 monitorCollection.sessionHandlers_[keys].push_back(sessionHandler); 338 ASSERT_NO_FATAL_FAILURE(monitorCollection.RemoveMonitor(session, 1)); 339 ASSERT_NO_FATAL_FAILURE(monitorCollection.RemoveMonitor(session, 2)); 340 SessionPtr session2 = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 341 eventType = 1; 342 sessionHandler = std::make_shared<EventPreMonitorHandler::SessionHandler>(session2, 1, eventType, keys); 343 monitorCollection.sessionHandlers_[keys].push_back(sessionHandler); 344 ASSERT_NO_FATAL_FAILURE(monitorCollection.RemoveMonitor(session2, 1)); 345 } 346 347 /** 348 * @tc.name: EventPreMonitorHandlerTest_IsEqualsKeys_003 349 * @tc.desc: Verify the invalid and valid event type of IsEqualsKeys 350 * @tc.type: FUNC 351 * @tc.require: 352 */ 353 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_IsEqualsKeys_001, TestSize.Level1) 354 { 355 CALL_TEST_DEBUG; 356 EventPreMonitorHandler::MonitorCollection monitorCollection; 357 std::vector<int32_t> newKeys; 358 std::vector<int32_t> oldKeys = {1, 2, 3}; 359 ASSERT_FALSE(monitorCollection.IsEqualsKeys(newKeys, oldKeys)); 360 newKeys = {1, 2, 3}; 361 ASSERT_TRUE(monitorCollection.IsEqualsKeys(newKeys, oldKeys)); 362 oldKeys = {1, 2, 3, 4}; 363 ASSERT_FALSE(monitorCollection.IsEqualsKeys(newKeys, oldKeys)); 364 oldKeys = {1, 2, 3}; 365 newKeys = {1, 2, 3, 4}; 366 ASSERT_FALSE(monitorCollection.IsEqualsKeys(newKeys, oldKeys)); 367 } 368 369 /** 370 * @tc.name: EventPreMonitorHandlerTest_SendToClient_001 371 * @tc.desc: Verify the keyEvent and pointerEvent of SendToClient 372 * @tc.type: FUNC 373 * @tc.require: 374 */ 375 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_SendToClient_001, TestSize.Level1) 376 { 377 CALL_TEST_DEBUG; 378 HandleEventType eventType = HANDLE_EVENT_TYPE_PRE_KEY; 379 std::vector<int32_t> keys = {1, 2, 3}; 380 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 381 auto sessionHandler = std::make_shared<EventPreMonitorHandler::SessionHandler>(session, 1, eventType, keys); 382 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create(); 383 NetPacket keyEventPkt(MmiMessageId::ON_PRE_KEY_EVENT); 384 ASSERT_NO_FATAL_FAILURE(sessionHandler->SendToClient(keyEvent, keyEventPkt, 1)); 385 } 386 387 /** 388 * @tc.name: EventPreMonitorHandlerTest_SendToClient_002 389 * @tc.desc: Verify the keyEvent and pointerEvent of SendToClient 390 * @tc.type: FUNC 391 * @tc.require: 392 */ 393 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_SendToClient_002, TestSize.Level1) 394 { 395 CALL_TEST_DEBUG; 396 HandleEventType eventType = HANDLE_EVENT_TYPE_PRE_KEY; 397 std::vector<int32_t> keys = {1, 2, 3}; 398 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 399 auto sessionHandler = std::make_shared<EventPreMonitorHandler::SessionHandler>(session, 1, eventType, keys); 400 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create(); 401 NetPacket keyEventPkt(MmiMessageId::ON_PRE_KEY_EVENT); 402 std::vector<KeyEvent::KeyItem> keyItems; 403 for (int i = 0; i < 400; ++i) { 404 KeyEvent::KeyItem item; 405 item.deviceId_ = i; 406 item.keyCode_ = i; 407 item.downTime_ = i; 408 keyItems.push_back(item); 409 } 410 keyEvent->SetKeyItem(keyItems); 411 ASSERT_NO_FATAL_FAILURE(sessionHandler->SendToClient(keyEvent, keyEventPkt, 1)); 412 } 413 414 /** 415 * @tc.name: EventPreMonitorHandlerTest_AddMonitor_001 416 * @tc.desc: Verify the invalid and valid event type of AddMonitor 417 * @tc.type: FUNC 418 * @tc.require: 419 */ 420 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_AddMonitor_002, TestSize.Level1) 421 { 422 CALL_TEST_DEBUG; 423 EventPreMonitorHandler::MonitorCollection monitorCollection; 424 std::vector<int32_t> keys = {1, 2, 3}; 425 HandleEventType eventType = HANDLE_EVENT_TYPE_PRE_KEY; 426 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 427 auto sessionHandler = std::make_shared<EventPreMonitorHandler::SessionHandler>(session, 1, eventType, keys); 428 for (int i = 0; i < 20; ++i) { 429 std::vector<int32_t> key = {i}; 430 std::list<std::shared_ptr<EventPreMonitorHandler::SessionHandler>> value; 431 value.push_back(std::make_shared<EventPreMonitorHandler::SessionHandler>(session, i, eventType, keys)); 432 monitorCollection.sessionHandlers_[key] = value; 433 } 434 int32_t ret = monitorCollection.AddMonitor(sessionHandler, keys); 435 EXPECT_EQ(ret, RET_ERR); 436 } 437 438 /** 439 * @tc.name: EventPreMonitorHandlerTest_AddMonitor_001 440 * @tc.desc: Verify the invalid and valid event type of AddMonitor 441 * @tc.type: FUNC 442 * @tc.require: 443 */ 444 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_AddMonitor_003, TestSize.Level1) 445 { 446 CALL_TEST_DEBUG; 447 EventPreMonitorHandler::MonitorCollection monitorCollection; 448 std::vector<int32_t> keys = {1, 2, 3}; 449 HandleEventType eventType = HANDLE_EVENT_TYPE_PRE_KEY; 450 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 451 auto sessionHandler = std::make_shared<EventPreMonitorHandler::SessionHandler>(session, 1, eventType, keys); 452 for (int i = 0; i < 15; ++i) { 453 std::vector<int32_t> key = {i}; 454 std::list<std::shared_ptr<EventPreMonitorHandler::SessionHandler>> value; 455 value.push_back(std::make_shared<EventPreMonitorHandler::SessionHandler>(session, i, eventType, keys)); 456 monitorCollection.sessionHandlers_[key] = value; 457 } 458 int32_t ret = monitorCollection.AddMonitor(sessionHandler, keys); 459 EXPECT_EQ(ret, RET_OK); 460 } 461 462 /** 463 * @tc.name: EventPreMonitorHandlerTest_IsEqualsKeys_002 464 * @tc.desc: Verify the invalid and valid event type of IsEqualsKeys 465 * @tc.type: FUNC 466 * @tc.require: 467 */ 468 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_IsEqualsKeys_002, TestSize.Level1) 469 { 470 CALL_TEST_DEBUG; 471 EventPreMonitorHandler::MonitorCollection monitorCollection; 472 std::vector<int32_t> newKeys = {1, 2, 3}; 473 std::vector<int32_t> oldKeys = {4, 5, 6}; 474 bool ret = monitorCollection.IsEqualsKeys(newKeys, oldKeys); 475 ASSERT_FALSE(ret); 476 } 477 478 #ifdef OHOS_BUILD_ENABLE_KEYBOARD 479 /** 480 * @tc.name: EventPreMonitorHandlerTest_HandleEvent_001 481 * @tc.desc: Verify HandleEvent 482 * @tc.type: FUNC 483 * @tc.require: 484 */ 485 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_HandleEvent_001, TestSize.Level1) 486 { 487 CALL_TEST_DEBUG; 488 EventPreMonitorHandler::MonitorCollection monitorCollection; 489 EventPreMonitorHandler eventPreMonitorHandler; 490 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create(); 491 ASSERT_NE(keyEvent, nullptr); 492 StreamBuffer streamBuffer; 493 streamBuffer.rwErrorStatus_ = CircleStreamBuffer::ErrorStatus::ERROR_STATUS_READ; 494 bool ret = monitorCollection.HandleEvent(keyEvent); 495 ASSERT_FALSE(ret); 496 streamBuffer.rwErrorStatus_ = CircleStreamBuffer::ErrorStatus::ERROR_STATUS_OK; 497 ret = monitorCollection.HandleEvent(keyEvent); 498 ASSERT_FALSE(ret); 499 HandleEventType eventType = HANDLE_EVENT_TYPE_PRE_KEY; 500 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 501 std::vector<int32_t> keys = {1, 2, 3}; 502 auto sessionHandler = std::make_shared<EventPreMonitorHandler::SessionHandler>(session, 1, eventType, keys); 503 eventPreMonitorHandler.monitors_.sessionHandlers_[keys] = 504 std::list<std::shared_ptr<EventPreMonitorHandler::SessionHandler>>(); 505 eventPreMonitorHandler.monitors_.sessionHandlers_[keys].push_back(sessionHandler); 506 sessionHandler->keys_ = {1, 2, 3}; 507 keyEvent->SetKeyCode(2); 508 ret = monitorCollection.HandleEvent(keyEvent); 509 ASSERT_FALSE(ret); 510 keyEvent->SetKeyCode(5); 511 ret = monitorCollection.HandleEvent(keyEvent); 512 ASSERT_FALSE(ret); 513 } 514 #endif // OHOS_BUILD_ENABLE_KEYBOARD 515 516 /** 517 * @tc.name: EventPreMonitorHandlerTest_Dump_001 518 * @tc.desc: Verify the invalid and valid event type of Dump 519 * @tc.type: FUNC 520 * @tc.require: 521 */ 522 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_Dump_001, TestSize.Level1) 523 { 524 CALL_TEST_DEBUG; 525 EventPreMonitorHandler::MonitorCollection monitorCollection; 526 int32_t fd = 1; 527 std::vector<std::string> args; 528 ASSERT_NO_FATAL_FAILURE(monitorCollection.Dump(fd, args)); 529 EventPreMonitorHandler eventPreMonitorHandler; 530 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 531 HandleEventType eventType = HANDLE_EVENT_TYPE_PRE_KEY; 532 std::vector<int32_t> keys = {1, 2, 3}; 533 auto sessionHandler = std::make_shared<EventPreMonitorHandler::SessionHandler>(session, 1, eventType, keys); 534 eventPreMonitorHandler.monitors_.sessionHandlers_[keys] = 535 std::list<std::shared_ptr<EventPreMonitorHandler::SessionHandler>>(); 536 eventPreMonitorHandler.monitors_.sessionHandlers_[keys].push_back(sessionHandler); 537 ASSERT_NO_FATAL_FAILURE(monitorCollection.Dump(fd, args)); 538 } 539 540 /** 541 * @tc.name: EventPreMonitorHandlerTest_AddInputHandler_002 542 * @tc.desc: Verify the invalid and valid event type of AddInputHandler 543 * @tc.type: FUNC 544 * @tc.require: 545 */ 546 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_AddInputHandler_002, TestSize.Level1) 547 { 548 CALL_TEST_DEBUG; 549 SessionPtr sess; 550 EventPreMonitorHandler eventPreMonitorHandler; 551 HandleEventType eventType = HANDLE_EVENT_TYPE_ALL; 552 std::vector<int32_t> keys = {1, 2, 3}; 553 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 554 ASSERT_NE(session, nullptr); 555 int32_t ret = eventPreMonitorHandler.AddInputHandler(sess, 1, eventType, keys); 556 EXPECT_EQ(ret, RET_ERR); 557 eventType = HANDLE_EVENT_TYPE_NONE; 558 ret = eventPreMonitorHandler.AddInputHandler(sess, 1, eventType, keys); 559 EXPECT_EQ(ret, RET_ERR); 560 } 561 562 /** 563 * @tc.name: EventPreMonitorHandlerTest_AddInputHandler_003 564 * @tc.desc: Verify the invalid and valid event type of AddInputHandler 565 * @tc.type: FUNC 566 * @tc.require: 567 */ 568 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_AddInputHandler_003, TestSize.Level1) 569 { 570 CALL_TEST_DEBUG; 571 EventPreMonitorHandler eventPreMonitorHandler; 572 SessionPtr sess = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 573 HandleEventType eventType = HANDLE_EVENT_TYPE_NONE; 574 std::vector<int32_t> keys = {1, 2, 3}; 575 ASSERT_NE(sess, nullptr); 576 int32_t ret = eventPreMonitorHandler.AddInputHandler(sess, g_handlerId, eventType, keys); 577 EXPECT_EQ(ret, RET_ERR); 578 eventType = HANDLE_EVENT_TYPE_ALL; 579 ret = eventPreMonitorHandler.AddInputHandler(sess, g_handlerId, eventType, keys); 580 EXPECT_EQ(ret, RET_OK); 581 } 582 583 /** 584 * @tc.name: EventPreMonitorHandlerTest_RemoveInputHandler_002 585 * @tc.desc: Verify the invalid and valid event type of RemoveInputHandler 586 * @tc.type: FUNC 587 * @tc.require: 588 */ 589 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_RemoveInputHandler_002, TestSize.Level1) 590 { 591 CALL_TEST_DEBUG; 592 EventPreMonitorHandler eventPreMonitorHandler; 593 SessionPtr sess = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 594 ASSERT_NO_FATAL_FAILURE(eventPreMonitorHandler.RemoveInputHandler(sess, g_handlerId)); 595 } 596 597 #ifdef OHOS_BUILD_ENABLE_KEYBOARD 598 /** 599 * @tc.name: EventPreMonitorHandlerTest_OnHandleEvent_002 600 * @tc.desc: Test OnHandleEvent 601 * @tc.type: FUNC 602 * @tc.require: 603 */ 604 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_OnHandleEvent_002, TestSize.Level1) 605 { 606 CALL_TEST_DEBUG; 607 EventPreMonitorHandler eventPreMonitorHandler; 608 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create(); 609 ASSERT_NE(keyEvent, nullptr); 610 InputEventHandler inputEventHandler ; 611 inputEventHandler.eventNormalizeHandler_ = std::make_shared<EventNormalizeHandler>(); 612 ASSERT_NE(inputEventHandler.eventNormalizeHandler_, nullptr); 613 keyEvent->SetKeyCode(1); 614 EventNormalizeHandler eventNormalizeHandler; 615 eventNormalizeHandler.currentHandleKeyCode_ = 2; 616 bool ret = eventPreMonitorHandler.OnHandleEvent(keyEvent); 617 ASSERT_FALSE(ret); 618 eventNormalizeHandler.currentHandleKeyCode_ = 1; 619 std::shared_ptr<InputEvent> inputEvent = InputEvent::Create(); 620 EXPECT_NE(inputEvent, nullptr); 621 inputEvent->bitwise_ = 0x00000002; 622 ret = eventPreMonitorHandler.OnHandleEvent(keyEvent); 623 ASSERT_FALSE(ret); 624 inputEvent->bitwise_ = 0x00000000; 625 ret = eventPreMonitorHandler.OnHandleEvent(keyEvent); 626 ASSERT_FALSE(ret); 627 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 628 HandleEventType eventType = HANDLE_EVENT_TYPE_PRE_KEY; 629 std::vector<int32_t> keys = {1, 2, 3}; 630 auto sessionHandler = std::make_shared<EventPreMonitorHandler::SessionHandler>(session, 1, eventType, keys); 631 eventPreMonitorHandler.monitors_.sessionHandlers_[keys] = 632 std::list<std::shared_ptr<EventPreMonitorHandler::SessionHandler>>(); 633 eventPreMonitorHandler.monitors_.sessionHandlers_[keys].push_back(sessionHandler); 634 ret = eventPreMonitorHandler.OnHandleEvent(keyEvent); 635 ASSERT_FALSE(ret); 636 } 637 #endif // OHOS_BUILD_ENABLE_KEYBOARD 638 639 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH) 640 /** 641 * @tc.name: EventPreMonitorHandlerTest_OnHandleEvent_002 642 * @tc.desc: Test Overrides the if (OnHandleEvent(PointerEvent)) branch of the OnHandleEvent function 643 * @tc.type: FUNC 644 * @tc.require: 645 */ 646 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_HandleEvent_002, TestSize.Level1) 647 { 648 CALL_TEST_DEBUG; 649 EventPreMonitorHandler::MonitorCollection monitorCollection; 650 std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create(); 651 ASSERT_NE(pointerEvent, nullptr); 652 bool ret = monitorCollection.HandleEvent(pointerEvent); 653 ASSERT_FALSE(ret); 654 } 655 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH 656 } // namespace MMI 657 } // namespace OHOS 658