]>
Commit | Line | Data |
---|---|---|
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 | # VirtualTree tests | |
9 | ||
10 | class TreeCtrl(object): | |
11 | def __init__(self, *args, **kwargs): | |
12 | self.children = {} | |
13 | self.font = {} | |
14 | self.colour = {} | |
15 | self.bgcolour = {} | |
16 | self.image = {} | |
17 | self.type = {} | |
18 | self.checked = {} | |
19 | super(TreeCtrl, self).__init__(*args, **kwargs) | |
20 | ||
21 | def OnGetItemFont(self, index): | |
22 | return self.font.get(index, wx.NullFont) | |
23 | ||
24 | def PrepareItemFont(self, index, font): | |
25 | self.font[index] = font | |
26 | ||
27 | def OnGetItemTextColour(self, index): | |
28 | return self.colour.get(index, wx.NullColour) | |
29 | ||
30 | def PrepareItemColour(self, index, colour): | |
31 | self.colour[index] = colour | |
32 | ||
33 | def OnGetItemBackgroundColour(self, index): | |
34 | return self.bgcolour.get(index, wx.NullColour) | |
35 | ||
36 | def PrepareItemBackgroundColour(self, index, colour): | |
37 | self.bgcolour[index] = colour | |
38 | ||
39 | def OnGetItemImage(self, index, which): | |
40 | return self.image.get(index, -1) | |
41 | ||
42 | def PrepareImage(self, index, imageIndex): | |
43 | self.image[index] = imageIndex | |
44 | ||
45 | def OnGetItemType(self, index): | |
46 | return self.type.get(index, 0) | |
47 | ||
48 | def PrepareType(self, index, itemType): | |
49 | self.type[index] = itemType | |
50 | ||
51 | def OnGetItemChecked(self, index): | |
52 | return self.checked.get(index, False) | |
53 | ||
54 | def PrepareChecked(self, index, checked): | |
55 | self.checked[index] = checked | |
56 | ||
57 | ||
58 | class VirtualTreeCtrl(TreeCtrl, treemixin.VirtualTree, | |
59 | wx.lib.customtreectrl.CustomTreeCtrl): | |
60 | ||
61 | def OnGetItemText(self, indices): | |
62 | return 'item %s'%'.'.join([str(index) for index in indices]) | |
63 | ||
64 | def OnGetChildrenCount(self, index=None): | |
65 | index = index or () | |
66 | return self.children.get(index, 0) | |
67 | ||
68 | def PrepareChildrenCount(self, index, childrenCount): | |
69 | self.children[index] = childrenCount | |
70 | ||
71 | ||
72 | ||
73 | class VirtualTreeCtrlTest_NoRootItems(unittest.TestCase): | |
74 | def setUp(self): | |
75 | self.frame = wx.Frame(None) | |
76 | self.tree = VirtualTreeCtrl(self.frame) | |
77 | self.tree.RefreshItems() | |
78 | ||
79 | def testNoRootItems(self): | |
80 | self.assertEqual(0, self.tree.GetCount()) | |
81 | ||
82 | def testAddTwoRootItems(self): | |
83 | self.tree.PrepareChildrenCount((), 2) | |
84 | self.tree.RefreshItems() | |
85 | self.assertEqual(2, self.tree.GetCount()) | |
86 | ||
87 | def testAddOneRootItemAndOneChild(self): | |
88 | self.tree.PrepareChildrenCount((), 1) | |
89 | self.tree.PrepareChildrenCount((0,), 1) | |
90 | self.tree.RefreshItems() | |
91 | self.tree.ExpandAll() | |
92 | self.assertEqual(2, self.tree.GetCount()) | |
93 | ||
94 | def testAddOneRootItemAndTwoChildren(self): | |
95 | self.tree.PrepareChildrenCount((), 1) | |
96 | self.tree.PrepareChildrenCount((0,), 2) | |
97 | self.tree.RefreshItems() | |
98 | self.tree.ExpandAll() | |
99 | self.assertEqual(3, self.tree.GetCount()) | |
100 | ||
101 | ||
102 | ||
103 | class VirtualTreeCtrlTest_OneRoot(unittest.TestCase): | |
104 | def setUp(self): | |
105 | self.frame = wx.Frame(None) | |
106 | self.tree = VirtualTreeCtrl(self.frame) | |
107 | self.tree.PrepareChildrenCount((), 1) | |
108 | self.tree.RefreshItems() | |
109 | ||
110 | def testOneRoot(self): | |
111 | self.assertEqual(1, self.tree.GetCount()) | |
112 | ||
113 | def testDeleteRootItem(self): | |
114 | self.tree.PrepareChildrenCount((), 0) | |
115 | self.tree.RefreshItems() | |
116 | self.assertEqual(0, self.tree.GetCount()) | |
117 | ||
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()) | |
123 | ||
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()) | |
129 | ||
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)) | |
135 | ||
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)) | |
141 | ||
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)) | |
147 | ||
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)) | |
153 | ||
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)) | |
160 | ||
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)) | |
167 | ||
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)) | |
173 | ||
174 | ||
175 | # TreeAPIHarmonizer tests | |
176 | ||
177 | class TreeAPIHarmonizerTestCase(unittest.TestCase): | |
178 | style = wx.TR_DEFAULT_STYLE | |
179 | ||
180 | def setUp(self): | |
181 | self.frame = wx.Frame(None) | |
182 | class HarmonizedTreeCtrl(treemixin.TreeAPIHarmonizer, self.TreeClass): | |
183 | pass | |
184 | self.tree = HarmonizedTreeCtrl(self.frame, style=self.style) | |
185 | self.eventsReceived = [] | |
186 | self.populateTree() | |
187 | ||
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] | |
192 | ||
193 | def onEvent(self, event): | |
194 | self.eventsReceived.append(event) | |
195 | ||
196 | ||
197 | class TreeAPIHarmonizerCommonTests(object): | |
198 | ''' Tests that should succeed for all tree controls and all styles. ''' | |
199 | ||
200 | def testGetItemType(self): | |
201 | self.assertEqual(0, self.tree.GetItemType(self.item)) | |
202 | ||
203 | def testGetItemImage_DefaultIcon(self): | |
204 | self.assertEqual(-1, self.tree.GetItemImage(self.item)) | |
205 | ||
206 | def testGetItemImage_SelectedIcon(self): | |
207 | self.assertEqual(-1, | |
208 | self.tree.GetItemImage(self.item, wx.TreeItemIcon_Selected)) | |
209 | ||
210 | def testGetItemImage_DefaultIcon_OtherColumn(self): | |
211 | self.assertEqual(-1, self.tree.GetItemImage(self.item, column=1)) | |
212 | ||
213 | def testGetItemImage_SelectedIcon_OtherColumn(self): | |
214 | self.assertEqual(-1, | |
215 | self.tree.GetItemImage(self.item, wx.TreeItemIcon_Selected, 1)) | |
216 | ||
217 | def testSetItemImage_DefaultIcon(self): | |
218 | self.tree.SetItemImage(self.item, -1) | |
219 | self.assertEqual(-1, self.tree.GetItemImage(self.item)) | |
220 | ||
221 | def testSetItemImage_SelectedIcon(self): | |
222 | self.tree.SetItemImage(self.item, -1, wx.TreeItemIcon_Selected) | |
223 | self.assertEqual(-1, | |
224 | self.tree.GetItemImage(self.item, wx.TreeItemIcon_Selected)) | |
225 | ||
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)) | |
229 | ||
230 | def testSetItemImage_SelectedIcon_OtherColumn(self): | |
231 | self.tree.SetItemImage(self.item, -1, wx.TreeItemIcon_Selected, 1) | |
232 | self.assertEqual(-1, | |
233 | self.tree.GetItemImage(self.item, wx.TreeItemIcon_Selected, 1)) | |
234 | ||
235 | def testExpandAll(self): | |
236 | self.tree.ExpandAll() | |
237 | ||
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)) | |
242 | ||
243 | ||
244 | class TreeAPIHarmonizerNoTreeListCtrlCommonTests(object): | |
245 | ''' Tests that should succeed for all tree controls, except the | |
246 | TreeListCtrl, and all styles. ''' | |
247 | ||
248 | def testGetMainWindow(self): | |
249 | self.assertEqual(self.tree, self.tree.GetMainWindow()) | |
250 | ||
251 | def testGetColumnCount(self): | |
252 | self.assertEqual(0, self.tree.GetColumnCount()) | |
253 | ||
254 | ||
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). ''' | |
258 | ||
259 | def testUnselectAll(self): | |
260 | self.tree.SelectItem(self.item) | |
261 | self.tree.UnselectAll() | |
262 | self.assertEqual([], self.tree.GetSelections()) | |
263 | ||
264 | def testGetSelections_NoSelection(self): | |
265 | self.tree.UnselectAll() | |
266 | self.assertEqual([], self.tree.GetSelections()) | |
267 | ||
268 | def testGetSelections_OneSelectedItem(self): | |
269 | self.tree.UnselectAll() | |
270 | self.tree.SelectItem(self.item) | |
271 | self.assertEqual([self.item], self.tree.GetSelections()) | |
272 | ||
273 | ||
274 | class TreeAPIHarmonizerMultipleSelectionTests(object): | |
275 | ''' Tests that should succeed for all tree controls when in multiple | |
276 | selection mode. ''' | |
277 | ||
278 | style = wx.TR_DEFAULT_STYLE | wx.TR_MULTIPLE | |
279 | ||
280 | def testUnselectAll(self): | |
281 | self.tree.SelectItem(self.item) | |
282 | self.tree.UnselectAll() | |
283 | self.assertEqual([], self.tree.GetSelections()) | |
284 | ||
285 | def testGetSelections_NoSelection(self): | |
286 | self.tree.UnselectAll() | |
287 | self.assertEqual([], self.tree.GetSelections()) | |
288 | ||
289 | def testGetSelections_OneSelectedItem(self): | |
290 | self.tree.UnselectAll() | |
291 | self.tree.SelectItem(self.item) | |
292 | self.assertEqual([self.item], self.tree.GetSelections()) | |
293 | ||
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()) | |
301 | ||
302 | ||
303 | class TreeAPIHarmonizerVisibleRootTests(object): | |
304 | ''' Tests that should succeed for all tree controls when the root item | |
305 | is not hidden. ''' | |
306 | ||
307 | def testGetCount(self): | |
308 | self.assertEqual(len(self.items), self.tree.GetCount()) | |
309 | ||
310 | def testSelectRoot(self): | |
311 | self.tree.SelectItem(self.root) | |
312 | self.assertEqual([self.root], self.tree.GetSelections()) | |
313 | ||
314 | ||
315 | class TreeAPIHarmonizerHiddenRootTests(object): | |
316 | ''' Tests that should succeed for all tree controls when the root item | |
317 | is hidden. ''' | |
318 | ||
319 | style = wx.TR_DEFAULT_STYLE | wx.TR_HIDE_ROOT | |
320 | ||
321 | def testGetCount(self): | |
322 | self.assertEqual(len(self.items) - 1, self.tree.GetCount()) | |
323 | ||
324 | def testSelectRoot(self): | |
325 | self.tree.SelectItem(self.root) | |
326 | self.assertEqual([], self.tree.GetSelections()) | |
327 | ||
328 | ||
329 | class TreeAPIHarmonizerWithTreeCtrlTestCase(TreeAPIHarmonizerTestCase): | |
330 | TreeClass = wx.TreeCtrl | |
331 | ||
332 | ||
333 | class TreeAPIHarmonizerWithTreeCtrlCommonTests( \ | |
334 | TreeAPIHarmonizerCommonTests, | |
335 | TreeAPIHarmonizerNoTreeListCtrlCommonTests): | |
336 | pass | |
337 | ||
338 | ||
339 | class TreeAPIHarmonizerWithTreeCtrl_SingleSelection( \ | |
340 | TreeAPIHarmonizerWithTreeCtrlCommonTests, | |
341 | TreeAPIHarmonizerSingleSelectionTests, | |
342 | TreeAPIHarmonizerWithTreeCtrlTestCase): | |
343 | pass | |
344 | ||
345 | ||
346 | class TreeAPIHarmonizerWithTreeCtrl_MultipleSelection( \ | |
347 | TreeAPIHarmonizerWithTreeCtrlCommonTests, | |
348 | TreeAPIHarmonizerMultipleSelectionTests, | |
349 | TreeAPIHarmonizerWithTreeCtrlTestCase): | |
350 | pass | |
351 | ||
352 | ||
353 | class TreeAPIHarmonizerWithTreeCtrl_VisibleRoot( \ | |
354 | TreeAPIHarmonizerWithTreeCtrlCommonTests, | |
355 | TreeAPIHarmonizerVisibleRootTests, | |
356 | TreeAPIHarmonizerWithTreeCtrlTestCase): | |
357 | pass | |
358 | ||
359 | ||
360 | class TreeAPIHarmonizerWithTreeCtrl_HiddenRoot( \ | |
361 | TreeAPIHarmonizerWithTreeCtrlCommonTests, | |
362 | TreeAPIHarmonizerHiddenRootTests, | |
363 | TreeAPIHarmonizerWithTreeCtrlTestCase): | |
364 | pass | |
365 | ||
366 | ||
367 | class TreeAPIHarmonizerWithTreeListCtrlTestCase(TreeAPIHarmonizerTestCase): | |
368 | TreeClass = wx.gizmos.TreeListCtrl | |
369 | ||
370 | def populateTree(self): | |
371 | self.tree.AddColumn('Column') | |
372 | super(TreeAPIHarmonizerWithTreeListCtrlTestCase, self).populateTree() | |
373 | ||
374 | ||
375 | class TreeAPIHarmonizerWithTreeListCtrlCommonTests( \ | |
376 | TreeAPIHarmonizerCommonTests): | |
377 | ||
378 | def testGetColumnCount(self): | |
379 | self.assertEqual(1, self.tree.GetColumnCount()) | |
380 | ||
381 | def testGetMainWindow(self): | |
382 | self.assertNotEqual(self.tree, self.tree.GetMainWindow()) | |
383 | ||
384 | ||
385 | class TreeAPIHarmonizerWithTreeListCtrl_SingleSelection( \ | |
386 | TreeAPIHarmonizerWithTreeListCtrlCommonTests, | |
387 | TreeAPIHarmonizerSingleSelectionTests, | |
388 | TreeAPIHarmonizerWithTreeListCtrlTestCase): | |
389 | pass | |
390 | ||
391 | ||
392 | class TreeAPIHarmonizerWithTreeListCtrl_MultipleSelection( \ | |
393 | TreeAPIHarmonizerWithTreeListCtrlCommonTests, | |
394 | TreeAPIHarmonizerMultipleSelectionTests, | |
395 | TreeAPIHarmonizerWithTreeListCtrlTestCase): | |
396 | ||
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()) | |
406 | ||
407 | ||
408 | class TreeAPIHarmonizerWithTreeListCtrl_VisibleRoot( \ | |
409 | TreeAPIHarmonizerWithTreeListCtrlCommonTests, | |
410 | TreeAPIHarmonizerVisibleRootTests, | |
411 | TreeAPIHarmonizerWithTreeListCtrlTestCase): | |
412 | pass | |
413 | ||
414 | ||
415 | class TreeAPIHarmonizerWithTreeListCtrl_HiddenRoot( \ | |
416 | TreeAPIHarmonizerWithTreeListCtrlCommonTests, | |
417 | TreeAPIHarmonizerHiddenRootTests, | |
418 | TreeAPIHarmonizerWithTreeListCtrlTestCase): | |
419 | pass | |
420 | ||
421 | ||
422 | class TreeAPIHarmonizerWithCustomTreeCtrlTestCase(TreeAPIHarmonizerTestCase): | |
423 | TreeClass = wx.lib.customtreectrl.CustomTreeCtrl | |
424 | ||
425 | ||
426 | class TreeAPIHarmonizerWithCustomTreeCtrlCommonTests( \ | |
427 | TreeAPIHarmonizerCommonTests, | |
428 | TreeAPIHarmonizerNoTreeListCtrlCommonTests): | |
429 | ||
430 | def testGetCheckItemType(self): | |
431 | item = self.tree.AppendItem(self.root, 'item', ct_type=1) | |
432 | self.assertEqual(1, self.tree.GetItemType(item)) | |
433 | ||
434 | def testGetRadioItemType(self): | |
435 | item = self.tree.AppendItem(self.root, 'item', ct_type=2) | |
436 | self.assertEqual(2, self.tree.GetItemType(item)) | |
437 | ||
438 | ||
439 | ||
440 | class TreeAPIHarmonizerWithCustomTreeCtrl_SingleSelection( \ | |
441 | TreeAPIHarmonizerWithCustomTreeCtrlCommonTests, | |
442 | TreeAPIHarmonizerSingleSelectionTests, | |
443 | TreeAPIHarmonizerWithCustomTreeCtrlTestCase): | |
444 | pass | |
445 | ||
446 | ||
447 | class TreeAPIHarmonizerWithCustomTreeCtrl_MultipleSelection( \ | |
448 | TreeAPIHarmonizerWithCustomTreeCtrlCommonTests, | |
449 | TreeAPIHarmonizerMultipleSelectionTests, | |
450 | TreeAPIHarmonizerWithCustomTreeCtrlTestCase): | |
451 | pass | |
452 | ||
453 | ||
454 | class TreeAPIHarmonizerWithCustomTreeCtrl_VisibleRoot( \ | |
455 | TreeAPIHarmonizerWithCustomTreeCtrlCommonTests, | |
456 | TreeAPIHarmonizerVisibleRootTests, | |
457 | TreeAPIHarmonizerWithCustomTreeCtrlTestCase): | |
458 | pass | |
459 | ||
460 | ||
461 | class TreeAPIHarmonizerWithCustomTreeCtrl_HiddenRoot( \ | |
462 | TreeAPIHarmonizerWithCustomTreeCtrlCommonTests, | |
463 | TreeAPIHarmonizerHiddenRootTests, | |
464 | TreeAPIHarmonizerWithCustomTreeCtrlTestCase): | |
465 | pass | |
466 | ||
467 | ||
468 | # TreeHelper tests | |
469 | ||
470 | class TreeHelperTestCase(unittest.TestCase): | |
471 | style = wx.TR_DEFAULT_STYLE | |
472 | ||
473 | def setUp(self): | |
474 | self.frame = wx.Frame(None) | |
475 | class HelperTreeCtrl(treemixin.TreeHelper, | |
476 | treemixin.TreeAPIHarmonizer, self.TreeClass): | |
477 | pass | |
478 | self.tree = HelperTreeCtrl(self.frame, style=self.style) | |
479 | self.populateTree() | |
480 | ||
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') | |
485 | ||
486 | ||
487 | class TreeHelperCommonTests(object): | |
488 | def testGetItemChildren_EmptyTree(self): | |
489 | self.tree.DeleteAllItems() | |
490 | self.assertEqual([], self.tree.GetItemChildren()) | |
491 | ||
492 | def testGetItemChildren_NoParent(self): | |
493 | self.assertEqual([self.item], self.tree.GetItemChildren()) | |
494 | ||
495 | def testGetItemChildren_RootItem(self): | |
496 | self.assertEqual([self.item], self.tree.GetItemChildren(self.root)) | |
497 | ||
498 | def testGetItemChildren_RegularItem(self): | |
499 | self.assertEqual([self.child], self.tree.GetItemChildren(self.item)) | |
500 | ||
501 | def testGetItemChildren_ItemWithoutChildren(self): | |
502 | self.assertEqual([], self.tree.GetItemChildren(self.child)) | |
503 | ||
504 | def testGetItemChildren_NoParent_Recursively(self): | |
505 | self.assertEqual([self.item, self.child], | |
506 | self.tree.GetItemChildren(recursively=True)) | |
507 | ||
508 | def testGetItemChildren_RootItem_Recursively(self): | |
509 | self.assertEqual([self.item, self.child], | |
510 | self.tree.GetItemChildren(self.root, True)) | |
511 | ||
512 | def testGetItemChildren_RegularItem_Recursively(self): | |
513 | self.assertEqual([self.child], | |
514 | self.tree.GetItemChildren(self.item, True)) | |
515 | ||
516 | def testGetItemChildren_ItemWithoutChildren_Recursively(self): | |
517 | self.assertEqual([], self.tree.GetItemChildren(self.child, True)) | |
518 | ||
519 | def testGetItemByIndex_RootItem(self): | |
520 | self.assertEqual(self.root, self.tree.GetItemByIndex(())) | |
521 | ||
522 | def testGetItemByIndex_RegularItem(self): | |
523 | self.assertEqual(self.item, self.tree.GetItemByIndex((0,))) | |
524 | ||
525 | def testGetItemByIndex_Child(self): | |
526 | self.assertEqual(self.child, self.tree.GetItemByIndex((0,0))) | |
527 | ||
528 | def testGetIndexOfItemRootItem(self): | |
529 | self.assertEqual((), self.tree.GetIndexOfItem(self.root)) | |
530 | ||
531 | def testGetIndexOfItemRegularItem(self): | |
532 | self.assertEqual((0,), self.tree.GetIndexOfItem(self.item)) | |
533 | ||
534 | def testGetIndexOfItemChild(self): | |
535 | self.assertEqual((0,0), self.tree.GetIndexOfItem(self.child)) | |
536 | ||
537 | ||
538 | class TreeHelperWithTreeCtrlTestCase(TreeHelperCommonTests, | |
539 | TreeHelperTestCase): | |
540 | TreeClass = wx.TreeCtrl | |
541 | ||
542 | ||
543 | class TreeHelperWithTreeListCtrlTestCase(TreeHelperCommonTests, | |
544 | TreeHelperTestCase): | |
545 | TreeClass = wx.gizmos.TreeListCtrl | |
546 | ||
547 | def populateTree(self): | |
548 | self.tree.AddColumn('Column') | |
549 | super(TreeHelperWithTreeListCtrlTestCase, self).populateTree() | |
550 | ||
551 | ||
552 | class TreeHelperWithCustomTreeCtrlTestCase(TreeHelperCommonTests, | |
553 | TreeHelperTestCase): | |
554 | TreeClass = wx.lib.customtreectrl.CustomTreeCtrl | |
555 | ||
556 | ||
557 | # ExpansionState tests | |
558 | ||
559 | class ExpansionStateTreeCtrl(treemixin.ExpansionState, wx.TreeCtrl): | |
560 | pass | |
561 | ||
562 | ||
563 | class GetExpansionStateTestCase(unittest.TestCase): | |
564 | def setUp(self): | |
565 | self.frame = wx.Frame(None) | |
566 | self.tree = ExpansionStateTreeCtrl(self.frame) | |
567 | ||
568 | def testEmptyTree(self): | |
569 | self.assertEqual([], self.tree.GetExpansionState()) | |
570 | ||
571 | def testRoot(self): | |
572 | self.tree.AddRoot('Root item') | |
573 | self.assertEqual([], self.tree.GetExpansionState()) | |
574 | ||
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()) | |
579 | ||
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()) | |
585 | ||
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) | |
593 | ||
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()) | |
600 | ||
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()) | |
608 | ||
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()) | |
618 | ||
619 | ||
620 | class SetExpansionStateTestCase(unittest.TestCase): | |
621 | def setUp(self): | |
622 | self.frame = wx.Frame(None) | |
623 | self.tree = ExpansionStateTreeCtrl(self.frame) | |
624 | ||
625 | def testEmptyTree(self): | |
626 | self.tree.SetExpansionState([]) | |
627 | ||
628 | def testRoot(self): | |
629 | root = self.tree.AddRoot('Root item') | |
630 | self.tree.SetExpansionState([]) | |
631 | self.failIf(self.tree.IsExpanded(root)) | |
632 | ||
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)) | |
638 | ||
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)) | |
644 | ||
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)) | |
651 | ||
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)) | |
658 | ||
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)) | |
665 | ||
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)) | |
673 | ||
674 | ||
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 | |
677 | ||
678 | class VanillaTreeTestCase(unittest.TestCase): | |
679 | style = wx.TR_DEFAULT_STYLE | |
680 | ||
681 | def setUp(self): | |
682 | self.frame = wx.Frame(None) | |
683 | self.tree = self.TreeClass(self.frame, style=self.style) | |
684 | self.eventsReceived = [] | |
685 | self.populateTree() | |
686 | ||
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] | |
691 | ||
692 | def onEvent(self, event): | |
693 | self.eventsReceived.append(event) | |
694 | ||
695 | ||
696 | class VanillaTreeCommonTests(object): | |
697 | ''' Tests that should succeed for all tree controls and all styles. ''' | |
698 | ||
699 | def testSetItemHasChildren(self): | |
700 | self.tree.SetItemHasChildren(self.item, True) | |
701 | self.failUnless(self.tree.ItemHasChildren(self.item)) | |
702 | ||
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)) | |
708 | ||
709 | ||
710 | class VanillaTreeCtrlTestCase(VanillaTreeCommonTests, VanillaTreeTestCase): | |
711 | TreeClass = wx.TreeCtrl | |
712 | ||
713 | ||
714 | class VanillaTreeListCtrlTestCase(VanillaTreeCommonTests, VanillaTreeTestCase): | |
715 | TreeClass = wx.gizmos.TreeListCtrl | |
716 | ||
717 | def populateTree(self): | |
718 | self.tree.AddColumn('Column 0') | |
719 | super(VanillaTreeListCtrlTestCase, self).populateTree() | |
720 | ||
721 | ||
722 | class VanillaCustomTreeCtrlTestCase(VanillaTreeCommonTests, | |
723 | VanillaTreeTestCase): | |
724 | TreeClass = wx.lib.customtreectrl.CustomTreeCtrl | |
725 | ||
726 | ||
727 | # Tests of the tree controls without any mixin, to document behaviour | |
728 | # that is either different between tree control widgets or undesired | |
729 | # behaviour. | |
730 | ||
731 | class TreeCtrlTestCase(unittest.TestCase): | |
732 | def setUp(self): | |
733 | self.frame = wx.Frame(None) | |
734 | self.tree = wx.TreeCtrl(self.frame, style=wx.TR_HIDE_ROOT) | |
735 | ||
736 | def testSelectHiddenRootItem(self): | |
737 | root = self.tree.AddRoot('Hidden root') | |
738 | self.tree.SelectItem(root) | |
739 | self.assertEqual(root, self.tree.GetSelection()) | |
740 | ||
741 | ||
742 | class CustomTreeCtrlTestCase(unittest.TestCase): | |
743 | def setUp(self): | |
744 | self.frame = wx.Frame(None) | |
745 | self.tree = wx.lib.customtreectrl.CustomTreeCtrl(self.frame, | |
746 | style=wx.TR_HIDE_ROOT) | |
747 | ||
748 | def testSelectHiddenRootItem(self): | |
749 | root = self.tree.AddRoot('Hidden root') | |
750 | self.tree.SelectItem(root) | |
751 | self.assertEqual(root, self.tree.GetSelection()) | |
752 | ||
753 | ||
754 | if __name__ == '__main__': | |
755 | app = wx.App(False) | |
756 | unittest.main() | |
757 | ||
758 |