Path: blob/master/tests/scene/test_center_container.cpp
59209 views
/**************************************************************************/1/* test_center_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_center_container)3334#include "scene/gui/center_container.h"35#include "scene/gui/control.h"36#include "scene/main/scene_tree.h"37#include "scene/main/window.h"3839namespace TestCenterContainer {4041TEST_CASE("[SceneTree][CenterContainer] Standard behavior") {42CenterContainer *center_container = memnew(CenterContainer);43Window *root = SceneTree::get_singleton()->get_root();44root->add_child(center_container);45Control *child_control = memnew(Control);46center_container->add_child(child_control);4748center_container->set_size(Size2(100, 100));49SceneTree::get_singleton()->process(0);5051CHECK_MESSAGE(52child_control->get_position().is_equal_approx(Point2(50, 50)),53"Child control is centered within the CenterContainer by default.");5455child_control->set_custom_minimum_size(Size2(20, 20));56SceneTree::get_singleton()->process(0);5758CHECK_MESSAGE(59child_control->get_position().is_equal_approx(Point2(40, 40)),60"Child control remains centered when custom minimum size is set.");6162child_control->set_custom_minimum_size(Size2(200, 200));63SceneTree::get_singleton()->process(0);6465CHECK_MESSAGE(66center_container->get_size().is_equal_approx(Size2(200, 200)),67"CenterContainer expands to accommodate the minimum size of its child control.");6869CHECK_MESSAGE(70child_control->get_position().is_equal_approx(Point2(0, 0)),71"Child control is positioned at the top-left corner when its minimum size equals the container size.");7273child_control->set_custom_minimum_size(Size2());74SceneTree::get_singleton()->process(0);7576CHECK_MESSAGE(77child_control->get_position().is_equal_approx(Point2(50, 50)),78"Child control re-centers when custom minimum size is reset.");7980CHECK_MESSAGE(81center_container->get_size().is_equal_approx(Size2(100, 100)),82"CenterContainer returns to original size when child control's custom minimum size is reset.");8384center_container->set_use_top_left(true);85SceneTree::get_singleton()->process(0);8687CHECK_MESSAGE(88child_control->get_position().is_equal_approx(Point2(0, 0)),89"Child control is aligned to the top-left corner when use_top_left is enabled.");9091center_container->set_layout_direction(Control::LAYOUT_DIRECTION_RTL);92SceneTree::get_singleton()->process(0);9394CHECK_MESSAGE(95child_control->get_position().is_equal_approx(Point2(100, 0)),96"Child control is aligned to the top-right corner in RTL layout direction when use_top_left is enabled.");9798memdelete(child_control);99memdelete(center_container);100}101102TEST_CASE("[SceneTree][CenterContainer] Multiple children") {103CenterContainer *center_container = memnew(CenterContainer);104Window *root = SceneTree::get_singleton()->get_root();105root->add_child(center_container);106107center_container->set_size(Size2(100, 100));108109Control *child_control_1 = memnew(Control);110Control *child_control_2 = memnew(Control);111center_container->add_child(child_control_1);112center_container->add_child(child_control_2);113SceneTree::get_singleton()->process(0);114115CHECK_MESSAGE(116child_control_1->get_position().is_equal_approx(Point2(50, 50)),117"First child control is centered within the CenterContainer.");118119CHECK_MESSAGE(120child_control_2->get_position().is_equal_approx(Point2(50, 50)),121"Second child control is also centered and overlaps the first child control by default.");122123child_control_1->set_custom_minimum_size(Size2(20, 20));124SceneTree::get_singleton()->process(0);125126CHECK_MESSAGE(127child_control_1->get_position().is_equal_approx(Point2(40, 40)),128"First child control remains centered when custom minimum size is set.");129130CHECK_MESSAGE(131child_control_2->get_position().is_equal_approx(Point2(50, 50)),132"Second child control remains centered and overlaps the first child control even when the first child has a custom minimum size.");133134child_control_1->set_custom_minimum_size(Size2(200, 0));135SceneTree::get_singleton()->process(0);136137CHECK_MESSAGE(138center_container->get_size().is_equal_approx(Size2(200, 100)),139"CenterContainer expands to accommodate the minimum size of the first child control.");140141CHECK_MESSAGE(142child_control_1->get_position().is_equal_approx(Point2(0, 50)),143"First child control is aligned to the left edge of the CenterContainer when its minimum width equals the container width.");144145CHECK_MESSAGE(146child_control_2->get_position().is_equal_approx(Point2(100, 50)),147"Second child control is centered on the new container center.");148149child_control_2->set_custom_minimum_size(Size2(0, 200));150SceneTree::get_singleton()->process(0);151152CHECK_MESSAGE(153center_container->get_size().is_equal_approx(Size2(200, 200)),154"CenterContainer expands to accommodate the minimum size of the second child control.");155156CHECK_MESSAGE(157child_control_1->get_position().is_equal_approx(Point2(0, 100)),158"First child control is aligned to the left edge and centered vertically when its minimum width equals the container width but its minimum height is smaller than the container height.");159160CHECK_MESSAGE(161child_control_2->get_position().is_equal_approx(Point2(100, 0)),162"Second child control is aligned to the top edge and centered horizontally when its minimum height equals the container height but its minimum width is smaller than the container width.");163164memdelete(child_control_2);165memdelete(child_control_1);166memdelete(center_container);167}168169} // namespace TestCenterContainer170171172