]>
git.saurik.com Git - wxWidgets.git/blob - tests/font/fonttest.cpp
2e9c3ebf9e4cb02502f4646727d3b9b003dba44b
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( GetSet
);
41 CPPUNIT_TEST( NativeFontInfo
);
42 CPPUNIT_TEST( NativeFontInfoUserDesc
);
43 CPPUNIT_TEST_SUITE_END();
46 void NativeFontInfo();
47 void NativeFontInfoUserDesc();
49 static const wxFont
*GetTestFonts(unsigned& numFonts
)
51 static const wxFont testfonts
[] =
57 wxFont(5, wxFONTFAMILY_TELETYPE
, wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
)
60 numFonts
= WXSIZEOF(testfonts
);
65 DECLARE_NO_COPY_CLASS(FontTestCase
)
68 // register in the unnamed registry so that these tests are run by default
69 CPPUNIT_TEST_SUITE_REGISTRATION( FontTestCase
);
71 // also include in it's own registry so that these tests can be run alone
72 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FontTestCase
, "FontTestCase" );
74 wxString
DumpFont(const wxFont
*font
)
76 // dumps the internal properties of a wxFont in the same order they
77 // are checked by wxFontBase::operator==()
79 wxASSERT(font
->IsOk());
82 s
.Printf(wxS("%d-%d;%d-%d-%d-%d-%d-%s-%d"),
84 font
->GetPixelSize().x
,
85 font
->GetPixelSize().y
,
89 font
->GetUnderlined() ? 1 : 0,
96 void FontTestCase::GetSet()
99 const wxFont
*pf
= GetTestFonts(numFonts
);
100 for ( size_t n
= 0; n
< numFonts
; n
++ )
104 // remember: getters can only be called when wxFont::IsOk() == true
105 CPPUNIT_ASSERT( test
.IsOk() );
108 // test Get/SetFaceName()
109 CPPUNIT_ASSERT( !test
.SetFaceName("a dummy face name") );
110 CPPUNIT_ASSERT( !test
.IsOk() );
112 CPPUNIT_ASSERT( test
.SetFaceName("Arial") );
113 CPPUNIT_ASSERT( test
.IsOk() );
116 // test Get/SetFamily()
118 test
.SetFamily( wxFONTFAMILY_ROMAN
);
119 CPPUNIT_ASSERT( test
.IsOk() );
120 CPPUNIT_ASSERT( wxFONTFAMILY_ROMAN
== test
.GetFamily() ||
121 wxFONTFAMILY_UNKNOWN
== test
.GetFamily() );
122 // note that there is always the possibility that GetFamily() returns
123 // wxFONTFAMILY_UNKNOWN so that we consider it as a valid return value
126 // test Get/SetEncoding()
128 //test.SetEncoding( wxFONTENCODING_KOI8 );
129 //CPPUNIT_ASSERT( test.IsOk() );
130 //CPPUNIT_ASSERT_EQUAL( wxFONTENCODING_KOI8 , test.GetEncoding() );
133 // test Get/SetPointSize()
135 test
.SetPointSize(30);
136 CPPUNIT_ASSERT( test
.IsOk() );
137 CPPUNIT_ASSERT_EQUAL( 30, test
.GetPointSize() );
140 // test Get/SetPixelSize()
142 test
.SetPixelSize(wxSize(0,30));
143 CPPUNIT_ASSERT( test
.IsOk() );
144 CPPUNIT_ASSERT( test
.GetPixelSize().GetHeight() <= 30 );
145 // NOTE: the match found by SetPixelSize() may be not 100% precise; it
146 // only grants that a font smaller than the required height will
150 // test Get/SetStyle()
152 test
.SetStyle(wxFONTSTYLE_SLANT
);
153 CPPUNIT_ASSERT( test
.IsOk() );
155 // on wxMSW wxFONTSTYLE_SLANT==wxFONTSTYLE_ITALIC
156 CPPUNIT_ASSERT( wxFONTSTYLE_SLANT
== test
.GetStyle() ||
157 wxFONTSTYLE_ITALIC
== test
.GetStyle() );
159 CPPUNIT_ASSERT_EQUAL( wxFONTSTYLE_SLANT
, test
.GetStyle() );
162 // test Get/SetUnderlined()
164 test
.SetUnderlined(true);
165 CPPUNIT_ASSERT( test
.IsOk() );
166 CPPUNIT_ASSERT_EQUAL( true, test
.GetUnderlined() );
169 // test Get/SetWeight()
171 test
.SetWeight(wxFONTWEIGHT_BOLD
);
172 CPPUNIT_ASSERT( test
.IsOk() );
173 CPPUNIT_ASSERT_EQUAL( wxFONTWEIGHT_BOLD
, test
.GetWeight() );
177 void FontTestCase::NativeFontInfo()
180 const wxFont
*pf
= GetTestFonts(numFonts
);
181 for ( size_t n
= 0; n
< numFonts
; n
++ )
185 const wxString
& nid
= test
.GetNativeFontInfoDesc();
186 CPPUNIT_ASSERT( !nid
.empty() );
187 // documented to be never empty
190 CPPUNIT_ASSERT( temp
.SetNativeFontInfo(nid
) );
191 CPPUNIT_ASSERT( temp
.IsOk() );
193 ("Test #%lu failed\ndump of test font: \"%s\"\ndump of temp font: \"%s\"", \
194 n
, DumpFont(&test
), DumpFont(&temp
)),
198 // test that clearly invalid font info strings do not work
200 CPPUNIT_ASSERT( !font
.SetNativeFontInfo("") );
201 CPPUNIT_ASSERT( !font
.SetNativeFontInfo("bloordyblop") );
204 void FontTestCase::NativeFontInfoUserDesc()
207 const wxFont
*pf
= GetTestFonts(numFonts
);
208 for ( size_t n
= 0; n
< numFonts
; n
++ )
212 const wxString
& niud
= test
.GetNativeFontInfoUserDesc();
213 CPPUNIT_ASSERT( !niud
.empty() );
214 // documented to be never empty
217 CPPUNIT_ASSERT( temp2
.SetNativeFontInfoUserDesc(niud
) );
218 CPPUNIT_ASSERT( temp2
.IsOk() );
221 // Pango saves/restores all font info in the user-friendly string:
223 ("Test #%lu failed; native info user desc was \"%s\" for test and \"%s\" for temp2", \
224 n
, niud
, temp2
.GetNativeFontInfoUserDesc()),
227 // NOTE: as documented GetNativeFontInfoUserDesc/SetNativeFontInfoUserDesc
228 // are not granted to save/restore all font info.
229 // In fact e.g. the font family is not saved at all; test only those
230 // info which GetNativeFontInfoUserDesc() does indeed save:
231 CPPUNIT_ASSERT_EQUAL( test
.GetWeight(), temp2
.GetWeight() );
232 CPPUNIT_ASSERT_EQUAL( test
.GetStyle(), temp2
.GetStyle() );
234 // if the original face name was empty, it means that any face name (in
235 // this family) can be used for the new font so we shouldn't be
236 // surprised to find that they differ in this case
237 const wxString facename
= test
.GetFaceName();
238 if ( !facename
.empty() )
240 CPPUNIT_ASSERT_EQUAL( facename
.Upper(), temp2
.GetFaceName().Upper() );
243 CPPUNIT_ASSERT_EQUAL( test
.GetPointSize(), temp2
.GetPointSize() );
244 CPPUNIT_ASSERT_EQUAL( test
.GetEncoding(), temp2
.GetEncoding() );
249 #endif // wxUSE_FONTMAP