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_moduleType = 3; 31 int32_t g_pid = 0; 32 int32_t g_writeFd = -1; 33 } // namespace 34 35 class EventPreMonitorHandlerTest : public testing::Test { 36 public: SetUpTestCase(void)37 static void SetUpTestCase(void) {} TearDownTestCase(void)38 static void TearDownTestCase(void) {} 39 }; 40 41 class MyInputEventConsumer : public IInputEventHandler::IInputEventConsumer { 42 public: OnInputEvent(InputHandlerType type,std::shared_ptr<KeyEvent> event) const43 void OnInputEvent(InputHandlerType type, std::shared_ptr<KeyEvent> event) const override {} OnInputEvent(InputHandlerType type,std::shared_ptr<PointerEvent> event) const44 void OnInputEvent(InputHandlerType type, std::shared_ptr<PointerEvent> event) const override {} OnInputEvent(InputHandlerType type,std::shared_ptr<AxisEvent> event) const45 void OnInputEvent(InputHandlerType type, std::shared_ptr<AxisEvent> event) const override {} 46 }; 47 48 /** 49 * @tc.name: EventPreMonitorHandlerTest_HandleKeyEvent_001 50 * @tc.desc: Test Overrides the if (HandleKeyEvent(keyEvent)) branch of the HandleKeyEvent function 51 * @tc.type: FUNC 52 * @tc.require: 53 */ 54 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_HandleKeyEvent_001, TestSize.Level1) 55 { 56 CALL_TEST_DEBUG; 57 EventPreMonitorHandler eventPreMonitorHandler; 58 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create(); 59 ASSERT_NE(keyEvent, nullptr); 60 ASSERT_NO_FATAL_FAILURE(eventPreMonitorHandler.HandleKeyEvent(keyEvent)); 61 } 62 63 /** 64 * @tc.name: EventPreMonitorHandlerTest_HandlePointerEvent_001 65 * @tc.desc: Test Overrides the if (HandlePointerEvent(pointerEvent)) branch of the HandlePointerEvent function 66 * @tc.type: FUNC 67 * @tc.require: 68 */ 69 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_HandlePointerEvent_001, TestSize.Level1) 70 { 71 CALL_TEST_DEBUG; 72 EventPreMonitorHandler eventPreMonitorHandler; 73 std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create(); 74 ASSERT_NE(pointerEvent, nullptr); 75 ASSERT_NO_FATAL_FAILURE(eventPreMonitorHandler.HandlePointerEvent(pointerEvent)); 76 } 77 78 /** 79 * @tc.name: EventPreMonitorHandlerTest_HandleTouchEvent_001 80 * @tc.desc: Test Overrides the if (HandleTouchEvent(pointerEvent)) branch of the HandleTouchEvent function 81 * @tc.type: FUNC 82 * @tc.require: 83 */ 84 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_HandleTouchEvent_001, TestSize.Level1) 85 { 86 CALL_TEST_DEBUG; 87 EventPreMonitorHandler eventPreMonitorHandler; 88 std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create(); 89 ASSERT_NE(pointerEvent, nullptr); 90 ASSERT_NO_FATAL_FAILURE(eventPreMonitorHandler.HandleTouchEvent(pointerEvent)); 91 } 92 93 /** 94 * @tc.name: EventPreMonitorHandlerTest_OnHandleEvent_001 95 * @tc.desc: Test Overrides the if (OnHandleEvent(keyEvent)) branch of the OnHandleEvent function 96 * @tc.type: FUNC 97 * @tc.require: 98 */ 99 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_OnHandleEvent_001, TestSize.Level1) 100 { 101 CALL_TEST_DEBUG; 102 EventPreMonitorHandler eventPreMonitorHandler; 103 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create(); 104 ASSERT_NE(keyEvent, nullptr); 105 keyEvent->SetKeyCode(KeyEvent::KEYCODE_VOLUME_UP); 106 bool ret = eventPreMonitorHandler.OnHandleEvent(keyEvent); 107 ASSERT_FALSE(ret); 108 keyEvent->bitwise_ = InputEvent::EVENT_FLAG_NO_MONITOR; 109 ASSERT_FALSE(eventPreMonitorHandler.OnHandleEvent(keyEvent)); 110 keyEvent->bitwise_ = InputEvent::EVENT_FLAG_NONE; 111 ASSERT_FALSE(eventPreMonitorHandler.OnHandleEvent(keyEvent)); 112 } 113 114 /** 115 * @tc.name: EventPreMonitorHandlerTest_InitSessionLostCallback_001 116 * @tc.desc: Verify the invalid and valid event type of InitSessionLostCallback 117 * @tc.type: FUNC 118 * @tc.require: 119 */ 120 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_InitSessionLostCallback_001, TestSize.Level1) 121 { 122 CALL_TEST_DEBUG; 123 EventPreMonitorHandler eventPreMonitorHandler; 124 eventPreMonitorHandler.sessionLostCallbackInitialized_ = true; 125 eventPreMonitorHandler.InitSessionLostCallback(); 126 eventPreMonitorHandler.sessionLostCallbackInitialized_ = false; 127 UDSServer udSever; 128 InputHandler->udsServer_ = &udSever; 129 auto udsServerPtr = InputHandler->GetUDSServer(); 130 EXPECT_NE(udsServerPtr, nullptr); 131 eventPreMonitorHandler.InitSessionLostCallback(); 132 InputHandler->udsServer_ = nullptr; 133 } 134 135 /** 136 * @tc.name: EventPreMonitorHandlerTest_AddInputHandler_001 137 * @tc.desc: Verify the invalid and valid event type of AddInputHandler 138 * @tc.type: FUNC 139 * @tc.require: 140 */ 141 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_AddInputHandler_001, TestSize.Level1) 142 { 143 CALL_TEST_DEBUG; 144 SessionPtr sess; 145 EventPreMonitorHandler eventPreMonitorHandler; 146 HandleEventType eventType = HANDLE_EVENT_TYPE_PRE_KEY; 147 std::vector<int32_t> keys = {1, 2, 3}; 148 int32_t ret = eventPreMonitorHandler.AddInputHandler(sess, 1, eventType, keys); 149 EXPECT_EQ(ret, RET_ERR); 150 eventType = HANDLE_EVENT_TYPE_NONE; 151 ret = eventPreMonitorHandler.AddInputHandler(sess, 1, eventType, keys); 152 EXPECT_EQ(ret, -1); 153 } 154 155 /** 156 * @tc.name: EventPreMonitorHandlerTest_RemoveInputHandler_001 157 * @tc.desc: Verify the invalid and valid event type of RemoveInputHandler 158 * @tc.type: FUNC 159 * @tc.require: 160 */ 161 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_RemoveInputHandler_001, TestSize.Level1) 162 { 163 CALL_TEST_DEBUG; 164 EventPreMonitorHandler eventPreMonitorHandler; 165 SessionPtr sess; 166 ASSERT_NO_FATAL_FAILURE(eventPreMonitorHandler.RemoveInputHandler(sess, 1)); 167 } 168 169 /** 170 * @tc.name: EventPreMonitorHandlerTest_OnSessionLost 171 * @tc.desc: Test OnSessionLost 172 * @tc.type: FUNC 173 * @tc.require: 174 */ 175 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_OnSessionLost, TestSize.Level1) 176 { 177 CALL_TEST_DEBUG; 178 EventPreMonitorHandler eventPreMonitorHandler; 179 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 180 HandleEventType eventType = HANDLE_EVENT_TYPE_PRE_KEY; 181 std::vector<int32_t> keys = {1, 2, 3}; 182 auto sessionHandler = std::make_shared<EventPreMonitorHandler::SessionHandler>(session, 1, eventType, keys); 183 eventPreMonitorHandler.monitors_.sessionHandlers_[keys] = 184 std::list<std::shared_ptr<EventPreMonitorHandler::SessionHandler>>(); 185 eventPreMonitorHandler.monitors_.sessionHandlers_[keys].push_back(sessionHandler); 186 ASSERT_NO_FATAL_FAILURE(eventPreMonitorHandler.OnSessionLost(session)); 187 } 188 189 /** 190 * @tc.name: EventPreMonitorHandlerTest_OnSessionLost_001 191 * @tc.desc: Test OnSessionLost 192 * @tc.type: FUNC 193 * @tc.require: 194 */ 195 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_OnSessionLost_001, TestSize.Level1) 196 { 197 CALL_TEST_DEBUG; 198 EventPreMonitorHandler eventPreMonitorHandler; 199 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 200 ASSERT_NO_FATAL_FAILURE(eventPreMonitorHandler.OnSessionLost(session)); 201 } 202 203 /** 204 * @tc.name: EventPreMonitorHandlerTest_AddMonitor_001 205 * @tc.desc: Verify the invalid and valid event type of AddMonitor 206 * @tc.type: FUNC 207 * @tc.require: 208 */ 209 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_AddMonitor_001, TestSize.Level1) 210 { 211 CALL_TEST_DEBUG; 212 EventPreMonitorHandler::MonitorCollection monitorCollection; 213 HandleEventType eventType = HANDLE_EVENT_TYPE_PRE_KEY; 214 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 215 std::vector<int32_t> keys = {1, 2, 3}; 216 auto sessionHandler = std::make_shared<EventPreMonitorHandler::SessionHandler>(session, 1, eventType, keys); 217 monitorCollection.sessionHandlers_[keys] = std::list<std::shared_ptr<EventPreMonitorHandler::SessionHandler>>(); 218 monitorCollection.sessionHandlers_[keys].push_back(sessionHandler); 219 for (int i = 0; i < MAX_N_INPUT_MONITORS - 2; i++) { 220 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 221 auto sessionHandler = std::make_shared<EventPreMonitorHandler::SessionHandler>(session, 1, eventType, keys); 222 monitorCollection.sessionHandlers_[keys].push_back(sessionHandler); 223 } 224 int32_t ret = monitorCollection.AddMonitor(sessionHandler, keys); 225 EXPECT_EQ(ret, RET_OK); 226 SessionPtr session2 = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 227 auto sessionHandler2 = std::make_shared<EventPreMonitorHandler::SessionHandler>(session2, 1, eventType, keys); 228 monitorCollection.sessionHandlers_[keys].push_back(sessionHandler2); 229 ret = monitorCollection.AddMonitor(sessionHandler2, keys); 230 EXPECT_EQ(ret, RET_OK); 231 } 232 233 /** 234 * @tc.name: EventPreMonitorHandlerTest_RemoveMonitor_001 235 * @tc.desc: Verify the invalid and valid event type of RemoveMonitor 236 * @tc.type: FUNC 237 * @tc.require: 238 */ 239 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_RemoveMonitor_001, TestSize.Level1) 240 { 241 CALL_TEST_DEBUG; 242 EventPreMonitorHandler::MonitorCollection monitorCollection; 243 HandleEventType eventType = HANDLE_EVENT_TYPE_PRE_KEY; 244 std::vector<int32_t> keys = {1, 2, 3}; 245 monitorCollection.sessionHandlers_[keys] = std::list<std::shared_ptr<EventPreMonitorHandler::SessionHandler>>(); 246 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 247 auto sessionHandler = std::make_shared<EventPreMonitorHandler::SessionHandler>(session, 1, eventType, keys); 248 ASSERT_NO_FATAL_FAILURE(monitorCollection.RemoveMonitor(session, 1)); 249 ASSERT_NO_FATAL_FAILURE(monitorCollection.RemoveMonitor(session, 2)); 250 monitorCollection.sessionHandlers_[keys].push_back(sessionHandler); 251 ASSERT_NO_FATAL_FAILURE(monitorCollection.RemoveMonitor(session, 1)); 252 ASSERT_NO_FATAL_FAILURE(monitorCollection.RemoveMonitor(session, 2)); 253 SessionPtr session2 = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 254 eventType = 1; 255 sessionHandler = std::make_shared<EventPreMonitorHandler::SessionHandler>(session2, 1, eventType, keys); 256 monitorCollection.sessionHandlers_[keys].push_back(sessionHandler); 257 ASSERT_NO_FATAL_FAILURE(monitorCollection.RemoveMonitor(session2, 1)); 258 } 259 260 /** 261 * @tc.name: EventPreMonitorHandlerTest_IsEqualsKeys_001 262 * @tc.desc: Verify the invalid and valid event type of IsEqualsKeys 263 * @tc.type: FUNC 264 * @tc.require: 265 */ 266 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_IsEqualsKeys_001, TestSize.Level1) 267 { 268 CALL_TEST_DEBUG; 269 EventPreMonitorHandler::MonitorCollection monitorCollection; 270 std::vector<int32_t> newKeys; 271 std::vector<int32_t> oldKeys = {1, 2, 3}; 272 ASSERT_FALSE(monitorCollection.IsEqualsKeys(newKeys, oldKeys)); 273 newKeys = {1, 2, 3}; 274 ASSERT_TRUE(monitorCollection.IsEqualsKeys(newKeys, oldKeys)); 275 oldKeys = {1, 2, 3, 4}; 276 ASSERT_FALSE(monitorCollection.IsEqualsKeys(newKeys, oldKeys)); 277 oldKeys = {1, 2, 3}; 278 newKeys = {1, 2, 3, 4}; 279 ASSERT_FALSE(monitorCollection.IsEqualsKeys(newKeys, oldKeys)); 280 } 281 282 /** 283 * @tc.name: EventPreMonitorHandlerTest_SendToClient_001 284 * @tc.desc: Verify the keyEvent and pointerEvent of SendToClient 285 * @tc.type: FUNC 286 * @tc.require: 287 */ 288 HWTEST_F(EventPreMonitorHandlerTest, EventPreMonitorHandlerTest_SendToClient_001, TestSize.Level1) 289 { 290 CALL_TEST_DEBUG; 291 HandleEventType eventType = HANDLE_EVENT_TYPE_PRE_KEY; 292 std::vector<int32_t> keys = {1, 2, 3}; 293 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid); 294 auto sessionHandler = std::make_shared<EventPreMonitorHandler::SessionHandler>(session, 1, eventType, keys); 295 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create(); 296 NetPacket keyEventPkt(MmiMessageId::ON_PRE_KEY_EVENT); 297 ASSERT_NO_FATAL_FAILURE(sessionHandler->SendToClient(keyEvent, keyEventPkt, 1)); 298 } 299 } // namespace MMI 300 } // namespace OHOS 301