Path: blob/master/tests/scene/test_option_button.cpp
45991 views
/**************************************************************************/1/* test_option_button.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "tests/test_macros.h"3132TEST_FORCE_LINK(test_option_button)3334#ifndef ADVANCED_GUI_DISABLED3536#include "scene/gui/option_button.h"3738namespace TestOptionButton {3940TEST_CASE("[SceneTree][OptionButton] Initialization") {41OptionButton *test_opt = memnew(OptionButton);4243SUBCASE("There should be no options right after initialization") {44CHECK_FALSE(test_opt->has_selectable_items());45CHECK(test_opt->get_item_count() == 0);46}4748memdelete(test_opt);49}5051TEST_CASE("[SceneTree][OptionButton] Single item") {52OptionButton *test_opt = memnew(OptionButton);5354SUBCASE("There should a single item after after adding one") {55test_opt->add_item("single", 1013);5657CHECK(test_opt->has_selectable_items());58CHECK(test_opt->get_item_count() == 1);59CHECK(test_opt->get_item_index(1013) == 0);60CHECK(test_opt->get_item_id(0) == 1013);6162test_opt->remove_item(0);6364CHECK_FALSE(test_opt->has_selectable_items());65CHECK(test_opt->get_item_count() == 0);66}6768SUBCASE("There should a single item after after adding an icon") {69Ref<Texture2D> test_icon = memnew(Texture2D);70test_opt->add_icon_item(test_icon, "icon", 345);7172CHECK(test_opt->has_selectable_items());73CHECK(test_opt->get_item_count() == 1);74CHECK(test_opt->get_item_index(345) == 0);75CHECK(test_opt->get_item_id(0) == 345);7677test_opt->remove_item(0);7879CHECK_FALSE(test_opt->has_selectable_items());80CHECK(test_opt->get_item_count() == 0);81}8283memdelete(test_opt);84}8586TEST_CASE("[SceneTree][OptionButton] Many items") {87OptionButton *test_opt = memnew(OptionButton);8889SUBCASE("Creating a complex structure and checking getters") {90// Regular item at index 0.91Ref<Texture2D> test_icon1 = memnew(Texture2D);92Ref<Texture2D> test_icon2 = memnew(Texture2D);93// Regular item at index 3.94Ref<Texture2D> test_icon4 = memnew(Texture2D);9596test_opt->add_item("first", 100);97test_opt->add_icon_item(test_icon1, "second_icon", 101);98test_opt->add_icon_item(test_icon2, "third_icon", 102);99test_opt->add_item("fourth", 104);100test_opt->add_icon_item(test_icon4, "fifth_icon", 104);101102// Disable test_icon4.103test_opt->set_item_disabled(4, true);104105CHECK(test_opt->has_selectable_items());106CHECK(test_opt->get_item_count() == 5);107108// Check for test_icon2.109CHECK(test_opt->get_item_index(102) == 2);110CHECK(test_opt->get_item_id(2) == 102);111112// Remove the two regular items.113test_opt->remove_item(3);114test_opt->remove_item(0);115116CHECK(test_opt->has_selectable_items());117CHECK(test_opt->get_item_count() == 3);118119// Check test_icon4.120CHECK(test_opt->get_item_index(104) == 2);121CHECK(test_opt->get_item_id(2) == 104);122123// Remove the two non-disabled icon items.124test_opt->remove_item(1);125test_opt->remove_item(0);126127CHECK_FALSE(test_opt->has_selectable_items());128CHECK(test_opt->get_item_count() == 1);129}130131SUBCASE("Getters and setters not related to structure") {132test_opt->add_item("regular", 2019);133134Ref<Texture2D> test_icon = memnew(Texture2D);135test_opt->add_icon_item(test_icon, "icon", 3092);136137// item_text.138test_opt->set_item_text(0, "example text");139CHECK(test_opt->get_item_text(0) == "example text");140141// item_metadata.142Dictionary m;143m["bool"] = true;144m["String"] = "yes";145test_opt->set_item_metadata(1, m);146CHECK(test_opt->get_item_metadata(1) == m);147148// item_tooltip.149test_opt->set_item_tooltip(0, "tooltip guide");150CHECK(test_opt->get_item_tooltip(0) == "tooltip guide");151152test_opt->remove_item(1);153test_opt->remove_item(0);154}155156memdelete(test_opt);157}158159} // namespace TestOptionButton160161#endif // ADVANCED_GUI_DISABLED162163164