test_shortcut.h 7.2 KB

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