Path: blob/master/tests/scene/test_aspect_ratio_container.cpp
59209 views
/**************************************************************************/1/* test_aspect_ratio_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_aspect_ratio_container)3334#include "scene/gui/aspect_ratio_container.h"35#include "scene/gui/control.h"36#include "scene/main/scene_tree.h"37#include "scene/main/window.h"3839namespace TestAspectRatioContainer {4041TEST_CASE("[SceneTree][AspectRatioContainer] Default Properties") {42AspectRatioContainer *aspect_ratio_container = memnew(AspectRatioContainer);43Window *root = SceneTree::get_singleton()->get_root();44root->add_child(aspect_ratio_container);45Control *child_control = memnew(Control);46aspect_ratio_container->add_child(child_control);4748CHECK_MESSAGE(49aspect_ratio_container->get_stretch_mode() == AspectRatioContainer::STRETCH_FIT,50"AspectRatioContainer stretch mode is set to STRETCH_FIT by default.");5152CHECK_MESSAGE(53Math::is_equal_approx(aspect_ratio_container->get_ratio(), 1.0f),54"AspectRatioContainer ratio is set to 1.0 by default.");5556CHECK_MESSAGE(57child_control->get_position().is_equal_approx(Point2(0, 0)),58"Child control is positioned at the top-left corner of the AspectRatioContainer by default.");5960CHECK_MESSAGE(61child_control->get_h_size_flags() == Control::SIZE_FILL,62"Child control horizontal size flags are set to SIZE_FILL by default.");6364CHECK_MESSAGE(65child_control->get_v_size_flags() == Control::SIZE_FILL,66"Child control vertical size flags are set to SIZE_FILL by default.");6768memdelete(child_control);69memdelete(aspect_ratio_container);70}7172TEST_CASE("[SceneTree][AspectRatioContainer] Aspect Ratio") {73AspectRatioContainer *aspect_ratio_container = memnew(AspectRatioContainer);74Window *root = SceneTree::get_singleton()->get_root();75root->add_child(aspect_ratio_container);76Control *child_control = memnew(Control);77aspect_ratio_container->add_child(child_control);7879aspect_ratio_container->set_size(Size2(200, 100));80SceneTree::get_singleton()->process(0);8182CHECK_MESSAGE(83child_control->get_size().is_equal_approx(Size2(100, 100)),84"Child control is resized to maintain aspect ratio when AspectRatioContainer is resized.");8586aspect_ratio_container->set_size(Size2(100, 200));87SceneTree::get_singleton()->process(0);8889CHECK_MESSAGE(90child_control->get_size().is_equal_approx(Size2(100, 100)),91"Child control is resized to maintain aspect ratio when AspectRatioContainer is resized.");9293aspect_ratio_container->set_ratio(2.0f);94SceneTree::get_singleton()->process(0);9596CHECK_MESSAGE(97child_control->get_size().is_equal_approx(Size2(100, 50)),98"Child control is resized to maintain aspect ratio when AspectRatioContainer ratio is changed.");99100aspect_ratio_container->set_ratio(0.5f);101SceneTree::get_singleton()->process(0);102103CHECK_MESSAGE(104child_control->get_size().is_equal_approx(Size2(100, 200)),105"Child control is resized to maintain aspect ratio when AspectRatioContainer ratio is changed.");106107aspect_ratio_container->set_size(Size2(200, 100));108SceneTree::get_singleton()->process(0);109110CHECK_MESSAGE(111child_control->get_size().is_equal_approx(Size2(50, 100)),112"Child control is resized to maintain aspect ratio when AspectRatioContainer is resized after ratio change.");113114memdelete(child_control);115memdelete(aspect_ratio_container);116}117118TEST_CASE("[SceneTree][AspectRatioContainer] Stretch Modes") {119AspectRatioContainer *aspect_ratio_container = memnew(AspectRatioContainer);120Window *root = SceneTree::get_singleton()->get_root();121root->add_child(aspect_ratio_container);122Control *child_control = memnew(Control);123aspect_ratio_container->add_child(child_control);124125aspect_ratio_container->set_size(Size2(200, 100));126SceneTree::get_singleton()->process(0);127128CHECK_MESSAGE(129child_control->get_size().is_equal_approx(Size2(100, 100)),130"Child control is resized to maintain aspect ratio in STRETCH_FIT mode.");131132aspect_ratio_container->set_stretch_mode(AspectRatioContainer::STRETCH_WIDTH_CONTROLS_HEIGHT);133SceneTree::get_singleton()->process(0);134135CHECK_MESSAGE(136child_control->get_size().is_equal_approx(Size2(200, 200)),137"Child control height is adjusted to maintain aspect ratio in STRETCH_WIDTH_CONTROLS_HEIGHT mode.");138139aspect_ratio_container->set_stretch_mode(AspectRatioContainer::STRETCH_HEIGHT_CONTROLS_WIDTH);140SceneTree::get_singleton()->process(0);141142CHECK_MESSAGE(143child_control->get_size().is_equal_approx(Size2(100, 100)),144"Child control width is adjusted to maintain aspect ratio in STRETCH_HEIGHT_CONTROLS_WIDTH mode.");145146aspect_ratio_container->set_stretch_mode(AspectRatioContainer::STRETCH_COVER);147SceneTree::get_singleton()->process(0);148149CHECK_MESSAGE(150child_control->get_size().is_equal_approx(Size2(200, 200)),151"Child control is resized to cover the entire AspectRatioContainer in STRETCH_COVER mode.");152153aspect_ratio_container->set_size(Size2(100, 200));154aspect_ratio_container->set_stretch_mode(AspectRatioContainer::STRETCH_FIT);155SceneTree::get_singleton()->process(0);156157CHECK_MESSAGE(158aspect_ratio_container->get_stretch_mode() == AspectRatioContainer::STRETCH_FIT,159"AspectRatioContainer stretch mode is set to STRETCH_FIT by default.");160161CHECK_MESSAGE(162child_control->get_size().is_equal_approx(Size2(100, 100)),163"Child control is resized to maintain aspect ratio in STRETCH_FIT mode.");164165aspect_ratio_container->set_stretch_mode(AspectRatioContainer::STRETCH_WIDTH_CONTROLS_HEIGHT);166SceneTree::get_singleton()->process(0);167168CHECK_MESSAGE(169child_control->get_size().is_equal_approx(Size2(100, 100)),170"Child control height is adjusted to maintain aspect ratio in STRETCH_WIDTH_CONTROLS_HEIGHT mode.");171172aspect_ratio_container->set_stretch_mode(AspectRatioContainer::STRETCH_HEIGHT_CONTROLS_WIDTH);173SceneTree::get_singleton()->process(0);174175CHECK_MESSAGE(176child_control->get_size().is_equal_approx(Size2(200, 200)),177"Child control width is adjusted to maintain aspect ratio in STRETCH_HEIGHT_CONTROLS_WIDTH mode.");178179aspect_ratio_container->set_stretch_mode(AspectRatioContainer::STRETCH_COVER);180SceneTree::get_singleton()->process(0);181182CHECK_MESSAGE(183child_control->get_size().is_equal_approx(Size2(200, 200)),184"Child control is resized to cover the entire AspectRatioContainer in STRETCH_COVER mode.");185186memdelete(child_control);187memdelete(aspect_ratio_container);188}189190TEST_CASE("[SceneTree][AspectRatioContainer] Alignment modes") {191AspectRatioContainer *aspect_ratio_container = memnew(AspectRatioContainer);192Window *root = SceneTree::get_singleton()->get_root();193root->add_child(aspect_ratio_container);194Control *child_control = memnew(Control);195aspect_ratio_container->add_child(child_control);196197SUBCASE("Horizontal Alignment") {198aspect_ratio_container->set_size(Size2(200, 100));199SceneTree::get_singleton()->process(0);200201CHECK_MESSAGE(202aspect_ratio_container->get_alignment_horizontal() == AspectRatioContainer::ALIGNMENT_CENTER,203"AspectRatioContainer horizontal alignment is set to ALIGNMENT_CENTER by default.");204205CHECK_MESSAGE(206child_control->get_position().is_equal_approx(Point2(50, 0)),207"Child control is aligned to the center in ALIGNMENT_CENTER horizontal alignment mode.");208209aspect_ratio_container->set_alignment_horizontal(AspectRatioContainer::ALIGNMENT_BEGIN);210SceneTree::get_singleton()->process(0);211212CHECK_MESSAGE(213child_control->get_position().is_equal_approx(Point2(0, 0)),214"Child control is aligned to the left in ALIGNMENT_BEGIN horizontal alignment mode.");215216aspect_ratio_container->set_alignment_horizontal(AspectRatioContainer::ALIGNMENT_END);217SceneTree::get_singleton()->process(0);218219CHECK_MESSAGE(220child_control->get_position().is_equal_approx(Point2(100, 0)),221"Child control is aligned to the right in ALIGNMENT_END horizontal alignment mode.");222}223224SUBCASE("Vertical Alignment") {225aspect_ratio_container->set_size(Size2(100, 200));226SceneTree::get_singleton()->process(0);227228CHECK_MESSAGE(229aspect_ratio_container->get_alignment_vertical() == AspectRatioContainer::ALIGNMENT_CENTER,230"AspectRatioContainer vertical alignment is set to ALIGNMENT_CENTER by default.");231232CHECK_MESSAGE(233child_control->get_position().is_equal_approx(Point2(0, 50)),234"Child control is aligned to the center in ALIGNMENT_CENTER vertical alignment mode.");235236aspect_ratio_container->set_alignment_vertical(AspectRatioContainer::ALIGNMENT_BEGIN);237SceneTree::get_singleton()->process(0);238239CHECK_MESSAGE(240child_control->get_position().is_equal_approx(Point2(0, 0)),241"Child control is aligned to the top in ALIGNMENT_BEGIN vertical alignment mode.");242243aspect_ratio_container->set_alignment_vertical(AspectRatioContainer::ALIGNMENT_END);244SceneTree::get_singleton()->process(0);245246CHECK_MESSAGE(247child_control->get_position().is_equal_approx(Point2(0, 100)),248"Child control is aligned to the bottom in ALIGNMENT_END vertical alignment mode.");249}250251memdelete(child_control);252memdelete(aspect_ratio_container);253}254255TEST_CASE("[SceneTree][AspectRatioContainer] Container Sizing Flags") {256AspectRatioContainer *aspect_ratio_container = memnew(AspectRatioContainer);257Window *root = SceneTree::get_singleton()->get_root();258root->add_child(aspect_ratio_container);259Control *child_control = memnew(Control);260aspect_ratio_container->add_child(child_control);261262SUBCASE("Horizontal Flags") {263aspect_ratio_container->set_size(Size2(200, 100));264SceneTree::get_singleton()->process(0);265266CHECK_MESSAGE(267child_control->get_position().is_equal_approx(Point2(50, 0)),268"Child control is vertically centered by default with SIZE_FILL.");269270child_control->set_h_size_flags(Control::SIZE_SHRINK_BEGIN);271SceneTree::get_singleton()->process(0);272273CHECK_MESSAGE(274child_control->get_position().is_equal_approx(Point2(50, 0)),275"Child control is aligned to the left of its would-be area with SIZE_FILL in SIZE_SHRINK_BEGIN horizontal size flag mode.");276277child_control->set_h_size_flags(Control::SIZE_SHRINK_CENTER);278SceneTree::get_singleton()->process(0);279280CHECK_MESSAGE(281child_control->get_position().is_equal_approx(Point2(100, 0)),282"Child control is centered in its would-be area with SIZE_FILL in SIZE_SHRINK_CENTER horizontal size flag mode.");283284child_control->set_h_size_flags(Control::SIZE_SHRINK_END);285SceneTree::get_singleton()->process(0);286287CHECK_MESSAGE(288child_control->get_position().is_equal_approx(Point2(150, 0)),289"Child control is aligned to the right of its would-be area with SIZE_FILL in SIZE_SHRINK_END horizontal size flag mode.");290}291292SUBCASE("Horizontal Flags RTL") {293aspect_ratio_container->set_size(Size2(200, 100));294aspect_ratio_container->set_layout_direction(Control::LAYOUT_DIRECTION_RTL);295SceneTree::get_singleton()->process(0);296297CHECK_MESSAGE(298child_control->get_position().is_equal_approx(Point2(50, 0)),299"Child control is vertically centered by default with SIZE_FILL.");300301child_control->set_h_size_flags(Control::SIZE_SHRINK_BEGIN);302SceneTree::get_singleton()->process(0);303304CHECK_MESSAGE(305child_control->get_position().is_equal_approx(Point2(150, 0)),306"Child control is aligned to the right of its would-be area with SIZE_FILL in RTL SIZE_SHRINK_BEGIN horizontal size flag mode.");307308child_control->set_h_size_flags(Control::SIZE_SHRINK_CENTER);309SceneTree::get_singleton()->process(0);310311CHECK_MESSAGE(312child_control->get_position().is_equal_approx(Point2(100, 0)),313"Child control is centered in its would-be area with SIZE_FILL in SIZE_SHRINK_CENTER horizontal size flag mode.");314315child_control->set_h_size_flags(Control::SIZE_SHRINK_END);316SceneTree::get_singleton()->process(0);317318CHECK_MESSAGE(319child_control->get_position().is_equal_approx(Point2(50, 0)),320"Child control is aligned to the left of its would-be area with SIZE_FILL in RTL SIZE_SHRINK_END horizontal size flag mode.");321}322323SUBCASE("Vertical Flags") {324aspect_ratio_container->set_size(Size2(100, 200));325SceneTree::get_singleton()->process(0);326327CHECK_MESSAGE(328child_control->get_position().is_equal_approx(Point2(0, 50)),329"Child control is horizontally centered by default with SIZE_FILL.");330331child_control->set_v_size_flags(Control::SIZE_SHRINK_BEGIN);332SceneTree::get_singleton()->process(0);333334CHECK_MESSAGE(335child_control->get_position().is_equal_approx(Point2(0, 50)),336"Child control is aligned to the top of its would-be area with SIZE_FILL in SIZE_SHRINK_BEGIN vertical size flag mode.");337338child_control->set_v_size_flags(Control::SIZE_SHRINK_CENTER);339SceneTree::get_singleton()->process(0);340341CHECK_MESSAGE(342child_control->get_position().is_equal_approx(Point2(0, 100)),343"Child control is centered in its would-be area with SIZE_FILL in SIZE_SHRINK_CENTER vertical size flag mode.");344345child_control->set_v_size_flags(Control::SIZE_SHRINK_END);346SceneTree::get_singleton()->process(0);347348CHECK_MESSAGE(349child_control->get_position().is_equal_approx(Point2(0, 150)),350"Child control is aligned to the bottom of its would-be area with SIZE_FILL in SIZE_SHRINK_END vertical size flag mode.");351}352353memdelete(child_control);354memdelete(aspect_ratio_container);355}356357} // namespace TestAspectRatioContainer358359360