Fix test for Windows in the new wxExecute() unit test.
[wxWidgets.git] / tests / controls / listbasetest.cpp
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
135 // As we have a header, the top item shouldn't be at (0, 0), but somewhere
136 // below the header.
137 //
138 // Notice that we consider that the header can't be less than 10 pixels
139 // because we don't know its exact height.
140 CPPUNIT_ASSERT( list->GetItemRect(0, r) );
141 CPPUNIT_ASSERT( r.y >= 10 );
142
143 // However if we remove the header now, the item should be at (0, 0).
144 list->SetWindowStyle(wxLC_REPORT | wxLC_NO_HEADER);
145 CPPUNIT_ASSERT( list->GetItemRect(0, r) );
146 CPPUNIT_ASSERT_EQUAL( 0, r.y );
147
148
149 //tidy up when we are finished
150 list->ClearAll();
151 }
152
153 void ListBaseTestCase::ItemText()
154 {
155 wxListCtrl* const list = GetList();
156
157 list->InsertColumn(0, "First");
158 list->InsertColumn(1, "Second");
159
160 list->InsertItem(0, "0,0");
161 CPPUNIT_ASSERT_EQUAL( "0,0", list->GetItemText(0) );
162 CPPUNIT_ASSERT_EQUAL( "", list->GetItemText(0, 1) );
163
164 list->SetItem(0, 1, "0,1");
165 CPPUNIT_ASSERT_EQUAL( "0,1", list->GetItemText(0, 1) );
166 }
167
168 void ListBaseTestCase::ChangeMode()
169 {
170 wxListCtrl* const list = GetList();
171
172 list->InsertColumn(0, "Header");
173 list->InsertItem(0, "First");
174 list->InsertItem(1, "Second");
175 CPPUNIT_ASSERT_EQUAL( 2, list->GetItemCount() );
176
177 // check that switching the mode preserves the items
178 list->SetWindowStyle(wxLC_ICON);
179 CPPUNIT_ASSERT_EQUAL( 2, list->GetItemCount() );
180 CPPUNIT_ASSERT_EQUAL( "First", list->GetItemText(0) );
181
182 // and so does switching back
183 list->SetWindowStyle(wxLC_REPORT);
184 CPPUNIT_ASSERT_EQUAL( 2, list->GetItemCount() );
185 CPPUNIT_ASSERT_EQUAL( "First", list->GetItemText(0) );
186
187 //tidy up when we are finished
188 list->ClearAll();
189 }
190
191 void ListBaseTestCase::ItemClick()
192 {
193 #if wxUSE_UIACTIONSIMULATOR
194
195 #ifdef __WXMSW__
196 // FIXME: This test fails on MSW buildbot slaves although works fine on
197 // development machine, no idea why. It seems to be a problem with
198 // wxUIActionSimulator rather the wxListCtrl control itself however.
199 if ( IsAutomaticTest() )
200 return;
201 #endif // __WXMSW__
202
203 wxListCtrl* const list = GetList();
204
205 list->InsertColumn(0, "Column 0", wxLIST_FORMAT_LEFT, 60);
206 list->InsertColumn(1, "Column 1", wxLIST_FORMAT_LEFT, 50);
207 list->InsertColumn(2, "Column 2", wxLIST_FORMAT_LEFT, 40);
208
209 list->InsertItem(0, "Item 0");
210 list->SetItem(0, 1, "first column");
211 list->SetItem(0, 2, "second column");
212
213 EventCounter selected(list, wxEVT_LIST_ITEM_SELECTED);
214 EventCounter focused(list, wxEVT_LIST_ITEM_FOCUSED);
215 EventCounter activated(list, wxEVT_LIST_ITEM_ACTIVATED);
216 EventCounter rclick(list, wxEVT_LIST_ITEM_RIGHT_CLICK);
217
218 wxUIActionSimulator sim;
219
220 wxRect pos;
221 list->GetItemRect(0, pos);
222
223 //We move in slightly so we are not on the edge
224 wxPoint point = list->ClientToScreen(pos.GetPosition()) + wxPoint(10, 5);
225
226 sim.MouseMove(point);
227 wxYield();
228
229 sim.MouseClick();
230 wxYield();
231
232 sim.MouseDblClick();
233 wxYield();
234
235 sim.MouseClick(wxMOUSE_BTN_RIGHT);
236 wxYield();
237
238 // when the first item was selected the focus changes to it, but not
239 // on subsequent clicks
240
241 // FIXME: This test fail under wxGTK & wxOSX because we get 3 FOCUSED events and
242 // 2 SELECTED ones instead of the one of each we expect for some
243 // reason, this needs to be debugged as it may indicate a bug in the
244 // generic wxListCtrl implementation.
245 #ifndef _WX_GENERIC_LISTCTRL_H_
246 CPPUNIT_ASSERT_EQUAL(1, focused.GetCount());
247 CPPUNIT_ASSERT_EQUAL(1, selected.GetCount());
248 #endif
249 CPPUNIT_ASSERT_EQUAL(1, activated.GetCount());
250 CPPUNIT_ASSERT_EQUAL(1, rclick.GetCount());
251
252 //tidy up when we are finished
253 list->ClearAll();
254 #endif // wxUSE_UIACTIONSIMULATOR
255 }
256
257 void ListBaseTestCase::KeyDown()
258 {
259 #if wxUSE_UIACTIONSIMULATOR
260 wxListCtrl* const list = GetList();
261
262 EventCounter keydown(list, wxEVT_LIST_KEY_DOWN);
263
264 wxUIActionSimulator sim;
265
266 list->SetFocus();
267 sim.Text("aAbB");
268 wxYield();
269
270 CPPUNIT_ASSERT_EQUAL(6, keydown.GetCount());
271 #endif
272 }
273
274 void ListBaseTestCase::DeleteItems()
275 {
276 #ifndef __WXOSX__
277 wxListCtrl* const list = GetList();
278
279 EventCounter deleteitem(list, wxEVT_LIST_DELETE_ITEM);
280 EventCounter deleteall(list, wxEVT_LIST_DELETE_ALL_ITEMS);
281
282
283 list->InsertColumn(0, "Column 0", wxLIST_FORMAT_LEFT, 60);
284 list->InsertColumn(1, "Column 1", wxLIST_FORMAT_LEFT, 50);
285 list->InsertColumn(2, "Column 2", wxLIST_FORMAT_LEFT, 40);
286
287 list->InsertItem(0, "Item 0");
288 list->InsertItem(1, "Item 1");
289 list->InsertItem(2, "Item 1");
290
291 list->DeleteItem(0);
292 list->DeleteItem(0);
293 list->DeleteAllItems();
294
295 //Add some new items to tests ClearAll with
296 list->InsertColumn(0, "Column 0");
297 list->InsertItem(0, "Item 0");
298 list->InsertItem(1, "Item 1");
299
300 //Check that ClearAll actually sends a DELETE_ALL_ITEMS event
301 list->ClearAll();
302
303 //ClearAll and DeleteAllItems shouldn't send an event if there was nothing
304 //to clear
305 list->ClearAll();
306 list->DeleteAllItems();
307
308 CPPUNIT_ASSERT_EQUAL(2, deleteitem.GetCount());
309 CPPUNIT_ASSERT_EQUAL(2, deleteall.GetCount());
310 #endif
311 }
312
313 void ListBaseTestCase::InsertItem()
314 {
315 wxListCtrl* const list = GetList();
316
317 EventCounter insert(list, wxEVT_LIST_INSERT_ITEM);
318
319 list->InsertColumn(0, "Column 0", wxLIST_FORMAT_LEFT, 60);
320
321 wxListItem item;
322 item.SetId(0);
323 item.SetText("some text");
324
325 list->InsertItem(item);
326 list->InsertItem(1, "more text");
327
328 CPPUNIT_ASSERT_EQUAL(2, insert.GetCount());
329 }
330
331 void ListBaseTestCase::Find()
332 {
333 wxListCtrl* const list = GetList();
334
335 // set up for the test
336 list->InsertColumn(0, "Column 0");
337 list->InsertColumn(1, "Column 1");
338
339 list->InsertItem(0, "Item 0");
340 list->SetItem(0, 1, "first column");
341
342 list->InsertItem(1, "Item 1");
343 list->SetItem(1, 1, "first column");
344
345 list->InsertItem(2, "Item 40");
346 list->SetItem(2, 1, "first column");
347
348 list->InsertItem(3, "ITEM 01");
349 list->SetItem(3, 1, "first column");
350
351 CPPUNIT_ASSERT_EQUAL(1, list->FindItem(-1, "Item 1"));
352 CPPUNIT_ASSERT_EQUAL(2, list->FindItem(-1, "Item 4", true));
353 CPPUNIT_ASSERT_EQUAL(2, list->FindItem(1, "Item 40"));
354 CPPUNIT_ASSERT_EQUAL(3, list->FindItem(2, "Item 0", true));
355 }
356
357 void ListBaseTestCase::Visible()
358 {
359 wxListCtrl* const list = GetList();
360
361 list->InsertColumn(0, "Column 0");
362
363 int count = list->GetCountPerPage();
364
365 for( int i = 0; i < count + 10; i++ )
366 {
367 list->InsertItem(i, wxString::Format("string %d", i));
368 }
369
370 CPPUNIT_ASSERT_EQUAL(count + 10, list->GetItemCount());
371 CPPUNIT_ASSERT_EQUAL(0, list->GetTopItem());
372
373 list->EnsureVisible(count + 9);
374
375 CPPUNIT_ASSERT(list->GetTopItem() != 0);
376 }
377
378 void ListBaseTestCase::ItemFormatting()
379 {
380 wxListCtrl* const list = GetList();
381
382 list->InsertColumn(0, "Column 0");
383
384 list->InsertItem(0, "Item 0");
385 list->InsertItem(1, "Item 1");
386 list->InsertItem(2, "Item 2");
387
388 list->SetTextColour(*wxYELLOW);
389 list->SetBackgroundColour(*wxGREEN);
390 list->SetItemTextColour(0, *wxRED);
391 list->SetItemBackgroundColour(1, *wxBLUE);
392
393 CPPUNIT_ASSERT_EQUAL(*wxGREEN, list->GetBackgroundColour());
394 CPPUNIT_ASSERT_EQUAL(*wxBLUE,list->GetItemBackgroundColour(1));
395
396 CPPUNIT_ASSERT_EQUAL(*wxYELLOW, list->GetTextColour());
397 CPPUNIT_ASSERT_EQUAL(*wxRED, list->GetItemTextColour(0));
398 }
399
400 void ListBaseTestCase::EditLabel()
401 {
402 #if wxUSE_UIACTIONSIMULATOR
403 wxListCtrl* const list = GetList();
404
405 list->SetWindowStyleFlag(wxLC_REPORT | wxLC_EDIT_LABELS);
406
407 list->InsertColumn(0, "Column 0");
408
409 list->InsertItem(0, "Item 0");
410 list->InsertItem(1, "Item 1");
411
412 EventCounter beginedit(list, wxEVT_LIST_BEGIN_LABEL_EDIT);
413 EventCounter endedit(list, wxEVT_LIST_END_LABEL_EDIT);
414
415 wxUIActionSimulator sim;
416
417 list->EditLabel(0);
418
419 sim.Text("sometext");
420 sim.Char(WXK_RETURN);
421
422 wxYield();
423
424 CPPUNIT_ASSERT_EQUAL(1, beginedit.GetCount());
425 CPPUNIT_ASSERT_EQUAL(1, endedit.GetCount());
426 #endif
427 }
428
429 void ListBaseTestCase::ImageList()
430 {
431 wxListCtrl* const list = GetList();
432
433 wxSize size(32, 32);
434
435 wxImageList* imglist = new wxImageList(size.x, size.y);
436 imglist->Add(wxArtProvider::GetIcon(wxART_INFORMATION, wxART_OTHER, size));
437 imglist->Add(wxArtProvider::GetIcon(wxART_QUESTION, wxART_OTHER, size));
438 imglist->Add(wxArtProvider::GetIcon(wxART_WARNING, wxART_OTHER, size));
439
440 list->AssignImageList(imglist, wxIMAGE_LIST_NORMAL);
441
442 CPPUNIT_ASSERT_EQUAL(imglist, list->GetImageList(wxIMAGE_LIST_NORMAL));
443 }
444
445 namespace
446 {
447 //From the sample but fixed so it actually inverts
448 int wxCALLBACK
449 MyCompareFunction(long item1, long item2, wxIntPtr WXUNUSED(sortData))
450 {
451 // inverse the order
452 if (item1 < item2)
453 return 1;
454 if (item1 > item2)
455 return -1;
456
457 return 0;
458 }
459
460 }
461
462 void ListBaseTestCase::Sort()
463 {
464 wxListCtrl* const list = GetList();
465
466 list->InsertColumn(0, "Column 0");
467
468 list->InsertItem(0, "Item 0");
469 list->SetItemData(0, 0);
470 list->InsertItem(1, "Item 1");
471 list->SetItemData(1, 1);
472
473 list->SortItems(MyCompareFunction, 0);
474
475 CPPUNIT_ASSERT_EQUAL("Item 1", list->GetItemText(0));
476 CPPUNIT_ASSERT_EQUAL("Item 0", list->GetItemText(1));
477 }
478
479 #endif //wxUSE_LISTCTRL