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