Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/scene/test_nine_patch_rect.cpp
59209 views
1
/**************************************************************************/
2
/* test_nine_patch_rect.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_nine_patch_rect)
34
35
#include "scene/gui/nine_patch_rect.h"
36
#include "scene/main/scene_tree.h"
37
#include "scene/main/window.h"
38
#include "scene/resources/texture.h"
39
#include "tests/signal_watcher.h"
40
41
namespace TestNinePatchRect {
42
43
TEST_CASE("[SceneTree][NinePatchRect] Default properties") {
44
NinePatchRect *nine_patch = memnew(NinePatchRect);
45
46
CHECK(nine_patch->get_texture().is_null());
47
48
CHECK(nine_patch->get_patch_margin(SIDE_LEFT) == 0);
49
CHECK(nine_patch->get_patch_margin(SIDE_TOP) == 0);
50
CHECK(nine_patch->get_patch_margin(SIDE_RIGHT) == 0);
51
CHECK(nine_patch->get_patch_margin(SIDE_BOTTOM) == 0);
52
53
CHECK(nine_patch->get_region_rect() == Rect2(0, 0, 0, 0));
54
CHECK(nine_patch->is_draw_center_enabled());
55
56
CHECK(nine_patch->get_h_axis_stretch_mode() == NinePatchRect::AXIS_STRETCH_MODE_STRETCH);
57
CHECK(nine_patch->get_v_axis_stretch_mode() == NinePatchRect::AXIS_STRETCH_MODE_STRETCH);
58
59
// Set in the NinePatchRect constructor, unlike the Control default.
60
CHECK(nine_patch->get_mouse_filter() == Control::MOUSE_FILTER_IGNORE);
61
62
CHECK(nine_patch->get_combined_minimum_size() == Size2(0, 0));
63
64
memdelete(nine_patch);
65
}
66
67
TEST_CASE("[SceneTree][NinePatchRect] Set/get texture") {
68
NinePatchRect *nine_patch = memnew(NinePatchRect);
69
70
Ref<Texture2D> texture;
71
texture.instantiate();
72
73
nine_patch->set_texture(texture);
74
CHECK(nine_patch->get_texture() == texture);
75
76
nine_patch->set_texture(Ref<Texture2D>());
77
CHECK(nine_patch->get_texture().is_null());
78
79
memdelete(nine_patch);
80
}
81
82
TEST_CASE("[SceneTree][NinePatchRect] texture_changed signal") {
83
NinePatchRect *nine_patch = memnew(NinePatchRect);
84
85
Ref<Texture2D> texture_a;
86
texture_a.instantiate();
87
Ref<Texture2D> texture_b;
88
texture_b.instantiate();
89
90
SIGNAL_WATCH(nine_patch, "texture_changed");
91
Array empty_signal_args = { {} };
92
93
SUBCASE("Setting a new texture emits the signal") {
94
nine_patch->set_texture(texture_a);
95
SIGNAL_CHECK("texture_changed", empty_signal_args);
96
}
97
98
SUBCASE("Setting the same texture does not re-emit") {
99
nine_patch->set_texture(texture_a);
100
SIGNAL_DISCARD("texture_changed");
101
102
nine_patch->set_texture(texture_a);
103
SIGNAL_CHECK_FALSE("texture_changed");
104
}
105
106
SUBCASE("Replacing one texture with another emits the signal") {
107
nine_patch->set_texture(texture_a);
108
SIGNAL_DISCARD("texture_changed");
109
110
nine_patch->set_texture(texture_b);
111
SIGNAL_CHECK("texture_changed", empty_signal_args);
112
}
113
114
SUBCASE("Clearing the texture emits the signal") {
115
nine_patch->set_texture(texture_a);
116
SIGNAL_DISCARD("texture_changed");
117
118
nine_patch->set_texture(Ref<Texture2D>());
119
SIGNAL_CHECK("texture_changed", empty_signal_args);
120
}
121
122
SIGNAL_UNWATCH(nine_patch, "texture_changed");
123
memdelete(nine_patch);
124
}
125
126
TEST_CASE("[SceneTree][NinePatchRect] Patch margins") {
127
NinePatchRect *nine_patch = memnew(NinePatchRect);
128
129
SUBCASE("Each side can be set and read independently") {
130
nine_patch->set_patch_margin(SIDE_LEFT, 5);
131
nine_patch->set_patch_margin(SIDE_TOP, 10);
132
nine_patch->set_patch_margin(SIDE_RIGHT, 15);
133
nine_patch->set_patch_margin(SIDE_BOTTOM, 20);
134
135
CHECK(nine_patch->get_patch_margin(SIDE_LEFT) == 5);
136
CHECK(nine_patch->get_patch_margin(SIDE_TOP) == 10);
137
CHECK(nine_patch->get_patch_margin(SIDE_RIGHT) == 15);
138
CHECK(nine_patch->get_patch_margin(SIDE_BOTTOM) == 20);
139
}
140
141
SUBCASE("Updating one side does not affect the others") {
142
nine_patch->set_patch_margin(SIDE_LEFT, 7);
143
nine_patch->set_patch_margin(SIDE_RIGHT, 9);
144
145
nine_patch->set_patch_margin(SIDE_LEFT, 42);
146
147
CHECK(nine_patch->get_patch_margin(SIDE_LEFT) == 42);
148
CHECK(nine_patch->get_patch_margin(SIDE_TOP) == 0);
149
CHECK(nine_patch->get_patch_margin(SIDE_RIGHT) == 9);
150
CHECK(nine_patch->get_patch_margin(SIDE_BOTTOM) == 0);
151
}
152
153
memdelete(nine_patch);
154
}
155
156
TEST_CASE("[SceneTree][NinePatchRect] Patch margins drive minimum size") {
157
NinePatchRect *nine_patch = memnew(NinePatchRect);
158
Window *root = SceneTree::get_singleton()->get_root();
159
root->add_child(nine_patch);
160
161
nine_patch->set_patch_margin(SIDE_LEFT, 10);
162
nine_patch->set_patch_margin(SIDE_TOP, 20);
163
nine_patch->set_patch_margin(SIDE_RIGHT, 30);
164
nine_patch->set_patch_margin(SIDE_BOTTOM, 40);
165
SceneTree::get_singleton()->process(0);
166
167
// Minimum width is left + right; minimum height is top + bottom.
168
CHECK(nine_patch->get_combined_minimum_size() == Size2(40, 60));
169
170
nine_patch->set_patch_margin(SIDE_RIGHT, 0);
171
SceneTree::get_singleton()->process(0);
172
CHECK(nine_patch->get_combined_minimum_size() == Size2(10, 60));
173
174
memdelete(nine_patch);
175
}
176
177
TEST_CASE("[SceneTree][NinePatchRect] Patch margin out-of-range index is guarded") {
178
NinePatchRect *nine_patch = memnew(NinePatchRect);
179
180
nine_patch->set_patch_margin(SIDE_LEFT, 11);
181
nine_patch->set_patch_margin(SIDE_TOP, 22);
182
nine_patch->set_patch_margin(SIDE_RIGHT, 33);
183
nine_patch->set_patch_margin(SIDE_BOTTOM, 44);
184
185
ERR_PRINT_OFF;
186
nine_patch->set_patch_margin((Side)4, 999);
187
const int out_of_range_value = nine_patch->get_patch_margin((Side)4);
188
ERR_PRINT_ON;
189
190
CHECK(out_of_range_value == 0);
191
CHECK(nine_patch->get_patch_margin(SIDE_LEFT) == 11);
192
CHECK(nine_patch->get_patch_margin(SIDE_TOP) == 22);
193
CHECK(nine_patch->get_patch_margin(SIDE_RIGHT) == 33);
194
CHECK(nine_patch->get_patch_margin(SIDE_BOTTOM) == 44);
195
196
memdelete(nine_patch);
197
}
198
199
TEST_CASE("[SceneTree][NinePatchRect] Region rect and draw center") {
200
NinePatchRect *nine_patch = memnew(NinePatchRect);
201
202
SUBCASE("Region rect is set and retrieved") {
203
const Rect2 region(8, 16, 32, 64);
204
nine_patch->set_region_rect(region);
205
CHECK(nine_patch->get_region_rect() == region);
206
}
207
208
SUBCASE("Draw center can be toggled") {
209
nine_patch->set_draw_center(false);
210
CHECK_FALSE(nine_patch->is_draw_center_enabled());
211
212
nine_patch->set_draw_center(true);
213
CHECK(nine_patch->is_draw_center_enabled());
214
}
215
216
memdelete(nine_patch);
217
}
218
219
TEST_CASE("[SceneTree][NinePatchRect] Axis stretch modes") {
220
NinePatchRect *nine_patch = memnew(NinePatchRect);
221
222
SUBCASE("Horizontal stretch mode accepts each value") {
223
nine_patch->set_h_axis_stretch_mode(NinePatchRect::AXIS_STRETCH_MODE_TILE);
224
CHECK(nine_patch->get_h_axis_stretch_mode() == NinePatchRect::AXIS_STRETCH_MODE_TILE);
225
226
nine_patch->set_h_axis_stretch_mode(NinePatchRect::AXIS_STRETCH_MODE_TILE_FIT);
227
CHECK(nine_patch->get_h_axis_stretch_mode() == NinePatchRect::AXIS_STRETCH_MODE_TILE_FIT);
228
229
nine_patch->set_h_axis_stretch_mode(NinePatchRect::AXIS_STRETCH_MODE_STRETCH);
230
CHECK(nine_patch->get_h_axis_stretch_mode() == NinePatchRect::AXIS_STRETCH_MODE_STRETCH);
231
}
232
233
SUBCASE("Vertical stretch mode accepts each value") {
234
nine_patch->set_v_axis_stretch_mode(NinePatchRect::AXIS_STRETCH_MODE_TILE);
235
CHECK(nine_patch->get_v_axis_stretch_mode() == NinePatchRect::AXIS_STRETCH_MODE_TILE);
236
237
nine_patch->set_v_axis_stretch_mode(NinePatchRect::AXIS_STRETCH_MODE_TILE_FIT);
238
CHECK(nine_patch->get_v_axis_stretch_mode() == NinePatchRect::AXIS_STRETCH_MODE_TILE_FIT);
239
240
nine_patch->set_v_axis_stretch_mode(NinePatchRect::AXIS_STRETCH_MODE_STRETCH);
241
CHECK(nine_patch->get_v_axis_stretch_mode() == NinePatchRect::AXIS_STRETCH_MODE_STRETCH);
242
}
243
244
SUBCASE("Horizontal and vertical stretch modes are independent") {
245
nine_patch->set_h_axis_stretch_mode(NinePatchRect::AXIS_STRETCH_MODE_TILE);
246
nine_patch->set_v_axis_stretch_mode(NinePatchRect::AXIS_STRETCH_MODE_TILE_FIT);
247
248
CHECK(nine_patch->get_h_axis_stretch_mode() == NinePatchRect::AXIS_STRETCH_MODE_TILE);
249
CHECK(nine_patch->get_v_axis_stretch_mode() == NinePatchRect::AXIS_STRETCH_MODE_TILE_FIT);
250
}
251
252
memdelete(nine_patch);
253
}
254
255
} // namespace TestNinePatchRect
256
257