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 static const int TEXT_HEIGHT
= 200;
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 class TextCtrlTestCase
: public TextEntryTestCase
, public CppUnit::TestCase
43 TextCtrlTestCase() { }
46 virtual void tearDown();
49 virtual wxTextEntry
*GetTestEntry() const { return m_text
; }
50 virtual wxWindow
*GetTestWindow() const { return m_text
; }
52 #define SINGLE_AND_MULTI_TESTS() \
53 WXUISIM_TEST( ReadOnly ); \
54 CPPUNIT_TEST( StreamInput ); \
55 CPPUNIT_TEST( Redirector )
57 CPPUNIT_TEST_SUITE( TextCtrlTestCase
);
58 // These tests run for single line text controls.
60 WXUISIM_TEST( MaxLength
);
61 SINGLE_AND_MULTI_TESTS();
63 // Now switch to the multi-line text controls.
64 CPPUNIT_TEST( PseudoTestSwitchToMultiLineStyle
);
66 // Rerun some of the tests above. Notice that not all of them pass, so
67 // we can't just use wxTEXT_ENTRY_TESTS() here. For some of them it's
68 // normal, e.g. Hint() test isn't supposed to work for multi-line
69 // controls. Others, such as InsertionPoint() and TextChangeEvents()
70 // don't pass neither but this could be a bug.
71 CPPUNIT_TEST( SetValue
);
72 CPPUNIT_TEST( Selection
);
73 CPPUNIT_TEST( Replace
);
74 WXUISIM_TEST( Editable
);
75 CPPUNIT_TEST( CopyPaste
);
76 CPPUNIT_TEST( UndoRedo
);
78 SINGLE_AND_MULTI_TESTS();
81 // All tests from now on are for multi-line controls only.
82 CPPUNIT_TEST( MultiLineReplace
);
83 //WXUISIM_TEST( ProcessEnter );
85 CPPUNIT_TEST( Style
);
86 CPPUNIT_TEST( FontStyle
);
87 CPPUNIT_TEST( Lines
);
88 CPPUNIT_TEST( LogTextCtrl
);
89 CPPUNIT_TEST( PositionToCoords
);
90 CPPUNIT_TEST( PositionToCoordsRich
);
91 CPPUNIT_TEST( PositionToCoordsRich2
);
92 CPPUNIT_TEST_SUITE_END();
94 void PseudoTestSwitchToMultiLineStyle()
96 ms_style
= wxTE_MULTILINE
;
99 void MultiLineReplace();
104 //void ProcessEnter();
110 void PositionToCoords();
111 void PositionToCoordsRich();
112 void PositionToCoordsRich2();
114 void DoPositionToCoordsTestWithStyle(long style
);
116 // Create the control with the following styles added to ms_style which may
117 // (or not) already contain wxTE_MULTILINE.
118 void CreateText(long extraStyles
);
122 static long ms_style
;
124 DECLARE_NO_COPY_CLASS(TextCtrlTestCase
)
127 // register in the unnamed registry so that these tests are run by default
128 CPPUNIT_TEST_SUITE_REGISTRATION( TextCtrlTestCase
);
130 // also include in its own registry so that these tests can be run alone
131 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TextCtrlTestCase
, "TextCtrlTestCase" );
133 // ----------------------------------------------------------------------------
134 // test initialization
135 // ----------------------------------------------------------------------------
137 // This is 0 initially and set to wxTE_MULTILINE later to allow running the
138 // same tests for both single and multi line controls.
139 long TextCtrlTestCase::ms_style
= 0;
141 void TextCtrlTestCase::CreateText(long extraStyles
)
144 if ( ms_style
== wxTE_MULTILINE
)
145 size
= wxSize(400, TEXT_HEIGHT
);
147 m_text
= new wxTextCtrl(wxTheApp
->GetTopWindow(), wxID_ANY
, "",
148 wxDefaultPosition
, size
,
149 ms_style
| extraStyles
);
152 void TextCtrlTestCase::setUp()
154 CreateText(ms_style
);
157 void TextCtrlTestCase::tearDown()
162 // ----------------------------------------------------------------------------
164 // ----------------------------------------------------------------------------
166 void TextCtrlTestCase::MultiLineReplace()
168 m_text
->SetValue("Hello replace\n"
170 m_text
->SetInsertionPoint(0);
172 m_text
->Replace(6, 13, "changed");
174 CPPUNIT_ASSERT_EQUAL("Hello changed\n"
177 CPPUNIT_ASSERT_EQUAL(13, m_text
->GetInsertionPoint());
179 m_text
->Replace(13, -1, "");
180 CPPUNIT_ASSERT_EQUAL("Hello changed", m_text
->GetValue());
181 CPPUNIT_ASSERT_EQUAL(13, m_text
->GetInsertionPoint());
184 void TextCtrlTestCase::ReadOnly()
186 #if wxUSE_UIACTIONSIMULATOR
187 // we need a read only control for this test so recreate it
189 CreateText(wxTE_READONLY
);
191 EventCounter
updated(m_text
, wxEVT_TEXT
);
195 wxUIActionSimulator sim
;
199 CPPUNIT_ASSERT_EQUAL("", m_text
->GetValue());
200 CPPUNIT_ASSERT_EQUAL(0, updated
.GetCount());
202 // SetEditable() is supposed to override wxTE_READONLY
203 m_text
->SetEditable(true);
206 // a ready only text field might not have been focusable at all
213 CPPUNIT_ASSERT_EQUAL("abcdef", m_text
->GetValue());
214 CPPUNIT_ASSERT_EQUAL(6, updated
.GetCount());
218 void TextCtrlTestCase::MaxLength()
220 #if wxUSE_UIACTIONSIMULATOR
221 EventCounter
updated(m_text
, wxEVT_TEXT
);
222 EventCounter
maxlen(m_text
, wxEVT_TEXT_MAXLEN
);
225 m_text
->SetMaxLength(10);
227 wxUIActionSimulator sim
;
231 CPPUNIT_ASSERT_EQUAL(0, maxlen
.GetCount());
236 CPPUNIT_ASSERT_EQUAL(0, maxlen
.GetCount());
237 CPPUNIT_ASSERT_EQUAL(10, updated
.GetCount());
245 CPPUNIT_ASSERT_EQUAL(1, maxlen
.GetCount());
246 CPPUNIT_ASSERT_EQUAL(0, updated
.GetCount());
251 m_text
->SetMaxLength(0);
256 CPPUNIT_ASSERT_EQUAL(0, maxlen
.GetCount());
257 CPPUNIT_ASSERT_EQUAL(1, updated
.GetCount());
261 void TextCtrlTestCase::StreamInput()
265 // Ensure we use decimal point and not a comma.
266 char * const locOld
= setlocale(LC_NUMERIC
, "C");
267 wxON_BLOCK_EXIT2( setlocale
, (int)LC_NUMERIC
, locOld
);
269 *m_text
<< "stringinput"
278 CPPUNIT_ASSERT_EQUAL("stringinput1010003.142.71ab", m_text
->GetValue());
280 m_text
->SetValue("");
282 #if wxHAS_TEXT_WINDOW_STREAM
284 std::ostream
stream(m_text
);
286 // We don't test a wide character as this is not a wide stream
287 stream
<< "stringinput"
296 CPPUNIT_ASSERT_EQUAL("stringinput1010003.142.71a", m_text
->GetValue());
298 #endif // wxHAS_TEXT_WINDOW_STREAM
302 void TextCtrlTestCase::Redirector()
304 #if wxHAS_TEXT_WINDOW_STREAM && wxUSE_STD_IOSTREAM
306 wxStreamToTextRedirector
redirect(m_text
);
308 std::cout
<< "stringinput"
315 CPPUNIT_ASSERT_EQUAL("stringinput1010003.142.71a", m_text
->GetValue());
321 void TextCtrlTestCase::ProcessEnter()
323 #if wxUSE_UIACTIONSIMULATOR
324 wxTestableFrame
* frame
= wxStaticCast(wxTheApp
->GetTopWindow(),
327 EventCounter
count(m_text
, wxEVT_TEXT_ENTER
);
331 wxUIActionSimulator sim
;
332 sim
.Char(WXK_RETURN
);
335 CPPUNIT_ASSERT_EQUAL(0, frame
->GetEventCount(wxEVT_TEXT_ENTER
));
337 // we need a text control with wxTE_PROCESS_ENTER for this test
339 CreateText(wxTE_PROCESS_ENTER
);
343 sim
.Char(WXK_RETURN
);
346 CPPUNIT_ASSERT_EQUAL(1, frame
->GetEventCount(wxEVT_TEXT_ENTER
));
351 void TextCtrlTestCase::Url()
353 #if wxUSE_UIACTIONSIMULATOR && defined(__WXMSW__)
355 CreateText(wxTE_RICH
| wxTE_AUTO_URL
);
357 EventCounter
url(m_text
, wxEVT_TEXT_URL
);
359 m_text
->AppendText("http://www.wxwidgets.org");
361 wxUIActionSimulator sim
;
362 sim
.MouseMove(m_text
->ClientToScreen(wxPoint(5, 5)));
366 CPPUNIT_ASSERT_EQUAL(1, url
.GetCount());
370 void TextCtrlTestCase::Style()
374 // We need wxTE_RICH under windows for style support
375 CreateText(wxTE_RICH
);
377 // Red text on a white background
378 m_text
->SetDefaultStyle(wxTextAttr(*wxRED
, *wxWHITE
));
380 CPPUNIT_ASSERT_EQUAL(m_text
->GetDefaultStyle().GetTextColour(), *wxRED
);
381 CPPUNIT_ASSERT_EQUAL(m_text
->GetDefaultStyle().GetBackgroundColour(),
384 m_text
->AppendText("red on white ");
386 // Red text on a grey background
387 m_text
->SetDefaultStyle(wxTextAttr(wxNullColour
, *wxLIGHT_GREY
));
389 CPPUNIT_ASSERT_EQUAL(m_text
->GetDefaultStyle().GetTextColour(), *wxRED
);
390 CPPUNIT_ASSERT_EQUAL(m_text
->GetDefaultStyle().GetBackgroundColour(),
393 m_text
->AppendText("red on grey ");
395 // Blue text on a grey background
396 m_text
->SetDefaultStyle(wxTextAttr(*wxBLUE
));
399 CPPUNIT_ASSERT_EQUAL(m_text
->GetDefaultStyle().GetTextColour(), *wxBLUE
);
400 CPPUNIT_ASSERT_EQUAL(m_text
->GetDefaultStyle().GetBackgroundColour(),
403 m_text
->AppendText("blue on grey");
405 // Get getting the style at a specific location
408 // We have to check that styles are supported
409 if(m_text
->GetStyle(3, style
))
411 CPPUNIT_ASSERT_EQUAL(style
.GetTextColour(), *wxRED
);
412 CPPUNIT_ASSERT_EQUAL(style
.GetBackgroundColour(), *wxWHITE
);
415 // And then setting the style
416 if(m_text
->SetStyle(15, 18, style
))
418 m_text
->GetStyle(17, style
);
420 CPPUNIT_ASSERT_EQUAL(style
.GetTextColour(), *wxRED
);
421 CPPUNIT_ASSERT_EQUAL(style
.GetBackgroundColour(), *wxWHITE
);
426 void TextCtrlTestCase::FontStyle()
428 // We need wxTE_RICH under MSW and wxTE_MULTILINE under GTK for style
429 // support so recreate the control with these styles.
431 CreateText(wxTE_RICH
);
433 // Check that we get back the same font from GetStyle() after setting it
434 // with SetDefaultStyle().
436 wxFONTFAMILY_DEFAULT
,
438 wxFONTWEIGHT_NORMAL
);
440 attrIn
.SetFont(fontIn
);
441 if ( !m_text
->SetDefaultStyle(attrIn
) )
443 // Skip the test if the styles are not supported.
447 m_text
->AppendText("Default font size 14");
450 m_text
->GetStyle(5, attrOut
);
452 CPPUNIT_ASSERT( attrOut
.HasFont() );
454 wxFont fontOut
= attrOut
.GetFont();
456 // Under MSW we get back an encoding in the font even though we hadn't
457 // specified it originally. It's not really a problem but we need this hack
458 // to prevent the assert below from failing because of it.
459 fontOut
.SetEncoding(fontIn
.GetEncoding());
461 CPPUNIT_ASSERT_EQUAL( fontIn
, fontOut
);
464 // Also check the same for SetStyle().
465 fontIn
.SetPointSize(10);
466 fontIn
.SetWeight(wxFONTWEIGHT_BOLD
);
467 attrIn
.SetFont(fontIn
);
468 m_text
->SetStyle(0, 6, attrIn
);
470 m_text
->GetStyle(4, attrOut
);
471 CPPUNIT_ASSERT( attrOut
.HasFont() );
473 fontOut
= attrOut
.GetFont();
475 fontOut
.SetEncoding(fontIn
.GetEncoding());
477 CPPUNIT_ASSERT_EQUAL( fontIn
, fontOut
);
480 void TextCtrlTestCase::Lines()
482 m_text
->SetValue("line1\nline2\nlong long line 3");
486 CPPUNIT_ASSERT_EQUAL(3, m_text
->GetNumberOfLines());
487 CPPUNIT_ASSERT_EQUAL(5, m_text
->GetLineLength(0));
488 CPPUNIT_ASSERT_EQUAL("line2", m_text
->GetLineText(1));
489 CPPUNIT_ASSERT_EQUAL(16, m_text
->GetLineLength(2));
491 m_text
->AppendText("\n\nMore text on line 5");
493 CPPUNIT_ASSERT_EQUAL(5, m_text
->GetNumberOfLines());
494 CPPUNIT_ASSERT_EQUAL(0, m_text
->GetLineLength(3));
495 CPPUNIT_ASSERT_EQUAL("", m_text
->GetLineText(3));
497 // Verify that wrapped lines count as 2 lines.
499 // This currently doesn't work neither in wxGTK nor wxOSX/Cocoa, see
500 // #12366, where GetNumberOfLines() always returns the number of logical,
501 // not physical, lines.
502 m_text
->AppendText("\n" + wxString(50, '1') + ' ' + wxString(50, '2'));
503 #if defined(__WXGTK__) || defined(__WXOSX_COCOA__)
504 CPPUNIT_ASSERT_EQUAL(6, m_text
->GetNumberOfLines());
506 CPPUNIT_ASSERT_EQUAL(7, m_text
->GetNumberOfLines());
510 void TextCtrlTestCase::LogTextCtrl()
512 CPPUNIT_ASSERT(m_text
->IsEmpty());
514 wxLogTextCtrl
* logtext
= new wxLogTextCtrl(m_text
);
516 wxLog
* old
= wxLog::SetActiveTarget(logtext
);
518 logtext
->LogText("text");
520 delete wxLog::SetActiveTarget(old
);
522 CPPUNIT_ASSERT(!m_text
->IsEmpty());
525 void TextCtrlTestCase::PositionToCoords()
527 DoPositionToCoordsTestWithStyle(0);
530 void TextCtrlTestCase::PositionToCoordsRich()
532 DoPositionToCoordsTestWithStyle(wxTE_RICH
);
535 void TextCtrlTestCase::PositionToCoordsRich2()
537 DoPositionToCoordsTestWithStyle(wxTE_RICH2
);
540 void TextCtrlTestCase::DoPositionToCoordsTestWithStyle(long style
)
545 // Asking for invalid index should fail.
546 WX_ASSERT_FAILS_WITH_ASSERT( m_text
->PositionToCoords(1) );
548 // Getting position shouldn't return wxDefaultPosition except if the method
549 // is not implemented at all in the current port.
550 const wxPoint pos0
= m_text
->PositionToCoords(0);
551 if ( pos0
== wxDefaultPosition
)
553 #if defined(__WXMSW__) || defined(__WXGTK20__)
554 CPPUNIT_FAIL( "PositionToCoords() unexpectedly failed." );
559 CPPUNIT_ASSERT(pos0
.x
>= 0);
560 CPPUNIT_ASSERT(pos0
.y
>= 0);
563 m_text
->SetValue("Hello");
564 wxYield(); // Let GTK layout the control correctly.
566 // Position of non-first character should be positive.
567 const long posHello4
= m_text
->PositionToCoords(4).x
;
568 CPPUNIT_ASSERT( posHello4
> 0 );
570 // Asking for position beyond the last character should succeed and return
571 // reasonable result.
572 CPPUNIT_ASSERT( m_text
->PositionToCoords(5).x
> posHello4
);
574 // But asking for the next position should fail.
575 WX_ASSERT_FAILS_WITH_ASSERT( m_text
->PositionToCoords(6) );
577 // Test getting the coordinates of the last character when it is in the
578 // beginning of a new line to exercise MSW code which has specific logic
580 m_text
->AppendText("\n");
581 const wxPoint posLast
= m_text
->PositionToCoords(m_text
->GetLastPosition());
582 CPPUNIT_ASSERT_EQUAL( pos0
.x
, posLast
.x
);
583 CPPUNIT_ASSERT( posLast
.y
> 0 );
586 // Add enough contents to the control to make sure it has a scrollbar.
587 m_text
->SetValue("First line" + wxString(50, '\n') + "Last line");
588 m_text
->SetInsertionPoint(0);
589 wxYield(); // Let GTK layout the control correctly.
591 // This shouldn't change anything for the first position coordinates.
592 CPPUNIT_ASSERT_EQUAL( pos0
, m_text
->PositionToCoords(0) );
594 // And the last one must be beyond the window boundary and so not be
595 // visible -- but getting its coordinate should still work.
598 m_text
->PositionToCoords(m_text
->GetLastPosition()).y
> TEXT_HEIGHT
602 // Now make it scroll to the end and check that the first position now has
603 // negative offset as its above the visible part of the window while the
604 // last position is in its bounds.
605 m_text
->SetInsertionPointEnd();
607 CPPUNIT_ASSERT( m_text
->PositionToCoords(0).y
< 0 );
610 m_text
->PositionToCoords(m_text
->GetInsertionPoint()).y
<= TEXT_HEIGHT
615 #endif //wxUSE_TEXTCTRL