1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/listbox.cpp
3 // Purpose: wxListBox unit test
4 // Author: Steven Lamerton
7 // Copyright: (c) 2010 Steven Lamerton
8 ///////////////////////////////////////////////////////////////////////////////
20 #include "wx/listbox.h"
23 #include "itemcontainertest.h"
24 #include "testableframe.h"
25 #include "wx/uiaction.h"
27 class ListBoxTestCase
: public ItemContainerTestCase
, public CppUnit::TestCase
33 virtual void tearDown();
36 virtual wxItemContainer
*GetContainer() const { return m_list
; }
37 virtual wxWindow
*GetContainerWindow() const { return m_list
; }
39 CPPUNIT_TEST_SUITE( ListBoxTestCase
);
40 wxITEM_CONTAINER_TESTS();
42 CPPUNIT_TEST( MultipleSelect
);
43 WXUISIM_TEST( ClickEvents
);
44 WXUISIM_TEST( ClickNotOnItem
);
45 CPPUNIT_TEST( HitTest
);
46 //We also run all tests as an ownerdrawn list box. We do not need to
47 //run the wxITEM_CONTAINER_TESTS as they are tested with wxCheckListBox
49 CPPUNIT_TEST( PseudoTest_OwnerDrawn
);
51 CPPUNIT_TEST( MultipleSelect
);
52 WXUISIM_TEST( ClickEvents
);
53 WXUISIM_TEST( ClickNotOnItem
);
54 CPPUNIT_TEST( HitTest
);
56 CPPUNIT_TEST_SUITE_END();
59 void MultipleSelect();
61 void ClickNotOnItem();
63 void PseudoTest_OwnerDrawn() { ms_ownerdrawn
= true; }
65 static bool ms_ownerdrawn
;
69 DECLARE_NO_COPY_CLASS(ListBoxTestCase
)
72 // register in the unnamed registry so that these tests are run by default
73 CPPUNIT_TEST_SUITE_REGISTRATION( ListBoxTestCase
);
75 // also include in its own registry so that these tests can be run alone
76 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ListBoxTestCase
, "ListBoxTestCase" );
78 //initialise the static variable
79 bool ListBoxTestCase::ms_ownerdrawn
= false;
81 void ListBoxTestCase::setUp()
85 m_list
= new wxListBox(wxTheApp
->GetTopWindow(), wxID_ANY
,
86 wxDefaultPosition
, wxSize(300, 200), 0, NULL
,
91 m_list
= new wxListBox(wxTheApp
->GetTopWindow(), wxID_ANY
,
92 wxDefaultPosition
, wxSize(300, 200));
96 void ListBoxTestCase::tearDown()
101 void ListBoxTestCase::Sort()
105 m_list
= new wxListBox(wxTheApp
->GetTopWindow(), wxID_ANY
,
106 wxDefaultPosition
, wxDefaultSize
, 0, 0,
109 wxArrayString testitems
;
110 testitems
.Add("aaa");
111 testitems
.Add("Aaa");
112 testitems
.Add("aba");
113 testitems
.Add("aaab");
114 testitems
.Add("aab");
115 testitems
.Add("AAA");
117 m_list
->Append(testitems
);
119 CPPUNIT_ASSERT_EQUAL("AAA", m_list
->GetString(0));
120 CPPUNIT_ASSERT_EQUAL("Aaa", m_list
->GetString(1));
121 CPPUNIT_ASSERT_EQUAL("aaa", m_list
->GetString(2));
122 CPPUNIT_ASSERT_EQUAL("aaab", m_list
->GetString(3));
123 CPPUNIT_ASSERT_EQUAL("aab", m_list
->GetString(4));
124 CPPUNIT_ASSERT_EQUAL("aba", m_list
->GetString(5));
128 CPPUNIT_ASSERT_EQUAL("a", m_list
->GetString(0));
132 void ListBoxTestCase::MultipleSelect()
135 m_list
= new wxListBox(wxTheApp
->GetTopWindow(), wxID_ANY
,
136 wxDefaultPosition
, wxDefaultSize
, 0, 0,
139 wxArrayString testitems
;
140 testitems
.Add("item 0");
141 testitems
.Add("item 1");
142 testitems
.Add("item 2");
143 testitems
.Add("item 3");
145 m_list
->Append(testitems
);
147 m_list
->SetSelection(0);
150 m_list
->GetSelections(selected
);
152 CPPUNIT_ASSERT_EQUAL(1, selected
.Count());
153 CPPUNIT_ASSERT_EQUAL(0, selected
.Item(0));
155 m_list
->SetSelection(2);
157 m_list
->GetSelections(selected
);
159 CPPUNIT_ASSERT_EQUAL(2, selected
.Count());
160 CPPUNIT_ASSERT_EQUAL(2, selected
.Item(1));
164 m_list
->GetSelections(selected
);
166 CPPUNIT_ASSERT_EQUAL(1, selected
.Count());
167 CPPUNIT_ASSERT_EQUAL(2, selected
.Item(0));
169 CPPUNIT_ASSERT(!m_list
->IsSelected(0));
170 CPPUNIT_ASSERT(!m_list
->IsSelected(1));
171 CPPUNIT_ASSERT(m_list
->IsSelected(2));
172 CPPUNIT_ASSERT(!m_list
->IsSelected(3));
174 m_list
->SetSelection(0);
175 m_list
->SetSelection(wxNOT_FOUND
);
177 m_list
->GetSelections(selected
);
178 CPPUNIT_ASSERT_EQUAL(0, selected
.Count());
181 void ListBoxTestCase::ClickEvents()
183 #if wxUSE_UIACTIONSIMULATOR
184 wxTestableFrame
* frame
= wxStaticCast(wxTheApp
->GetTopWindow(),
187 EventCounter
selected(frame
, wxEVT_COMMAND_LISTBOX_SELECTED
);
188 EventCounter
dclicked(frame
, wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
);
190 wxUIActionSimulator sim
;
192 wxArrayString testitems
;
193 testitems
.Add("item 0");
194 testitems
.Add("item 1");
195 testitems
.Add("item 2");
197 m_list
->Append(testitems
);
202 sim
.MouseMove(m_list
->ClientToScreen(wxPoint(10, 10)));
208 CPPUNIT_ASSERT_EQUAL(1, selected
.GetCount());
213 CPPUNIT_ASSERT_EQUAL(1, dclicked
.GetCount());
217 void ListBoxTestCase::ClickNotOnItem()
219 #if wxUSE_UIACTIONSIMULATOR
220 wxTestableFrame
* frame
= wxStaticCast(wxTheApp
->GetTopWindow(),
223 EventCounter
selected(frame
, wxEVT_COMMAND_LISTBOX_SELECTED
);
224 EventCounter
dclicked(frame
, wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
);
226 wxUIActionSimulator sim
;
228 wxArrayString testitems
;
229 testitems
.Add("item 0");
230 testitems
.Add("item 1");
231 testitems
.Add("item 2");
233 m_list
->Append(testitems
);
235 // It is important to set a valid selection: if the control doesn't have
236 // any, clicking anywhere in it, even outside of any item, selects the
237 // first item in the control under GTK resulting in a selection changed
238 // event. This is not a wx bug, just the native platform behaviour so
239 // simply avoid it by starting with a valid selection.
240 m_list
->SetSelection(0);
245 sim
.MouseMove(m_list
->ClientToScreen(wxPoint(m_list
->GetSize().x
- 10, m_list
->GetSize().y
- 10)));
254 //If we are not clicking on an item we shouldn't have any events
255 CPPUNIT_ASSERT_EQUAL(0, selected
.GetCount());
256 CPPUNIT_ASSERT_EQUAL(0, dclicked
.GetCount());
260 void ListBoxTestCase::HitTest()
262 wxArrayString testitems
;
263 testitems
.Add("item 0");
264 testitems
.Add("item 1");
265 testitems
.Add("item 2");
267 m_list
->Append(testitems
);
270 // The control needs to be realized for HitTest() to work.
274 CPPUNIT_ASSERT_EQUAL( 0, m_list
->HitTest(5, 5) );
276 CPPUNIT_ASSERT_EQUAL( wxNOT_FOUND
, m_list
->HitTest(290, 190) );
279 #endif //wxUSE_LISTBOX