Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/scene/test_center_container.cpp
59209 views
1
/**************************************************************************/
2
/* test_center_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_center_container)
34
35
#include "scene/gui/center_container.h"
36
#include "scene/gui/control.h"
37
#include "scene/main/scene_tree.h"
38
#include "scene/main/window.h"
39
40
namespace TestCenterContainer {
41
42
TEST_CASE("[SceneTree][CenterContainer] Standard behavior") {
43
CenterContainer *center_container = memnew(CenterContainer);
44
Window *root = SceneTree::get_singleton()->get_root();
45
root->add_child(center_container);
46
Control *child_control = memnew(Control);
47
center_container->add_child(child_control);
48
49
center_container->set_size(Size2(100, 100));
50
SceneTree::get_singleton()->process(0);
51
52
CHECK_MESSAGE(
53
child_control->get_position().is_equal_approx(Point2(50, 50)),
54
"Child control is centered within the CenterContainer by default.");
55
56
child_control->set_custom_minimum_size(Size2(20, 20));
57
SceneTree::get_singleton()->process(0);
58
59
CHECK_MESSAGE(
60
child_control->get_position().is_equal_approx(Point2(40, 40)),
61
"Child control remains centered when custom minimum size is set.");
62
63
child_control->set_custom_minimum_size(Size2(200, 200));
64
SceneTree::get_singleton()->process(0);
65
66
CHECK_MESSAGE(
67
center_container->get_size().is_equal_approx(Size2(200, 200)),
68
"CenterContainer expands to accommodate the minimum size of its child control.");
69
70
CHECK_MESSAGE(
71
child_control->get_position().is_equal_approx(Point2(0, 0)),
72
"Child control is positioned at the top-left corner when its minimum size equals the container size.");
73
74
child_control->set_custom_minimum_size(Size2());
75
SceneTree::get_singleton()->process(0);
76
77
CHECK_MESSAGE(
78
child_control->get_position().is_equal_approx(Point2(50, 50)),
79
"Child control re-centers when custom minimum size is reset.");
80
81
CHECK_MESSAGE(
82
center_container->get_size().is_equal_approx(Size2(100, 100)),
83
"CenterContainer returns to original size when child control's custom minimum size is reset.");
84
85
center_container->set_use_top_left(true);
86
SceneTree::get_singleton()->process(0);
87
88
CHECK_MESSAGE(
89
child_control->get_position().is_equal_approx(Point2(0, 0)),
90
"Child control is aligned to the top-left corner when use_top_left is enabled.");
91
92
center_container->set_layout_direction(Control::LAYOUT_DIRECTION_RTL);
93
SceneTree::get_singleton()->process(0);
94
95
CHECK_MESSAGE(
96
child_control->get_position().is_equal_approx(Point2(100, 0)),
97
"Child control is aligned to the top-right corner in RTL layout direction when use_top_left is enabled.");
98
99
memdelete(child_control);
100
memdelete(center_container);
101
}
102
103
TEST_CASE("[SceneTree][CenterContainer] Multiple children") {
104
CenterContainer *center_container = memnew(CenterContainer);
105
Window *root = SceneTree::get_singleton()->get_root();
106
root->add_child(center_container);
107
108
center_container->set_size(Size2(100, 100));
109
110
Control *child_control_1 = memnew(Control);
111
Control *child_control_2 = memnew(Control);
112
center_container->add_child(child_control_1);
113
center_container->add_child(child_control_2);
114
SceneTree::get_singleton()->process(0);
115
116
CHECK_MESSAGE(
117
child_control_1->get_position().is_equal_approx(Point2(50, 50)),
118
"First child control is centered within the CenterContainer.");
119
120
CHECK_MESSAGE(
121
child_control_2->get_position().is_equal_approx(Point2(50, 50)),
122
"Second child control is also centered and overlaps the first child control by default.");
123
124
child_control_1->set_custom_minimum_size(Size2(20, 20));
125
SceneTree::get_singleton()->process(0);
126
127
CHECK_MESSAGE(
128
child_control_1->get_position().is_equal_approx(Point2(40, 40)),
129
"First child control remains centered when custom minimum size is set.");
130
131
CHECK_MESSAGE(
132
child_control_2->get_position().is_equal_approx(Point2(50, 50)),
133
"Second child control remains centered and overlaps the first child control even when the first child has a custom minimum size.");
134
135
child_control_1->set_custom_minimum_size(Size2(200, 0));
136
SceneTree::get_singleton()->process(0);
137
138
CHECK_MESSAGE(
139
center_container->get_size().is_equal_approx(Size2(200, 100)),
140
"CenterContainer expands to accommodate the minimum size of the first child control.");
141
142
CHECK_MESSAGE(
143
child_control_1->get_position().is_equal_approx(Point2(0, 50)),
144
"First child control is aligned to the left edge of the CenterContainer when its minimum width equals the container width.");
145
146
CHECK_MESSAGE(
147
child_control_2->get_position().is_equal_approx(Point2(100, 50)),
148
"Second child control is centered on the new container center.");
149
150
child_control_2->set_custom_minimum_size(Size2(0, 200));
151
SceneTree::get_singleton()->process(0);
152
153
CHECK_MESSAGE(
154
center_container->get_size().is_equal_approx(Size2(200, 200)),
155
"CenterContainer expands to accommodate the minimum size of the second child control.");
156
157
CHECK_MESSAGE(
158
child_control_1->get_position().is_equal_approx(Point2(0, 100)),
159
"First child control is aligned to the left edge and centered vertically when its minimum width equals the container width but its minimum height is smaller than the container height.");
160
161
CHECK_MESSAGE(
162
child_control_2->get_position().is_equal_approx(Point2(100, 0)),
163
"Second child control is aligned to the top edge and centered horizontally when its minimum height equals the container height but its minimum width is smaller than the container width.");
164
165
memdelete(child_control_2);
166
memdelete(child_control_1);
167
memdelete(center_container);
168
}
169
170
} // namespace TestCenterContainer
171
172