]> git.saurik.com Git - wxWidgets.git/blob - tests/controls/textctrltest.cpp
Merge the new GUI tests from SOC2010_GUI_TEST branch.
[wxWidgets.git] / tests / controls / textctrltest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/textctrltest.cpp
3 // Purpose: wxTextCtrl unit test
4 // Author: Vadim Zeitlin
5 // Created: 2007-09-25
6 // RCS-ID: $Id$
7 // Copyright: (c) 2007 Vadim Zeitlin <vadim@wxwidgets.org>
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "testprec.h"
15
16 #if wxUSE_TEXTCTRL
17
18 #ifdef __BORLANDC__
19 #pragma hdrstop
20 #endif
21
22 #ifndef WX_PRECOMP
23 #include "wx/app.h"
24 #include "wx/textctrl.h"
25 #endif // WX_PRECOMP
26
27 #include "textentrytest.h"
28 #include "testableframe.h"
29 #include "asserthelper.h"
30 #include "wx/uiaction.h"
31
32 // ----------------------------------------------------------------------------
33 // test class
34 // ----------------------------------------------------------------------------
35
36 class TextCtrlTestCase : public TextEntryTestCase, public CppUnit::TestCase
37 {
38 public:
39 TextCtrlTestCase() { }
40
41 virtual void setUp();
42 virtual void tearDown();
43
44 private:
45 virtual wxTextEntry *GetTestEntry() const { return m_text; }
46 virtual wxWindow *GetTestWindow() const { return m_text; }
47
48 CPPUNIT_TEST_SUITE( TextCtrlTestCase );
49 wxTEXT_ENTRY_TESTS();
50 CPPUNIT_TEST( MultiLineReplace );
51 WXUISIM_TEST( ReadOnly );
52 WXUISIM_TEST( MaxLength );
53 CPPUNIT_TEST( StreamInput );
54 CPPUNIT_TEST( Redirector );
55 //WXUISIM_TEST( ProcessEnter );
56 WXUISIM_TEST( Url );
57 CPPUNIT_TEST( Style );
58 CPPUNIT_TEST( Lines );
59 CPPUNIT_TEST( LogTextCtrl );
60 CPPUNIT_TEST_SUITE_END();
61
62 void MultiLineReplace();
63 void ReadOnly();
64 void MaxLength();
65 void StreamInput();
66 void Redirector();
67 //void ProcessEnter();
68 void Url();
69 void Style();
70 void Lines();
71 void LogTextCtrl();
72
73 wxTextCtrl *m_text;
74
75 DECLARE_NO_COPY_CLASS(TextCtrlTestCase)
76 };
77
78 // register in the unnamed registry so that these tests are run by default
79 CPPUNIT_TEST_SUITE_REGISTRATION( TextCtrlTestCase );
80
81 // also include in it's own registry so that these tests can be run alone
82 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TextCtrlTestCase, "TextCtrlTestCase" );
83
84 // ----------------------------------------------------------------------------
85 // test initialization
86 // ----------------------------------------------------------------------------
87
88 void TextCtrlTestCase::setUp()
89 {
90 m_text = new wxTextCtrl(wxTheApp->GetTopWindow(), wxID_ANY);
91 }
92
93 void TextCtrlTestCase::tearDown()
94 {
95 wxDELETE(m_text);
96 }
97
98 // ----------------------------------------------------------------------------
99 // tests themselves
100 // ----------------------------------------------------------------------------
101
102 void TextCtrlTestCase::MultiLineReplace()
103 {
104 // we need a multiline control for this test so recreate it
105 delete m_text;
106 m_text = new wxTextCtrl(wxTheApp->GetTopWindow(), wxID_ANY, "",
107 wxDefaultPosition, wxDefaultSize,
108 wxTE_MULTILINE);
109
110 m_text->SetValue("Hello replace\n"
111 "0123456789012");
112 m_text->SetInsertionPoint(0);
113
114 m_text->Replace(6, 13, "changed");
115
116 CPPUNIT_ASSERT_EQUAL("Hello changed\n"
117 "0123456789012",
118 m_text->GetValue());
119 CPPUNIT_ASSERT_EQUAL(13, m_text->GetInsertionPoint());
120
121 m_text->Replace(13, -1, "");
122 CPPUNIT_ASSERT_EQUAL("Hello changed", m_text->GetValue());
123 CPPUNIT_ASSERT_EQUAL(13, m_text->GetInsertionPoint());
124
125 delete m_text;
126 m_text = new wxTextCtrl(wxTheApp->GetTopWindow(), wxID_ANY);
127 }
128
129 void TextCtrlTestCase::ReadOnly()
130 {
131 #if wxUSE_UIACTIONSIMULATOR
132 // we need a read only control for this test so recreate it
133 delete m_text;
134 m_text = new wxTextCtrl(wxTheApp->GetTopWindow(), wxID_ANY, "",
135 wxDefaultPosition, wxDefaultSize,
136 wxTE_READONLY);
137
138 wxTestableFrame* frame = wxStaticCast(wxTheApp->GetTopWindow(),
139 wxTestableFrame);
140
141 EventCounter count(m_text, wxEVT_COMMAND_TEXT_UPDATED);
142
143 m_text->SetFocus();
144
145 wxUIActionSimulator sim;
146 sim.Text("abcdef");
147 wxYield();
148
149 CPPUNIT_ASSERT_EQUAL("", m_text->GetValue());
150 CPPUNIT_ASSERT_EQUAL(0, frame->GetEventCount());
151
152 // SetEditable() is supposed to override wxTE_READONLY
153 m_text->SetEditable(true);
154
155 sim.Text("abcdef");
156 wxYield();
157
158 CPPUNIT_ASSERT_EQUAL("abcdef", m_text->GetValue());
159 CPPUNIT_ASSERT_EQUAL(6, frame->GetEventCount());
160
161 delete m_text;
162 m_text = new wxTextCtrl(wxTheApp->GetTopWindow(), wxID_ANY);
163 #endif
164 }
165
166 void TextCtrlTestCase::MaxLength()
167 {
168 #if wxUSE_UIACTIONSIMULATOR
169 wxTestableFrame* frame = wxStaticCast(wxTheApp->GetTopWindow(),
170 wxTestableFrame);
171
172 EventCounter count(m_text, wxEVT_COMMAND_TEXT_UPDATED);
173 EventCounter count1(m_text, wxEVT_COMMAND_TEXT_MAXLEN);
174
175 m_text->SetFocus();
176 m_text->SetMaxLength(10);
177
178 wxUIActionSimulator sim;
179 sim.Text("abcdef");
180 wxYield();
181
182 CPPUNIT_ASSERT_EQUAL(0, frame->GetEventCount(wxEVT_COMMAND_TEXT_MAXLEN));
183
184 sim.Text("ghij");
185 wxYield();
186
187 CPPUNIT_ASSERT_EQUAL(0, frame->GetEventCount(wxEVT_COMMAND_TEXT_MAXLEN));
188 CPPUNIT_ASSERT_EQUAL(10, frame->GetEventCount(wxEVT_COMMAND_TEXT_UPDATED));
189
190 sim.Text("k");
191 wxYield();
192
193 CPPUNIT_ASSERT_EQUAL(1, frame->GetEventCount(wxEVT_COMMAND_TEXT_MAXLEN));
194 CPPUNIT_ASSERT_EQUAL(0, frame->GetEventCount(wxEVT_COMMAND_TEXT_UPDATED));
195
196 m_text->SetMaxLength(0);
197
198 sim.Text("k");
199 wxYield();
200
201 CPPUNIT_ASSERT_EQUAL(0, frame->GetEventCount(wxEVT_COMMAND_TEXT_MAXLEN));
202 CPPUNIT_ASSERT_EQUAL(1, frame->GetEventCount(wxEVT_COMMAND_TEXT_UPDATED));
203 #endif
204 }
205
206 void TextCtrlTestCase::StreamInput()
207 {
208 #ifndef __WXOSX__
209 *m_text << "stringinput"
210 << 10
211 << 1000L
212 << 3.14f
213 << 2.71
214 << 'a'
215 << L'b';
216
217 CPPUNIT_ASSERT_EQUAL("stringinput1010003.142.71ab", m_text->GetValue());
218
219 m_text->SetValue("");
220
221 #if wxHAS_TEXT_WINDOW_STREAM
222
223 std::ostream stream(m_text);
224
225 // We don't test a wide character as this is not a wide stream
226 stream << "stringinput"
227 << 10
228 << 1000L
229 << 3.14f
230 << 2.71
231 << 'a';
232
233 stream.flush();
234
235 CPPUNIT_ASSERT_EQUAL("stringinput1010003.142.71a", m_text->GetValue());
236
237 #endif
238 #endif
239 }
240
241 void TextCtrlTestCase::Redirector()
242 {
243 #if wxHAS_TEXT_WINDOW_STREAM && wxUSE_STD_IOSTREAM
244
245 wxStreamToTextRedirector redirect(m_text);
246
247 std::cout << "stringinput"
248 << 10
249 << 1000L
250 << 3.14f
251 << 2.71
252 << 'a';
253
254 CPPUNIT_ASSERT_EQUAL("stringinput1010003.142.71a", m_text->GetValue());
255
256 #endif
257 }
258
259 #if 0
260 void TextCtrlTestCase::ProcessEnter()
261 {
262 #if wxUSE_UIACTIONSIMULATOR
263 wxTestableFrame* frame = wxStaticCast(wxTheApp->GetTopWindow(),
264 wxTestableFrame);
265
266 EventCounter count(m_text, wxEVT_COMMAND_TEXT_ENTER);
267
268 m_text->SetFocus();
269
270 wxUIActionSimulator sim;
271 sim.Char(WXK_RETURN);
272 wxYield();
273
274 CPPUNIT_ASSERT_EQUAL(0, frame->GetEventCount(wxEVT_COMMAND_TEXT_ENTER));
275
276 // we need a text control with wxTE_PROCESS_ENTER for this test
277 delete m_text;
278 m_text = new wxTextCtrl(wxTheApp->GetTopWindow(), wxID_ANY, "",
279 wxDefaultPosition, wxDefaultSize,
280 wxTE_PROCESS_ENTER);
281
282 m_text->SetFocus();
283
284 sim.Char(WXK_RETURN);
285 wxYield();
286
287 CPPUNIT_ASSERT_EQUAL(1, frame->GetEventCount(wxEVT_COMMAND_TEXT_ENTER));
288 #endif
289 }
290 #endif
291
292 void TextCtrlTestCase::Url()
293 {
294 #if wxUSE_UIACTIONSIMULATOR && defined(__WXMSW__)
295 delete m_text;
296 m_text = new wxTextCtrl(wxTheApp->GetTopWindow(), wxID_ANY, "",
297 wxDefaultPosition, wxDefaultSize,
298 wxTE_MULTILINE | wxTE_RICH | wxTE_AUTO_URL);
299
300 wxTestableFrame* frame = wxStaticCast(wxTheApp->GetTopWindow(),
301 wxTestableFrame);
302
303 EventCounter count(m_text, wxEVT_COMMAND_TEXT_URL);
304
305 m_text->AppendText("http://www.wxwidgets.org");
306
307 wxUIActionSimulator sim;
308 sim.MouseMove(m_text->ClientToScreen(wxPoint(5, 5)));
309 sim.MouseClick();
310 wxYield();
311
312 CPPUNIT_ASSERT_EQUAL(1, frame->GetEventCount());
313 #endif
314 }
315
316 void TextCtrlTestCase::Style()
317 {
318 #ifndef __WXOSX__
319 delete m_text;
320 // We need wxTE_RICH under windows for style support
321 m_text = new wxTextCtrl(wxTheApp->GetTopWindow(), wxID_ANY, "",
322 wxDefaultPosition, wxDefaultSize, wxTE_RICH);
323
324 // Red text on a white background
325 m_text->SetDefaultStyle(wxTextAttr(*wxRED, *wxWHITE));
326
327 CPPUNIT_ASSERT_EQUAL(m_text->GetDefaultStyle().GetTextColour(), *wxRED);
328 CPPUNIT_ASSERT_EQUAL(m_text->GetDefaultStyle().GetBackgroundColour(),
329 *wxWHITE);
330
331 m_text->AppendText("red on white ");
332
333 // Red text on a grey background
334 m_text->SetDefaultStyle(wxTextAttr(wxNullColour, *wxLIGHT_GREY));
335
336 CPPUNIT_ASSERT_EQUAL(m_text->GetDefaultStyle().GetTextColour(), *wxRED);
337 CPPUNIT_ASSERT_EQUAL(m_text->GetDefaultStyle().GetBackgroundColour(),
338 *wxLIGHT_GREY);
339
340 m_text->AppendText("red on grey ");
341
342 // Blue text on a grey background
343 m_text->SetDefaultStyle(wxTextAttr(*wxBLUE));
344
345
346 CPPUNIT_ASSERT_EQUAL(m_text->GetDefaultStyle().GetTextColour(), *wxBLUE);
347 CPPUNIT_ASSERT_EQUAL(m_text->GetDefaultStyle().GetBackgroundColour(),
348 *wxLIGHT_GREY);
349
350 m_text->AppendText("blue on grey");
351
352 // Get getting the style at a specific location
353 wxTextAttr style;
354
355 // We have to check that styles are supported
356 if(m_text->GetStyle(3, style))
357 {
358 CPPUNIT_ASSERT_EQUAL(style.GetTextColour(), *wxRED);
359 CPPUNIT_ASSERT_EQUAL(style.GetBackgroundColour(), *wxWHITE);
360 }
361
362 // And then setting the style
363 if(m_text->SetStyle(15, 18, style))
364 {
365 m_text->GetStyle(17, style);
366
367 CPPUNIT_ASSERT_EQUAL(style.GetTextColour(), *wxRED);
368 CPPUNIT_ASSERT_EQUAL(style.GetBackgroundColour(), *wxWHITE);
369 }
370 #endif
371 }
372
373 void TextCtrlTestCase::Lines()
374 {
375 #ifndef __WXOSX__
376 delete m_text;
377 m_text = new wxTextCtrl(wxTheApp->GetTopWindow(), wxID_ANY, "",
378 wxDefaultPosition, wxSize(400, 200), wxTE_MULTILINE | wxTE_DONTWRAP);
379
380 m_text->SetValue("line1\nline2\nlong long line 3");
381 m_text->Refresh();
382 m_text->Update();
383
384 CPPUNIT_ASSERT_EQUAL(3, m_text->GetNumberOfLines());
385 CPPUNIT_ASSERT_EQUAL(5, m_text->GetLineLength(0));
386 CPPUNIT_ASSERT_EQUAL("line2", m_text->GetLineText(1));
387 CPPUNIT_ASSERT_EQUAL(16, m_text->GetLineLength(2));
388
389 m_text->AppendText("\n\nMore text on line 5");
390
391 CPPUNIT_ASSERT_EQUAL(5, m_text->GetNumberOfLines());
392 CPPUNIT_ASSERT_EQUAL(0, m_text->GetLineLength(3));
393 CPPUNIT_ASSERT_EQUAL("", m_text->GetLineText(3));
394 #endif
395 }
396
397 void TextCtrlTestCase::LogTextCtrl()
398 {
399 delete m_text;
400 m_text = new wxTextCtrl(wxTheApp->GetTopWindow(), wxID_ANY, "",
401 wxDefaultPosition, wxSize(400, 200),
402 wxTE_MULTILINE);
403
404 CPPUNIT_ASSERT(m_text->IsEmpty());
405
406 wxLogTextCtrl* logtext = new wxLogTextCtrl(m_text);
407
408 wxLog* old = wxLog::SetActiveTarget(logtext);
409
410 logtext->LogText("text");
411
412 delete wxLog::SetActiveTarget(old);
413
414 CPPUNIT_ASSERT(!m_text->IsEmpty());
415 }
416
417 #endif //wxUSE_TEXTCTRL