test_input_event_key.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. #ifndef TEST_INPUT_EVENT_KEY_H
  31. #define TEST_INPUT_EVENT_KEY_H
  32. #include "core/input/input_event.h"
  33. #include "core/os/keyboard.h"
  34. #include "tests/test_macros.h"
  35. namespace TestInputEventKey {
  36. TEST_CASE("[InputEventKey] Key correctly registers being pressed") {
  37. InputEventKey key;
  38. key.set_pressed(true);
  39. CHECK(key.is_pressed() == true);
  40. key.set_pressed(false);
  41. CHECK(key.is_pressed() == false);
  42. }
  43. TEST_CASE("[InputEventKey] Key correctly stores and retrieves keycode") {
  44. InputEventKey key;
  45. key.set_keycode(Key::ENTER);
  46. CHECK(key.get_keycode() == Key::ENTER);
  47. CHECK(key.get_keycode() != Key::PAUSE);
  48. key.set_physical_keycode(Key::BACKSPACE);
  49. CHECK(key.get_physical_keycode() == Key::BACKSPACE);
  50. CHECK(key.get_physical_keycode() != Key::PAUSE);
  51. }
  52. TEST_CASE("[InputEventKey] Key correctly stores and retrieves keycode with modifiers") {
  53. InputEventKey key;
  54. key.set_keycode(Key::ENTER);
  55. key.set_ctrl_pressed(true);
  56. CHECK(key.get_keycode_with_modifiers() == (Key::ENTER | KeyModifierMask::CTRL));
  57. CHECK(key.get_keycode_with_modifiers() != (Key::ENTER | KeyModifierMask::SHIFT));
  58. CHECK(key.get_keycode_with_modifiers() != Key::ENTER);
  59. key.set_physical_keycode(Key::SPACE);
  60. key.set_ctrl_pressed(true);
  61. CHECK(key.get_physical_keycode_with_modifiers() == (Key::SPACE | KeyModifierMask::CTRL));
  62. CHECK(key.get_physical_keycode_with_modifiers() != (Key::SPACE | KeyModifierMask::SHIFT));
  63. CHECK(key.get_physical_keycode_with_modifiers() != Key::SPACE);
  64. }
  65. TEST_CASE("[InputEventKey] Key correctly stores and retrieves unicode") {
  66. InputEventKey key;
  67. key.set_unicode('x');
  68. CHECK(key.get_unicode() == 'x');
  69. CHECK(key.get_unicode() != 'y');
  70. }
  71. TEST_CASE("[InputEventKey] Key correctly stores and checks echo") {
  72. InputEventKey key;
  73. key.set_echo(true);
  74. CHECK(key.is_echo() == true);
  75. key.set_echo(false);
  76. CHECK(key.is_echo() == false);
  77. }
  78. TEST_CASE("[InputEventKey] Key correctly converts itself to text") {
  79. InputEventKey none_key;
  80. // These next three tests test the functionality of getting a key that is set to None
  81. // as text. These cases are a bit weird, since None has no textual representation
  82. // (find_keycode_name(Key::NONE) results in a nullptr). Thus, these tests look weird
  83. // with only (Physical) or a lonely modifier with (Physical) but (as far as I
  84. // understand the code, that is intended behaviour.
  85. // Key is None without a physical key.
  86. none_key.set_keycode(Key::NONE);
  87. CHECK(none_key.as_text() == " (Physical)");
  88. // Key is none and has modifiers.
  89. none_key.set_ctrl_pressed(true);
  90. CHECK(none_key.as_text() == "Ctrl+ (Physical)");
  91. // Key is None WITH a physical key AND modifiers.
  92. none_key.set_physical_keycode(Key::ENTER);
  93. CHECK(none_key.as_text() == "Ctrl+Enter (Physical)");
  94. InputEventKey none_key2;
  95. // Key is None without modifers with a physical key.
  96. none_key2.set_keycode(Key::NONE);
  97. none_key2.set_physical_keycode(Key::ENTER);
  98. CHECK(none_key2.as_text() == "Enter (Physical)");
  99. InputEventKey key;
  100. // Key has keycode.
  101. key.set_keycode(Key::SPACE);
  102. CHECK(key.as_text() != "");
  103. CHECK(key.as_text() == "Space");
  104. // Key has keycode and modifiers.
  105. key.set_ctrl_pressed(true);
  106. CHECK(key.as_text() != "Space");
  107. CHECK(key.as_text() == "Ctrl+Space");
  108. // Since the keycode is set to Key::NONE upon initialization of the
  109. // InputEventKey and you can only update it with another Key, the keycode
  110. // cannot be empty, so the kc.is_empty() case cannot be tested.
  111. }
  112. TEST_CASE("[InputEventKey] Key correctly converts its state to a string representation") {
  113. InputEventKey none_key;
  114. // Set physical to true.
  115. CHECK(none_key.to_string() == "InputEventKey: keycode=0 (), mods=none, physical=true, pressed=false, echo=false");
  116. // Set physical key to Escape.
  117. none_key.set_physical_keycode(Key::ESCAPE);
  118. CHECK(none_key.to_string() == "InputEventKey: keycode=16777217 (Escape), mods=none, physical=true, pressed=false, echo=false");
  119. InputEventKey key;
  120. // Set physical to None, set keycode to Space.
  121. key.set_keycode(Key::SPACE);
  122. CHECK(key.to_string() == "InputEventKey: keycode=32 (Space), mods=none, physical=false, pressed=false, echo=false");
  123. // Set pressed to true.
  124. key.set_pressed(true);
  125. CHECK(key.to_string() == "InputEventKey: keycode=32 (Space), mods=none, physical=false, pressed=true, echo=false");
  126. // set echo to true.
  127. key.set_echo(true);
  128. CHECK(key.to_string() == "InputEventKey: keycode=32 (Space), mods=none, physical=false, pressed=true, echo=true");
  129. // Press Ctrl and Alt.
  130. key.set_ctrl_pressed(true);
  131. key.set_alt_pressed(true);
  132. CHECK(key.to_string() == "InputEventKey: keycode=32 (Space), mods=Ctrl+Alt, physical=false, pressed=true, echo=true");
  133. }
  134. TEST_CASE("[InputEventKey] Key is correctly converted to reference") {
  135. InputEventKey base_key;
  136. Ref<InputEventKey> key_ref = base_key.create_reference(Key::ENTER);
  137. CHECK(key_ref->get_keycode() == Key::ENTER);
  138. }
  139. TEST_CASE("[InputEventKey] Keys are correctly matched based on action") {
  140. bool pressed = false;
  141. float strength, raw_strength = 0.0;
  142. InputEventKey key;
  143. // Nullptr.
  144. CHECK_MESSAGE(key.action_match(nullptr, false, 0.0f, &pressed, &strength, &raw_strength) == false, "nullptr as key reference should result in false");
  145. // Match on keycode.
  146. key.set_keycode(Key::SPACE);
  147. Ref<InputEventKey> match = key.create_reference(Key::SPACE);
  148. Ref<InputEventKey> no_match = key.create_reference(Key::ENTER);
  149. CHECK(key.action_match(match, false, 0.0f, &pressed, &strength, &raw_strength) == true);
  150. CHECK(key.action_match(no_match, false, 0.0f, &pressed, &strength, &raw_strength) == false);
  151. // Check that values are correctly transferred to the pointers.
  152. CHECK(pressed == false);
  153. CHECK(strength < 0.5);
  154. CHECK(raw_strength < 0.5);
  155. match->set_pressed(true);
  156. key.action_match(match, false, 0.0f, &pressed, &strength, &raw_strength);
  157. CHECK(pressed == true);
  158. CHECK(strength > 0.5);
  159. CHECK(raw_strength > 0.5);
  160. // Tests when keycode is None: Then you rely on physical keycode.
  161. InputEventKey none_key;
  162. none_key.set_physical_keycode(Key::SPACE);
  163. Ref<InputEventKey> match_none = none_key.create_reference(Key::NONE);
  164. match_none->set_physical_keycode(Key::SPACE);
  165. Ref<InputEventKey> no_match_none = none_key.create_reference(Key::NONE);
  166. no_match_none->set_physical_keycode(Key::ENTER);
  167. CHECK(none_key.action_match(match_none, false, 0.0f, &pressed, &strength, &raw_strength) == true);
  168. CHECK(none_key.action_match(no_match_none, false, 0.0f, &pressed, &strength, &raw_strength) == false);
  169. // Test exact match.
  170. InputEventKey key2, ref_key;
  171. key2.set_keycode(Key::SPACE);
  172. Ref<InputEventKey> match2 = ref_key.create_reference(Key::SPACE);
  173. // Now both press Ctrl and Shift.
  174. key2.set_ctrl_pressed(true);
  175. key2.set_shift_pressed(true);
  176. match2->set_ctrl_pressed(true);
  177. match2->set_shift_pressed(true);
  178. // Now they should match.
  179. bool exact_match = true;
  180. CHECK(key2.action_match(match2, exact_match, 0.0f, &pressed, &strength, &raw_strength) == true);
  181. // Modify matching key such that it does no longer match in terms of modifiers: Shift
  182. // is no longer pressed.
  183. match2->set_shift_pressed(false);
  184. CHECK(match2->is_shift_pressed() == false);
  185. CHECK(key2.action_match(match2, exact_match, 0.0f, &pressed, &strength, &raw_strength) == false);
  186. }
  187. TEST_CASE("[IsMatch] Keys are correctly matched") {
  188. // Key with NONE as keycode.
  189. InputEventKey key;
  190. key.set_keycode(Key::NONE);
  191. key.set_physical_keycode(Key::SPACE);
  192. // Nullptr.
  193. CHECK(key.is_match(nullptr, false) == false);
  194. Ref<InputEventKey> none_ref = key.create_reference(Key::NONE);
  195. none_ref->set_physical_keycode(Key::SPACE);
  196. CHECK(key.is_match(none_ref, false) == true);
  197. none_ref->set_physical_keycode(Key::ENTER);
  198. CHECK(key.is_match(none_ref, false) == false);
  199. none_ref->set_physical_keycode(Key::SPACE);
  200. key.set_ctrl_pressed(true);
  201. none_ref->set_ctrl_pressed(false);
  202. CHECK(key.is_match(none_ref, true) == false);
  203. none_ref->set_ctrl_pressed(true);
  204. CHECK(key.is_match(none_ref, true) == true);
  205. // Ref with actual keycode.
  206. InputEventKey key2;
  207. key2.set_keycode(Key::SPACE);
  208. Ref<InputEventKey> match = key2.create_reference(Key::SPACE);
  209. Ref<InputEventKey> no_match = key2.create_reference(Key::ENTER);
  210. CHECK(key2.is_match(match, false) == true);
  211. CHECK(key2.is_match(no_match, false) == false);
  212. // Now the keycode is the same, but the modifiers differ.
  213. no_match->set_keycode(Key::SPACE);
  214. key2.set_ctrl_pressed(true);
  215. match->set_ctrl_pressed(true);
  216. no_match->set_shift_pressed(true);
  217. CHECK(key2.is_match(match, true) == true);
  218. CHECK(key2.is_match(no_match, true) == false);
  219. }
  220. } // namespace TestInputEventKey
  221. #endif // TEST_INPUT_EVENT_KEY_H