Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/scene/test_grid_container.cpp
59209 views
1
/**************************************************************************/
2
/* test_grid_container.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_grid_container)
34
35
#include "scene/gui/control.h"
36
#include "scene/gui/grid_container.h"
37
#include "scene/main/scene_tree.h"
38
#include "scene/main/window.h"
39
40
namespace TestGridContainer {
41
42
TEST_CASE("[SceneTree][GridContainer] Default properties") {
43
GridContainer *grid_container = memnew(GridContainer);
44
Window *root = SceneTree::get_singleton()->get_root();
45
root->add_child(grid_container);
46
47
grid_container->set_size(Size2(200, 200));
48
grid_container->add_theme_constant_override("h_separation", 0);
49
grid_container->add_theme_constant_override("v_separation", 0);
50
51
Control *child_control = memnew(Control);
52
child_control->set_custom_minimum_size(Size2(20, 10));
53
grid_container->add_child(child_control);
54
SceneTree::get_singleton()->process(0);
55
56
CHECK_MESSAGE(
57
grid_container->get_columns() == 1,
58
"GridContainer columns are set to 1 by default.");
59
60
CHECK_MESSAGE(
61
child_control->get_position().is_equal_approx(Point2(0, 0)),
62
"Single child control starts at the top-left corner.");
63
64
CHECK_MESSAGE(
65
child_control->get_size().is_equal_approx(Size2(20, 10)),
66
"Single child control keeps its minimum size when no expansion is requested.");
67
68
memdelete(child_control);
69
memdelete(grid_container);
70
}
71
72
TEST_CASE("[SceneTree][GridContainer] Columns and placement order") {
73
GridContainer *grid_container = memnew(GridContainer);
74
Window *root = SceneTree::get_singleton()->get_root();
75
root->add_child(grid_container);
76
77
Control *child_control_1 = memnew(Control);
78
Control *child_control_2 = memnew(Control);
79
Control *child_control_3 = memnew(Control);
80
Control *child_control_4 = memnew(Control);
81
child_control_1->set_custom_minimum_size(Size2(10, 10));
82
child_control_2->set_custom_minimum_size(Size2(10, 10));
83
child_control_3->set_custom_minimum_size(Size2(10, 10));
84
child_control_4->set_custom_minimum_size(Size2(10, 10));
85
86
grid_container->set_columns(2);
87
grid_container->set_size(Size2(200, 200));
88
grid_container->add_theme_constant_override("h_separation", 0);
89
grid_container->add_theme_constant_override("v_separation", 0);
90
grid_container->add_child(child_control_1);
91
grid_container->add_child(child_control_2);
92
grid_container->add_child(child_control_3);
93
grid_container->add_child(child_control_4);
94
SceneTree::get_singleton()->process(0);
95
96
CHECK_MESSAGE(
97
grid_container->get_columns() == 2,
98
"GridContainer uses the configured number of columns.");
99
100
CHECK_MESSAGE(
101
child_control_1->get_position().is_equal_approx(Point2(0, 0)),
102
"First child control is placed at row 0, column 0.");
103
104
CHECK_MESSAGE(
105
child_control_2->get_position().is_equal_approx(Point2(10, 0)),
106
"Second child control is placed at row 0, column 1.");
107
108
CHECK_MESSAGE(
109
child_control_3->get_position().is_equal_approx(Point2(0, 10)),
110
"Third child control is placed at row 1, column 0.");
111
112
CHECK_MESSAGE(
113
child_control_4->get_position().is_equal_approx(Point2(10, 10)),
114
"Fourth child control is placed at row 1, column 1.");
115
116
memdelete(child_control_4);
117
memdelete(child_control_3);
118
memdelete(child_control_2);
119
memdelete(child_control_1);
120
memdelete(grid_container);
121
}
122
123
TEST_CASE("[SceneTree][GridContainer] Separation and minimum size") {
124
GridContainer *grid_container = memnew(GridContainer);
125
Window *root = SceneTree::get_singleton()->get_root();
126
root->add_child(grid_container);
127
128
Control *child_control_1 = memnew(Control);
129
Control *child_control_2 = memnew(Control);
130
Control *child_control_3 = memnew(Control);
131
child_control_1->set_custom_minimum_size(Size2(10, 20));
132
child_control_2->set_custom_minimum_size(Size2(30, 10));
133
child_control_3->set_custom_minimum_size(Size2(15, 40));
134
135
grid_container->set_columns(2);
136
grid_container->add_theme_constant_override("h_separation", 5);
137
grid_container->add_theme_constant_override("v_separation", 7);
138
grid_container->add_child(child_control_1);
139
grid_container->add_child(child_control_2);
140
grid_container->add_child(child_control_3);
141
SceneTree::get_singleton()->process(0);
142
143
CHECK_MESSAGE(
144
grid_container->get_h_separation() == 5,
145
"GridContainer reports overridden horizontal separation.");
146
147
CHECK_MESSAGE(
148
grid_container->get_minimum_size().is_equal_approx(Size2(50, 67)),
149
"GridContainer minimum size matches per-column and per-row maxima plus separations.");
150
151
grid_container->set_size(Size2(50, 67));
152
SceneTree::get_singleton()->process(0);
153
154
CHECK_MESSAGE(
155
child_control_1->get_position().is_equal_approx(Point2(0, 0)),
156
"First child control is placed at row 0, column 0 with configured separations.");
157
158
CHECK_MESSAGE(
159
child_control_2->get_position().is_equal_approx(Point2(20, 0)),
160
"Second child control is offset by column width and horizontal separation.");
161
162
CHECK_MESSAGE(
163
child_control_3->get_position().is_equal_approx(Point2(0, 27)),
164
"Third child control is offset by row height and vertical separation.");
165
166
CHECK_MESSAGE(
167
child_control_1->get_size().is_equal_approx(Size2(15, 20)),
168
"First child control fills the first grid cell dimensions.");
169
170
CHECK_MESSAGE(
171
child_control_2->get_size().is_equal_approx(Size2(30, 20)),
172
"Second child control fills the second grid cell dimensions.");
173
174
CHECK_MESSAGE(
175
child_control_3->get_size().is_equal_approx(Size2(15, 40)),
176
"Third child control fills the second-row first-column cell dimensions.");
177
178
memdelete(child_control_3);
179
memdelete(child_control_2);
180
memdelete(child_control_1);
181
memdelete(grid_container);
182
}
183
184
TEST_CASE("[SceneTree][GridContainer] Expanding rows and columns") {
185
GridContainer *grid_container = memnew(GridContainer);
186
Window *root = SceneTree::get_singleton()->get_root();
187
root->add_child(grid_container);
188
189
Control *child_control_1 = memnew(Control);
190
Control *child_control_2 = memnew(Control);
191
Control *child_control_3 = memnew(Control);
192
child_control_1->set_custom_minimum_size(Size2(20, 20));
193
child_control_2->set_custom_minimum_size(Size2(20, 20));
194
child_control_3->set_custom_minimum_size(Size2(20, 20));
195
child_control_1->set_h_size_flags(Control::SIZE_EXPAND_FILL);
196
child_control_1->set_v_size_flags(Control::SIZE_EXPAND_FILL);
197
198
grid_container->set_columns(2);
199
grid_container->add_theme_constant_override("h_separation", 0);
200
grid_container->add_theme_constant_override("v_separation", 0);
201
grid_container->set_size(Size2(100, 100));
202
grid_container->add_child(child_control_1);
203
grid_container->add_child(child_control_2);
204
grid_container->add_child(child_control_3);
205
SceneTree::get_singleton()->process(0);
206
207
CHECK_MESSAGE(
208
child_control_1->get_size().is_equal_approx(Size2(80, 80)),
209
"Expanded child control grows with its expanded column and row.");
210
211
CHECK_MESSAGE(
212
child_control_2->get_size().is_equal_approx(Size2(20, 80)),
213
"Non-expanded column still receives expanded row height.");
214
215
CHECK_MESSAGE(
216
child_control_3->get_size().is_equal_approx(Size2(80, 20)),
217
"Non-expanded row still receives expanded column width.");
218
219
CHECK_MESSAGE(
220
child_control_2->get_position().is_equal_approx(Point2(80, 0)),
221
"Second child control is positioned after expanded first column width.");
222
223
CHECK_MESSAGE(
224
child_control_3->get_position().is_equal_approx(Point2(0, 80)),
225
"Third child control is positioned after expanded first row height.");
226
227
memdelete(child_control_3);
228
memdelete(child_control_2);
229
memdelete(child_control_1);
230
memdelete(grid_container);
231
}
232
233
TEST_CASE("[SceneTree][GridContainer] RTL placement") {
234
GridContainer *grid_container = memnew(GridContainer);
235
Window *root = SceneTree::get_singleton()->get_root();
236
root->add_child(grid_container);
237
238
Control *child_control_1 = memnew(Control);
239
Control *child_control_2 = memnew(Control);
240
child_control_1->set_custom_minimum_size(Size2(20, 20));
241
child_control_2->set_custom_minimum_size(Size2(20, 20));
242
243
grid_container->set_columns(2);
244
grid_container->add_theme_constant_override("h_separation", 0);
245
grid_container->add_theme_constant_override("v_separation", 0);
246
grid_container->set_size(Size2(40, 20));
247
grid_container->add_child(child_control_1);
248
grid_container->add_child(child_control_2);
249
grid_container->set_layout_direction(Control::LAYOUT_DIRECTION_RTL);
250
SceneTree::get_singleton()->process(0);
251
252
CHECK_MESSAGE(
253
child_control_1->get_position().is_equal_approx(Point2(20, 0)),
254
"First child control starts in the right column in RTL mode.");
255
256
CHECK_MESSAGE(
257
child_control_2->get_position().is_equal_approx(Point2(0, 0)),
258
"Second child control is placed in the left column in RTL mode.");
259
260
memdelete(child_control_2);
261
memdelete(child_control_1);
262
memdelete(grid_container);
263
}
264
265
} // namespace TestGridContainer
266
267