]>
Commit | Line | Data |
---|---|---|
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 | #include "asserthelper.h" | |
30 | ||
31 | // ---------------------------------------------------------------------------- | |
32 | // test class | |
33 | // ---------------------------------------------------------------------------- | |
34 | ||
35 | class FontTestCase : public CppUnit::TestCase | |
36 | { | |
37 | public: | |
38 | FontTestCase() { } | |
39 | ||
40 | private: | |
41 | CPPUNIT_TEST_SUITE( FontTestCase ); | |
42 | CPPUNIT_TEST( Construct ); | |
43 | CPPUNIT_TEST( GetSet ); | |
44 | CPPUNIT_TEST( NativeFontInfo ); | |
45 | CPPUNIT_TEST( NativeFontInfoUserDesc ); | |
46 | CPPUNIT_TEST_SUITE_END(); | |
47 | ||
48 | void Construct(); | |
49 | void GetSet(); | |
50 | void NativeFontInfo(); | |
51 | void NativeFontInfoUserDesc(); | |
52 | ||
53 | static const wxFont *GetTestFonts(unsigned& numFonts) | |
54 | { | |
55 | static const wxFont testfonts[] = | |
56 | { | |
57 | *wxNORMAL_FONT, | |
58 | *wxSMALL_FONT, | |
59 | *wxITALIC_FONT, | |
60 | *wxSWISS_FONT, | |
61 | wxFont(5, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL) | |
62 | }; | |
63 | ||
64 | numFonts = WXSIZEOF(testfonts); | |
65 | ||
66 | return testfonts; | |
67 | } | |
68 | ||
69 | DECLARE_NO_COPY_CLASS(FontTestCase) | |
70 | }; | |
71 | ||
72 | // register in the unnamed registry so that these tests are run by default | |
73 | CPPUNIT_TEST_SUITE_REGISTRATION( FontTestCase ); | |
74 | ||
75 | // also include in its own registry so that these tests can be run alone | |
76 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FontTestCase, "FontTestCase" ); | |
77 | ||
78 | wxString DumpFont(const wxFont *font) | |
79 | { | |
80 | // dumps the internal properties of a wxFont in the same order they | |
81 | // are checked by wxFontBase::operator==() | |
82 | ||
83 | wxASSERT(font->IsOk()); | |
84 | ||
85 | wxString s; | |
86 | s.Printf(wxS("%d-%d;%d-%d-%d-%d-%d-%s-%d"), | |
87 | font->GetPointSize(), | |
88 | font->GetPixelSize().x, | |
89 | font->GetPixelSize().y, | |
90 | font->GetFamily(), | |
91 | font->GetStyle(), | |
92 | font->GetWeight(), | |
93 | font->GetUnderlined() ? 1 : 0, | |
94 | font->GetFaceName(), | |
95 | font->GetEncoding()); | |
96 | ||
97 | return s; | |
98 | } | |
99 | ||
100 | void FontTestCase::Construct() | |
101 | { | |
102 | // The main purpose of this test is to verify that the font ctors below | |
103 | // compile because it's easy to introduce ambiguities due to the number of | |
104 | // overloaded wxFont ctors. | |
105 | ||
106 | CPPUNIT_ASSERT( wxFont(10, wxFONTFAMILY_DEFAULT).IsOk() ); | |
107 | CPPUNIT_ASSERT( wxFont(10, wxFONTFAMILY_DEFAULT, | |
108 | wxFONTFLAG_DEFAULT).IsOk() ); | |
109 | CPPUNIT_ASSERT( wxFont(10, wxFONTFAMILY_DEFAULT, | |
110 | wxFONTSTYLE_NORMAL, | |
111 | wxFONTWEIGHT_NORMAL).IsOk() ); | |
112 | ||
113 | #if FUTURE_WXWIN_COMPATIBILITY_3_0 | |
114 | // Tests relying on the soon-to-be-deprecated ctor taking ints and not | |
115 | // wxFontXXX enum elements. | |
116 | CPPUNIT_ASSERT( wxFont(10, wxDEFAULT, wxNORMAL, wxNORMAL).IsOk() ); | |
117 | #endif // FUTURE_WXWIN_COMPATIBILITY_3_0 | |
118 | } | |
119 | ||
120 | void FontTestCase::GetSet() | |
121 | { | |
122 | unsigned numFonts; | |
123 | const wxFont *pf = GetTestFonts(numFonts); | |
124 | for ( unsigned n = 0; n < numFonts; n++ ) | |
125 | { | |
126 | wxFont test(*pf++); | |
127 | ||
128 | // remember: getters can only be called when wxFont::IsOk() == true | |
129 | CPPUNIT_ASSERT( test.IsOk() ); | |
130 | ||
131 | ||
132 | // test Get/SetFaceName() | |
133 | CPPUNIT_ASSERT( !test.SetFaceName("a dummy face name") ); | |
134 | CPPUNIT_ASSERT( !test.IsOk() ); | |
135 | ||
136 | // if the call to SetFaceName() below fails on your system/port, | |
137 | // consider adding another branch to this #if | |
138 | #if defined(__WXMSW__) || defined(__WXOSX__) | |
139 | static const char *knownGoodFaceName = "Arial"; | |
140 | #else | |
141 | static const char *knownGoodFaceName = "Monospace"; | |
142 | #endif | |
143 | ||
144 | WX_ASSERT_MESSAGE | |
145 | ( | |
146 | ("failed to set face name \"%s\" for test font #%u\n" | |
147 | "(this failure is harmless if this face name is not " | |
148 | "available on this system)", knownGoodFaceName, n), | |
149 | test.SetFaceName(knownGoodFaceName) | |
150 | ); | |
151 | CPPUNIT_ASSERT( test.IsOk() ); | |
152 | ||
153 | ||
154 | // test Get/SetFamily() | |
155 | ||
156 | test.SetFamily( wxFONTFAMILY_ROMAN ); | |
157 | CPPUNIT_ASSERT( test.IsOk() ); | |
158 | ||
159 | // note that there is always the possibility that GetFamily() returns | |
160 | // wxFONTFAMILY_DEFAULT (meaning "unknown" in this case) so that we | |
161 | // consider it as a valid return value | |
162 | const wxFontFamily family = test.GetFamily(); | |
163 | if ( family != wxFONTFAMILY_DEFAULT ) | |
164 | CPPUNIT_ASSERT_EQUAL( wxFONTFAMILY_ROMAN, family ); | |
165 | ||
166 | ||
167 | // test Get/SetEncoding() | |
168 | ||
169 | //test.SetEncoding( wxFONTENCODING_KOI8 ); | |
170 | //CPPUNIT_ASSERT( test.IsOk() ); | |
171 | //CPPUNIT_ASSERT_EQUAL( wxFONTENCODING_KOI8 , test.GetEncoding() ); | |
172 | ||
173 | ||
174 | // test Get/SetPointSize() | |
175 | ||
176 | test.SetPointSize(30); | |
177 | CPPUNIT_ASSERT( test.IsOk() ); | |
178 | CPPUNIT_ASSERT_EQUAL( 30, test.GetPointSize() ); | |
179 | ||
180 | ||
181 | // test Get/SetPixelSize() | |
182 | ||
183 | test.SetPixelSize(wxSize(0,30)); | |
184 | CPPUNIT_ASSERT( test.IsOk() ); | |
185 | CPPUNIT_ASSERT( test.GetPixelSize().GetHeight() <= 30 ); | |
186 | // NOTE: the match found by SetPixelSize() may be not 100% precise; it | |
187 | // only grants that a font smaller than the required height will | |
188 | // be selected | |
189 | ||
190 | ||
191 | // test Get/SetStyle() | |
192 | ||
193 | test.SetStyle(wxFONTSTYLE_SLANT); | |
194 | CPPUNIT_ASSERT( test.IsOk() ); | |
195 | #ifdef __WXMSW__ | |
196 | // on wxMSW wxFONTSTYLE_SLANT==wxFONTSTYLE_ITALIC | |
197 | CPPUNIT_ASSERT( wxFONTSTYLE_SLANT == test.GetStyle() || | |
198 | wxFONTSTYLE_ITALIC == test.GetStyle() ); | |
199 | #else | |
200 | CPPUNIT_ASSERT_EQUAL( wxFONTSTYLE_SLANT, test.GetStyle() ); | |
201 | #endif | |
202 | ||
203 | // test Get/SetUnderlined() | |
204 | ||
205 | test.SetUnderlined(true); | |
206 | CPPUNIT_ASSERT( test.IsOk() ); | |
207 | CPPUNIT_ASSERT_EQUAL( true, test.GetUnderlined() ); | |
208 | ||
209 | // test Get/SetStrikethrough() | |
210 | ||
211 | test.SetStrikethrough(true); | |
212 | CPPUNIT_ASSERT( test.IsOk() ); | |
213 | CPPUNIT_ASSERT_EQUAL( true, test.GetStrikethrough() ); | |
214 | ||
215 | ||
216 | // test Get/SetWeight() | |
217 | ||
218 | test.SetWeight(wxFONTWEIGHT_BOLD); | |
219 | CPPUNIT_ASSERT( test.IsOk() ); | |
220 | CPPUNIT_ASSERT_EQUAL( wxFONTWEIGHT_BOLD, test.GetWeight() ); | |
221 | } | |
222 | } | |
223 | ||
224 | void FontTestCase::NativeFontInfo() | |
225 | { | |
226 | unsigned numFonts; | |
227 | const wxFont *pf = GetTestFonts(numFonts); | |
228 | for ( size_t n = 0; n < numFonts; n++ ) | |
229 | { | |
230 | wxFont test(*pf++); | |
231 | ||
232 | const wxString& nid = test.GetNativeFontInfoDesc(); | |
233 | CPPUNIT_ASSERT( !nid.empty() ); | |
234 | // documented to be never empty | |
235 | ||
236 | wxFont temp; | |
237 | CPPUNIT_ASSERT( temp.SetNativeFontInfo(nid) ); | |
238 | CPPUNIT_ASSERT( temp.IsOk() ); | |
239 | WX_ASSERT_MESSAGE( | |
240 | ("Test #%lu failed\ndump of test font: \"%s\"\ndump of temp font: \"%s\"", \ | |
241 | n, DumpFont(&test), DumpFont(&temp)), | |
242 | temp == test ); | |
243 | } | |
244 | ||
245 | // test that clearly invalid font info strings do not work | |
246 | wxFont font; | |
247 | CPPUNIT_ASSERT( !font.SetNativeFontInfo("") ); | |
248 | ||
249 | // pango_font_description_from_string() used by wxFont in wxGTK and wxX11 | |
250 | // never returns an error at all so this assertion fails there -- and as it | |
251 | // doesn't seem to be possible to do anything about it maybe we should | |
252 | // change wxMSW and other ports to also accept any strings? | |
253 | #if !defined(__WXGTK__) && !defined(__WXX11__) | |
254 | CPPUNIT_ASSERT( !font.SetNativeFontInfo("bloordyblop") ); | |
255 | #endif | |
256 | ||
257 | // Pango font description doesn't have 'underlined' and 'strikethrough' | |
258 | // attributes, so wxNativeFontInfo implements these itself. Test if these | |
259 | // are properly preserved by wxNativeFontInfo or its string description. | |
260 | font.SetUnderlined(true); | |
261 | font.SetStrikethrough(true); | |
262 | CPPUNIT_ASSERT_EQUAL(font, wxFont(font)); | |
263 | CPPUNIT_ASSERT_EQUAL(font, wxFont(*font.GetNativeFontInfo())); | |
264 | CPPUNIT_ASSERT_EQUAL(font, wxFont(font.GetNativeFontInfoDesc())); | |
265 | font.SetUnderlined(false); | |
266 | CPPUNIT_ASSERT_EQUAL(font, wxFont(font)); | |
267 | CPPUNIT_ASSERT_EQUAL(font, wxFont(*font.GetNativeFontInfo())); | |
268 | CPPUNIT_ASSERT_EQUAL(font, wxFont(font.GetNativeFontInfoDesc())); | |
269 | font.SetUnderlined(true); | |
270 | font.SetStrikethrough(false); | |
271 | CPPUNIT_ASSERT_EQUAL(font, wxFont(font)); | |
272 | CPPUNIT_ASSERT_EQUAL(font, wxFont(*font.GetNativeFontInfo())); | |
273 | CPPUNIT_ASSERT_EQUAL(font, wxFont(font.GetNativeFontInfoDesc())); | |
274 | // note: the GetNativeFontInfoUserDesc() doesn't preserve all attributes | |
275 | // according to docs, so it is not tested. | |
276 | } | |
277 | ||
278 | void FontTestCase::NativeFontInfoUserDesc() | |
279 | { | |
280 | unsigned numFonts; | |
281 | const wxFont *pf = GetTestFonts(numFonts); | |
282 | for ( size_t n = 0; n < numFonts; n++ ) | |
283 | { | |
284 | wxFont test(*pf++); | |
285 | ||
286 | const wxString& niud = test.GetNativeFontInfoUserDesc(); | |
287 | CPPUNIT_ASSERT( !niud.empty() ); | |
288 | // documented to be never empty | |
289 | ||
290 | wxFont temp2; | |
291 | CPPUNIT_ASSERT( temp2.SetNativeFontInfoUserDesc(niud) ); | |
292 | CPPUNIT_ASSERT( temp2.IsOk() ); | |
293 | ||
294 | #ifdef __WXGTK__ | |
295 | // Pango saves/restores all font info in the user-friendly string: | |
296 | WX_ASSERT_MESSAGE( | |
297 | ("Test #%lu failed; native info user desc was \"%s\" for test and \"%s\" for temp2", \ | |
298 | n, niud, temp2.GetNativeFontInfoUserDesc()), | |
299 | temp2 == test ); | |
300 | #else | |
301 | // NOTE: as documented GetNativeFontInfoUserDesc/SetNativeFontInfoUserDesc | |
302 | // are not granted to save/restore all font info. | |
303 | // In fact e.g. the font family is not saved at all; test only those | |
304 | // info which GetNativeFontInfoUserDesc() does indeed save: | |
305 | CPPUNIT_ASSERT_EQUAL( test.GetWeight(), temp2.GetWeight() ); | |
306 | CPPUNIT_ASSERT_EQUAL( test.GetStyle(), temp2.GetStyle() ); | |
307 | ||
308 | // if the original face name was empty, it means that any face name (in | |
309 | // this family) can be used for the new font so we shouldn't be | |
310 | // surprised to find that they differ in this case | |
311 | const wxString facename = test.GetFaceName(); | |
312 | if ( !facename.empty() ) | |
313 | { | |
314 | CPPUNIT_ASSERT_EQUAL( facename.Upper(), temp2.GetFaceName().Upper() ); | |
315 | } | |
316 | ||
317 | CPPUNIT_ASSERT_EQUAL( test.GetPointSize(), temp2.GetPointSize() ); | |
318 | CPPUNIT_ASSERT_EQUAL( test.GetEncoding(), temp2.GetEncoding() ); | |
319 | #endif | |
320 | } | |
321 | } | |
322 | ||
323 | #endif // wxUSE_FONTMAP |