1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/listbasetest.cpp
3 // Purpose: Base class for wxListCtrl and wxListView tests
4 // Author: Steven Lamerton
6 // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>,
7 // (c) 2010 Steven Lamerton
8 ///////////////////////////////////////////////////////////////////////////////
22 #include "wx/listctrl.h"
23 #include "listbasetest.h"
24 #include "testableframe.h"
25 #include "asserthelper.h"
26 #include "wx/uiaction.h"
27 #include "wx/imaglist.h"
28 #include "wx/artprov.h"
30 void ListBaseTestCase::ColumnsOrder()
32 #ifdef wxHAS_LISTCTRL_COLUMN_ORDER
33 wxListCtrl
* const list
= GetList();
37 li
.SetMask(wxLIST_MASK_TEXT
);
39 // first set up some columns
40 static const int NUM_COLS
= 3;
42 list
->InsertColumn(0, "Column 0");
43 list
->InsertColumn(1, "Column 1");
44 list
->InsertColumn(2, "Column 2");
46 // and a couple of test items too
47 list
->InsertItem(0, "Item 0");
48 list
->SetItem(0, 1, "first in first");
50 list
->InsertItem(1, "Item 1");
51 list
->SetItem(1, 2, "second in second");
54 // check that the order is natural in the beginning
55 const wxArrayInt orderOrig
= list
->GetColumnsOrder();
56 for ( n
= 0; n
< NUM_COLS
; n
++ )
57 CPPUNIT_ASSERT_EQUAL( n
, orderOrig
[n
] );
59 // then rearrange them: using { 2, 0, 1 } order means that column 2 is
60 // shown first, then column 0 and finally column 1
65 list
->SetColumnsOrder(order
);
67 // check that we get back the same order as we set
68 const wxArrayInt orderNew
= list
->GetColumnsOrder();
69 for ( n
= 0; n
< NUM_COLS
; n
++ )
70 CPPUNIT_ASSERT_EQUAL( order
[n
], orderNew
[n
] );
72 // and the order -> index mappings for individual columns
73 for ( n
= 0; n
< NUM_COLS
; n
++ )
74 CPPUNIT_ASSERT_EQUAL( order
[n
], list
->GetColumnIndexFromOrder(n
) );
76 // and also the reverse mapping
77 CPPUNIT_ASSERT_EQUAL( 1, list
->GetColumnOrder(0) );
78 CPPUNIT_ASSERT_EQUAL( 2, list
->GetColumnOrder(1) );
79 CPPUNIT_ASSERT_EQUAL( 0, list
->GetColumnOrder(2) );
82 // finally check that accessors still use indices, not order
83 CPPUNIT_ASSERT( list
->GetColumn(0, li
) );
84 CPPUNIT_ASSERT_EQUAL( "Column 0", li
.GetText() );
88 CPPUNIT_ASSERT( list
->GetItem(li
) );
89 CPPUNIT_ASSERT_EQUAL( "first in first", li
.GetText() );
93 CPPUNIT_ASSERT( list
->GetItem(li
) );
94 CPPUNIT_ASSERT_EQUAL( "second in second", li
.GetText() );
96 //tidy up when we are finished
98 #endif // wxHAS_LISTCTRL_COLUMN_ORDER
103 void ListBaseTestCase::ItemRect()
105 wxListCtrl
* const list
= GetList();
107 // set up for the test
108 list
->InsertColumn(0, "Column 0", wxLIST_FORMAT_LEFT
, 60);
109 list
->InsertColumn(1, "Column 1", wxLIST_FORMAT_LEFT
, 50);
110 list
->InsertColumn(2, "Column 2", wxLIST_FORMAT_LEFT
, 40);
112 list
->InsertItem(0, "Item 0");
113 list
->SetItem(0, 1, "first column");
114 list
->SetItem(0, 1, "second column");
118 WX_ASSERT_FAILS_WITH_ASSERT( list
->GetItemRect(1, r
) );
119 CPPUNIT_ASSERT( list
->GetItemRect(0, r
) );
120 CPPUNIT_ASSERT_EQUAL( 150, r
.GetWidth() );
122 CPPUNIT_ASSERT( list
->GetSubItemRect(0, 0, r
) );
123 CPPUNIT_ASSERT_EQUAL( 60, r
.GetWidth() );
125 CPPUNIT_ASSERT( list
->GetSubItemRect(0, 1, r
) );
126 CPPUNIT_ASSERT_EQUAL( 50, r
.GetWidth() );
128 CPPUNIT_ASSERT( list
->GetSubItemRect(0, 2, r
) );
129 CPPUNIT_ASSERT_EQUAL( 40, r
.GetWidth() );
131 WX_ASSERT_FAILS_WITH_ASSERT( list
->GetSubItemRect(0, 3, r
) );
134 // As we have a header, the top item shouldn't be at (0, 0), but somewhere
137 // Notice that we consider that the header can't be less than 10 pixels
138 // because we don't know its exact height.
139 CPPUNIT_ASSERT( list
->GetItemRect(0, r
) );
140 CPPUNIT_ASSERT( r
.y
>= 10 );
142 // However if we remove the header now, the item should be at (0, 0).
143 list
->SetWindowStyle(wxLC_REPORT
| wxLC_NO_HEADER
);
144 CPPUNIT_ASSERT( list
->GetItemRect(0, r
) );
145 CPPUNIT_ASSERT_EQUAL( 0, r
.y
);
148 //tidy up when we are finished
152 void ListBaseTestCase::ItemText()
154 wxListCtrl
* const list
= GetList();
156 list
->InsertColumn(0, "First");
157 list
->InsertColumn(1, "Second");
159 list
->InsertItem(0, "0,0");
160 CPPUNIT_ASSERT_EQUAL( "0,0", list
->GetItemText(0) );
161 CPPUNIT_ASSERT_EQUAL( "", list
->GetItemText(0, 1) );
163 list
->SetItem(0, 1, "0,1");
164 CPPUNIT_ASSERT_EQUAL( "0,1", list
->GetItemText(0, 1) );
167 void ListBaseTestCase::ChangeMode()
169 wxListCtrl
* const list
= GetList();
171 list
->InsertColumn(0, "Header");
172 list
->InsertItem(0, "First");
173 list
->InsertItem(1, "Second");
174 CPPUNIT_ASSERT_EQUAL( 2, list
->GetItemCount() );
176 // check that switching the mode preserves the items
177 list
->SetWindowStyle(wxLC_ICON
);
178 CPPUNIT_ASSERT_EQUAL( 2, list
->GetItemCount() );
179 CPPUNIT_ASSERT_EQUAL( "First", list
->GetItemText(0) );
181 // and so does switching back
182 list
->SetWindowStyle(wxLC_REPORT
);
183 CPPUNIT_ASSERT_EQUAL( 2, list
->GetItemCount() );
184 CPPUNIT_ASSERT_EQUAL( "First", list
->GetItemText(0) );
186 //tidy up when we are finished
190 void ListBaseTestCase::ItemClick()
192 #if wxUSE_UIACTIONSIMULATOR
195 // FIXME: This test fails on MSW buildbot slaves although works fine on
196 // development machine, no idea why. It seems to be a problem with
197 // wxUIActionSimulator rather the wxListCtrl control itself however.
198 if ( IsAutomaticTest() )
202 wxListCtrl
* const list
= GetList();
204 list
->InsertColumn(0, "Column 0", wxLIST_FORMAT_LEFT
, 60);
205 list
->InsertColumn(1, "Column 1", wxLIST_FORMAT_LEFT
, 50);
206 list
->InsertColumn(2, "Column 2", wxLIST_FORMAT_LEFT
, 40);
208 list
->InsertItem(0, "Item 0");
209 list
->SetItem(0, 1, "first column");
210 list
->SetItem(0, 2, "second column");
212 EventCounter
selected(list
, wxEVT_LIST_ITEM_SELECTED
);
213 EventCounter
focused(list
, wxEVT_LIST_ITEM_FOCUSED
);
214 EventCounter
activated(list
, wxEVT_LIST_ITEM_ACTIVATED
);
215 EventCounter
rclick(list
, wxEVT_LIST_ITEM_RIGHT_CLICK
);
217 wxUIActionSimulator sim
;
220 list
->GetItemRect(0, pos
);
222 //We move in slightly so we are not on the edge
223 wxPoint point
= list
->ClientToScreen(pos
.GetPosition()) + wxPoint(10, 5);
225 sim
.MouseMove(point
);
234 sim
.MouseClick(wxMOUSE_BTN_RIGHT
);
237 // when the first item was selected the focus changes to it, but not
238 // on subsequent clicks
240 // FIXME: This test fail under wxGTK & wxOSX because we get 3 FOCUSED events and
241 // 2 SELECTED ones instead of the one of each we expect for some
242 // reason, this needs to be debugged as it may indicate a bug in the
243 // generic wxListCtrl implementation.
244 #ifndef _WX_GENERIC_LISTCTRL_H_
245 CPPUNIT_ASSERT_EQUAL(1, focused
.GetCount());
246 CPPUNIT_ASSERT_EQUAL(1, selected
.GetCount());
248 CPPUNIT_ASSERT_EQUAL(1, activated
.GetCount());
249 CPPUNIT_ASSERT_EQUAL(1, rclick
.GetCount());
251 //tidy up when we are finished
253 #endif // wxUSE_UIACTIONSIMULATOR
256 void ListBaseTestCase::KeyDown()
258 #if wxUSE_UIACTIONSIMULATOR
259 wxListCtrl
* const list
= GetList();
261 EventCounter
keydown(list
, wxEVT_LIST_KEY_DOWN
);
263 wxUIActionSimulator sim
;
269 CPPUNIT_ASSERT_EQUAL(6, keydown
.GetCount());
273 void ListBaseTestCase::DeleteItems()
276 wxListCtrl
* const list
= GetList();
278 EventCounter
deleteitem(list
, wxEVT_LIST_DELETE_ITEM
);
279 EventCounter
deleteall(list
, wxEVT_LIST_DELETE_ALL_ITEMS
);
282 list
->InsertColumn(0, "Column 0", wxLIST_FORMAT_LEFT
, 60);
283 list
->InsertColumn(1, "Column 1", wxLIST_FORMAT_LEFT
, 50);
284 list
->InsertColumn(2, "Column 2", wxLIST_FORMAT_LEFT
, 40);
286 list
->InsertItem(0, "Item 0");
287 list
->InsertItem(1, "Item 1");
288 list
->InsertItem(2, "Item 1");
292 list
->DeleteAllItems();
294 //Add some new items to tests ClearAll with
295 list
->InsertColumn(0, "Column 0");
296 list
->InsertItem(0, "Item 0");
297 list
->InsertItem(1, "Item 1");
299 //Check that ClearAll actually sends a DELETE_ALL_ITEMS event
302 //ClearAll and DeleteAllItems shouldn't send an event if there was nothing
305 list
->DeleteAllItems();
307 CPPUNIT_ASSERT_EQUAL(2, deleteitem
.GetCount());
308 CPPUNIT_ASSERT_EQUAL(2, deleteall
.GetCount());
312 void ListBaseTestCase::InsertItem()
314 wxListCtrl
* const list
= GetList();
316 EventCounter
insert(list
, wxEVT_LIST_INSERT_ITEM
);
318 list
->InsertColumn(0, "Column 0", wxLIST_FORMAT_LEFT
, 60);
322 item
.SetText("some text");
324 list
->InsertItem(item
);
325 list
->InsertItem(1, "more text");
327 CPPUNIT_ASSERT_EQUAL(2, insert
.GetCount());
330 void ListBaseTestCase::Find()
332 wxListCtrl
* const list
= GetList();
334 // set up for the test
335 list
->InsertColumn(0, "Column 0");
336 list
->InsertColumn(1, "Column 1");
338 list
->InsertItem(0, "Item 0");
339 list
->SetItem(0, 1, "first column");
341 list
->InsertItem(1, "Item 1");
342 list
->SetItem(1, 1, "first column");
344 list
->InsertItem(2, "Item 40");
345 list
->SetItem(2, 1, "first column");
347 list
->InsertItem(3, "ITEM 01");
348 list
->SetItem(3, 1, "first column");
350 CPPUNIT_ASSERT_EQUAL(1, list
->FindItem(-1, "Item 1"));
351 CPPUNIT_ASSERT_EQUAL(2, list
->FindItem(-1, "Item 4", true));
352 CPPUNIT_ASSERT_EQUAL(2, list
->FindItem(1, "Item 40"));
353 CPPUNIT_ASSERT_EQUAL(3, list
->FindItem(2, "Item 0", true));
356 void ListBaseTestCase::Visible()
358 wxListCtrl
* const list
= GetList();
360 list
->InsertColumn(0, "Column 0");
362 int count
= list
->GetCountPerPage();
364 for( int i
= 0; i
< count
+ 10; i
++ )
366 list
->InsertItem(i
, wxString::Format("string %d", i
));
369 CPPUNIT_ASSERT_EQUAL(count
+ 10, list
->GetItemCount());
370 CPPUNIT_ASSERT_EQUAL(0, list
->GetTopItem());
372 list
->EnsureVisible(count
+ 9);
374 CPPUNIT_ASSERT(list
->GetTopItem() != 0);
377 void ListBaseTestCase::ItemFormatting()
379 wxListCtrl
* const list
= GetList();
381 list
->InsertColumn(0, "Column 0");
383 list
->InsertItem(0, "Item 0");
384 list
->InsertItem(1, "Item 1");
385 list
->InsertItem(2, "Item 2");
387 list
->SetTextColour(*wxYELLOW
);
388 list
->SetBackgroundColour(*wxGREEN
);
389 list
->SetItemTextColour(0, *wxRED
);
390 list
->SetItemBackgroundColour(1, *wxBLUE
);
392 CPPUNIT_ASSERT_EQUAL(*wxGREEN
, list
->GetBackgroundColour());
393 CPPUNIT_ASSERT_EQUAL(*wxBLUE
,list
->GetItemBackgroundColour(1));
395 CPPUNIT_ASSERT_EQUAL(*wxYELLOW
, list
->GetTextColour());
396 CPPUNIT_ASSERT_EQUAL(*wxRED
, list
->GetItemTextColour(0));
399 void ListBaseTestCase::EditLabel()
401 #if wxUSE_UIACTIONSIMULATOR
402 wxListCtrl
* const list
= GetList();
404 list
->SetWindowStyleFlag(wxLC_REPORT
| wxLC_EDIT_LABELS
);
406 list
->InsertColumn(0, "Column 0");
408 list
->InsertItem(0, "Item 0");
409 list
->InsertItem(1, "Item 1");
411 EventCounter
beginedit(list
, wxEVT_LIST_BEGIN_LABEL_EDIT
);
412 EventCounter
endedit(list
, wxEVT_LIST_END_LABEL_EDIT
);
414 wxUIActionSimulator sim
;
418 sim
.Text("sometext");
419 sim
.Char(WXK_RETURN
);
423 CPPUNIT_ASSERT_EQUAL(1, beginedit
.GetCount());
424 CPPUNIT_ASSERT_EQUAL(1, endedit
.GetCount());
428 void ListBaseTestCase::ImageList()
430 wxListCtrl
* const list
= GetList();
434 wxImageList
* imglist
= new wxImageList(size
.x
, size
.y
);
435 imglist
->Add(wxArtProvider::GetIcon(wxART_INFORMATION
, wxART_OTHER
, size
));
436 imglist
->Add(wxArtProvider::GetIcon(wxART_QUESTION
, wxART_OTHER
, size
));
437 imglist
->Add(wxArtProvider::GetIcon(wxART_WARNING
, wxART_OTHER
, size
));
439 list
->AssignImageList(imglist
, wxIMAGE_LIST_NORMAL
);
441 CPPUNIT_ASSERT_EQUAL(imglist
, list
->GetImageList(wxIMAGE_LIST_NORMAL
));
446 //From the sample but fixed so it actually inverts
448 MyCompareFunction(long item1
, long item2
, wxIntPtr
WXUNUSED(sortData
))
461 void ListBaseTestCase::Sort()
463 wxListCtrl
* const list
= GetList();
465 list
->InsertColumn(0, "Column 0");
467 list
->InsertItem(0, "Item 0");
468 list
->SetItemData(0, 0);
469 list
->InsertItem(1, "Item 1");
470 list
->SetItemData(1, 1);
472 list
->SortItems(MyCompareFunction
, 0);
474 CPPUNIT_ASSERT_EQUAL("Item 1", list
->GetItemText(0));
475 CPPUNIT_ASSERT_EQUAL("Item 0", list
->GetItemText(1));
478 #endif //wxUSE_LISTCTRL