listboxtest.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/controls/listbox.cpp
  3. // Purpose: wxListBox unit test
  4. // Author: Steven Lamerton
  5. // Created: 2010-06-29
  6. // Copyright: (c) 2010 Steven Lamerton
  7. ///////////////////////////////////////////////////////////////////////////////
  8. #include "testprec.h"
  9. #if wxUSE_LISTBOX
  10. #ifdef __BORLANDC__
  11. #pragma hdrstop
  12. #endif
  13. #ifndef WX_PRECOMP
  14. #include "wx/app.h"
  15. #include "wx/listbox.h"
  16. #endif // WX_PRECOMP
  17. #include "itemcontainertest.h"
  18. #include "testableframe.h"
  19. #include "wx/uiaction.h"
  20. class ListBoxTestCase : public ItemContainerTestCase, public CppUnit::TestCase
  21. {
  22. public:
  23. ListBoxTestCase() { }
  24. virtual void setUp();
  25. virtual void tearDown();
  26. private:
  27. virtual wxItemContainer *GetContainer() const { return m_list; }
  28. virtual wxWindow *GetContainerWindow() const { return m_list; }
  29. CPPUNIT_TEST_SUITE( ListBoxTestCase );
  30. wxITEM_CONTAINER_TESTS();
  31. CPPUNIT_TEST( Sort );
  32. CPPUNIT_TEST( MultipleSelect );
  33. WXUISIM_TEST( ClickEvents );
  34. WXUISIM_TEST( ClickNotOnItem );
  35. CPPUNIT_TEST( HitTest );
  36. //We also run all tests as an ownerdrawn list box. We do not need to
  37. //run the wxITEM_CONTAINER_TESTS as they are tested with wxCheckListBox
  38. #ifdef __WXMSW__
  39. CPPUNIT_TEST( PseudoTest_OwnerDrawn );
  40. CPPUNIT_TEST( Sort );
  41. CPPUNIT_TEST( MultipleSelect );
  42. WXUISIM_TEST( ClickEvents );
  43. WXUISIM_TEST( ClickNotOnItem );
  44. CPPUNIT_TEST( HitTest );
  45. #endif
  46. CPPUNIT_TEST_SUITE_END();
  47. void Sort();
  48. void MultipleSelect();
  49. void ClickEvents();
  50. void ClickNotOnItem();
  51. void HitTest();
  52. void PseudoTest_OwnerDrawn() { ms_ownerdrawn = true; }
  53. static bool ms_ownerdrawn;
  54. wxListBox* m_list;
  55. DECLARE_NO_COPY_CLASS(ListBoxTestCase)
  56. };
  57. // register in the unnamed registry so that these tests are run by default
  58. CPPUNIT_TEST_SUITE_REGISTRATION( ListBoxTestCase );
  59. // also include in its own registry so that these tests can be run alone
  60. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ListBoxTestCase, "ListBoxTestCase" );
  61. //initialise the static variable
  62. bool ListBoxTestCase::ms_ownerdrawn = false;
  63. void ListBoxTestCase::setUp()
  64. {
  65. if( ms_ownerdrawn )
  66. {
  67. m_list = new wxListBox(wxTheApp->GetTopWindow(), wxID_ANY,
  68. wxDefaultPosition, wxSize(300, 200), 0, NULL,
  69. wxLB_OWNERDRAW);
  70. }
  71. else
  72. {
  73. m_list = new wxListBox(wxTheApp->GetTopWindow(), wxID_ANY,
  74. wxDefaultPosition, wxSize(300, 200));
  75. }
  76. }
  77. void ListBoxTestCase::tearDown()
  78. {
  79. wxDELETE(m_list);
  80. }
  81. void ListBoxTestCase::Sort()
  82. {
  83. #ifndef __WXOSX__
  84. wxDELETE(m_list);
  85. m_list = new wxListBox(wxTheApp->GetTopWindow(), wxID_ANY,
  86. wxDefaultPosition, wxDefaultSize, 0, 0,
  87. wxLB_SORT);
  88. wxArrayString testitems;
  89. testitems.Add("aaa");
  90. testitems.Add("Aaa");
  91. testitems.Add("aba");
  92. testitems.Add("aaab");
  93. testitems.Add("aab");
  94. testitems.Add("AAA");
  95. m_list->Append(testitems);
  96. CPPUNIT_ASSERT_EQUAL("AAA", m_list->GetString(0));
  97. CPPUNIT_ASSERT_EQUAL("Aaa", m_list->GetString(1));
  98. CPPUNIT_ASSERT_EQUAL("aaa", m_list->GetString(2));
  99. CPPUNIT_ASSERT_EQUAL("aaab", m_list->GetString(3));
  100. CPPUNIT_ASSERT_EQUAL("aab", m_list->GetString(4));
  101. CPPUNIT_ASSERT_EQUAL("aba", m_list->GetString(5));
  102. m_list->Append("a");
  103. CPPUNIT_ASSERT_EQUAL("a", m_list->GetString(0));
  104. #endif
  105. }
  106. void ListBoxTestCase::MultipleSelect()
  107. {
  108. wxDELETE(m_list);
  109. m_list = new wxListBox(wxTheApp->GetTopWindow(), wxID_ANY,
  110. wxDefaultPosition, wxDefaultSize, 0, 0,
  111. wxLB_MULTIPLE);
  112. wxArrayString testitems;
  113. testitems.Add("item 0");
  114. testitems.Add("item 1");
  115. testitems.Add("item 2");
  116. testitems.Add("item 3");
  117. m_list->Append(testitems);
  118. m_list->SetSelection(0);
  119. wxArrayInt selected;
  120. m_list->GetSelections(selected);
  121. CPPUNIT_ASSERT_EQUAL(1, selected.Count());
  122. CPPUNIT_ASSERT_EQUAL(0, selected.Item(0));
  123. m_list->SetSelection(2);
  124. m_list->GetSelections(selected);
  125. CPPUNIT_ASSERT_EQUAL(2, selected.Count());
  126. CPPUNIT_ASSERT_EQUAL(2, selected.Item(1));
  127. m_list->Deselect(0);
  128. m_list->GetSelections(selected);
  129. CPPUNIT_ASSERT_EQUAL(1, selected.Count());
  130. CPPUNIT_ASSERT_EQUAL(2, selected.Item(0));
  131. CPPUNIT_ASSERT(!m_list->IsSelected(0));
  132. CPPUNIT_ASSERT(!m_list->IsSelected(1));
  133. CPPUNIT_ASSERT(m_list->IsSelected(2));
  134. CPPUNIT_ASSERT(!m_list->IsSelected(3));
  135. m_list->SetSelection(0);
  136. m_list->SetSelection(wxNOT_FOUND);
  137. m_list->GetSelections(selected);
  138. CPPUNIT_ASSERT_EQUAL(0, selected.Count());
  139. }
  140. void ListBoxTestCase::ClickEvents()
  141. {
  142. #if wxUSE_UIACTIONSIMULATOR
  143. wxTestableFrame* frame = wxStaticCast(wxTheApp->GetTopWindow(),
  144. wxTestableFrame);
  145. EventCounter selected(frame, wxEVT_LISTBOX);
  146. EventCounter dclicked(frame, wxEVT_LISTBOX_DCLICK);
  147. wxUIActionSimulator sim;
  148. wxArrayString testitems;
  149. testitems.Add("item 0");
  150. testitems.Add("item 1");
  151. testitems.Add("item 2");
  152. m_list->Append(testitems);
  153. m_list->Update();
  154. m_list->Refresh();
  155. sim.MouseMove(m_list->ClientToScreen(wxPoint(10, 10)));
  156. wxYield();
  157. sim.MouseClick();
  158. wxYield();
  159. CPPUNIT_ASSERT_EQUAL(1, selected.GetCount());
  160. sim.MouseDblClick();
  161. wxYield();
  162. CPPUNIT_ASSERT_EQUAL(1, dclicked.GetCount());
  163. #endif
  164. }
  165. void ListBoxTestCase::ClickNotOnItem()
  166. {
  167. #if wxUSE_UIACTIONSIMULATOR
  168. wxTestableFrame* frame = wxStaticCast(wxTheApp->GetTopWindow(),
  169. wxTestableFrame);
  170. EventCounter selected(frame, wxEVT_LISTBOX);
  171. EventCounter dclicked(frame, wxEVT_LISTBOX_DCLICK);
  172. wxUIActionSimulator sim;
  173. wxArrayString testitems;
  174. testitems.Add("item 0");
  175. testitems.Add("item 1");
  176. testitems.Add("item 2");
  177. m_list->Append(testitems);
  178. // It is important to set a valid selection: if the control doesn't have
  179. // any, clicking anywhere in it, even outside of any item, selects the
  180. // first item in the control under GTK resulting in a selection changed
  181. // event. This is not a wx bug, just the native platform behaviour so
  182. // simply avoid it by starting with a valid selection.
  183. m_list->SetSelection(0);
  184. m_list->Update();
  185. m_list->Refresh();
  186. sim.MouseMove(m_list->ClientToScreen(wxPoint(m_list->GetSize().x - 10, m_list->GetSize().y - 10)));
  187. wxYield();
  188. sim.MouseClick();
  189. wxYield();
  190. sim.MouseDblClick();
  191. wxYield();
  192. //If we are not clicking on an item we shouldn't have any events
  193. CPPUNIT_ASSERT_EQUAL(0, selected.GetCount());
  194. CPPUNIT_ASSERT_EQUAL(0, dclicked.GetCount());
  195. #endif
  196. }
  197. void ListBoxTestCase::HitTest()
  198. {
  199. wxArrayString testitems;
  200. testitems.Add("item 0");
  201. testitems.Add("item 1");
  202. testitems.Add("item 2");
  203. m_list->Append(testitems);
  204. #ifdef __WXGTK__
  205. // The control needs to be realized for HitTest() to work.
  206. wxYield();
  207. #endif
  208. CPPUNIT_ASSERT_EQUAL( 0, m_list->HitTest(5, 5) );
  209. CPPUNIT_ASSERT_EQUAL( wxNOT_FOUND, m_list->HitTest(290, 190) );
  210. }
  211. #endif //wxUSE_LISTBOX