1 /*
2 * Copyright 2024 Kelly Helmut Lord
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/device.h>
8 #include <zephyr/input/input.h>
9 #include <zephyr/kernel.h>
10 #include <zephyr/ztest.h>
11
12 static const struct device *const fake_dev = DEVICE_DT_GET(
13 DT_NODELABEL(fake_input_device));
14 static const struct device *const double_tap_dev = DEVICE_DT_GET(
15 DT_NODELABEL(double_tap));
16
17 DEVICE_DT_DEFINE(DT_INST(0, vnd_input_device), NULL, NULL, NULL, NULL,
18 PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEVICE, NULL);
19
20 static int event_count;
21 static struct input_event last_events[2];
22
test_cb(struct input_event * evt,void * user_data)23 static void test_cb(struct input_event *evt, void *user_data)
24 {
25 TC_PRINT("%s: %d %x %d\n", __func__, event_count, evt->code, evt->value);
26
27 event_count++;
28 memcpy(&last_events[1], &last_events[0], sizeof(struct input_event));
29 memcpy(&last_events[0], evt, sizeof(struct input_event));
30 }
31 INPUT_CALLBACK_DEFINE(double_tap_dev, test_cb, NULL);
32
ZTEST(double_tap,test_double_tap_test)33 ZTEST(double_tap, test_double_tap_test)
34 {
35 zassert_equal(event_count, 0);
36
37 /* ignored */
38 input_report_key(fake_dev, INPUT_KEY_3, 1, true, K_FOREVER);
39 input_report_key(fake_dev, INPUT_KEY_3, 0, true, K_FOREVER);
40 zassert_equal(event_count, 0);
41 input_report_abs(fake_dev, INPUT_KEY_0, 1, true, K_FOREVER);
42 input_report_abs(fake_dev, INPUT_KEY_0, 0, true, K_FOREVER);
43 zassert_equal(event_count, 0);
44
45 /* double tap*/
46 input_report_key(fake_dev, INPUT_KEY_0, 1, true, K_FOREVER);
47 k_sleep(K_MSEC(50));
48 input_report_key(fake_dev, INPUT_KEY_0, 0, true, K_FOREVER);
49 k_sleep(K_MSEC(50));
50 input_report_key(fake_dev, INPUT_KEY_0, 1, true, K_FOREVER);
51 k_sleep(K_MSEC(50));
52 input_report_key(fake_dev, INPUT_KEY_0, 0, true, K_FOREVER);
53 zassert_equal(event_count, 2);
54 zassert_equal(last_events[1].type, INPUT_EV_KEY);
55 zassert_equal(last_events[1].code, INPUT_KEY_X);
56 zassert_equal(last_events[1].value, 1);
57 zassert_equal(last_events[0].type, INPUT_EV_KEY);
58 zassert_equal(last_events[0].code, INPUT_KEY_X);
59 zassert_equal(last_events[0].value, 0);
60
61 /* double tap - other key */
62 input_report_key(fake_dev, INPUT_KEY_1, 1, true, K_FOREVER);
63 k_sleep(K_MSEC(50));
64 input_report_key(fake_dev, INPUT_KEY_1, 0, true, K_FOREVER);
65 k_sleep(K_MSEC(50));
66 input_report_key(fake_dev, INPUT_KEY_1, 1, true, K_FOREVER);
67 k_sleep(K_MSEC(50));
68 input_report_key(fake_dev, INPUT_KEY_1, 0, true, K_FOREVER);
69 zassert_equal(event_count, 4);
70 zassert_equal(last_events[1].type, INPUT_EV_KEY);
71 zassert_equal(last_events[1].code, INPUT_KEY_Y);
72 zassert_equal(last_events[1].value, 1);
73 zassert_equal(last_events[0].type, INPUT_EV_KEY);
74 zassert_equal(last_events[0].code, INPUT_KEY_Y);
75 zassert_equal(last_events[0].value, 0);
76 }
77
78 ZTEST_SUITE(double_tap, NULL, NULL, NULL, NULL, NULL);
79