OpenTTD Source 20241224-master-gee860a5c8e
test_window_desc.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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 <http://www.gnu.org/licenses/>.
6 */
7
10#include "../stdafx.h"
11
12#include "../3rdparty/catch2/catch.hpp"
13
14#include "mock_environment.h"
15
16#include "../window_gui.h"
17
22extern std::vector<WindowDesc*> *_window_descs;
23
24
26private:
27 MockEnvironment &mock = MockEnvironment::Instance();
28};
29
30
31TEST_CASE("WindowDesc - ini_key uniqueness")
32{
33 std::set<std::string> seen;
34
35 for (const WindowDesc *window_desc : *_window_descs) {
36
37 if (window_desc->ini_key == nullptr) continue;
38
39 CAPTURE(window_desc->ini_key);
40 CHECK((seen.find(window_desc->ini_key) == std::end(seen)));
41
42 seen.insert(window_desc->ini_key);
43 }
44}
45
46TEST_CASE("WindowDesc - ini_key validity")
47{
48 const WindowDesc *window_desc = GENERATE(from_range(std::begin(*_window_descs), std::end(*_window_descs)));
49
50 bool has_inikey = window_desc->ini_key != nullptr;
51 bool has_widget = std::any_of(std::begin(window_desc->nwid_parts), std::end(window_desc->nwid_parts), [](const NWidgetPart &part) { return part.type == WWT_DEFSIZEBOX || part.type == WWT_STICKYBOX; });
52
53 INFO(fmt::format("{}:{}", window_desc->source_location.file_name(), window_desc->source_location.line()));
54 CAPTURE(has_inikey);
55 CAPTURE(has_widget);
56
57 CHECK((has_widget == has_inikey));
58}
59
66static bool IsNWidgetTreeClosed(std::span<const NWidgetPart> nwid_parts)
67{
68 int depth = 0;
69 for (const auto nwid : nwid_parts) {
70 if (IsContainerWidgetType(nwid.type)) ++depth;
71 if (nwid.type == WPT_ENDCONTAINER) --depth;
72 }
73 return depth == 0;
74}
75
76TEST_CASE("WindowDesc - NWidgetParts properly closed")
77{
78 const WindowDesc *window_desc = GENERATE(from_range(std::begin(*_window_descs), std::end(*_window_descs)));
79
80 INFO(fmt::format("{}:{}", window_desc->source_location.file_name(), window_desc->source_location.line()));
81
82 CHECK(IsNWidgetTreeClosed(window_desc->nwid_parts));
83}
84
85TEST_CASE_METHOD(WindowDescTestsFixture, "WindowDesc - NWidgetPart validity")
86{
87 const WindowDesc *window_desc = GENERATE(from_range(std::begin(*_window_descs), std::end(*_window_descs)));
88
89 INFO(fmt::format("{}:{}", window_desc->source_location.file_name(), window_desc->source_location.line()));
90
91 NWidgetStacked *shade_select = nullptr;
92 std::unique_ptr<NWidgetBase> root = nullptr;
93
94 REQUIRE_NOTHROW(root = MakeWindowNWidgetTree(window_desc->nwid_parts, &shade_select));
95 CHECK((root != nullptr));
96}
Singleton class to set up the mock environemnt once.
Stacked widgets, widgets all occupying the same space in the window.
std::unique_ptr< NWidgetBase > MakeWindowNWidgetTree(std::span< const NWidgetPart > nwid_parts, NWidgetStacked **shade_select)
Make a nested widget tree for a window from a parts array.
Definition widget.cpp:3316
Singleton instance to create a mock FontCache/SpriteCache environment.
Partial widget specification to allow NWidgets to be written nested.
High level window description.
Definition window_gui.h:159
const char * ini_key
Key to store window defaults in openttd.cfg. nullptr if nothing shall be stored.
Definition window_gui.h:172
const std::source_location source_location
Source location of this definition.
Definition window_gui.h:168
const std::span< const NWidgetPart > nwid_parts
Span of nested widget parts describing the window.
Definition window_gui.h:174
static bool IsNWidgetTreeClosed(std::span< const NWidgetPart > nwid_parts)
Test if a NWidgetTree is properly closed, meaning the number of container-type parts matches the numb...
std::vector< WindowDesc * > * _window_descs
List of WindowDescs.
Definition window.cpp:99
bool IsContainerWidgetType(WidgetType tp)
Test if WidgetType is a container widget.
Definition widget.cpp:3240
@ WPT_ENDCONTAINER
Widget part to denote end of a container.