Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/scene/test_aspect_ratio_container.cpp
59209 views
1
/**************************************************************************/
2
/* test_aspect_ratio_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_aspect_ratio_container)
34
35
#include "scene/gui/aspect_ratio_container.h"
36
#include "scene/gui/control.h"
37
#include "scene/main/scene_tree.h"
38
#include "scene/main/window.h"
39
40
namespace TestAspectRatioContainer {
41
42
TEST_CASE("[SceneTree][AspectRatioContainer] Default Properties") {
43
AspectRatioContainer *aspect_ratio_container = memnew(AspectRatioContainer);
44
Window *root = SceneTree::get_singleton()->get_root();
45
root->add_child(aspect_ratio_container);
46
Control *child_control = memnew(Control);
47
aspect_ratio_container->add_child(child_control);
48
49
CHECK_MESSAGE(
50
aspect_ratio_container->get_stretch_mode() == AspectRatioContainer::STRETCH_FIT,
51
"AspectRatioContainer stretch mode is set to STRETCH_FIT by default.");
52
53
CHECK_MESSAGE(
54
Math::is_equal_approx(aspect_ratio_container->get_ratio(), 1.0f),
55
"AspectRatioContainer ratio is set to 1.0 by default.");
56
57
CHECK_MESSAGE(
58
child_control->get_position().is_equal_approx(Point2(0, 0)),
59
"Child control is positioned at the top-left corner of the AspectRatioContainer by default.");
60
61
CHECK_MESSAGE(
62
child_control->get_h_size_flags() == Control::SIZE_FILL,
63
"Child control horizontal size flags are set to SIZE_FILL by default.");
64
65
CHECK_MESSAGE(
66
child_control->get_v_size_flags() == Control::SIZE_FILL,
67
"Child control vertical size flags are set to SIZE_FILL by default.");
68
69
memdelete(child_control);
70
memdelete(aspect_ratio_container);
71
}
72
73
TEST_CASE("[SceneTree][AspectRatioContainer] Aspect Ratio") {
74
AspectRatioContainer *aspect_ratio_container = memnew(AspectRatioContainer);
75
Window *root = SceneTree::get_singleton()->get_root();
76
root->add_child(aspect_ratio_container);
77
Control *child_control = memnew(Control);
78
aspect_ratio_container->add_child(child_control);
79
80
aspect_ratio_container->set_size(Size2(200, 100));
81
SceneTree::get_singleton()->process(0);
82
83
CHECK_MESSAGE(
84
child_control->get_size().is_equal_approx(Size2(100, 100)),
85
"Child control is resized to maintain aspect ratio when AspectRatioContainer is resized.");
86
87
aspect_ratio_container->set_size(Size2(100, 200));
88
SceneTree::get_singleton()->process(0);
89
90
CHECK_MESSAGE(
91
child_control->get_size().is_equal_approx(Size2(100, 100)),
92
"Child control is resized to maintain aspect ratio when AspectRatioContainer is resized.");
93
94
aspect_ratio_container->set_ratio(2.0f);
95
SceneTree::get_singleton()->process(0);
96
97
CHECK_MESSAGE(
98
child_control->get_size().is_equal_approx(Size2(100, 50)),
99
"Child control is resized to maintain aspect ratio when AspectRatioContainer ratio is changed.");
100
101
aspect_ratio_container->set_ratio(0.5f);
102
SceneTree::get_singleton()->process(0);
103
104
CHECK_MESSAGE(
105
child_control->get_size().is_equal_approx(Size2(100, 200)),
106
"Child control is resized to maintain aspect ratio when AspectRatioContainer ratio is changed.");
107
108
aspect_ratio_container->set_size(Size2(200, 100));
109
SceneTree::get_singleton()->process(0);
110
111
CHECK_MESSAGE(
112
child_control->get_size().is_equal_approx(Size2(50, 100)),
113
"Child control is resized to maintain aspect ratio when AspectRatioContainer is resized after ratio change.");
114
115
memdelete(child_control);
116
memdelete(aspect_ratio_container);
117
}
118
119
TEST_CASE("[SceneTree][AspectRatioContainer] Stretch Modes") {
120
AspectRatioContainer *aspect_ratio_container = memnew(AspectRatioContainer);
121
Window *root = SceneTree::get_singleton()->get_root();
122
root->add_child(aspect_ratio_container);
123
Control *child_control = memnew(Control);
124
aspect_ratio_container->add_child(child_control);
125
126
aspect_ratio_container->set_size(Size2(200, 100));
127
SceneTree::get_singleton()->process(0);
128
129
CHECK_MESSAGE(
130
child_control->get_size().is_equal_approx(Size2(100, 100)),
131
"Child control is resized to maintain aspect ratio in STRETCH_FIT mode.");
132
133
aspect_ratio_container->set_stretch_mode(AspectRatioContainer::STRETCH_WIDTH_CONTROLS_HEIGHT);
134
SceneTree::get_singleton()->process(0);
135
136
CHECK_MESSAGE(
137
child_control->get_size().is_equal_approx(Size2(200, 200)),
138
"Child control height is adjusted to maintain aspect ratio in STRETCH_WIDTH_CONTROLS_HEIGHT mode.");
139
140
aspect_ratio_container->set_stretch_mode(AspectRatioContainer::STRETCH_HEIGHT_CONTROLS_WIDTH);
141
SceneTree::get_singleton()->process(0);
142
143
CHECK_MESSAGE(
144
child_control->get_size().is_equal_approx(Size2(100, 100)),
145
"Child control width is adjusted to maintain aspect ratio in STRETCH_HEIGHT_CONTROLS_WIDTH mode.");
146
147
aspect_ratio_container->set_stretch_mode(AspectRatioContainer::STRETCH_COVER);
148
SceneTree::get_singleton()->process(0);
149
150
CHECK_MESSAGE(
151
child_control->get_size().is_equal_approx(Size2(200, 200)),
152
"Child control is resized to cover the entire AspectRatioContainer in STRETCH_COVER mode.");
153
154
aspect_ratio_container->set_size(Size2(100, 200));
155
aspect_ratio_container->set_stretch_mode(AspectRatioContainer::STRETCH_FIT);
156
SceneTree::get_singleton()->process(0);
157
158
CHECK_MESSAGE(
159
aspect_ratio_container->get_stretch_mode() == AspectRatioContainer::STRETCH_FIT,
160
"AspectRatioContainer stretch mode is set to STRETCH_FIT by default.");
161
162
CHECK_MESSAGE(
163
child_control->get_size().is_equal_approx(Size2(100, 100)),
164
"Child control is resized to maintain aspect ratio in STRETCH_FIT mode.");
165
166
aspect_ratio_container->set_stretch_mode(AspectRatioContainer::STRETCH_WIDTH_CONTROLS_HEIGHT);
167
SceneTree::get_singleton()->process(0);
168
169
CHECK_MESSAGE(
170
child_control->get_size().is_equal_approx(Size2(100, 100)),
171
"Child control height is adjusted to maintain aspect ratio in STRETCH_WIDTH_CONTROLS_HEIGHT mode.");
172
173
aspect_ratio_container->set_stretch_mode(AspectRatioContainer::STRETCH_HEIGHT_CONTROLS_WIDTH);
174
SceneTree::get_singleton()->process(0);
175
176
CHECK_MESSAGE(
177
child_control->get_size().is_equal_approx(Size2(200, 200)),
178
"Child control width is adjusted to maintain aspect ratio in STRETCH_HEIGHT_CONTROLS_WIDTH mode.");
179
180
aspect_ratio_container->set_stretch_mode(AspectRatioContainer::STRETCH_COVER);
181
SceneTree::get_singleton()->process(0);
182
183
CHECK_MESSAGE(
184
child_control->get_size().is_equal_approx(Size2(200, 200)),
185
"Child control is resized to cover the entire AspectRatioContainer in STRETCH_COVER mode.");
186
187
memdelete(child_control);
188
memdelete(aspect_ratio_container);
189
}
190
191
TEST_CASE("[SceneTree][AspectRatioContainer] Alignment modes") {
192
AspectRatioContainer *aspect_ratio_container = memnew(AspectRatioContainer);
193
Window *root = SceneTree::get_singleton()->get_root();
194
root->add_child(aspect_ratio_container);
195
Control *child_control = memnew(Control);
196
aspect_ratio_container->add_child(child_control);
197
198
SUBCASE("Horizontal Alignment") {
199
aspect_ratio_container->set_size(Size2(200, 100));
200
SceneTree::get_singleton()->process(0);
201
202
CHECK_MESSAGE(
203
aspect_ratio_container->get_alignment_horizontal() == AspectRatioContainer::ALIGNMENT_CENTER,
204
"AspectRatioContainer horizontal alignment is set to ALIGNMENT_CENTER by default.");
205
206
CHECK_MESSAGE(
207
child_control->get_position().is_equal_approx(Point2(50, 0)),
208
"Child control is aligned to the center in ALIGNMENT_CENTER horizontal alignment mode.");
209
210
aspect_ratio_container->set_alignment_horizontal(AspectRatioContainer::ALIGNMENT_BEGIN);
211
SceneTree::get_singleton()->process(0);
212
213
CHECK_MESSAGE(
214
child_control->get_position().is_equal_approx(Point2(0, 0)),
215
"Child control is aligned to the left in ALIGNMENT_BEGIN horizontal alignment mode.");
216
217
aspect_ratio_container->set_alignment_horizontal(AspectRatioContainer::ALIGNMENT_END);
218
SceneTree::get_singleton()->process(0);
219
220
CHECK_MESSAGE(
221
child_control->get_position().is_equal_approx(Point2(100, 0)),
222
"Child control is aligned to the right in ALIGNMENT_END horizontal alignment mode.");
223
}
224
225
SUBCASE("Vertical Alignment") {
226
aspect_ratio_container->set_size(Size2(100, 200));
227
SceneTree::get_singleton()->process(0);
228
229
CHECK_MESSAGE(
230
aspect_ratio_container->get_alignment_vertical() == AspectRatioContainer::ALIGNMENT_CENTER,
231
"AspectRatioContainer vertical alignment is set to ALIGNMENT_CENTER by default.");
232
233
CHECK_MESSAGE(
234
child_control->get_position().is_equal_approx(Point2(0, 50)),
235
"Child control is aligned to the center in ALIGNMENT_CENTER vertical alignment mode.");
236
237
aspect_ratio_container->set_alignment_vertical(AspectRatioContainer::ALIGNMENT_BEGIN);
238
SceneTree::get_singleton()->process(0);
239
240
CHECK_MESSAGE(
241
child_control->get_position().is_equal_approx(Point2(0, 0)),
242
"Child control is aligned to the top in ALIGNMENT_BEGIN vertical alignment mode.");
243
244
aspect_ratio_container->set_alignment_vertical(AspectRatioContainer::ALIGNMENT_END);
245
SceneTree::get_singleton()->process(0);
246
247
CHECK_MESSAGE(
248
child_control->get_position().is_equal_approx(Point2(0, 100)),
249
"Child control is aligned to the bottom in ALIGNMENT_END vertical alignment mode.");
250
}
251
252
memdelete(child_control);
253
memdelete(aspect_ratio_container);
254
}
255
256
TEST_CASE("[SceneTree][AspectRatioContainer] Container Sizing Flags") {
257
AspectRatioContainer *aspect_ratio_container = memnew(AspectRatioContainer);
258
Window *root = SceneTree::get_singleton()->get_root();
259
root->add_child(aspect_ratio_container);
260
Control *child_control = memnew(Control);
261
aspect_ratio_container->add_child(child_control);
262
263
SUBCASE("Horizontal Flags") {
264
aspect_ratio_container->set_size(Size2(200, 100));
265
SceneTree::get_singleton()->process(0);
266
267
CHECK_MESSAGE(
268
child_control->get_position().is_equal_approx(Point2(50, 0)),
269
"Child control is vertically centered by default with SIZE_FILL.");
270
271
child_control->set_h_size_flags(Control::SIZE_SHRINK_BEGIN);
272
SceneTree::get_singleton()->process(0);
273
274
CHECK_MESSAGE(
275
child_control->get_position().is_equal_approx(Point2(50, 0)),
276
"Child control is aligned to the left of its would-be area with SIZE_FILL in SIZE_SHRINK_BEGIN horizontal size flag mode.");
277
278
child_control->set_h_size_flags(Control::SIZE_SHRINK_CENTER);
279
SceneTree::get_singleton()->process(0);
280
281
CHECK_MESSAGE(
282
child_control->get_position().is_equal_approx(Point2(100, 0)),
283
"Child control is centered in its would-be area with SIZE_FILL in SIZE_SHRINK_CENTER horizontal size flag mode.");
284
285
child_control->set_h_size_flags(Control::SIZE_SHRINK_END);
286
SceneTree::get_singleton()->process(0);
287
288
CHECK_MESSAGE(
289
child_control->get_position().is_equal_approx(Point2(150, 0)),
290
"Child control is aligned to the right of its would-be area with SIZE_FILL in SIZE_SHRINK_END horizontal size flag mode.");
291
}
292
293
SUBCASE("Horizontal Flags RTL") {
294
aspect_ratio_container->set_size(Size2(200, 100));
295
aspect_ratio_container->set_layout_direction(Control::LAYOUT_DIRECTION_RTL);
296
SceneTree::get_singleton()->process(0);
297
298
CHECK_MESSAGE(
299
child_control->get_position().is_equal_approx(Point2(50, 0)),
300
"Child control is vertically centered by default with SIZE_FILL.");
301
302
child_control->set_h_size_flags(Control::SIZE_SHRINK_BEGIN);
303
SceneTree::get_singleton()->process(0);
304
305
CHECK_MESSAGE(
306
child_control->get_position().is_equal_approx(Point2(150, 0)),
307
"Child control is aligned to the right of its would-be area with SIZE_FILL in RTL SIZE_SHRINK_BEGIN horizontal size flag mode.");
308
309
child_control->set_h_size_flags(Control::SIZE_SHRINK_CENTER);
310
SceneTree::get_singleton()->process(0);
311
312
CHECK_MESSAGE(
313
child_control->get_position().is_equal_approx(Point2(100, 0)),
314
"Child control is centered in its would-be area with SIZE_FILL in SIZE_SHRINK_CENTER horizontal size flag mode.");
315
316
child_control->set_h_size_flags(Control::SIZE_SHRINK_END);
317
SceneTree::get_singleton()->process(0);
318
319
CHECK_MESSAGE(
320
child_control->get_position().is_equal_approx(Point2(50, 0)),
321
"Child control is aligned to the left of its would-be area with SIZE_FILL in RTL SIZE_SHRINK_END horizontal size flag mode.");
322
}
323
324
SUBCASE("Vertical Flags") {
325
aspect_ratio_container->set_size(Size2(100, 200));
326
SceneTree::get_singleton()->process(0);
327
328
CHECK_MESSAGE(
329
child_control->get_position().is_equal_approx(Point2(0, 50)),
330
"Child control is horizontally centered by default with SIZE_FILL.");
331
332
child_control->set_v_size_flags(Control::SIZE_SHRINK_BEGIN);
333
SceneTree::get_singleton()->process(0);
334
335
CHECK_MESSAGE(
336
child_control->get_position().is_equal_approx(Point2(0, 50)),
337
"Child control is aligned to the top of its would-be area with SIZE_FILL in SIZE_SHRINK_BEGIN vertical size flag mode.");
338
339
child_control->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
340
SceneTree::get_singleton()->process(0);
341
342
CHECK_MESSAGE(
343
child_control->get_position().is_equal_approx(Point2(0, 100)),
344
"Child control is centered in its would-be area with SIZE_FILL in SIZE_SHRINK_CENTER vertical size flag mode.");
345
346
child_control->set_v_size_flags(Control::SIZE_SHRINK_END);
347
SceneTree::get_singleton()->process(0);
348
349
CHECK_MESSAGE(
350
child_control->get_position().is_equal_approx(Point2(0, 150)),
351
"Child control is aligned to the bottom of its would-be area with SIZE_FILL in SIZE_SHRINK_END vertical size flag mode.");
352
}
353
354
memdelete(child_control);
355
memdelete(aspect_ratio_container);
356
}
357
358
} // namespace TestAspectRatioContainer
359
360