From fc53007ab4996d0fc657b5c5f1a0b944b0caa641 Mon Sep 17 00:00:00 2001 From: Artur Wieczorek Date: Mon, 2 Jul 2018 20:08:29 +0200 Subject: [PATCH] Add unit test for current point of wxGraphicsPath Verify that last point is properly positioned after the operations on wxGraphicsPath. See #18111. --- tests/Makefile.in | 4 + tests/graphics/graphpath.cpp | 401 +++++++++++++++++++++++++++++++++ tests/makefile.bcc | 4 + tests/makefile.gcc | 4 + tests/makefile.vc | 4 + tests/test.bkl | 1 + tests/test_gui.vcxproj | 1 + tests/test_gui.vcxproj.filters | 3 + tests/test_vc7_test_gui.vcproj | 3 + tests/test_vc8_test_gui.vcproj | 4 + tests/test_vc9_test_gui.vcproj | 4 + 11 files changed, 433 insertions(+) create mode 100644 tests/graphics/graphpath.cpp diff --git a/tests/Makefile.in b/tests/Makefile.in index bc37e442f9..951b321f93 100644 --- a/tests/Makefile.in +++ b/tests/Makefile.in @@ -183,6 +183,7 @@ TEST_GUI_OBJECTS = \ test_gui_boundingbox.o \ test_gui_clippingbox.o \ test_gui_graphmatrix.o \ + test_gui_graphpath.o \ test_gui_config.o \ test_gui_bitmapcomboboxtest.o \ test_gui_bitmaptogglebuttontest.o \ @@ -818,6 +819,9 @@ test_gui_clippingbox.o: $(srcdir)/graphics/clippingbox.cpp $(TEST_GUI_ODEP) test_gui_graphmatrix.o: $(srcdir)/graphics/graphmatrix.cpp $(TEST_GUI_ODEP) $(CXXC) -c -o $@ $(TEST_GUI_CXXFLAGS) $(srcdir)/graphics/graphmatrix.cpp +test_gui_graphpath.o: $(srcdir)/graphics/graphpath.cpp $(TEST_GUI_ODEP) + $(CXXC) -c -o $@ $(TEST_GUI_CXXFLAGS) $(srcdir)/graphics/graphpath.cpp + test_gui_config.o: $(srcdir)/config/config.cpp $(TEST_GUI_ODEP) $(CXXC) -c -o $@ $(TEST_GUI_CXXFLAGS) $(srcdir)/config/config.cpp diff --git a/tests/graphics/graphpath.cpp b/tests/graphics/graphpath.cpp new file mode 100644 index 0000000000..7cd9a2a999 --- /dev/null +++ b/tests/graphics/graphpath.cpp @@ -0,0 +1,401 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: tests/graphics/grsppsth.cpp +// Purpose: graphics path unit tests +// Author: Artur Wieczorek +// Created: 2018-07-01 +// Copyright: (c) 2018 wxWidgets development team +/////////////////////////////////////////////////////////////////////////////// + +// ---------------------------------------------------------------------------- +// headers +// ---------------------------------------------------------------------------- + +#include "testprec.h" + +#ifdef __BORLANDC__ + #pragma hdrstop +#endif + +#if wxUSE_GRAPHICS_CONTEXT + +#include "wx/bitmap.h" +#include "wx/dcmemory.h" +#include "wx/dcgraph.h" +#include "wx/scopedptr.h" + +static void DoAllTests(wxGraphicsContext* gc); + +// For MSW we have individual test cases for each graphics renderer +// so we don't need to execute tests with default renderer. +#ifndef __WXMSW__ + +TEST_CASE("GraphicsPathTestCase", "[path]") +{ + wxBitmap bmp(500, 500); + wxMemoryDC mdc(bmp); + wxScopedPtr gc(wxGraphicsRenderer::GetDefaultRenderer()->CreateContext(mdc)); + REQUIRE(gc); + DoAllTests(gc.get()); +} + +#else + +#if wxUSE_GRAPHICS_GDIPLUS +TEST_CASE("GraphicsPathTestCaseGDIPlus", "[path][gdi+]") +{ + wxBitmap bmp(500, 500); + wxMemoryDC mdc(bmp); + wxScopedPtr gc(wxGraphicsRenderer::GetGDIPlusRenderer()->CreateContext(mdc)); + REQUIRE(gc); + DoAllTests(gc.get()); +} +#endif // wxUSE_GRAPHICS_GDIPLUS + +#if wxUSE_GRAPHICS_DIRECT2D +TEST_CASE("GraphicsPathTestCaseDirect2D", "[path][d2d]") +{ + wxBitmap bmp(500, 500); + wxMemoryDC mdc(bmp); + wxScopedPtr gc(wxGraphicsRenderer::GetDirect2DRenderer()->CreateContext(mdc)); + REQUIRE(gc); + DoAllTests(gc.get()); +} +#endif // wxUSE_GRAPHICS_DIRECT2D + +#endif // __WXMSW__ / !__WXMSW__ + +#if wxUSE_CAIRO +TEST_CASE("GraphicsPathTestCaseCairo", "[path][cairo]") +{ + wxBitmap bmp(500, 500); + wxMemoryDC mdc(bmp); + wxScopedPtr gc(wxGraphicsRenderer::GetCairoRenderer()->CreateContext(mdc)); + REQUIRE(gc); + DoAllTests(gc.get()); +} +#endif // wxUSE_CAIRO + +#define WX_CHECK_POINT(p1, p2, tolerance) \ + CHECK(fabs(p1.m_x - p2.m_x) <= tolerance); \ + CHECK(fabs(p1.m_y - p2.m_y) <= tolerance) + +static void TestCurrentPoint(wxGraphicsContext* gc) +{ + // No current point + { + wxGraphicsPath path = gc->CreatePath(); + // Should return (0, 0) if current point is not yet set. + wxPoint2DDouble cp = path.GetCurrentPoint(); + WX_CHECK_POINT(cp, wxPoint2DDouble(0, 0), 0); + } + // MoveToPoint + { + wxGraphicsPath path = gc->CreatePath(); + wxPoint2DDouble pt(27, 35); + path.MoveToPoint(pt); + wxPoint2DDouble cp = path.GetCurrentPoint(); + WX_CHECK_POINT(cp, pt, 1E-3); + } + // AddLineToPoint - no current point + { + wxGraphicsPath path = gc->CreatePath(); + wxPoint2DDouble pt(27, 35); + path.AddLineToPoint(pt); + wxPoint2DDouble cp = path.GetCurrentPoint(); + WX_CHECK_POINT(cp, pt, 1E-3); + } + // AddLineToPoint + { + wxGraphicsPath path = gc->CreatePath(); + path.MoveToPoint(10, 18); + wxPoint2DDouble pt(37, 45); + path.AddLineToPoint(pt); + wxPoint2DDouble cp = path.GetCurrentPoint(); + WX_CHECK_POINT(cp, pt, 1E-3); + } + // AddArc - no current point + { + wxGraphicsPath path = gc->CreatePath(); + const wxDouble x = 100; + const wxDouble y = 150; + const wxDouble r = 40; + path.AddArc(x, y, r, 0, M_PI/2, true); + wxPoint2DDouble cp = path.GetCurrentPoint(); + WX_CHECK_POINT(cp, wxPoint2DDouble(x, y + r), 1E-3); + } + // AddArc + { + wxGraphicsPath path = gc->CreatePath(); + path.MoveToPoint(20, 38); + const wxDouble x = 200; + const wxDouble y = 50; + const wxDouble r = 40; + path.AddArc(x, y, r, 0, M_PI / 2, true); + wxPoint2DDouble cp = path.GetCurrentPoint(); + WX_CHECK_POINT(cp, wxPoint2DDouble(x, y + r), 1E-3); + } + // AddArcToPoint - no current point + { + wxGraphicsPath path = gc->CreatePath(); + const wxDouble x1 = 80; + const wxDouble y1 = 80; + const wxDouble x2 = -30; + const wxDouble y2 = y1; + const wxDouble r = 20; + wxASSERT(x1 == y1 && y2 == y1); // alpha = 45 deg + double d = r / tan(45 / 180.0 * M_PI / 2.0); + path.AddArcToPoint(x1, y1, x2, y2, r); + wxPoint2DDouble cp = path.GetCurrentPoint(); + WX_CHECK_POINT(cp, wxPoint2DDouble(x1 - d, y2), 1E-3); + } + // AddArcToPoint + { + wxGraphicsPath path = gc->CreatePath(); + const wxDouble x0 = 20; + const wxDouble y0 = 20; + path.MoveToPoint(x0, y0); + const wxDouble x1 = 80; + const wxDouble y1 = 80; + const wxDouble x2 = 140; + const wxDouble y2 = y1; + const wxDouble r = 20; + wxASSERT(x0 == y0 && x1 == y1 && y2 == y1); // alpha = 135 deg + double d = r / tan(135 / 180.0 * M_PI / 2.0); + path.AddArcToPoint(x1, y1, x2, y2, r); + wxPoint2DDouble cp = path.GetCurrentPoint(); + WX_CHECK_POINT(cp, wxPoint2DDouble(x1 + d, y2), 1E-3); + } + // AddCurveToPoint - no current point + { + wxGraphicsPath path = gc->CreatePath(); + const wxDouble x1 = 102; + const wxDouble y1 = 230; + const wxDouble x2 = 153; + const wxDouble y2 = 25; + const wxDouble x3 = 230; + const wxDouble y3 = 128; + path.AddCurveToPoint(x1, y1, x2, y2, x3, y3); + wxPoint2DDouble cp = path.GetCurrentPoint(); + WX_CHECK_POINT(cp, wxPoint2DDouble(x3, y3), 1E-3); + } + // AddCurveToPoint + { + wxGraphicsPath path = gc->CreatePath(); + const wxDouble x0 = 25; + const wxDouble y0 = 128; + path.MoveToPoint(x0, y0); + const wxDouble x1 = 102; + const wxDouble y1 = 230; + const wxDouble x2 = 153; + const wxDouble y2 = 25; + const wxDouble x3 = 230; + const wxDouble y3 = 128; + path.AddCurveToPoint(x1, y1, x2, y2, x3, y3); + wxPoint2DDouble cp = path.GetCurrentPoint(); + WX_CHECK_POINT(cp, wxPoint2DDouble(x3, y3), 1E-3); + } + // AddQuadCurveToPoint - no current point + { + wxGraphicsPath path = gc->CreatePath(); + const wxDouble x1 = 200; + const wxDouble y1 = 200; + const wxDouble x2 = 300; + const wxDouble y2 = 100; + path.AddQuadCurveToPoint(x1, y1, x2, y2); + wxPoint2DDouble cp = path.GetCurrentPoint(); + WX_CHECK_POINT(cp, wxPoint2DDouble(x2, y2), 1E-3); + } + // AddQuadCurveToPoint + { + wxGraphicsPath path = gc->CreatePath(); + const wxDouble x0 = 20; + const wxDouble y0 = 100; + path.MoveToPoint(x0, y0); + const wxDouble x1 = 200; + const wxDouble y1 = 200; + const wxDouble x2 = 300; + const wxDouble y2 = 100; + path.AddQuadCurveToPoint(x1, y1, x2, y2); + wxPoint2DDouble cp = path.GetCurrentPoint(); + WX_CHECK_POINT(cp, wxPoint2DDouble(x2, y2), 1E-3); + } + // AddCircle - no current point + { + wxGraphicsPath path = gc->CreatePath(); + const wxDouble x = 100; + const wxDouble y = 150; + const wxDouble r = 30; + path.AddCircle(x, y, r); + wxPoint2DDouble cp = path.GetCurrentPoint(); + WX_CHECK_POINT(cp, wxPoint2DDouble(x + r, y), 1E-3); + } + // AddCircle + { + wxGraphicsPath path = gc->CreatePath(); + path.MoveToPoint(50, 80); + const wxDouble x = 100; + const wxDouble y = 140; + const wxDouble r = 40; + path.AddCircle(x, y, r); + wxPoint2DDouble cp = path.GetCurrentPoint(); + WX_CHECK_POINT(cp, wxPoint2DDouble(x + r, y), 1E-3); + } + // AddEllipse - no current point + { + wxGraphicsPath path = gc->CreatePath(); + const wxDouble x = 100; + const wxDouble y = 150; + const wxDouble w = 40; + const wxDouble h = 20; + path.AddEllipse(x, y, w, h); + wxPoint2DDouble cp = path.GetCurrentPoint(); + WX_CHECK_POINT(cp, wxPoint2DDouble(x + w, y + h / 2), 1E-3); + } + // AddEllipse + { + wxGraphicsPath path = gc->CreatePath(); + path.MoveToPoint(50, 60); + const wxDouble x = 100; + const wxDouble y = 150; + const wxDouble w = 40; + const wxDouble h = 20; + path.AddEllipse(x, y, w, h); + wxPoint2DDouble cp = path.GetCurrentPoint(); + WX_CHECK_POINT(cp, wxPoint2DDouble(x + w, y + h / 2), 1E-3); + } + // AddRectangle - no current point + { + wxGraphicsPath path = gc->CreatePath(); + const wxDouble x = 100; + const wxDouble y = 150; + path.AddRectangle(x, y, 40, 20); + wxPoint2DDouble cp = path.GetCurrentPoint(); + WX_CHECK_POINT(cp, wxPoint2DDouble(x, y), 1E-3); + } + // AddRectangle + { + wxGraphicsPath path = gc->CreatePath(); + path.MoveToPoint(50, 60); + const wxDouble x = 100; + const wxDouble y = 150; + path.AddRectangle(x, y, 50, 30); + wxPoint2DDouble cp = path.GetCurrentPoint(); + WX_CHECK_POINT(cp, wxPoint2DDouble(x, y), 1E-3); + } + // AddRoundedRectangle - no current point + { + wxGraphicsPath path = gc->CreatePath(); + const wxDouble x = 100; + const wxDouble y = 150; + const wxDouble w = 40; + const wxDouble h = 20; + path.AddRoundedRectangle(x, y, w, h, 5); + wxPoint2DDouble cp = path.GetCurrentPoint(); + WX_CHECK_POINT(cp, wxPoint2DDouble(x + w, y + h / 2), 1E-3); + } + // AddRoundedRectangle - no current point, radius = 0 + { + wxGraphicsPath path = gc->CreatePath(); + const wxDouble x = 100; + const wxDouble y = 150; + path.AddRoundedRectangle(x, y, 40, 20, 0); // Should behave like AddRectangle + wxPoint2DDouble cp = path.GetCurrentPoint(); + WX_CHECK_POINT(cp, wxPoint2DDouble(x, y), 1E-3); + } + // AddRoundedRectangle + { + wxGraphicsPath path = gc->CreatePath(); + path.MoveToPoint(50, 60); + const wxDouble x = 100; + const wxDouble y = 150; + const wxDouble w = 40; + const wxDouble h = 20; + path.AddRoundedRectangle(x, y, w, h, 5); + wxPoint2DDouble cp = path.GetCurrentPoint(); + WX_CHECK_POINT(cp, wxPoint2DDouble(x + w, y + h / 2), 1E-3); + } + // AddRoundedRectangle - radius = 0 + { + wxGraphicsPath path = gc->CreatePath(); + const wxDouble x0 = 50; + const wxDouble y0 = 60; + path.MoveToPoint(x0, y0); + const wxDouble x = 100; + const wxDouble y = 150; + const wxDouble w = 40; + const wxDouble h = 20; + path.AddRoundedRectangle(x, y, w, h, 0); // Should behave like AddRectangle + wxPoint2DDouble cp = path.GetCurrentPoint(); + WX_CHECK_POINT(cp, wxPoint2DDouble(x, y), 1E-3); + } + // CloseSubpath - no current point + { + wxGraphicsPath path = gc->CreatePath(); + const wxDouble x0 = 50; + const wxDouble y0 = 80; + path.AddLineToPoint(x0, y0); + path.AddArcToPoint(100, 160, 50, 200, 30); + path.CloseSubpath(); + wxPoint2DDouble cp = path.GetCurrentPoint(); + WX_CHECK_POINT(cp, wxPoint2DDouble(x0, y0), 1E-3); + } + // CloseSubpath + { + wxGraphicsPath path = gc->CreatePath(); + const wxDouble x0 = 10; + const wxDouble y0 = 20; + path.MoveToPoint(x0, y0); + path.AddLineToPoint(50, 80); + path.AddArcToPoint(100, 160, 50, 200, 30); + path.CloseSubpath(); + wxPoint2DDouble cp = path.GetCurrentPoint(); + WX_CHECK_POINT(cp, wxPoint2DDouble(x0, y0), 1E-3); + } + // AddPath - no current point + { + // Path to be added + wxGraphicsPath path2 = gc->CreatePath(); + path2.AddArcToPoint(100, 160, 50, 200, 30); + path2.AddLineToPoint(50, 80); + path2.CloseSubpath(); + wxPoint2DDouble cp2 = path2.GetCurrentPoint(); + // Main path + wxGraphicsPath path = gc->CreatePath(); + path.AddLineToPoint(50, 80); + const wxDouble x = 100; + const wxDouble y = 140; + path.AddRectangle(x, y, 50, 200); + path.AddPath(path2); + wxPoint2DDouble cp = path.GetCurrentPoint(); + WX_CHECK_POINT(cp, cp2, 1E-3); + } + // AddPath + { + // Path to be added + wxGraphicsPath path2 = gc->CreatePath(); + path2.AddArcToPoint(100, 160, 50, 200, 30); + path2.AddLineToPoint(50, 80); + path2.CloseSubpath(); + wxPoint2DDouble cp2 = path2.GetCurrentPoint(); + // Main path + wxGraphicsPath path = gc->CreatePath(); + path.MoveToPoint(15, 35); + path.AddLineToPoint(50, 80); + const wxDouble x = 100; + const wxDouble y = 140; + const wxDouble r = 20; + path.AddCircle(x, y, r); + wxPoint2DDouble cp0 = path.GetCurrentPoint(); + path.AddPath(path2); + wxPoint2DDouble cp = path.GetCurrentPoint(); + WX_CHECK_POINT(cp, cp2, 1E-3); + } +} + +static void DoAllTests(wxGraphicsContext* gc) +{ + gc->DisableOffset(); + TestCurrentPoint(gc); +} + +#endif // wxUSE_GRAPHICS_CONTEXT diff --git a/tests/makefile.bcc b/tests/makefile.bcc index 69ff216d07..7e7fa64007 100644 --- a/tests/makefile.bcc +++ b/tests/makefile.bcc @@ -169,6 +169,7 @@ TEST_GUI_OBJECTS = \ $(OBJS)\test_gui_boundingbox.obj \ $(OBJS)\test_gui_clippingbox.obj \ $(OBJS)\test_gui_graphmatrix.obj \ + $(OBJS)\test_gui_graphpath.obj \ $(OBJS)\test_gui_config.obj \ $(OBJS)\test_gui_bitmapcomboboxtest.obj \ $(OBJS)\test_gui_bitmaptogglebuttontest.obj \ @@ -871,6 +872,9 @@ $(OBJS)\test_gui_clippingbox.obj: .\graphics\clippingbox.cpp $(OBJS)\test_gui_graphmatrix.obj: .\graphics\graphmatrix.cpp $(CXX) -q -c -P -o$@ $(TEST_GUI_CXXFLAGS) .\graphics\graphmatrix.cpp +$(OBJS)\test_gui_graphpath.obj: .\graphics\graphpath.cpp + $(CXX) -q -c -P -o$@ $(TEST_GUI_CXXFLAGS) .\graphics\graphpath.cpp + $(OBJS)\test_gui_config.obj: .\config\config.cpp $(CXX) -q -c -P -o$@ $(TEST_GUI_CXXFLAGS) .\config\config.cpp diff --git a/tests/makefile.gcc b/tests/makefile.gcc index 9855000899..b5a1f1843c 100644 --- a/tests/makefile.gcc +++ b/tests/makefile.gcc @@ -164,6 +164,7 @@ TEST_GUI_OBJECTS = \ $(OBJS)\test_gui_boundingbox.o \ $(OBJS)\test_gui_clippingbox.o \ $(OBJS)\test_gui_graphmatrix.o \ + $(OBJS)\test_gui_graphpath.o \ $(OBJS)\test_gui_config.o \ $(OBJS)\test_gui_bitmapcomboboxtest.o \ $(OBJS)\test_gui_bitmaptogglebuttontest.o \ @@ -848,6 +849,9 @@ $(OBJS)\test_gui_clippingbox.o: ./graphics/clippingbox.cpp $(OBJS)\test_gui_graphmatrix.o: ./graphics/graphmatrix.cpp $(CXX) -c -o $@ $(TEST_GUI_CXXFLAGS) $(CPPDEPS) $< +$(OBJS)\test_gui_graphpath.o: ./graphics/graphpath.cpp + $(CXX) -c -o $@ $(TEST_GUI_CXXFLAGS) $(CPPDEPS) $< + $(OBJS)\test_gui_config.o: ./config/config.cpp $(CXX) -c -o $@ $(TEST_GUI_CXXFLAGS) $(CPPDEPS) $< diff --git a/tests/makefile.vc b/tests/makefile.vc index 60ecf9ff0c..abbc0c4998 100644 --- a/tests/makefile.vc +++ b/tests/makefile.vc @@ -175,6 +175,7 @@ TEST_GUI_OBJECTS = \ $(OBJS)\test_gui_boundingbox.obj \ $(OBJS)\test_gui_clippingbox.obj \ $(OBJS)\test_gui_graphmatrix.obj \ + $(OBJS)\test_gui_graphpath.obj \ $(OBJS)\test_gui_config.obj \ $(OBJS)\test_gui_bitmapcomboboxtest.obj \ $(OBJS)\test_gui_bitmaptogglebuttontest.obj \ @@ -1050,6 +1051,9 @@ $(OBJS)\test_gui_clippingbox.obj: .\graphics\clippingbox.cpp $(OBJS)\test_gui_graphmatrix.obj: .\graphics\graphmatrix.cpp $(CXX) /c /nologo /TP /Fo$@ $(TEST_GUI_CXXFLAGS) .\graphics\graphmatrix.cpp +$(OBJS)\test_gui_graphpath.obj: .\graphics\graphpath.cpp + $(CXX) /c /nologo /TP /Fo$@ $(TEST_GUI_CXXFLAGS) .\graphics\graphpath.cpp + $(OBJS)\test_gui_config.obj: .\config\config.cpp $(CXX) /c /nologo /TP /Fo$@ $(TEST_GUI_CXXFLAGS) .\config\config.cpp diff --git a/tests/test.bkl b/tests/test.bkl index 4db76b86cb..2df7aef927 100644 --- a/tests/test.bkl +++ b/tests/test.bkl @@ -182,6 +182,7 @@ graphics/boundingbox.cpp graphics/clippingbox.cpp graphics/graphmatrix.cpp + graphics/graphpath.cpp config/config.cpp controls/bitmapcomboboxtest.cpp controls/bitmaptogglebuttontest.cpp diff --git a/tests/test_gui.vcxproj b/tests/test_gui.vcxproj index d7710c370f..cab7296bce 100644 --- a/tests/test_gui.vcxproj +++ b/tests/test_gui.vcxproj @@ -530,6 +530,7 @@ + diff --git a/tests/test_gui.vcxproj.filters b/tests/test_gui.vcxproj.filters index bd3fbfdc4d..a8a252cac3 100644 --- a/tests/test_gui.vcxproj.filters +++ b/tests/test_gui.vcxproj.filters @@ -293,6 +293,9 @@ Source Files + + Source Files + Source Files diff --git a/tests/test_vc7_test_gui.vcproj b/tests/test_vc7_test_gui.vcproj index eade1cba94..44e695495c 100644 --- a/tests/test_vc7_test_gui.vcproj +++ b/tests/test_vc7_test_gui.vcproj @@ -418,6 +418,9 @@ + + diff --git a/tests/test_vc8_test_gui.vcproj b/tests/test_vc8_test_gui.vcproj index 8ab1a7002d..170871ff8d 100644 --- a/tests/test_vc8_test_gui.vcproj +++ b/tests/test_vc8_test_gui.vcproj @@ -1026,6 +1026,10 @@ RelativePath=".\graphics\graphmatrix.cpp" > + + diff --git a/tests/test_vc9_test_gui.vcproj b/tests/test_vc9_test_gui.vcproj index 6852c292ab..c4cfee6991 100644 --- a/tests/test_vc9_test_gui.vcproj +++ b/tests/test_vc9_test_gui.vcproj @@ -998,6 +998,10 @@ RelativePath=".\graphics\graphmatrix.cpp" > + +