test_option_button.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #ifndef TEST_OPTION_BUTTON_H
  31. #define TEST_OPTION_BUTTON_H
  32. #include "scene/gui/option_button.h"
  33. #include "tests/test_macros.h"
  34. namespace TestOptionButton {
  35. TEST_CASE("[SceneTree][OptionButton] Initialization") {
  36. OptionButton *test_opt = memnew(OptionButton);
  37. SUBCASE("There should be no options right after initialization") {
  38. CHECK_FALSE(test_opt->has_selectable_items());
  39. CHECK(test_opt->get_item_count() == 0);
  40. }
  41. memdelete(test_opt);
  42. }
  43. TEST_CASE("[SceneTree][OptionButton] Single item") {
  44. OptionButton *test_opt = memnew(OptionButton);
  45. SUBCASE("There should a single item after after adding one") {
  46. test_opt->add_item("single", 1013);
  47. CHECK(test_opt->has_selectable_items());
  48. CHECK(test_opt->get_item_count() == 1);
  49. CHECK(test_opt->get_item_index(1013) == 0);
  50. CHECK(test_opt->get_item_id(0) == 1013);
  51. test_opt->remove_item(0);
  52. CHECK_FALSE(test_opt->has_selectable_items());
  53. CHECK(test_opt->get_item_count() == 0);
  54. }
  55. SUBCASE("There should a single item after after adding an icon") {
  56. Ref<Texture2D> test_icon = memnew(Texture2D);
  57. test_opt->add_icon_item(test_icon, "icon", 345);
  58. CHECK(test_opt->has_selectable_items());
  59. CHECK(test_opt->get_item_count() == 1);
  60. CHECK(test_opt->get_item_index(345) == 0);
  61. CHECK(test_opt->get_item_id(0) == 345);
  62. test_opt->remove_item(0);
  63. CHECK_FALSE(test_opt->has_selectable_items());
  64. CHECK(test_opt->get_item_count() == 0);
  65. }
  66. memdelete(test_opt);
  67. }
  68. TEST_CASE("[SceneTree][OptionButton] Complex structure") {
  69. OptionButton *test_opt = memnew(OptionButton);
  70. SUBCASE("Creating a complex structure and checking getters") {
  71. // Regular item at index 0.
  72. Ref<Texture2D> test_icon1 = memnew(Texture2D);
  73. Ref<Texture2D> test_icon2 = memnew(Texture2D);
  74. // Regular item at index 3.
  75. Ref<Texture2D> test_icon4 = memnew(Texture2D);
  76. test_opt->add_item("first", 100);
  77. test_opt->add_icon_item(test_icon1, "second_icon", 101);
  78. test_opt->add_icon_item(test_icon2, "third_icon", 102);
  79. test_opt->add_item("fourth", 104);
  80. test_opt->add_icon_item(test_icon4, "fifth_icon", 104);
  81. // Disable test_icon4.
  82. test_opt->set_item_disabled(4, true);
  83. CHECK(test_opt->has_selectable_items());
  84. CHECK(test_opt->get_item_count() == 5);
  85. // Check for test_icon2.
  86. CHECK(test_opt->get_item_index(102) == 2);
  87. CHECK(test_opt->get_item_id(2) == 102);
  88. // Remove the two regular items.
  89. test_opt->remove_item(3);
  90. test_opt->remove_item(0);
  91. CHECK(test_opt->has_selectable_items());
  92. CHECK(test_opt->get_item_count() == 3);
  93. // Check test_icon4.
  94. CHECK(test_opt->get_item_index(104) == 2);
  95. CHECK(test_opt->get_item_id(2) == 104);
  96. // Remove the two non-disabled icon items.
  97. test_opt->remove_item(1);
  98. test_opt->remove_item(0);
  99. CHECK_FALSE(test_opt->has_selectable_items());
  100. CHECK(test_opt->get_item_count() == 1);
  101. }
  102. memdelete(test_opt);
  103. }
  104. } // namespace TestOptionButton
  105. #endif // TEST_OPTION_BUTTON_H