Debian Codegraph @

tests/scene/test_option_button.h

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