diff options
author | Akshay <[email protected]> | 2022-04-10 12:13:40 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2022-04-10 12:13:40 +0100 |
commit | dc90387ce7d8ba7b607d9c48540bf6d8b560f14d (patch) | |
tree | 4ccb8fa5886b66fa9d480edef74236c27f035e16 /tests/basic/test_keypress.cpp |
Diffstat (limited to 'tests/basic/test_keypress.cpp')
-rw-r--r-- | tests/basic/test_keypress.cpp | 317 |
1 files changed, 317 insertions, 0 deletions
diff --git a/tests/basic/test_keypress.cpp b/tests/basic/test_keypress.cpp new file mode 100644 index 000000000..1c175c9d5 --- /dev/null +++ b/tests/basic/test_keypress.cpp | |||
@@ -0,0 +1,317 @@ | |||
1 | /* Copyright 2017 Fred Sundvik | ||
2 | * | ||
3 | * This program is free software: you can redistribute it and/or modify | ||
4 | * it under the terms of the GNU General Public License as published by | ||
5 | * the Free Software Foundation, either version 2 of the License, or | ||
6 | * (at your option) any later version. | ||
7 | * | ||
8 | * This program is distributed in the hope that it will be useful, | ||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
11 | * GNU General Public License for more details. | ||
12 | * | ||
13 | * You should have received a copy of the GNU General Public License | ||
14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
15 | */ | ||
16 | |||
17 | #include "keycode.h" | ||
18 | #include "test_common.hpp" | ||
19 | |||
20 | using testing::_; | ||
21 | using testing::InSequence; | ||
22 | |||
23 | class KeyPress : public TestFixture {}; | ||
24 | |||
25 | TEST_F(KeyPress, SendKeyboardIsNotCalledWhenNoKeyIsPressed) { | ||
26 | TestDriver driver; | ||
27 | EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0); | ||
28 | keyboard_task(); | ||
29 | } | ||
30 | |||
31 | TEST_F(KeyPress, CorrectKeyIsReportedWhenPressed) { | ||
32 | TestDriver driver; | ||
33 | auto key = KeymapKey(0, 0, 0, KC_A); | ||
34 | |||
35 | set_keymap({key}); | ||
36 | |||
37 | key.press(); | ||
38 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(key.report_code))); | ||
39 | keyboard_task(); | ||
40 | |||
41 | key.release(); | ||
42 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())); | ||
43 | keyboard_task(); | ||
44 | } | ||
45 | |||
46 | TEST_F(KeyPress, ANonMappedKeyDoesNothing) { | ||
47 | TestDriver driver; | ||
48 | auto key = KeymapKey(0, 0, 0, KC_NO); | ||
49 | |||
50 | set_keymap({key}); | ||
51 | |||
52 | key.press(); | ||
53 | EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0); | ||
54 | keyboard_task(); | ||
55 | keyboard_task(); | ||
56 | } | ||
57 | |||
58 | TEST_F(KeyPress, CorrectKeysAreReportedWhenTwoKeysArePressed) { | ||
59 | TestDriver driver; | ||
60 | auto key_b = KeymapKey(0, 0, 0, KC_B); | ||
61 | auto key_c = KeymapKey(0, 1, 1, KC_C); | ||
62 | |||
63 | set_keymap({key_b, key_c}); | ||
64 | |||
65 | key_b.press(); | ||
66 | key_c.press(); | ||
67 | // Note that QMK only processes one key at a time | ||
68 | // See issue #1476 for more information | ||
69 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(key_b.report_code))); | ||
70 | keyboard_task(); | ||
71 | |||
72 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(key_b.report_code, key_c.report_code))); | ||
73 | keyboard_task(); | ||
74 | |||
75 | key_b.release(); | ||
76 | key_c.release(); | ||
77 | // Note that the first key released is the first one in the matrix order | ||
78 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(key_c.report_code))); | ||
79 | keyboard_task(); | ||
80 | |||
81 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())); | ||
82 | keyboard_task(); | ||
83 | } | ||
84 | |||
85 | TEST_F(KeyPress, LeftShiftIsReportedCorrectly) { | ||
86 | TestDriver driver; | ||
87 | auto key_a = KeymapKey(0, 0, 0, KC_A); | ||
88 | auto key_lsft = KeymapKey(0, 3, 0, KC_LSFT); | ||
89 | |||
90 | set_keymap({key_a, key_lsft}); | ||
91 | |||
92 | key_lsft.press(); | ||
93 | key_a.press(); | ||
94 | |||
95 | // Unfortunately modifiers are also processed in the wrong order | ||
96 | // See issue #1476 for more information | ||
97 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(key_a.report_code))); | ||
98 | keyboard_task(); | ||
99 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(key_a.report_code, key_lsft.report_code))); | ||
100 | keyboard_task(); | ||
101 | |||
102 | key_a.release(); | ||
103 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(key_lsft.report_code))); | ||
104 | keyboard_task(); | ||
105 | |||
106 | key_lsft.release(); | ||
107 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())); | ||
108 | keyboard_task(); | ||
109 | } | ||
110 | |||
111 | TEST_F(KeyPress, PressLeftShiftAndControl) { | ||
112 | TestDriver driver; | ||
113 | auto key_lsft = KeymapKey(0, 3, 0, KC_LSFT); | ||
114 | auto key_lctrl = KeymapKey(0, 5, 0, KC_LCTRL); | ||
115 | |||
116 | set_keymap({key_lctrl, key_lsft}); | ||
117 | |||
118 | key_lsft.press(); | ||
119 | key_lctrl.press(); | ||
120 | |||
121 | // Unfortunately modifiers are also processed in the wrong order | ||
122 | // See issue #1476 for more information | ||
123 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(key_lsft.report_code))); | ||
124 | keyboard_task(); | ||
125 | |||
126 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(key_lsft.report_code, key_lctrl.report_code))); | ||
127 | keyboard_task(); | ||
128 | |||
129 | key_lsft.release(); | ||
130 | key_lctrl.release(); | ||
131 | |||
132 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(key_lctrl.report_code))); | ||
133 | keyboard_task(); | ||
134 | |||
135 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())); | ||
136 | keyboard_task(); | ||
137 | } | ||
138 | |||
139 | TEST_F(KeyPress, LeftAndRightShiftCanBePressedAtTheSameTime) { | ||
140 | TestDriver driver; | ||
141 | auto key_lsft = KeymapKey(0, 3, 0, KC_LSFT); | ||
142 | auto key_rsft = KeymapKey(0, 4, 0, KC_RSFT); | ||
143 | |||
144 | set_keymap({key_rsft, key_lsft}); | ||
145 | |||
146 | key_lsft.press(); | ||
147 | key_rsft.press(); | ||
148 | // Unfortunately modifiers are also processed in the wrong order | ||
149 | // See issue #1476 for more information | ||
150 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(key_lsft.report_code))); | ||
151 | keyboard_task(); | ||
152 | |||
153 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(key_lsft.report_code, key_rsft.report_code))); | ||
154 | keyboard_task(); | ||
155 | |||
156 | key_lsft.release(); | ||
157 | key_rsft.release(); | ||
158 | |||
159 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(key_rsft.report_code))); | ||
160 | keyboard_task(); | ||
161 | |||
162 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())); | ||
163 | keyboard_task(); | ||
164 | } | ||
165 | |||
166 | TEST_F(KeyPress, RightShiftLeftControlAndCharWithTheSameKey) { | ||
167 | TestDriver driver; | ||
168 | auto combo_key = KeymapKey(0, 0, 0, RSFT(LCTL(KC_O))); | ||
169 | |||
170 | set_keymap({combo_key}); | ||
171 | |||
172 | // BUG: The press is split into two reports | ||
173 | // BUG: It reports RSFT instead of LSFT | ||
174 | // See issue #524 for more information | ||
175 | // The underlying cause is that we use only one bit to represent the right hand | ||
176 | // modifiers. | ||
177 | combo_key.press(); | ||
178 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_RSFT, KC_RCTRL))); | ||
179 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_RSFT, KC_RCTRL, KC_O))); | ||
180 | keyboard_task(); | ||
181 | |||
182 | combo_key.release(); | ||
183 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_RSFT, KC_RCTRL))); | ||
184 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())); | ||
185 | keyboard_task(); | ||
186 | } | ||
187 | |||
188 | TEST_F(KeyPress, PressPlusEqualReleaseBeforePress) { | ||
189 | TestDriver driver; | ||
190 | InSequence s; | ||
191 | auto key_plus = KeymapKey(0, 1, 1, KC_PLUS); | ||
192 | auto key_eql = KeymapKey(0, 0, 1, KC_EQL); | ||
193 | |||
194 | set_keymap({key_plus, key_eql}); | ||
195 | |||
196 | key_plus.press(); | ||
197 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT))); | ||
198 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT, KC_EQL))); | ||
199 | run_one_scan_loop(); | ||
200 | testing::Mock::VerifyAndClearExpectations(&driver); | ||
201 | |||
202 | key_plus.release(); | ||
203 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT))); | ||
204 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())); | ||
205 | run_one_scan_loop(); | ||
206 | testing::Mock::VerifyAndClearExpectations(&driver); | ||
207 | |||
208 | key_eql.press(); | ||
209 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(key_eql.report_code))); | ||
210 | run_one_scan_loop(); | ||
211 | testing::Mock::VerifyAndClearExpectations(&driver); | ||
212 | |||
213 | key_eql.release(); | ||
214 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())); | ||
215 | run_one_scan_loop(); | ||
216 | testing::Mock::VerifyAndClearExpectations(&driver); | ||
217 | } | ||
218 | |||
219 | TEST_F(KeyPress, PressPlusEqualDontReleaseBeforePress) { | ||
220 | TestDriver driver; | ||
221 | InSequence s; | ||
222 | auto key_plus = KeymapKey(0, 1, 1, KC_PLUS); | ||
223 | auto key_eql = KeymapKey(0, 0, 1, KC_EQL); | ||
224 | |||
225 | set_keymap({key_plus, key_eql}); | ||
226 | |||
227 | key_plus.press(); | ||
228 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT))); | ||
229 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT, KC_EQL))); | ||
230 | run_one_scan_loop(); | ||
231 | testing::Mock::VerifyAndClearExpectations(&driver); | ||
232 | |||
233 | key_eql.press(); | ||
234 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())); | ||
235 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_EQUAL))); | ||
236 | run_one_scan_loop(); | ||
237 | testing::Mock::VerifyAndClearExpectations(&driver); | ||
238 | |||
239 | key_plus.release(); | ||
240 | // BUG: Should really still return KC_EQL, but this is fine too | ||
241 | // It's also called twice for some reason | ||
242 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(2); | ||
243 | run_one_scan_loop(); | ||
244 | testing::Mock::VerifyAndClearExpectations(&driver); | ||
245 | |||
246 | key_eql.release(); | ||
247 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())); | ||
248 | run_one_scan_loop(); | ||
249 | testing::Mock::VerifyAndClearExpectations(&driver); | ||
250 | } | ||
251 | |||
252 | TEST_F(KeyPress, PressEqualPlusReleaseBeforePress) { | ||
253 | TestDriver driver; | ||
254 | InSequence s; | ||
255 | auto key_plus = KeymapKey(0, 1, 1, KC_PLUS); | ||
256 | auto key_eql = KeymapKey(0, 0, 1, KC_EQL); | ||
257 | |||
258 | set_keymap({key_plus, key_eql}); | ||
259 | |||
260 | key_eql.press(); | ||
261 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_EQL))); | ||
262 | run_one_scan_loop(); | ||
263 | testing::Mock::VerifyAndClearExpectations(&driver); | ||
264 | |||
265 | key_eql.release(); | ||
266 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())); | ||
267 | run_one_scan_loop(); | ||
268 | testing::Mock::VerifyAndClearExpectations(&driver); | ||
269 | |||
270 | key_plus.press(); | ||
271 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT))); | ||
272 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT, KC_EQL))); | ||
273 | run_one_scan_loop(); | ||
274 | testing::Mock::VerifyAndClearExpectations(&driver); | ||
275 | |||
276 | key_plus.release(); | ||
277 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT))); | ||
278 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())); | ||
279 | run_one_scan_loop(); | ||
280 | testing::Mock::VerifyAndClearExpectations(&driver); | ||
281 | } | ||
282 | |||
283 | TEST_F(KeyPress, PressEqualPlusDontReleaseBeforePress) { | ||
284 | TestDriver driver; | ||
285 | InSequence s; | ||
286 | auto key_plus = KeymapKey(0, 1, 1, KC_PLUS); | ||
287 | auto key_eql = KeymapKey(0, 0, 1, KC_EQL); | ||
288 | |||
289 | set_keymap({key_plus, key_eql}); | ||
290 | |||
291 | key_eql.press(); | ||
292 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_EQL))); | ||
293 | run_one_scan_loop(); | ||
294 | testing::Mock::VerifyAndClearExpectations(&driver); | ||
295 | |||
296 | key_plus.press(); | ||
297 | // BUG: The sequence is a bit strange, but it works, the end result is that | ||
298 | // KC_PLUS is sent | ||
299 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LEFT_SHIFT, KC_EQUAL))); | ||
300 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LEFT_SHIFT))); | ||
301 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LEFT_SHIFT, KC_EQUAL))); | ||
302 | run_one_scan_loop(); | ||
303 | testing::Mock::VerifyAndClearExpectations(&driver); | ||
304 | |||
305 | key_eql.release(); | ||
306 | // I guess it's fine to still report shift here | ||
307 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LEFT_SHIFT))); | ||
308 | run_one_scan_loop(); | ||
309 | testing::Mock::VerifyAndClearExpectations(&driver); | ||
310 | |||
311 | key_plus.release(); | ||
312 | // This report is not needed | ||
313 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LEFT_SHIFT))); | ||
314 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())); | ||
315 | run_one_scan_loop(); | ||
316 | testing::Mock::VerifyAndClearExpectations(&driver); | ||
317 | } \ No newline at end of file | ||