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