]> git.saurik.com Git - wxWidgets.git/blame - tests/controls/textentrytest.cpp
Add ClearSelection for msw ie and gtk webkit, with a stub for osx webkit. Document...
[wxWidgets.git] / tests / controls / textentrytest.cpp
CommitLineData
f0f6a32d
VZ
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// RCS-ID: $Id$
7// Copyright: (c) 2007, 2008 Vadim Zeitlin <vadim@wxwidgets.org>
8///////////////////////////////////////////////////////////////////////////////
9
10#include "testprec.h"
11
12#ifndef WX_PRECOMP
232fdc63 13 #include "wx/app.h"
f0f6a32d
VZ
14 #include "wx/event.h"
15 #include "wx/textentry.h"
16 #include "wx/window.h"
17#endif // WX_PRECOMP
18
19#include "textentrytest.h"
232fdc63
VZ
20#include "testableframe.h"
21#include "wx/uiaction.h"
f0f6a32d
VZ
22
23void TextEntryTestCase::SetValue()
24{
25 wxTextEntry * const entry = GetTestEntry();
26
27 CPPUNIT_ASSERT( entry->IsEmpty() );
28
29 entry->SetValue("foo");
30 CPPUNIT_ASSERT_EQUAL( "foo", entry->GetValue() );
31
32 entry->SetValue("");
33 CPPUNIT_ASSERT( entry->IsEmpty() );
34
35 entry->SetValue("hi");
36 CPPUNIT_ASSERT_EQUAL( "hi", entry->GetValue() );
37
38 entry->SetValue("bye");
39 CPPUNIT_ASSERT_EQUAL( "bye", entry->GetValue() );
40}
41
3c778901
VZ
42void TextEntryTestCase::TextChangeEvents()
43{
232fdc63
VZ
44 wxTestableFrame* frame = wxStaticCast(wxTheApp->GetTopWindow(),
45 wxTestableFrame);
f0f6a32d 46
232fdc63 47 EventCounter count(GetTestWindow(), wxEVT_COMMAND_TEXT_UPDATED);
f0f6a32d
VZ
48
49 wxTextEntry * const entry = GetTestEntry();
50
51 // notice that SetValue() generates an event even if the text didn't change
52 entry->SetValue("");
232fdc63 53 CPPUNIT_ASSERT_EQUAL( 1, frame->GetEventCount() );
f0f6a32d
VZ
54
55 entry->SetValue("foo");
232fdc63 56 CPPUNIT_ASSERT_EQUAL( 1, frame->GetEventCount() );
f0f6a32d
VZ
57
58 entry->SetValue("foo");
232fdc63 59 CPPUNIT_ASSERT_EQUAL( 1, frame->GetEventCount() );
f0f6a32d
VZ
60
61 entry->ChangeValue("bar");
232fdc63 62 CPPUNIT_ASSERT_EQUAL( 0, frame->GetEventCount() );
f0f6a32d
VZ
63
64 entry->AppendText("bar");
232fdc63 65 CPPUNIT_ASSERT_EQUAL( 1, frame->GetEventCount() );
f0f6a32d
VZ
66
67 entry->Replace(3, 6, "baz");
232fdc63 68 CPPUNIT_ASSERT_EQUAL( 1, frame->GetEventCount() );
f0f6a32d
VZ
69
70 entry->Remove(0, 3);
232fdc63 71 CPPUNIT_ASSERT_EQUAL( 1, frame->GetEventCount() );
f0f6a32d
VZ
72
73 entry->WriteText("foo");
232fdc63 74 CPPUNIT_ASSERT_EQUAL( 1, frame->GetEventCount() );
f0f6a32d
VZ
75
76 entry->Clear();
232fdc63 77 CPPUNIT_ASSERT_EQUAL( 1, frame->GetEventCount() );
f0f6a32d
VZ
78}
79
dc7f9c9c
VZ
80void TextEntryTestCase::CheckStringSelection(const char *sel)
81{
82 CPPUNIT_ASSERT_EQUAL( sel, GetTestEntry()->GetStringSelection() );
83}
84
f0f6a32d
VZ
85void TextEntryTestCase::AssertSelection(int from, int to, const char *sel)
86{
87 wxTextEntry * const entry = GetTestEntry();
88
89 CPPUNIT_ASSERT( entry->HasSelection() );
90
91 long fromReal,
92 toReal;
93 entry->GetSelection(&fromReal, &toReal);
94 CPPUNIT_ASSERT_EQUAL( from, fromReal );
95 CPPUNIT_ASSERT_EQUAL( to, toReal );
f0f6a32d
VZ
96
97 CPPUNIT_ASSERT_EQUAL( from, entry->GetInsertionPoint() );
dc7f9c9c
VZ
98
99 CheckStringSelection(sel);
f0f6a32d
VZ
100}
101
102void TextEntryTestCase::Selection()
103{
104 wxTextEntry * const entry = GetTestEntry();
105
106 entry->SetValue("0123456789");
107
108 entry->SetSelection(2, 4);
109 AssertSelection(2, 4, "23"); // not "234"!
110
111 entry->SetSelection(3, -1);
112 AssertSelection(3, 10, "3456789");
113
114 entry->SelectAll();
115 AssertSelection(0, 10, "0123456789");
116
117 entry->SetSelection(0, 0);
118 CPPUNIT_ASSERT( !entry->HasSelection() );
119}
120
121void TextEntryTestCase::InsertionPoint()
122{
123 wxTextEntry * const entry = GetTestEntry();
124
125 CPPUNIT_ASSERT_EQUAL( 0, entry->GetLastPosition() );
126 CPPUNIT_ASSERT_EQUAL( 0, entry->GetInsertionPoint() );
127
128 entry->SetValue("0"); // should put the insertion point in front
129 CPPUNIT_ASSERT_EQUAL( 1, entry->GetLastPosition() );
130 CPPUNIT_ASSERT_EQUAL( 0, entry->GetInsertionPoint() );
131
132 entry->AppendText("12"); // should update the insertion point position
133 CPPUNIT_ASSERT_EQUAL( 3, entry->GetLastPosition() );
134 CPPUNIT_ASSERT_EQUAL( 3, entry->GetInsertionPoint() );
135
136 entry->SetInsertionPoint(1);
137 CPPUNIT_ASSERT_EQUAL( 3, entry->GetLastPosition() );
138 CPPUNIT_ASSERT_EQUAL( 1, entry->GetInsertionPoint() );
139
140 entry->SetInsertionPointEnd();
141 CPPUNIT_ASSERT_EQUAL( 3, entry->GetInsertionPoint() );
142
143 entry->SetInsertionPoint(0);
144 entry->WriteText("-"); // should move it after the written text
145 CPPUNIT_ASSERT_EQUAL( 4, entry->GetLastPosition() );
146 CPPUNIT_ASSERT_EQUAL( 1, entry->GetInsertionPoint() );
f40377be
VZ
147
148 entry->SetValue("something different"); // should still reset the caret
149 CPPUNIT_ASSERT_EQUAL( 0, entry->GetInsertionPoint() );
f0f6a32d
VZ
150}
151
059979d8
VZ
152void TextEntryTestCase::Replace()
153{
154 wxTextEntry * const entry = GetTestEntry();
155
d911dc04 156 entry->SetValue("Hello replace!"
059979d8
VZ
157 "0123456789012");
158 entry->SetInsertionPoint(0);
159
160 entry->Replace(6, 13, "changed");
161
d911dc04 162 CPPUNIT_ASSERT_EQUAL("Hello changed!"
059979d8
VZ
163 "0123456789012",
164 entry->GetValue());
165 CPPUNIT_ASSERT_EQUAL(13, entry->GetInsertionPoint());
166
167 entry->Replace(13, -1, "");
168 CPPUNIT_ASSERT_EQUAL("Hello changed", entry->GetValue());
169 CPPUNIT_ASSERT_EQUAL(13, entry->GetInsertionPoint());
170
171 entry->Replace(0, 6, "Un");
172 CPPUNIT_ASSERT_EQUAL("Unchanged", entry->GetValue());
173 CPPUNIT_ASSERT_EQUAL(2, entry->GetInsertionPoint());
174}
175
232fdc63
VZ
176void TextEntryTestCase::Editable()
177{
178#if wxUSE_UIACTIONSIMULATOR
179 wxTestableFrame* frame = wxStaticCast(wxTheApp->GetTopWindow(),
180 wxTestableFrame);
181
182 wxTextEntry * const entry = GetTestEntry();
183 wxWindow * const window = GetTestWindow();
184
185 EventCounter count(window, wxEVT_COMMAND_TEXT_UPDATED);
186
187 window->SetFocus();
188 wxYield();
189
190 wxUIActionSimulator sim;
191 sim.Text("abcdef");
192 wxYield();
193
194 CPPUNIT_ASSERT_EQUAL("abcdef", entry->GetValue());
195 CPPUNIT_ASSERT_EQUAL(6, frame->GetEventCount());
196
197 entry->SetEditable(false);
198 sim.Text("gh");
199 wxYield();
200
201 CPPUNIT_ASSERT_EQUAL("abcdef", entry->GetValue());
202 CPPUNIT_ASSERT_EQUAL(0, frame->GetEventCount());
203#endif
204}
205
206void TextEntryTestCase::Hint()
207{
208 GetTestEntry()->SetHint("This is a hint");
209 CPPUNIT_ASSERT_EQUAL("", GetTestEntry()->GetValue());
210}
211
212void TextEntryTestCase::CopyPaste()
213{
214#ifndef __WXOSX__
215 wxTextEntry * const entry = GetTestEntry();
216
217 entry->AppendText("sometext");
218 entry->SelectAll();
219
220 if(entry->CanCopy() && entry->CanPaste())
221 {
222 entry->Copy();
223 entry->Clear();
224 CPPUNIT_ASSERT(entry->IsEmpty());
225
226 wxYield();
227
228 entry->Paste();
229 CPPUNIT_ASSERT_EQUAL("sometext", entry->GetValue());
230 }
231#endif
232}
233
234void TextEntryTestCase::UndoRedo()
235{
236 wxTextEntry * const entry = GetTestEntry();
237
238 entry->AppendText("sometext");
239
240 if(entry->CanUndo())
241 {
242 entry->Undo();
243 CPPUNIT_ASSERT(entry->IsEmpty());
244
245 if(entry->CanRedo())
246 {
247 entry->Redo();
248 CPPUNIT_ASSERT_EQUAL("sometext", entry->GetValue());
249 }
250 }
251}