X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/2fc9c1ea77eb02938debb6d4c2bdebd052309cc2..cd794127ff73748f8199587897f57aa02c5ef059:/tests/graphics/measuring.cpp diff --git a/tests/graphics/measuring.cpp b/tests/graphics/measuring.cpp index 9affc9882c..ae02d27239 100644 --- a/tests/graphics/measuring.cpp +++ b/tests/graphics/measuring.cpp @@ -1,10 +1,11 @@ /////////////////////////////////////////////////////////////////////////////// // Name: tests/graphics/measuring.cpp // Purpose: Tests for wxGraphicsRenderer::CreateMeasuringContext -// Author: Kevin Ollivier +// Author: Kevin Ollivier, Vadim Zeitlin (non wxGC parts) // Created: 2008-02-12 -// RCS-ID: $Id: clientsize.cpp 53740 2008-05-25 02:56:22Z VZ $ +// RCS-ID: $Id$ // Copyright: (c) 2008 Kevin Ollivier +// (c) 2012 Vadim Zeitlin /////////////////////////////////////////////////////////////////////////////// // ---------------------------------------------------------------------------- @@ -13,8 +14,6 @@ #include "testprec.h" -#if wxUSE_GRAPHICS_CONTEXT - #ifdef __BORLANDC__ #pragma hdrstop #endif @@ -25,56 +24,130 @@ #include "wx/window.h" #endif // WX_PRECOMP -#include "wx/graphics.h" +// wxCairoRenderer::CreateMeasuringContext() is not implement for wxX11 +#if wxUSE_GRAPHICS_CONTEXT && !defined(__WXX11__) + #include "wx/graphics.h" + #define TEST_GC +#endif + +#include "wx/dcclient.h" +#include "wx/dcps.h" +#include "wx/metafile.h" + // ---------------------------------------------------------------------------- // test class // ---------------------------------------------------------------------------- -class MeasuringContextTestCase : public CppUnit::TestCase +class MeasuringTextTestCase : public CppUnit::TestCase { public: - MeasuringContextTestCase() { } - - virtual void setUp(); - virtual void tearDown(); + MeasuringTextTestCase() { } private: - CPPUNIT_TEST_SUITE( MeasuringContextTestCase ); - CPPUNIT_TEST( GetTextExtent ); + CPPUNIT_TEST_SUITE( MeasuringTextTestCase ); + CPPUNIT_TEST( DCGetTextExtent ); + CPPUNIT_TEST( WindowGetTextExtent ); + CPPUNIT_TEST( GetPartialTextExtent ); +#ifdef TEST_GC + CPPUNIT_TEST( GraphicsGetTextExtent ); +#endif // TEST_GC CPPUNIT_TEST_SUITE_END(); - void GetTextExtent(); + void DCGetTextExtent(); + void WindowGetTextExtent(); + + void GetPartialTextExtent(); - wxWindow *m_win; +#ifdef TEST_GC + void GraphicsGetTextExtent(); +#endif // TEST_GC - DECLARE_NO_COPY_CLASS(MeasuringContextTestCase) + DECLARE_NO_COPY_CLASS(MeasuringTextTestCase) }; // register in the unnamed registry so that these tests are run by default -CPPUNIT_TEST_SUITE_REGISTRATION( MeasuringContextTestCase ); +CPPUNIT_TEST_SUITE_REGISTRATION( MeasuringTextTestCase ); -// also include in it's own registry so that these tests can be run alone -CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MeasuringContextTestCase, "MeasuringContextTestCase" ); +// also include in its own registry so that these tests can be run alone +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MeasuringTextTestCase, "MeasuringTextTestCase" ); // ---------------------------------------------------------------------------- -// test initialization +// helper for XXXTextExtent() methods +// ---------------------------------------------------------------------------- + +template +struct GetTextExtentTester +{ + // Constructor runs a couple of simple tests for GetTextExtent(). + GetTextExtentTester(const T& obj) + { + // Test that getting the height only doesn't crash. + int y; + obj.GetTextExtent("H", NULL, &y); + + CPPUNIT_ASSERT( y > 1 ); + + wxSize size = obj.GetTextExtent("Hello"); + CPPUNIT_ASSERT( size.x > 1 ); + CPPUNIT_ASSERT_EQUAL( y, size.y ); + } +}; + +// ---------------------------------------------------------------------------- +// tests themselves // ---------------------------------------------------------------------------- -void MeasuringContextTestCase::setUp() +void MeasuringTextTestCase::DCGetTextExtent() { - m_win = wxTheApp->GetTopWindow(); + wxClientDC dc(wxTheApp->GetTopWindow()); + + GetTextExtentTester testDC(dc); + + int w; + dc.GetMultiLineTextExtent("Good\nbye", &w, NULL); + const wxSize sz = dc.GetTextExtent("Good"); + CPPUNIT_ASSERT_EQUAL( sz.x, w ); + + CPPUNIT_ASSERT( dc.GetMultiLineTextExtent("Good\nbye").y >= 2*sz.y ); + + // Test the functions with some other DC kinds also. +#if wxUSE_PRINTING_ARCHITECTURE && wxUSE_POSTSCRIPT + wxPostScriptDC psdc; + // wxPostScriptDC doesn't have any font set by default but its + // GetTextExtent() requires one to be set. This is probably a bug and we + // should set the default font in it implicitly but for now just work + // around it. + psdc.SetFont(*wxNORMAL_FONT); + GetTextExtentTester testPS(psdc); +#endif + +#if wxUSE_ENH_METAFILE + wxEnhMetaFileDC metadc; + GetTextExtentTester testMF(metadc); +#endif } -void MeasuringContextTestCase::tearDown() +void MeasuringTextTestCase::WindowGetTextExtent() { - m_win = NULL; + wxWindow* const win = wxTheApp->GetTopWindow(); + + GetTextExtentTester testWin(*win); } -// ---------------------------------------------------------------------------- -// tests themselves -// ---------------------------------------------------------------------------- +void MeasuringTextTestCase::GetPartialTextExtent() +{ + wxClientDC dc(wxTheApp->GetTopWindow()); + + wxArrayInt widths; + CPPUNIT_ASSERT( dc.GetPartialTextExtents("Hello", widths) ); + CPPUNIT_ASSERT_EQUAL( 5, widths.size() ); + CPPUNIT_ASSERT_EQUAL( widths[0], dc.GetTextExtent("H").x ); + CPPUNIT_ASSERT_EQUAL( widths[4], dc.GetTextExtent("Hello").x ); +} + +#ifdef TEST_GC -void MeasuringContextTestCase::GetTextExtent() +void MeasuringTextTestCase::GraphicsGetTextExtent() { wxGraphicsRenderer* renderer = wxGraphicsRenderer::GetDefaultRenderer(); CPPUNIT_ASSERT(renderer); @@ -92,4 +165,4 @@ void MeasuringContextTestCase::GetTextExtent() } -#endif +#endif // TEST_GC