Debian Codegraph @

tests/scene/test_theme.h

raw symbols SCIP
1 /**************************************************************************/
2 /* test_theme.h */
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 #pragma once
32
33 #include "scene/resources/image_texture.h"
34 #include "scene/resources/style_box_flat.h"
35 #include "scene/resources/theme.h"
36 #include "tests/test_tools.h"
37
38 #include "thirdparty/doctest/doctest.h"
39
40 namespace TestTheme {
41
42 class Fixture {
43 public:
44 struct DataEntry {
45 Theme::DataType type;
46 Variant value;
47 } const valid_data[Theme::DATA_TYPE_MAX] = {
48 { Theme::DATA_TYPE_COLOR, Color() },
49 { Theme::DATA_TYPE_CONSTANT, 42 },
50 { Theme::DATA_TYPE_FONT, Ref<FontFile>(memnew(FontFile)) },
51 { Theme::DATA_TYPE_FONT_SIZE, 42 },
52 { Theme::DATA_TYPE_ICON, Ref<Texture>(memnew(ImageTexture)) },
53 { Theme::DATA_TYPE_STYLEBOX, Ref<StyleBox>(memnew(StyleBoxFlat)) },
54 };
55
56 const StringName valid_item_name = "valid_item_name";
57 const StringName valid_type_name = "ValidTypeName";
58 };
59
60 TEST_CASE_FIXTURE(Fixture, "[Theme] Good theme type names") {
61 StringName names[] = {
62 "", // Empty name.
63 "CapitalizedName",
64 "snake_cased_name",
65 "42",
66 "_Underscore_",
67 };
68
69 SUBCASE("add_type") {
70 for (const StringName &name : names) {
71 Ref<Theme> theme = memnew(Theme);
72
73 ErrorDetector ed;
74 theme->add_type(name);
75 CHECK_FALSE(ed.has_error);
76 }
77 }
78
79 SUBCASE("set_theme_item") {
80 for (const StringName &name : names) {
81 for (const DataEntry &entry : valid_data) {
82 Ref<Theme> theme = memnew(Theme);
83
84 ErrorDetector ed;
85 theme->set_theme_item(entry.type, valid_item_name, name, entry.value);
86 CHECK_FALSE(ed.has_error);
87 }
88 }
89 }
90
91 SUBCASE("add_theme_item_type") {
92 for (const StringName &name : names) {
93 for (const DataEntry &entry : valid_data) {
94 Ref<Theme> theme = memnew(Theme);
95
96 ErrorDetector ed;
97 theme->add_theme_item_type(entry.type, name);
98 CHECK_FALSE(ed.has_error);
99 }
100 }
101 }
102
103 SUBCASE("set_type_variation") {
104 for (const StringName &name : names) {
105 if (name == StringName()) { // Skip empty here, not allowed.
106 continue;
107 }
108 Ref<Theme> theme = memnew(Theme);
109
110 ErrorDetector ed;
111 theme->set_type_variation(valid_type_name, name);
112 CHECK_FALSE(ed.has_error);
113 }
114 for (const StringName &name : names) {
115 if (name == StringName()) { // Skip empty here, not allowed.
116 continue;
117 }
118 Ref<Theme> theme = memnew(Theme);
119
120 ErrorDetector ed;
121 theme->set_type_variation(name, valid_type_name);
122 CHECK_FALSE(ed.has_error);
123 }
124 }
125 }
126
127 TEST_CASE_FIXTURE(Fixture, "[Theme] Bad theme type names") {
128 StringName names[] = {
129 "With/Slash",
130 "With Space",
131 "With@various$symbols!",
132 String::utf8("contains_汉字"),
133 };
134
135 ERR_PRINT_OFF; // All these rightfully print errors.
136
137 SUBCASE("add_type") {
138 for (const StringName &name : names) {
139 Ref<Theme> theme = memnew(Theme);
140
141 ErrorDetector ed;
142 theme->add_type(name);
143 CHECK(ed.has_error);
144 }
145 }
146
147 SUBCASE("set_theme_item") {
148 for (const StringName &name : names) {
149 for (const DataEntry &entry : valid_data) {
150 Ref<Theme> theme = memnew(Theme);
151
152 ErrorDetector ed;
153 theme->set_theme_item(entry.type, valid_item_name, name, entry.value);
154 CHECK(ed.has_error);
155 }
156 }
157 }
158
159 SUBCASE("add_theme_item_type") {
160 for (const StringName &name : names) {
161 for (const DataEntry &entry : valid_data) {
162 Ref<Theme> theme = memnew(Theme);
163
164 ErrorDetector ed;
165 theme->add_theme_item_type(entry.type, name);
166 CHECK(ed.has_error);
167 }
168 }
169 }
170
171 SUBCASE("set_type_variation") {
172 for (const StringName &name : names) {
173 Ref<Theme> theme = memnew(Theme);
174
175 ErrorDetector ed;
176 theme->set_type_variation(valid_type_name, name);
177 CHECK(ed.has_error);
178 }
179 for (const StringName &name : names) {
180 Ref<Theme> theme = memnew(Theme);
181
182 ErrorDetector ed;
183 theme->set_type_variation(name, valid_type_name);
184 CHECK(ed.has_error);
185 }
186 }
187
188 ERR_PRINT_ON;
189 }
190
191 TEST_CASE_FIXTURE(Fixture, "[Theme] Good theme item names") {
192 StringName names[] = {
193 "CapitalizedName",
194 "snake_cased_name",
195 "42",
196 "_Underscore_",
197 };
198
199 SUBCASE("set_theme_item") {
200 for (const StringName &name : names) {
201 for (const DataEntry &entry : valid_data) {
202 Ref<Theme> theme = memnew(Theme);
203
204 ErrorDetector ed;
205 theme->set_theme_item(entry.type, name, valid_type_name, entry.value);
206 CHECK_FALSE(ed.has_error);
207 CHECK(theme->has_theme_item(entry.type, name, valid_type_name));
208 }
209 }
210 }
211
212 SUBCASE("rename_theme_item") {
213 for (const StringName &name : names) {
214 for (const DataEntry &entry : valid_data) {
215 Ref<Theme> theme = memnew(Theme);
216 theme->set_theme_item(entry.type, valid_item_name, valid_type_name, entry.value);
217
218 ErrorDetector ed;
219 theme->rename_theme_item(entry.type, valid_item_name, name, valid_type_name);
220 CHECK_FALSE(ed.has_error);
221 CHECK_FALSE(theme->has_theme_item(entry.type, valid_item_name, valid_type_name));
222 CHECK(theme->has_theme_item(entry.type, name, valid_type_name));
223 }
224 }
225 }
226 }
227
228 TEST_CASE_FIXTURE(Fixture, "[Theme] Bad theme item names") {
229 StringName names[] = {
230 "", // Empty name.
231 "With/Slash",
232 "With Space",
233 "With@various$symbols!",
234 String::utf8("contains_汉字"),
235 };
236
237 ERR_PRINT_OFF; // All these rightfully print errors.
238
239 SUBCASE("set_theme_item") {
240 for (const StringName &name : names) {
241 for (const DataEntry &entry : valid_data) {
242 Ref<Theme> theme = memnew(Theme);
243
244 ErrorDetector ed;
245 theme->set_theme_item(entry.type, name, valid_type_name, entry.value);
246 CHECK(ed.has_error);
247 CHECK_FALSE(theme->has_theme_item(entry.type, name, valid_type_name));
248 }
249 }
250 }
251
252 SUBCASE("rename_theme_item") {
253 for (const StringName &name : names) {
254 for (const DataEntry &entry : valid_data) {
255 Ref<Theme> theme = memnew(Theme);
256 theme->set_theme_item(entry.type, valid_item_name, valid_type_name, entry.value);
257
258 ErrorDetector ed;
259 theme->rename_theme_item(entry.type, valid_item_name, name, valid_type_name);
260 CHECK(ed.has_error);
261 CHECK(theme->has_theme_item(entry.type, valid_item_name, valid_type_name));
262 CHECK_FALSE(theme->has_theme_item(entry.type, name, valid_type_name));
263 }
264 }
265 }
266
267 ERR_PRINT_ON;
268 }
269
270 } // namespace TestTheme