]> git.saurik.com Git - wxWidgets.git/blob - tests/controls/listbasetest.cpp
Remove "release" suffix from wx-config links used in RPMs.
[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 //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 {
178 // FIXME: This test fail under wxGTK because we get 3 FOCUSED events and
179 // 2 SELECTED ones instead of the one of each we expect for some
180 // reason, this needs to be debugged as it may indicate a bug in the
181 // generic wxListCtrl implementation.
182 #if wxUSE_UIACTIONSIMULATOR && !defined(__WXGTK__)
183
184 // FIXME: This test fails on MSW buildbot slaves although works fine on
185 // development machine, no idea why. It seems to be a problem with
186 // wxUIActionSimulator rather the wxListCtrl control itself however.
187 if ( wxGetUserId().Lower().Matches("buildslave*") )
188 return;
189
190 wxTestableFrame* frame = wxStaticCast(wxTheApp->GetTopWindow(),
191 wxTestableFrame);
192
193 wxListCtrl* const list = GetList();
194
195 list->InsertColumn(0, "Column 0", wxLIST_FORMAT_LEFT, 60);
196 list->InsertColumn(1, "Column 1", wxLIST_FORMAT_LEFT, 50);
197 list->InsertColumn(2, "Column 2", wxLIST_FORMAT_LEFT, 40);
198
199 list->InsertItem(0, "Item 0");
200 list->SetItem(0, 1, "first column");
201 list->SetItem(0, 2, "second column");
202
203 EventCounter count(list, wxEVT_COMMAND_LIST_ITEM_SELECTED);
204 EventCounter count1(list, wxEVT_COMMAND_LIST_ITEM_FOCUSED);
205 EventCounter count2(list, wxEVT_COMMAND_LIST_ITEM_ACTIVATED);
206 EventCounter count3(list, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK);
207
208 wxUIActionSimulator sim;
209
210 wxRect pos;
211 list->GetItemRect(0, pos);
212
213 //We move in slightly so we are not on the edge
214 wxPoint point = list->ClientToScreen(pos.GetPosition()) + wxPoint(2, 2);
215
216 sim.MouseMove(point);
217 wxYield();
218
219 sim.MouseClick();
220 wxYield();
221
222 sim.MouseDblClick();
223 wxYield();
224
225 sim.MouseClick(wxMOUSE_BTN_RIGHT);
226 wxYield();
227
228 // when the first item was selected the focus changes to it, but not
229 // on subsequent clicks
230 CPPUNIT_ASSERT_EQUAL(1, frame->GetEventCount(wxEVT_COMMAND_LIST_ITEM_FOCUSED));
231 CPPUNIT_ASSERT_EQUAL(1, frame->GetEventCount(wxEVT_COMMAND_LIST_ITEM_SELECTED));
232 CPPUNIT_ASSERT_EQUAL(1, frame->GetEventCount(wxEVT_COMMAND_LIST_ITEM_ACTIVATED));
233 CPPUNIT_ASSERT_EQUAL(1, frame->GetEventCount(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK));
234
235 //tidy up when we are finished
236 list->ClearAll();
237 #endif // wxUSE_UIACTIONSIMULATOR
238 }
239
240 void ListBaseTestCase::KeyDown()
241 {
242 #if wxUSE_UIACTIONSIMULATOR
243 wxTestableFrame* frame = wxStaticCast(wxTheApp->GetTopWindow(),
244 wxTestableFrame);
245
246 wxListCtrl* const list = GetList();
247
248 EventCounter count(list, wxEVT_COMMAND_LIST_KEY_DOWN);
249
250 wxUIActionSimulator sim;
251
252 list->SetFocus();
253 sim.Text("aAbB");
254 wxYield();
255
256 CPPUNIT_ASSERT_EQUAL(6, frame->GetEventCount());
257 #endif
258 }
259
260 void ListBaseTestCase::DeleteItems()
261 {
262 #ifndef __WXOSX__
263 wxTestableFrame* frame = wxStaticCast(wxTheApp->GetTopWindow(),
264 wxTestableFrame);
265
266 wxListCtrl* const list = GetList();
267
268 EventCounter count(list, wxEVT_COMMAND_LIST_DELETE_ITEM);
269 EventCounter count1(list, wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS);
270
271
272 list->InsertColumn(0, "Column 0", wxLIST_FORMAT_LEFT, 60);
273 list->InsertColumn(1, "Column 1", wxLIST_FORMAT_LEFT, 50);
274 list->InsertColumn(2, "Column 2", wxLIST_FORMAT_LEFT, 40);
275
276 list->InsertItem(0, "Item 0");
277 list->InsertItem(1, "Item 1");
278 list->InsertItem(2, "Item 1");
279
280 list->DeleteItem(0);
281 list->DeleteItem(0);
282 list->DeleteAllItems();
283
284 //Add some new items to tests ClearAll with
285 list->InsertColumn(0, "Column 0");
286 list->InsertItem(0, "Item 0");
287 list->InsertItem(1, "Item 1");
288
289 //Check that ClearAll actually sends a DELETE_ALL_ITEMS event
290 list->ClearAll();
291
292 //ClearAll and DeleteAllItems shouldn't send an event if there was nothing
293 //to clear
294 list->ClearAll();
295 list->DeleteAllItems();
296
297 CPPUNIT_ASSERT_EQUAL(2, frame->GetEventCount(wxEVT_COMMAND_LIST_DELETE_ITEM));
298 CPPUNIT_ASSERT_EQUAL(2, frame->GetEventCount(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS));
299 #endif
300 }
301
302 void ListBaseTestCase::InsertItem()
303 {
304 wxTestableFrame* frame = wxStaticCast(wxTheApp->GetTopWindow(),
305 wxTestableFrame);
306
307 wxListCtrl* const list = GetList();
308
309 EventCounter count(list, wxEVT_COMMAND_LIST_INSERT_ITEM);
310
311 list->InsertColumn(0, "Column 0", wxLIST_FORMAT_LEFT, 60);
312
313 wxListItem item;
314 item.SetId(0);
315 item.SetText("some text");
316
317 list->InsertItem(item);
318 list->InsertItem(1, "more text");
319
320 CPPUNIT_ASSERT_EQUAL(2, frame->GetEventCount(wxEVT_COMMAND_LIST_INSERT_ITEM));
321 }
322
323 void ListBaseTestCase::Find()
324 {
325 wxListCtrl* const list = GetList();
326
327 // set up for the test
328 list->InsertColumn(0, "Column 0");
329 list->InsertColumn(1, "Column 1");
330
331 list->InsertItem(0, "Item 0");
332 list->SetItem(0, 1, "first column");
333
334 list->InsertItem(1, "Item 1");
335 list->SetItem(1, 1, "first column");
336
337 list->InsertItem(2, "Item 40");
338 list->SetItem(2, 1, "first column");
339
340 list->InsertItem(3, "ITEM 01");
341 list->SetItem(3, 1, "first column");
342
343 CPPUNIT_ASSERT_EQUAL(1, list->FindItem(-1, "Item 1"));
344 CPPUNIT_ASSERT_EQUAL(2, list->FindItem(-1, "Item 4", true));
345 CPPUNIT_ASSERT_EQUAL(2, list->FindItem(1, "Item 40"));
346 CPPUNIT_ASSERT_EQUAL(3, list->FindItem(2, "Item 0", true));
347 }
348
349 void ListBaseTestCase::Visible()
350 {
351 wxListCtrl* const list = GetList();
352
353 list->InsertColumn(0, "Column 0");
354
355 int count = list->GetCountPerPage();
356
357 for( int i = 0; i < count + 10; i++ )
358 {
359 list->InsertItem(i, wxString::Format("string %d", i));
360 }
361
362 CPPUNIT_ASSERT_EQUAL(count + 10, list->GetItemCount());
363 CPPUNIT_ASSERT_EQUAL(0, list->GetTopItem());
364
365 list->EnsureVisible(count + 9);
366
367 CPPUNIT_ASSERT(list->GetTopItem() != 0);
368 }
369
370 void ListBaseTestCase::ItemFormatting()
371 {
372 wxListCtrl* const list = GetList();
373
374 list->InsertColumn(0, "Column 0");
375
376 list->InsertItem(0, "Item 0");
377 list->InsertItem(1, "Item 1");
378 list->InsertItem(2, "Item 2");
379
380 list->SetTextColour(*wxYELLOW);
381 list->SetBackgroundColour(*wxGREEN);
382 list->SetItemTextColour(0, *wxRED);
383 list->SetItemBackgroundColour(1, *wxBLUE);
384
385 CPPUNIT_ASSERT_EQUAL(*wxGREEN, list->GetBackgroundColour());
386 CPPUNIT_ASSERT_EQUAL(*wxBLUE,list->GetItemBackgroundColour(1));
387
388 CPPUNIT_ASSERT_EQUAL(*wxYELLOW, list->GetTextColour());
389 CPPUNIT_ASSERT_EQUAL(*wxRED, list->GetItemTextColour(0));
390 }
391
392 void ListBaseTestCase::EditLabel()
393 {
394 #if wxUSE_UIACTIONSIMULATOR
395 wxListCtrl* const list = GetList();
396
397 list->SetWindowStyleFlag(wxLC_REPORT | wxLC_EDIT_LABELS);
398
399 list->InsertColumn(0, "Column 0");
400
401 list->InsertItem(0, "Item 0");
402 list->InsertItem(1, "Item 1");
403
404 wxTestableFrame* frame = wxStaticCast(wxTheApp->GetTopWindow(),
405 wxTestableFrame);
406
407 EventCounter count(list, wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT);
408 EventCounter count1(list, wxEVT_COMMAND_LIST_END_LABEL_EDIT);
409
410 wxUIActionSimulator sim;
411
412 list->EditLabel(0);
413
414 sim.Text("sometext");
415 sim.Char(WXK_RETURN);
416
417 wxYield();
418
419 CPPUNIT_ASSERT_EQUAL(1, frame->GetEventCount(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT));
420 CPPUNIT_ASSERT_EQUAL(1, frame->GetEventCount(wxEVT_COMMAND_LIST_END_LABEL_EDIT));
421 #endif
422 }
423
424 void ListBaseTestCase::ImageList()
425 {
426 wxListCtrl* const list = GetList();
427
428 wxSize size(32, 32);
429
430 wxImageList* imglist = new wxImageList(size.x, size.y);
431 imglist->Add(wxArtProvider::GetIcon(wxART_INFORMATION, wxART_OTHER, size));
432 imglist->Add(wxArtProvider::GetIcon(wxART_QUESTION, wxART_OTHER, size));
433 imglist->Add(wxArtProvider::GetIcon(wxART_WARNING, wxART_OTHER, size));
434
435 list->AssignImageList(imglist, wxIMAGE_LIST_NORMAL);
436
437 CPPUNIT_ASSERT_EQUAL(imglist, list->GetImageList(wxIMAGE_LIST_NORMAL));
438 }
439
440 namespace
441 {
442 //From the sample but fixed so it actually inverts
443 int wxCALLBACK
444 MyCompareFunction(long item1, long item2, wxIntPtr WXUNUSED(sortData))
445 {
446 // inverse the order
447 if (item1 < item2)
448 return 1;
449 if (item1 > item2)
450 return -1;
451
452 return 0;
453 }
454
455 }
456
457 void ListBaseTestCase::Sort()
458 {
459 wxListCtrl* const list = GetList();
460
461 list->InsertColumn(0, "Column 0");
462
463 list->InsertItem(0, "Item 0");
464 list->SetItemData(0, 0);
465 list->InsertItem(1, "Item 1");
466 list->SetItemData(1, 1);
467
468 list->SortItems(MyCompareFunction, 0);
469
470 CPPUNIT_ASSERT_EQUAL("Item 1", list->GetItemText(0));
471 CPPUNIT_ASSERT_EQUAL("Item 0", list->GetItemText(1));
472 }
473
474 #endif //wxUSE_LISTCTRL