]>
Commit | Line | Data |
---|---|---|
6ed100b4 RD |
1 | |
2 | ||
3 | import wx, unittest | |
4 | from wx.lib.combotreebox import ComboTreeBox, IterableTreeCtrl | |
5 | ||
6 | ||
7 | class ComboTreeBoxTest(unittest.TestCase): | |
8 | def setUp(self): | |
9 | self.comboBoxEventReceived = False | |
10 | frame = wx.Frame(None) | |
11 | self.comboBox = ComboTreeBox(frame, platform=platform) | |
12 | self.tree = self.comboBox._popupFrame.GetTree() | |
13 | ||
14 | def onComboBox(self, event): | |
15 | self.comboBoxEventReceived = True | |
16 | ||
17 | def testComboBoxIsEmptyByDefault(self): | |
18 | self.assertEqual(0, self.comboBox.GetCount()) | |
19 | ||
20 | def testAddingOneItem(self): | |
21 | self.comboBox.Append('Item 1') | |
22 | self.assertEqual(1, self.comboBox.GetCount()) | |
23 | ||
24 | def testAddingTwoItems(self): | |
25 | self.comboBox.Append('Item 1') | |
26 | self.comboBox.Append('Item 2') | |
27 | self.assertEqual(2, self.comboBox.GetCount()) | |
28 | ||
29 | def testAddingTwoParentAndChild(self): | |
30 | item1 = self.comboBox.Append('Item 1') | |
31 | self.comboBox.Append('Item 2', item1) | |
32 | self.assertEqual(2, self.comboBox.GetCount()) | |
33 | ||
34 | def testSelectingAnItemPutsItInTheComboBox(self): | |
35 | self.comboBox.Append('Item 1') | |
36 | self.comboBox.Bind(wx.EVT_COMBOBOX, self.onComboBox) | |
37 | self.comboBox.NotifyItemSelected('Item 1') | |
38 | self.failUnless(self.comboBoxEventReceived) | |
39 | ||
40 | def testClear(self): | |
41 | self.comboBox.Append('Item 1') | |
42 | self.comboBox.Clear() | |
43 | self.assertEqual(0, self.comboBox.GetCount()) | |
44 | ||
45 | def testDelete(self): | |
46 | self.comboBox.Append('Item 1') | |
47 | self.comboBox.Delete(self.tree.GetFirstItem()) | |
48 | self.assertEqual(0, self.comboBox.GetCount()) | |
49 | ||
50 | def testGetSelection_NoItems(self): | |
51 | self.failIf(self.comboBox.GetSelection().IsOk()) | |
52 | ||
53 | def testGetSelection_NoSelection(self): | |
54 | self.comboBox.Append('Item 1') | |
55 | self.failIf(self.comboBox.GetSelection().IsOk()) | |
56 | ||
57 | def testGetSelection_WithSelection(self): | |
58 | item1 = self.comboBox.Append('Item 1') | |
59 | self.comboBox.SetValue('Item 1') | |
60 | self.assertEqual(item1, self.comboBox.GetSelection()) | |
61 | ||
62 | def testGetSelection_EquallyNamedNodes_SelectedInTree(self): | |
63 | item1 = self.comboBox.Append('Item') | |
64 | item2 = self.comboBox.Append('Item') | |
65 | self.tree.SelectItem(item2) | |
66 | self.assertEqual(self.tree.GetSelection(), self.comboBox.GetSelection()) | |
67 | ||
68 | def testGetSelection_EquallyNamedNodes_TypedInTextBox(self): | |
69 | item1 = self.comboBox.Append('Item') | |
70 | item2 = self.comboBox.Append('Item') | |
71 | self.comboBox.SetValue('Item') | |
72 | self.assertEqual(item1, self.comboBox.GetSelection()) | |
73 | ||
74 | def testFindString_NotPresent(self): | |
75 | self.comboBox.Append('Item 1') | |
76 | self.failIf(self.comboBox.FindString('Item 2').IsOk()) | |
77 | ||
78 | def testFindString_Present(self): | |
79 | self.comboBox.Append('Item 1') | |
80 | self.assertEqual(self.tree.GetFirstItem(), | |
81 | self.comboBox.FindString('Item 1')) | |
82 | ||
83 | def testFindString_Child(self): | |
84 | parent = self.comboBox.Append('Parent') | |
85 | child = self.comboBox.Append('Child', parent=parent) | |
86 | self.assertEqual(child, self.comboBox.FindString('Child')) | |
87 | ||
88 | def testGetString_NotPresent(self): | |
89 | self.assertEqual('', self.comboBox.GetString(self.tree.GetFirstItem())) | |
90 | ||
91 | def testGetString_Present(self): | |
92 | self.comboBox.Append('Item 1') | |
93 | self.assertEqual('Item 1', | |
94 | self.comboBox.GetString(self.tree.GetFirstItem())) | |
95 | ||
96 | def testGetStringSelection_NotPresent(self): | |
97 | self.assertEqual('', self.comboBox.GetStringSelection()) | |
98 | ||
99 | def testGetStringSelection_Present(self): | |
100 | self.comboBox.SetValue('Item 1') | |
101 | self.assertEqual('Item 1', self.comboBox.GetStringSelection()) | |
102 | ||
103 | def testInsertAsFirstItem(self): | |
104 | self.comboBox.Insert('Item 1') | |
105 | self.assertEqual('Item 1', | |
106 | self.comboBox.GetString(self.tree.GetFirstItem())) | |
107 | ||
108 | def testInsertAsFirstItemBeforeExistingItem(self): | |
109 | item1 = self.comboBox.Append('Item 1') | |
110 | item2 = self.comboBox.Insert('Item 2') | |
111 | self.assertEqual(item2, self.tree.GetFirstItem()) | |
112 | ||
113 | def testInsertAsFirstChildBeforeExistingChild(self): | |
114 | parent = self.comboBox.Append('parent') | |
115 | child1 = self.comboBox.Append('child 1', parent) | |
116 | child2 = self.comboBox.Insert('child 2', parent=parent) | |
117 | self.assertEqual(child2, self.tree.GetFirstChild(parent)[0]) | |
118 | ||
119 | def testSelect(self): | |
120 | item1 = self.comboBox.Append('Item 1') | |
121 | self.comboBox.Select(item1) | |
122 | self.assertEqual('Item 1', self.comboBox.GetValue()) | |
123 | ||
124 | def testSetString(self): | |
125 | item1 = self.comboBox.Append('Item 1') | |
126 | self.comboBox.SetString(item1, 'Item 2') | |
127 | self.assertEqual('Item 2', self.comboBox.GetString(item1)) | |
128 | ||
129 | def testSetStringSelection_ExistingString(self): | |
130 | self.comboBox.Append('Hi') | |
131 | self.comboBox.SetStringSelection('Hi') | |
132 | self.assertEqual('Hi', self.comboBox.GetStringSelection()) | |
133 | ||
134 | def testSetStringSelection_NonExistingString(self): | |
135 | self.comboBox.SetStringSelection('Hi') | |
136 | self.assertEqual('', self.comboBox.GetStringSelection()) | |
137 | ||
138 | def testAppendWithClientData(self): | |
139 | item1 = self.comboBox.Append('Item 1', clientData=[1,2,3]) | |
140 | self.assertEqual([1,2,3], self.comboBox.GetClientData(item1)) | |
141 | ||
142 | def testInsertWithClientData(self): | |
143 | item1 = self.comboBox.Append('Item 1') | |
144 | item2 = self.comboBox.Insert('Item 2', previous=item1, | |
145 | clientData=[1,2,3]) | |
146 | self.assertEqual([1,2,3], self.comboBox.GetClientData(item2)) | |
147 | ||
148 | def testSetClientData(self): | |
149 | item1 = self.comboBox.Append('Item 1') | |
150 | self.comboBox.SetClientData(item1, [1,2,3]) | |
151 | self.assertEqual([1,2,3], self.comboBox.GetClientData(item1)) | |
152 | ||
153 | def testFindClientData(self): | |
154 | item1 = self.comboBox.Append('Item 1', clientData='A') | |
155 | self.assertEqual(item1, self.comboBox.FindClientData('A')) | |
156 | ||
157 | def testFindClientData_NoItems(self): | |
158 | self.failIf(self.comboBox.FindClientData('A')) | |
159 | ||
160 | def testFindClientData_NoSuchData(self): | |
161 | item1 = self.comboBox.Append('Item 1', clientData='A') | |
162 | self.failIf(self.comboBox.FindClientData('B')) | |
163 | ||
164 | def testSetClientDataSelection(self): | |
165 | item1 = self.comboBox.Append('Item 1', clientData='A') | |
166 | self.comboBox.SetClientDataSelection('A') | |
167 | self.assertEqual(item1, self.comboBox.GetSelection()) | |
168 | ||
169 | def testSetClientDataSelection_NoSuchData(self): | |
170 | item1 = self.comboBox.Append('Item 1', clientData='A') | |
171 | self.comboBox.SetClientDataSelection('B') | |
172 | self.failIf(self.comboBox.GetSelection()) | |
173 | ||
174 | ||
175 | class SortedComboTreeBoxTest(unittest.TestCase): | |
176 | def setUp(self): | |
177 | frame = wx.Frame(None) | |
178 | self.comboBox = ComboTreeBox(frame, style=wx.CB_SORT, platform=platform) | |
179 | self.tree = self.comboBox._popupFrame.GetTree() | |
180 | ||
181 | def testAppend(self): | |
182 | itemB = self.comboBox.Append('B') | |
183 | itemA = self.comboBox.Append('A') | |
184 | self.assertEqual(itemA, self.tree.GetFirstItem()) | |
185 | ||
186 | def testInsert(self): | |
187 | itemA = self.comboBox.Append('A') | |
188 | itemB = self.comboBox.Insert('B') | |
189 | self.assertEqual(itemA, self.tree.GetFirstItem()) | |
190 | ||
191 | def testAppend_Child(self): | |
192 | itemA = self.comboBox.Append('A') | |
193 | itemA2 = self.comboBox.Append('2', parent=itemA) | |
194 | itemA1 = self.comboBox.Append('1', parent=itemA) | |
195 | self.assertEqual(itemA1, self.tree.GetFirstChild(itemA)[0]) | |
196 | ||
197 | def testInsert_Child(self): | |
198 | itemA = self.comboBox.Append('A') | |
199 | itemA1 = self.comboBox.Append('1', parent=itemA) | |
200 | itemA2 = self.comboBox.Insert('2', parent=itemA) | |
201 | self.assertEqual(itemA1, self.tree.GetFirstChild(itemA)[0]) | |
202 | ||
203 | def testSetString(self): | |
204 | itemB = self.comboBox.Append('B') | |
205 | itemC = self.comboBox.Append('C') | |
206 | self.comboBox.SetString(itemC, 'A') | |
207 | self.assertEqual(itemC, self.tree.GetFirstItem()) | |
208 | ||
209 | ||
210 | class ReadOnlyComboTreeBoxTest(unittest.TestCase): | |
211 | def setUp(self): | |
212 | frame = wx.Frame(None) | |
213 | self.comboBox = ComboTreeBox(frame, style=wx.CB_READONLY) | |
214 | self.tree = self.comboBox._popupFrame.GetTree() | |
215 | ||
216 | def testSetValue_ToNonExistingValue(self): | |
217 | self.comboBox.SetValue('Ignored value') | |
218 | self.assertEqual('', self.comboBox.GetValue()) | |
219 | ||
220 | def testSetValue_ToExistingValue(self): | |
221 | self.comboBox.Append('This works') | |
222 | self.comboBox.SetValue('This works') | |
223 | self.assertEqual('This works', self.comboBox.GetValue()) | |
224 | ||
225 | ||
226 | class IterableTreeCtrlTest(unittest.TestCase): | |
227 | def setUp(self): | |
228 | self.frame = wx.Frame(None) | |
229 | self.tree = IterableTreeCtrl(self.frame) | |
230 | self.root = self.tree.AddRoot('root') | |
231 | ||
232 | def testPreviousOfRootIsInvalid(self): | |
233 | item = self.tree.GetPreviousItem(self.root) | |
234 | self.failIf(item.IsOk()) | |
235 | ||
236 | def testPreviousOfChildOfRootIsRoot(self): | |
237 | child = self.tree.AppendItem(self.root, 'child') | |
238 | self.assertEqual(self.root, self.tree.GetPreviousItem(child)) | |
239 | ||
240 | def testPreviousOfSecondChildOfRootIsFirstChild(self): | |
241 | child1 = self.tree.AppendItem(self.root, 'child1') | |
242 | child2 = self.tree.AppendItem(self.root, 'child2') | |
243 | self.assertEqual(child1, self.tree.GetPreviousItem(child2)) | |
244 | ||
245 | def testPreviousOfGrandChildIsChild(self): | |
246 | child = self.tree.AppendItem(self.root, 'child') | |
247 | grandchild = self.tree.AppendItem(child, 'grandchild') | |
248 | self.assertEqual(child, self.tree.GetPreviousItem(grandchild)) | |
249 | ||
250 | def testPreviousOfSecondChildWhenFirstChildHasChildIsThatChild(self): | |
251 | child1 = self.tree.AppendItem(self.root, 'child1') | |
252 | grandchild = self.tree.AppendItem(child1, 'child of child1') | |
253 | child2 = self.tree.AppendItem(self.root, 'child2') | |
254 | self.assertEqual(grandchild, self.tree.GetPreviousItem(child2)) | |
255 | ||
256 | def testPreviousOfSecondChildWhenFirstChildHasGrandChildIsThatGrandChild(self): | |
257 | child1 = self.tree.AppendItem(self.root, 'child1') | |
258 | grandchild = self.tree.AppendItem(child1, 'child of child1') | |
259 | greatgrandchild = self.tree.AppendItem(grandchild, | |
260 | 'grandchild of child1') | |
261 | child2 = self.tree.AppendItem(self.root, 'child2') | |
262 | self.assertEqual(greatgrandchild, self.tree.GetPreviousItem(child2)) | |
263 | ||
264 | def testNextOfRootIsInvalidWhenRootHasNoChildren(self): | |
265 | item = self.tree.GetNextItem(self.root) | |
266 | self.failIf(item.IsOk()) | |
267 | ||
268 | def testNextOfRootIsItsChildWhenRootHasOneChild(self): | |
269 | child = self.tree.AppendItem(self.root, 'child') | |
270 | self.assertEqual(child, self.tree.GetNextItem(self.root)) | |
271 | ||
272 | def testNextOfLastChildIsInvalid(self): | |
273 | child = self.tree.AppendItem(self.root, 'child') | |
274 | self.failIf(self.tree.GetNextItem(child).IsOk()) | |
275 | ||
276 | def testNextOfFirstChildIsSecondChild(self): | |
277 | child1 = self.tree.AppendItem(self.root, 'child1') | |
278 | child2 = self.tree.AppendItem(self.root, 'child2') | |
279 | self.assertEqual(child2, self.tree.GetNextItem(child1)) | |
280 | ||
281 | def testNextOfGrandChildIsItsParentsSibling(self): | |
282 | child1 = self.tree.AppendItem(self.root, 'child1') | |
283 | grandchild = self.tree.AppendItem(child1, 'child of child1') | |
284 | child2 = self.tree.AppendItem(self.root, 'child2') | |
285 | self.assertEqual(child2, self.tree.GetNextItem(grandchild)) | |
286 | ||
287 | def testNextOfGreatGrandChildIsItsParentsSiblingRecursively(self): | |
288 | child1 = self.tree.AppendItem(self.root, 'child1') | |
289 | grandchild = self.tree.AppendItem(child1, 'child of child1') | |
290 | greatgrandchild = self.tree.AppendItem(grandchild, | |
291 | 'grandchild of child1') | |
292 | child2 = self.tree.AppendItem(self.root, 'child2') | |
293 | self.assertEqual(child2, self.tree.GetNextItem(greatgrandchild)) | |
294 | ||
295 | def testNextOfGrandChildWhenItIsLastIsInvalid(self): | |
296 | child = self.tree.AppendItem(self.root, 'child') | |
297 | grandchild = self.tree.AppendItem(child, 'child of child') | |
298 | self.failIf(self.tree.GetNextItem(grandchild).IsOk()) | |
299 | ||
300 | def testFirstItemIsRoot(self): | |
301 | self.assertEqual(self.root, self.tree.GetFirstItem()) | |
302 | ||
303 | def testGetFirstItemWithoutRootIsInvalid(self): | |
304 | tree = IterableTreeCtrl(self.frame) | |
305 | self.failIf(tree.GetFirstItem().IsOk()) | |
306 | ||
307 | def testGetSelection_NoSelection(self): | |
308 | self.tree.Unselect() | |
309 | self.failIf(self.tree.GetSelection().IsOk()) | |
310 | ||
311 | def testGetSelection_RootItemSelected(self): | |
312 | self.tree.SelectItem(self.tree.GetRootItem()) | |
313 | self.assertEqual(self.tree.GetRootItem(), self.tree.GetSelection()) | |
314 | ||
315 | def testGetSelection_OtherItem(self): | |
316 | child = self.tree.AppendItem(self.root, 'child') | |
317 | self.tree.SelectItem(child) | |
318 | self.assertEqual(child, self.tree.GetSelection()) | |
319 | ||
320 | ||
321 | class IterableTreeCtrlWithHiddenRootTest(unittest.TestCase): | |
322 | def setUp(self): | |
323 | frame = wx.Frame(None) | |
324 | self.tree = IterableTreeCtrl(frame, style=wx.TR_HIDE_ROOT) | |
325 | self.root = self.tree.AddRoot('root') | |
326 | ||
327 | def testPreviousOfChildOfRootIsInvalid(self): | |
328 | child = self.tree.AppendItem(self.root, 'child') | |
329 | self.failIf(self.tree.GetPreviousItem(child).IsOk()) | |
330 | ||
331 | def testNextOfGrandChildWhenItIsLastIsInvalid(self): | |
332 | child = self.tree.AppendItem(self.root, 'child') | |
333 | grandchild = self.tree.AppendItem(child, 'child of child') | |
334 | self.failIf(self.tree.GetNextItem(grandchild).IsOk()) | |
335 | ||
336 | def testRootIsNotTheFirstItem(self): | |
337 | self.failIf(self.tree.GetFirstItem().IsOk()) | |
338 | ||
339 | def testFirstChildOfRootIsTheFirstItem(self): | |
340 | child = self.tree.AppendItem(self.root, 'child') | |
341 | self.assertEqual(child, self.tree.GetFirstItem()) | |
342 | ||
343 | def testGetSelection_NoSelection(self): | |
344 | self.tree.Unselect() | |
345 | self.failIf(self.tree.GetSelection().IsOk()) | |
346 | ||
347 | def testGetSelection_RootItemSelected(self): | |
348 | # Apparently, selecting a hidden root item crashes wxPython on | |
349 | # Windows, so don't do that. | |
350 | if '__WXMSW__' not in wx.PlatformInfo: | |
351 | self.tree.SelectItem(self.tree.GetRootItem()) | |
352 | self.failIf(self.tree.GetSelection().IsOk()) | |
353 | ||
354 | def testGetSelection_OtherItem(self): | |
355 | child = self.tree.AppendItem(self.root, 'child') | |
356 | self.tree.SelectItem(child) | |
357 | self.assertEqual(child, self.tree.GetSelection()) | |
358 | ||
359 | ||
360 | ||
361 | if __name__ == '__main__': | |
362 | import sys | |
363 | if len(sys.argv) > 1: | |
364 | platform = sys.argv[1].upper() | |
365 | del sys.argv[1] | |
366 | else: | |
367 | platform = None | |
368 | ||
369 | app = wx.App(False) | |
370 | unittest.main() | |
371 |