test_theme.h 8.1 KB

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