]> git.saurik.com Git - wxWidgets.git/blob - tests/controls/itemcontainertest.cpp
Minor fixes to allow web sample to compile
[wxWidgets.git] / tests / controls / itemcontainertest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/itemcontainertest.cpp
3 // Purpose: wxItemContainer 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 #ifndef WX_PRECOMP
13 #include "wx/app.h"
14 #include "wx/ctrlsub.h"
15 #endif // WX_PRECOMP
16
17 #include "wx/scopeguard.h"
18
19 #include "itemcontainertest.h"
20
21 void ItemContainerTestCase::Append()
22 {
23 wxItemContainer * const container = GetContainer();
24
25 container->Append("item 0");
26
27 CPPUNIT_ASSERT_EQUAL("item 0", container->GetString(0));
28
29 wxArrayString testitems;
30 testitems.Add("item 1");
31 testitems.Add("item 2");
32
33 container->Append(testitems);
34
35 CPPUNIT_ASSERT_EQUAL("item 1", container->GetString(1));
36 CPPUNIT_ASSERT_EQUAL("item 2", container->GetString(2));
37
38 wxString arritems[] = { "item 3", "item 4" };
39
40 container->Append(2, arritems);
41
42 CPPUNIT_ASSERT_EQUAL("item 3", container->GetString(3));
43 CPPUNIT_ASSERT_EQUAL("item 4", container->GetString(4));
44 }
45
46 void ItemContainerTestCase::Insert()
47 {
48 wxItemContainer * const container = GetContainer();
49
50 CPPUNIT_ASSERT_EQUAL( 0, container->Insert("item 0", 0) );
51 CPPUNIT_ASSERT_EQUAL("item 0", container->GetString(0));
52
53 wxArrayString testitems;
54 testitems.Add("item 1");
55 testitems.Add("item 2");
56
57 CPPUNIT_ASSERT_EQUAL( 1, container->Insert(testitems, 0) );
58
59 CPPUNIT_ASSERT_EQUAL("item 1", container->GetString(0));
60 CPPUNIT_ASSERT_EQUAL("item 2", container->GetString(1));
61
62 wxString arritems[] = { "item 3", "item 4" };
63
64 CPPUNIT_ASSERT_EQUAL( 2, container->Insert(2, arritems, 1) );
65 CPPUNIT_ASSERT_EQUAL("item 3", container->GetString(1));
66 CPPUNIT_ASSERT_EQUAL("item 4", container->GetString(2));
67 }
68
69 void ItemContainerTestCase::Count()
70 {
71 wxItemContainer * const container = GetContainer();
72
73 CPPUNIT_ASSERT(container->IsEmpty());
74
75 wxArrayString testitems;
76 testitems.Add("item 0");
77 testitems.Add("item 1");
78 testitems.Add("item 2");
79 testitems.Add("item 3");
80
81 container->Append(testitems);
82
83 CPPUNIT_ASSERT(!container->IsEmpty());
84 CPPUNIT_ASSERT_EQUAL(4, container->GetCount());
85
86 container->Delete(0);
87
88 CPPUNIT_ASSERT_EQUAL(3, container->GetCount());
89
90 container->Delete(0);
91 container->Delete(0);
92
93 CPPUNIT_ASSERT_EQUAL(1, container->GetCount());
94
95 container->Insert(testitems, 1);
96
97 CPPUNIT_ASSERT_EQUAL(5, container->GetCount());
98 }
99
100 void ItemContainerTestCase::ItemSelection()
101 {
102 wxItemContainer * const container = GetContainer();
103
104 wxArrayString testitems;
105 testitems.Add("item 0");
106 testitems.Add("item 1");
107 testitems.Add("item 2");
108 testitems.Add("ITEM 2"); // The same as the last one except for case.
109
110 container->Append(testitems);
111
112 container->SetSelection(wxNOT_FOUND);
113 CPPUNIT_ASSERT_EQUAL(wxNOT_FOUND, container->GetSelection());
114 CPPUNIT_ASSERT_EQUAL("", container->GetStringSelection());
115
116 container->SetSelection(1);
117 CPPUNIT_ASSERT_EQUAL(1, container->GetSelection());
118 CPPUNIT_ASSERT_EQUAL("item 1", container->GetStringSelection());
119
120 CPPUNIT_ASSERT( container->SetStringSelection("item 2") );
121 CPPUNIT_ASSERT_EQUAL(2, container->GetSelection());
122 CPPUNIT_ASSERT_EQUAL("item 2", container->GetStringSelection());
123
124 // Check that selecting a non-existent item fails.
125 CPPUNIT_ASSERT( !container->SetStringSelection("bloordyblop") );
126
127 // Check that SetStringSelection() is case-insensitive.
128 CPPUNIT_ASSERT( container->SetStringSelection("ITEM 2") );
129 CPPUNIT_ASSERT_EQUAL(2, container->GetSelection());
130 CPPUNIT_ASSERT_EQUAL("item 2", container->GetStringSelection());
131 }
132
133 void ItemContainerTestCase::FindString()
134 {
135 wxItemContainer * const container = GetContainer();
136
137 wxArrayString testitems;
138 testitems.Add("item 0");
139 testitems.Add("item 1");
140 testitems.Add("item 2");
141 testitems.Add("item 3");
142
143 container->Append(testitems);
144
145 CPPUNIT_ASSERT_EQUAL(1, container->FindString("item 1"));
146 CPPUNIT_ASSERT_EQUAL(1, container->FindString("ITEM 1"));
147 CPPUNIT_ASSERT_EQUAL(wxNOT_FOUND, container->FindString("ITEM 1", true));
148 }
149
150 void ItemContainerTestCase::ClientData()
151 {
152 wxItemContainer * const container = GetContainer();
153
154 wxStringClientData* item0data = new wxStringClientData("item0data");
155 wxStringClientData* item1data = new wxStringClientData("item1data");
156 wxStringClientData* item2data = new wxStringClientData("item2data");
157
158 container->Append("item 0", item0data);
159
160 CPPUNIT_ASSERT_EQUAL(static_cast<wxClientData*>(item0data),
161 container->GetClientObject(0));
162
163 container->Append("item 1");
164 container->SetClientObject(1, item1data);
165
166 CPPUNIT_ASSERT_EQUAL(static_cast<wxClientData*>(item1data),
167 container->GetClientObject(1));
168
169 container->Insert("item 2", 2, item2data);
170
171 CPPUNIT_ASSERT_EQUAL(static_cast<wxClientData*>(item2data),
172 container->GetClientObject(2));
173
174 WX_ASSERT_FAILS_WITH_ASSERT( container->SetClientObject((unsigned)-1, item0data) );
175 WX_ASSERT_FAILS_WITH_ASSERT( container->SetClientObject(12345, item0data) );
176 }
177
178 void ItemContainerTestCase::VoidData()
179 {
180 wxItemContainer * const container = GetContainer();
181
182 wxString item0data("item0data"), item1data("item0data"),
183 item2data("item0data");
184
185 void* item0 = &item0data;
186 void* item1 = &item1data;
187 void* item2 = &item2data;
188
189 container->Append("item 0", item0);
190
191 CPPUNIT_ASSERT_EQUAL(item0, container->GetClientData(0));
192
193 container->Append("item 1");
194 container->SetClientData(1, item1);
195
196 CPPUNIT_ASSERT_EQUAL(item1, container->GetClientData(1));
197
198 container->Insert("item 2", 2, item2);
199
200 CPPUNIT_ASSERT_EQUAL(item2, container->GetClientData(2));
201
202 WX_ASSERT_FAILS_WITH_ASSERT( container->SetClientData((unsigned)-1, NULL) );
203 WX_ASSERT_FAILS_WITH_ASSERT( container->SetClientData(12345, NULL) );
204 }
205
206 void ItemContainerTestCase::Set()
207 {
208 wxItemContainer * const container = GetContainer();
209
210 wxArrayString testitems;
211 testitems.Add("item 0");
212 testitems.Add("item 1");
213
214 container->Append(testitems);
215
216 wxArrayString newtestitems;
217 newtestitems.Add("new item 0");
218 newtestitems.Add("new item 1");
219 newtestitems.Add("new item 2");
220 newtestitems.Add("new item 3");
221
222 container->Set(newtestitems);
223
224 CPPUNIT_ASSERT_EQUAL(4, container->GetCount());
225 CPPUNIT_ASSERT_EQUAL("new item 1", container->GetString(1));
226
227 wxString arrnewitems[] = { "even newer 0", "event newer 1" };
228
229 container->Set(2, arrnewitems);
230
231 CPPUNIT_ASSERT_EQUAL(2, container->GetCount());
232 CPPUNIT_ASSERT_EQUAL("even newer 0", container->GetString(0));
233 }
234
235 void ItemContainerTestCase::SetString()
236 {
237 wxItemContainer * const container = GetContainer();
238
239 wxArrayString testitems;
240 testitems.Add("item 0");
241 testitems.Add("item 1");
242 testitems.Add("item 2");
243 testitems.Add("item 3");
244
245 container->Append(testitems);
246
247 container->SetString(0, "new item 0");
248 #ifndef __WXOSX__
249 container->SetString(2, "");
250 #endif
251
252 CPPUNIT_ASSERT_EQUAL("new item 0", container->GetString(0));
253 #ifndef __WXOSX__
254 CPPUNIT_ASSERT_EQUAL("", container->GetString(2));
255 #endif
256 }
257
258 void ItemContainerTestCase::SetSelection()
259 {
260 wxItemContainer * const container = GetContainer();
261
262 container->Append("first");
263 container->Append("second");
264
265 // This class is used to check that SetSelection() doesn't generate any
266 // events, as documented.
267 class CommandEventHandler : public wxEvtHandler
268 {
269 public:
270 virtual bool ProcessEvent(wxEvent& event)
271 {
272 CPPUNIT_ASSERT_MESSAGE
273 (
274 "unexpected command event from SetSelection",
275 !event.IsCommandEvent()
276 );
277
278 return wxEvtHandler::ProcessEvent(event);
279 }
280 } h;
281
282 wxWindow * const win = GetContainerWindow();
283 win->PushEventHandler(&h);
284 wxON_BLOCK_EXIT_OBJ1( *win, wxWindow::PopEventHandler, false );
285
286 container->SetSelection(0);
287 CPPUNIT_ASSERT_EQUAL( 0, container->GetSelection() );
288
289 container->SetSelection(1);
290 CPPUNIT_ASSERT_EQUAL( 1, container->GetSelection() );
291 }