1 /*
2 * Copyright 2023 Google LLC
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 longpress_dev = DEVICE_DT_GET(
15 DT_NODELABEL(longpress));
16 static const struct device *const longpress_no_short_dev = DEVICE_DT_GET(
17 DT_NODELABEL(longpress_no_short));
18
19 DEVICE_DT_DEFINE(DT_INST(0, vnd_input_device), NULL, NULL, NULL, NULL,
20 PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEVICE, NULL);
21
22 static int event_count;
23 static struct input_event last_events[2];
24
test_cb(struct input_event * evt,void * user_data)25 static void test_cb(struct input_event *evt, void *user_data)
26 {
27 TC_PRINT("%s: %d %x %d\n", __func__, event_count, evt->code, evt->value);
28
29 event_count++;
30 memcpy(&last_events[1], &last_events[0], sizeof(struct input_event));
31 memcpy(&last_events[0], evt, sizeof(struct input_event));
32 }
33 INPUT_CALLBACK_DEFINE(longpress_dev, test_cb, NULL);
34
35 static int event_count_no_short;
36 static struct input_event last_events_no_short[2];
test_cb_no_short(struct input_event * evt,void * user_data)37 static void test_cb_no_short(struct input_event *evt, void *user_data)
38 {
39 TC_PRINT("%s: %d %x %d\n", __func__, event_count_no_short, evt->code, evt->value);
40
41 event_count_no_short++;
42 memcpy(&last_events_no_short[1], &last_events_no_short[0], sizeof(struct input_event));
43 memcpy(&last_events_no_short[0], evt, sizeof(struct input_event));
44 }
45 INPUT_CALLBACK_DEFINE(longpress_no_short_dev, test_cb_no_short, NULL);
46
ZTEST(longpress,test_longpress_test)47 ZTEST(longpress, test_longpress_test)
48 {
49 zassert_equal(event_count, 0);
50
51 /* ignored */
52 input_report_key(fake_dev, INPUT_KEY_3, 1, true, K_FOREVER);
53 input_report_key(fake_dev, INPUT_KEY_3, 0, true, K_FOREVER);
54 zassert_equal(event_count, 0);
55 zassert_equal(event_count_no_short, 0);
56 input_report_abs(fake_dev, INPUT_KEY_0, 1, true, K_FOREVER);
57 input_report_abs(fake_dev, INPUT_KEY_0, 0, true, K_FOREVER);
58 zassert_equal(event_count, 0);
59 zassert_equal(event_count_no_short, 0);
60
61 /* short press */
62 input_report_key(fake_dev, INPUT_KEY_0, 1, true, K_FOREVER);
63 k_sleep(K_MSEC(50));
64 input_report_key(fake_dev, INPUT_KEY_0, 0, true, K_FOREVER);
65 zassert_equal(event_count, 2);
66 zassert_equal(last_events[1].type, INPUT_EV_KEY);
67 zassert_equal(last_events[1].code, INPUT_KEY_A);
68 zassert_equal(last_events[1].value, 1);
69 zassert_equal(last_events[0].type, INPUT_EV_KEY);
70 zassert_equal(last_events[0].code, INPUT_KEY_A);
71 zassert_equal(last_events[0].value, 0);
72 zassert_equal(event_count_no_short, 0);
73
74 /* short press - other key */
75 input_report_key(fake_dev, INPUT_KEY_1, 1, true, K_FOREVER);
76 k_sleep(K_MSEC(50));
77 input_report_key(fake_dev, INPUT_KEY_1, 0, true, K_FOREVER);
78 zassert_equal(event_count, 4);
79 zassert_equal(last_events[1].type, INPUT_EV_KEY);
80 zassert_equal(last_events[1].code, INPUT_KEY_B);
81 zassert_equal(last_events[1].value, 1);
82 zassert_equal(last_events[0].type, INPUT_EV_KEY);
83 zassert_equal(last_events[0].code, INPUT_KEY_B);
84 zassert_equal(last_events[0].value, 0);
85 zassert_equal(event_count_no_short, 0);
86
87 /* long press */
88 input_report_key(fake_dev, INPUT_KEY_0, 1, true, K_FOREVER);
89 k_sleep(K_MSEC(150));
90 input_report_key(fake_dev, INPUT_KEY_0, 0, true, K_FOREVER);
91 zassert_equal(event_count, 6);
92 zassert_equal(last_events[1].type, INPUT_EV_KEY);
93 zassert_equal(last_events[1].code, INPUT_KEY_X);
94 zassert_equal(last_events[1].value, 1);
95 zassert_equal(last_events[0].type, INPUT_EV_KEY);
96 zassert_equal(last_events[0].code, INPUT_KEY_X);
97 zassert_equal(last_events[0].value, 0);
98
99 zassert_equal(event_count_no_short, 2);
100 zassert_equal(last_events_no_short[1].type, INPUT_EV_KEY);
101 zassert_equal(last_events_no_short[1].code, INPUT_KEY_X);
102 zassert_equal(last_events_no_short[1].value, 1);
103 zassert_equal(last_events_no_short[0].type, INPUT_EV_KEY);
104 zassert_equal(last_events_no_short[0].code, INPUT_KEY_X);
105 zassert_equal(last_events_no_short[0].value, 0);
106
107 /* long press - other key */
108 input_report_key(fake_dev, INPUT_KEY_1, 1, true, K_FOREVER);
109 k_sleep(K_MSEC(150));
110 input_report_key(fake_dev, INPUT_KEY_1, 0, true, K_FOREVER);
111 zassert_equal(event_count, 8);
112 zassert_equal(last_events[1].type, INPUT_EV_KEY);
113 zassert_equal(last_events[1].code, INPUT_KEY_Y);
114 zassert_equal(last_events[1].value, 1);
115 zassert_equal(last_events[0].type, INPUT_EV_KEY);
116 zassert_equal(last_events[0].code, INPUT_KEY_Y);
117 zassert_equal(last_events[0].value, 0);
118
119 zassert_equal(event_count_no_short, 4);
120 zassert_equal(last_events_no_short[1].type, INPUT_EV_KEY);
121 zassert_equal(last_events_no_short[1].code, INPUT_KEY_Y);
122 zassert_equal(last_events_no_short[1].value, 1);
123 zassert_equal(last_events_no_short[0].type, INPUT_EV_KEY);
124 zassert_equal(last_events_no_short[0].code, INPUT_KEY_Y);
125 zassert_equal(last_events_no_short[0].value, 0);
126 }
127
128 ZTEST_SUITE(longpress, NULL, NULL, NULL, NULL, NULL);
129