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 it's 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));
175 void ListBoxTestCase::ClickEvents()
177 #if wxUSE_UIACTIONSIMULATOR
178 wxTestableFrame
* frame
= wxStaticCast(wxTheApp
->GetTopWindow(),
181 EventCounter
count(frame
, wxEVT_COMMAND_LISTBOX_SELECTED
);
182 EventCounter
count1(frame
, wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
);
184 wxUIActionSimulator sim
;
186 wxArrayString testitems
;
187 testitems
.Add("item 0");
188 testitems
.Add("item 1");
189 testitems
.Add("item 2");
191 m_list
->Append(testitems
);
196 sim
.MouseMove(m_list
->ClientToScreen(wxPoint(10, 10)));
202 CPPUNIT_ASSERT_EQUAL(1, frame
->GetEventCount());
207 CPPUNIT_ASSERT_EQUAL(1, frame
->GetEventCount());
211 void ListBoxTestCase::ClickNotOnItem()
213 #if wxUSE_UIACTIONSIMULATOR
214 wxTestableFrame
* frame
= wxStaticCast(wxTheApp
->GetTopWindow(),
217 EventCounter
count(frame
, wxEVT_COMMAND_LISTBOX_SELECTED
);
218 EventCounter
count1(frame
, wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
);
220 wxUIActionSimulator sim
;
222 wxArrayString testitems
;
223 testitems
.Add("item 0");
224 testitems
.Add("item 1");
225 testitems
.Add("item 2");
227 m_list
->Append(testitems
);
229 // It is important to set a valid selection: if the control doesn't have
230 // any, clicking anywhere in it, even outside of any item, selects the
231 // first item in the control under GTK resulting in a selection changed
232 // event. This is not a wx bug, just the native platform behaviour so
233 // simply avoid it by starting with a valid selection.
234 m_list
->SetSelection(0);
239 sim
.MouseMove(m_list
->ClientToScreen(wxPoint(m_list
->GetSize().x
- 10, m_list
->GetSize().y
- 10)));
248 //If we are not clicking on an item we shouldn't have any events
249 CPPUNIT_ASSERT_EQUAL(0, frame
->GetEventCount());
253 void ListBoxTestCase::HitTest()
255 wxArrayString testitems
;
256 testitems
.Add("item 0");
257 testitems
.Add("item 1");
258 testitems
.Add("item 2");
260 m_list
->Append(testitems
);
263 // The control needs to be realized for HitTest() to work.
267 CPPUNIT_ASSERT_EQUAL( 0, m_list
->HitTest(5, 5) );
269 CPPUNIT_ASSERT_EQUAL( wxNOT_FOUND
, m_list
->HitTest(290, 190) );
272 #endif //wxUSE_LISTBOX