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