File: test_list.py

package info (click to toggle)
pigment-python 0.3.12-2.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,820 kB
  • ctags: 2,955
  • sloc: python: 11,664; sh: 10,131; makefile: 250; ansic: 97
file content (212 lines) | stat: -rw-r--r-- 7,737 bytes parent folder | download | duplicates (3)
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
# -*- mode: python; coding: utf-8 -*-
#
# Pigment Python tools unit tests
#
# Copyright © 2006, 2007, 2008 Fluendo Embedded S.L.
#
# 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 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., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

import unittest
import sys
import gobject

from pgm.graph.group import Group
from pgm.graph.image import Image
from pgm.widgets.list import List
import pgm
 
class TestList(unittest.TestCase):

    def test_image(self):
        canvas = pgm.Canvas()

        l = List(canvas, pgm.DRAWABLE_MIDDLE)
        img = Image()

        pygobj_has_toggle_notify = gobject.pygobject_version >= (2, 13, 0)
        if pygobj_has_toggle_notify:
            initial_refcount = 2
        else:
            initial_refcount = 3
        expected_refcount = initial_refcount

        self.assertEqual(sys.getrefcount(l), initial_refcount)
        self.assertEqual(sys.getrefcount(img), initial_refcount)

        # adding img directly to the list _as a Group_
        l.add(img)
        if pygobj_has_toggle_notify:
            # here one reference is taken by the group, another by the canvas
            expected_refcount += 2
        else:
            # just a reference by the group
            expected_refcount += 1
        self.assertEqual (sys.getrefcount(img), expected_refcount)

        # trying to add img again to the list _as a Group_
        l.add(img)
        self.assertEqual(sys.getrefcount(img), expected_refcount)

        # removing img from the list
        l.remove(img)
        if pygobj_has_toggle_notify:
            expected_refcount -= 2
        else:
            expected_refcount -= 1
        self.assertEqual(sys.getrefcount(img), expected_refcount)

        # same but adding img to the canvas before adding it to the list _as a
        # Group_
        canvas.add(pgm.DRAWABLE_MIDDLE, img)
        if pygobj_has_toggle_notify:
            # g_object_ref is called on the object here, so pygobject calls
            # Py_INCREF on img
            expected_refcount += 1
        self.assertEqual(sys.getrefcount(img), expected_refcount)

        l.add(img)
        expected_refcount += 1
        self.assertEqual(sys.getrefcount(img), expected_refcount)
        l.remove(img)
        if pygobj_has_toggle_notify:
            # a ref is also removed from the canvas here
            expected_refcount -= 2
        else:
            expected_refcount -= 1
        self.assertEqual(sys.getrefcount(img), expected_refcount)

        canvas.remove(img)
        self.assertEqual(sys.getrefcount(img), initial_refcount)

        # adding img directly to the list _as a List_

        l.append(img)
        # 4 references added: one for the Group, one for the List, two for the
        # implicitely animated object (implicit + one modifier)
        expected_refcount += 4
        if pygobj_has_toggle_notify:
            # the Group takes two refs here (one in the canvas)
            expected_refcount += 1

        self.assertEqual(sys.getrefcount(img), expected_refcount)
        self.assertEqual(len(l.widgets), 1)
        self.assertEqual(len(l.animated_widgets), 1)

        # trying to add img again to the list _as a List_
        l.append(img)
        self.assertEqual(sys.getrefcount(img), expected_refcount)
        self.assertEqual(len(l.widgets), 1)
        self.assertEqual(len(l.animated_widgets), 1)

        # removing img from the list
        l.remove(img)
        self.assertEqual(len(l.widgets), 0)
        self.assertEqual(len(l.animated_widgets), 0)
        self.assertEqual(sys.getrefcount(img), initial_refcount)

    def test_group(self):
        canvas = pgm.Canvas()
        l = List(canvas, pgm.DRAWABLE_MIDDLE)
        g = Group(canvas, pgm.DRAWABLE_MIDDLE)


        pygobj_has_toggle_notify = gobject.pygobject_version >= (2, 13, 0)
        if pygobj_has_toggle_notify:
            g_initial_refcount = 2
            l_initial_refcount = 2
        else:
            g_initial_refcount = 3
            l_initial_refcount = 3

        self.assertEqual(sys.getrefcount(l), l_initial_refcount)
        self.assertEqual(sys.getrefcount(g), g_initial_refcount)

        # adding group directly to the list _as a List_
        l.append(g)
        self.assertEqual(sys.getrefcount(g), g_initial_refcount+6)
        self.assertEqual(len(l.widgets), 1)
        self.assertEqual(len(l.animated_widgets), 1)

        # removing group from the list
        l.remove(g)
        self.assertEqual(len(l.widgets), 0)
        self.assertEqual(len(l.animated_widgets), 0)
        self.assertEqual(sys.getrefcount(g), g_initial_refcount)

    def test_multiple(self):
        canvas = pgm.Canvas()
        l = List(canvas, pgm.DRAWABLE_MIDDLE)
        g = Group(canvas, pgm.DRAWABLE_MIDDLE)
        img = Image()
        pygobj_has_toggle_notify = gobject.pygobject_version >= (2, 13, 0)
        if pygobj_has_toggle_notify:
            img_initial_refcount = 2
            g_initial_refcount = g_expected_refcount = 2
            l_initial_refcount = 2
        else:
            img_initial_refcount = 3
            g_initial_refcount = g_expected_refcount = 3
            l_initial_refcount = 3
        img_expected_refcount = img_initial_refcount

        self.assertEqual(sys.getrefcount(l), l_initial_refcount)
        self.assertEqual(sys.getrefcount(g), g_initial_refcount)
        self.assertEqual(sys.getrefcount(img), img_initial_refcount)

        # adding group directly to the list _as a List_
        l.append(g)
        l.append(img)
        if pygobj_has_toggle_notify:
            img_expected_refcount += 5
        else:
            img_expected_refcount += 4
        g_expected_refcount += 6
        self.assertEqual(sys.getrefcount(img), img_expected_refcount)
        self.assertEqual(sys.getrefcount(g), g_expected_refcount)
        self.assertEqual(len(l.widgets), 2)
        self.assertEqual(len(l.animated_widgets), 2)

        # removing img from the list
        l.remove(img)
        if pygobj_has_toggle_notify:
            img_expected_refcount -= 5
        else:
            img_expected_refcount -= 4
        self.assertEqual(len(l.widgets), 1)
        self.assertEqual(len(l.animated_widgets), 1)
        self.assertEqual(sys.getrefcount(img), img_expected_refcount)

        # adding img to the group
        g.add(img)
        if pygobj_has_toggle_notify:
            img_expected_refcount += 2
        else:
            img_expected_refcount += 1
        self.assertEqual(len(l.widgets), 1)
        self.assertEqual(len(l.animated_widgets), 1)
        self.assertEqual(sys.getrefcount(img), img_expected_refcount)
        self.assertEqual(sys.getrefcount(g), g_expected_refcount)

        # removing group from the list
        l.remove(g)
        g.remove(img)
        g_expected_refcount -= 6
        img_expected_refcount -= 1
        self.assertEqual(len(l.widgets), 0)
        self.assertEqual(len(l.animated_widgets), 0)
        self.assertEqual(sys.getrefcount(l), l_initial_refcount)
        self.assertEqual(sys.getrefcount(g), g_initial_refcount)
        self.assertEqual(sys.getrefcount(img), img_initial_refcount)