]> git.saurik.com Git - wxWidgets.git/blame - wxPython/tests/TreeMixinTest.py
Various fixes and improvements to get look-and-feel similar across
[wxWidgets.git] / wxPython / tests / TreeMixinTest.py
CommitLineData
7d36c6a8 1import wx, wx.gizmos, wx.lib.customtreectrl, unittest, treemixin
cbfc9df6
RD
2
3
4# VirtualTree tests
5
6class TreeCtrl(object):
7 def __init__(self, *args, **kwargs):
8 self.children = {}
9 self.font = {}
10 self.colour = {}
11 self.bgcolour = {}
12 self.image = {}
13 self.type = {}
14 self.checked = {}
15 super(TreeCtrl, self).__init__(*args, **kwargs)
16
17 def OnGetItemFont(self, index):
18 return self.font.get(index, wx.NullFont)
19
20 def PrepareItemFont(self, index, font):
21 self.font[index] = font
22
23 def OnGetItemTextColour(self, index):
24 return self.colour.get(index, wx.NullColour)
25
26 def PrepareItemColour(self, index, colour):
27 self.colour[index] = colour
28
29 def OnGetItemBackgroundColour(self, index):
30 return self.bgcolour.get(index, wx.NullColour)
31
32 def PrepareItemBackgroundColour(self, index, colour):
33 self.bgcolour[index] = colour
34
35 def OnGetItemImage(self, index, which):
36 return self.image.get(index, -1)
37
38 def PrepareImage(self, index, imageIndex):
39 self.image[index] = imageIndex
40
41 def OnGetItemType(self, index):
42 return self.type.get(index, 0)
43
44 def PrepareType(self, index, itemType):
45 self.type[index] = itemType
46
47 def OnGetItemChecked(self, index):
48 return self.checked.get(index, False)
49
50 def PrepareChecked(self, index, checked):
51 self.checked[index] = checked
52
53
54class VirtualTreeCtrl(TreeCtrl, treemixin.VirtualTree,
55 wx.lib.customtreectrl.CustomTreeCtrl):
56
57 def OnGetItemText(self, indices):
58 return 'item %s'%'.'.join([str(index) for index in indices])
59
60 def OnGetChildrenCount(self, index=None):
61 index = index or ()
62 return self.children.get(index, 0)
63
64 def PrepareChildrenCount(self, index, childrenCount):
65 self.children[index] = childrenCount
66
67
68
69class VirtualTreeCtrlTest_NoRootItems(unittest.TestCase):
70 def setUp(self):
71 self.frame = wx.Frame(None)
72 self.tree = VirtualTreeCtrl(self.frame)
73 self.tree.RefreshItems()
74
75 def testNoRootItems(self):
76 self.assertEqual(0, self.tree.GetCount())
77
78 def testAddTwoRootItems(self):
79 self.tree.PrepareChildrenCount((), 2)
80 self.tree.RefreshItems()
81 self.assertEqual(2, self.tree.GetCount())
82
83 def testAddOneRootItemAndOneChild(self):
84 self.tree.PrepareChildrenCount((), 1)
85 self.tree.PrepareChildrenCount((0,), 1)
86 self.tree.RefreshItems()
87 self.tree.ExpandAll()
88 self.assertEqual(2, self.tree.GetCount())
89
90 def testAddOneRootItemAndTwoChildren(self):
91 self.tree.PrepareChildrenCount((), 1)
92 self.tree.PrepareChildrenCount((0,), 2)
93 self.tree.RefreshItems()
94 self.tree.ExpandAll()
95 self.assertEqual(3, self.tree.GetCount())
96
97
98
99class VirtualTreeCtrlTest_OneRoot(unittest.TestCase):
100 def setUp(self):
101 self.frame = wx.Frame(None)
102 self.tree = VirtualTreeCtrl(self.frame)
103 self.tree.PrepareChildrenCount((), 1)
104 self.tree.RefreshItems()
105
106 def testOneRoot(self):
107 self.assertEqual(1, self.tree.GetCount())
108
109 def testDeleteRootItem(self):
110 self.tree.PrepareChildrenCount((), 0)
111 self.tree.RefreshItems()
112 self.assertEqual(0, self.tree.GetCount())
113
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())
119
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())
125
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))
131
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))
137
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))
143
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))
149
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))
156
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))
163
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))
169
7d36c6a8
RD
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))
177
cbfc9df6
RD
178
179# TreeAPIHarmonizer tests
180
181class TreeAPIHarmonizerTestCase(unittest.TestCase):
182 style = wx.TR_DEFAULT_STYLE
183
184 def setUp(self):
185 self.frame = wx.Frame(None)
186 class HarmonizedTreeCtrl(treemixin.TreeAPIHarmonizer, self.TreeClass):
187 pass
188 self.tree = HarmonizedTreeCtrl(self.frame, style=self.style)
189 self.eventsReceived = []
190 self.populateTree()
191
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]
196
197 def onEvent(self, event):
198 self.eventsReceived.append(event)
199
200
201class TreeAPIHarmonizerCommonTests(object):
202 ''' Tests that should succeed for all tree controls and all styles. '''
203
204 def testGetItemType(self):
205 self.assertEqual(0, self.tree.GetItemType(self.item))
206
207 def testGetItemImage_DefaultIcon(self):
208 self.assertEqual(-1, self.tree.GetItemImage(self.item))
209
210 def testGetItemImage_SelectedIcon(self):
211 self.assertEqual(-1,
212 self.tree.GetItemImage(self.item, wx.TreeItemIcon_Selected))
213
214 def testGetItemImage_DefaultIcon_OtherColumn(self):
215 self.assertEqual(-1, self.tree.GetItemImage(self.item, column=1))
216
217 def testGetItemImage_SelectedIcon_OtherColumn(self):
218 self.assertEqual(-1,
219 self.tree.GetItemImage(self.item, wx.TreeItemIcon_Selected, 1))
220
221 def testSetItemImage_DefaultIcon(self):
222 self.tree.SetItemImage(self.item, -1)
223 self.assertEqual(-1, self.tree.GetItemImage(self.item))
224
225 def testSetItemImage_SelectedIcon(self):
226 self.tree.SetItemImage(self.item, -1, wx.TreeItemIcon_Selected)
227 self.assertEqual(-1,
228 self.tree.GetItemImage(self.item, wx.TreeItemIcon_Selected))
229
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))
233
234 def testSetItemImage_SelectedIcon_OtherColumn(self):
235 self.tree.SetItemImage(self.item, -1, wx.TreeItemIcon_Selected, 1)
236 self.assertEqual(-1,
237 self.tree.GetItemImage(self.item, wx.TreeItemIcon_Selected, 1))
238
239 def testExpandAll(self):
240 self.tree.ExpandAll()
241
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))
246
7d36c6a8
RD
247 def testGetFirstVisibleItem(self):
248 self.tree.DeleteAllItems()
249 self.failIf(self.tree.GetFirstVisibleItem())
250
cbfc9df6
RD
251
252class TreeAPIHarmonizerNoTreeListCtrlCommonTests(object):
253 ''' Tests that should succeed for all tree controls, except the
254 TreeListCtrl, and all styles. '''
255
256 def testGetMainWindow(self):
257 self.assertEqual(self.tree, self.tree.GetMainWindow())
258
259 def testGetColumnCount(self):
260 self.assertEqual(0, self.tree.GetColumnCount())
261
262
263class TreeAPIHarmonizerSingleSelectionTests(object):
264 ''' Tests that should succeed for all tree controls when in single
265 selection mode (which is the default selection mode). '''
266
267 def testUnselectAll(self):
268 self.tree.SelectItem(self.item)
269 self.tree.UnselectAll()
270 self.assertEqual([], self.tree.GetSelections())
271
272 def testGetSelections_NoSelection(self):
273 self.tree.UnselectAll()
274 self.assertEqual([], self.tree.GetSelections())
275
276 def testGetSelections_OneSelectedItem(self):
277 self.tree.UnselectAll()
278 self.tree.SelectItem(self.item)
279 self.assertEqual([self.item], self.tree.GetSelections())
280
281
282class TreeAPIHarmonizerMultipleSelectionTests(object):
283 ''' Tests that should succeed for all tree controls when in multiple
284 selection mode. '''
285
286 style = wx.TR_DEFAULT_STYLE | wx.TR_MULTIPLE
287
288 def testUnselectAll(self):
289 self.tree.SelectItem(self.item)
290 self.tree.UnselectAll()
291 self.assertEqual([], self.tree.GetSelections())
292
293 def testGetSelections_NoSelection(self):
294 self.tree.UnselectAll()
295 self.assertEqual([], self.tree.GetSelections())
296
297 def testGetSelections_OneSelectedItem(self):
298 self.tree.UnselectAll()
299 self.tree.SelectItem(self.item)
300 self.assertEqual([self.item], self.tree.GetSelections())
301
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())
309
310
311class TreeAPIHarmonizerVisibleRootTests(object):
312 ''' Tests that should succeed for all tree controls when the root item
313 is not hidden. '''
314
315 def testGetCount(self):
316 self.assertEqual(len(self.items), self.tree.GetCount())
317
318 def testSelectRoot(self):
319 self.tree.SelectItem(self.root)
320 self.assertEqual([self.root], self.tree.GetSelections())
321
322
323class TreeAPIHarmonizerHiddenRootTests(object):
324 ''' Tests that should succeed for all tree controls when the root item
325 is hidden. '''
326
327 style = wx.TR_DEFAULT_STYLE | wx.TR_HIDE_ROOT
328
329 def testGetCount(self):
330 self.assertEqual(len(self.items) - 1, self.tree.GetCount())
331
332 def testSelectRoot(self):
333 self.tree.SelectItem(self.root)
334 self.assertEqual([], self.tree.GetSelections())
335
336
337class TreeAPIHarmonizerWithTreeCtrlTestCase(TreeAPIHarmonizerTestCase):
338 TreeClass = wx.TreeCtrl
339
340
341class TreeAPIHarmonizerWithTreeCtrlCommonTests( \
342 TreeAPIHarmonizerCommonTests,
343 TreeAPIHarmonizerNoTreeListCtrlCommonTests):
344 pass
345
346
347class TreeAPIHarmonizerWithTreeCtrl_SingleSelection( \
348 TreeAPIHarmonizerWithTreeCtrlCommonTests,
349 TreeAPIHarmonizerSingleSelectionTests,
350 TreeAPIHarmonizerWithTreeCtrlTestCase):
351 pass
352
353
354class TreeAPIHarmonizerWithTreeCtrl_MultipleSelection( \
355 TreeAPIHarmonizerWithTreeCtrlCommonTests,
356 TreeAPIHarmonizerMultipleSelectionTests,
357 TreeAPIHarmonizerWithTreeCtrlTestCase):
358 pass
359
360
361class TreeAPIHarmonizerWithTreeCtrl_VisibleRoot( \
362 TreeAPIHarmonizerWithTreeCtrlCommonTests,
363 TreeAPIHarmonizerVisibleRootTests,
364 TreeAPIHarmonizerWithTreeCtrlTestCase):
365 pass
366
367
368class TreeAPIHarmonizerWithTreeCtrl_HiddenRoot( \
369 TreeAPIHarmonizerWithTreeCtrlCommonTests,
370 TreeAPIHarmonizerHiddenRootTests,
371 TreeAPIHarmonizerWithTreeCtrlTestCase):
372 pass
373
374
375class TreeAPIHarmonizerWithTreeListCtrlTestCase(TreeAPIHarmonizerTestCase):
376 TreeClass = wx.gizmos.TreeListCtrl
377
378 def populateTree(self):
379 self.tree.AddColumn('Column')
380 super(TreeAPIHarmonizerWithTreeListCtrlTestCase, self).populateTree()
381
382
383class TreeAPIHarmonizerWithTreeListCtrlCommonTests( \
384 TreeAPIHarmonizerCommonTests):
385
386 def testGetColumnCount(self):
387 self.assertEqual(1, self.tree.GetColumnCount())
388
389 def testGetMainWindow(self):
390 self.assertNotEqual(self.tree, self.tree.GetMainWindow())
391
392
393class TreeAPIHarmonizerWithTreeListCtrl_SingleSelection( \
394 TreeAPIHarmonizerWithTreeListCtrlCommonTests,
395 TreeAPIHarmonizerSingleSelectionTests,
396 TreeAPIHarmonizerWithTreeListCtrlTestCase):
397 pass
398
399
400class TreeAPIHarmonizerWithTreeListCtrl_MultipleSelection( \
401 TreeAPIHarmonizerWithTreeListCtrlCommonTests,
402 TreeAPIHarmonizerMultipleSelectionTests,
403 TreeAPIHarmonizerWithTreeListCtrlTestCase):
404
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())
414
415
416class TreeAPIHarmonizerWithTreeListCtrl_VisibleRoot( \
417 TreeAPIHarmonizerWithTreeListCtrlCommonTests,
418 TreeAPIHarmonizerVisibleRootTests,
419 TreeAPIHarmonizerWithTreeListCtrlTestCase):
420 pass
421
422
423class TreeAPIHarmonizerWithTreeListCtrl_HiddenRoot( \
424 TreeAPIHarmonizerWithTreeListCtrlCommonTests,
425 TreeAPIHarmonizerHiddenRootTests,
426 TreeAPIHarmonizerWithTreeListCtrlTestCase):
427 pass
428
429
430class TreeAPIHarmonizerWithCustomTreeCtrlTestCase(TreeAPIHarmonizerTestCase):
431 TreeClass = wx.lib.customtreectrl.CustomTreeCtrl
432
433
434class TreeAPIHarmonizerWithCustomTreeCtrlCommonTests( \
435 TreeAPIHarmonizerCommonTests,
436 TreeAPIHarmonizerNoTreeListCtrlCommonTests):
437
438 def testGetCheckItemType(self):
439 item = self.tree.AppendItem(self.root, 'item', ct_type=1)
440 self.assertEqual(1, self.tree.GetItemType(item))
441
442 def testGetRadioItemType(self):
443 item = self.tree.AppendItem(self.root, 'item', ct_type=2)
444 self.assertEqual(2, self.tree.GetItemType(item))
445
446
447
448class TreeAPIHarmonizerWithCustomTreeCtrl_SingleSelection( \
449 TreeAPIHarmonizerWithCustomTreeCtrlCommonTests,
450 TreeAPIHarmonizerSingleSelectionTests,
451 TreeAPIHarmonizerWithCustomTreeCtrlTestCase):
452 pass
453
454
455class TreeAPIHarmonizerWithCustomTreeCtrl_MultipleSelection( \
456 TreeAPIHarmonizerWithCustomTreeCtrlCommonTests,
457 TreeAPIHarmonizerMultipleSelectionTests,
458 TreeAPIHarmonizerWithCustomTreeCtrlTestCase):
459 pass
460
461
462class TreeAPIHarmonizerWithCustomTreeCtrl_VisibleRoot( \
463 TreeAPIHarmonizerWithCustomTreeCtrlCommonTests,
464 TreeAPIHarmonizerVisibleRootTests,
465 TreeAPIHarmonizerWithCustomTreeCtrlTestCase):
466 pass
467
468
469class TreeAPIHarmonizerWithCustomTreeCtrl_HiddenRoot( \
470 TreeAPIHarmonizerWithCustomTreeCtrlCommonTests,
471 TreeAPIHarmonizerHiddenRootTests,
472 TreeAPIHarmonizerWithCustomTreeCtrlTestCase):
473 pass
474
475
476# TreeHelper tests
477
478class TreeHelperTestCase(unittest.TestCase):
479 style = wx.TR_DEFAULT_STYLE
480
481 def setUp(self):
482 self.frame = wx.Frame(None)
483 class HelperTreeCtrl(treemixin.TreeHelper,
484 treemixin.TreeAPIHarmonizer, self.TreeClass):
485 pass
486 self.tree = HelperTreeCtrl(self.frame, style=self.style)
487 self.populateTree()
488
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')
493
494
495class TreeHelperCommonTests(object):
496 def testGetItemChildren_EmptyTree(self):
497 self.tree.DeleteAllItems()
498 self.assertEqual([], self.tree.GetItemChildren())
499
500 def testGetItemChildren_NoParent(self):
501 self.assertEqual([self.item], self.tree.GetItemChildren())
502
503 def testGetItemChildren_RootItem(self):
504 self.assertEqual([self.item], self.tree.GetItemChildren(self.root))
505
506 def testGetItemChildren_RegularItem(self):
507 self.assertEqual([self.child], self.tree.GetItemChildren(self.item))
508
509 def testGetItemChildren_ItemWithoutChildren(self):
510 self.assertEqual([], self.tree.GetItemChildren(self.child))
511
512 def testGetItemChildren_NoParent_Recursively(self):
513 self.assertEqual([self.item, self.child],
514 self.tree.GetItemChildren(recursively=True))
515
516 def testGetItemChildren_RootItem_Recursively(self):
517 self.assertEqual([self.item, self.child],
518 self.tree.GetItemChildren(self.root, True))
519
520 def testGetItemChildren_RegularItem_Recursively(self):
521 self.assertEqual([self.child],
522 self.tree.GetItemChildren(self.item, True))
523
524 def testGetItemChildren_ItemWithoutChildren_Recursively(self):
525 self.assertEqual([], self.tree.GetItemChildren(self.child, True))
526
527 def testGetItemByIndex_RootItem(self):
528 self.assertEqual(self.root, self.tree.GetItemByIndex(()))
529
530 def testGetItemByIndex_RegularItem(self):
531 self.assertEqual(self.item, self.tree.GetItemByIndex((0,)))
532
533 def testGetItemByIndex_Child(self):
534 self.assertEqual(self.child, self.tree.GetItemByIndex((0,0)))
535
536 def testGetIndexOfItemRootItem(self):
537 self.assertEqual((), self.tree.GetIndexOfItem(self.root))
538
539 def testGetIndexOfItemRegularItem(self):
540 self.assertEqual((0,), self.tree.GetIndexOfItem(self.item))
541
542 def testGetIndexOfItemChild(self):
543 self.assertEqual((0,0), self.tree.GetIndexOfItem(self.child))
544
545
546class TreeHelperWithTreeCtrlTestCase(TreeHelperCommonTests,
547 TreeHelperTestCase):
548 TreeClass = wx.TreeCtrl
549
550
551class TreeHelperWithTreeListCtrlTestCase(TreeHelperCommonTests,
552 TreeHelperTestCase):
553 TreeClass = wx.gizmos.TreeListCtrl
554
555 def populateTree(self):
556 self.tree.AddColumn('Column')
557 super(TreeHelperWithTreeListCtrlTestCase, self).populateTree()
558
559
560class TreeHelperWithCustomTreeCtrlTestCase(TreeHelperCommonTests,
561 TreeHelperTestCase):
562 TreeClass = wx.lib.customtreectrl.CustomTreeCtrl
563
564
565# ExpansionState tests
566
567class ExpansionStateTreeCtrl(treemixin.ExpansionState, wx.TreeCtrl):
568 pass
569
570
571class GetExpansionStateTestCase(unittest.TestCase):
572 def setUp(self):
573 self.frame = wx.Frame(None)
574 self.tree = ExpansionStateTreeCtrl(self.frame)
575
576 def testEmptyTree(self):
577 self.assertEqual([], self.tree.GetExpansionState())
578
579 def testRoot(self):
580 self.tree.AddRoot('Root item')
581 self.assertEqual([], self.tree.GetExpansionState())
582
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())
587
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())
593
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)
601
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())
608
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())
616
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())
626
627
628class SetExpansionStateTestCase(unittest.TestCase):
629 def setUp(self):
630 self.frame = wx.Frame(None)
631 self.tree = ExpansionStateTreeCtrl(self.frame)
632
633 def testEmptyTree(self):
634 self.tree.SetExpansionState([])
635
636 def testRoot(self):
637 root = self.tree.AddRoot('Root item')
638 self.tree.SetExpansionState([])
639 self.failIf(self.tree.IsExpanded(root))
640
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))
646
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))
652
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))
659
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))
666
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))
673
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))
681
682
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
685
686class VanillaTreeTestCase(unittest.TestCase):
687 style = wx.TR_DEFAULT_STYLE
688
689 def setUp(self):
690 self.frame = wx.Frame(None)
691 self.tree = self.TreeClass(self.frame, style=self.style)
692 self.eventsReceived = []
693 self.populateTree()
694
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]
699
700 def onEvent(self, event):
701 self.eventsReceived.append(event)
702
703
704class VanillaTreeCommonTests(object):
705 ''' Tests that should succeed for all tree controls and all styles. '''
706
707 def testSetItemHasChildren(self):
708 self.tree.SetItemHasChildren(self.item, True)
709 self.failUnless(self.tree.ItemHasChildren(self.item))
710
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))
716
717
7d36c6a8 718
cbfc9df6
RD
719class VanillaTreeCtrlTestCase(VanillaTreeCommonTests, VanillaTreeTestCase):
720 TreeClass = wx.TreeCtrl
721
722
723class VanillaTreeListCtrlTestCase(VanillaTreeCommonTests, VanillaTreeTestCase):
724 TreeClass = wx.gizmos.TreeListCtrl
725
726 def populateTree(self):
727 self.tree.AddColumn('Column 0')
728 super(VanillaTreeListCtrlTestCase, self).populateTree()
729
730
731class VanillaCustomTreeCtrlTestCase(VanillaTreeCommonTests,
732 VanillaTreeTestCase):
733 TreeClass = wx.lib.customtreectrl.CustomTreeCtrl
734
735
7d36c6a8
RD
736class VanillaTreeItemIdTestCase(unittest.TestCase):
737 def testTreeItemIdIsFalseDefault(self):
738 self.failIf(wx.TreeItemId())
cbfc9df6
RD
739
740
741if __name__ == '__main__':
742 app = wx.App(False)
743 unittest.main()
744
745