Debian Codegraph @

tests/core/input/test_shortcut.h

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