1 import wx
, wx
.gizmos
, wx
.lib
.customtreectrl
, unittest
, treemixin
6 class TreeCtrl(object):
7 def __init__(self
, *args
, **kwargs
):
15 super(TreeCtrl
, self
).__init
__(*args
, **kwargs
)
17 def OnGetItemFont(self
, index
):
18 return self
.font
.get(index
, wx
.NullFont
)
20 def PrepareItemFont(self
, index
, font
):
21 self
.font
[index
] = font
23 def OnGetItemTextColour(self
, index
):
24 return self
.colour
.get(index
, wx
.NullColour
)
26 def PrepareItemColour(self
, index
, colour
):
27 self
.colour
[index
] = colour
29 def OnGetItemBackgroundColour(self
, index
):
30 return self
.bgcolour
.get(index
, wx
.NullColour
)
32 def PrepareItemBackgroundColour(self
, index
, colour
):
33 self
.bgcolour
[index
] = colour
35 def OnGetItemImage(self
, index
, which
):
36 return self
.image
.get(index
, -1)
38 def PrepareImage(self
, index
, imageIndex
):
39 self
.image
[index
] = imageIndex
41 def OnGetItemType(self
, index
):
42 return self
.type.get(index
, 0)
44 def PrepareType(self
, index
, itemType
):
45 self
.type[index
] = itemType
47 def OnGetItemChecked(self
, index
):
48 return self
.checked
.get(index
, False)
50 def PrepareChecked(self
, index
, checked
):
51 self
.checked
[index
] = checked
54 class VirtualTreeCtrl(TreeCtrl
, treemixin
.VirtualTree
,
55 wx
.lib
.customtreectrl
.CustomTreeCtrl
):
57 def OnGetItemText(self
, indices
):
58 return 'item %s'%'.'.join([str(index
) for index
in indices
])
60 def OnGetChildrenCount(self
, index
=None):
62 return self
.children
.get(index
, 0)
64 def PrepareChildrenCount(self
, index
, childrenCount
):
65 self
.children
[index
] = childrenCount
69 class VirtualTreeCtrlTest_NoRootItems(unittest
.TestCase
):
71 self
.frame
= wx
.Frame(None)
72 self
.tree
= VirtualTreeCtrl(self
.frame
)
73 self
.tree
.RefreshItems()
75 def testNoRootItems(self
):
76 self
.assertEqual(0, self
.tree
.GetCount())
78 def testAddTwoRootItems(self
):
79 self
.tree
.PrepareChildrenCount((), 2)
80 self
.tree
.RefreshItems()
81 self
.assertEqual(2, self
.tree
.GetCount())
83 def testAddOneRootItemAndOneChild(self
):
84 self
.tree
.PrepareChildrenCount((), 1)
85 self
.tree
.PrepareChildrenCount((0,), 1)
86 self
.tree
.RefreshItems()
88 self
.assertEqual(2, self
.tree
.GetCount())
90 def testAddOneRootItemAndTwoChildren(self
):
91 self
.tree
.PrepareChildrenCount((), 1)
92 self
.tree
.PrepareChildrenCount((0,), 2)
93 self
.tree
.RefreshItems()
95 self
.assertEqual(3, self
.tree
.GetCount())
99 class VirtualTreeCtrlTest_OneRoot(unittest
.TestCase
):
101 self
.frame
= wx
.Frame(None)
102 self
.tree
= VirtualTreeCtrl(self
.frame
)
103 self
.tree
.PrepareChildrenCount((), 1)
104 self
.tree
.RefreshItems()
106 def testOneRoot(self
):
107 self
.assertEqual(1, self
.tree
.GetCount())
109 def testDeleteRootItem(self
):
110 self
.tree
.PrepareChildrenCount((), 0)
111 self
.tree
.RefreshItems()
112 self
.assertEqual(0, self
.tree
.GetCount())
114 def testAddOneChild(self
):
115 self
.tree
.PrepareChildrenCount((0,), 1)
116 self
.tree
.RefreshItems()
117 self
.tree
.ExpandAll()
118 self
.assertEqual(2, self
.tree
.GetCount())
120 def testAddTwoChildren(self
):
121 self
.tree
.PrepareChildrenCount((0,), 2)
122 self
.tree
.RefreshItems()
123 self
.tree
.ExpandAll()
124 self
.assertEqual(3, self
.tree
.GetCount())
126 def testChangeFont(self
):
127 self
.tree
.PrepareItemFont((0,), wx
.SMALL_FONT
)
128 self
.tree
.RefreshItems()
129 item
, cookie
= self
.tree
.GetFirstChild(self
.tree
.GetRootItem())
130 self
.assertEqual(wx
.SMALL_FONT
, self
.tree
.GetItemFont(item
))
132 def testChangeColour(self
):
133 self
.tree
.PrepareItemColour((0,), wx
.RED
)
134 self
.tree
.RefreshItems()
135 item
, cookie
= self
.tree
.GetFirstChild(self
.tree
.GetRootItem())
136 self
.assertEqual(wx
.RED
, self
.tree
.GetItemTextColour(item
))
138 def testChangeBackgroundColour(self
):
139 self
.tree
.PrepareItemBackgroundColour((0,), wx
.RED
)
140 self
.tree
.RefreshItems()
141 item
, cookie
= self
.tree
.GetFirstChild(self
.tree
.GetRootItem())
142 self
.assertEqual(wx
.RED
, self
.tree
.GetItemBackgroundColour(item
))
144 def testChangeImage(self
):
145 self
.tree
.PrepareImage((0,), 0)
146 self
.tree
.RefreshItems()
147 item
, cookie
= self
.tree
.GetFirstChild(self
.tree
.GetRootItem())
148 self
.assertEqual(0, self
.tree
.GetItemImage(item
))
150 def testChangeType(self
):
151 self
.tree
.PrepareType((0,), 2)
152 self
.tree
.PrepareChecked((0,), True)
153 self
.tree
.RefreshItems()
154 item
, cookie
= self
.tree
.GetFirstChild(self
.tree
.GetRootItem())
155 self
.failUnless(self
.tree
.IsItemChecked(item
))
157 def testChangeTypeAndAddChildren(self
):
158 self
.tree
.PrepareType((0,), 1)
159 self
.tree
.PrepareChildrenCount((0,), 1)
160 self
.tree
.RefreshItems()
161 item
, cookie
= self
.tree
.GetFirstChild(self
.tree
.GetRootItem())
162 self
.failUnless(self
.tree
.ItemHasChildren(item
))
164 def testRefreshItem(self
):
165 self
.tree
.PrepareItemColour((0,), wx
.RED
)
166 self
.tree
.RefreshItem((0,))
167 item
, cookie
= self
.tree
.GetFirstChild(self
.tree
.GetRootItem())
168 self
.assertEqual(wx
.RED
, self
.tree
.GetItemTextColour(item
))
170 def testRefreshItemThatHasNoCorrespondingNodeInTheTree(self
):
171 # The item to refresh might be in a collapsed branch of the tree
172 # and not present yet.
173 self
.tree
.PrepareChildrenCount((0,), 1)
174 self
.tree
.RefreshItem((0,0))
175 item
, cookie
= self
.tree
.GetFirstChild(self
.tree
.GetRootItem())
176 self
.failIf(self
.tree
.ItemHasChildren(item
))
179 # TreeAPIHarmonizer tests
181 class TreeAPIHarmonizerTestCase(unittest
.TestCase
):
182 style
= wx
.TR_DEFAULT_STYLE
185 self
.frame
= wx
.Frame(None)
186 class HarmonizedTreeCtrl(treemixin
.TreeAPIHarmonizer
, self
.TreeClass
):
188 self
.tree
= HarmonizedTreeCtrl(self
.frame
, style
=self
.style
)
189 self
.eventsReceived
= []
192 def populateTree(self
):
193 self
.root
= self
.tree
.AddRoot('Root')
194 self
.item
= self
.tree
.AppendItem(self
.root
, 'Item')
195 self
.items
= [self
.root
, self
.item
]
197 def onEvent(self
, event
):
198 self
.eventsReceived
.append(event
)
201 class TreeAPIHarmonizerCommonTests(object):
202 ''' Tests that should succeed for all tree controls and all styles. '''
204 def testGetItemType(self
):
205 self
.assertEqual(0, self
.tree
.GetItemType(self
.item
))
207 def testGetItemImage_DefaultIcon(self
):
208 self
.assertEqual(-1, self
.tree
.GetItemImage(self
.item
))
210 def testGetItemImage_SelectedIcon(self
):
212 self
.tree
.GetItemImage(self
.item
, wx
.TreeItemIcon_Selected
))
214 def testGetItemImage_DefaultIcon_OtherColumn(self
):
215 self
.assertEqual(-1, self
.tree
.GetItemImage(self
.item
, column
=1))
217 def testGetItemImage_SelectedIcon_OtherColumn(self
):
219 self
.tree
.GetItemImage(self
.item
, wx
.TreeItemIcon_Selected
, 1))
221 def testSetItemImage_DefaultIcon(self
):
222 self
.tree
.SetItemImage(self
.item
, -1)
223 self
.assertEqual(-1, self
.tree
.GetItemImage(self
.item
))
225 def testSetItemImage_SelectedIcon(self
):
226 self
.tree
.SetItemImage(self
.item
, -1, wx
.TreeItemIcon_Selected
)
228 self
.tree
.GetItemImage(self
.item
, wx
.TreeItemIcon_Selected
))
230 def testSetItemImage_DefaultIcon_OtherColumn(self
):
231 self
.tree
.SetItemImage(self
.item
, -1, column
=1)
232 self
.assertEqual(-1, self
.tree
.GetItemImage(self
.item
, column
=1))
234 def testSetItemImage_SelectedIcon_OtherColumn(self
):
235 self
.tree
.SetItemImage(self
.item
, -1, wx
.TreeItemIcon_Selected
, 1)
237 self
.tree
.GetItemImage(self
.item
, wx
.TreeItemIcon_Selected
, 1))
239 def testExpandAll(self
):
240 self
.tree
.ExpandAll()
242 def testExpandAllChildren(self
):
243 self
.tree
.AppendItem(self
.item
, 'child')
244 self
.tree
.ExpandAllChildren(self
.item
)
245 self
.failUnless(self
.tree
.IsExpanded(self
.item
))
247 def testGetFirstVisibleItem(self
):
248 self
.tree
.DeleteAllItems()
249 self
.failIf(self
.tree
.GetFirstVisibleItem())
252 class TreeAPIHarmonizerNoTreeListCtrlCommonTests(object):
253 ''' Tests that should succeed for all tree controls, except the
254 TreeListCtrl, and all styles. '''
256 def testGetMainWindow(self
):
257 self
.assertEqual(self
.tree
, self
.tree
.GetMainWindow())
259 def testGetColumnCount(self
):
260 self
.assertEqual(0, self
.tree
.GetColumnCount())
263 class TreeAPIHarmonizerSingleSelectionTests(object):
264 ''' Tests that should succeed for all tree controls when in single
265 selection mode (which is the default selection mode). '''
267 def testUnselectAll(self
):
268 self
.tree
.SelectItem(self
.item
)
269 self
.tree
.UnselectAll()
270 self
.assertEqual([], self
.tree
.GetSelections())
272 def testGetSelections_NoSelection(self
):
273 self
.tree
.UnselectAll()
274 self
.assertEqual([], self
.tree
.GetSelections())
276 def testGetSelections_OneSelectedItem(self
):
277 self
.tree
.UnselectAll()
278 self
.tree
.SelectItem(self
.item
)
279 self
.assertEqual([self
.item
], self
.tree
.GetSelections())
282 class TreeAPIHarmonizerMultipleSelectionTests(object):
283 ''' Tests that should succeed for all tree controls when in multiple
286 style
= wx
.TR_DEFAULT_STYLE | wx
.TR_MULTIPLE
288 def testUnselectAll(self
):
289 self
.tree
.SelectItem(self
.item
)
290 self
.tree
.UnselectAll()
291 self
.assertEqual([], self
.tree
.GetSelections())
293 def testGetSelections_NoSelection(self
):
294 self
.tree
.UnselectAll()
295 self
.assertEqual([], self
.tree
.GetSelections())
297 def testGetSelections_OneSelectedItem(self
):
298 self
.tree
.UnselectAll()
299 self
.tree
.SelectItem(self
.item
)
300 self
.assertEqual([self
.item
], self
.tree
.GetSelections())
302 def testGetSelections_TwoSelectedItems(self
):
303 item2
= self
.tree
.AppendItem(self
.root
, 'item 2')
304 self
.tree
.ExpandAll()
305 self
.tree
.UnselectAll()
306 self
.tree
.SelectItem(item2
)
307 self
.tree
.SelectItem(self
.item
)
308 self
.assertEqual([self
.item
, item2
], self
.tree
.GetSelections())
311 class TreeAPIHarmonizerVisibleRootTests(object):
312 ''' Tests that should succeed for all tree controls when the root item
315 def testGetCount(self
):
316 self
.assertEqual(len(self
.items
), self
.tree
.GetCount())
318 def testSelectRoot(self
):
319 self
.tree
.SelectItem(self
.root
)
320 self
.assertEqual([self
.root
], self
.tree
.GetSelections())
323 class TreeAPIHarmonizerHiddenRootTests(object):
324 ''' Tests that should succeed for all tree controls when the root item
327 style
= wx
.TR_DEFAULT_STYLE | wx
.TR_HIDE_ROOT
329 def testGetCount(self
):
330 self
.assertEqual(len(self
.items
) - 1, self
.tree
.GetCount())
332 def testSelectRoot(self
):
333 self
.tree
.SelectItem(self
.root
)
334 self
.assertEqual([], self
.tree
.GetSelections())
337 class TreeAPIHarmonizerWithTreeCtrlTestCase(TreeAPIHarmonizerTestCase
):
338 TreeClass
= wx
.TreeCtrl
341 class TreeAPIHarmonizerWithTreeCtrlCommonTests( \
342 TreeAPIHarmonizerCommonTests
,
343 TreeAPIHarmonizerNoTreeListCtrlCommonTests
):
347 class TreeAPIHarmonizerWithTreeCtrl_SingleSelection( \
348 TreeAPIHarmonizerWithTreeCtrlCommonTests
,
349 TreeAPIHarmonizerSingleSelectionTests
,
350 TreeAPIHarmonizerWithTreeCtrlTestCase
):
354 class TreeAPIHarmonizerWithTreeCtrl_MultipleSelection( \
355 TreeAPIHarmonizerWithTreeCtrlCommonTests
,
356 TreeAPIHarmonizerMultipleSelectionTests
,
357 TreeAPIHarmonizerWithTreeCtrlTestCase
):
361 class TreeAPIHarmonizerWithTreeCtrl_VisibleRoot( \
362 TreeAPIHarmonizerWithTreeCtrlCommonTests
,
363 TreeAPIHarmonizerVisibleRootTests
,
364 TreeAPIHarmonizerWithTreeCtrlTestCase
):
368 class TreeAPIHarmonizerWithTreeCtrl_HiddenRoot( \
369 TreeAPIHarmonizerWithTreeCtrlCommonTests
,
370 TreeAPIHarmonizerHiddenRootTests
,
371 TreeAPIHarmonizerWithTreeCtrlTestCase
):
375 class TreeAPIHarmonizerWithTreeListCtrlTestCase(TreeAPIHarmonizerTestCase
):
376 TreeClass
= wx
.gizmos
.TreeListCtrl
378 def populateTree(self
):
379 self
.tree
.AddColumn('Column')
380 super(TreeAPIHarmonizerWithTreeListCtrlTestCase
, self
).populateTree()
383 class TreeAPIHarmonizerWithTreeListCtrlCommonTests( \
384 TreeAPIHarmonizerCommonTests
):
386 def testGetColumnCount(self
):
387 self
.assertEqual(1, self
.tree
.GetColumnCount())
389 def testGetMainWindow(self
):
390 self
.assertNotEqual(self
.tree
, self
.tree
.GetMainWindow())
393 class TreeAPIHarmonizerWithTreeListCtrl_SingleSelection( \
394 TreeAPIHarmonizerWithTreeListCtrlCommonTests
,
395 TreeAPIHarmonizerSingleSelectionTests
,
396 TreeAPIHarmonizerWithTreeListCtrlTestCase
):
400 class TreeAPIHarmonizerWithTreeListCtrl_MultipleSelection( \
401 TreeAPIHarmonizerWithTreeListCtrlCommonTests
,
402 TreeAPIHarmonizerMultipleSelectionTests
,
403 TreeAPIHarmonizerWithTreeListCtrlTestCase
):
405 def testGetSelections_TwoSelectedItems(self
):
406 ''' Override TreeAPIHarmonizerMultipleSelectionTests.-
407 testGetSelections_TwoSelectedItems, because
408 TreeListCtrl.SelectItem needs an extra parameter. '''
409 self
.tree
.UnselectAll()
410 item2
= self
.tree
.AppendItem(self
.root
, 'item 2')
411 self
.tree
.SelectItem(self
.item
)
412 self
.tree
.SelectItem(item2
, unselect_others
=False)
413 self
.assertEqual([self
.item
, item2
], self
.tree
.GetSelections())
416 class TreeAPIHarmonizerWithTreeListCtrl_VisibleRoot( \
417 TreeAPIHarmonizerWithTreeListCtrlCommonTests
,
418 TreeAPIHarmonizerVisibleRootTests
,
419 TreeAPIHarmonizerWithTreeListCtrlTestCase
):
423 class TreeAPIHarmonizerWithTreeListCtrl_HiddenRoot( \
424 TreeAPIHarmonizerWithTreeListCtrlCommonTests
,
425 TreeAPIHarmonizerHiddenRootTests
,
426 TreeAPIHarmonizerWithTreeListCtrlTestCase
):
430 class TreeAPIHarmonizerWithCustomTreeCtrlTestCase(TreeAPIHarmonizerTestCase
):
431 TreeClass
= wx
.lib
.customtreectrl
.CustomTreeCtrl
434 class TreeAPIHarmonizerWithCustomTreeCtrlCommonTests( \
435 TreeAPIHarmonizerCommonTests
,
436 TreeAPIHarmonizerNoTreeListCtrlCommonTests
):
438 def testGetCheckItemType(self
):
439 item
= self
.tree
.AppendItem(self
.root
, 'item', ct_type
=1)
440 self
.assertEqual(1, self
.tree
.GetItemType(item
))
442 def testGetRadioItemType(self
):
443 item
= self
.tree
.AppendItem(self
.root
, 'item', ct_type
=2)
444 self
.assertEqual(2, self
.tree
.GetItemType(item
))
448 class TreeAPIHarmonizerWithCustomTreeCtrl_SingleSelection( \
449 TreeAPIHarmonizerWithCustomTreeCtrlCommonTests
,
450 TreeAPIHarmonizerSingleSelectionTests
,
451 TreeAPIHarmonizerWithCustomTreeCtrlTestCase
):
455 class TreeAPIHarmonizerWithCustomTreeCtrl_MultipleSelection( \
456 TreeAPIHarmonizerWithCustomTreeCtrlCommonTests
,
457 TreeAPIHarmonizerMultipleSelectionTests
,
458 TreeAPIHarmonizerWithCustomTreeCtrlTestCase
):
462 class TreeAPIHarmonizerWithCustomTreeCtrl_VisibleRoot( \
463 TreeAPIHarmonizerWithCustomTreeCtrlCommonTests
,
464 TreeAPIHarmonizerVisibleRootTests
,
465 TreeAPIHarmonizerWithCustomTreeCtrlTestCase
):
469 class TreeAPIHarmonizerWithCustomTreeCtrl_HiddenRoot( \
470 TreeAPIHarmonizerWithCustomTreeCtrlCommonTests
,
471 TreeAPIHarmonizerHiddenRootTests
,
472 TreeAPIHarmonizerWithCustomTreeCtrlTestCase
):
478 class TreeHelperTestCase(unittest
.TestCase
):
479 style
= wx
.TR_DEFAULT_STYLE
482 self
.frame
= wx
.Frame(None)
483 class HelperTreeCtrl(treemixin
.TreeHelper
,
484 treemixin
.TreeAPIHarmonizer
, self
.TreeClass
):
486 self
.tree
= HelperTreeCtrl(self
.frame
, style
=self
.style
)
489 def populateTree(self
):
490 self
.root
= self
.tree
.AddRoot('Root')
491 self
.item
= self
.tree
.AppendItem(self
.root
, 'Item')
492 self
.child
= self
.tree
.AppendItem(self
.item
, 'Child')
495 class TreeHelperCommonTests(object):
496 def testGetItemChildren_EmptyTree(self
):
497 self
.tree
.DeleteAllItems()
498 self
.assertEqual([], self
.tree
.GetItemChildren())
500 def testGetItemChildren_NoParent(self
):
501 self
.assertEqual([self
.item
], self
.tree
.GetItemChildren())
503 def testGetItemChildren_RootItem(self
):
504 self
.assertEqual([self
.item
], self
.tree
.GetItemChildren(self
.root
))
506 def testGetItemChildren_RegularItem(self
):
507 self
.assertEqual([self
.child
], self
.tree
.GetItemChildren(self
.item
))
509 def testGetItemChildren_ItemWithoutChildren(self
):
510 self
.assertEqual([], self
.tree
.GetItemChildren(self
.child
))
512 def testGetItemChildren_NoParent_Recursively(self
):
513 self
.assertEqual([self
.item
, self
.child
],
514 self
.tree
.GetItemChildren(recursively
=True))
516 def testGetItemChildren_RootItem_Recursively(self
):
517 self
.assertEqual([self
.item
, self
.child
],
518 self
.tree
.GetItemChildren(self
.root
, True))
520 def testGetItemChildren_RegularItem_Recursively(self
):
521 self
.assertEqual([self
.child
],
522 self
.tree
.GetItemChildren(self
.item
, True))
524 def testGetItemChildren_ItemWithoutChildren_Recursively(self
):
525 self
.assertEqual([], self
.tree
.GetItemChildren(self
.child
, True))
527 def testGetItemByIndex_RootItem(self
):
528 self
.assertEqual(self
.root
, self
.tree
.GetItemByIndex(()))
530 def testGetItemByIndex_RegularItem(self
):
531 self
.assertEqual(self
.item
, self
.tree
.GetItemByIndex((0,)))
533 def testGetItemByIndex_Child(self
):
534 self
.assertEqual(self
.child
, self
.tree
.GetItemByIndex((0,0)))
536 def testGetIndexOfItemRootItem(self
):
537 self
.assertEqual((), self
.tree
.GetIndexOfItem(self
.root
))
539 def testGetIndexOfItemRegularItem(self
):
540 self
.assertEqual((0,), self
.tree
.GetIndexOfItem(self
.item
))
542 def testGetIndexOfItemChild(self
):
543 self
.assertEqual((0,0), self
.tree
.GetIndexOfItem(self
.child
))
546 class TreeHelperWithTreeCtrlTestCase(TreeHelperCommonTests
,
548 TreeClass
= wx
.TreeCtrl
551 class TreeHelperWithTreeListCtrlTestCase(TreeHelperCommonTests
,
553 TreeClass
= wx
.gizmos
.TreeListCtrl
555 def populateTree(self
):
556 self
.tree
.AddColumn('Column')
557 super(TreeHelperWithTreeListCtrlTestCase
, self
).populateTree()
560 class TreeHelperWithCustomTreeCtrlTestCase(TreeHelperCommonTests
,
562 TreeClass
= wx
.lib
.customtreectrl
.CustomTreeCtrl
565 # ExpansionState tests
567 class ExpansionStateTreeCtrl(treemixin
.ExpansionState
, wx
.TreeCtrl
):
571 class GetExpansionStateTestCase(unittest
.TestCase
):
573 self
.frame
= wx
.Frame(None)
574 self
.tree
= ExpansionStateTreeCtrl(self
.frame
)
576 def testEmptyTree(self
):
577 self
.assertEqual([], self
.tree
.GetExpansionState())
580 self
.tree
.AddRoot('Root item')
581 self
.assertEqual([], self
.tree
.GetExpansionState())
583 def testRoot_Child(self
):
584 root
= self
.tree
.AddRoot('Root item')
585 child
= self
.tree
.AppendItem(root
, 'Child')
586 self
.assertEqual([], self
.tree
.GetExpansionState())
588 def testExpandedRoot_Child(self
):
589 root
= self
.tree
.AddRoot('Root item')
590 child
= self
.tree
.AppendItem(root
, 'Child')
591 self
.tree
.Expand(root
)
592 self
.assertEqual([()], self
.tree
.GetExpansionState())
594 def testExpandedRoot_Child_Child(self
):
595 root
= self
.tree
.AddRoot('Root item')
596 child
= self
.tree
.AppendItem(root
, 'Child')
597 grandChild
= self
.tree
.AppendItem(child
, 'Grandchild')
598 self
.tree
.Expand(root
)
599 # Property should work too:
600 self
.assertEqual([()], self
.tree
.ExpansionState
)
602 def testRoot_ExpandedChild_Child(self
):
603 root
= self
.tree
.AddRoot('Root item')
604 child
= self
.tree
.AppendItem(root
, 'Child')
605 grandChild
= self
.tree
.AppendItem(child
, 'Grandchild')
606 self
.tree
.Expand(child
)
607 self
.assertEqual([], self
.tree
.GetExpansionState())
609 def testExpandedRoot_ExpandedChild_Child(self
):
610 root
= self
.tree
.AddRoot('Root item')
611 child
= self
.tree
.AppendItem(root
, 'Child')
612 grandChild
= self
.tree
.AppendItem(child
, 'Grandchild')
613 self
.tree
.Expand(child
)
614 self
.tree
.Expand(root
)
615 self
.assertEqual([(), (0,)], self
.tree
.GetExpansionState())
617 def testExpandedRoot_ExpandedChild_ExpandedChild(self
):
618 root
= self
.tree
.AddRoot('Root item')
619 child
= self
.tree
.AppendItem(root
, 'Child')
620 grandChild
= self
.tree
.AppendItem(child
, 'Grandchild')
621 grandGrandChild
= self
.tree
.AppendItem(grandChild
, 'Grandgrandchild')
622 self
.tree
.Expand(root
)
623 self
.tree
.Expand(child
)
624 self
.tree
.Expand(grandChild
)
625 self
.assertEqual([(), (0,), (0,0)], self
.tree
.GetExpansionState())
628 class SetExpansionStateTestCase(unittest
.TestCase
):
630 self
.frame
= wx
.Frame(None)
631 self
.tree
= ExpansionStateTreeCtrl(self
.frame
)
633 def testEmptyTree(self
):
634 self
.tree
.SetExpansionState([])
637 root
= self
.tree
.AddRoot('Root item')
638 self
.tree
.SetExpansionState([])
639 self
.failIf(self
.tree
.IsExpanded(root
))
641 def testRoot_Child(self
):
642 root
= self
.tree
.AddRoot('Root item')
643 child
= self
.tree
.AppendItem(root
, 'Child')
644 self
.tree
.SetExpansionState([])
645 self
.failIf(self
.tree
.IsExpanded(root
))
647 def testExpandedRoot_Child(self
):
648 root
= self
.tree
.AddRoot('Root item')
649 child
= self
.tree
.AppendItem(root
, 'Child')
650 self
.tree
.ExpansionState
= [()] # Property should work too
651 self
.failUnless(self
.tree
.IsExpanded(root
))
653 def testExpandedRoot_Child_Child(self
):
654 root
= self
.tree
.AddRoot('Root item')
655 child
= self
.tree
.AppendItem(root
, 'Child')
656 grandChild
= self
.tree
.AppendItem(child
, 'Grandchild')
657 self
.tree
.SetExpansionState([()])
658 self
.failIf(self
.tree
.IsExpanded(child
))
660 def testExpandedRoot_ExpandedChild_Child(self
):
661 root
= self
.tree
.AddRoot('Root item')
662 child
= self
.tree
.AppendItem(root
, 'Child')
663 grandChild
= self
.tree
.AppendItem(child
, 'Grandchild')
664 self
.tree
.SetExpansionState([(), (0,)])
665 self
.failUnless(self
.tree
.IsExpanded(child
))
667 def testRoot_ExpandedChild_Child(self
):
668 root
= self
.tree
.AddRoot('Root item')
669 child
= self
.tree
.AppendItem(root
, 'Child')
670 grandChild
= self
.tree
.AppendItem(child
, 'Grandchild')
671 self
.tree
.SetExpansionState([(0,)])
672 self
.failIf(self
.tree
.IsExpanded(child
))
674 def testExpandedRoot_ExpandedChild_ExpandedChild(self
):
675 root
= self
.tree
.AddRoot('Root item')
676 child
= self
.tree
.AppendItem(root
, 'Child')
677 grandChild
= self
.tree
.AppendItem(child
, 'Grandchild')
678 grandGrandChild
= self
.tree
.AppendItem(grandChild
, 'Grandgrandchild')
679 self
.tree
.SetExpansionState([(), (0,), (0,0)])
680 self
.failUnless(self
.tree
.IsExpanded(grandChild
))
683 # Tests of the tree controls without any mixin, to document behaviour that
684 # already works, and works the same, for all tree control widgets
686 class VanillaTreeTestCase(unittest
.TestCase
):
687 style
= wx
.TR_DEFAULT_STYLE
690 self
.frame
= wx
.Frame(None)
691 self
.tree
= self
.TreeClass(self
.frame
, style
=self
.style
)
692 self
.eventsReceived
= []
695 def populateTree(self
):
696 self
.root
= self
.tree
.AddRoot('Root')
697 self
.item
= self
.tree
.AppendItem(self
.root
, 'Item')
698 self
.items
= [self
.root
, self
.item
]
700 def onEvent(self
, event
):
701 self
.eventsReceived
.append(event
)
704 class VanillaTreeCommonTests(object):
705 ''' Tests that should succeed for all tree controls and all styles. '''
707 def testSetItemHasChildren(self
):
708 self
.tree
.SetItemHasChildren(self
.item
, True)
709 self
.failUnless(self
.tree
.ItemHasChildren(self
.item
))
711 def testExpandItemWithPlus(self
):
712 self
.tree
.Bind(wx
.EVT_TREE_ITEM_EXPANDING
, self
.onEvent
)
713 self
.tree
.SetItemHasChildren(self
.item
, True)
714 self
.tree
.Expand(self
.item
)
715 self
.assertEqual(1, len(self
.eventsReceived
))
719 class VanillaTreeCtrlTestCase(VanillaTreeCommonTests
, VanillaTreeTestCase
):
720 TreeClass
= wx
.TreeCtrl
723 class VanillaTreeListCtrlTestCase(VanillaTreeCommonTests
, VanillaTreeTestCase
):
724 TreeClass
= wx
.gizmos
.TreeListCtrl
726 def populateTree(self
):
727 self
.tree
.AddColumn('Column 0')
728 super(VanillaTreeListCtrlTestCase
, self
).populateTree()
731 class VanillaCustomTreeCtrlTestCase(VanillaTreeCommonTests
,
732 VanillaTreeTestCase
):
733 TreeClass
= wx
.lib
.customtreectrl
.CustomTreeCtrl
736 class VanillaTreeItemIdTestCase(unittest
.TestCase
):
737 def testTreeItemIdIsFalseDefault(self
):
738 self
.failIf(wx
.TreeItemId())
741 if __name__
== '__main__':