Skip to content

Commit fccbc9e

Browse files
Add unit test for in-game window positioning
1 parent a3b7fd1 commit fccbc9e

1 file changed

Lines changed: 166 additions & 0 deletions

File tree

tests/s25Main/UI/testIngameWindow.cpp

Lines changed: 166 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,166 @@ BOOST_AUTO_TEST_CASE(SaveAndRestoreMinimized)
228232
}
229233
}
230234

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

0 commit comments

Comments
 (0)