listbasetest.cpp 13 KB

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