]> git.saurik.com Git - wxWidgets.git/blob - tests/controls/textctrltest.cpp
avoiding warning because of missing define
[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( Selection );
42 CPPUNIT_TEST( InsertionPoint );
43 CPPUNIT_TEST_SUITE_END();
44
45 void SetValue();
46 void TextChangeEvents();
47 void Selection();
48 void InsertionPoint();
49
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);
53
54 wxTextCtrl *m_text;
55 wxFrame *m_frame;
56
57 DECLARE_NO_COPY_CLASS(TextCtrlTestCase)
58 };
59
60 // register in the unnamed registry so that these tests are run by default
61 CPPUNIT_TEST_SUITE_REGISTRATION( TextCtrlTestCase );
62
63 // also include in it's own registry so that these tests can be run alone
64 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TextCtrlTestCase, "TextCtrlTestCase" );
65
66 // ----------------------------------------------------------------------------
67 // test initialization
68 // ----------------------------------------------------------------------------
69
70 void TextCtrlTestCase::setUp()
71 {
72 m_text = new wxTextCtrl(wxTheApp->GetTopWindow(), wxID_ANY);
73 }
74
75 void TextCtrlTestCase::tearDown()
76 {
77 delete m_text;
78 m_text = NULL;
79 }
80
81 // ----------------------------------------------------------------------------
82 // tests themselves
83 // ----------------------------------------------------------------------------
84
85 void TextCtrlTestCase::SetValue()
86 {
87 CPPUNIT_ASSERT( m_text->IsEmpty() );
88
89 m_text->SetValue("foo");
90 CPPUNIT_ASSERT_EQUAL( "foo", m_text->GetValue() );
91
92 m_text->SetValue("");
93 CPPUNIT_ASSERT( m_text->IsEmpty() );
94
95 m_text->SetValue("hi");
96 CPPUNIT_ASSERT_EQUAL( "hi", m_text->GetValue() );
97
98 m_text->SetValue("bye");
99 CPPUNIT_ASSERT_EQUAL( "bye", m_text->GetValue() );
100 }
101
102 void TextCtrlTestCase::TextChangeEvents()
103 {
104 class TextTestEventHandler : public wxEvtHandler
105 {
106 public:
107 TextTestEventHandler() { m_events = 0; }
108
109 // calling this automatically resets the events counter
110 int GetEvents()
111 {
112 const int events = m_events;
113 m_events = 0;
114 return events;
115 }
116
117 void OnText(wxCommandEvent& WXUNUSED(event)) { m_events++; }
118
119 private:
120 int m_events;
121 } handler;
122
123 m_text->Connect(wxEVT_COMMAND_TEXT_UPDATED,
124 wxCommandEventHandler(TextTestEventHandler::OnText),
125 NULL,
126 &handler);
127
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() );
131
132 m_text->SetValue("foo");
133 CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
134
135 m_text->SetValue("foo");
136 CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
137
138 m_text->ChangeValue("bar");
139 CPPUNIT_ASSERT_EQUAL( 0, handler.GetEvents() );
140
141 m_text->AppendText("bar");
142 CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
143
144 m_text->Replace(3, 6, "baz");
145 CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
146
147 m_text->Remove(0, 3);
148 CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
149
150 m_text->WriteText("foo");
151 CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
152
153 m_text->Clear();
154 CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
155 }
156
157 void TextCtrlTestCase::AssertSelection(int from, int to, const char *sel)
158 {
159 CPPUNIT_ASSERT( m_text->HasSelection() );
160
161 long fromReal,
162 toReal;
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() );
167
168 CPPUNIT_ASSERT_EQUAL( from, m_text->GetInsertionPoint() );
169 }
170
171 void TextCtrlTestCase::Selection()
172 {
173 m_text->SetValue("0123456789");
174
175 m_text->SetSelection(2, 4);
176 AssertSelection(2, 4, "23"); // not "234"!
177
178 m_text->SetSelection(3, -1);
179 AssertSelection(3, 10, "3456789");
180
181 m_text->SelectAll();
182 AssertSelection(0, 10, "0123456789");
183
184 m_text->SetSelection(0, 0);
185 CPPUNIT_ASSERT( !m_text->HasSelection() );
186 }
187
188 void TextCtrlTestCase::InsertionPoint()
189 {
190 CPPUNIT_ASSERT_EQUAL( 0, m_text->GetLastPosition() );
191 CPPUNIT_ASSERT_EQUAL( 0, m_text->GetInsertionPoint() );
192
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() );
196
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() );
200
201 m_text->SetInsertionPoint(1);
202 CPPUNIT_ASSERT_EQUAL( 3, m_text->GetLastPosition() );
203 CPPUNIT_ASSERT_EQUAL( 1, m_text->GetInsertionPoint() );
204
205 m_text->SetInsertionPointEnd();
206 CPPUNIT_ASSERT_EQUAL( 3, m_text->GetInsertionPoint() );
207
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() );
212 }
213