1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/textctrltest.cpp
3 // Purpose: wxTextCtrl unit test
4 // Author: Vadim Zeitlin
6 // Copyright: (c) 2007 Vadim Zeitlin <vadim@wxwidgets.org>
7 ///////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
23 #include "wx/textctrl.h"
26 #include "wx/scopeguard.h"
28 #include "textentrytest.h"
29 #include "testableframe.h"
30 #include "asserthelper.h"
31 #include "wx/uiaction.h"
33 static const int TEXT_HEIGHT
= 200;
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 class TextCtrlTestCase
: public TextEntryTestCase
, public CppUnit::TestCase
42 TextCtrlTestCase() { }
45 virtual void tearDown();
48 virtual wxTextEntry
*GetTestEntry() const { return m_text
; }
49 virtual wxWindow
*GetTestWindow() const { return m_text
; }
51 #define SINGLE_AND_MULTI_TESTS() \
52 WXUISIM_TEST( ReadOnly ); \
53 CPPUNIT_TEST( StreamInput ); \
54 CPPUNIT_TEST( Redirector )
56 CPPUNIT_TEST_SUITE( TextCtrlTestCase
);
57 // These tests run for single line text controls.
59 WXUISIM_TEST( MaxLength
);
60 SINGLE_AND_MULTI_TESTS();
62 // Now switch to the multi-line text controls.
63 CPPUNIT_TEST( PseudoTestSwitchToMultiLineStyle
);
65 // Rerun some of the tests above. Notice that not all of them pass, so
66 // we can't just use wxTEXT_ENTRY_TESTS() here. For some of them it's
67 // normal, e.g. Hint() test isn't supposed to work for multi-line
68 // controls. Others, such as InsertionPoint() and TextChangeEvents()
69 // don't pass neither but this could be a bug.
70 CPPUNIT_TEST( SetValue
);
71 CPPUNIT_TEST( Selection
);
72 CPPUNIT_TEST( Replace
);
73 WXUISIM_TEST( Editable
);
74 CPPUNIT_TEST( CopyPaste
);
75 CPPUNIT_TEST( UndoRedo
);
77 SINGLE_AND_MULTI_TESTS();
80 // All tests from now on are for multi-line controls only.
81 CPPUNIT_TEST( MultiLineReplace
);
82 //WXUISIM_TEST( ProcessEnter );
84 CPPUNIT_TEST( Style
);
85 CPPUNIT_TEST( FontStyle
);
86 CPPUNIT_TEST( Lines
);
87 CPPUNIT_TEST( LogTextCtrl
);
88 CPPUNIT_TEST( PositionToCoords
);
89 CPPUNIT_TEST( PositionToCoordsRich
);
90 CPPUNIT_TEST( PositionToCoordsRich2
);
91 CPPUNIT_TEST_SUITE_END();
93 void PseudoTestSwitchToMultiLineStyle()
95 ms_style
= wxTE_MULTILINE
;
98 void MultiLineReplace();
103 //void ProcessEnter();
109 void PositionToCoords();
110 void PositionToCoordsRich();
111 void PositionToCoordsRich2();
113 void DoPositionToCoordsTestWithStyle(long style
);
115 // Create the control with the following styles added to ms_style which may
116 // (or not) already contain wxTE_MULTILINE.
117 void CreateText(long extraStyles
);
121 static long ms_style
;
123 DECLARE_NO_COPY_CLASS(TextCtrlTestCase
)
126 // register in the unnamed registry so that these tests are run by default
127 CPPUNIT_TEST_SUITE_REGISTRATION( TextCtrlTestCase
);
129 // also include in its own registry so that these tests can be run alone
130 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TextCtrlTestCase
, "TextCtrlTestCase" );
132 // ----------------------------------------------------------------------------
133 // test initialization
134 // ----------------------------------------------------------------------------
136 // This is 0 initially and set to wxTE_MULTILINE later to allow running the
137 // same tests for both single and multi line controls.
138 long TextCtrlTestCase::ms_style
= 0;
140 void TextCtrlTestCase::CreateText(long extraStyles
)
143 if ( ms_style
== wxTE_MULTILINE
)
144 size
= wxSize(400, TEXT_HEIGHT
);
146 m_text
= new wxTextCtrl(wxTheApp
->GetTopWindow(), wxID_ANY
, "",
147 wxDefaultPosition
, size
,
148 ms_style
| extraStyles
);
151 void TextCtrlTestCase::setUp()
153 CreateText(ms_style
);
156 void TextCtrlTestCase::tearDown()
161 // ----------------------------------------------------------------------------
163 // ----------------------------------------------------------------------------
165 void TextCtrlTestCase::MultiLineReplace()
167 m_text
->SetValue("Hello replace\n"
169 m_text
->SetInsertionPoint(0);
171 m_text
->Replace(6, 13, "changed");
173 CPPUNIT_ASSERT_EQUAL("Hello changed\n"
176 CPPUNIT_ASSERT_EQUAL(13, m_text
->GetInsertionPoint());
178 m_text
->Replace(13, -1, "");
179 CPPUNIT_ASSERT_EQUAL("Hello changed", m_text
->GetValue());
180 CPPUNIT_ASSERT_EQUAL(13, m_text
->GetInsertionPoint());
183 void TextCtrlTestCase::ReadOnly()
185 #if wxUSE_UIACTIONSIMULATOR
186 // we need a read only control for this test so recreate it
188 CreateText(wxTE_READONLY
);
190 EventCounter
updated(m_text
, wxEVT_TEXT
);
194 wxUIActionSimulator sim
;
198 CPPUNIT_ASSERT_EQUAL("", m_text
->GetValue());
199 CPPUNIT_ASSERT_EQUAL(0, updated
.GetCount());
201 // SetEditable() is supposed to override wxTE_READONLY
202 m_text
->SetEditable(true);
205 // a ready only text field might not have been focusable at all
212 CPPUNIT_ASSERT_EQUAL("abcdef", m_text
->GetValue());
213 CPPUNIT_ASSERT_EQUAL(6, updated
.GetCount());
217 void TextCtrlTestCase::MaxLength()
219 #if wxUSE_UIACTIONSIMULATOR
220 EventCounter
updated(m_text
, wxEVT_TEXT
);
221 EventCounter
maxlen(m_text
, wxEVT_TEXT_MAXLEN
);
224 m_text
->SetMaxLength(10);
226 wxUIActionSimulator sim
;
230 CPPUNIT_ASSERT_EQUAL(0, maxlen
.GetCount());
235 CPPUNIT_ASSERT_EQUAL(0, maxlen
.GetCount());
236 CPPUNIT_ASSERT_EQUAL(10, updated
.GetCount());
244 CPPUNIT_ASSERT_EQUAL(1, maxlen
.GetCount());
245 CPPUNIT_ASSERT_EQUAL(0, updated
.GetCount());
250 m_text
->SetMaxLength(0);
255 CPPUNIT_ASSERT_EQUAL(0, maxlen
.GetCount());
256 CPPUNIT_ASSERT_EQUAL(1, updated
.GetCount());
260 void TextCtrlTestCase::StreamInput()
264 // Ensure we use decimal point and not a comma.
265 char * const locOld
= setlocale(LC_NUMERIC
, "C");
266 wxON_BLOCK_EXIT2( setlocale
, (int)LC_NUMERIC
, locOld
);
268 *m_text
<< "stringinput"
277 CPPUNIT_ASSERT_EQUAL("stringinput1010003.142.71ab", m_text
->GetValue());
279 m_text
->SetValue("");
281 #if wxHAS_TEXT_WINDOW_STREAM
283 std::ostream
stream(m_text
);
285 // We don't test a wide character as this is not a wide stream
286 stream
<< "stringinput"
295 CPPUNIT_ASSERT_EQUAL("stringinput1010003.142.71a", m_text
->GetValue());
297 #endif // wxHAS_TEXT_WINDOW_STREAM
301 void TextCtrlTestCase::Redirector()
303 #if wxHAS_TEXT_WINDOW_STREAM && wxUSE_STD_IOSTREAM
305 wxStreamToTextRedirector
redirect(m_text
);
307 std::cout
<< "stringinput"
314 CPPUNIT_ASSERT_EQUAL("stringinput1010003.142.71a", m_text
->GetValue());
320 void TextCtrlTestCase::ProcessEnter()
322 #if wxUSE_UIACTIONSIMULATOR
323 wxTestableFrame
* frame
= wxStaticCast(wxTheApp
->GetTopWindow(),
326 EventCounter
count(m_text
, wxEVT_TEXT_ENTER
);
330 wxUIActionSimulator sim
;
331 sim
.Char(WXK_RETURN
);
334 CPPUNIT_ASSERT_EQUAL(0, frame
->GetEventCount(wxEVT_TEXT_ENTER
));
336 // we need a text control with wxTE_PROCESS_ENTER for this test
338 CreateText(wxTE_PROCESS_ENTER
);
342 sim
.Char(WXK_RETURN
);
345 CPPUNIT_ASSERT_EQUAL(1, frame
->GetEventCount(wxEVT_TEXT_ENTER
));
350 void TextCtrlTestCase::Url()
352 #if wxUSE_UIACTIONSIMULATOR && defined(__WXMSW__)
354 CreateText(wxTE_RICH
| wxTE_AUTO_URL
);
356 EventCounter
url(m_text
, wxEVT_TEXT_URL
);
358 m_text
->AppendText("http://www.wxwidgets.org");
360 wxUIActionSimulator sim
;
361 sim
.MouseMove(m_text
->ClientToScreen(wxPoint(5, 5)));
365 CPPUNIT_ASSERT_EQUAL(1, url
.GetCount());
369 void TextCtrlTestCase::Style()
373 // We need wxTE_RICH under windows for style support
374 CreateText(wxTE_RICH
);
376 // Red text on a white background
377 m_text
->SetDefaultStyle(wxTextAttr(*wxRED
, *wxWHITE
));
379 CPPUNIT_ASSERT_EQUAL(m_text
->GetDefaultStyle().GetTextColour(), *wxRED
);
380 CPPUNIT_ASSERT_EQUAL(m_text
->GetDefaultStyle().GetBackgroundColour(),
383 m_text
->AppendText("red on white ");
385 // Red text on a grey background
386 m_text
->SetDefaultStyle(wxTextAttr(wxNullColour
, *wxLIGHT_GREY
));
388 CPPUNIT_ASSERT_EQUAL(m_text
->GetDefaultStyle().GetTextColour(), *wxRED
);
389 CPPUNIT_ASSERT_EQUAL(m_text
->GetDefaultStyle().GetBackgroundColour(),
392 m_text
->AppendText("red on grey ");
394 // Blue text on a grey background
395 m_text
->SetDefaultStyle(wxTextAttr(*wxBLUE
));
398 CPPUNIT_ASSERT_EQUAL(m_text
->GetDefaultStyle().GetTextColour(), *wxBLUE
);
399 CPPUNIT_ASSERT_EQUAL(m_text
->GetDefaultStyle().GetBackgroundColour(),
402 m_text
->AppendText("blue on grey");
404 // Get getting the style at a specific location
407 // We have to check that styles are supported
408 if(m_text
->GetStyle(3, style
))
410 CPPUNIT_ASSERT_EQUAL(style
.GetTextColour(), *wxRED
);
411 CPPUNIT_ASSERT_EQUAL(style
.GetBackgroundColour(), *wxWHITE
);
414 // And then setting the style
415 if(m_text
->SetStyle(15, 18, style
))
417 m_text
->GetStyle(17, style
);
419 CPPUNIT_ASSERT_EQUAL(style
.GetTextColour(), *wxRED
);
420 CPPUNIT_ASSERT_EQUAL(style
.GetBackgroundColour(), *wxWHITE
);
425 void TextCtrlTestCase::FontStyle()
427 // We need wxTE_RICH under MSW and wxTE_MULTILINE under GTK for style
428 // support so recreate the control with these styles.
430 CreateText(wxTE_RICH
);
432 // Check that we get back the same font from GetStyle() after setting it
433 // with SetDefaultStyle().
435 wxFONTFAMILY_DEFAULT
,
437 wxFONTWEIGHT_NORMAL
);
439 attrIn
.SetFont(fontIn
);
440 if ( !m_text
->SetDefaultStyle(attrIn
) )
442 // Skip the test if the styles are not supported.
446 m_text
->AppendText("Default font size 14");
449 m_text
->GetStyle(5, attrOut
);
451 CPPUNIT_ASSERT( attrOut
.HasFont() );
453 wxFont fontOut
= attrOut
.GetFont();
455 // Under MSW we get back an encoding in the font even though we hadn't
456 // specified it originally. It's not really a problem but we need this hack
457 // to prevent the assert below from failing because of it.
458 fontOut
.SetEncoding(fontIn
.GetEncoding());
460 CPPUNIT_ASSERT_EQUAL( fontIn
, fontOut
);
463 // Also check the same for SetStyle().
464 fontIn
.SetPointSize(10);
465 fontIn
.SetWeight(wxFONTWEIGHT_BOLD
);
466 attrIn
.SetFont(fontIn
);
467 m_text
->SetStyle(0, 6, attrIn
);
469 m_text
->GetStyle(4, attrOut
);
470 CPPUNIT_ASSERT( attrOut
.HasFont() );
472 fontOut
= attrOut
.GetFont();
474 fontOut
.SetEncoding(fontIn
.GetEncoding());
476 CPPUNIT_ASSERT_EQUAL( fontIn
, fontOut
);
479 void TextCtrlTestCase::Lines()
481 m_text
->SetValue("line1\nline2\nlong long line 3");
485 CPPUNIT_ASSERT_EQUAL(3, m_text
->GetNumberOfLines());
486 CPPUNIT_ASSERT_EQUAL(5, m_text
->GetLineLength(0));
487 CPPUNIT_ASSERT_EQUAL("line2", m_text
->GetLineText(1));
488 CPPUNIT_ASSERT_EQUAL(16, m_text
->GetLineLength(2));
490 m_text
->AppendText("\n\nMore text on line 5");
492 CPPUNIT_ASSERT_EQUAL(5, m_text
->GetNumberOfLines());
493 CPPUNIT_ASSERT_EQUAL(0, m_text
->GetLineLength(3));
494 CPPUNIT_ASSERT_EQUAL("", m_text
->GetLineText(3));
496 // Verify that wrapped lines count as 2 lines.
498 // This currently doesn't work neither in wxGTK nor wxOSX/Cocoa, see
499 // #12366, where GetNumberOfLines() always returns the number of logical,
500 // not physical, lines.
501 m_text
->AppendText("\n" + wxString(50, '1') + ' ' + wxString(50, '2'));
502 #if defined(__WXGTK__) || defined(__WXOSX_COCOA__)
503 CPPUNIT_ASSERT_EQUAL(6, m_text
->GetNumberOfLines());
505 CPPUNIT_ASSERT_EQUAL(7, m_text
->GetNumberOfLines());
509 void TextCtrlTestCase::LogTextCtrl()
511 CPPUNIT_ASSERT(m_text
->IsEmpty());
513 wxLogTextCtrl
* logtext
= new wxLogTextCtrl(m_text
);
515 wxLog
* old
= wxLog::SetActiveTarget(logtext
);
517 logtext
->LogText("text");
519 delete wxLog::SetActiveTarget(old
);
521 CPPUNIT_ASSERT(!m_text
->IsEmpty());
524 void TextCtrlTestCase::PositionToCoords()
526 DoPositionToCoordsTestWithStyle(0);
529 void TextCtrlTestCase::PositionToCoordsRich()
531 DoPositionToCoordsTestWithStyle(wxTE_RICH
);
534 void TextCtrlTestCase::PositionToCoordsRich2()
536 DoPositionToCoordsTestWithStyle(wxTE_RICH2
);
539 void TextCtrlTestCase::DoPositionToCoordsTestWithStyle(long style
)
544 // Asking for invalid index should fail.
545 WX_ASSERT_FAILS_WITH_ASSERT( m_text
->PositionToCoords(1) );
547 // Getting position shouldn't return wxDefaultPosition except if the method
548 // is not implemented at all in the current port.
549 const wxPoint pos0
= m_text
->PositionToCoords(0);
550 if ( pos0
== wxDefaultPosition
)
552 #if defined(__WXMSW__) || defined(__WXGTK20__)
553 CPPUNIT_FAIL( "PositionToCoords() unexpectedly failed." );
558 CPPUNIT_ASSERT(pos0
.x
>= 0);
559 CPPUNIT_ASSERT(pos0
.y
>= 0);
562 m_text
->SetValue("Hello");
563 wxYield(); // Let GTK layout the control correctly.
565 // Position of non-first character should be positive.
566 const long posHello4
= m_text
->PositionToCoords(4).x
;
567 CPPUNIT_ASSERT( posHello4
> 0 );
569 // Asking for position beyond the last character should succeed and return
570 // reasonable result.
571 CPPUNIT_ASSERT( m_text
->PositionToCoords(5).x
> posHello4
);
573 // But asking for the next position should fail.
574 WX_ASSERT_FAILS_WITH_ASSERT( m_text
->PositionToCoords(6) );
576 // Test getting the coordinates of the last character when it is in the
577 // beginning of a new line to exercise MSW code which has specific logic
579 m_text
->AppendText("\n");
580 const wxPoint posLast
= m_text
->PositionToCoords(m_text
->GetLastPosition());
581 CPPUNIT_ASSERT_EQUAL( pos0
.x
, posLast
.x
);
582 CPPUNIT_ASSERT( posLast
.y
> 0 );
585 // Add enough contents to the control to make sure it has a scrollbar.
586 m_text
->SetValue("First line" + wxString(50, '\n') + "Last line");
587 m_text
->SetInsertionPoint(0);
588 wxYield(); // Let GTK layout the control correctly.
590 // This shouldn't change anything for the first position coordinates.
591 CPPUNIT_ASSERT_EQUAL( pos0
, m_text
->PositionToCoords(0) );
593 // And the last one must be beyond the window boundary and so not be
594 // visible -- but getting its coordinate should still work.
597 m_text
->PositionToCoords(m_text
->GetLastPosition()).y
> TEXT_HEIGHT
601 // Now make it scroll to the end and check that the first position now has
602 // negative offset as its above the visible part of the window while the
603 // last position is in its bounds.
604 m_text
->SetInsertionPointEnd();
606 CPPUNIT_ASSERT( m_text
->PositionToCoords(0).y
< 0 );
609 m_text
->PositionToCoords(m_text
->GetInsertionPoint()).y
<= TEXT_HEIGHT
614 #endif //wxUSE_TEXTCTRL