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 "textentrytest.h"
28 #include "testableframe.h"
29 #include "asserthelper.h"
30 #include "wx/uiaction.h"
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
36 class TextCtrlTestCase
: public TextEntryTestCase
, public CppUnit::TestCase
39 TextCtrlTestCase() { }
42 virtual void tearDown();
45 virtual wxTextEntry
*GetTestEntry() const { return m_text
; }
46 virtual wxWindow
*GetTestWindow() const { return m_text
; }
48 CPPUNIT_TEST_SUITE( TextCtrlTestCase
);
50 CPPUNIT_TEST( MultiLineReplace
);
51 WXUISIM_TEST( ReadOnly
);
52 WXUISIM_TEST( MaxLength
);
53 CPPUNIT_TEST( StreamInput
);
54 CPPUNIT_TEST( Redirector
);
55 //WXUISIM_TEST( ProcessEnter );
57 CPPUNIT_TEST( Style
);
58 CPPUNIT_TEST( Lines
);
59 CPPUNIT_TEST( LogTextCtrl
);
60 CPPUNIT_TEST_SUITE_END();
62 void MultiLineReplace();
67 //void ProcessEnter();
75 DECLARE_NO_COPY_CLASS(TextCtrlTestCase
)
78 // register in the unnamed registry so that these tests are run by default
79 CPPUNIT_TEST_SUITE_REGISTRATION( TextCtrlTestCase
);
81 // also include in it's own registry so that these tests can be run alone
82 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TextCtrlTestCase
, "TextCtrlTestCase" );
84 // ----------------------------------------------------------------------------
85 // test initialization
86 // ----------------------------------------------------------------------------
88 void TextCtrlTestCase::setUp()
90 m_text
= new wxTextCtrl(wxTheApp
->GetTopWindow(), wxID_ANY
);
93 void TextCtrlTestCase::tearDown()
98 // ----------------------------------------------------------------------------
100 // ----------------------------------------------------------------------------
102 void TextCtrlTestCase::MultiLineReplace()
104 // we need a multiline control for this test so recreate it
106 m_text
= new wxTextCtrl(wxTheApp
->GetTopWindow(), wxID_ANY
, "",
107 wxDefaultPosition
, wxDefaultSize
,
110 m_text
->SetValue("Hello replace\n"
112 m_text
->SetInsertionPoint(0);
114 m_text
->Replace(6, 13, "changed");
116 CPPUNIT_ASSERT_EQUAL("Hello changed\n"
119 CPPUNIT_ASSERT_EQUAL(13, m_text
->GetInsertionPoint());
121 m_text
->Replace(13, -1, "");
122 CPPUNIT_ASSERT_EQUAL("Hello changed", m_text
->GetValue());
123 CPPUNIT_ASSERT_EQUAL(13, m_text
->GetInsertionPoint());
126 m_text
= new wxTextCtrl(wxTheApp
->GetTopWindow(), wxID_ANY
);
129 void TextCtrlTestCase::ReadOnly()
131 #if wxUSE_UIACTIONSIMULATOR
132 // we need a read only control for this test so recreate it
134 m_text
= new wxTextCtrl(wxTheApp
->GetTopWindow(), wxID_ANY
, "",
135 wxDefaultPosition
, wxDefaultSize
,
138 wxTestableFrame
* frame
= wxStaticCast(wxTheApp
->GetTopWindow(),
141 EventCounter
count(m_text
, wxEVT_COMMAND_TEXT_UPDATED
);
145 wxUIActionSimulator sim
;
149 CPPUNIT_ASSERT_EQUAL("", m_text
->GetValue());
150 CPPUNIT_ASSERT_EQUAL(0, frame
->GetEventCount());
152 // SetEditable() is supposed to override wxTE_READONLY
153 m_text
->SetEditable(true);
158 CPPUNIT_ASSERT_EQUAL("abcdef", m_text
->GetValue());
159 CPPUNIT_ASSERT_EQUAL(6, frame
->GetEventCount());
162 m_text
= new wxTextCtrl(wxTheApp
->GetTopWindow(), wxID_ANY
);
166 void TextCtrlTestCase::MaxLength()
168 #if wxUSE_UIACTIONSIMULATOR
169 wxTestableFrame
* frame
= wxStaticCast(wxTheApp
->GetTopWindow(),
172 EventCounter
count(m_text
, wxEVT_COMMAND_TEXT_UPDATED
);
173 EventCounter
count1(m_text
, wxEVT_COMMAND_TEXT_MAXLEN
);
176 m_text
->SetMaxLength(10);
178 wxUIActionSimulator sim
;
182 CPPUNIT_ASSERT_EQUAL(0, frame
->GetEventCount(wxEVT_COMMAND_TEXT_MAXLEN
));
187 CPPUNIT_ASSERT_EQUAL(0, frame
->GetEventCount(wxEVT_COMMAND_TEXT_MAXLEN
));
188 CPPUNIT_ASSERT_EQUAL(10, frame
->GetEventCount(wxEVT_COMMAND_TEXT_UPDATED
));
193 CPPUNIT_ASSERT_EQUAL(1, frame
->GetEventCount(wxEVT_COMMAND_TEXT_MAXLEN
));
194 CPPUNIT_ASSERT_EQUAL(0, frame
->GetEventCount(wxEVT_COMMAND_TEXT_UPDATED
));
196 m_text
->SetMaxLength(0);
201 CPPUNIT_ASSERT_EQUAL(0, frame
->GetEventCount(wxEVT_COMMAND_TEXT_MAXLEN
));
202 CPPUNIT_ASSERT_EQUAL(1, frame
->GetEventCount(wxEVT_COMMAND_TEXT_UPDATED
));
206 void TextCtrlTestCase::StreamInput()
209 *m_text
<< "stringinput"
217 CPPUNIT_ASSERT_EQUAL("stringinput1010003.142.71ab", m_text
->GetValue());
219 m_text
->SetValue("");
221 #if wxHAS_TEXT_WINDOW_STREAM
223 std::ostream
stream(m_text
);
225 // We don't test a wide character as this is not a wide stream
226 stream
<< "stringinput"
235 CPPUNIT_ASSERT_EQUAL("stringinput1010003.142.71a", m_text
->GetValue());
241 void TextCtrlTestCase::Redirector()
243 #if wxHAS_TEXT_WINDOW_STREAM && wxUSE_STD_IOSTREAM
245 wxStreamToTextRedirector
redirect(m_text
);
247 std::cout
<< "stringinput"
254 CPPUNIT_ASSERT_EQUAL("stringinput1010003.142.71a", m_text
->GetValue());
260 void TextCtrlTestCase::ProcessEnter()
262 #if wxUSE_UIACTIONSIMULATOR
263 wxTestableFrame
* frame
= wxStaticCast(wxTheApp
->GetTopWindow(),
266 EventCounter
count(m_text
, wxEVT_COMMAND_TEXT_ENTER
);
270 wxUIActionSimulator sim
;
271 sim
.Char(WXK_RETURN
);
274 CPPUNIT_ASSERT_EQUAL(0, frame
->GetEventCount(wxEVT_COMMAND_TEXT_ENTER
));
276 // we need a text control with wxTE_PROCESS_ENTER for this test
278 m_text
= new wxTextCtrl(wxTheApp
->GetTopWindow(), wxID_ANY
, "",
279 wxDefaultPosition
, wxDefaultSize
,
284 sim
.Char(WXK_RETURN
);
287 CPPUNIT_ASSERT_EQUAL(1, frame
->GetEventCount(wxEVT_COMMAND_TEXT_ENTER
));
292 void TextCtrlTestCase::Url()
294 #if wxUSE_UIACTIONSIMULATOR && defined(__WXMSW__)
296 m_text
= new wxTextCtrl(wxTheApp
->GetTopWindow(), wxID_ANY
, "",
297 wxDefaultPosition
, wxDefaultSize
,
298 wxTE_MULTILINE
| wxTE_RICH
| wxTE_AUTO_URL
);
300 wxTestableFrame
* frame
= wxStaticCast(wxTheApp
->GetTopWindow(),
303 EventCounter
count(m_text
, wxEVT_COMMAND_TEXT_URL
);
305 m_text
->AppendText("http://www.wxwidgets.org");
307 wxUIActionSimulator sim
;
308 sim
.MouseMove(m_text
->ClientToScreen(wxPoint(5, 5)));
312 CPPUNIT_ASSERT_EQUAL(1, frame
->GetEventCount());
316 void TextCtrlTestCase::Style()
320 // We need wxTE_RICH under windows for style support
321 m_text
= new wxTextCtrl(wxTheApp
->GetTopWindow(), wxID_ANY
, "",
322 wxDefaultPosition
, wxDefaultSize
, wxTE_RICH
);
324 // Red text on a white background
325 m_text
->SetDefaultStyle(wxTextAttr(*wxRED
, *wxWHITE
));
327 CPPUNIT_ASSERT_EQUAL(m_text
->GetDefaultStyle().GetTextColour(), *wxRED
);
328 CPPUNIT_ASSERT_EQUAL(m_text
->GetDefaultStyle().GetBackgroundColour(),
331 m_text
->AppendText("red on white ");
333 // Red text on a grey background
334 m_text
->SetDefaultStyle(wxTextAttr(wxNullColour
, *wxLIGHT_GREY
));
336 CPPUNIT_ASSERT_EQUAL(m_text
->GetDefaultStyle().GetTextColour(), *wxRED
);
337 CPPUNIT_ASSERT_EQUAL(m_text
->GetDefaultStyle().GetBackgroundColour(),
340 m_text
->AppendText("red on grey ");
342 // Blue text on a grey background
343 m_text
->SetDefaultStyle(wxTextAttr(*wxBLUE
));
346 CPPUNIT_ASSERT_EQUAL(m_text
->GetDefaultStyle().GetTextColour(), *wxBLUE
);
347 CPPUNIT_ASSERT_EQUAL(m_text
->GetDefaultStyle().GetBackgroundColour(),
350 m_text
->AppendText("blue on grey");
352 // Get getting the style at a specific location
355 // We have to check that styles are supported
356 if(m_text
->GetStyle(3, style
))
358 CPPUNIT_ASSERT_EQUAL(style
.GetTextColour(), *wxRED
);
359 CPPUNIT_ASSERT_EQUAL(style
.GetBackgroundColour(), *wxWHITE
);
362 // And then setting the style
363 if(m_text
->SetStyle(15, 18, style
))
365 m_text
->GetStyle(17, style
);
367 CPPUNIT_ASSERT_EQUAL(style
.GetTextColour(), *wxRED
);
368 CPPUNIT_ASSERT_EQUAL(style
.GetBackgroundColour(), *wxWHITE
);
373 void TextCtrlTestCase::Lines()
377 m_text
= new wxTextCtrl(wxTheApp
->GetTopWindow(), wxID_ANY
, "",
378 wxDefaultPosition
, wxSize(400, 200), wxTE_MULTILINE
| wxTE_DONTWRAP
);
380 m_text
->SetValue("line1\nline2\nlong long line 3");
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));
389 m_text
->AppendText("\n\nMore text on line 5");
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));
397 void TextCtrlTestCase::LogTextCtrl()
400 m_text
= new wxTextCtrl(wxTheApp
->GetTopWindow(), wxID_ANY
, "",
401 wxDefaultPosition
, wxSize(400, 200),
404 CPPUNIT_ASSERT(m_text
->IsEmpty());
406 wxLogTextCtrl
* logtext
= new wxLogTextCtrl(m_text
);
408 wxLog
* old
= wxLog::SetActiveTarget(logtext
);
410 logtext
->LogText("text");
412 delete wxLog::SetActiveTarget(old
);
414 CPPUNIT_ASSERT(!m_text
->IsEmpty());
417 #endif //wxUSE_TEXTCTRL