• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023-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 <gtest/gtest.h>
17 
18 #include "joystick_event_processor.h"
19 #include "mmi_log.h"
20 #include <iomanip>
21 #include "key_map_manager.h"
22 #include "key_event_normalize.h"
23 #include "key_unicode_transformation.h"
24 
25 #undef MMI_LOG_TAG
26 #define MMI_LOG_TAG "JoystickEventProcessorTest"
27 
28 #define KEY_MAX			0x2ff
29 #define KEY_CNT			(KEY_MAX + 1)
30 
31 typedef void (*libinput_seat_destroy_func) (struct libinput_seat *seat);
32 
33 struct libinput_device_config {};
34 
35 struct list {
36     struct list *next, *prev;
37 };
38 
39 struct libinput_device_group {
40     int refcount;
41     void *user_data;
42     char *identifier; /* unique identifier or NULL for singletons */
43     struct list link;
44 };
45 
46 struct libinput_seat {
47     struct libinput *libinput;
48     struct list link;
49     struct list devices_list;
50     void *user_data;
51     int refcount;
52     libinput_seat_destroy_func destroy;
53 
54     char *physical_name;
55     char *logical_name;
56 
57     uint32_t slot_map;
58 
59     uint32_t button_count[KEY_CNT];
60 };
61 
62 struct libinput_device {
63     struct libinput_seat *seat;
64     struct libinput_device_group *group;
65     struct list link;
66     struct list event_listeners;
67     void *user_data;
68     int refcount;
69     struct libinput_device_config config;
70 };
71 
72 struct libinput_event {
73     enum libinput_event_type type;
74     struct libinput_device *device;
75 };
76 
77 namespace OHOS {
78 namespace MMI {
79 namespace {
80 using namespace testing::ext;
81 } // namespace
82 
83 class JoystickEventProcessorTest : public testing::Test {
84 public:
SetUpTestCase(void)85     static void SetUpTestCase(void) {}
TearDownTestCase(void)86     static void TearDownTestCase(void) {}
SetUp()87     void SetUp() {}
TearDown()88     void TearDown() {}
89 };
90 
91 /**
92  * @tc.name: JoystickEventProcessorTest_OnButtonEvent
93  * @tc.desc: Test OnButtonEvent
94  * @tc.type: FUNC
95  * @tc.require:
96  */
97 HWTEST_F(JoystickEventProcessorTest, JoystickEventProcessorTest_OnButtonEvent, TestSize.Level1)
98 {
99     CALL_TEST_DEBUG;
100     int32_t deviceId { 2 };
101     JoystickEventProcessor joystick(deviceId);
102     libinput_event libInputEvent;
103     ASSERT_EQ(joystick.OnButtonEvent(&libInputEvent), nullptr);
104 }
105 
106 /**
107  * @tc.name: JoystickEventProcessorTest_OnAxisEvent_001
108  * @tc.desc: Test OnAxisEvent
109  * @tc.type: FUNC
110  * @tc.require:
111  */
112 HWTEST_F(JoystickEventProcessorTest, JoystickEventProcessorTest_OnAxisEvent_001, TestSize.Level1)
113 {
114     CALL_TEST_DEBUG;
115     int32_t deviceId = 2;
116     auto joystickEventProcessor = std::make_shared<JoystickEventProcessor>(deviceId);
117         libinput_event libInputEvent = {
118         .type = LIBINPUT_EVENT_NONE,
119         .device = nullptr
120     };
121     ASSERT_EQ(joystickEventProcessor->OnButtonEvent(&libInputEvent), nullptr);
122 }
123 
124 /**
125  * @tc.name: JoystickEventProcessorTest_CheckIntention
126  * @tc.desc: Test CheckIntention
127  * @tc.type: FUNC
128  * @tc.require:
129  */
130 HWTEST_F(JoystickEventProcessorTest, JoystickEventProcessorTest_CheckIntention, TestSize.Level1)
131 {
132     CALL_TEST_DEBUG;
133     int32_t deviceId { 2 };
134     JoystickEventProcessor joystick(deviceId);
135     std::shared_ptr<PointerEvent> pointerEvent;
136     ASSERT_NO_FATAL_FAILURE(
__anon33a885d50202(std::shared_ptr<OHOS::MMI::KeyEvent>) 137         joystick.CheckIntention(pointerEvent, [=] (std::shared_ptr<OHOS::MMI::KeyEvent>) { return; }));
138 }
139 
140 /**
141  * @tc.name: JoystickEventProcessorTest_CheckIntention_002
142  * @tc.desc: Test CheckIntention
143  * @tc.type: FUNC
144  * @tc.require:
145  */
146 HWTEST_F(JoystickEventProcessorTest, JoystickEventProcessorTest_CheckIntention_002, TestSize.Level1)
147 {
148     CALL_TEST_DEBUG;
149     auto JoystickEvent = new JoystickEventProcessor(2);
150     std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create();
151     ASSERT_NE(pointerEvent, nullptr);
152     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_JOYSTICK);
153     pointerEvent->axes_ = PointerEvent::AXIS_TYPE_ABS_HAT0X;
154     double axisValue = 0;
155     pointerEvent->axisValues_[PointerEvent::AXIS_TYPE_ABS_HAT0X] = axisValue;
156     JoystickEvent->pressedButtons_ = {KeyEvent::KEYCODE_DPAD_LEFT};
157     ASSERT_NO_FATAL_FAILURE(
__anon33a885d50302(std::shared_ptr<OHOS::MMI::KeyEvent>) 158         JoystickEvent->CheckIntention(pointerEvent, [=] (std::shared_ptr<OHOS::MMI::KeyEvent>) { return; }));
159 }
160 
161 /**
162  * @tc.name: JoystickEventProcessorTest_CheckIntention_003
163  * @tc.desc: Test CheckIntention
164  * @tc.type: FUNC
165  * @tc.require:
166  */
167 HWTEST_F(JoystickEventProcessorTest, JoystickEventProcessorTest_CheckIntention_003, TestSize.Level1)
168 {
169     CALL_TEST_DEBUG;
170     auto JoystickEvent = new JoystickEventProcessor(2);
171     std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create();
172     ASSERT_NE(pointerEvent, nullptr);
173     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_JOYSTICK);
174     pointerEvent->axes_ = PointerEvent::AXIS_TYPE_ABS_HAT0Y;
175     double axisValue = 0;
176     pointerEvent->axisValues_[PointerEvent::AXIS_TYPE_ABS_HAT0Y] = axisValue;
177     JoystickEvent->pressedButtons_ = {KeyEvent::KEYCODE_DPAD_DOWN};
178     ASSERT_NO_FATAL_FAILURE(
__anon33a885d50402(std::shared_ptr<OHOS::MMI::KeyEvent>) 179         JoystickEvent->CheckIntention(pointerEvent, [=] (std::shared_ptr<OHOS::MMI::KeyEvent>) { return; }));
180 }
181 
182 /**
183  * @tc.name: JoystickEventProcessorTest_CheckHAT0X
184  * @tc.desc: Test CheckHAT0X
185  * @tc.type: FUNC
186  * @tc.require:
187  */
188 HWTEST_F(JoystickEventProcessorTest, JoystickEventProcessorTest_CheckHAT0X, TestSize.Level1)
189 {
190     CALL_TEST_DEBUG;
191     int32_t deviceId { 2 };
192     JoystickEventProcessor joystick(deviceId);
193     std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create();
194     std::vector<KeyEvent::KeyItem> buttonEvents;
195     ASSERT_NO_FATAL_FAILURE(joystick.CheckHAT0X(pointerEvent, buttonEvents));
196 }
197 
198 /**
199  * @tc.name: JoystickEventProcessorTest_CheckHAT0X_002
200  * @tc.desc: Test CheckHAT0X
201  * @tc.type: FUNC
202  * @tc.require:
203  */
204 HWTEST_F(JoystickEventProcessorTest, JoystickEventProcessorTest_CheckHAT0X_002, TestSize.Level1)
205 {
206     CALL_TEST_DEBUG;
207     auto JoystickEvent = new JoystickEventProcessor(2);
208     std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create();
209     pointerEvent->axes_ = PointerEvent::AXIS_TYPE_SCROLL_VERTICAL;
210     std::vector<KeyEvent::KeyItem> buttonEvents;
211     ASSERT_NO_FATAL_FAILURE(JoystickEvent->CheckHAT0X(pointerEvent, buttonEvents));
212 }
213 
214 /**
215  * @tc.name: JoystickEventProcessorTest_CheckHAT0X_003
216  * @tc.desc: Test CheckHAT0X
217  * @tc.type: FUNC
218  * @tc.require:
219  */
220 HWTEST_F(JoystickEventProcessorTest, JoystickEventProcessorTest_CheckHAT0X_003, TestSize.Level1)
221 {
222     CALL_TEST_DEBUG;
223     auto JoystickEvent = new JoystickEventProcessor(2);
224     std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create();
225     std::vector<KeyEvent::KeyItem> buttonEvents;
226     pointerEvent->axes_ = PointerEvent::AXIS_TYPE_ABS_HAT0X;
227     double axisValue = 0.02;
228     pointerEvent->axisValues_[PointerEvent::AXIS_TYPE_ABS_HAT0X] = axisValue;
229     ASSERT_NO_FATAL_FAILURE(JoystickEvent->CheckHAT0X(pointerEvent, buttonEvents));
230     axisValue = -0.02;
231     pointerEvent->axisValues_[PointerEvent::AXIS_TYPE_ABS_HAT0X] = axisValue;
232     ASSERT_NO_FATAL_FAILURE(JoystickEvent->CheckHAT0X(pointerEvent, buttonEvents));
233 }
234 
235 /**
236  * @tc.name: JoystickEventProcessorTest_CheckHAT0X_004
237  * @tc.desc: Test CheckHAT0X
238  * @tc.type: FUNC
239  * @tc.require:
240  */
241 HWTEST_F(JoystickEventProcessorTest, JoystickEventProcessorTest_CheckHAT0X_004, TestSize.Level1)
242 {
243     CALL_TEST_DEBUG;
244     auto JoystickEvent = new JoystickEventProcessor(2);
245     std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create();
246     std::vector<KeyEvent::KeyItem> buttonEvents;
247     pointerEvent->axes_ = PointerEvent::AXIS_TYPE_ABS_HAT0X;
248     double axisValue = 0;
249     pointerEvent->axisValues_[PointerEvent::AXIS_TYPE_ABS_HAT0X] = axisValue;
250     JoystickEvent->pressedButtons_ = {KeyEvent::KEYCODE_DPAD_LEFT};
251     ASSERT_NO_FATAL_FAILURE(JoystickEvent->CheckHAT0X(pointerEvent, buttonEvents));
252     JoystickEvent->pressedButtons_ = {KeyEvent::KEYCODE_DPAD_RIGHT};
253     ASSERT_NO_FATAL_FAILURE(JoystickEvent->CheckHAT0X(pointerEvent, buttonEvents));
254 }
255 
256 /**
257  * @tc.name: JoystickEventProcessorTest_CheckHAT0X_005
258  * @tc.desc: Test CheckHAT0X
259  * @tc.type: FUNC
260  * @tc.require:
261  */
262 HWTEST_F(JoystickEventProcessorTest, JoystickEventProcessorTest_CheckHAT0X_005, TestSize.Level1)
263 {
264     CALL_TEST_DEBUG;
265     auto JoystickEvent = new JoystickEventProcessor(2);
266     std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create();
267     std::vector<KeyEvent::KeyItem> buttonEvents;
268     pointerEvent->axes_ = PointerEvent::AXIS_TYPE_ABS_HAT0X;
269     double axisValue = 0;
270     pointerEvent->axisValues_[PointerEvent::AXIS_TYPE_ABS_HAT0X] = axisValue;
271     ASSERT_NO_FATAL_FAILURE(JoystickEvent->CheckHAT0X(pointerEvent, buttonEvents));
272 }
273 
274 /**
275  * @tc.name: JoystickEventProcessorTest_CheckHAT0Y
276  * @tc.desc: Test CheckHAT0Y
277  * @tc.type: FUNC
278  * @tc.require:
279  */
280 HWTEST_F(JoystickEventProcessorTest, JoystickEventProcessorTest_CheckHAT0Y, TestSize.Level1)
281 {
282     CALL_TEST_DEBUG;
283     int32_t deviceId { 2 };
284     JoystickEventProcessor joystick(deviceId);
285     std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create();
286     std::vector<KeyEvent::KeyItem> buttonEvents;
287     ASSERT_NO_FATAL_FAILURE(joystick.CheckHAT0Y(pointerEvent, buttonEvents));
288 }
289 
290 /**
291  * @tc.name: JoystickEventProcessorTest_CheckHAT0Y_002
292  * @tc.desc: Test CheckHAT0Y
293  * @tc.type: FUNC
294  * @tc.require:
295  */
296 HWTEST_F(JoystickEventProcessorTest, JoystickEventProcessorTest_CheckHAT0Y_002, TestSize.Level1)
297 {
298     CALL_TEST_DEBUG;
299     auto JoystickEvent = new JoystickEventProcessor(2);
300     std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create();
301     pointerEvent->axes_ = PointerEvent::AXIS_TYPE_SCROLL_VERTICAL;
302     std::vector<KeyEvent::KeyItem> buttonEvents;
303     ASSERT_NO_FATAL_FAILURE(JoystickEvent->CheckHAT0Y(pointerEvent, buttonEvents));
304 }
305 
306 /**
307  * @tc.name: JoystickEventProcessorTest_CheckHAT0Y_003
308  * @tc.desc: Test CheckHAT0Y
309  * @tc.type: FUNC
310  * @tc.require:
311  */
312 HWTEST_F(JoystickEventProcessorTest, JoystickEventProcessorTest_CheckHAT0Y_003, TestSize.Level1)
313 {
314     CALL_TEST_DEBUG;
315     auto JoystickEvent = new JoystickEventProcessor(2);
316     std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create();
317     std::vector<KeyEvent::KeyItem> buttonEvents;
318     pointerEvent->axes_ = PointerEvent::AXIS_TYPE_ABS_HAT0Y;
319     double axisValue = 0.02;
320     pointerEvent->axisValues_[PointerEvent::AXIS_TYPE_ABS_HAT0Y] = axisValue;
321     ASSERT_NO_FATAL_FAILURE(JoystickEvent->CheckHAT0Y(pointerEvent, buttonEvents));
322     axisValue = -0.02;
323     pointerEvent->axisValues_[PointerEvent::AXIS_TYPE_ABS_HAT0Y] = axisValue;
324     ASSERT_NO_FATAL_FAILURE(JoystickEvent->CheckHAT0Y(pointerEvent, buttonEvents));
325 }
326 
327 /**
328  * @tc.name: JoystickEventProcessorTest_CheckHAT0Y_004
329  * @tc.desc: Test CheckHAT0Y
330  * @tc.type: FUNC
331  * @tc.require:
332  */
333 HWTEST_F(JoystickEventProcessorTest, JoystickEventProcessorTest_CheckHAT0Y_004, TestSize.Level1)
334 {
335     CALL_TEST_DEBUG;
336     auto JoystickEvent = new JoystickEventProcessor(2);
337     std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create();
338     std::vector<KeyEvent::KeyItem> buttonEvents;
339     pointerEvent->axes_ = PointerEvent::AXIS_TYPE_ABS_HAT0Y;
340     double axisValue = 0;
341     pointerEvent->axisValues_[PointerEvent::AXIS_TYPE_ABS_HAT0Y] = axisValue;
342     JoystickEvent->pressedButtons_ = {KeyEvent::KEYCODE_DPAD_DOWN};
343     ASSERT_NO_FATAL_FAILURE(JoystickEvent->CheckHAT0Y(pointerEvent, buttonEvents));
344     JoystickEvent->pressedButtons_ = {KeyEvent::KEYCODE_DPAD_UP};
345     ASSERT_NO_FATAL_FAILURE(JoystickEvent->CheckHAT0Y(pointerEvent, buttonEvents));
346 }
347 
348 /**
349  * @tc.name: JoystickEventProcessorTest_CheckHAT0Y_005
350  * @tc.desc: Test CheckHAT0Y
351  * @tc.type: FUNC
352  * @tc.require:
353  */
354 HWTEST_F(JoystickEventProcessorTest, JoystickEventProcessorTest_CheckHAT0Y_005, TestSize.Level1)
355 {
356     CALL_TEST_DEBUG;
357     auto JoystickEvent = new JoystickEventProcessor(2);
358     std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create();
359     std::vector<KeyEvent::KeyItem> buttonEvents;
360     pointerEvent->axes_ = PointerEvent::AXIS_TYPE_ABS_HAT0Y;
361     double axisValue = 0;
362     pointerEvent->axisValues_[PointerEvent::AXIS_TYPE_ABS_HAT0Y] = axisValue;
363     ASSERT_NO_FATAL_FAILURE(JoystickEvent->CheckHAT0Y(pointerEvent, buttonEvents));
364 }
365 
366 /**
367  * @tc.name: JoystickEventProcessorTest_UpdateButtonState
368  * @tc.desc: Test UpdateButtonState
369  * @tc.type: FUNC
370  * @tc.require:
371  */
372 HWTEST_F(JoystickEventProcessorTest, JoystickEventProcessorTest_UpdateButtonState, TestSize.Level1)
373 {
374     CALL_TEST_DEBUG;
375     int32_t deviceId { 2 };
376     JoystickEventProcessor joystick(deviceId);
377     KeyEvent::KeyItem keyItem {};
378     ASSERT_NO_FATAL_FAILURE(joystick.UpdateButtonState(keyItem));
379 }
380 
381 /**
382  * @tc.name: JoystickEventProcessorTest_FormatButtonEvent
383  * @tc.desc: Test FormatButtonEvent
384  * @tc.type: FUNC
385  * @tc.require:
386  */
387 HWTEST_F(JoystickEventProcessorTest, JoystickEventProcessorTest_FormatButtonEvent, TestSize.Level1)
388 {
389     CALL_TEST_DEBUG;
390     int32_t deviceId { 2 };
391     JoystickEventProcessor joystick(deviceId);
392     KeyEvent::KeyItem keyItem {};
393     EXPECT_NE(joystick.FormatButtonEvent(keyItem), nullptr);
394 }
395 
396 /**
397  * @tc.name: JoystickEventProcessorTest_CleanUpKeyEvent
398  * @tc.desc: Test CleanUpKeyEvent
399  * @tc.type: FUNC
400  * @tc.require:
401  */
402 HWTEST_F(JoystickEventProcessorTest, JoystickEventProcessorTest_CleanUpKeyEvent, TestSize.Level1)
403 {
404     CALL_TEST_DEBUG;
405     int32_t deviceId { 2 };
406     JoystickEventProcessor joystick(deviceId);
407     EXPECT_NE(joystick.CleanUpKeyEvent(), nullptr);
408 }
409 
410 /**
411  * @tc.name: JoystickEventProcessorTest_DumpJoystickAxisEvent
412  * @tc.desc: Test DumpJoystickAxisEvent
413  * @tc.type: FUNC
414  * @tc.require:
415  */
416 HWTEST_F(JoystickEventProcessorTest, JoystickEventProcessorTest_DumpJoystickAxisEvent, TestSize.Level1)
417 {
418     CALL_TEST_DEBUG;
419     int32_t deviceId { 2 };
420     JoystickEventProcessor joystick(deviceId);
421     std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create();
422     ASSERT_NE(joystick.DumpJoystickAxisEvent(pointerEvent), "");
423 }
424 
425 /**
426  * @tc.name: JoystickEventProcessorTest_Normalize
427  * @tc.desc: Test Normalize
428  * @tc.type: FUNC
429  * @tc.require:
430  */
431 HWTEST_F(JoystickEventProcessorTest, JoystickEventProcessorTest_Normalize, TestSize.Level1)
432 {
433     CALL_TEST_DEBUG;
434     int32_t deviceId { 2 };
435     JoystickEventProcessor joystick(deviceId);
436     struct libinput_event_joystick_axis_abs_info axis {};
437     double low  { 2.1 };
438     double high { 1.3 };
439     ASSERT_NE(joystick.Normalize(axis, low, high), 3.5f);
440 }
441 
442 /**
443  * @tc.name: JoystickEventProcessorTest_Normalize_002
444  * @tc.desc: Test Normalize
445  * @tc.type: FUNC
446  * @tc.require:
447  */
448 HWTEST_F(JoystickEventProcessorTest, JoystickEventProcessorTest_Normalize_002, TestSize.Level1)
449 {
450     CALL_TEST_DEBUG;
451     auto JoystickEvent = new JoystickEventProcessor(2);
452     const struct libinput_event_joystick_axis_abs_info axis = {
453         .code = 1,
454         .value = 5,
455         .maximum = 0,
456         .minimum  = 10,
457         .fuzz = 0,
458         .flat = 0,
459         .resolution = 0,
460         .standardValue = 1.0
461     };
462     double low = 0.0f;
463     double high = 1.0f;
464     ASSERT_EQ(JoystickEvent->Normalize(axis, low, high), 0.0f);
465 }
466 
467 /**
468  * @tc.name: JoystickEventProcessorTest_Normalize_003
469  * @tc.desc: Test Normalize
470  * @tc.type: FUNC
471  * @tc.require:
472  */
473 HWTEST_F(JoystickEventProcessorTest, JoystickEventProcessorTest_Normalize_003, TestSize.Level1)
474 {
475     CALL_TEST_DEBUG;
476     auto JoystickEvent = new JoystickEventProcessor(2);
477     const struct libinput_event_joystick_axis_abs_info axis = {
478         .code = 1,
479         .value = 20,
480         .maximum = 10,
481         .minimum  = 0,
482         .fuzz = 0,
483         .flat = 0,
484         .resolution = 0,
485         .standardValue = 1.0
486     };
487     double low = 0.0f;
488     double high = 1.0f;
489     ASSERT_EQ(JoystickEvent->Normalize(axis, low, high), 1.0f);
490 }
491 
492 /**
493  * @tc.name: JoystickEventProcessorTest_Normalize_004
494  * @tc.desc: Test Normalize
495  * @tc.type: FUNC
496  * @tc.require:
497  */
498 HWTEST_F(JoystickEventProcessorTest, JoystickEventProcessorTest_Normalize_004, TestSize.Level1)
499 {
500     CALL_TEST_DEBUG;
501     auto JoystickEvent = new JoystickEventProcessor(2);
502     const struct libinput_event_joystick_axis_abs_info axis = {
503         .code = 1,
504         .value = 5,
505         .maximum = 10,
506         .minimum  = 0,
507         .fuzz = 0,
508         .flat = 0,
509         .resolution = 0,
510         .standardValue = 1.0
511     };
512     double low = 0.0f;
513     double high = 1.0f;
514     ASSERT_EQ(JoystickEvent->Normalize(axis, low, high), 0.5f);
515 }
516 } // namespace MMI
517 } // namespace OHOS
518