test_main.cpp 12 KB

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