Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/scene/test_margin_container.cpp
59209 views
1
/**************************************************************************/
2
/* test_margin_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_margin_container)
34
35
#include "scene/gui/control.h"
36
#include "scene/gui/margin_container.h"
37
#include "scene/main/scene_tree.h"
38
#include "scene/main/window.h"
39
40
namespace TestMarginContainer {
41
42
TEST_CASE("[SceneTree][MarginContainer] Default margins and layout") {
43
MarginContainer *margin_container = memnew(MarginContainer);
44
Window *root = SceneTree::get_singleton()->get_root();
45
root->add_child(margin_container);
46
47
Control *child_control = memnew(Control);
48
child_control->set_custom_minimum_size(Size2(20, 10));
49
margin_container->add_child(child_control);
50
51
margin_container->set_size(Size2(100, 80));
52
SceneTree::get_singleton()->process(0);
53
54
CHECK_MESSAGE(
55
margin_container->get_margin_size(SIDE_LEFT) == 0,
56
"Left margin is 0 by default.");
57
58
CHECK_MESSAGE(
59
margin_container->get_margin_size(SIDE_TOP) == 0,
60
"Top margin is 0 by default.");
61
62
CHECK_MESSAGE(
63
margin_container->get_margin_size(SIDE_RIGHT) == 0,
64
"Right margin is 0 by default.");
65
66
CHECK_MESSAGE(
67
margin_container->get_margin_size(SIDE_BOTTOM) == 0,
68
"Bottom margin is 0 by default.");
69
70
CHECK_MESSAGE(
71
child_control->get_position().is_equal_approx(Point2(0, 0)),
72
"Child control starts at the top-left corner when all margins are 0.");
73
74
CHECK_MESSAGE(
75
child_control->get_size().is_equal_approx(Size2(100, 80)),
76
"Child control fills the container area when all margins are 0.");
77
78
CHECK_MESSAGE(
79
margin_container->get_minimum_size().is_equal_approx(Size2(20, 10)),
80
"Minimum size equals the child minimum size when all margins are 0.");
81
82
memdelete(child_control);
83
memdelete(margin_container);
84
}
85
86
TEST_CASE("[SceneTree][MarginContainer] Custom margins affect layout and minimum size") {
87
MarginContainer *margin_container = memnew(MarginContainer);
88
Window *root = SceneTree::get_singleton()->get_root();
89
root->add_child(margin_container);
90
91
margin_container->add_theme_constant_override("margin_left", 10);
92
margin_container->add_theme_constant_override("margin_top", 20);
93
margin_container->add_theme_constant_override("margin_right", 30);
94
margin_container->add_theme_constant_override("margin_bottom", 40);
95
96
Control *child_control = memnew(Control);
97
child_control->set_custom_minimum_size(Size2(20, 10));
98
margin_container->add_child(child_control);
99
100
margin_container->set_size(Size2(200, 150));
101
SceneTree::get_singleton()->process(0);
102
103
CHECK_MESSAGE(
104
child_control->get_position().is_equal_approx(Point2(10, 20)),
105
"Child control is offset by left and top margins.");
106
107
CHECK_MESSAGE(
108
child_control->get_size().is_equal_approx(Size2(160, 90)),
109
"Child control size is reduced by left/right and top/bottom margins.");
110
111
CHECK_MESSAGE(
112
margin_container->get_minimum_size().is_equal_approx(Size2(60, 70)),
113
"Minimum size equals child minimum size plus all configured margins.");
114
115
memdelete(child_control);
116
memdelete(margin_container);
117
}
118
119
TEST_CASE("[SceneTree][MarginContainer] Multiple children use maximum minimum size") {
120
MarginContainer *margin_container = memnew(MarginContainer);
121
Window *root = SceneTree::get_singleton()->get_root();
122
root->add_child(margin_container);
123
124
margin_container->add_theme_constant_override("margin_left", 1);
125
margin_container->add_theme_constant_override("margin_top", 2);
126
margin_container->add_theme_constant_override("margin_right", 3);
127
margin_container->add_theme_constant_override("margin_bottom", 4);
128
129
Control *child_control_1 = memnew(Control);
130
Control *child_control_2 = memnew(Control);
131
child_control_1->set_custom_minimum_size(Size2(40, 10));
132
child_control_2->set_custom_minimum_size(Size2(20, 50));
133
margin_container->add_child(child_control_1);
134
margin_container->add_child(child_control_2);
135
SceneTree::get_singleton()->process(0);
136
137
CHECK_MESSAGE(
138
margin_container->get_minimum_size().is_equal_approx(Size2(44, 56)),
139
"Minimum size uses maximum child minimum width/height plus margins.");
140
141
memdelete(child_control_2);
142
memdelete(child_control_1);
143
memdelete(margin_container);
144
}
145
146
} // namespace TestMarginContainer
147
148