]>
git.saurik.com Git - wxWidgets.git/blob - 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)
7 // Copyright: (c) 2007, 2008 Vadim Zeitlin <vadim@wxwidgets.org>
8 ///////////////////////////////////////////////////////////////////////////////
15 #include "wx/textentry.h"
16 #include "wx/window.h"
19 #include "textentrytest.h"
20 #include "testableframe.h"
21 #include "wx/uiaction.h"
23 void TextEntryTestCase::SetValue()
25 wxTextEntry
* const entry
= GetTestEntry();
27 CPPUNIT_ASSERT( entry
->IsEmpty() );
29 entry
->SetValue("foo");
30 CPPUNIT_ASSERT_EQUAL( "foo", entry
->GetValue() );
33 CPPUNIT_ASSERT( entry
->IsEmpty() );
35 entry
->SetValue("hi");
36 CPPUNIT_ASSERT_EQUAL( "hi", entry
->GetValue() );
38 entry
->SetValue("bye");
39 CPPUNIT_ASSERT_EQUAL( "bye", entry
->GetValue() );
42 void TextEntryTestCase::TextChangeEvents()
44 EventCounter
updated(GetTestWindow(), wxEVT_TEXT
);
46 wxTextEntry
* const entry
= GetTestEntry();
48 // notice that SetValue() generates an event even if the text didn't change
50 CPPUNIT_ASSERT_EQUAL( 1, updated
.GetCount() );
53 entry
->SetValue("foo");
54 CPPUNIT_ASSERT_EQUAL( 1, updated
.GetCount() );
57 entry
->SetValue("foo");
58 CPPUNIT_ASSERT_EQUAL( 1, updated
.GetCount() );
61 entry
->ChangeValue("bar");
62 CPPUNIT_ASSERT_EQUAL( 0, updated
.GetCount() );
64 entry
->AppendText("bar");
65 CPPUNIT_ASSERT_EQUAL( 1, updated
.GetCount() );
68 entry
->Replace(3, 6, "baz");
69 CPPUNIT_ASSERT_EQUAL( 1, updated
.GetCount() );
73 CPPUNIT_ASSERT_EQUAL( 1, updated
.GetCount() );
76 entry
->WriteText("foo");
77 CPPUNIT_ASSERT_EQUAL( 1, updated
.GetCount() );
81 CPPUNIT_ASSERT_EQUAL( 1, updated
.GetCount() );
85 void TextEntryTestCase::CheckStringSelection(const char *sel
)
87 CPPUNIT_ASSERT_EQUAL( sel
, GetTestEntry()->GetStringSelection() );
90 void TextEntryTestCase::AssertSelection(int from
, int to
, const char *sel
)
92 wxTextEntry
* const entry
= GetTestEntry();
94 CPPUNIT_ASSERT( entry
->HasSelection() );
98 entry
->GetSelection(&fromReal
, &toReal
);
99 CPPUNIT_ASSERT_EQUAL( from
, fromReal
);
100 CPPUNIT_ASSERT_EQUAL( to
, toReal
);
102 CPPUNIT_ASSERT_EQUAL( from
, entry
->GetInsertionPoint() );
104 CheckStringSelection(sel
);
107 void TextEntryTestCase::Selection()
109 wxTextEntry
* const entry
= GetTestEntry();
111 entry
->SetValue("0123456789");
113 entry
->SetSelection(2, 4);
114 AssertSelection(2, 4, "23"); // not "234"!
116 entry
->SetSelection(3, -1);
117 AssertSelection(3, 10, "3456789");
120 AssertSelection(0, 10, "0123456789");
122 entry
->SetSelection(0, 0);
123 CPPUNIT_ASSERT( !entry
->HasSelection() );
126 void TextEntryTestCase::InsertionPoint()
128 wxTextEntry
* const entry
= GetTestEntry();
130 CPPUNIT_ASSERT_EQUAL( 0, entry
->GetLastPosition() );
131 CPPUNIT_ASSERT_EQUAL( 0, entry
->GetInsertionPoint() );
133 entry
->SetValue("0"); // should put the insertion point in front
134 CPPUNIT_ASSERT_EQUAL( 1, entry
->GetLastPosition() );
135 CPPUNIT_ASSERT_EQUAL( 0, entry
->GetInsertionPoint() );
137 entry
->AppendText("12"); // should update the insertion point position
138 CPPUNIT_ASSERT_EQUAL( 3, entry
->GetLastPosition() );
139 CPPUNIT_ASSERT_EQUAL( 3, entry
->GetInsertionPoint() );
141 entry
->SetInsertionPoint(1);
142 CPPUNIT_ASSERT_EQUAL( 3, entry
->GetLastPosition() );
143 CPPUNIT_ASSERT_EQUAL( 1, entry
->GetInsertionPoint() );
145 entry
->SetInsertionPointEnd();
146 CPPUNIT_ASSERT_EQUAL( 3, entry
->GetInsertionPoint() );
148 entry
->SetInsertionPoint(0);
149 entry
->WriteText("-"); // should move it after the written text
150 CPPUNIT_ASSERT_EQUAL( 4, entry
->GetLastPosition() );
151 CPPUNIT_ASSERT_EQUAL( 1, entry
->GetInsertionPoint() );
153 entry
->SetValue("something different"); // should still reset the caret
154 CPPUNIT_ASSERT_EQUAL( 0, entry
->GetInsertionPoint() );
157 void TextEntryTestCase::Replace()
159 wxTextEntry
* const entry
= GetTestEntry();
161 entry
->SetValue("Hello replace!"
163 entry
->SetInsertionPoint(0);
165 entry
->Replace(6, 13, "changed");
167 CPPUNIT_ASSERT_EQUAL("Hello changed!"
170 CPPUNIT_ASSERT_EQUAL(13, entry
->GetInsertionPoint());
172 entry
->Replace(13, -1, "");
173 CPPUNIT_ASSERT_EQUAL("Hello changed", entry
->GetValue());
174 CPPUNIT_ASSERT_EQUAL(13, entry
->GetInsertionPoint());
176 entry
->Replace(0, 6, "Un");
177 CPPUNIT_ASSERT_EQUAL("Unchanged", entry
->GetValue());
178 CPPUNIT_ASSERT_EQUAL(2, entry
->GetInsertionPoint());
181 void TextEntryTestCase::Editable()
183 #if wxUSE_UIACTIONSIMULATOR
186 // FIXME: For some reason this test regularly (although not always) fails
187 // in wxGTK build bot builds when testing wxBitmapComboBox, but I
188 // can't reproduce the failure locally. For now, disable this check
189 // to let the entire test suite pass in automatic tests instead of
190 // failing sporadically.
191 if ( wxStrcmp(GetTestWindow()->GetClassInfo()->GetClassName(),
192 "wxBitmapComboBox") == 0 &&
199 wxTextEntry
* const entry
= GetTestEntry();
200 wxWindow
* const window
= GetTestWindow();
202 EventCounter
updated(window
, wxEVT_TEXT
);
207 wxUIActionSimulator sim
;
211 CPPUNIT_ASSERT_EQUAL("abcdef", entry
->GetValue());
212 CPPUNIT_ASSERT_EQUAL(6, updated
.GetCount());
216 entry
->SetEditable(false);
220 CPPUNIT_ASSERT_EQUAL("abcdef", entry
->GetValue());
221 CPPUNIT_ASSERT_EQUAL(0, updated
.GetCount());
225 void TextEntryTestCase::Hint()
227 GetTestEntry()->SetHint("This is a hint");
228 CPPUNIT_ASSERT_EQUAL("", GetTestEntry()->GetValue());
231 void TextEntryTestCase::CopyPaste()
234 wxTextEntry
* const entry
= GetTestEntry();
236 entry
->AppendText("sometext");
239 if(entry
->CanCopy() && entry
->CanPaste())
243 CPPUNIT_ASSERT(entry
->IsEmpty());
248 CPPUNIT_ASSERT_EQUAL("sometext", entry
->GetValue());
253 void TextEntryTestCase::UndoRedo()
255 wxTextEntry
* const entry
= GetTestEntry();
257 entry
->AppendText("sometext");
262 CPPUNIT_ASSERT(entry
->IsEmpty());
267 CPPUNIT_ASSERT_EQUAL("sometext", entry
->GetValue());