]>
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)
6 // Copyright: (c) 2007, 2008 Vadim Zeitlin <vadim@wxwidgets.org>
7 ///////////////////////////////////////////////////////////////////////////////
14 #include "wx/textentry.h"
15 #include "wx/window.h"
18 #include "textentrytest.h"
19 #include "testableframe.h"
20 #include "wx/uiaction.h"
22 void TextEntryTestCase::SetValue()
24 wxTextEntry
* const entry
= GetTestEntry();
26 CPPUNIT_ASSERT( entry
->IsEmpty() );
28 entry
->SetValue("foo");
29 CPPUNIT_ASSERT_EQUAL( "foo", entry
->GetValue() );
32 CPPUNIT_ASSERT( entry
->IsEmpty() );
34 entry
->SetValue("hi");
35 CPPUNIT_ASSERT_EQUAL( "hi", entry
->GetValue() );
37 entry
->SetValue("bye");
38 CPPUNIT_ASSERT_EQUAL( "bye", entry
->GetValue() );
41 void TextEntryTestCase::TextChangeEvents()
43 EventCounter
updated(GetTestWindow(), wxEVT_TEXT
);
45 wxTextEntry
* const entry
= GetTestEntry();
47 // notice that SetValue() generates an event even if the text didn't change
49 CPPUNIT_ASSERT_EQUAL( 1, updated
.GetCount() );
52 entry
->SetValue("foo");
53 CPPUNIT_ASSERT_EQUAL( 1, updated
.GetCount() );
56 entry
->SetValue("foo");
57 CPPUNIT_ASSERT_EQUAL( 1, updated
.GetCount() );
60 entry
->ChangeValue("bar");
61 CPPUNIT_ASSERT_EQUAL( 0, updated
.GetCount() );
63 entry
->AppendText("bar");
64 CPPUNIT_ASSERT_EQUAL( 1, updated
.GetCount() );
67 entry
->Replace(3, 6, "baz");
68 CPPUNIT_ASSERT_EQUAL( 1, updated
.GetCount() );
72 CPPUNIT_ASSERT_EQUAL( 1, updated
.GetCount() );
75 entry
->WriteText("foo");
76 CPPUNIT_ASSERT_EQUAL( 1, updated
.GetCount() );
80 CPPUNIT_ASSERT_EQUAL( 1, updated
.GetCount() );
84 void TextEntryTestCase::CheckStringSelection(const char *sel
)
86 CPPUNIT_ASSERT_EQUAL( sel
, GetTestEntry()->GetStringSelection() );
89 void TextEntryTestCase::AssertSelection(int from
, int to
, const char *sel
)
91 wxTextEntry
* const entry
= GetTestEntry();
93 CPPUNIT_ASSERT( entry
->HasSelection() );
97 entry
->GetSelection(&fromReal
, &toReal
);
98 CPPUNIT_ASSERT_EQUAL( from
, fromReal
);
99 CPPUNIT_ASSERT_EQUAL( to
, toReal
);
101 CPPUNIT_ASSERT_EQUAL( from
, entry
->GetInsertionPoint() );
103 CheckStringSelection(sel
);
106 void TextEntryTestCase::Selection()
108 wxTextEntry
* const entry
= GetTestEntry();
110 entry
->SetValue("0123456789");
112 entry
->SetSelection(2, 4);
113 AssertSelection(2, 4, "23"); // not "234"!
115 entry
->SetSelection(3, -1);
116 AssertSelection(3, 10, "3456789");
119 AssertSelection(0, 10, "0123456789");
121 entry
->SetSelection(0, 0);
122 CPPUNIT_ASSERT( !entry
->HasSelection() );
125 void TextEntryTestCase::InsertionPoint()
127 wxTextEntry
* const entry
= GetTestEntry();
129 CPPUNIT_ASSERT_EQUAL( 0, entry
->GetLastPosition() );
130 CPPUNIT_ASSERT_EQUAL( 0, entry
->GetInsertionPoint() );
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() );
136 entry
->AppendText("12"); // should update the insertion point position
137 CPPUNIT_ASSERT_EQUAL( 3, entry
->GetLastPosition() );
138 CPPUNIT_ASSERT_EQUAL( 3, entry
->GetInsertionPoint() );
140 entry
->SetInsertionPoint(1);
141 CPPUNIT_ASSERT_EQUAL( 3, entry
->GetLastPosition() );
142 CPPUNIT_ASSERT_EQUAL( 1, entry
->GetInsertionPoint() );
144 entry
->SetInsertionPointEnd();
145 CPPUNIT_ASSERT_EQUAL( 3, entry
->GetInsertionPoint() );
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() );
152 entry
->SetValue("something different"); // should still reset the caret
153 CPPUNIT_ASSERT_EQUAL( 0, entry
->GetInsertionPoint() );
156 void TextEntryTestCase::Replace()
158 wxTextEntry
* const entry
= GetTestEntry();
160 entry
->SetValue("Hello replace!"
162 entry
->SetInsertionPoint(0);
164 entry
->Replace(6, 13, "changed");
166 CPPUNIT_ASSERT_EQUAL("Hello changed!"
169 CPPUNIT_ASSERT_EQUAL(13, entry
->GetInsertionPoint());
171 entry
->Replace(13, -1, "");
172 CPPUNIT_ASSERT_EQUAL("Hello changed", entry
->GetValue());
173 CPPUNIT_ASSERT_EQUAL(13, entry
->GetInsertionPoint());
175 entry
->Replace(0, 6, "Un");
176 CPPUNIT_ASSERT_EQUAL("Unchanged", entry
->GetValue());
177 CPPUNIT_ASSERT_EQUAL(2, entry
->GetInsertionPoint());
180 void TextEntryTestCase::Editable()
182 #if wxUSE_UIACTIONSIMULATOR
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 &&
198 wxTextEntry
* const entry
= GetTestEntry();
199 wxWindow
* const window
= GetTestWindow();
201 EventCounter
updated(window
, wxEVT_TEXT
);
206 wxUIActionSimulator sim
;
210 CPPUNIT_ASSERT_EQUAL("abcdef", entry
->GetValue());
211 CPPUNIT_ASSERT_EQUAL(6, updated
.GetCount());
215 entry
->SetEditable(false);
219 CPPUNIT_ASSERT_EQUAL("abcdef", entry
->GetValue());
220 CPPUNIT_ASSERT_EQUAL(0, updated
.GetCount());
224 void TextEntryTestCase::Hint()
226 GetTestEntry()->SetHint("This is a hint");
227 CPPUNIT_ASSERT_EQUAL("", GetTestEntry()->GetValue());
230 void TextEntryTestCase::CopyPaste()
233 wxTextEntry
* const entry
= GetTestEntry();
235 entry
->AppendText("sometext");
238 if(entry
->CanCopy() && entry
->CanPaste())
242 CPPUNIT_ASSERT(entry
->IsEmpty());
247 CPPUNIT_ASSERT_EQUAL("sometext", entry
->GetValue());
252 void TextEntryTestCase::UndoRedo()
254 wxTextEntry
* const entry
= GetTestEntry();
256 entry
->AppendText("sometext");
261 CPPUNIT_ASSERT(entry
->IsEmpty());
266 CPPUNIT_ASSERT_EQUAL("sometext", entry
->GetValue());