The rounded corners look really dumb at this size.
[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 // Copyright: (c) 2008 Kevin Ollivier <kevino@theolliviers.com>
7 // (c) 2012 Vadim Zeitlin <vadim@wxwidgets.org>
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "testprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #ifndef WX_PRECOMP
21 #include "wx/app.h"
22 #include "wx/font.h"
23 #include "wx/window.h"
24 #endif // WX_PRECOMP
25
26 // wxCairoRenderer::CreateMeasuringContext() is not implement for wxX11
27 #if wxUSE_GRAPHICS_CONTEXT && !defined(__WXX11__)
28 #include "wx/graphics.h"
29 #define TEST_GC
30 #endif
31
32 #include "wx/dcclient.h"
33 #include "wx/dcps.h"
34 #include "wx/metafile.h"
35
36 // ----------------------------------------------------------------------------
37 // test class
38 // ----------------------------------------------------------------------------
39
40 class MeasuringTextTestCase : public CppUnit::TestCase
41 {
42 public:
43 MeasuringTextTestCase() { }
44
45 private:
46 CPPUNIT_TEST_SUITE( MeasuringTextTestCase );
47 CPPUNIT_TEST( DCGetTextExtent );
48 CPPUNIT_TEST( LeadingAndDescent );
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 void DCGetTextExtent();
57 void LeadingAndDescent();
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 // helper for XXXTextExtent() methods
77 // ----------------------------------------------------------------------------
78
79 template <typename T>
80 struct GetTextExtentTester
81 {
82 // Constructor runs a couple of simple tests for GetTextExtent().
83 GetTextExtentTester(const T& obj)
84 {
85 // Test that getting the height only doesn't crash.
86 int y;
87 obj.GetTextExtent("H", NULL, &y);
88
89 CPPUNIT_ASSERT( y > 1 );
90
91 wxSize size = obj.GetTextExtent("Hello");
92 CPPUNIT_ASSERT( size.x > 1 );
93 CPPUNIT_ASSERT_EQUAL( y, size.y );
94 }
95 };
96
97 // ----------------------------------------------------------------------------
98 // tests themselves
99 // ----------------------------------------------------------------------------
100
101 void MeasuringTextTestCase::DCGetTextExtent()
102 {
103 wxClientDC dc(wxTheApp->GetTopWindow());
104
105 GetTextExtentTester<wxClientDC> testDC(dc);
106
107 int w;
108 dc.GetMultiLineTextExtent("Good\nbye", &w, NULL);
109 const wxSize sz = dc.GetTextExtent("Good");
110 CPPUNIT_ASSERT_EQUAL( sz.x, w );
111
112 CPPUNIT_ASSERT( dc.GetMultiLineTextExtent("Good\nbye").y >= 2*sz.y );
113
114 // Test the functions with some other DC kinds also.
115 #if wxUSE_PRINTING_ARCHITECTURE && wxUSE_POSTSCRIPT
116 wxPostScriptDC psdc;
117 // wxPostScriptDC doesn't have any font set by default but its
118 // GetTextExtent() requires one to be set. This is probably a bug and we
119 // should set the default font in it implicitly but for now just work
120 // around it.
121 psdc.SetFont(*wxNORMAL_FONT);
122 GetTextExtentTester<wxPostScriptDC> testPS(psdc);
123 #endif
124
125 #if wxUSE_ENH_METAFILE
126 wxEnhMetaFileDC metadc;
127 GetTextExtentTester<wxEnhMetaFileDC> testMF(metadc);
128 #endif
129 }
130
131 void MeasuringTextTestCase::LeadingAndDescent()
132 {
133 wxClientDC dc(wxTheApp->GetTopWindow());
134
135 // Retrieving just the descent should work.
136 int descent = -17;
137 dc.GetTextExtent("foo", NULL, NULL, &descent);
138 CPPUNIT_ASSERT( descent != -17 );
139
140 // Same for external leading.
141 int leading = -289;
142 dc.GetTextExtent("foo", NULL, NULL, NULL, &leading);
143 CPPUNIT_ASSERT( leading != -289 );
144
145 // And both should also work for the empty string as they retrieve the
146 // values valid for the entire font and not just this string.
147 int descent2,
148 leading2;
149 dc.GetTextExtent("", NULL, NULL, &descent2, &leading2);
150
151 CPPUNIT_ASSERT_EQUAL( descent, descent2 );
152 CPPUNIT_ASSERT_EQUAL( leading, leading2 );
153 }
154
155 void MeasuringTextTestCase::WindowGetTextExtent()
156 {
157 wxWindow* const win = wxTheApp->GetTopWindow();
158
159 GetTextExtentTester<wxWindow> testWin(*win);
160 }
161
162 void MeasuringTextTestCase::GetPartialTextExtent()
163 {
164 wxClientDC dc(wxTheApp->GetTopWindow());
165
166 wxArrayInt widths;
167 CPPUNIT_ASSERT( dc.GetPartialTextExtents("Hello", widths) );
168 CPPUNIT_ASSERT_EQUAL( 5, widths.size() );
169 CPPUNIT_ASSERT_EQUAL( widths[0], dc.GetTextExtent("H").x );
170 CPPUNIT_ASSERT_EQUAL( widths[4], dc.GetTextExtent("Hello").x );
171 }
172
173 #ifdef TEST_GC
174
175 void MeasuringTextTestCase::GraphicsGetTextExtent()
176 {
177 wxGraphicsRenderer* renderer = wxGraphicsRenderer::GetDefaultRenderer();
178 CPPUNIT_ASSERT(renderer);
179 wxGraphicsContext* context = renderer->CreateMeasuringContext();
180 CPPUNIT_ASSERT(context);
181 wxFont font(12, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
182 CPPUNIT_ASSERT(font.IsOk());
183 context->SetFont(font, *wxBLACK);
184 double width, height, descent, externalLeading = 0.0;
185 context->GetTextExtent("x", &width, &height, &descent, &externalLeading);
186
187 // TODO: Determine a way to make these tests more robust.
188 CPPUNIT_ASSERT(width > 0.0);
189 CPPUNIT_ASSERT(height > 0.0);
190
191 }
192
193 #endif // TEST_GC