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 ///////////////////////////////////////////////////////////////////////////////
14 #include "wx/textentry.h"
15 #include "wx/window.h"
18 #include "textentrytest.h"
20 void TextEntryTestCase::SetValue()
22 wxTextEntry
* const entry
= GetTestEntry();
24 CPPUNIT_ASSERT( entry
->IsEmpty() );
26 entry
->SetValue("foo");
27 CPPUNIT_ASSERT_EQUAL( "foo", entry
->GetValue() );
30 CPPUNIT_ASSERT( entry
->IsEmpty() );
32 entry
->SetValue("hi");
33 CPPUNIT_ASSERT_EQUAL( "hi", entry
->GetValue() );
35 entry
->SetValue("bye");
36 CPPUNIT_ASSERT_EQUAL( "bye", entry
->GetValue() );
41 class TextTestEventHandler
: public wxEvtHandler
44 TextTestEventHandler() { m_events
= 0; }
46 // calling this automatically resets the events counter
49 const int events
= m_events
;
54 void OnText(wxCommandEvent
& WXUNUSED(event
)) { m_events
++; }
61 void TextEntryTestCase::TextChangeEvents()
63 TextTestEventHandler handler
;
65 GetTestWindow()->Connect
67 wxEVT_COMMAND_TEXT_UPDATED
,
68 wxCommandEventHandler(TextTestEventHandler::OnText
),
73 wxTextEntry
* const entry
= GetTestEntry();
75 // notice that SetValue() generates an event even if the text didn't change
77 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetEvents() );
79 entry
->SetValue("foo");
80 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetEvents() );
82 entry
->SetValue("foo");
83 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetEvents() );
85 entry
->ChangeValue("bar");
86 CPPUNIT_ASSERT_EQUAL( 0, handler
.GetEvents() );
88 entry
->AppendText("bar");
89 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetEvents() );
91 entry
->Replace(3, 6, "baz");
92 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetEvents() );
95 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetEvents() );
97 entry
->WriteText("foo");
98 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetEvents() );
101 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetEvents() );
104 void TextEntryTestCase::CheckStringSelection(const char *sel
)
106 CPPUNIT_ASSERT_EQUAL( sel
, GetTestEntry()->GetStringSelection() );
109 void TextEntryTestCase::AssertSelection(int from
, int to
, const char *sel
)
111 wxTextEntry
* const entry
= GetTestEntry();
113 CPPUNIT_ASSERT( entry
->HasSelection() );
117 entry
->GetSelection(&fromReal
, &toReal
);
118 CPPUNIT_ASSERT_EQUAL( from
, fromReal
);
119 CPPUNIT_ASSERT_EQUAL( to
, toReal
);
121 CPPUNIT_ASSERT_EQUAL( from
, entry
->GetInsertionPoint() );
123 CheckStringSelection(sel
);
126 void TextEntryTestCase::Selection()
128 wxTextEntry
* const entry
= GetTestEntry();
130 entry
->SetValue("0123456789");
132 entry
->SetSelection(2, 4);
133 AssertSelection(2, 4, "23"); // not "234"!
135 entry
->SetSelection(3, -1);
136 AssertSelection(3, 10, "3456789");
139 AssertSelection(0, 10, "0123456789");
141 entry
->SetSelection(0, 0);
142 CPPUNIT_ASSERT( !entry
->HasSelection() );
145 void TextEntryTestCase::InsertionPoint()
147 wxTextEntry
* const entry
= GetTestEntry();
149 CPPUNIT_ASSERT_EQUAL( 0, entry
->GetLastPosition() );
150 CPPUNIT_ASSERT_EQUAL( 0, entry
->GetInsertionPoint() );
152 entry
->SetValue("0"); // should put the insertion point in front
153 CPPUNIT_ASSERT_EQUAL( 1, entry
->GetLastPosition() );
154 CPPUNIT_ASSERT_EQUAL( 0, entry
->GetInsertionPoint() );
156 entry
->AppendText("12"); // should update the insertion point position
157 CPPUNIT_ASSERT_EQUAL( 3, entry
->GetLastPosition() );
158 CPPUNIT_ASSERT_EQUAL( 3, entry
->GetInsertionPoint() );
160 entry
->SetInsertionPoint(1);
161 CPPUNIT_ASSERT_EQUAL( 3, entry
->GetLastPosition() );
162 CPPUNIT_ASSERT_EQUAL( 1, entry
->GetInsertionPoint() );
164 entry
->SetInsertionPointEnd();
165 CPPUNIT_ASSERT_EQUAL( 3, entry
->GetInsertionPoint() );
167 entry
->SetInsertionPoint(0);
168 entry
->WriteText("-"); // should move it after the written text
169 CPPUNIT_ASSERT_EQUAL( 4, entry
->GetLastPosition() );
170 CPPUNIT_ASSERT_EQUAL( 1, entry
->GetInsertionPoint() );
173 void TextEntryTestCase::Replace()
175 wxTextEntry
* const entry
= GetTestEntry();
177 entry
->SetValue("Hello replace!"
179 entry
->SetInsertionPoint(0);
181 entry
->Replace(6, 13, "changed");
183 CPPUNIT_ASSERT_EQUAL("Hello changed!"
186 CPPUNIT_ASSERT_EQUAL(13, entry
->GetInsertionPoint());
188 entry
->Replace(13, -1, "");
189 CPPUNIT_ASSERT_EQUAL("Hello changed", entry
->GetValue());
190 CPPUNIT_ASSERT_EQUAL(13, entry
->GetInsertionPoint());
192 entry
->Replace(0, 6, "Un");
193 CPPUNIT_ASSERT_EQUAL("Unchanged", entry
->GetValue());
194 CPPUNIT_ASSERT_EQUAL(2, entry
->GetInsertionPoint());