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() );
39 void TextEntryTestCase::TextChangeEvents()
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
++; }
60 GetTestWindow()->Connect
62 wxEVT_COMMAND_TEXT_UPDATED
,
63 wxCommandEventHandler(TextTestEventHandler::OnText
),
68 wxTextEntry
* const entry
= GetTestEntry();
70 // notice that SetValue() generates an event even if the text didn't change
72 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetEvents() );
74 entry
->SetValue("foo");
75 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetEvents() );
77 entry
->SetValue("foo");
78 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetEvents() );
80 entry
->ChangeValue("bar");
81 CPPUNIT_ASSERT_EQUAL( 0, handler
.GetEvents() );
83 entry
->AppendText("bar");
84 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetEvents() );
86 entry
->Replace(3, 6, "baz");
87 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetEvents() );
90 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetEvents() );
92 entry
->WriteText("foo");
93 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetEvents() );
96 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetEvents() );
99 void TextEntryTestCase::CheckStringSelection(const char *sel
)
101 CPPUNIT_ASSERT_EQUAL( sel
, GetTestEntry()->GetStringSelection() );
104 void TextEntryTestCase::AssertSelection(int from
, int to
, const char *sel
)
106 wxTextEntry
* const entry
= GetTestEntry();
108 CPPUNIT_ASSERT( entry
->HasSelection() );
112 entry
->GetSelection(&fromReal
, &toReal
);
113 CPPUNIT_ASSERT_EQUAL( from
, fromReal
);
114 CPPUNIT_ASSERT_EQUAL( to
, toReal
);
116 CPPUNIT_ASSERT_EQUAL( from
, entry
->GetInsertionPoint() );
118 CheckStringSelection(sel
);
121 void TextEntryTestCase::Selection()
123 wxTextEntry
* const entry
= GetTestEntry();
125 entry
->SetValue("0123456789");
127 entry
->SetSelection(2, 4);
128 AssertSelection(2, 4, "23"); // not "234"!
130 entry
->SetSelection(3, -1);
131 AssertSelection(3, 10, "3456789");
134 AssertSelection(0, 10, "0123456789");
136 entry
->SetSelection(0, 0);
137 CPPUNIT_ASSERT( !entry
->HasSelection() );
140 void TextEntryTestCase::InsertionPoint()
142 wxTextEntry
* const entry
= GetTestEntry();
144 CPPUNIT_ASSERT_EQUAL( 0, entry
->GetLastPosition() );
145 CPPUNIT_ASSERT_EQUAL( 0, entry
->GetInsertionPoint() );
147 entry
->SetValue("0"); // should put the insertion point in front
148 CPPUNIT_ASSERT_EQUAL( 1, entry
->GetLastPosition() );
149 CPPUNIT_ASSERT_EQUAL( 0, entry
->GetInsertionPoint() );
151 entry
->AppendText("12"); // should update the insertion point position
152 CPPUNIT_ASSERT_EQUAL( 3, entry
->GetLastPosition() );
153 CPPUNIT_ASSERT_EQUAL( 3, entry
->GetInsertionPoint() );
155 entry
->SetInsertionPoint(1);
156 CPPUNIT_ASSERT_EQUAL( 3, entry
->GetLastPosition() );
157 CPPUNIT_ASSERT_EQUAL( 1, entry
->GetInsertionPoint() );
159 entry
->SetInsertionPointEnd();
160 CPPUNIT_ASSERT_EQUAL( 3, entry
->GetInsertionPoint() );
162 entry
->SetInsertionPoint(0);
163 entry
->WriteText("-"); // should move it after the written text
164 CPPUNIT_ASSERT_EQUAL( 4, entry
->GetLastPosition() );
165 CPPUNIT_ASSERT_EQUAL( 1, entry
->GetInsertionPoint() );
168 void TextEntryTestCase::Replace()
170 wxTextEntry
* const entry
= GetTestEntry();
172 entry
->SetValue("Hello replace\n"
174 entry
->SetInsertionPoint(0);
176 entry
->Replace(6, 13, "changed");
178 CPPUNIT_ASSERT_EQUAL("Hello changed\n"
181 CPPUNIT_ASSERT_EQUAL(13, entry
->GetInsertionPoint());
183 entry
->Replace(13, -1, "");
184 CPPUNIT_ASSERT_EQUAL("Hello changed", entry
->GetValue());
185 CPPUNIT_ASSERT_EQUAL(13, entry
->GetInsertionPoint());
187 entry
->Replace(0, 6, "Un");
188 CPPUNIT_ASSERT_EQUAL("Unchanged", entry
->GetValue());
189 CPPUNIT_ASSERT_EQUAL(2, entry
->GetInsertionPoint());