test_option_button.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**************************************************************************/
  2. /* test_option_button.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. #pragma once
  31. #include "scene/gui/option_button.h"
  32. #include "tests/test_macros.h"
  33. namespace TestOptionButton {
  34. TEST_CASE("[SceneTree][OptionButton] Initialization") {
  35. OptionButton *test_opt = memnew(OptionButton);
  36. SUBCASE("There should be no options right after initialization") {
  37. CHECK_FALSE(test_opt->has_selectable_items());
  38. CHECK(test_opt->get_item_count() == 0);
  39. }
  40. memdelete(test_opt);
  41. }
  42. TEST_CASE("[SceneTree][OptionButton] Single item") {
  43. OptionButton *test_opt = memnew(OptionButton);
  44. SUBCASE("There should a single item after after adding one") {
  45. test_opt->add_item("single", 1013);
  46. CHECK(test_opt->has_selectable_items());
  47. CHECK(test_opt->get_item_count() == 1);
  48. CHECK(test_opt->get_item_index(1013) == 0);
  49. CHECK(test_opt->get_item_id(0) == 1013);
  50. test_opt->remove_item(0);
  51. CHECK_FALSE(test_opt->has_selectable_items());
  52. CHECK(test_opt->get_item_count() == 0);
  53. }
  54. SUBCASE("There should a single item after after adding an icon") {
  55. Ref<Texture2D> test_icon = memnew(Texture2D);
  56. test_opt->add_icon_item(test_icon, "icon", 345);
  57. CHECK(test_opt->has_selectable_items());
  58. CHECK(test_opt->get_item_count() == 1);
  59. CHECK(test_opt->get_item_index(345) == 0);
  60. CHECK(test_opt->get_item_id(0) == 345);
  61. test_opt->remove_item(0);
  62. CHECK_FALSE(test_opt->has_selectable_items());
  63. CHECK(test_opt->get_item_count() == 0);
  64. }
  65. memdelete(test_opt);
  66. }
  67. TEST_CASE("[SceneTree][OptionButton] Many items") {
  68. OptionButton *test_opt = memnew(OptionButton);
  69. SUBCASE("Creating a complex structure and checking getters") {
  70. // Regular item at index 0.
  71. Ref<Texture2D> test_icon1 = memnew(Texture2D);
  72. Ref<Texture2D> test_icon2 = memnew(Texture2D);
  73. // Regular item at index 3.
  74. Ref<Texture2D> test_icon4 = memnew(Texture2D);
  75. test_opt->add_item("first", 100);
  76. test_opt->add_icon_item(test_icon1, "second_icon", 101);
  77. test_opt->add_icon_item(test_icon2, "third_icon", 102);
  78. test_opt->add_item("fourth", 104);
  79. test_opt->add_icon_item(test_icon4, "fifth_icon", 104);
  80. // Disable test_icon4.
  81. test_opt->set_item_disabled(4, true);
  82. CHECK(test_opt->has_selectable_items());
  83. CHECK(test_opt->get_item_count() == 5);
  84. // Check for test_icon2.
  85. CHECK(test_opt->get_item_index(102) == 2);
  86. CHECK(test_opt->get_item_id(2) == 102);
  87. // Remove the two regular items.
  88. test_opt->remove_item(3);
  89. test_opt->remove_item(0);
  90. CHECK(test_opt->has_selectable_items());
  91. CHECK(test_opt->get_item_count() == 3);
  92. // Check test_icon4.
  93. CHECK(test_opt->get_item_index(104) == 2);
  94. CHECK(test_opt->get_item_id(2) == 104);
  95. // Remove the two non-disabled icon items.
  96. test_opt->remove_item(1);
  97. test_opt->remove_item(0);
  98. CHECK_FALSE(test_opt->has_selectable_items());
  99. CHECK(test_opt->get_item_count() == 1);
  100. }
  101. SUBCASE("Getters and setters not related to structure") {
  102. test_opt->add_item("regular", 2019);
  103. Ref<Texture2D> test_icon = memnew(Texture2D);
  104. test_opt->add_icon_item(test_icon, "icon", 3092);
  105. // item_text.
  106. test_opt->set_item_text(0, "example text");
  107. CHECK(test_opt->get_item_text(0) == "example text");
  108. // item_metadata.
  109. Dictionary m;
  110. m["bool"] = true;
  111. m["String"] = "yes";
  112. test_opt->set_item_metadata(1, m);
  113. CHECK(test_opt->get_item_metadata(1) == m);
  114. // item_tooltip.
  115. test_opt->set_item_tooltip(0, "tooltip guide");
  116. CHECK(test_opt->get_item_tooltip(0) == "tooltip guide");
  117. test_opt->remove_item(1);
  118. test_opt->remove_item(0);
  119. }
  120. memdelete(test_opt);
  121. }
  122. } // namespace TestOptionButton