Branch and push for 2.0
[profile/ivi/pygobject2.git] / tests / test_gobject.py
1 # -*- Mode: Python -*-
2
3 import unittest
4
5 import gobject
6 import sys
7 import testhelper
8
9
10 class TestGObjectAPI(unittest.TestCase):
11     def testGObjectModule(self):
12         obj = gobject.GObject()
13         self.assertEquals(obj.__module__,
14                           'gobject._gobject')
15
16
17 class TestReferenceCounting(unittest.TestCase):
18     def testRegularObject(self):
19         obj = gobject.GObject()
20         self.assertEquals(obj.__grefcount__, 1)
21
22         obj = gobject.new(gobject.GObject)
23         self.assertEquals(obj.__grefcount__, 1)
24
25     def testFloatingWithSinkFunc(self):
26         obj = testhelper.FloatingWithSinkFunc()
27         self.assertEquals(obj.__grefcount__, 1)
28
29         obj = gobject.new(testhelper.FloatingWithSinkFunc)
30         self.assertEquals(obj.__grefcount__, 1)
31
32     def testFloatingWithoutSinkFunc(self):
33         obj = testhelper.FloatingWithoutSinkFunc()
34         self.assertEquals(obj.__grefcount__, 1)
35
36         obj = gobject.new(testhelper.FloatingWithoutSinkFunc)
37         self.assertEquals(obj.__grefcount__, 1)
38
39     def testOwnedByLibrary(self):
40         # Upon creation, the refcount of the object should be 2:
41         # - someone already has a reference on the new object.
42         # - the python wrapper should hold its own reference.
43         obj = testhelper.OwnedByLibrary()
44         self.assertEquals(obj.__grefcount__, 2)
45
46         # We ask the library to release its reference, so the only
47         # remaining ref should be our wrapper's. Once the wrapper
48         # will run out of scope, the object will get finalized.
49         obj.release()
50         self.assertEquals(obj.__grefcount__, 1)
51
52     def testOwnedByLibraryOutOfScope(self):
53         obj = testhelper.OwnedByLibrary()
54         self.assertEquals(obj.__grefcount__, 2)
55
56         # We are manually taking the object out of scope. This means
57         # that our wrapper has been freed, and its reference dropped. We
58         # cannot check it but the refcount should now be 1 (the ref held
59         # by the library is still there, we didn't call release()
60         obj = None
61
62         # When we get the object back from the lib, the wrapper is
63         # re-created, so our refcount will be 2 once again.
64         obj = testhelper.owned_by_library_get_instance_list()[0]
65         self.assertEquals(obj.__grefcount__, 2)
66
67         obj.release()
68         self.assertEquals(obj.__grefcount__, 1)
69
70     def testOwnedByLibraryUsingGObjectNew(self):
71         # Upon creation, the refcount of the object should be 2:
72         # - someone already has a reference on the new object.
73         # - the python wrapper should hold its own reference.
74         obj = gobject.new(testhelper.OwnedByLibrary)
75         self.assertEquals(obj.__grefcount__, 2)
76
77         # We ask the library to release its reference, so the only
78         # remaining ref should be our wrapper's. Once the wrapper
79         # will run out of scope, the object will get finalized.
80         obj.release()
81         self.assertEquals(obj.__grefcount__, 1)
82
83     def testOwnedByLibraryOutOfScopeUsingGobjectNew(self):
84         obj = gobject.new(testhelper.OwnedByLibrary)
85         self.assertEquals(obj.__grefcount__, 2)
86
87         # We are manually taking the object out of scope. This means
88         # that our wrapper has been freed, and its reference dropped. We
89         # cannot check it but the refcount should now be 1 (the ref held
90         # by the library is still there, we didn't call release()
91         obj = None
92
93         # When we get the object back from the lib, the wrapper is
94         # re-created, so our refcount will be 2 once again.
95         obj = testhelper.owned_by_library_get_instance_list()[0]
96         self.assertEquals(obj.__grefcount__, 2)
97
98         obj.release()
99         self.assertEquals(obj.__grefcount__, 1)
100
101     def testFloatingAndSunk(self):
102         # Upon creation, the refcount of the object should be 2:
103         # - someone already has a reference on the new object.
104         # - the python wrapper should hold its own reference.
105         obj = testhelper.FloatingAndSunk()
106         self.assertEquals(obj.__grefcount__, 2)
107
108         # We ask the library to release its reference, so the only
109         # remaining ref should be our wrapper's. Once the wrapper
110         # will run out of scope, the object will get finalized.
111         obj.release()
112         self.assertEquals(obj.__grefcount__, 1)
113
114     def testFloatingAndSunkOutOfScope(self):
115         obj = testhelper.FloatingAndSunk()
116         self.assertEquals(obj.__grefcount__, 2)
117
118         # We are manually taking the object out of scope. This means
119         # that our wrapper has been freed, and its reference dropped. We
120         # cannot check it but the refcount should now be 1 (the ref held
121         # by the library is still there, we didn't call release()
122         obj = None
123
124         # When we get the object back from the lib, the wrapper is
125         # re-created, so our refcount will be 2 once again.
126         obj = testhelper.floating_and_sunk_get_instance_list()[0]
127         self.assertEquals(obj.__grefcount__, 2)
128
129         obj.release()
130         self.assertEquals(obj.__grefcount__, 1)
131
132  
133     def testFloatingAndSunkUsingGObjectNew(self):
134         # Upon creation, the refcount of the object should be 2:
135         # - someone already has a reference on the new object.
136         # - the python wrapper should hold its own reference.
137         obj = gobject.new(testhelper.FloatingAndSunk)
138         self.assertEquals(obj.__grefcount__, 2)
139
140         # We ask the library to release its reference, so the only
141         # remaining ref should be our wrapper's. Once the wrapper
142         # will run out of scope, the object will get finalized.
143         obj.release()
144         self.assertEquals(obj.__grefcount__, 1)
145
146     def testFloatingAndSunkOutOfScopeUsingGObjectNew(self):
147         obj = gobject.new(testhelper.FloatingAndSunk)
148         self.assertEquals(obj.__grefcount__, 2)
149
150         # We are manually taking the object out of scope. This means
151         # that our wrapper has been freed, and its reference dropped. We
152         # cannot check it but the refcount should now be 1 (the ref held
153         # by the library is still there, we didn't call release()
154         obj = None
155
156         # When we get the object back from the lib, the wrapper is
157         # re-created, so our refcount will be 2 once again.
158         obj = testhelper.floating_and_sunk_get_instance_list()[0]
159         self.assertEquals(obj.__grefcount__, 2)
160
161         obj.release()
162         self.assertEquals(obj.__grefcount__, 1)
163
164 class A(gobject.GObject):
165     def __init__(self):
166         super(A, self).__init__()
167
168 class TestPythonReferenceCounting(unittest.TestCase):
169     # Newly created instances should alwayshave two references: one for
170     # the GC, and one for the bound variable in the local scope.
171
172     def testNewInstanceHasTwoRefs(self):
173         obj = gobject.GObject()
174         self.assertEquals(sys.getrefcount(obj), 2)
175
176     def testNewInstanceHasTwoRefsUsingGObjectNew(self):
177         obj = gobject.new(gobject.GObject)
178         self.assertEquals(sys.getrefcount(obj), 2)
179
180     def testNewSubclassInstanceHasTwoRefs(self):
181         obj = A()
182         self.assertEquals(sys.getrefcount(obj), 2)
183
184     def testNewSubclassInstanceHasTwoRefsUsingGObjectNew(self):
185         obj = gobject.new(A)
186         self.assertEquals(sys.getrefcount(obj), 2)