1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/itemcontainertest.cpp
3 // Purpose: wxItemContainer unit test
4 // Author: Steven Lamerton
6 // Copyright: (c) 2010 Steven Lamerton
7 ///////////////////////////////////////////////////////////////////////////////
13 #include "wx/ctrlsub.h"
16 #include "wx/scopeguard.h"
18 #include "itemcontainertest.h"
20 void ItemContainerTestCase::Append()
22 wxItemContainer
* const container
= GetContainer();
24 container
->Append("item 0");
26 CPPUNIT_ASSERT_EQUAL("item 0", container
->GetString(0));
28 wxArrayString testitems
;
29 testitems
.Add("item 1");
30 testitems
.Add("item 2");
32 container
->Append(testitems
);
34 CPPUNIT_ASSERT_EQUAL("item 1", container
->GetString(1));
35 CPPUNIT_ASSERT_EQUAL("item 2", container
->GetString(2));
37 wxString arritems
[] = { "item 3", "item 4" };
39 container
->Append(2, arritems
);
41 CPPUNIT_ASSERT_EQUAL("item 3", container
->GetString(3));
42 CPPUNIT_ASSERT_EQUAL("item 4", container
->GetString(4));
45 void ItemContainerTestCase::Insert()
47 wxItemContainer
* const container
= GetContainer();
49 CPPUNIT_ASSERT_EQUAL( 0, container
->Insert("item 0", 0) );
50 CPPUNIT_ASSERT_EQUAL("item 0", container
->GetString(0));
52 wxArrayString testitems
;
53 testitems
.Add("item 1");
54 testitems
.Add("item 2");
56 CPPUNIT_ASSERT_EQUAL( 1, container
->Insert(testitems
, 0) );
58 CPPUNIT_ASSERT_EQUAL("item 1", container
->GetString(0));
59 CPPUNIT_ASSERT_EQUAL("item 2", container
->GetString(1));
61 wxString arritems
[] = { "item 3", "item 4" };
63 CPPUNIT_ASSERT_EQUAL( 2, container
->Insert(2, arritems
, 1) );
64 CPPUNIT_ASSERT_EQUAL("item 3", container
->GetString(1));
65 CPPUNIT_ASSERT_EQUAL("item 4", container
->GetString(2));
68 void ItemContainerTestCase::Count()
70 wxItemContainer
* const container
= GetContainer();
72 CPPUNIT_ASSERT(container
->IsEmpty());
74 wxArrayString testitems
;
75 testitems
.Add("item 0");
76 testitems
.Add("item 1");
77 testitems
.Add("item 2");
78 testitems
.Add("item 3");
80 container
->Append(testitems
);
82 CPPUNIT_ASSERT(!container
->IsEmpty());
83 CPPUNIT_ASSERT_EQUAL(4, container
->GetCount());
87 CPPUNIT_ASSERT_EQUAL(3, container
->GetCount());
92 CPPUNIT_ASSERT_EQUAL(1, container
->GetCount());
94 container
->Insert(testitems
, 1);
96 CPPUNIT_ASSERT_EQUAL(5, container
->GetCount());
99 void ItemContainerTestCase::ItemSelection()
101 wxItemContainer
* const container
= GetContainer();
103 wxArrayString testitems
;
104 testitems
.Add("item 0");
105 testitems
.Add("item 1");
106 testitems
.Add("item 2");
107 testitems
.Add("ITEM 2"); // The same as the last one except for case.
109 container
->Append(testitems
);
111 container
->SetSelection(wxNOT_FOUND
);
112 CPPUNIT_ASSERT_EQUAL(wxNOT_FOUND
, container
->GetSelection());
113 CPPUNIT_ASSERT_EQUAL("", container
->GetStringSelection());
115 container
->SetSelection(1);
116 CPPUNIT_ASSERT_EQUAL(1, container
->GetSelection());
117 CPPUNIT_ASSERT_EQUAL("item 1", container
->GetStringSelection());
119 CPPUNIT_ASSERT( container
->SetStringSelection("item 2") );
120 CPPUNIT_ASSERT_EQUAL(2, container
->GetSelection());
121 CPPUNIT_ASSERT_EQUAL("item 2", container
->GetStringSelection());
123 // Check that selecting a non-existent item fails.
124 CPPUNIT_ASSERT( !container
->SetStringSelection("bloordyblop") );
126 // Check that SetStringSelection() is case-insensitive.
127 CPPUNIT_ASSERT( container
->SetStringSelection("ITEM 2") );
128 CPPUNIT_ASSERT_EQUAL(2, container
->GetSelection());
129 CPPUNIT_ASSERT_EQUAL("item 2", container
->GetStringSelection());
132 void ItemContainerTestCase::FindString()
134 wxItemContainer
* const container
= GetContainer();
136 wxArrayString testitems
;
137 testitems
.Add("item 0");
138 testitems
.Add("item 1");
139 testitems
.Add("item 2");
140 testitems
.Add("item 3");
142 container
->Append(testitems
);
144 CPPUNIT_ASSERT_EQUAL(1, container
->FindString("item 1"));
145 CPPUNIT_ASSERT_EQUAL(1, container
->FindString("ITEM 1"));
146 CPPUNIT_ASSERT_EQUAL(wxNOT_FOUND
, container
->FindString("ITEM 1", true));
149 void ItemContainerTestCase::ClientData()
151 wxItemContainer
* const container
= GetContainer();
153 wxStringClientData
* item0data
= new wxStringClientData("item0data");
154 wxStringClientData
* item1data
= new wxStringClientData("item1data");
155 wxStringClientData
* item2data
= new wxStringClientData("item2data");
157 container
->Append("item 0", item0data
);
159 CPPUNIT_ASSERT_EQUAL(static_cast<wxClientData
*>(item0data
),
160 container
->GetClientObject(0));
162 container
->Append("item 1");
163 container
->SetClientObject(1, item1data
);
165 CPPUNIT_ASSERT_EQUAL(static_cast<wxClientData
*>(item1data
),
166 container
->GetClientObject(1));
168 container
->Insert("item 2", 2, item2data
);
170 CPPUNIT_ASSERT_EQUAL(static_cast<wxClientData
*>(item2data
),
171 container
->GetClientObject(2));
173 WX_ASSERT_FAILS_WITH_ASSERT( container
->SetClientObject((unsigned)-1, item0data
) );
174 WX_ASSERT_FAILS_WITH_ASSERT( container
->SetClientObject(12345, item0data
) );
177 void ItemContainerTestCase::VoidData()
179 wxItemContainer
* const container
= GetContainer();
181 wxString
item0data("item0data"), item1data("item0data"),
182 item2data("item0data");
184 void* item0
= &item0data
;
185 void* item1
= &item1data
;
186 void* item2
= &item2data
;
188 container
->Append("item 0", item0
);
190 CPPUNIT_ASSERT_EQUAL(item0
, container
->GetClientData(0));
192 container
->Append("item 1");
193 container
->SetClientData(1, item1
);
195 CPPUNIT_ASSERT_EQUAL(item1
, container
->GetClientData(1));
197 container
->Insert("item 2", 2, item2
);
199 CPPUNIT_ASSERT_EQUAL(item2
, container
->GetClientData(2));
201 WX_ASSERT_FAILS_WITH_ASSERT( container
->SetClientData((unsigned)-1, NULL
) );
202 WX_ASSERT_FAILS_WITH_ASSERT( container
->SetClientData(12345, NULL
) );
205 void ItemContainerTestCase::Set()
207 wxItemContainer
* const container
= GetContainer();
209 wxArrayString testitems
;
210 testitems
.Add("item 0");
211 testitems
.Add("item 1");
213 container
->Append(testitems
);
215 wxArrayString newtestitems
;
216 newtestitems
.Add("new item 0");
217 newtestitems
.Add("new item 1");
218 newtestitems
.Add("new item 2");
219 newtestitems
.Add("new item 3");
221 container
->Set(newtestitems
);
223 CPPUNIT_ASSERT_EQUAL(4, container
->GetCount());
224 CPPUNIT_ASSERT_EQUAL("new item 1", container
->GetString(1));
226 wxString arrnewitems
[] = { "even newer 0", "event newer 1" };
228 container
->Set(2, arrnewitems
);
230 CPPUNIT_ASSERT_EQUAL(2, container
->GetCount());
231 CPPUNIT_ASSERT_EQUAL("even newer 0", container
->GetString(0));
234 void ItemContainerTestCase::SetString()
236 wxItemContainer
* const container
= GetContainer();
238 wxArrayString testitems
;
239 testitems
.Add("item 0");
240 testitems
.Add("item 1");
241 testitems
.Add("item 2");
242 testitems
.Add("item 3");
244 container
->Append(testitems
);
246 container
->SetSelection(0);
247 container
->SetString(0, "new item 0");
248 CPPUNIT_ASSERT_EQUAL("new item 0", container
->GetString(0));
250 // Modifying the item shouldn't deselect it.
251 CPPUNIT_ASSERT_EQUAL(0, container
->GetSelection());
253 // wxOSX doesn't support having empty items in some containers.
255 container
->SetString(2, "");
256 CPPUNIT_ASSERT_EQUAL("", container
->GetString(2));
260 void ItemContainerTestCase::SetSelection()
262 wxItemContainer
* const container
= GetContainer();
264 container
->Append("first");
265 container
->Append("second");
267 // This class is used to check that SetSelection() doesn't generate any
268 // events, as documented.
269 class CommandEventHandler
: public wxEvtHandler
272 virtual bool ProcessEvent(wxEvent
& event
)
274 CPPUNIT_ASSERT_MESSAGE
276 "unexpected command event from SetSelection",
277 !event
.IsCommandEvent()
280 return wxEvtHandler::ProcessEvent(event
);
284 wxWindow
* const win
= GetContainerWindow();
285 win
->PushEventHandler(&h
);
286 wxON_BLOCK_EXIT_OBJ1( *win
, wxWindow::PopEventHandler
, false );
288 container
->SetSelection(0);
289 CPPUNIT_ASSERT_EQUAL( 0, container
->GetSelection() );
291 container
->SetSelection(1);
292 CPPUNIT_ASSERT_EQUAL( 1, container
->GetSelection() );