]> git.saurik.com Git - wxWidgets.git/blame - tests/font/fonttest.cpp
Basque translations update from Xabier Aramendi.
[wxWidgets.git] / tests / font / fonttest.cpp
CommitLineData
5b06a80f
FM
1///////////////////////////////////////////////////////////////////////////////
2// Name: tests/font/fonttest.cpp
3// Purpose: wxFont unit test
4// Author: Francesco Montorsi
5// Created: 16.3.09
5b06a80f
FM
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
a349dc10
VZ
28#include "asserthelper.h"
29
5b06a80f
FM
30// ----------------------------------------------------------------------------
31// test class
32// ----------------------------------------------------------------------------
33
34class FontTestCase : public CppUnit::TestCase
35{
36public:
37 FontTestCase() { }
38
39private:
40 CPPUNIT_TEST_SUITE( FontTestCase );
cec8d677 41 CPPUNIT_TEST( Construct );
5b06a80f 42 CPPUNIT_TEST( GetSet );
c56fc0dc
VZ
43 CPPUNIT_TEST( NativeFontInfo );
44 CPPUNIT_TEST( NativeFontInfoUserDesc );
5b06a80f
FM
45 CPPUNIT_TEST_SUITE_END();
46
cec8d677 47 void Construct();
5b06a80f 48 void GetSet();
c56fc0dc
VZ
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 }
5b06a80f
FM
67
68 DECLARE_NO_COPY_CLASS(FontTestCase)
69};
70
71// register in the unnamed registry so that these tests are run by default
72CPPUNIT_TEST_SUITE_REGISTRATION( FontTestCase );
73
e3778b4d 74// also include in its own registry so that these tests can be run alone
5b06a80f
FM
75CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FontTestCase, "FontTestCase" );
76
aeecbca0
FM
77wxString DumpFont(const wxFont *font)
78{
79 // dumps the internal properties of a wxFont in the same order they
80 // are checked by wxFontBase::operator==()
c56fc0dc 81
aeecbca0 82 wxASSERT(font->IsOk());
c56fc0dc 83
aeecbca0
FM
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}
5b06a80f 98
cec8d677
VZ
99void 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
cec8d677
VZ
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
5b06a80f
FM
116void FontTestCase::GetSet()
117{
c56fc0dc
VZ
118 unsigned numFonts;
119 const wxFont *pf = GetTestFonts(numFonts);
b7cb8553 120 for ( unsigned n = 0; n < numFonts; n++ )
5b06a80f 121 {
c56fc0dc 122 wxFont test(*pf++);
5b06a80f
FM
123
124 // remember: getters can only be called when wxFont::IsOk() == true
125 CPPUNIT_ASSERT( test.IsOk() );
126
127
128 // test Get/SetFaceName()
5b06a80f
FM
129 CPPUNIT_ASSERT( !test.SetFaceName("a dummy face name") );
130 CPPUNIT_ASSERT( !test.IsOk() );
131
3f7c8a87
VZ
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
b7cb8553 137 static const char *knownGoodFaceName = "Monospace";
3f7c8a87
VZ
138#endif
139
b7cb8553
VZ
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 );
5b06a80f
FM
147 CPPUNIT_ASSERT( test.IsOk() );
148
149
150 // test Get/SetFamily()
151
6aea1e4a 152 test.SetFamily( wxFONTFAMILY_ROMAN );
14a83cbf 153 CPPUNIT_ASSERT( test.IsOk() );
aa6b8882
VZ
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 );
6aea1e4a 161
5b06a80f 162
5b06a80f
FM
163 // test Get/SetEncoding()
164
165 //test.SetEncoding( wxFONTENCODING_KOI8 );
14a83cbf 166 //CPPUNIT_ASSERT( test.IsOk() );
5b06a80f
FM
167 //CPPUNIT_ASSERT_EQUAL( wxFONTENCODING_KOI8 , test.GetEncoding() );
168
c56fc0dc
VZ
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
a349dc10
VZ
205 // test Get/SetStrikethrough()
206
9b1aeb4c
VZ
207 // Strike through support not implemented in wxOSX currently.
208#ifndef __WXOSX__
a349dc10
VZ
209 test.SetStrikethrough(true);
210 CPPUNIT_ASSERT( test.IsOk() );
211 CPPUNIT_ASSERT_EQUAL( true, test.GetStrikethrough() );
9b1aeb4c 212#endif // !__WXOSX__
a349dc10 213
c56fc0dc
VZ
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
223void 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
5b06a80f
FM
231 const wxString& nid = test.GetNativeFontInfoDesc();
232 CPPUNIT_ASSERT( !nid.empty() );
14a83cbf 233 // documented to be never empty
c56fc0dc 234
5b06a80f 235 wxFont temp;
14a83cbf
FM
236 CPPUNIT_ASSERT( temp.SetNativeFontInfo(nid) );
237 CPPUNIT_ASSERT( temp.IsOk() );
c56fc0dc 238 WX_ASSERT_MESSAGE(
aeecbca0
FM
239 ("Test #%lu failed\ndump of test font: \"%s\"\ndump of temp font: \"%s\"", \
240 n, DumpFont(&test), DumpFont(&temp)),
9b5e0a6d 241 temp == test );
c56fc0dc
VZ
242 }
243
244 // test that clearly invalid font info strings do not work
245 wxFont font;
246 CPPUNIT_ASSERT( !font.SetNativeFontInfo("") );
541aafe2
VZ
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__)
c56fc0dc 253 CPPUNIT_ASSERT( !font.SetNativeFontInfo("bloordyblop") );
541aafe2 254#endif
a349dc10
VZ
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.
c56fc0dc
VZ
275}
276
277void 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++);
14a83cbf 284
5b06a80f
FM
285 const wxString& niud = test.GetNativeFontInfoUserDesc();
286 CPPUNIT_ASSERT( !niud.empty() );
14a83cbf 287 // documented to be never empty
c56fc0dc 288
5b06a80f 289 wxFont temp2;
14a83cbf
FM
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:
c56fc0dc 295 WX_ASSERT_MESSAGE(
6aafb945
FM
296 ("Test #%lu failed; native info user desc was \"%s\" for test and \"%s\" for temp2", \
297 n, niud, temp2.GetNativeFontInfoUserDesc()),
9b5e0a6d 298 temp2 == test );
14a83cbf
FM
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() );
14a83cbf 306
c56fc0dc
VZ
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 }
14a83cbf 315
c56fc0dc
VZ
316 CPPUNIT_ASSERT_EQUAL( test.GetPointSize(), temp2.GetPointSize() );
317 CPPUNIT_ASSERT_EQUAL( test.GetEncoding(), temp2.GetEncoding() );
14a83cbf 318#endif
5b06a80f
FM
319 }
320}
321
322#endif // wxUSE_FONTMAP