1"""
2FIXME: None of these tests actually use the MachPort
3"""
4from PyObjCTools.TestSupport import *
5from CoreFoundation import *
6
7
8class TestMachPort (TestCase):
9    def testTypes(self):
10        self.assertIsCFType(CFMachPortRef)
11
12    def testTypeID(self):
13        self.assertIsInstance(CFMachPortGetTypeID(), (int, long))
14    def testCreate(self):
15        class Context: pass
16        context = Context()
17
18        def callout(port, msg, size, info):
19            pass
20
21        # This one cannot be tested without bindings to the low-level mach_port API's
22        #port, shouldFree = CFMachPortCreateWithPort(None, 1, callout, context, None)
23        #self.assertIsInstance(port, CFMachPortRef)
24        #self.assertIs(shouldFree is True or shouldFree, False)
25        port, shouldFree = CFMachPortCreate(None, callout, context, None)
26        self.assertIsInstance(port, CFMachPortRef)
27        self.assertIs(shouldFree is True or shouldFree, False)
28        idx = CFMachPortGetPort(port)
29        self.assertIsInstance(idx, (int, long))
30        ctx = CFMachPortGetContext(port, None)
31        self.assertIs(ctx, context)
32        cb = CFMachPortGetInvalidationCallBack(port)
33        self.assertIs(cb, None)
34        global didInvalidate
35        didInvalidate=False
36        def invalidate(port, info):
37            global didInvalidate
38            didInvalidate = True
39
40        CFMachPortSetInvalidationCallBack(port, invalidate)
41        cb = CFMachPortGetInvalidationCallBack(port)
42        self.assertIs(invalidate, cb)
43        rls = CFMachPortCreateRunLoopSource(None, port, 0)
44        self.assertIsInstance(rls, CFRunLoopSourceRef)
45        self.assertTrue(CFMachPortIsValid(port))
46        CFMachPortInvalidate(port)
47        self.assertFalse(CFMachPortIsValid(port))
48        self.assertTrue(didInvalidate)
49
50if __name__ == "__main__":
51    main()
52