1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/graphics/ellipsization.cpp
3 // Purpose: wxControlBase::*Ellipsize* unit test
4 // Author: Francesco Montorsi
6 // Copyright: (c) 2010 Francesco Montorsi
7 ///////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
19 #include "wx/control.h"
20 #include "wx/dcmemory.h"
22 // ----------------------------------------------------------------------------
24 // ----------------------------------------------------------------------------
26 class EllipsizationTestCase
: public CppUnit::TestCase
29 EllipsizationTestCase() { }
32 CPPUNIT_TEST_SUITE( EllipsizationTestCase
);
33 CPPUNIT_TEST( NormalCase
);
34 CPPUNIT_TEST( EnoughSpace
);
35 CPPUNIT_TEST( VeryLittleSpace
);
36 CPPUNIT_TEST( HasThreeDots
);
37 CPPUNIT_TEST_SUITE_END();
41 void VeryLittleSpace();
44 DECLARE_NO_COPY_CLASS(EllipsizationTestCase
)
47 // register in the unnamed registry so that these tests are run by default
48 CPPUNIT_TEST_SUITE_REGISTRATION( EllipsizationTestCase
);
50 // also include in its own registry so that these tests can be run alone
51 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( EllipsizationTestCase
, "EllipsizationTestCase" );
53 void EllipsizationTestCase::NormalCase()
57 static const char *stringsToTest
[] =
63 "\xCE\xB1", // U03B1 (GREEK SMALL LETTER ALPHA)
65 "a very very very very very very very long string",
66 // alpha+beta+gamma+delta+epsilon+zeta+eta+theta+iota
67 "\xCE\xB1\xCE\xB2\xCE\xB3\xCE\xB4\xCE\xB5\xCE\xB6\xCE\xB7\xCE\xB8\xCE\xB9",
68 "\t", "\t\t\t\t\t", "a\tstring\twith\ttabs",
69 "\n", "\n\n\n\n\n", "a\nstring\nwith\nnewlines",
70 "&", "&&&&&&&", "a&string&with&newlines",
71 "\t\n&", "a\t\n&string\t\n&with\t\n&many\t\n&chars"
74 static const int flagsToTest
[] =
77 wxELLIPSIZE_FLAGS_PROCESS_MNEMONICS
,
78 wxELLIPSIZE_FLAGS_EXPAND_TABS
,
79 wxELLIPSIZE_FLAGS_PROCESS_MNEMONICS
| wxELLIPSIZE_FLAGS_EXPAND_TABS
82 static const wxEllipsizeMode modesToTest
[] =
89 int widthsToTest
[] = { 50, 100, 150 };
91 for ( unsigned int s
= 0; s
< WXSIZEOF(stringsToTest
); s
++ )
93 const wxString str
= wxString::FromUTF8(stringsToTest
[s
]);
95 for ( unsigned int f
= 0; f
< WXSIZEOF(flagsToTest
); f
++ )
97 for ( unsigned int m
= 0; m
< WXSIZEOF(modesToTest
); m
++ )
99 for ( unsigned int w
= 0; w
< WXSIZEOF(widthsToTest
); w
++ )
101 wxString ret
= wxControl::Ellipsize
113 "invalid ellipsization for \"%s\" (%dpx, should be <=%dpx)",
115 dc
.GetMultiLineTextExtent(ret
).GetWidth(),
118 dc
.GetMultiLineTextExtent(ret
).GetWidth() <= widthsToTest
[w
]
127 void EllipsizationTestCase::EnoughSpace()
129 // No ellipsization should occur if there's plenty of space.
133 CPPUNIT_ASSERT_EQUAL("some label",
134 wxControl::Ellipsize("some label", dc
, wxELLIPSIZE_START
, 200));
135 CPPUNIT_ASSERT_EQUAL("some label",
136 wxControl::Ellipsize("some label", dc
, wxELLIPSIZE_MIDDLE
, 200));
137 CPPUNIT_ASSERT_EQUAL("some label",
138 wxControl::Ellipsize("some label", dc
, wxELLIPSIZE_END
, 200));
142 void EllipsizationTestCase::VeryLittleSpace()
144 // If there's not enough space, the shortened label should still contain "..." and one character
148 CPPUNIT_ASSERT_EQUAL("...l",
149 wxControl::Ellipsize("some label", dc
, wxELLIPSIZE_START
, 5));
150 CPPUNIT_ASSERT_EQUAL("s...",
151 wxControl::Ellipsize("some label", dc
, wxELLIPSIZE_MIDDLE
, 5));
152 CPPUNIT_ASSERT_EQUAL("s...",
153 wxControl::Ellipsize("some label1", dc
, wxELLIPSIZE_MIDDLE
, 5));
154 CPPUNIT_ASSERT_EQUAL("s...",
155 wxControl::Ellipsize("some label", dc
, wxELLIPSIZE_END
, 5));
159 void EllipsizationTestCase::HasThreeDots()
163 CPPUNIT_ASSERT( wxControl::Ellipsize("some longer text", dc
, wxELLIPSIZE_START
, 80).StartsWith("...") );
164 CPPUNIT_ASSERT( !wxControl::Ellipsize("some longer text", dc
, wxELLIPSIZE_START
, 80).EndsWith("...") );
166 CPPUNIT_ASSERT( wxControl::Ellipsize("some longer text", dc
, wxELLIPSIZE_END
, 80).EndsWith("...") );
168 CPPUNIT_ASSERT( wxControl::Ellipsize("some longer text", dc
, wxELLIPSIZE_MIDDLE
, 80).Contains("...") );
169 CPPUNIT_ASSERT( !wxControl::Ellipsize("some longer text", dc
, wxELLIPSIZE_MIDDLE
, 80).StartsWith("...") );
170 CPPUNIT_ASSERT( !wxControl::Ellipsize("some longer text", dc
, wxELLIPSIZE_MIDDLE
, 80).EndsWith("...") );