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