1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/graphics/ellipsization.cpp
3 // Purpose: wxControlBase::*Ellipsize* unit test
4 // Author: Francesco Montorsi
7 // Copyright: (c) 2010 Francesco Montorsi
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
20 #include "wx/control.h"
21 #include "wx/dcmemory.h"
23 // ----------------------------------------------------------------------------
25 // ----------------------------------------------------------------------------
27 class EllipsizationTestCase
: public CppUnit::TestCase
30 EllipsizationTestCase() { }
33 CPPUNIT_TEST_SUITE( EllipsizationTestCase
);
34 CPPUNIT_TEST( NormalCase
);
35 CPPUNIT_TEST( EnoughSpace
);
36 CPPUNIT_TEST( VeryLittleSpace
);
37 CPPUNIT_TEST( HasThreeDots
);
38 CPPUNIT_TEST_SUITE_END();
42 void VeryLittleSpace();
45 DECLARE_NO_COPY_CLASS(EllipsizationTestCase
)
48 // register in the unnamed registry so that these tests are run by default
49 CPPUNIT_TEST_SUITE_REGISTRATION( EllipsizationTestCase
);
51 // also include in it's own registry so that these tests can be run alone
52 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( EllipsizationTestCase
, "EllipsizationTestCase" );
54 void EllipsizationTestCase::NormalCase()
58 static const char *stringsToTest
[] =
64 "\xCE\xB1", // U03B1 (GREEK SMALL LETTER ALPHA)
66 "a very very very very very very very long string",
67 // alpha+beta+gamma+delta+epsilon+zeta+eta+theta+iota
68 "\xCE\xB1\xCE\xB2\xCE\xB3\xCE\xB4\xCE\xB5\xCE\xB6\xCE\xB7\xCE\xB8\xCE\xB9",
69 "\t", "\t\t\t\t\t", "a\tstring\twith\ttabs",
70 "\n", "\n\n\n\n\n", "a\nstring\nwith\nnewlines",
71 "&", "&&&&&&&", "a&string&with&newlines",
72 "\t\n&", "a\t\n&string\t\n&with\t\n&many\t\n&chars"
75 static const int flagsToTest
[] =
78 wxELLIPSIZE_FLAGS_PROCESS_MNEMONICS
,
79 wxELLIPSIZE_FLAGS_EXPAND_TABS
,
80 wxELLIPSIZE_FLAGS_PROCESS_MNEMONICS
| wxELLIPSIZE_FLAGS_EXPAND_TABS
83 static const wxEllipsizeMode modesToTest
[] =
90 int widthsToTest
[] = { 50, 100, 150 };
92 for ( unsigned int s
= 0; s
< WXSIZEOF(stringsToTest
); s
++ )
94 const wxString str
= wxString::FromUTF8(stringsToTest
[s
]);
96 for ( unsigned int f
= 0; f
< WXSIZEOF(flagsToTest
); f
++ )
98 for ( unsigned int m
= 0; m
< WXSIZEOF(modesToTest
); m
++ )
100 for ( unsigned int w
= 0; w
< WXSIZEOF(widthsToTest
); w
++ )
102 wxString ret
= wxControl::Ellipsize
114 "invalid ellipsization for \"%s\" (%dpx, should be <=%dpx)",
116 dc
.GetMultiLineTextExtent(ret
).GetWidth(),
119 dc
.GetMultiLineTextExtent(ret
).GetWidth() <= widthsToTest
[w
]
128 void EllipsizationTestCase::EnoughSpace()
130 // No ellipsization should occur if there's plenty of space.
134 CPPUNIT_ASSERT_EQUAL("some label",
135 wxControl::Ellipsize("some label", dc
, wxELLIPSIZE_START
, 200));
136 CPPUNIT_ASSERT_EQUAL("some label",
137 wxControl::Ellipsize("some label", dc
, wxELLIPSIZE_MIDDLE
, 200));
138 CPPUNIT_ASSERT_EQUAL("some label",
139 wxControl::Ellipsize("some label", dc
, wxELLIPSIZE_END
, 200));
143 void EllipsizationTestCase::VeryLittleSpace()
145 // If there's not enough space, the shortened label should still contain "..." and one character
149 CPPUNIT_ASSERT_EQUAL("...l",
150 wxControl::Ellipsize("some label", dc
, wxELLIPSIZE_START
, 5));
151 CPPUNIT_ASSERT_EQUAL("s...",
152 wxControl::Ellipsize("some label", dc
, wxELLIPSIZE_MIDDLE
, 5));
153 CPPUNIT_ASSERT_EQUAL("s...",
154 wxControl::Ellipsize("some label1", dc
, wxELLIPSIZE_MIDDLE
, 5));
155 CPPUNIT_ASSERT_EQUAL("s...",
156 wxControl::Ellipsize("some label", dc
, wxELLIPSIZE_END
, 5));
160 void EllipsizationTestCase::HasThreeDots()
164 CPPUNIT_ASSERT( wxControl::Ellipsize("some longer text", dc
, wxELLIPSIZE_START
, 80).StartsWith("...") );
165 CPPUNIT_ASSERT( !wxControl::Ellipsize("some longer text", dc
, wxELLIPSIZE_START
, 80).EndsWith("...") );
167 CPPUNIT_ASSERT( wxControl::Ellipsize("some longer text", dc
, wxELLIPSIZE_END
, 80).EndsWith("...") );
169 CPPUNIT_ASSERT( wxControl::Ellipsize("some longer text", dc
, wxELLIPSIZE_MIDDLE
, 80).Contains("...") );
170 CPPUNIT_ASSERT( !wxControl::Ellipsize("some longer text", dc
, wxELLIPSIZE_MIDDLE
, 80).StartsWith("...") );
171 CPPUNIT_ASSERT( !wxControl::Ellipsize("some longer text", dc
, wxELLIPSIZE_MIDDLE
, 80).EndsWith("...") );