Define _CRT_NONSTDC_NO_WARNINGS for zlib compilation with MSVC.
[wxWidgets.git] / tests / controls / textentrytest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/textentrytest.cpp
3 // Purpose: TestEntryTestCase implementation
4 // Author: Vadim Zeitlin
5 // Created: 2008-09-19 (extracted from textctrltest.cpp)
6 // Copyright: (c) 2007, 2008 Vadim Zeitlin <vadim@wxwidgets.org>
7 ///////////////////////////////////////////////////////////////////////////////
8
9 #include "testprec.h"
10
11 #ifndef WX_PRECOMP
12 #include "wx/app.h"
13 #include "wx/event.h"
14 #include "wx/textentry.h"
15 #include "wx/window.h"
16 #endif // WX_PRECOMP
17
18 #include "textentrytest.h"
19 #include "testableframe.h"
20 #include "wx/uiaction.h"
21
22 void TextEntryTestCase::SetValue()
23 {
24 wxTextEntry * const entry = GetTestEntry();
25
26 CPPUNIT_ASSERT( entry->IsEmpty() );
27
28 entry->SetValue("foo");
29 CPPUNIT_ASSERT_EQUAL( "foo", entry->GetValue() );
30
31 entry->SetValue("");
32 CPPUNIT_ASSERT( entry->IsEmpty() );
33
34 entry->SetValue("hi");
35 CPPUNIT_ASSERT_EQUAL( "hi", entry->GetValue() );
36
37 entry->SetValue("bye");
38 CPPUNIT_ASSERT_EQUAL( "bye", entry->GetValue() );
39 }
40
41 void TextEntryTestCase::TextChangeEvents()
42 {
43 EventCounter updated(GetTestWindow(), wxEVT_TEXT);
44
45 wxTextEntry * const entry = GetTestEntry();
46
47 // notice that SetValue() generates an event even if the text didn't change
48 entry->SetValue("");
49 CPPUNIT_ASSERT_EQUAL( 1, updated.GetCount() );
50 updated.Clear();
51
52 entry->SetValue("foo");
53 CPPUNIT_ASSERT_EQUAL( 1, updated.GetCount() );
54 updated.Clear();
55
56 entry->SetValue("foo");
57 CPPUNIT_ASSERT_EQUAL( 1, updated.GetCount() );
58 updated.Clear();
59
60 entry->ChangeValue("bar");
61 CPPUNIT_ASSERT_EQUAL( 0, updated.GetCount() );
62
63 entry->AppendText("bar");
64 CPPUNIT_ASSERT_EQUAL( 1, updated.GetCount() );
65 updated.Clear();
66
67 entry->Replace(3, 6, "baz");
68 CPPUNIT_ASSERT_EQUAL( 1, updated.GetCount() );
69 updated.Clear();
70
71 entry->Remove(0, 3);
72 CPPUNIT_ASSERT_EQUAL( 1, updated.GetCount() );
73 updated.Clear();
74
75 entry->WriteText("foo");
76 CPPUNIT_ASSERT_EQUAL( 1, updated.GetCount() );
77 updated.Clear();
78
79 entry->Clear();
80 CPPUNIT_ASSERT_EQUAL( 1, updated.GetCount() );
81 updated.Clear();
82 }
83
84 void TextEntryTestCase::CheckStringSelection(const char *sel)
85 {
86 CPPUNIT_ASSERT_EQUAL( sel, GetTestEntry()->GetStringSelection() );
87 }
88
89 void TextEntryTestCase::AssertSelection(int from, int to, const char *sel)
90 {
91 wxTextEntry * const entry = GetTestEntry();
92
93 CPPUNIT_ASSERT( entry->HasSelection() );
94
95 long fromReal,
96 toReal;
97 entry->GetSelection(&fromReal, &toReal);
98 CPPUNIT_ASSERT_EQUAL( from, fromReal );
99 CPPUNIT_ASSERT_EQUAL( to, toReal );
100
101 CPPUNIT_ASSERT_EQUAL( from, entry->GetInsertionPoint() );
102
103 CheckStringSelection(sel);
104 }
105
106 void TextEntryTestCase::Selection()
107 {
108 wxTextEntry * const entry = GetTestEntry();
109
110 entry->SetValue("0123456789");
111
112 entry->SetSelection(2, 4);
113 AssertSelection(2, 4, "23"); // not "234"!
114
115 entry->SetSelection(3, -1);
116 AssertSelection(3, 10, "3456789");
117
118 entry->SelectAll();
119 AssertSelection(0, 10, "0123456789");
120
121 entry->SetSelection(0, 0);
122 CPPUNIT_ASSERT( !entry->HasSelection() );
123 }
124
125 void TextEntryTestCase::InsertionPoint()
126 {
127 wxTextEntry * const entry = GetTestEntry();
128
129 CPPUNIT_ASSERT_EQUAL( 0, entry->GetLastPosition() );
130 CPPUNIT_ASSERT_EQUAL( 0, entry->GetInsertionPoint() );
131
132 entry->SetValue("0"); // should put the insertion point in front
133 CPPUNIT_ASSERT_EQUAL( 1, entry->GetLastPosition() );
134 CPPUNIT_ASSERT_EQUAL( 0, entry->GetInsertionPoint() );
135
136 entry->AppendText("12"); // should update the insertion point position
137 CPPUNIT_ASSERT_EQUAL( 3, entry->GetLastPosition() );
138 CPPUNIT_ASSERT_EQUAL( 3, entry->GetInsertionPoint() );
139
140 entry->SetInsertionPoint(1);
141 CPPUNIT_ASSERT_EQUAL( 3, entry->GetLastPosition() );
142 CPPUNIT_ASSERT_EQUAL( 1, entry->GetInsertionPoint() );
143
144 entry->SetInsertionPointEnd();
145 CPPUNIT_ASSERT_EQUAL( 3, entry->GetInsertionPoint() );
146
147 entry->SetInsertionPoint(0);
148 entry->WriteText("-"); // should move it after the written text
149 CPPUNIT_ASSERT_EQUAL( 4, entry->GetLastPosition() );
150 CPPUNIT_ASSERT_EQUAL( 1, entry->GetInsertionPoint() );
151
152 entry->SetValue("something different"); // should still reset the caret
153 CPPUNIT_ASSERT_EQUAL( 0, entry->GetInsertionPoint() );
154 }
155
156 void TextEntryTestCase::Replace()
157 {
158 wxTextEntry * const entry = GetTestEntry();
159
160 entry->SetValue("Hello replace!"
161 "0123456789012");
162 entry->SetInsertionPoint(0);
163
164 entry->Replace(6, 13, "changed");
165
166 CPPUNIT_ASSERT_EQUAL("Hello changed!"
167 "0123456789012",
168 entry->GetValue());
169 CPPUNIT_ASSERT_EQUAL(13, entry->GetInsertionPoint());
170
171 entry->Replace(13, -1, "");
172 CPPUNIT_ASSERT_EQUAL("Hello changed", entry->GetValue());
173 CPPUNIT_ASSERT_EQUAL(13, entry->GetInsertionPoint());
174
175 entry->Replace(0, 6, "Un");
176 CPPUNIT_ASSERT_EQUAL("Unchanged", entry->GetValue());
177 CPPUNIT_ASSERT_EQUAL(2, entry->GetInsertionPoint());
178 }
179
180 void TextEntryTestCase::Editable()
181 {
182 #if wxUSE_UIACTIONSIMULATOR
183
184 #ifdef __WXGTK__
185 // FIXME: For some reason this test regularly (although not always) fails
186 // in wxGTK build bot builds when testing wxBitmapComboBox, but I
187 // can't reproduce the failure locally. For now, disable this check
188 // to let the entire test suite pass in automatic tests instead of
189 // failing sporadically.
190 if ( wxStrcmp(GetTestWindow()->GetClassInfo()->GetClassName(),
191 "wxBitmapComboBox") == 0 &&
192 IsAutomaticTest() )
193 {
194 return;
195 }
196 #endif // __WGTK__
197
198 wxTextEntry * const entry = GetTestEntry();
199 wxWindow * const window = GetTestWindow();
200
201 EventCounter updated(window, wxEVT_TEXT);
202
203 window->SetFocus();
204 wxYield();
205
206 wxUIActionSimulator sim;
207 sim.Text("abcdef");
208 wxYield();
209
210 CPPUNIT_ASSERT_EQUAL("abcdef", entry->GetValue());
211 CPPUNIT_ASSERT_EQUAL(6, updated.GetCount());
212
213 updated.Clear();
214
215 entry->SetEditable(false);
216 sim.Text("gh");
217 wxYield();
218
219 CPPUNIT_ASSERT_EQUAL("abcdef", entry->GetValue());
220 CPPUNIT_ASSERT_EQUAL(0, updated.GetCount());
221 #endif
222 }
223
224 void TextEntryTestCase::Hint()
225 {
226 GetTestEntry()->SetHint("This is a hint");
227 CPPUNIT_ASSERT_EQUAL("", GetTestEntry()->GetValue());
228 }
229
230 void TextEntryTestCase::CopyPaste()
231 {
232 #ifndef __WXOSX__
233 wxTextEntry * const entry = GetTestEntry();
234
235 entry->AppendText("sometext");
236 entry->SelectAll();
237
238 if(entry->CanCopy() && entry->CanPaste())
239 {
240 entry->Copy();
241 entry->Clear();
242 CPPUNIT_ASSERT(entry->IsEmpty());
243
244 wxYield();
245
246 entry->Paste();
247 CPPUNIT_ASSERT_EQUAL("sometext", entry->GetValue());
248 }
249 #endif
250 }
251
252 void TextEntryTestCase::UndoRedo()
253 {
254 wxTextEntry * const entry = GetTestEntry();
255
256 entry->AppendText("sometext");
257
258 if(entry->CanUndo())
259 {
260 entry->Undo();
261 CPPUNIT_ASSERT(entry->IsEmpty());
262
263 if(entry->CanRedo())
264 {
265 entry->Redo();
266 CPPUNIT_ASSERT_EQUAL("sometext", entry->GetValue());
267 }
268 }
269 }