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 246 247 248 249 250 251 252 253
|
#include <SFML/Window/WindowBase.hpp>
// Other 1st party headers
#include <SFML/Window/Event.hpp>
#include <SFML/Window/VideoMode.hpp>
#include <SFML/System/String.hpp>
#include <catch2/catch_test_macros.hpp>
#include <WindowUtil.hpp>
#include <chrono>
#include <memory>
#include <type_traits>
#include <utility>
TEST_CASE("[Window] sf::WindowBase", runDisplayTests())
{
SECTION("Type traits")
{
STATIC_CHECK(std::has_virtual_destructor_v<sf::WindowBase>);
STATIC_CHECK(!std::is_copy_constructible_v<sf::WindowBase>);
STATIC_CHECK(!std::is_copy_assignable_v<sf::WindowBase>);
STATIC_CHECK(std::is_nothrow_move_constructible_v<sf::WindowBase>);
STATIC_CHECK(std::is_nothrow_move_assignable_v<sf::WindowBase>);
}
SECTION("Construction")
{
SECTION("Default constructor")
{
const sf::WindowBase windowBase;
CHECK(!windowBase.isOpen());
CHECK(windowBase.getPosition() == sf::Vector2i());
CHECK(windowBase.getSize() == sf::Vector2u());
CHECK(!windowBase.hasFocus());
CHECK(windowBase.getNativeHandle() == sf::WindowHandle());
}
SECTION("Mode and title constructor")
{
const sf::WindowBase windowBase(sf::VideoMode({360, 240}), "WindowBase Tests");
CHECK(windowBase.isOpen());
CHECK(windowBase.getSize() == sf::Vector2u(360, 240));
CHECK(windowBase.getNativeHandle() != sf::WindowHandle());
}
SECTION("Mode, title, and style constructor")
{
const sf::WindowBase windowBase(sf::VideoMode({360, 240}), "WindowBase Tests", sf::Style::Resize);
CHECK(windowBase.isOpen());
CHECK(windowBase.getSize() == sf::Vector2u(360, 240));
CHECK(windowBase.getNativeHandle() != sf::WindowHandle());
}
SECTION("Mode, title, style, and state constructor")
{
const sf::WindowBase windowBase(sf::VideoMode({360, 240}), "WindowBase Tests", sf::Style::Resize, sf::State::Windowed);
CHECK(windowBase.isOpen());
CHECK(windowBase.getSize() == sf::Vector2u(360, 240));
CHECK(windowBase.getNativeHandle() != sf::WindowHandle());
}
SECTION("Mode, title, and state constructor")
{
const sf::WindowBase windowBase(sf::VideoMode({360, 240}), "WindowBase Tests", sf::State::Windowed);
CHECK(windowBase.isOpen());
CHECK(windowBase.getSize() == sf::Vector2u(360, 240));
CHECK(windowBase.getNativeHandle() != sf::WindowHandle());
}
}
SECTION("create()")
{
sf::WindowBase windowBase;
SECTION("Mode and title")
{
windowBase.create(sf::VideoMode({240, 360}), "WindowBase Tests");
CHECK(windowBase.isOpen());
CHECK(windowBase.getSize() == sf::Vector2u(240, 360));
CHECK(windowBase.getNativeHandle() != sf::WindowHandle());
}
SECTION("Mode, title, and style")
{
windowBase.create(sf::VideoMode({240, 360}), "WindowBase Tests", sf::Style::Resize);
CHECK(windowBase.isOpen());
CHECK(windowBase.getSize() == sf::Vector2u(240, 360));
CHECK(windowBase.getNativeHandle() != sf::WindowHandle());
}
SECTION("Mode, title, and state")
{
windowBase.create(sf::VideoMode({240, 360}), "WindowBase Tests", sf::State::Windowed);
CHECK(windowBase.isOpen());
CHECK(windowBase.getSize() == sf::Vector2u(240, 360));
CHECK(windowBase.getNativeHandle() != sf::WindowHandle());
}
SECTION("Mode, title, style, and state")
{
windowBase.create(sf::VideoMode({240, 360}), "WindowBase Tests", sf::Style::Resize, sf::State::Windowed);
CHECK(windowBase.isOpen());
CHECK(windowBase.getSize() == sf::Vector2u(240, 360));
CHECK(windowBase.getNativeHandle() != sf::WindowHandle());
}
}
SECTION("close()")
{
sf::WindowBase windowBase(sf::VideoMode({360, 240}), "WindowBase Tests");
windowBase.close();
CHECK(!windowBase.isOpen());
}
SECTION("pollEvent()")
{
SECTION("Uninitialized window")
{
sf::WindowBase windowBase;
CHECK(!windowBase.pollEvent());
}
}
SECTION("waitEvent()")
{
SECTION("Uninitialized window")
{
sf::WindowBase windowBase;
CHECK(!windowBase.waitEvent());
}
SECTION("Initialized window")
{
sf::WindowBase windowBase(sf::VideoMode({360, 240}), "WindowBase Tests");
constexpr auto timeout = sf::milliseconds(100);
const auto startTime = std::chrono::steady_clock::now();
const auto event = windowBase.waitEvent(timeout);
const auto elapsed = std::chrono::steady_clock::now() - startTime;
REQUIRE(elapsed < (timeout + sf::milliseconds(100)).toDuration());
if (elapsed <= timeout.toDuration())
CHECK(event);
else
CHECK(!event);
}
}
SECTION("Set/get position")
{
sf::WindowBase windowBase;
windowBase.setPosition({12, 34});
CHECK(windowBase.getPosition() == sf::Vector2i());
}
SECTION("Set/get size")
{
SECTION("Uninitialized window")
{
sf::WindowBase windowBase;
windowBase.setSize({128, 256});
CHECK(windowBase.getSize() == sf::Vector2u());
}
SECTION("Initialized window")
{
sf::WindowBase windowBase(sf::VideoMode({360, 240}), "WindowBase Tests");
windowBase.setSize({128, 256});
CHECK(windowBase.getSize() == sf::Vector2u(128, 256));
}
SECTION("Minimum size")
{
sf::WindowBase windowBase(sf::VideoMode({360, 240}), "WindowBase Tests");
windowBase.setMinimumSize(sf::Vector2u(128, 256));
windowBase.setSize({100, 100});
CHECK(windowBase.getSize() == sf::Vector2u(128, 256));
}
SECTION("Maximum size")
{
sf::WindowBase windowBase(sf::VideoMode({360, 240}), "WindowBase Tests");
windowBase.setMaximumSize(sf::Vector2u(128, 256));
windowBase.setSize({400, 400});
CHECK(windowBase.getSize() == sf::Vector2u(128, 256));
}
}
SECTION("setMinimumSize()")
{
sf::WindowBase windowBase(sf::VideoMode({100, 100}), "WindowBase Tests", sf::Style::Default ^ sf::Style::Resize);
windowBase.setMinimumSize(sf::Vector2u(200, 300));
CHECK(windowBase.getSize() == sf::Vector2u(200, 300));
windowBase.setMaximumSize(sf::Vector2u(200, 300));
}
SECTION("setMinimumSize()")
{
sf::WindowBase windowBase(sf::VideoMode({400, 400}), "WindowBase Tests", sf::Style::Default ^ sf::Style::Resize);
windowBase.setMaximumSize(sf::Vector2u(200, 300));
CHECK(windowBase.getSize() == sf::Vector2u(200, 300));
windowBase.setMinimumSize(sf::Vector2u(200, 300));
}
// Test for compilation but do not run. This code sometimes hangs indefinitely
// when running on the BuildBot CI pipeline. Because it contains no
// assertions we have nothing to gain by running it anyways
(void)[]
{
sf::WindowBase windowBase(sf::VideoMode({360, 240}), "WindowBase Tests");
// Should compile if user provides only a specific handler
windowBase.handleEvents([](const sf::Event::Closed&) {});
// Should compile if user provides only a catch-all
windowBase.handleEvents([](const auto&) {});
// Should compile if user provides both a specific handler and a catch-all
windowBase.handleEvents([](const sf::Event::Closed&) {}, [](const auto&) {});
// Should compile if user provides a handler taking an event subtype by value or reference,
// but not rvalue reference because it would never be called.
windowBase.handleEvents([](sf::Event::Closed) {});
windowBase.handleEvents([](const sf::Event::Closed) {});
windowBase.handleEvents([](sf::Event::Closed&) {});
windowBase.handleEvents([](const sf::Event::Closed&) {});
// Should compile if user provides a move-only handler
windowBase.handleEvents([p = std::make_unique<int>()](const sf::Event::Closed&) {});
// Should compile if user provides a handler with deleted rvalue ref-qualified call operator
struct LvalueOnlyHandler
{
void operator()(const sf::Event::Closed&) &
{
}
void operator()(const sf::Event::Closed&) && = delete;
};
windowBase.handleEvents(LvalueOnlyHandler{});
// Should compile if user provides a reference to a handler
auto handler = [](const sf::Event::Closed&) {};
windowBase.handleEvents(handler);
windowBase.handleEvents(std::as_const(handler));
// Should compile if user provides a function pointer
windowBase.handleEvents(+[](const sf::Event::Closed&) {});
};
}
|