]> git.saurik.com Git - wxWidgets.git/blob - tests/controls/textentrytest.cpp
rewrite wxContractPath as wxFileName::ReplaceEnvVariable and wxFileName::ReplaceHomeD...
[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 void TextEntryTestCase::TextChangeEvents()
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 } handler;
59
60 GetTestWindow()->Connect
61 (
62 wxEVT_COMMAND_TEXT_UPDATED,
63 wxCommandEventHandler(TextTestEventHandler::OnText),
64 NULL,
65 &handler
66 );
67
68 wxTextEntry * const entry = GetTestEntry();
69
70 // notice that SetValue() generates an event even if the text didn't change
71 entry->SetValue("");
72 CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
73
74 entry->SetValue("foo");
75 CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
76
77 entry->SetValue("foo");
78 CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
79
80 entry->ChangeValue("bar");
81 CPPUNIT_ASSERT_EQUAL( 0, handler.GetEvents() );
82
83 entry->AppendText("bar");
84 CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
85
86 entry->Replace(3, 6, "baz");
87 CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
88
89 entry->Remove(0, 3);
90 CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
91
92 entry->WriteText("foo");
93 CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
94
95 entry->Clear();
96 CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
97 }
98
99 void TextEntryTestCase::CheckStringSelection(const char *sel)
100 {
101 CPPUNIT_ASSERT_EQUAL( sel, GetTestEntry()->GetStringSelection() );
102 }
103
104 void TextEntryTestCase::AssertSelection(int from, int to, const char *sel)
105 {
106 wxTextEntry * const entry = GetTestEntry();
107
108 CPPUNIT_ASSERT( entry->HasSelection() );
109
110 long fromReal,
111 toReal;
112 entry->GetSelection(&fromReal, &toReal);
113 CPPUNIT_ASSERT_EQUAL( from, fromReal );
114 CPPUNIT_ASSERT_EQUAL( to, toReal );
115
116 CPPUNIT_ASSERT_EQUAL( from, entry->GetInsertionPoint() );
117
118 CheckStringSelection(sel);
119 }
120
121 void TextEntryTestCase::Selection()
122 {
123 wxTextEntry * const entry = GetTestEntry();
124
125 entry->SetValue("0123456789");
126
127 entry->SetSelection(2, 4);
128 AssertSelection(2, 4, "23"); // not "234"!
129
130 entry->SetSelection(3, -1);
131 AssertSelection(3, 10, "3456789");
132
133 entry->SelectAll();
134 AssertSelection(0, 10, "0123456789");
135
136 entry->SetSelection(0, 0);
137 CPPUNIT_ASSERT( !entry->HasSelection() );
138 }
139
140 void TextEntryTestCase::InsertionPoint()
141 {
142 wxTextEntry * const entry = GetTestEntry();
143
144 CPPUNIT_ASSERT_EQUAL( 0, entry->GetLastPosition() );
145 CPPUNIT_ASSERT_EQUAL( 0, entry->GetInsertionPoint() );
146
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() );
150
151 entry->AppendText("12"); // should update the insertion point position
152 CPPUNIT_ASSERT_EQUAL( 3, entry->GetLastPosition() );
153 CPPUNIT_ASSERT_EQUAL( 3, entry->GetInsertionPoint() );
154
155 entry->SetInsertionPoint(1);
156 CPPUNIT_ASSERT_EQUAL( 3, entry->GetLastPosition() );
157 CPPUNIT_ASSERT_EQUAL( 1, entry->GetInsertionPoint() );
158
159 entry->SetInsertionPointEnd();
160 CPPUNIT_ASSERT_EQUAL( 3, entry->GetInsertionPoint() );
161
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() );
166 }
167
168 void TextEntryTestCase::Replace()
169 {
170 wxTextEntry * const entry = GetTestEntry();
171
172 entry->SetValue("Hello replace\n"
173 "0123456789012");
174 entry->SetInsertionPoint(0);
175
176 entry->Replace(6, 13, "changed");
177
178 CPPUNIT_ASSERT_EQUAL("Hello changed\n"
179 "0123456789012",
180 entry->GetValue());
181 CPPUNIT_ASSERT_EQUAL(13, entry->GetInsertionPoint());
182
183 entry->Replace(13, -1, "");
184 CPPUNIT_ASSERT_EQUAL("Hello changed", entry->GetValue());
185 CPPUNIT_ASSERT_EQUAL(13, entry->GetInsertionPoint());
186
187 entry->Replace(0, 6, "Un");
188 CPPUNIT_ASSERT_EQUAL("Unchanged", entry->GetValue());
189 CPPUNIT_ASSERT_EQUAL(2, entry->GetInsertionPoint());
190 }
191