test_theme.h 8.0 KB

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