]> git.saurik.com Git - wxWidgets.git/blame - tests/graphics/ellipsization.cpp
Applied patch #15540: wxRichTextTable: crashes due to an invalid focus object (dghart)
[wxWidgets.git] / tests / graphics / ellipsization.cpp
CommitLineData
a41b1a1b
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: tests/graphics/ellipsization.cpp
3// Purpose: wxControlBase::*Ellipsize* unit test
4// Author: Francesco Montorsi
5// Created: 2010-03-10
a41b1a1b
VZ
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
26class EllipsizationTestCase : public CppUnit::TestCase
27{
28public:
29 EllipsizationTestCase() { }
30
31private:
32 CPPUNIT_TEST_SUITE( EllipsizationTestCase );
9a493724
VS
33 CPPUNIT_TEST( NormalCase );
34 CPPUNIT_TEST( EnoughSpace );
35 CPPUNIT_TEST( VeryLittleSpace );
36 CPPUNIT_TEST( HasThreeDots );
a41b1a1b
VZ
37 CPPUNIT_TEST_SUITE_END();
38
9a493724
VS
39 void NormalCase();
40 void EnoughSpace();
41 void VeryLittleSpace();
42 void HasThreeDots();
a41b1a1b
VZ
43
44 DECLARE_NO_COPY_CLASS(EllipsizationTestCase)
45};
46
47// register in the unnamed registry so that these tests are run by default
48CPPUNIT_TEST_SUITE_REGISTRATION( EllipsizationTestCase );
49
e3778b4d 50// also include in its own registry so that these tests can be run alone
a41b1a1b
VZ
51CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( EllipsizationTestCase, "EllipsizationTestCase" );
52
9a493724 53void EllipsizationTestCase::NormalCase()
a41b1a1b
VZ
54{
55 wxMemoryDC dc;
56
968d3a24
VZ
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",
a41b1a1b
VZ
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 };
968d3a24
VZ
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
9a493724 89 int widthsToTest[] = { 50, 100, 150 };
a41b1a1b 90
968d3a24
VZ
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++ )
a41b1a1b 100 {
9a493724 101 wxString ret = wxControl::Ellipsize
968d3a24
VZ
102 (
103 str,
104 dc,
105 modesToTest[m],
106 widthsToTest[w],
107 flagsToTest[f]
108 );
a41b1a1b 109
968d3a24
VZ
110 WX_ASSERT_MESSAGE
111 (
9a493724
VS
112 (
113 "invalid ellipsization for \"%s\" (%dpx, should be <=%dpx)",
114 str,
115 dc.GetMultiLineTextExtent(ret).GetWidth(),
116 widthsToTest[w]
117 ),
968d3a24
VZ
118 dc.GetMultiLineTextExtent(ret).GetWidth() <= widthsToTest[w]
119 );
a41b1a1b 120 }
968d3a24
VZ
121 }
122 }
123 }
a41b1a1b 124}
9a493724
VS
125
126
127void 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
142void 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
159void 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}