ellipsization.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/graphics/ellipsization.cpp
  3. // Purpose: wxControlBase::*Ellipsize* unit test
  4. // Author: Francesco Montorsi
  5. // Created: 2010-03-10
  6. // Copyright: (c) 2010 Francesco Montorsi
  7. ///////////////////////////////////////////////////////////////////////////////
  8. // ----------------------------------------------------------------------------
  9. // headers
  10. // ----------------------------------------------------------------------------
  11. #include "testprec.h"
  12. #ifdef __BORLANDC__
  13. #pragma hdrstop
  14. #endif
  15. #include "wx/control.h"
  16. #include "wx/dcmemory.h"
  17. // ----------------------------------------------------------------------------
  18. // test class
  19. // ----------------------------------------------------------------------------
  20. class EllipsizationTestCase : public CppUnit::TestCase
  21. {
  22. public:
  23. EllipsizationTestCase() { }
  24. private:
  25. CPPUNIT_TEST_SUITE( EllipsizationTestCase );
  26. CPPUNIT_TEST( NormalCase );
  27. CPPUNIT_TEST( EnoughSpace );
  28. CPPUNIT_TEST( VeryLittleSpace );
  29. CPPUNIT_TEST( HasThreeDots );
  30. CPPUNIT_TEST_SUITE_END();
  31. void NormalCase();
  32. void EnoughSpace();
  33. void VeryLittleSpace();
  34. void HasThreeDots();
  35. DECLARE_NO_COPY_CLASS(EllipsizationTestCase)
  36. };
  37. // register in the unnamed registry so that these tests are run by default
  38. CPPUNIT_TEST_SUITE_REGISTRATION( EllipsizationTestCase );
  39. // also include in its own registry so that these tests can be run alone
  40. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( EllipsizationTestCase, "EllipsizationTestCase" );
  41. void EllipsizationTestCase::NormalCase()
  42. {
  43. wxMemoryDC dc;
  44. static const char *stringsToTest[] =
  45. {
  46. "N",
  47. ".",
  48. "x",
  49. "foobar",
  50. "\xCE\xB1", // U03B1 (GREEK SMALL LETTER ALPHA)
  51. "Another test",
  52. "a very very very very very very very long string",
  53. // alpha+beta+gamma+delta+epsilon+zeta+eta+theta+iota
  54. "\xCE\xB1\xCE\xB2\xCE\xB3\xCE\xB4\xCE\xB5\xCE\xB6\xCE\xB7\xCE\xB8\xCE\xB9",
  55. "\t", "\t\t\t\t\t", "a\tstring\twith\ttabs",
  56. "\n", "\n\n\n\n\n", "a\nstring\nwith\nnewlines",
  57. "&", "&&&&&&&", "a&string&with&newlines",
  58. "\t\n&", "a\t\n&string\t\n&with\t\n&many\t\n&chars"
  59. };
  60. static const int flagsToTest[] =
  61. {
  62. 0,
  63. wxELLIPSIZE_FLAGS_PROCESS_MNEMONICS,
  64. wxELLIPSIZE_FLAGS_EXPAND_TABS,
  65. wxELLIPSIZE_FLAGS_PROCESS_MNEMONICS | wxELLIPSIZE_FLAGS_EXPAND_TABS
  66. };
  67. static const wxEllipsizeMode modesToTest[] =
  68. {
  69. wxELLIPSIZE_START,
  70. wxELLIPSIZE_MIDDLE,
  71. wxELLIPSIZE_END
  72. };
  73. int widthsToTest[] = { 50, 100, 150 };
  74. for ( unsigned int s = 0; s < WXSIZEOF(stringsToTest); s++ )
  75. {
  76. const wxString str = wxString::FromUTF8(stringsToTest[s]);
  77. for ( unsigned int f = 0; f < WXSIZEOF(flagsToTest); f++ )
  78. {
  79. for ( unsigned int m = 0; m < WXSIZEOF(modesToTest); m++ )
  80. {
  81. for ( unsigned int w = 0; w < WXSIZEOF(widthsToTest); w++ )
  82. {
  83. wxString ret = wxControl::Ellipsize
  84. (
  85. str,
  86. dc,
  87. modesToTest[m],
  88. widthsToTest[w],
  89. flagsToTest[f]
  90. );
  91. WX_ASSERT_MESSAGE
  92. (
  93. (
  94. "invalid ellipsization for \"%s\" (%dpx, should be <=%dpx)",
  95. str,
  96. dc.GetMultiLineTextExtent(ret).GetWidth(),
  97. widthsToTest[w]
  98. ),
  99. dc.GetMultiLineTextExtent(ret).GetWidth() <= widthsToTest[w]
  100. );
  101. }
  102. }
  103. }
  104. }
  105. }
  106. void EllipsizationTestCase::EnoughSpace()
  107. {
  108. // No ellipsization should occur if there's plenty of space.
  109. wxMemoryDC dc;
  110. CPPUNIT_ASSERT_EQUAL("some label",
  111. wxControl::Ellipsize("some label", dc, wxELLIPSIZE_START, 200));
  112. CPPUNIT_ASSERT_EQUAL("some label",
  113. wxControl::Ellipsize("some label", dc, wxELLIPSIZE_MIDDLE, 200));
  114. CPPUNIT_ASSERT_EQUAL("some label",
  115. wxControl::Ellipsize("some label", dc, wxELLIPSIZE_END, 200));
  116. }
  117. void EllipsizationTestCase::VeryLittleSpace()
  118. {
  119. // If there's not enough space, the shortened label should still contain "..." and one character
  120. wxMemoryDC dc;
  121. CPPUNIT_ASSERT_EQUAL("...l",
  122. wxControl::Ellipsize("some label", dc, wxELLIPSIZE_START, 5));
  123. CPPUNIT_ASSERT_EQUAL("s...",
  124. wxControl::Ellipsize("some label", dc, wxELLIPSIZE_MIDDLE, 5));
  125. CPPUNIT_ASSERT_EQUAL("s...",
  126. wxControl::Ellipsize("some label1", dc, wxELLIPSIZE_MIDDLE, 5));
  127. CPPUNIT_ASSERT_EQUAL("s...",
  128. wxControl::Ellipsize("some label", dc, wxELLIPSIZE_END, 5));
  129. }
  130. void EllipsizationTestCase::HasThreeDots()
  131. {
  132. wxMemoryDC dc;
  133. CPPUNIT_ASSERT( wxControl::Ellipsize("some longer text", dc, wxELLIPSIZE_START, 80).StartsWith("...") );
  134. CPPUNIT_ASSERT( !wxControl::Ellipsize("some longer text", dc, wxELLIPSIZE_START, 80).EndsWith("...") );
  135. CPPUNIT_ASSERT( wxControl::Ellipsize("some longer text", dc, wxELLIPSIZE_END, 80).EndsWith("...") );
  136. CPPUNIT_ASSERT( wxControl::Ellipsize("some longer text", dc, wxELLIPSIZE_MIDDLE, 80).Contains("...") );
  137. CPPUNIT_ASSERT( !wxControl::Ellipsize("some longer text", dc, wxELLIPSIZE_MIDDLE, 80).StartsWith("...") );
  138. CPPUNIT_ASSERT( !wxControl::Ellipsize("some longer text", dc, wxELLIPSIZE_MIDDLE, 80).EndsWith("...") );
  139. }