feat: godot-engine-source-4.3-stable

This commit is contained in:
Jan van der Weide 2025-01-17 16:36:38 +01:00
parent c59a7dcade
commit 7125d019b5
11149 changed files with 5070401 additions and 0 deletions

View file

@ -0,0 +1,333 @@
/**************************************************************************/
/* test_shader_preprocessor.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. */
/**************************************************************************/
#ifndef TEST_SHADER_PREPROCESSOR_H
#define TEST_SHADER_PREPROCESSOR_H
#include "servers/rendering/shader_preprocessor.h"
#include "tests/test_macros.h"
#include <cctype>
namespace TestShaderPreprocessor {
void erase_all_empty(Vector<String> &p_vec) {
int idx = p_vec.find(" ");
while (idx >= 0) {
p_vec.remove_at(idx);
idx = p_vec.find(" ");
}
}
bool is_variable_char(unsigned char c) {
return std::isalnum(c) || c == '_';
}
bool is_operator_char(unsigned char c) {
return (c == '*') || (c == '+') || (c == '-') || (c == '/') || ((c >= '<') && (c <= '>'));
}
// Remove unnecessary spaces from a line.
String remove_spaces(String &p_str) {
String res;
// Result is guaranteed to not be longer than the input.
res.resize(p_str.size());
int wp = 0;
char32_t last = 0;
bool has_removed = false;
for (int n = 0; n < p_str.size(); n++) {
// These test cases only use ASCII.
unsigned char c = static_cast<unsigned char>(p_str[n]);
if (std::isblank(c)) {
has_removed = true;
} else {
if (has_removed) {
// Insert a space to avoid joining things that could potentially form a new token.
// E.g. "float x" or "- -".
if ((is_variable_char(c) && is_variable_char(last)) ||
(is_operator_char(c) && is_operator_char(last))) {
res[wp++] = ' ';
}
has_removed = false;
}
res[wp++] = c;
last = c;
}
}
res.resize(wp);
return res;
}
// The pre-processor changes indentation and inserts spaces when inserting macros.
// Re-format the code, without changing its meaning, to make it easier to compare.
String compact_spaces(String &p_str) {
Vector<String> lines = p_str.split("\n", false);
erase_all_empty(lines);
for (String &line : lines) {
line = remove_spaces(line);
}
return String("\n").join(lines);
}
#define CHECK_SHADER_EQ(a, b) CHECK_EQ(compact_spaces(a), compact_spaces(b))
#define CHECK_SHADER_NE(a, b) CHECK_NE(compact_spaces(a), compact_spaces(b))
TEST_CASE("[ShaderPreprocessor] Simple defines") {
String code(
"#define X 1.0 // comment\n"
"#define Y mix\n"
"#define Z X\n"
"\n"
"#define func0 \\\n"
" vec3 my_fun(vec3 arg) {\\\n"
" return pow(arg, 2.2);\\\n"
" }\n"
"\n"
"func0\n"
"\n"
"fragment() {\n"
" ALBEDO = vec3(X);\n"
" float x = Y(0., Z, X);\n"
" #undef X\n"
" float X = x;\n"
" x = -Z;\n"
"}\n");
String expected(
"vec3 my_fun(vec3 arg) { return pow(arg, 2.2); }\n"
"\n"
"fragment() {\n"
" ALBEDO = vec3( 1.0 );\n"
" float x = mix(0., 1.0 , 1.0 );\n"
" float X = x;\n"
" x = -X;\n"
"}\n");
String result;
ShaderPreprocessor preprocessor;
CHECK_EQ(preprocessor.preprocess(code, String("file.gdshader"), result), Error::OK);
CHECK_SHADER_EQ(result, expected);
}
TEST_CASE("[ShaderPreprocessor] Avoid merging adjacent tokens") {
String code(
"#define X -10\n"
"#define Y(s) s\n"
"\n"
"fragment() {\n"
" float v = 1.0-X-Y(-2);\n"
"}\n");
String expected(
"fragment() {\n"
" float v = 1.0 - -10 - -2;\n"
"}\n");
String result;
ShaderPreprocessor preprocessor;
CHECK_EQ(preprocessor.preprocess(code, String("file.gdshader"), result), Error::OK);
CHECK_SHADER_EQ(result, expected);
}
TEST_CASE("[ShaderPreprocessor] Complex defines") {
String code(
"const float X = 2.0;\n"
"#define A(X) X*2.\n"
"#define X 1.0\n"
"#define Y Z(X, W)\n"
"#define Z max\n"
"#define C(X, Y) Z(A(Y), B(X))\n"
"#define W -X\n"
"#define B(X) X*3.\n"
"\n"
"fragment() {\n"
" float x = Y;\n"
" float y = C(5., 7.0);\n"
"}\n");
String expected(
"const float X = 2.0;\n"
"fragment() {\n"
" float x = max(1.0, - 1.0);\n"
" float y = max(7.0*2. , 5.*3.);\n"
"}\n");
String result;
ShaderPreprocessor preprocessor;
CHECK_EQ(preprocessor.preprocess(code, String("file.gdshader"), result), Error::OK);
CHECK_SHADER_EQ(result, expected);
}
TEST_CASE("[ShaderPreprocessor] Concatenation") {
String code(
"fragment() {\n"
" #define X 1 // this is fine ##\n"
" #define y 2\n"
" #define z 3##.## 1## 4 ## 59\n"
" #define Z(y) X ## y\n"
" #define Z2(y) y##X\n"
" #define W(y) X, y\n"
" #define A(x) fl## oat a = 1##x ##.3 ## x\n"
" #define C(x, y) x##.##y\n"
" #define J(x) x##=\n"
" float Z(y) = 1.2;\n"
" float Z(z) = 2.3;\n"
" float Z2(y) = z;\n"
" float Z2(z) = 2.3;\n"
" int b = max(W(3));\n"
" Xy J(+) b J(=) 3 ? 0.1 : 0.2;\n"
" A(9);\n"
" Xy = C(X, y);\n"
"}\n");
String expected(
"fragment() {\n"
" float Xy = 1.2;\n"
" float Xz = 2.3;\n"
" float yX = 3.1459;\n"
" float zX = 2.3;\n"
" int b = max(1, 3);\n"
" Xy += b == 3 ? 0.1 : 0.2;\n"
" float a = 19.39;\n"
" Xy = 1.2;\n"
"}\n");
String result;
ShaderPreprocessor preprocessor;
CHECK_EQ(preprocessor.preprocess(code, String("file.gdshader"), result), Error::OK);
CHECK_SHADER_EQ(result, expected);
}
TEST_CASE("[ShaderPreprocessor] Nested concatenation") {
// Concatenation ## should not expand adjacent tokens if they are macros,
// but this is currently not implemented in Godot's shader preprocessor.
// To force expanding, an extra macro should be required (B in this case).
String code(
"fragment() {\n"
" vec2 X = vec2(0);\n"
" #define X 1\n"
" #define y 2\n"
" #define B(x, y) C(x, y)\n"
" #define C(x, y) x##.##y\n"
" C(X, y) = B(X, y);\n"
"}\n");
String expected(
"fragment() {\n"
" vec2 X = vec2(0);\n"
" X.y = 1.2;\n"
"}\n");
String result;
ShaderPreprocessor preprocessor;
CHECK_EQ(preprocessor.preprocess(code, String("file.gdshader"), result), Error::OK);
// TODO: Reverse the check when/if this is changed.
CHECK_SHADER_NE(result, expected);
}
TEST_CASE("[ShaderPreprocessor] Concatenation sorting network") {
String code(
"fragment() {\n"
" #define ARR(X) test##X\n"
" #define ACMP(a, b) ARR(a) > ARR(b)\n"
" #define ASWAP(a, b) tmp = ARR(b); ARR(b) = ARR(a); ARR(a) = tmp;\n"
" #define ACSWAP(a, b) if(ACMP(a, b)) { ASWAP(a, b) }\n"
" float test0 = 1.2;\n"
" float test1 = 0.34;\n"
" float test3 = 0.8;\n"
" float test4 = 2.9;\n"
" float tmp;\n"
" ACSWAP(0,2)\n"
" ACSWAP(1,3)\n"
" ACSWAP(0,1)\n"
" ACSWAP(2,3)\n"
" ACSWAP(1,2)\n"
"}\n");
String expected(
"fragment() {\n"
" float test0 = 1.2;\n"
" float test1 = 0.34;\n"
" float test3 = 0.8;\n"
" float test4 = 2.9;\n"
" float tmp;\n"
" if(test0 > test2) { tmp = test2; test2 = test0; test0 = tmp; }\n"
" if(test1 > test3) { tmp = test3; test3 = test1; test1 = tmp; }\n"
" if(test0 > test1) { tmp = test1; test1 = test0; test0 = tmp; }\n"
" if(test2 > test3) { tmp = test3; test3 = test2; test2 = tmp; }\n"
" if(test1 > test2) { tmp = test2; test2 = test1; test1 = tmp; }\n"
"}\n");
String result;
ShaderPreprocessor preprocessor;
CHECK_EQ(preprocessor.preprocess(code, String("file.gdshader"), result), Error::OK);
CHECK_SHADER_EQ(result, expected);
}
TEST_CASE("[ShaderPreprocessor] Undefined behavior") {
// None of these are valid concatenation, nor valid shader code.
// Don't care about results, just make sure there's no crash.
const String filename("somefile.gdshader");
String result;
ShaderPreprocessor preprocessor;
preprocessor.preprocess("#define X ###\nX\n", filename, result);
preprocessor.preprocess("#define X ####\nX\n", filename, result);
preprocessor.preprocess("#define X #####\nX\n", filename, result);
preprocessor.preprocess("#define X 1 ### 2\nX\n", filename, result);
preprocessor.preprocess("#define X 1 #### 2\nX\n", filename, result);
preprocessor.preprocess("#define X 1 ##### 2\nX\n", filename, result);
preprocessor.preprocess("#define X ### 2\nX\n", filename, result);
preprocessor.preprocess("#define X #### 2\nX\n", filename, result);
preprocessor.preprocess("#define X ##### 2\nX\n", filename, result);
preprocessor.preprocess("#define X 1 ###\nX\n", filename, result);
preprocessor.preprocess("#define X 1 ####\nX\n", filename, result);
preprocessor.preprocess("#define X 1 #####\nX\n", filename, result);
}
TEST_CASE("[ShaderPreprocessor] Invalid concatenations") {
const String filename("somefile.gdshader");
String result;
ShaderPreprocessor preprocessor;
CHECK_NE(preprocessor.preprocess("#define X ##", filename, result), Error::OK);
CHECK_NE(preprocessor.preprocess("#define X 1 ##", filename, result), Error::OK);
CHECK_NE(preprocessor.preprocess("#define X ## 1", filename, result), Error::OK);
CHECK_NE(preprocessor.preprocess("#define X(y) ## ", filename, result), Error::OK);
CHECK_NE(preprocessor.preprocess("#define X(y) y ## ", filename, result), Error::OK);
CHECK_NE(preprocessor.preprocess("#define X(y) ## y", filename, result), Error::OK);
}
} // namespace TestShaderPreprocessor
#endif // TEST_SHADER_PREPROCESSOR_H

View file

@ -0,0 +1,47 @@
/**************************************************************************/
/* test_navigation_server_2d.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. */
/**************************************************************************/
#ifndef TEST_NAVIGATION_SERVER_2D_H
#define TEST_NAVIGATION_SERVER_2D_H
#include "servers/navigation_server_2d.h"
#include "tests/test_macros.h"
namespace TestNavigationServer2D {
TEST_SUITE("[Navigation]") {
TEST_CASE("[NavigationServer2D] Server should be empty when initialized") {
NavigationServer2D *navigation_server = NavigationServer2D::get_singleton();
CHECK_EQ(navigation_server->get_maps().size(), 0);
}
}
} //namespace TestNavigationServer2D
#endif // TEST_NAVIGATION_SERVER_2D_H

View file

@ -0,0 +1,794 @@
/**************************************************************************/
/* test_navigation_server_3d.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. */
/**************************************************************************/
#ifndef TEST_NAVIGATION_SERVER_3D_H
#define TEST_NAVIGATION_SERVER_3D_H
#include "scene/3d/mesh_instance_3d.h"
#include "scene/resources/3d/primitive_meshes.h"
#include "servers/navigation_server_3d.h"
namespace TestNavigationServer3D {
// TODO: Find a more generic way to create `Callable` mocks.
class CallableMock : public Object {
GDCLASS(CallableMock, Object);
public:
void function1(Variant arg0) {
function1_calls++;
function1_latest_arg0 = arg0;
}
unsigned function1_calls{ 0 };
Variant function1_latest_arg0{};
};
static inline Array build_array() {
return Array();
}
template <typename... Targs>
static inline Array build_array(Variant item, Targs... Fargs) {
Array a = build_array(Fargs...);
a.push_front(item);
return a;
}
TEST_SUITE("[Navigation]") {
TEST_CASE("[NavigationServer3D] Server should be empty when initialized") {
NavigationServer3D *navigation_server = NavigationServer3D::get_singleton();
CHECK_EQ(navigation_server->get_maps().size(), 0);
SUBCASE("'ProcessInfo' should report all counters empty as well") {
CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_ACTIVE_MAPS), 0);
CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_REGION_COUNT), 0);
CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_AGENT_COUNT), 0);
CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_LINK_COUNT), 0);
CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_POLYGON_COUNT), 0);
CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_EDGE_COUNT), 0);
CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_EDGE_MERGE_COUNT), 0);
CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_EDGE_CONNECTION_COUNT), 0);
CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_EDGE_FREE_COUNT), 0);
}
}
TEST_CASE("[NavigationServer3D] Server should manage agent properly") {
NavigationServer3D *navigation_server = NavigationServer3D::get_singleton();
RID agent = navigation_server->agent_create();
CHECK(agent.is_valid());
SUBCASE("'ProcessInfo' should not report dangling agent") {
CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_AGENT_COUNT), 0);
}
SUBCASE("Setters/getters should work") {
bool initial_use_3d_avoidance = navigation_server->agent_get_use_3d_avoidance(agent);
navigation_server->agent_set_use_3d_avoidance(agent, !initial_use_3d_avoidance);
navigation_server->process(0.0); // Give server some cycles to commit.
CHECK_EQ(navigation_server->agent_get_use_3d_avoidance(agent), !initial_use_3d_avoidance);
// TODO: Add remaining setters/getters once the missing getters are added.
}
SUBCASE("'ProcessInfo' should report agent with active map") {
RID map = navigation_server->map_create();
CHECK(map.is_valid());
navigation_server->map_set_active(map, true);
navigation_server->agent_set_map(agent, map);
navigation_server->process(0.0); // Give server some cycles to commit.
CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_AGENT_COUNT), 1);
navigation_server->agent_set_map(agent, RID());
navigation_server->free(map);
navigation_server->process(0.0); // Give server some cycles to commit.
CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_AGENT_COUNT), 0);
}
navigation_server->free(agent);
}
TEST_CASE("[NavigationServer3D] Server should manage map properly") {
NavigationServer3D *navigation_server = NavigationServer3D::get_singleton();
RID map;
CHECK_FALSE(map.is_valid());
SUBCASE("Queries against invalid map should return empty or invalid values") {
ERR_PRINT_OFF;
CHECK_EQ(navigation_server->map_get_closest_point(map, Vector3(7, 7, 7)), Vector3());
CHECK_EQ(navigation_server->map_get_closest_point_normal(map, Vector3(7, 7, 7)), Vector3());
CHECK_FALSE(navigation_server->map_get_closest_point_owner(map, Vector3(7, 7, 7)).is_valid());
CHECK_EQ(navigation_server->map_get_closest_point_to_segment(map, Vector3(7, 7, 7), Vector3(8, 8, 8), true), Vector3());
CHECK_EQ(navigation_server->map_get_closest_point_to_segment(map, Vector3(7, 7, 7), Vector3(8, 8, 8), false), Vector3());
CHECK_EQ(navigation_server->map_get_path(map, Vector3(7, 7, 7), Vector3(8, 8, 8), true).size(), 0);
CHECK_EQ(navigation_server->map_get_path(map, Vector3(7, 7, 7), Vector3(8, 8, 8), false).size(), 0);
Ref<NavigationPathQueryParameters3D> query_parameters = memnew(NavigationPathQueryParameters3D);
query_parameters->set_map(map);
query_parameters->set_start_position(Vector3(7, 7, 7));
query_parameters->set_target_position(Vector3(8, 8, 8));
Ref<NavigationPathQueryResult3D> query_result = memnew(NavigationPathQueryResult3D);
navigation_server->query_path(query_parameters, query_result);
CHECK_EQ(query_result->get_path().size(), 0);
CHECK_EQ(query_result->get_path_types().size(), 0);
CHECK_EQ(query_result->get_path_rids().size(), 0);
CHECK_EQ(query_result->get_path_owner_ids().size(), 0);
ERR_PRINT_ON;
}
map = navigation_server->map_create();
CHECK(map.is_valid());
CHECK_EQ(navigation_server->get_maps().size(), 1);
SUBCASE("'ProcessInfo' should not report inactive map") {
CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_ACTIVE_MAPS), 0);
}
SUBCASE("Setters/getters should work") {
navigation_server->map_set_cell_size(map, 0.55);
navigation_server->map_set_edge_connection_margin(map, 0.66);
navigation_server->map_set_link_connection_radius(map, 0.77);
navigation_server->map_set_up(map, Vector3(1, 0, 0));
bool initial_use_edge_connections = navigation_server->map_get_use_edge_connections(map);
navigation_server->map_set_use_edge_connections(map, !initial_use_edge_connections);
navigation_server->process(0.0); // Give server some cycles to commit.
CHECK_EQ(navigation_server->map_get_cell_size(map), doctest::Approx(0.55));
CHECK_EQ(navigation_server->map_get_edge_connection_margin(map), doctest::Approx(0.66));
CHECK_EQ(navigation_server->map_get_link_connection_radius(map), doctest::Approx(0.77));
CHECK_EQ(navigation_server->map_get_up(map), Vector3(1, 0, 0));
CHECK_EQ(navigation_server->map_get_use_edge_connections(map), !initial_use_edge_connections);
}
SUBCASE("'ProcessInfo' should report map iff active") {
navigation_server->map_set_active(map, true);
navigation_server->process(0.0); // Give server some cycles to commit.
CHECK(navigation_server->map_is_active(map));
CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_ACTIVE_MAPS), 1);
navigation_server->map_set_active(map, false);
navigation_server->process(0.0); // Give server some cycles to commit.
CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_ACTIVE_MAPS), 0);
}
SUBCASE("Number of agents should be reported properly") {
RID agent = navigation_server->agent_create();
CHECK(agent.is_valid());
navigation_server->agent_set_map(agent, map);
navigation_server->process(0.0); // Give server some cycles to commit.
CHECK_EQ(navigation_server->map_get_agents(map).size(), 1);
navigation_server->free(agent);
navigation_server->process(0.0); // Give server some cycles to commit.
CHECK_EQ(navigation_server->map_get_agents(map).size(), 0);
}
SUBCASE("Number of links should be reported properly") {
RID link = navigation_server->link_create();
CHECK(link.is_valid());
navigation_server->link_set_map(link, map);
navigation_server->process(0.0); // Give server some cycles to commit.
CHECK_EQ(navigation_server->map_get_links(map).size(), 1);
navigation_server->free(link);
navigation_server->process(0.0); // Give server some cycles to commit.
CHECK_EQ(navigation_server->map_get_links(map).size(), 0);
}
SUBCASE("Number of obstacles should be reported properly") {
RID obstacle = navigation_server->obstacle_create();
CHECK(obstacle.is_valid());
navigation_server->obstacle_set_map(obstacle, map);
navigation_server->process(0.0); // Give server some cycles to commit.
CHECK_EQ(navigation_server->map_get_obstacles(map).size(), 1);
navigation_server->free(obstacle);
navigation_server->process(0.0); // Give server some cycles to commit.
CHECK_EQ(navigation_server->map_get_obstacles(map).size(), 0);
}
SUBCASE("Number of regions should be reported properly") {
RID region = navigation_server->region_create();
CHECK(region.is_valid());
navigation_server->region_set_map(region, map);
navigation_server->process(0.0); // Give server some cycles to commit.
CHECK_EQ(navigation_server->map_get_regions(map).size(), 1);
navigation_server->free(region);
navigation_server->process(0.0); // Give server some cycles to commit.
CHECK_EQ(navigation_server->map_get_regions(map).size(), 0);
}
SUBCASE("Queries against empty map should return empty or invalid values") {
navigation_server->map_set_active(map, true);
navigation_server->process(0.0); // Give server some cycles to commit.
ERR_PRINT_OFF;
CHECK_EQ(navigation_server->map_get_closest_point(map, Vector3(7, 7, 7)), Vector3());
CHECK_EQ(navigation_server->map_get_closest_point_normal(map, Vector3(7, 7, 7)), Vector3());
CHECK_FALSE(navigation_server->map_get_closest_point_owner(map, Vector3(7, 7, 7)).is_valid());
CHECK_EQ(navigation_server->map_get_closest_point_to_segment(map, Vector3(7, 7, 7), Vector3(8, 8, 8), true), Vector3());
CHECK_EQ(navigation_server->map_get_closest_point_to_segment(map, Vector3(7, 7, 7), Vector3(8, 8, 8), false), Vector3());
CHECK_EQ(navigation_server->map_get_path(map, Vector3(7, 7, 7), Vector3(8, 8, 8), true).size(), 0);
CHECK_EQ(navigation_server->map_get_path(map, Vector3(7, 7, 7), Vector3(8, 8, 8), false).size(), 0);
Ref<NavigationPathQueryParameters3D> query_parameters = memnew(NavigationPathQueryParameters3D);
query_parameters->set_map(map);
query_parameters->set_start_position(Vector3(7, 7, 7));
query_parameters->set_target_position(Vector3(8, 8, 8));
Ref<NavigationPathQueryResult3D> query_result = memnew(NavigationPathQueryResult3D);
navigation_server->query_path(query_parameters, query_result);
CHECK_EQ(query_result->get_path().size(), 0);
CHECK_EQ(query_result->get_path_types().size(), 0);
CHECK_EQ(query_result->get_path_rids().size(), 0);
CHECK_EQ(query_result->get_path_owner_ids().size(), 0);
ERR_PRINT_ON;
navigation_server->map_set_active(map, false);
navigation_server->process(0.0); // Give server some cycles to commit.
}
navigation_server->free(map);
navigation_server->process(0.0); // Give server some cycles to actually remove map.
CHECK_EQ(navigation_server->get_maps().size(), 0);
}
TEST_CASE("[NavigationServer3D] Server should manage link properly") {
NavigationServer3D *navigation_server = NavigationServer3D::get_singleton();
RID link = navigation_server->link_create();
CHECK(link.is_valid());
SUBCASE("'ProcessInfo' should not report dangling link") {
CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_LINK_COUNT), 0);
}
SUBCASE("Setters/getters should work") {
bool initial_bidirectional = navigation_server->link_is_bidirectional(link);
navigation_server->link_set_bidirectional(link, !initial_bidirectional);
navigation_server->link_set_end_position(link, Vector3(7, 7, 7));
navigation_server->link_set_enter_cost(link, 0.55);
navigation_server->link_set_navigation_layers(link, 6);
navigation_server->link_set_owner_id(link, ObjectID((int64_t)7));
navigation_server->link_set_start_position(link, Vector3(8, 8, 8));
navigation_server->link_set_travel_cost(link, 0.66);
navigation_server->process(0.0); // Give server some cycles to commit.
CHECK_EQ(navigation_server->link_is_bidirectional(link), !initial_bidirectional);
CHECK_EQ(navigation_server->link_get_end_position(link), Vector3(7, 7, 7));
CHECK_EQ(navigation_server->link_get_enter_cost(link), doctest::Approx(0.55));
CHECK_EQ(navigation_server->link_get_navigation_layers(link), 6);
CHECK_EQ(navigation_server->link_get_owner_id(link), ObjectID((int64_t)7));
CHECK_EQ(navigation_server->link_get_start_position(link), Vector3(8, 8, 8));
CHECK_EQ(navigation_server->link_get_travel_cost(link), doctest::Approx(0.66));
}
SUBCASE("'ProcessInfo' should report link with active map") {
RID map = navigation_server->map_create();
CHECK(map.is_valid());
navigation_server->map_set_active(map, true);
navigation_server->link_set_map(link, map);
navigation_server->process(0.0); // Give server some cycles to commit.
CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_LINK_COUNT), 1);
navigation_server->link_set_map(link, RID());
navigation_server->free(map);
navigation_server->process(0.0); // Give server some cycles to commit.
CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_LINK_COUNT), 0);
}
navigation_server->free(link);
}
TEST_CASE("[NavigationServer3D] Server should manage obstacles properly") {
NavigationServer3D *navigation_server = NavigationServer3D::get_singleton();
RID obstacle = navigation_server->obstacle_create();
CHECK(obstacle.is_valid());
// TODO: Add tests for setters/getters once getters are added.
navigation_server->free(obstacle);
}
TEST_CASE("[NavigationServer3D] Server should manage regions properly") {
NavigationServer3D *navigation_server = NavigationServer3D::get_singleton();
RID region = navigation_server->region_create();
CHECK(region.is_valid());
SUBCASE("'ProcessInfo' should not report dangling region") {
CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_REGION_COUNT), 0);
}
SUBCASE("Setters/getters should work") {
bool initial_use_edge_connections = navigation_server->region_get_use_edge_connections(region);
navigation_server->region_set_enter_cost(region, 0.55);
navigation_server->region_set_navigation_layers(region, 5);
navigation_server->region_set_owner_id(region, ObjectID((int64_t)7));
navigation_server->region_set_travel_cost(region, 0.66);
navigation_server->region_set_use_edge_connections(region, !initial_use_edge_connections);
navigation_server->process(0.0); // Give server some cycles to commit.
CHECK_EQ(navigation_server->region_get_enter_cost(region), doctest::Approx(0.55));
CHECK_EQ(navigation_server->region_get_navigation_layers(region), 5);
CHECK_EQ(navigation_server->region_get_owner_id(region), ObjectID((int64_t)7));
CHECK_EQ(navigation_server->region_get_travel_cost(region), doctest::Approx(0.66));
CHECK_EQ(navigation_server->region_get_use_edge_connections(region), !initial_use_edge_connections);
}
SUBCASE("'ProcessInfo' should report region with active map") {
RID map = navigation_server->map_create();
CHECK(map.is_valid());
navigation_server->map_set_active(map, true);
navigation_server->region_set_map(region, map);
navigation_server->process(0.0); // Give server some cycles to commit.
CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_REGION_COUNT), 1);
navigation_server->region_set_map(region, RID());
navigation_server->free(map);
navigation_server->process(0.0); // Give server some cycles to commit.
CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_REGION_COUNT), 0);
}
SUBCASE("Queries against empty region should return empty or invalid values") {
ERR_PRINT_OFF;
CHECK_EQ(navigation_server->region_get_connections_count(region), 0);
CHECK_EQ(navigation_server->region_get_connection_pathway_end(region, 55), Vector3());
CHECK_EQ(navigation_server->region_get_connection_pathway_start(region, 55), Vector3());
ERR_PRINT_ON;
}
navigation_server->free(region);
}
// This test case does not check precise values on purpose - to not be too sensitivte.
TEST_CASE("[NavigationServer3D] Server should move agent properly") {
NavigationServer3D *navigation_server = NavigationServer3D::get_singleton();
RID map = navigation_server->map_create();
RID agent = navigation_server->agent_create();
navigation_server->map_set_active(map, true);
navigation_server->agent_set_map(agent, map);
navigation_server->agent_set_avoidance_enabled(agent, true);
navigation_server->agent_set_velocity(agent, Vector3(1, 0, 1));
CallableMock agent_avoidance_callback_mock;
navigation_server->agent_set_avoidance_callback(agent, callable_mp(&agent_avoidance_callback_mock, &CallableMock::function1));
CHECK_EQ(agent_avoidance_callback_mock.function1_calls, 0);
navigation_server->process(0.0); // Give server some cycles to commit.
CHECK_EQ(agent_avoidance_callback_mock.function1_calls, 1);
CHECK_NE(agent_avoidance_callback_mock.function1_latest_arg0, Vector3(0, 0, 0));
navigation_server->free(agent);
navigation_server->free(map);
}
// This test case does not check precise values on purpose - to not be too sensitivte.
TEST_CASE("[NavigationServer3D] Server should make agents avoid each other when avoidance enabled") {
NavigationServer3D *navigation_server = NavigationServer3D::get_singleton();
RID map = navigation_server->map_create();
RID agent_1 = navigation_server->agent_create();
RID agent_2 = navigation_server->agent_create();
navigation_server->map_set_active(map, true);
navigation_server->agent_set_map(agent_1, map);
navigation_server->agent_set_avoidance_enabled(agent_1, true);
navigation_server->agent_set_position(agent_1, Vector3(0, 0, 0));
navigation_server->agent_set_radius(agent_1, 1);
navigation_server->agent_set_velocity(agent_1, Vector3(1, 0, 0));
CallableMock agent_1_avoidance_callback_mock;
navigation_server->agent_set_avoidance_callback(agent_1, callable_mp(&agent_1_avoidance_callback_mock, &CallableMock::function1));
navigation_server->agent_set_map(agent_2, map);
navigation_server->agent_set_avoidance_enabled(agent_2, true);
navigation_server->agent_set_position(agent_2, Vector3(2.5, 0, 0.5));
navigation_server->agent_set_radius(agent_2, 1);
navigation_server->agent_set_velocity(agent_2, Vector3(-1, 0, 0));
CallableMock agent_2_avoidance_callback_mock;
navigation_server->agent_set_avoidance_callback(agent_2, callable_mp(&agent_2_avoidance_callback_mock, &CallableMock::function1));
CHECK_EQ(agent_1_avoidance_callback_mock.function1_calls, 0);
CHECK_EQ(agent_2_avoidance_callback_mock.function1_calls, 0);
navigation_server->process(0.0); // Give server some cycles to commit.
CHECK_EQ(agent_1_avoidance_callback_mock.function1_calls, 1);
CHECK_EQ(agent_2_avoidance_callback_mock.function1_calls, 1);
Vector3 agent_1_safe_velocity = agent_1_avoidance_callback_mock.function1_latest_arg0;
Vector3 agent_2_safe_velocity = agent_2_avoidance_callback_mock.function1_latest_arg0;
CHECK_MESSAGE(agent_1_safe_velocity.x > 0, "agent 1 should move a bit along desired velocity (+X)");
CHECK_MESSAGE(agent_2_safe_velocity.x < 0, "agent 2 should move a bit along desired velocity (-X)");
CHECK_MESSAGE(agent_1_safe_velocity.z < 0, "agent 1 should move a bit to the side so that it avoids agent 2");
CHECK_MESSAGE(agent_2_safe_velocity.z > 0, "agent 2 should move a bit to the side so that it avoids agent 1");
navigation_server->free(agent_2);
navigation_server->free(agent_1);
navigation_server->free(map);
}
TEST_CASE("[NavigationServer3D] Server should make agents avoid dynamic obstacles when avoidance enabled") {
NavigationServer3D *navigation_server = NavigationServer3D::get_singleton();
RID map = navigation_server->map_create();
RID agent_1 = navigation_server->agent_create();
RID obstacle_1 = navigation_server->obstacle_create();
navigation_server->map_set_active(map, true);
navigation_server->agent_set_map(agent_1, map);
navigation_server->agent_set_avoidance_enabled(agent_1, true);
navigation_server->agent_set_position(agent_1, Vector3(0, 0, 0));
navigation_server->agent_set_radius(agent_1, 1);
navigation_server->agent_set_velocity(agent_1, Vector3(1, 0, 0));
CallableMock agent_1_avoidance_callback_mock;
navigation_server->agent_set_avoidance_callback(agent_1, callable_mp(&agent_1_avoidance_callback_mock, &CallableMock::function1));
navigation_server->obstacle_set_map(obstacle_1, map);
navigation_server->obstacle_set_avoidance_enabled(obstacle_1, true);
navigation_server->obstacle_set_position(obstacle_1, Vector3(2.5, 0, 0.5));
navigation_server->obstacle_set_radius(obstacle_1, 1);
CHECK_EQ(agent_1_avoidance_callback_mock.function1_calls, 0);
navigation_server->process(0.0); // Give server some cycles to commit.
CHECK_EQ(agent_1_avoidance_callback_mock.function1_calls, 1);
Vector3 agent_1_safe_velocity = agent_1_avoidance_callback_mock.function1_latest_arg0;
CHECK_MESSAGE(agent_1_safe_velocity.x > 0, "Agent 1 should move a bit along desired velocity (+X).");
CHECK_MESSAGE(agent_1_safe_velocity.z < 0, "Agent 1 should move a bit to the side so that it avoids obstacle.");
navigation_server->free(obstacle_1);
navigation_server->free(agent_1);
navigation_server->free(map);
navigation_server->process(0.0); // Give server some cycles to commit.
}
TEST_CASE("[NavigationServer3D] Server should make agents avoid static obstacles when avoidance enabled") {
NavigationServer3D *navigation_server = NavigationServer3D::get_singleton();
RID map = navigation_server->map_create();
RID agent_1 = navigation_server->agent_create();
RID agent_2 = navigation_server->agent_create();
RID obstacle_1 = navigation_server->obstacle_create();
navigation_server->map_set_active(map, true);
navigation_server->agent_set_map(agent_1, map);
navigation_server->agent_set_avoidance_enabled(agent_1, true);
navigation_server->agent_set_radius(agent_1, 1.6); // Have hit the obstacle already.
navigation_server->agent_set_velocity(agent_1, Vector3(1, 0, 0));
CallableMock agent_1_avoidance_callback_mock;
navigation_server->agent_set_avoidance_callback(agent_1, callable_mp(&agent_1_avoidance_callback_mock, &CallableMock::function1));
navigation_server->agent_set_map(agent_2, map);
navigation_server->agent_set_avoidance_enabled(agent_2, true);
navigation_server->agent_set_radius(agent_2, 1.4); // Haven't hit the obstacle yet.
navigation_server->agent_set_velocity(agent_2, Vector3(1, 0, 0));
CallableMock agent_2_avoidance_callback_mock;
navigation_server->agent_set_avoidance_callback(agent_2, callable_mp(&agent_2_avoidance_callback_mock, &CallableMock::function1));
navigation_server->obstacle_set_map(obstacle_1, map);
navigation_server->obstacle_set_avoidance_enabled(obstacle_1, true);
PackedVector3Array obstacle_1_vertices;
SUBCASE("Static obstacles should work on ground level") {
navigation_server->agent_set_position(agent_1, Vector3(0, 0, 0));
navigation_server->agent_set_position(agent_2, Vector3(0, 0, 5));
obstacle_1_vertices.push_back(Vector3(1.5, 0, 0.5));
obstacle_1_vertices.push_back(Vector3(1.5, 0, 4.5));
}
SUBCASE("Static obstacles should work when elevated") {
navigation_server->agent_set_position(agent_1, Vector3(0, 5, 0));
navigation_server->agent_set_position(agent_2, Vector3(0, 5, 5));
obstacle_1_vertices.push_back(Vector3(1.5, 0, 0.5));
obstacle_1_vertices.push_back(Vector3(1.5, 0, 4.5));
navigation_server->obstacle_set_position(obstacle_1, Vector3(0, 5, 0));
}
navigation_server->obstacle_set_vertices(obstacle_1, obstacle_1_vertices);
CHECK_EQ(agent_1_avoidance_callback_mock.function1_calls, 0);
CHECK_EQ(agent_2_avoidance_callback_mock.function1_calls, 0);
navigation_server->process(0.0); // Give server some cycles to commit.
CHECK_EQ(agent_1_avoidance_callback_mock.function1_calls, 1);
CHECK_EQ(agent_2_avoidance_callback_mock.function1_calls, 1);
Vector3 agent_1_safe_velocity = agent_1_avoidance_callback_mock.function1_latest_arg0;
Vector3 agent_2_safe_velocity = agent_2_avoidance_callback_mock.function1_latest_arg0;
CHECK_MESSAGE(agent_1_safe_velocity.x > 0, "Agent 1 should move a bit along desired velocity (+X).");
CHECK_MESSAGE(agent_1_safe_velocity.z < 0, "Agent 1 should move a bit to the side so that it avoids obstacle.");
CHECK_MESSAGE(agent_2_safe_velocity.x > 0, "Agent 2 should move a bit along desired velocity (+X).");
CHECK_MESSAGE(agent_2_safe_velocity.z == 0, "Agent 2 should not move to the side.");
navigation_server->free(obstacle_1);
navigation_server->free(agent_2);
navigation_server->free(agent_1);
navigation_server->free(map);
navigation_server->process(0.0); // Give server some cycles to commit.
}
#ifndef DISABLE_DEPRECATED
// This test case uses only public APIs on purpose - other test cases use simplified baking.
// FIXME: Remove once deprecated `region_bake_navigation_mesh()` is removed.
TEST_CASE("[NavigationServer3D][SceneTree][DEPRECATED] Server should be able to bake map correctly") {
NavigationServer3D *navigation_server = NavigationServer3D::get_singleton();
// Prepare scene tree with simple mesh to serve as an input geometry.
Node3D *node_3d = memnew(Node3D);
SceneTree::get_singleton()->get_root()->add_child(node_3d);
Ref<PlaneMesh> plane_mesh = memnew(PlaneMesh);
plane_mesh->set_size(Size2(10.0, 10.0));
MeshInstance3D *mesh_instance = memnew(MeshInstance3D);
mesh_instance->set_mesh(plane_mesh);
node_3d->add_child(mesh_instance);
// Prepare anything necessary to bake navigation mesh.
RID map = navigation_server->map_create();
RID region = navigation_server->region_create();
Ref<NavigationMesh> navigation_mesh = memnew(NavigationMesh);
navigation_server->map_set_active(map, true);
navigation_server->region_set_map(region, map);
navigation_server->region_set_navigation_mesh(region, navigation_mesh);
navigation_server->process(0.0); // Give server some cycles to commit.
CHECK_EQ(navigation_mesh->get_polygon_count(), 0);
CHECK_EQ(navigation_mesh->get_vertices().size(), 0);
ERR_PRINT_OFF;
navigation_server->region_bake_navigation_mesh(navigation_mesh, node_3d);
ERR_PRINT_ON;
// FIXME: The above line should trigger the update (line below) under the hood.
navigation_server->region_set_navigation_mesh(region, navigation_mesh); // Force update.
CHECK_EQ(navigation_mesh->get_polygon_count(), 2);
CHECK_EQ(navigation_mesh->get_vertices().size(), 4);
SUBCASE("Map should emit signal and take newly baked navigation mesh into account") {
SIGNAL_WATCH(navigation_server, "map_changed");
SIGNAL_CHECK_FALSE("map_changed");
navigation_server->process(0.0); // Give server some cycles to commit.
SIGNAL_CHECK("map_changed", build_array(build_array(map)));
SIGNAL_UNWATCH(navigation_server, "map_changed");
CHECK_NE(navigation_server->map_get_closest_point(map, Vector3(0, 0, 0)), Vector3(0, 0, 0));
}
navigation_server->free(region);
navigation_server->free(map);
navigation_server->process(0.0); // Give server some cycles to commit.
memdelete(mesh_instance);
memdelete(node_3d);
}
#endif // DISABLE_DEPRECATED
TEST_CASE("[NavigationServer3D][SceneTree] Server should be able to parse geometry") {
NavigationServer3D *navigation_server = NavigationServer3D::get_singleton();
// Prepare scene tree with simple mesh to serve as an input geometry.
Node3D *node_3d = memnew(Node3D);
SceneTree::get_singleton()->get_root()->add_child(node_3d);
Ref<PlaneMesh> plane_mesh = memnew(PlaneMesh);
plane_mesh->set_size(Size2(10.0, 10.0));
MeshInstance3D *mesh_instance = memnew(MeshInstance3D);
mesh_instance->set_mesh(plane_mesh);
node_3d->add_child(mesh_instance);
Ref<NavigationMesh> navigation_mesh = memnew(NavigationMesh);
Ref<NavigationMeshSourceGeometryData3D> source_geometry = memnew(NavigationMeshSourceGeometryData3D);
CHECK_EQ(source_geometry->get_vertices().size(), 0);
CHECK_EQ(source_geometry->get_indices().size(), 0);
navigation_server->parse_source_geometry_data(navigation_mesh, source_geometry, mesh_instance);
CHECK_EQ(source_geometry->get_vertices().size(), 12);
CHECK_EQ(source_geometry->get_indices().size(), 6);
SUBCASE("By default, parsing should remove any data that was parsed before") {
navigation_server->parse_source_geometry_data(navigation_mesh, source_geometry, mesh_instance);
CHECK_EQ(source_geometry->get_vertices().size(), 12);
CHECK_EQ(source_geometry->get_indices().size(), 6);
}
SUBCASE("Parsed geometry should be extendible with other geometry") {
source_geometry->merge(source_geometry); // Merging with itself.
const Vector<float> vertices = source_geometry->get_vertices();
const Vector<int> indices = source_geometry->get_indices();
REQUIRE_EQ(vertices.size(), 24);
REQUIRE_EQ(indices.size(), 12);
// Check if first newly added vertex is the same as first vertex.
CHECK_EQ(vertices[0], vertices[12]);
CHECK_EQ(vertices[1], vertices[13]);
CHECK_EQ(vertices[2], vertices[14]);
// Check if first newly added index is the same as first index.
CHECK_EQ(indices[0] + 4, indices[6]);
}
memdelete(mesh_instance);
memdelete(node_3d);
}
// This test case uses only public APIs on purpose - other test cases use simplified baking.
TEST_CASE("[NavigationServer3D][SceneTree] Server should be able to bake map correctly") {
NavigationServer3D *navigation_server = NavigationServer3D::get_singleton();
// Prepare scene tree with simple mesh to serve as an input geometry.
Node3D *node_3d = memnew(Node3D);
SceneTree::get_singleton()->get_root()->add_child(node_3d);
Ref<PlaneMesh> plane_mesh = memnew(PlaneMesh);
plane_mesh->set_size(Size2(10.0, 10.0));
MeshInstance3D *mesh_instance = memnew(MeshInstance3D);
mesh_instance->set_mesh(plane_mesh);
node_3d->add_child(mesh_instance);
// Prepare anything necessary to bake navigation mesh.
RID map = navigation_server->map_create();
RID region = navigation_server->region_create();
Ref<NavigationMesh> navigation_mesh = memnew(NavigationMesh);
navigation_server->map_set_active(map, true);
navigation_server->region_set_map(region, map);
navigation_server->region_set_navigation_mesh(region, navigation_mesh);
navigation_server->process(0.0); // Give server some cycles to commit.
CHECK_EQ(navigation_mesh->get_polygon_count(), 0);
CHECK_EQ(navigation_mesh->get_vertices().size(), 0);
Ref<NavigationMeshSourceGeometryData3D> source_geometry = memnew(NavigationMeshSourceGeometryData3D);
navigation_server->parse_source_geometry_data(navigation_mesh, source_geometry, node_3d);
navigation_server->bake_from_source_geometry_data(navigation_mesh, source_geometry, Callable());
// FIXME: The above line should trigger the update (line below) under the hood.
navigation_server->region_set_navigation_mesh(region, navigation_mesh); // Force update.
CHECK_EQ(navigation_mesh->get_polygon_count(), 2);
CHECK_EQ(navigation_mesh->get_vertices().size(), 4);
SUBCASE("Map should emit signal and take newly baked navigation mesh into account") {
SIGNAL_WATCH(navigation_server, "map_changed");
SIGNAL_CHECK_FALSE("map_changed");
navigation_server->process(0.0); // Give server some cycles to commit.
SIGNAL_CHECK("map_changed", build_array(build_array(map)));
SIGNAL_UNWATCH(navigation_server, "map_changed");
CHECK_NE(navigation_server->map_get_closest_point(map, Vector3(0, 0, 0)), Vector3(0, 0, 0));
}
navigation_server->free(region);
navigation_server->free(map);
navigation_server->process(0.0); // Give server some cycles to commit.
memdelete(mesh_instance);
memdelete(node_3d);
}
// This test case does not check precise values on purpose - to not be too sensitivte.
TEST_CASE("[NavigationServer3D] Server should respond to queries against valid map properly") {
NavigationServer3D *navigation_server = NavigationServer3D::get_singleton();
Ref<NavigationMesh> navigation_mesh = memnew(NavigationMesh);
Ref<NavigationMeshSourceGeometryData3D> source_geometry = memnew(NavigationMeshSourceGeometryData3D);
Array arr;
arr.resize(RS::ARRAY_MAX);
BoxMesh::create_mesh_array(arr, Vector3(10.0, 0.001, 10.0));
source_geometry->add_mesh_array(arr, Transform3D());
navigation_server->bake_from_source_geometry_data(navigation_mesh, source_geometry, Callable());
CHECK_NE(navigation_mesh->get_polygon_count(), 0);
CHECK_NE(navigation_mesh->get_vertices().size(), 0);
RID map = navigation_server->map_create();
RID region = navigation_server->region_create();
navigation_server->map_set_active(map, true);
navigation_server->region_set_map(region, map);
navigation_server->region_set_navigation_mesh(region, navigation_mesh);
navigation_server->process(0.0); // Give server some cycles to commit.
SUBCASE("Simple queries should return non-default values") {
CHECK_NE(navigation_server->map_get_closest_point(map, Vector3(0, 0, 0)), Vector3(0, 0, 0));
CHECK_NE(navigation_server->map_get_closest_point_normal(map, Vector3(0, 0, 0)), Vector3());
CHECK(navigation_server->map_get_closest_point_owner(map, Vector3(0, 0, 0)).is_valid());
CHECK_NE(navigation_server->map_get_closest_point_to_segment(map, Vector3(0, 0, 0), Vector3(1, 1, 1), false), Vector3());
CHECK_NE(navigation_server->map_get_closest_point_to_segment(map, Vector3(0, 0, 0), Vector3(1, 1, 1), true), Vector3());
CHECK_NE(navigation_server->map_get_path(map, Vector3(0, 0, 0), Vector3(10, 0, 10), true).size(), 0);
CHECK_NE(navigation_server->map_get_path(map, Vector3(0, 0, 0), Vector3(10, 0, 10), false).size(), 0);
}
SUBCASE("'map_get_closest_point_to_segment' with 'use_collision' should return default if segment doesn't intersect map") {
CHECK_EQ(navigation_server->map_get_closest_point_to_segment(map, Vector3(1, 2, 1), Vector3(1, 1, 1), true), Vector3());
}
SUBCASE("Elaborate query with 'CORRIDORFUNNEL' post-processing should yield non-empty result") {
Ref<NavigationPathQueryParameters3D> query_parameters = memnew(NavigationPathQueryParameters3D);
query_parameters->set_map(map);
query_parameters->set_start_position(Vector3(0, 0, 0));
query_parameters->set_target_position(Vector3(10, 0, 10));
query_parameters->set_path_postprocessing(NavigationPathQueryParameters3D::PATH_POSTPROCESSING_CORRIDORFUNNEL);
Ref<NavigationPathQueryResult3D> query_result = memnew(NavigationPathQueryResult3D);
navigation_server->query_path(query_parameters, query_result);
CHECK_NE(query_result->get_path().size(), 0);
CHECK_NE(query_result->get_path_types().size(), 0);
CHECK_NE(query_result->get_path_rids().size(), 0);
CHECK_NE(query_result->get_path_owner_ids().size(), 0);
}
SUBCASE("Elaborate query with 'EDGECENTERED' post-processing should yield non-empty result") {
Ref<NavigationPathQueryParameters3D> query_parameters = memnew(NavigationPathQueryParameters3D);
query_parameters->set_map(map);
query_parameters->set_start_position(Vector3(10, 0, 10));
query_parameters->set_target_position(Vector3(0, 0, 0));
query_parameters->set_path_postprocessing(NavigationPathQueryParameters3D::PATH_POSTPROCESSING_EDGECENTERED);
Ref<NavigationPathQueryResult3D> query_result = memnew(NavigationPathQueryResult3D);
navigation_server->query_path(query_parameters, query_result);
CHECK_NE(query_result->get_path().size(), 0);
CHECK_NE(query_result->get_path_types().size(), 0);
CHECK_NE(query_result->get_path_rids().size(), 0);
CHECK_NE(query_result->get_path_owner_ids().size(), 0);
}
SUBCASE("Elaborate query with non-matching navigation layer mask should yield empty result") {
Ref<NavigationPathQueryParameters3D> query_parameters = memnew(NavigationPathQueryParameters3D);
query_parameters->set_map(map);
query_parameters->set_start_position(Vector3(10, 0, 10));
query_parameters->set_target_position(Vector3(0, 0, 0));
query_parameters->set_navigation_layers(2);
Ref<NavigationPathQueryResult3D> query_result = memnew(NavigationPathQueryResult3D);
navigation_server->query_path(query_parameters, query_result);
CHECK_EQ(query_result->get_path().size(), 0);
CHECK_EQ(query_result->get_path_types().size(), 0);
CHECK_EQ(query_result->get_path_rids().size(), 0);
CHECK_EQ(query_result->get_path_owner_ids().size(), 0);
}
SUBCASE("Elaborate query without metadata flags should yield path only") {
Ref<NavigationPathQueryParameters3D> query_parameters = memnew(NavigationPathQueryParameters3D);
query_parameters->set_map(map);
query_parameters->set_start_position(Vector3(10, 0, 10));
query_parameters->set_target_position(Vector3(0, 0, 0));
query_parameters->set_metadata_flags(0);
Ref<NavigationPathQueryResult3D> query_result = memnew(NavigationPathQueryResult3D);
navigation_server->query_path(query_parameters, query_result);
CHECK_NE(query_result->get_path().size(), 0);
CHECK_EQ(query_result->get_path_types().size(), 0);
CHECK_EQ(query_result->get_path_rids().size(), 0);
CHECK_EQ(query_result->get_path_owner_ids().size(), 0);
}
navigation_server->free(region);
navigation_server->free(map);
navigation_server->process(0.0); // Give server some cycles to commit.
}
// FIXME: The race condition mentioned below is actually a problem and fails on CI (GH-90613).
/*
TEST_CASE("[NavigationServer3D] Server should be able to bake asynchronously") {
NavigationServer3D *navigation_server = NavigationServer3D::get_singleton();
Ref<NavigationMesh> navigation_mesh = memnew(NavigationMesh);
Ref<NavigationMeshSourceGeometryData3D> source_geometry = memnew(NavigationMeshSourceGeometryData3D);
Array arr;
arr.resize(RS::ARRAY_MAX);
BoxMesh::create_mesh_array(arr, Vector3(10.0, 0.001, 10.0));
source_geometry->add_mesh_array(arr, Transform3D());
// Race condition is present below, but baking should take many orders of magnitude
// longer than basic checks on the main thread, so it's fine.
navigation_server->bake_from_source_geometry_data_async(navigation_mesh, source_geometry, Callable());
CHECK(navigation_server->is_baking_navigation_mesh(navigation_mesh));
CHECK_EQ(navigation_mesh->get_polygon_count(), 0);
CHECK_EQ(navigation_mesh->get_vertices().size(), 0);
}
*/
}
} //namespace TestNavigationServer3D
#endif // TEST_NAVIGATION_SERVER_3D_H

View file

@ -0,0 +1,866 @@
/**************************************************************************/
/* test_text_server.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. */
/**************************************************************************/
#ifndef TEST_TEXT_SERVER_H
#define TEST_TEXT_SERVER_H
#ifdef TOOLS_ENABLED
#include "editor/themes/builtin_fonts.gen.h"
#include "servers/text_server.h"
#include "tests/test_macros.h"
namespace TestTextServer {
TEST_SUITE("[TextServer]") {
TEST_CASE("[TextServer] Init, font loading and shaping") {
SUBCASE("[TextServer] Loading fonts") {
for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
if (!ts->has_feature(TextServer::FEATURE_FONT_DYNAMIC)) {
continue;
}
RID font = ts->create_font();
ts->font_set_data_ptr(font, _font_NotoSans_Regular, _font_NotoSans_Regular_size);
CHECK_FALSE_MESSAGE(font == RID(), "Loading font failed.");
ts->free_rid(font);
}
}
SUBCASE("[TextServer] Text layout: Font fallback") {
for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
if (!ts->has_feature(TextServer::FEATURE_FONT_DYNAMIC) || !ts->has_feature(TextServer::FEATURE_SIMPLE_LAYOUT)) {
continue;
}
RID font1 = ts->create_font();
ts->font_set_data_ptr(font1, _font_NotoSans_Regular, _font_NotoSans_Regular_size);
ts->font_set_allow_system_fallback(font1, false);
RID font2 = ts->create_font();
ts->font_set_data_ptr(font2, _font_NotoSansThai_Regular, _font_NotoSansThai_Regular_size);
ts->font_set_allow_system_fallback(font2, false);
Array font;
font.push_back(font1);
font.push_back(font2);
String test = U"คนอ้วน khon uan ראה";
// 6^ 17^
RID ctx = ts->create_shaped_text();
CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
bool ok = ts->shaped_text_add_string(ctx, test, font, 16);
CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
const Glyph *glyphs = ts->shaped_text_get_glyphs(ctx);
int gl_size = ts->shaped_text_get_glyph_count(ctx);
CHECK_FALSE_MESSAGE(gl_size == 0, "Shaping failed");
for (int j = 0; j < gl_size; j++) {
if (glyphs[j].start < 6) {
CHECK_FALSE_MESSAGE(glyphs[j].font_rid != font[1], "Incorrect font selected.");
}
if ((glyphs[j].start > 6) && (glyphs[j].start < 16)) {
CHECK_FALSE_MESSAGE(glyphs[j].font_rid != font[0], "Incorrect font selected.");
}
if (glyphs[j].start > 16) {
CHECK_FALSE_MESSAGE(glyphs[j].font_rid != RID(), "Incorrect font selected.");
CHECK_FALSE_MESSAGE(glyphs[j].index != test[glyphs[j].start], "Incorrect glyph index.");
}
CHECK_FALSE_MESSAGE((glyphs[j].start < 0 || glyphs[j].end > test.length()), "Incorrect glyph range.");
CHECK_FALSE_MESSAGE(glyphs[j].font_size != 16, "Incorrect glyph font size.");
}
ts->free_rid(ctx);
for (int j = 0; j < font.size(); j++) {
ts->free_rid(font[j]);
}
font.clear();
}
}
SUBCASE("[TextServer] Text layout: BiDi") {
for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
if (!ts->has_feature(TextServer::FEATURE_FONT_DYNAMIC) || !ts->has_feature(TextServer::FEATURE_BIDI_LAYOUT)) {
continue;
}
RID font1 = ts->create_font();
ts->font_set_data_ptr(font1, _font_NotoSans_Regular, _font_NotoSans_Regular_size);
RID font2 = ts->create_font();
ts->font_set_data_ptr(font2, _font_NotoNaskhArabicUI_Regular, _font_NotoNaskhArabicUI_Regular_size);
Array font;
font.push_back(font1);
font.push_back(font2);
String test = U"Arabic (اَلْعَرَبِيَّةُ, al-ʿarabiyyah)";
// 7^ 26^
RID ctx = ts->create_shaped_text();
CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
bool ok = ts->shaped_text_add_string(ctx, test, font, 16);
CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
const Glyph *glyphs = ts->shaped_text_get_glyphs(ctx);
int gl_size = ts->shaped_text_get_glyph_count(ctx);
CHECK_FALSE_MESSAGE(gl_size == 0, "Shaping failed");
for (int j = 0; j < gl_size; j++) {
if (glyphs[j].count > 0) {
if (glyphs[j].start < 7) {
CHECK_FALSE_MESSAGE(((glyphs[j].flags & TextServer::GRAPHEME_IS_RTL) == TextServer::GRAPHEME_IS_RTL), "Incorrect direction.");
}
if ((glyphs[j].start > 8) && (glyphs[j].start < 23)) {
CHECK_FALSE_MESSAGE(((glyphs[j].flags & TextServer::GRAPHEME_IS_RTL) != TextServer::GRAPHEME_IS_RTL), "Incorrect direction.");
}
if (glyphs[j].start > 26) {
CHECK_FALSE_MESSAGE(((glyphs[j].flags & TextServer::GRAPHEME_IS_RTL) == TextServer::GRAPHEME_IS_RTL), "Incorrect direction.");
}
}
}
ts->free_rid(ctx);
for (int j = 0; j < font.size(); j++) {
ts->free_rid(font[j]);
}
font.clear();
}
}
SUBCASE("[TextServer] Text layout: Line break and align points") {
for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
if (!ts->has_feature(TextServer::FEATURE_FONT_DYNAMIC) || !ts->has_feature(TextServer::FEATURE_SIMPLE_LAYOUT)) {
continue;
}
RID font1 = ts->create_font();
ts->font_set_data_ptr(font1, _font_NotoSans_Regular, _font_NotoSans_Regular_size);
ts->font_set_allow_system_fallback(font1, false);
RID font2 = ts->create_font();
ts->font_set_data_ptr(font2, _font_NotoSansThai_Regular, _font_NotoSansThai_Regular_size);
ts->font_set_allow_system_fallback(font2, false);
RID font3 = ts->create_font();
ts->font_set_data_ptr(font3, _font_NotoNaskhArabicUI_Regular, _font_NotoNaskhArabicUI_Regular_size);
ts->font_set_allow_system_fallback(font3, false);
Array font;
font.push_back(font1);
font.push_back(font2);
font.push_back(font3);
{
RID ctx = ts->create_shaped_text();
CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
ts->shaped_text_add_string(ctx, U"Xtest", font, 10);
ts->shaped_text_add_string(ctx, U"xs", font, 10);
RID sctx = ts->shaped_text_substr(ctx, 1, 5);
CHECK_FALSE_MESSAGE(sctx == RID(), "Creating substring text buffer failed.");
PackedInt32Array sbrk = ts->shaped_text_get_character_breaks(sctx);
CHECK_FALSE_MESSAGE(sbrk.size() != 5, "Invalid substring char breaks number.");
if (sbrk.size() == 5) {
CHECK_FALSE_MESSAGE(sbrk[0] != 2, "Invalid substring char break position.");
CHECK_FALSE_MESSAGE(sbrk[1] != 3, "Invalid substring char break position.");
CHECK_FALSE_MESSAGE(sbrk[2] != 4, "Invalid substring char break position.");
CHECK_FALSE_MESSAGE(sbrk[3] != 5, "Invalid substring char break position.");
CHECK_FALSE_MESSAGE(sbrk[4] != 6, "Invalid substring char break position.");
}
PackedInt32Array fbrk = ts->shaped_text_get_character_breaks(ctx);
CHECK_FALSE_MESSAGE(fbrk.size() != 7, "Invalid char breaks number.");
if (fbrk.size() == 7) {
CHECK_FALSE_MESSAGE(fbrk[0] != 1, "Invalid char break position.");
CHECK_FALSE_MESSAGE(fbrk[1] != 2, "Invalid char break position.");
CHECK_FALSE_MESSAGE(fbrk[2] != 3, "Invalid char break position.");
CHECK_FALSE_MESSAGE(fbrk[3] != 4, "Invalid char break position.");
CHECK_FALSE_MESSAGE(fbrk[4] != 5, "Invalid char break position.");
CHECK_FALSE_MESSAGE(fbrk[5] != 6, "Invalid char break position.");
CHECK_FALSE_MESSAGE(fbrk[6] != 7, "Invalid char break position.");
}
PackedInt32Array rbrk = ts->string_get_character_breaks(U"Xtestxs");
CHECK_FALSE_MESSAGE(rbrk.size() != 7, "Invalid char breaks number.");
if (rbrk.size() == 7) {
CHECK_FALSE_MESSAGE(rbrk[0] != 1, "Invalid char break position.");
CHECK_FALSE_MESSAGE(rbrk[1] != 2, "Invalid char break position.");
CHECK_FALSE_MESSAGE(rbrk[2] != 3, "Invalid char break position.");
CHECK_FALSE_MESSAGE(rbrk[3] != 4, "Invalid char break position.");
CHECK_FALSE_MESSAGE(rbrk[4] != 5, "Invalid char break position.");
CHECK_FALSE_MESSAGE(rbrk[5] != 6, "Invalid char break position.");
CHECK_FALSE_MESSAGE(rbrk[6] != 7, "Invalid char break position.");
}
ts->free_rid(sctx);
ts->free_rid(ctx);
}
if (ts->has_feature(TextServer::FEATURE_BREAK_ITERATORS)) {
RID ctx = ts->create_shaped_text();
CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
ts->shaped_text_add_string(ctx, U"X❤🔥", font, 10);
ts->shaped_text_add_string(ctx, U"xs", font, 10);
RID sctx = ts->shaped_text_substr(ctx, 1, 5);
CHECK_FALSE_MESSAGE(sctx == RID(), "Creating substring text buffer failed.");
PackedInt32Array sbrk = ts->shaped_text_get_character_breaks(sctx);
CHECK_FALSE_MESSAGE(sbrk.size() != 2, "Invalid substring char breaks number.");
if (sbrk.size() == 2) {
CHECK_FALSE_MESSAGE(sbrk[0] != 5, "Invalid substring char break position.");
CHECK_FALSE_MESSAGE(sbrk[1] != 6, "Invalid substring char break position.");
}
PackedInt32Array fbrk = ts->shaped_text_get_character_breaks(ctx);
CHECK_FALSE_MESSAGE(fbrk.size() != 4, "Invalid char breaks number.");
if (fbrk.size() == 4) {
CHECK_FALSE_MESSAGE(fbrk[0] != 1, "Invalid char break position.");
CHECK_FALSE_MESSAGE(fbrk[1] != 5, "Invalid char break position.");
CHECK_FALSE_MESSAGE(fbrk[2] != 6, "Invalid char break position.");
CHECK_FALSE_MESSAGE(fbrk[3] != 7, "Invalid char break position.");
}
PackedInt32Array rbrk = ts->string_get_character_breaks(U"X❤🔥xs");
CHECK_FALSE_MESSAGE(rbrk.size() != 4, "Invalid char breaks number.");
if (rbrk.size() == 4) {
CHECK_FALSE_MESSAGE(rbrk[0] != 1, "Invalid char break position.");
CHECK_FALSE_MESSAGE(rbrk[1] != 5, "Invalid char break position.");
CHECK_FALSE_MESSAGE(rbrk[2] != 6, "Invalid char break position.");
CHECK_FALSE_MESSAGE(rbrk[3] != 7, "Invalid char break position.");
}
ts->free_rid(sctx);
ts->free_rid(ctx);
}
{
String test = U"Test test long text long text\n";
RID ctx = ts->create_shaped_text();
CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
bool ok = ts->shaped_text_add_string(ctx, test, font, 16);
CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
ts->shaped_text_update_breaks(ctx);
ts->shaped_text_update_justification_ops(ctx);
const Glyph *glyphs = ts->shaped_text_get_glyphs(ctx);
int gl_size = ts->shaped_text_get_glyph_count(ctx);
CHECK_FALSE_MESSAGE(gl_size != 30, "Invalid glyph count.");
for (int j = 0; j < gl_size; j++) {
bool hard = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_HARD) == TextServer::GRAPHEME_IS_BREAK_HARD;
bool soft = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_SOFT) == TextServer::GRAPHEME_IS_BREAK_SOFT;
bool space = (glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE;
bool virt = (glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL;
bool elo = (glyphs[j].flags & TextServer::GRAPHEME_IS_ELONGATION) == TextServer::GRAPHEME_IS_ELONGATION;
if (j == 4 || j == 9 || j == 14 || j == 19 || j == 24) {
CHECK_FALSE_MESSAGE((!soft || !space || hard || virt || elo), "Invalid glyph flags.");
} else if (j == 29) {
CHECK_FALSE_MESSAGE((soft || !space || !hard || virt || elo), "Invalid glyph flags.");
} else {
CHECK_FALSE_MESSAGE((soft || space || hard || virt || elo), "Invalid glyph flags.");
}
}
ts->free_rid(ctx);
}
{
String test = U"الحمـد";
RID ctx = ts->create_shaped_text();
CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
bool ok = ts->shaped_text_add_string(ctx, test, font, 16);
CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
ts->shaped_text_update_breaks(ctx);
const Glyph *glyphs = ts->shaped_text_get_glyphs(ctx);
int gl_size = ts->shaped_text_get_glyph_count(ctx);
CHECK_FALSE_MESSAGE(gl_size != 6, "Invalid glyph count.");
for (int j = 0; j < gl_size; j++) {
bool hard = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_HARD) == TextServer::GRAPHEME_IS_BREAK_HARD;
bool soft = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_SOFT) == TextServer::GRAPHEME_IS_BREAK_SOFT;
bool space = (glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE;
bool virt = (glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL;
bool elo = (glyphs[j].flags & TextServer::GRAPHEME_IS_ELONGATION) == TextServer::GRAPHEME_IS_ELONGATION;
CHECK_FALSE_MESSAGE((soft || space || hard || virt || elo), "Invalid glyph flags.");
}
if (ts->has_feature(TextServer::FEATURE_KASHIDA_JUSTIFICATION)) {
ts->shaped_text_update_justification_ops(ctx);
glyphs = ts->shaped_text_get_glyphs(ctx);
gl_size = ts->shaped_text_get_glyph_count(ctx);
CHECK_FALSE_MESSAGE(gl_size != 6, "Invalid glyph count.");
for (int j = 0; j < gl_size; j++) {
bool hard = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_HARD) == TextServer::GRAPHEME_IS_BREAK_HARD;
bool soft = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_SOFT) == TextServer::GRAPHEME_IS_BREAK_SOFT;
bool space = (glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE;
bool virt = (glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL;
bool elo = (glyphs[j].flags & TextServer::GRAPHEME_IS_ELONGATION) == TextServer::GRAPHEME_IS_ELONGATION;
if (j == 1) {
CHECK_FALSE_MESSAGE((soft || space || hard || virt || !elo), "Invalid glyph flags.");
} else {
CHECK_FALSE_MESSAGE((soft || space || hard || virt || elo), "Invalid glyph flags.");
}
}
}
ts->free_rid(ctx);
}
{
String test = U"الحمد";
RID ctx = ts->create_shaped_text();
CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
bool ok = ts->shaped_text_add_string(ctx, test, font, 16);
CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
ts->shaped_text_update_breaks(ctx);
const Glyph *glyphs = ts->shaped_text_get_glyphs(ctx);
int gl_size = ts->shaped_text_get_glyph_count(ctx);
CHECK_FALSE_MESSAGE(gl_size != 5, "Invalid glyph count.");
for (int j = 0; j < gl_size; j++) {
bool hard = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_HARD) == TextServer::GRAPHEME_IS_BREAK_HARD;
bool soft = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_SOFT) == TextServer::GRAPHEME_IS_BREAK_SOFT;
bool space = (glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE;
bool virt = (glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL;
bool elo = (glyphs[j].flags & TextServer::GRAPHEME_IS_ELONGATION) == TextServer::GRAPHEME_IS_ELONGATION;
CHECK_FALSE_MESSAGE((soft || space || hard || virt || elo), "Invalid glyph flags.");
}
if (ts->has_feature(TextServer::FEATURE_KASHIDA_JUSTIFICATION)) {
ts->shaped_text_update_justification_ops(ctx);
glyphs = ts->shaped_text_get_glyphs(ctx);
gl_size = ts->shaped_text_get_glyph_count(ctx);
CHECK_FALSE_MESSAGE(gl_size != 6, "Invalid glyph count.");
for (int j = 0; j < gl_size; j++) {
bool hard = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_HARD) == TextServer::GRAPHEME_IS_BREAK_HARD;
bool soft = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_SOFT) == TextServer::GRAPHEME_IS_BREAK_SOFT;
bool space = (glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE;
bool virt = (glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL;
bool elo = (glyphs[j].flags & TextServer::GRAPHEME_IS_ELONGATION) == TextServer::GRAPHEME_IS_ELONGATION;
if (j == 1) {
CHECK_FALSE_MESSAGE((soft || space || hard || !virt || !elo), "Invalid glyph flags.");
} else {
CHECK_FALSE_MESSAGE((soft || space || hard || virt || elo), "Invalid glyph flags.");
}
}
}
ts->free_rid(ctx);
}
{
String test = U"الحمـد الرياضي العربي";
RID ctx = ts->create_shaped_text();
CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
bool ok = ts->shaped_text_add_string(ctx, test, font, 16);
CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
ts->shaped_text_update_breaks(ctx);
const Glyph *glyphs = ts->shaped_text_get_glyphs(ctx);
int gl_size = ts->shaped_text_get_glyph_count(ctx);
CHECK_FALSE_MESSAGE(gl_size != 21, "Invalid glyph count.");
for (int j = 0; j < gl_size; j++) {
bool hard = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_HARD) == TextServer::GRAPHEME_IS_BREAK_HARD;
bool soft = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_SOFT) == TextServer::GRAPHEME_IS_BREAK_SOFT;
bool space = (glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE;
bool virt = (glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL;
bool elo = (glyphs[j].flags & TextServer::GRAPHEME_IS_ELONGATION) == TextServer::GRAPHEME_IS_ELONGATION;
if (j == 6 || j == 14) {
CHECK_FALSE_MESSAGE((!soft || !space || hard || virt || elo), "Invalid glyph flags.");
} else {
CHECK_FALSE_MESSAGE((soft || space || hard || virt || elo), "Invalid glyph flags.");
}
}
if (ts->has_feature(TextServer::FEATURE_KASHIDA_JUSTIFICATION)) {
ts->shaped_text_update_justification_ops(ctx);
glyphs = ts->shaped_text_get_glyphs(ctx);
gl_size = ts->shaped_text_get_glyph_count(ctx);
CHECK_FALSE_MESSAGE(gl_size != 23, "Invalid glyph count.");
for (int j = 0; j < gl_size; j++) {
bool hard = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_HARD) == TextServer::GRAPHEME_IS_BREAK_HARD;
bool soft = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_SOFT) == TextServer::GRAPHEME_IS_BREAK_SOFT;
bool space = (glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE;
bool virt = (glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL;
bool elo = (glyphs[j].flags & TextServer::GRAPHEME_IS_ELONGATION) == TextServer::GRAPHEME_IS_ELONGATION;
if (j == 7 || j == 16) {
CHECK_FALSE_MESSAGE((!soft || !space || hard || virt || elo), "Invalid glyph flags.");
} else if (j == 3 || j == 9) {
CHECK_FALSE_MESSAGE((soft || space || hard || !virt || !elo), "Invalid glyph flags.");
} else if (j == 18) {
CHECK_FALSE_MESSAGE((soft || space || hard || virt || !elo), "Invalid glyph flags.");
} else {
CHECK_FALSE_MESSAGE((soft || space || hard || virt || elo), "Invalid glyph flags.");
}
}
}
ts->free_rid(ctx);
}
{
String test = U"เป็น ภาษา ราชการ และ ภาษา";
RID ctx = ts->create_shaped_text();
CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
bool ok = ts->shaped_text_add_string(ctx, test, font, 16);
CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
ts->shaped_text_update_breaks(ctx);
ts->shaped_text_update_justification_ops(ctx);
const Glyph *glyphs = ts->shaped_text_get_glyphs(ctx);
int gl_size = ts->shaped_text_get_glyph_count(ctx);
CHECK_FALSE_MESSAGE(gl_size != 25, "Invalid glyph count.");
for (int j = 0; j < gl_size; j++) {
bool hard = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_HARD) == TextServer::GRAPHEME_IS_BREAK_HARD;
bool soft = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_SOFT) == TextServer::GRAPHEME_IS_BREAK_SOFT;
bool space = (glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE;
bool virt = (glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL;
bool elo = (glyphs[j].flags & TextServer::GRAPHEME_IS_ELONGATION) == TextServer::GRAPHEME_IS_ELONGATION;
if (j == 4 || j == 9 || j == 16 || j == 20) {
CHECK_FALSE_MESSAGE((!soft || !space || hard || virt || elo), "Invalid glyph flags.");
} else {
CHECK_FALSE_MESSAGE((soft || space || hard || virt || elo), "Invalid glyph flags.");
}
}
ts->free_rid(ctx);
}
if (ts->has_feature(TextServer::FEATURE_BREAK_ITERATORS)) {
String test = U"เป็นภาษาราชการและภาษา";
RID ctx = ts->create_shaped_text();
CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
bool ok = ts->shaped_text_add_string(ctx, test, font, 16);
CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
ts->shaped_text_update_breaks(ctx);
ts->shaped_text_update_justification_ops(ctx);
const Glyph *glyphs = ts->shaped_text_get_glyphs(ctx);
int gl_size = ts->shaped_text_get_glyph_count(ctx);
CHECK_FALSE_MESSAGE(gl_size != 25, "Invalid glyph count.");
for (int j = 0; j < gl_size; j++) {
bool hard = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_HARD) == TextServer::GRAPHEME_IS_BREAK_HARD;
bool soft = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_SOFT) == TextServer::GRAPHEME_IS_BREAK_SOFT;
bool space = (glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE;
bool virt = (glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL;
bool elo = (glyphs[j].flags & TextServer::GRAPHEME_IS_ELONGATION) == TextServer::GRAPHEME_IS_ELONGATION;
if (j == 4 || j == 9 || j == 16 || j == 20) {
CHECK_FALSE_MESSAGE((!soft || !space || hard || !virt || elo), "Invalid glyph flags.");
} else {
CHECK_FALSE_MESSAGE((soft || space || hard || virt || elo), "Invalid glyph flags.");
}
}
ts->free_rid(ctx);
}
for (int j = 0; j < font.size(); j++) {
ts->free_rid(font[j]);
}
font.clear();
}
}
SUBCASE("[TextServer] Text layout: Line breaking") {
for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
if (!ts->has_feature(TextServer::FEATURE_FONT_DYNAMIC) || !ts->has_feature(TextServer::FEATURE_SIMPLE_LAYOUT)) {
continue;
}
String test_1 = U"test test test";
// 5^ 10^
RID font1 = ts->create_font();
ts->font_set_data_ptr(font1, _font_NotoSans_Regular, _font_NotoSans_Regular_size);
RID font2 = ts->create_font();
ts->font_set_data_ptr(font2, _font_NotoSansThai_Regular, _font_NotoSansThai_Regular_size);
Array font;
font.push_back(font1);
font.push_back(font2);
RID ctx = ts->create_shaped_text();
CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
bool ok = ts->shaped_text_add_string(ctx, test_1, font, 16);
CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
PackedInt32Array brks = ts->shaped_text_get_line_breaks(ctx, 1);
CHECK_FALSE_MESSAGE(brks.size() != 6, "Invalid line breaks number.");
if (brks.size() == 6) {
CHECK_FALSE_MESSAGE(brks[0] != 0, "Invalid line break position.");
CHECK_FALSE_MESSAGE(brks[1] != 5, "Invalid line break position.");
CHECK_FALSE_MESSAGE(brks[2] != 5, "Invalid line break position.");
CHECK_FALSE_MESSAGE(brks[3] != 10, "Invalid line break position.");
CHECK_FALSE_MESSAGE(brks[4] != 10, "Invalid line break position.");
CHECK_FALSE_MESSAGE(brks[5] != 14, "Invalid line break position.");
}
ts->free_rid(ctx);
for (int j = 0; j < font.size(); j++) {
ts->free_rid(font[j]);
}
font.clear();
}
}
SUBCASE("[TextServer] Text layout: Justification") {
for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
if (!ts->has_feature(TextServer::FEATURE_FONT_DYNAMIC) || !ts->has_feature(TextServer::FEATURE_SIMPLE_LAYOUT)) {
continue;
}
RID font1 = ts->create_font();
ts->font_set_data_ptr(font1, _font_NotoSans_Regular, _font_NotoSans_Regular_size);
RID font2 = ts->create_font();
ts->font_set_data_ptr(font2, _font_NotoNaskhArabicUI_Regular, _font_NotoNaskhArabicUI_Regular_size);
Array font;
font.push_back(font1);
font.push_back(font2);
String test_1 = U"الحمد";
String test_2 = U"الحمد test";
String test_3 = U"test test";
// 7^ 26^
RID ctx;
bool ok;
float width_old, width;
if (ts->has_feature(TextServer::FEATURE_KASHIDA_JUSTIFICATION)) {
ctx = ts->create_shaped_text();
CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
ok = ts->shaped_text_add_string(ctx, test_1, font, 16);
CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
width_old = ts->shaped_text_get_width(ctx);
width = ts->shaped_text_fit_to_width(ctx, 100, TextServer::JUSTIFICATION_WORD_BOUND);
CHECK_FALSE_MESSAGE((width != width_old), "Invalid fill width.");
width = ts->shaped_text_fit_to_width(ctx, 100, TextServer::JUSTIFICATION_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA);
CHECK_FALSE_MESSAGE((width <= width_old || width > 100), "Invalid fill width.");
ts->free_rid(ctx);
ctx = ts->create_shaped_text();
CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
ok = ts->shaped_text_add_string(ctx, test_2, font, 16);
CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
width_old = ts->shaped_text_get_width(ctx);
width = ts->shaped_text_fit_to_width(ctx, 100, TextServer::JUSTIFICATION_WORD_BOUND);
CHECK_FALSE_MESSAGE((width <= width_old || width > 100), "Invalid fill width.");
width = ts->shaped_text_fit_to_width(ctx, 100, TextServer::JUSTIFICATION_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA);
CHECK_FALSE_MESSAGE((width <= width_old || width > 100), "Invalid fill width.");
ts->free_rid(ctx);
}
ctx = ts->create_shaped_text();
CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
ok = ts->shaped_text_add_string(ctx, test_3, font, 16);
CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
width_old = ts->shaped_text_get_width(ctx);
width = ts->shaped_text_fit_to_width(ctx, 100, TextServer::JUSTIFICATION_WORD_BOUND);
CHECK_FALSE_MESSAGE((width <= width_old || width > 100), "Invalid fill width.");
ts->free_rid(ctx);
for (int j = 0; j < font.size(); j++) {
ts->free_rid(font[j]);
}
font.clear();
}
}
SUBCASE("[TextServer] Unicode identifiers") {
for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
static const char32_t *data[19] = { U"-30", U"100", U"10.1", U"10,1", U"1e2", U"1e-2", U"1e2e3", U"0xAB", U"AB", U"Test1", U"1Test", U"Test*1", U"test_testeT", U"test_tes teT", U"عَلَيْكُمْ", U"عَلَيْكُمْTest", U"ӒӖӚӜ", U"_test", U"ÂÃÄÅĀĂĄÇĆĈĊ" };
static bool isid[19] = { false, false, false, false, false, false, false, false, true, true, false, false, true, false, true, true, true, true, true };
for (int j = 0; j < 19; j++) {
String s = String(data[j]);
CHECK(ts->is_valid_identifier(s) == isid[j]);
}
if (ts->has_feature(TextServer::FEATURE_UNICODE_IDENTIFIERS)) {
// Test UAX 3.2 ZW(N)J usage.
CHECK(ts->is_valid_identifier(U"\u0646\u0627\u0645\u0647\u200C\u0627\u06CC"));
CHECK(ts->is_valid_identifier(U"\u0D26\u0D43\u0D15\u0D4D\u200C\u0D38\u0D3E\u0D15\u0D4D\u0D37\u0D3F"));
CHECK(ts->is_valid_identifier(U"\u0DC1\u0DCA\u200D\u0DBB\u0DD3"));
}
}
}
SUBCASE("[TextServer] Unicode letters") {
for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
struct ul_testcase {
int fail_index = -1; // Expecting failure at given index.
char32_t text[10]; // Using 0 as the terminator.
};
ul_testcase cases[14] = {
{
0,
{ 0x2D, 0x33, 0x30, 0, 0, 0, 0, 0, 0, 0 }, // "-30"
},
{
1,
{ 0x61, 0x2E, 0x31, 0, 0, 0, 0, 0, 0, 0 }, // "a.1"
},
{
1,
{ 0x61, 0x2C, 0x31, 0, 0, 0, 0, 0, 0, 0 }, // "a,1"
},
{
0,
{ 0x31, 0x65, 0x2D, 0x32, 0, 0, 0, 0, 0, 0 }, // "1e-2"
},
{
0,
{ 0xAB, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // "Left-Pointing Double Angle Quotation Mark"
},
{
-1,
{ 0x41, 0x42, 0, 0, 0, 0, 0, 0, 0, 0 }, // "AB"
},
{
4,
{ 0x54, 0x65, 0x73, 0x74, 0x31, 0, 0, 0, 0, 0 }, // "Test1"
},
{
2,
{ 0x54, 0x65, 0x2A, 0x73, 0x74, 0, 0, 0, 0, 0 }, // "Te*st"
},
{
4,
{ 0x74, 0x65, 0x73, 0x74, 0x5F, 0x74, 0x65, 0x73, 0x74, 0x65 }, // "test_teste"
},
{
4,
{ 0x74, 0x65, 0x73, 0x74, 0x20, 0x74, 0x65, 0x73, 0x74, 0 }, // "test test"
},
{
-1,
{ 0x643, 0x402, 0x716, 0xB05, 0, 0, 0, 0, 0, 0 }, // "كЂܖଅ" (arabic letters),
},
{
-1,
{ 0x643, 0x402, 0x716, 0xB05, 0x54, 0x65, 0x73, 0x74, 0x30AA, 0x4E21 }, // 0-3 arabic letters, 4-7 latin letters, 8-9 CJK letters
},
{
-1,
{ 0x4D2, 0x4D6, 0x4DA, 0x4DC, 0, 0, 0, 0, 0, 0 }, // "ӒӖӚӜ" cyrillic letters
},
{
-1,
{ 0xC2, 0xC3, 0xC4, 0xC5, 0x100, 0x102, 0x104, 0xC7, 0x106, 0x108 }, // "ÂÃÄÅĀĂĄÇĆĈ" rarer latin letters
},
};
for (int j = 0; j < 14; j++) {
ul_testcase test = cases[j];
int failed_on_index = -1;
for (int k = 0; k < 10; k++) {
char32_t character = test.text[k];
if (character == 0) {
break;
}
if (!ts->is_valid_letter(character)) {
failed_on_index = k;
break;
}
}
if (test.fail_index == -1) {
CHECK_MESSAGE(test.fail_index == failed_on_index, "In interface ", ts->get_name() + ": In test case ", j, ", the character at index ", failed_on_index, " should have been a letter.");
} else {
CHECK_MESSAGE(test.fail_index == failed_on_index, "In interface ", ts->get_name() + ": In test case ", j, ", expected first non-letter at index ", test.fail_index, ", but found at index ", failed_on_index);
}
}
}
}
SUBCASE("[TextServer] Strip Diacritics") {
for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
if (ts->has_feature(TextServer::FEATURE_SHAPING)) {
CHECK(ts->strip_diacritics(U"ٱلسَّلَامُ عَلَيْكُمْ") == U"ٱلسلام عليكم");
}
CHECK(ts->strip_diacritics(U"pêches épinards tomates fraises") == U"peches epinards tomates fraises");
CHECK(ts->strip_diacritics(U"ΆΈΉΊΌΎΏΪΫϓϔ") == U"ΑΕΗΙΟΥΩΙΥΥΥ");
CHECK(ts->strip_diacritics(U"άέήίΐϊΰϋόύώ") == U"αεηιιιυυουω");
CHECK(ts->strip_diacritics(U"ЀЁЃ ЇЌЍӢӤЙ ЎӮӰӲ ӐӒӖӚӜӞ ӦӪ Ӭ Ӵ Ӹ") == U"ЕЕГ ІКИИИИ УУУУ ААЕӘЖЗ ОӨ Э Ч Ы");
CHECK(ts->strip_diacritics(U"ѐёѓ їќѝӣӥй ўӯӱӳ ӑӓӗӛӝӟ ӧӫ ӭ ӵ ӹ") == U"еег ікииии уууу ааеәжз оө э ч ы");
CHECK(ts->strip_diacritics(U"ÀÁÂÃÄÅĀĂĄÇĆĈĊČĎÈÉÊËĒĔĖĘĚĜĞĠĢĤÌÍÎÏĨĪĬĮİĴĶĹĻĽÑŃŅŇŊÒÓÔÕÖØŌŎŐƠŔŖŘŚŜŞŠŢŤÙÚÛÜŨŪŬŮŰŲƯŴÝŶŹŻŽ") == U"AAAAAAAAACCCCCDEEEEEEEEEGGGGHIIIIIIIIIJKLLLNNNNŊOOOOOØOOOORRRSSSSTTUUUUUUUUUUUWYYZZZ");
CHECK(ts->strip_diacritics(U"àáâãäåāăąçćĉċčďèéêëēĕėęěĝğġģĥìíîïĩīĭįĵķĺļľñńņňŋòóôõöøōŏőơŕŗřśŝşšţťùúûüũūŭůűųưŵýÿŷźżž") == U"aaaaaaaaacccccdeeeeeeeeegggghiiiiiiiijklllnnnnŋoooooøoooorrrssssttuuuuuuuuuuuwyyyzzz");
CHECK(ts->strip_diacritics(U"ǍǏȈǑǪǬȌȎȪȬȮȰǓǕǗǙǛȔȖǞǠǺȀȂȦǢǼǦǴǨǸȆȐȒȘȚȞȨ Ḁ ḂḄḆ Ḉ ḊḌḎḐḒ ḔḖḘḚḜ Ḟ Ḡ ḢḤḦḨḪ ḬḮ ḰḲḴ ḶḸḺḼ ḾṀṂ ṄṆṈṊ ṌṎṐṒ ṔṖ ṘṚṜṞ ṠṢṤṦṨ ṪṬṮṰ ṲṴṶṸṺ") == U"AIIOOOOOOOOOUUUUUUUAAAAAAÆÆGGKNERRSTHE A BBB C DDDDD EEEEE F G HHHHH II KKK LLLL MMM NNNN OOOO PP RRRR SSSSS TTTT UUUUU");
CHECK(ts->strip_diacritics(U"ǎǐȉȋǒǫǭȍȏȫȭȯȱǔǖǘǚǜȕȗǟǡǻȁȃȧǣǽǧǵǩǹȇȑȓșțȟȩ ḁ ḃḅḇ ḉ ḋḍḏḑḓ ḟ ḡ ḭḯ ḱḳḵ ḷḹḻḽ ḿṁṃ ṅṇṉṋ ṍṏṑṓ ṗṕ ṙṛṝṟ ṡṣṥṧṩ ṫṭṯṱ ṳṵṷṹṻ") == U"aiiiooooooooouuuuuuuaaaaaaææggknerrsthe a bbb c ddddd f g ii kkk llll mmm nnnn oooo pp rrrr sssss tttt uuuuu");
CHECK(ts->strip_diacritics(U"ṼṾ ẀẂẄẆẈ ẊẌ Ẏ ẐẒẔ") == U"VV WWWWW XX Y ZZZ");
CHECK(ts->strip_diacritics(U"ṽṿ ẁẃẅẇẉ ẋẍ ẏ ẑẓẕ ẖ ẗẘẙẛ") == U"vv wwwww xx y zzz h twys");
}
}
SUBCASE("[TextServer] Word break") {
for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
if (!ts->has_feature(TextServer::FEATURE_SIMPLE_LAYOUT)) {
continue;
}
CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
{
String text1 = U"linguistically similar and effectively form";
// 14^ 22^ 26^ 38^
PackedInt32Array breaks = ts->string_get_word_breaks(text1, "en");
CHECK(breaks.size() == 10);
if (breaks.size() == 10) {
CHECK(breaks[0] == 0);
CHECK(breaks[1] == 14);
CHECK(breaks[2] == 15);
CHECK(breaks[3] == 22);
CHECK(breaks[4] == 23);
CHECK(breaks[5] == 26);
CHECK(breaks[6] == 27);
CHECK(breaks[7] == 38);
CHECK(breaks[8] == 39);
CHECK(breaks[9] == 43);
}
}
if (ts->has_feature(TextServer::FEATURE_BREAK_ITERATORS)) {
String text2 = U"เป็นภาษาราชการและภาษาประจำชาติของประเทศไทย";
// เป็น ภาษา ราชการ และ ภาษา ประจำ ชาติ ของ ประเทศไทย
// 3^ 7^ 13^ 16^ 20^ 25^ 29^ 32^
PackedInt32Array breaks = ts->string_get_word_breaks(text2, "th");
CHECK(breaks.size() == 18);
if (breaks.size() == 18) {
CHECK(breaks[0] == 0);
CHECK(breaks[1] == 4);
CHECK(breaks[2] == 4);
CHECK(breaks[3] == 8);
CHECK(breaks[4] == 8);
CHECK(breaks[5] == 14);
CHECK(breaks[6] == 14);
CHECK(breaks[7] == 17);
CHECK(breaks[8] == 17);
CHECK(breaks[9] == 21);
CHECK(breaks[10] == 21);
CHECK(breaks[11] == 26);
CHECK(breaks[12] == 26);
CHECK(breaks[13] == 30);
CHECK(breaks[14] == 30);
CHECK(breaks[15] == 33);
CHECK(breaks[16] == 33);
CHECK(breaks[17] == 42);
}
}
if (ts->has_feature(TextServer::FEATURE_BREAK_ITERATORS)) {
String text2 = U"U+2764 U+FE0F U+200D U+1F525 ; 13.1 # ❤️‍🔥";
PackedInt32Array breaks = ts->string_get_character_breaks(text2, "en");
CHECK(breaks.size() == 39);
if (breaks.size() == 39) {
CHECK(breaks[0] == 1);
CHECK(breaks[1] == 2);
CHECK(breaks[2] == 3);
CHECK(breaks[3] == 4);
CHECK(breaks[4] == 5);
CHECK(breaks[5] == 6);
CHECK(breaks[6] == 7);
CHECK(breaks[7] == 8);
CHECK(breaks[8] == 9);
CHECK(breaks[9] == 10);
CHECK(breaks[10] == 11);
CHECK(breaks[11] == 12);
CHECK(breaks[12] == 13);
CHECK(breaks[13] == 14);
CHECK(breaks[14] == 15);
CHECK(breaks[15] == 16);
CHECK(breaks[16] == 17);
CHECK(breaks[17] == 18);
CHECK(breaks[18] == 19);
CHECK(breaks[19] == 20);
CHECK(breaks[20] == 21);
CHECK(breaks[21] == 22);
CHECK(breaks[22] == 23);
CHECK(breaks[23] == 24);
CHECK(breaks[24] == 25);
CHECK(breaks[25] == 26);
CHECK(breaks[26] == 27);
CHECK(breaks[27] == 28);
CHECK(breaks[28] == 29);
CHECK(breaks[29] == 30);
CHECK(breaks[30] == 31);
CHECK(breaks[31] == 32);
CHECK(breaks[32] == 33);
CHECK(breaks[33] == 34);
CHECK(breaks[34] == 35);
CHECK(breaks[35] == 36);
CHECK(breaks[36] == 37);
CHECK(breaks[37] == 38);
CHECK(breaks[38] == 42);
}
}
}
}
}
}
}; // namespace TestTextServer
#endif // TOOLS_ENABLED
#endif // TEST_TEXT_SERVER_H