1 import wx
, wx
.gizmos
, wx
.lib
.customtreectrl
, unittest
5 from wx
.lib
.mixins
import treemixin
11 class TreeCtrl(object):
12 def __init__(self
, *args
, **kwargs
):
20 super(TreeCtrl
, self
).__init
__(*args
, **kwargs
)
22 def OnGetItemFont(self
, index
):
23 return self
.font
.get(index
, wx
.NullFont
)
25 def PrepareItemFont(self
, index
, font
):
26 self
.font
[index
] = font
28 def OnGetItemTextColour(self
, index
):
29 return self
.colour
.get(index
, wx
.NullColour
)
31 def PrepareItemColour(self
, index
, colour
):
32 self
.colour
[index
] = colour
34 def OnGetItemBackgroundColour(self
, index
):
35 return self
.bgcolour
.get(index
, wx
.NullColour
)
37 def PrepareItemBackgroundColour(self
, index
, colour
):
38 self
.bgcolour
[index
] = colour
40 def OnGetItemImage(self
, index
, which
):
41 return self
.image
.get(index
, -1)
43 def PrepareImage(self
, index
, imageIndex
):
44 self
.image
[index
] = imageIndex
46 def OnGetItemType(self
, index
):
47 return self
.type.get(index
, 0)
49 def PrepareType(self
, index
, itemType
):
50 self
.type[index
] = itemType
52 def OnGetItemChecked(self
, index
):
53 return self
.checked
.get(index
, False)
55 def PrepareChecked(self
, index
, checked
):
56 self
.checked
[index
] = checked
59 class VirtualTreeCtrl(TreeCtrl
, treemixin
.VirtualTree
,
60 wx
.lib
.customtreectrl
.CustomTreeCtrl
):
62 def OnGetItemText(self
, indices
):
63 return 'item %s'%'.'.join([str(index
) for index
in indices
])
65 def OnGetChildrenCount(self
, index
=None):
67 return self
.children
.get(index
, 0)
69 def PrepareChildrenCount(self
, index
, childrenCount
):
70 self
.children
[index
] = childrenCount
74 class VirtualTreeCtrlTest_NoRootItems(unittest
.TestCase
):
76 self
.frame
= wx
.Frame(None)
77 self
.tree
= VirtualTreeCtrl(self
.frame
)
78 self
.tree
.RefreshItems()
80 def testNoRootItems(self
):
81 self
.assertEqual(0, self
.tree
.GetCount())
83 def testAddTwoRootItems(self
):
84 self
.tree
.PrepareChildrenCount((), 2)
85 self
.tree
.RefreshItems()
86 self
.assertEqual(2, self
.tree
.GetCount())
88 def testAddOneRootItemAndOneChild(self
):
89 self
.tree
.PrepareChildrenCount((), 1)
90 self
.tree
.PrepareChildrenCount((0,), 1)
91 self
.tree
.RefreshItems()
93 self
.assertEqual(2, self
.tree
.GetCount())
95 def testAddOneRootItemAndTwoChildren(self
):
96 self
.tree
.PrepareChildrenCount((), 1)
97 self
.tree
.PrepareChildrenCount((0,), 2)
98 self
.tree
.RefreshItems()
100 self
.assertEqual(3, self
.tree
.GetCount())
104 class VirtualTreeCtrlTest_OneRoot(unittest
.TestCase
):
106 self
.frame
= wx
.Frame(None)
107 self
.tree
= VirtualTreeCtrl(self
.frame
)
108 self
.tree
.PrepareChildrenCount((), 1)
109 self
.tree
.RefreshItems()
111 def testOneRoot(self
):
112 self
.assertEqual(1, self
.tree
.GetCount())
114 def testDeleteRootItem(self
):
115 self
.tree
.PrepareChildrenCount((), 0)
116 self
.tree
.RefreshItems()
117 self
.assertEqual(0, self
.tree
.GetCount())
119 def testAddOneChild(self
):
120 self
.tree
.PrepareChildrenCount((0,), 1)
121 self
.tree
.RefreshItems()
122 self
.tree
.ExpandAll()
123 self
.assertEqual(2, self
.tree
.GetCount())
125 def testAddTwoChildren(self
):
126 self
.tree
.PrepareChildrenCount((0,), 2)
127 self
.tree
.RefreshItems()
128 self
.tree
.ExpandAll()
129 self
.assertEqual(3, self
.tree
.GetCount())
131 def testChangeFont(self
):
132 self
.tree
.PrepareItemFont((0,), wx
.SMALL_FONT
)
133 self
.tree
.RefreshItems()
134 item
, cookie
= self
.tree
.GetFirstChild(self
.tree
.GetRootItem())
135 self
.assertEqual(wx
.SMALL_FONT
, self
.tree
.GetItemFont(item
))
137 def testChangeColour(self
):
138 self
.tree
.PrepareItemColour((0,), wx
.RED
)
139 self
.tree
.RefreshItems()
140 item
, cookie
= self
.tree
.GetFirstChild(self
.tree
.GetRootItem())
141 self
.assertEqual(wx
.RED
, self
.tree
.GetItemTextColour(item
))
143 def testChangeBackgroundColour(self
):
144 self
.tree
.PrepareItemBackgroundColour((0,), wx
.RED
)
145 self
.tree
.RefreshItems()
146 item
, cookie
= self
.tree
.GetFirstChild(self
.tree
.GetRootItem())
147 self
.assertEqual(wx
.RED
, self
.tree
.GetItemBackgroundColour(item
))
149 def testChangeImage(self
):
150 self
.tree
.PrepareImage((0,), 0)
151 self
.tree
.RefreshItems()
152 item
, cookie
= self
.tree
.GetFirstChild(self
.tree
.GetRootItem())
153 self
.assertEqual(0, self
.tree
.GetItemImage(item
))
155 def testChangeType(self
):
156 self
.tree
.PrepareType((0,), 2)
157 self
.tree
.PrepareChecked((0,), True)
158 self
.tree
.RefreshItems()
159 item
, cookie
= self
.tree
.GetFirstChild(self
.tree
.GetRootItem())
160 self
.failUnless(self
.tree
.IsItemChecked(item
))
162 def testChangeTypeAndAddChildren(self
):
163 self
.tree
.PrepareType((0,), 1)
164 self
.tree
.PrepareChildrenCount((0,), 1)
165 self
.tree
.RefreshItems()
166 item
, cookie
= self
.tree
.GetFirstChild(self
.tree
.GetRootItem())
167 self
.failUnless(self
.tree
.ItemHasChildren(item
))
169 def testRefreshItem(self
):
170 self
.tree
.PrepareItemColour((0,), wx
.RED
)
171 self
.tree
.RefreshItem((0,))
172 item
, cookie
= self
.tree
.GetFirstChild(self
.tree
.GetRootItem())
173 self
.assertEqual(wx
.RED
, self
.tree
.GetItemTextColour(item
))
175 def testRefreshItemThatHasNoCorrespondingNodeInTheTree(self
):
176 # The item to refresh might be in a collapsed branch of the tree
177 # and not present yet.
178 self
.tree
.PrepareChildrenCount((0,), 1)
179 self
.tree
.RefreshItem((0,0))
180 item
, cookie
= self
.tree
.GetFirstChild(self
.tree
.GetRootItem())
181 self
.failIf(self
.tree
.ItemHasChildren(item
))
184 # TreeAPIHarmonizer tests
186 class TreeAPIHarmonizerTestCase(unittest
.TestCase
):
187 style
= wx
.TR_DEFAULT_STYLE
190 self
.frame
= wx
.Frame(None)
191 class HarmonizedTreeCtrl(treemixin
.TreeAPIHarmonizer
, self
.TreeClass
):
193 self
.tree
= HarmonizedTreeCtrl(self
.frame
, style
=self
.style
)
194 self
.eventsReceived
= []
197 def populateTree(self
):
198 self
.root
= self
.tree
.AddRoot('Root')
199 self
.item
= self
.tree
.AppendItem(self
.root
, 'Item')
200 self
.items
= [self
.root
, self
.item
]
202 def onEvent(self
, event
):
203 self
.eventsReceived
.append(event
)
206 class TreeAPIHarmonizerCommonTests(object):
207 ''' Tests that should succeed for all tree controls and all styles. '''
209 def testGetItemType(self
):
210 self
.assertEqual(0, self
.tree
.GetItemType(self
.item
))
212 def testGetItemImage_DefaultIcon(self
):
213 self
.assertEqual(-1, self
.tree
.GetItemImage(self
.item
))
215 def testGetItemImage_SelectedIcon(self
):
217 self
.tree
.GetItemImage(self
.item
, wx
.TreeItemIcon_Selected
))
219 def testGetItemImage_DefaultIcon_OtherColumn(self
):
220 self
.assertEqual(-1, self
.tree
.GetItemImage(self
.item
, column
=1))
222 def testGetItemImage_SelectedIcon_OtherColumn(self
):
224 self
.tree
.GetItemImage(self
.item
, wx
.TreeItemIcon_Selected
, 1))
226 def testSetItemImage_DefaultIcon(self
):
227 self
.tree
.SetItemImage(self
.item
, -1)
228 self
.assertEqual(-1, self
.tree
.GetItemImage(self
.item
))
230 def testSetItemImage_SelectedIcon(self
):
231 self
.tree
.SetItemImage(self
.item
, -1, wx
.TreeItemIcon_Selected
)
233 self
.tree
.GetItemImage(self
.item
, wx
.TreeItemIcon_Selected
))
235 def testSetItemImage_DefaultIcon_OtherColumn(self
):
236 self
.tree
.SetItemImage(self
.item
, -1, column
=1)
237 self
.assertEqual(-1, self
.tree
.GetItemImage(self
.item
, column
=1))
239 def testSetItemImage_SelectedIcon_OtherColumn(self
):
240 self
.tree
.SetItemImage(self
.item
, -1, wx
.TreeItemIcon_Selected
, 1)
242 self
.tree
.GetItemImage(self
.item
, wx
.TreeItemIcon_Selected
, 1))
244 def testExpandAll(self
):
245 self
.tree
.ExpandAll()
247 def testExpandAllChildren(self
):
248 self
.tree
.AppendItem(self
.item
, 'child')
249 self
.tree
.ExpandAllChildren(self
.item
)
250 self
.failUnless(self
.tree
.IsExpanded(self
.item
))
252 def testGetFirstVisibleItem(self
):
253 self
.tree
.DeleteAllItems()
254 self
.failIf(self
.tree
.GetFirstVisibleItem())
257 class TreeAPIHarmonizerNoTreeListCtrlCommonTests(object):
258 ''' Tests that should succeed for all tree controls, except the
259 TreeListCtrl, and all styles. '''
261 def testGetMainWindow(self
):
262 self
.assertEqual(self
.tree
, self
.tree
.GetMainWindow())
264 def testGetColumnCount(self
):
265 self
.assertEqual(0, self
.tree
.GetColumnCount())
268 class TreeAPIHarmonizerSingleSelectionTests(object):
269 ''' Tests that should succeed for all tree controls when in single
270 selection mode (which is the default selection mode). '''
272 def testUnselectAll(self
):
273 self
.tree
.SelectItem(self
.item
)
274 self
.tree
.UnselectAll()
275 self
.assertEqual([], self
.tree
.GetSelections())
277 def testGetSelections_NoSelection(self
):
278 self
.tree
.UnselectAll()
279 self
.assertEqual([], self
.tree
.GetSelections())
281 def testGetSelections_OneSelectedItem(self
):
282 self
.tree
.UnselectAll()
283 self
.tree
.SelectItem(self
.item
)
284 self
.assertEqual([self
.item
], self
.tree
.GetSelections())
287 class TreeAPIHarmonizerMultipleSelectionTests(object):
288 ''' Tests that should succeed for all tree controls when in multiple
291 style
= wx
.TR_DEFAULT_STYLE | wx
.TR_MULTIPLE
293 def testUnselectAll(self
):
294 self
.tree
.SelectItem(self
.item
)
295 self
.tree
.UnselectAll()
296 self
.assertEqual([], self
.tree
.GetSelections())
298 def testGetSelections_NoSelection(self
):
299 self
.tree
.UnselectAll()
300 self
.assertEqual([], self
.tree
.GetSelections())
302 def testGetSelections_OneSelectedItem(self
):
303 self
.tree
.UnselectAll()
304 self
.tree
.SelectItem(self
.item
)
305 self
.assertEqual([self
.item
], self
.tree
.GetSelections())
307 def testGetSelections_TwoSelectedItems(self
):
308 item2
= self
.tree
.AppendItem(self
.root
, 'item 2')
309 self
.tree
.ExpandAll()
310 self
.tree
.UnselectAll()
311 self
.tree
.SelectItem(item2
)
312 self
.tree
.SelectItem(self
.item
)
313 self
.assertEqual([self
.item
, item2
], self
.tree
.GetSelections())
316 class TreeAPIHarmonizerVisibleRootTests(object):
317 ''' Tests that should succeed for all tree controls when the root item
320 def testGetCount(self
):
321 self
.assertEqual(len(self
.items
), self
.tree
.GetCount())
323 def testSelectRoot(self
):
324 self
.tree
.SelectItem(self
.root
)
325 self
.assertEqual([self
.root
], self
.tree
.GetSelections())
328 class TreeAPIHarmonizerHiddenRootTests(object):
329 ''' Tests that should succeed for all tree controls when the root item
332 style
= wx
.TR_DEFAULT_STYLE | wx
.TR_HIDE_ROOT
334 def testGetCount(self
):
335 self
.assertEqual(len(self
.items
) - 1, self
.tree
.GetCount())
337 def testSelectRoot(self
):
338 self
.tree
.SelectItem(self
.root
)
339 self
.assertEqual([], self
.tree
.GetSelections())
342 class TreeAPIHarmonizerWithTreeCtrlTestCase(TreeAPIHarmonizerTestCase
):
343 TreeClass
= wx
.TreeCtrl
346 class TreeAPIHarmonizerWithTreeCtrlCommonTests( \
347 TreeAPIHarmonizerCommonTests
,
348 TreeAPIHarmonizerNoTreeListCtrlCommonTests
):
352 class TreeAPIHarmonizerWithTreeCtrl_SingleSelection( \
353 TreeAPIHarmonizerWithTreeCtrlCommonTests
,
354 TreeAPIHarmonizerSingleSelectionTests
,
355 TreeAPIHarmonizerWithTreeCtrlTestCase
):
359 class TreeAPIHarmonizerWithTreeCtrl_MultipleSelection( \
360 TreeAPIHarmonizerWithTreeCtrlCommonTests
,
361 TreeAPIHarmonizerMultipleSelectionTests
,
362 TreeAPIHarmonizerWithTreeCtrlTestCase
):
366 class TreeAPIHarmonizerWithTreeCtrl_VisibleRoot( \
367 TreeAPIHarmonizerWithTreeCtrlCommonTests
,
368 TreeAPIHarmonizerVisibleRootTests
,
369 TreeAPIHarmonizerWithTreeCtrlTestCase
):
373 class TreeAPIHarmonizerWithTreeCtrl_HiddenRoot( \
374 TreeAPIHarmonizerWithTreeCtrlCommonTests
,
375 TreeAPIHarmonizerHiddenRootTests
,
376 TreeAPIHarmonizerWithTreeCtrlTestCase
):
380 class TreeAPIHarmonizerWithTreeListCtrlTestCase(TreeAPIHarmonizerTestCase
):
381 TreeClass
= wx
.gizmos
.TreeListCtrl
383 def populateTree(self
):
384 self
.tree
.AddColumn('Column')
385 super(TreeAPIHarmonizerWithTreeListCtrlTestCase
, self
).populateTree()
388 class TreeAPIHarmonizerWithTreeListCtrlCommonTests( \
389 TreeAPIHarmonizerCommonTests
):
391 def testGetColumnCount(self
):
392 self
.assertEqual(1, self
.tree
.GetColumnCount())
394 def testGetMainWindow(self
):
395 self
.assertNotEqual(self
.tree
, self
.tree
.GetMainWindow())
398 class TreeAPIHarmonizerWithTreeListCtrl_SingleSelection( \
399 TreeAPIHarmonizerWithTreeListCtrlCommonTests
,
400 TreeAPIHarmonizerSingleSelectionTests
,
401 TreeAPIHarmonizerWithTreeListCtrlTestCase
):
405 class TreeAPIHarmonizerWithTreeListCtrl_MultipleSelection( \
406 TreeAPIHarmonizerWithTreeListCtrlCommonTests
,
407 TreeAPIHarmonizerMultipleSelectionTests
,
408 TreeAPIHarmonizerWithTreeListCtrlTestCase
):
410 def testGetSelections_TwoSelectedItems(self
):
411 ''' Override TreeAPIHarmonizerMultipleSelectionTests.-
412 testGetSelections_TwoSelectedItems, because
413 TreeListCtrl.SelectItem needs an extra parameter. '''
414 self
.tree
.UnselectAll()
415 item2
= self
.tree
.AppendItem(self
.root
, 'item 2')
416 self
.tree
.SelectItem(self
.item
)
417 self
.tree
.SelectItem(item2
, unselect_others
=False)
418 self
.assertEqual([self
.item
, item2
], self
.tree
.GetSelections())
421 class TreeAPIHarmonizerWithTreeListCtrl_VisibleRoot( \
422 TreeAPIHarmonizerWithTreeListCtrlCommonTests
,
423 TreeAPIHarmonizerVisibleRootTests
,
424 TreeAPIHarmonizerWithTreeListCtrlTestCase
):
428 class TreeAPIHarmonizerWithTreeListCtrl_HiddenRoot( \
429 TreeAPIHarmonizerWithTreeListCtrlCommonTests
,
430 TreeAPIHarmonizerHiddenRootTests
,
431 TreeAPIHarmonizerWithTreeListCtrlTestCase
):
435 class TreeAPIHarmonizerWithCustomTreeCtrlTestCase(TreeAPIHarmonizerTestCase
):
436 TreeClass
= wx
.lib
.customtreectrl
.CustomTreeCtrl
439 class TreeAPIHarmonizerWithCustomTreeCtrlCommonTests( \
440 TreeAPIHarmonizerCommonTests
,
441 TreeAPIHarmonizerNoTreeListCtrlCommonTests
):
443 def testGetCheckItemType(self
):
444 item
= self
.tree
.AppendItem(self
.root
, 'item', ct_type
=1)
445 self
.assertEqual(1, self
.tree
.GetItemType(item
))
447 def testGetRadioItemType(self
):
448 item
= self
.tree
.AppendItem(self
.root
, 'item', ct_type
=2)
449 self
.assertEqual(2, self
.tree
.GetItemType(item
))
453 class TreeAPIHarmonizerWithCustomTreeCtrl_SingleSelection( \
454 TreeAPIHarmonizerWithCustomTreeCtrlCommonTests
,
455 TreeAPIHarmonizerSingleSelectionTests
,
456 TreeAPIHarmonizerWithCustomTreeCtrlTestCase
):
460 class TreeAPIHarmonizerWithCustomTreeCtrl_MultipleSelection( \
461 TreeAPIHarmonizerWithCustomTreeCtrlCommonTests
,
462 TreeAPIHarmonizerMultipleSelectionTests
,
463 TreeAPIHarmonizerWithCustomTreeCtrlTestCase
):
467 class TreeAPIHarmonizerWithCustomTreeCtrl_VisibleRoot( \
468 TreeAPIHarmonizerWithCustomTreeCtrlCommonTests
,
469 TreeAPIHarmonizerVisibleRootTests
,
470 TreeAPIHarmonizerWithCustomTreeCtrlTestCase
):
474 class TreeAPIHarmonizerWithCustomTreeCtrl_HiddenRoot( \
475 TreeAPIHarmonizerWithCustomTreeCtrlCommonTests
,
476 TreeAPIHarmonizerHiddenRootTests
,
477 TreeAPIHarmonizerWithCustomTreeCtrlTestCase
):
483 class TreeHelperTestCase(unittest
.TestCase
):
484 style
= wx
.TR_DEFAULT_STYLE
487 self
.frame
= wx
.Frame(None)
488 class HelperTreeCtrl(treemixin
.TreeHelper
,
489 treemixin
.TreeAPIHarmonizer
, self
.TreeClass
):
491 self
.tree
= HelperTreeCtrl(self
.frame
, style
=self
.style
)
494 def populateTree(self
):
495 self
.root
= self
.tree
.AddRoot('Root')
496 self
.item
= self
.tree
.AppendItem(self
.root
, 'Item')
497 self
.child
= self
.tree
.AppendItem(self
.item
, 'Child')
500 class TreeHelperCommonTests(object):
501 def testGetItemChildren_EmptyTree(self
):
502 self
.tree
.DeleteAllItems()
503 self
.assertEqual([], self
.tree
.GetItemChildren())
505 def testGetItemChildren_NoParent(self
):
506 self
.assertEqual([self
.item
], self
.tree
.GetItemChildren())
508 def testGetItemChildren_RootItem(self
):
509 self
.assertEqual([self
.item
], self
.tree
.GetItemChildren(self
.root
))
511 def testGetItemChildren_RegularItem(self
):
512 self
.assertEqual([self
.child
], self
.tree
.GetItemChildren(self
.item
))
514 def testGetItemChildren_ItemWithoutChildren(self
):
515 self
.assertEqual([], self
.tree
.GetItemChildren(self
.child
))
517 def testGetItemChildren_NoParent_Recursively(self
):
518 self
.assertEqual([self
.item
, self
.child
],
519 self
.tree
.GetItemChildren(recursively
=True))
521 def testGetItemChildren_RootItem_Recursively(self
):
522 self
.assertEqual([self
.item
, self
.child
],
523 self
.tree
.GetItemChildren(self
.root
, True))
525 def testGetItemChildren_RegularItem_Recursively(self
):
526 self
.assertEqual([self
.child
],
527 self
.tree
.GetItemChildren(self
.item
, True))
529 def testGetItemChildren_ItemWithoutChildren_Recursively(self
):
530 self
.assertEqual([], self
.tree
.GetItemChildren(self
.child
, True))
532 def testGetItemByIndex_RootItem(self
):
533 self
.assertEqual(self
.root
, self
.tree
.GetItemByIndex(()))
535 def testGetItemByIndex_RegularItem(self
):
536 self
.assertEqual(self
.item
, self
.tree
.GetItemByIndex((0,)))
538 def testGetItemByIndex_Child(self
):
539 self
.assertEqual(self
.child
, self
.tree
.GetItemByIndex((0,0)))
541 def testGetIndexOfItemRootItem(self
):
542 self
.assertEqual((), self
.tree
.GetIndexOfItem(self
.root
))
544 def testGetIndexOfItemRegularItem(self
):
545 self
.assertEqual((0,), self
.tree
.GetIndexOfItem(self
.item
))
547 def testGetIndexOfItemChild(self
):
548 self
.assertEqual((0,0), self
.tree
.GetIndexOfItem(self
.child
))
551 class TreeHelperWithTreeCtrlTestCase(TreeHelperCommonTests
,
553 TreeClass
= wx
.TreeCtrl
556 class TreeHelperWithTreeListCtrlTestCase(TreeHelperCommonTests
,
558 TreeClass
= wx
.gizmos
.TreeListCtrl
560 def populateTree(self
):
561 self
.tree
.AddColumn('Column')
562 super(TreeHelperWithTreeListCtrlTestCase
, self
).populateTree()
565 class TreeHelperWithCustomTreeCtrlTestCase(TreeHelperCommonTests
,
567 TreeClass
= wx
.lib
.customtreectrl
.CustomTreeCtrl
570 # ExpansionState tests
572 class ExpansionStateTreeCtrl(treemixin
.ExpansionState
, wx
.TreeCtrl
):
576 class GetExpansionStateTestCase(unittest
.TestCase
):
578 self
.frame
= wx
.Frame(None)
579 self
.tree
= ExpansionStateTreeCtrl(self
.frame
)
581 def testEmptyTree(self
):
582 self
.assertEqual([], self
.tree
.GetExpansionState())
585 self
.tree
.AddRoot('Root item')
586 self
.assertEqual([], self
.tree
.GetExpansionState())
588 def testRoot_Child(self
):
589 root
= self
.tree
.AddRoot('Root item')
590 child
= self
.tree
.AppendItem(root
, 'Child')
591 self
.assertEqual([], self
.tree
.GetExpansionState())
593 def testExpandedRoot_Child(self
):
594 root
= self
.tree
.AddRoot('Root item')
595 child
= self
.tree
.AppendItem(root
, 'Child')
596 self
.tree
.Expand(root
)
597 self
.assertEqual([()], self
.tree
.GetExpansionState())
599 def testExpandedRoot_Child_Child(self
):
600 root
= self
.tree
.AddRoot('Root item')
601 child
= self
.tree
.AppendItem(root
, 'Child')
602 grandChild
= self
.tree
.AppendItem(child
, 'Grandchild')
603 self
.tree
.Expand(root
)
604 # Property should work too:
605 self
.assertEqual([()], self
.tree
.ExpansionState
)
607 def testRoot_ExpandedChild_Child(self
):
608 root
= self
.tree
.AddRoot('Root item')
609 child
= self
.tree
.AppendItem(root
, 'Child')
610 grandChild
= self
.tree
.AppendItem(child
, 'Grandchild')
611 self
.tree
.Expand(child
)
612 self
.assertEqual([], self
.tree
.GetExpansionState())
614 def testExpandedRoot_ExpandedChild_Child(self
):
615 root
= self
.tree
.AddRoot('Root item')
616 child
= self
.tree
.AppendItem(root
, 'Child')
617 grandChild
= self
.tree
.AppendItem(child
, 'Grandchild')
618 self
.tree
.Expand(child
)
619 self
.tree
.Expand(root
)
620 self
.assertEqual([(), (0,)], self
.tree
.GetExpansionState())
622 def testExpandedRoot_ExpandedChild_ExpandedChild(self
):
623 root
= self
.tree
.AddRoot('Root item')
624 child
= self
.tree
.AppendItem(root
, 'Child')
625 grandChild
= self
.tree
.AppendItem(child
, 'Grandchild')
626 grandGrandChild
= self
.tree
.AppendItem(grandChild
, 'Grandgrandchild')
627 self
.tree
.Expand(root
)
628 self
.tree
.Expand(child
)
629 self
.tree
.Expand(grandChild
)
630 self
.assertEqual([(), (0,), (0,0)], self
.tree
.GetExpansionState())
633 class SetExpansionStateTestCase(unittest
.TestCase
):
635 self
.frame
= wx
.Frame(None)
636 self
.tree
= ExpansionStateTreeCtrl(self
.frame
)
638 def testEmptyTree(self
):
639 self
.tree
.SetExpansionState([])
642 root
= self
.tree
.AddRoot('Root item')
643 self
.tree
.SetExpansionState([])
644 self
.failIf(self
.tree
.IsExpanded(root
))
646 def testRoot_Child(self
):
647 root
= self
.tree
.AddRoot('Root item')
648 child
= self
.tree
.AppendItem(root
, 'Child')
649 self
.tree
.SetExpansionState([])
650 self
.failIf(self
.tree
.IsExpanded(root
))
652 def testExpandedRoot_Child(self
):
653 root
= self
.tree
.AddRoot('Root item')
654 child
= self
.tree
.AppendItem(root
, 'Child')
655 self
.tree
.ExpansionState
= [()] # Property should work too
656 self
.failUnless(self
.tree
.IsExpanded(root
))
658 def testExpandedRoot_Child_Child(self
):
659 root
= self
.tree
.AddRoot('Root item')
660 child
= self
.tree
.AppendItem(root
, 'Child')
661 grandChild
= self
.tree
.AppendItem(child
, 'Grandchild')
662 self
.tree
.SetExpansionState([()])
663 self
.failIf(self
.tree
.IsExpanded(child
))
665 def testExpandedRoot_ExpandedChild_Child(self
):
666 root
= self
.tree
.AddRoot('Root item')
667 child
= self
.tree
.AppendItem(root
, 'Child')
668 grandChild
= self
.tree
.AppendItem(child
, 'Grandchild')
669 self
.tree
.SetExpansionState([(), (0,)])
670 self
.failUnless(self
.tree
.IsExpanded(child
))
672 def testRoot_ExpandedChild_Child(self
):
673 root
= self
.tree
.AddRoot('Root item')
674 child
= self
.tree
.AppendItem(root
, 'Child')
675 grandChild
= self
.tree
.AppendItem(child
, 'Grandchild')
676 self
.tree
.SetExpansionState([(0,)])
677 self
.failIf(self
.tree
.IsExpanded(child
))
679 def testExpandedRoot_ExpandedChild_ExpandedChild(self
):
680 root
= self
.tree
.AddRoot('Root item')
681 child
= self
.tree
.AppendItem(root
, 'Child')
682 grandChild
= self
.tree
.AppendItem(child
, 'Grandchild')
683 grandGrandChild
= self
.tree
.AppendItem(grandChild
, 'Grandgrandchild')
684 self
.tree
.SetExpansionState([(), (0,), (0,0)])
685 self
.failUnless(self
.tree
.IsExpanded(grandChild
))
688 # Tests of the tree controls without any mixin, to document behaviour that
689 # already works, and works the same, for all tree control widgets
691 class VanillaTreeTestCase(unittest
.TestCase
):
692 style
= wx
.TR_DEFAULT_STYLE
695 self
.frame
= wx
.Frame(None)
696 self
.tree
= self
.TreeClass(self
.frame
, style
=self
.style
)
697 self
.eventsReceived
= []
700 def populateTree(self
):
701 self
.root
= self
.tree
.AddRoot('Root')
702 self
.item
= self
.tree
.AppendItem(self
.root
, 'Item')
703 self
.items
= [self
.root
, self
.item
]
705 def onEvent(self
, event
):
706 self
.eventsReceived
.append(event
)
709 class VanillaTreeCommonTests(object):
710 ''' Tests that should succeed for all tree controls and all styles. '''
712 def testSetItemHasChildren(self
):
713 self
.tree
.SetItemHasChildren(self
.item
, True)
714 self
.failUnless(self
.tree
.ItemHasChildren(self
.item
))
716 def testExpandItemWithPlus(self
):
717 self
.tree
.Bind(wx
.EVT_TREE_ITEM_EXPANDING
, self
.onEvent
)
718 self
.tree
.SetItemHasChildren(self
.item
, True)
719 self
.tree
.Expand(self
.item
)
720 self
.assertEqual(1, len(self
.eventsReceived
))
724 class VanillaTreeCtrlTestCase(VanillaTreeCommonTests
, VanillaTreeTestCase
):
725 TreeClass
= wx
.TreeCtrl
728 class VanillaTreeListCtrlTestCase(VanillaTreeCommonTests
, VanillaTreeTestCase
):
729 TreeClass
= wx
.gizmos
.TreeListCtrl
731 def populateTree(self
):
732 self
.tree
.AddColumn('Column 0')
733 super(VanillaTreeListCtrlTestCase
, self
).populateTree()
736 class VanillaCustomTreeCtrlTestCase(VanillaTreeCommonTests
,
737 VanillaTreeTestCase
):
738 TreeClass
= wx
.lib
.customtreectrl
.CustomTreeCtrl
741 class VanillaTreeItemIdTestCase(unittest
.TestCase
):
742 def testTreeItemIdIsFalseDefault(self
):
743 self
.failIf(wx
.TreeItemId())
746 if __name__
== '__main__':