Path: blob/master/tests/scene/test_nine_patch_rect.cpp
59209 views
/**************************************************************************/1/* test_nine_patch_rect.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_nine_patch_rect)3334#include "scene/gui/nine_patch_rect.h"35#include "scene/main/scene_tree.h"36#include "scene/main/window.h"37#include "scene/resources/texture.h"38#include "tests/signal_watcher.h"3940namespace TestNinePatchRect {4142TEST_CASE("[SceneTree][NinePatchRect] Default properties") {43NinePatchRect *nine_patch = memnew(NinePatchRect);4445CHECK(nine_patch->get_texture().is_null());4647CHECK(nine_patch->get_patch_margin(SIDE_LEFT) == 0);48CHECK(nine_patch->get_patch_margin(SIDE_TOP) == 0);49CHECK(nine_patch->get_patch_margin(SIDE_RIGHT) == 0);50CHECK(nine_patch->get_patch_margin(SIDE_BOTTOM) == 0);5152CHECK(nine_patch->get_region_rect() == Rect2(0, 0, 0, 0));53CHECK(nine_patch->is_draw_center_enabled());5455CHECK(nine_patch->get_h_axis_stretch_mode() == NinePatchRect::AXIS_STRETCH_MODE_STRETCH);56CHECK(nine_patch->get_v_axis_stretch_mode() == NinePatchRect::AXIS_STRETCH_MODE_STRETCH);5758// Set in the NinePatchRect constructor, unlike the Control default.59CHECK(nine_patch->get_mouse_filter() == Control::MOUSE_FILTER_IGNORE);6061CHECK(nine_patch->get_combined_minimum_size() == Size2(0, 0));6263memdelete(nine_patch);64}6566TEST_CASE("[SceneTree][NinePatchRect] Set/get texture") {67NinePatchRect *nine_patch = memnew(NinePatchRect);6869Ref<Texture2D> texture;70texture.instantiate();7172nine_patch->set_texture(texture);73CHECK(nine_patch->get_texture() == texture);7475nine_patch->set_texture(Ref<Texture2D>());76CHECK(nine_patch->get_texture().is_null());7778memdelete(nine_patch);79}8081TEST_CASE("[SceneTree][NinePatchRect] texture_changed signal") {82NinePatchRect *nine_patch = memnew(NinePatchRect);8384Ref<Texture2D> texture_a;85texture_a.instantiate();86Ref<Texture2D> texture_b;87texture_b.instantiate();8889SIGNAL_WATCH(nine_patch, "texture_changed");90Array empty_signal_args = { {} };9192SUBCASE("Setting a new texture emits the signal") {93nine_patch->set_texture(texture_a);94SIGNAL_CHECK("texture_changed", empty_signal_args);95}9697SUBCASE("Setting the same texture does not re-emit") {98nine_patch->set_texture(texture_a);99SIGNAL_DISCARD("texture_changed");100101nine_patch->set_texture(texture_a);102SIGNAL_CHECK_FALSE("texture_changed");103}104105SUBCASE("Replacing one texture with another emits the signal") {106nine_patch->set_texture(texture_a);107SIGNAL_DISCARD("texture_changed");108109nine_patch->set_texture(texture_b);110SIGNAL_CHECK("texture_changed", empty_signal_args);111}112113SUBCASE("Clearing the texture emits the signal") {114nine_patch->set_texture(texture_a);115SIGNAL_DISCARD("texture_changed");116117nine_patch->set_texture(Ref<Texture2D>());118SIGNAL_CHECK("texture_changed", empty_signal_args);119}120121SIGNAL_UNWATCH(nine_patch, "texture_changed");122memdelete(nine_patch);123}124125TEST_CASE("[SceneTree][NinePatchRect] Patch margins") {126NinePatchRect *nine_patch = memnew(NinePatchRect);127128SUBCASE("Each side can be set and read independently") {129nine_patch->set_patch_margin(SIDE_LEFT, 5);130nine_patch->set_patch_margin(SIDE_TOP, 10);131nine_patch->set_patch_margin(SIDE_RIGHT, 15);132nine_patch->set_patch_margin(SIDE_BOTTOM, 20);133134CHECK(nine_patch->get_patch_margin(SIDE_LEFT) == 5);135CHECK(nine_patch->get_patch_margin(SIDE_TOP) == 10);136CHECK(nine_patch->get_patch_margin(SIDE_RIGHT) == 15);137CHECK(nine_patch->get_patch_margin(SIDE_BOTTOM) == 20);138}139140SUBCASE("Updating one side does not affect the others") {141nine_patch->set_patch_margin(SIDE_LEFT, 7);142nine_patch->set_patch_margin(SIDE_RIGHT, 9);143144nine_patch->set_patch_margin(SIDE_LEFT, 42);145146CHECK(nine_patch->get_patch_margin(SIDE_LEFT) == 42);147CHECK(nine_patch->get_patch_margin(SIDE_TOP) == 0);148CHECK(nine_patch->get_patch_margin(SIDE_RIGHT) == 9);149CHECK(nine_patch->get_patch_margin(SIDE_BOTTOM) == 0);150}151152memdelete(nine_patch);153}154155TEST_CASE("[SceneTree][NinePatchRect] Patch margins drive minimum size") {156NinePatchRect *nine_patch = memnew(NinePatchRect);157Window *root = SceneTree::get_singleton()->get_root();158root->add_child(nine_patch);159160nine_patch->set_patch_margin(SIDE_LEFT, 10);161nine_patch->set_patch_margin(SIDE_TOP, 20);162nine_patch->set_patch_margin(SIDE_RIGHT, 30);163nine_patch->set_patch_margin(SIDE_BOTTOM, 40);164SceneTree::get_singleton()->process(0);165166// Minimum width is left + right; minimum height is top + bottom.167CHECK(nine_patch->get_combined_minimum_size() == Size2(40, 60));168169nine_patch->set_patch_margin(SIDE_RIGHT, 0);170SceneTree::get_singleton()->process(0);171CHECK(nine_patch->get_combined_minimum_size() == Size2(10, 60));172173memdelete(nine_patch);174}175176TEST_CASE("[SceneTree][NinePatchRect] Patch margin out-of-range index is guarded") {177NinePatchRect *nine_patch = memnew(NinePatchRect);178179nine_patch->set_patch_margin(SIDE_LEFT, 11);180nine_patch->set_patch_margin(SIDE_TOP, 22);181nine_patch->set_patch_margin(SIDE_RIGHT, 33);182nine_patch->set_patch_margin(SIDE_BOTTOM, 44);183184ERR_PRINT_OFF;185nine_patch->set_patch_margin((Side)4, 999);186const int out_of_range_value = nine_patch->get_patch_margin((Side)4);187ERR_PRINT_ON;188189CHECK(out_of_range_value == 0);190CHECK(nine_patch->get_patch_margin(SIDE_LEFT) == 11);191CHECK(nine_patch->get_patch_margin(SIDE_TOP) == 22);192CHECK(nine_patch->get_patch_margin(SIDE_RIGHT) == 33);193CHECK(nine_patch->get_patch_margin(SIDE_BOTTOM) == 44);194195memdelete(nine_patch);196}197198TEST_CASE("[SceneTree][NinePatchRect] Region rect and draw center") {199NinePatchRect *nine_patch = memnew(NinePatchRect);200201SUBCASE("Region rect is set and retrieved") {202const Rect2 region(8, 16, 32, 64);203nine_patch->set_region_rect(region);204CHECK(nine_patch->get_region_rect() == region);205}206207SUBCASE("Draw center can be toggled") {208nine_patch->set_draw_center(false);209CHECK_FALSE(nine_patch->is_draw_center_enabled());210211nine_patch->set_draw_center(true);212CHECK(nine_patch->is_draw_center_enabled());213}214215memdelete(nine_patch);216}217218TEST_CASE("[SceneTree][NinePatchRect] Axis stretch modes") {219NinePatchRect *nine_patch = memnew(NinePatchRect);220221SUBCASE("Horizontal stretch mode accepts each value") {222nine_patch->set_h_axis_stretch_mode(NinePatchRect::AXIS_STRETCH_MODE_TILE);223CHECK(nine_patch->get_h_axis_stretch_mode() == NinePatchRect::AXIS_STRETCH_MODE_TILE);224225nine_patch->set_h_axis_stretch_mode(NinePatchRect::AXIS_STRETCH_MODE_TILE_FIT);226CHECK(nine_patch->get_h_axis_stretch_mode() == NinePatchRect::AXIS_STRETCH_MODE_TILE_FIT);227228nine_patch->set_h_axis_stretch_mode(NinePatchRect::AXIS_STRETCH_MODE_STRETCH);229CHECK(nine_patch->get_h_axis_stretch_mode() == NinePatchRect::AXIS_STRETCH_MODE_STRETCH);230}231232SUBCASE("Vertical stretch mode accepts each value") {233nine_patch->set_v_axis_stretch_mode(NinePatchRect::AXIS_STRETCH_MODE_TILE);234CHECK(nine_patch->get_v_axis_stretch_mode() == NinePatchRect::AXIS_STRETCH_MODE_TILE);235236nine_patch->set_v_axis_stretch_mode(NinePatchRect::AXIS_STRETCH_MODE_TILE_FIT);237CHECK(nine_patch->get_v_axis_stretch_mode() == NinePatchRect::AXIS_STRETCH_MODE_TILE_FIT);238239nine_patch->set_v_axis_stretch_mode(NinePatchRect::AXIS_STRETCH_MODE_STRETCH);240CHECK(nine_patch->get_v_axis_stretch_mode() == NinePatchRect::AXIS_STRETCH_MODE_STRETCH);241}242243SUBCASE("Horizontal and vertical stretch modes are independent") {244nine_patch->set_h_axis_stretch_mode(NinePatchRect::AXIS_STRETCH_MODE_TILE);245nine_patch->set_v_axis_stretch_mode(NinePatchRect::AXIS_STRETCH_MODE_TILE_FIT);246247CHECK(nine_patch->get_h_axis_stretch_mode() == NinePatchRect::AXIS_STRETCH_MODE_TILE);248CHECK(nine_patch->get_v_axis_stretch_mode() == NinePatchRect::AXIS_STRETCH_MODE_TILE_FIT);249}250251memdelete(nine_patch);252}253254} // namespace TestNinePatchRect255256257