]> git.saurik.com Git - wxWidgets.git/blob - tests/controls/textentrytest.cpp
extract wxTextEntry unit tests in a reusable base class
[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::AssertSelection(int from, int to, const char *sel)
100 {
101 wxTextEntry * const entry = GetTestEntry();
102
103 CPPUNIT_ASSERT( entry->HasSelection() );
104
105 long fromReal,
106 toReal;
107 entry->GetSelection(&fromReal, &toReal);
108 CPPUNIT_ASSERT_EQUAL( from, fromReal );
109 CPPUNIT_ASSERT_EQUAL( to, toReal );
110 CPPUNIT_ASSERT_EQUAL( sel, entry->GetStringSelection() );
111
112 CPPUNIT_ASSERT_EQUAL( from, entry->GetInsertionPoint() );
113 }
114
115 void TextEntryTestCase::Selection()
116 {
117 wxTextEntry * const entry = GetTestEntry();
118
119 entry->SetValue("0123456789");
120
121 entry->SetSelection(2, 4);
122 AssertSelection(2, 4, "23"); // not "234"!
123
124 entry->SetSelection(3, -1);
125 AssertSelection(3, 10, "3456789");
126
127 entry->SelectAll();
128 AssertSelection(0, 10, "0123456789");
129
130 entry->SetSelection(0, 0);
131 CPPUNIT_ASSERT( !entry->HasSelection() );
132 }
133
134 void TextEntryTestCase::InsertionPoint()
135 {
136 wxTextEntry * const entry = GetTestEntry();
137
138 CPPUNIT_ASSERT_EQUAL( 0, entry->GetLastPosition() );
139 CPPUNIT_ASSERT_EQUAL( 0, entry->GetInsertionPoint() );
140
141 entry->SetValue("0"); // should put the insertion point in front
142 CPPUNIT_ASSERT_EQUAL( 1, entry->GetLastPosition() );
143 CPPUNIT_ASSERT_EQUAL( 0, entry->GetInsertionPoint() );
144
145 entry->AppendText("12"); // should update the insertion point position
146 CPPUNIT_ASSERT_EQUAL( 3, entry->GetLastPosition() );
147 CPPUNIT_ASSERT_EQUAL( 3, entry->GetInsertionPoint() );
148
149 entry->SetInsertionPoint(1);
150 CPPUNIT_ASSERT_EQUAL( 3, entry->GetLastPosition() );
151 CPPUNIT_ASSERT_EQUAL( 1, entry->GetInsertionPoint() );
152
153 entry->SetInsertionPointEnd();
154 CPPUNIT_ASSERT_EQUAL( 3, entry->GetInsertionPoint() );
155
156 entry->SetInsertionPoint(0);
157 entry->WriteText("-"); // should move it after the written text
158 CPPUNIT_ASSERT_EQUAL( 4, entry->GetLastPosition() );
159 CPPUNIT_ASSERT_EQUAL( 1, entry->GetInsertionPoint() );
160 }
161