]> git.saurik.com Git - wxWidgets.git/blob - tests/controls/listboxtest.cpp
Add new wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED event. Implement for all backends, exten...
[wxWidgets.git] / tests / controls / listboxtest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/listbox.cpp
3 // Purpose: wxListBox unit test
4 // Author: Steven Lamerton
5 // Created: 2010-06-29
6 // RCS-ID: $Id$
7 // Copyright: (c) 2010 Steven Lamerton
8 ///////////////////////////////////////////////////////////////////////////////
9
10 #include "testprec.h"
11
12 #if wxUSE_LISTBOX
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #ifndef WX_PRECOMP
19 #include "wx/app.h"
20 #include "wx/listbox.h"
21 #endif // WX_PRECOMP
22
23 #include "itemcontainertest.h"
24 #include "testableframe.h"
25 #include "wx/uiaction.h"
26
27 class ListBoxTestCase : public ItemContainerTestCase, public CppUnit::TestCase
28 {
29 public:
30 ListBoxTestCase() { }
31
32 virtual void setUp();
33 virtual void tearDown();
34
35 private:
36 virtual wxItemContainer *GetContainer() const { return m_list; }
37 virtual wxWindow *GetContainerWindow() const { return m_list; }
38
39 CPPUNIT_TEST_SUITE( ListBoxTestCase );
40 wxITEM_CONTAINER_TESTS();
41 CPPUNIT_TEST( Sort );
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
48 #ifdef __WXMSW__
49 CPPUNIT_TEST( PseudoTest_OwnerDrawn );
50 CPPUNIT_TEST( Sort );
51 CPPUNIT_TEST( MultipleSelect );
52 WXUISIM_TEST( ClickEvents );
53 WXUISIM_TEST( ClickNotOnItem );
54 CPPUNIT_TEST( HitTest );
55 #endif
56 CPPUNIT_TEST_SUITE_END();
57
58 void Sort();
59 void MultipleSelect();
60 void ClickEvents();
61 void ClickNotOnItem();
62 void HitTest();
63 void PseudoTest_OwnerDrawn() { ms_ownerdrawn = true; }
64
65 static bool ms_ownerdrawn;
66
67 wxListBox* m_list;
68
69 DECLARE_NO_COPY_CLASS(ListBoxTestCase)
70 };
71
72 // register in the unnamed registry so that these tests are run by default
73 CPPUNIT_TEST_SUITE_REGISTRATION( ListBoxTestCase );
74
75 // also include in its own registry so that these tests can be run alone
76 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ListBoxTestCase, "ListBoxTestCase" );
77
78 //initialise the static variable
79 bool ListBoxTestCase::ms_ownerdrawn = false;
80
81 void ListBoxTestCase::setUp()
82 {
83 if( ms_ownerdrawn )
84 {
85 m_list = new wxListBox(wxTheApp->GetTopWindow(), wxID_ANY,
86 wxDefaultPosition, wxSize(300, 200), 0, NULL,
87 wxLB_OWNERDRAW);
88 }
89 else
90 {
91 m_list = new wxListBox(wxTheApp->GetTopWindow(), wxID_ANY,
92 wxDefaultPosition, wxSize(300, 200));
93 }
94 }
95
96 void ListBoxTestCase::tearDown()
97 {
98 wxDELETE(m_list);
99 }
100
101 void ListBoxTestCase::Sort()
102 {
103 #ifndef __WXOSX__
104 wxDELETE(m_list);
105 m_list = new wxListBox(wxTheApp->GetTopWindow(), wxID_ANY,
106 wxDefaultPosition, wxDefaultSize, 0, 0,
107 wxLB_SORT);
108
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");
116
117 m_list->Append(testitems);
118
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));
125
126 m_list->Append("a");
127
128 CPPUNIT_ASSERT_EQUAL("a", m_list->GetString(0));
129 #endif
130 }
131
132 void ListBoxTestCase::MultipleSelect()
133 {
134 wxDELETE(m_list);
135 m_list = new wxListBox(wxTheApp->GetTopWindow(), wxID_ANY,
136 wxDefaultPosition, wxDefaultSize, 0, 0,
137 wxLB_MULTIPLE);
138
139 wxArrayString testitems;
140 testitems.Add("item 0");
141 testitems.Add("item 1");
142 testitems.Add("item 2");
143 testitems.Add("item 3");
144
145 m_list->Append(testitems);
146
147 m_list->SetSelection(0);
148
149 wxArrayInt selected;
150 m_list->GetSelections(selected);
151
152 CPPUNIT_ASSERT_EQUAL(1, selected.Count());
153 CPPUNIT_ASSERT_EQUAL(0, selected.Item(0));
154
155 m_list->SetSelection(2);
156
157 m_list->GetSelections(selected);
158
159 CPPUNIT_ASSERT_EQUAL(2, selected.Count());
160 CPPUNIT_ASSERT_EQUAL(2, selected.Item(1));
161
162 m_list->Deselect(0);
163
164 m_list->GetSelections(selected);
165
166 CPPUNIT_ASSERT_EQUAL(1, selected.Count());
167 CPPUNIT_ASSERT_EQUAL(2, selected.Item(0));
168
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));
173
174 m_list->SetSelection(0);
175 m_list->SetSelection(wxNOT_FOUND);
176
177 m_list->GetSelections(selected);
178 CPPUNIT_ASSERT_EQUAL(0, selected.Count());
179 }
180
181 void ListBoxTestCase::ClickEvents()
182 {
183 #if wxUSE_UIACTIONSIMULATOR
184 wxTestableFrame* frame = wxStaticCast(wxTheApp->GetTopWindow(),
185 wxTestableFrame);
186
187 EventCounter count(frame, wxEVT_COMMAND_LISTBOX_SELECTED);
188 EventCounter count1(frame, wxEVT_COMMAND_LISTBOX_DOUBLECLICKED);
189
190 wxUIActionSimulator sim;
191
192 wxArrayString testitems;
193 testitems.Add("item 0");
194 testitems.Add("item 1");
195 testitems.Add("item 2");
196
197 m_list->Append(testitems);
198
199 m_list->Update();
200 m_list->Refresh();
201
202 sim.MouseMove(m_list->ClientToScreen(wxPoint(10, 10)));
203 wxYield();
204
205 sim.MouseClick();
206 wxYield();
207
208 CPPUNIT_ASSERT_EQUAL(1, frame->GetEventCount());
209
210 sim.MouseDblClick();
211 wxYield();
212
213 CPPUNIT_ASSERT_EQUAL(1, frame->GetEventCount());
214 #endif
215 }
216
217 void ListBoxTestCase::ClickNotOnItem()
218 {
219 #if wxUSE_UIACTIONSIMULATOR
220 wxTestableFrame* frame = wxStaticCast(wxTheApp->GetTopWindow(),
221 wxTestableFrame);
222
223 EventCounter count(frame, wxEVT_COMMAND_LISTBOX_SELECTED);
224 EventCounter count1(frame, wxEVT_COMMAND_LISTBOX_DOUBLECLICKED);
225
226 wxUIActionSimulator sim;
227
228 wxArrayString testitems;
229 testitems.Add("item 0");
230 testitems.Add("item 1");
231 testitems.Add("item 2");
232
233 m_list->Append(testitems);
234
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);
241
242 m_list->Update();
243 m_list->Refresh();
244
245 sim.MouseMove(m_list->ClientToScreen(wxPoint(m_list->GetSize().x - 10, m_list->GetSize().y - 10)));
246 wxYield();
247
248 sim.MouseClick();
249 wxYield();
250
251 sim.MouseDblClick();
252 wxYield();
253
254 //If we are not clicking on an item we shouldn't have any events
255 CPPUNIT_ASSERT_EQUAL(0, frame->GetEventCount());
256 #endif
257 }
258
259 void ListBoxTestCase::HitTest()
260 {
261 wxArrayString testitems;
262 testitems.Add("item 0");
263 testitems.Add("item 1");
264 testitems.Add("item 2");
265
266 m_list->Append(testitems);
267
268 #ifdef __WXGTK__
269 // The control needs to be realized for HitTest() to work.
270 wxYield();
271 #endif
272
273 CPPUNIT_ASSERT_EQUAL( 0, m_list->HitTest(5, 5) );
274
275 CPPUNIT_ASSERT_EQUAL( wxNOT_FOUND, m_list->HitTest(290, 190) );
276 }
277
278 #endif //wxUSE_LISTBOX