Path: blob/master/tests/scene/test_margin_container.cpp
59209 views
/**************************************************************************/1/* test_margin_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_margin_container)3334#include "scene/gui/control.h"35#include "scene/gui/margin_container.h"36#include "scene/main/scene_tree.h"37#include "scene/main/window.h"3839namespace TestMarginContainer {4041TEST_CASE("[SceneTree][MarginContainer] Default margins and layout") {42MarginContainer *margin_container = memnew(MarginContainer);43Window *root = SceneTree::get_singleton()->get_root();44root->add_child(margin_container);4546Control *child_control = memnew(Control);47child_control->set_custom_minimum_size(Size2(20, 10));48margin_container->add_child(child_control);4950margin_container->set_size(Size2(100, 80));51SceneTree::get_singleton()->process(0);5253CHECK_MESSAGE(54margin_container->get_margin_size(SIDE_LEFT) == 0,55"Left margin is 0 by default.");5657CHECK_MESSAGE(58margin_container->get_margin_size(SIDE_TOP) == 0,59"Top margin is 0 by default.");6061CHECK_MESSAGE(62margin_container->get_margin_size(SIDE_RIGHT) == 0,63"Right margin is 0 by default.");6465CHECK_MESSAGE(66margin_container->get_margin_size(SIDE_BOTTOM) == 0,67"Bottom margin is 0 by default.");6869CHECK_MESSAGE(70child_control->get_position().is_equal_approx(Point2(0, 0)),71"Child control starts at the top-left corner when all margins are 0.");7273CHECK_MESSAGE(74child_control->get_size().is_equal_approx(Size2(100, 80)),75"Child control fills the container area when all margins are 0.");7677CHECK_MESSAGE(78margin_container->get_minimum_size().is_equal_approx(Size2(20, 10)),79"Minimum size equals the child minimum size when all margins are 0.");8081memdelete(child_control);82memdelete(margin_container);83}8485TEST_CASE("[SceneTree][MarginContainer] Custom margins affect layout and minimum size") {86MarginContainer *margin_container = memnew(MarginContainer);87Window *root = SceneTree::get_singleton()->get_root();88root->add_child(margin_container);8990margin_container->add_theme_constant_override("margin_left", 10);91margin_container->add_theme_constant_override("margin_top", 20);92margin_container->add_theme_constant_override("margin_right", 30);93margin_container->add_theme_constant_override("margin_bottom", 40);9495Control *child_control = memnew(Control);96child_control->set_custom_minimum_size(Size2(20, 10));97margin_container->add_child(child_control);9899margin_container->set_size(Size2(200, 150));100SceneTree::get_singleton()->process(0);101102CHECK_MESSAGE(103child_control->get_position().is_equal_approx(Point2(10, 20)),104"Child control is offset by left and top margins.");105106CHECK_MESSAGE(107child_control->get_size().is_equal_approx(Size2(160, 90)),108"Child control size is reduced by left/right and top/bottom margins.");109110CHECK_MESSAGE(111margin_container->get_minimum_size().is_equal_approx(Size2(60, 70)),112"Minimum size equals child minimum size plus all configured margins.");113114memdelete(child_control);115memdelete(margin_container);116}117118TEST_CASE("[SceneTree][MarginContainer] Multiple children use maximum minimum size") {119MarginContainer *margin_container = memnew(MarginContainer);120Window *root = SceneTree::get_singleton()->get_root();121root->add_child(margin_container);122123margin_container->add_theme_constant_override("margin_left", 1);124margin_container->add_theme_constant_override("margin_top", 2);125margin_container->add_theme_constant_override("margin_right", 3);126margin_container->add_theme_constant_override("margin_bottom", 4);127128Control *child_control_1 = memnew(Control);129Control *child_control_2 = memnew(Control);130child_control_1->set_custom_minimum_size(Size2(40, 10));131child_control_2->set_custom_minimum_size(Size2(20, 50));132margin_container->add_child(child_control_1);133margin_container->add_child(child_control_2);134SceneTree::get_singleton()->process(0);135136CHECK_MESSAGE(137margin_container->get_minimum_size().is_equal_approx(Size2(44, 56)),138"Minimum size uses maximum child minimum width/height plus margins.");139140memdelete(child_control_2);141memdelete(child_control_1);142memdelete(margin_container);143}144145} // namespace TestMarginContainer146147148