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 container
->Insert("item 0", 0);
52 CPPUNIT_ASSERT_EQUAL("item 0", container
->GetString(0));
54 wxArrayString testitems
;
55 testitems
.Add("item 1");
56 testitems
.Add("item 2");
58 container
->Insert(testitems
, 0);
60 CPPUNIT_ASSERT_EQUAL("item 1", container
->GetString(0));
61 CPPUNIT_ASSERT_EQUAL("item 2", container
->GetString(1));
63 wxString arritems
[] = { "item 3", "item 4" };
65 container
->Insert(2, arritems
, 1);
67 CPPUNIT_ASSERT_EQUAL("item 3", container
->GetString(1));
68 CPPUNIT_ASSERT_EQUAL("item 4", container
->GetString(2));
71 void ItemContainerTestCase::Count()
73 wxItemContainer
* const container
= GetContainer();
75 CPPUNIT_ASSERT(container
->IsEmpty());
77 wxArrayString testitems
;
78 testitems
.Add("item 0");
79 testitems
.Add("item 1");
80 testitems
.Add("item 2");
81 testitems
.Add("item 3");
83 container
->Append(testitems
);
85 CPPUNIT_ASSERT(!container
->IsEmpty());
86 CPPUNIT_ASSERT_EQUAL(4, container
->GetCount());
90 CPPUNIT_ASSERT_EQUAL(3, container
->GetCount());
95 CPPUNIT_ASSERT_EQUAL(1, container
->GetCount());
97 container
->Insert(testitems
, 1);
99 CPPUNIT_ASSERT_EQUAL(5, container
->GetCount());
102 void ItemContainerTestCase::ItemSelection()
104 wxItemContainer
* const container
= GetContainer();
106 wxArrayString testitems
;
107 testitems
.Add("item 0");
108 testitems
.Add("item 1");
109 testitems
.Add("item 2");
110 testitems
.Add("item 3");
112 container
->Append(testitems
);
114 container
->SetSelection(wxNOT_FOUND
);
116 CPPUNIT_ASSERT_EQUAL(wxNOT_FOUND
, container
->GetSelection());
117 CPPUNIT_ASSERT_EQUAL("", container
->GetStringSelection());
119 container
->SetSelection(1);
121 CPPUNIT_ASSERT_EQUAL(1, container
->GetSelection());
122 CPPUNIT_ASSERT_EQUAL("item 1", container
->GetStringSelection());
124 container
->SetStringSelection("item 2");
126 CPPUNIT_ASSERT_EQUAL(2, container
->GetSelection());
127 CPPUNIT_ASSERT_EQUAL("item 2", container
->GetStringSelection());
130 void ItemContainerTestCase::FindString()
132 wxItemContainer
* const container
= GetContainer();
134 wxArrayString testitems
;
135 testitems
.Add("item 0");
136 testitems
.Add("item 1");
137 testitems
.Add("item 2");
138 testitems
.Add("item 3");
140 container
->Append(testitems
);
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));
147 void ItemContainerTestCase::ClientData()
149 wxItemContainer
* const container
= GetContainer();
151 wxStringClientData
* item0data
= new wxStringClientData("item0data");
152 wxStringClientData
* item1data
= new wxStringClientData("item1data");
153 wxStringClientData
* item2data
= new wxStringClientData("item2data");
155 container
->Append("item 0", item0data
);
157 CPPUNIT_ASSERT_EQUAL(static_cast<wxClientData
*>(item0data
),
158 container
->GetClientObject(0));
160 container
->Append("item 1");
161 container
->SetClientObject(1, item1data
);
163 CPPUNIT_ASSERT_EQUAL(static_cast<wxClientData
*>(item1data
),
164 container
->GetClientObject(1));
166 container
->Insert("item 2", 2, item2data
);
168 CPPUNIT_ASSERT_EQUAL(static_cast<wxClientData
*>(item2data
),
169 container
->GetClientObject(2));
172 void ItemContainerTestCase::VoidData()
174 wxItemContainer
* const container
= GetContainer();
176 wxString
item0data("item0data"), item1data("item0data"),
177 item2data("item0data");
179 void* item0
= &item0data
;
180 void* item1
= &item1data
;
181 void* item2
= &item2data
;
183 container
->Append("item 0", item0
);
185 CPPUNIT_ASSERT_EQUAL(item0
, container
->GetClientData(0));
187 container
->Append("item 1");
188 container
->SetClientData(1, item1
);
190 CPPUNIT_ASSERT_EQUAL(item1
, container
->GetClientData(1));
192 container
->Insert("item 2", 2, item2
);
194 CPPUNIT_ASSERT_EQUAL(item2
, container
->GetClientData(2));
197 void ItemContainerTestCase::Set()
199 wxItemContainer
* const container
= GetContainer();
201 wxArrayString testitems
;
202 testitems
.Add("item 0");
203 testitems
.Add("item 1");
205 container
->Append(testitems
);
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");
213 container
->Set(newtestitems
);
215 CPPUNIT_ASSERT_EQUAL(4, container
->GetCount());
216 CPPUNIT_ASSERT_EQUAL("new item 1", container
->GetString(1));
218 wxString arrnewitems
[] = { "even newer 0", "event newer 1" };
220 container
->Set(2, arrnewitems
);
222 CPPUNIT_ASSERT_EQUAL(2, container
->GetCount());
223 CPPUNIT_ASSERT_EQUAL("even newer 0", container
->GetString(0));
226 void ItemContainerTestCase::SetString()
228 wxItemContainer
* const container
= GetContainer();
230 wxArrayString testitems
;
231 testitems
.Add("item 0");
232 testitems
.Add("item 1");
233 testitems
.Add("item 2");
234 testitems
.Add("item 3");
236 container
->Append(testitems
);
238 container
->SetString(0, "new item 0");
240 container
->SetString(2, "");
243 CPPUNIT_ASSERT_EQUAL("new item 0", container
->GetString(0));
245 CPPUNIT_ASSERT_EQUAL("", container
->GetString(2));
249 void ItemContainerTestCase::SetSelection()
251 wxItemContainer
* const container
= GetContainer();
253 container
->Append("first");
254 container
->Append("second");
256 // This class is used to check that SetSelection() doesn't generate any
257 // events, as documented.
258 class CommandEventHandler
: public wxEvtHandler
261 virtual bool ProcessEvent(wxEvent
& event
)
263 CPPUNIT_ASSERT_MESSAGE
265 "unexpected command event from SetSelection",
266 !event
.IsCommandEvent()
269 return wxEvtHandler::ProcessEvent(event
);
273 wxWindow
* const win
= GetContainerWindow();
274 win
->PushEventHandler(&h
);
275 wxON_BLOCK_EXIT_OBJ1( *win
, wxWindow::PopEventHandler
, false );
277 container
->SetSelection(0);
278 CPPUNIT_ASSERT_EQUAL( 0, container
->GetSelection() );
280 container
->SetSelection(1);
281 CPPUNIT_ASSERT_EQUAL( 1, container
->GetSelection() );