Allow wxTextMeasure to work with non-native wxDC objects too.
[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 #include "wx/dcps.h"
35 #include "wx/metafile.h"
36
37 // ----------------------------------------------------------------------------
38 // test class
39 // ----------------------------------------------------------------------------
40
41 class MeasuringTextTestCase : public CppUnit::TestCase
42 {
43 public:
44 MeasuringTextTestCase() { }
45
46 private:
47 CPPUNIT_TEST_SUITE( MeasuringTextTestCase );
48 CPPUNIT_TEST( DCGetTextExtent );
49 CPPUNIT_TEST( WindowGetTextExtent );
50 CPPUNIT_TEST( GetPartialTextExtent );
51 #ifdef TEST_GC
52 CPPUNIT_TEST( GraphicsGetTextExtent );
53 #endif // TEST_GC
54 CPPUNIT_TEST_SUITE_END();
55
56 template <typename T>
57 void DoTestGetTextExtent(const T& obj);
58
59 void DCGetTextExtent();
60 void WindowGetTextExtent();
61
62 void GetPartialTextExtent();
63
64 #ifdef TEST_GC
65 void GraphicsGetTextExtent();
66 #endif // TEST_GC
67
68 DECLARE_NO_COPY_CLASS(MeasuringTextTestCase)
69 };
70
71 // register in the unnamed registry so that these tests are run by default
72 CPPUNIT_TEST_SUITE_REGISTRATION( MeasuringTextTestCase );
73
74 // also include in its own registry so that these tests can be run alone
75 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MeasuringTextTestCase, "MeasuringTextTestCase" );
76
77 // ----------------------------------------------------------------------------
78 // tests themselves
79 // ----------------------------------------------------------------------------
80
81 template <typename T>
82 void MeasuringTextTestCase::DoTestGetTextExtent(const T& obj)
83 {
84 // Test that getting the height only doesn't crash.
85 int y;
86 obj.GetTextExtent("H", NULL, &y);
87
88 CPPUNIT_ASSERT( y > 1 );
89
90 wxSize size = obj.GetTextExtent("Hello");
91 CPPUNIT_ASSERT( size.x > 1 );
92 CPPUNIT_ASSERT_EQUAL( y, size.y );
93 }
94
95 void MeasuringTextTestCase::DCGetTextExtent()
96 {
97 wxClientDC dc(wxTheApp->GetTopWindow());
98
99 DoTestGetTextExtent(dc);
100
101 int w;
102 dc.GetMultiLineTextExtent("Good\nbye", &w, NULL);
103 const wxSize sz = dc.GetTextExtent("Good");
104 CPPUNIT_ASSERT_EQUAL( sz.x, w );
105
106 CPPUNIT_ASSERT( dc.GetMultiLineTextExtent("Good\nbye").y >= 2*sz.y );
107
108 // Test the functions with some other DC kinds also.
109 #if wxUSE_PRINTING_ARCHITECTURE && wxUSE_POSTSCRIPT
110 wxPostScriptDC psdc;
111 // wxPostScriptDC doesn't have any font set by default but its
112 // GetTextExtent() requires one to be set. This is probably a bug and we
113 // should set the default font in it implicitly but for now just work
114 // around it.
115 psdc.SetFont(*wxNORMAL_FONT);
116 DoTestGetTextExtent(psdc);
117 #endif
118
119 #if wxUSE_ENH_METAFILE
120 wxEnhMetaFileDC metadc;
121 DoTestGetTextExtent(metadc);
122 #endif
123 }
124
125 void MeasuringTextTestCase::WindowGetTextExtent()
126 {
127 wxWindow* const win = wxTheApp->GetTopWindow();
128
129 DoTestGetTextExtent(*win);
130 }
131
132 void MeasuringTextTestCase::GetPartialTextExtent()
133 {
134 wxClientDC dc(wxTheApp->GetTopWindow());
135
136 wxArrayInt widths;
137 CPPUNIT_ASSERT( dc.GetPartialTextExtents("Hello", widths) );
138 CPPUNIT_ASSERT_EQUAL( 5, widths.size() );
139 CPPUNIT_ASSERT_EQUAL( widths[0], dc.GetTextExtent("H").x );
140 CPPUNIT_ASSERT_EQUAL( widths[4], dc.GetTextExtent("Hello").x );
141 }
142
143 #ifdef TEST_GC
144
145 void MeasuringTextTestCase::GraphicsGetTextExtent()
146 {
147 wxGraphicsRenderer* renderer = wxGraphicsRenderer::GetDefaultRenderer();
148 CPPUNIT_ASSERT(renderer);
149 wxGraphicsContext* context = renderer->CreateMeasuringContext();
150 CPPUNIT_ASSERT(context);
151 wxFont font(12, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
152 CPPUNIT_ASSERT(font.IsOk());
153 context->SetFont(font, *wxBLACK);
154 double width, height, descent, externalLeading = 0.0;
155 context->GetTextExtent("x", &width, &height, &descent, &externalLeading);
156
157 // TODO: Determine a way to make these tests more robust.
158 CPPUNIT_ASSERT(width > 0.0);
159 CPPUNIT_ASSERT(height > 0.0);
160
161 }
162
163 #endif // TEST_GC