Fix function wxControlBase::DoEllipsizeSingleLine to really make sure that the ellips...
[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( Ellipsize );
35 CPPUNIT_TEST_SUITE_END();
36
37 void Ellipsize();
38
39 DECLARE_NO_COPY_CLASS(EllipsizationTestCase)
40 };
41
42 // register in the unnamed registry so that these tests are run by default
43 CPPUNIT_TEST_SUITE_REGISTRATION( EllipsizationTestCase );
44
45 // also include in it's own registry so that these tests can be run alone
46 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( EllipsizationTestCase, "EllipsizationTestCase" );
47
48 void EllipsizationTestCase::Ellipsize()
49 {
50 wxMemoryDC dc;
51
52 wxString stringsToTest[] =
53 {
54 "N", ".", "x", "foobar", wxS("\u03B1"), "Another test", "a very very very very very very very long string",
55 "\xCE\xB1\xCE\xB2\xCE\xB3\xCE\xB4\xCE\xB5\xCE\xB6\xCE\xB7\xCE\xB8\xCE\xB9",
56 // alpha+beta+gamma+delta+epsilon+zeta+eta+theta+iota
57 "\t", "\t\t\t\t\t", "a\tstring\twith\ttabs",
58 "\n", "\n\n\n\n\n", "a\nstring\nwith\nnewlines",
59 "&", "&&&&&&&", "a&string&with&newlines",
60 "\t\n&", "a\t\n&string\t\n&with\t\n&many\t\n&chars"
61 };
62 int flagsToTest[] = { 0, wxELLIPSIZE_FLAGS_PROCESS_MNEMONICS, wxELLIPSIZE_FLAGS_EXPAND_TABS,
63 wxELLIPSIZE_FLAGS_PROCESS_MNEMONICS|wxELLIPSIZE_FLAGS_EXPAND_TABS };
64 wxEllipsizeMode modesToTest[] = { wxELLIPSIZE_START, wxELLIPSIZE_MIDDLE, wxELLIPSIZE_END };
65 int widthsToTest[] = { 0, 1, 2, 3, 10, 20, 100 };
66
67 for (unsigned int s=0; s<WXSIZEOF(stringsToTest); s++)
68 for (unsigned int f=0; f<WXSIZEOF(flagsToTest); f++)
69 for (unsigned int m=0; m<WXSIZEOF(modesToTest); m++)
70 for (unsigned int w=0; w<WXSIZEOF(widthsToTest); w++)
71 {
72 wxString ret = wxControlBase::Ellipsize(stringsToTest[s], dc, modesToTest[m],
73 widthsToTest[w], flagsToTest[f]);
74
75 CPPUNIT_ASSERT_MESSAGE((std::string)("invalid ellipsization for: " + stringsToTest[s]),
76 dc.GetMultiLineTextExtent(ret).GetWidth() <= widthsToTest[w]);
77 }
78 }