measuring.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/graphics/measuring.cpp
  3. // Purpose: Tests for wxGraphicsRenderer::CreateMeasuringContext
  4. // Author: Kevin Ollivier, Vadim Zeitlin (non wxGC parts)
  5. // Created: 2008-02-12
  6. // Copyright: (c) 2008 Kevin Ollivier <kevino@theolliviers.com>
  7. // (c) 2012 Vadim Zeitlin <vadim@wxwidgets.org>
  8. ///////////////////////////////////////////////////////////////////////////////
  9. // ----------------------------------------------------------------------------
  10. // headers
  11. // ----------------------------------------------------------------------------
  12. #include "testprec.h"
  13. #ifdef __BORLANDC__
  14. #pragma hdrstop
  15. #endif
  16. #ifndef WX_PRECOMP
  17. #include "wx/app.h"
  18. #include "wx/font.h"
  19. #include "wx/window.h"
  20. #endif // WX_PRECOMP
  21. // wxCairoRenderer::CreateMeasuringContext() is not implement for wxX11
  22. #if wxUSE_GRAPHICS_CONTEXT && !defined(__WXX11__)
  23. #include "wx/graphics.h"
  24. #define TEST_GC
  25. #endif
  26. #include "wx/dcclient.h"
  27. #include "wx/dcps.h"
  28. #include "wx/metafile.h"
  29. // ----------------------------------------------------------------------------
  30. // test class
  31. // ----------------------------------------------------------------------------
  32. class MeasuringTextTestCase : public CppUnit::TestCase
  33. {
  34. public:
  35. MeasuringTextTestCase() { }
  36. private:
  37. CPPUNIT_TEST_SUITE( MeasuringTextTestCase );
  38. CPPUNIT_TEST( DCGetTextExtent );
  39. CPPUNIT_TEST( LeadingAndDescent );
  40. CPPUNIT_TEST( WindowGetTextExtent );
  41. CPPUNIT_TEST( GetPartialTextExtent );
  42. #ifdef TEST_GC
  43. CPPUNIT_TEST( GraphicsGetTextExtent );
  44. #endif // TEST_GC
  45. CPPUNIT_TEST_SUITE_END();
  46. void DCGetTextExtent();
  47. void LeadingAndDescent();
  48. void WindowGetTextExtent();
  49. void GetPartialTextExtent();
  50. #ifdef TEST_GC
  51. void GraphicsGetTextExtent();
  52. #endif // TEST_GC
  53. DECLARE_NO_COPY_CLASS(MeasuringTextTestCase)
  54. };
  55. // register in the unnamed registry so that these tests are run by default
  56. CPPUNIT_TEST_SUITE_REGISTRATION( MeasuringTextTestCase );
  57. // also include in its own registry so that these tests can be run alone
  58. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MeasuringTextTestCase, "MeasuringTextTestCase" );
  59. // ----------------------------------------------------------------------------
  60. // helper for XXXTextExtent() methods
  61. // ----------------------------------------------------------------------------
  62. template <typename T>
  63. struct GetTextExtentTester
  64. {
  65. // Constructor runs a couple of simple tests for GetTextExtent().
  66. GetTextExtentTester(const T& obj)
  67. {
  68. // Test that getting the height only doesn't crash.
  69. int y;
  70. obj.GetTextExtent("H", NULL, &y);
  71. CPPUNIT_ASSERT( y > 1 );
  72. wxSize size = obj.GetTextExtent("Hello");
  73. CPPUNIT_ASSERT( size.x > 1 );
  74. CPPUNIT_ASSERT_EQUAL( y, size.y );
  75. }
  76. };
  77. // ----------------------------------------------------------------------------
  78. // tests themselves
  79. // ----------------------------------------------------------------------------
  80. void MeasuringTextTestCase::DCGetTextExtent()
  81. {
  82. wxClientDC dc(wxTheApp->GetTopWindow());
  83. GetTextExtentTester<wxClientDC> testDC(dc);
  84. int w;
  85. dc.GetMultiLineTextExtent("Good\nbye", &w, NULL);
  86. const wxSize sz = dc.GetTextExtent("Good");
  87. CPPUNIT_ASSERT_EQUAL( sz.x, w );
  88. CPPUNIT_ASSERT( dc.GetMultiLineTextExtent("Good\nbye").y >= 2*sz.y );
  89. // Test the functions with some other DC kinds also.
  90. #if wxUSE_PRINTING_ARCHITECTURE && wxUSE_POSTSCRIPT
  91. wxPostScriptDC psdc;
  92. // wxPostScriptDC doesn't have any font set by default but its
  93. // GetTextExtent() requires one to be set. This is probably a bug and we
  94. // should set the default font in it implicitly but for now just work
  95. // around it.
  96. psdc.SetFont(*wxNORMAL_FONT);
  97. GetTextExtentTester<wxPostScriptDC> testPS(psdc);
  98. #endif
  99. #if wxUSE_ENH_METAFILE
  100. wxEnhMetaFileDC metadc;
  101. GetTextExtentTester<wxEnhMetaFileDC> testMF(metadc);
  102. #endif
  103. }
  104. void MeasuringTextTestCase::LeadingAndDescent()
  105. {
  106. wxClientDC dc(wxTheApp->GetTopWindow());
  107. // Retrieving just the descent should work.
  108. int descent = -17;
  109. dc.GetTextExtent("foo", NULL, NULL, &descent);
  110. CPPUNIT_ASSERT( descent != -17 );
  111. // Same for external leading.
  112. int leading = -289;
  113. dc.GetTextExtent("foo", NULL, NULL, NULL, &leading);
  114. CPPUNIT_ASSERT( leading != -289 );
  115. // And both should also work for the empty string as they retrieve the
  116. // values valid for the entire font and not just this string.
  117. int descent2,
  118. leading2;
  119. dc.GetTextExtent("", NULL, NULL, &descent2, &leading2);
  120. CPPUNIT_ASSERT_EQUAL( descent, descent2 );
  121. CPPUNIT_ASSERT_EQUAL( leading, leading2 );
  122. }
  123. void MeasuringTextTestCase::WindowGetTextExtent()
  124. {
  125. wxWindow* const win = wxTheApp->GetTopWindow();
  126. GetTextExtentTester<wxWindow> testWin(*win);
  127. }
  128. void MeasuringTextTestCase::GetPartialTextExtent()
  129. {
  130. wxClientDC dc(wxTheApp->GetTopWindow());
  131. wxArrayInt widths;
  132. CPPUNIT_ASSERT( dc.GetPartialTextExtents("Hello", widths) );
  133. CPPUNIT_ASSERT_EQUAL( 5, widths.size() );
  134. CPPUNIT_ASSERT_EQUAL( widths[0], dc.GetTextExtent("H").x );
  135. CPPUNIT_ASSERT_EQUAL( widths[4], dc.GetTextExtent("Hello").x );
  136. }
  137. #ifdef TEST_GC
  138. void MeasuringTextTestCase::GraphicsGetTextExtent()
  139. {
  140. wxGraphicsRenderer* renderer = wxGraphicsRenderer::GetDefaultRenderer();
  141. CPPUNIT_ASSERT(renderer);
  142. wxGraphicsContext* context = renderer->CreateMeasuringContext();
  143. CPPUNIT_ASSERT(context);
  144. wxFont font(12, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
  145. CPPUNIT_ASSERT(font.IsOk());
  146. context->SetFont(font, *wxBLACK);
  147. double width, height, descent, externalLeading = 0.0;
  148. context->GetTextExtent("x", &width, &height, &descent, &externalLeading);
  149. // TODO: Determine a way to make these tests more robust.
  150. CPPUNIT_ASSERT(width > 0.0);
  151. CPPUNIT_ASSERT(height > 0.0);
  152. }
  153. #endif // TEST_GC