]> git.saurik.com Git - wxWidgets.git/blame - tests/font/fonttest.cpp
Suppress harmless MSVC warnings about int to bool conversions.
[wxWidgets.git] / tests / font / fonttest.cpp
CommitLineData
5b06a80f
FM
1///////////////////////////////////////////////////////////////////////////////
2// Name: tests/font/fonttest.cpp
3// Purpose: wxFont unit test
4// Author: Francesco Montorsi
5// Created: 16.3.09
6// RCS-ID: $Id$
7// Copyright: (c) 2009 Francesco Montorsi
8// Licence: wxWindows licence
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/wx.h"
23#endif // WX_PRECOMP
24
25#if wxUSE_FONTMAP
26
27#include "wx/font.h"
28
29// ----------------------------------------------------------------------------
30// test class
31// ----------------------------------------------------------------------------
32
33class FontTestCase : public CppUnit::TestCase
34{
35public:
36 FontTestCase() { }
37
38private:
39 CPPUNIT_TEST_SUITE( FontTestCase );
cec8d677 40 CPPUNIT_TEST( Construct );
5b06a80f 41 CPPUNIT_TEST( GetSet );
c56fc0dc
VZ
42 CPPUNIT_TEST( NativeFontInfo );
43 CPPUNIT_TEST( NativeFontInfoUserDesc );
5b06a80f
FM
44 CPPUNIT_TEST_SUITE_END();
45
cec8d677 46 void Construct();
5b06a80f 47 void GetSet();
c56fc0dc
VZ
48 void NativeFontInfo();
49 void NativeFontInfoUserDesc();
50
51 static const wxFont *GetTestFonts(unsigned& numFonts)
52 {
53 static const wxFont testfonts[] =
54 {
55 *wxNORMAL_FONT,
56 *wxSMALL_FONT,
57 *wxITALIC_FONT,
58 *wxSWISS_FONT,
59 wxFont(5, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL)
60 };
61
62 numFonts = WXSIZEOF(testfonts);
63
64 return testfonts;
65 }
5b06a80f
FM
66
67 DECLARE_NO_COPY_CLASS(FontTestCase)
68};
69
70// register in the unnamed registry so that these tests are run by default
71CPPUNIT_TEST_SUITE_REGISTRATION( FontTestCase );
72
e3778b4d 73// also include in its own registry so that these tests can be run alone
5b06a80f
FM
74CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FontTestCase, "FontTestCase" );
75
aeecbca0
FM
76wxString DumpFont(const wxFont *font)
77{
78 // dumps the internal properties of a wxFont in the same order they
79 // are checked by wxFontBase::operator==()
c56fc0dc 80
aeecbca0 81 wxASSERT(font->IsOk());
c56fc0dc 82
aeecbca0
FM
83 wxString s;
84 s.Printf(wxS("%d-%d;%d-%d-%d-%d-%d-%s-%d"),
85 font->GetPointSize(),
86 font->GetPixelSize().x,
87 font->GetPixelSize().y,
88 font->GetFamily(),
89 font->GetStyle(),
90 font->GetWeight(),
91 font->GetUnderlined() ? 1 : 0,
92 font->GetFaceName(),
93 font->GetEncoding());
94
95 return s;
96}
5b06a80f 97
cec8d677
VZ
98void FontTestCase::Construct()
99{
100 // The main purpose of this test is to verify that the font ctors below
101 // compile because it's easy to introduce ambiguities due to the number of
102 // overloaded wxFont ctors.
103
104 CPPUNIT_ASSERT( wxFont(10, wxFONTFAMILY_DEFAULT).IsOk() );
105 CPPUNIT_ASSERT( wxFont(10, wxFONTFAMILY_DEFAULT,
106 wxFONTFLAG_DEFAULT).IsOk() );
107 CPPUNIT_ASSERT( wxFont(10, wxFONTFAMILY_DEFAULT,
108 wxFONTSTYLE_NORMAL,
109 wxFONTWEIGHT_NORMAL).IsOk() );
110
111#if FUTURE_WXWIN_COMPATIBILITY_3_0
112 // Tests relying on the soon-to-be-deprecated ctor taking ints and not
113 // wxFontXXX enum elements.
114 CPPUNIT_ASSERT( wxFont(10, wxDEFAULT, wxNORMAL, wxNORMAL).IsOk() );
115#endif // FUTURE_WXWIN_COMPATIBILITY_3_0
116}
117
5b06a80f
FM
118void FontTestCase::GetSet()
119{
c56fc0dc
VZ
120 unsigned numFonts;
121 const wxFont *pf = GetTestFonts(numFonts);
b7cb8553 122 for ( unsigned n = 0; n < numFonts; n++ )
5b06a80f 123 {
c56fc0dc 124 wxFont test(*pf++);
5b06a80f
FM
125
126 // remember: getters can only be called when wxFont::IsOk() == true
127 CPPUNIT_ASSERT( test.IsOk() );
128
129
130 // test Get/SetFaceName()
5b06a80f
FM
131 CPPUNIT_ASSERT( !test.SetFaceName("a dummy face name") );
132 CPPUNIT_ASSERT( !test.IsOk() );
133
3f7c8a87
VZ
134 // if the call to SetFaceName() below fails on your system/port,
135 // consider adding another branch to this #if
136#if defined(__WXMSW__) || defined(__WXOSX__)
137 static const char *knownGoodFaceName = "Arial";
138#else
b7cb8553 139 static const char *knownGoodFaceName = "Monospace";
3f7c8a87
VZ
140#endif
141
b7cb8553
VZ
142 WX_ASSERT_MESSAGE
143 (
144 ("failed to set face name \"%s\" for test font #%u\n"
145 "(this failure is harmless if this face name is not "
146 "available on this system)", knownGoodFaceName, n),
147 test.SetFaceName(knownGoodFaceName)
148 );
5b06a80f
FM
149 CPPUNIT_ASSERT( test.IsOk() );
150
151
152 // test Get/SetFamily()
153
6aea1e4a 154 test.SetFamily( wxFONTFAMILY_ROMAN );
14a83cbf 155 CPPUNIT_ASSERT( test.IsOk() );
aa6b8882
VZ
156
157 // note that there is always the possibility that GetFamily() returns
158 // wxFONTFAMILY_DEFAULT (meaning "unknown" in this case) so that we
159 // consider it as a valid return value
160 const wxFontFamily family = test.GetFamily();
161 if ( family != wxFONTFAMILY_DEFAULT )
162 CPPUNIT_ASSERT_EQUAL( wxFONTFAMILY_ROMAN, family );
6aea1e4a 163
5b06a80f 164
5b06a80f
FM
165 // test Get/SetEncoding()
166
167 //test.SetEncoding( wxFONTENCODING_KOI8 );
14a83cbf 168 //CPPUNIT_ASSERT( test.IsOk() );
5b06a80f
FM
169 //CPPUNIT_ASSERT_EQUAL( wxFONTENCODING_KOI8 , test.GetEncoding() );
170
c56fc0dc
VZ
171
172 // test Get/SetPointSize()
173
174 test.SetPointSize(30);
175 CPPUNIT_ASSERT( test.IsOk() );
176 CPPUNIT_ASSERT_EQUAL( 30, test.GetPointSize() );
177
178
179 // test Get/SetPixelSize()
180
181 test.SetPixelSize(wxSize(0,30));
182 CPPUNIT_ASSERT( test.IsOk() );
183 CPPUNIT_ASSERT( test.GetPixelSize().GetHeight() <= 30 );
184 // NOTE: the match found by SetPixelSize() may be not 100% precise; it
185 // only grants that a font smaller than the required height will
186 // be selected
187
188
189 // test Get/SetStyle()
190
191 test.SetStyle(wxFONTSTYLE_SLANT);
192 CPPUNIT_ASSERT( test.IsOk() );
193#ifdef __WXMSW__
194 // on wxMSW wxFONTSTYLE_SLANT==wxFONTSTYLE_ITALIC
195 CPPUNIT_ASSERT( wxFONTSTYLE_SLANT == test.GetStyle() ||
196 wxFONTSTYLE_ITALIC == test.GetStyle() );
197#else
198 CPPUNIT_ASSERT_EQUAL( wxFONTSTYLE_SLANT, test.GetStyle() );
199#endif
200
201 // test Get/SetUnderlined()
202
203 test.SetUnderlined(true);
204 CPPUNIT_ASSERT( test.IsOk() );
205 CPPUNIT_ASSERT_EQUAL( true, test.GetUnderlined() );
206
207
208 // test Get/SetWeight()
209
210 test.SetWeight(wxFONTWEIGHT_BOLD);
211 CPPUNIT_ASSERT( test.IsOk() );
212 CPPUNIT_ASSERT_EQUAL( wxFONTWEIGHT_BOLD, test.GetWeight() );
213 }
214}
215
216void FontTestCase::NativeFontInfo()
217{
218 unsigned numFonts;
219 const wxFont *pf = GetTestFonts(numFonts);
220 for ( size_t n = 0; n < numFonts; n++ )
221 {
222 wxFont test(*pf++);
223
5b06a80f
FM
224 const wxString& nid = test.GetNativeFontInfoDesc();
225 CPPUNIT_ASSERT( !nid.empty() );
14a83cbf 226 // documented to be never empty
c56fc0dc 227
5b06a80f 228 wxFont temp;
14a83cbf
FM
229 CPPUNIT_ASSERT( temp.SetNativeFontInfo(nid) );
230 CPPUNIT_ASSERT( temp.IsOk() );
c56fc0dc 231 WX_ASSERT_MESSAGE(
aeecbca0
FM
232 ("Test #%lu failed\ndump of test font: \"%s\"\ndump of temp font: \"%s\"", \
233 n, DumpFont(&test), DumpFont(&temp)),
9b5e0a6d 234 temp == test );
c56fc0dc
VZ
235 }
236
237 // test that clearly invalid font info strings do not work
238 wxFont font;
239 CPPUNIT_ASSERT( !font.SetNativeFontInfo("") );
541aafe2
VZ
240
241 // pango_font_description_from_string() used by wxFont in wxGTK and wxX11
242 // never returns an error at all so this assertion fails there -- and as it
243 // doesn't seem to be possible to do anything about it maybe we should
244 // change wxMSW and other ports to also accept any strings?
245#if !defined(__WXGTK__) && !defined(__WXX11__)
c56fc0dc 246 CPPUNIT_ASSERT( !font.SetNativeFontInfo("bloordyblop") );
541aafe2 247#endif
c56fc0dc
VZ
248}
249
250void FontTestCase::NativeFontInfoUserDesc()
251{
252 unsigned numFonts;
253 const wxFont *pf = GetTestFonts(numFonts);
254 for ( size_t n = 0; n < numFonts; n++ )
255 {
256 wxFont test(*pf++);
14a83cbf 257
5b06a80f
FM
258 const wxString& niud = test.GetNativeFontInfoUserDesc();
259 CPPUNIT_ASSERT( !niud.empty() );
14a83cbf 260 // documented to be never empty
c56fc0dc 261
5b06a80f 262 wxFont temp2;
14a83cbf
FM
263 CPPUNIT_ASSERT( temp2.SetNativeFontInfoUserDesc(niud) );
264 CPPUNIT_ASSERT( temp2.IsOk() );
265
266#ifdef __WXGTK__
267 // Pango saves/restores all font info in the user-friendly string:
c56fc0dc 268 WX_ASSERT_MESSAGE(
6aafb945
FM
269 ("Test #%lu failed; native info user desc was \"%s\" for test and \"%s\" for temp2", \
270 n, niud, temp2.GetNativeFontInfoUserDesc()),
9b5e0a6d 271 temp2 == test );
14a83cbf
FM
272#else
273 // NOTE: as documented GetNativeFontInfoUserDesc/SetNativeFontInfoUserDesc
274 // are not granted to save/restore all font info.
275 // In fact e.g. the font family is not saved at all; test only those
276 // info which GetNativeFontInfoUserDesc() does indeed save:
277 CPPUNIT_ASSERT_EQUAL( test.GetWeight(), temp2.GetWeight() );
278 CPPUNIT_ASSERT_EQUAL( test.GetStyle(), temp2.GetStyle() );
14a83cbf 279
c56fc0dc
VZ
280 // if the original face name was empty, it means that any face name (in
281 // this family) can be used for the new font so we shouldn't be
282 // surprised to find that they differ in this case
283 const wxString facename = test.GetFaceName();
284 if ( !facename.empty() )
285 {
286 CPPUNIT_ASSERT_EQUAL( facename.Upper(), temp2.GetFaceName().Upper() );
287 }
14a83cbf 288
c56fc0dc
VZ
289 CPPUNIT_ASSERT_EQUAL( test.GetPointSize(), temp2.GetPointSize() );
290 CPPUNIT_ASSERT_EQUAL( test.GetEncoding(), temp2.GetEncoding() );
14a83cbf 291#endif
5b06a80f
FM
292 }
293}
294
295#endif // wxUSE_FONTMAP