test_main.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. #include "test_main.h"
  31. #include "modules/modules_enabled.gen.h"
  32. #ifdef TOOLS_ENABLED
  33. #include "editor/editor_paths.h"
  34. #include "editor/editor_settings.h"
  35. #endif // TOOLS_ENABLED
  36. #include "tests/core/config/test_project_settings.h"
  37. #include "tests/core/input/test_input_event.h"
  38. #include "tests/core/input/test_input_event_key.h"
  39. #include "tests/core/input/test_input_event_mouse.h"
  40. #include "tests/core/input/test_shortcut.h"
  41. #include "tests/core/io/test_config_file.h"
  42. #include "tests/core/io/test_file_access.h"
  43. #include "tests/core/io/test_http_client.h"
  44. #include "tests/core/io/test_image.h"
  45. #include "tests/core/io/test_ip.h"
  46. #include "tests/core/io/test_json.h"
  47. #include "tests/core/io/test_json_native.h"
  48. #include "tests/core/io/test_logger.h"
  49. #include "tests/core/io/test_marshalls.h"
  50. #include "tests/core/io/test_packet_peer.h"
  51. #include "tests/core/io/test_pck_packer.h"
  52. #include "tests/core/io/test_resource.h"
  53. #include "tests/core/io/test_resource_uid.h"
  54. #include "tests/core/io/test_stream_peer.h"
  55. #include "tests/core/io/test_stream_peer_buffer.h"
  56. #include "tests/core/io/test_tcp_server.h"
  57. #include "tests/core/io/test_udp_server.h"
  58. #include "tests/core/io/test_xml_parser.h"
  59. #include "tests/core/math/test_aabb.h"
  60. #include "tests/core/math/test_astar.h"
  61. #include "tests/core/math/test_basis.h"
  62. #include "tests/core/math/test_color.h"
  63. #include "tests/core/math/test_expression.h"
  64. #include "tests/core/math/test_geometry_2d.h"
  65. #include "tests/core/math/test_geometry_3d.h"
  66. #include "tests/core/math/test_math_funcs.h"
  67. #include "tests/core/math/test_plane.h"
  68. #include "tests/core/math/test_projection.h"
  69. #include "tests/core/math/test_quaternion.h"
  70. #include "tests/core/math/test_random_number_generator.h"
  71. #include "tests/core/math/test_rect2.h"
  72. #include "tests/core/math/test_rect2i.h"
  73. #include "tests/core/math/test_transform_2d.h"
  74. #include "tests/core/math/test_transform_3d.h"
  75. #include "tests/core/math/test_vector2.h"
  76. #include "tests/core/math/test_vector2i.h"
  77. #include "tests/core/math/test_vector3.h"
  78. #include "tests/core/math/test_vector3i.h"
  79. #include "tests/core/math/test_vector4.h"
  80. #include "tests/core/math/test_vector4i.h"
  81. #include "tests/core/object/test_class_db.h"
  82. #include "tests/core/object/test_method_bind.h"
  83. #include "tests/core/object/test_object.h"
  84. #include "tests/core/object/test_undo_redo.h"
  85. #include "tests/core/os/test_os.h"
  86. #include "tests/core/string/test_fuzzy_search.h"
  87. #include "tests/core/string/test_node_path.h"
  88. #include "tests/core/string/test_string.h"
  89. #include "tests/core/string/test_translation.h"
  90. #include "tests/core/string/test_translation_server.h"
  91. #include "tests/core/templates/test_a_hash_map.h"
  92. #include "tests/core/templates/test_command_queue.h"
  93. #include "tests/core/templates/test_hash_map.h"
  94. #include "tests/core/templates/test_hash_set.h"
  95. #include "tests/core/templates/test_list.h"
  96. #include "tests/core/templates/test_local_vector.h"
  97. #include "tests/core/templates/test_lru.h"
  98. #include "tests/core/templates/test_oa_hash_map.h"
  99. #include "tests/core/templates/test_paged_array.h"
  100. #include "tests/core/templates/test_rid.h"
  101. #include "tests/core/templates/test_vector.h"
  102. #include "tests/core/test_crypto.h"
  103. #include "tests/core/test_hashing_context.h"
  104. #include "tests/core/test_time.h"
  105. #include "tests/core/threads/test_worker_thread_pool.h"
  106. #include "tests/core/variant/test_array.h"
  107. #include "tests/core/variant/test_callable.h"
  108. #include "tests/core/variant/test_dictionary.h"
  109. #include "tests/core/variant/test_variant.h"
  110. #include "tests/core/variant/test_variant_utility.h"
  111. #include "tests/scene/test_animation.h"
  112. #include "tests/scene/test_audio_stream_wav.h"
  113. #include "tests/scene/test_bit_map.h"
  114. #include "tests/scene/test_button.h"
  115. #include "tests/scene/test_camera_2d.h"
  116. #include "tests/scene/test_control.h"
  117. #include "tests/scene/test_curve.h"
  118. #include "tests/scene/test_curve_2d.h"
  119. #include "tests/scene/test_curve_3d.h"
  120. #include "tests/scene/test_fontfile.h"
  121. #include "tests/scene/test_gradient.h"
  122. #include "tests/scene/test_gradient_texture.h"
  123. #include "tests/scene/test_image_texture.h"
  124. #include "tests/scene/test_image_texture_3d.h"
  125. #include "tests/scene/test_instance_placeholder.h"
  126. #include "tests/scene/test_node.h"
  127. #include "tests/scene/test_node_2d.h"
  128. #include "tests/scene/test_packed_scene.h"
  129. #include "tests/scene/test_parallax_2d.h"
  130. #include "tests/scene/test_path_2d.h"
  131. #include "tests/scene/test_path_follow_2d.h"
  132. #include "tests/scene/test_physics_material.h"
  133. #include "tests/scene/test_sprite_frames.h"
  134. #include "tests/scene/test_style_box_texture.h"
  135. #include "tests/scene/test_texture_progress_bar.h"
  136. #include "tests/scene/test_theme.h"
  137. #include "tests/scene/test_timer.h"
  138. #include "tests/scene/test_viewport.h"
  139. #include "tests/scene/test_visual_shader.h"
  140. #include "tests/scene/test_window.h"
  141. #include "tests/servers/rendering/test_shader_preprocessor.h"
  142. #include "tests/servers/test_text_server.h"
  143. #include "tests/test_validate_testing.h"
  144. #ifndef ADVANCED_GUI_DISABLED
  145. #include "tests/scene/test_code_edit.h"
  146. #include "tests/scene/test_color_picker.h"
  147. #include "tests/scene/test_graph_node.h"
  148. #include "tests/scene/test_option_button.h"
  149. #include "tests/scene/test_split_container.h"
  150. #include "tests/scene/test_tab_bar.h"
  151. #include "tests/scene/test_tab_container.h"
  152. #include "tests/scene/test_text_edit.h"
  153. #include "tests/scene/test_tree.h"
  154. #endif // ADVANCED_GUI_DISABLED
  155. #ifndef _3D_DISABLED
  156. #ifdef MODULE_NAVIGATION_ENABLED
  157. #include "tests/scene/test_navigation_agent_2d.h"
  158. #include "tests/scene/test_navigation_agent_3d.h"
  159. #include "tests/scene/test_navigation_obstacle_2d.h"
  160. #include "tests/scene/test_navigation_obstacle_3d.h"
  161. #include "tests/scene/test_navigation_region_2d.h"
  162. #include "tests/scene/test_navigation_region_3d.h"
  163. #include "tests/servers/test_navigation_server_2d.h"
  164. #include "tests/servers/test_navigation_server_3d.h"
  165. #endif // MODULE_NAVIGATION_ENABLED
  166. #include "tests/scene/test_arraymesh.h"
  167. #include "tests/scene/test_camera_3d.h"
  168. #include "tests/scene/test_gltf_document.h"
  169. #include "tests/scene/test_height_map_shape_3d.h"
  170. #include "tests/scene/test_path_3d.h"
  171. #include "tests/scene/test_path_follow_3d.h"
  172. #include "tests/scene/test_primitives.h"
  173. #include "tests/scene/test_skeleton_3d.h"
  174. #include "tests/scene/test_sky.h"
  175. #endif // _3D_DISABLED
  176. #include "modules/modules_tests.gen.h"
  177. #include "tests/display_server_mock.h"
  178. #include "tests/test_macros.h"
  179. #include "scene/theme/theme_db.h"
  180. #ifndef _3D_DISABLED
  181. #include "servers/navigation_server_2d.h"
  182. #include "servers/navigation_server_3d.h"
  183. #endif // _3D_DISABLED
  184. #include "servers/physics_server_2d.h"
  185. #include "servers/physics_server_2d_dummy.h"
  186. #ifndef _3D_DISABLED
  187. #include "servers/physics_server_3d.h"
  188. #include "servers/physics_server_3d_dummy.h"
  189. #endif // _3D_DISABLED
  190. #include "servers/rendering/rendering_server_default.h"
  191. int test_main(int argc, char *argv[]) {
  192. bool run_tests = true;
  193. // Convert arguments to Godot's command-line.
  194. List<String> args;
  195. for (int i = 0; i < argc; i++) {
  196. args.push_back(String::utf8(argv[i]));
  197. }
  198. OS::get_singleton()->set_cmdline("", args, List<String>());
  199. DisplayServerMock::register_mock_driver();
  200. WorkerThreadPool::get_singleton()->init();
  201. // Run custom test tools.
  202. if (test_commands) {
  203. for (const KeyValue<String, TestFunc> &E : (*test_commands)) {
  204. if (args.find(E.key)) {
  205. const TestFunc &test_func = E.value;
  206. test_func();
  207. run_tests = false;
  208. break;
  209. }
  210. }
  211. if (!run_tests) {
  212. delete test_commands;
  213. return 0;
  214. }
  215. }
  216. // Doctest runner.
  217. doctest::Context test_context;
  218. LocalVector<String> test_args;
  219. // Clean arguments of "--test" from the args.
  220. for (int x = 0; x < argc; x++) {
  221. String arg = String(argv[x]);
  222. if (arg != "--test") {
  223. test_args.push_back(arg);
  224. }
  225. }
  226. if (test_args.size() > 0) {
  227. // Convert Godot command line arguments back to standard arguments.
  228. char **doctest_args = new char *[test_args.size()];
  229. for (uint32_t x = 0; x < test_args.size(); x++) {
  230. // Operation to convert Godot string to non wchar string.
  231. CharString cs = test_args[x].utf8();
  232. const char *str = cs.get_data();
  233. // Allocate the string copy.
  234. doctest_args[x] = new char[strlen(str) + 1];
  235. // Copy this into memory.
  236. memcpy(doctest_args[x], str, strlen(str) + 1);
  237. }
  238. test_context.applyCommandLine(test_args.size(), doctest_args);
  239. for (uint32_t x = 0; x < test_args.size(); x++) {
  240. delete[] doctest_args[x];
  241. }
  242. delete[] doctest_args;
  243. }
  244. return test_context.run();
  245. }
  246. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  247. struct GodotTestCaseListener : public doctest::IReporter {
  248. GodotTestCaseListener(const doctest::ContextOptions &p_in) {}
  249. SignalWatcher *signal_watcher = nullptr;
  250. PhysicsServer2D *physics_server_2d = nullptr;
  251. #ifndef _3D_DISABLED
  252. PhysicsServer3D *physics_server_3d = nullptr;
  253. NavigationServer3D *navigation_server_3d = nullptr;
  254. NavigationServer2D *navigation_server_2d = nullptr;
  255. #endif // _3D_DISABLED
  256. void test_case_start(const doctest::TestCaseData &p_in) override {
  257. reinitialize();
  258. String name = String(p_in.m_name);
  259. String suite_name = String(p_in.m_test_suite);
  260. if (name.contains("[SceneTree]") || name.contains("[Editor]")) {
  261. memnew(MessageQueue);
  262. memnew(Input);
  263. Input::get_singleton()->set_use_accumulated_input(false);
  264. Error err = OK;
  265. OS::get_singleton()->set_has_server_feature_callback(nullptr);
  266. for (int i = 0; i < DisplayServer::get_create_function_count(); i++) {
  267. if (String("mock") == DisplayServer::get_create_function_name(i)) {
  268. 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);
  269. break;
  270. }
  271. }
  272. memnew(RenderingServerDefault());
  273. RenderingServerDefault::get_singleton()->init();
  274. RenderingServerDefault::get_singleton()->set_render_loop_enabled(false);
  275. // ThemeDB requires RenderingServer to initialize the default theme.
  276. // So we have to do this for each test case. Also make sure there is
  277. // no residual theme from something else.
  278. ThemeDB::get_singleton()->finalize_theme();
  279. ThemeDB::get_singleton()->initialize_theme_noproject();
  280. #ifndef _3D_DISABLED
  281. physics_server_3d = PhysicsServer3DManager::get_singleton()->new_default_server();
  282. if (!physics_server_3d) {
  283. physics_server_3d = memnew(PhysicsServer3DDummy);
  284. }
  285. physics_server_3d->init();
  286. #endif // _3D_DISABLED
  287. physics_server_2d = PhysicsServer2DManager::get_singleton()->new_default_server();
  288. if (!physics_server_2d) {
  289. physics_server_2d = memnew(PhysicsServer2DDummy);
  290. }
  291. physics_server_2d->init();
  292. #ifndef _3D_DISABLED
  293. ERR_PRINT_OFF;
  294. navigation_server_3d = NavigationServer3DManager::new_default_server();
  295. navigation_server_2d = NavigationServer2DManager::new_default_server();
  296. ERR_PRINT_ON;
  297. #endif // _3D_DISABLED
  298. memnew(InputMap);
  299. InputMap::get_singleton()->load_default();
  300. memnew(SceneTree);
  301. SceneTree::get_singleton()->initialize();
  302. if (!DisplayServer::get_singleton()->has_feature(DisplayServer::Feature::FEATURE_SUBWINDOWS)) {
  303. SceneTree::get_singleton()->get_root()->set_embedding_subwindows(true);
  304. }
  305. #ifdef TOOLS_ENABLED
  306. if (name.contains("[Editor]")) {
  307. Engine::get_singleton()->set_editor_hint(true);
  308. EditorPaths::create();
  309. EditorSettings::create();
  310. }
  311. #endif // TOOLS_ENABLED
  312. return;
  313. }
  314. if (name.contains("[Audio]")) {
  315. // The last driver index should always be the dummy driver.
  316. int dummy_idx = AudioDriverManager::get_driver_count() - 1;
  317. AudioDriverManager::initialize(dummy_idx);
  318. AudioServer *audio_server = memnew(AudioServer);
  319. audio_server->init();
  320. return;
  321. }
  322. #ifndef _3D_DISABLED
  323. if (suite_name.contains("[Navigation]") && navigation_server_2d == nullptr && navigation_server_3d == nullptr) {
  324. ERR_PRINT_OFF;
  325. navigation_server_3d = NavigationServer3DManager::new_default_server();
  326. navigation_server_2d = NavigationServer2DManager::new_default_server();
  327. ERR_PRINT_ON;
  328. return;
  329. }
  330. #endif // _3D_DISABLED
  331. }
  332. void test_case_end(const doctest::CurrentTestCaseStats &) override {
  333. #ifdef TOOLS_ENABLED
  334. if (EditorSettings::get_singleton()) {
  335. EditorSettings::destroy();
  336. // Instantiating the EditorSettings singleton sets the locale to the editor's language.
  337. TranslationServer::get_singleton()->set_locale("en");
  338. }
  339. if (EditorPaths::get_singleton()) {
  340. EditorPaths::free();
  341. }
  342. #endif // TOOLS_ENABLED
  343. Engine::get_singleton()->set_editor_hint(false);
  344. if (SceneTree::get_singleton()) {
  345. SceneTree::get_singleton()->finalize();
  346. }
  347. if (MessageQueue::get_singleton()) {
  348. MessageQueue::get_singleton()->flush();
  349. }
  350. if (SceneTree::get_singleton()) {
  351. memdelete(SceneTree::get_singleton());
  352. }
  353. #ifndef _3D_DISABLED
  354. if (navigation_server_3d) {
  355. memdelete(navigation_server_3d);
  356. navigation_server_3d = nullptr;
  357. }
  358. if (navigation_server_2d) {
  359. memdelete(navigation_server_2d);
  360. navigation_server_2d = nullptr;
  361. }
  362. #endif // _3D_DISABLED
  363. #ifndef _3D_DISABLED
  364. if (physics_server_3d) {
  365. physics_server_3d->finish();
  366. memdelete(physics_server_3d);
  367. physics_server_3d = nullptr;
  368. }
  369. #endif // _3D_DISABLED
  370. if (physics_server_2d) {
  371. physics_server_2d->finish();
  372. memdelete(physics_server_2d);
  373. physics_server_2d = nullptr;
  374. }
  375. if (Input::get_singleton()) {
  376. memdelete(Input::get_singleton());
  377. }
  378. if (RenderingServer::get_singleton()) {
  379. // ThemeDB requires RenderingServer to finalize the default theme.
  380. // So we have to do this for each test case.
  381. ThemeDB::get_singleton()->finalize_theme();
  382. RenderingServer::get_singleton()->sync();
  383. RenderingServer::get_singleton()->global_shader_parameters_clear();
  384. RenderingServer::get_singleton()->finish();
  385. memdelete(RenderingServer::get_singleton());
  386. }
  387. if (DisplayServer::get_singleton()) {
  388. memdelete(DisplayServer::get_singleton());
  389. }
  390. if (InputMap::get_singleton()) {
  391. memdelete(InputMap::get_singleton());
  392. }
  393. if (MessageQueue::get_singleton()) {
  394. MessageQueue::get_singleton()->flush();
  395. memdelete(MessageQueue::get_singleton());
  396. }
  397. if (AudioServer::get_singleton()) {
  398. AudioServer::get_singleton()->finish();
  399. memdelete(AudioServer::get_singleton());
  400. }
  401. }
  402. void test_run_start() override {
  403. signal_watcher = memnew(SignalWatcher);
  404. }
  405. void test_run_end(const doctest::TestRunStats &) override {
  406. memdelete(signal_watcher);
  407. }
  408. void test_case_reenter(const doctest::TestCaseData &) override {
  409. reinitialize();
  410. }
  411. void subcase_start(const doctest::SubcaseSignature &) override {
  412. reinitialize();
  413. }
  414. void report_query(const doctest::QueryData &) override {}
  415. void test_case_exception(const doctest::TestCaseException &) override {}
  416. void subcase_end() override {}
  417. void log_assert(const doctest::AssertData &in) override {}
  418. void log_message(const doctest::MessageData &) override {}
  419. void test_case_skipped(const doctest::TestCaseData &) override {}
  420. private:
  421. void reinitialize() {
  422. Math::seed(0x60d07);
  423. SignalWatcher::get_singleton()->_clear_signals();
  424. }
  425. };
  426. REGISTER_LISTENER("GodotTestCaseListener", 1, GodotTestCaseListener);