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 "mmi_log.h"
25 
26 #undef MMI_LOG_TAG
27 #define MMI_LOG_TAG "FingerprintEventProcessorTest"
28 
29 namespace OHOS {
30 namespace MMI {
31 namespace {
32 using namespace testing;
33 using namespace testing::ext;
34 }
35 class FingerprintEventProcessorTest : public testing::Test {
36 public:
SetUpTestCase(void)37     static void SetUpTestCase(void){};
TearDownTestCase(void)38     static void TearDownTestCase(void){};
SetUp()39     void SetUp(){};
TearDown()40     void TearDown(){};
41 };
42 
43 #ifdef OHOS_BUILD_ENABLE_FINGERPRINT
44 /**
45  * @tc.name: FingerprintEventProcessorTest_IsFingerprintEvent_EventIsNull
46  * @tc.desc: Test IsFingerprintEvent
47  * @tc.type: FUNC
48  * @tc.require:
49  */
50 HWTEST_F(FingerprintEventProcessorTest, FingerprintEventProcessorTest_IsFingerprintEvent_EventIsNull, TestSize.Level1)
51 {
52     struct libinput_event* event = NULL;
53     EXPECT_FALSE(FingerprintEventHdr->IsFingerprintEvent(event));
54 }
55 
56 /**
57  * @tc.name: FingerprintEventProcessorTest_IsFingerprintEvent_NameIsNotFingerprint
58  * @tc.desc: Test IsFingerprintEvent
59  * @tc.type: FUNC
60  * @tc.require:
61  */
62 HWTEST_F(FingerprintEventProcessorTest,
63     FingerprintEventProcessorTest_IsFingerprintEvent_NameIsNotFingerprint, TestSize.Level1)
64 {
65     NiceMock<LibinputInterfaceMock> mock;
66     struct libinput_event event;
67     struct libinput_device device;
68     EXPECT_CALL(mock, GetDevice)
69         .WillRepeatedly(Return(&device));
70     EXPECT_CALL(mock, DeviceGetName)
71         .WillOnce(Return(const_cast<char*>("not_fingerprint_source_key")))
72         .WillOnce(Return(const_cast<char*>("hw_fingerprint_mouse")));
73     EXPECT_FALSE(FingerprintEventHdr->IsFingerprintEvent(&event));
74     EXPECT_TRUE(FingerprintEventHdr->IsFingerprintEvent(&event));
75 }
76 
77 /**
78  * @tc.name: FingerprintEventProcessorTest_IsFingerprintEvent_NameIsFingerprintSourceKey_001
79  * @tc.desc: Test IsFingerprintEvent
80  * @tc.type: FUNC
81  * @tc.require:
82  */
83 HWTEST_F(FingerprintEventProcessorTest,
84     FingerprintEventProcessorTest_IsFingerprintEvent_NameIsFingerprintSourceKey_001, TestSize.Level1)
85 {
86     NiceMock<LibinputInterfaceMock> mock;
87     struct libinput_event event;
88     struct libinput_device device;
89     struct libinput_event_keyboard keyBoardEvent;
90     EXPECT_CALL(mock, GetDevice)
91         .WillOnce(Return(&device));
92     EXPECT_CALL(mock, DeviceGetName)
93         .WillOnce(Return(const_cast<char*>("fingerprint")));
94     EXPECT_CALL(mock, LibinputEventGetKeyboardEvent)
95         .WillOnce(Return(&keyBoardEvent));
96     EXPECT_CALL(mock, LibinputEventKeyboardGetKey)
97         .WillOnce(Return(100));
98     EXPECT_FALSE(FingerprintEventHdr->IsFingerprintEvent(&event));
99 }
100 
101 /**
102  * @tc.name: FingerprintEventProcessorTest_IsFingerprintEvent_NameIsFingerprintSourceKey_002
103  * @tc.desc: Test IsFingerprintEvent
104  * @tc.type: FUNC
105  * @tc.require:
106  */
107 HWTEST_F(FingerprintEventProcessorTest,
108     FingerprintEventProcessorTest_IsFingerprintEvent_NameIsFingerprintSourceKey_002, TestSize.Level1)
109 {
110     NiceMock<LibinputInterfaceMock> mock;
111     struct libinput_event event;
112     struct libinput_device device;
113     struct libinput_event_keyboard keyBoardEvent;
114     EXPECT_CALL(mock, GetDevice)
115         .WillRepeatedly(Return(&device));
116     EXPECT_CALL(mock, DeviceGetName)
117         .WillRepeatedly(Return(const_cast<char*>("fingerprint")));
118     EXPECT_CALL(mock, LibinputEventGetKeyboardEvent)
119         .WillRepeatedly(Return(&keyBoardEvent));
120     EXPECT_CALL(mock, LibinputEventKeyboardGetKey)
121         .WillOnce(Return(FingerprintEventProcessor::FINGERPRINT_CODE_DOWN))
122         .WillOnce(Return(FingerprintEventProcessor::FINGERPRINT_CODE_UP))
123         .WillOnce(Return(FingerprintEventProcessor::FINGERPRINT_CODE_CLICK))
124         .WillOnce(Return(FingerprintEventProcessor::FINGERPRINT_CODE_RETOUCH));
125     EXPECT_TRUE(FingerprintEventHdr->IsFingerprintEvent(&event));
126     EXPECT_TRUE(FingerprintEventHdr->IsFingerprintEvent(&event));
127     EXPECT_TRUE(FingerprintEventHdr->IsFingerprintEvent(&event));
128     EXPECT_TRUE(FingerprintEventHdr->IsFingerprintEvent(&event));
129 }
130 
131 /**
132  * @tc.name: FingerprintEventProcessorTest_HandleFingerprintEvent_NameIsFingerprintSourceKey
133  * @tc.desc: Test HandleFingerprintEvent
134  * @tc.type: FUNC
135  * @tc.require:
136  */
137 HWTEST_F(FingerprintEventProcessorTest,
138     FingerprintEventProcessorTest_HandleFingerprintEvent_NameIsFingerprintSourceKey, TestSize.Level1)
139 {
140     NiceMock<LibinputInterfaceMock> mock;
141     struct libinput_event event;
142     struct libinput_device device;
143     struct libinput_event_keyboard keyBoardEvent;
144     EXPECT_CALL(mock, GetDevice)
145         .WillOnce(Return(&device));
146     EXPECT_CALL(mock, DeviceGetName)
147         .WillOnce(Return(const_cast<char*>("fingerprint")));
148     EXPECT_CALL(mock, LibinputEventGetKeyboardEvent)
149         .WillOnce(Return(&keyBoardEvent));
150     EXPECT_CALL(mock, LibinputEventKeyboardGetKey)
151         .WillOnce(Return(100));
152     EXPECT_CALL(mock, LibinputEventKeyboardGetKeyState)
153         .WillOnce(Return(LIBINPUT_KEY_STATE_PRESSED));
154     EXPECT_EQ(FingerprintEventHdr->HandleFingerprintEvent(&event), ERR_OK);
155 }
156 
157 /**
158  * @tc.name: FingerprintEventProcessorTest_HandleFingerprintEvent_NameIsFingerprintSourcePoint
159  * @tc.desc: Test HandleFingerprintEvent
160  * @tc.type: FUNC
161  * @tc.require:
162  */
163 HWTEST_F(FingerprintEventProcessorTest,
164     FingerprintEventProcessorTest_HandleFingerprintEvent_NameIsFingerprintSourcePoint, TestSize.Level1)
165 {
166     NiceMock<LibinputInterfaceMock> mock;
167     struct libinput_event event;
168     struct libinput_device device;
169     struct libinput_event_keyboard keyBoardEvent;
170     struct libinput_event_pointer rawPointerEvent;
171     InputHandler->BuildInputHandlerChain();
172     EXPECT_CALL(mock, GetDevice)
173         .WillOnce(Return(&device));
174     EXPECT_CALL(mock, DeviceGetName)
175         .WillOnce(Return(const_cast<char*>("hw_fingerprint_mouse")));
176     EXPECT_CALL(mock, LibinputGetPointerEvent)
177         .WillOnce(Return(&rawPointerEvent));
178     EXPECT_EQ(FingerprintEventHdr->HandleFingerprintEvent(&event), RET_OK);
179 }
180 
181 /**
182  * @tc.name: FingerprintEventProcessorTest_HandleFingerprintEvent_NameNotFingerprintSourceKey_001
183  * @tc.desc: Test HandleFingerprintEvent
184  * @tc.type: FUNC
185  * @tc.require:
186  */
187 HWTEST_F(FingerprintEventProcessorTest,
188     FingerprintEventProcessorTest_HandleFingerprintEvent_NameNotFingerprintSourceKey_001, TestSize.Level1)
189 {
190     NiceMock<LibinputInterfaceMock> mock;
191     struct libinput_event event;
192     struct libinput_device device;
193     struct libinput_event_keyboard keyBoardEvent;
194     EXPECT_CALL(mock, GetDevice)
195         .WillOnce(Return(&device));
196     EXPECT_CALL(mock, DeviceGetName)
197         .WillOnce(Return(const_cast<char*>("not_fingerprint_source_key")));
198     EXPECT_EQ(FingerprintEventHdr->HandleFingerprintEvent(&event), MMI::PARAM_INPUT_INVALID);
199 }
200 
201 /**
202  * @tc.name: FingerprintEventProcessorTest_AnalyseKeyEvent_StateIsLibinputKeyStatePressed
203  * @tc.desc: Test AnalyseKeyEvent
204  * @tc.type: FUNC
205  * @tc.require:
206  */
207 HWTEST_F(FingerprintEventProcessorTest,
208     FingerprintEventProcessorTest_AnalyseKeyEvent_StateIsLibinputKeyStatePressed, TestSize.Level1)
209 {
210     NiceMock<LibinputInterfaceMock> mock;
211     struct libinput_event event;
212     struct libinput_event_keyboard keyBoardEvent;
213     EXPECT_CALL(mock, LibinputEventGetKeyboardEvent)
214         .WillOnce(Return(&keyBoardEvent));
215     EXPECT_CALL(mock, LibinputEventKeyboardGetKey)
216         .WillOnce(Return(FingerprintEventProcessor::FINGERPRINT_CODE_DOWN));
217     EXPECT_CALL(mock, LibinputEventKeyboardGetKeyState)
218         .WillOnce(Return(LIBINPUT_KEY_STATE_PRESSED));
219     EXPECT_EQ(FingerprintEventHdr->AnalyseKeyEvent(&event), ERR_OK);
220 }
221 
222 /**
223  * @tc.name: FingerprintEventProcessorTest_AnalyseKeyEvent_StateNotLibinputKeyStatePressed
224  * @tc.desc: Test AnalyseKeyEvent
225  * @tc.type: FUNC
226  * @tc.require:
227  */
228 HWTEST_F(FingerprintEventProcessorTest,
229     FingerprintEventProcessorTest_AnalyseKeyEvent_StateNotLibinputKeyStatePressed, TestSize.Level1)
230 {
231     NiceMock<LibinputInterfaceMock> mock;
232     struct libinput_event event;
233     struct libinput_event_keyboard keyBoardEvent;
234     EXPECT_CALL(mock, LibinputEventGetKeyboardEvent)
235         .WillRepeatedly(Return(&keyBoardEvent));
236     EXPECT_CALL(mock, LibinputEventKeyboardGetKey)
237         .WillOnce(Return(FingerprintEventProcessor::FINGERPRINT_CODE_DOWN))
238         .WillOnce(Return(FingerprintEventProcessor::FINGERPRINT_CODE_UP))
239         .WillOnce(Return(FingerprintEventProcessor::FINGERPRINT_CODE_RETOUCH))
240         .WillOnce(Return(FingerprintEventProcessor::FINGERPRINT_CODE_CLICK))
241         .WillOnce(Return(100));
242     EXPECT_CALL(mock, LibinputEventKeyboardGetKeyState)
243         .WillRepeatedly(Return(LIBINPUT_KEY_STATE_RELEASED));
244     InputHandler->BuildInputHandlerChain();
245     EXPECT_EQ(FingerprintEventHdr->AnalyseKeyEvent(&event), ERR_OK);
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), MMI::UNKNOWN_EVENT);
250 }
251 
252 /**
253  * @tc.name: FingerprintEventProcessorTest_SetPowerAndVolumeKeyState_001
254  * @tc.desc: Test HandleFingerprintEvent (keyCode != KEY_POWER) Branch
255  * @tc.type: FUNC
256  * @tc.require:
257  */
258 HWTEST_F(FingerprintEventProcessorTest, FingerprintEventProcessorTest_SetPowerAndVolumeKeyState_001, TestSize.Level1)
259 {
260     NiceMock<LibinputInterfaceMock> mock;
261     struct libinput_event event;
262     struct libinput_device device;
263     struct libinput_event_keyboard keyBoardEvent;
264     EXPECT_CALL(mock, GetDevice)
265         .WillOnce(Return(&device));
266     EXPECT_CALL(mock, LibinputEventGetKeyboardEvent)
267         .WillOnce(Return(&keyBoardEvent));
268     EXPECT_CALL(mock, LibinputEventKeyboardGetKey)
269         .WillOnce(Return(100));
270     ASSERT_NO_FATAL_FAILURE(FingerprintEventHdr->SetPowerAndVolumeKeyState(&event));
271 }
272 
273 /**
274  * @tc.name: FingerprintEventProcessorTest_SetPowerAndVolumeKeyState_002
275  * @tc.desc: Test HandleFingerprintEvent (keyAction == KeyEvent::KEY_ACTION_DOWN) Branch
276  * @tc.type: FUNC
277  * @tc.require:
278  */
279 HWTEST_F(FingerprintEventProcessorTest, FingerprintEventProcessorTest_SetPowerAndVolumeKeyState_002, TestSize.Level1)
280 {
281     NiceMock<LibinputInterfaceMock> mock;
282     struct libinput_event event;
283     struct libinput_device device;
284     struct libinput_event_keyboard keyBoardEvent;
285     EXPECT_CALL(mock, GetDevice)
286         .WillOnce(Return(&device));
287     EXPECT_CALL(mock, LibinputEventGetKeyboardEvent)
288         .WillOnce(Return(&keyBoardEvent));
289     EXPECT_CALL(mock, LibinputEventKeyboardGetKey)
290         .WillOnce(Return(116));
291     EXPECT_CALL(mock, LibinputEventKeyboardGetKeyState)
292         .WillOnce(Return(LIBINPUT_KEY_STATE_PRESSED));
293     ASSERT_NO_FATAL_FAILURE(FingerprintEventHdr->SetPowerAndVolumeKeyState(&event));
294 }
295 
296 /**
297  * @tc.name: FingerprintEventProcessorTest_SetPowerAndVolumeKeyState_003
298  * @tc.desc: Test HandleFingerprintEvent (keyAction != KeyEvent::KEY_ACTION_DOWN) Branch
299  * @tc.type: FUNC
300  * @tc.require:
301  */
302 HWTEST_F(FingerprintEventProcessorTest, FingerprintEventProcessorTest_SetPowerAndVolumeKeyState_003, TestSize.Level1)
303 {
304     NiceMock<LibinputInterfaceMock> mock;
305     struct libinput_event event;
306     struct libinput_device device;
307     struct libinput_event_keyboard keyBoardEvent;
308     EXPECT_CALL(mock, GetDevice)
309         .WillOnce(Return(&device));
310     EXPECT_CALL(mock, LibinputEventGetKeyboardEvent)
311         .WillOnce(Return(&keyBoardEvent));
312     EXPECT_CALL(mock, LibinputEventKeyboardGetKey)
313         .WillOnce(Return(116));
314     EXPECT_CALL(mock, LibinputEventKeyboardGetKeyState)
315         .WillOnce(Return(LIBINPUT_KEY_STATE_RELEASED));
316     ASSERT_NO_FATAL_FAILURE(FingerprintEventHdr->SetPowerAndVolumeKeyState(&event));
317 }
318 
319 #endif // OHOS_BUILD_ENABLE_FINGERPRINT
320 } // namespace MMI
321 } // namespace OHOS