Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/scene/test_foldable_container.cpp
59209 views
1
/**************************************************************************/
2
/* test_foldable_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_foldable_container)
34
35
#ifndef ADVANCED_GUI_DISABLED
36
37
#include "scene/gui/control.h"
38
#include "scene/gui/foldable_container.h"
39
#include "scene/main/scene_tree.h"
40
#include "scene/main/window.h"
41
42
namespace TestFoldableContainer {
43
44
TEST_CASE("[SceneTree][FoldableContainer] Default properties") {
45
FoldableContainer *foldable_container = memnew(FoldableContainer);
46
Window *root = SceneTree::get_singleton()->get_root();
47
root->add_child(foldable_container);
48
SceneTree::get_singleton()->process(0);
49
50
CHECK_MESSAGE(
51
!foldable_container->is_folded(),
52
"FoldableContainer is expanded by default.");
53
54
CHECK_MESSAGE(
55
foldable_container->get_title().is_empty(),
56
"FoldableContainer title is empty by default.");
57
58
CHECK_MESSAGE(
59
foldable_container->get_title_alignment() == HORIZONTAL_ALIGNMENT_LEFT,
60
"FoldableContainer title alignment is set to left by default.");
61
62
CHECK_MESSAGE(
63
foldable_container->get_title_position() == FoldableContainer::POSITION_TOP,
64
"FoldableContainer title position is set to top by default.");
65
66
CHECK_MESSAGE(
67
foldable_container->get_title_text_direction() == Control::TEXT_DIRECTION_AUTO,
68
"FoldableContainer title text direction is set to auto by default.");
69
70
CHECK_MESSAGE(
71
foldable_container->get_title_text_overrun_behavior() == TextServer::OVERRUN_NO_TRIMMING,
72
"FoldableContainer title overrun behavior is set to no trimming by default.");
73
74
CHECK_MESSAGE(
75
foldable_container->get_language().is_empty(),
76
"FoldableContainer language is empty by default.");
77
78
CHECK_MESSAGE(
79
foldable_container->get_focus_mode() == Control::FOCUS_ALL,
80
"FoldableContainer focus mode is set to FOCUS_ALL by default.");
81
82
CHECK_MESSAGE(
83
foldable_container->get_mouse_filter() == Control::MOUSE_FILTER_STOP,
84
"FoldableContainer mouse filter is set to MOUSE_FILTER_STOP by default.");
85
86
CHECK_MESSAGE(
87
foldable_container->get_foldable_group().is_null(),
88
"FoldableContainer has no foldable group by default.");
89
90
memdelete(foldable_container);
91
}
92
93
TEST_CASE("[SceneTree][FoldableContainer] Fold and expand behavior") {
94
FoldableContainer *foldable_container = memnew(FoldableContainer("Section"));
95
Window *root = SceneTree::get_singleton()->get_root();
96
root->add_child(foldable_container);
97
98
Control *child_control = memnew(Control);
99
child_control->set_custom_minimum_size(Size2(120, 80));
100
foldable_container->add_child(child_control);
101
foldable_container->set_size(Size2(200, 200));
102
SceneTree::get_singleton()->process(0);
103
104
Size2 expanded_minimum_size = foldable_container->get_minimum_size();
105
106
CHECK_MESSAGE(
107
child_control->is_visible(),
108
"Child control is visible when FoldableContainer is expanded.");
109
110
foldable_container->fold();
111
SceneTree::get_singleton()->process(0);
112
113
CHECK_MESSAGE(
114
foldable_container->is_folded(),
115
"FoldableContainer is folded after calling fold().");
116
117
CHECK_MESSAGE(
118
!child_control->is_visible(),
119
"Child control is hidden when FoldableContainer is folded.");
120
121
CHECK_MESSAGE(
122
foldable_container->get_minimum_size().y < expanded_minimum_size.y,
123
"Folded minimum size is smaller than expanded minimum size when content is present.");
124
125
foldable_container->expand();
126
SceneTree::get_singleton()->process(0);
127
128
CHECK_MESSAGE(
129
!foldable_container->is_folded(),
130
"FoldableContainer is expanded after calling expand().");
131
132
CHECK_MESSAGE(
133
child_control->is_visible(),
134
"Child control is visible again after FoldableContainer is expanded.");
135
136
foldable_container->set_folded(true);
137
SceneTree::get_singleton()->process(0);
138
139
CHECK_MESSAGE(
140
foldable_container->is_folded(),
141
"set_folded(true) folds the FoldableContainer.");
142
143
foldable_container->set_folded(false);
144
SceneTree::get_singleton()->process(0);
145
146
CHECK_MESSAGE(
147
!foldable_container->is_folded(),
148
"set_folded(false) expands the FoldableContainer.");
149
150
memdelete(child_control);
151
memdelete(foldable_container);
152
}
153
154
TEST_CASE("[SceneTree][FoldableContainer] Title position and title bar controls") {
155
FoldableContainer *foldable_container = memnew(FoldableContainer("Section"));
156
Window *root = SceneTree::get_singleton()->get_root();
157
root->add_child(foldable_container);
158
159
Control *content_control = memnew(Control);
160
content_control->set_custom_minimum_size(Size2(40, 40));
161
foldable_container->add_child(content_control);
162
163
Control *title_control = memnew(Control);
164
title_control->set_custom_minimum_size(Size2(30, 20));
165
foldable_container->add_title_bar_control(title_control);
166
167
foldable_container->set_size(Size2(200, 140));
168
SceneTree::get_singleton()->process(0);
169
170
CHECK_MESSAGE(
171
title_control->get_parent() == foldable_container,
172
"Title bar control is reparented to the FoldableContainer when added.");
173
174
real_t top_title_control_y = title_control->get_position().y;
175
real_t top_content_y = content_control->get_position().y;
176
177
foldable_container->set_title_position(FoldableContainer::POSITION_BOTTOM);
178
SceneTree::get_singleton()->process(0);
179
180
real_t bottom_title_control_y = title_control->get_position().y;
181
real_t bottom_content_y = content_control->get_position().y;
182
183
CHECK_MESSAGE(
184
bottom_title_control_y > top_title_control_y,
185
"Title bar controls move to the bottom when title position is set to POSITION_BOTTOM.");
186
187
CHECK_MESSAGE(
188
bottom_content_y < top_content_y,
189
"Content area shifts upward when title position is changed from top to bottom.");
190
191
foldable_container->remove_title_bar_control(title_control);
192
193
CHECK_MESSAGE(
194
title_control->get_parent() == nullptr,
195
"Title bar control is detached from FoldableContainer when removed.");
196
197
memdelete(title_control);
198
memdelete(content_control);
199
memdelete(foldable_container);
200
}
201
202
TEST_CASE("[SceneTree][FoldableContainer] FoldableGroup behavior") {
203
Ref<FoldableGroup> foldable_group;
204
foldable_group.instantiate();
205
206
FoldableContainer *container_a = memnew(FoldableContainer("A"));
207
FoldableContainer *container_b = memnew(FoldableContainer("B"));
208
Window *root = SceneTree::get_singleton()->get_root();
209
root->add_child(container_a);
210
root->add_child(container_b);
211
212
container_a->set_folded(true);
213
container_b->set_folded(true);
214
container_a->set_foldable_group(foldable_group);
215
container_b->set_foldable_group(foldable_group);
216
SceneTree::get_singleton()->process(0);
217
218
CHECK_MESSAGE(
219
!container_a->is_folded(),
220
"First grouped FoldableContainer auto-expands when allow_folding_all is false and no container is expanded.");
221
222
CHECK_MESSAGE(
223
container_b->is_folded(),
224
"Second grouped FoldableContainer remains folded when another container in the group is expanded.");
225
226
CHECK_MESSAGE(
227
foldable_group->get_expanded_container() == container_a,
228
"FoldableGroup tracks the currently expanded container.");
229
230
container_b->set_folded(false);
231
SceneTree::get_singleton()->process(0);
232
233
CHECK_MESSAGE(
234
container_a->is_folded(),
235
"Expanding one grouped FoldableContainer folds the previously expanded container.");
236
237
CHECK_MESSAGE(
238
!container_b->is_folded(),
239
"Expanded grouped FoldableContainer remains unfolded.");
240
241
CHECK_MESSAGE(
242
foldable_group->get_expanded_container() == container_b,
243
"FoldableGroup updates expanded container after a different container is expanded.");
244
245
container_b->set_folded(true);
246
SceneTree::get_singleton()->process(0);
247
248
CHECK_MESSAGE(
249
!container_b->is_folded(),
250
"Expanded grouped FoldableContainer cannot be folded when allow_folding_all is false.");
251
252
foldable_group->set_allow_folding_all(true);
253
container_b->set_folded(true);
254
SceneTree::get_singleton()->process(0);
255
256
CHECK_MESSAGE(
257
container_b->is_folded(),
258
"Grouped FoldableContainer can be folded when allow_folding_all is true.");
259
260
CHECK_MESSAGE(
261
foldable_group->get_expanded_container() == nullptr,
262
"FoldableGroup has no expanded container when all grouped containers are folded.");
263
264
memdelete(container_b);
265
memdelete(container_a);
266
}
267
268
} // namespace TestFoldableContainer
269
270
#endif
271
272