test_input_event_key.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. #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 retrieves location") {
  72. InputEventKey key;
  73. CHECK(key.get_location() == KeyLocation::UNSPECIFIED);
  74. key.set_location(KeyLocation::LEFT);
  75. CHECK(key.get_location() == KeyLocation::LEFT);
  76. CHECK(key.get_location() != KeyLocation::RIGHT);
  77. }
  78. TEST_CASE("[InputEventKey] Key correctly stores and checks echo") {
  79. InputEventKey key;
  80. key.set_echo(true);
  81. CHECK(key.is_echo() == true);
  82. key.set_echo(false);
  83. CHECK(key.is_echo() == false);
  84. }
  85. TEST_CASE("[InputEventKey] Key correctly converts itself to text") {
  86. InputEventKey none_key;
  87. // These next three tests test the functionality of getting a key that is set to None
  88. // as text. These cases are a bit weird, since None has no textual representation
  89. // (find_keycode_name(Key::NONE) results in a nullptr). Thus, these tests look weird
  90. // with only (Physical) or a lonely modifier with (Physical) but (as far as I
  91. // understand the code, that is intended behavior.
  92. // Key is None without a physical key.
  93. none_key.set_keycode(Key::NONE);
  94. CHECK(none_key.as_text() == "(Unset)");
  95. // Key is none and has modifiers.
  96. none_key.set_ctrl_pressed(true);
  97. CHECK(none_key.as_text() == "Ctrl+(Unset)");
  98. // Key is None WITH a physical key AND modifiers.
  99. none_key.set_physical_keycode(Key::ENTER);
  100. CHECK(none_key.as_text() == "Ctrl+Enter (Physical)");
  101. InputEventKey none_key2;
  102. // Key is None without modifiers with a physical key.
  103. none_key2.set_keycode(Key::NONE);
  104. none_key2.set_physical_keycode(Key::ENTER);
  105. CHECK(none_key2.as_text() == "Enter (Physical)");
  106. InputEventKey key;
  107. // Key has keycode.
  108. key.set_keycode(Key::SPACE);
  109. CHECK(key.as_text() != "");
  110. CHECK(key.as_text() == "Space");
  111. // Key has keycode and modifiers.
  112. key.set_ctrl_pressed(true);
  113. CHECK(key.as_text() != "Space");
  114. CHECK(key.as_text() == "Ctrl+Space");
  115. // Since the keycode is set to Key::NONE upon initialization of the
  116. // InputEventKey and you can only update it with another Key, the keycode
  117. // cannot be empty, so the kc.is_empty() case cannot be tested.
  118. }
  119. TEST_CASE("[InputEventKey] Key correctly converts its state to a string representation") {
  120. InputEventKey none_key;
  121. CHECK(none_key.to_string() == "InputEventKey: keycode=(Unset), mods=none, physical=false, location=unspecified, pressed=false, echo=false");
  122. // Set physical key to Escape.
  123. none_key.set_physical_keycode(Key::ESCAPE);
  124. CHECK(none_key.to_string() == "InputEventKey: keycode=4194305 (Escape), mods=none, physical=true, location=unspecified, pressed=false, echo=false");
  125. InputEventKey key;
  126. // Set physical to None, set keycode to Space.
  127. key.set_keycode(Key::SPACE);
  128. CHECK(key.to_string() == "InputEventKey: keycode=32 (Space), mods=none, physical=false, location=unspecified, pressed=false, echo=false");
  129. // Set location
  130. key.set_location(KeyLocation::RIGHT);
  131. CHECK(key.to_string() == "InputEventKey: keycode=32 (Space), mods=none, physical=false, location=right, pressed=false, echo=false");
  132. // Set pressed to true.
  133. key.set_pressed(true);
  134. CHECK(key.to_string() == "InputEventKey: keycode=32 (Space), mods=none, physical=false, location=right, pressed=true, echo=false");
  135. // set echo to true.
  136. key.set_echo(true);
  137. CHECK(key.to_string() == "InputEventKey: keycode=32 (Space), mods=none, physical=false, location=right, pressed=true, echo=true");
  138. // Press Ctrl and Alt.
  139. key.set_ctrl_pressed(true);
  140. key.set_alt_pressed(true);
  141. #ifdef MACOS_ENABLED
  142. CHECK(key.to_string() == "InputEventKey: keycode=32 (Space), mods=Ctrl+Option, physical=false, location=right, pressed=true, echo=true");
  143. #else
  144. CHECK(key.to_string() == "InputEventKey: keycode=32 (Space), mods=Ctrl+Alt, physical=false, location=right, pressed=true, echo=true");
  145. #endif
  146. }
  147. TEST_CASE("[InputEventKey] Key is correctly converted to reference") {
  148. InputEventKey base_key;
  149. Ref<InputEventKey> key_ref = base_key.create_reference(Key::ENTER);
  150. CHECK(key_ref->get_keycode() == Key::ENTER);
  151. }
  152. TEST_CASE("[InputEventKey] Keys are correctly matched based on action") {
  153. bool pressed = false;
  154. float strength, raw_strength = 0.0;
  155. InputEventKey key;
  156. // Nullptr.
  157. CHECK_MESSAGE(key.action_match(nullptr, false, 0.0f, &pressed, &strength, &raw_strength) == false, "nullptr as key reference should result in false");
  158. // Match on keycode.
  159. key.set_keycode(Key::SPACE);
  160. Ref<InputEventKey> match = key.create_reference(Key::SPACE);
  161. Ref<InputEventKey> no_match = key.create_reference(Key::ENTER);
  162. CHECK(key.action_match(match, false, 0.0f, &pressed, &strength, &raw_strength) == true);
  163. CHECK(key.action_match(no_match, false, 0.0f, &pressed, &strength, &raw_strength) == false);
  164. // Check that values are correctly transferred to the pointers.
  165. CHECK(pressed == false);
  166. CHECK(strength < 0.5);
  167. CHECK(raw_strength < 0.5);
  168. match->set_pressed(true);
  169. key.action_match(match, false, 0.0f, &pressed, &strength, &raw_strength);
  170. CHECK(pressed == true);
  171. CHECK(strength > 0.5);
  172. CHECK(raw_strength > 0.5);
  173. // Tests when keycode is None: Then you rely on physical keycode.
  174. InputEventKey none_key;
  175. none_key.set_physical_keycode(Key::SPACE);
  176. Ref<InputEventKey> match_none = none_key.create_reference(Key::NONE);
  177. match_none->set_physical_keycode(Key::SPACE);
  178. Ref<InputEventKey> no_match_none = none_key.create_reference(Key::NONE);
  179. no_match_none->set_physical_keycode(Key::ENTER);
  180. CHECK(none_key.action_match(match_none, false, 0.0f, &pressed, &strength, &raw_strength) == true);
  181. CHECK(none_key.action_match(no_match_none, false, 0.0f, &pressed, &strength, &raw_strength) == false);
  182. // Test exact match.
  183. InputEventKey key2, ref_key;
  184. key2.set_keycode(Key::SPACE);
  185. Ref<InputEventKey> match2 = ref_key.create_reference(Key::SPACE);
  186. // Now both press Ctrl and Shift.
  187. key2.set_ctrl_pressed(true);
  188. key2.set_shift_pressed(true);
  189. match2->set_ctrl_pressed(true);
  190. match2->set_shift_pressed(true);
  191. // Now they should match.
  192. bool exact_match = true;
  193. CHECK(key2.action_match(match2, exact_match, 0.0f, &pressed, &strength, &raw_strength) == true);
  194. // Modify matching key such that it does no longer match in terms of modifiers: Shift
  195. // is no longer pressed.
  196. match2->set_shift_pressed(false);
  197. CHECK(match2->is_shift_pressed() == false);
  198. CHECK(key2.action_match(match2, exact_match, 0.0f, &pressed, &strength, &raw_strength) == false);
  199. }
  200. TEST_CASE("[IsMatch] Keys are correctly matched") {
  201. // Key with NONE as keycode.
  202. InputEventKey key;
  203. key.set_keycode(Key::NONE);
  204. key.set_physical_keycode(Key::SPACE);
  205. // Nullptr.
  206. CHECK(key.is_match(nullptr, false) == false);
  207. Ref<InputEventKey> none_ref = key.create_reference(Key::NONE);
  208. none_ref->set_physical_keycode(Key::SPACE);
  209. CHECK(key.is_match(none_ref, false) == true);
  210. none_ref->set_physical_keycode(Key::ENTER);
  211. CHECK(key.is_match(none_ref, false) == false);
  212. none_ref->set_physical_keycode(Key::SPACE);
  213. key.set_ctrl_pressed(true);
  214. none_ref->set_ctrl_pressed(false);
  215. CHECK(key.is_match(none_ref, true) == false);
  216. none_ref->set_ctrl_pressed(true);
  217. CHECK(key.is_match(none_ref, true) == true);
  218. // Ref with actual keycode.
  219. InputEventKey key2;
  220. key2.set_keycode(Key::SPACE);
  221. Ref<InputEventKey> match = key2.create_reference(Key::SPACE);
  222. Ref<InputEventKey> no_match = key2.create_reference(Key::ENTER);
  223. CHECK(key2.is_match(match, false) == true);
  224. CHECK(key2.is_match(no_match, false) == false);
  225. // Now the keycode is the same, but the modifiers differ.
  226. no_match->set_keycode(Key::SPACE);
  227. key2.set_ctrl_pressed(true);
  228. match->set_ctrl_pressed(true);
  229. no_match->set_shift_pressed(true);
  230. CHECK(key2.is_match(match, true) == true);
  231. CHECK(key2.is_match(no_match, true) == false);
  232. // Physical key with location.
  233. InputEventKey key3;
  234. key3.set_keycode(Key::NONE);
  235. key3.set_physical_keycode(Key::SHIFT);
  236. Ref<InputEventKey> loc_ref = key.create_reference(Key::NONE);
  237. loc_ref->set_keycode(Key::SHIFT);
  238. loc_ref->set_physical_keycode(Key::SHIFT);
  239. CHECK(key3.is_match(loc_ref, false) == true);
  240. key3.set_location(KeyLocation::UNSPECIFIED);
  241. CHECK(key3.is_match(loc_ref, false) == true);
  242. loc_ref->set_location(KeyLocation::LEFT);
  243. CHECK(key3.is_match(loc_ref, false) == true);
  244. key3.set_location(KeyLocation::LEFT);
  245. CHECK(key3.is_match(loc_ref, false) == true);
  246. key3.set_location(KeyLocation::RIGHT);
  247. CHECK(key3.is_match(loc_ref, false) == false);
  248. // Keycode key with location.
  249. key3.set_physical_keycode(Key::NONE);
  250. key3.set_keycode(Key::SHIFT);
  251. CHECK(key3.is_match(loc_ref, false) == true);
  252. }
  253. } // namespace TestInputEventKey
  254. #endif // TEST_INPUT_EVENT_KEY_H