provide more info when a check for native font [user] info fails
[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
55 void FontTestCase::GetSet()
56 {
57 static const wxFont testfonts[] =
58 {
59 *wxNORMAL_FONT,
60 *wxSMALL_FONT,
61 *wxITALIC_FONT,
62 *wxSWISS_FONT,
63 wxFont(5, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL)
64 };
65
66 for ( size_t n = 0; n < WXSIZEOF(testfonts); n++ )
67 {
68 wxFont test(testfonts[n]);
69
70 // remember: getters can only be called when wxFont::IsOk() == true
71 CPPUNIT_ASSERT( test.IsOk() );
72
73
74 // test Get/SetFaceName()
75
76 const wxString& fn = test.GetFaceName();
77 CPPUNIT_ASSERT( !fn.empty() );
78
79 CPPUNIT_ASSERT( !test.SetFaceName("a dummy face name") );
80 CPPUNIT_ASSERT( !test.IsOk() );
81
82 CPPUNIT_ASSERT( test.SetFaceName(fn) );
83 CPPUNIT_ASSERT( test.IsOk() );
84
85
86 // test Get/SetFamily()
87
88 test.SetFamily( wxFONTFAMILY_ROMAN );
89 CPPUNIT_ASSERT( test.IsOk() );
90 CPPUNIT_ASSERT( wxFONTFAMILY_ROMAN == test.GetFamily() ||
91 wxFONTFAMILY_UNKNOWN == test.GetFamily() );
92 // note that there is always the possibility that GetFamily() returns
93 // wxFONTFAMILY_UNKNOWN so that we consider it as a valid return value
94
95
96 // test Get/SetEncoding()
97
98 //test.SetEncoding( wxFONTENCODING_KOI8 );
99 //CPPUNIT_ASSERT( test.IsOk() );
100 //CPPUNIT_ASSERT_EQUAL( wxFONTENCODING_KOI8 , test.GetEncoding() );
101
102
103 // test Get/SetNativeFontInfo
104
105 const wxString& nid = test.GetNativeFontInfoDesc();
106 CPPUNIT_ASSERT( !nid.empty() );
107 // documented to be never empty
108
109 wxFont temp;
110 CPPUNIT_ASSERT( temp.SetNativeFontInfo(nid) );
111 CPPUNIT_ASSERT( temp.IsOk() );
112 WX_ASSERT_MESSAGE(
113 ("Test #%lu failed; native info desc was \"%s\"", n, nid),
114 temp == test );
115
116
117 // test Get/SetNativeFontInfoUserDesc
118
119 const wxString& niud = test.GetNativeFontInfoUserDesc();
120 CPPUNIT_ASSERT( !niud.empty() );
121 // documented to be never empty
122
123 wxFont temp2;
124 CPPUNIT_ASSERT( temp2.SetNativeFontInfoUserDesc(niud) );
125 CPPUNIT_ASSERT( temp2.IsOk() );
126
127 #ifdef __WXGTK__
128 // Pango saves/restores all font info in the user-friendly string:
129 WX_ASSERT_MESSAGE(
130 ("Test #%lu failed; native info user desc was \"%s\"", n, niud),
131 temp2 == test );
132 #else
133 // NOTE: as documented GetNativeFontInfoUserDesc/SetNativeFontInfoUserDesc
134 // are not granted to save/restore all font info.
135 // In fact e.g. the font family is not saved at all; test only those
136 // info which GetNativeFontInfoUserDesc() does indeed save:
137 CPPUNIT_ASSERT_EQUAL( test.GetWeight(), temp2.GetWeight() );
138 CPPUNIT_ASSERT_EQUAL( test.GetStyle(), temp2.GetStyle() );
139 CPPUNIT_ASSERT( test.GetFaceName().CmpNoCase(temp2.GetFaceName()) == 0 );
140 CPPUNIT_ASSERT_EQUAL( test.GetPointSize(), temp2.GetPointSize() );
141 CPPUNIT_ASSERT_EQUAL( test.GetEncoding(), temp2.GetEncoding() );
142 #endif
143
144
145 // test Get/SetPointSize()
146
147 test.SetPointSize(30);
148 CPPUNIT_ASSERT( test.IsOk() );
149 CPPUNIT_ASSERT_EQUAL( 30, test.GetPointSize() );
150
151
152 // test Get/SetPixelSize()
153
154 test.SetPixelSize(wxSize(0,30));
155 CPPUNIT_ASSERT( test.IsOk() );
156 CPPUNIT_ASSERT( test.GetPixelSize().GetHeight() <= 30 );
157 // NOTE: the match found by SetPixelSize() may be not 100% precise; it
158 // only grants that a font smaller than the required height will
159 // be selected
160
161
162 // test Get/SetStyle()
163
164 test.SetStyle(wxFONTSTYLE_SLANT);
165 CPPUNIT_ASSERT( test.IsOk() );
166 #ifdef __WXMSW__
167 // on wxMSW wxFONTSTYLE_SLANT==wxFONTSTYLE_ITALIC
168 CPPUNIT_ASSERT( wxFONTSTYLE_SLANT == test.GetStyle() ||
169 wxFONTSTYLE_ITALIC == test.GetStyle() );
170 #else
171 CPPUNIT_ASSERT_EQUAL( wxFONTSTYLE_SLANT, test.GetStyle() );
172 #endif
173
174 // test Get/SetUnderlined()
175
176 test.SetUnderlined(true);
177 CPPUNIT_ASSERT( test.IsOk() );
178 CPPUNIT_ASSERT_EQUAL( true, test.GetUnderlined() );
179
180
181 // test Get/SetWeight()
182
183 test.SetWeight(wxFONTWEIGHT_BOLD);
184 CPPUNIT_ASSERT( test.IsOk() );
185 CPPUNIT_ASSERT_EQUAL( wxFONTWEIGHT_BOLD, test.GetWeight() );
186 }
187 }
188
189 #endif // wxUSE_FONTMAP