]> git.saurik.com Git - wxWidgets.git/blob - tests/controls/label.cpp
Always send wxEVT_COMMAND_RICHTEXT_DELETE event if delete or back key pressed
[wxWidgets.git] / tests / controls / label.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/label.cpp
3 // Purpose: wxControl and wxStaticText label tests
4 // Author: Francesco Montorsi
5 // Created: 2010-3-21
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 #ifndef WX_PRECOMP
21 #include "wx/app.h"
22 #endif // WX_PRECOMP
23
24 #include "wx/control.h"
25 #include "wx/stattext.h"
26 #include "wx/checkbox.h"
27
28 // ----------------------------------------------------------------------------
29 // test class
30 // ----------------------------------------------------------------------------
31
32 class LabelTestCase : public CppUnit::TestCase
33 {
34 public:
35 LabelTestCase() { }
36
37 virtual void setUp();
38 virtual void tearDown();
39
40 private:
41 CPPUNIT_TEST_SUITE( LabelTestCase );
42 CPPUNIT_TEST( GetLabel );
43 CPPUNIT_TEST( GetLabelText );
44 CPPUNIT_TEST( Statics );
45 CPPUNIT_TEST_SUITE_END();
46
47 void GetLabel();
48 void GetLabelText();
49 void Statics();
50
51 wxStaticText *m_st;
52
53 // we cannot test wxControl directly (it's abstract) so we rather test wxCheckBox
54 wxCheckBox *m_cb;
55
56 DECLARE_NO_COPY_CLASS(LabelTestCase)
57 };
58
59 // register in the unnamed registry so that these tests are run by default
60 CPPUNIT_TEST_SUITE_REGISTRATION( LabelTestCase );
61
62 // also include in its own registry so that these tests can be run alone
63 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( LabelTestCase, "LabelTestCase" );
64
65 // ----------------------------------------------------------------------------
66 // test initialization
67 // ----------------------------------------------------------------------------
68
69 #define ORIGINAL_LABEL "original label"
70
71 void LabelTestCase::setUp()
72 {
73 m_st = new wxStaticText(wxTheApp->GetTopWindow(), wxID_ANY, ORIGINAL_LABEL);
74
75 m_cb = new wxCheckBox(wxTheApp->GetTopWindow(), wxID_ANY, ORIGINAL_LABEL);
76
77 CPPUNIT_ASSERT_EQUAL( ORIGINAL_LABEL, m_st->GetLabel() );
78 CPPUNIT_ASSERT_EQUAL( ORIGINAL_LABEL, m_cb->GetLabel() );
79 }
80
81 void LabelTestCase::tearDown()
82 {
83 wxDELETE(m_st);
84 wxDELETE(m_cb);
85 }
86
87 // ----------------------------------------------------------------------------
88 // the tests themselves
89 // ----------------------------------------------------------------------------
90
91 #define SET_LABEL(str) \
92 m_st->SetLabel(str); \
93 m_cb->SetLabel(str);
94
95 #define SET_LABEL_TEXT(str) \
96 m_st->SetLabelText(str); \
97 m_cb->SetLabelText(str);
98
99 void LabelTestCase::GetLabel()
100 {
101 const wxString testLabelArray[] = {
102 "label without mnemonics and markup",
103 "label with &mnemonic",
104 "label with <span foreground='blue'>some</span> <b>markup</b>",
105 "label with <span foreground='blue'>some</span> <b>markup</b> and &mnemonic",
106 };
107
108 // test calls to SetLabel() and then to GetLabel()
109
110 for ( unsigned int s = 0; s < WXSIZEOF(testLabelArray); s++ )
111 {
112 SET_LABEL(testLabelArray[s]);
113
114 // GetLabel() should always return the string passed to SetLabel()
115 CPPUNIT_ASSERT_EQUAL( testLabelArray[s], m_st->GetLabel() );
116 CPPUNIT_ASSERT_EQUAL( testLabelArray[s], m_cb->GetLabel() );
117 }
118
119
120 // test calls to SetLabelText() and then to GetLabel()
121
122 const wxString& testLabel = "label without mnemonics and markup";
123 SET_LABEL_TEXT(testLabel);
124 CPPUNIT_ASSERT_EQUAL( testLabel, m_st->GetLabel() );
125 CPPUNIT_ASSERT_EQUAL( testLabel, m_cb->GetLabel() );
126
127 const wxString& testLabel2 = "label with &mnemonic";
128 const wxString& testLabelText2 = "label with &&mnemonic";
129 SET_LABEL_TEXT(testLabel2);
130 CPPUNIT_ASSERT_EQUAL( testLabelText2, m_st->GetLabel() );
131 CPPUNIT_ASSERT_EQUAL( testLabelText2, m_cb->GetLabel() );
132
133 const wxString& testLabel3 = "label with <span foreground='blue'>some</span> <b>markup</b>";
134 SET_LABEL_TEXT(testLabel3);
135 CPPUNIT_ASSERT_EQUAL( testLabel3, m_st->GetLabel() );
136 CPPUNIT_ASSERT_EQUAL( testLabel3, m_cb->GetLabel() );
137
138 const wxString& testLabel4 = "label with <span foreground='blue'>some</span> <b>markup</b> and &mnemonic";
139 const wxString& testLabelText4 = "label with <span foreground='blue'>some</span> <b>markup</b> and &&mnemonic";
140 SET_LABEL_TEXT(testLabel4);
141 CPPUNIT_ASSERT_EQUAL( testLabelText4, m_st->GetLabel() );
142 CPPUNIT_ASSERT_EQUAL( testLabelText4, m_cb->GetLabel() );
143 }
144
145 void LabelTestCase::GetLabelText()
146 {
147 // test calls to SetLabel() and then to GetLabelText()
148
149 const wxString& testLabel = "label without mnemonics and markup";
150 SET_LABEL(testLabel);
151 CPPUNIT_ASSERT_EQUAL( testLabel, m_st->GetLabelText() );
152 CPPUNIT_ASSERT_EQUAL( testLabel, m_cb->GetLabelText() );
153
154 const wxString& testLabel2 = "label with &mnemonic";
155 const wxString& testLabelText2 = "label with mnemonic";
156 SET_LABEL(testLabel2);
157 CPPUNIT_ASSERT_EQUAL( testLabelText2, m_st->GetLabelText() );
158 CPPUNIT_ASSERT_EQUAL( testLabelText2, m_cb->GetLabelText() );
159
160 const wxString& testLabel3 = "label with <span foreground='blue'>some</span> <b>markup</b>";
161 SET_LABEL(testLabel3);
162 CPPUNIT_ASSERT_EQUAL( testLabel3, m_st->GetLabelText() );
163 CPPUNIT_ASSERT_EQUAL( testLabel3, m_cb->GetLabelText() );
164
165 const wxString& testLabel4 = "label with <span foreground='blue'>some</span> <b>markup</b> and &mnemonic";
166 const wxString& testLabelText4 = "label with <span foreground='blue'>some</span> <b>markup</b> and mnemonic";
167 SET_LABEL(testLabel4);
168 CPPUNIT_ASSERT_EQUAL( testLabelText4, m_st->GetLabelText() );
169 CPPUNIT_ASSERT_EQUAL( testLabelText4, m_cb->GetLabelText() );
170
171
172 const wxString testLabelArray[] = {
173 "label without mnemonics and markup",
174 "label with &mnemonic",
175 "label with <span foreground='blue'>some</span> <b>markup</b>",
176 "label with <span foreground='blue'>some</span> <b>markup</b> and &mnemonic",
177 };
178
179 // test calls to SetLabelText() and then to GetLabelText()
180
181 for ( unsigned int s = 0; s < WXSIZEOF(testLabelArray); s++ )
182 {
183 SET_LABEL_TEXT(testLabelArray[s]);
184
185 // GetLabelText() should always return the string passed to SetLabelText()
186 CPPUNIT_ASSERT_EQUAL( testLabelArray[s], m_st->GetLabelText() );
187 CPPUNIT_ASSERT_EQUAL( testLabelArray[s], m_cb->GetLabelText() );
188 }
189 }
190
191 void LabelTestCase::Statics()
192 {
193 CPPUNIT_ASSERT_EQUAL( "mnemonic", wxControl::RemoveMnemonics("&mnemonic") );
194 CPPUNIT_ASSERT_EQUAL( "&mnemonic", wxControl::RemoveMnemonics("&&mnemonic") );
195 CPPUNIT_ASSERT_EQUAL( "&mnemonic", wxControl::RemoveMnemonics("&&&mnemonic") );
196 }