fonttest.cpp 10 KB

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