test_main.cpp 15 KB

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