Factor out text measurement from wxDC and wxWindow into wxTextMeasure.
[wxWidgets.git] / tests / graphics / measuring.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/graphics/measuring.cpp
3 // Purpose: Tests for wxGraphicsRenderer::CreateMeasuringContext
4 // Author: Kevin Ollivier, Vadim Zeitlin (non wxGC parts)
5 // Created: 2008-02-12
6 // RCS-ID: $Id$
7 // Copyright: (c) 2008 Kevin Ollivier <kevino@theolliviers.com>
8 // (c) 2012 Vadim Zeitlin <vadim@wxwidgets.org>
9 ///////////////////////////////////////////////////////////////////////////////
10
11 // ----------------------------------------------------------------------------
12 // headers
13 // ----------------------------------------------------------------------------
14
15 #include "testprec.h"
16
17 #ifdef __BORLANDC__
18 #pragma hdrstop
19 #endif
20
21 #ifndef WX_PRECOMP
22 #include "wx/app.h"
23 #include "wx/font.h"
24 #include "wx/window.h"
25 #endif // WX_PRECOMP
26
27 // wxCairoRenderer::CreateMeasuringContext() is not implement for wxX11
28 #if wxUSE_GRAPHICS_CONTEXT && !defined(__WXX11__)
29 #include "wx/graphics.h"
30 #define TEST_GC
31 #endif
32
33 #include "wx/dcclient.h"
34
35 // ----------------------------------------------------------------------------
36 // test class
37 // ----------------------------------------------------------------------------
38
39 class MeasuringTextTestCase : public CppUnit::TestCase
40 {
41 public:
42 MeasuringTextTestCase() { }
43
44 private:
45 CPPUNIT_TEST_SUITE( MeasuringTextTestCase );
46 CPPUNIT_TEST( DCGetTextExtent );
47 CPPUNIT_TEST( WindowGetTextExtent );
48 CPPUNIT_TEST( GetPartialTextExtent );
49 #ifdef TEST_GC
50 CPPUNIT_TEST( GraphicsGetTextExtent );
51 #endif // TEST_GC
52 CPPUNIT_TEST_SUITE_END();
53
54 template <typename T>
55 void DoTestGetTextExtent(const T& obj);
56
57 void DCGetTextExtent();
58 void WindowGetTextExtent();
59
60 void GetPartialTextExtent();
61
62 #ifdef TEST_GC
63 void GraphicsGetTextExtent();
64 #endif // TEST_GC
65
66 DECLARE_NO_COPY_CLASS(MeasuringTextTestCase)
67 };
68
69 // register in the unnamed registry so that these tests are run by default
70 CPPUNIT_TEST_SUITE_REGISTRATION( MeasuringTextTestCase );
71
72 // also include in its own registry so that these tests can be run alone
73 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MeasuringTextTestCase, "MeasuringTextTestCase" );
74
75 // ----------------------------------------------------------------------------
76 // tests themselves
77 // ----------------------------------------------------------------------------
78
79 template <typename T>
80 void MeasuringTextTestCase::DoTestGetTextExtent(const T& obj)
81 {
82 // Test that getting the height only doesn't crash.
83 int y;
84 obj.GetTextExtent("H", NULL, &y);
85
86 CPPUNIT_ASSERT( y > 1 );
87
88 wxSize size = obj.GetTextExtent("Hello");
89 CPPUNIT_ASSERT( size.x > 1 );
90 CPPUNIT_ASSERT_EQUAL( y, size.y );
91 }
92
93 void MeasuringTextTestCase::DCGetTextExtent()
94 {
95 wxClientDC dc(wxTheApp->GetTopWindow());
96
97 DoTestGetTextExtent(dc);
98 }
99
100 void MeasuringTextTestCase::WindowGetTextExtent()
101 {
102 wxWindow* const win = wxTheApp->GetTopWindow();
103
104 DoTestGetTextExtent(*win);
105 }
106
107 void MeasuringTextTestCase::GetPartialTextExtent()
108 {
109 wxClientDC dc(wxTheApp->GetTopWindow());
110
111 wxArrayInt widths;
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 );
116 }
117
118 #ifdef TEST_GC
119
120 void MeasuringTextTestCase::GraphicsGetTextExtent()
121 {
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);
131
132 // TODO: Determine a way to make these tests more robust.
133 CPPUNIT_ASSERT(width > 0.0);
134 CPPUNIT_ASSERT(height > 0.0);
135
136 }
137
138 #endif // TEST_GC