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