/
test_tree_editor.py
297 lines (241 loc) · 8.82 KB
/
test_tree_editor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# ------------------------------------------------------------------------------
#
# Copyright (c) 2012, Enthought, Inc.
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only
# under the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Author: Pietro Berkes
# Date: Dec 2012
#
# ------------------------------------------------------------------------------
import unittest
from pyface.api import GUI
from traits.api import Bool, HasTraits, Instance, List, Str
from traitsui.api import (
Item,
ObjectTreeNode,
TreeEditor,
TreeNode,
TreeNodeObject,
View,
)
from traitsui.tests._tools import (
create_ui,
press_ok_button,
requires_toolkit,
reraise_exceptions,
ToolkitName,
)
class BogusWrap(HasTraits):
""" A bogus class representing a bogus tree. """
name = Str("Totally bogus")
class Bogus(HasTraits):
""" A bogus class representing a bogus tree. """
name = Str("Bogus")
bogus_list = List()
wrapped_bogus = Instance(BogusWrap)
def _wrapped_bogus_default(self):
return BogusWrap(bogus_list=[Bogus()])
class BogusTreeView(HasTraits):
""" A traitsui view visualizing Bogus objects as trees. """
bogus = Instance(Bogus)
hide_root = Bool()
word_wrap = Bool()
nodes = List(TreeNode)
def _nodes_default(self):
return [
TreeNode(node_for=[Bogus], children="bogus_list", label="=Bogus")
]
def default_traits_view(self):
tree_editor = TreeEditor(
nodes=self.nodes,
hide_root=self.hide_root,
editable=False,
word_wrap=self.word_wrap,
)
traits_view = View(
Item(name="bogus", id="engine", editor=tree_editor), buttons=["OK"]
)
return traits_view
class BogusTreeNodeObject(TreeNodeObject):
""" A bogus tree node. """
name = Str("Bogus")
bogus_list = List()
class BogusTreeNodeObjectView(HasTraits):
""" A traitsui view visualizing Bogus objects as trees. """
bogus = Instance(BogusTreeNodeObject)
hide_root = Bool()
nodes = List(TreeNode)
def _nodes_default(self):
return [
TreeNode(
node_for=[BogusTreeNodeObject],
children="bogus_list",
label="=Bogus",
)
]
def default_traits_view(self):
tree_editor = TreeEditor(
nodes=self.nodes, hide_root=self.hide_root, editable=False
)
traits_view = View(
Item(name="bogus", id="engine", editor=tree_editor), buttons=["OK"]
)
return traits_view
class TestTreeView(unittest.TestCase):
# test for wx is failing for other reasons.
# It might pass once we receive fix for enthought/pyface#558
@requires_toolkit([ToolkitName.qt])
def test_tree_editor_with_nested_ui(self):
tree_editor = TreeEditor(
nodes=[
TreeNode(
node_for=[Bogus],
auto_open=True,
children="bogus_list",
label="bogus",
view=View(Item("name")),
),
],
hide_root=True,
)
bogus_list = [Bogus()]
object_view = BogusTreeView(bogus=Bogus(bogus_list=bogus_list))
view = View(Item("bogus", id="tree", editor=tree_editor))
with reraise_exceptions(), \
create_ui(object_view, dict(view=view)) as ui:
editor = ui.info.tree
editor.selected = bogus_list[0]
GUI.process_events()
def _test_tree_editor_releases_listeners(
self, hide_root, nodes=None, trait="bogus_list", expected_listeners=1
):
""" The TreeEditor should release the listener to the root node's children
when it's disposed of.
"""
bogus = Bogus(bogus_list=[Bogus()])
tree_editor_view = BogusTreeView(
bogus=bogus, hide_root=hide_root, nodes=nodes
)
with reraise_exceptions(), \
create_ui(tree_editor_view) as ui:
# The TreeEditor sets a listener on the bogus object's
# children list
notifiers_list = bogus.trait(trait)._notifiers(False)
self.assertEqual(expected_listeners, len(notifiers_list))
# The listener should be removed after the UI has been closed
notifiers_list = bogus.trait(trait)._notifiers(False)
self.assertEqual(0, len(notifiers_list))
def _test_tree_node_object_releases_listeners(
self, hide_root, nodes=None, trait="bogus_list", expected_listeners=1
):
""" The TreeEditor should release the listener to the root node's children
when it's disposed of.
"""
bogus = BogusTreeNodeObject(bogus_list=[BogusTreeNodeObject()])
tree_editor_view = BogusTreeNodeObjectView(
bogus=bogus, hide_root=hide_root, nodes=nodes
)
with reraise_exceptions(), \
create_ui(tree_editor_view) as ui:
# The TreeEditor sets a listener on the bogus object's
# children list
notifiers_list = bogus.trait(trait)._notifiers(False)
self.assertEqual(expected_listeners, len(notifiers_list))
if trait == "name":
# fire a label change
bogus.name = "Something else"
else:
# change the children
bogus.bogus_list.append(BogusTreeNodeObject())
# The listener should be removed after the UI has been closed
notifiers_list = bogus.trait(trait)._notifiers(False)
self.assertEqual(0, len(notifiers_list))
@requires_toolkit([ToolkitName.qt])
def test_tree_editor_listeners_with_shown_root(self):
nodes = [
TreeNode(node_for=[Bogus], children="bogus_list", label="=Bogus")
]
self._test_tree_editor_releases_listeners(hide_root=False, nodes=nodes)
@requires_toolkit([ToolkitName.qt])
def test_tree_editor_listeners_with_hidden_root(self):
nodes = [
TreeNode(node_for=[Bogus], children="bogus_list", label="=Bogus")
]
self._test_tree_editor_releases_listeners(hide_root=True, nodes=nodes)
@requires_toolkit([ToolkitName.qt])
def test_tree_editor_label_listener(self):
nodes = [
TreeNode(node_for=[Bogus], children="bogus_list", label="name")
]
self._test_tree_editor_releases_listeners(
hide_root=False, nodes=nodes, trait="name"
)
@requires_toolkit([ToolkitName.qt])
def test_tree_editor_xgetattr_label_listener(self):
nodes = [
TreeNode(
node_for=[Bogus],
children="bogus_list",
label="wrapped_bogus.name",
)
]
self._test_tree_editor_releases_listeners(
hide_root=False,
nodes=nodes,
trait="wrapped_bogus",
expected_listeners=2,
)
@requires_toolkit([ToolkitName.qt])
def test_tree_node_object_listeners_with_shown_root(self):
nodes = [
ObjectTreeNode(
node_for=[BogusTreeNodeObject],
children="bogus_list",
label="=Bogus",
)
]
self._test_tree_node_object_releases_listeners(
nodes=nodes, hide_root=False
)
@requires_toolkit([ToolkitName.qt])
def test_tree_node_object_listeners_with_hidden_root(self):
nodes = [
ObjectTreeNode(
node_for=[BogusTreeNodeObject],
children="bogus_list",
label="=Bogus",
)
]
self._test_tree_node_object_releases_listeners(
nodes=nodes, hide_root=True
)
@requires_toolkit([ToolkitName.qt])
def test_tree_node_object_label_listener(self):
nodes = [
ObjectTreeNode(
node_for=[BogusTreeNodeObject],
children="bogus_list",
label="name",
)
]
self._test_tree_node_object_releases_listeners(
nodes=nodes, hide_root=False, trait="name"
)
@requires_toolkit([ToolkitName.qt, ToolkitName.wx])
def test_smoke_save_restore_prefs(self):
bogus = Bogus(bogus_list=[Bogus()])
tree_editor_view = BogusTreeView(bogus=bogus)
with create_ui(tree_editor_view) as ui:
prefs = ui.get_prefs()
ui.set_prefs(prefs)
@requires_toolkit([ToolkitName.qt])
def test_smoke_word_wrap(self):
bogus = Bogus(bogus_list=[Bogus()])
tree_editor_view = BogusTreeView(bogus=bogus, word_wrap=True)
with create_ui(tree_editor_view):
pass