1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/textctrltest.cpp
3 // Purpose: wxTextCtrl unit test
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2007 Vadim Zeitlin <vadim@wxwidgets.org>
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
24 #include "wx/textctrl.h"
27 #include "wx/scopeguard.h"
29 #include "textentrytest.h"
30 #include "testableframe.h"
31 #include "asserthelper.h"
32 #include "wx/uiaction.h"
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 class TextCtrlTestCase
: public TextEntryTestCase
, public CppUnit::TestCase
41 TextCtrlTestCase() { }
44 virtual void tearDown();
47 virtual wxTextEntry
*GetTestEntry() const { return m_text
; }
48 virtual wxWindow
*GetTestWindow() const { return m_text
; }
50 CPPUNIT_TEST_SUITE( TextCtrlTestCase
);
52 CPPUNIT_TEST( MultiLineReplace
);
53 WXUISIM_TEST( ReadOnly
);
54 WXUISIM_TEST( MaxLength
);
55 CPPUNIT_TEST( StreamInput
);
56 CPPUNIT_TEST( Redirector
);
57 //WXUISIM_TEST( ProcessEnter );
59 CPPUNIT_TEST( Style
);
60 CPPUNIT_TEST( Lines
);
61 CPPUNIT_TEST( LogTextCtrl
);
62 CPPUNIT_TEST_SUITE_END();
64 void MultiLineReplace();
69 //void ProcessEnter();
77 DECLARE_NO_COPY_CLASS(TextCtrlTestCase
)
80 // register in the unnamed registry so that these tests are run by default
81 CPPUNIT_TEST_SUITE_REGISTRATION( TextCtrlTestCase
);
83 // also include in it's own registry so that these tests can be run alone
84 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TextCtrlTestCase
, "TextCtrlTestCase" );
86 // ----------------------------------------------------------------------------
87 // test initialization
88 // ----------------------------------------------------------------------------
90 void TextCtrlTestCase::setUp()
92 m_text
= new wxTextCtrl(wxTheApp
->GetTopWindow(), wxID_ANY
);
95 void TextCtrlTestCase::tearDown()
100 // ----------------------------------------------------------------------------
102 // ----------------------------------------------------------------------------
104 void TextCtrlTestCase::MultiLineReplace()
106 // we need a multiline control for this test so recreate it
108 m_text
= new wxTextCtrl(wxTheApp
->GetTopWindow(), wxID_ANY
, "",
109 wxDefaultPosition
, wxDefaultSize
,
112 m_text
->SetValue("Hello replace\n"
114 m_text
->SetInsertionPoint(0);
116 m_text
->Replace(6, 13, "changed");
118 CPPUNIT_ASSERT_EQUAL("Hello changed\n"
121 CPPUNIT_ASSERT_EQUAL(13, m_text
->GetInsertionPoint());
123 m_text
->Replace(13, -1, "");
124 CPPUNIT_ASSERT_EQUAL("Hello changed", m_text
->GetValue());
125 CPPUNIT_ASSERT_EQUAL(13, m_text
->GetInsertionPoint());
128 m_text
= new wxTextCtrl(wxTheApp
->GetTopWindow(), wxID_ANY
);
131 void TextCtrlTestCase::ReadOnly()
133 #if wxUSE_UIACTIONSIMULATOR
134 // we need a read only control for this test so recreate it
136 m_text
= new wxTextCtrl(wxTheApp
->GetTopWindow(), wxID_ANY
, "",
137 wxDefaultPosition
, wxDefaultSize
,
140 wxTestableFrame
* frame
= wxStaticCast(wxTheApp
->GetTopWindow(),
143 EventCounter
count(m_text
, wxEVT_COMMAND_TEXT_UPDATED
);
147 wxUIActionSimulator sim
;
151 CPPUNIT_ASSERT_EQUAL("", m_text
->GetValue());
152 CPPUNIT_ASSERT_EQUAL(0, frame
->GetEventCount());
154 // SetEditable() is supposed to override wxTE_READONLY
155 m_text
->SetEditable(true);
160 CPPUNIT_ASSERT_EQUAL("abcdef", m_text
->GetValue());
161 CPPUNIT_ASSERT_EQUAL(6, frame
->GetEventCount());
164 m_text
= new wxTextCtrl(wxTheApp
->GetTopWindow(), wxID_ANY
);
168 void TextCtrlTestCase::MaxLength()
170 #if wxUSE_UIACTIONSIMULATOR
171 wxTestableFrame
* frame
= wxStaticCast(wxTheApp
->GetTopWindow(),
174 EventCounter
count(m_text
, wxEVT_COMMAND_TEXT_UPDATED
);
175 EventCounter
count1(m_text
, wxEVT_COMMAND_TEXT_MAXLEN
);
178 m_text
->SetMaxLength(10);
180 wxUIActionSimulator sim
;
184 CPPUNIT_ASSERT_EQUAL(0, frame
->GetEventCount(wxEVT_COMMAND_TEXT_MAXLEN
));
189 CPPUNIT_ASSERT_EQUAL(0, frame
->GetEventCount(wxEVT_COMMAND_TEXT_MAXLEN
));
190 CPPUNIT_ASSERT_EQUAL(10, frame
->GetEventCount(wxEVT_COMMAND_TEXT_UPDATED
));
195 CPPUNIT_ASSERT_EQUAL(1, frame
->GetEventCount(wxEVT_COMMAND_TEXT_MAXLEN
));
196 CPPUNIT_ASSERT_EQUAL(0, frame
->GetEventCount(wxEVT_COMMAND_TEXT_UPDATED
));
198 m_text
->SetMaxLength(0);
203 CPPUNIT_ASSERT_EQUAL(0, frame
->GetEventCount(wxEVT_COMMAND_TEXT_MAXLEN
));
204 CPPUNIT_ASSERT_EQUAL(1, frame
->GetEventCount(wxEVT_COMMAND_TEXT_UPDATED
));
208 void TextCtrlTestCase::StreamInput()
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
);
216 *m_text
<< "stringinput"
225 CPPUNIT_ASSERT_EQUAL("stringinput1010003.142.71ab", m_text
->GetValue());
227 m_text
->SetValue("");
229 #if wxHAS_TEXT_WINDOW_STREAM
231 std::ostream
stream(m_text
);
233 // We don't test a wide character as this is not a wide stream
234 stream
<< "stringinput"
243 CPPUNIT_ASSERT_EQUAL("stringinput1010003.142.71a", m_text
->GetValue());
245 #endif // wxHAS_TEXT_WINDOW_STREAM
249 void TextCtrlTestCase::Redirector()
251 #if wxHAS_TEXT_WINDOW_STREAM && wxUSE_STD_IOSTREAM
253 wxStreamToTextRedirector
redirect(m_text
);
255 std::cout
<< "stringinput"
262 CPPUNIT_ASSERT_EQUAL("stringinput1010003.142.71a", m_text
->GetValue());
268 void TextCtrlTestCase::ProcessEnter()
270 #if wxUSE_UIACTIONSIMULATOR
271 wxTestableFrame
* frame
= wxStaticCast(wxTheApp
->GetTopWindow(),
274 EventCounter
count(m_text
, wxEVT_COMMAND_TEXT_ENTER
);
278 wxUIActionSimulator sim
;
279 sim
.Char(WXK_RETURN
);
282 CPPUNIT_ASSERT_EQUAL(0, frame
->GetEventCount(wxEVT_COMMAND_TEXT_ENTER
));
284 // we need a text control with wxTE_PROCESS_ENTER for this test
286 m_text
= new wxTextCtrl(wxTheApp
->GetTopWindow(), wxID_ANY
, "",
287 wxDefaultPosition
, wxDefaultSize
,
292 sim
.Char(WXK_RETURN
);
295 CPPUNIT_ASSERT_EQUAL(1, frame
->GetEventCount(wxEVT_COMMAND_TEXT_ENTER
));
300 void TextCtrlTestCase::Url()
302 #if wxUSE_UIACTIONSIMULATOR && defined(__WXMSW__)
304 m_text
= new wxTextCtrl(wxTheApp
->GetTopWindow(), wxID_ANY
, "",
305 wxDefaultPosition
, wxDefaultSize
,
306 wxTE_MULTILINE
| wxTE_RICH
| wxTE_AUTO_URL
);
308 wxTestableFrame
* frame
= wxStaticCast(wxTheApp
->GetTopWindow(),
311 EventCounter
count(m_text
, wxEVT_COMMAND_TEXT_URL
);
313 m_text
->AppendText("http://www.wxwidgets.org");
315 wxUIActionSimulator sim
;
316 sim
.MouseMove(m_text
->ClientToScreen(wxPoint(5, 5)));
320 CPPUNIT_ASSERT_EQUAL(1, frame
->GetEventCount());
324 void TextCtrlTestCase::Style()
328 // We need wxTE_RICH under windows for style support
329 m_text
= new wxTextCtrl(wxTheApp
->GetTopWindow(), wxID_ANY
, "",
330 wxDefaultPosition
, wxDefaultSize
, wxTE_RICH
);
332 // Red text on a white background
333 m_text
->SetDefaultStyle(wxTextAttr(*wxRED
, *wxWHITE
));
335 CPPUNIT_ASSERT_EQUAL(m_text
->GetDefaultStyle().GetTextColour(), *wxRED
);
336 CPPUNIT_ASSERT_EQUAL(m_text
->GetDefaultStyle().GetBackgroundColour(),
339 m_text
->AppendText("red on white ");
341 // Red text on a grey background
342 m_text
->SetDefaultStyle(wxTextAttr(wxNullColour
, *wxLIGHT_GREY
));
344 CPPUNIT_ASSERT_EQUAL(m_text
->GetDefaultStyle().GetTextColour(), *wxRED
);
345 CPPUNIT_ASSERT_EQUAL(m_text
->GetDefaultStyle().GetBackgroundColour(),
348 m_text
->AppendText("red on grey ");
350 // Blue text on a grey background
351 m_text
->SetDefaultStyle(wxTextAttr(*wxBLUE
));
354 CPPUNIT_ASSERT_EQUAL(m_text
->GetDefaultStyle().GetTextColour(), *wxBLUE
);
355 CPPUNIT_ASSERT_EQUAL(m_text
->GetDefaultStyle().GetBackgroundColour(),
358 m_text
->AppendText("blue on grey");
360 // Get getting the style at a specific location
363 // We have to check that styles are supported
364 if(m_text
->GetStyle(3, style
))
366 CPPUNIT_ASSERT_EQUAL(style
.GetTextColour(), *wxRED
);
367 CPPUNIT_ASSERT_EQUAL(style
.GetBackgroundColour(), *wxWHITE
);
370 // And then setting the style
371 if(m_text
->SetStyle(15, 18, style
))
373 m_text
->GetStyle(17, style
);
375 CPPUNIT_ASSERT_EQUAL(style
.GetTextColour(), *wxRED
);
376 CPPUNIT_ASSERT_EQUAL(style
.GetBackgroundColour(), *wxWHITE
);
381 void TextCtrlTestCase::Lines()
385 m_text
= new wxTextCtrl(wxTheApp
->GetTopWindow(), wxID_ANY
, "",
386 wxDefaultPosition
, wxSize(400, 200), wxTE_MULTILINE
| wxTE_DONTWRAP
);
388 m_text
->SetValue("line1\nline2\nlong long line 3");
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));
397 m_text
->AppendText("\n\nMore text on line 5");
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));
405 void TextCtrlTestCase::LogTextCtrl()
408 m_text
= new wxTextCtrl(wxTheApp
->GetTopWindow(), wxID_ANY
, "",
409 wxDefaultPosition
, wxSize(400, 200),
412 CPPUNIT_ASSERT(m_text
->IsEmpty());
414 wxLogTextCtrl
* logtext
= new wxLogTextCtrl(m_text
);
416 wxLog
* old
= wxLog::SetActiveTarget(logtext
);
418 logtext
->LogText("text");
420 delete wxLog::SetActiveTarget(old
);
422 CPPUNIT_ASSERT(!m_text
->IsEmpty());
425 #endif //wxUSE_TEXTCTRL