From 4c2180e260ada6c78ceed340b84c5b3d768c4ac1 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Tue, 19 Jan 2016 05:31:19 +0300 Subject: [PATCH 1/9] Add first portion of live window tests --- .gitignore | 1 + tests/CMakeLists.txt | 1 + tests/live_window.cc | 68 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 tests/live_window.cc diff --git a/.gitignore b/.gitignore index a0771e7..ca7b258 100644 --- a/.gitignore +++ b/.gitignore @@ -39,6 +39,7 @@ libSDL2pp.so.* tests/*.exe tests/live_mixer tests/live_rendering +tests/live_window tests/test_error tests/test_optional tests/test_pointrect diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 7424787..36bb3d1 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -10,6 +10,7 @@ SET(CLI_TESTS # live tests require X11 display and/or audio output SET(LIVE_TESTS live_rendering + live_window ) IF(SDL2PP_WITH_MIXER) diff --git a/tests/live_window.cc b/tests/live_window.cc new file mode 100644 index 0000000..0c1a9d9 --- /dev/null +++ b/tests/live_window.cc @@ -0,0 +1,68 @@ +#include + +#include +#include + +#include "testing.h" + +using namespace SDL2pp; + +static void ProcessEvents() { + SDL_Event event; + while (SDL_PollEvent(&event)) { + } +} + +BEGIN_TEST(int, char*[]) + SDL sdl(SDL_INIT_VIDEO); + Window window("libSDL2pp test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 320, 240, 0); + + ProcessEvents(); + + { + // Size + EXPECT_EQUAL(window.GetSize(), Point(320, 240)); + EXPECT_EQUAL(window.GetWidth(), 320); + EXPECT_EQUAL(window.GetHeight(), 240); + + window.SetSize(400, 300); + + EXPECT_EQUAL(window.GetSize(), Point(400, 300)); + EXPECT_EQUAL(window.GetWidth(), 400); + EXPECT_EQUAL(window.GetHeight(), 300); + } + + { + // Title + EXPECT_EQUAL(window.GetTitle(), "libSDL2pp test"); + + window.SetTitle("libSDL2pp window test"); + + EXPECT_EQUAL(window.GetTitle(), "libSDL2pp window test"); + } + + { + // Drawable size + EXPECT_EQUAL(window.GetDrawableSize(), Point(window.GetDrawableWidth(), window.GetDrawableHeight())); + + // Drawable may be larget than window size, see SDL docs + // Should we compare with multiplies of window size? + EXPECT_TRUE(window.GetDrawableWidth() >= window.GetWidth()); + EXPECT_TRUE(window.GetDrawableHeight() >= window.GetHeight()); + EXPECT_TRUE(window.GetDrawableWidth() > window.GetDrawableHeight()); + } + + { + // Position + Point old_pos = window.GetPosition(); + + window.SetPosition(old_pos + Point(2, 1)); + + EXPECT_EQUAL(window.GetPosition(), old_pos + Point(2, 1)); + + window.SetPosition(old_pos.x + 4, old_pos.y + 2); + + EXPECT_EQUAL(window.GetPosition(), old_pos + Point(4, 2)); + } + +END_TEST() From 317cf6b39189dd4cde709240ff2292165be848f2 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Thu, 21 Jan 2016 22:05:47 +0300 Subject: [PATCH 2/9] Add more window tests --- tests/live_window.cc | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/tests/live_window.cc b/tests/live_window.cc index 0c1a9d9..1d98a86 100644 --- a/tests/live_window.cc +++ b/tests/live_window.cc @@ -65,4 +65,45 @@ BEGIN_TEST(int, char*[]) EXPECT_EQUAL(window.GetPosition(), old_pos + Point(4, 2)); } + { + // Min/max size + window.SetMinimumSize(16, 16); + window.SetMaximumSize(1600, 1600); + + EXPECT_EQUAL(window.GetMinimumSize(), Point(16, 16)); + EXPECT_EQUAL(window.GetMaximumSize(), Point(1600, 1600)); + + window.SetMinimumSize(Point(32, 32)); + window.SetMaximumSize(Point(3200, 3200)); + + EXPECT_EQUAL(window.GetMinimumSize(), Point(32, 32)); + EXPECT_EQUAL(window.GetMaximumSize(), Point(3200, 3200)); + } + + { + // Grab + EXPECT_TRUE(!window.GetGrab()); + + window.SetGrab(true); + + EXPECT_TRUE(window.GetGrab()); + + window.SetGrab(false); + + EXPECT_TRUE(!window.GetGrab()); + } + + { + // Brightness + EXPECT_EQUAL(window.GetBrightness(), 1.0f); + + window.SetBrightness(1.2f); + + EXPECT_EQUAL(window.GetBrightness(), 1.2f); + + window.SetBrightness(1.0f); + + EXPECT_EQUAL(window.GetBrightness(), 1.0f); + } + END_TEST() From 0e8331bbab4b70cb8faa411944571ae8f0a63bd5 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Sun, 24 Jan 2016 19:53:04 +0300 Subject: [PATCH 3/9] More window tests --- tests/live_window.cc | 113 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 95 insertions(+), 18 deletions(-) diff --git a/tests/live_window.cc b/tests/live_window.cc index 1d98a86..287aba9 100644 --- a/tests/live_window.cc +++ b/tests/live_window.cc @@ -1,4 +1,4 @@ -#include +#include #include #include @@ -7,29 +7,37 @@ using namespace SDL2pp; -static void ProcessEvents() { +static void EventSleep(Uint32 ms) { + Uint32 now = SDL_GetTicks(); + Uint32 deadline = now + ms; + SDL_Event event; - while (SDL_PollEvent(&event)) { - } + while ((now = SDL_GetTicks()) < deadline) + SDL_WaitEventTimeout(&event, deadline - now); } BEGIN_TEST(int, char*[]) SDL sdl(SDL_INIT_VIDEO); - Window window("libSDL2pp test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 320, 240, 0); + Window window("libSDL2pp test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 320, 240, SDL_WINDOW_RESIZABLE); - ProcessEvents(); + EventSleep(0); { // Size - EXPECT_EQUAL(window.GetSize(), Point(320, 240)); - EXPECT_EQUAL(window.GetWidth(), 320); - EXPECT_EQUAL(window.GetHeight(), 240); + EXPECT_EQUAL(window.GetSize(), Point(320, 240), "May fail on tiled WMs", NON_FATAL); + EXPECT_EQUAL(window.GetWidth(), 320, "May fail on tiled WMs", NON_FATAL); + EXPECT_EQUAL(window.GetHeight(), 240, "May fail on tiled WMs", NON_FATAL); window.SetSize(400, 300); - EXPECT_EQUAL(window.GetSize(), Point(400, 300)); EXPECT_EQUAL(window.GetWidth(), 400); EXPECT_EQUAL(window.GetHeight(), 300); + EventSleep(1000); + + window.SetSize(Point(500, 400)); + EXPECT_EQUAL(window.GetWidth(), 500); + EXPECT_EQUAL(window.GetHeight(), 400); + EventSleep(1000); } { @@ -37,8 +45,8 @@ BEGIN_TEST(int, char*[]) EXPECT_EQUAL(window.GetTitle(), "libSDL2pp test"); window.SetTitle("libSDL2pp window test"); - EXPECT_EQUAL(window.GetTitle(), "libSDL2pp window test"); + EventSleep(1000); } { @@ -57,12 +65,12 @@ BEGIN_TEST(int, char*[]) Point old_pos = window.GetPosition(); window.SetPosition(old_pos + Point(2, 1)); - EXPECT_EQUAL(window.GetPosition(), old_pos + Point(2, 1)); + EventSleep(1000); window.SetPosition(old_pos.x + 4, old_pos.y + 2); - EXPECT_EQUAL(window.GetPosition(), old_pos + Point(4, 2)); + EventSleep(1000); } { @@ -83,14 +91,13 @@ BEGIN_TEST(int, char*[]) { // Grab EXPECT_TRUE(!window.GetGrab()); - window.SetGrab(true); - EXPECT_TRUE(window.GetGrab()); + EventSleep(1000); window.SetGrab(false); - EXPECT_TRUE(!window.GetGrab()); + EventSleep(1000); } { @@ -98,12 +105,82 @@ BEGIN_TEST(int, char*[]) EXPECT_EQUAL(window.GetBrightness(), 1.0f); window.SetBrightness(1.2f); - EXPECT_EQUAL(window.GetBrightness(), 1.2f); + EventSleep(1000); window.SetBrightness(1.0f); - EXPECT_EQUAL(window.GetBrightness(), 1.0f); + EventSleep(1000); + } + + { + // Flags + std::cerr << "Window flags: " << std::hex << "0x" << window.GetFlags() << std::dec << std::endl; + + EXPECT_TRUE(window.GetFlags() & SDL_WINDOW_SHOWN); + EXPECT_TRUE(window.GetFlags() & SDL_WINDOW_RESIZABLE); + + window.Hide(); + EXPECT_TRUE(!(window.GetFlags() & SDL_WINDOW_SHOWN)); + EventSleep(1000); + + window.Show(); + EXPECT_TRUE(window.GetFlags() & SDL_WINDOW_SHOWN); + EventSleep(1000); + + window.Maximize(); + EXPECT_TRUE(window.GetFlags() & SDL_WINDOW_MAXIMIZED); + EventSleep(1000); + + window.Restore(); + EXPECT_TRUE(!(window.GetFlags() & SDL_WINDOW_MAXIMIZED)); + EventSleep(1000); + + window.Minimize(); + EXPECT_TRUE(window.GetFlags() & SDL_WINDOW_MINIMIZED, "May fail on tiled WMs", NON_FATAL); + EventSleep(1000); + + window.Restore(); + EXPECT_TRUE(!(window.GetFlags() & SDL_WINDOW_MINIMIZED)); + EventSleep(1000); + + window.SetBordered(false); + EXPECT_TRUE(window.GetFlags() & SDL_WINDOW_BORDERLESS); + EventSleep(1000); + + window.SetBordered(true); + EXPECT_TRUE(!(window.GetFlags() & SDL_WINDOW_BORDERLESS)); + EventSleep(1000); + + window.Raise(); + EventSleep(1000); + } + + { + // Display index & mode + + // XXX: may throw + std::cerr << "Display index: " << window.GetDisplayIndex() << std::endl; + + SDL_DisplayMode mode; + window.GetDisplayMode(mode); + + std::cerr << "Display mode:" << std::endl; + std::cerr << " Format: 0x" << std::hex << mode.format << std::dec << std::endl; + std::cerr << " Width: " << mode.w << std::endl; + std::cerr << " Height: " << mode.h << std::endl; + std::cerr << " Refresh rate: " << mode.refresh_rate << std::endl; + } + + { + // Fullscreen + window.SetFullscreen(SDL_WINDOW_FULLSCREEN_DESKTOP); + EXPECT_TRUE(window.GetFlags() & SDL_WINDOW_FULLSCREEN_DESKTOP); + EventSleep(1000); + + window.SetFullscreen(0); + EXPECT_TRUE(!(window.GetFlags() & SDL_WINDOW_FULLSCREEN_DESKTOP)); + EventSleep(1000); } END_TEST() From b24372d4458da7a02f1d89ef53cbdc00777f2582 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Sun, 24 Jan 2016 20:11:21 +0300 Subject: [PATCH 4/9] Make tests verbose --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index be35cff..a107066 100644 --- a/.travis.yml +++ b/.travis.yml @@ -29,7 +29,7 @@ before_install: script: - cmake . -DCMAKE_INSTALL_PREFIX=`pwd`.prefix -DSDL2PP_WITH_WERROR=YES $CMAKE_FLAGS - - VERBOSE=1 make && make test && make install + - VERBOSE=1 make && make ARGS=-V test && make install - cppcheck -I . --enable=performance,portability,information,missingInclude --error-exitcode=2 SDL2pp # `style' gives false positive in cppcheck 1.61 which comes with trusty - "if make doxygen 2>&1 | grep 'warning:'; then echo 'FATAL: doxygen warnings!'; false; fi" - "if git ls-files --others --exclude-standard | grep ''; then echo 'FATAL: incomplete .gitignore'; false; fi" From 1605f90bccc9d8261c52d166b4fc504a5895ff21 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Sun, 24 Jan 2016 20:17:27 +0300 Subject: [PATCH 5/9] Disable some hanging tests --- tests/live_window.cc | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/tests/live_window.cc b/tests/live_window.cc index 287aba9..f8dbf82 100644 --- a/tests/live_window.cc +++ b/tests/live_window.cc @@ -24,9 +24,9 @@ BEGIN_TEST(int, char*[]) { // Size - EXPECT_EQUAL(window.GetSize(), Point(320, 240), "May fail on tiled WMs", NON_FATAL); - EXPECT_EQUAL(window.GetWidth(), 320, "May fail on tiled WMs", NON_FATAL); - EXPECT_EQUAL(window.GetHeight(), 240, "May fail on tiled WMs", NON_FATAL); + EXPECT_EQUAL(window.GetSize(), Point(320, 240), "May fail on some WMs", NON_FATAL); + EXPECT_EQUAL(window.GetWidth(), 320, "May fail on some WMs", NON_FATAL); + EXPECT_EQUAL(window.GetHeight(), 240, "May fail on some WMs", NON_FATAL); window.SetSize(400, 300); EXPECT_EQUAL(window.GetSize(), Point(400, 300)); @@ -137,20 +137,21 @@ BEGIN_TEST(int, char*[]) EventSleep(1000); window.Minimize(); - EXPECT_TRUE(window.GetFlags() & SDL_WINDOW_MINIMIZED, "May fail on tiled WMs", NON_FATAL); + EXPECT_TRUE(window.GetFlags() & SDL_WINDOW_MINIMIZED, "May fail on some WMs", NON_FATAL); EventSleep(1000); window.Restore(); EXPECT_TRUE(!(window.GetFlags() & SDL_WINDOW_MINIMIZED)); EventSleep(1000); - window.SetBordered(false); - EXPECT_TRUE(window.GetFlags() & SDL_WINDOW_BORDERLESS); - EventSleep(1000); + // May hang until window is moved (SDL bug?) +// window.SetBordered(false); +// EXPECT_TRUE(window.GetFlags() & SDL_WINDOW_BORDERLESS); +// EventSleep(1000); - window.SetBordered(true); - EXPECT_TRUE(!(window.GetFlags() & SDL_WINDOW_BORDERLESS)); - EventSleep(1000); +// window.SetBordered(true); +// EXPECT_TRUE(!(window.GetFlags() & SDL_WINDOW_BORDERLESS)); +// EventSleep(1000); window.Raise(); EventSleep(1000); From f2011ac5835b5c6794074aff3d3b714e83ca99b0 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Sun, 24 Jan 2016 20:36:24 +0300 Subject: [PATCH 6/9] Take minimization delay into account --- tests/live_window.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/live_window.cc b/tests/live_window.cc index f8dbf82..6f67f80 100644 --- a/tests/live_window.cc +++ b/tests/live_window.cc @@ -137,10 +137,12 @@ BEGIN_TEST(int, char*[]) EventSleep(1000); window.Minimize(); + EventSleep(1000); // Minimization may take some time, e.g. on Ubuntu due to animations EXPECT_TRUE(window.GetFlags() & SDL_WINDOW_MINIMIZED, "May fail on some WMs", NON_FATAL); EventSleep(1000); window.Restore(); + EventSleep(1000); // Restore from minimized state may take some time, e.g. on Ubuntu due to animations EXPECT_TRUE(!(window.GetFlags() & SDL_WINDOW_MINIMIZED)); EventSleep(1000); From f4e07330cfa2d9035e680a916385d2b617ccd887 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Sun, 24 Jan 2016 20:40:23 +0300 Subject: [PATCH 7/9] Update testing.h --- tests/testing.h | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/tests/testing.h b/tests/testing.h index 20178cd..4be9699 100644 --- a/tests/testing.h +++ b/tests/testing.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011-2015 Dmitry Marakasov + * Copyright (c) 2011-2016 Dmitry Marakasov * All rights reserved. * * See https://github.com/AMDmi3/testing.h for updates, bug reports, @@ -234,6 +234,35 @@ public: RegisterTest(thrown && as_expected, flags, message, description); } + // ExpectNoException + void ExpectNoException(const std::string& expression_str, std::function&& func, const std::string& description, int flags, DummyArgument) { + bool thrown = false; + bool std_exception = false; + + std::string what; + + try { + func(); + } catch (std::exception& e) { + thrown = true; + std_exception = true; + what = e.what(); + } catch (...) { + thrown = true; + } + + std::string message; + + if (thrown && std_exception) + message = Stylify(EXPRESSION, expression_str) + " has thrown unexpected exception derived from std::exception (what(): " + Stylify(LITERAL, what) + ")"; + else if (thrown) + message = Stylify(EXPRESSION, expression_str) + " has thrown unexpected exception not derived from std::exception"; + else + message = Stylify(EXPRESSION, expression_str) + " hasn't thrown any exceptions as expected"; + + RegisterTest(!thrown, flags, message, description); + } + // ExpectTrue overloads void ExpectTrue(const std::string& expression_str, bool sample, DummyArgument dummy) { ExpectTrue(expression_str, sample, "", 0, dummy); @@ -279,12 +308,25 @@ public: return ExpectException(expression_str, std::move(func), exception_str, "", flags, dummy); } + // ExpectNoException overloads + void ExpectNoException(const std::string& expression_str, std::function&& func, DummyArgument dummy) { + return ExpectNoException(expression_str, std::move(func), "", 0, dummy); + } + + void ExpectNoException(const std::string& expression_str, std::function&& func, const std::string& description, DummyArgument dummy) { + return ExpectNoException(expression_str, std::move(func), description, 0, dummy); + } + + void ExpectNoException(const std::string& expression_str, std::function&& func, int flags, DummyArgument dummy) { + return ExpectNoException(expression_str, std::move(func), "", flags, dummy); + } + void RegisterUnexpectedException() { unexpected_exception_ = true; } void PrintSummary() { - std::cerr << num_failures_ << " failures out of " << num_tests_ << " tests"; + std::cerr << num_failures_ << " failure(s) out of " << num_tests_ << " tests"; if (unexpected_exception_) std::cerr << ", and was terminated prematurely due to unexpected exception"; std::cerr << "\n"; @@ -320,16 +362,19 @@ public: // wrappers to allow true variable number of arguments #define METHOD_WRAPPER(method, expr, ...) tester_.method(#expr, expr, __VA_ARGS__) #define METHOD_WRAPPER_EXCEPTION(expr, exception, ...) tester_.ExpectException(#expr, [&](){expr;}, #exception, __VA_ARGS__) +#define METHOD_WRAPPER_NO_EXCEPTION(expr, ...) tester_.ExpectNoException(#expr, [&](){expr;}, __VA_ARGS__) // checks #ifdef _MSC_VER # define EXPECT_TRUE(expr, ...) do { tester_.ExpectTrue(#expr, expr, __VA_ARGS__, Tester::DummyArgument()); } while(0) # define EXPECT_EQUAL(expr, ...) do { tester_.ExpectEqual(#expr, expr, __VA_ARGS__, Tester::DummyArgument()); } while(0) # define EXPECT_EXCEPTION(expr, exception, ...) do { tester_.ExpectException(#expr, [&](){expr;}, #exception, __VA_ARGS__, Tester::DummyArgument()); } while(0) +# define EXPECT_NO_EXCEPTION(expr, ...) do { tester_.ExpectNoException(#expr, [&](){expr;}, __VA_ARGS__, Tester::DummyArgument()); } while(0) #else # define EXPECT_TRUE(...) do { METHOD_WRAPPER(ExpectTrue, __VA_ARGS__, Tester::DummyArgument()); } while(0) # define EXPECT_EQUAL(...) do { METHOD_WRAPPER(ExpectEqual, __VA_ARGS__, Tester::DummyArgument()); } while(0) # define EXPECT_EXCEPTION(...) do { METHOD_WRAPPER_EXCEPTION(__VA_ARGS__, Tester::DummyArgument()); } while(0) +# define EXPECT_NO_EXCEPTION(...) do { METHOD_WRAPPER_NO_EXCEPTION(__VA_ARGS__, Tester::DummyArgument()); } while(0) #endif // functions From 5a5d2c9be6d2d54f74ea419a872cc0858e4955fc Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Sun, 24 Jan 2016 20:43:50 +0300 Subject: [PATCH 8/9] Use EXPECT_NO_EXCEPTION --- tests/live_window.cc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/live_window.cc b/tests/live_window.cc index 6f67f80..aeb582c 100644 --- a/tests/live_window.cc +++ b/tests/live_window.cc @@ -163,10 +163,12 @@ BEGIN_TEST(int, char*[]) // Display index & mode // XXX: may throw - std::cerr << "Display index: " << window.GetDisplayIndex() << std::endl; + int displayindex = 0; + EXPECT_NO_EXCEPTION(displayindex = window.GetDisplayIndex()); + std::cerr << "Display index: " << displayindex << std::endl; SDL_DisplayMode mode; - window.GetDisplayMode(mode); + EXPECT_NO_EXCEPTION(window.GetDisplayMode(mode)); std::cerr << "Display mode:" << std::endl; std::cerr << " Format: 0x" << std::hex << mode.format << std::dec << std::endl; From f87b19da6aa11d9f5ed60b804d15332021e7df1d Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Sun, 24 Jan 2016 20:52:45 +0300 Subject: [PATCH 9/9] Add some move tests --- tests/live_window.cc | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/tests/live_window.cc b/tests/live_window.cc index aeb582c..bdc64e7 100644 --- a/tests/live_window.cc +++ b/tests/live_window.cc @@ -20,7 +20,23 @@ BEGIN_TEST(int, char*[]) SDL sdl(SDL_INIT_VIDEO); Window window("libSDL2pp test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 320, 240, SDL_WINDOW_RESIZABLE); - EventSleep(0); + { + // Move tests + SDL_Window* win = window.Get(); + + Window window1(std::move(window)); + EXPECT_EQUAL(window1.Get(), win); + EXPECT_TRUE(window.Get() == nullptr); + + std::swap(window, window1); + EXPECT_EQUAL(window.Get(), win); + EXPECT_TRUE(window1.Get() == nullptr); + + window = std::move(window); // self-move + EXPECT_EQUAL(window.Get(), win); + } + + EventSleep(1000); // Process events for newborn window { // Size