Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/test_main.cpp
20816 views
1
/**************************************************************************/
2
/* test_main.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "test_main.h"
32
33
#include "core/error/error_macros.h"
34
#include "core/io/dir_access.h"
35
#include "modules/modules_enabled.gen.h"
36
37
#ifdef TOOLS_ENABLED
38
#include "editor/file_system/editor_paths.h"
39
#include "editor/settings/editor_settings.h"
40
#endif // TOOLS_ENABLED
41
42
#include "tests/core/config/test_project_settings.h"
43
#include "tests/core/input/test_input_event.h"
44
#include "tests/core/input/test_input_event_key.h"
45
#include "tests/core/input/test_input_event_mouse.h"
46
#include "tests/core/input/test_shortcut.h"
47
#include "tests/core/io/test_config_file.h"
48
#include "tests/core/io/test_file_access.h"
49
#include "tests/core/io/test_http_client.h"
50
#include "tests/core/io/test_image.h"
51
#include "tests/core/io/test_ip.h"
52
#include "tests/core/io/test_ip_address.h"
53
#include "tests/core/io/test_json.h"
54
#include "tests/core/io/test_json_native.h"
55
#include "tests/core/io/test_logger.h"
56
#include "tests/core/io/test_marshalls.h"
57
#include "tests/core/io/test_packet_peer.h"
58
#include "tests/core/io/test_pck_packer.h"
59
#include "tests/core/io/test_resource.h"
60
#include "tests/core/io/test_resource_uid.h"
61
#include "tests/core/io/test_stream_peer.h"
62
#include "tests/core/io/test_stream_peer_buffer.h"
63
#include "tests/core/io/test_stream_peer_gzip.h"
64
#include "tests/core/io/test_stream_peer_tcp.h"
65
#include "tests/core/io/test_tcp_server.h"
66
#include "tests/core/io/test_udp_server.h"
67
#include "tests/core/io/test_uds_server.h"
68
#include "tests/core/io/test_xml_parser.h"
69
#include "tests/core/math/test_aabb.h"
70
#include "tests/core/math/test_astar.h"
71
#include "tests/core/math/test_basis.h"
72
#include "tests/core/math/test_color.h"
73
#include "tests/core/math/test_expression.h"
74
#include "tests/core/math/test_geometry_2d.h"
75
#include "tests/core/math/test_geometry_3d.h"
76
#include "tests/core/math/test_math_funcs.h"
77
#include "tests/core/math/test_plane.h"
78
#include "tests/core/math/test_projection.h"
79
#include "tests/core/math/test_quaternion.h"
80
#include "tests/core/math/test_random_number_generator.h"
81
#include "tests/core/math/test_rect2.h"
82
#include "tests/core/math/test_rect2i.h"
83
#include "tests/core/math/test_transform_2d.h"
84
#include "tests/core/math/test_transform_3d.h"
85
#include "tests/core/math/test_vector2.h"
86
#include "tests/core/math/test_vector2i.h"
87
#include "tests/core/math/test_vector3.h"
88
#include "tests/core/math/test_vector3i.h"
89
#include "tests/core/math/test_vector4.h"
90
#include "tests/core/math/test_vector4i.h"
91
#include "tests/core/object/test_class_db.h"
92
#include "tests/core/object/test_method_bind.h"
93
#include "tests/core/object/test_object.h"
94
#include "tests/core/object/test_undo_redo.h"
95
#include "tests/core/os/test_os.h"
96
#include "tests/core/string/test_fuzzy_search.h"
97
#include "tests/core/string/test_node_path.h"
98
#include "tests/core/string/test_string.h"
99
#include "tests/core/string/test_translation.h"
100
#include "tests/core/string/test_translation_server.h"
101
#include "tests/core/templates/test_a_hash_map.h"
102
#include "tests/core/templates/test_command_queue.h"
103
#include "tests/core/templates/test_fixed_vector.h"
104
#include "tests/core/templates/test_hash_map.h"
105
#include "tests/core/templates/test_hash_set.h"
106
#include "tests/core/templates/test_list.h"
107
#include "tests/core/templates/test_local_vector.h"
108
#include "tests/core/templates/test_lru.h"
109
#include "tests/core/templates/test_paged_array.h"
110
#include "tests/core/templates/test_rid.h"
111
#include "tests/core/templates/test_self_list.h"
112
#include "tests/core/templates/test_span.h"
113
#include "tests/core/templates/test_vector.h"
114
#include "tests/core/templates/test_vset.h"
115
#include "tests/core/test_crypto.h"
116
#include "tests/core/test_hashing_context.h"
117
#include "tests/core/test_time.h"
118
#include "tests/core/threads/test_worker_thread_pool.h"
119
#include "tests/core/variant/test_array.h"
120
#include "tests/core/variant/test_callable.h"
121
#include "tests/core/variant/test_dictionary.h"
122
#include "tests/core/variant/test_variant.h"
123
#include "tests/core/variant/test_variant_utility.h"
124
#include "tests/scene/test_animation.h"
125
#include "tests/scene/test_animation_blend_tree.h"
126
#include "tests/scene/test_animation_player.h"
127
#include "tests/scene/test_audio_stream_wav.h"
128
#include "tests/scene/test_bit_map.h"
129
#include "tests/scene/test_button.h"
130
#include "tests/scene/test_camera_2d.h"
131
#include "tests/scene/test_control.h"
132
#include "tests/scene/test_curve.h"
133
#include "tests/scene/test_curve_2d.h"
134
#include "tests/scene/test_curve_3d.h"
135
#include "tests/scene/test_fontfile.h"
136
#include "tests/scene/test_gradient.h"
137
#include "tests/scene/test_gradient_texture.h"
138
#include "tests/scene/test_image_texture.h"
139
#include "tests/scene/test_image_texture_3d.h"
140
#include "tests/scene/test_instance_placeholder.h"
141
#include "tests/scene/test_node.h"
142
#include "tests/scene/test_node_2d.h"
143
#include "tests/scene/test_packed_scene.h"
144
#include "tests/scene/test_parallax_2d.h"
145
#include "tests/scene/test_path_2d.h"
146
#include "tests/scene/test_path_follow_2d.h"
147
#include "tests/scene/test_sprite_2d.h"
148
#include "tests/scene/test_sprite_frames.h"
149
#include "tests/scene/test_style_box_texture.h"
150
#include "tests/scene/test_texture_progress_bar.h"
151
#include "tests/scene/test_theme.h"
152
#include "tests/scene/test_timer.h"
153
#include "tests/scene/test_viewport.h"
154
#include "tests/scene/test_visual_shader.h"
155
#include "tests/scene/test_window.h"
156
#include "tests/servers/rendering/test_shader_preprocessor.h"
157
#include "tests/servers/test_nav_heap.h"
158
#include "tests/servers/test_text_server.h"
159
#include "tests/test_validate_testing.h"
160
161
#ifndef ADVANCED_GUI_DISABLED
162
#include "tests/scene/test_code_edit.h"
163
#include "tests/scene/test_color_picker.h"
164
#include "tests/scene/test_graph_node.h"
165
#include "tests/scene/test_option_button.h"
166
#include "tests/scene/test_split_container.h"
167
#include "tests/scene/test_tab_bar.h"
168
#include "tests/scene/test_tab_container.h"
169
#include "tests/scene/test_text_edit.h"
170
#include "tests/scene/test_tree.h"
171
#endif // ADVANCED_GUI_DISABLED
172
173
#ifndef _3D_DISABLED
174
#include "tests/core/math/test_triangle_mesh.h"
175
#include "tests/scene/test_arraymesh.h"
176
#include "tests/scene/test_camera_3d.h"
177
#include "tests/scene/test_convert_transform_modifier_3d.h"
178
#include "tests/scene/test_copy_transform_modifier_3d.h"
179
#include "tests/scene/test_decal.h"
180
#ifdef MODULE_GLTF_ENABLED
181
#include "tests/scene/test_gltf_document.h"
182
#endif
183
#include "tests/scene/test_path_3d.h"
184
#include "tests/scene/test_path_follow_3d.h"
185
#include "tests/scene/test_primitives.h"
186
#include "tests/scene/test_skeleton_3d.h"
187
#include "tests/scene/test_sky.h"
188
#endif // _3D_DISABLED
189
190
#ifndef PHYSICS_3D_DISABLED
191
#include "tests/scene/test_height_map_shape_3d.h"
192
#include "tests/scene/test_physics_material.h"
193
#endif // PHYSICS_3D_DISABLED
194
195
#ifdef MODULE_NAVIGATION_2D_ENABLED
196
#include "tests/scene/test_navigation_agent_2d.h"
197
#include "tests/scene/test_navigation_obstacle_2d.h"
198
#include "tests/scene/test_navigation_region_2d.h"
199
#include "tests/servers/test_navigation_server_2d.h"
200
#include "tests/servers/test_triangle2.h"
201
#endif // MODULE_NAVIGATION_2D_ENABLED
202
203
#ifdef MODULE_NAVIGATION_3D_ENABLED
204
#include "tests/scene/test_navigation_agent_3d.h"
205
#include "tests/scene/test_navigation_obstacle_3d.h"
206
#include "tests/scene/test_navigation_region_3d.h"
207
#include "tests/servers/test_navigation_server_3d.h"
208
#endif // MODULE_NAVIGATION_3D_ENABLED
209
210
#include "modules/modules_tests.gen.h"
211
212
#include "tests/display_server_mock.h"
213
#include "tests/test_macros.h"
214
215
#include "scene/theme/theme_db.h"
216
217
#ifndef NAVIGATION_2D_DISABLED
218
#include "servers/navigation_2d/navigation_server_2d.h"
219
#endif // NAVIGATION_2D_DISABLED
220
#ifndef NAVIGATION_3D_DISABLED
221
#include "servers/navigation_3d/navigation_server_3d.h"
222
#endif // NAVIGATION_3D_DISABLED
223
224
#ifndef PHYSICS_2D_DISABLED
225
#include "servers/physics_2d/physics_server_2d.h"
226
#include "servers/physics_2d/physics_server_2d_dummy.h"
227
#endif // PHYSICS_2D_DISABLED
228
#ifndef PHYSICS_3D_DISABLED
229
#include "servers/physics_3d/physics_server_3d.h"
230
#include "servers/physics_3d/physics_server_3d_dummy.h"
231
#endif // PHYSICS_3D_DISABLED
232
233
#include "servers/rendering/rendering_server_default.h"
234
235
int test_main(int argc, char *argv[]) {
236
bool run_tests = true;
237
238
// Convert arguments to Godot's command-line.
239
List<String> args;
240
241
for (int i = 0; i < argc; i++) {
242
args.push_back(String::utf8(argv[i]));
243
}
244
OS::get_singleton()->set_cmdline("", args, List<String>());
245
DisplayServerMock::register_mock_driver();
246
247
WorkerThreadPool::get_singleton()->init();
248
249
{
250
const String test_path = TestUtils::get_temp_path("");
251
Ref<DirAccess> da = DirAccess::open(test_path); // get_temp_path() automatically creates the folder.
252
ERR_FAIL_COND_V(da.is_null(), 0);
253
ERR_FAIL_COND_V_MSG(da->erase_contents_recursive() != OK, 0, "Failed to delete files");
254
}
255
256
// Run custom test tools.
257
if (test_commands) {
258
for (const KeyValue<String, TestFunc> &E : (*test_commands)) {
259
if (args.find(E.key)) {
260
const TestFunc &test_func = E.value;
261
test_func();
262
run_tests = false;
263
break;
264
}
265
}
266
if (!run_tests) {
267
delete test_commands;
268
return 0;
269
}
270
}
271
// Doctest runner.
272
doctest::Context test_context;
273
LocalVector<String> test_args;
274
275
// Clean arguments of "--test" from the args.
276
for (int x = 0; x < argc; x++) {
277
String arg = String(argv[x]);
278
if (arg != "--test") {
279
test_args.push_back(arg);
280
}
281
}
282
283
if (test_args.size() > 0) {
284
// Convert Godot command line arguments back to standard arguments.
285
char **doctest_args = new char *[test_args.size()];
286
for (uint32_t x = 0; x < test_args.size(); x++) {
287
// Operation to convert Godot string to non wchar string.
288
CharString cs = test_args[x].utf8();
289
const char *str = cs.get_data();
290
// Allocate the string copy.
291
doctest_args[x] = new char[strlen(str) + 1];
292
// Copy this into memory.
293
memcpy(doctest_args[x], str, strlen(str) + 1);
294
}
295
296
test_context.applyCommandLine(test_args.size(), doctest_args);
297
298
for (uint32_t x = 0; x < test_args.size(); x++) {
299
delete[] doctest_args[x];
300
}
301
delete[] doctest_args;
302
}
303
304
return test_context.run();
305
}
306
307
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
308
309
struct GodotTestCaseListener : public doctest::IReporter {
310
GodotTestCaseListener(const doctest::ContextOptions &p_in) {}
311
312
SignalWatcher *signal_watcher = nullptr;
313
314
#ifndef PHYSICS_2D_DISABLED
315
PhysicsServer2D *physics_server_2d = nullptr;
316
#endif // PHYSICS_2D_DISABLED
317
#ifndef PHYSICS_3D_DISABLED
318
PhysicsServer3D *physics_server_3d = nullptr;
319
#endif // PHYSICS_3D_DISABLED
320
321
#ifndef NAVIGATION_2D_DISABLED
322
NavigationServer2D *navigation_server_2d = nullptr;
323
#endif // NAVIGATION_2D_DISABLED
324
#ifndef NAVIGATION_3D_DISABLED
325
NavigationServer3D *navigation_server_3d = nullptr;
326
#endif // NAVIGATION_3D_DISABLED
327
328
void test_case_start(const doctest::TestCaseData &p_in) override {
329
reinitialize();
330
331
String name = String(p_in.m_name);
332
String suite_name = String(p_in.m_test_suite);
333
334
if (name.contains("[SceneTree]") || name.contains("[Editor]")) {
335
memnew(MessageQueue);
336
337
memnew(Input);
338
Input::get_singleton()->set_use_accumulated_input(false);
339
340
Error err = OK;
341
OS::get_singleton()->set_has_server_feature_callback(nullptr);
342
for (int i = 0; i < DisplayServer::get_create_function_count(); i++) {
343
if (String("mock") == DisplayServer::get_create_function_name(i)) {
344
DisplayServer::create(i, "", DisplayServer::WindowMode::WINDOW_MODE_MINIMIZED, DisplayServer::VSyncMode::VSYNC_ENABLED, 0, nullptr, Vector2i(0, 0), DisplayServer::SCREEN_PRIMARY, DisplayServer::CONTEXT_EDITOR, 0, err);
345
break;
346
}
347
}
348
memnew(RenderingServerDefault());
349
RenderingServerDefault::get_singleton()->init();
350
RenderingServerDefault::get_singleton()->set_render_loop_enabled(false);
351
352
// ThemeDB requires RenderingServer to initialize the default theme.
353
// So we have to do this for each test case. Also make sure there is
354
// no residual theme from something else.
355
ThemeDB::get_singleton()->finalize_theme();
356
ThemeDB::get_singleton()->initialize_theme();
357
358
#ifndef PHYSICS_3D_DISABLED
359
physics_server_3d = PhysicsServer3DManager::get_singleton()->new_default_server();
360
if (!physics_server_3d) {
361
physics_server_3d = memnew(PhysicsServer3DDummy);
362
}
363
physics_server_3d->init();
364
#endif // PHYSICS_3D_DISABLED
365
366
#ifndef PHYSICS_2D_DISABLED
367
physics_server_2d = PhysicsServer2DManager::get_singleton()->new_default_server();
368
if (!physics_server_2d) {
369
physics_server_2d = memnew(PhysicsServer2DDummy);
370
}
371
physics_server_2d->init();
372
#endif // PHYSICS_2D_DISABLED
373
374
ERR_PRINT_OFF;
375
#ifndef NAVIGATION_3D_DISABLED
376
navigation_server_3d = NavigationServer3DManager::get_singleton()->new_default_server();
377
#endif // NAVIGATION_3D_DISABLED
378
#ifndef NAVIGATION_2D_DISABLED
379
navigation_server_2d = NavigationServer2DManager::get_singleton()->new_default_server();
380
#endif // NAVIGATION_2D_DISABLED
381
ERR_PRINT_ON;
382
383
memnew(InputMap);
384
InputMap::get_singleton()->load_default();
385
386
memnew(SceneTree);
387
SceneTree::get_singleton()->initialize();
388
if (!DisplayServer::get_singleton()->has_feature(DisplayServer::Feature::FEATURE_SUBWINDOWS)) {
389
SceneTree::get_singleton()->get_root()->set_embedding_subwindows(true);
390
}
391
392
#ifdef TOOLS_ENABLED
393
if (name.contains("[Editor]")) {
394
Engine::get_singleton()->set_editor_hint(true);
395
EditorPaths::create();
396
EditorSettings::create();
397
}
398
#endif // TOOLS_ENABLED
399
400
return;
401
}
402
403
if (name.contains("[Audio]")) {
404
// The last driver index should always be the dummy driver.
405
int dummy_idx = AudioDriverManager::get_driver_count() - 1;
406
AudioDriverManager::initialize(dummy_idx);
407
AudioServer *audio_server = memnew(AudioServer);
408
audio_server->init();
409
return;
410
}
411
412
#ifndef NAVIGATION_3D_DISABLED
413
if (suite_name.contains("[Navigation3D]") && navigation_server_3d == nullptr) {
414
ERR_PRINT_OFF;
415
navigation_server_3d = NavigationServer3DManager::get_singleton()->new_default_server();
416
ERR_PRINT_ON;
417
return;
418
}
419
#endif // NAVIGATION_3D_DISABLED
420
421
#ifndef NAVIGATION_2D_DISABLED
422
if (suite_name.contains("[Navigation2D]") && navigation_server_2d == nullptr) {
423
ERR_PRINT_OFF;
424
navigation_server_2d = NavigationServer2DManager::get_singleton()->new_default_server();
425
ERR_PRINT_ON;
426
return;
427
}
428
#endif // NAVIGATION_2D_DISABLED
429
}
430
431
void test_case_end(const doctest::CurrentTestCaseStats &) override {
432
#ifdef TOOLS_ENABLED
433
if (EditorSettings::get_singleton()) {
434
EditorSettings::destroy();
435
436
// Instantiating the EditorSettings singleton sets the locale to the editor's language.
437
TranslationServer::get_singleton()->set_locale("en");
438
}
439
if (EditorPaths::get_singleton()) {
440
EditorPaths::free();
441
}
442
#endif // TOOLS_ENABLED
443
444
Engine::get_singleton()->set_editor_hint(false);
445
446
if (SceneTree::get_singleton()) {
447
SceneTree::get_singleton()->finalize();
448
}
449
450
if (MessageQueue::get_singleton()) {
451
MessageQueue::get_singleton()->flush();
452
}
453
454
if (SceneTree::get_singleton()) {
455
memdelete(SceneTree::get_singleton());
456
}
457
458
#ifndef NAVIGATION_3D_DISABLED
459
if (navigation_server_3d) {
460
memdelete(navigation_server_3d);
461
navigation_server_3d = nullptr;
462
}
463
#endif // NAVIGATION_3D_DISABLED
464
465
#ifndef NAVIGATION_2D_DISABLED
466
if (navigation_server_2d) {
467
memdelete(navigation_server_2d);
468
navigation_server_2d = nullptr;
469
}
470
#endif // NAVIGATION_2D_DISABLED
471
472
#ifndef PHYSICS_3D_DISABLED
473
if (physics_server_3d) {
474
physics_server_3d->finish();
475
memdelete(physics_server_3d);
476
physics_server_3d = nullptr;
477
}
478
#endif // PHYSICS_3D_DISABLED
479
480
#ifndef PHYSICS_2D_DISABLED
481
if (physics_server_2d) {
482
physics_server_2d->finish();
483
memdelete(physics_server_2d);
484
physics_server_2d = nullptr;
485
}
486
#endif // PHYSICS_2D_DISABLED
487
488
if (Input::get_singleton()) {
489
memdelete(Input::get_singleton());
490
}
491
492
if (RenderingServer::get_singleton()) {
493
// ThemeDB requires RenderingServer to finalize the default theme.
494
// So we have to do this for each test case.
495
ThemeDB::get_singleton()->finalize_theme();
496
497
RenderingServer::get_singleton()->sync();
498
RenderingServer::get_singleton()->global_shader_parameters_clear();
499
RenderingServer::get_singleton()->finish();
500
memdelete(RenderingServer::get_singleton());
501
}
502
503
if (DisplayServer::get_singleton()) {
504
memdelete(DisplayServer::get_singleton());
505
}
506
507
if (InputMap::get_singleton()) {
508
memdelete(InputMap::get_singleton());
509
}
510
511
if (MessageQueue::get_singleton()) {
512
MessageQueue::get_singleton()->flush();
513
memdelete(MessageQueue::get_singleton());
514
}
515
516
if (AudioServer::get_singleton()) {
517
AudioServer::get_singleton()->finish();
518
memdelete(AudioServer::get_singleton());
519
}
520
}
521
522
void test_run_start() override {
523
signal_watcher = memnew(SignalWatcher);
524
}
525
526
void test_run_end(const doctest::TestRunStats &) override {
527
memdelete(signal_watcher);
528
}
529
530
void test_case_reenter(const doctest::TestCaseData &) override {
531
reinitialize();
532
}
533
534
void subcase_start(const doctest::SubcaseSignature &) override {
535
reinitialize();
536
}
537
538
void report_query(const doctest::QueryData &) override {}
539
void test_case_exception(const doctest::TestCaseException &) override {}
540
void subcase_end() override {}
541
542
void log_assert(const doctest::AssertData &in) override {}
543
void log_message(const doctest::MessageData &) override {}
544
void test_case_skipped(const doctest::TestCaseData &) override {}
545
546
private:
547
void reinitialize() {
548
Math::seed(0x60d07);
549
SignalWatcher::get_singleton()->_clear_signals();
550
}
551
};
552
553
REGISTER_LISTENER("GodotTestCaseListener", 1, GodotTestCaseListener);
554
555