Skip to content

Commit 014f0d5

Browse files
Add unit test for in-game window positioning
1 parent df17ca3 commit 014f0d5

1 file changed

Lines changed: 169 additions & 0 deletions

File tree

tests/s25Main/UI/testIngameWindow.cpp

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// SPDX-License-Identifier: GPL-2.0-or-later
44

55
#include "DrawPoint.h"
6+
#include "Loader.h"
67
#include "Point.h"
78
#include "PointOutput.h"
89
#include "Settings.h"
@@ -14,20 +15,23 @@
1415
#include "controls/ctrlPercent.h"
1516
#include "controls/ctrlProgress.h"
1617
#include "desktops/dskGameLobby.h"
18+
#include "drivers/VideoDriverWrapper.h"
1719
#include "helpers/format.hpp"
1820
#include "ingameWindows/IngameWindow.h"
1921
#include "ingameWindows/iwConnecting.h"
2022
#include "ingameWindows/iwDirectIPConnect.h"
2123
#include "ingameWindows/iwHelp.h"
2224
#include "ingameWindows/iwMapGenerator.h"
2325
#include "ingameWindows/iwMsgbox.h"
26+
#include "ogl/glArchivItem_Bitmap.h"
2427
#include "uiHelper/uiHelpers.hpp"
2528
#include "gameTypes/GameTypesOutput.h"
2629
#include "gameData/const_gui_ids.h"
2730
#include "rttr/test/random.hpp"
2831
#include "s25util/StringConversion.h"
2932
#include <turtle/mock.hpp>
3033
#include <boost/test/unit_test.hpp>
34+
#include <functional>
3135

3236
// LCOV_EXCL_START
3337
BOOST_TEST_DONT_PRINT_LOG_VALUE(rttr::mapGenerator::MapStyle)
@@ -228,4 +232,169 @@ BOOST_AUTO_TEST_CASE(SaveAndRestoreMinimized)
228232
}
229233
}
230234

235+
namespace {
236+
void WindowPositioning_testOne(IngameWindow& wnd, const char* msg, std::function<void()> main,
237+
std::function<void()> minimize)
238+
{
239+
BOOST_TEST_MESSAGE(msg << " (1)");
240+
main();
241+
242+
BOOST_TEST_MESSAGE("Minimize");
243+
wnd.SetMinimized(true);
244+
245+
minimize();
246+
247+
BOOST_TEST_MESSAGE("Un-minimize");
248+
wnd.SetMinimized(false);
249+
250+
BOOST_TEST_MESSAGE(msg << " (2)");
251+
main();
252+
}
253+
} // namespace
254+
255+
BOOST_AUTO_TEST_CASE(WindowPositioning)
256+
{
257+
VIDEODRIVER.ResizeScreen(VideoMode(800, 600), false);
258+
259+
const auto renderSize = VIDEODRIVER.GetRenderSize();
260+
261+
constexpr auto idPersisted = CGI_MINIMAP;
262+
constexpr auto idNonPersisted = CGI_OBSERVATION;
263+
constexpr auto wndSizeS = Extent(50, 50);
264+
constexpr auto wndSizeM = Extent(90, 90);
265+
constexpr auto wndSizeL = Extent(200, 200);
266+
constexpr auto offset = DrawPoint(100, 100);
267+
268+
auto it = SETTINGS.windows.persistentSettings.find(idNonPersisted);
269+
BOOST_REQUIRE(it == SETTINGS.windows.persistentSettings.end());
270+
271+
it = SETTINGS.windows.persistentSettings.find(idPersisted);
272+
BOOST_REQUIRE(it != SETTINGS.windows.persistentSettings.end());
273+
auto& settings = it->second;
274+
275+
// Calculate minimized height
276+
const auto minHeight = LOADER.GetImageN("resource", 42)->getHeight() // title bar
277+
+ LOADER.GetImageN("resource", 40)->getHeight(); // bottom bar
278+
279+
{
280+
BOOST_TEST_MESSAGE("Persisted window, fresh settings, posLastOrCenter");
281+
282+
settings = PersistentWindowSettings{};
283+
284+
IngameWindow wnd(idPersisted, IngameWindow::posLastOrCenter, wndSizeM, "Test Window", nullptr);
285+
286+
WindowPositioning_testOne(
287+
wnd, "Window should be centered",
288+
[&]() {
289+
BOOST_TEST(wnd.GetPos() == (renderSize / 2 - wndSizeM / 2));
290+
BOOST_TEST(wnd.GetPos() == settings.lastPos);
291+
BOOST_TEST(wnd.GetPos() == settings.restorePos);
292+
BOOST_TEST(wnd.GetSize() == wndSizeM);
293+
},
294+
[&]() {
295+
BOOST_TEST(wnd.GetPos() == settings.lastPos);
296+
BOOST_TEST(wnd.GetPos() == settings.restorePos);
297+
BOOST_TEST(wnd.GetPos() == (renderSize / 2 - wndSizeM / 2));
298+
BOOST_TEST(wnd.GetSize() == Extent(wndSizeM.x, minHeight));
299+
});
300+
301+
const auto restorePos = renderSize - offset; // new position is also the restorePos
302+
WindowPositioning_testOne(
303+
wnd, "Move window into bottom right corner, not connecting with the screen edges",
304+
[&]() {
305+
wnd.SetPos(restorePos);
306+
BOOST_TEST(wnd.GetPos() == restorePos);
307+
BOOST_TEST(wnd.GetPos() == settings.lastPos);
308+
BOOST_TEST(wnd.GetPos() == settings.restorePos);
309+
BOOST_TEST(wnd.GetSize() == wndSizeM);
310+
},
311+
[&]() {
312+
BOOST_TEST(wnd.GetPos() == restorePos);
313+
BOOST_TEST(wnd.GetPos() == settings.lastPos);
314+
BOOST_TEST(wnd.GetPos() == settings.restorePos);
315+
BOOST_TEST(wnd.GetSize() == Extent(wndSizeM.x, minHeight));
316+
});
317+
318+
WindowPositioning_testOne(
319+
wnd, "Resize up (L), window connects with screen edges and should move",
320+
[&]() {
321+
wnd.Resize(wndSizeL);
322+
BOOST_TEST(wnd.GetPos() == DrawPoint(renderSize - wndSizeL));
323+
BOOST_TEST(wnd.GetPos() == settings.lastPos);
324+
BOOST_TEST(restorePos == settings.restorePos);
325+
BOOST_TEST(wnd.GetSize() == wndSizeL);
326+
},
327+
[&]() {
328+
BOOST_TEST(wnd.GetPos() == renderSize - DrawPoint(wndSizeL.x, minHeight));
329+
BOOST_TEST(wnd.GetPos() == settings.lastPos);
330+
BOOST_TEST(restorePos == settings.restorePos);
331+
BOOST_TEST(wnd.GetSize() == Extent(wndSizeL.x, minHeight));
332+
});
333+
334+
WindowPositioning_testOne(
335+
wnd, "Resize down (S), window no longer connects with screen edges and should move to restorePos",
336+
[&]() {
337+
wnd.Resize(wndSizeS);
338+
BOOST_TEST(wnd.GetPos() == restorePos);
339+
BOOST_TEST(wnd.GetPos() == settings.lastPos);
340+
BOOST_TEST(restorePos == settings.restorePos);
341+
BOOST_TEST(wnd.GetSize() == wndSizeS);
342+
},
343+
[&]() {
344+
BOOST_TEST(wnd.GetPos() == restorePos);
345+
BOOST_TEST(wnd.GetPos() == settings.lastPos);
346+
BOOST_TEST(restorePos == settings.restorePos);
347+
BOOST_TEST(wnd.GetSize() == Extent(wndSizeS.x, minHeight));
348+
});
349+
}
350+
351+
{
352+
BOOST_TEST_MESSAGE("Non-persisted window, posAtMouse");
353+
354+
// the offset subtracted from the window position for posAtMouse
355+
constexpr auto cursorOffset = DrawPoint(-20, wndSizeM.y / 2);
356+
const auto restorePos = renderSize - offset; // initial window position is also the restorePos
357+
358+
VIDEODRIVER.SetMousePos(restorePos + cursorOffset);
359+
BOOST_REQUIRE(VIDEODRIVER.GetMousePos() == (restorePos + cursorOffset));
360+
361+
IngameWindow wnd(idNonPersisted, IngameWindow::posAtMouse, wndSizeM, "Test Window", nullptr);
362+
363+
WindowPositioning_testOne(
364+
wnd, "Window should be at cursor",
365+
[&]() {
366+
BOOST_TEST(wnd.GetPos() == restorePos);
367+
BOOST_TEST(wnd.GetSize() == wndSizeM);
368+
},
369+
[&]() {
370+
BOOST_TEST(wnd.GetPos() == restorePos);
371+
BOOST_TEST(wnd.GetSize() == Extent(wndSizeM.x, minHeight));
372+
});
373+
374+
WindowPositioning_testOne(
375+
wnd, "Resize up (L), window connects with screen edges and should move",
376+
[&]() {
377+
wnd.Resize(wndSizeL);
378+
BOOST_TEST(wnd.GetPos() == DrawPoint(renderSize - wndSizeL));
379+
BOOST_TEST(wnd.GetSize() == wndSizeL);
380+
},
381+
[&]() {
382+
BOOST_TEST(wnd.GetPos() == renderSize - DrawPoint(wndSizeL.x, minHeight));
383+
BOOST_TEST(wnd.GetSize() == Extent(wndSizeL.x, minHeight));
384+
});
385+
386+
WindowPositioning_testOne(
387+
wnd, "Resize down (S), window no longer connects with screen edges and should move to restorePos",
388+
[&]() {
389+
wnd.Resize(wndSizeS);
390+
BOOST_TEST(wnd.GetPos() == restorePos);
391+
BOOST_TEST(wnd.GetSize() == wndSizeS);
392+
},
393+
[&]() {
394+
BOOST_TEST(wnd.GetPos() == restorePos);
395+
BOOST_TEST(wnd.GetSize() == Extent(wndSizeS.x, minHeight));
396+
});
397+
}
398+
}
399+
231400
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)