]> git.saurik.com Git - wxWidgets.git/blob - tests/controls/textctrltest.cpp
04e5939a5ea53fe6346fa015e5e9e3e9330dcb67
[wxWidgets.git] / tests / controls / textctrltest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/textctrl/textctrltest.cpp
3 // Purpose: wxTextCtrl unit test
4 // Author: Vadim Zeitlin
5 // Created: 2007-09-25
6 // RCS-ID: $Id$
7 // Copyright: (c) 2007 Vadim Zeitlin <vadim@wxwidgets.org>
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "testprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #ifndef WX_PRECOMP
21 #include "wx/app.h"
22 #include "wx/textctrl.h"
23 #endif // WX_PRECOMP
24
25 // ----------------------------------------------------------------------------
26 // test class
27 // ----------------------------------------------------------------------------
28
29 class TextCtrlTestCase : public CppUnit::TestCase
30 {
31 public:
32 TextCtrlTestCase() { }
33
34 virtual void setUp();
35 virtual void tearDown();
36
37 private:
38 CPPUNIT_TEST_SUITE( TextCtrlTestCase );
39 CPPUNIT_TEST( SetValue );
40 CPPUNIT_TEST( TextChangeEvents );
41 CPPUNIT_TEST( Selection );
42 CPPUNIT_TEST_SUITE_END();
43
44 void SetValue();
45 void TextChangeEvents();
46 void Selection();
47
48 // Selection() test helper: verify that selection is as described by the
49 // function parameters
50 void AssertSelection(int from, int to, const char *sel);
51
52 wxTextCtrl *m_text;
53 wxFrame *m_frame;
54
55 DECLARE_NO_COPY_CLASS(TextCtrlTestCase)
56 };
57
58 // register in the unnamed registry so that these tests are run by default
59 CPPUNIT_TEST_SUITE_REGISTRATION( TextCtrlTestCase );
60
61 // also include in it's own registry so that these tests can be run alone
62 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TextCtrlTestCase, "TextCtrlTestCase" );
63
64 // ----------------------------------------------------------------------------
65 // test initialization
66 // ----------------------------------------------------------------------------
67
68 void TextCtrlTestCase::setUp()
69 {
70 m_text = new wxTextCtrl(wxTheApp->GetTopWindow(), wxID_ANY);
71 }
72
73 void TextCtrlTestCase::tearDown()
74 {
75 delete m_text;
76 m_text = NULL;
77 }
78
79 // ----------------------------------------------------------------------------
80 // tests themselves
81 // ----------------------------------------------------------------------------
82
83 void TextCtrlTestCase::SetValue()
84 {
85 CPPUNIT_ASSERT( m_text->IsEmpty() );
86
87 m_text->SetValue("foo");
88 CPPUNIT_ASSERT_EQUAL( "foo", m_text->GetValue() );
89
90 m_text->SetValue("");
91 CPPUNIT_ASSERT( m_text->IsEmpty() );
92
93 m_text->SetValue("hi");
94 CPPUNIT_ASSERT_EQUAL( "hi", m_text->GetValue() );
95
96 m_text->SetValue("bye");
97 CPPUNIT_ASSERT_EQUAL( "bye", m_text->GetValue() );
98 }
99
100 void TextCtrlTestCase::TextChangeEvents()
101 {
102 class TextTestEventHandler : public wxEvtHandler
103 {
104 public:
105 TextTestEventHandler() { m_events = 0; }
106
107 // calling this automatically resets the events counter
108 int GetEvents()
109 {
110 const int events = m_events;
111 m_events = 0;
112 return events;
113 }
114
115 void OnText(wxCommandEvent& WXUNUSED(event)) { m_events++; }
116
117 private:
118 int m_events;
119 } handler;
120
121 m_text->Connect(wxEVT_COMMAND_TEXT_UPDATED,
122 wxCommandEventHandler(TextTestEventHandler::OnText),
123 NULL,
124 &handler);
125
126 // notice that SetValue() generates an event even if the text didn't change
127 m_text->SetValue("");
128 CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
129
130 m_text->SetValue("foo");
131 CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
132
133 m_text->SetValue("foo");
134 CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
135
136 m_text->ChangeValue("bar");
137 CPPUNIT_ASSERT_EQUAL( 0, handler.GetEvents() );
138
139 m_text->AppendText("bar");
140 CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
141
142 m_text->Replace(3, 6, "baz");
143 CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
144
145 m_text->Remove(0, 3);
146 CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
147
148 m_text->WriteText("foo");
149 CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
150
151 m_text->Clear();
152 CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
153 }
154
155 void TextCtrlTestCase::AssertSelection(int from, int to, const char *sel)
156 {
157 CPPUNIT_ASSERT( m_text->HasSelection() );
158
159 long fromReal,
160 toReal;
161 m_text->GetSelection(&fromReal, &toReal);
162 CPPUNIT_ASSERT_EQUAL( from, fromReal );
163 CPPUNIT_ASSERT_EQUAL( to, toReal );
164 CPPUNIT_ASSERT_EQUAL( sel, m_text->GetStringSelection() );
165
166 CPPUNIT_ASSERT_EQUAL( from, m_text->GetInsertionPoint() );
167 }
168
169 void TextCtrlTestCase::Selection()
170 {
171 m_text->SetValue("0123456789");
172
173 m_text->SetSelection(2, 4);
174 AssertSelection(2, 4, "23"); // not "234"!
175
176 m_text->SetSelection(3, -1);
177 AssertSelection(3, 10, "3456789");
178
179 m_text->SelectAll();
180 AssertSelection(0, 10, "0123456789");
181
182 m_text->SetSelection(0, 0);
183 CPPUNIT_ASSERT( !m_text->HasSelection() );
184 }