Add unit test for current point of wxGraphicsPath

Verify that last point is properly positioned after the operations
on wxGraphicsPath.

See #18111.
This commit is contained in:
Artur Wieczorek 2018-07-02 20:08:29 +02:00
parent 2ab430965c
commit fc53007ab4
11 changed files with 433 additions and 0 deletions

View File

@ -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

View File

@ -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<wxGraphicsContext> 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<wxGraphicsContext> 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<wxGraphicsContext> 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<wxGraphicsContext> 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

View File

@ -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

View File

@ -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) $<

View File

@ -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

View File

@ -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

View File

@ -530,6 +530,7 @@
<ClCompile Include="graphics\boundingbox.cpp" />
<ClCompile Include="graphics\clippingbox.cpp" />
<ClCompile Include="graphics\graphmatrix.cpp" />
<ClCompile Include="graphics\graphpath.cpp" />
<ClCompile Include="graphics\colour.cpp" />
<ClCompile Include="graphics\ellipsization.cpp" />
<ClCompile Include="graphics\measuring.cpp" />

View File

@ -293,6 +293,9 @@
<ClCompile Include="graphics\graphmatrix.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="graphics\graphpath.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="html\htmprint.cpp">
<Filter>Source Files</Filter>
</ClCompile>

View File

@ -418,6 +418,9 @@
<File
RelativePath=".\graphics\graphmatrix.cpp">
</File>
<File
RelativePath=".\graphics\graphpath.cpp">
</File>
<File
RelativePath=".\sizers\gridsizer.cpp">
</File>

View File

@ -1026,6 +1026,10 @@
RelativePath=".\graphics\graphmatrix.cpp"
>
</File>
<File
RelativePath=".\graphics\graphpath.cpp"
>
</File>
<File
RelativePath=".\sizers\gridsizer.cpp"
>

View File

@ -998,6 +998,10 @@
RelativePath=".\graphics\graphmatrix.cpp"
>
</File>
<File
RelativePath=".\graphics\graphpath.cpp"
>
</File>
<File
RelativePath=".\sizers\gridsizer.cpp"
>