get rid of special WX_ASSERT_FOO_EQUAL macros by defining CppUnit::assertEquals(...
[wxWidgets.git] / tests / controls / textctrltest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/textctrl/textctrltest.cpp
3 // Purpose: wxTextCtrl unit test
4 // Author: Vadim Zeitlin
5 // Created: 2007-09-25
6 // RCS-ID: $Id$
7 // Copyright: (c) 2007 Vadim Zeitlin <vadim@wxwidgets.org>
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "testprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #ifndef WX_PRECOMP
21 #include "wx/app.h"
22 #include "wx/textctrl.h"
23 #endif // WX_PRECOMP
24
25 // ----------------------------------------------------------------------------
26 // test class
27 // ----------------------------------------------------------------------------
28
29 class TextCtrlTestCase : public CppUnit::TestCase
30 {
31 public:
32 TextCtrlTestCase() { }
33
34 virtual void setUp();
35 virtual void tearDown();
36
37 private:
38 CPPUNIT_TEST_SUITE( TextCtrlTestCase );
39 CPPUNIT_TEST( SetValue );
40 CPPUNIT_TEST( TextChangeEvents );
41 CPPUNIT_TEST_SUITE_END();
42
43 void SetValue();
44 void TextChangeEvents();
45
46 wxTextCtrl *m_text;
47 wxFrame *m_frame;
48
49 DECLARE_NO_COPY_CLASS(TextCtrlTestCase)
50 };
51
52 // register in the unnamed registry so that these tests are run by default
53 CPPUNIT_TEST_SUITE_REGISTRATION( TextCtrlTestCase );
54
55 // also include in it's own registry so that these tests can be run alone
56 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TextCtrlTestCase, "TextCtrlTestCase" );
57
58 // ----------------------------------------------------------------------------
59 // test initialization
60 // ----------------------------------------------------------------------------
61
62 void TextCtrlTestCase::setUp()
63 {
64 m_text = new wxTextCtrl(wxTheApp->GetTopWindow(), wxID_ANY);
65 }
66
67 void TextCtrlTestCase::tearDown()
68 {
69 delete m_text;
70 m_text = NULL;
71 }
72
73 // ----------------------------------------------------------------------------
74 // tests themselves
75 // ----------------------------------------------------------------------------
76
77 void TextCtrlTestCase::SetValue()
78 {
79 CPPUNIT_ASSERT( m_text->IsEmpty() );
80
81 m_text->SetValue("foo");
82 CPPUNIT_ASSERT_EQUAL( "foo", m_text->GetValue() );
83
84 m_text->SetValue("");
85 CPPUNIT_ASSERT( m_text->IsEmpty() );
86
87 m_text->SetValue("hi");
88 CPPUNIT_ASSERT_EQUAL( "hi", m_text->GetValue() );
89
90 m_text->SetValue("bye");
91 CPPUNIT_ASSERT_EQUAL( "bye", m_text->GetValue() );
92 }
93
94 void TextCtrlTestCase::TextChangeEvents()
95 {
96 class TextTestEventHandler : public wxEvtHandler
97 {
98 public:
99 TextTestEventHandler() { m_events = 0; }
100
101 // calling this automatically resets the events counter
102 int GetEvents()
103 {
104 const int events = m_events;
105 m_events = 0;
106 return events;
107 }
108
109 void OnText(wxCommandEvent& WXUNUSED(event)) { m_events++; }
110
111 private:
112 int m_events;
113 } handler;
114
115 m_text->Connect(wxEVT_COMMAND_TEXT_UPDATED,
116 wxCommandEventHandler(TextTestEventHandler::OnText),
117 NULL,
118 &handler);
119
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() );
123
124 m_text->SetValue("foo");
125 CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
126
127 m_text->SetValue("foo");
128 CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
129
130 m_text->ChangeValue("bar");
131 CPPUNIT_ASSERT_EQUAL( 0, handler.GetEvents() );
132
133 m_text->AppendText("bar");
134 CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
135
136 m_text->Replace(3, 6, "baz");
137 CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
138
139 m_text->Remove(0, 3);
140 CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
141
142 m_text->WriteText("foo");
143 CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
144
145 m_text->Clear();
146 CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
147 }
148