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_SUITE_END();
45 void TextChangeEvents();
48 // Selection() test helper: verify that selection is as described by the
49 // function parameters
50 void AssertSelection(int from
, int to
, const char *sel
);
55 DECLARE_NO_COPY_CLASS(TextCtrlTestCase
)
58 // register in the unnamed registry so that these tests are run by default
59 CPPUNIT_TEST_SUITE_REGISTRATION( TextCtrlTestCase
);
61 // also include in it's own registry so that these tests can be run alone
62 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TextCtrlTestCase
, "TextCtrlTestCase" );
64 // ----------------------------------------------------------------------------
65 // test initialization
66 // ----------------------------------------------------------------------------
68 void TextCtrlTestCase::setUp()
70 m_text
= new wxTextCtrl(wxTheApp
->GetTopWindow(), wxID_ANY
);
73 void TextCtrlTestCase::tearDown()
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 void TextCtrlTestCase::SetValue()
85 CPPUNIT_ASSERT( m_text
->IsEmpty() );
87 m_text
->SetValue("foo");
88 CPPUNIT_ASSERT_EQUAL( "foo", m_text
->GetValue() );
91 CPPUNIT_ASSERT( m_text
->IsEmpty() );
93 m_text
->SetValue("hi");
94 CPPUNIT_ASSERT_EQUAL( "hi", m_text
->GetValue() );
96 m_text
->SetValue("bye");
97 CPPUNIT_ASSERT_EQUAL( "bye", m_text
->GetValue() );
100 void TextCtrlTestCase::TextChangeEvents()
102 class TextTestEventHandler
: public wxEvtHandler
105 TextTestEventHandler() { m_events
= 0; }
107 // calling this automatically resets the events counter
110 const int events
= m_events
;
115 void OnText(wxCommandEvent
& WXUNUSED(event
)) { m_events
++; }
121 m_text
->Connect(wxEVT_COMMAND_TEXT_UPDATED
,
122 wxCommandEventHandler(TextTestEventHandler::OnText
),
126 // notice that SetValue() generates an event even if the text didn't change
127 m_text
->SetValue("");
128 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetEvents() );
130 m_text
->SetValue("foo");
131 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetEvents() );
133 m_text
->SetValue("foo");
134 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetEvents() );
136 m_text
->ChangeValue("bar");
137 CPPUNIT_ASSERT_EQUAL( 0, handler
.GetEvents() );
139 m_text
->AppendText("bar");
140 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetEvents() );
142 m_text
->Replace(3, 6, "baz");
143 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetEvents() );
145 m_text
->Remove(0, 3);
146 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetEvents() );
148 m_text
->WriteText("foo");
149 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetEvents() );
152 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetEvents() );
155 void TextCtrlTestCase::AssertSelection(int from
, int to
, const char *sel
)
157 CPPUNIT_ASSERT( m_text
->HasSelection() );
161 m_text
->GetSelection(&fromReal
, &toReal
);
162 CPPUNIT_ASSERT_EQUAL( from
, fromReal
);
163 CPPUNIT_ASSERT_EQUAL( to
, toReal
);
164 CPPUNIT_ASSERT_EQUAL( sel
, m_text
->GetStringSelection() );
166 CPPUNIT_ASSERT_EQUAL( from
, m_text
->GetInsertionPoint() );
169 void TextCtrlTestCase::Selection()
171 m_text
->SetValue("0123456789");
173 m_text
->SetSelection(2, 4);
174 AssertSelection(2, 4, "23"); // not "234"!
176 m_text
->SetSelection(3, -1);
177 AssertSelection(3, 10, "3456789");
180 AssertSelection(0, 10, "0123456789");
182 m_text
->SetSelection(0, 0);
183 CPPUNIT_ASSERT( !m_text
->HasSelection() );