]> git.saurik.com Git - wxWidgets.git/blob - tests/controls/textentrytest.cpp
Explicitly document event types for EVT_CLOSE().
[wxWidgets.git] / 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 // RCS-ID: $Id$
7 // Copyright: (c) 2007, 2008 Vadim Zeitlin <vadim@wxwidgets.org>
8 ///////////////////////////////////////////////////////////////////////////////
9
10 #include "testprec.h"
11
12 #ifndef WX_PRECOMP
13 #include "wx/event.h"
14 #include "wx/textentry.h"
15 #include "wx/window.h"
16 #endif // WX_PRECOMP
17
18 #include "textentrytest.h"
19
20 void TextEntryTestCase::SetValue()
21 {
22 wxTextEntry * const entry = GetTestEntry();
23
24 CPPUNIT_ASSERT( entry->IsEmpty() );
25
26 entry->SetValue("foo");
27 CPPUNIT_ASSERT_EQUAL( "foo", entry->GetValue() );
28
29 entry->SetValue("");
30 CPPUNIT_ASSERT( entry->IsEmpty() );
31
32 entry->SetValue("hi");
33 CPPUNIT_ASSERT_EQUAL( "hi", entry->GetValue() );
34
35 entry->SetValue("bye");
36 CPPUNIT_ASSERT_EQUAL( "bye", entry->GetValue() );
37 }
38
39 namespace
40 {
41 class TextTestEventHandler : public wxEvtHandler
42 {
43 public:
44 TextTestEventHandler() { m_events = 0; }
45
46 // calling this automatically resets the events counter
47 int GetEvents()
48 {
49 const int events = m_events;
50 m_events = 0;
51 return events;
52 }
53
54 void OnText(wxCommandEvent& WXUNUSED(event)) { m_events++; }
55
56 private:
57 int m_events;
58 };
59 }
60
61 void TextEntryTestCase::TextChangeEvents()
62 {
63 TextTestEventHandler handler;
64
65 GetTestWindow()->Connect
66 (
67 wxEVT_COMMAND_TEXT_UPDATED,
68 wxCommandEventHandler(TextTestEventHandler::OnText),
69 NULL,
70 &handler
71 );
72
73 wxTextEntry * const entry = GetTestEntry();
74
75 // notice that SetValue() generates an event even if the text didn't change
76 entry->SetValue("");
77 CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
78
79 entry->SetValue("foo");
80 CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
81
82 entry->SetValue("foo");
83 CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
84
85 entry->ChangeValue("bar");
86 CPPUNIT_ASSERT_EQUAL( 0, handler.GetEvents() );
87
88 entry->AppendText("bar");
89 CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
90
91 entry->Replace(3, 6, "baz");
92 CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
93
94 entry->Remove(0, 3);
95 CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
96
97 entry->WriteText("foo");
98 CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
99
100 entry->Clear();
101 CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
102 }
103
104 void TextEntryTestCase::CheckStringSelection(const char *sel)
105 {
106 CPPUNIT_ASSERT_EQUAL( sel, GetTestEntry()->GetStringSelection() );
107 }
108
109 void TextEntryTestCase::AssertSelection(int from, int to, const char *sel)
110 {
111 wxTextEntry * const entry = GetTestEntry();
112
113 CPPUNIT_ASSERT( entry->HasSelection() );
114
115 long fromReal,
116 toReal;
117 entry->GetSelection(&fromReal, &toReal);
118 CPPUNIT_ASSERT_EQUAL( from, fromReal );
119 CPPUNIT_ASSERT_EQUAL( to, toReal );
120
121 CPPUNIT_ASSERT_EQUAL( from, entry->GetInsertionPoint() );
122
123 CheckStringSelection(sel);
124 }
125
126 void TextEntryTestCase::Selection()
127 {
128 wxTextEntry * const entry = GetTestEntry();
129
130 entry->SetValue("0123456789");
131
132 entry->SetSelection(2, 4);
133 AssertSelection(2, 4, "23"); // not "234"!
134
135 entry->SetSelection(3, -1);
136 AssertSelection(3, 10, "3456789");
137
138 entry->SelectAll();
139 AssertSelection(0, 10, "0123456789");
140
141 entry->SetSelection(0, 0);
142 CPPUNIT_ASSERT( !entry->HasSelection() );
143 }
144
145 void TextEntryTestCase::InsertionPoint()
146 {
147 wxTextEntry * const entry = GetTestEntry();
148
149 CPPUNIT_ASSERT_EQUAL( 0, entry->GetLastPosition() );
150 CPPUNIT_ASSERT_EQUAL( 0, entry->GetInsertionPoint() );
151
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() );
155
156 entry->AppendText("12"); // should update the insertion point position
157 CPPUNIT_ASSERT_EQUAL( 3, entry->GetLastPosition() );
158 CPPUNIT_ASSERT_EQUAL( 3, entry->GetInsertionPoint() );
159
160 entry->SetInsertionPoint(1);
161 CPPUNIT_ASSERT_EQUAL( 3, entry->GetLastPosition() );
162 CPPUNIT_ASSERT_EQUAL( 1, entry->GetInsertionPoint() );
163
164 entry->SetInsertionPointEnd();
165 CPPUNIT_ASSERT_EQUAL( 3, entry->GetInsertionPoint() );
166
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() );
171 }
172
173 void TextEntryTestCase::Replace()
174 {
175 wxTextEntry * const entry = GetTestEntry();
176
177 entry->SetValue("Hello replace!"
178 "0123456789012");
179 entry->SetInsertionPoint(0);
180
181 entry->Replace(6, 13, "changed");
182
183 CPPUNIT_ASSERT_EQUAL("Hello changed!"
184 "0123456789012",
185 entry->GetValue());
186 CPPUNIT_ASSERT_EQUAL(13, entry->GetInsertionPoint());
187
188 entry->Replace(13, -1, "");
189 CPPUNIT_ASSERT_EQUAL("Hello changed", entry->GetValue());
190 CPPUNIT_ASSERT_EQUAL(13, entry->GetInsertionPoint());
191
192 entry->Replace(0, 6, "Un");
193 CPPUNIT_ASSERT_EQUAL("Unchanged", entry->GetValue());
194 CPPUNIT_ASSERT_EQUAL(2, entry->GetInsertionPoint());
195 }
196