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