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