]> git.saurik.com Git - wxWidgets.git/blob - tests/controls/itemcontainertest.cpp
Minor wxPNGHandler cleanup.
[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
175 void ItemContainerTestCase::VoidData()
176 {
177 wxItemContainer * const container = GetContainer();
178
179 wxString item0data("item0data"), item1data("item0data"),
180 item2data("item0data");
181
182 void* item0 = &item0data;
183 void* item1 = &item1data;
184 void* item2 = &item2data;
185
186 container->Append("item 0", item0);
187
188 CPPUNIT_ASSERT_EQUAL(item0, container->GetClientData(0));
189
190 container->Append("item 1");
191 container->SetClientData(1, item1);
192
193 CPPUNIT_ASSERT_EQUAL(item1, container->GetClientData(1));
194
195 container->Insert("item 2", 2, item2);
196
197 CPPUNIT_ASSERT_EQUAL(item2, container->GetClientData(2));
198 }
199
200 void ItemContainerTestCase::Set()
201 {
202 wxItemContainer * const container = GetContainer();
203
204 wxArrayString testitems;
205 testitems.Add("item 0");
206 testitems.Add("item 1");
207
208 container->Append(testitems);
209
210 wxArrayString newtestitems;
211 newtestitems.Add("new item 0");
212 newtestitems.Add("new item 1");
213 newtestitems.Add("new item 2");
214 newtestitems.Add("new item 3");
215
216 container->Set(newtestitems);
217
218 CPPUNIT_ASSERT_EQUAL(4, container->GetCount());
219 CPPUNIT_ASSERT_EQUAL("new item 1", container->GetString(1));
220
221 wxString arrnewitems[] = { "even newer 0", "event newer 1" };
222
223 container->Set(2, arrnewitems);
224
225 CPPUNIT_ASSERT_EQUAL(2, container->GetCount());
226 CPPUNIT_ASSERT_EQUAL("even newer 0", container->GetString(0));
227 }
228
229 void ItemContainerTestCase::SetString()
230 {
231 wxItemContainer * const container = GetContainer();
232
233 wxArrayString testitems;
234 testitems.Add("item 0");
235 testitems.Add("item 1");
236 testitems.Add("item 2");
237 testitems.Add("item 3");
238
239 container->Append(testitems);
240
241 container->SetString(0, "new item 0");
242 #ifndef __WXOSX__
243 container->SetString(2, "");
244 #endif
245
246 CPPUNIT_ASSERT_EQUAL("new item 0", container->GetString(0));
247 #ifndef __WXOSX__
248 CPPUNIT_ASSERT_EQUAL("", container->GetString(2));
249 #endif
250 }
251
252 void ItemContainerTestCase::SetSelection()
253 {
254 wxItemContainer * const container = GetContainer();
255
256 container->Append("first");
257 container->Append("second");
258
259 // This class is used to check that SetSelection() doesn't generate any
260 // events, as documented.
261 class CommandEventHandler : public wxEvtHandler
262 {
263 public:
264 virtual bool ProcessEvent(wxEvent& event)
265 {
266 CPPUNIT_ASSERT_MESSAGE
267 (
268 "unexpected command event from SetSelection",
269 !event.IsCommandEvent()
270 );
271
272 return wxEvtHandler::ProcessEvent(event);
273 }
274 } h;
275
276 wxWindow * const win = GetContainerWindow();
277 win->PushEventHandler(&h);
278 wxON_BLOCK_EXIT_OBJ1( *win, wxWindow::PopEventHandler, false );
279
280 container->SetSelection(0);
281 CPPUNIT_ASSERT_EQUAL( 0, container->GetSelection() );
282
283 container->SetSelection(1);
284 CPPUNIT_ASSERT_EQUAL( 1, container->GetSelection() );
285 }