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