0.2.0-dev Download ZIP
384 lines ·12.7 KB Raw
| 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/config/engine.h" |
| 34 | #include "core/input/input.h" |
| 35 | #include "core/input/input_map.h" |
| 36 | #include "core/io/dir_access.h" |
| 37 | #include "core/object/worker_thread_pool.h" |
| 38 | #include "core/os/os.h" |
| 39 | #include "core/string/translation_server.h" |
| 40 | #include "scene/main/scene_tree.h" |
| 41 | #include "scene/main/window.h" |
| 42 | #include "scene/theme/theme_db.h" |
| 43 | #include "servers/audio/audio_driver.h" |
| 44 | #include "servers/audio/audio_server.h" |
| 45 | #include "servers/display/accessibility_server.h" |
| 46 | #include "servers/rendering/rendering_server.h" |
| 47 | #include "tests/display_server_mock.h" |
| 48 | #include "tests/force_link.gen.h" |
| 49 | #include "tests/signal_watcher.h" |
| 50 | #include "tests/test_macros.h" |
| 51 | #include "tests/test_utils.h" |
| 52 | |
| 53 | #ifdef TOOLS_ENABLED |
| 54 | #include "editor/file_system/editor_paths.h" |
| 55 | #include "editor/settings/editor_settings.h" |
| 56 | #endif // TOOLS_ENABLED |
| 57 | |
| 58 | #ifndef NAVIGATION_2D_DISABLED |
| 59 | #include "servers/navigation_2d/navigation_server_2d.h" |
| 60 | #include "servers/navigation_2d/navigation_server_2d_manager.h" |
| 61 | #endif // NAVIGATION_2D_DISABLED |
| 62 | #ifndef NAVIGATION_3D_DISABLED |
| 63 | #include "servers/navigation_3d/navigation_server_3d.h" |
| 64 | #include "servers/navigation_3d/navigation_server_3d_manager.h" |
| 65 | #endif // NAVIGATION_3D_DISABLED |
| 66 | |
| 67 | #ifndef PHYSICS_2D_DISABLED |
| 68 | #include "servers/physics_2d/physics_server_2d.h" |
| 69 | #include "servers/physics_2d/physics_server_2d_dummy.h" |
| 70 | #include "servers/physics_2d/physics_server_2d_manager.h" |
| 71 | #endif // PHYSICS_2D_DISABLED |
| 72 | #ifndef PHYSICS_3D_DISABLED |
| 73 | #include "servers/physics_3d/physics_server_3d.h" |
| 74 | #include "servers/physics_3d/physics_server_3d_dummy.h" |
| 75 | #include "servers/physics_3d/physics_server_3d_manager.h" |
| 76 | #endif // PHYSICS_3D_DISABLED |
| 77 | |
| 78 | #include "modules/modules_tests.gen.h" // IWYU pragma: keep // TODO: Migrate module tests to compilation files. |
| 79 | |
| 80 | int test_main(int argc, char *argv[]) { |
| 81 | ForceLink::force_link_tests(); |
| 82 | |
| 83 | bool run_tests = true; |
| 84 | |
| 85 | // Convert arguments to Godot's command-line. |
| 86 | List<String> args; |
| 87 | |
| 88 | for (int i = 0; i < argc; i++) { |
| 89 | args.push_back(String::utf8(argv[i])); |
| 90 | } |
| 91 | OS::get_singleton()->set_cmdline("", args, List<String>()); |
| 92 | DisplayServerMock::register_mock_driver(); |
| 93 | |
| 94 | WorkerThreadPool::get_singleton()->init(); |
| 95 | |
| 96 | { |
| 97 | const String test_path = TestUtils::get_temp_path(""); |
| 98 | Ref<DirAccess> da = DirAccess::open(test_path); // get_temp_path() automatically creates the folder. |
| 99 | ERR_FAIL_COND_V(da.is_null(), 0); |
| 100 | ERR_FAIL_COND_V_MSG(da->erase_contents_recursive() != OK, 0, "Failed to delete files"); |
| 101 | } |
| 102 | |
| 103 | // Run custom test tools. |
| 104 | if (test_commands) { |
| 105 | int res = 0; |
| 106 | for (const KeyValue<String, TestFunc> &E : (*test_commands)) { |
| 107 | if (args.find(E.key)) { |
| 108 | const TestFunc &test_func = E.value; |
| 109 | res = test_func(); |
| 110 | run_tests = false; |
| 111 | break; |
| 112 | } |
| 113 | } |
| 114 | if (!run_tests) { |
| 115 | delete test_commands; |
| 116 | return res; |
| 117 | } |
| 118 | } |
| 119 | // Doctest runner. |
| 120 | doctest::Context test_context; |
| 121 | LocalVector<String> test_args; |
| 122 | |
| 123 | // Clean arguments of "--test" from the args. |
| 124 | for (int x = 0; x < argc; x++) { |
| 125 | String arg = String(argv[x]); |
| 126 | if (arg != "--test") { |
| 127 | test_args.push_back(arg); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | if (test_args.size() > 0) { |
| 132 | // Convert Godot command line arguments back to standard arguments. |
| 133 | char **doctest_args = new char *[test_args.size()]; |
| 134 | for (uint32_t x = 0; x < test_args.size(); x++) { |
| 135 | // Operation to convert Godot string to non wchar string. |
| 136 | CharString cs = test_args[x].utf8(); |
| 137 | const char *str = cs.get_data(); |
| 138 | // Allocate the string copy. |
| 139 | doctest_args[x] = new char[strlen(str) + 1]; |
| 140 | // Copy this into memory. |
| 141 | memcpy(doctest_args[x], str, strlen(str) + 1); |
| 142 | } |
| 143 | |
| 144 | test_context.applyCommandLine(test_args.size(), doctest_args); |
| 145 | |
| 146 | for (uint32_t x = 0; x < test_args.size(); x++) { |
| 147 | delete[] doctest_args[x]; |
| 148 | } |
| 149 | delete[] doctest_args; |
| 150 | } |
| 151 | |
| 152 | return test_context.run(); |
| 153 | } |
| 154 | |
| 155 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 156 | |
| 157 | struct GodotTestCaseListener : public doctest::IReporter { |
| 158 | GodotTestCaseListener(const doctest::ContextOptions &p_in) {} |
| 159 | |
| 160 | SignalWatcher *signal_watcher = nullptr; |
| 161 | |
| 162 | #ifndef PHYSICS_2D_DISABLED |
| 163 | PhysicsServer2D *physics_server_2d = nullptr; |
| 164 | #endif // PHYSICS_2D_DISABLED |
| 165 | #ifndef PHYSICS_3D_DISABLED |
| 166 | PhysicsServer3D *physics_server_3d = nullptr; |
| 167 | #endif // PHYSICS_3D_DISABLED |
| 168 | |
| 169 | #ifndef NAVIGATION_2D_DISABLED |
| 170 | NavigationServer2D *navigation_server_2d = nullptr; |
| 171 | #endif // NAVIGATION_2D_DISABLED |
| 172 | #ifndef NAVIGATION_3D_DISABLED |
| 173 | NavigationServer3D *navigation_server_3d = nullptr; |
| 174 | #endif // NAVIGATION_3D_DISABLED |
| 175 | |
| 176 | void test_case_start(const doctest::TestCaseData &p_in) override { |
| 177 | reinitialize(); |
| 178 | |
| 179 | String name = String(p_in.m_name); |
| 180 | [[maybe_unused]] String suite_name = String(p_in.m_test_suite); |
| 181 | |
| 182 | if (name.contains("[SceneTree]") || name.contains("[Editor]")) { |
| 183 | memnew(Input); |
| 184 | Input::get_singleton()->set_use_accumulated_input(false); |
| 185 | |
| 186 | Error err = OK; |
| 187 | OS::get_singleton()->set_has_server_feature_callback(nullptr); |
| 188 | |
| 189 | for (int i = 0; i < AccessibilityServer::get_create_function_count(); i++) { |
| 190 | if (String("dummy") == AccessibilityServer::get_create_function_name(i)) { |
| 191 | AccessibilityServer::create(i, err); |
| 192 | break; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | for (int i = 0; i < DisplayServer::get_create_function_count(); i++) { |
| 197 | if (String("mock") == DisplayServer::get_create_function_name(i)) { |
| 198 | DisplayServer::create(i, "", DisplayServerEnums::WindowMode::WINDOW_MODE_MINIMIZED, DisplayServerEnums::VSyncMode::VSYNC_ENABLED, 0, nullptr, Vector2i(0, 0), DisplayServerEnums::SCREEN_PRIMARY, DisplayServerEnums::CONTEXT_EDITOR, 0, err); |
| 199 | break; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | // ThemeDB requires RenderingServer to initialize the default theme. |
| 204 | // So we have to do this for each test case. Also make sure there is |
| 205 | // no residual theme from something else. |
| 206 | ThemeDB::get_singleton()->finalize_theme(); |
| 207 | ThemeDB::get_singleton()->initialize_theme(); |
| 208 | |
| 209 | #ifndef PHYSICS_3D_DISABLED |
| 210 | physics_server_3d = PhysicsServer3DManager::get_singleton()->new_default_server(); |
| 211 | if (!physics_server_3d) { |
| 212 | physics_server_3d = memnew(PhysicsServer3DDummy); |
| 213 | } |
| 214 | physics_server_3d->init(); |
| 215 | #endif // PHYSICS_3D_DISABLED |
| 216 | |
| 217 | #ifndef PHYSICS_2D_DISABLED |
| 218 | physics_server_2d = PhysicsServer2DManager::get_singleton()->new_default_server(); |
| 219 | if (!physics_server_2d) { |
| 220 | physics_server_2d = memnew(PhysicsServer2DDummy); |
| 221 | } |
| 222 | physics_server_2d->init(); |
| 223 | #endif // PHYSICS_2D_DISABLED |
| 224 | |
| 225 | ERR_PRINT_OFF; |
| 226 | #ifndef NAVIGATION_3D_DISABLED |
| 227 | navigation_server_3d = NavigationServer3DManager::get_singleton()->new_default_server(); |
| 228 | #endif // NAVIGATION_3D_DISABLED |
| 229 | #ifndef NAVIGATION_2D_DISABLED |
| 230 | navigation_server_2d = NavigationServer2DManager::get_singleton()->new_default_server(); |
| 231 | #endif // NAVIGATION_2D_DISABLED |
| 232 | ERR_PRINT_ON; |
| 233 | |
| 234 | memnew(InputMap); |
| 235 | InputMap::get_singleton()->load_default(); |
| 236 | |
| 237 | memnew(SceneTree); |
| 238 | SceneTree::get_singleton()->initialize(); |
| 239 | if (!DisplayServer::get_singleton()->has_feature(DisplayServerEnums::FEATURE_SUBWINDOWS)) { |
| 240 | SceneTree::get_singleton()->get_root()->set_embedding_subwindows(true); |
| 241 | } |
| 242 | |
| 243 | #ifdef TOOLS_ENABLED |
| 244 | if (name.contains("[Editor]")) { |
| 245 | Engine::get_singleton()->set_editor_hint(true); |
| 246 | EditorPaths::create(); |
| 247 | EditorSettings::create(); |
| 248 | } |
| 249 | #endif // TOOLS_ENABLED |
| 250 | |
| 251 | return; |
| 252 | } |
| 253 | |
| 254 | if (name.contains("[Audio]")) { |
| 255 | // The last driver index should always be the dummy driver. |
| 256 | int dummy_idx = AudioDriverManager::get_driver_count() - 1; |
| 257 | AudioDriverManager::initialize(dummy_idx); |
| 258 | AudioServer *audio_server = memnew(AudioServer); |
| 259 | audio_server->init(); |
| 260 | return; |
| 261 | } |
| 262 | |
| 263 | #ifndef NAVIGATION_3D_DISABLED |
| 264 | if (suite_name.contains("[Navigation3D]") && navigation_server_3d == nullptr) { |
| 265 | ERR_PRINT_OFF; |
| 266 | navigation_server_3d = NavigationServer3DManager::get_singleton()->new_default_server(); |
| 267 | ERR_PRINT_ON; |
| 268 | return; |
| 269 | } |
| 270 | #endif // NAVIGATION_3D_DISABLED |
| 271 | |
| 272 | #ifndef NAVIGATION_2D_DISABLED |
| 273 | if (suite_name.contains("[Navigation2D]") && navigation_server_2d == nullptr) { |
| 274 | ERR_PRINT_OFF; |
| 275 | navigation_server_2d = NavigationServer2DManager::get_singleton()->new_default_server(); |
| 276 | ERR_PRINT_ON; |
| 277 | return; |
| 278 | } |
| 279 | #endif // NAVIGATION_2D_DISABLED |
| 280 | } |
| 281 | |
| 282 | void test_case_end(const doctest::CurrentTestCaseStats &) override { |
| 283 | #ifdef TOOLS_ENABLED |
| 284 | if (EditorSettings::get_singleton()) { |
| 285 | EditorSettings::destroy(); |
| 286 | |
| 287 | // Instantiating the EditorSettings singleton sets the locale to the editor's language. |
| 288 | TranslationServer::get_singleton()->set_locale("en"); |
| 289 | } |
| 290 | if (EditorPaths::get_singleton()) { |
| 291 | EditorPaths::free(); |
| 292 | } |
| 293 | #endif // TOOLS_ENABLED |
| 294 | |
| 295 | Engine::get_singleton()->set_editor_hint(false); |
| 296 | |
| 297 | if (SceneTree::get_singleton()) { |
| 298 | SceneTree::get_singleton()->finalize(); |
| 299 | } |
| 300 | |
| 301 | if (MessageQueue::get_singleton()) { |
| 302 | MessageQueue::get_singleton()->flush(); |
| 303 | } |
| 304 | |
| 305 | memdelete(SceneTree::get_singleton()); |
| 306 | |
| 307 | #ifndef NAVIGATION_3D_DISABLED |
| 308 | if (navigation_server_3d) { |
| 309 | memdelete(navigation_server_3d); |
| 310 | navigation_server_3d = nullptr; |
| 311 | } |
| 312 | #endif // NAVIGATION_3D_DISABLED |
| 313 | |
| 314 | #ifndef NAVIGATION_2D_DISABLED |
| 315 | if (navigation_server_2d) { |
| 316 | memdelete(navigation_server_2d); |
| 317 | navigation_server_2d = nullptr; |
| 318 | } |
| 319 | #endif // NAVIGATION_2D_DISABLED |
| 320 | |
| 321 | #ifndef PHYSICS_3D_DISABLED |
| 322 | if (physics_server_3d) { |
| 323 | physics_server_3d->finish(); |
| 324 | memdelete(physics_server_3d); |
| 325 | physics_server_3d = nullptr; |
| 326 | } |
| 327 | #endif // PHYSICS_3D_DISABLED |
| 328 | |
| 329 | #ifndef PHYSICS_2D_DISABLED |
| 330 | if (physics_server_2d) { |
| 331 | physics_server_2d->finish(); |
| 332 | memdelete(physics_server_2d); |
| 333 | physics_server_2d = nullptr; |
| 334 | } |
| 335 | #endif // PHYSICS_2D_DISABLED |
| 336 | |
| 337 | memdelete(Input::get_singleton()); |
| 338 | |
| 339 | if (RenderingServer::get_singleton()) { |
| 340 | ThemeDB::get_singleton()->finalize_theme(); |
| 341 | } |
| 342 | |
| 343 | memdelete(AccessibilityServer::get_singleton()); |
| 344 | memdelete(DisplayServer::get_singleton()); |
| 345 | memdelete(InputMap::get_singleton()); |
| 346 | |
| 347 | if (AudioServer::get_singleton()) { |
| 348 | AudioServer::get_singleton()->finish(); |
| 349 | memdelete(AudioServer::get_singleton()); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | void test_run_start() override { |
| 354 | signal_watcher = memnew(SignalWatcher); |
| 355 | } |
| 356 | |
| 357 | void test_run_end(const doctest::TestRunStats &) override { |
| 358 | memdelete(signal_watcher); |
| 359 | } |
| 360 | |
| 361 | void test_case_reenter(const doctest::TestCaseData &) override { |
| 362 | reinitialize(); |
| 363 | } |
| 364 | |
| 365 | void subcase_start(const doctest::SubcaseSignature &) override { |
| 366 | reinitialize(); |
| 367 | } |
| 368 | |
| 369 | void report_query(const doctest::QueryData &) override {} |
| 370 | void test_case_exception(const doctest::TestCaseException &) override {} |
| 371 | void subcase_end() override {} |
| 372 | |
| 373 | void log_assert(const doctest::AssertData &in) override {} |
| 374 | void log_message(const doctest::MessageData &) override {} |
| 375 | void test_case_skipped(const doctest::TestCaseData &) override {} |
| 376 | |
| 377 | private: |
| 378 | void reinitialize() { |
| 379 | Math::seed(0x60d07); |
| 380 | SignalWatcher::get_singleton()->_clear_signals(); |
| 381 | } |
| 382 | }; |
| 383 | |
| 384 | REGISTER_LISTENER("GodotTestCaseListener", 1, GodotTestCaseListener); |