summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEdward Moulsdale <eddie.moulsdale@gmail.com>2024-07-18 19:37:01 +0100
committerEdward Moulsdale <eddie.moulsdale@gmail.com>2024-07-18 19:37:01 +0100
commit80ba71c3951211334e13f8d50f0dc0e035aad03c (patch)
treedc863a16495c089269bb60afd0cf49eafaabd7c4
parentff8a2780ee777c2456ce42368e1065774c7c4c3f (diff)
downloadredot-engine-80ba71c3951211334e13f8d50f0dc0e035aad03c.tar.gz
Add tests for OptionButton
-rw-r--r--tests/scene/test_option_button.h136
-rw-r--r--tests/test_main.cpp1
2 files changed, 137 insertions, 0 deletions
diff --git a/tests/scene/test_option_button.h b/tests/scene/test_option_button.h
new file mode 100644
index 0000000000..56c1c7d611
--- /dev/null
+++ b/tests/scene/test_option_button.h
@@ -0,0 +1,136 @@
+/**************************************************************************/
+/* test_option_button.h */
+/**************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/**************************************************************************/
+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/**************************************************************************/
+
+#ifndef TEST_OPTION_BUTTON_H
+#define TEST_OPTION_BUTTON_H
+
+#include "scene/gui/option_button.h"
+
+#include "tests/test_macros.h"
+
+namespace TestOptionButton {
+
+TEST_CASE("[SceneTree][OptionButton] Initialization") {
+ OptionButton *test_opt = memnew(OptionButton);
+
+ SUBCASE("There should be no options right after initialization") {
+ CHECK_FALSE(test_opt->has_selectable_items());
+ CHECK(test_opt->get_item_count() == 0);
+ }
+
+ memdelete(test_opt);
+}
+
+TEST_CASE("[SceneTree][OptionButton] Single item") {
+ OptionButton *test_opt = memnew(OptionButton);
+
+ SUBCASE("There should a single item after after adding one") {
+ test_opt->add_item("single", 1013);
+
+ CHECK(test_opt->has_selectable_items());
+ CHECK(test_opt->get_item_count() == 1);
+ CHECK(test_opt->get_item_index(1013) == 0);
+ CHECK(test_opt->get_item_id(0) == 1013);
+
+ test_opt->remove_item(0);
+
+ CHECK_FALSE(test_opt->has_selectable_items());
+ CHECK(test_opt->get_item_count() == 0);
+ }
+
+ SUBCASE("There should a single item after after adding an icon") {
+ Ref<Texture2D> test_icon = memnew(Texture2D);
+ test_opt->add_icon_item(test_icon, "icon", 345);
+
+ CHECK(test_opt->has_selectable_items());
+ CHECK(test_opt->get_item_count() == 1);
+ CHECK(test_opt->get_item_index(345) == 0);
+ CHECK(test_opt->get_item_id(0) == 345);
+
+ test_opt->remove_item(0);
+
+ CHECK_FALSE(test_opt->has_selectable_items());
+ CHECK(test_opt->get_item_count() == 0);
+ }
+
+ memdelete(test_opt);
+}
+
+TEST_CASE("[SceneTree][OptionButton] Complex structure") {
+ OptionButton *test_opt = memnew(OptionButton);
+
+ SUBCASE("Creating a complex structure and checking getters") {
+ // Regular item at index 0.
+ Ref<Texture2D> test_icon1 = memnew(Texture2D);
+ Ref<Texture2D> test_icon2 = memnew(Texture2D);
+ // Regular item at index 3.
+ Ref<Texture2D> test_icon4 = memnew(Texture2D);
+
+ test_opt->add_item("first", 100);
+ test_opt->add_icon_item(test_icon1, "second_icon", 101);
+ test_opt->add_icon_item(test_icon2, "third_icon", 102);
+ test_opt->add_item("fourth", 104);
+ test_opt->add_icon_item(test_icon4, "fifth_icon", 104);
+
+ // Disable test_icon4.
+ test_opt->set_item_disabled(4, true);
+
+ CHECK(test_opt->has_selectable_items());
+ CHECK(test_opt->get_item_count() == 5);
+
+ // Check for test_icon2.
+ CHECK(test_opt->get_item_index(102) == 2);
+ CHECK(test_opt->get_item_id(2) == 102);
+
+ // Remove the two regular items.
+ test_opt->remove_item(3);
+ test_opt->remove_item(0);
+
+ CHECK(test_opt->has_selectable_items());
+ CHECK(test_opt->get_item_count() == 3);
+
+ // Check test_icon4.
+ CHECK(test_opt->get_item_index(104) == 2);
+ CHECK(test_opt->get_item_id(2) == 104);
+
+ // Remove the two non-disabled icon items.
+ test_opt->remove_item(1);
+ test_opt->remove_item(0);
+
+ CHECK_FALSE(test_opt->has_selectable_items());
+ CHECK(test_opt->get_item_count() == 1);
+ }
+
+ memdelete(test_opt);
+}
+
+} // namespace TestOptionButton
+
+#endif // TEST_OPTION_BUTTON_H
diff --git a/tests/test_main.cpp b/tests/test_main.cpp
index ddbf2f50e4..f1cf91d3f1 100644
--- a/tests/test_main.cpp
+++ b/tests/test_main.cpp
@@ -131,6 +131,7 @@
#include "tests/scene/test_code_edit.h"
#include "tests/scene/test_color_picker.h"
#include "tests/scene/test_graph_node.h"
+#include "tests/scene/test_option_button.h"
#include "tests/scene/test_text_edit.h"
#endif // ADVANCED_GUI_DISABLED