]>
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
7 // Copyright: (c) 2009 Francesco Montorsi
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ----------------------------------------------------------------------------
13 // ----------------------------------------------------------------------------
29 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
33 class FontTestCase
: public CppUnit::TestCase
39 CPPUNIT_TEST_SUITE( FontTestCase
);
40 CPPUNIT_TEST( Construct
);
41 CPPUNIT_TEST( GetSet
);
42 CPPUNIT_TEST( NativeFontInfo
);
43 CPPUNIT_TEST( NativeFontInfoUserDesc
);
44 CPPUNIT_TEST_SUITE_END();
48 void NativeFontInfo();
49 void NativeFontInfoUserDesc();
51 static const wxFont
*GetTestFonts(unsigned& numFonts
)
53 static const wxFont testfonts
[] =
59 wxFont(5, wxFONTFAMILY_TELETYPE
, wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
)
62 numFonts
= WXSIZEOF(testfonts
);
67 DECLARE_NO_COPY_CLASS(FontTestCase
)
70 // register in the unnamed registry so that these tests are run by default
71 CPPUNIT_TEST_SUITE_REGISTRATION( FontTestCase
);
73 // also include in its own registry so that these tests can be run alone
74 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FontTestCase
, "FontTestCase" );
76 wxString
DumpFont(const wxFont
*font
)
78 // dumps the internal properties of a wxFont in the same order they
79 // are checked by wxFontBase::operator==()
81 wxASSERT(font
->IsOk());
84 s
.Printf(wxS("%d-%d;%d-%d-%d-%d-%d-%s-%d"),
86 font
->GetPixelSize().x
,
87 font
->GetPixelSize().y
,
91 font
->GetUnderlined() ? 1 : 0,
98 void FontTestCase::Construct()
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.
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
,
109 wxFONTWEIGHT_NORMAL
).IsOk() );
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
118 void FontTestCase::GetSet()
121 const wxFont
*pf
= GetTestFonts(numFonts
);
122 for ( unsigned n
= 0; n
< numFonts
; n
++ )
126 // remember: getters can only be called when wxFont::IsOk() == true
127 CPPUNIT_ASSERT( test
.IsOk() );
130 // test Get/SetFaceName()
131 CPPUNIT_ASSERT( !test
.SetFaceName("a dummy face name") );
132 CPPUNIT_ASSERT( !test
.IsOk() );
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";
139 static const char *knownGoodFaceName
= "Monospace";
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
)
149 CPPUNIT_ASSERT( test
.IsOk() );
152 // test Get/SetFamily()
154 test
.SetFamily( wxFONTFAMILY_ROMAN
);
155 CPPUNIT_ASSERT( test
.IsOk() );
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
);
165 // test Get/SetEncoding()
167 //test.SetEncoding( wxFONTENCODING_KOI8 );
168 //CPPUNIT_ASSERT( test.IsOk() );
169 //CPPUNIT_ASSERT_EQUAL( wxFONTENCODING_KOI8 , test.GetEncoding() );
172 // test Get/SetPointSize()
174 test
.SetPointSize(30);
175 CPPUNIT_ASSERT( test
.IsOk() );
176 CPPUNIT_ASSERT_EQUAL( 30, test
.GetPointSize() );
179 // test Get/SetPixelSize()
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
189 // test Get/SetStyle()
191 test
.SetStyle(wxFONTSTYLE_SLANT
);
192 CPPUNIT_ASSERT( test
.IsOk() );
194 // on wxMSW wxFONTSTYLE_SLANT==wxFONTSTYLE_ITALIC
195 CPPUNIT_ASSERT( wxFONTSTYLE_SLANT
== test
.GetStyle() ||
196 wxFONTSTYLE_ITALIC
== test
.GetStyle() );
198 CPPUNIT_ASSERT_EQUAL( wxFONTSTYLE_SLANT
, test
.GetStyle() );
201 // test Get/SetUnderlined()
203 test
.SetUnderlined(true);
204 CPPUNIT_ASSERT( test
.IsOk() );
205 CPPUNIT_ASSERT_EQUAL( true, test
.GetUnderlined() );
208 // test Get/SetWeight()
210 test
.SetWeight(wxFONTWEIGHT_BOLD
);
211 CPPUNIT_ASSERT( test
.IsOk() );
212 CPPUNIT_ASSERT_EQUAL( wxFONTWEIGHT_BOLD
, test
.GetWeight() );
216 void FontTestCase::NativeFontInfo()
219 const wxFont
*pf
= GetTestFonts(numFonts
);
220 for ( size_t n
= 0; n
< numFonts
; n
++ )
224 const wxString
& nid
= test
.GetNativeFontInfoDesc();
225 CPPUNIT_ASSERT( !nid
.empty() );
226 // documented to be never empty
229 CPPUNIT_ASSERT( temp
.SetNativeFontInfo(nid
) );
230 CPPUNIT_ASSERT( temp
.IsOk() );
232 ("Test #%lu failed\ndump of test font: \"%s\"\ndump of temp font: \"%s\"", \
233 n
, DumpFont(&test
), DumpFont(&temp
)),
237 // test that clearly invalid font info strings do not work
239 CPPUNIT_ASSERT( !font
.SetNativeFontInfo("") );
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__)
246 CPPUNIT_ASSERT( !font
.SetNativeFontInfo("bloordyblop") );
250 void FontTestCase::NativeFontInfoUserDesc()
253 const wxFont
*pf
= GetTestFonts(numFonts
);
254 for ( size_t n
= 0; n
< numFonts
; n
++ )
258 const wxString
& niud
= test
.GetNativeFontInfoUserDesc();
259 CPPUNIT_ASSERT( !niud
.empty() );
260 // documented to be never empty
263 CPPUNIT_ASSERT( temp2
.SetNativeFontInfoUserDesc(niud
) );
264 CPPUNIT_ASSERT( temp2
.IsOk() );
267 // Pango saves/restores all font info in the user-friendly string:
269 ("Test #%lu failed; native info user desc was \"%s\" for test and \"%s\" for temp2", \
270 n
, niud
, temp2
.GetNativeFontInfoUserDesc()),
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() );
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() )
286 CPPUNIT_ASSERT_EQUAL( facename
.Upper(), temp2
.GetFaceName().Upper() );
289 CPPUNIT_ASSERT_EQUAL( test
.GetPointSize(), temp2
.GetPointSize() );
290 CPPUNIT_ASSERT_EQUAL( test
.GetEncoding(), temp2
.GetEncoding() );
295 #endif // wxUSE_FONTMAP