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));
175 void ItemContainerTestCase::VoidData()
177 wxItemContainer
* const container
= GetContainer();
179 wxString
item0data("item0data"), item1data("item0data"),
180 item2data("item0data");
182 void* item0
= &item0data
;
183 void* item1
= &item1data
;
184 void* item2
= &item2data
;
186 container
->Append("item 0", item0
);
188 CPPUNIT_ASSERT_EQUAL(item0
, container
->GetClientData(0));
190 container
->Append("item 1");
191 container
->SetClientData(1, item1
);
193 CPPUNIT_ASSERT_EQUAL(item1
, container
->GetClientData(1));
195 container
->Insert("item 2", 2, item2
);
197 CPPUNIT_ASSERT_EQUAL(item2
, container
->GetClientData(2));
200 void ItemContainerTestCase::Set()
202 wxItemContainer
* const container
= GetContainer();
204 wxArrayString testitems
;
205 testitems
.Add("item 0");
206 testitems
.Add("item 1");
208 container
->Append(testitems
);
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");
216 container
->Set(newtestitems
);
218 CPPUNIT_ASSERT_EQUAL(4, container
->GetCount());
219 CPPUNIT_ASSERT_EQUAL("new item 1", container
->GetString(1));
221 wxString arrnewitems
[] = { "even newer 0", "event newer 1" };
223 container
->Set(2, arrnewitems
);
225 CPPUNIT_ASSERT_EQUAL(2, container
->GetCount());
226 CPPUNIT_ASSERT_EQUAL("even newer 0", container
->GetString(0));
229 void ItemContainerTestCase::SetString()
231 wxItemContainer
* const container
= GetContainer();
233 wxArrayString testitems
;
234 testitems
.Add("item 0");
235 testitems
.Add("item 1");
236 testitems
.Add("item 2");
237 testitems
.Add("item 3");
239 container
->Append(testitems
);
241 container
->SetString(0, "new item 0");
243 container
->SetString(2, "");
246 CPPUNIT_ASSERT_EQUAL("new item 0", container
->GetString(0));
248 CPPUNIT_ASSERT_EQUAL("", container
->GetString(2));
252 void ItemContainerTestCase::SetSelection()
254 wxItemContainer
* const container
= GetContainer();
256 container
->Append("first");
257 container
->Append("second");
259 // This class is used to check that SetSelection() doesn't generate any
260 // events, as documented.
261 class CommandEventHandler
: public wxEvtHandler
264 virtual bool ProcessEvent(wxEvent
& event
)
266 CPPUNIT_ASSERT_MESSAGE
268 "unexpected command event from SetSelection",
269 !event
.IsCommandEvent()
272 return wxEvtHandler::ProcessEvent(event
);
276 wxWindow
* const win
= GetContainerWindow();
277 win
->PushEventHandler(&h
);
278 wxON_BLOCK_EXIT_OBJ1( *win
, wxWindow::PopEventHandler
, false );
280 container
->SetSelection(0);
281 CPPUNIT_ASSERT_EQUAL( 0, container
->GetSelection() );
283 container
->SetSelection(1);
284 CPPUNIT_ASSERT_EQUAL( 1, container
->GetSelection() );