dump all font properties upon failure
[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_SUITE_END();
42
43 void GetSet();
44
45 DECLARE_NO_COPY_CLASS(FontTestCase)
46 };
47
48 // register in the unnamed registry so that these tests are run by default
49 CPPUNIT_TEST_SUITE_REGISTRATION( FontTestCase );
50
51 // also include in it's own registry so that these tests can be run alone
52 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FontTestCase, "FontTestCase" );
53
54 wxString DumpFont(const wxFont *font)
55 {
56 // dumps the internal properties of a wxFont in the same order they
57 // are checked by wxFontBase::operator==()
58
59 wxASSERT(font->IsOk());
60
61 wxString s;
62 s.Printf(wxS("%d-%d;%d-%d-%d-%d-%d-%s-%d"),
63 font->GetPointSize(),
64 font->GetPixelSize().x,
65 font->GetPixelSize().y,
66 font->GetFamily(),
67 font->GetStyle(),
68 font->GetWeight(),
69 font->GetUnderlined() ? 1 : 0,
70 font->GetFaceName(),
71 font->GetEncoding());
72
73 return s;
74 }
75
76 void FontTestCase::GetSet()
77 {
78 static const wxFont testfonts[] =
79 {
80 *wxNORMAL_FONT,
81 *wxSMALL_FONT,
82 *wxITALIC_FONT,
83 *wxSWISS_FONT,
84 wxFont(5, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL)
85 };
86
87 for ( size_t n = 0; n < WXSIZEOF(testfonts); n++ )
88 {
89 wxFont test(testfonts[n]);
90
91 // remember: getters can only be called when wxFont::IsOk() == true
92 CPPUNIT_ASSERT( test.IsOk() );
93
94
95 // test Get/SetFaceName()
96
97 const wxString& fn = test.GetFaceName();
98 CPPUNIT_ASSERT( !fn.empty() );
99
100 CPPUNIT_ASSERT( !test.SetFaceName("a dummy face name") );
101 CPPUNIT_ASSERT( !test.IsOk() );
102
103 CPPUNIT_ASSERT( test.SetFaceName(fn) );
104 CPPUNIT_ASSERT( test.IsOk() );
105
106
107 // test Get/SetFamily()
108
109 test.SetFamily( wxFONTFAMILY_ROMAN );
110 CPPUNIT_ASSERT( test.IsOk() );
111 CPPUNIT_ASSERT( wxFONTFAMILY_ROMAN == test.GetFamily() ||
112 wxFONTFAMILY_UNKNOWN == test.GetFamily() );
113 // note that there is always the possibility that GetFamily() returns
114 // wxFONTFAMILY_UNKNOWN so that we consider it as a valid return value
115
116
117 // test Get/SetEncoding()
118
119 //test.SetEncoding( wxFONTENCODING_KOI8 );
120 //CPPUNIT_ASSERT( test.IsOk() );
121 //CPPUNIT_ASSERT_EQUAL( wxFONTENCODING_KOI8 , test.GetEncoding() );
122
123
124 // test Get/SetNativeFontInfo
125
126 const wxString& nid = test.GetNativeFontInfoDesc();
127 CPPUNIT_ASSERT( !nid.empty() );
128 // documented to be never empty
129
130 wxFont temp;
131 CPPUNIT_ASSERT( temp.SetNativeFontInfo(nid) );
132 CPPUNIT_ASSERT( temp.IsOk() );
133 WX_ASSERT_MESSAGE(
134 ("Test #%lu failed\ndump of test font: \"%s\"\ndump of temp font: \"%s\"", \
135 n, DumpFont(&test), DumpFont(&temp)),
136 temp == test );
137
138
139 // test Get/SetNativeFontInfoUserDesc
140
141 const wxString& niud = test.GetNativeFontInfoUserDesc();
142 CPPUNIT_ASSERT( !niud.empty() );
143 // documented to be never empty
144
145 wxFont temp2;
146 CPPUNIT_ASSERT( temp2.SetNativeFontInfoUserDesc(niud) );
147 CPPUNIT_ASSERT( temp2.IsOk() );
148
149 #ifdef __WXGTK__
150 // Pango saves/restores all font info in the user-friendly string:
151 WX_ASSERT_MESSAGE(
152 ("Test #%lu failed; native info user desc was \"%s\" for test and \"%s\" for temp2", \
153 n, niud, temp2.GetNativeFontInfoUserDesc()),
154 temp2 == test );
155 #else
156 // NOTE: as documented GetNativeFontInfoUserDesc/SetNativeFontInfoUserDesc
157 // are not granted to save/restore all font info.
158 // In fact e.g. the font family is not saved at all; test only those
159 // info which GetNativeFontInfoUserDesc() does indeed save:
160 CPPUNIT_ASSERT_EQUAL( test.GetWeight(), temp2.GetWeight() );
161 CPPUNIT_ASSERT_EQUAL( test.GetStyle(), temp2.GetStyle() );
162 CPPUNIT_ASSERT( test.GetFaceName().CmpNoCase(temp2.GetFaceName()) == 0 );
163 CPPUNIT_ASSERT_EQUAL( test.GetPointSize(), temp2.GetPointSize() );
164 CPPUNIT_ASSERT_EQUAL( test.GetEncoding(), temp2.GetEncoding() );
165 #endif
166
167
168 // test Get/SetPointSize()
169
170 test.SetPointSize(30);
171 CPPUNIT_ASSERT( test.IsOk() );
172 CPPUNIT_ASSERT_EQUAL( 30, test.GetPointSize() );
173
174
175 // test Get/SetPixelSize()
176
177 test.SetPixelSize(wxSize(0,30));
178 CPPUNIT_ASSERT( test.IsOk() );
179 CPPUNIT_ASSERT( test.GetPixelSize().GetHeight() <= 30 );
180 // NOTE: the match found by SetPixelSize() may be not 100% precise; it
181 // only grants that a font smaller than the required height will
182 // be selected
183
184
185 // test Get/SetStyle()
186
187 test.SetStyle(wxFONTSTYLE_SLANT);
188 CPPUNIT_ASSERT( test.IsOk() );
189 #ifdef __WXMSW__
190 // on wxMSW wxFONTSTYLE_SLANT==wxFONTSTYLE_ITALIC
191 CPPUNIT_ASSERT( wxFONTSTYLE_SLANT == test.GetStyle() ||
192 wxFONTSTYLE_ITALIC == test.GetStyle() );
193 #else
194 CPPUNIT_ASSERT_EQUAL( wxFONTSTYLE_SLANT, test.GetStyle() );
195 #endif
196
197 // test Get/SetUnderlined()
198
199 test.SetUnderlined(true);
200 CPPUNIT_ASSERT( test.IsOk() );
201 CPPUNIT_ASSERT_EQUAL( true, test.GetUnderlined() );
202
203
204 // test Get/SetWeight()
205
206 test.SetWeight(wxFONTWEIGHT_BOLD);
207 CPPUNIT_ASSERT( test.IsOk() );
208 CPPUNIT_ASSERT_EQUAL( wxFONTWEIGHT_BOLD, test.GetWeight() );
209 }
210 }
211
212 #endif // wxUSE_FONTMAP