From 015a9e5be59715f60fca9ead9404e8f2d82233dc Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Tue, 11 Feb 2014 07:39:39 +0400 Subject: [PATCH] Add partial GUI test --- tests/CMakeLists.txt | 1 + tests/gui_rendering.cc | 209 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 210 insertions(+) create mode 100644 tests/gui_rendering.cc diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index e9560d8..e10b22f 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -20,6 +20,7 @@ SET(CLI_TESTS # tests which test graphics functionality and thus requre working # and OpenGL-enabled X11 display SET(GUI_TESTS + gui_rendering ) FOREACH(TEST ${HEADER_TESTS}) diff --git a/tests/gui_rendering.cc b/tests/gui_rendering.cc new file mode 100644 index 0000000..b1eee20 --- /dev/null +++ b/tests/gui_rendering.cc @@ -0,0 +1,209 @@ +#include + +#include +#include + +#include "testing.h" + +using namespace SDL2pp; + +class PixelInspector { +private: + std::vector pixels_; + int width_; + int height_; + int bpp_; + +public: + PixelInspector(int width, int height, int bpp) : pixels_(width * height * bpp, 0), width_(width), height_(height), bpp_(bpp) { + } + + void Retrieve(Renderer& renderer) { + renderer.ReadPixels(Rect(0, 0, width_, height_), SDL_PIXELFORMAT_ARGB8888, pixels_.data(), width_ * bpp_); + } + + bool Test(int x, int y, int r, int g, int b, int a = -1) { + int offset = (x + y * width_) * bpp_; + + if (b >= 0 && pixels_[offset] != b) + return false; + if (g >= 0 && pixels_[offset + 1] != g) + return false; + if (r >= 0 && pixels_[offset + 2] != r) + return false; + if (a >= 0 && pixels_[offset + 3] != a) + return false; + + return true; + } + + bool Test3x3(int x, int y, int mask, int r, int g, int b, int a = -1) { + for (int dy = -1; dy <= 1; dy++) { + for (int dx = -1; dx <= 1; dx++) { + bool maskbit = mask & (1 << ((1 - dx) + (1 - dy) * 4)); + if (Test(x + dx, y + dy, r, g, b, a) != !!maskbit) + return false; + } + } + return true; + } +}; + +BEGIN_TEST() + SDL sdl(SDL_INIT_VIDEO); + Window window("libSDL2pp test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 320, 240, 0); + Renderer renderer(window, -1, SDL_RENDERER_ACCELERATED); + + PixelInspector pixels(320, 240, 4); + + { + // clear + renderer.SetDrawColor(1, 2, 3); + renderer.Clear(); + pixels.Retrieve(renderer); + + EXPECT_TRUE(pixels.Test(0, 0, 1, 2, 3)); + } + + { + // draw points + renderer.SetDrawColor(0,0,0); + renderer.Clear(); + + renderer.SetDrawColor(255, 128, 0); + renderer.DrawPoint(10, 10); + + renderer.SetDrawColor(0, 255, 128); + renderer.DrawPoint(20, 20); + + renderer.SetDrawColor(128, 0, 255); + Point points[] = { {30, 30} }; + renderer.DrawPoints(points, 1); + pixels.Retrieve(renderer); + + EXPECT_TRUE(pixels.Test3x3(10, 10, 0x020, 255, 128, 0)); + EXPECT_TRUE(pixels.Test3x3(20, 20, 0x020, 0, 255, 128)); + EXPECT_TRUE(pixels.Test3x3(30, 30, 0x020, 128, 0, 255)); + } + + { + // draw lines + renderer.SetDrawColor(0, 0, 0); + renderer.Clear(); + + renderer.SetDrawColor(255, 128, 0); + renderer.DrawLine(10, 10, 10, 50); + + renderer.SetDrawColor(0, 255, 128); + renderer.DrawLine(Point(20, 10), Point(20, 50)); + + renderer.SetDrawColor(128, 0, 255); + Point points[] = { {30, 10}, {30, 50} }; + renderer.DrawLines(points, 2); + + pixels.Retrieve(renderer); + + EXPECT_TRUE(pixels.Test3x3(10, 20, 0x222, 255, 128, 0)); + EXPECT_TRUE(pixels.Test3x3(20, 20, 0x222, 0, 255, 128)); + EXPECT_TRUE(pixels.Test3x3(30, 20, 0x222, 128, 0, 255)); + } + + { + // draw rects + renderer.SetDrawColor(0, 0, 0); + renderer.Clear(); + + renderer.SetDrawColor(255, 128, 0); + renderer.DrawRect(10, 10, 19, 19); + + renderer.SetDrawColor(0, 255, 128); + renderer.DrawRect(Point(30, 10), Point(39, 19)); + + renderer.SetDrawColor(128, 0, 255); + renderer.DrawRect(Rect(10, 30, 10, 10)); + + renderer.SetDrawColor(128, 192, 255); + Rect rects[] = { {30, 30, 10, 10 } }; + renderer.DrawRects(rects, 1); + + pixels.Retrieve(renderer); + + EXPECT_TRUE(pixels.Test3x3(10, 10, 0x032, 255, 128, 0)); + EXPECT_TRUE(pixels.Test3x3(19, 10, 0x062, 255, 128, 0)); + EXPECT_TRUE(pixels.Test3x3(10, 19, 0x230, 255, 128, 0)); + EXPECT_TRUE(pixels.Test3x3(19, 19, 0x260, 255, 128, 0)); + + EXPECT_TRUE(pixels.Test3x3(30, 10, 0x032, 0, 255, 128)); + EXPECT_TRUE(pixels.Test3x3(39, 10, 0x062, 0, 255, 128)); + EXPECT_TRUE(pixels.Test3x3(30, 19, 0x230, 0, 255, 128)); + EXPECT_TRUE(pixels.Test3x3(39, 19, 0x260, 0, 255, 128)); + + EXPECT_TRUE(pixels.Test3x3(10, 30, 0x032, 128, 0, 255)); + EXPECT_TRUE(pixels.Test3x3(19, 30, 0x062, 128, 0, 255)); + EXPECT_TRUE(pixels.Test3x3(10, 39, 0x230, 128, 0, 255)); + EXPECT_TRUE(pixels.Test3x3(19, 39, 0x260, 128, 0, 255)); + + EXPECT_TRUE(pixels.Test3x3(30, 30, 0x032, 128, 192, 255)); + EXPECT_TRUE(pixels.Test3x3(39, 30, 0x062, 128, 192, 255)); + EXPECT_TRUE(pixels.Test3x3(30, 39, 0x230, 128, 192, 255)); + EXPECT_TRUE(pixels.Test3x3(39, 39, 0x260, 128, 192, 255)); + } + + { + // fill rects + renderer.SetDrawColor(0, 0, 0); + renderer.Clear(); + + renderer.SetDrawColor(255, 128, 0); + renderer.FillRect(10, 10, 19, 19); + + renderer.SetDrawColor(0, 255, 128); + renderer.FillRect(Point(30, 10), Point(39, 19)); + + renderer.SetDrawColor(128, 0, 255); + renderer.FillRect(Rect(10, 30, 10, 10)); + + renderer.SetDrawColor(128, 192, 255); + Rect rects[] = { {30, 30, 10, 10 } }; + renderer.FillRects(rects, 1); + + pixels.Retrieve(renderer); + + EXPECT_TRUE(pixels.Test3x3(10, 10, 0x033, 255, 128, 0)); + EXPECT_TRUE(pixels.Test3x3(19, 10, 0x066, 255, 128, 0)); + EXPECT_TRUE(pixels.Test3x3(10, 19, 0x330, 255, 128, 0)); + EXPECT_TRUE(pixels.Test3x3(19, 19, 0x660, 255, 128, 0)); + + EXPECT_TRUE(pixels.Test3x3(30, 10, 0x033, 0, 255, 128)); + EXPECT_TRUE(pixels.Test3x3(39, 10, 0x066, 0, 255, 128)); + EXPECT_TRUE(pixels.Test3x3(30, 19, 0x330, 0, 255, 128)); + EXPECT_TRUE(pixels.Test3x3(39, 19, 0x660, 0, 255, 128)); + + EXPECT_TRUE(pixels.Test3x3(10, 30, 0x033, 128, 0, 255)); + EXPECT_TRUE(pixels.Test3x3(19, 30, 0x066, 128, 0, 255)); + EXPECT_TRUE(pixels.Test3x3(10, 39, 0x330, 128, 0, 255)); + EXPECT_TRUE(pixels.Test3x3(19, 39, 0x660, 128, 0, 255)); + + EXPECT_TRUE(pixels.Test3x3(30, 30, 0x033, 128, 192, 255)); + EXPECT_TRUE(pixels.Test3x3(39, 30, 0x066, 128, 192, 255)); + EXPECT_TRUE(pixels.Test3x3(30, 39, 0x330, 128, 192, 255)); + EXPECT_TRUE(pixels.Test3x3(39, 39, 0x660, 128, 192, 255)); + } + + { + // blend + renderer.SetDrawColor(0, 0, 0); + renderer.Clear(); + + renderer.SetDrawBlendMode(SDL_BLENDMODE_BLEND); + renderer.SetDrawColor(255, 255, 255, 127); + renderer.FillRect(10, 10, 19, 19); + + pixels.Retrieve(renderer); + + EXPECT_TRUE(pixels.Test3x3(10, 10, 0x033, 127, 127, 127)); + EXPECT_TRUE(pixels.Test3x3(19, 10, 0x066, 127, 127, 127)); + EXPECT_TRUE(pixels.Test3x3(10, 19, 0x330, 127, 127, 127)); + EXPECT_TRUE(pixels.Test3x3(19, 19, 0x660, 127, 127, 127)); + } +END_TEST()