1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/listbasetest.cpp
3 // Purpose: Base class for wxListCtrl and wxListView tests
4 // Author: Steven Lamerton
7 // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>,
8 // (c) 2010 Steven Lamerton
9 ///////////////////////////////////////////////////////////////////////////////
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"
31 void ListBaseTestCase::ColumnsOrder()
33 #ifdef wxHAS_LISTCTRL_COLUMN_ORDER
34 wxListCtrl
* const list
= GetList();
38 li
.SetMask(wxLIST_MASK_TEXT
);
40 // first set up some columns
41 static const int NUM_COLS
= 3;
43 list
->InsertColumn(0, "Column 0");
44 list
->InsertColumn(1, "Column 1");
45 list
->InsertColumn(2, "Column 2");
47 // and a couple of test items too
48 list
->InsertItem(0, "Item 0");
49 list
->SetItem(0, 1, "first in first");
51 list
->InsertItem(1, "Item 1");
52 list
->SetItem(1, 2, "second in second");
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
] );
60 // then rearrange them: using { 2, 0, 1 } order means that column 2 is
61 // shown first, then column 0 and finally column 1
66 list
->SetColumnsOrder(order
);
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
] );
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
) );
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) );
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() );
89 CPPUNIT_ASSERT( list
->GetItem(li
) );
90 CPPUNIT_ASSERT_EQUAL( "first in first", li
.GetText() );
94 CPPUNIT_ASSERT( list
->GetItem(li
) );
95 CPPUNIT_ASSERT_EQUAL( "second in second", li
.GetText() );
97 //tidy up when we are finished
99 #endif // wxHAS_LISTCTRL_COLUMN_ORDER
104 void ListBaseTestCase::ItemRect()
106 wxListCtrl
* const list
= GetList();
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);
113 list
->InsertItem(0, "Item 0");
114 list
->SetItem(0, 1, "first column");
115 list
->SetItem(0, 1, "second column");
119 WX_ASSERT_FAILS_WITH_ASSERT( list
->GetItemRect(1, r
) );
120 CPPUNIT_ASSERT( list
->GetItemRect(0, r
) );
121 CPPUNIT_ASSERT_EQUAL( 150, r
.GetWidth() );
123 CPPUNIT_ASSERT( list
->GetSubItemRect(0, 0, r
) );
124 CPPUNIT_ASSERT_EQUAL( 60, r
.GetWidth() );
126 CPPUNIT_ASSERT( list
->GetSubItemRect(0, 1, r
) );
127 CPPUNIT_ASSERT_EQUAL( 50, r
.GetWidth() );
129 CPPUNIT_ASSERT( list
->GetSubItemRect(0, 2, r
) );
130 CPPUNIT_ASSERT_EQUAL( 40, r
.GetWidth() );
132 WX_ASSERT_FAILS_WITH_ASSERT( list
->GetSubItemRect(0, 3, r
) );
134 //tidy up when we are finished
138 void ListBaseTestCase::ItemText()
140 wxListCtrl
* const list
= GetList();
142 list
->InsertColumn(0, "First");
143 list
->InsertColumn(1, "Second");
145 list
->InsertItem(0, "0,0");
146 CPPUNIT_ASSERT_EQUAL( "0,0", list
->GetItemText(0) );
147 CPPUNIT_ASSERT_EQUAL( "", list
->GetItemText(0, 1) );
149 list
->SetItem(0, 1, "0,1");
150 CPPUNIT_ASSERT_EQUAL( "0,1", list
->GetItemText(0, 1) );
153 void ListBaseTestCase::ChangeMode()
155 wxListCtrl
* const list
= GetList();
157 list
->InsertColumn(0, "Header");
158 list
->InsertItem(0, "First");
159 list
->InsertItem(1, "Second");
160 CPPUNIT_ASSERT_EQUAL( 2, list
->GetItemCount() );
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) );
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) );
172 //tidy up when we are finished
176 void ListBaseTestCase::ItemClick()
178 #if wxUSE_UIACTIONSIMULATOR && !defined(__WXGTK__)
179 wxTestableFrame
* frame
= wxStaticCast(wxTheApp
->GetTopWindow(),
182 wxListCtrl
* const list
= GetList();
184 list
->InsertColumn(0, "Column 0", wxLIST_FORMAT_LEFT
, 60);
185 list
->InsertColumn(1, "Column 1", wxLIST_FORMAT_LEFT
, 50);
186 list
->InsertColumn(2, "Column 2", wxLIST_FORMAT_LEFT
, 40);
188 list
->InsertItem(0, "Item 0");
189 list
->SetItem(0, 1, "first column");
190 list
->SetItem(0, 2, "second column");
192 list
->InsertItem(1, "Item 1");
193 list
->SetItemState(1, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
195 EventCounter
count(list
, wxEVT_COMMAND_LIST_ITEM_SELECTED
);
196 EventCounter
count1(list
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
197 EventCounter
count2(list
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
198 EventCounter
count3(list
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
);
200 wxUIActionSimulator sim
;
203 list
->GetItemRect(0, pos
);
205 //We move in slightly so we are not on the edge
206 wxPoint point
= list
->ClientToScreen(pos
.GetPosition()) + wxPoint(2, 2);
208 sim
.MouseMove(point
);
217 sim
.MouseClick(wxMOUSE_BTN_RIGHT
);
220 // when the first item was selected the focus changes to it, but not
221 // on subsequent clicks
222 CPPUNIT_ASSERT_EQUAL(1, frame
->GetEventCount(wxEVT_COMMAND_LIST_ITEM_FOCUSED
));
223 CPPUNIT_ASSERT_EQUAL(1, frame
->GetEventCount(wxEVT_COMMAND_LIST_ITEM_SELECTED
));
224 CPPUNIT_ASSERT_EQUAL(1, frame
->GetEventCount(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
));
225 CPPUNIT_ASSERT_EQUAL(1, frame
->GetEventCount(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
));
227 //tidy up when we are finished
229 #endif // wxUSE_UIACTIONSIMULATOR
232 void ListBaseTestCase::KeyDown()
234 #if wxUSE_UIACTIONSIMULATOR
235 wxTestableFrame
* frame
= wxStaticCast(wxTheApp
->GetTopWindow(),
238 wxListCtrl
* const list
= GetList();
240 EventCounter
count(list
, wxEVT_COMMAND_LIST_KEY_DOWN
);
242 wxUIActionSimulator sim
;
248 CPPUNIT_ASSERT_EQUAL(6, frame
->GetEventCount());
252 void ListBaseTestCase::DeleteItems()
255 wxTestableFrame
* frame
= wxStaticCast(wxTheApp
->GetTopWindow(),
258 wxListCtrl
* const list
= GetList();
260 EventCounter
count(list
, wxEVT_COMMAND_LIST_DELETE_ITEM
);
261 EventCounter
count1(list
, wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
);
264 list
->InsertColumn(0, "Column 0", wxLIST_FORMAT_LEFT
, 60);
265 list
->InsertColumn(1, "Column 1", wxLIST_FORMAT_LEFT
, 50);
266 list
->InsertColumn(2, "Column 2", wxLIST_FORMAT_LEFT
, 40);
268 list
->InsertItem(0, "Item 0");
269 list
->InsertItem(1, "Item 1");
270 list
->InsertItem(2, "Item 1");
274 list
->DeleteAllItems();
276 //Add some new items to tests ClearAll with
277 list
->InsertColumn(0, "Column 0");
278 list
->InsertItem(0, "Item 0");
279 list
->InsertItem(1, "Item 1");
281 //Check that ClearAll actually sends a DELETE_ALL_ITEMS event
284 //ClearAll and DeleteAllItems shouldn't send an event if there was nothing
287 list
->DeleteAllItems();
289 CPPUNIT_ASSERT_EQUAL(2, frame
->GetEventCount(wxEVT_COMMAND_LIST_DELETE_ITEM
));
290 CPPUNIT_ASSERT_EQUAL(2, frame
->GetEventCount(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
));
294 void ListBaseTestCase::InsertItem()
296 wxTestableFrame
* frame
= wxStaticCast(wxTheApp
->GetTopWindow(),
299 wxListCtrl
* const list
= GetList();
301 EventCounter
count(list
, wxEVT_COMMAND_LIST_INSERT_ITEM
);
303 list
->InsertColumn(0, "Column 0", wxLIST_FORMAT_LEFT
, 60);
307 item
.SetText("some text");
309 list
->InsertItem(item
);
310 list
->InsertItem(1, "more text");
312 CPPUNIT_ASSERT_EQUAL(2, frame
->GetEventCount(wxEVT_COMMAND_LIST_INSERT_ITEM
));
315 void ListBaseTestCase::Find()
317 wxListCtrl
* const list
= GetList();
319 // set up for the test
320 list
->InsertColumn(0, "Column 0");
321 list
->InsertColumn(1, "Column 1");
323 list
->InsertItem(0, "Item 0");
324 list
->SetItem(0, 1, "first column");
326 list
->InsertItem(1, "Item 1");
327 list
->SetItem(1, 1, "first column");
329 list
->InsertItem(2, "Item 40");
330 list
->SetItem(2, 1, "first column");
332 list
->InsertItem(3, "ITEM 01");
333 list
->SetItem(3, 1, "first column");
335 CPPUNIT_ASSERT_EQUAL(1, list
->FindItem(-1, "Item 1"));
336 CPPUNIT_ASSERT_EQUAL(2, list
->FindItem(-1, "Item 4", true));
337 CPPUNIT_ASSERT_EQUAL(2, list
->FindItem(1, "Item 40"));
338 CPPUNIT_ASSERT_EQUAL(3, list
->FindItem(2, "Item 0", true));
341 void ListBaseTestCase::Visible()
343 wxListCtrl
* const list
= GetList();
345 list
->InsertColumn(0, "Column 0");
347 int count
= list
->GetCountPerPage();
349 for( int i
= 0; i
< count
+ 10; i
++ )
351 list
->InsertItem(i
, wxString::Format("string %d", i
));
354 CPPUNIT_ASSERT_EQUAL(count
+ 10, list
->GetItemCount());
355 CPPUNIT_ASSERT_EQUAL(0, list
->GetTopItem());
357 list
->EnsureVisible(count
+ 9);
359 CPPUNIT_ASSERT(list
->GetTopItem() != 0);
362 void ListBaseTestCase::ItemFormatting()
364 wxListCtrl
* const list
= GetList();
366 list
->InsertColumn(0, "Column 0");
368 list
->InsertItem(0, "Item 0");
369 list
->InsertItem(1, "Item 1");
370 list
->InsertItem(2, "Item 2");
372 list
->SetTextColour(*wxYELLOW
);
373 list
->SetBackgroundColour(*wxGREEN
);
374 list
->SetItemTextColour(0, *wxRED
);
375 list
->SetItemBackgroundColour(1, *wxBLUE
);
377 CPPUNIT_ASSERT_EQUAL(*wxGREEN
, list
->GetBackgroundColour());
378 CPPUNIT_ASSERT_EQUAL(*wxBLUE
,list
->GetItemBackgroundColour(1));
380 CPPUNIT_ASSERT_EQUAL(*wxYELLOW
, list
->GetTextColour());
381 CPPUNIT_ASSERT_EQUAL(*wxRED
, list
->GetItemTextColour(0));
384 void ListBaseTestCase::EditLabel()
386 #if wxUSE_UIACTIONSIMULATOR
387 wxListCtrl
* const list
= GetList();
389 list
->SetWindowStyleFlag(wxLC_REPORT
| wxLC_EDIT_LABELS
);
391 list
->InsertColumn(0, "Column 0");
393 list
->InsertItem(0, "Item 0");
394 list
->InsertItem(1, "Item 1");
396 wxTestableFrame
* frame
= wxStaticCast(wxTheApp
->GetTopWindow(),
399 EventCounter
count(list
, wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
);
400 EventCounter
count1(list
, wxEVT_COMMAND_LIST_END_LABEL_EDIT
);
402 wxUIActionSimulator sim
;
406 sim
.Text("sometext");
407 sim
.Char(WXK_RETURN
);
411 CPPUNIT_ASSERT_EQUAL(1, frame
->GetEventCount(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
));
412 CPPUNIT_ASSERT_EQUAL(1, frame
->GetEventCount(wxEVT_COMMAND_LIST_END_LABEL_EDIT
));
416 void ListBaseTestCase::ImageList()
418 wxListCtrl
* const list
= GetList();
422 wxImageList
* imglist
= new wxImageList(size
.x
, size
.y
);
423 imglist
->Add(wxArtProvider::GetIcon(wxART_INFORMATION
, wxART_OTHER
, size
));
424 imglist
->Add(wxArtProvider::GetIcon(wxART_QUESTION
, wxART_OTHER
, size
));
425 imglist
->Add(wxArtProvider::GetIcon(wxART_WARNING
, wxART_OTHER
, size
));
427 list
->AssignImageList(imglist
, wxIMAGE_LIST_NORMAL
);
429 CPPUNIT_ASSERT_EQUAL(imglist
, list
->GetImageList(wxIMAGE_LIST_NORMAL
));
434 //From the sample but fixed so it actually inverts
436 MyCompareFunction(long item1
, long item2
, wxIntPtr
WXUNUSED(sortData
))
449 void ListBaseTestCase::Sort()
451 wxListCtrl
* const list
= GetList();
453 list
->InsertColumn(0, "Column 0");
455 list
->InsertItem(0, "Item 0");
456 list
->SetItemData(0, 0);
457 list
->InsertItem(1, "Item 1");
458 list
->SetItemData(1, 1);
460 list
->SortItems(MyCompareFunction
, 0);
462 CPPUNIT_ASSERT_EQUAL("Item 1", list
->GetItemText(0));
463 CPPUNIT_ASSERT_EQUAL("Item 0", list
->GetItemText(1));
466 #endif //wxUSE_LISTCTRL