1 import wx
, wx
.gizmos
, wx
.lib
.customtreectrl
, unittest
5 from wx
.lib
.mixins
import treemixin
10 class TreeCtrl(object):
11 def __init__(self
, *args
, **kwargs
):
19 super(TreeCtrl
, self
).__init
__(*args
, **kwargs
)
21 def OnGetItemFont(self
, index
):
22 return self
.font
.get(index
, wx
.NullFont
)
24 def PrepareItemFont(self
, index
, font
):
25 self
.font
[index
] = font
27 def OnGetItemTextColour(self
, index
):
28 return self
.colour
.get(index
, wx
.NullColour
)
30 def PrepareItemColour(self
, index
, colour
):
31 self
.colour
[index
] = colour
33 def OnGetItemBackgroundColour(self
, index
):
34 return self
.bgcolour
.get(index
, wx
.NullColour
)
36 def PrepareItemBackgroundColour(self
, index
, colour
):
37 self
.bgcolour
[index
] = colour
39 def OnGetItemImage(self
, index
, which
):
40 return self
.image
.get(index
, -1)
42 def PrepareImage(self
, index
, imageIndex
):
43 self
.image
[index
] = imageIndex
45 def OnGetItemType(self
, index
):
46 return self
.type.get(index
, 0)
48 def PrepareType(self
, index
, itemType
):
49 self
.type[index
] = itemType
51 def OnGetItemChecked(self
, index
):
52 return self
.checked
.get(index
, False)
54 def PrepareChecked(self
, index
, checked
):
55 self
.checked
[index
] = checked
58 class VirtualTreeCtrl(TreeCtrl
, treemixin
.VirtualTree
,
59 wx
.lib
.customtreectrl
.CustomTreeCtrl
):
61 def OnGetItemText(self
, indices
):
62 return 'item %s'%'.'.join([str(index
) for index
in indices
])
64 def OnGetChildrenCount(self
, index
=None):
66 return self
.children
.get(index
, 0)
68 def PrepareChildrenCount(self
, index
, childrenCount
):
69 self
.children
[index
] = childrenCount
73 class VirtualTreeCtrlTest_NoRootItems(unittest
.TestCase
):
75 self
.frame
= wx
.Frame(None)
76 self
.tree
= VirtualTreeCtrl(self
.frame
)
77 self
.tree
.RefreshItems()
79 def testNoRootItems(self
):
80 self
.assertEqual(0, self
.tree
.GetCount())
82 def testAddTwoRootItems(self
):
83 self
.tree
.PrepareChildrenCount((), 2)
84 self
.tree
.RefreshItems()
85 self
.assertEqual(2, self
.tree
.GetCount())
87 def testAddOneRootItemAndOneChild(self
):
88 self
.tree
.PrepareChildrenCount((), 1)
89 self
.tree
.PrepareChildrenCount((0,), 1)
90 self
.tree
.RefreshItems()
92 self
.assertEqual(2, self
.tree
.GetCount())
94 def testAddOneRootItemAndTwoChildren(self
):
95 self
.tree
.PrepareChildrenCount((), 1)
96 self
.tree
.PrepareChildrenCount((0,), 2)
97 self
.tree
.RefreshItems()
99 self
.assertEqual(3, self
.tree
.GetCount())
103 class VirtualTreeCtrlTest_OneRoot(unittest
.TestCase
):
105 self
.frame
= wx
.Frame(None)
106 self
.tree
= VirtualTreeCtrl(self
.frame
)
107 self
.tree
.PrepareChildrenCount((), 1)
108 self
.tree
.RefreshItems()
110 def testOneRoot(self
):
111 self
.assertEqual(1, self
.tree
.GetCount())
113 def testDeleteRootItem(self
):
114 self
.tree
.PrepareChildrenCount((), 0)
115 self
.tree
.RefreshItems()
116 self
.assertEqual(0, self
.tree
.GetCount())
118 def testAddOneChild(self
):
119 self
.tree
.PrepareChildrenCount((0,), 1)
120 self
.tree
.RefreshItems()
121 self
.tree
.ExpandAll()
122 self
.assertEqual(2, self
.tree
.GetCount())
124 def testAddTwoChildren(self
):
125 self
.tree
.PrepareChildrenCount((0,), 2)
126 self
.tree
.RefreshItems()
127 self
.tree
.ExpandAll()
128 self
.assertEqual(3, self
.tree
.GetCount())
130 def testChangeFont(self
):
131 self
.tree
.PrepareItemFont((0,), wx
.SMALL_FONT
)
132 self
.tree
.RefreshItems()
133 item
, cookie
= self
.tree
.GetFirstChild(self
.tree
.GetRootItem())
134 self
.assertEqual(wx
.SMALL_FONT
, self
.tree
.GetItemFont(item
))
136 def testChangeColour(self
):
137 self
.tree
.PrepareItemColour((0,), wx
.RED
)
138 self
.tree
.RefreshItems()
139 item
, cookie
= self
.tree
.GetFirstChild(self
.tree
.GetRootItem())
140 self
.assertEqual(wx
.RED
, self
.tree
.GetItemTextColour(item
))
142 def testChangeBackgroundColour(self
):
143 self
.tree
.PrepareItemBackgroundColour((0,), wx
.RED
)
144 self
.tree
.RefreshItems()
145 item
, cookie
= self
.tree
.GetFirstChild(self
.tree
.GetRootItem())
146 self
.assertEqual(wx
.RED
, self
.tree
.GetItemBackgroundColour(item
))
148 def testChangeImage(self
):
149 self
.tree
.PrepareImage((0,), 0)
150 self
.tree
.RefreshItems()
151 item
, cookie
= self
.tree
.GetFirstChild(self
.tree
.GetRootItem())
152 self
.assertEqual(0, self
.tree
.GetItemImage(item
))
154 def testChangeType(self
):
155 self
.tree
.PrepareType((0,), 2)
156 self
.tree
.PrepareChecked((0,), True)
157 self
.tree
.RefreshItems()
158 item
, cookie
= self
.tree
.GetFirstChild(self
.tree
.GetRootItem())
159 self
.failUnless(self
.tree
.IsItemChecked(item
))
161 def testChangeTypeAndAddChildren(self
):
162 self
.tree
.PrepareType((0,), 1)
163 self
.tree
.PrepareChildrenCount((0,), 1)
164 self
.tree
.RefreshItems()
165 item
, cookie
= self
.tree
.GetFirstChild(self
.tree
.GetRootItem())
166 self
.failUnless(self
.tree
.ItemHasChildren(item
))
168 def testRefreshItem(self
):
169 self
.tree
.PrepareItemColour((0,), wx
.RED
)
170 self
.tree
.RefreshItem((0,))
171 item
, cookie
= self
.tree
.GetFirstChild(self
.tree
.GetRootItem())
172 self
.assertEqual(wx
.RED
, self
.tree
.GetItemTextColour(item
))
175 # TreeAPIHarmonizer tests
177 class TreeAPIHarmonizerTestCase(unittest
.TestCase
):
178 style
= wx
.TR_DEFAULT_STYLE
181 self
.frame
= wx
.Frame(None)
182 class HarmonizedTreeCtrl(treemixin
.TreeAPIHarmonizer
, self
.TreeClass
):
184 self
.tree
= HarmonizedTreeCtrl(self
.frame
, style
=self
.style
)
185 self
.eventsReceived
= []
188 def populateTree(self
):
189 self
.root
= self
.tree
.AddRoot('Root')
190 self
.item
= self
.tree
.AppendItem(self
.root
, 'Item')
191 self
.items
= [self
.root
, self
.item
]
193 def onEvent(self
, event
):
194 self
.eventsReceived
.append(event
)
197 class TreeAPIHarmonizerCommonTests(object):
198 ''' Tests that should succeed for all tree controls and all styles. '''
200 def testGetItemType(self
):
201 self
.assertEqual(0, self
.tree
.GetItemType(self
.item
))
203 def testGetItemImage_DefaultIcon(self
):
204 self
.assertEqual(-1, self
.tree
.GetItemImage(self
.item
))
206 def testGetItemImage_SelectedIcon(self
):
208 self
.tree
.GetItemImage(self
.item
, wx
.TreeItemIcon_Selected
))
210 def testGetItemImage_DefaultIcon_OtherColumn(self
):
211 self
.assertEqual(-1, self
.tree
.GetItemImage(self
.item
, column
=1))
213 def testGetItemImage_SelectedIcon_OtherColumn(self
):
215 self
.tree
.GetItemImage(self
.item
, wx
.TreeItemIcon_Selected
, 1))
217 def testSetItemImage_DefaultIcon(self
):
218 self
.tree
.SetItemImage(self
.item
, -1)
219 self
.assertEqual(-1, self
.tree
.GetItemImage(self
.item
))
221 def testSetItemImage_SelectedIcon(self
):
222 self
.tree
.SetItemImage(self
.item
, -1, wx
.TreeItemIcon_Selected
)
224 self
.tree
.GetItemImage(self
.item
, wx
.TreeItemIcon_Selected
))
226 def testSetItemImage_DefaultIcon_OtherColumn(self
):
227 self
.tree
.SetItemImage(self
.item
, -1, column
=1)
228 self
.assertEqual(-1, self
.tree
.GetItemImage(self
.item
, column
=1))
230 def testSetItemImage_SelectedIcon_OtherColumn(self
):
231 self
.tree
.SetItemImage(self
.item
, -1, wx
.TreeItemIcon_Selected
, 1)
233 self
.tree
.GetItemImage(self
.item
, wx
.TreeItemIcon_Selected
, 1))
235 def testExpandAll(self
):
236 self
.tree
.ExpandAll()
238 def testExpandAllChildren(self
):
239 self
.tree
.AppendItem(self
.item
, 'child')
240 self
.tree
.ExpandAllChildren(self
.item
)
241 self
.failUnless(self
.tree
.IsExpanded(self
.item
))
244 class TreeAPIHarmonizerNoTreeListCtrlCommonTests(object):
245 ''' Tests that should succeed for all tree controls, except the
246 TreeListCtrl, and all styles. '''
248 def testGetMainWindow(self
):
249 self
.assertEqual(self
.tree
, self
.tree
.GetMainWindow())
251 def testGetColumnCount(self
):
252 self
.assertEqual(0, self
.tree
.GetColumnCount())
255 class TreeAPIHarmonizerSingleSelectionTests(object):
256 ''' Tests that should succeed for all tree controls when in single
257 selection mode (which is the default selection mode). '''
259 def testUnselectAll(self
):
260 self
.tree
.SelectItem(self
.item
)
261 self
.tree
.UnselectAll()
262 self
.assertEqual([], self
.tree
.GetSelections())
264 def testGetSelections_NoSelection(self
):
265 self
.tree
.UnselectAll()
266 self
.assertEqual([], self
.tree
.GetSelections())
268 def testGetSelections_OneSelectedItem(self
):
269 self
.tree
.UnselectAll()
270 self
.tree
.SelectItem(self
.item
)
271 self
.assertEqual([self
.item
], self
.tree
.GetSelections())
274 class TreeAPIHarmonizerMultipleSelectionTests(object):
275 ''' Tests that should succeed for all tree controls when in multiple
278 style
= wx
.TR_DEFAULT_STYLE | wx
.TR_MULTIPLE
280 def testUnselectAll(self
):
281 self
.tree
.SelectItem(self
.item
)
282 self
.tree
.UnselectAll()
283 self
.assertEqual([], self
.tree
.GetSelections())
285 def testGetSelections_NoSelection(self
):
286 self
.tree
.UnselectAll()
287 self
.assertEqual([], self
.tree
.GetSelections())
289 def testGetSelections_OneSelectedItem(self
):
290 self
.tree
.UnselectAll()
291 self
.tree
.SelectItem(self
.item
)
292 self
.assertEqual([self
.item
], self
.tree
.GetSelections())
294 def testGetSelections_TwoSelectedItems(self
):
295 item2
= self
.tree
.AppendItem(self
.root
, 'item 2')
296 self
.tree
.ExpandAll()
297 self
.tree
.UnselectAll()
298 self
.tree
.SelectItem(item2
)
299 self
.tree
.SelectItem(self
.item
)
300 self
.assertEqual([self
.item
, item2
], self
.tree
.GetSelections())
303 class TreeAPIHarmonizerVisibleRootTests(object):
304 ''' Tests that should succeed for all tree controls when the root item
307 def testGetCount(self
):
308 self
.assertEqual(len(self
.items
), self
.tree
.GetCount())
310 def testSelectRoot(self
):
311 self
.tree
.SelectItem(self
.root
)
312 self
.assertEqual([self
.root
], self
.tree
.GetSelections())
315 class TreeAPIHarmonizerHiddenRootTests(object):
316 ''' Tests that should succeed for all tree controls when the root item
319 style
= wx
.TR_DEFAULT_STYLE | wx
.TR_HIDE_ROOT
321 def testGetCount(self
):
322 self
.assertEqual(len(self
.items
) - 1, self
.tree
.GetCount())
324 def testSelectRoot(self
):
325 self
.tree
.SelectItem(self
.root
)
326 self
.assertEqual([], self
.tree
.GetSelections())
329 class TreeAPIHarmonizerWithTreeCtrlTestCase(TreeAPIHarmonizerTestCase
):
330 TreeClass
= wx
.TreeCtrl
333 class TreeAPIHarmonizerWithTreeCtrlCommonTests( \
334 TreeAPIHarmonizerCommonTests
,
335 TreeAPIHarmonizerNoTreeListCtrlCommonTests
):
339 class TreeAPIHarmonizerWithTreeCtrl_SingleSelection( \
340 TreeAPIHarmonizerWithTreeCtrlCommonTests
,
341 TreeAPIHarmonizerSingleSelectionTests
,
342 TreeAPIHarmonizerWithTreeCtrlTestCase
):
346 class TreeAPIHarmonizerWithTreeCtrl_MultipleSelection( \
347 TreeAPIHarmonizerWithTreeCtrlCommonTests
,
348 TreeAPIHarmonizerMultipleSelectionTests
,
349 TreeAPIHarmonizerWithTreeCtrlTestCase
):
353 class TreeAPIHarmonizerWithTreeCtrl_VisibleRoot( \
354 TreeAPIHarmonizerWithTreeCtrlCommonTests
,
355 TreeAPIHarmonizerVisibleRootTests
,
356 TreeAPIHarmonizerWithTreeCtrlTestCase
):
360 class TreeAPIHarmonizerWithTreeCtrl_HiddenRoot( \
361 TreeAPIHarmonizerWithTreeCtrlCommonTests
,
362 TreeAPIHarmonizerHiddenRootTests
,
363 TreeAPIHarmonizerWithTreeCtrlTestCase
):
367 class TreeAPIHarmonizerWithTreeListCtrlTestCase(TreeAPIHarmonizerTestCase
):
368 TreeClass
= wx
.gizmos
.TreeListCtrl
370 def populateTree(self
):
371 self
.tree
.AddColumn('Column')
372 super(TreeAPIHarmonizerWithTreeListCtrlTestCase
, self
).populateTree()
375 class TreeAPIHarmonizerWithTreeListCtrlCommonTests( \
376 TreeAPIHarmonizerCommonTests
):
378 def testGetColumnCount(self
):
379 self
.assertEqual(1, self
.tree
.GetColumnCount())
381 def testGetMainWindow(self
):
382 self
.assertNotEqual(self
.tree
, self
.tree
.GetMainWindow())
385 class TreeAPIHarmonizerWithTreeListCtrl_SingleSelection( \
386 TreeAPIHarmonizerWithTreeListCtrlCommonTests
,
387 TreeAPIHarmonizerSingleSelectionTests
,
388 TreeAPIHarmonizerWithTreeListCtrlTestCase
):
392 class TreeAPIHarmonizerWithTreeListCtrl_MultipleSelection( \
393 TreeAPIHarmonizerWithTreeListCtrlCommonTests
,
394 TreeAPIHarmonizerMultipleSelectionTests
,
395 TreeAPIHarmonizerWithTreeListCtrlTestCase
):
397 def testGetSelections_TwoSelectedItems(self
):
398 ''' Override TreeAPIHarmonizerMultipleSelectionTests.-
399 testGetSelections_TwoSelectedItems, because
400 TreeListCtrl.SelectItem needs an extra parameter. '''
401 self
.tree
.UnselectAll()
402 item2
= self
.tree
.AppendItem(self
.root
, 'item 2')
403 self
.tree
.SelectItem(self
.item
)
404 self
.tree
.SelectItem(item2
, unselect_others
=False)
405 self
.assertEqual([self
.item
, item2
], self
.tree
.GetSelections())
408 class TreeAPIHarmonizerWithTreeListCtrl_VisibleRoot( \
409 TreeAPIHarmonizerWithTreeListCtrlCommonTests
,
410 TreeAPIHarmonizerVisibleRootTests
,
411 TreeAPIHarmonizerWithTreeListCtrlTestCase
):
415 class TreeAPIHarmonizerWithTreeListCtrl_HiddenRoot( \
416 TreeAPIHarmonizerWithTreeListCtrlCommonTests
,
417 TreeAPIHarmonizerHiddenRootTests
,
418 TreeAPIHarmonizerWithTreeListCtrlTestCase
):
422 class TreeAPIHarmonizerWithCustomTreeCtrlTestCase(TreeAPIHarmonizerTestCase
):
423 TreeClass
= wx
.lib
.customtreectrl
.CustomTreeCtrl
426 class TreeAPIHarmonizerWithCustomTreeCtrlCommonTests( \
427 TreeAPIHarmonizerCommonTests
,
428 TreeAPIHarmonizerNoTreeListCtrlCommonTests
):
430 def testGetCheckItemType(self
):
431 item
= self
.tree
.AppendItem(self
.root
, 'item', ct_type
=1)
432 self
.assertEqual(1, self
.tree
.GetItemType(item
))
434 def testGetRadioItemType(self
):
435 item
= self
.tree
.AppendItem(self
.root
, 'item', ct_type
=2)
436 self
.assertEqual(2, self
.tree
.GetItemType(item
))
440 class TreeAPIHarmonizerWithCustomTreeCtrl_SingleSelection( \
441 TreeAPIHarmonizerWithCustomTreeCtrlCommonTests
,
442 TreeAPIHarmonizerSingleSelectionTests
,
443 TreeAPIHarmonizerWithCustomTreeCtrlTestCase
):
447 class TreeAPIHarmonizerWithCustomTreeCtrl_MultipleSelection( \
448 TreeAPIHarmonizerWithCustomTreeCtrlCommonTests
,
449 TreeAPIHarmonizerMultipleSelectionTests
,
450 TreeAPIHarmonizerWithCustomTreeCtrlTestCase
):
454 class TreeAPIHarmonizerWithCustomTreeCtrl_VisibleRoot( \
455 TreeAPIHarmonizerWithCustomTreeCtrlCommonTests
,
456 TreeAPIHarmonizerVisibleRootTests
,
457 TreeAPIHarmonizerWithCustomTreeCtrlTestCase
):
461 class TreeAPIHarmonizerWithCustomTreeCtrl_HiddenRoot( \
462 TreeAPIHarmonizerWithCustomTreeCtrlCommonTests
,
463 TreeAPIHarmonizerHiddenRootTests
,
464 TreeAPIHarmonizerWithCustomTreeCtrlTestCase
):
470 class TreeHelperTestCase(unittest
.TestCase
):
471 style
= wx
.TR_DEFAULT_STYLE
474 self
.frame
= wx
.Frame(None)
475 class HelperTreeCtrl(treemixin
.TreeHelper
,
476 treemixin
.TreeAPIHarmonizer
, self
.TreeClass
):
478 self
.tree
= HelperTreeCtrl(self
.frame
, style
=self
.style
)
481 def populateTree(self
):
482 self
.root
= self
.tree
.AddRoot('Root')
483 self
.item
= self
.tree
.AppendItem(self
.root
, 'Item')
484 self
.child
= self
.tree
.AppendItem(self
.item
, 'Child')
487 class TreeHelperCommonTests(object):
488 def testGetItemChildren_EmptyTree(self
):
489 self
.tree
.DeleteAllItems()
490 self
.assertEqual([], self
.tree
.GetItemChildren())
492 def testGetItemChildren_NoParent(self
):
493 self
.assertEqual([self
.item
], self
.tree
.GetItemChildren())
495 def testGetItemChildren_RootItem(self
):
496 self
.assertEqual([self
.item
], self
.tree
.GetItemChildren(self
.root
))
498 def testGetItemChildren_RegularItem(self
):
499 self
.assertEqual([self
.child
], self
.tree
.GetItemChildren(self
.item
))
501 def testGetItemChildren_ItemWithoutChildren(self
):
502 self
.assertEqual([], self
.tree
.GetItemChildren(self
.child
))
504 def testGetItemChildren_NoParent_Recursively(self
):
505 self
.assertEqual([self
.item
, self
.child
],
506 self
.tree
.GetItemChildren(recursively
=True))
508 def testGetItemChildren_RootItem_Recursively(self
):
509 self
.assertEqual([self
.item
, self
.child
],
510 self
.tree
.GetItemChildren(self
.root
, True))
512 def testGetItemChildren_RegularItem_Recursively(self
):
513 self
.assertEqual([self
.child
],
514 self
.tree
.GetItemChildren(self
.item
, True))
516 def testGetItemChildren_ItemWithoutChildren_Recursively(self
):
517 self
.assertEqual([], self
.tree
.GetItemChildren(self
.child
, True))
519 def testGetItemByIndex_RootItem(self
):
520 self
.assertEqual(self
.root
, self
.tree
.GetItemByIndex(()))
522 def testGetItemByIndex_RegularItem(self
):
523 self
.assertEqual(self
.item
, self
.tree
.GetItemByIndex((0,)))
525 def testGetItemByIndex_Child(self
):
526 self
.assertEqual(self
.child
, self
.tree
.GetItemByIndex((0,0)))
528 def testGetIndexOfItemRootItem(self
):
529 self
.assertEqual((), self
.tree
.GetIndexOfItem(self
.root
))
531 def testGetIndexOfItemRegularItem(self
):
532 self
.assertEqual((0,), self
.tree
.GetIndexOfItem(self
.item
))
534 def testGetIndexOfItemChild(self
):
535 self
.assertEqual((0,0), self
.tree
.GetIndexOfItem(self
.child
))
538 class TreeHelperWithTreeCtrlTestCase(TreeHelperCommonTests
,
540 TreeClass
= wx
.TreeCtrl
543 class TreeHelperWithTreeListCtrlTestCase(TreeHelperCommonTests
,
545 TreeClass
= wx
.gizmos
.TreeListCtrl
547 def populateTree(self
):
548 self
.tree
.AddColumn('Column')
549 super(TreeHelperWithTreeListCtrlTestCase
, self
).populateTree()
552 class TreeHelperWithCustomTreeCtrlTestCase(TreeHelperCommonTests
,
554 TreeClass
= wx
.lib
.customtreectrl
.CustomTreeCtrl
557 # ExpansionState tests
559 class ExpansionStateTreeCtrl(treemixin
.ExpansionState
, wx
.TreeCtrl
):
563 class GetExpansionStateTestCase(unittest
.TestCase
):
565 self
.frame
= wx
.Frame(None)
566 self
.tree
= ExpansionStateTreeCtrl(self
.frame
)
568 def testEmptyTree(self
):
569 self
.assertEqual([], self
.tree
.GetExpansionState())
572 self
.tree
.AddRoot('Root item')
573 self
.assertEqual([], self
.tree
.GetExpansionState())
575 def testRoot_Child(self
):
576 root
= self
.tree
.AddRoot('Root item')
577 child
= self
.tree
.AppendItem(root
, 'Child')
578 self
.assertEqual([], self
.tree
.GetExpansionState())
580 def testExpandedRoot_Child(self
):
581 root
= self
.tree
.AddRoot('Root item')
582 child
= self
.tree
.AppendItem(root
, 'Child')
583 self
.tree
.Expand(root
)
584 self
.assertEqual([()], self
.tree
.GetExpansionState())
586 def testExpandedRoot_Child_Child(self
):
587 root
= self
.tree
.AddRoot('Root item')
588 child
= self
.tree
.AppendItem(root
, 'Child')
589 grandChild
= self
.tree
.AppendItem(child
, 'Grandchild')
590 self
.tree
.Expand(root
)
591 # Property should work too:
592 self
.assertEqual([()], self
.tree
.ExpansionState
)
594 def testRoot_ExpandedChild_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(child
)
599 self
.assertEqual([], self
.tree
.GetExpansionState())
601 def testExpandedRoot_ExpandedChild_Child(self
):
602 root
= self
.tree
.AddRoot('Root item')
603 child
= self
.tree
.AppendItem(root
, 'Child')
604 grandChild
= self
.tree
.AppendItem(child
, 'Grandchild')
605 self
.tree
.Expand(child
)
606 self
.tree
.Expand(root
)
607 self
.assertEqual([(), (0,)], self
.tree
.GetExpansionState())
609 def testExpandedRoot_ExpandedChild_ExpandedChild(self
):
610 root
= self
.tree
.AddRoot('Root item')
611 child
= self
.tree
.AppendItem(root
, 'Child')
612 grandChild
= self
.tree
.AppendItem(child
, 'Grandchild')
613 grandGrandChild
= self
.tree
.AppendItem(grandChild
, 'Grandgrandchild')
614 self
.tree
.Expand(root
)
615 self
.tree
.Expand(child
)
616 self
.tree
.Expand(grandChild
)
617 self
.assertEqual([(), (0,), (0,0)], self
.tree
.GetExpansionState())
620 class SetExpansionStateTestCase(unittest
.TestCase
):
622 self
.frame
= wx
.Frame(None)
623 self
.tree
= ExpansionStateTreeCtrl(self
.frame
)
625 def testEmptyTree(self
):
626 self
.tree
.SetExpansionState([])
629 root
= self
.tree
.AddRoot('Root item')
630 self
.tree
.SetExpansionState([])
631 self
.failIf(self
.tree
.IsExpanded(root
))
633 def testRoot_Child(self
):
634 root
= self
.tree
.AddRoot('Root item')
635 child
= self
.tree
.AppendItem(root
, 'Child')
636 self
.tree
.SetExpansionState([])
637 self
.failIf(self
.tree
.IsExpanded(root
))
639 def testExpandedRoot_Child(self
):
640 root
= self
.tree
.AddRoot('Root item')
641 child
= self
.tree
.AppendItem(root
, 'Child')
642 self
.tree
.ExpansionState
= [()] # Property should work too
643 self
.failUnless(self
.tree
.IsExpanded(root
))
645 def testExpandedRoot_Child_Child(self
):
646 root
= self
.tree
.AddRoot('Root item')
647 child
= self
.tree
.AppendItem(root
, 'Child')
648 grandChild
= self
.tree
.AppendItem(child
, 'Grandchild')
649 self
.tree
.SetExpansionState([()])
650 self
.failIf(self
.tree
.IsExpanded(child
))
652 def testExpandedRoot_ExpandedChild_Child(self
):
653 root
= self
.tree
.AddRoot('Root item')
654 child
= self
.tree
.AppendItem(root
, 'Child')
655 grandChild
= self
.tree
.AppendItem(child
, 'Grandchild')
656 self
.tree
.SetExpansionState([(), (0,)])
657 self
.failUnless(self
.tree
.IsExpanded(child
))
659 def testRoot_ExpandedChild_Child(self
):
660 root
= self
.tree
.AddRoot('Root item')
661 child
= self
.tree
.AppendItem(root
, 'Child')
662 grandChild
= self
.tree
.AppendItem(child
, 'Grandchild')
663 self
.tree
.SetExpansionState([(0,)])
664 self
.failIf(self
.tree
.IsExpanded(child
))
666 def testExpandedRoot_ExpandedChild_ExpandedChild(self
):
667 root
= self
.tree
.AddRoot('Root item')
668 child
= self
.tree
.AppendItem(root
, 'Child')
669 grandChild
= self
.tree
.AppendItem(child
, 'Grandchild')
670 grandGrandChild
= self
.tree
.AppendItem(grandChild
, 'Grandgrandchild')
671 self
.tree
.SetExpansionState([(), (0,), (0,0)])
672 self
.failUnless(self
.tree
.IsExpanded(grandChild
))
675 # Tests of the tree controls without any mixin, to document behaviour that
676 # already works, and works the same, for all tree control widgets
678 class VanillaTreeTestCase(unittest
.TestCase
):
679 style
= wx
.TR_DEFAULT_STYLE
682 self
.frame
= wx
.Frame(None)
683 self
.tree
= self
.TreeClass(self
.frame
, style
=self
.style
)
684 self
.eventsReceived
= []
687 def populateTree(self
):
688 self
.root
= self
.tree
.AddRoot('Root')
689 self
.item
= self
.tree
.AppendItem(self
.root
, 'Item')
690 self
.items
= [self
.root
, self
.item
]
692 def onEvent(self
, event
):
693 self
.eventsReceived
.append(event
)
696 class VanillaTreeCommonTests(object):
697 ''' Tests that should succeed for all tree controls and all styles. '''
699 def testSetItemHasChildren(self
):
700 self
.tree
.SetItemHasChildren(self
.item
, True)
701 self
.failUnless(self
.tree
.ItemHasChildren(self
.item
))
703 def testExpandItemWithPlus(self
):
704 self
.tree
.Bind(wx
.EVT_TREE_ITEM_EXPANDING
, self
.onEvent
)
705 self
.tree
.SetItemHasChildren(self
.item
, True)
706 self
.tree
.Expand(self
.item
)
707 self
.assertEqual(1, len(self
.eventsReceived
))
710 class VanillaTreeCtrlTestCase(VanillaTreeCommonTests
, VanillaTreeTestCase
):
711 TreeClass
= wx
.TreeCtrl
714 class VanillaTreeListCtrlTestCase(VanillaTreeCommonTests
, VanillaTreeTestCase
):
715 TreeClass
= wx
.gizmos
.TreeListCtrl
717 def populateTree(self
):
718 self
.tree
.AddColumn('Column 0')
719 super(VanillaTreeListCtrlTestCase
, self
).populateTree()
722 class VanillaCustomTreeCtrlTestCase(VanillaTreeCommonTests
,
723 VanillaTreeTestCase
):
724 TreeClass
= wx
.lib
.customtreectrl
.CustomTreeCtrl
727 # Tests of the tree controls without any mixin, to document behaviour
728 # that is either different between tree control widgets or undesired
731 class TreeCtrlTestCase(unittest
.TestCase
):
733 self
.frame
= wx
.Frame(None)
734 self
.tree
= wx
.TreeCtrl(self
.frame
, style
=wx
.TR_HIDE_ROOT
)
736 def testSelectHiddenRootItem(self
):
737 root
= self
.tree
.AddRoot('Hidden root')
738 self
.tree
.SelectItem(root
)
739 self
.assertEqual(root
, self
.tree
.GetSelection())
742 class CustomTreeCtrlTestCase(unittest
.TestCase
):
744 self
.frame
= wx
.Frame(None)
745 self
.tree
= wx
.lib
.customtreectrl
.CustomTreeCtrl(self
.frame
,
746 style
=wx
.TR_HIDE_ROOT
)
748 def testSelectHiddenRootItem(self
):
749 root
= self
.tree
.AddRoot('Hidden root')
750 self
.tree
.SelectItem(root
)
751 self
.assertEqual(root
, self
.tree
.GetSelection())
754 if __name__
== '__main__':