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