use a different known good face name under non-MSW
[wxWidgets.git] / tests / font / fonttest.cpp
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
33 class FontTestCase : public CppUnit::TestCase
34 {
35 public:
36 FontTestCase() { }
37
38 private:
39 CPPUNIT_TEST_SUITE( FontTestCase );
40 CPPUNIT_TEST( GetSet );
41 CPPUNIT_TEST( NativeFontInfo );
42 CPPUNIT_TEST( NativeFontInfoUserDesc );
43 CPPUNIT_TEST_SUITE_END();
44
45 void GetSet();
46 void NativeFontInfo();
47 void NativeFontInfoUserDesc();
48
49 static const wxFont *GetTestFonts(unsigned& numFonts)
50 {
51 static const wxFont testfonts[] =
52 {
53 *wxNORMAL_FONT,
54 *wxSMALL_FONT,
55 *wxITALIC_FONT,
56 *wxSWISS_FONT,
57 wxFont(5, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL)
58 };
59
60 numFonts = WXSIZEOF(testfonts);
61
62 return testfonts;
63 }
64
65 DECLARE_NO_COPY_CLASS(FontTestCase)
66 };
67
68 // register in the unnamed registry so that these tests are run by default
69 CPPUNIT_TEST_SUITE_REGISTRATION( FontTestCase );
70
71 // also include in it's own registry so that these tests can be run alone
72 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FontTestCase, "FontTestCase" );
73
74 wxString DumpFont(const wxFont *font)
75 {
76 // dumps the internal properties of a wxFont in the same order they
77 // are checked by wxFontBase::operator==()
78
79 wxASSERT(font->IsOk());
80
81 wxString s;
82 s.Printf(wxS("%d-%d;%d-%d-%d-%d-%d-%s-%d"),
83 font->GetPointSize(),
84 font->GetPixelSize().x,
85 font->GetPixelSize().y,
86 font->GetFamily(),
87 font->GetStyle(),
88 font->GetWeight(),
89 font->GetUnderlined() ? 1 : 0,
90 font->GetFaceName(),
91 font->GetEncoding());
92
93 return s;
94 }
95
96 void FontTestCase::GetSet()
97 {
98 unsigned numFonts;
99 const wxFont *pf = GetTestFonts(numFonts);
100 for ( size_t n = 0; n < numFonts; n++ )
101 {
102 wxFont test(*pf++);
103
104 // remember: getters can only be called when wxFont::IsOk() == true
105 CPPUNIT_ASSERT( test.IsOk() );
106
107
108 // test Get/SetFaceName()
109 CPPUNIT_ASSERT( !test.SetFaceName("a dummy face name") );
110 CPPUNIT_ASSERT( !test.IsOk() );
111
112 // if the call to SetFaceName() below fails on your system/port,
113 // consider adding another branch to this #if
114 #if defined(__WXMSW__) || defined(__WXOSX__)
115 static const char *knownGoodFaceName = "Arial";
116 #else
117 static const char *knownGoodFaceName = "Fixed";
118 #endif
119
120 CPPUNIT_ASSERT( test.SetFaceName(knownGoodFaceName) );
121 CPPUNIT_ASSERT( test.IsOk() );
122
123
124 // test Get/SetFamily()
125
126 test.SetFamily( wxFONTFAMILY_ROMAN );
127 CPPUNIT_ASSERT( test.IsOk() );
128 CPPUNIT_ASSERT( wxFONTFAMILY_ROMAN == test.GetFamily() ||
129 wxFONTFAMILY_UNKNOWN == test.GetFamily() );
130 // note that there is always the possibility that GetFamily() returns
131 // wxFONTFAMILY_UNKNOWN so that we consider it as a valid return value
132
133
134 // test Get/SetEncoding()
135
136 //test.SetEncoding( wxFONTENCODING_KOI8 );
137 //CPPUNIT_ASSERT( test.IsOk() );
138 //CPPUNIT_ASSERT_EQUAL( wxFONTENCODING_KOI8 , test.GetEncoding() );
139
140
141 // test Get/SetPointSize()
142
143 test.SetPointSize(30);
144 CPPUNIT_ASSERT( test.IsOk() );
145 CPPUNIT_ASSERT_EQUAL( 30, test.GetPointSize() );
146
147
148 // test Get/SetPixelSize()
149
150 test.SetPixelSize(wxSize(0,30));
151 CPPUNIT_ASSERT( test.IsOk() );
152 CPPUNIT_ASSERT( test.GetPixelSize().GetHeight() <= 30 );
153 // NOTE: the match found by SetPixelSize() may be not 100% precise; it
154 // only grants that a font smaller than the required height will
155 // be selected
156
157
158 // test Get/SetStyle()
159
160 test.SetStyle(wxFONTSTYLE_SLANT);
161 CPPUNIT_ASSERT( test.IsOk() );
162 #ifdef __WXMSW__
163 // on wxMSW wxFONTSTYLE_SLANT==wxFONTSTYLE_ITALIC
164 CPPUNIT_ASSERT( wxFONTSTYLE_SLANT == test.GetStyle() ||
165 wxFONTSTYLE_ITALIC == test.GetStyle() );
166 #else
167 CPPUNIT_ASSERT_EQUAL( wxFONTSTYLE_SLANT, test.GetStyle() );
168 #endif
169
170 // test Get/SetUnderlined()
171
172 test.SetUnderlined(true);
173 CPPUNIT_ASSERT( test.IsOk() );
174 CPPUNIT_ASSERT_EQUAL( true, test.GetUnderlined() );
175
176
177 // test Get/SetWeight()
178
179 test.SetWeight(wxFONTWEIGHT_BOLD);
180 CPPUNIT_ASSERT( test.IsOk() );
181 CPPUNIT_ASSERT_EQUAL( wxFONTWEIGHT_BOLD, test.GetWeight() );
182 }
183 }
184
185 void FontTestCase::NativeFontInfo()
186 {
187 unsigned numFonts;
188 const wxFont *pf = GetTestFonts(numFonts);
189 for ( size_t n = 0; n < numFonts; n++ )
190 {
191 wxFont test(*pf++);
192
193 const wxString& nid = test.GetNativeFontInfoDesc();
194 CPPUNIT_ASSERT( !nid.empty() );
195 // documented to be never empty
196
197 wxFont temp;
198 CPPUNIT_ASSERT( temp.SetNativeFontInfo(nid) );
199 CPPUNIT_ASSERT( temp.IsOk() );
200 WX_ASSERT_MESSAGE(
201 ("Test #%lu failed\ndump of test font: \"%s\"\ndump of temp font: \"%s\"", \
202 n, DumpFont(&test), DumpFont(&temp)),
203 temp == test );
204 }
205
206 // test that clearly invalid font info strings do not work
207 wxFont font;
208 CPPUNIT_ASSERT( !font.SetNativeFontInfo("") );
209 CPPUNIT_ASSERT( !font.SetNativeFontInfo("bloordyblop") );
210 }
211
212 void FontTestCase::NativeFontInfoUserDesc()
213 {
214 unsigned numFonts;
215 const wxFont *pf = GetTestFonts(numFonts);
216 for ( size_t n = 0; n < numFonts; n++ )
217 {
218 wxFont test(*pf++);
219
220 const wxString& niud = test.GetNativeFontInfoUserDesc();
221 CPPUNIT_ASSERT( !niud.empty() );
222 // documented to be never empty
223
224 wxFont temp2;
225 CPPUNIT_ASSERT( temp2.SetNativeFontInfoUserDesc(niud) );
226 CPPUNIT_ASSERT( temp2.IsOk() );
227
228 #ifdef __WXGTK__
229 // Pango saves/restores all font info in the user-friendly string:
230 WX_ASSERT_MESSAGE(
231 ("Test #%lu failed; native info user desc was \"%s\" for test and \"%s\" for temp2", \
232 n, niud, temp2.GetNativeFontInfoUserDesc()),
233 temp2 == test );
234 #else
235 // NOTE: as documented GetNativeFontInfoUserDesc/SetNativeFontInfoUserDesc
236 // are not granted to save/restore all font info.
237 // In fact e.g. the font family is not saved at all; test only those
238 // info which GetNativeFontInfoUserDesc() does indeed save:
239 CPPUNIT_ASSERT_EQUAL( test.GetWeight(), temp2.GetWeight() );
240 CPPUNIT_ASSERT_EQUAL( test.GetStyle(), temp2.GetStyle() );
241
242 // if the original face name was empty, it means that any face name (in
243 // this family) can be used for the new font so we shouldn't be
244 // surprised to find that they differ in this case
245 const wxString facename = test.GetFaceName();
246 if ( !facename.empty() )
247 {
248 CPPUNIT_ASSERT_EQUAL( facename.Upper(), temp2.GetFaceName().Upper() );
249 }
250
251 CPPUNIT_ASSERT_EQUAL( test.GetPointSize(), temp2.GetPointSize() );
252 CPPUNIT_ASSERT_EQUAL( test.GetEncoding(), temp2.GetEncoding() );
253 #endif
254 }
255 }
256
257 #endif // wxUSE_FONTMAP