]> git.saurik.com Git - wxWidgets.git/blob - tests/font/fonttest.cpp
wxRTC: save and load the 'shown' status in case there's a situation where layout...
[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 // Copyright: (c) 2009 Francesco Montorsi
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "testprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #ifndef WX_PRECOMP
21 #include "wx/wx.h"
22 #endif // WX_PRECOMP
23
24 #if wxUSE_FONTMAP
25
26 #include "wx/font.h"
27
28 #include "asserthelper.h"
29
30 // ----------------------------------------------------------------------------
31 // test class
32 // ----------------------------------------------------------------------------
33
34 class FontTestCase : public CppUnit::TestCase
35 {
36 public:
37 FontTestCase() { }
38
39 private:
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();
46
47 void Construct();
48 void GetSet();
49 void NativeFontInfo();
50 void NativeFontInfoUserDesc();
51
52 static const wxFont *GetTestFonts(unsigned& numFonts)
53 {
54 static const wxFont testfonts[] =
55 {
56 *wxNORMAL_FONT,
57 *wxSMALL_FONT,
58 *wxITALIC_FONT,
59 *wxSWISS_FONT,
60 wxFont(5, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL)
61 };
62
63 numFonts = WXSIZEOF(testfonts);
64
65 return testfonts;
66 }
67
68 DECLARE_NO_COPY_CLASS(FontTestCase)
69 };
70
71 // register in the unnamed registry so that these tests are run by default
72 CPPUNIT_TEST_SUITE_REGISTRATION( FontTestCase );
73
74 // also include in its own registry so that these tests can be run alone
75 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FontTestCase, "FontTestCase" );
76
77 wxString DumpFont(const wxFont *font)
78 {
79 // dumps the internal properties of a wxFont in the same order they
80 // are checked by wxFontBase::operator==()
81
82 wxASSERT(font->IsOk());
83
84 wxString s;
85 s.Printf(wxS("%d-%d;%d-%d-%d-%d-%d-%s-%d"),
86 font->GetPointSize(),
87 font->GetPixelSize().x,
88 font->GetPixelSize().y,
89 font->GetFamily(),
90 font->GetStyle(),
91 font->GetWeight(),
92 font->GetUnderlined() ? 1 : 0,
93 font->GetFaceName(),
94 font->GetEncoding());
95
96 return s;
97 }
98
99 void FontTestCase::Construct()
100 {
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.
104
105 CPPUNIT_ASSERT( wxFont(10, wxFONTFAMILY_DEFAULT,
106 wxFONTSTYLE_NORMAL,
107 wxFONTWEIGHT_NORMAL).IsOk() );
108
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
114 }
115
116 void FontTestCase::GetSet()
117 {
118 unsigned numFonts;
119 const wxFont *pf = GetTestFonts(numFonts);
120 for ( unsigned n = 0; n < numFonts; n++ )
121 {
122 wxFont test(*pf++);
123
124 // remember: getters can only be called when wxFont::IsOk() == true
125 CPPUNIT_ASSERT( test.IsOk() );
126
127
128 // test Get/SetFaceName()
129 CPPUNIT_ASSERT( !test.SetFaceName("a dummy face name") );
130 CPPUNIT_ASSERT( !test.IsOk() );
131
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";
136 #else
137 static const char *knownGoodFaceName = "Monospace";
138 #endif
139
140 WX_ASSERT_MESSAGE
141 (
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)
146 );
147 CPPUNIT_ASSERT( test.IsOk() );
148
149
150 // test Get/SetFamily()
151
152 test.SetFamily( wxFONTFAMILY_ROMAN );
153 CPPUNIT_ASSERT( test.IsOk() );
154
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 );
161
162
163 // test Get/SetEncoding()
164
165 //test.SetEncoding( wxFONTENCODING_KOI8 );
166 //CPPUNIT_ASSERT( test.IsOk() );
167 //CPPUNIT_ASSERT_EQUAL( wxFONTENCODING_KOI8 , test.GetEncoding() );
168
169
170 // test Get/SetPointSize()
171
172 test.SetPointSize(30);
173 CPPUNIT_ASSERT( test.IsOk() );
174 CPPUNIT_ASSERT_EQUAL( 30, test.GetPointSize() );
175
176
177 // test Get/SetPixelSize()
178
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
184 // be selected
185
186
187 // test Get/SetStyle()
188
189 test.SetStyle(wxFONTSTYLE_SLANT);
190 CPPUNIT_ASSERT( test.IsOk() );
191 #ifdef __WXMSW__
192 // on wxMSW wxFONTSTYLE_SLANT==wxFONTSTYLE_ITALIC
193 CPPUNIT_ASSERT( wxFONTSTYLE_SLANT == test.GetStyle() ||
194 wxFONTSTYLE_ITALIC == test.GetStyle() );
195 #else
196 CPPUNIT_ASSERT_EQUAL( wxFONTSTYLE_SLANT, test.GetStyle() );
197 #endif
198
199 // test Get/SetUnderlined()
200
201 test.SetUnderlined(true);
202 CPPUNIT_ASSERT( test.IsOk() );
203 CPPUNIT_ASSERT_EQUAL( true, test.GetUnderlined() );
204
205 // test Get/SetStrikethrough()
206
207 // Strike through support not implemented in wxOSX currently.
208 #ifndef __WXOSX__
209 test.SetStrikethrough(true);
210 CPPUNIT_ASSERT( test.IsOk() );
211 CPPUNIT_ASSERT_EQUAL( true, test.GetStrikethrough() );
212 #endif // !__WXOSX__
213
214
215 // test Get/SetWeight()
216
217 test.SetWeight(wxFONTWEIGHT_BOLD);
218 CPPUNIT_ASSERT( test.IsOk() );
219 CPPUNIT_ASSERT_EQUAL( wxFONTWEIGHT_BOLD, test.GetWeight() );
220 }
221 }
222
223 void FontTestCase::NativeFontInfo()
224 {
225 unsigned numFonts;
226 const wxFont *pf = GetTestFonts(numFonts);
227 for ( size_t n = 0; n < numFonts; n++ )
228 {
229 wxFont test(*pf++);
230
231 const wxString& nid = test.GetNativeFontInfoDesc();
232 CPPUNIT_ASSERT( !nid.empty() );
233 // documented to be never empty
234
235 wxFont temp;
236 CPPUNIT_ASSERT( temp.SetNativeFontInfo(nid) );
237 CPPUNIT_ASSERT( temp.IsOk() );
238 WX_ASSERT_MESSAGE(
239 ("Test #%lu failed\ndump of test font: \"%s\"\ndump of temp font: \"%s\"", \
240 n, DumpFont(&test), DumpFont(&temp)),
241 temp == test );
242 }
243
244 // test that clearly invalid font info strings do not work
245 wxFont font;
246 CPPUNIT_ASSERT( !font.SetNativeFontInfo("") );
247
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") );
254 #endif
255
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.
275 }
276
277 void FontTestCase::NativeFontInfoUserDesc()
278 {
279 unsigned numFonts;
280 const wxFont *pf = GetTestFonts(numFonts);
281 for ( size_t n = 0; n < numFonts; n++ )
282 {
283 wxFont test(*pf++);
284
285 const wxString& niud = test.GetNativeFontInfoUserDesc();
286 CPPUNIT_ASSERT( !niud.empty() );
287 // documented to be never empty
288
289 wxFont temp2;
290 CPPUNIT_ASSERT( temp2.SetNativeFontInfoUserDesc(niud) );
291 CPPUNIT_ASSERT( temp2.IsOk() );
292
293 #ifdef __WXGTK__
294 // Pango saves/restores all font info in the user-friendly string:
295 WX_ASSERT_MESSAGE(
296 ("Test #%lu failed; native info user desc was \"%s\" for test and \"%s\" for temp2", \
297 n, niud, temp2.GetNativeFontInfoUserDesc()),
298 temp2 == test );
299 #else
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() );
306
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() )
312 {
313 CPPUNIT_ASSERT_EQUAL( facename.Upper(), temp2.GetFaceName().Upper() );
314 }
315
316 CPPUNIT_ASSERT_EQUAL( test.GetPointSize(), temp2.GetPointSize() );
317 CPPUNIT_ASSERT_EQUAL( test.GetEncoding(), temp2.GetEncoding() );
318 #endif
319 }
320 }
321
322 #endif // wxUSE_FONTMAP