Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

Add desired size tests

Enzo Novoselicafe6ce21000f
diff --git a/tests/scene/mock_control.h b/tests/scene/mock_control.h
new file mode 100644
index 00000000000..9b1c57978d3
--- /dev/null
+++ b/tests/scene/mock_control.h
@@ -0,0 +1,62 @@
+/**************************************************************************/
+/* mock_control.h */
+/**************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/**************************************************************************/
+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/**************************************************************************/
+
+#pragma once
+
+#include "scene/gui/control.h"
+
+// Mock Control used for testing size related functionality.
+// The minimum and desired sizes can be set manually for testing various layouts.
+class MockControl : public Control {
+ GDSOFTCLASS(MockControl, Control);
+
+private:
+ Size2 minimum_size;
+ Size2 desired_size;
+
+public:
+ Size2 get_minimum_size() const override {
+ return minimum_size;
+ }
+
+ Size2 get_desired_size() const override {
+ return desired_size;
+ }
+
+ void set_minimum_size(Size2 p_size) {
+ minimum_size = p_size;
+ update_minimum_size();
+ }
+
+ void set_desired_size(Size2 p_size) {
+ desired_size = p_size;
+ update_desired_size();
+ }
+};
diff --git a/tests/scene/test_box_container.cpp b/tests/scene/test_box_container.cpp
index 8f086e7e0f4..22d9f8ec9cd 100644
--- a/tests/scene/test_box_container.cpp
+++ b/tests/scene/test_box_container.cpp
@@ -36,6 +36,7 @@ TEST_FORCE_LINK(test_box_container)
#include "scene/gui/control.h"
#include "scene/main/scene_tree.h"
#include "scene/main/window.h"
+#include "tests/scene/mock_control.h"
namespace TestBoxContainer {
@@ -341,6 +342,132 @@ TEST_CASE("[SceneTree][BoxContainer] HBoxContainer") {
memdelete(child_control_2);
}
+ SUBCASE("Desired size children") {
+ MockControl *child_control_1 = memnew(MockControl);
+ MockControl *child_control_2 = memnew(MockControl);
+ hbox_container->add_child(child_control_1);
+ hbox_container->add_child(child_control_2);
+
+ child_control_1->set_desired_size(Size2(50, 0));
+ child_control_2->set_desired_size(Size2(50, 0));
+
+ // Set a custom maximum size to ensure the container does not expand to accommodate children in "a + b > c" case at first.
+ // It is also necessary to set max size propagation to false to prevent children's bound desired sizes from being capped in "a + b > c" case.
+ hbox_container->set_size(Size2());
+ hbox_container->set_custom_maximum_size(Size2(100, 100));
+ hbox_container->set_propagate_maximum_size(false);
+ hbox_container->add_theme_constant_override("separation", 0);
+ SceneTree::get_singleton()->process(0);
+
+ CHECK_MESSAGE(
+ hbox_container->get_size().is_equal_approx(Size2(100, 0)),
+ "Container expands to accommodate children up to its maximum size. Case: a + b = c");
+
+ CHECK_MESSAGE(
+ (child_control_1->get_size().is_equal_approx(Size2(50, 0)) && child_control_2->get_size().is_equal_approx(Size2(50, 0))),
+ "Children control with equal desired sizes take equal space. Case: a + b = c");
+
+ hbox_container->set_size(Size2());
+ child_control_1->set_desired_size(Size2(200, 0));
+ child_control_2->set_desired_size(Size2(200, 0));
+ SceneTree::get_singleton()->process(0);
+
+ CHECK_MESSAGE(
+ hbox_container->get_size().is_equal_approx(Size2(100, 0)),
+ "Container expands to accommodate children up to its maximum size. Case: a + b > c");
+
+ CHECK_MESSAGE(
+ (child_control_1->get_size().is_equal_approx(Size2(50, 0)) && child_control_2->get_size().is_equal_approx(Size2(50, 0))),
+ "Children control with equal desired sizes take equal space. Case: a + b > c");
+
+ hbox_container->set_size(Size2());
+ child_control_1->set_desired_size(Size2(10, 0));
+ child_control_2->set_desired_size(Size2(10, 0));
+ SceneTree::get_singleton()->process(0);
+
+ CHECK_MESSAGE(
+ hbox_container->get_size().is_equal_approx(Size2(20, 0)),
+ "Container only expands as much as needed to accommodate children. Case: a + b < c");
+
+ CHECK_MESSAGE(
+ (child_control_1->get_size().is_equal_approx(Size2(10, 0)) && child_control_2->get_size().is_equal_approx(Size2(10, 0))),
+ "Children control with equal desired sizes take equal space. Case: a + b < c");
+
+ hbox_container->set_size(Size2());
+ child_control_1->set_desired_size(Size2(75, 0));
+ child_control_2->set_desired_size(Size2(25, 0));
+ SceneTree::get_singleton()->process(0);
+
+ CHECK_MESSAGE(
+ hbox_container->get_size().is_equal_approx(Size2(100, 0)),
+ "Container expands to accommodate children up to its maximum size. Case: a + b = c");
+
+ CHECK_MESSAGE(
+ (child_control_1->get_size().is_equal_approx(Size2(75, 0)) && child_control_2->get_size().is_equal_approx(Size2(25, 0))),
+ "Children control with different desired sizes take different space. Case: a + b = c");
+
+ hbox_container->set_size(Size2());
+ child_control_1->set_desired_size(Size2(300, 0));
+ child_control_2->set_desired_size(Size2(100, 0));
+ SceneTree::get_singleton()->process(0);
+
+ CHECK_MESSAGE(
+ hbox_container->get_size().is_equal_approx(Size2(100, 0)),
+ "Container expands to accommodate children up to its maximum size. Case: a + b > c");
+
+ CHECK_MESSAGE(
+ (child_control_1->get_size().is_equal_approx(Size2(75, 0)) && child_control_2->get_size().is_equal_approx(Size2(25, 0))),
+ "Children control with different desired sizes take different space. Case: a + b > c");
+
+ hbox_container->set_size(Size2());
+ child_control_1->set_desired_size(Size2(40, 0));
+ child_control_2->set_desired_size(Size2(20, 0));
+ SceneTree::get_singleton()->process(0);
+
+ CHECK_MESSAGE(
+ hbox_container->get_size().is_equal_approx(Size2(60, 0)),
+ "Container only expands as much as needed to accommodate children. Case: a + b < c");
+
+ CHECK_MESSAGE(
+ (child_control_1->get_size().is_equal_approx(Size2(40, 0)) && child_control_2->get_size().is_equal_approx(Size2(20, 0))),
+ "Children control with different desired sizes take different space. Case: a + b < c");
+
+ memdelete(child_control_1);
+ memdelete(child_control_2);
+ }
+
+ SUBCASE("Desired size takes priority over expanding children") {
+ MockControl *child_control_1 = memnew(MockControl);
+ MockControl *child_control_2 = memnew(MockControl);
+ Control *child_control_3 = memnew(Control);
+ hbox_container->add_child(child_control_1);
+ hbox_container->add_child(child_control_2);
+ hbox_container->add_child(child_control_3);
+
+ child_control_1->set_desired_size(Size2(50, 0));
+ child_control_2->set_desired_size(Size2(50, 0));
+ child_control_3->set_h_size_flags(Control::SIZE_EXPAND_FILL);
+
+ hbox_container->set_size(Size2());
+ hbox_container->set_custom_maximum_size(Size2(100, 100));
+ hbox_container->set_propagate_maximum_size(false);
+ hbox_container->add_theme_constant_override("separation", 0);
+ SceneTree::get_singleton()->process(0);
+
+ CHECK_MESSAGE(
+ hbox_container->get_size().is_equal_approx(Size2(100, 0)),
+ "Container desired sizes take priority over expanding children.");
+ CHECK_MESSAGE(
+ (child_control_1->get_size().is_equal_approx(Size2(50, 0)) &&
+ child_control_2->get_size().is_equal_approx(Size2(50, 0)) &&
+ child_control_3->get_size().is_equal_approx(Size2(0, 0))),
+ "Expanding children do not take space from children with desired sizes.");
+
+ memdelete(child_control_3);
+ memdelete(child_control_2);
+ memdelete(child_control_1);
+ }
+
memdelete(hbox_container);
}
@@ -619,6 +746,132 @@ TEST_CASE("[SceneTree][BoxContainer] VBoxContainer") {
memdelete(child_control_2);
}
+ SUBCASE("Desired size children") {
+ MockControl *child_control_1 = memnew(MockControl);
+ MockControl *child_control_2 = memnew(MockControl);
+ vbox_container->add_child(child_control_1);
+ vbox_container->add_child(child_control_2);
+
+ child_control_1->set_desired_size(Size2(0, 50));
+ child_control_2->set_desired_size(Size2(0, 50));
+
+ // Set a custom maximum size to ensure the container does not expand to accommodate children in "a + b > c" case at first.
+ // It is also necessary to set max size propagation to false to prevent children's bound desired sizes from being capped in "a + b > c" case.
+ vbox_container->set_size(Size2());
+ vbox_container->set_custom_maximum_size(Size2(100, 100));
+ vbox_container->set_propagate_maximum_size(false);
+ vbox_container->add_theme_constant_override("separation", 0);
+ SceneTree::get_singleton()->process(0);
+
+ CHECK_MESSAGE(
+ vbox_container->get_size().is_equal_approx(Size2(0, 100)),
+ "Container expands to accommodate children up to its maximum size. Case: a + b = c");
+
+ CHECK_MESSAGE(
+ (child_control_1->get_size().is_equal_approx(Size2(0, 50)) && child_control_2->get_size().is_equal_approx(Size2(0, 50))),
+ "Children control with equal desired sizes take equal space. Case: a + b = c");
+
+ vbox_container->set_size(Size2());
+ child_control_1->set_desired_size(Size2(0, 200));
+ child_control_2->set_desired_size(Size2(0, 200));
+ SceneTree::get_singleton()->process(0);
+
+ CHECK_MESSAGE(
+ vbox_container->get_size().is_equal_approx(Size2(0, 100)),
+ "Container expands to accommodate children up to its maximum size. Case: a + b > c");
+
+ CHECK_MESSAGE(
+ (child_control_1->get_size().is_equal_approx(Size2(0, 50)) && child_control_2->get_size().is_equal_approx(Size2(0, 50))),
+ "Children control with equal desired sizes take equal space. Case: a + b > c");
+
+ vbox_container->set_size(Size2());
+ child_control_1->set_desired_size(Size2(0, 10));
+ child_control_2->set_desired_size(Size2(0, 10));
+ SceneTree::get_singleton()->process(0);
+
+ CHECK_MESSAGE(
+ vbox_container->get_size().is_equal_approx(Size2(0, 20)),
+ "Container only expands as much as needed to accommodate children. Case: a + b < c");
+
+ CHECK_MESSAGE(
+ (child_control_1->get_size().is_equal_approx(Size2(0, 10)) && child_control_2->get_size().is_equal_approx(Size2(0, 10))),
+ "Children control with equal desired sizes take equal space. Case: a + b < c");
+
+ vbox_container->set_size(Size2());
+ child_control_1->set_desired_size(Size2(0, 75));
+ child_control_2->set_desired_size(Size2(0, 25));
+ SceneTree::get_singleton()->process(0);
+
+ CHECK_MESSAGE(
+ vbox_container->get_size().is_equal_approx(Size2(0, 100)),
+ "Container expands to accommodate children up to its maximum size. Case: a + b = c");
+
+ CHECK_MESSAGE(
+ (child_control_1->get_size().is_equal_approx(Size2(0, 75)) && child_control_2->get_size().is_equal_approx(Size2(0, 25))),
+ "Children control with different desired sizes take different space. Case: a + b = c");
+
+ vbox_container->set_size(Size2());
+ child_control_1->set_desired_size(Size2(0, 300));
+ child_control_2->set_desired_size(Size2(0, 100));
+ SceneTree::get_singleton()->process(0);
+
+ CHECK_MESSAGE(
+ vbox_container->get_size().is_equal_approx(Size2(0, 100)),
+ "Container expands to accommodate children up to its maximum size. Case: a + b > c");
+
+ CHECK_MESSAGE(
+ (child_control_1->get_size().is_equal_approx(Size2(0, 75)) && child_control_2->get_size().is_equal_approx(Size2(0, 25))),
+ "Children control with different desired sizes take different space. Case: a + b > c");
+
+ vbox_container->set_size(Size2());
+ child_control_1->set_desired_size(Size2(0, 40));
+ child_control_2->set_desired_size(Size2(0, 20));
+ SceneTree::get_singleton()->process(0);
+
+ CHECK_MESSAGE(
+ vbox_container->get_size().is_equal_approx(Size2(0, 60)),
+ "Container only expands as much as needed to accommodate children. Case: a + b < c");
+
+ CHECK_MESSAGE(
+ (child_control_1->get_size().is_equal_approx(Size2(0, 40)) && child_control_2->get_size().is_equal_approx(Size2(0, 20))),
+ "Children control with different desired sizes take different space. Case: a + b < c");
+
+ memdelete(child_control_1);
+ memdelete(child_control_2);
+ }
+
+ SUBCASE("Desired size takes priority over expanding children") {
+ MockControl *child_control_1 = memnew(MockControl);
+ MockControl *child_control_2 = memnew(MockControl);
+ Control *child_control_3 = memnew(Control);
+ vbox_container->add_child(child_control_1);
+ vbox_container->add_child(child_control_2);
+ vbox_container->add_child(child_control_3);
+
+ child_control_1->set_desired_size(Size2(0, 50));
+ child_control_2->set_desired_size(Size2(0, 50));
+ child_control_3->set_v_size_flags(Control::SIZE_EXPAND_FILL);
+
+ vbox_container->set_size(Size2());
+ vbox_container->set_custom_maximum_size(Size2(100, 100));
+ vbox_container->set_propagate_maximum_size(false);
+ vbox_container->add_theme_constant_override("separation", 0);
+ SceneTree::get_singleton()->process(0);
+
+ CHECK_MESSAGE(
+ vbox_container->get_size().is_equal_approx(Size2(0, 100)),
+ "Container desired sizes take priority over expanding children.");
+ CHECK_MESSAGE(
+ (child_control_1->get_size().is_equal_approx(Size2(0, 50)) &&
+ child_control_2->get_size().is_equal_approx(Size2(0, 50)) &&
+ child_control_3->get_size().is_equal_approx(Size2(0, 0))),
+ "Expanding children do not take space from children with desired sizes.");
+
+ memdelete(child_control_3);
+ memdelete(child_control_2);
+ memdelete(child_control_1);
+ }
+
memdelete(vbox_container);
}
diff --git a/tests/scene/test_flow_container.cpp b/tests/scene/test_flow_container.cpp
index 3131b3b61a9..03c395445c8 100644
--- a/tests/scene/test_flow_container.cpp
+++ b/tests/scene/test_flow_container.cpp
@@ -36,6 +36,7 @@ TEST_FORCE_LINK(test_flow_container)
#include "scene/gui/flow_container.h"
#include "scene/main/scene_tree.h"
#include "scene/main/window.h"
+#include "tests/scene/mock_control.h"
namespace TestFlowContainer {
@@ -245,6 +246,37 @@ TEST_CASE("[SceneTree][FlowContainer] HFlowContainer") {
memdelete(child_control_1);
}
+ SUBCASE("Desired size children") {
+ MockControl *child_control_1 = memnew(MockControl);
+ MockControl *child_control_2 = memnew(MockControl);
+ child_control_1->set_desired_size(Size2(60, 20));
+ child_control_2->set_desired_size(Size2(40, 30));
+ hflow_container->set_size(Size2());
+ hflow_container->set_custom_maximum_size(Size2(100, 100));
+ hflow_container->set_propagate_maximum_size(false);
+ hflow_container->add_child(child_control_1);
+ hflow_container->add_child(child_control_2);
+ SceneTree::get_singleton()->process(0);
+
+ CHECK_MESSAGE(
+ hflow_container->get_size().is_equal_approx(Size2(60, 50)),
+ "HFlowContainer expands to fit desired child sizes while respecting its maximum size.");
+ CHECK_MESSAGE(
+ hflow_container->get_line_count() == 2,
+ "HFlowContainer wraps desired-size children when their combined width does not fit.");
+ CHECK_MESSAGE(
+ (child_control_1->get_position().is_equal_approx(Point2(0, 0)) &&
+ child_control_2->get_position().is_equal_approx(Point2(0, 20))),
+ "Desired-size children are placed on consecutive wrapped lines.");
+ CHECK_MESSAGE(
+ (child_control_1->get_size().is_equal_approx(Size2(60, 20)) &&
+ child_control_2->get_size().is_equal_approx(Size2(40, 30))),
+ "Desired-size children keep their desired dimensions on separate lines.");
+
+ memdelete(child_control_2);
+ memdelete(child_control_1);
+ }
+
SUBCASE("Reverse fill") {
Control *child_control_1 = memnew(Control);
Control *child_control_2 = memnew(Control);
@@ -510,6 +542,37 @@ TEST_CASE("[SceneTree][FlowContainer] VFlowContainer") {
memdelete(child_control_1);
}
+ SUBCASE("Desired size children") {
+ MockControl *child_control_1 = memnew(MockControl);
+ MockControl *child_control_2 = memnew(MockControl);
+ child_control_1->set_desired_size(Size2(20, 60));
+ child_control_2->set_desired_size(Size2(30, 40));
+ vflow_container->set_size(Size2());
+ vflow_container->set_custom_maximum_size(Size2(100, 100));
+ vflow_container->set_propagate_maximum_size(false);
+ vflow_container->add_child(child_control_1);
+ vflow_container->add_child(child_control_2);
+ SceneTree::get_singleton()->process(0);
+
+ CHECK_MESSAGE(
+ vflow_container->get_size().is_equal_approx(Size2(50, 60)),
+ "VFlowContainer expands to fit desired child sizes while respecting its maximum size.");
+ CHECK_MESSAGE(
+ vflow_container->get_line_count() == 2,
+ "VFlowContainer wraps desired-size children when their combined height does not fit.");
+ CHECK_MESSAGE(
+ (child_control_1->get_position().is_equal_approx(Point2(0, 0)) &&
+ child_control_2->get_position().is_equal_approx(Point2(20, 0))),
+ "Desired-size children are placed in consecutive wrapped columns.");
+ CHECK_MESSAGE(
+ (child_control_1->get_size().is_equal_approx(Size2(20, 60)) &&
+ child_control_2->get_size().is_equal_approx(Size2(30, 40))),
+ "Desired-size children keep their desired dimensions in separate columns.");
+
+ memdelete(child_control_2);
+ memdelete(child_control_1);
+ }
+
SUBCASE("Reverse fill") {
Control *child_control_1 = memnew(Control);
Control *child_control_2 = memnew(Control);
diff --git a/tests/scene/test_grid_container.cpp b/tests/scene/test_grid_container.cpp
index a37481fceb4..2c0e3c98411 100644
--- a/tests/scene/test_grid_container.cpp
+++ b/tests/scene/test_grid_container.cpp
@@ -36,6 +36,7 @@ TEST_FORCE_LINK(test_grid_container)
#include "scene/gui/grid_container.h"
#include "scene/main/scene_tree.h"
#include "scene/main/window.h"
+#include "tests/scene/mock_control.h"
namespace TestGridContainer {
@@ -230,6 +231,130 @@ TEST_CASE("[SceneTree][GridContainer] Expanding rows and columns") {
memdelete(grid_container);
}
+TEST_CASE("[SceneTree][GridContainer] Desired size children") {
+ GridContainer *grid_container = memnew(GridContainer);
+ Window *root = SceneTree::get_singleton()->get_root();
+ root->add_child(grid_container);
+
+ MockControl *top_left = memnew(MockControl);
+ Control *top_right = memnew(Control);
+ Control *bottom_left = memnew(Control);
+ MockControl *bottom_right = memnew(MockControl);
+ grid_container->set_columns(2);
+ grid_container->add_theme_constant_override("h_separation", 0);
+ grid_container->add_theme_constant_override("v_separation", 0);
+ grid_container->set_custom_maximum_size(Size2(100, 100));
+ grid_container->set_propagate_maximum_size(false);
+ grid_container->add_child(top_left);
+ grid_container->add_child(top_right);
+ grid_container->add_child(bottom_left);
+ grid_container->add_child(bottom_right);
+
+ top_right->set_h_size_flags(Control::SIZE_EXPAND_FILL);
+ top_right->set_v_size_flags(Control::SIZE_EXPAND_FILL);
+ bottom_left->set_h_size_flags(Control::SIZE_EXPAND_FILL);
+ bottom_left->set_v_size_flags(Control::SIZE_EXPAND_FILL);
+ top_left->set_desired_size(Size2(50, 50));
+ bottom_right->set_desired_size(Size2(50, 50));
+ grid_container->set_size(Size2());
+ SceneTree::get_singleton()->process(0);
+
+ CHECK_MESSAGE(
+ grid_container->get_size().is_equal_approx(Size2(100, 100)),
+ "GridContainer expands to accommodate desired sizes up to its maximum size. Case: a + b = c");
+ CHECK_MESSAGE(
+ (top_left->get_size().is_equal_approx(Size2(50, 50)) &&
+ top_right->get_size().is_equal_approx(Size2(50, 50)) &&
+ bottom_left->get_size().is_equal_approx(Size2(50, 50)) &&
+ bottom_right->get_size().is_equal_approx(Size2(50, 50))),
+ "GridContainer splits desired sizes equally across rows and columns. Case: a + b = c");
+
+ top_right->set_h_size_flags(Control::SIZE_FILL);
+ top_right->set_v_size_flags(Control::SIZE_FILL);
+ bottom_left->set_h_size_flags(Control::SIZE_FILL);
+ bottom_left->set_v_size_flags(Control::SIZE_FILL);
+ top_left->set_desired_size(Size2(200, 200));
+ bottom_right->set_desired_size(Size2(200, 200));
+ grid_container->set_size(Size2());
+ SceneTree::get_singleton()->process(0);
+
+ CHECK_MESSAGE(
+ grid_container->get_size().is_equal_approx(Size2(100, 100)),
+ "GridContainer expands to accommodate desired sizes up to its maximum size. Case: a + b > c");
+ CHECK_MESSAGE(
+ (top_left->get_size().is_equal_approx(Size2(50, 50)) &&
+ top_right->get_size().is_equal_approx(Size2(50, 50)) &&
+ bottom_left->get_size().is_equal_approx(Size2(50, 50)) &&
+ bottom_right->get_size().is_equal_approx(Size2(50, 50))),
+ "GridContainer splits space equally across rows and columns. Case: a + b > c");
+
+ top_left->set_desired_size(Size2(10, 10));
+ bottom_right->set_desired_size(Size2(10, 10));
+ grid_container->set_size(Size2());
+ SceneTree::get_singleton()->process(0);
+
+ CHECK_MESSAGE(
+ grid_container->get_size().is_equal_approx(Size2(20, 20)),
+ "GridContainer only expands as much as needed to accommodate desired sizes. Case: a + b < c");
+ CHECK_MESSAGE(
+ (top_left->get_size().is_equal_approx(Size2(10, 10)) &&
+ top_right->get_size().is_equal_approx(Size2(10, 10)) &&
+ bottom_left->get_size().is_equal_approx(Size2(10, 10)) &&
+ bottom_right->get_size().is_equal_approx(Size2(10, 10))),
+ "GridContainer splits desired sizes equally across rows and columns. Case: a + b < c");
+
+ top_left->set_desired_size(Size2(75, 75));
+ bottom_right->set_desired_size(Size2(25, 25));
+ grid_container->set_size(Size2());
+ SceneTree::get_singleton()->process(0);
+
+ CHECK_MESSAGE(
+ grid_container->get_size().is_equal_approx(Size2(100, 100)),
+ "GridContainer expands to accommodate desired sizes up to its maximum size. Case: a + b = c");
+ CHECK_MESSAGE(
+ (top_left->get_size().is_equal_approx(Size2(75, 75)) &&
+ top_right->get_size().is_equal_approx(Size2(25, 75)) &&
+ bottom_left->get_size().is_equal_approx(Size2(75, 25)) &&
+ bottom_right->get_size().is_equal_approx(Size2(25, 25))),
+ "GridContainer splits different desired sizes across rows and columns. Case: a + b = c");
+
+ top_left->set_desired_size(Size2(300, 300));
+ bottom_right->set_desired_size(Size2(100, 100));
+ grid_container->set_size(Size2());
+ SceneTree::get_singleton()->process(0);
+
+ CHECK_MESSAGE(
+ grid_container->get_size().is_equal_approx(Size2(100, 100)),
+ "GridContainer expands to accommodate desired sizes up to its maximum size. Case: a + b > c");
+ CHECK_MESSAGE(
+ (top_left->get_size().is_equal_approx(Size2(75, 75)) &&
+ top_right->get_size().is_equal_approx(Size2(25, 75)) &&
+ bottom_left->get_size().is_equal_approx(Size2(75, 25)) &&
+ bottom_right->get_size().is_equal_approx(Size2(25, 25))),
+ "GridContainer splits different desired sizes across rows and columns. Case: a + b > c");
+
+ top_left->set_desired_size(Size2(40, 40));
+ bottom_right->set_desired_size(Size2(20, 20));
+ grid_container->set_size(Size2());
+ SceneTree::get_singleton()->process(0);
+
+ CHECK_MESSAGE(
+ grid_container->get_size().is_equal_approx(Size2(60, 60)),
+ "GridContainer only expands as much as needed to accommodate desired sizes. Case: a + b < c");
+ CHECK_MESSAGE(
+ (top_left->get_size().is_equal_approx(Size2(40, 40)) &&
+ top_right->get_size().is_equal_approx(Size2(20, 40)) &&
+ bottom_left->get_size().is_equal_approx(Size2(40, 20)) &&
+ bottom_right->get_size().is_equal_approx(Size2(20, 20))),
+ "GridContainer splits different desired sizes across rows and columns. Case: a + b < c");
+
+ memdelete(bottom_right);
+ memdelete(bottom_left);
+ memdelete(top_right);
+ memdelete(top_left);
+ memdelete(grid_container);
+}
+
TEST_CASE("[SceneTree][GridContainer] RTL placement") {
GridContainer *grid_container = memnew(GridContainer);
Window *root = SceneTree::get_singleton()->get_root();
diff --git a/tests/scene/test_label.cpp b/tests/scene/test_label.cpp
index 24e617d4a7a..eb895ca5ba6 100644
--- a/tests/scene/test_label.cpp
+++ b/tests/scene/test_label.cpp
@@ -146,6 +146,8 @@ TEST_CASE("[SceneTree][Label] Sizing") {
test_label->set_text("This is a long text that should be wrapped and exceeds minimum size.");
SceneTree::get_singleton()->process(0);
+ real_t label_height = test_label->get_size().height;
+
CHECK_MESSAGE(
test_label->get_size().width <= max_width,
"Label width will increase up to the custom maximum width with AUTOWRAP_OFF.");
@@ -162,6 +164,9 @@ TEST_CASE("[SceneTree][Label] Sizing") {
CHECK_MESSAGE(
test_label->get_size().width > min_width,
"Label width will increase beyond the custom minimum width with AUTOWRAP_ARBITRARY.");
+ CHECK_MESSAGE(
+ test_label->get_size().height > label_height,
+ "Label height will increase with AUTOWRAP_ARBITRARY.");
test_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD);
SceneTree::get_singleton()->process(0);
@@ -172,6 +177,9 @@ TEST_CASE("[SceneTree][Label] Sizing") {
CHECK_MESSAGE(
test_label->get_size().width > min_width,
"Label width will increase beyond the custom minimum width with AUTOWRAP_WORD.");
+ CHECK_MESSAGE(
+ test_label->get_size().height > label_height,
+ "Label height will increase with AUTOWRAP_WORD.");
test_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);
SceneTree::get_singleton()->process(0);
@@ -182,6 +190,9 @@ TEST_CASE("[SceneTree][Label] Sizing") {
CHECK_MESSAGE(
test_label->get_size().width > min_width,
"Label width will increase beyond the custom minimum width with AUTOWRAP_WORD_SMART.");
+ CHECK_MESSAGE(
+ test_label->get_size().height > label_height,
+ "Label height will increase with AUTOWRAP_WORD_SMART.");
test_label->set_autowrap_mode(TextServer::AUTOWRAP_OFF);
test_label->set_clip_text(true);
@@ -193,6 +204,9 @@ TEST_CASE("[SceneTree][Label] Sizing") {
CHECK_MESSAGE(
test_label->get_size().width > min_width,
"Label width will increase beyond the custom minimum width with clip_text.");
+ CHECK_MESSAGE(
+ test_label->get_size().height == label_height,
+ "Label height will not increase with clip_text.");
test_label->set_clip_text(false);
test_label->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_CHAR);
@@ -204,6 +218,9 @@ TEST_CASE("[SceneTree][Label] Sizing") {
CHECK_MESSAGE(
test_label->get_size().width > min_width,
"Label width will increase beyond the custom minimum width with OVERRUN_TRIM_CHAR.");
+ CHECK_MESSAGE(
+ test_label->get_size().height == label_height,
+ "Label height will not increase with OVERRUN_TRIM_CHAR.");
test_label->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_WORD);
SceneTree::get_singleton()->process(0);
@@ -214,6 +231,9 @@ TEST_CASE("[SceneTree][Label] Sizing") {
CHECK_MESSAGE(
test_label->get_size().width > min_width,
"Label width will increase beyond the custom minimum width with OVERRUN_TRIM_WORD.");
+ CHECK_MESSAGE(
+ test_label->get_size().height == label_height,
+ "Label height will not increase with OVERRUN_TRIM_WORD.");
test_label->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
SceneTree::get_singleton()->process(0);
@@ -224,6 +244,9 @@ TEST_CASE("[SceneTree][Label] Sizing") {
CHECK_MESSAGE(
test_label->get_size().width > min_width,
"Label width will increase beyond the custom minimum width with OVERRUN_TRIM_ELLIPSIS.");
+ CHECK_MESSAGE(
+ test_label->get_size().height == label_height,
+ "Label height will not increase with OVERRUN_TRIM_ELLIPSIS.");
test_label->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_WORD_ELLIPSIS);
SceneTree::get_singleton()->process(0);
@@ -234,6 +257,9 @@ TEST_CASE("[SceneTree][Label] Sizing") {
CHECK_MESSAGE(
test_label->get_size().width > min_width,
"Label width will increase beyond the custom minimum width with OVERRUN_TRIM_WORD_ELLIPSIS.");
+ CHECK_MESSAGE(
+ test_label->get_size().height == label_height,
+ "Label height will not increase with OVERRUN_TRIM_WORD_ELLIPSIS.");
test_label->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS_FORCE);
SceneTree::get_singleton()->process(0);
@@ -244,6 +270,9 @@ TEST_CASE("[SceneTree][Label] Sizing") {
CHECK_MESSAGE(
test_label->get_size().width > min_width,
"Label width will increase beyond the custom minimum width with OVERRUN_TRIM_ELLIPSIS_FORCE.");
+ CHECK_MESSAGE(
+ test_label->get_size().height == label_height,
+ "Label height will not increase with OVERRUN_TRIM_ELLIPSIS_FORCE.");
test_label->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_WORD_ELLIPSIS_FORCE);
SceneTree::get_singleton()->process(0);
@@ -254,6 +283,9 @@ TEST_CASE("[SceneTree][Label] Sizing") {
CHECK_MESSAGE(
test_label->get_size().width > min_width,
"Label width will increase beyond the custom minimum width with OVERRUN_TRIM_WORD_ELLIPSIS_FORCE.");
+ CHECK_MESSAGE(
+ test_label->get_size().height == label_height,
+ "Label height will not increase with OVERRUN_TRIM_WORD_ELLIPSIS_FORCE.");
memdelete(test_label);
}
diff --git a/tests/scene/test_tab_bar.cpp b/tests/scene/test_tab_bar.cpp
index 99ae8774180..ed93f8977e7 100644
--- a/tests/scene/test_tab_bar.cpp
+++ b/tests/scene/test_tab_bar.cpp
@@ -1089,6 +1089,63 @@ TEST_CASE("[SceneTree][TabBar] layout and offset") {
memdelete(tab_bar);
}
+TEST_CASE("[SceneTree][TabBar] Sizing") {
+ const String tab_title = "A tab title that should fit.";
+ const real_t minimum_width = 25;
+ const real_t maximum_width = 1000;
+
+ TabBar *single_tab_bar = memnew(TabBar);
+ TabBar *all_tabs_bar = memnew(TabBar);
+ Window *root = SceneTree::get_singleton()->get_root();
+ root->add_child(single_tab_bar);
+ root->add_child(all_tabs_bar);
+
+ single_tab_bar->set_clip_tabs(true);
+ single_tab_bar->set_custom_maximum_size(Size2(maximum_width, -1));
+ single_tab_bar->set_custom_minimum_size(Size2(minimum_width, 0));
+ single_tab_bar->add_tab(tab_title);
+
+ all_tabs_bar->set_clip_tabs(true);
+ all_tabs_bar->set_custom_maximum_size(Size2(maximum_width, -1));
+ all_tabs_bar->set_custom_minimum_size(Size2(minimum_width, 0));
+ all_tabs_bar->add_tab(tab_title);
+ all_tabs_bar->add_tab(tab_title);
+ all_tabs_bar->add_tab(tab_title);
+ SceneTree::get_singleton()->process(0);
+
+ CHECK_MESSAGE(
+ all_tabs_bar->get_size().width > single_tab_bar->get_size().width,
+ "TabBar expands beyond the one-tab minimum size when all tabs fit.");
+ CHECK_MESSAGE(
+ all_tabs_bar->get_size().width <= maximum_width,
+ "TabBar width will increase up to the custom maximum width when all tabs fit.");
+ CHECK_FALSE(all_tabs_bar->get_offset_buttons_visible());
+
+ // Limit the bar so the first two tabs fit after reserving space for the offset buttons, but not all three.
+ const real_t tab_separation = all_tabs_bar->get_theme_constant("tab_separation");
+ const real_t offset_buttons_width = all_tabs_bar->get_theme_icon("decrement_icon")->get_width() + all_tabs_bar->get_theme_icon("increment_icon")->get_width();
+ const real_t partial_fit_maximum_width = all_tabs_bar->get_tab_rect(0).size.width + all_tabs_bar->get_tab_rect(1).size.width + tab_separation + offset_buttons_width + 1;
+ single_tab_bar->set_custom_maximum_size(Size2(partial_fit_maximum_width, -1));
+ all_tabs_bar->set_custom_maximum_size(Size2(partial_fit_maximum_width, -1));
+ SceneTree::get_singleton()->process(0);
+
+ CHECK_MESSAGE(
+ all_tabs_bar->get_size().width > single_tab_bar->get_size().width,
+ "TabBar expands beyond the one-tab minimum size when some tabs fit.");
+ CHECK_MESSAGE(
+ all_tabs_bar->get_size().width <= partial_fit_maximum_width,
+ "TabBar width will increase up to the custom maximum width when some tabs fit.");
+ CHECK_MESSAGE(
+ all_tabs_bar->get_offset_buttons_visible(),
+ "TabBar shows offset buttons when not all tabs fit.");
+ CHECK_MESSAGE(
+ ((all_tabs_bar->get_tab_rect(1).position.x > 0) && (all_tabs_bar->get_tab_rect(2).position.x == 0)),
+ "TabBar displays some tabs while clipping the remaining tabs.");
+
+ memdelete(all_tabs_bar);
+ memdelete(single_tab_bar);
+}
+
TEST_CASE("[SceneTree][TabBar] Mouse interaction") {
TabBar *tab_bar = memnew(TabBar);
SceneTree::get_singleton()->get_root()->add_child(tab_bar);