Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/scene/test_label.cpp
59209 views
1
/**************************************************************************/
2
/* test_label.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_label)
34
35
#include "modules/modules_enabled.gen.h" // IWYU pragma: keep. Needed for MODULE_TEXT_SERVER_FB_ENABLED and MODULE_TEXT_SERVER_ADV_ENABLED definitions.
36
37
#if defined(MODULE_TEXT_SERVER_FB_ENABLED) || defined(MODULE_TEXT_SERVER_ADV_ENABLED)
38
39
#include "scene/gui/label.h"
40
#include "scene/main/scene_tree.h"
41
#include "scene/main/window.h"
42
43
namespace TestLabel {
44
45
TEST_CASE("[SceneTree][Label] Custom minimum size") {
46
// This is an anti-regression test case introduced in GH-116640. When the old minimum size behavior is removed, this test case should be removed too.
47
Label *test_label = memnew(Label);
48
Window *root = SceneTree::get_singleton()->get_root();
49
root->add_child(test_label);
50
51
real_t min_width = 50;
52
53
test_label->set_custom_minimum_size(Size2(min_width, 0));
54
test_label->set_text("This is a long text that should be wrapped and exceeds minimum size.");
55
SceneTree::get_singleton()->process(0);
56
57
CHECK_MESSAGE(
58
test_label->get_size().width > min_width,
59
"Label width will increase to fit the text with AUTOWRAP_OFF.");
60
61
test_label->set_autowrap_mode(TextServer::AUTOWRAP_ARBITRARY);
62
SceneTree::get_singleton()->process(0);
63
64
CHECK_MESSAGE(
65
Math::is_equal_approx(test_label->get_size().width, min_width),
66
"Label width will be equal to custom minimum width with AUTOWRAP_ARBITRARY.");
67
68
test_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD);
69
SceneTree::get_singleton()->process(0);
70
71
CHECK_MESSAGE(
72
Math::is_equal_approx(test_label->get_size().width, min_width),
73
"Label width will be equal to custom minimum width with AUTOWRAP_WORD.");
74
75
test_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);
76
SceneTree::get_singleton()->process(0);
77
78
CHECK_MESSAGE(
79
Math::is_equal_approx(test_label->get_size().width, min_width),
80
"Label width will be equal to custom minimum width with AUTOWRAP_WORD_SMART.");
81
82
test_label->set_autowrap_mode(TextServer::AUTOWRAP_OFF);
83
test_label->set_clip_text(true);
84
SceneTree::get_singleton()->process(0);
85
86
CHECK_MESSAGE(
87
Math::is_equal_approx(test_label->get_size().width, min_width),
88
"Label width will be equal to custom minimum width with clip_text.");
89
90
test_label->set_clip_text(false);
91
test_label->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_CHAR);
92
SceneTree::get_singleton()->process(0);
93
94
CHECK_MESSAGE(
95
Math::is_equal_approx(test_label->get_size().width, min_width),
96
"Label width will be equal to custom minimum width with OVERRUN_TRIM_CHAR.");
97
98
test_label->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_WORD);
99
SceneTree::get_singleton()->process(0);
100
101
CHECK_MESSAGE(
102
Math::is_equal_approx(test_label->get_size().width, min_width),
103
"Label width will be equal to custom minimum width with OVERRUN_TRIM_WORD.");
104
105
test_label->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
106
SceneTree::get_singleton()->process(0);
107
108
CHECK_MESSAGE(
109
Math::is_equal_approx(test_label->get_size().width, min_width),
110
"Label width will be equal to custom minimum width with OVERRUN_TRIM_ELLIPSIS.");
111
112
test_label->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_WORD_ELLIPSIS);
113
SceneTree::get_singleton()->process(0);
114
115
CHECK_MESSAGE(
116
Math::is_equal_approx(test_label->get_size().width, min_width),
117
"Label width will be equal to custom minimum width with OVERRUN_TRIM_WORD_ELLIPSIS.");
118
119
test_label->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS_FORCE);
120
SceneTree::get_singleton()->process(0);
121
122
CHECK_MESSAGE(
123
Math::is_equal_approx(test_label->get_size().width, min_width),
124
"Label width will be equal to custom minimum width with OVERRUN_TRIM_ELLIPSIS_FORCE.");
125
126
test_label->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_WORD_ELLIPSIS_FORCE);
127
SceneTree::get_singleton()->process(0);
128
129
CHECK_MESSAGE(
130
Math::is_equal_approx(test_label->get_size().width, min_width),
131
"Label width will be equal to custom minimum width with OVERRUN_TRIM_WORD_ELLIPSIS_FORCE.");
132
133
memdelete(test_label);
134
}
135
136
TEST_CASE("[SceneTree][Label] Sizing") {
137
Label *test_label = memnew(Label);
138
Window *root = SceneTree::get_singleton()->get_root();
139
root->add_child(test_label);
140
141
real_t max_width = 50;
142
real_t min_width = 25;
143
144
test_label->set_custom_maximum_size(Size2(max_width, -1));
145
test_label->set_custom_minimum_size(Size2(min_width, 0));
146
test_label->set_text("This is a long text that should be wrapped and exceeds minimum size.");
147
SceneTree::get_singleton()->process(0);
148
149
CHECK_MESSAGE(
150
test_label->get_size().width <= max_width,
151
"Label width will increase up to the custom maximum width with AUTOWRAP_OFF.");
152
CHECK_MESSAGE(
153
test_label->get_size().width > min_width,
154
"Label width will increase beyond the custom minimum width with AUTOWRAP_OFF.");
155
156
test_label->set_autowrap_mode(TextServer::AUTOWRAP_ARBITRARY);
157
SceneTree::get_singleton()->process(0);
158
159
CHECK_MESSAGE(
160
test_label->get_size().width <= max_width,
161
"Label width will increase up to the custom maximum width with AUTOWRAP_ARBITRARY.");
162
CHECK_MESSAGE(
163
test_label->get_size().width > min_width,
164
"Label width will increase beyond the custom minimum width with AUTOWRAP_ARBITRARY.");
165
166
test_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD);
167
SceneTree::get_singleton()->process(0);
168
169
CHECK_MESSAGE(
170
test_label->get_size().width <= max_width,
171
"Label width will increase up to the custom maximum width with AUTOWRAP_WORD.");
172
CHECK_MESSAGE(
173
test_label->get_size().width > min_width,
174
"Label width will increase beyond the custom minimum width with AUTOWRAP_WORD.");
175
176
test_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);
177
SceneTree::get_singleton()->process(0);
178
179
CHECK_MESSAGE(
180
test_label->get_size().width <= max_width,
181
"Label width will increase up to the custom maximum width with AUTOWRAP_WORD_SMART.");
182
CHECK_MESSAGE(
183
test_label->get_size().width > min_width,
184
"Label width will increase beyond the custom minimum width with AUTOWRAP_WORD_SMART.");
185
186
test_label->set_autowrap_mode(TextServer::AUTOWRAP_OFF);
187
test_label->set_clip_text(true);
188
SceneTree::get_singleton()->process(0);
189
190
CHECK_MESSAGE(
191
test_label->get_size().width <= max_width,
192
"Label width will increase up to the custom maximum width with clip_text.");
193
CHECK_MESSAGE(
194
test_label->get_size().width > min_width,
195
"Label width will increase beyond the custom minimum width with clip_text.");
196
197
test_label->set_clip_text(false);
198
test_label->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_CHAR);
199
SceneTree::get_singleton()->process(0);
200
201
CHECK_MESSAGE(
202
test_label->get_size().width <= max_width,
203
"Label width will increase up to the custom maximum width with OVERRUN_TRIM_CHAR.");
204
CHECK_MESSAGE(
205
test_label->get_size().width > min_width,
206
"Label width will increase beyond the custom minimum width with OVERRUN_TRIM_CHAR.");
207
208
test_label->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_WORD);
209
SceneTree::get_singleton()->process(0);
210
211
CHECK_MESSAGE(
212
test_label->get_size().width <= max_width,
213
"Label width will increase up to the custom maximum width with OVERRUN_TRIM_WORD.");
214
CHECK_MESSAGE(
215
test_label->get_size().width > min_width,
216
"Label width will increase beyond the custom minimum width with OVERRUN_TRIM_WORD.");
217
218
test_label->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
219
SceneTree::get_singleton()->process(0);
220
221
CHECK_MESSAGE(
222
test_label->get_size().width <= max_width,
223
"Label width will increase up to the custom maximum width with OVERRUN_TRIM_ELLIPSIS.");
224
CHECK_MESSAGE(
225
test_label->get_size().width > min_width,
226
"Label width will increase beyond the custom minimum width with OVERRUN_TRIM_ELLIPSIS.");
227
228
test_label->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_WORD_ELLIPSIS);
229
SceneTree::get_singleton()->process(0);
230
231
CHECK_MESSAGE(
232
test_label->get_size().width <= max_width,
233
"Label width will increase up to the custom maximum width with OVERRUN_TRIM_WORD_ELLIPSIS.");
234
CHECK_MESSAGE(
235
test_label->get_size().width > min_width,
236
"Label width will increase beyond the custom minimum width with OVERRUN_TRIM_WORD_ELLIPSIS.");
237
238
test_label->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS_FORCE);
239
SceneTree::get_singleton()->process(0);
240
241
CHECK_MESSAGE(
242
test_label->get_size().width <= max_width,
243
"Label width will increase up to the custom maximum width with OVERRUN_TRIM_ELLIPSIS_FORCE.");
244
CHECK_MESSAGE(
245
test_label->get_size().width > min_width,
246
"Label width will increase beyond the custom minimum width with OVERRUN_TRIM_ELLIPSIS_FORCE.");
247
248
test_label->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_WORD_ELLIPSIS_FORCE);
249
SceneTree::get_singleton()->process(0);
250
251
CHECK_MESSAGE(
252
test_label->get_size().width <= max_width,
253
"Label width will increase up to the custom maximum width with OVERRUN_TRIM_WORD_ELLIPSIS_FORCE.");
254
CHECK_MESSAGE(
255
test_label->get_size().width > min_width,
256
"Label width will increase beyond the custom minimum width with OVERRUN_TRIM_WORD_ELLIPSIS_FORCE.");
257
258
memdelete(test_label);
259
}
260
261
} // namespace TestLabel
262
263
#endif // MODULE_TEXT_SERVER_FB_ENABLED || MODULE_TEXT_SERVER_ADV_ENABLED
264
265