From fb9cbc087958a39efaf6acbd917d0d42eb28cf4c Mon Sep 17 00:00:00 2001 From: Artur Wieczorek Date: Sun, 18 Sep 2016 23:09:44 +0200 Subject: [PATCH] Add test suite to check wxGraphicsMatrix operations Implemented tests of some basic operations like Invert() and Concat(). See #17670. --- tests/Makefile.in | 4 + tests/graphics/graphmatrix.cpp | 350 ++++++++++++++++++++ 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 | 589 +++++++++++++++++---------------- tests/test_vc7_test_gui.vcproj | 3 + tests/test_vc8_test_gui.vcproj | 4 + tests/test_vc9_test_gui.vcproj | 4 + 11 files changed, 675 insertions(+), 293 deletions(-) create mode 100644 tests/graphics/graphmatrix.cpp diff --git a/tests/Makefile.in b/tests/Makefile.in index 668bb925b3..2f11c9fd7b 100644 --- a/tests/Makefile.in +++ b/tests/Makefile.in @@ -183,6 +183,7 @@ TEST_GUI_OBJECTS = \ test_gui_affinematrix.o \ test_gui_boundingbox.o \ test_gui_clippingbox.o \ + test_gui_graphmatrix.o \ test_gui_config.o \ test_gui_bitmapcomboboxtest.o \ test_gui_bitmaptogglebuttontest.o \ @@ -824,6 +825,9 @@ test_gui_boundingbox.o: $(srcdir)/graphics/boundingbox.cpp $(TEST_GUI_ODEP) test_gui_clippingbox.o: $(srcdir)/graphics/clippingbox.cpp $(TEST_GUI_ODEP) $(CXXC) -c -o $@ $(TEST_GUI_CXXFLAGS) $(srcdir)/graphics/clippingbox.cpp +test_gui_graphmatrix.o: $(srcdir)/graphics/graphmatrix.cpp $(TEST_GUI_ODEP) + $(CXXC) -c -o $@ $(TEST_GUI_CXXFLAGS) $(srcdir)/graphics/graphmatrix.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/graphmatrix.cpp b/tests/graphics/graphmatrix.cpp new file mode 100644 index 0000000000..c0e64ef8fe --- /dev/null +++ b/tests/graphics/graphmatrix.cpp @@ -0,0 +1,350 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: tests/graphics/graphmatrix.cpp +// Purpose: Graphics matrix unit test +// Author: Artur Wieczorek +// Created: 2016-09-18 +// Copyright: (c) 2016 wxWidgets development team +/////////////////////////////////////////////////////////////////////////////// + +// ---------------------------------------------------------------------------- +// headers +// ---------------------------------------------------------------------------- + +#include "testprec.h" + +#ifdef __BORLANDC__ + #pragma hdrstop +#endif + +#if wxUSE_GRAPHICS_CONTEXT + +#include "wx/graphics.h" +#include "wx/dcmemory.h" + +// ---------------------------------------------------------------------------- +// Graphics matrix test classes +// ---------------------------------------------------------------------------- + +class GraphicsMatrixTestCaseBase : public CppUnit::TestCase +{ +public: + GraphicsMatrixTestCaseBase() + { + m_bmp.Create(100, 100); + m_dc.SelectObject(m_bmp); + m_rend = NULL; + m_ctx = NULL; + } + + ~GraphicsMatrixTestCaseBase() + { + m_dc.SelectObject(wxNullBitmap); + m_bmp = wxNullBitmap; + } + + virtual void setUp() wxOVERRIDE + { + wxASSERT( m_rend ); + m_ctx = m_rend->CreateContext(m_dc); + } + + virtual void tearDown() wxOVERRIDE + { + delete m_ctx; + m_ctx = NULL; + } + +protected: + void InitState(); + void InvertMatrix(); + void Concat1(); + void Concat2(); + void Concat3(); + + wxGraphicsRenderer* m_rend; + +private: + void CheckMatrix(const wxGraphicsMatrix& m, + double a, double b, double c, double d, + double tx, double ty); + + wxBitmap m_bmp; + wxMemoryDC m_dc; + wxGraphicsContext* m_ctx; + + wxDECLARE_NO_COPY_CLASS(GraphicsMatrixTestCaseBase); +}; + +// ======================== +// wxGraphicsContext tests +// ======================== + +#ifdef __WXMSW__ +// GDI+ and Direct2D are available only under MSW. + +#if wxUSE_GRAPHICS_GDIPLUS + +class GraphicsMatrixTestCaseGDIPlus : public GraphicsMatrixTestCaseBase +{ +public: + GraphicsMatrixTestCaseGDIPlus() + { + m_rend = wxGraphicsRenderer::GetGDIPlusRenderer(); + } + + virtual ~GraphicsMatrixTestCaseGDIPlus() + { + } + +private: + CPPUNIT_TEST_SUITE( GraphicsMatrixTestCaseGDIPlus ); + CPPUNIT_TEST( InitState ); + CPPUNIT_TEST( InvertMatrix ); + CPPUNIT_TEST( Concat1 ); + CPPUNIT_TEST( Concat2 ); + CPPUNIT_TEST( Concat3 ); + CPPUNIT_TEST_SUITE_END(); + +protected: + wxDECLARE_NO_COPY_CLASS(GraphicsMatrixTestCaseGDIPlus); +}; + +// register in the unnamed registry so that these tests are run by default +CPPUNIT_TEST_SUITE_REGISTRATION( GraphicsMatrixTestCaseGDIPlus ); + +// also include in it's own registry so that these tests can be run alone +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( GraphicsMatrixTestCaseGDIPlus, "GraphicsMatrixTestCaseGDIPlus" ); + +#endif // wxUSE_GRAPHICS_GDIPLUS + +#if wxUSE_GRAPHICS_DIRECT2D + +class GraphicsMatrixTestCaseDirect2D : public GraphicsMatrixTestCaseBase +{ +public: + GraphicsMatrixTestCaseDirect2D() + { + m_rend = wxGraphicsRenderer::GetDirect2DRenderer(); + } + + virtual ~GraphicsMatrixTestCaseDirect2D() + { + } + +private: + CPPUNIT_TEST_SUITE( GraphicsMatrixTestCaseDirect2D ); + CPPUNIT_TEST( InitState ); + CPPUNIT_TEST( InvertMatrix ); + CPPUNIT_TEST( Concat1 ); + CPPUNIT_TEST( Concat2 ); + CPPUNIT_TEST( Concat3 ); + CPPUNIT_TEST_SUITE_END(); + +protected: + wxDECLARE_NO_COPY_CLASS(GraphicsMatrixTestCaseDirect2D); +}; + +// register in the unnamed registry so that these tests are run by default +CPPUNIT_TEST_SUITE_REGISTRATION( GraphicsMatrixTestCaseDirect2D ); + +// also include in it's own registry so that these tests can be run alone +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( GraphicsMatrixTestCaseDirect2D, "GraphicsMatrixTestCaseDirect2D" ); + +#endif // wxUSE_GRAPHICS_DIRECT2D + +#endif // __WXMSW__ + +#if wxUSE_CAIRO + +class GraphicsMatrixTestCaseCairo : public GraphicsMatrixTestCaseBase +{ +public: + GraphicsMatrixTestCaseCairo() + { + m_rend = wxGraphicsRenderer::GetCairoRenderer(); + } + + virtual ~GraphicsMatrixTestCaseCairo() + { + } + +private: + CPPUNIT_TEST_SUITE( GraphicsMatrixTestCaseCairo ); + CPPUNIT_TEST( InitState ); + CPPUNIT_TEST( InvertMatrix ); + CPPUNIT_TEST( Concat1 ); + CPPUNIT_TEST( Concat2 ); + CPPUNIT_TEST( Concat3 ); + CPPUNIT_TEST_SUITE_END(); + +protected: + wxDECLARE_NO_COPY_CLASS(GraphicsMatrixTestCaseCairo); +}; + +// register in the unnamed registry so that these tests are run by default +CPPUNIT_TEST_SUITE_REGISTRATION( GraphicsMatrixTestCaseCairo ); + +// also include in it's own registry so that these tests can be run alone +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( GraphicsMatrixTestCaseCairo, "GraphicsMatrixTestCaseCairo" ); + +#endif // wxUSE_CAIRO + +class GraphicsMatrixTestCaseDefault : public GraphicsMatrixTestCaseBase +{ +public: + GraphicsMatrixTestCaseDefault() + { + m_rend = wxGraphicsRenderer::GetDefaultRenderer(); + } + + virtual ~GraphicsMatrixTestCaseDefault() + { + } + +private: + CPPUNIT_TEST_SUITE( GraphicsMatrixTestCaseDefault ); + CPPUNIT_TEST( InitState ); + CPPUNIT_TEST( InvertMatrix ); + CPPUNIT_TEST( Concat1 ); + CPPUNIT_TEST( Concat2 ); + CPPUNIT_TEST( Concat3 ); + CPPUNIT_TEST_SUITE_END(); + +protected: + wxDECLARE_NO_COPY_CLASS(GraphicsMatrixTestCaseDefault); +}; + +// register in the unnamed registry so that these tests are run by default +CPPUNIT_TEST_SUITE_REGISTRATION( GraphicsMatrixTestCaseDefault ); + +// also include in it's own registry so that these tests can be run alone +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( GraphicsMatrixTestCaseDefault, "GraphicsMatrixTestCaseDefault" ); + +// ===== Implementation ===== +static inline double RoundVal(double v) +{ + wxString s = wxString::Format(wxS("%g"), v); + s.ToDouble(&v); + return v; +} + +void GraphicsMatrixTestCaseBase::CheckMatrix(const wxGraphicsMatrix& m, + double a, double b, double c, double d, + double tx, double ty) +{ + double cur_a, cur_b, cur_c, cur_d, cur_tx, cur_ty; + m.Get(&cur_a, &cur_b, &cur_c, &cur_d, &cur_tx, &cur_ty); + + wxString msg; + + if ( RoundVal(a) != RoundVal(cur_a) ) + { + if ( !msg.empty() ) + { + msg += wxS("\n- "); + } + msg += wxString::Format(wxS("Invalid m11 value: Actual: %g Expected: %g"), + cur_a, a ); + } + + if ( RoundVal(b) != RoundVal(cur_b) ) + { + if ( !msg.empty() ) + { + msg += wxS("\n- "); + } + msg += wxString::Format(wxS("Invalid m12 value: Actual: %g Expected: %g"), + cur_b, b ); + } + + if ( RoundVal(c) != RoundVal(cur_c) ) + { + if ( !msg.empty() ) + { + msg += wxS("\n- "); + } + msg += wxString::Format(wxS("Invalid m21 value: Actual: %g Expected: %g"), + cur_c, c ); + } + + if ( RoundVal(d) != RoundVal(cur_d) ) + { + if ( !msg.empty() ) + { + msg += wxS("\n- "); + } + msg += wxString::Format(wxS("Invalid m22 value: Actual: %g Expected: %g"), + cur_d, d ); + } + + if ( RoundVal(tx) != RoundVal(cur_tx) ) + { + if ( !msg.empty() ) + { + msg += wxS("\n- "); + } + msg += wxString::Format(wxS("Invalid tx value: Actual: %g Expected: %g"), + cur_tx, tx ); + } + + if ( RoundVal(ty) != RoundVal(cur_ty) ) + { + if ( !msg.empty() ) + { + msg += wxS("\n- "); + } + msg += wxString::Format(wxS("Invalid ty value: Actual: %g Expected: %g"), + cur_ty, ty ); + } + + if( !msg.empty() ) + { + wxCharBuffer buffer = msg.ToUTF8(); + CPPUNIT_FAIL( buffer.data() ); + } +} + +void GraphicsMatrixTestCaseBase::InitState() +{ + wxGraphicsMatrix m = m_ctx->CreateMatrix(); + + CheckMatrix(m, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0); +} + +void GraphicsMatrixTestCaseBase::InvertMatrix() +{ + wxGraphicsMatrix m = m_ctx->CreateMatrix(2.0, 1.0, 1.0, 1.0, 1.0, 1.0); + m.Invert(); + + CheckMatrix(m, 1.0, -1.0, -1.0, 2.0, 0.0, -1.0); +} + +void GraphicsMatrixTestCaseBase::Concat1() +{ + wxGraphicsMatrix m1 = m_ctx->CreateMatrix(0.9, 0.4, -0.4, 0.9, 0.0, 0.0); + wxGraphicsMatrix m2 = m_ctx->CreateMatrix(1.0, 0.0, 0.0, 1.0, 3.0, 5.0); + m1.Concat(m2); + + CheckMatrix(m1, 0.9, 0.4, -0.4, 0.9, 0.7, 5.7); +} + +void GraphicsMatrixTestCaseBase::Concat2() +{ + wxGraphicsMatrix m1 = m_ctx->CreateMatrix(0.9, 0.4, -0.4, 0.9, 0.0, 0.0); + wxGraphicsMatrix m2 = m_ctx->CreateMatrix(1.0, 0.0, 0.0, 1.0, 3.0, 5.0); + m2.Concat(m1); + + CheckMatrix(m2, 0.9, 0.4, -0.4, 0.9, 3.0, 5.0); +} + +void GraphicsMatrixTestCaseBase::Concat3() +{ + wxGraphicsMatrix m1 = m_ctx->CreateMatrix(0.9, 0.4, -0.4, 0.9, 0.0, 0.0); + wxGraphicsMatrix m2 = m_ctx->CreateMatrix(1.0, 0.0, 0.0, 1.0, 3.0, 5.0); + wxGraphicsMatrix m = m1; + m.Concat(m2); + + CheckMatrix(m, 0.9, 0.4, -0.4, 0.9, 0.7, 5.7); +} + +#endif // wxUSE_GRAPHICS_CONTEXT diff --git a/tests/makefile.bcc b/tests/makefile.bcc index 4de882317d..1082200e75 100644 --- a/tests/makefile.bcc +++ b/tests/makefile.bcc @@ -167,6 +167,7 @@ TEST_GUI_OBJECTS = \ $(OBJS)\test_gui_affinematrix.obj \ $(OBJS)\test_gui_boundingbox.obj \ $(OBJS)\test_gui_clippingbox.obj \ + $(OBJS)\test_gui_graphmatrix.obj \ $(OBJS)\test_gui_config.obj \ $(OBJS)\test_gui_bitmapcomboboxtest.obj \ $(OBJS)\test_gui_bitmaptogglebuttontest.obj \ @@ -860,6 +861,9 @@ $(OBJS)\test_gui_boundingbox.obj: .\graphics\boundingbox.cpp $(OBJS)\test_gui_clippingbox.obj: .\graphics\clippingbox.cpp $(CXX) -q -c -P -o$@ $(TEST_GUI_CXXFLAGS) .\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_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 5dc1004a37..eb75bbb2e1 100644 --- a/tests/makefile.gcc +++ b/tests/makefile.gcc @@ -161,6 +161,7 @@ TEST_GUI_OBJECTS = \ $(OBJS)\test_gui_affinematrix.o \ $(OBJS)\test_gui_boundingbox.o \ $(OBJS)\test_gui_clippingbox.o \ + $(OBJS)\test_gui_graphmatrix.o \ $(OBJS)\test_gui_config.o \ $(OBJS)\test_gui_bitmapcomboboxtest.o \ $(OBJS)\test_gui_bitmaptogglebuttontest.o \ @@ -836,6 +837,9 @@ $(OBJS)\test_gui_boundingbox.o: ./graphics/boundingbox.cpp $(OBJS)\test_gui_clippingbox.o: ./graphics/clippingbox.cpp $(CXX) -c -o $@ $(TEST_GUI_CXXFLAGS) $(CPPDEPS) $< +$(OBJS)\test_gui_graphmatrix.o: ./graphics/graphmatrix.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 676d02e15b..93cd758a87 100644 --- a/tests/makefile.vc +++ b/tests/makefile.vc @@ -171,6 +171,7 @@ TEST_GUI_OBJECTS = \ $(OBJS)\test_gui_affinematrix.obj \ $(OBJS)\test_gui_boundingbox.obj \ $(OBJS)\test_gui_clippingbox.obj \ + $(OBJS)\test_gui_graphmatrix.obj \ $(OBJS)\test_gui_config.obj \ $(OBJS)\test_gui_bitmapcomboboxtest.obj \ $(OBJS)\test_gui_bitmaptogglebuttontest.obj \ @@ -1037,6 +1038,9 @@ $(OBJS)\test_gui_boundingbox.obj: .\graphics\boundingbox.cpp $(OBJS)\test_gui_clippingbox.obj: .\graphics\clippingbox.cpp $(CXX) /c /nologo /TP /Fo$@ $(TEST_GUI_CXXFLAGS) .\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_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 03998d257b..d913bd9e5c 100644 --- a/tests/test.bkl +++ b/tests/test.bkl @@ -181,6 +181,7 @@ graphics/affinematrix.cpp graphics/boundingbox.cpp graphics/clippingbox.cpp + graphics/graphmatrix.cpp config/config.cpp controls/bitmapcomboboxtest.cpp controls/bitmaptogglebuttontest.cpp diff --git a/tests/test_gui.vcxproj b/tests/test_gui.vcxproj index b5199fe130..6fb8112a6d 100644 --- a/tests/test_gui.vcxproj +++ b/tests/test_gui.vcxproj @@ -529,6 +529,7 @@ + diff --git a/tests/test_gui.vcxproj.filters b/tests/test_gui.vcxproj.filters index 801080ba5e..583050573b 100644 --- a/tests/test_gui.vcxproj.filters +++ b/tests/test_gui.vcxproj.filters @@ -1,293 +1,296 @@ - - - - - {4FC737F1-C7A5-4376-A066-2A32D752A2FF} - cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx - - - {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} - rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav - - - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - - - Resource Files - - - + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav + + + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + + + Resource Files + + + diff --git a/tests/test_vc7_test_gui.vcproj b/tests/test_vc7_test_gui.vcproj index ed7e734217..8086ea6e90 100644 --- a/tests/test_vc7_test_gui.vcproj +++ b/tests/test_vc7_test_gui.vcproj @@ -412,6 +412,9 @@ + + diff --git a/tests/test_vc8_test_gui.vcproj b/tests/test_vc8_test_gui.vcproj index a8a9ed514c..dd860bb8dc 100644 --- a/tests/test_vc8_test_gui.vcproj +++ b/tests/test_vc8_test_gui.vcproj @@ -1018,6 +1018,10 @@ RelativePath=".\controls\gaugetest.cpp" > + + diff --git a/tests/test_vc9_test_gui.vcproj b/tests/test_vc9_test_gui.vcproj index f53fa6ec21..9518494ceb 100644 --- a/tests/test_vc9_test_gui.vcproj +++ b/tests/test_vc9_test_gui.vcproj @@ -990,6 +990,10 @@ RelativePath=".\controls\gaugetest.cpp" > + +