]>
git.saurik.com Git - wxWidgets.git/blob - tests/graphics/measuring.cpp
dbf032bee0498d9d0da2be4834958ea8e92bcda9
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/graphics/measuring.cpp
3 // Purpose: Tests for wxGraphicsRenderer::CreateMeasuringContext
4 // Author: Kevin Ollivier, Vadim Zeitlin (non wxGC parts)
7 // Copyright: (c) 2008 Kevin Ollivier <kevino@theolliviers.com>
8 // (c) 2012 Vadim Zeitlin <vadim@wxwidgets.org>
9 ///////////////////////////////////////////////////////////////////////////////
11 // ----------------------------------------------------------------------------
13 // ----------------------------------------------------------------------------
24 #include "wx/window.h"
27 // wxCairoRenderer::CreateMeasuringContext() is not implement for wxX11
28 #if wxUSE_GRAPHICS_CONTEXT && !defined(__WXX11__)
29 #include "wx/graphics.h"
33 #include "wx/dcclient.h"
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 class MeasuringTextTestCase
: public CppUnit::TestCase
42 MeasuringTextTestCase() { }
45 CPPUNIT_TEST_SUITE( MeasuringTextTestCase
);
46 CPPUNIT_TEST( DCGetTextExtent
);
47 CPPUNIT_TEST( WindowGetTextExtent
);
48 CPPUNIT_TEST( GetPartialTextExtent
);
50 CPPUNIT_TEST( GraphicsGetTextExtent
);
52 CPPUNIT_TEST_SUITE_END();
55 void DoTestGetTextExtent(const T
& obj
);
57 void DCGetTextExtent();
58 void WindowGetTextExtent();
60 void GetPartialTextExtent();
63 void GraphicsGetTextExtent();
66 DECLARE_NO_COPY_CLASS(MeasuringTextTestCase
)
69 // register in the unnamed registry so that these tests are run by default
70 CPPUNIT_TEST_SUITE_REGISTRATION( MeasuringTextTestCase
);
72 // also include in its own registry so that these tests can be run alone
73 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MeasuringTextTestCase
, "MeasuringTextTestCase" );
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
80 void MeasuringTextTestCase::DoTestGetTextExtent(const T
& obj
)
82 // Test that getting the height only doesn't crash.
84 obj
.GetTextExtent("H", NULL
, &y
);
86 CPPUNIT_ASSERT( y
> 1 );
88 wxSize size
= obj
.GetTextExtent("Hello");
89 CPPUNIT_ASSERT( size
.x
> 1 );
90 CPPUNIT_ASSERT_EQUAL( y
, size
.y
);
93 void MeasuringTextTestCase::DCGetTextExtent()
95 wxClientDC
dc(wxTheApp
->GetTopWindow());
97 DoTestGetTextExtent(dc
);
100 void MeasuringTextTestCase::WindowGetTextExtent()
102 wxWindow
* const win
= wxTheApp
->GetTopWindow();
104 DoTestGetTextExtent(*win
);
107 void MeasuringTextTestCase::GetPartialTextExtent()
109 wxClientDC
dc(wxTheApp
->GetTopWindow());
112 CPPUNIT_ASSERT( dc
.GetPartialTextExtents("Hello", widths
) );
113 CPPUNIT_ASSERT_EQUAL( 5, widths
.size() );
114 CPPUNIT_ASSERT_EQUAL( widths
[0], dc
.GetTextExtent("H").x
);
115 CPPUNIT_ASSERT_EQUAL( widths
[4], dc
.GetTextExtent("Hello").x
);
120 void MeasuringTextTestCase::GraphicsGetTextExtent()
122 wxGraphicsRenderer
* renderer
= wxGraphicsRenderer::GetDefaultRenderer();
123 CPPUNIT_ASSERT(renderer
);
124 wxGraphicsContext
* context
= renderer
->CreateMeasuringContext();
125 CPPUNIT_ASSERT(context
);
126 wxFont
font(12, wxFONTFAMILY_DEFAULT
, wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
);
127 CPPUNIT_ASSERT(font
.IsOk());
128 context
->SetFont(font
, *wxBLACK
);
129 double width
, height
, descent
, externalLeading
= 0.0;
130 context
->GetTextExtent("x", &width
, &height
, &descent
, &externalLeading
);
132 // TODO: Determine a way to make these tests more robust.
133 CPPUNIT_ASSERT(width
> 0.0);
134 CPPUNIT_ASSERT(height
> 0.0);