Path: blob/master/tests/scene/test_foldable_container.cpp
59209 views
/**************************************************************************/1/* test_foldable_container.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_foldable_container)3334#ifndef ADVANCED_GUI_DISABLED3536#include "scene/gui/control.h"37#include "scene/gui/foldable_container.h"38#include "scene/main/scene_tree.h"39#include "scene/main/window.h"4041namespace TestFoldableContainer {4243TEST_CASE("[SceneTree][FoldableContainer] Default properties") {44FoldableContainer *foldable_container = memnew(FoldableContainer);45Window *root = SceneTree::get_singleton()->get_root();46root->add_child(foldable_container);47SceneTree::get_singleton()->process(0);4849CHECK_MESSAGE(50!foldable_container->is_folded(),51"FoldableContainer is expanded by default.");5253CHECK_MESSAGE(54foldable_container->get_title().is_empty(),55"FoldableContainer title is empty by default.");5657CHECK_MESSAGE(58foldable_container->get_title_alignment() == HORIZONTAL_ALIGNMENT_LEFT,59"FoldableContainer title alignment is set to left by default.");6061CHECK_MESSAGE(62foldable_container->get_title_position() == FoldableContainer::POSITION_TOP,63"FoldableContainer title position is set to top by default.");6465CHECK_MESSAGE(66foldable_container->get_title_text_direction() == Control::TEXT_DIRECTION_AUTO,67"FoldableContainer title text direction is set to auto by default.");6869CHECK_MESSAGE(70foldable_container->get_title_text_overrun_behavior() == TextServer::OVERRUN_NO_TRIMMING,71"FoldableContainer title overrun behavior is set to no trimming by default.");7273CHECK_MESSAGE(74foldable_container->get_language().is_empty(),75"FoldableContainer language is empty by default.");7677CHECK_MESSAGE(78foldable_container->get_focus_mode() == Control::FOCUS_ALL,79"FoldableContainer focus mode is set to FOCUS_ALL by default.");8081CHECK_MESSAGE(82foldable_container->get_mouse_filter() == Control::MOUSE_FILTER_STOP,83"FoldableContainer mouse filter is set to MOUSE_FILTER_STOP by default.");8485CHECK_MESSAGE(86foldable_container->get_foldable_group().is_null(),87"FoldableContainer has no foldable group by default.");8889memdelete(foldable_container);90}9192TEST_CASE("[SceneTree][FoldableContainer] Fold and expand behavior") {93FoldableContainer *foldable_container = memnew(FoldableContainer("Section"));94Window *root = SceneTree::get_singleton()->get_root();95root->add_child(foldable_container);9697Control *child_control = memnew(Control);98child_control->set_custom_minimum_size(Size2(120, 80));99foldable_container->add_child(child_control);100foldable_container->set_size(Size2(200, 200));101SceneTree::get_singleton()->process(0);102103Size2 expanded_minimum_size = foldable_container->get_minimum_size();104105CHECK_MESSAGE(106child_control->is_visible(),107"Child control is visible when FoldableContainer is expanded.");108109foldable_container->fold();110SceneTree::get_singleton()->process(0);111112CHECK_MESSAGE(113foldable_container->is_folded(),114"FoldableContainer is folded after calling fold().");115116CHECK_MESSAGE(117!child_control->is_visible(),118"Child control is hidden when FoldableContainer is folded.");119120CHECK_MESSAGE(121foldable_container->get_minimum_size().y < expanded_minimum_size.y,122"Folded minimum size is smaller than expanded minimum size when content is present.");123124foldable_container->expand();125SceneTree::get_singleton()->process(0);126127CHECK_MESSAGE(128!foldable_container->is_folded(),129"FoldableContainer is expanded after calling expand().");130131CHECK_MESSAGE(132child_control->is_visible(),133"Child control is visible again after FoldableContainer is expanded.");134135foldable_container->set_folded(true);136SceneTree::get_singleton()->process(0);137138CHECK_MESSAGE(139foldable_container->is_folded(),140"set_folded(true) folds the FoldableContainer.");141142foldable_container->set_folded(false);143SceneTree::get_singleton()->process(0);144145CHECK_MESSAGE(146!foldable_container->is_folded(),147"set_folded(false) expands the FoldableContainer.");148149memdelete(child_control);150memdelete(foldable_container);151}152153TEST_CASE("[SceneTree][FoldableContainer] Title position and title bar controls") {154FoldableContainer *foldable_container = memnew(FoldableContainer("Section"));155Window *root = SceneTree::get_singleton()->get_root();156root->add_child(foldable_container);157158Control *content_control = memnew(Control);159content_control->set_custom_minimum_size(Size2(40, 40));160foldable_container->add_child(content_control);161162Control *title_control = memnew(Control);163title_control->set_custom_minimum_size(Size2(30, 20));164foldable_container->add_title_bar_control(title_control);165166foldable_container->set_size(Size2(200, 140));167SceneTree::get_singleton()->process(0);168169CHECK_MESSAGE(170title_control->get_parent() == foldable_container,171"Title bar control is reparented to the FoldableContainer when added.");172173real_t top_title_control_y = title_control->get_position().y;174real_t top_content_y = content_control->get_position().y;175176foldable_container->set_title_position(FoldableContainer::POSITION_BOTTOM);177SceneTree::get_singleton()->process(0);178179real_t bottom_title_control_y = title_control->get_position().y;180real_t bottom_content_y = content_control->get_position().y;181182CHECK_MESSAGE(183bottom_title_control_y > top_title_control_y,184"Title bar controls move to the bottom when title position is set to POSITION_BOTTOM.");185186CHECK_MESSAGE(187bottom_content_y < top_content_y,188"Content area shifts upward when title position is changed from top to bottom.");189190foldable_container->remove_title_bar_control(title_control);191192CHECK_MESSAGE(193title_control->get_parent() == nullptr,194"Title bar control is detached from FoldableContainer when removed.");195196memdelete(title_control);197memdelete(content_control);198memdelete(foldable_container);199}200201TEST_CASE("[SceneTree][FoldableContainer] FoldableGroup behavior") {202Ref<FoldableGroup> foldable_group;203foldable_group.instantiate();204205FoldableContainer *container_a = memnew(FoldableContainer("A"));206FoldableContainer *container_b = memnew(FoldableContainer("B"));207Window *root = SceneTree::get_singleton()->get_root();208root->add_child(container_a);209root->add_child(container_b);210211container_a->set_folded(true);212container_b->set_folded(true);213container_a->set_foldable_group(foldable_group);214container_b->set_foldable_group(foldable_group);215SceneTree::get_singleton()->process(0);216217CHECK_MESSAGE(218!container_a->is_folded(),219"First grouped FoldableContainer auto-expands when allow_folding_all is false and no container is expanded.");220221CHECK_MESSAGE(222container_b->is_folded(),223"Second grouped FoldableContainer remains folded when another container in the group is expanded.");224225CHECK_MESSAGE(226foldable_group->get_expanded_container() == container_a,227"FoldableGroup tracks the currently expanded container.");228229container_b->set_folded(false);230SceneTree::get_singleton()->process(0);231232CHECK_MESSAGE(233container_a->is_folded(),234"Expanding one grouped FoldableContainer folds the previously expanded container.");235236CHECK_MESSAGE(237!container_b->is_folded(),238"Expanded grouped FoldableContainer remains unfolded.");239240CHECK_MESSAGE(241foldable_group->get_expanded_container() == container_b,242"FoldableGroup updates expanded container after a different container is expanded.");243244container_b->set_folded(true);245SceneTree::get_singleton()->process(0);246247CHECK_MESSAGE(248!container_b->is_folded(),249"Expanded grouped FoldableContainer cannot be folded when allow_folding_all is false.");250251foldable_group->set_allow_folding_all(true);252container_b->set_folded(true);253SceneTree::get_singleton()->process(0);254255CHECK_MESSAGE(256container_b->is_folded(),257"Grouped FoldableContainer can be folded when allow_folding_all is true.");258259CHECK_MESSAGE(260foldable_group->get_expanded_container() == nullptr,261"FoldableGroup has no expanded container when all grouped containers are folded.");262263memdelete(container_b);264memdelete(container_a);265}266267} // namespace TestFoldableContainer268269#endif270271272