]>
Commit | Line | Data |
---|---|---|
232fdc63 VZ |
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 | ||
bce926c5 VZ |
17 | #include "wx/scopeguard.h" |
18 | ||
232fdc63 VZ |
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 | container->Insert("item 0", 0); | |
51 | ||
52 | CPPUNIT_ASSERT_EQUAL("item 0", container->GetString(0)); | |
53 | ||
54 | wxArrayString testitems; | |
55 | testitems.Add("item 1"); | |
56 | testitems.Add("item 2"); | |
57 | ||
58 | container->Insert(testitems, 0); | |
59 | ||
60 | CPPUNIT_ASSERT_EQUAL("item 1", container->GetString(0)); | |
61 | CPPUNIT_ASSERT_EQUAL("item 2", container->GetString(1)); | |
62 | ||
63 | wxString arritems[] = { "item 3", "item 4" }; | |
64 | ||
65 | container->Insert(2, arritems, 1); | |
66 | ||
67 | CPPUNIT_ASSERT_EQUAL("item 3", container->GetString(1)); | |
68 | CPPUNIT_ASSERT_EQUAL("item 4", container->GetString(2)); | |
69 | } | |
70 | ||
71 | void ItemContainerTestCase::Count() | |
72 | { | |
73 | wxItemContainer * const container = GetContainer(); | |
74 | ||
75 | CPPUNIT_ASSERT(container->IsEmpty()); | |
76 | ||
77 | wxArrayString testitems; | |
78 | testitems.Add("item 0"); | |
79 | testitems.Add("item 1"); | |
80 | testitems.Add("item 2"); | |
81 | testitems.Add("item 3"); | |
82 | ||
83 | container->Append(testitems); | |
84 | ||
85 | CPPUNIT_ASSERT(!container->IsEmpty()); | |
86 | CPPUNIT_ASSERT_EQUAL(4, container->GetCount()); | |
87 | ||
88 | container->Delete(0); | |
89 | ||
90 | CPPUNIT_ASSERT_EQUAL(3, container->GetCount()); | |
91 | ||
92 | container->Delete(0); | |
93 | container->Delete(0); | |
94 | ||
95 | CPPUNIT_ASSERT_EQUAL(1, container->GetCount()); | |
96 | ||
97 | container->Insert(testitems, 1); | |
98 | ||
99 | CPPUNIT_ASSERT_EQUAL(5, container->GetCount()); | |
100 | } | |
101 | ||
102 | void ItemContainerTestCase::ItemSelection() | |
103 | { | |
104 | wxItemContainer * const container = GetContainer(); | |
105 | ||
106 | wxArrayString testitems; | |
107 | testitems.Add("item 0"); | |
108 | testitems.Add("item 1"); | |
109 | testitems.Add("item 2"); | |
110 | testitems.Add("item 3"); | |
111 | ||
112 | container->Append(testitems); | |
113 | ||
114 | container->SetSelection(wxNOT_FOUND); | |
115 | ||
116 | CPPUNIT_ASSERT_EQUAL(wxNOT_FOUND, container->GetSelection()); | |
117 | CPPUNIT_ASSERT_EQUAL("", container->GetStringSelection()); | |
118 | ||
119 | container->SetSelection(1); | |
120 | ||
121 | CPPUNIT_ASSERT_EQUAL(1, container->GetSelection()); | |
122 | CPPUNIT_ASSERT_EQUAL("item 1", container->GetStringSelection()); | |
123 | ||
124 | container->SetStringSelection("item 2"); | |
125 | ||
126 | CPPUNIT_ASSERT_EQUAL(2, container->GetSelection()); | |
127 | CPPUNIT_ASSERT_EQUAL("item 2", container->GetStringSelection()); | |
128 | } | |
129 | ||
130 | void ItemContainerTestCase::FindString() | |
131 | { | |
132 | wxItemContainer * const container = GetContainer(); | |
133 | ||
134 | wxArrayString testitems; | |
135 | testitems.Add("item 0"); | |
136 | testitems.Add("item 1"); | |
137 | testitems.Add("item 2"); | |
138 | testitems.Add("item 3"); | |
139 | ||
140 | container->Append(testitems); | |
141 | ||
142 | CPPUNIT_ASSERT_EQUAL(1, container->FindString("item 1")); | |
143 | CPPUNIT_ASSERT_EQUAL(1, container->FindString("ITEM 1")); | |
144 | CPPUNIT_ASSERT_EQUAL(wxNOT_FOUND, container->FindString("ITEM 1", true)); | |
145 | } | |
146 | ||
147 | void ItemContainerTestCase::ClientData() | |
148 | { | |
149 | wxItemContainer * const container = GetContainer(); | |
150 | ||
151 | wxStringClientData* item0data = new wxStringClientData("item0data"); | |
152 | wxStringClientData* item1data = new wxStringClientData("item1data"); | |
153 | wxStringClientData* item2data = new wxStringClientData("item2data"); | |
154 | ||
155 | container->Append("item 0", item0data); | |
156 | ||
157 | CPPUNIT_ASSERT_EQUAL(static_cast<wxClientData*>(item0data), | |
158 | container->GetClientObject(0)); | |
159 | ||
160 | container->Append("item 1"); | |
161 | container->SetClientObject(1, item1data); | |
162 | ||
163 | CPPUNIT_ASSERT_EQUAL(static_cast<wxClientData*>(item1data), | |
164 | container->GetClientObject(1)); | |
165 | ||
166 | container->Insert("item 2", 2, item2data); | |
167 | ||
168 | CPPUNIT_ASSERT_EQUAL(static_cast<wxClientData*>(item2data), | |
169 | container->GetClientObject(2)); | |
170 | } | |
171 | ||
172 | void ItemContainerTestCase::VoidData() | |
173 | { | |
174 | wxItemContainer * const container = GetContainer(); | |
175 | ||
176 | wxString item0data("item0data"), item1data("item0data"), | |
177 | item2data("item0data"); | |
178 | ||
179 | void* item0 = &item0data; | |
180 | void* item1 = &item1data; | |
181 | void* item2 = &item2data; | |
182 | ||
183 | container->Append("item 0", item0); | |
184 | ||
185 | CPPUNIT_ASSERT_EQUAL(item0, container->GetClientData(0)); | |
186 | ||
187 | container->Append("item 1"); | |
188 | container->SetClientData(1, item1); | |
189 | ||
190 | CPPUNIT_ASSERT_EQUAL(item1, container->GetClientData(1)); | |
191 | ||
192 | container->Insert("item 2", 2, item2); | |
193 | ||
194 | CPPUNIT_ASSERT_EQUAL(item2, container->GetClientData(2)); | |
195 | } | |
196 | ||
197 | void ItemContainerTestCase::Set() | |
198 | { | |
199 | wxItemContainer * const container = GetContainer(); | |
200 | ||
201 | wxArrayString testitems; | |
202 | testitems.Add("item 0"); | |
203 | testitems.Add("item 1"); | |
204 | ||
205 | container->Append(testitems); | |
206 | ||
207 | wxArrayString newtestitems; | |
208 | newtestitems.Add("new item 0"); | |
209 | newtestitems.Add("new item 1"); | |
210 | newtestitems.Add("new item 2"); | |
211 | newtestitems.Add("new item 3"); | |
212 | ||
213 | container->Set(newtestitems); | |
214 | ||
215 | CPPUNIT_ASSERT_EQUAL(4, container->GetCount()); | |
216 | CPPUNIT_ASSERT_EQUAL("new item 1", container->GetString(1)); | |
217 | ||
218 | wxString arrnewitems[] = { "even newer 0", "event newer 1" }; | |
219 | ||
220 | container->Set(2, arrnewitems); | |
221 | ||
222 | CPPUNIT_ASSERT_EQUAL(2, container->GetCount()); | |
223 | CPPUNIT_ASSERT_EQUAL("even newer 0", container->GetString(0)); | |
224 | } | |
225 | ||
226 | void ItemContainerTestCase::SetString() | |
227 | { | |
228 | wxItemContainer * const container = GetContainer(); | |
229 | ||
230 | wxArrayString testitems; | |
231 | testitems.Add("item 0"); | |
232 | testitems.Add("item 1"); | |
233 | testitems.Add("item 2"); | |
234 | testitems.Add("item 3"); | |
235 | ||
236 | container->Append(testitems); | |
237 | ||
238 | container->SetString(0, "new item 0"); | |
239 | #ifndef __WXOSX__ | |
240 | container->SetString(2, ""); | |
241 | #endif | |
242 | ||
243 | CPPUNIT_ASSERT_EQUAL("new item 0", container->GetString(0)); | |
244 | #ifndef __WXOSX__ | |
245 | CPPUNIT_ASSERT_EQUAL("", container->GetString(2)); | |
246 | #endif | |
247 | } | |
bce926c5 VZ |
248 | |
249 | void ItemContainerTestCase::SetSelection() | |
250 | { | |
251 | wxItemContainer * const container = GetContainer(); | |
252 | ||
253 | container->Append("first"); | |
254 | container->Append("second"); | |
255 | ||
256 | // This class is used to check that SetSelection() doesn't generate any | |
257 | // events, as documented. | |
258 | class CommandEventHandler : public wxEvtHandler | |
259 | { | |
260 | public: | |
261 | virtual bool ProcessEvent(wxEvent& event) | |
262 | { | |
263 | CPPUNIT_ASSERT_MESSAGE | |
264 | ( | |
265 | "unexpected command event from SetSelection", | |
266 | !event.IsCommandEvent() | |
267 | ); | |
268 | ||
269 | return wxEvtHandler::ProcessEvent(event); | |
270 | } | |
271 | } h; | |
272 | ||
273 | wxWindow * const win = GetContainerWindow(); | |
274 | win->PushEventHandler(&h); | |
275 | wxON_BLOCK_EXIT_OBJ1( *win, wxWindow::PopEventHandler, false ); | |
276 | ||
277 | container->SetSelection(0); | |
278 | CPPUNIT_ASSERT_EQUAL( 0, container->GetSelection() ); | |
279 | ||
280 | container->SetSelection(1); | |
281 | CPPUNIT_ASSERT_EQUAL( 1, container->GetSelection() ); | |
282 | } |