test_theme.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /**************************************************************************/
  2. /* test_theme.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. #include "test_theme.h"
  31. #include "core/os/os.h"
  32. #include "main/tests/test_tools.h"
  33. #include "scene/resources/theme.h"
  34. #define CHECK(X) \
  35. if (!(X)) { \
  36. OS::get_singleton()->print("\tFAIL at line %d: %s\n", __LINE__, #X); \
  37. return false; \
  38. } else { \
  39. OS::get_singleton()->print("\tPASS\n"); \
  40. }
  41. namespace TestTheme {
  42. class Fixture {
  43. public:
  44. struct DataEntry {
  45. Theme::DataType type;
  46. Variant value;
  47. } valid_data[Theme::DATA_TYPE_MAX] = {
  48. { Theme::DATA_TYPE_COLOR, Color() },
  49. { Theme::DATA_TYPE_CONSTANT, 42 },
  50. { Theme::DATA_TYPE_FONT, Variant() },
  51. { Theme::DATA_TYPE_ICON, Variant() },
  52. { Theme::DATA_TYPE_STYLEBOX, Variant() },
  53. };
  54. const StringName valid_item_name = "valid_item_name";
  55. const StringName valid_type_name = "ValidTypeName";
  56. // Part of `valid_data` initialization is moved here so that it compiles in Visual Studio 2017.
  57. // See issue #63975.
  58. Fixture() {
  59. valid_data[Theme::DATA_TYPE_FONT].value = Ref<Font>(memnew(BitmapFont));
  60. valid_data[Theme::DATA_TYPE_ICON].value = Ref<Texture>(memnew(ImageTexture));
  61. valid_data[Theme::DATA_TYPE_STYLEBOX].value = Ref<StyleBox>(memnew(StyleBoxFlat));
  62. }
  63. };
  64. bool test_good_theme_type_names() {
  65. Fixture fixture;
  66. StringName names[] = {
  67. "", // Empty name.
  68. "CapitalizedName",
  69. "snake_cased_name",
  70. "42",
  71. "_Underscore_",
  72. };
  73. // add_type
  74. for (const StringName &name : names) {
  75. Ref<Theme> theme = memnew(Theme);
  76. ErrorDetector ed;
  77. theme->add_type(name);
  78. CHECK(!ed.has_error);
  79. }
  80. // set_theme_item
  81. for (const StringName &name : names) {
  82. for (const Fixture::DataEntry &entry : fixture.valid_data) {
  83. Ref<Theme> theme = memnew(Theme);
  84. ErrorDetector ed;
  85. theme->set_theme_item(entry.type, fixture.valid_item_name, name, entry.value);
  86. CHECK(!ed.has_error);
  87. }
  88. }
  89. // add_theme_item_type
  90. for (const StringName &name : names) {
  91. for (const Fixture::DataEntry &entry : fixture.valid_data) {
  92. Ref<Theme> theme = memnew(Theme);
  93. ErrorDetector ed;
  94. theme->add_theme_item_type(entry.type, name);
  95. CHECK(!ed.has_error);
  96. }
  97. }
  98. // set_type_variation
  99. for (const StringName &name : names) {
  100. Ref<Theme> theme = memnew(Theme);
  101. ErrorDetector ed;
  102. theme->set_type_variation(fixture.valid_type_name, name);
  103. CHECK(ed.has_error == (name == StringName()));
  104. }
  105. for (const StringName &name : names) {
  106. Ref<Theme> theme = memnew(Theme);
  107. ErrorDetector ed;
  108. theme->set_type_variation(name, fixture.valid_type_name);
  109. CHECK(ed.has_error == (name == StringName()));
  110. }
  111. return true;
  112. }
  113. bool test_bad_theme_type_names() {
  114. Fixture fixture;
  115. StringName names[] = {
  116. "With/Slash",
  117. "With Space",
  118. "With@various$symbols!",
  119. String::utf8("contains_汉字"),
  120. };
  121. // add_type
  122. for (const StringName &name : names) {
  123. Ref<Theme> theme = memnew(Theme);
  124. ErrorDetector ed;
  125. theme->add_type(name);
  126. CHECK(ed.has_error);
  127. }
  128. // set_theme_item
  129. for (const StringName &name : names) {
  130. for (const Fixture::DataEntry &entry : fixture.valid_data) {
  131. Ref<Theme> theme = memnew(Theme);
  132. ErrorDetector ed;
  133. theme->set_theme_item(entry.type, fixture.valid_item_name, name, entry.value);
  134. CHECK(ed.has_error);
  135. }
  136. }
  137. // add_theme_item_type
  138. for (const StringName &name : names) {
  139. for (const Fixture::DataEntry &entry : fixture.valid_data) {
  140. Ref<Theme> theme = memnew(Theme);
  141. ErrorDetector ed;
  142. theme->add_theme_item_type(entry.type, name);
  143. CHECK(ed.has_error);
  144. }
  145. }
  146. // set_type_variation
  147. for (const StringName &name : names) {
  148. Ref<Theme> theme = memnew(Theme);
  149. ErrorDetector ed;
  150. theme->set_type_variation(fixture.valid_type_name, name);
  151. CHECK(ed.has_error);
  152. }
  153. for (const StringName &name : names) {
  154. Ref<Theme> theme = memnew(Theme);
  155. ErrorDetector ed;
  156. theme->set_type_variation(name, fixture.valid_type_name);
  157. CHECK(ed.has_error);
  158. }
  159. return true;
  160. }
  161. bool test_good_theme_item_names() {
  162. Fixture fixture;
  163. StringName names[] = {
  164. "CapitalizedName",
  165. "snake_cased_name",
  166. "42",
  167. "_Underscore_",
  168. };
  169. // set_theme_item
  170. for (const StringName &name : names) {
  171. for (const Fixture::DataEntry &entry : fixture.valid_data) {
  172. Ref<Theme> theme = memnew(Theme);
  173. ErrorDetector ed;
  174. theme->set_theme_item(entry.type, name, fixture.valid_type_name, entry.value);
  175. CHECK(!ed.has_error);
  176. CHECK(theme->has_theme_item(entry.type, name, fixture.valid_type_name));
  177. }
  178. }
  179. // rename_theme_item
  180. for (const StringName &name : names) {
  181. for (const Fixture::DataEntry &entry : fixture.valid_data) {
  182. Ref<Theme> theme = memnew(Theme);
  183. theme->set_theme_item(entry.type, fixture.valid_item_name, fixture.valid_type_name, entry.value);
  184. ErrorDetector ed;
  185. theme->rename_theme_item(entry.type, fixture.valid_item_name, name, fixture.valid_type_name);
  186. CHECK(!ed.has_error);
  187. CHECK(!theme->has_theme_item(entry.type, fixture.valid_item_name, fixture.valid_type_name));
  188. CHECK(theme->has_theme_item(entry.type, name, fixture.valid_type_name));
  189. }
  190. }
  191. return true;
  192. }
  193. bool test_bad_theme_item_names() {
  194. Fixture fixture;
  195. StringName names[] = {
  196. "", // Empty name.
  197. "With/Slash",
  198. "With Space",
  199. "With@various$symbols!",
  200. String::utf8("contains_汉字"),
  201. };
  202. // set_theme_item
  203. for (const StringName &name : names) {
  204. for (const Fixture::DataEntry &entry : fixture.valid_data) {
  205. Ref<Theme> theme = memnew(Theme);
  206. ErrorDetector ed;
  207. theme->set_theme_item(entry.type, name, fixture.valid_type_name, entry.value);
  208. CHECK(ed.has_error);
  209. CHECK(!theme->has_theme_item(entry.type, name, fixture.valid_type_name));
  210. }
  211. }
  212. // rename_theme_item
  213. for (const StringName &name : names) {
  214. for (const Fixture::DataEntry &entry : fixture.valid_data) {
  215. Ref<Theme> theme = memnew(Theme);
  216. theme->set_theme_item(entry.type, fixture.valid_item_name, fixture.valid_type_name, entry.value);
  217. ErrorDetector ed;
  218. theme->rename_theme_item(entry.type, fixture.valid_item_name, name, fixture.valid_type_name);
  219. CHECK(ed.has_error);
  220. CHECK(theme->has_theme_item(entry.type, fixture.valid_item_name, fixture.valid_type_name));
  221. CHECK(!theme->has_theme_item(entry.type, name, fixture.valid_type_name));
  222. }
  223. }
  224. return true;
  225. }
  226. typedef bool (*TestFunc)();
  227. TestFunc test_funcs[] = {
  228. test_good_theme_type_names,
  229. test_bad_theme_type_names,
  230. test_good_theme_item_names,
  231. test_bad_theme_item_names,
  232. nullptr
  233. };
  234. MainLoop *test() {
  235. int count = 0;
  236. int passed = 0;
  237. while (true) {
  238. if (!test_funcs[count]) {
  239. break;
  240. }
  241. bool pass = test_funcs[count]();
  242. if (pass) {
  243. passed++;
  244. }
  245. OS::get_singleton()->print("\t%s\n", pass ? "PASS" : "FAILED");
  246. count++;
  247. }
  248. OS::get_singleton()->print("\n\n\n");
  249. OS::get_singleton()->print("*************\n");
  250. OS::get_singleton()->print("***TOTALS!***\n");
  251. OS::get_singleton()->print("*************\n");
  252. OS::get_singleton()->print("Passed %i of %i tests\n", passed, count);
  253. return nullptr;
  254. }
  255. } // namespace TestTheme
  256. #undef CHECK