1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
#include <SFML/Window/WindowHandle.hpp>
// Other 1st party headers
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Window/Window.hpp>
#include <SFML/Window/WindowBase.hpp>
#include <catch2/catch_test_macros.hpp>
#include <catch2/generators/catch_generators.hpp>
#include <SystemUtil.hpp>
#include <optional>
#ifndef NOMINMAX
#define NOMINMAX
#endif
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <Windows.h>
namespace
{
bool gotWmShowWindow = false;
LRESULT WINAPI wndProc(HWND handle, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_SHOWWINDOW:
gotWmShowWindow = true;
assert(wParam == TRUE); // If wParam is TRUE, the window is being shown
assert(lParam == 0); // If lParam is zero, the message was sent because of a call to the ShowWindow function
break;
case WM_CLOSE:
PostQuitMessage(0);
return 0;
}
return DefWindowProcW(handle, message, wParam, lParam);
}
sf::WindowHandle createWindowWrapper(LPWSTR className, HINSTANCE hInstance, DWORD dwExStyle, bool withMenu)
{
HMENU hMenu = nullptr;
if (withMenu)
{
hMenu = CreateMenu();
AppendMenuW(hMenu, MF_STRING, 1, L"Test");
}
return CreateWindowExW(dwExStyle,
className,
L"WindowHandle Tests",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
640,
480,
nullptr,
hMenu,
hInstance,
nullptr);
}
} // namespace
TEST_CASE("[Window] sf::WindowHandle (Win32)")
{
const auto exStyle = GENERATE(DWORD{0}, WS_EX_TOOLWINDOW | WS_EX_CLIENTEDGE);
const auto withMenu = GENERATE(false, true);
INFO("ExStyle: " << std::hex << exStyle << ", withMenu: " << withMenu);
const WNDCLASSW classInfo{{}, wndProc, {}, {}, GetModuleHandleW(nullptr), {}, {}, {}, {}, L"sfml_WindowHandleTests"};
const ATOM winClassId = RegisterClassW(&classInfo);
REQUIRE(winClassId);
const sf::WindowHandle handle = createWindowWrapper(reinterpret_cast<LPWSTR>(static_cast<ULONG_PTR>(winClassId)),
classInfo.hInstance,
exStyle,
withMenu);
REQUIRE(handle);
REQUIRE(!gotWmShowWindow);
REQUIRE(IsWindow(handle));
RECT windowRect{};
REQUIRE(GetWindowRect(handle, &windowRect));
const auto position = sf::Vector2i(sf::Vector2(windowRect.left, windowRect.top));
RECT clientRect{};
REQUIRE(GetClientRect(handle, &clientRect));
const auto initialSize = sf::Vector2u(
sf::Vector2(clientRect.right - clientRect.left, clientRect.bottom - clientRect.top));
constexpr sf::Vector2u newSize(640, 480);
SECTION("sf::WindowBase")
{
std::optional<sf::WindowBase> windowBase;
SECTION("WindowHandle constructor")
{
windowBase.emplace(handle);
}
SECTION("create(WindowHandle)")
{
windowBase.emplace().create(handle);
}
INFO("ExStyle: " << std::hex << exStyle << ", withMenu: " << withMenu);
CHECK(windowBase->isOpen());
CHECK(windowBase->getPosition() == position);
CHECK(windowBase->getSize() == initialSize);
CHECK(windowBase->getNativeHandle() == handle);
CHECK(windowBase->getSize() != newSize);
windowBase->setSize(newSize);
REQUIRE(GetClientRect(handle, &clientRect));
const auto size = sf::Vector2u(sf::Vector2(clientRect.right - clientRect.left, clientRect.bottom - clientRect.top));
CHECK(size == newSize); // Validate that the actual client rect is indeed what we asked for
CHECK(windowBase->getSize() == size); // Validate that the `getSize` also returns the _actual_ client size
}
SECTION("sf::Window")
{
std::optional<sf::Window> window;
SECTION("Default context settings")
{
SECTION("WindowHandle constructor")
{
window.emplace(handle);
}
SECTION("create(WindowHandle)")
{
window.emplace().create(handle);
}
INFO("ExStyle: " << std::hex << exStyle << ", withMenu: " << withMenu);
CHECK(window->getSettings().attributeFlags == sf::ContextSettings::Default);
}
SECTION("Custom context settings")
{
static constexpr sf::ContextSettings contextSettings{/* depthBits*/ 1, /* stencilBits */ 1, /* antiAliasingLevel */ 1};
SECTION("WindowHandle constructor")
{
window.emplace(handle, contextSettings);
}
SECTION("create(WindowHandle)")
{
window.emplace().create(handle, contextSettings);
}
INFO("ExStyle: " << std::hex << exStyle << ", withMenu: " << withMenu);
CHECK(window->getSettings().depthBits >= 1);
CHECK(window->getSettings().stencilBits >= 1);
CHECK(window->getSettings().antiAliasingLevel >= 1);
}
INFO("ExStyle: " << std::hex << exStyle << ", withMenu: " << withMenu);
CHECK(window->isOpen());
CHECK(window->getPosition() == position);
CHECK(window->getSize() == initialSize);
CHECK(window->getNativeHandle() == handle);
CHECK(window->getSize() != newSize);
window->setSize(newSize);
REQUIRE(GetClientRect(handle, &clientRect));
const auto size = sf::Vector2u(sf::Vector2(clientRect.right - clientRect.left, clientRect.bottom - clientRect.top));
CHECK(size == newSize); // Validate that the actual client rect is indeed what we asked for
CHECK(window->getSize() == size); // Validate that the `getSize` also returns the _actual_ client size
}
SECTION("sf::RenderWindow")
{
std::optional<sf::RenderWindow> renderWindow;
SECTION("Default context settings")
{
SECTION("WindowHandle constructor")
{
renderWindow.emplace(handle);
}
SECTION("create(WindowHandle)")
{
renderWindow.emplace().create(handle);
}
INFO("ExStyle: " << std::hex << exStyle << ", withMenu: " << withMenu);
CHECK(renderWindow->getSettings().attributeFlags == sf::ContextSettings::Default);
}
SECTION("Custom context settings")
{
static constexpr sf::ContextSettings contextSettings{/* depthBits*/ 1, /* stencilBits */ 1, /* antiAliasingLevel */ 1};
SECTION("WindowHandle constructor")
{
renderWindow.emplace(handle, contextSettings);
}
SECTION("create(WindowHandle)")
{
renderWindow.emplace().create(handle, contextSettings);
}
INFO("ExStyle: " << std::hex << exStyle << ", withMenu: " << withMenu);
CHECK(renderWindow->getSettings().depthBits >= 1);
CHECK(renderWindow->getSettings().stencilBits >= 1);
CHECK(renderWindow->getSettings().antiAliasingLevel >= 1);
}
INFO("ExStyle: " << std::hex << exStyle << ", withMenu: " << withMenu);
CHECK(renderWindow->isOpen());
CHECK(renderWindow->getPosition() == position);
CHECK(renderWindow->getSize() == initialSize);
CHECK(renderWindow->getNativeHandle() == handle);
CHECK(renderWindow->getSize() != newSize);
renderWindow->setSize(newSize);
REQUIRE(GetClientRect(handle, &clientRect));
const auto size = sf::Vector2u(sf::Vector2(clientRect.right - clientRect.left, clientRect.bottom - clientRect.top));
CHECK(size == newSize); // Validate that the actual client rect is indeed what we asked for
CHECK(renderWindow->getSize() == size); // Validate that the `getSize` also returns the _actual_ client size
}
INFO("ExStyle: " << std::hex << exStyle << ", withMenu: " << withMenu);
CHECK(gotWmShowWindow);
CHECK(IsWindow(handle)); // The window is not destroyed
CHECK(DestroyWindow(handle));
CHECK(UnregisterClassW(classInfo.lpszClassName, classInfo.hInstance));
gotWmShowWindow = false;
CHECK(!IsWindow(handle)); // Now it is gone
}
|