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