1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/listbox.cpp
3 // Purpose: wxListBox unit test
4 // Author: Steven Lamerton
6 // Copyright: (c) 2010 Steven Lamerton
7 ///////////////////////////////////////////////////////////////////////////////
19 #include "wx/listbox.h"
22 #include "itemcontainertest.h"
23 #include "testableframe.h"
24 #include "wx/uiaction.h"
26 class ListBoxTestCase
: public ItemContainerTestCase
, public CppUnit::TestCase
32 virtual void tearDown();
35 virtual wxItemContainer
*GetContainer() const { return m_list
; }
36 virtual wxWindow
*GetContainerWindow() const { return m_list
; }
38 CPPUNIT_TEST_SUITE( ListBoxTestCase
);
39 wxITEM_CONTAINER_TESTS();
41 CPPUNIT_TEST( MultipleSelect
);
42 WXUISIM_TEST( ClickEvents
);
43 WXUISIM_TEST( ClickNotOnItem
);
44 CPPUNIT_TEST( HitTest
);
45 //We also run all tests as an ownerdrawn list box. We do not need to
46 //run the wxITEM_CONTAINER_TESTS as they are tested with wxCheckListBox
48 CPPUNIT_TEST( PseudoTest_OwnerDrawn
);
50 CPPUNIT_TEST( MultipleSelect
);
51 WXUISIM_TEST( ClickEvents
);
52 WXUISIM_TEST( ClickNotOnItem
);
53 CPPUNIT_TEST( HitTest
);
55 CPPUNIT_TEST_SUITE_END();
58 void MultipleSelect();
60 void ClickNotOnItem();
62 void PseudoTest_OwnerDrawn() { ms_ownerdrawn
= true; }
64 static bool ms_ownerdrawn
;
68 DECLARE_NO_COPY_CLASS(ListBoxTestCase
)
71 // register in the unnamed registry so that these tests are run by default
72 CPPUNIT_TEST_SUITE_REGISTRATION( ListBoxTestCase
);
74 // also include in its own registry so that these tests can be run alone
75 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ListBoxTestCase
, "ListBoxTestCase" );
77 //initialise the static variable
78 bool ListBoxTestCase::ms_ownerdrawn
= false;
80 void ListBoxTestCase::setUp()
84 m_list
= new wxListBox(wxTheApp
->GetTopWindow(), wxID_ANY
,
85 wxDefaultPosition
, wxSize(300, 200), 0, NULL
,
90 m_list
= new wxListBox(wxTheApp
->GetTopWindow(), wxID_ANY
,
91 wxDefaultPosition
, wxSize(300, 200));
95 void ListBoxTestCase::tearDown()
100 void ListBoxTestCase::Sort()
104 m_list
= new wxListBox(wxTheApp
->GetTopWindow(), wxID_ANY
,
105 wxDefaultPosition
, wxDefaultSize
, 0, 0,
108 wxArrayString testitems
;
109 testitems
.Add("aaa");
110 testitems
.Add("Aaa");
111 testitems
.Add("aba");
112 testitems
.Add("aaab");
113 testitems
.Add("aab");
114 testitems
.Add("AAA");
116 m_list
->Append(testitems
);
118 CPPUNIT_ASSERT_EQUAL("AAA", m_list
->GetString(0));
119 CPPUNIT_ASSERT_EQUAL("Aaa", m_list
->GetString(1));
120 CPPUNIT_ASSERT_EQUAL("aaa", m_list
->GetString(2));
121 CPPUNIT_ASSERT_EQUAL("aaab", m_list
->GetString(3));
122 CPPUNIT_ASSERT_EQUAL("aab", m_list
->GetString(4));
123 CPPUNIT_ASSERT_EQUAL("aba", m_list
->GetString(5));
127 CPPUNIT_ASSERT_EQUAL("a", m_list
->GetString(0));
131 void ListBoxTestCase::MultipleSelect()
134 m_list
= new wxListBox(wxTheApp
->GetTopWindow(), wxID_ANY
,
135 wxDefaultPosition
, wxDefaultSize
, 0, 0,
138 wxArrayString testitems
;
139 testitems
.Add("item 0");
140 testitems
.Add("item 1");
141 testitems
.Add("item 2");
142 testitems
.Add("item 3");
144 m_list
->Append(testitems
);
146 m_list
->SetSelection(0);
149 m_list
->GetSelections(selected
);
151 CPPUNIT_ASSERT_EQUAL(1, selected
.Count());
152 CPPUNIT_ASSERT_EQUAL(0, selected
.Item(0));
154 m_list
->SetSelection(2);
156 m_list
->GetSelections(selected
);
158 CPPUNIT_ASSERT_EQUAL(2, selected
.Count());
159 CPPUNIT_ASSERT_EQUAL(2, selected
.Item(1));
163 m_list
->GetSelections(selected
);
165 CPPUNIT_ASSERT_EQUAL(1, selected
.Count());
166 CPPUNIT_ASSERT_EQUAL(2, selected
.Item(0));
168 CPPUNIT_ASSERT(!m_list
->IsSelected(0));
169 CPPUNIT_ASSERT(!m_list
->IsSelected(1));
170 CPPUNIT_ASSERT(m_list
->IsSelected(2));
171 CPPUNIT_ASSERT(!m_list
->IsSelected(3));
173 m_list
->SetSelection(0);
174 m_list
->SetSelection(wxNOT_FOUND
);
176 m_list
->GetSelections(selected
);
177 CPPUNIT_ASSERT_EQUAL(0, selected
.Count());
180 void ListBoxTestCase::ClickEvents()
182 #if wxUSE_UIACTIONSIMULATOR
183 wxTestableFrame
* frame
= wxStaticCast(wxTheApp
->GetTopWindow(),
186 EventCounter
selected(frame
, wxEVT_LISTBOX
);
187 EventCounter
dclicked(frame
, wxEVT_LISTBOX_DCLICK
);
189 wxUIActionSimulator sim
;
191 wxArrayString testitems
;
192 testitems
.Add("item 0");
193 testitems
.Add("item 1");
194 testitems
.Add("item 2");
196 m_list
->Append(testitems
);
201 sim
.MouseMove(m_list
->ClientToScreen(wxPoint(10, 10)));
207 CPPUNIT_ASSERT_EQUAL(1, selected
.GetCount());
212 CPPUNIT_ASSERT_EQUAL(1, dclicked
.GetCount());
216 void ListBoxTestCase::ClickNotOnItem()
218 #if wxUSE_UIACTIONSIMULATOR
219 wxTestableFrame
* frame
= wxStaticCast(wxTheApp
->GetTopWindow(),
222 EventCounter
selected(frame
, wxEVT_LISTBOX
);
223 EventCounter
dclicked(frame
, wxEVT_LISTBOX_DCLICK
);
225 wxUIActionSimulator sim
;
227 wxArrayString testitems
;
228 testitems
.Add("item 0");
229 testitems
.Add("item 1");
230 testitems
.Add("item 2");
232 m_list
->Append(testitems
);
234 // It is important to set a valid selection: if the control doesn't have
235 // any, clicking anywhere in it, even outside of any item, selects the
236 // first item in the control under GTK resulting in a selection changed
237 // event. This is not a wx bug, just the native platform behaviour so
238 // simply avoid it by starting with a valid selection.
239 m_list
->SetSelection(0);
244 sim
.MouseMove(m_list
->ClientToScreen(wxPoint(m_list
->GetSize().x
- 10, m_list
->GetSize().y
- 10)));
253 //If we are not clicking on an item we shouldn't have any events
254 CPPUNIT_ASSERT_EQUAL(0, selected
.GetCount());
255 CPPUNIT_ASSERT_EQUAL(0, dclicked
.GetCount());
259 void ListBoxTestCase::HitTest()
261 wxArrayString testitems
;
262 testitems
.Add("item 0");
263 testitems
.Add("item 1");
264 testitems
.Add("item 2");
266 m_list
->Append(testitems
);
269 // The control needs to be realized for HitTest() to work.
273 CPPUNIT_ASSERT_EQUAL( 0, m_list
->HitTest(5, 5) );
275 CPPUNIT_ASSERT_EQUAL( wxNOT_FOUND
, m_list
->HitTest(290, 190) );
278 #endif //wxUSE_LISTBOX