]> git.saurik.com Git - wxWidgets.git/blob - tests/controls/label.cpp
2cc8afccbb0dc77ee29a0987019a02815aad7a3c
[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, *m_stWithMarkup;
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 it's 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 m_stWithMarkup = new wxStaticText(wxTheApp->GetTopWindow(), wxID_ANY, ORIGINAL_LABEL,
75 wxDefaultPosition, wxDefaultSize, wxST_MARKUP);
76
77 m_cb = new wxCheckBox(wxTheApp->GetTopWindow(), wxID_ANY, ORIGINAL_LABEL);
78
79 CPPUNIT_ASSERT_EQUAL( ORIGINAL_LABEL, m_st->GetLabel() );
80 CPPUNIT_ASSERT_EQUAL( ORIGINAL_LABEL, m_stWithMarkup->GetLabel() );
81 CPPUNIT_ASSERT_EQUAL( ORIGINAL_LABEL, m_cb->GetLabel() );
82 }
83
84 void LabelTestCase::tearDown()
85 {
86 wxDELETE(m_st);
87 wxDELETE(m_stWithMarkup);
88 wxDELETE(m_cb);
89 }
90
91 // ----------------------------------------------------------------------------
92 // the tests themselves
93 // ----------------------------------------------------------------------------
94
95 #define SET_LABEL(str) \
96 m_st->SetLabel(str); \
97 m_stWithMarkup->SetLabel(str); \
98 m_cb->SetLabel(str);
99
100 #define SET_LABEL_TEXT(str) \
101 m_st->SetLabelText(str); \
102 m_stWithMarkup->SetLabelText(str); \
103 m_cb->SetLabelText(str);
104
105 void LabelTestCase::GetLabel()
106 {
107 const wxString testLabelArray[] = {
108 "label without mnemonics and markup",
109 "label with &mnemonic",
110 "label with <span foreground='blue'>some</span> <b>markup</b>",
111 "label with <span foreground='blue'>some</span> <b>markup</b> and &mnemonic",
112 };
113
114 // test calls to SetLabel() and then to GetLabel()
115
116 for ( unsigned int s = 0; s < WXSIZEOF(testLabelArray); s++ )
117 {
118 SET_LABEL(testLabelArray[s]);
119
120 // GetLabel() should always return the string passed to SetLabel()
121 CPPUNIT_ASSERT_EQUAL( testLabelArray[s], m_st->GetLabel() );
122 CPPUNIT_ASSERT_EQUAL( testLabelArray[s], m_stWithMarkup->GetLabel() );
123 CPPUNIT_ASSERT_EQUAL( testLabelArray[s], m_cb->GetLabel() );
124 }
125
126
127 // test calls to SetLabelText() and then to GetLabel()
128
129 const wxString& testLabel = "label without mnemonics and markup";
130 SET_LABEL_TEXT(testLabel);
131 CPPUNIT_ASSERT_EQUAL( testLabel, m_st->GetLabel() );
132 CPPUNIT_ASSERT_EQUAL( testLabel, m_stWithMarkup->GetLabel() );
133 CPPUNIT_ASSERT_EQUAL( testLabel, m_cb->GetLabel() );
134
135 const wxString& testLabel2 = "label with &mnemonic";
136 const wxString& testLabelText2 = "label with &&mnemonic";
137 SET_LABEL_TEXT(testLabel2);
138 CPPUNIT_ASSERT_EQUAL( testLabelText2, m_st->GetLabel() );
139 CPPUNIT_ASSERT_EQUAL( "label with &amp;mnemonic", m_stWithMarkup->GetLabel() );
140 CPPUNIT_ASSERT_EQUAL( testLabelText2, m_cb->GetLabel() );
141
142 const wxString& testLabel3 = "label with <span foreground='blue'>some</span> <b>markup</b>";
143 SET_LABEL_TEXT(testLabel3);
144 CPPUNIT_ASSERT_EQUAL( testLabel3, m_st->GetLabel() );
145 CPPUNIT_ASSERT_EQUAL( "label with &lt;span foreground=&apos;blue&apos;&gt;some&lt;/span&gt; &lt;b&gt;markup&lt;/b&gt;", m_stWithMarkup->GetLabel() );
146 CPPUNIT_ASSERT_EQUAL( testLabel3, m_cb->GetLabel() );
147
148 const wxString& testLabel4 = "label with <span foreground='blue'>some</span> <b>markup</b> and &mnemonic";
149 const wxString& testLabelText4 = "label with <span foreground='blue'>some</span> <b>markup</b> and &&mnemonic";
150 SET_LABEL_TEXT(testLabel4);
151 CPPUNIT_ASSERT_EQUAL( testLabelText4, m_st->GetLabel() );
152 CPPUNIT_ASSERT_EQUAL( "label with &lt;span foreground=&apos;blue&apos;&gt;some&lt;/span&gt; &lt;b&gt;markup&lt;/b&gt; and &amp;mnemonic", m_stWithMarkup->GetLabel() );
153 CPPUNIT_ASSERT_EQUAL( testLabelText4, m_cb->GetLabel() );
154 }
155
156 void LabelTestCase::GetLabelText()
157 {
158 // test calls to SetLabel() and then to GetLabelText()
159
160 const wxString& testLabel = "label without mnemonics and markup";
161 SET_LABEL(testLabel);
162 CPPUNIT_ASSERT_EQUAL( testLabel, m_st->GetLabelText() );
163 CPPUNIT_ASSERT_EQUAL( testLabel, m_stWithMarkup->GetLabelText() );
164 CPPUNIT_ASSERT_EQUAL( testLabel, m_cb->GetLabelText() );
165
166 const wxString& testLabel2 = "label with &mnemonic";
167 const wxString& testLabelText2 = "label with mnemonic";
168 SET_LABEL(testLabel2);
169 CPPUNIT_ASSERT_EQUAL( testLabelText2, m_st->GetLabelText() );
170 CPPUNIT_ASSERT_EQUAL( testLabelText2, m_stWithMarkup->GetLabelText() );
171 CPPUNIT_ASSERT_EQUAL( testLabelText2, m_cb->GetLabelText() );
172
173 const wxString& testLabel3 = "label with <span foreground='blue'>some</span> <b>markup</b>";
174 SET_LABEL(testLabel3);
175 CPPUNIT_ASSERT_EQUAL( testLabel3, m_st->GetLabelText() );
176 CPPUNIT_ASSERT_EQUAL( "label with some markup", m_stWithMarkup->GetLabelText() );
177 CPPUNIT_ASSERT_EQUAL( testLabel3, m_cb->GetLabelText() );
178
179 const wxString& testLabel4 = "label with <span foreground='blue'>some</span> <b>markup</b> and &mnemonic";
180 const wxString& testLabelText4 = "label with <span foreground='blue'>some</span> <b>markup</b> and mnemonic";
181 SET_LABEL(testLabel4);
182 CPPUNIT_ASSERT_EQUAL( testLabelText4, m_st->GetLabelText() );
183 CPPUNIT_ASSERT_EQUAL( "label with some markup and mnemonic", m_stWithMarkup->GetLabelText() );
184 CPPUNIT_ASSERT_EQUAL( testLabelText4, m_cb->GetLabelText() );
185
186
187 const wxString testLabelArray[] = {
188 "label without mnemonics and markup",
189 "label with &mnemonic",
190 "label with <span foreground='blue'>some</span> <b>markup</b>",
191 "label with <span foreground='blue'>some</span> <b>markup</b> and &mnemonic",
192 };
193
194 // test calls to SetLabelText() and then to GetLabelText()
195
196 for ( unsigned int s = 0; s < WXSIZEOF(testLabelArray); s++ )
197 {
198 SET_LABEL_TEXT(testLabelArray[s]);
199
200 // GetLabelText() should always return the string passed to SetLabelText()
201 CPPUNIT_ASSERT_EQUAL( testLabelArray[s], m_st->GetLabelText() );
202 CPPUNIT_ASSERT_EQUAL( testLabelArray[s], m_stWithMarkup->GetLabelText() );
203 CPPUNIT_ASSERT_EQUAL( testLabelArray[s], m_cb->GetLabelText() );
204 }
205 }
206
207 void LabelTestCase::Statics()
208 {
209 CPPUNIT_ASSERT_EQUAL( "mnemonic", wxControl::RemoveMnemonics("&mnemonic") );
210 CPPUNIT_ASSERT_EQUAL( "&mnemonic", wxControl::RemoveMnemonics("&&mnemonic") );
211 CPPUNIT_ASSERT_EQUAL( "&mnemonic", wxControl::RemoveMnemonics("&&&mnemonic") );
212 CPPUNIT_ASSERT_EQUAL( "", wxStaticText::RemoveMarkup("<b></b>") );
213 }