| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 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 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
none_key.set_keycode(Key::NONE); |
| 118 |
CHECK(none_key.as_text() == "(unset)"); |
| 119 |
|
| 120 |
|
| 121 |
none_key.set_ctrl_pressed(true); |
| 122 |
CHECK(none_key.as_text() == "Ctrl+(unset)"); |
| 123 |
|
| 124 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 167 |
key.set_keycode(Key::SPACE); |
| 168 |
CHECK(key.as_text() != ""); |
| 169 |
CHECK(key.as_text() == "Space"); |
| 170 |
|
| 171 |
|
| 172 |
key.set_ctrl_pressed(true); |
| 173 |
CHECK(key.as_text() != "Space"); |
| 174 |
CHECK(key.as_text() == "Ctrl+Space"); |
| 175 |
|
| 176 |
|
| 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 |
|
| 188 |
|
| 189 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 278 |
InputEventKey key2, ref_key; |
| 279 |
key2.set_keycode(Key::SPACE); |
| 280 |
|
| 281 |
Ref<InputEventKey> match2 = ref_key.create_reference(Key::SPACE); |
| 282 |
|
| 283 |
|
| 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 |
|
| 291 |
bool exact_match = true; |
| 292 |
CHECK(key2.action_match(match2, exact_match, 0.0f, &pressed, &strength, &raw_strength) == true); |
| 293 |
|
| 294 |
|
| 295 |
|
| 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 |
|
| 303 |
InputEventKey key; |
| 304 |
key.set_keycode(Key::NONE); |
| 305 |
key.set_physical_keycode(Key::SPACE); |
| 306 |
|
| 307 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 371 |
key3.set_physical_keycode(Key::NONE); |
| 372 |
key3.set_keycode(Key::SHIFT); |
| 373 |
CHECK(key3.is_match(loc_ref, false) == true); |
| 374 |
} |
| 375 |
} |