1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/itemcontainertest.cpp
3 // Purpose: wxItemContainer unit test
4 // Author: Steven Lamerton
7 // Copyright: (c) 2010 Steven Lamerton
8 ///////////////////////////////////////////////////////////////////////////////
14 #include "wx/ctrlsub.h"
17 #include "wx/scopeguard.h"
19 #include "itemcontainertest.h"
21 void ItemContainerTestCase::Append()
23 wxItemContainer
* const container
= GetContainer();
25 container
->Append("item 0");
27 CPPUNIT_ASSERT_EQUAL("item 0", container
->GetString(0));
29 wxArrayString testitems
;
30 testitems
.Add("item 1");
31 testitems
.Add("item 2");
33 container
->Append(testitems
);
35 CPPUNIT_ASSERT_EQUAL("item 1", container
->GetString(1));
36 CPPUNIT_ASSERT_EQUAL("item 2", container
->GetString(2));
38 wxString arritems
[] = { "item 3", "item 4" };
40 container
->Append(2, arritems
);
42 CPPUNIT_ASSERT_EQUAL("item 3", container
->GetString(3));
43 CPPUNIT_ASSERT_EQUAL("item 4", container
->GetString(4));
46 void ItemContainerTestCase::Insert()
48 wxItemContainer
* const container
= GetContainer();
50 CPPUNIT_ASSERT_EQUAL( 0, container
->Insert("item 0", 0) );
51 CPPUNIT_ASSERT_EQUAL("item 0", container
->GetString(0));
53 wxArrayString testitems
;
54 testitems
.Add("item 1");
55 testitems
.Add("item 2");
57 CPPUNIT_ASSERT_EQUAL( 1, container
->Insert(testitems
, 0) );
59 CPPUNIT_ASSERT_EQUAL("item 1", container
->GetString(0));
60 CPPUNIT_ASSERT_EQUAL("item 2", container
->GetString(1));
62 wxString arritems
[] = { "item 3", "item 4" };
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));
69 void ItemContainerTestCase::Count()
71 wxItemContainer
* const container
= GetContainer();
73 CPPUNIT_ASSERT(container
->IsEmpty());
75 wxArrayString testitems
;
76 testitems
.Add("item 0");
77 testitems
.Add("item 1");
78 testitems
.Add("item 2");
79 testitems
.Add("item 3");
81 container
->Append(testitems
);
83 CPPUNIT_ASSERT(!container
->IsEmpty());
84 CPPUNIT_ASSERT_EQUAL(4, container
->GetCount());
88 CPPUNIT_ASSERT_EQUAL(3, container
->GetCount());
93 CPPUNIT_ASSERT_EQUAL(1, container
->GetCount());
95 container
->Insert(testitems
, 1);
97 CPPUNIT_ASSERT_EQUAL(5, container
->GetCount());
100 void ItemContainerTestCase::ItemSelection()
102 wxItemContainer
* const container
= GetContainer();
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.
110 container
->Append(testitems
);
112 container
->SetSelection(wxNOT_FOUND
);
113 CPPUNIT_ASSERT_EQUAL(wxNOT_FOUND
, container
->GetSelection());
114 CPPUNIT_ASSERT_EQUAL("", container
->GetStringSelection());
116 container
->SetSelection(1);
117 CPPUNIT_ASSERT_EQUAL(1, container
->GetSelection());
118 CPPUNIT_ASSERT_EQUAL("item 1", container
->GetStringSelection());
120 CPPUNIT_ASSERT( container
->SetStringSelection("item 2") );
121 CPPUNIT_ASSERT_EQUAL(2, container
->GetSelection());
122 CPPUNIT_ASSERT_EQUAL("item 2", container
->GetStringSelection());
124 // Check that selecting a non-existent item fails.
125 CPPUNIT_ASSERT( !container
->SetStringSelection("bloordyblop") );
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());
133 void ItemContainerTestCase::FindString()
135 wxItemContainer
* const container
= GetContainer();
137 wxArrayString testitems
;
138 testitems
.Add("item 0");
139 testitems
.Add("item 1");
140 testitems
.Add("item 2");
141 testitems
.Add("item 3");
143 container
->Append(testitems
);
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));
150 void ItemContainerTestCase::ClientData()
152 wxItemContainer
* const container
= GetContainer();
154 wxStringClientData
* item0data
= new wxStringClientData("item0data");
155 wxStringClientData
* item1data
= new wxStringClientData("item1data");
156 wxStringClientData
* item2data
= new wxStringClientData("item2data");
158 container
->Append("item 0", item0data
);
160 CPPUNIT_ASSERT_EQUAL(static_cast<wxClientData
*>(item0data
),
161 container
->GetClientObject(0));
163 container
->Append("item 1");
164 container
->SetClientObject(1, item1data
);
166 CPPUNIT_ASSERT_EQUAL(static_cast<wxClientData
*>(item1data
),
167 container
->GetClientObject(1));
169 container
->Insert("item 2", 2, item2data
);
171 CPPUNIT_ASSERT_EQUAL(static_cast<wxClientData
*>(item2data
),
172 container
->GetClientObject(2));
174 WX_ASSERT_FAILS_WITH_ASSERT( container
->SetClientObject((unsigned)-1, item0data
) );
175 WX_ASSERT_FAILS_WITH_ASSERT( container
->SetClientObject(12345, item0data
) );
178 void ItemContainerTestCase::VoidData()
180 wxItemContainer
* const container
= GetContainer();
182 wxString
item0data("item0data"), item1data("item0data"),
183 item2data("item0data");
185 void* item0
= &item0data
;
186 void* item1
= &item1data
;
187 void* item2
= &item2data
;
189 container
->Append("item 0", item0
);
191 CPPUNIT_ASSERT_EQUAL(item0
, container
->GetClientData(0));
193 container
->Append("item 1");
194 container
->SetClientData(1, item1
);
196 CPPUNIT_ASSERT_EQUAL(item1
, container
->GetClientData(1));
198 container
->Insert("item 2", 2, item2
);
200 CPPUNIT_ASSERT_EQUAL(item2
, container
->GetClientData(2));
202 WX_ASSERT_FAILS_WITH_ASSERT( container
->SetClientData((unsigned)-1, NULL
) );
203 WX_ASSERT_FAILS_WITH_ASSERT( container
->SetClientData(12345, NULL
) );
206 void ItemContainerTestCase::Set()
208 wxItemContainer
* const container
= GetContainer();
210 wxArrayString testitems
;
211 testitems
.Add("item 0");
212 testitems
.Add("item 1");
214 container
->Append(testitems
);
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");
222 container
->Set(newtestitems
);
224 CPPUNIT_ASSERT_EQUAL(4, container
->GetCount());
225 CPPUNIT_ASSERT_EQUAL("new item 1", container
->GetString(1));
227 wxString arrnewitems
[] = { "even newer 0", "event newer 1" };
229 container
->Set(2, arrnewitems
);
231 CPPUNIT_ASSERT_EQUAL(2, container
->GetCount());
232 CPPUNIT_ASSERT_EQUAL("even newer 0", container
->GetString(0));
235 void ItemContainerTestCase::SetString()
237 wxItemContainer
* const container
= GetContainer();
239 wxArrayString testitems
;
240 testitems
.Add("item 0");
241 testitems
.Add("item 1");
242 testitems
.Add("item 2");
243 testitems
.Add("item 3");
245 container
->Append(testitems
);
247 container
->SetString(0, "new item 0");
249 container
->SetString(2, "");
252 CPPUNIT_ASSERT_EQUAL("new item 0", container
->GetString(0));
254 CPPUNIT_ASSERT_EQUAL("", container
->GetString(2));
258 void ItemContainerTestCase::SetSelection()
260 wxItemContainer
* const container
= GetContainer();
262 container
->Append("first");
263 container
->Append("second");
265 // This class is used to check that SetSelection() doesn't generate any
266 // events, as documented.
267 class CommandEventHandler
: public wxEvtHandler
270 virtual bool ProcessEvent(wxEvent
& event
)
272 CPPUNIT_ASSERT_MESSAGE
274 "unexpected command event from SetSelection",
275 !event
.IsCommandEvent()
278 return wxEvtHandler::ProcessEvent(event
);
282 wxWindow
* const win
= GetContainerWindow();
283 win
->PushEventHandler(&h
);
284 wxON_BLOCK_EXIT_OBJ1( *win
, wxWindow::PopEventHandler
, false );
286 container
->SetSelection(0);
287 CPPUNIT_ASSERT_EQUAL( 0, container
->GetSelection() );
289 container
->SetSelection(1);
290 CPPUNIT_ASSERT_EQUAL( 1, container
->GetSelection() );