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 wxTestableFrame
* frame
= wxStaticCast(wxTheApp
->GetTopWindow(),
47 EventCounter
count(GetTestWindow(), wxEVT_COMMAND_TEXT_UPDATED
);
49 wxTextEntry
* const entry
= GetTestEntry();
51 // notice that SetValue() generates an event even if the text didn't change
53 CPPUNIT_ASSERT_EQUAL( 1, frame
->GetEventCount() );
55 entry
->SetValue("foo");
56 CPPUNIT_ASSERT_EQUAL( 1, frame
->GetEventCount() );
58 entry
->SetValue("foo");
59 CPPUNIT_ASSERT_EQUAL( 1, frame
->GetEventCount() );
61 entry
->ChangeValue("bar");
62 CPPUNIT_ASSERT_EQUAL( 0, frame
->GetEventCount() );
64 entry
->AppendText("bar");
65 CPPUNIT_ASSERT_EQUAL( 1, frame
->GetEventCount() );
67 entry
->Replace(3, 6, "baz");
68 CPPUNIT_ASSERT_EQUAL( 1, frame
->GetEventCount() );
71 CPPUNIT_ASSERT_EQUAL( 1, frame
->GetEventCount() );
73 entry
->WriteText("foo");
74 CPPUNIT_ASSERT_EQUAL( 1, frame
->GetEventCount() );
77 CPPUNIT_ASSERT_EQUAL( 1, frame
->GetEventCount() );
80 void TextEntryTestCase::CheckStringSelection(const char *sel
)
82 CPPUNIT_ASSERT_EQUAL( sel
, GetTestEntry()->GetStringSelection() );
85 void TextEntryTestCase::AssertSelection(int from
, int to
, const char *sel
)
87 wxTextEntry
* const entry
= GetTestEntry();
89 CPPUNIT_ASSERT( entry
->HasSelection() );
93 entry
->GetSelection(&fromReal
, &toReal
);
94 CPPUNIT_ASSERT_EQUAL( from
, fromReal
);
95 CPPUNIT_ASSERT_EQUAL( to
, toReal
);
97 CPPUNIT_ASSERT_EQUAL( from
, entry
->GetInsertionPoint() );
99 CheckStringSelection(sel
);
102 void TextEntryTestCase::Selection()
104 wxTextEntry
* const entry
= GetTestEntry();
106 entry
->SetValue("0123456789");
108 entry
->SetSelection(2, 4);
109 AssertSelection(2, 4, "23"); // not "234"!
111 entry
->SetSelection(3, -1);
112 AssertSelection(3, 10, "3456789");
115 AssertSelection(0, 10, "0123456789");
117 entry
->SetSelection(0, 0);
118 CPPUNIT_ASSERT( !entry
->HasSelection() );
121 void TextEntryTestCase::InsertionPoint()
123 wxTextEntry
* const entry
= GetTestEntry();
125 CPPUNIT_ASSERT_EQUAL( 0, entry
->GetLastPosition() );
126 CPPUNIT_ASSERT_EQUAL( 0, entry
->GetInsertionPoint() );
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() );
132 entry
->AppendText("12"); // should update the insertion point position
133 CPPUNIT_ASSERT_EQUAL( 3, entry
->GetLastPosition() );
134 CPPUNIT_ASSERT_EQUAL( 3, entry
->GetInsertionPoint() );
136 entry
->SetInsertionPoint(1);
137 CPPUNIT_ASSERT_EQUAL( 3, entry
->GetLastPosition() );
138 CPPUNIT_ASSERT_EQUAL( 1, entry
->GetInsertionPoint() );
140 entry
->SetInsertionPointEnd();
141 CPPUNIT_ASSERT_EQUAL( 3, entry
->GetInsertionPoint() );
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() );
148 entry
->SetValue("something different"); // should still reset the caret
149 CPPUNIT_ASSERT_EQUAL( 0, entry
->GetInsertionPoint() );
152 void TextEntryTestCase::Replace()
154 wxTextEntry
* const entry
= GetTestEntry();
156 entry
->SetValue("Hello replace!"
158 entry
->SetInsertionPoint(0);
160 entry
->Replace(6, 13, "changed");
162 CPPUNIT_ASSERT_EQUAL("Hello changed!"
165 CPPUNIT_ASSERT_EQUAL(13, entry
->GetInsertionPoint());
167 entry
->Replace(13, -1, "");
168 CPPUNIT_ASSERT_EQUAL("Hello changed", entry
->GetValue());
169 CPPUNIT_ASSERT_EQUAL(13, entry
->GetInsertionPoint());
171 entry
->Replace(0, 6, "Un");
172 CPPUNIT_ASSERT_EQUAL("Unchanged", entry
->GetValue());
173 CPPUNIT_ASSERT_EQUAL(2, entry
->GetInsertionPoint());
176 void TextEntryTestCase::Editable()
178 #if wxUSE_UIACTIONSIMULATOR
179 wxTestableFrame
* frame
= wxStaticCast(wxTheApp
->GetTopWindow(),
182 wxTextEntry
* const entry
= GetTestEntry();
183 wxWindow
* const window
= GetTestWindow();
185 EventCounter
count(window
, wxEVT_COMMAND_TEXT_UPDATED
);
190 wxUIActionSimulator sim
;
194 CPPUNIT_ASSERT_EQUAL("abcdef", entry
->GetValue());
195 CPPUNIT_ASSERT_EQUAL(6, frame
->GetEventCount());
197 entry
->SetEditable(false);
201 CPPUNIT_ASSERT_EQUAL("abcdef", entry
->GetValue());
202 CPPUNIT_ASSERT_EQUAL(0, frame
->GetEventCount());
206 void TextEntryTestCase::Hint()
208 GetTestEntry()->SetHint("This is a hint");
209 CPPUNIT_ASSERT_EQUAL("", GetTestEntry()->GetValue());
212 void TextEntryTestCase::CopyPaste()
215 wxTextEntry
* const entry
= GetTestEntry();
217 entry
->AppendText("sometext");
220 if(entry
->CanCopy() && entry
->CanPaste())
224 CPPUNIT_ASSERT(entry
->IsEmpty());
229 CPPUNIT_ASSERT_EQUAL("sometext", entry
->GetValue());
234 void TextEntryTestCase::UndoRedo()
236 wxTextEntry
* const entry
= GetTestEntry();
238 entry
->AppendText("sometext");
243 CPPUNIT_ASSERT(entry
->IsEmpty());
248 CPPUNIT_ASSERT_EQUAL("sometext", entry
->GetValue());