The rounded corners look really dumb at this size.
[wxWidgets.git] / tests / graphics / ellipsization.cpp
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 // ----------------------------------------------------------------------------
10 // headers
11 // ----------------------------------------------------------------------------
12
13 #include "testprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #include "wx/control.h"
20 #include "wx/dcmemory.h"
21
22 // ----------------------------------------------------------------------------
23 // test class
24 // ----------------------------------------------------------------------------
25
26 class EllipsizationTestCase : public CppUnit::TestCase
27 {
28 public:
29 EllipsizationTestCase() { }
30
31 private:
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();
38
39 void NormalCase();
40 void EnoughSpace();
41 void VeryLittleSpace();
42 void HasThreeDots();
43
44 DECLARE_NO_COPY_CLASS(EllipsizationTestCase)
45 };
46
47 // register in the unnamed registry so that these tests are run by default
48 CPPUNIT_TEST_SUITE_REGISTRATION( EllipsizationTestCase );
49
50 // also include in its own registry so that these tests can be run alone
51 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( EllipsizationTestCase, "EllipsizationTestCase" );
52
53 void EllipsizationTestCase::NormalCase()
54 {
55 wxMemoryDC dc;
56
57 static const char *stringsToTest[] =
58 {
59 "N",
60 ".",
61 "x",
62 "foobar",
63 "\xCE\xB1", // U03B1 (GREEK SMALL LETTER ALPHA)
64 "Another test",
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"
72 };
73
74 static const int flagsToTest[] =
75 {
76 0,
77 wxELLIPSIZE_FLAGS_PROCESS_MNEMONICS,
78 wxELLIPSIZE_FLAGS_EXPAND_TABS,
79 wxELLIPSIZE_FLAGS_PROCESS_MNEMONICS | wxELLIPSIZE_FLAGS_EXPAND_TABS
80 };
81
82 static const wxEllipsizeMode modesToTest[] =
83 {
84 wxELLIPSIZE_START,
85 wxELLIPSIZE_MIDDLE,
86 wxELLIPSIZE_END
87 };
88
89 int widthsToTest[] = { 50, 100, 150 };
90
91 for ( unsigned int s = 0; s < WXSIZEOF(stringsToTest); s++ )
92 {
93 const wxString str = wxString::FromUTF8(stringsToTest[s]);
94
95 for ( unsigned int f = 0; f < WXSIZEOF(flagsToTest); f++ )
96 {
97 for ( unsigned int m = 0; m < WXSIZEOF(modesToTest); m++ )
98 {
99 for ( unsigned int w = 0; w < WXSIZEOF(widthsToTest); w++ )
100 {
101 wxString ret = wxControl::Ellipsize
102 (
103 str,
104 dc,
105 modesToTest[m],
106 widthsToTest[w],
107 flagsToTest[f]
108 );
109
110 WX_ASSERT_MESSAGE
111 (
112 (
113 "invalid ellipsization for \"%s\" (%dpx, should be <=%dpx)",
114 str,
115 dc.GetMultiLineTextExtent(ret).GetWidth(),
116 widthsToTest[w]
117 ),
118 dc.GetMultiLineTextExtent(ret).GetWidth() <= widthsToTest[w]
119 );
120 }
121 }
122 }
123 }
124 }
125
126
127 void EllipsizationTestCase::EnoughSpace()
128 {
129 // No ellipsization should occur if there's plenty of space.
130
131 wxMemoryDC dc;
132
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));
139 }
140
141
142 void EllipsizationTestCase::VeryLittleSpace()
143 {
144 // If there's not enough space, the shortened label should still contain "..." and one character
145
146 wxMemoryDC dc;
147
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));
156 }
157
158
159 void EllipsizationTestCase::HasThreeDots()
160 {
161 wxMemoryDC dc;
162
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("...") );
165
166 CPPUNIT_ASSERT( wxControl::Ellipsize("some longer text", dc, wxELLIPSIZE_END, 80).EndsWith("...") );
167
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("...") );
171 }