1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/textctrl/textctrltest.cpp
3 // Purpose: wxTextCtrl unit test
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2007 Vadim Zeitlin <vadim@wxwidgets.org>
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
22 #include "wx/textctrl.h"
25 // ----------------------------------------------------------------------------
27 // ----------------------------------------------------------------------------
29 class TextCtrlTestCase
: public CppUnit::TestCase
32 TextCtrlTestCase() { }
35 virtual void tearDown();
38 CPPUNIT_TEST_SUITE( TextCtrlTestCase
);
39 CPPUNIT_TEST( SetValue
);
40 CPPUNIT_TEST( TextChangeEvents
);
41 CPPUNIT_TEST( Selection
);
42 CPPUNIT_TEST( InsertionPoint
);
43 CPPUNIT_TEST_SUITE_END();
46 void TextChangeEvents();
48 void InsertionPoint();
50 // Selection() test helper: verify that selection is as described by the
51 // function parameters
52 void AssertSelection(int from
, int to
, const char *sel
);
57 DECLARE_NO_COPY_CLASS(TextCtrlTestCase
)
60 // register in the unnamed registry so that these tests are run by default
61 CPPUNIT_TEST_SUITE_REGISTRATION( TextCtrlTestCase
);
63 // also include in it's own registry so that these tests can be run alone
64 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TextCtrlTestCase
, "TextCtrlTestCase" );
66 // ----------------------------------------------------------------------------
67 // test initialization
68 // ----------------------------------------------------------------------------
70 void TextCtrlTestCase::setUp()
72 m_text
= new wxTextCtrl(wxTheApp
->GetTopWindow(), wxID_ANY
);
75 void TextCtrlTestCase::tearDown()
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 void TextCtrlTestCase::SetValue()
87 CPPUNIT_ASSERT( m_text
->IsEmpty() );
89 m_text
->SetValue("foo");
90 CPPUNIT_ASSERT_EQUAL( "foo", m_text
->GetValue() );
93 CPPUNIT_ASSERT( m_text
->IsEmpty() );
95 m_text
->SetValue("hi");
96 CPPUNIT_ASSERT_EQUAL( "hi", m_text
->GetValue() );
98 m_text
->SetValue("bye");
99 CPPUNIT_ASSERT_EQUAL( "bye", m_text
->GetValue() );
102 void TextCtrlTestCase::TextChangeEvents()
104 class TextTestEventHandler
: public wxEvtHandler
107 TextTestEventHandler() { m_events
= 0; }
109 // calling this automatically resets the events counter
112 const int events
= m_events
;
117 void OnText(wxCommandEvent
& WXUNUSED(event
)) { m_events
++; }
123 m_text
->Connect(wxEVT_COMMAND_TEXT_UPDATED
,
124 wxCommandEventHandler(TextTestEventHandler::OnText
),
128 // notice that SetValue() generates an event even if the text didn't change
129 m_text
->SetValue("");
130 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetEvents() );
132 m_text
->SetValue("foo");
133 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetEvents() );
135 m_text
->SetValue("foo");
136 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetEvents() );
138 m_text
->ChangeValue("bar");
139 CPPUNIT_ASSERT_EQUAL( 0, handler
.GetEvents() );
141 m_text
->AppendText("bar");
142 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetEvents() );
144 m_text
->Replace(3, 6, "baz");
145 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetEvents() );
147 m_text
->Remove(0, 3);
148 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetEvents() );
150 m_text
->WriteText("foo");
151 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetEvents() );
154 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetEvents() );
157 void TextCtrlTestCase::AssertSelection(int from
, int to
, const char *sel
)
159 CPPUNIT_ASSERT( m_text
->HasSelection() );
163 m_text
->GetSelection(&fromReal
, &toReal
);
164 CPPUNIT_ASSERT_EQUAL( from
, fromReal
);
165 CPPUNIT_ASSERT_EQUAL( to
, toReal
);
166 CPPUNIT_ASSERT_EQUAL( sel
, m_text
->GetStringSelection() );
168 CPPUNIT_ASSERT_EQUAL( from
, m_text
->GetInsertionPoint() );
171 void TextCtrlTestCase::Selection()
173 m_text
->SetValue("0123456789");
175 m_text
->SetSelection(2, 4);
176 AssertSelection(2, 4, "23"); // not "234"!
178 m_text
->SetSelection(3, -1);
179 AssertSelection(3, 10, "3456789");
182 AssertSelection(0, 10, "0123456789");
184 m_text
->SetSelection(0, 0);
185 CPPUNIT_ASSERT( !m_text
->HasSelection() );
188 void TextCtrlTestCase::InsertionPoint()
190 CPPUNIT_ASSERT_EQUAL( 0, m_text
->GetLastPosition() );
191 CPPUNIT_ASSERT_EQUAL( 0, m_text
->GetInsertionPoint() );
193 m_text
->SetValue("0"); // should put the insertion point in front
194 CPPUNIT_ASSERT_EQUAL( 1, m_text
->GetLastPosition() );
195 CPPUNIT_ASSERT_EQUAL( 0, m_text
->GetInsertionPoint() );
197 m_text
->AppendText("12"); // should update the insertion point position
198 CPPUNIT_ASSERT_EQUAL( 3, m_text
->GetLastPosition() );
199 CPPUNIT_ASSERT_EQUAL( 3, m_text
->GetInsertionPoint() );
201 m_text
->SetInsertionPoint(1);
202 CPPUNIT_ASSERT_EQUAL( 3, m_text
->GetLastPosition() );
203 CPPUNIT_ASSERT_EQUAL( 1, m_text
->GetInsertionPoint() );
205 m_text
->SetInsertionPointEnd();
206 CPPUNIT_ASSERT_EQUAL( 3, m_text
->GetInsertionPoint() );
208 m_text
->SetInsertionPoint(0);
209 m_text
->WriteText("-"); // should move it after the written text
210 CPPUNIT_ASSERT_EQUAL( 4, m_text
->GetLastPosition() );
211 CPPUNIT_ASSERT_EQUAL( 1, m_text
->GetInsertionPoint() );