1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include <gmock/gmock.h> 17 #include <gtest/gtest.h> 18 19 #include "define_multimodal.h" 20 #include "error_multimodal.h" 21 #include "fingerprint_event_processor.h" 22 #include "input_event_handler.h" 23 #include "libinput_mock.h" 24 #include "special_input_device_parser.h" 25 #include "mmi_log.h" 26 27 #undef MMI_LOG_TAG 28 #define MMI_LOG_TAG "FingerprintEventProcessorTest" 29 30 namespace OHOS { 31 namespace MMI { 32 namespace { 33 using namespace testing; 34 using namespace testing::ext; 35 const std::string FINGERPRINT_MOUSE { SPECIAL_INPUT_DEVICE_PARSER.GetInputDevName("FINGER_PRINT_MOUSE")} ; 36 } 37 class FingerprintEventProcessorTest : public testing::Test { 38 public: SetUpTestCase(void)39 static void SetUpTestCase(void){}; TearDownTestCase(void)40 static void TearDownTestCase(void){}; SetUp()41 void SetUp(){}; TearDown()42 void TearDown(){}; 43 }; 44 45 #ifdef OHOS_BUILD_ENABLE_FINGERPRINT 46 /** 47 * @tc.name: FingerprintEventProcessorTest_IsFingerprintEvent_EventIsNull 48 * @tc.desc: Test IsFingerprintEvent 49 * @tc.type: FUNC 50 * @tc.require: 51 */ 52 HWTEST_F(FingerprintEventProcessorTest, FingerprintEventProcessorTest_IsFingerprintEvent_EventIsNull, TestSize.Level1) 53 { 54 struct libinput_event* event = NULL; 55 EXPECT_FALSE(FingerprintEventHdr->IsFingerprintEvent(event)); 56 } 57 58 /** 59 * @tc.name: FingerprintEventProcessorTest_IsFingerprintEvent_NameIsNotFingerprint 60 * @tc.desc: Test IsFingerprintEvent 61 * @tc.type: FUNC 62 * @tc.require: 63 */ 64 HWTEST_F(FingerprintEventProcessorTest, 65 FingerprintEventProcessorTest_IsFingerprintEvent_NameIsNotFingerprint, TestSize.Level1) 66 { 67 NiceMock<LibinputInterfaceMock> mock; 68 struct libinput_event event; 69 struct libinput_device device; 70 EXPECT_CALL(mock, GetDevice) 71 .WillRepeatedly(Return(&device)); 72 EXPECT_CALL(mock, DeviceGetName) 73 .WillOnce(Return(const_cast<char*>("not_fingerprint_source_key"))) 74 .WillOnce(Return(const_cast<char*>(FINGERPRINT_MOUSE.c_str()))); 75 EXPECT_FALSE(FingerprintEventHdr->IsFingerprintEvent(&event)); 76 EXPECT_TRUE(FingerprintEventHdr->IsFingerprintEvent(&event)); 77 } 78 79 /** 80 * @tc.name: FingerprintEventProcessorTest_IsFingerprintEvent_NameIsFingerprintSourceKey_001 81 * @tc.desc: Test IsFingerprintEvent 82 * @tc.type: FUNC 83 * @tc.require: 84 */ 85 HWTEST_F(FingerprintEventProcessorTest, 86 FingerprintEventProcessorTest_IsFingerprintEvent_NameIsFingerprintSourceKey_001, TestSize.Level1) 87 { 88 NiceMock<LibinputInterfaceMock> mock; 89 struct libinput_event event; 90 struct libinput_device device; 91 struct libinput_event_keyboard keyBoardEvent; 92 EXPECT_CALL(mock, GetDevice) 93 .WillOnce(Return(&device)); 94 EXPECT_CALL(mock, DeviceGetName) 95 .WillOnce(Return(const_cast<char*>("fingerprint"))); 96 EXPECT_CALL(mock, LibinputEventGetKeyboardEvent) 97 .WillOnce(Return(&keyBoardEvent)); 98 EXPECT_CALL(mock, LibinputEventKeyboardGetKey) 99 .WillOnce(Return(100)); 100 EXPECT_FALSE(FingerprintEventHdr->IsFingerprintEvent(&event)); 101 } 102 103 /** 104 * @tc.name: FingerprintEventProcessorTest_IsFingerprintEvent_NameIsFingerprintSourceKey_002 105 * @tc.desc: Test IsFingerprintEvent 106 * @tc.type: FUNC 107 * @tc.require: 108 */ 109 HWTEST_F(FingerprintEventProcessorTest, 110 FingerprintEventProcessorTest_IsFingerprintEvent_NameIsFingerprintSourceKey_002, TestSize.Level1) 111 { 112 NiceMock<LibinputInterfaceMock> mock; 113 struct libinput_event event; 114 struct libinput_device device; 115 struct libinput_event_keyboard keyBoardEvent; 116 EXPECT_CALL(mock, GetDevice) 117 .WillRepeatedly(Return(&device)); 118 EXPECT_CALL(mock, DeviceGetName) 119 .WillRepeatedly(Return(const_cast<char*>("fingerprint"))); 120 EXPECT_CALL(mock, LibinputEventGetKeyboardEvent) 121 .WillRepeatedly(Return(&keyBoardEvent)); 122 EXPECT_CALL(mock, LibinputEventKeyboardGetKey) 123 .WillOnce(Return(FingerprintEventProcessor::FINGERPRINT_CODE_DOWN)) 124 .WillOnce(Return(FingerprintEventProcessor::FINGERPRINT_CODE_UP)) 125 .WillOnce(Return(FingerprintEventProcessor::FINGERPRINT_CODE_CLICK)) 126 .WillOnce(Return(FingerprintEventProcessor::FINGERPRINT_CODE_RETOUCH)); 127 EXPECT_TRUE(FingerprintEventHdr->IsFingerprintEvent(&event)); 128 EXPECT_TRUE(FingerprintEventHdr->IsFingerprintEvent(&event)); 129 EXPECT_TRUE(FingerprintEventHdr->IsFingerprintEvent(&event)); 130 EXPECT_TRUE(FingerprintEventHdr->IsFingerprintEvent(&event)); 131 } 132 133 /** 134 * @tc.name: FingerprintEventProcessorTest_HandleFingerprintEvent_NameIsFingerprintSourceKey 135 * @tc.desc: Test HandleFingerprintEvent 136 * @tc.type: FUNC 137 * @tc.require: 138 */ 139 HWTEST_F(FingerprintEventProcessorTest, 140 FingerprintEventProcessorTest_HandleFingerprintEvent_NameIsFingerprintSourceKey, TestSize.Level1) 141 { 142 NiceMock<LibinputInterfaceMock> mock; 143 struct libinput_event event; 144 struct libinput_device device; 145 struct libinput_event_keyboard keyBoardEvent; 146 EXPECT_CALL(mock, GetDevice) 147 .WillOnce(Return(&device)); 148 EXPECT_CALL(mock, DeviceGetName) 149 .WillOnce(Return(const_cast<char*>("fingerprint"))); 150 EXPECT_CALL(mock, LibinputEventGetKeyboardEvent) 151 .WillOnce(Return(&keyBoardEvent)); 152 EXPECT_CALL(mock, LibinputEventKeyboardGetKey) 153 .WillOnce(Return(100)); 154 EXPECT_CALL(mock, LibinputEventKeyboardGetKeyState) 155 .WillOnce(Return(LIBINPUT_KEY_STATE_PRESSED)); 156 EXPECT_EQ(FingerprintEventHdr->HandleFingerprintEvent(&event), ERR_OK); 157 } 158 159 /** 160 * @tc.name: FingerprintEventProcessorTest_HandleFingerprintEvent_NameIsFingerprintSourcePoint 161 * @tc.desc: Test HandleFingerprintEvent 162 * @tc.type: FUNC 163 * @tc.require: 164 */ 165 HWTEST_F(FingerprintEventProcessorTest, 166 FingerprintEventProcessorTest_HandleFingerprintEvent_NameIsFingerprintSourcePoint, TestSize.Level1) 167 { 168 NiceMock<LibinputInterfaceMock> mock; 169 struct libinput_event event; 170 struct libinput_device device; 171 struct libinput_event_keyboard keyBoardEvent; 172 struct libinput_event_pointer rawPointerEvent; 173 InputHandler->BuildInputHandlerChain(); 174 EXPECT_CALL(mock, GetDevice) 175 .WillOnce(Return(&device)); 176 EXPECT_CALL(mock, DeviceGetName) 177 .WillOnce(Return(const_cast<char*>(FINGERPRINT_MOUSE.c_str()))); 178 EXPECT_CALL(mock, LibinputGetPointerEvent) 179 .WillOnce(Return(&rawPointerEvent)); 180 EXPECT_EQ(FingerprintEventHdr->HandleFingerprintEvent(&event), RET_OK); 181 } 182 183 /** 184 * @tc.name: FingerprintEventProcessorTest_HandleFingerprintEvent_NameNotFingerprintSourceKey_001 185 * @tc.desc: Test HandleFingerprintEvent 186 * @tc.type: FUNC 187 * @tc.require: 188 */ 189 HWTEST_F(FingerprintEventProcessorTest, 190 FingerprintEventProcessorTest_HandleFingerprintEvent_NameNotFingerprintSourceKey_001, TestSize.Level1) 191 { 192 NiceMock<LibinputInterfaceMock> mock; 193 struct libinput_event event; 194 struct libinput_device device; 195 struct libinput_event_keyboard keyBoardEvent; 196 EXPECT_CALL(mock, GetDevice) 197 .WillOnce(Return(&device)); 198 EXPECT_CALL(mock, DeviceGetName) 199 .WillOnce(Return(const_cast<char*>("not_fingerprint_source_key"))); 200 EXPECT_EQ(FingerprintEventHdr->HandleFingerprintEvent(&event), MMI::PARAM_INPUT_INVALID); 201 } 202 203 /** 204 * @tc.name: FingerprintEventProcessorTest_AnalyseKeyEvent_StateIsLibinputKeyStatePressed 205 * @tc.desc: Test AnalyseKeyEvent 206 * @tc.type: FUNC 207 * @tc.require: 208 */ 209 HWTEST_F(FingerprintEventProcessorTest, 210 FingerprintEventProcessorTest_AnalyseKeyEvent_StateIsLibinputKeyStatePressed, TestSize.Level1) 211 { 212 NiceMock<LibinputInterfaceMock> mock; 213 struct libinput_event event; 214 struct libinput_event_keyboard keyBoardEvent; 215 EXPECT_CALL(mock, LibinputEventGetKeyboardEvent) 216 .WillOnce(Return(&keyBoardEvent)); 217 EXPECT_CALL(mock, LibinputEventKeyboardGetKey) 218 .WillOnce(Return(FingerprintEventProcessor::FINGERPRINT_CODE_DOWN)); 219 EXPECT_CALL(mock, LibinputEventKeyboardGetKeyState) 220 .WillOnce(Return(LIBINPUT_KEY_STATE_PRESSED)); 221 EXPECT_EQ(FingerprintEventHdr->AnalyseKeyEvent(&event), ERR_OK); 222 } 223 224 /** 225 * @tc.name: FingerprintEventProcessorTest_AnalyseKeyEvent_StateNotLibinputKeyStatePressed 226 * @tc.desc: Test AnalyseKeyEvent 227 * @tc.type: FUNC 228 * @tc.require: 229 */ 230 HWTEST_F(FingerprintEventProcessorTest, 231 FingerprintEventProcessorTest_AnalyseKeyEvent_StateNotLibinputKeyStatePressed, TestSize.Level1) 232 { 233 NiceMock<LibinputInterfaceMock> mock; 234 struct libinput_event event; 235 struct libinput_event_keyboard keyBoardEvent; 236 EXPECT_CALL(mock, LibinputEventGetKeyboardEvent) 237 .WillRepeatedly(Return(&keyBoardEvent)); 238 EXPECT_CALL(mock, LibinputEventKeyboardGetKey) 239 .WillOnce(Return(FingerprintEventProcessor::FINGERPRINT_CODE_DOWN)) 240 .WillOnce(Return(FingerprintEventProcessor::FINGERPRINT_CODE_UP)) 241 .WillOnce(Return(FingerprintEventProcessor::FINGERPRINT_CODE_RETOUCH)) 242 .WillOnce(Return(FingerprintEventProcessor::FINGERPRINT_CODE_CLICK)) 243 .WillOnce(Return(100)); 244 EXPECT_CALL(mock, LibinputEventKeyboardGetKeyState) 245 .WillRepeatedly(Return(LIBINPUT_KEY_STATE_RELEASED)); 246 EXPECT_EQ(FingerprintEventHdr->AnalyseKeyEvent(&event), ERR_OK); 247 EXPECT_EQ(FingerprintEventHdr->AnalyseKeyEvent(&event), ERR_OK); 248 EXPECT_EQ(FingerprintEventHdr->AnalyseKeyEvent(&event), ERR_OK); 249 EXPECT_EQ(FingerprintEventHdr->AnalyseKeyEvent(&event), ERR_OK); 250 EXPECT_EQ(FingerprintEventHdr->AnalyseKeyEvent(&event), MMI::UNKNOWN_EVENT); 251 } 252 253 /** 254 * @tc.name: FingerprintEventProcessorTest_SetPowerAndVolumeKeyState_001 255 * @tc.desc: Test HandleFingerprintEvent (keyCode != KEY_POWER) Branch 256 * @tc.type: FUNC 257 * @tc.require: 258 */ 259 HWTEST_F(FingerprintEventProcessorTest, FingerprintEventProcessorTest_SetPowerAndVolumeKeyState_001, TestSize.Level1) 260 { 261 NiceMock<LibinputInterfaceMock> mock; 262 struct libinput_event event; 263 struct libinput_device device; 264 struct libinput_event_keyboard keyBoardEvent; 265 EXPECT_CALL(mock, GetDevice) 266 .WillOnce(Return(&device)); 267 EXPECT_CALL(mock, LibinputEventGetKeyboardEvent) 268 .WillOnce(Return(&keyBoardEvent)); 269 EXPECT_CALL(mock, LibinputEventKeyboardGetKey) 270 .WillOnce(Return(100)); 271 ASSERT_NO_FATAL_FAILURE(FingerprintEventHdr->SetPowerAndVolumeKeyState(&event)); 272 } 273 274 /** 275 * @tc.name: FingerprintEventProcessorTest_SetPowerAndVolumeKeyState_002 276 * @tc.desc: Test HandleFingerprintEvent (keyAction == KeyEvent::KEY_ACTION_DOWN) Branch 277 * @tc.type: FUNC 278 * @tc.require: 279 */ 280 HWTEST_F(FingerprintEventProcessorTest, FingerprintEventProcessorTest_SetPowerAndVolumeKeyState_002, TestSize.Level1) 281 { 282 NiceMock<LibinputInterfaceMock> mock; 283 struct libinput_event event; 284 struct libinput_device device; 285 struct libinput_event_keyboard keyBoardEvent; 286 EXPECT_CALL(mock, GetDevice) 287 .WillOnce(Return(&device)); 288 EXPECT_CALL(mock, LibinputEventGetKeyboardEvent) 289 .WillOnce(Return(&keyBoardEvent)); 290 EXPECT_CALL(mock, LibinputEventKeyboardGetKey) 291 .WillOnce(Return(116)); 292 EXPECT_CALL(mock, LibinputEventKeyboardGetKeyState) 293 .WillOnce(Return(LIBINPUT_KEY_STATE_PRESSED)); 294 ASSERT_NO_FATAL_FAILURE(FingerprintEventHdr->SetPowerAndVolumeKeyState(&event)); 295 } 296 297 /** 298 * @tc.name: FingerprintEventProcessorTest_SetPowerAndVolumeKeyState_003 299 * @tc.desc: Test HandleFingerprintEvent (keyAction != KeyEvent::KEY_ACTION_DOWN) Branch 300 * @tc.type: FUNC 301 * @tc.require: 302 */ 303 HWTEST_F(FingerprintEventProcessorTest, FingerprintEventProcessorTest_SetPowerAndVolumeKeyState_003, TestSize.Level1) 304 { 305 NiceMock<LibinputInterfaceMock> mock; 306 struct libinput_event event; 307 struct libinput_device device; 308 struct libinput_event_keyboard keyBoardEvent; 309 EXPECT_CALL(mock, GetDevice) 310 .WillOnce(Return(&device)); 311 EXPECT_CALL(mock, LibinputEventGetKeyboardEvent) 312 .WillOnce(Return(&keyBoardEvent)); 313 EXPECT_CALL(mock, LibinputEventKeyboardGetKey) 314 .WillOnce(Return(116)); 315 EXPECT_CALL(mock, LibinputEventKeyboardGetKeyState) 316 .WillOnce(Return(LIBINPUT_KEY_STATE_RELEASED)); 317 ASSERT_NO_FATAL_FAILURE(FingerprintEventHdr->SetPowerAndVolumeKeyState(&event)); 318 } 319 320 #endif // OHOS_BUILD_ENABLE_FINGERPRINT 321 } // namespace MMI 322 } // namespace OHOS