]>
Commit | Line | Data |
---|---|---|
232fdc63 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: tests/controls/listbasetest.cpp | |
3 | // Purpose: Base class for wxListCtrl and wxListView tests | |
4 | // Author: Steven Lamerton | |
5 | // Created: 2010-07-20 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>, | |
8 | // (c) 2010 Steven Lamerton | |
9 | /////////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | #include "testprec.h" | |
12 | ||
13 | #if wxUSE_LISTCTRL | |
14 | ||
15 | #ifdef __BORLANDC__ | |
16 | #pragma hdrstop | |
17 | #endif | |
18 | ||
19 | #ifndef WX_PRECOMP | |
20 | #include "wx/app.h" | |
21 | #endif // WX_PRECOMP | |
22 | ||
23 | #include "wx/listctrl.h" | |
24 | #include "listbasetest.h" | |
25 | #include "testableframe.h" | |
26 | #include "asserthelper.h" | |
27 | #include "wx/uiaction.h" | |
28 | #include "wx/imaglist.h" | |
29 | #include "wx/artprov.h" | |
30 | ||
31 | void ListBaseTestCase::ColumnsOrder() | |
32 | { | |
33 | #ifdef wxHAS_LISTCTRL_COLUMN_ORDER | |
34 | wxListCtrl* const list = GetList(); | |
35 | ||
36 | int n; | |
37 | wxListItem li; | |
38 | li.SetMask(wxLIST_MASK_TEXT); | |
39 | ||
40 | // first set up some columns | |
41 | static const int NUM_COLS = 3; | |
42 | ||
43 | list->InsertColumn(0, "Column 0"); | |
44 | list->InsertColumn(1, "Column 1"); | |
45 | list->InsertColumn(2, "Column 2"); | |
46 | ||
47 | // and a couple of test items too | |
48 | list->InsertItem(0, "Item 0"); | |
49 | list->SetItem(0, 1, "first in first"); | |
50 | ||
51 | list->InsertItem(1, "Item 1"); | |
52 | list->SetItem(1, 2, "second in second"); | |
53 | ||
54 | ||
55 | // check that the order is natural in the beginning | |
56 | const wxArrayInt orderOrig = list->GetColumnsOrder(); | |
57 | for ( n = 0; n < NUM_COLS; n++ ) | |
58 | CPPUNIT_ASSERT_EQUAL( n, orderOrig[n] ); | |
59 | ||
60 | // then rearrange them: using { 2, 0, 1 } order means that column 2 is | |
61 | // shown first, then column 0 and finally column 1 | |
62 | wxArrayInt order(3); | |
63 | order[0] = 2; | |
64 | order[1] = 0; | |
65 | order[2] = 1; | |
66 | list->SetColumnsOrder(order); | |
67 | ||
68 | // check that we get back the same order as we set | |
69 | const wxArrayInt orderNew = list->GetColumnsOrder(); | |
70 | for ( n = 0; n < NUM_COLS; n++ ) | |
71 | CPPUNIT_ASSERT_EQUAL( order[n], orderNew[n] ); | |
72 | ||
73 | // and the order -> index mappings for individual columns | |
74 | for ( n = 0; n < NUM_COLS; n++ ) | |
75 | CPPUNIT_ASSERT_EQUAL( order[n], list->GetColumnIndexFromOrder(n) ); | |
76 | ||
77 | // and also the reverse mapping | |
78 | CPPUNIT_ASSERT_EQUAL( 1, list->GetColumnOrder(0) ); | |
79 | CPPUNIT_ASSERT_EQUAL( 2, list->GetColumnOrder(1) ); | |
80 | CPPUNIT_ASSERT_EQUAL( 0, list->GetColumnOrder(2) ); | |
81 | ||
82 | ||
83 | // finally check that accessors still use indices, not order | |
84 | CPPUNIT_ASSERT( list->GetColumn(0, li) ); | |
85 | CPPUNIT_ASSERT_EQUAL( "Column 0", li.GetText() ); | |
86 | ||
87 | li.SetId(0); | |
88 | li.SetColumn(1); | |
89 | CPPUNIT_ASSERT( list->GetItem(li) ); | |
90 | CPPUNIT_ASSERT_EQUAL( "first in first", li.GetText() ); | |
91 | ||
92 | li.SetId(1); | |
93 | li.SetColumn(2); | |
94 | CPPUNIT_ASSERT( list->GetItem(li) ); | |
95 | CPPUNIT_ASSERT_EQUAL( "second in second", li.GetText() ); | |
96 | ||
97 | //tidy up when we are finished | |
98 | list->ClearAll(); | |
99 | #endif // wxHAS_LISTCTRL_COLUMN_ORDER | |
100 | } | |
101 | ||
102 | ||
103 | ||
104 | void ListBaseTestCase::ItemRect() | |
105 | { | |
106 | wxListCtrl* const list = GetList(); | |
107 | ||
108 | // set up for the test | |
109 | list->InsertColumn(0, "Column 0", wxLIST_FORMAT_LEFT, 60); | |
110 | list->InsertColumn(1, "Column 1", wxLIST_FORMAT_LEFT, 50); | |
111 | list->InsertColumn(2, "Column 2", wxLIST_FORMAT_LEFT, 40); | |
112 | ||
113 | list->InsertItem(0, "Item 0"); | |
114 | list->SetItem(0, 1, "first column"); | |
115 | list->SetItem(0, 1, "second column"); | |
116 | ||
117 | // do test | |
118 | wxRect r; | |
119 | WX_ASSERT_FAILS_WITH_ASSERT( list->GetItemRect(1, r) ); | |
120 | CPPUNIT_ASSERT( list->GetItemRect(0, r) ); | |
121 | CPPUNIT_ASSERT_EQUAL( 150, r.GetWidth() ); | |
122 | ||
123 | CPPUNIT_ASSERT( list->GetSubItemRect(0, 0, r) ); | |
124 | CPPUNIT_ASSERT_EQUAL( 60, r.GetWidth() ); | |
125 | ||
126 | CPPUNIT_ASSERT( list->GetSubItemRect(0, 1, r) ); | |
127 | CPPUNIT_ASSERT_EQUAL( 50, r.GetWidth() ); | |
128 | ||
129 | CPPUNIT_ASSERT( list->GetSubItemRect(0, 2, r) ); | |
130 | CPPUNIT_ASSERT_EQUAL( 40, r.GetWidth() ); | |
131 | ||
132 | WX_ASSERT_FAILS_WITH_ASSERT( list->GetSubItemRect(0, 3, r) ); | |
133 | ||
134 | //tidy up when we are finished | |
135 | list->ClearAll(); | |
136 | } | |
137 | ||
138 | void ListBaseTestCase::ItemText() | |
139 | { | |
140 | wxListCtrl* const list = GetList(); | |
141 | ||
142 | list->InsertColumn(0, "First"); | |
143 | list->InsertColumn(1, "Second"); | |
144 | ||
145 | list->InsertItem(0, "0,0"); | |
146 | CPPUNIT_ASSERT_EQUAL( "0,0", list->GetItemText(0) ); | |
147 | CPPUNIT_ASSERT_EQUAL( "", list->GetItemText(0, 1) ); | |
148 | ||
149 | list->SetItem(0, 1, "0,1"); | |
150 | CPPUNIT_ASSERT_EQUAL( "0,1", list->GetItemText(0, 1) ); | |
151 | } | |
152 | ||
153 | void ListBaseTestCase::ChangeMode() | |
154 | { | |
155 | wxListCtrl* const list = GetList(); | |
156 | ||
157 | list->InsertColumn(0, "Header"); | |
158 | list->InsertItem(0, "First"); | |
159 | list->InsertItem(1, "Second"); | |
160 | CPPUNIT_ASSERT_EQUAL( 2, list->GetItemCount() ); | |
161 | ||
162 | // check that switching the mode preserves the items | |
163 | list->SetWindowStyle(wxLC_ICON); | |
164 | CPPUNIT_ASSERT_EQUAL( 2, list->GetItemCount() ); | |
165 | CPPUNIT_ASSERT_EQUAL( "First", list->GetItemText(0) ); | |
166 | ||
167 | // and so does switching back | |
168 | list->SetWindowStyle(wxLC_REPORT); | |
169 | CPPUNIT_ASSERT_EQUAL( 2, list->GetItemCount() ); | |
170 | CPPUNIT_ASSERT_EQUAL( "First", list->GetItemText(0) ); | |
171 | ||
172 | //tidy up when we are finished | |
173 | list->ClearAll(); | |
174 | } | |
175 | ||
176 | void ListBaseTestCase::ItemClick() | |
177 | { | |
689c6ca6 | 178 | #if wxUSE_UIACTIONSIMULATOR |
e2032c2e | 179 | |
f1287154 | 180 | #ifdef __WXMSW__ |
e2032c2e VZ |
181 | // FIXME: This test fails on MSW buildbot slaves although works fine on |
182 | // development machine, no idea why. It seems to be a problem with | |
183 | // wxUIActionSimulator rather the wxListCtrl control itself however. | |
f1287154 | 184 | if ( IsAutomaticTest() ) |
e2032c2e | 185 | return; |
f1287154 | 186 | #endif // __WXMSW__ |
e2032c2e | 187 | |
232fdc63 VZ |
188 | wxListCtrl* const list = GetList(); |
189 | ||
232fdc63 VZ |
190 | list->InsertColumn(0, "Column 0", wxLIST_FORMAT_LEFT, 60); |
191 | list->InsertColumn(1, "Column 1", wxLIST_FORMAT_LEFT, 50); | |
192 | list->InsertColumn(2, "Column 2", wxLIST_FORMAT_LEFT, 40); | |
193 | ||
194 | list->InsertItem(0, "Item 0"); | |
195 | list->SetItem(0, 1, "first column"); | |
196 | list->SetItem(0, 2, "second column"); | |
197 | ||
ce7fe42e VZ |
198 | EventCounter selected(list, wxEVT_LIST_ITEM_SELECTED); |
199 | EventCounter focused(list, wxEVT_LIST_ITEM_FOCUSED); | |
200 | EventCounter activated(list, wxEVT_LIST_ITEM_ACTIVATED); | |
201 | EventCounter rclick(list, wxEVT_LIST_ITEM_RIGHT_CLICK); | |
1cef35ca | 202 | |
232fdc63 VZ |
203 | wxUIActionSimulator sim; |
204 | ||
205 | wxRect pos; | |
206 | list->GetItemRect(0, pos); | |
207 | ||
208 | //We move in slightly so we are not on the edge | |
689c6ca6 | 209 | wxPoint point = list->ClientToScreen(pos.GetPosition()) + wxPoint(10, 5); |
232fdc63 VZ |
210 | |
211 | sim.MouseMove(point); | |
212 | wxYield(); | |
213 | ||
214 | sim.MouseClick(); | |
215 | wxYield(); | |
216 | ||
217 | sim.MouseDblClick(); | |
218 | wxYield(); | |
219 | ||
220 | sim.MouseClick(wxMOUSE_BTN_RIGHT); | |
221 | wxYield(); | |
222 | ||
223 | // when the first item was selected the focus changes to it, but not | |
224 | // on subsequent clicks | |
689c6ca6 SC |
225 | |
226 | // FIXME: This test fail under wxGTK & wxOSX because we get 3 FOCUSED events and | |
227 | // 2 SELECTED ones instead of the one of each we expect for some | |
228 | // reason, this needs to be debugged as it may indicate a bug in the | |
229 | // generic wxListCtrl implementation. | |
230 | #ifndef _WX_GENERIC_LISTCTRL_H_ | |
744d91d4 SL |
231 | CPPUNIT_ASSERT_EQUAL(1, focused.GetCount()); |
232 | CPPUNIT_ASSERT_EQUAL(1, selected.GetCount()); | |
689c6ca6 | 233 | #endif |
744d91d4 SL |
234 | CPPUNIT_ASSERT_EQUAL(1, activated.GetCount()); |
235 | CPPUNIT_ASSERT_EQUAL(1, rclick.GetCount()); | |
232fdc63 VZ |
236 | |
237 | //tidy up when we are finished | |
238 | list->ClearAll(); | |
239 | #endif // wxUSE_UIACTIONSIMULATOR | |
240 | } | |
241 | ||
242 | void ListBaseTestCase::KeyDown() | |
243 | { | |
92d9d10f | 244 | #if wxUSE_UIACTIONSIMULATOR |
232fdc63 VZ |
245 | wxListCtrl* const list = GetList(); |
246 | ||
ce7fe42e | 247 | EventCounter keydown(list, wxEVT_LIST_KEY_DOWN); |
232fdc63 VZ |
248 | |
249 | wxUIActionSimulator sim; | |
250 | ||
251 | list->SetFocus(); | |
232fdc63 VZ |
252 | sim.Text("aAbB"); |
253 | wxYield(); | |
254 | ||
744d91d4 | 255 | CPPUNIT_ASSERT_EQUAL(6, keydown.GetCount()); |
232fdc63 VZ |
256 | #endif |
257 | } | |
258 | ||
259 | void ListBaseTestCase::DeleteItems() | |
260 | { | |
261 | #ifndef __WXOSX__ | |
232fdc63 VZ |
262 | wxListCtrl* const list = GetList(); |
263 | ||
ce7fe42e VZ |
264 | EventCounter deleteitem(list, wxEVT_LIST_DELETE_ITEM); |
265 | EventCounter deleteall(list, wxEVT_LIST_DELETE_ALL_ITEMS); | |
232fdc63 VZ |
266 | |
267 | ||
268 | list->InsertColumn(0, "Column 0", wxLIST_FORMAT_LEFT, 60); | |
269 | list->InsertColumn(1, "Column 1", wxLIST_FORMAT_LEFT, 50); | |
270 | list->InsertColumn(2, "Column 2", wxLIST_FORMAT_LEFT, 40); | |
271 | ||
272 | list->InsertItem(0, "Item 0"); | |
273 | list->InsertItem(1, "Item 1"); | |
274 | list->InsertItem(2, "Item 1"); | |
275 | ||
276 | list->DeleteItem(0); | |
277 | list->DeleteItem(0); | |
278 | list->DeleteAllItems(); | |
279 | ||
280 | //Add some new items to tests ClearAll with | |
281 | list->InsertColumn(0, "Column 0"); | |
282 | list->InsertItem(0, "Item 0"); | |
283 | list->InsertItem(1, "Item 1"); | |
284 | ||
285 | //Check that ClearAll actually sends a DELETE_ALL_ITEMS event | |
286 | list->ClearAll(); | |
287 | ||
288 | //ClearAll and DeleteAllItems shouldn't send an event if there was nothing | |
289 | //to clear | |
290 | list->ClearAll(); | |
291 | list->DeleteAllItems(); | |
292 | ||
744d91d4 SL |
293 | CPPUNIT_ASSERT_EQUAL(2, deleteitem.GetCount()); |
294 | CPPUNIT_ASSERT_EQUAL(2, deleteall.GetCount()); | |
232fdc63 VZ |
295 | #endif |
296 | } | |
297 | ||
298 | void ListBaseTestCase::InsertItem() | |
299 | { | |
232fdc63 VZ |
300 | wxListCtrl* const list = GetList(); |
301 | ||
ce7fe42e | 302 | EventCounter insert(list, wxEVT_LIST_INSERT_ITEM); |
232fdc63 VZ |
303 | |
304 | list->InsertColumn(0, "Column 0", wxLIST_FORMAT_LEFT, 60); | |
305 | ||
306 | wxListItem item; | |
307 | item.SetId(0); | |
308 | item.SetText("some text"); | |
309 | ||
310 | list->InsertItem(item); | |
311 | list->InsertItem(1, "more text"); | |
312 | ||
744d91d4 | 313 | CPPUNIT_ASSERT_EQUAL(2, insert.GetCount()); |
232fdc63 VZ |
314 | } |
315 | ||
316 | void ListBaseTestCase::Find() | |
317 | { | |
318 | wxListCtrl* const list = GetList(); | |
319 | ||
320 | // set up for the test | |
321 | list->InsertColumn(0, "Column 0"); | |
322 | list->InsertColumn(1, "Column 1"); | |
323 | ||
324 | list->InsertItem(0, "Item 0"); | |
325 | list->SetItem(0, 1, "first column"); | |
326 | ||
327 | list->InsertItem(1, "Item 1"); | |
328 | list->SetItem(1, 1, "first column"); | |
329 | ||
330 | list->InsertItem(2, "Item 40"); | |
331 | list->SetItem(2, 1, "first column"); | |
332 | ||
333 | list->InsertItem(3, "ITEM 01"); | |
334 | list->SetItem(3, 1, "first column"); | |
335 | ||
336 | CPPUNIT_ASSERT_EQUAL(1, list->FindItem(-1, "Item 1")); | |
337 | CPPUNIT_ASSERT_EQUAL(2, list->FindItem(-1, "Item 4", true)); | |
338 | CPPUNIT_ASSERT_EQUAL(2, list->FindItem(1, "Item 40")); | |
339 | CPPUNIT_ASSERT_EQUAL(3, list->FindItem(2, "Item 0", true)); | |
340 | } | |
341 | ||
342 | void ListBaseTestCase::Visible() | |
343 | { | |
344 | wxListCtrl* const list = GetList(); | |
345 | ||
346 | list->InsertColumn(0, "Column 0"); | |
347 | ||
348 | int count = list->GetCountPerPage(); | |
349 | ||
350 | for( int i = 0; i < count + 10; i++ ) | |
351 | { | |
352 | list->InsertItem(i, wxString::Format("string %d", i)); | |
353 | } | |
354 | ||
355 | CPPUNIT_ASSERT_EQUAL(count + 10, list->GetItemCount()); | |
356 | CPPUNIT_ASSERT_EQUAL(0, list->GetTopItem()); | |
357 | ||
358 | list->EnsureVisible(count + 9); | |
359 | ||
360 | CPPUNIT_ASSERT(list->GetTopItem() != 0); | |
361 | } | |
362 | ||
363 | void ListBaseTestCase::ItemFormatting() | |
364 | { | |
365 | wxListCtrl* const list = GetList(); | |
366 | ||
367 | list->InsertColumn(0, "Column 0"); | |
368 | ||
369 | list->InsertItem(0, "Item 0"); | |
370 | list->InsertItem(1, "Item 1"); | |
371 | list->InsertItem(2, "Item 2"); | |
372 | ||
373 | list->SetTextColour(*wxYELLOW); | |
374 | list->SetBackgroundColour(*wxGREEN); | |
375 | list->SetItemTextColour(0, *wxRED); | |
376 | list->SetItemBackgroundColour(1, *wxBLUE); | |
377 | ||
378 | CPPUNIT_ASSERT_EQUAL(*wxGREEN, list->GetBackgroundColour()); | |
379 | CPPUNIT_ASSERT_EQUAL(*wxBLUE,list->GetItemBackgroundColour(1)); | |
380 | ||
381 | CPPUNIT_ASSERT_EQUAL(*wxYELLOW, list->GetTextColour()); | |
382 | CPPUNIT_ASSERT_EQUAL(*wxRED, list->GetItemTextColour(0)); | |
383 | } | |
384 | ||
385 | void ListBaseTestCase::EditLabel() | |
386 | { | |
387 | #if wxUSE_UIACTIONSIMULATOR | |
388 | wxListCtrl* const list = GetList(); | |
389 | ||
390 | list->SetWindowStyleFlag(wxLC_REPORT | wxLC_EDIT_LABELS); | |
391 | ||
392 | list->InsertColumn(0, "Column 0"); | |
393 | ||
394 | list->InsertItem(0, "Item 0"); | |
395 | list->InsertItem(1, "Item 1"); | |
396 | ||
ce7fe42e VZ |
397 | EventCounter beginedit(list, wxEVT_LIST_BEGIN_LABEL_EDIT); |
398 | EventCounter endedit(list, wxEVT_LIST_END_LABEL_EDIT); | |
232fdc63 VZ |
399 | |
400 | wxUIActionSimulator sim; | |
401 | ||
402 | list->EditLabel(0); | |
403 | ||
404 | sim.Text("sometext"); | |
405 | sim.Char(WXK_RETURN); | |
406 | ||
407 | wxYield(); | |
408 | ||
744d91d4 SL |
409 | CPPUNIT_ASSERT_EQUAL(1, beginedit.GetCount()); |
410 | CPPUNIT_ASSERT_EQUAL(1, endedit.GetCount()); | |
232fdc63 VZ |
411 | #endif |
412 | } | |
413 | ||
414 | void ListBaseTestCase::ImageList() | |
415 | { | |
416 | wxListCtrl* const list = GetList(); | |
417 | ||
418 | wxSize size(32, 32); | |
419 | ||
420 | wxImageList* imglist = new wxImageList(size.x, size.y); | |
421 | imglist->Add(wxArtProvider::GetIcon(wxART_INFORMATION, wxART_OTHER, size)); | |
422 | imglist->Add(wxArtProvider::GetIcon(wxART_QUESTION, wxART_OTHER, size)); | |
423 | imglist->Add(wxArtProvider::GetIcon(wxART_WARNING, wxART_OTHER, size)); | |
424 | ||
425 | list->AssignImageList(imglist, wxIMAGE_LIST_NORMAL); | |
426 | ||
427 | CPPUNIT_ASSERT_EQUAL(imglist, list->GetImageList(wxIMAGE_LIST_NORMAL)); | |
428 | } | |
429 | ||
430 | namespace | |
431 | { | |
432 | //From the sample but fixed so it actually inverts | |
433 | int wxCALLBACK | |
434 | MyCompareFunction(long item1, long item2, wxIntPtr WXUNUSED(sortData)) | |
435 | { | |
436 | // inverse the order | |
437 | if (item1 < item2) | |
438 | return 1; | |
439 | if (item1 > item2) | |
440 | return -1; | |
441 | ||
442 | return 0; | |
443 | } | |
444 | ||
445 | } | |
446 | ||
447 | void ListBaseTestCase::Sort() | |
448 | { | |
449 | wxListCtrl* const list = GetList(); | |
450 | ||
451 | list->InsertColumn(0, "Column 0"); | |
452 | ||
453 | list->InsertItem(0, "Item 0"); | |
454 | list->SetItemData(0, 0); | |
455 | list->InsertItem(1, "Item 1"); | |
456 | list->SetItemData(1, 1); | |
457 | ||
458 | list->SortItems(MyCompareFunction, 0); | |
459 | ||
460 | CPPUNIT_ASSERT_EQUAL("Item 1", list->GetItemText(0)); | |
461 | CPPUNIT_ASSERT_EQUAL("Item 0", list->GetItemText(1)); | |
462 | } | |
463 | ||
464 | #endif //wxUSE_LISTCTRL |