Debian Codegraph @

tests/unit/platform/test_mac_objc.py

raw symbols SCIP
1 # Test the Objective-C integration.
2 import unittest
3 import weakref
4 import gc
5
6 import pyglet
7 from tests.annotations import require_platform, Platform
8
9 pytestmark = require_platform(Platform.OSX)
10
11 if pyglet.compat_platform in ("darwin",):
12 from pyglet.libs.darwin import AutoReleasePool, ObjCSubclass, ObjCClass
13 from pyglet.libs.darwin.cocoapy.runtime import get_cached_instances, send_super
14
15 NSObject = ObjCClass('NSObject')
16
17
18 class ObjCIntegrationTest(unittest.TestCase):
19 def test_objc_leak_gc(self):
20 """Test deleting """
21 start_count = len(get_cached_instances())
22 test_object = NSObject.alloc().init()
23 del test_object
24
25 gc.collect()
26
27 self.assertEqual(len(get_cached_instances()), start_count)
28
29 def test_objc_leak_cache_release_manual_delete(self):
30 start_count = len(get_cached_instances())
31 test_object = NSObject.alloc().init()
32
33 self.assertIs(test_object._retained, True)
34
35 test_object.release()
36
37 self.assertIs(test_object._retained, False)
38
39 del test_object
40
41 self.assertEqual(len(get_cached_instances()), start_count)
42
43 def test_objc_subclass_dealloc_release(self):
44 start_count = len(get_cached_instances())
45
46 allocated = True
47
48 class MyCustomObjectTestRelease_Implementation:
49 MyCustomObjectTestRelease = ObjCSubclass("NSObject", "MyCustomObjectTestRelease")
50
51 @MyCustomObjectTestRelease.method('v')
52 def dealloc(self) -> None:
53 nonlocal allocated
54 allocated = False
55 send_super(self, 'dealloc')
56
57 MyCustomObjectTestRelease = ObjCClass('MyCustomObjectTestRelease')
58
59 instance = MyCustomObjectTestRelease.alloc().init()
60 instance.release()
61
62 self.assertEqual(allocated, False)
63
64 del instance
65
66 self.assertEqual(len(get_cached_instances()), start_count)
67
68 def test_objc_subclass_dealloc_autorelease(self):
69 """Pytest doesn't like reusing an ObjCClass in the setUpClass. Just make a new one for testing."""
70 start_count = len(get_cached_instances())
71
72 allocated = True
73
74 class MyCustomObjectTestAutorelease_Implementation:
75 MyCustomObjectTestAutorelease = ObjCSubclass("NSObject", "MyCustomObjectTestAutorelease")
76
77 @MyCustomObjectTestAutorelease.method('v')
78 def dealloc(self) -> None:
79 nonlocal allocated
80 allocated = False
81 send_super(self, 'dealloc')
82
83 MyCustomObjectTestAutorelease = ObjCClass('MyCustomObjectTestAutorelease')
84
85 with AutoReleasePool():
86 MyCustomObjectTestAutorelease.alloc().init().autorelease()
87
88 self.assertEqual(allocated, False)
89 self.assertEqual(len(get_cached_instances()), start_count)
90
91 def test_objc_association(self):
92 """Make sure associated Python objects don't get GC'd until the ObjC instance does."""
93 with AutoReleasePool():
94 test_object = NSObject.alloc().init()
95
96 class Data:
97 pass
98
99 data = Data()
100
101 data_ref = weakref.ref(data)
102
103 test_object.associate("data", data)
104
105 del data
106
107 gc.collect()
108
109 self.assertIs(test_object.data, data_ref())
110
111 del test_object
112 gc.collect()
113
114 self.assertIsNone(data_ref())