Path: blob/master/tests/scene/test_grid_container.cpp
59209 views
/**************************************************************************/1/* test_grid_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_grid_container)3334#include "scene/gui/control.h"35#include "scene/gui/grid_container.h"36#include "scene/main/scene_tree.h"37#include "scene/main/window.h"3839namespace TestGridContainer {4041TEST_CASE("[SceneTree][GridContainer] Default properties") {42GridContainer *grid_container = memnew(GridContainer);43Window *root = SceneTree::get_singleton()->get_root();44root->add_child(grid_container);4546grid_container->set_size(Size2(200, 200));47grid_container->add_theme_constant_override("h_separation", 0);48grid_container->add_theme_constant_override("v_separation", 0);4950Control *child_control = memnew(Control);51child_control->set_custom_minimum_size(Size2(20, 10));52grid_container->add_child(child_control);53SceneTree::get_singleton()->process(0);5455CHECK_MESSAGE(56grid_container->get_columns() == 1,57"GridContainer columns are set to 1 by default.");5859CHECK_MESSAGE(60child_control->get_position().is_equal_approx(Point2(0, 0)),61"Single child control starts at the top-left corner.");6263CHECK_MESSAGE(64child_control->get_size().is_equal_approx(Size2(20, 10)),65"Single child control keeps its minimum size when no expansion is requested.");6667memdelete(child_control);68memdelete(grid_container);69}7071TEST_CASE("[SceneTree][GridContainer] Columns and placement order") {72GridContainer *grid_container = memnew(GridContainer);73Window *root = SceneTree::get_singleton()->get_root();74root->add_child(grid_container);7576Control *child_control_1 = memnew(Control);77Control *child_control_2 = memnew(Control);78Control *child_control_3 = memnew(Control);79Control *child_control_4 = memnew(Control);80child_control_1->set_custom_minimum_size(Size2(10, 10));81child_control_2->set_custom_minimum_size(Size2(10, 10));82child_control_3->set_custom_minimum_size(Size2(10, 10));83child_control_4->set_custom_minimum_size(Size2(10, 10));8485grid_container->set_columns(2);86grid_container->set_size(Size2(200, 200));87grid_container->add_theme_constant_override("h_separation", 0);88grid_container->add_theme_constant_override("v_separation", 0);89grid_container->add_child(child_control_1);90grid_container->add_child(child_control_2);91grid_container->add_child(child_control_3);92grid_container->add_child(child_control_4);93SceneTree::get_singleton()->process(0);9495CHECK_MESSAGE(96grid_container->get_columns() == 2,97"GridContainer uses the configured number of columns.");9899CHECK_MESSAGE(100child_control_1->get_position().is_equal_approx(Point2(0, 0)),101"First child control is placed at row 0, column 0.");102103CHECK_MESSAGE(104child_control_2->get_position().is_equal_approx(Point2(10, 0)),105"Second child control is placed at row 0, column 1.");106107CHECK_MESSAGE(108child_control_3->get_position().is_equal_approx(Point2(0, 10)),109"Third child control is placed at row 1, column 0.");110111CHECK_MESSAGE(112child_control_4->get_position().is_equal_approx(Point2(10, 10)),113"Fourth child control is placed at row 1, column 1.");114115memdelete(child_control_4);116memdelete(child_control_3);117memdelete(child_control_2);118memdelete(child_control_1);119memdelete(grid_container);120}121122TEST_CASE("[SceneTree][GridContainer] Separation and minimum size") {123GridContainer *grid_container = memnew(GridContainer);124Window *root = SceneTree::get_singleton()->get_root();125root->add_child(grid_container);126127Control *child_control_1 = memnew(Control);128Control *child_control_2 = memnew(Control);129Control *child_control_3 = memnew(Control);130child_control_1->set_custom_minimum_size(Size2(10, 20));131child_control_2->set_custom_minimum_size(Size2(30, 10));132child_control_3->set_custom_minimum_size(Size2(15, 40));133134grid_container->set_columns(2);135grid_container->add_theme_constant_override("h_separation", 5);136grid_container->add_theme_constant_override("v_separation", 7);137grid_container->add_child(child_control_1);138grid_container->add_child(child_control_2);139grid_container->add_child(child_control_3);140SceneTree::get_singleton()->process(0);141142CHECK_MESSAGE(143grid_container->get_h_separation() == 5,144"GridContainer reports overridden horizontal separation.");145146CHECK_MESSAGE(147grid_container->get_minimum_size().is_equal_approx(Size2(50, 67)),148"GridContainer minimum size matches per-column and per-row maxima plus separations.");149150grid_container->set_size(Size2(50, 67));151SceneTree::get_singleton()->process(0);152153CHECK_MESSAGE(154child_control_1->get_position().is_equal_approx(Point2(0, 0)),155"First child control is placed at row 0, column 0 with configured separations.");156157CHECK_MESSAGE(158child_control_2->get_position().is_equal_approx(Point2(20, 0)),159"Second child control is offset by column width and horizontal separation.");160161CHECK_MESSAGE(162child_control_3->get_position().is_equal_approx(Point2(0, 27)),163"Third child control is offset by row height and vertical separation.");164165CHECK_MESSAGE(166child_control_1->get_size().is_equal_approx(Size2(15, 20)),167"First child control fills the first grid cell dimensions.");168169CHECK_MESSAGE(170child_control_2->get_size().is_equal_approx(Size2(30, 20)),171"Second child control fills the second grid cell dimensions.");172173CHECK_MESSAGE(174child_control_3->get_size().is_equal_approx(Size2(15, 40)),175"Third child control fills the second-row first-column cell dimensions.");176177memdelete(child_control_3);178memdelete(child_control_2);179memdelete(child_control_1);180memdelete(grid_container);181}182183TEST_CASE("[SceneTree][GridContainer] Expanding rows and columns") {184GridContainer *grid_container = memnew(GridContainer);185Window *root = SceneTree::get_singleton()->get_root();186root->add_child(grid_container);187188Control *child_control_1 = memnew(Control);189Control *child_control_2 = memnew(Control);190Control *child_control_3 = memnew(Control);191child_control_1->set_custom_minimum_size(Size2(20, 20));192child_control_2->set_custom_minimum_size(Size2(20, 20));193child_control_3->set_custom_minimum_size(Size2(20, 20));194child_control_1->set_h_size_flags(Control::SIZE_EXPAND_FILL);195child_control_1->set_v_size_flags(Control::SIZE_EXPAND_FILL);196197grid_container->set_columns(2);198grid_container->add_theme_constant_override("h_separation", 0);199grid_container->add_theme_constant_override("v_separation", 0);200grid_container->set_size(Size2(100, 100));201grid_container->add_child(child_control_1);202grid_container->add_child(child_control_2);203grid_container->add_child(child_control_3);204SceneTree::get_singleton()->process(0);205206CHECK_MESSAGE(207child_control_1->get_size().is_equal_approx(Size2(80, 80)),208"Expanded child control grows with its expanded column and row.");209210CHECK_MESSAGE(211child_control_2->get_size().is_equal_approx(Size2(20, 80)),212"Non-expanded column still receives expanded row height.");213214CHECK_MESSAGE(215child_control_3->get_size().is_equal_approx(Size2(80, 20)),216"Non-expanded row still receives expanded column width.");217218CHECK_MESSAGE(219child_control_2->get_position().is_equal_approx(Point2(80, 0)),220"Second child control is positioned after expanded first column width.");221222CHECK_MESSAGE(223child_control_3->get_position().is_equal_approx(Point2(0, 80)),224"Third child control is positioned after expanded first row height.");225226memdelete(child_control_3);227memdelete(child_control_2);228memdelete(child_control_1);229memdelete(grid_container);230}231232TEST_CASE("[SceneTree][GridContainer] RTL placement") {233GridContainer *grid_container = memnew(GridContainer);234Window *root = SceneTree::get_singleton()->get_root();235root->add_child(grid_container);236237Control *child_control_1 = memnew(Control);238Control *child_control_2 = memnew(Control);239child_control_1->set_custom_minimum_size(Size2(20, 20));240child_control_2->set_custom_minimum_size(Size2(20, 20));241242grid_container->set_columns(2);243grid_container->add_theme_constant_override("h_separation", 0);244grid_container->add_theme_constant_override("v_separation", 0);245grid_container->set_size(Size2(40, 20));246grid_container->add_child(child_control_1);247grid_container->add_child(child_control_2);248grid_container->set_layout_direction(Control::LAYOUT_DIRECTION_RTL);249SceneTree::get_singleton()->process(0);250251CHECK_MESSAGE(252child_control_1->get_position().is_equal_approx(Point2(20, 0)),253"First child control starts in the right column in RTL mode.");254255CHECK_MESSAGE(256child_control_2->get_position().is_equal_approx(Point2(0, 0)),257"Second child control is placed in the left column in RTL mode.");258259memdelete(child_control_2);260memdelete(child_control_1);261memdelete(grid_container);262}263264} // namespace TestGridContainer265266267