Debian Codegraph @

tests/core/input/test_input_event_key.h

raw symbols SCIP
1 /**************************************************************************/
2 /* test_input_event_key.h */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #pragma once
32
33 #include "core/input/input_event.h"
34 #include "core/os/keyboard.h"
35
36 #include "tests/test_macros.h"
37
38 namespace TestInputEventKey {
39
40 TEST_CASE("[InputEventKey] Key correctly registers being pressed") {
41 InputEventKey key;
42 key.set_pressed(true);
43 CHECK(key.is_pressed() == true);
44
45 key.set_pressed(false);
46 CHECK(key.is_pressed() == false);
47 }
48
49 TEST_CASE("[InputEventKey] Key correctly stores and retrieves keycode") {
50 InputEventKey key;
51
52 key.set_keycode(Key::ENTER);
53 CHECK(key.get_keycode() == Key::ENTER);
54 CHECK(key.get_keycode() != Key::PAUSE);
55
56 key.set_physical_keycode(Key::BACKSPACE);
57 CHECK(key.get_physical_keycode() == Key::BACKSPACE);
58 CHECK(key.get_physical_keycode() != Key::PAUSE);
59 }
60
61 TEST_CASE("[InputEventKey] Key correctly stores and retrieves keycode with modifiers") {
62 InputEventKey key;
63
64 key.set_keycode(Key::ENTER);
65 key.set_ctrl_pressed(true);
66
67 CHECK(key.get_keycode_with_modifiers() == (Key::ENTER | KeyModifierMask::CTRL));
68 CHECK(key.get_keycode_with_modifiers() != (Key::ENTER | KeyModifierMask::SHIFT));
69 CHECK(key.get_keycode_with_modifiers() != Key::ENTER);
70
71 key.set_physical_keycode(Key::SPACE);
72 key.set_ctrl_pressed(true);
73
74 CHECK(key.get_physical_keycode_with_modifiers() == (Key::SPACE | KeyModifierMask::CTRL));
75 CHECK(key.get_physical_keycode_with_modifiers() != (Key::SPACE | KeyModifierMask::SHIFT));
76 CHECK(key.get_physical_keycode_with_modifiers() != Key::SPACE);
77 }
78
79 TEST_CASE("[InputEventKey] Key correctly stores and retrieves unicode") {
80 InputEventKey key;
81
82 key.set_unicode('x');
83 CHECK(key.get_unicode() == 'x');
84 CHECK(key.get_unicode() != 'y');
85 }
86
87 TEST_CASE("[InputEventKey] Key correctly stores and retrieves location") {
88 InputEventKey key;
89
90 CHECK(key.get_location() == KeyLocation::UNSPECIFIED);
91
92 key.set_location(KeyLocation::LEFT);
93 CHECK(key.get_location() == KeyLocation::LEFT);
94 CHECK(key.get_location() != KeyLocation::RIGHT);
95 }
96
97 TEST_CASE("[InputEventKey] Key correctly stores and checks echo") {
98 InputEventKey key;
99
100 key.set_echo(true);
101 CHECK(key.is_echo() == true);
102
103 key.set_echo(false);
104 CHECK(key.is_echo() == false);
105 }
106
107 TEST_CASE("[InputEventKey] Key correctly converts itself to text") {
108 InputEventKey none_key;
109
110 // These next three tests test the functionality of getting a key that is set to None
111 // as text. These cases are a bit weird, since None has no textual representation
112 // (find_keycode_name(Key::NONE) results in a nullptr). Thus, these tests look weird
113 // with only "- Physical" or a lonely modifier with "- Physical" but (as far as I
114 // understand the code, that is intended behavior.
115
116 // Key is None without a physical key.
117 none_key.set_keycode(Key::NONE);
118 CHECK(none_key.as_text() == "(unset)");
119
120 // Key is none and has modifiers.
121 none_key.set_ctrl_pressed(true);
122 CHECK(none_key.as_text() == "Ctrl+(unset)");
123
124 // Key is None WITH a physical key AND modifiers.
125 none_key.set_physical_keycode(Key::ENTER);
126 CHECK(none_key.as_text() == "Ctrl+Enter");
127
128 none_key.set_physical_keycode(Key::W);
129 CHECK(none_key.as_text() == "Ctrl+W - Physical");
130
131 // Key is None WITH a physical key AND multiple modifiers, checks for correct ordering.
132 none_key.set_physical_keycode(Key::ENTER);
133 none_key.set_alt_pressed(true);
134 none_key.set_shift_pressed(true);
135 #ifdef MACOS_ENABLED
136 CHECK(none_key.as_text() != "Ctrl+Shift+Option+Enter");
137 CHECK(none_key.as_text() == "Ctrl+Option+Shift+Enter");
138 #else
139 CHECK(none_key.as_text() != "Ctrl+Shift+Alt+Enter");
140 CHECK(none_key.as_text() == "Ctrl+Alt+Shift+Enter");
141 #endif
142 none_key.set_physical_keycode(Key::W);
143 #ifdef MACOS_ENABLED
144 CHECK(none_key.as_text() != "Ctrl+Shift+Option+W - Physical");
145 CHECK(none_key.as_text() == "Ctrl+Option+Shift+W - Physical");
146 #else
147 CHECK(none_key.as_text() != "Ctrl+Shift+Alt+W - Physical");
148 CHECK(none_key.as_text() == "Ctrl+Alt+Shift+W - Physical");
149 #endif
150
151 InputEventKey none_key2;
152
153 // Key is None without modifiers with a physical key.
154 none_key2.set_keycode(Key::NONE);
155 none_key2.set_physical_keycode(Key::W);
156
157 CHECK(none_key2.as_text() == "W - Physical");
158
159 none_key2.set_keycode(Key::NONE);
160 none_key2.set_physical_keycode(Key::ENTER);
161
162 CHECK(none_key2.as_text() == "Enter");
163
164 InputEventKey key;
165
166 // Key has keycode.
167 key.set_keycode(Key::SPACE);
168 CHECK(key.as_text() != "");
169 CHECK(key.as_text() == "Space");
170
171 // Key has keycode and modifiers.
172 key.set_ctrl_pressed(true);
173 CHECK(key.as_text() != "Space");
174 CHECK(key.as_text() == "Ctrl+Space");
175
176 // Key has keycode and multiple modifiers, checks for correct ordering.
177 key.set_alt_pressed(true);
178 key.set_shift_pressed(true);
179 #ifdef MACOS_ENABLED
180 CHECK(key.as_text() != "Ctrl+Shift+Option+Space");
181 CHECK(key.as_text() == "Ctrl+Option+Shift+Space");
182 #else
183 CHECK(key.as_text() != "Ctrl+Shift+Alt+Space");
184 CHECK(key.as_text() == "Ctrl+Alt+Shift+Space");
185 #endif
186
187 // Since the keycode is set to Key::NONE upon initialization of the
188 // InputEventKey and you can only update it with another Key, the keycode
189 // cannot be empty, so the kc.is_empty() case cannot be tested.
190 }
191
192 TEST_CASE("[InputEventKey] Key correctly converts its state to a string representation") {
193 InputEventKey none_key;
194
195 CHECK(none_key.to_string() == "InputEventKey: keycode=(unset), mods=none, physical=false, location=unspecified, pressed=false, echo=false");
196 // Set physical key to Escape.
197 none_key.set_physical_keycode(Key::ESCAPE);
198 CHECK(none_key.to_string() == "InputEventKey: keycode=4194305 (Escape), mods=none, physical=true, location=unspecified, pressed=false, echo=false");
199
200 InputEventKey key;
201
202 // Set physical to None, set keycode to Space.
203 key.set_keycode(Key::SPACE);
204 CHECK(key.to_string() == "InputEventKey: keycode=32 (Space), mods=none, physical=false, location=unspecified, pressed=false, echo=false");
205
206 // Set location
207 key.set_location(KeyLocation::RIGHT);
208 CHECK(key.to_string() == "InputEventKey: keycode=32 (Space), mods=none, physical=false, location=right, pressed=false, echo=false");
209
210 // Set pressed to true.
211 key.set_pressed(true);
212 CHECK(key.to_string() == "InputEventKey: keycode=32 (Space), mods=none, physical=false, location=right, pressed=true, echo=false");
213
214 // set echo to true.
215 key.set_echo(true);
216 CHECK(key.to_string() == "InputEventKey: keycode=32 (Space), mods=none, physical=false, location=right, pressed=true, echo=true");
217
218 // Press Ctrl and Alt.
219 key.set_ctrl_pressed(true);
220 key.set_alt_pressed(true);
221 #ifdef MACOS_ENABLED
222 CHECK(key.to_string() == "InputEventKey: keycode=32 (Space), mods=Ctrl+Option, physical=false, location=right, pressed=true, echo=true");
223 #else
224 CHECK(key.to_string() == "InputEventKey: keycode=32 (Space), mods=Ctrl+Alt, physical=false, location=right, pressed=true, echo=true");
225 #endif
226 }
227
228 TEST_CASE("[InputEventKey] Key is correctly converted to reference") {
229 InputEventKey base_key;
230 Ref<InputEventKey> key_ref = base_key.create_reference(Key::ENTER);
231
232 CHECK(key_ref->get_keycode() == Key::ENTER);
233 }
234
235 TEST_CASE("[InputEventKey] Keys are correctly matched based on action") {
236 bool pressed = false;
237 float strength, raw_strength = 0.0;
238
239 InputEventKey key;
240
241 // Nullptr.
242 CHECK_MESSAGE(key.action_match(nullptr, false, 0.0f, &pressed, &strength, &raw_strength) == false, "nullptr as key reference should result in false");
243
244 // Match on keycode.
245 key.set_keycode(Key::SPACE);
246 Ref<InputEventKey> match = key.create_reference(Key::SPACE);
247 Ref<InputEventKey> no_match = key.create_reference(Key::ENTER);
248
249 CHECK(key.action_match(match, false, 0.0f, &pressed, &strength, &raw_strength) == true);
250 CHECK(key.action_match(no_match, false, 0.0f, &pressed, &strength, &raw_strength) == false);
251
252 // Check that values are correctly transferred to the pointers.
253 CHECK(pressed == false);
254 CHECK(strength < 0.5);
255 CHECK(raw_strength < 0.5);
256
257 match->set_pressed(true);
258 key.action_match(match, false, 0.0f, &pressed, &strength, &raw_strength);
259
260 CHECK(pressed == true);
261 CHECK(strength > 0.5);
262 CHECK(raw_strength > 0.5);
263
264 // Tests when keycode is None: Then you rely on physical keycode.
265 InputEventKey none_key;
266 none_key.set_physical_keycode(Key::SPACE);
267
268 Ref<InputEventKey> match_none = none_key.create_reference(Key::NONE);
269 match_none->set_physical_keycode(Key::SPACE);
270
271 Ref<InputEventKey> no_match_none = none_key.create_reference(Key::NONE);
272 no_match_none->set_physical_keycode(Key::ENTER);
273
274 CHECK(none_key.action_match(match_none, false, 0.0f, &pressed, &strength, &raw_strength) == true);
275 CHECK(none_key.action_match(no_match_none, false, 0.0f, &pressed, &strength, &raw_strength) == false);
276
277 // Test exact match.
278 InputEventKey key2, ref_key;
279 key2.set_keycode(Key::SPACE);
280
281 Ref<InputEventKey> match2 = ref_key.create_reference(Key::SPACE);
282
283 // Now both press Ctrl and Shift.
284 key2.set_ctrl_pressed(true);
285 key2.set_shift_pressed(true);
286
287 match2->set_ctrl_pressed(true);
288 match2->set_shift_pressed(true);
289
290 // Now they should match.
291 bool exact_match = true;
292 CHECK(key2.action_match(match2, exact_match, 0.0f, &pressed, &strength, &raw_strength) == true);
293
294 // Modify matching key such that it does no longer match in terms of modifiers: Shift
295 // is no longer pressed.
296 match2->set_shift_pressed(false);
297 CHECK(match2->is_shift_pressed() == false);
298 CHECK(key2.action_match(match2, exact_match, 0.0f, &pressed, &strength, &raw_strength) == false);
299 }
300
301 TEST_CASE("[IsMatch] Keys are correctly matched") {
302 // Key with NONE as keycode.
303 InputEventKey key;
304 key.set_keycode(Key::NONE);
305 key.set_physical_keycode(Key::SPACE);
306
307 // Nullptr.
308 CHECK(key.is_match(nullptr, false) == false);
309
310 Ref<InputEventKey> none_ref = key.create_reference(Key::NONE);
311
312 none_ref->set_physical_keycode(Key::SPACE);
313 CHECK(key.is_match(none_ref, false) == true);
314
315 none_ref->set_physical_keycode(Key::ENTER);
316 CHECK(key.is_match(none_ref, false) == false);
317
318 none_ref->set_physical_keycode(Key::SPACE);
319
320 key.set_ctrl_pressed(true);
321 none_ref->set_ctrl_pressed(false);
322 CHECK(key.is_match(none_ref, true) == false);
323
324 none_ref->set_ctrl_pressed(true);
325 CHECK(key.is_match(none_ref, true) == true);
326
327 // Ref with actual keycode.
328 InputEventKey key2;
329 key2.set_keycode(Key::SPACE);
330
331 Ref<InputEventKey> match = key2.create_reference(Key::SPACE);
332 Ref<InputEventKey> no_match = key2.create_reference(Key::ENTER);
333
334 CHECK(key2.is_match(match, false) == true);
335 CHECK(key2.is_match(no_match, false) == false);
336
337 // Now the keycode is the same, but the modifiers differ.
338 no_match->set_keycode(Key::SPACE);
339
340 key2.set_ctrl_pressed(true);
341 match->set_ctrl_pressed(true);
342 no_match->set_shift_pressed(true);
343
344 CHECK(key2.is_match(match, true) == true);
345 CHECK(key2.is_match(no_match, true) == false);
346
347 // Physical key with location.
348 InputEventKey key3;
349 key3.set_keycode(Key::NONE);
350 key3.set_physical_keycode(Key::SHIFT);
351
352 Ref<InputEventKey> loc_ref = key.create_reference(Key::NONE);
353
354 loc_ref->set_keycode(Key::SHIFT);
355 loc_ref->set_physical_keycode(Key::SHIFT);
356
357 CHECK(key3.is_match(loc_ref, false) == true);
358 key3.set_location(KeyLocation::UNSPECIFIED);
359 CHECK(key3.is_match(loc_ref, false) == true);
360
361 loc_ref->set_location(KeyLocation::LEFT);
362 CHECK(key3.is_match(loc_ref, false) == true);
363
364 key3.set_location(KeyLocation::LEFT);
365 CHECK(key3.is_match(loc_ref, false) == true);
366
367 key3.set_location(KeyLocation::RIGHT);
368 CHECK(key3.is_match(loc_ref, false) == false);
369
370 // Keycode key with location.
371 key3.set_physical_keycode(Key::NONE);
372 key3.set_keycode(Key::SHIFT);
373 CHECK(key3.is_match(loc_ref, false) == true);
374 }
375 } // namespace TestInputEventKey