summaryrefslogtreecommitdiff
path: root/tests/test_generictreemodel.py
blob: 6ba71bc53b29d0c1a4707159a259cc7465350e44 (plain)
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# -*- Mode: Python; py-indent-offset: 4 -*-
# test_generictreemodel - Tests for GenericTreeModel
# Copyright (C) 2013 Simon Feltman
#
#   test_generictreemodel.py: Tests for GenericTreeModel
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
# USA


# system
import gc
import sys
import weakref
import unittest

# pygobject
from gi.repository import GObject

try:
    from gi.repository import Gtk
    from pygtkcompat.generictreemodel import GenericTreeModel
    from pygtkcompat.generictreemodel import _get_user_data_as_pyobject
    has_gtk = True
except ImportError:
    GenericTreeModel = object
    has_gtk = False


class Node(object):
    """Represents a generic node with name, value, and children."""
    def __init__(self, name, value, *children):
        self.name = name
        self.value = value
        self.children = list(children)
        self.parent = None
        self.next = None

        for i, child in enumerate(children):
            child.parent = weakref.ref(self)
            if i < len(children) - 1:
                child.next = weakref.ref(children[i + 1])

    def __repr__(self):
        return 'Node("%s", %s)' % (self.name, self.value)


class TesterModel(GenericTreeModel):
    def __init__(self):
        super(TesterModel, self).__init__()
        self.root = Node('root', 0,
                         Node('spam', 1,
                              Node('sushi', 2),
                              Node('bread', 3)
                         ),
                         Node('eggs', 4)
                        )

    def on_get_flags(self):
        return 0

    def on_get_n_columns(self):
        return 2

    def on_get_column_type(self, n):
        return (str, int)[n]

    def on_get_iter(self, path):
        node = self.root
        path = list(path)
        idx = path.pop(0)
        while path:
            idx = path.pop(0)
            node = node.children[idx]
        return node

    def on_get_path(self, node):
        def rec_get_path(n):
            for i, child in enumerate(n.children):
                if child == node:
                    return [i]
                else:
                    res = rec_get_path(child)
                    if res:
                        res.insert(0, i)

        return rec_get_path(self.root)

    def on_get_value(self, node, column):
        if column == 0:
            return node.name
        elif column == 1:
            return node.value

    def on_iter_has_child(self, node):
        return bool(node.children)

    def on_iter_next(self, node):
        if node.next:
            return node.next()

    def on_iter_children(self, node):
        if node:
            return node.children[0]
        else:
            return self.root

    def on_iter_n_children(self, node):
        if node is None:
            return 1
        return len(node.children)

    def on_iter_nth_child(self, node, n):
        if node is None:
            assert n == 0
            return self.root
        return node.children[n]

    def on_iter_parent(self, child):
        if child.parent:
            return child.parent()


@unittest.skipUnless(has_gtk, 'Gtk not available')
class TestReferences(unittest.TestCase):
    def setUp(self):
        pass

    def test_c_tree_iter_user_data_as_pyobject(self):
        obj = object()
        obj_id = id(obj)
        ref_count = sys.getrefcount(obj)

        # This is essentially a stolen ref in the context of _CTreeIter.get_user_data_as_pyobject
        it = Gtk.TreeIter()
        it.user_data = obj_id

        obj2 = _get_user_data_as_pyobject(it)
        self.assertEqual(obj, obj2)
        self.assertEqual(sys.getrefcount(obj), ref_count + 1)

    def test_leak_references_on(self):
        model = TesterModel()
        obj_ref = weakref.ref(model.root)
        # Initial refcount is 1 for model.root + the temporary
        self.assertEqual(sys.getrefcount(model.root), 2)

        # Iter increases by 1 do to assignment to iter.user_data
        res, it = model.do_get_iter([0])
        self.assertEqual(id(model.root), it.user_data)
        self.assertEqual(sys.getrefcount(model.root), 3)

        # Verify getting a TreeIter more then once does not further increase
        # the ref count.
        res2, it2 = model.do_get_iter([0])
        self.assertEqual(id(model.root), it2.user_data)
        self.assertEqual(sys.getrefcount(model.root), 3)

        # Deleting the iter does not decrease refcount because references
        # leak by default (they are stored in the held_refs pool)
        del it
        gc.collect()
        self.assertEqual(sys.getrefcount(model.root), 3)

        # Deleting a model should free all held references to user data
        # stored by TreeIters
        del model
        gc.collect()
        self.assertEqual(obj_ref(), None)

    def test_row_deleted_frees_refs(self):
        model = TesterModel()
        obj_ref = weakref.ref(model.root)
        # Initial refcount is 1 for model.root + the temporary
        self.assertEqual(sys.getrefcount(model.root), 2)

        # Iter increases by 1 do to assignment to iter.user_data
        res, it = model.do_get_iter([0])
        self.assertEqual(id(model.root), it.user_data)
        self.assertEqual(sys.getrefcount(model.root), 3)

        # Notifying the underlying model of a row_deleted should decrease the
        # ref count.
        model.row_deleted(Gtk.TreePath('0'), model.root)
        self.assertEqual(sys.getrefcount(model.root), 2)

        # Finally deleting the actual object should collect it completely
        del model.root
        gc.collect()
        self.assertEqual(obj_ref(), None)

    def test_leak_references_off(self):
        model = TesterModel()
        model.leak_references = False

        obj_ref = weakref.ref(model.root)
        # Initial refcount is 1 for model.root + the temporary
        self.assertEqual(sys.getrefcount(model.root), 2)

        # Iter does not increas count by 1 when leak_references is false
        res, it = model.do_get_iter([0])
        self.assertEqual(id(model.root), it.user_data)
        self.assertEqual(sys.getrefcount(model.root), 2)

        # Deleting the iter does not decrease refcount because assigning user_data
        # eats references and does not release them.
        del it
        gc.collect()
        self.assertEqual(sys.getrefcount(model.root), 2)

        # Deleting the model decreases the final ref, and the object is collected
        del model
        gc.collect()
        self.assertEqual(obj_ref(), None)

    def test_iteration_refs(self):
        # Pull iterators off the model using the wrapped C API which will
        # then call back into the python overrides.
        model = TesterModel()
        nodes = [node for node in model.iter_depth_first()]
        values = [node.value for node in nodes]

        # Verify depth first ordering
        self.assertEqual(values, [0, 1, 2, 3, 4])

        # Verify ref counts for each of the nodes.
        # 5 refs for each node at this point:
        #   1 - ref held in getrefcount function
        #   2 - ref held by "node" var during iteration
        #   3 - ref held by local "nodes" var
        #   4 - ref held by the root/children graph itself
        #   5 - ref held by the model "held_refs" instance var
        for node in nodes:
            self.assertEqual(sys.getrefcount(node), 5)

        # A second iteration and storage of the nodes in a new list
        # should only increase refcounts by 1 even though new
        # iterators are created and assigned.
        nodes2 = [node for node in model.iter_depth_first()]
        for node in nodes2:
            self.assertEqual(sys.getrefcount(node), 6)

        # Hold weak refs and start verifying ref collection.
        node_refs = [weakref.ref(node) for node in nodes]

        # First round of collection
        del nodes2
        gc.collect()
        for node in nodes:
            self.assertEqual(sys.getrefcount(node), 5)

        # Second round of collection, no more local lists of nodes.
        del nodes
        gc.collect()
        for ref in node_refs:
            node = ref()
            self.assertEqual(sys.getrefcount(node), 4)

        # Using invalidate_iters or row_deleted(path, node) will clear out
        # the pooled refs held internal to the GenericTreeModel implementation.
        model.invalidate_iters()
        self.assertEqual(len(model._held_refs), 0)
        gc.collect()
        for ref in node_refs:
            node = ref()
            self.assertEqual(sys.getrefcount(node), 3)

        # Deleting the root node at this point should allow all nodes to be collected
        # as there is no longer a way to reach the children
        del node  # node still in locals() from last iteration
        del model.root
        gc.collect()
        for ref in node_refs:
            self.assertEqual(ref(), None)


@unittest.skipUnless(has_gtk, 'Gtk not available')
class TestIteration(unittest.TestCase):
    def test_iter_next_root(self):
        model = TesterModel()
        it = model.get_iter([0])
        self.assertEqual(it.user_data, id(model.root))
        self.assertEqual(model.root.next, None)

        it = model.iter_next(it)
        self.assertEqual(it, None)

    def test_iter_next_multiple(self):
        model = TesterModel()
        it = model.get_iter([0, 0])
        self.assertEqual(it.user_data, id(model.root.children[0]))

        it = model.iter_next(it)
        self.assertEqual(it.user_data, id(model.root.children[1]))

        it = model.iter_next(it)
        self.assertEqual(it, None)


class ErrorModel(GenericTreeModel):
    # All on_* methods will raise a NotImplementedError by default
    pass


@unittest.skipUnless(has_gtk, 'Gtk not available')
class ExceptHook(object):
    """
    Temporarily installs an exception hook in a context which
    expects the given exc_type to be raised. This allows verification
    of exceptions that occur within python gi callbacks but
    are never bubbled through from python to C back to python.
    This works because exception hooks are called in PyErr_Print.
    """
    def __init__(self, *expected_exc_types):
        self._expected_exc_types = expected_exc_types
        self._exceptions = []

    def _excepthook(self, exc_type, value, traceback):
        self._exceptions.append((exc_type, value))

    def __enter__(self):
        self._oldhook = sys.excepthook
        sys.excepthook = self._excepthook
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        sys.excepthook = self._oldhook
        error_message = 'Expecting the following exceptions: %s, got: %s' % \
            (str(self._expected_exc_types), '\n'.join([str(item) for item in self._exceptions]))

        assert len(self._expected_exc_types) == len(self._exceptions), error_message

        for expected, got in zip(self._expected_exc_types, [exc[0] for exc in self._exceptions]):
            assert issubclass(got, expected), error_message


@unittest.skipUnless(has_gtk, 'Gtk not available')
class TestReturnsAfterError(unittest.TestCase):
    def setUp(self):
        self.model = ErrorModel()

    def test_get_flags(self):
        with ExceptHook(NotImplementedError):
            flags = self.model.get_flags()
        self.assertEqual(flags, 0)

    def test_get_n_columns(self):
        with ExceptHook(NotImplementedError):
            count = self.model.get_n_columns()
        self.assertEqual(count, 0)

    def test_get_column_type(self):
        with ExceptHook(NotImplementedError, TypeError):
            col_type = self.model.get_column_type(0)
        self.assertEqual(col_type, GObject.TYPE_INVALID)

    def test_get_iter(self):
        with ExceptHook(NotImplementedError):
            self.assertRaises(ValueError, self.model.get_iter, Gtk.TreePath(0))

    def test_get_path(self):
        it = self.model.create_tree_iter('foo')
        with ExceptHook(NotImplementedError):
            path = self.model.get_path(it)
        self.assertEqual(path, None)

    def test_get_value(self):
        it = self.model.create_tree_iter('foo')
        with ExceptHook(NotImplementedError):
            try:
                self.model.get_value(it, 0)
            except TypeError:
                pass  # silence TypeError converting None to GValue

    def test_iter_has_child(self):
        it = self.model.create_tree_iter('foo')
        with ExceptHook(NotImplementedError):
            res = self.model.iter_has_child(it)
        self.assertEqual(res, False)

    def test_iter_next(self):
        it = self.model.create_tree_iter('foo')
        with ExceptHook(NotImplementedError):
            res = self.model.iter_next(it)
        self.assertEqual(res, None)

    def test_iter_children(self):
        with ExceptHook(NotImplementedError):
            res = self.model.iter_children(None)
        self.assertEqual(res, None)

    def test_iter_n_children(self):
        with ExceptHook(NotImplementedError):
            res = self.model.iter_n_children(None)
        self.assertEqual(res, 0)

    def test_iter_nth_child(self):
        with ExceptHook(NotImplementedError):
            res = self.model.iter_nth_child(None, 0)
        self.assertEqual(res, None)

    def test_iter_parent(self):
        child = self.model.create_tree_iter('foo')
        with ExceptHook(NotImplementedError):
            res = self.model.iter_parent(child)
        self.assertEqual(res, None)

if __name__ == '__main__':
    unittest.main()