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_SUITE_END();
44 void TextChangeEvents();
49 DECLARE_NO_COPY_CLASS(TextCtrlTestCase
)
52 // register in the unnamed registry so that these tests are run by default
53 CPPUNIT_TEST_SUITE_REGISTRATION( TextCtrlTestCase
);
55 // also include in it's own registry so that these tests can be run alone
56 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TextCtrlTestCase
, "TextCtrlTestCase" );
58 // ----------------------------------------------------------------------------
59 // test initialization
60 // ----------------------------------------------------------------------------
62 void TextCtrlTestCase::setUp()
64 m_text
= new wxTextCtrl(wxTheApp
->GetTopWindow(), wxID_ANY
);
67 void TextCtrlTestCase::tearDown()
73 // ----------------------------------------------------------------------------
75 // ----------------------------------------------------------------------------
77 void TextCtrlTestCase::SetValue()
79 CPPUNIT_ASSERT( m_text
->IsEmpty() );
81 m_text
->SetValue("foo");
82 WX_ASSERT_STR_EQUAL( "foo", m_text
->GetValue() );
85 CPPUNIT_ASSERT( m_text
->IsEmpty() );
87 m_text
->SetValue("hi");
88 WX_ASSERT_STR_EQUAL( "hi", m_text
->GetValue() );
90 m_text
->SetValue("bye");
91 WX_ASSERT_STR_EQUAL( "bye", m_text
->GetValue() );
94 void TextCtrlTestCase::TextChangeEvents()
96 class TextTestEventHandler
: public wxEvtHandler
99 TextTestEventHandler() { m_events
= 0; }
101 // calling this automatically resets the events counter
104 const int events
= m_events
;
109 void OnText(wxCommandEvent
& WXUNUSED(event
)) { m_events
++; }
115 m_text
->Connect(wxEVT_COMMAND_TEXT_UPDATED
,
116 wxCommandEventHandler(TextTestEventHandler::OnText
),
120 // notice that SetValue() generates an event even if the text didn't change
121 m_text
->SetValue("");
122 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetEvents() );
124 m_text
->SetValue("foo");
125 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetEvents() );
127 m_text
->SetValue("foo");
128 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetEvents() );
130 m_text
->ChangeValue("bar");
131 CPPUNIT_ASSERT_EQUAL( 0, handler
.GetEvents() );
133 m_text
->AppendText("bar");
134 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetEvents() );
136 m_text
->Replace(3, 6, "baz");
137 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetEvents() );
139 m_text
->Remove(0, 3);
140 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetEvents() );
142 m_text
->WriteText("foo");
143 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetEvents() );
146 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetEvents() );