]>
Commit | Line | Data |
---|---|---|
232fdc63 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: tests/controls/listbox.cpp | |
3 | // Purpose: wxListBox unit test | |
4 | // Author: Steven Lamerton | |
5 | // Created: 2010-06-29 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2010 Steven Lamerton | |
8 | /////////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | #include "testprec.h" | |
11 | ||
12 | #if wxUSE_LISTBOX | |
13 | ||
14 | #ifdef __BORLANDC__ | |
15 | #pragma hdrstop | |
16 | #endif | |
17 | ||
18 | #ifndef WX_PRECOMP | |
19 | #include "wx/app.h" | |
20 | #include "wx/listbox.h" | |
21 | #endif // WX_PRECOMP | |
22 | ||
23 | #include "itemcontainertest.h" | |
24 | #include "testableframe.h" | |
25 | #include "wx/uiaction.h" | |
26 | ||
27 | class ListBoxTestCase : public ItemContainerTestCase, public CppUnit::TestCase | |
28 | { | |
29 | public: | |
30 | ListBoxTestCase() { } | |
31 | ||
32 | virtual void setUp(); | |
33 | virtual void tearDown(); | |
34 | ||
35 | private: | |
36 | virtual wxItemContainer *GetContainer() const { return m_list; } | |
37 | virtual wxWindow *GetContainerWindow() const { return m_list; } | |
38 | ||
39 | CPPUNIT_TEST_SUITE( ListBoxTestCase ); | |
40 | wxITEM_CONTAINER_TESTS(); | |
41 | CPPUNIT_TEST( Sort ); | |
42 | CPPUNIT_TEST( MultipleSelect ); | |
43 | WXUISIM_TEST( ClickEvents ); | |
44 | WXUISIM_TEST( ClickNotOnItem ); | |
45 | CPPUNIT_TEST( HitTest ); | |
48d13339 | 46 | //We also run all tests as an ownerdrawn list box. We do not need to |
232fdc63 VZ |
47 | //run the wxITEM_CONTAINER_TESTS as they are tested with wxCheckListBox |
48 | #ifdef __WXMSW__ | |
49 | CPPUNIT_TEST( PseudoTest_OwnerDrawn ); | |
48d13339 | 50 | CPPUNIT_TEST( Sort ); |
232fdc63 VZ |
51 | CPPUNIT_TEST( MultipleSelect ); |
52 | WXUISIM_TEST( ClickEvents ); | |
53 | WXUISIM_TEST( ClickNotOnItem ); | |
54 | CPPUNIT_TEST( HitTest ); | |
55 | #endif | |
56 | CPPUNIT_TEST_SUITE_END(); | |
57 | ||
58 | void Sort(); | |
59 | void MultipleSelect(); | |
60 | void ClickEvents(); | |
61 | void ClickNotOnItem(); | |
62 | void HitTest(); | |
63 | void PseudoTest_OwnerDrawn() { ms_ownerdrawn = true; } | |
64 | ||
65 | static bool ms_ownerdrawn; | |
66 | ||
67 | wxListBox* m_list; | |
68 | ||
69 | DECLARE_NO_COPY_CLASS(ListBoxTestCase) | |
70 | }; | |
71 | ||
72 | // register in the unnamed registry so that these tests are run by default | |
73 | CPPUNIT_TEST_SUITE_REGISTRATION( ListBoxTestCase ); | |
74 | ||
75 | // also include in it's own registry so that these tests can be run alone | |
76 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ListBoxTestCase, "ListBoxTestCase" ); | |
77 | ||
78 | //initialise the static variable | |
79 | bool ListBoxTestCase::ms_ownerdrawn = false; | |
80 | ||
81 | void ListBoxTestCase::setUp() | |
82 | { | |
83 | if( ms_ownerdrawn ) | |
84 | { | |
85 | m_list = new wxListBox(wxTheApp->GetTopWindow(), wxID_ANY, | |
86 | wxDefaultPosition, wxSize(300, 200), 0, NULL, | |
87 | wxLB_OWNERDRAW); | |
88 | } | |
89 | else | |
90 | { | |
91 | m_list = new wxListBox(wxTheApp->GetTopWindow(), wxID_ANY, | |
92 | wxDefaultPosition, wxSize(300, 200)); | |
93 | } | |
94 | } | |
95 | ||
96 | void ListBoxTestCase::tearDown() | |
97 | { | |
98 | wxDELETE(m_list); | |
99 | } | |
100 | ||
101 | void ListBoxTestCase::Sort() | |
102 | { | |
6e269b25 | 103 | #ifndef __WXOSX__ |
232fdc63 VZ |
104 | wxDELETE(m_list); |
105 | m_list = new wxListBox(wxTheApp->GetTopWindow(), wxID_ANY, | |
106 | wxDefaultPosition, wxDefaultSize, 0, 0, | |
107 | wxLB_SORT); | |
108 | ||
109 | wxArrayString testitems; | |
110 | testitems.Add("aaa"); | |
111 | testitems.Add("Aaa"); | |
112 | testitems.Add("aba"); | |
113 | testitems.Add("aaab"); | |
114 | testitems.Add("aab"); | |
115 | testitems.Add("AAA"); | |
116 | ||
117 | m_list->Append(testitems); | |
118 | ||
119 | CPPUNIT_ASSERT_EQUAL("AAA", m_list->GetString(0)); | |
120 | CPPUNIT_ASSERT_EQUAL("Aaa", m_list->GetString(1)); | |
121 | CPPUNIT_ASSERT_EQUAL("aaa", m_list->GetString(2)); | |
122 | CPPUNIT_ASSERT_EQUAL("aaab", m_list->GetString(3)); | |
123 | CPPUNIT_ASSERT_EQUAL("aab", m_list->GetString(4)); | |
124 | CPPUNIT_ASSERT_EQUAL("aba", m_list->GetString(5)); | |
125 | ||
126 | m_list->Append("a"); | |
127 | ||
128 | CPPUNIT_ASSERT_EQUAL("a", m_list->GetString(0)); | |
129 | #endif | |
130 | } | |
131 | ||
132 | void ListBoxTestCase::MultipleSelect() | |
133 | { | |
134 | wxDELETE(m_list); | |
135 | m_list = new wxListBox(wxTheApp->GetTopWindow(), wxID_ANY, | |
136 | wxDefaultPosition, wxDefaultSize, 0, 0, | |
137 | wxLB_MULTIPLE); | |
138 | ||
139 | wxArrayString testitems; | |
140 | testitems.Add("item 0"); | |
141 | testitems.Add("item 1"); | |
142 | testitems.Add("item 2"); | |
143 | testitems.Add("item 3"); | |
144 | ||
145 | m_list->Append(testitems); | |
146 | ||
147 | m_list->SetSelection(0); | |
148 | ||
149 | wxArrayInt selected; | |
150 | m_list->GetSelections(selected); | |
151 | ||
152 | CPPUNIT_ASSERT_EQUAL(1, selected.Count()); | |
153 | CPPUNIT_ASSERT_EQUAL(0, selected.Item(0)); | |
154 | ||
155 | m_list->SetSelection(2); | |
156 | ||
157 | m_list->GetSelections(selected); | |
158 | ||
159 | CPPUNIT_ASSERT_EQUAL(2, selected.Count()); | |
160 | CPPUNIT_ASSERT_EQUAL(2, selected.Item(1)); | |
161 | ||
162 | m_list->Deselect(0); | |
163 | ||
164 | m_list->GetSelections(selected); | |
165 | ||
166 | CPPUNIT_ASSERT_EQUAL(1, selected.Count()); | |
167 | CPPUNIT_ASSERT_EQUAL(2, selected.Item(0)); | |
168 | ||
169 | CPPUNIT_ASSERT(!m_list->IsSelected(0)); | |
170 | CPPUNIT_ASSERT(!m_list->IsSelected(1)); | |
171 | CPPUNIT_ASSERT(m_list->IsSelected(2)); | |
172 | CPPUNIT_ASSERT(!m_list->IsSelected(3)); | |
173 | } | |
174 | ||
175 | void ListBoxTestCase::ClickEvents() | |
176 | { | |
177 | #if wxUSE_UIACTIONSIMULATOR | |
178 | wxTestableFrame* frame = wxStaticCast(wxTheApp->GetTopWindow(), | |
179 | wxTestableFrame); | |
180 | ||
181 | EventCounter count(frame, wxEVT_COMMAND_LISTBOX_SELECTED); | |
182 | EventCounter count1(frame, wxEVT_COMMAND_LISTBOX_DOUBLECLICKED); | |
183 | ||
184 | wxUIActionSimulator sim; | |
185 | ||
186 | wxArrayString testitems; | |
187 | testitems.Add("item 0"); | |
188 | testitems.Add("item 1"); | |
189 | testitems.Add("item 2"); | |
190 | ||
191 | m_list->Append(testitems); | |
192 | ||
193 | m_list->Update(); | |
194 | m_list->Refresh(); | |
195 | ||
196 | sim.MouseMove(m_list->ClientToScreen(wxPoint(10, 10))); | |
197 | wxYield(); | |
198 | ||
199 | sim.MouseClick(); | |
200 | wxYield(); | |
201 | ||
202 | CPPUNIT_ASSERT_EQUAL(1, frame->GetEventCount()); | |
203 | ||
204 | sim.MouseDblClick(); | |
205 | wxYield(); | |
206 | ||
207 | CPPUNIT_ASSERT_EQUAL(1, frame->GetEventCount()); | |
208 | #endif | |
209 | } | |
210 | ||
211 | void ListBoxTestCase::ClickNotOnItem() | |
212 | { | |
213 | #if wxUSE_UIACTIONSIMULATOR | |
214 | wxTestableFrame* frame = wxStaticCast(wxTheApp->GetTopWindow(), | |
215 | wxTestableFrame); | |
216 | ||
217 | EventCounter count(frame, wxEVT_COMMAND_LISTBOX_SELECTED); | |
218 | EventCounter count1(frame, wxEVT_COMMAND_LISTBOX_DOUBLECLICKED); | |
219 | ||
220 | wxUIActionSimulator sim; | |
221 | ||
222 | wxArrayString testitems; | |
223 | testitems.Add("item 0"); | |
224 | testitems.Add("item 1"); | |
225 | testitems.Add("item 2"); | |
226 | ||
227 | m_list->Append(testitems); | |
228 | ||
229 | m_list->Update(); | |
230 | m_list->Refresh(); | |
231 | ||
232 | sim.MouseMove(m_list->ClientToScreen(wxPoint(m_list->GetSize().x - 10, m_list->GetSize().y - 10))); | |
233 | wxYield(); | |
234 | ||
235 | sim.MouseClick(); | |
236 | wxYield(); | |
237 | ||
238 | sim.MouseDblClick(); | |
239 | wxYield(); | |
240 | ||
241 | //If we are not clicking on an item we shouldn't have any events | |
242 | CPPUNIT_ASSERT_EQUAL(0, frame->GetEventCount()); | |
243 | #endif | |
244 | } | |
245 | ||
246 | void ListBoxTestCase::HitTest() | |
247 | { | |
232fdc63 VZ |
248 | wxArrayString testitems; |
249 | testitems.Add("item 0"); | |
250 | testitems.Add("item 1"); | |
251 | testitems.Add("item 2"); | |
252 | ||
253 | m_list->Append(testitems); | |
254 | ||
3b5c9663 VZ |
255 | #ifdef __WXGTK__ |
256 | // The control needs to be realized for HitTest() to work. | |
257 | wxYield(); | |
232fdc63 | 258 | #endif |
3b5c9663 VZ |
259 | |
260 | CPPUNIT_ASSERT_EQUAL( 0, m_list->HitTest(5, 5) ); | |
261 | ||
262 | CPPUNIT_ASSERT_EQUAL( wxNOT_FOUND, m_list->HitTest(290, 190) ); | |
232fdc63 VZ |
263 | } |
264 | ||
265 | #endif //wxUSE_LISTBOX |