From b1eb5533ebb800f8951cbc5914e041a0e2d8338d Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Thu, 2 Nov 2023 11:16:37 +0000 Subject: [PATCH] Add: WindowDesc unit test to validate NWidgetPart lists. --- src/tests/CMakeLists.txt | 1 + src/tests/mock_environment.h | 39 ++++++++++++++++++++++++++++++++++ src/tests/test_window_desc.cpp | 23 ++++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 src/tests/mock_environment.h diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt index 18390c1eb8..b8380bed24 100644 --- a/src/tests/CMakeLists.txt +++ b/src/tests/CMakeLists.txt @@ -1,6 +1,7 @@ add_test_files( landscape_partial_pixel_z.cpp math_func.cpp + mock_environment.h mock_fontcache.h mock_spritecache.cpp mock_spritecache.h diff --git a/src/tests/mock_environment.h b/src/tests/mock_environment.h new file mode 100644 index 0000000000..470914d002 --- /dev/null +++ b/src/tests/mock_environment.h @@ -0,0 +1,39 @@ +/* + * This file is part of OpenTTD. + * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. + * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see . + */ + +/** @file mock_environment.h Singleton instance to create a mock FontCache/SpriteCache environment. */ + +#ifndef MOCK_ENVIRONMENT_H +#define MOCK_ENVIRONMENT_H + +#include "mock_fontcache.h" +#include "mock_spritecache.h" + +/** Singleton class to set up the mock environemnt once. */ +class MockEnvironment { +public: + static MockEnvironment &Instance() + { + static MockEnvironment instance; + return instance; + } + + MockEnvironment(MockEnvironment const &) = delete; + void operator=(MockEnvironment const &) = delete; + +private: + MockEnvironment() + { + /* Mock SpriteCache initialization is needed for some widget generators. */ + MockGfxLoadSprites(); + + /* Mock FontCache initialization is needed for some NWidgetParts. */ + MockFontCache::InitializeFontCaches(); + } +}; + +#endif /* MOCK_ENVIRONMENT_H */ diff --git a/src/tests/test_window_desc.cpp b/src/tests/test_window_desc.cpp index 64f95c58b5..1a5be4a1c6 100644 --- a/src/tests/test_window_desc.cpp +++ b/src/tests/test_window_desc.cpp @@ -11,6 +11,8 @@ #include "../3rdparty/catch2/catch.hpp" +#include "mock_environment.h" + #include "../window_gui.h" /** @@ -19,6 +21,13 @@ */ extern std::vector *_window_descs; + +class WindowDescTestsFixture { +private: + MockEnvironment &mock = MockEnvironment::Instance(); +}; + + TEST_CASE("WindowDesc - ini_key uniqueness") { std::set seen; @@ -73,3 +82,17 @@ TEST_CASE("WindowDesc - NWidgetParts properly closed") CHECK(IsNWidgetTreeClosed(window_desc->nwid_begin, window_desc->nwid_end)); } + +TEST_CASE_METHOD(WindowDescTestsFixture, "WindowDesc - NWidgetPart validity") +{ + const WindowDesc *window_desc = GENERATE(from_range(std::begin(*_window_descs), std::end(*_window_descs))); + + INFO(fmt::format("{}:{}", window_desc->file, window_desc->line)); + + int biggest_index = -1; + NWidgetStacked *shade_select = nullptr; + NWidgetBase *root = nullptr; + + REQUIRE_NOTHROW(root = MakeWindowNWidgetTree(window_desc->nwid_begin, window_desc->nwid_end, &biggest_index, &shade_select)); + CHECK((root != nullptr)); +}