]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/tests/test_comboTreeBox.py
4 from wx
.lib
.combotreebox
import ComboTreeBox
, IterableTreeCtrl
7 class ComboTreeBoxTest(unittest
.TestCase
):
9 self
.comboBoxEventReceived
= False
10 frame
= wx
.Frame(None)
11 self
.comboBox
= ComboTreeBox(frame
, platform
=platform
)
12 self
.tree
= self
.comboBox
._popupFrame
.GetTree()
14 def onComboBox(self
, event
):
15 self
.comboBoxEventReceived
= True
17 def testComboBoxIsEmptyByDefault(self
):
18 self
.assertEqual(0, self
.comboBox
.GetCount())
20 def testAddingOneItem(self
):
21 self
.comboBox
.Append('Item 1')
22 self
.assertEqual(1, self
.comboBox
.GetCount())
24 def testAddingTwoItems(self
):
25 self
.comboBox
.Append('Item 1')
26 self
.comboBox
.Append('Item 2')
27 self
.assertEqual(2, self
.comboBox
.GetCount())
29 def testAddingTwoParentAndChild(self
):
30 item1
= self
.comboBox
.Append('Item 1')
31 self
.comboBox
.Append('Item 2', item1
)
32 self
.assertEqual(2, self
.comboBox
.GetCount())
34 def testSelectingAnItemPutsItInTheComboBox(self
):
35 self
.comboBox
.Append('Item 1')
36 self
.comboBox
.Bind(wx
.EVT_COMBOBOX
, self
.onComboBox
)
37 self
.comboBox
.NotifyItemSelected('Item 1')
38 self
.failUnless(self
.comboBoxEventReceived
)
41 self
.comboBox
.Append('Item 1')
43 self
.assertEqual(0, self
.comboBox
.GetCount())
46 self
.comboBox
.Append('Item 1')
47 self
.comboBox
.Delete(self
.tree
.GetFirstItem())
48 self
.assertEqual(0, self
.comboBox
.GetCount())
50 def testGetSelection_NoItems(self
):
51 self
.failIf(self
.comboBox
.GetSelection().IsOk())
53 def testGetSelection_NoSelection(self
):
54 self
.comboBox
.Append('Item 1')
55 self
.failIf(self
.comboBox
.GetSelection().IsOk())
57 def testGetSelection_WithSelection(self
):
58 item1
= self
.comboBox
.Append('Item 1')
59 self
.comboBox
.SetValue('Item 1')
60 self
.assertEqual(item1
, self
.comboBox
.GetSelection())
62 def testGetSelection_EquallyNamedNodes_SelectedInTree(self
):
63 item1
= self
.comboBox
.Append('Item')
64 item2
= self
.comboBox
.Append('Item')
65 self
.tree
.SelectItem(item2
)
66 self
.assertEqual(self
.tree
.GetSelection(), self
.comboBox
.GetSelection())
68 def testGetSelection_EquallyNamedNodes_TypedInTextBox(self
):
69 item1
= self
.comboBox
.Append('Item')
70 item2
= self
.comboBox
.Append('Item')
71 self
.comboBox
.SetValue('Item')
72 self
.assertEqual(item1
, self
.comboBox
.GetSelection())
74 def testFindString_NotPresent(self
):
75 self
.comboBox
.Append('Item 1')
76 self
.failIf(self
.comboBox
.FindString('Item 2').IsOk())
78 def testFindString_Present(self
):
79 self
.comboBox
.Append('Item 1')
80 self
.assertEqual(self
.tree
.GetFirstItem(),
81 self
.comboBox
.FindString('Item 1'))
83 def testFindString_Child(self
):
84 parent
= self
.comboBox
.Append('Parent')
85 child
= self
.comboBox
.Append('Child', parent
=parent
)
86 self
.assertEqual(child
, self
.comboBox
.FindString('Child'))
88 def testGetString_NotPresent(self
):
89 self
.assertEqual('', self
.comboBox
.GetString(self
.tree
.GetFirstItem()))
91 def testGetString_Present(self
):
92 self
.comboBox
.Append('Item 1')
93 self
.assertEqual('Item 1',
94 self
.comboBox
.GetString(self
.tree
.GetFirstItem()))
96 def testGetStringSelection_NotPresent(self
):
97 self
.assertEqual('', self
.comboBox
.GetStringSelection())
99 def testGetStringSelection_Present(self
):
100 self
.comboBox
.SetValue('Item 1')
101 self
.assertEqual('Item 1', self
.comboBox
.GetStringSelection())
103 def testInsertAsFirstItem(self
):
104 self
.comboBox
.Insert('Item 1')
105 self
.assertEqual('Item 1',
106 self
.comboBox
.GetString(self
.tree
.GetFirstItem()))
108 def testInsertAsFirstItemBeforeExistingItem(self
):
109 item1
= self
.comboBox
.Append('Item 1')
110 item2
= self
.comboBox
.Insert('Item 2')
111 self
.assertEqual(item2
, self
.tree
.GetFirstItem())
113 def testInsertAsFirstChildBeforeExistingChild(self
):
114 parent
= self
.comboBox
.Append('parent')
115 child1
= self
.comboBox
.Append('child 1', parent
)
116 child2
= self
.comboBox
.Insert('child 2', parent
=parent
)
117 self
.assertEqual(child2
, self
.tree
.GetFirstChild(parent
)[0])
119 def testSelect(self
):
120 item1
= self
.comboBox
.Append('Item 1')
121 self
.comboBox
.Select(item1
)
122 self
.assertEqual('Item 1', self
.comboBox
.GetValue())
124 def testSetString(self
):
125 item1
= self
.comboBox
.Append('Item 1')
126 self
.comboBox
.SetString(item1
, 'Item 2')
127 self
.assertEqual('Item 2', self
.comboBox
.GetString(item1
))
129 def testSetStringSelection_ExistingString(self
):
130 self
.comboBox
.Append('Hi')
131 self
.comboBox
.SetStringSelection('Hi')
132 self
.assertEqual('Hi', self
.comboBox
.GetStringSelection())
134 def testSetStringSelection_NonExistingString(self
):
135 self
.comboBox
.SetStringSelection('Hi')
136 self
.assertEqual('', self
.comboBox
.GetStringSelection())
138 def testAppendWithClientData(self
):
139 item1
= self
.comboBox
.Append('Item 1', clientData
=[1,2,3])
140 self
.assertEqual([1,2,3], self
.comboBox
.GetClientData(item1
))
142 def testInsertWithClientData(self
):
143 item1
= self
.comboBox
.Append('Item 1')
144 item2
= self
.comboBox
.Insert('Item 2', previous
=item1
,
146 self
.assertEqual([1,2,3], self
.comboBox
.GetClientData(item2
))
148 def testSetClientData(self
):
149 item1
= self
.comboBox
.Append('Item 1')
150 self
.comboBox
.SetClientData(item1
, [1,2,3])
151 self
.assertEqual([1,2,3], self
.comboBox
.GetClientData(item1
))
153 def testFindClientData(self
):
154 item1
= self
.comboBox
.Append('Item 1', clientData
='A')
155 self
.assertEqual(item1
, self
.comboBox
.FindClientData('A'))
157 def testFindClientData_NoItems(self
):
158 self
.failIf(self
.comboBox
.FindClientData('A'))
160 def testFindClientData_NoSuchData(self
):
161 item1
= self
.comboBox
.Append('Item 1', clientData
='A')
162 self
.failIf(self
.comboBox
.FindClientData('B'))
164 def testSetClientDataSelection(self
):
165 item1
= self
.comboBox
.Append('Item 1', clientData
='A')
166 self
.comboBox
.SetClientDataSelection('A')
167 self
.assertEqual(item1
, self
.comboBox
.GetSelection())
169 def testSetClientDataSelection_NoSuchData(self
):
170 item1
= self
.comboBox
.Append('Item 1', clientData
='A')
171 self
.comboBox
.SetClientDataSelection('B')
172 self
.failIf(self
.comboBox
.GetSelection())
175 class SortedComboTreeBoxTest(unittest
.TestCase
):
177 frame
= wx
.Frame(None)
178 self
.comboBox
= ComboTreeBox(frame
, style
=wx
.CB_SORT
, platform
=platform
)
179 self
.tree
= self
.comboBox
._popupFrame
.GetTree()
181 def testAppend(self
):
182 itemB
= self
.comboBox
.Append('B')
183 itemA
= self
.comboBox
.Append('A')
184 self
.assertEqual(itemA
, self
.tree
.GetFirstItem())
186 def testInsert(self
):
187 itemA
= self
.comboBox
.Append('A')
188 itemB
= self
.comboBox
.Insert('B')
189 self
.assertEqual(itemA
, self
.tree
.GetFirstItem())
191 def testAppend_Child(self
):
192 itemA
= self
.comboBox
.Append('A')
193 itemA2
= self
.comboBox
.Append('2', parent
=itemA
)
194 itemA1
= self
.comboBox
.Append('1', parent
=itemA
)
195 self
.assertEqual(itemA1
, self
.tree
.GetFirstChild(itemA
)[0])
197 def testInsert_Child(self
):
198 itemA
= self
.comboBox
.Append('A')
199 itemA1
= self
.comboBox
.Append('1', parent
=itemA
)
200 itemA2
= self
.comboBox
.Insert('2', parent
=itemA
)
201 self
.assertEqual(itemA1
, self
.tree
.GetFirstChild(itemA
)[0])
203 def testSetString(self
):
204 itemB
= self
.comboBox
.Append('B')
205 itemC
= self
.comboBox
.Append('C')
206 self
.comboBox
.SetString(itemC
, 'A')
207 self
.assertEqual(itemC
, self
.tree
.GetFirstItem())
210 class ReadOnlyComboTreeBoxTest(unittest
.TestCase
):
212 frame
= wx
.Frame(None)
213 self
.comboBox
= ComboTreeBox(frame
, style
=wx
.CB_READONLY
)
214 self
.tree
= self
.comboBox
._popupFrame
.GetTree()
216 def testSetValue_ToNonExistingValue(self
):
217 self
.comboBox
.SetValue('Ignored value')
218 self
.assertEqual('', self
.comboBox
.GetValue())
220 def testSetValue_ToExistingValue(self
):
221 self
.comboBox
.Append('This works')
222 self
.comboBox
.SetValue('This works')
223 self
.assertEqual('This works', self
.comboBox
.GetValue())
226 class IterableTreeCtrlTest(unittest
.TestCase
):
228 self
.frame
= wx
.Frame(None)
229 self
.tree
= IterableTreeCtrl(self
.frame
)
230 self
.root
= self
.tree
.AddRoot('root')
232 def testPreviousOfRootIsInvalid(self
):
233 item
= self
.tree
.GetPreviousItem(self
.root
)
234 self
.failIf(item
.IsOk())
236 def testPreviousOfChildOfRootIsRoot(self
):
237 child
= self
.tree
.AppendItem(self
.root
, 'child')
238 self
.assertEqual(self
.root
, self
.tree
.GetPreviousItem(child
))
240 def testPreviousOfSecondChildOfRootIsFirstChild(self
):
241 child1
= self
.tree
.AppendItem(self
.root
, 'child1')
242 child2
= self
.tree
.AppendItem(self
.root
, 'child2')
243 self
.assertEqual(child1
, self
.tree
.GetPreviousItem(child2
))
245 def testPreviousOfGrandChildIsChild(self
):
246 child
= self
.tree
.AppendItem(self
.root
, 'child')
247 grandchild
= self
.tree
.AppendItem(child
, 'grandchild')
248 self
.assertEqual(child
, self
.tree
.GetPreviousItem(grandchild
))
250 def testPreviousOfSecondChildWhenFirstChildHasChildIsThatChild(self
):
251 child1
= self
.tree
.AppendItem(self
.root
, 'child1')
252 grandchild
= self
.tree
.AppendItem(child1
, 'child of child1')
253 child2
= self
.tree
.AppendItem(self
.root
, 'child2')
254 self
.assertEqual(grandchild
, self
.tree
.GetPreviousItem(child2
))
256 def testPreviousOfSecondChildWhenFirstChildHasGrandChildIsThatGrandChild(self
):
257 child1
= self
.tree
.AppendItem(self
.root
, 'child1')
258 grandchild
= self
.tree
.AppendItem(child1
, 'child of child1')
259 greatgrandchild
= self
.tree
.AppendItem(grandchild
,
260 'grandchild of child1')
261 child2
= self
.tree
.AppendItem(self
.root
, 'child2')
262 self
.assertEqual(greatgrandchild
, self
.tree
.GetPreviousItem(child2
))
264 def testNextOfRootIsInvalidWhenRootHasNoChildren(self
):
265 item
= self
.tree
.GetNextItem(self
.root
)
266 self
.failIf(item
.IsOk())
268 def testNextOfRootIsItsChildWhenRootHasOneChild(self
):
269 child
= self
.tree
.AppendItem(self
.root
, 'child')
270 self
.assertEqual(child
, self
.tree
.GetNextItem(self
.root
))
272 def testNextOfLastChildIsInvalid(self
):
273 child
= self
.tree
.AppendItem(self
.root
, 'child')
274 self
.failIf(self
.tree
.GetNextItem(child
).IsOk())
276 def testNextOfFirstChildIsSecondChild(self
):
277 child1
= self
.tree
.AppendItem(self
.root
, 'child1')
278 child2
= self
.tree
.AppendItem(self
.root
, 'child2')
279 self
.assertEqual(child2
, self
.tree
.GetNextItem(child1
))
281 def testNextOfGrandChildIsItsParentsSibling(self
):
282 child1
= self
.tree
.AppendItem(self
.root
, 'child1')
283 grandchild
= self
.tree
.AppendItem(child1
, 'child of child1')
284 child2
= self
.tree
.AppendItem(self
.root
, 'child2')
285 self
.assertEqual(child2
, self
.tree
.GetNextItem(grandchild
))
287 def testNextOfGreatGrandChildIsItsParentsSiblingRecursively(self
):
288 child1
= self
.tree
.AppendItem(self
.root
, 'child1')
289 grandchild
= self
.tree
.AppendItem(child1
, 'child of child1')
290 greatgrandchild
= self
.tree
.AppendItem(grandchild
,
291 'grandchild of child1')
292 child2
= self
.tree
.AppendItem(self
.root
, 'child2')
293 self
.assertEqual(child2
, self
.tree
.GetNextItem(greatgrandchild
))
295 def testNextOfGrandChildWhenItIsLastIsInvalid(self
):
296 child
= self
.tree
.AppendItem(self
.root
, 'child')
297 grandchild
= self
.tree
.AppendItem(child
, 'child of child')
298 self
.failIf(self
.tree
.GetNextItem(grandchild
).IsOk())
300 def testFirstItemIsRoot(self
):
301 self
.assertEqual(self
.root
, self
.tree
.GetFirstItem())
303 def testGetFirstItemWithoutRootIsInvalid(self
):
304 tree
= IterableTreeCtrl(self
.frame
)
305 self
.failIf(tree
.GetFirstItem().IsOk())
307 def testGetSelection_NoSelection(self
):
309 self
.failIf(self
.tree
.GetSelection().IsOk())
311 def testGetSelection_RootItemSelected(self
):
312 self
.tree
.SelectItem(self
.tree
.GetRootItem())
313 self
.assertEqual(self
.tree
.GetRootItem(), self
.tree
.GetSelection())
315 def testGetSelection_OtherItem(self
):
316 child
= self
.tree
.AppendItem(self
.root
, 'child')
317 self
.tree
.SelectItem(child
)
318 self
.assertEqual(child
, self
.tree
.GetSelection())
321 class IterableTreeCtrlWithHiddenRootTest(unittest
.TestCase
):
323 frame
= wx
.Frame(None)
324 self
.tree
= IterableTreeCtrl(frame
, style
=wx
.TR_HIDE_ROOT
)
325 self
.root
= self
.tree
.AddRoot('root')
327 def testPreviousOfChildOfRootIsInvalid(self
):
328 child
= self
.tree
.AppendItem(self
.root
, 'child')
329 self
.failIf(self
.tree
.GetPreviousItem(child
).IsOk())
331 def testNextOfGrandChildWhenItIsLastIsInvalid(self
):
332 child
= self
.tree
.AppendItem(self
.root
, 'child')
333 grandchild
= self
.tree
.AppendItem(child
, 'child of child')
334 self
.failIf(self
.tree
.GetNextItem(grandchild
).IsOk())
336 def testRootIsNotTheFirstItem(self
):
337 self
.failIf(self
.tree
.GetFirstItem().IsOk())
339 def testFirstChildOfRootIsTheFirstItem(self
):
340 child
= self
.tree
.AppendItem(self
.root
, 'child')
341 self
.assertEqual(child
, self
.tree
.GetFirstItem())
343 def testGetSelection_NoSelection(self
):
345 self
.failIf(self
.tree
.GetSelection().IsOk())
347 def testGetSelection_RootItemSelected(self
):
348 # Apparently, selecting a hidden root item crashes wxPython on
349 # Windows, so don't do that.
350 if '__WXMSW__' not in wx
.PlatformInfo
:
351 self
.tree
.SelectItem(self
.tree
.GetRootItem())
352 self
.failIf(self
.tree
.GetSelection().IsOk())
354 def testGetSelection_OtherItem(self
):
355 child
= self
.tree
.AppendItem(self
.root
, 'child')
356 self
.tree
.SelectItem(child
)
357 self
.assertEqual(child
, self
.tree
.GetSelection())
361 if __name__
== '__main__':
363 if len(sys
.argv
) > 1:
364 platform
= sys
.argv
[1].upper()