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