]>
git.saurik.com Git - wxWidgets.git/blob - tests/font/fonttest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/font/fonttest.cpp
3 // Purpose: wxFont unit test
4 // Author: Francesco Montorsi
6 // Copyright: (c) 2009 Francesco Montorsi
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
28 #include "asserthelper.h"
30 // ----------------------------------------------------------------------------
32 // ----------------------------------------------------------------------------
34 class FontTestCase
: public CppUnit::TestCase
40 CPPUNIT_TEST_SUITE( FontTestCase
);
41 CPPUNIT_TEST( Construct
);
42 CPPUNIT_TEST( GetSet
);
43 CPPUNIT_TEST( NativeFontInfo
);
44 CPPUNIT_TEST( NativeFontInfoUserDesc
);
45 CPPUNIT_TEST_SUITE_END();
49 void NativeFontInfo();
50 void NativeFontInfoUserDesc();
52 static const wxFont
*GetTestFonts(unsigned& numFonts
)
54 static const wxFont testfonts
[] =
60 wxFont(5, wxFONTFAMILY_TELETYPE
, wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
)
63 numFonts
= WXSIZEOF(testfonts
);
68 DECLARE_NO_COPY_CLASS(FontTestCase
)
71 // register in the unnamed registry so that these tests are run by default
72 CPPUNIT_TEST_SUITE_REGISTRATION( FontTestCase
);
74 // also include in its own registry so that these tests can be run alone
75 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FontTestCase
, "FontTestCase" );
77 wxString
DumpFont(const wxFont
*font
)
79 // dumps the internal properties of a wxFont in the same order they
80 // are checked by wxFontBase::operator==()
82 wxASSERT(font
->IsOk());
85 s
.Printf(wxS("%d-%d;%d-%d-%d-%d-%d-%s-%d"),
87 font
->GetPixelSize().x
,
88 font
->GetPixelSize().y
,
92 font
->GetUnderlined() ? 1 : 0,
99 void FontTestCase::Construct()
101 // The main purpose of this test is to verify that the font ctors below
102 // compile because it's easy to introduce ambiguities due to the number of
103 // overloaded wxFont ctors.
105 CPPUNIT_ASSERT( wxFont(10, wxFONTFAMILY_DEFAULT
,
107 wxFONTWEIGHT_NORMAL
).IsOk() );
109 #if FUTURE_WXWIN_COMPATIBILITY_3_0
110 // Tests relying on the soon-to-be-deprecated ctor taking ints and not
111 // wxFontXXX enum elements.
112 CPPUNIT_ASSERT( wxFont(10, wxDEFAULT
, wxNORMAL
, wxNORMAL
).IsOk() );
113 #endif // FUTURE_WXWIN_COMPATIBILITY_3_0
116 void FontTestCase::GetSet()
119 const wxFont
*pf
= GetTestFonts(numFonts
);
120 for ( unsigned n
= 0; n
< numFonts
; n
++ )
124 // remember: getters can only be called when wxFont::IsOk() == true
125 CPPUNIT_ASSERT( test
.IsOk() );
128 // test Get/SetFaceName()
129 CPPUNIT_ASSERT( !test
.SetFaceName("a dummy face name") );
130 CPPUNIT_ASSERT( !test
.IsOk() );
132 // if the call to SetFaceName() below fails on your system/port,
133 // consider adding another branch to this #if
134 #if defined(__WXMSW__) || defined(__WXOSX__)
135 static const char *knownGoodFaceName
= "Arial";
137 static const char *knownGoodFaceName
= "Monospace";
142 ("failed to set face name \"%s\" for test font #%u\n"
143 "(this failure is harmless if this face name is not "
144 "available on this system)", knownGoodFaceName
, n
),
145 test
.SetFaceName(knownGoodFaceName
)
147 CPPUNIT_ASSERT( test
.IsOk() );
150 // test Get/SetFamily()
152 test
.SetFamily( wxFONTFAMILY_ROMAN
);
153 CPPUNIT_ASSERT( test
.IsOk() );
155 // note that there is always the possibility that GetFamily() returns
156 // wxFONTFAMILY_DEFAULT (meaning "unknown" in this case) so that we
157 // consider it as a valid return value
158 const wxFontFamily family
= test
.GetFamily();
159 if ( family
!= wxFONTFAMILY_DEFAULT
)
160 CPPUNIT_ASSERT_EQUAL( wxFONTFAMILY_ROMAN
, family
);
163 // test Get/SetEncoding()
165 //test.SetEncoding( wxFONTENCODING_KOI8 );
166 //CPPUNIT_ASSERT( test.IsOk() );
167 //CPPUNIT_ASSERT_EQUAL( wxFONTENCODING_KOI8 , test.GetEncoding() );
170 // test Get/SetPointSize()
172 test
.SetPointSize(30);
173 CPPUNIT_ASSERT( test
.IsOk() );
174 CPPUNIT_ASSERT_EQUAL( 30, test
.GetPointSize() );
177 // test Get/SetPixelSize()
179 test
.SetPixelSize(wxSize(0,30));
180 CPPUNIT_ASSERT( test
.IsOk() );
181 CPPUNIT_ASSERT( test
.GetPixelSize().GetHeight() <= 30 );
182 // NOTE: the match found by SetPixelSize() may be not 100% precise; it
183 // only grants that a font smaller than the required height will
187 // test Get/SetStyle()
189 test
.SetStyle(wxFONTSTYLE_SLANT
);
190 CPPUNIT_ASSERT( test
.IsOk() );
192 // on wxMSW wxFONTSTYLE_SLANT==wxFONTSTYLE_ITALIC
193 CPPUNIT_ASSERT( wxFONTSTYLE_SLANT
== test
.GetStyle() ||
194 wxFONTSTYLE_ITALIC
== test
.GetStyle() );
196 CPPUNIT_ASSERT_EQUAL( wxFONTSTYLE_SLANT
, test
.GetStyle() );
199 // test Get/SetUnderlined()
201 test
.SetUnderlined(true);
202 CPPUNIT_ASSERT( test
.IsOk() );
203 CPPUNIT_ASSERT_EQUAL( true, test
.GetUnderlined() );
205 // test Get/SetStrikethrough()
207 // Strike through support not implemented in wxOSX currently.
209 test
.SetStrikethrough(true);
210 CPPUNIT_ASSERT( test
.IsOk() );
211 CPPUNIT_ASSERT_EQUAL( true, test
.GetStrikethrough() );
215 // test Get/SetWeight()
217 test
.SetWeight(wxFONTWEIGHT_BOLD
);
218 CPPUNIT_ASSERT( test
.IsOk() );
219 CPPUNIT_ASSERT_EQUAL( wxFONTWEIGHT_BOLD
, test
.GetWeight() );
223 void FontTestCase::NativeFontInfo()
226 const wxFont
*pf
= GetTestFonts(numFonts
);
227 for ( size_t n
= 0; n
< numFonts
; n
++ )
231 const wxString
& nid
= test
.GetNativeFontInfoDesc();
232 CPPUNIT_ASSERT( !nid
.empty() );
233 // documented to be never empty
236 CPPUNIT_ASSERT( temp
.SetNativeFontInfo(nid
) );
237 CPPUNIT_ASSERT( temp
.IsOk() );
239 ("Test #%lu failed\ndump of test font: \"%s\"\ndump of temp font: \"%s\"", \
240 n
, DumpFont(&test
), DumpFont(&temp
)),
244 // test that clearly invalid font info strings do not work
246 CPPUNIT_ASSERT( !font
.SetNativeFontInfo("") );
248 // pango_font_description_from_string() used by wxFont in wxGTK and wxX11
249 // never returns an error at all so this assertion fails there -- and as it
250 // doesn't seem to be possible to do anything about it maybe we should
251 // change wxMSW and other ports to also accept any strings?
252 #if !defined(__WXGTK__) && !defined(__WXX11__)
253 CPPUNIT_ASSERT( !font
.SetNativeFontInfo("bloordyblop") );
256 // Pango font description doesn't have 'underlined' and 'strikethrough'
257 // attributes, so wxNativeFontInfo implements these itself. Test if these
258 // are properly preserved by wxNativeFontInfo or its string description.
259 font
.SetUnderlined(true);
260 font
.SetStrikethrough(true);
261 CPPUNIT_ASSERT_EQUAL(font
, wxFont(font
));
262 CPPUNIT_ASSERT_EQUAL(font
, wxFont(*font
.GetNativeFontInfo()));
263 CPPUNIT_ASSERT_EQUAL(font
, wxFont(font
.GetNativeFontInfoDesc()));
264 font
.SetUnderlined(false);
265 CPPUNIT_ASSERT_EQUAL(font
, wxFont(font
));
266 CPPUNIT_ASSERT_EQUAL(font
, wxFont(*font
.GetNativeFontInfo()));
267 CPPUNIT_ASSERT_EQUAL(font
, wxFont(font
.GetNativeFontInfoDesc()));
268 font
.SetUnderlined(true);
269 font
.SetStrikethrough(false);
270 CPPUNIT_ASSERT_EQUAL(font
, wxFont(font
));
271 CPPUNIT_ASSERT_EQUAL(font
, wxFont(*font
.GetNativeFontInfo()));
272 CPPUNIT_ASSERT_EQUAL(font
, wxFont(font
.GetNativeFontInfoDesc()));
273 // note: the GetNativeFontInfoUserDesc() doesn't preserve all attributes
274 // according to docs, so it is not tested.
277 void FontTestCase::NativeFontInfoUserDesc()
280 const wxFont
*pf
= GetTestFonts(numFonts
);
281 for ( size_t n
= 0; n
< numFonts
; n
++ )
285 const wxString
& niud
= test
.GetNativeFontInfoUserDesc();
286 CPPUNIT_ASSERT( !niud
.empty() );
287 // documented to be never empty
290 CPPUNIT_ASSERT( temp2
.SetNativeFontInfoUserDesc(niud
) );
291 CPPUNIT_ASSERT( temp2
.IsOk() );
294 // Pango saves/restores all font info in the user-friendly string:
296 ("Test #%lu failed; native info user desc was \"%s\" for test and \"%s\" for temp2", \
297 n
, niud
, temp2
.GetNativeFontInfoUserDesc()),
300 // NOTE: as documented GetNativeFontInfoUserDesc/SetNativeFontInfoUserDesc
301 // are not granted to save/restore all font info.
302 // In fact e.g. the font family is not saved at all; test only those
303 // info which GetNativeFontInfoUserDesc() does indeed save:
304 CPPUNIT_ASSERT_EQUAL( test
.GetWeight(), temp2
.GetWeight() );
305 CPPUNIT_ASSERT_EQUAL( test
.GetStyle(), temp2
.GetStyle() );
307 // if the original face name was empty, it means that any face name (in
308 // this family) can be used for the new font so we shouldn't be
309 // surprised to find that they differ in this case
310 const wxString facename
= test
.GetFaceName();
311 if ( !facename
.empty() )
313 CPPUNIT_ASSERT_EQUAL( facename
.Upper(), temp2
.GetFaceName().Upper() );
316 CPPUNIT_ASSERT_EQUAL( test
.GetPointSize(), temp2
.GetPointSize() );
317 CPPUNIT_ASSERT_EQUAL( test
.GetEncoding(), temp2
.GetEncoding() );
322 #endif // wxUSE_FONTMAP