test_shortcut.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**************************************************************************/
  2. /* test_shortcut.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_SHORTCUT_H
  31. #define TEST_SHORTCUT_H
  32. #include "core/input/input_event.h"
  33. #include "core/input/shortcut.h"
  34. #include "core/io/config_file.h"
  35. #include "core/object/ref_counted.h"
  36. #include "core/os/keyboard.h"
  37. #include "core/os/os.h"
  38. #include "tests/test_macros.h"
  39. namespace TestShortcut {
  40. TEST_CASE("[Shortcut] Empty shortcut should have no valid events and text equal to None") {
  41. Shortcut s;
  42. CHECK(s.get_as_text() == "None");
  43. CHECK(s.has_valid_event() == false);
  44. }
  45. TEST_CASE("[Shortcut] Setting and getting an event should result in the same event as the input") {
  46. Ref<InputEventKey> k1;
  47. Ref<InputEventKey> k2;
  48. k1.instantiate();
  49. k2.instantiate();
  50. k1->set_keycode(Key::ENTER);
  51. k2->set_keycode(Key::BACKSPACE);
  52. // Cast to InputEvent so the internal code recognizes the objects.
  53. Ref<InputEvent> e1 = k1;
  54. Ref<InputEvent> e2 = k2;
  55. Array input_array;
  56. input_array.append(e1);
  57. input_array.append(e2);
  58. Shortcut s;
  59. s.set_events(input_array);
  60. // Get result, read it out, check whether it equals the input.
  61. Array result_array = s.get_events();
  62. Ref<InputEventKey> result1 = result_array.front();
  63. Ref<InputEventKey> result2 = result_array.back();
  64. CHECK(result1->get_keycode() == k1->get_keycode());
  65. CHECK(result2->get_keycode() == k2->get_keycode());
  66. }
  67. TEST_CASE("[Shortcut] 'set_events_list' should result in the same events as the input") {
  68. Ref<InputEventKey> k1;
  69. Ref<InputEventKey> k2;
  70. k1.instantiate();
  71. k2.instantiate();
  72. k1->set_keycode(Key::ENTER);
  73. k2->set_keycode(Key::BACKSPACE);
  74. // Cast to InputEvent so the set_events_list() method recognizes the objects.
  75. Ref<InputEvent> e1 = k1;
  76. Ref<InputEvent> e2 = k2;
  77. List<Ref<InputEvent>> list;
  78. list.push_back(e1);
  79. list.push_back(e2);
  80. Shortcut s;
  81. s.set_events_list(&list);
  82. // Get result, read it out, check whether it equals the input.
  83. Array result_array = s.get_events();
  84. Ref<InputEventKey> result1 = result_array.front();
  85. Ref<InputEventKey> result2 = result_array.back();
  86. CHECK(result1->get_keycode() == k1->get_keycode());
  87. CHECK(result2->get_keycode() == k2->get_keycode());
  88. }
  89. TEST_CASE("[Shortcut] 'matches_event' should correctly match the same event") {
  90. Ref<InputEventKey> original; // The one we compare with.
  91. Ref<InputEventKey> similar_but_not_equal; // Same keycode, different event.
  92. Ref<InputEventKey> different; // Different event, different keycode.
  93. Ref<InputEventKey> copy; // Copy of original event.
  94. original.instantiate();
  95. similar_but_not_equal.instantiate();
  96. different.instantiate();
  97. copy.instantiate();
  98. original->set_keycode(Key::ENTER);
  99. similar_but_not_equal->set_keycode(Key::ENTER);
  100. similar_but_not_equal->set_keycode(Key::ESCAPE);
  101. copy = original;
  102. // Only the copy is really the same, so only that one should match.
  103. // The rest should not match.
  104. Ref<InputEvent> e_original = original;
  105. Ref<InputEvent> e_similar_but_not_equal = similar_but_not_equal;
  106. Ref<InputEvent> e_different = different;
  107. Ref<InputEvent> e_copy = copy;
  108. Array a;
  109. a.append(e_original);
  110. Shortcut s;
  111. s.set_events(a);
  112. CHECK(s.matches_event(e_similar_but_not_equal) == false);
  113. CHECK(s.matches_event(e_different) == false);
  114. CHECK(s.matches_event(e_copy) == true);
  115. }
  116. TEST_CASE("[Shortcut] 'get_as_text' text representation should be correct") {
  117. Ref<InputEventKey> same;
  118. // k2 will not go into the shortcut but only be used to compare.
  119. Ref<InputEventKey> different;
  120. same.instantiate();
  121. different.instantiate();
  122. same->set_keycode(Key::ENTER);
  123. different->set_keycode(Key::ESCAPE);
  124. Ref<InputEvent> key_event1 = same;
  125. Array a;
  126. a.append(key_event1);
  127. Shortcut s;
  128. s.set_events(a);
  129. CHECK(s.get_as_text() == same->as_text());
  130. CHECK(s.get_as_text() != different->as_text());
  131. }
  132. TEST_CASE("[Shortcut] Event validity should be correctly checked.") {
  133. Ref<InputEventKey> valid;
  134. // k2 will not go into the shortcut but only be used to compare.
  135. Ref<InputEventKey> invalid = nullptr;
  136. valid.instantiate();
  137. valid->set_keycode(Key::ENTER);
  138. Ref<InputEvent> valid_event = valid;
  139. Ref<InputEvent> invalid_event = invalid;
  140. Array a;
  141. a.append(invalid_event);
  142. a.append(valid_event);
  143. Shortcut s;
  144. s.set_events(a);
  145. CHECK(s.has_valid_event() == true);
  146. Array b;
  147. b.append(invalid_event);
  148. Shortcut shortcut_with_invalid_event;
  149. shortcut_with_invalid_event.set_events(b);
  150. CHECK(shortcut_with_invalid_event.has_valid_event() == false);
  151. }
  152. TEST_CASE("[Shortcut] Equal arrays should be recognized as such.") {
  153. Ref<InputEventKey> k1;
  154. // k2 will not go into the shortcut but only be used to compare.
  155. Ref<InputEventKey> k2;
  156. k1.instantiate();
  157. k2.instantiate();
  158. k1->set_keycode(Key::ENTER);
  159. k2->set_keycode(Key::ESCAPE);
  160. Ref<InputEvent> key_event1 = k1;
  161. Ref<InputEvent> key_event2 = k2;
  162. Array same;
  163. same.append(key_event1);
  164. Array same_as_same;
  165. same_as_same.append(key_event1);
  166. Array different1;
  167. different1.append(key_event2);
  168. Array different2;
  169. different2.append(key_event1);
  170. different2.append(key_event2);
  171. Array different3;
  172. Shortcut s;
  173. CHECK(s.is_event_array_equal(same, same_as_same) == true);
  174. CHECK(s.is_event_array_equal(same, different1) == false);
  175. CHECK(s.is_event_array_equal(same, different2) == false);
  176. CHECK(s.is_event_array_equal(same, different3) == false);
  177. }
  178. } // namespace TestShortcut
  179. #endif // TEST_SHORTCUT_H