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.failUnlessIsCFType(CFMachPortRef)
11
12    def testTypeID(self):
13        self.failUnless(isinstance(CFMachPortGetTypeID(), (int, long)))
14
15    def testCreate(self):
16        class Context: pass
17        context = Context()
18
19        def callout(port, msg, size, info):
20            pass
21
22        # This one cannot be tested without bindings to the low-level mach_port API's
23        #port, shouldFree = CFMachPortCreateWithPort(None, 1, callout, context, None)
24        #self.failUnless(isinstance(port, CFMachPortRef))
25        #self.failUnless(shouldFree is True or shouldFree is False)
26
27        port, shouldFree = CFMachPortCreate(None, callout, context, None)
28        self.failUnless(isinstance(port, CFMachPortRef))
29        self.failUnless(shouldFree is True or shouldFree is False)
30
31        idx = CFMachPortGetPort(port)
32        self.failUnless(isinstance(idx, (int, long)))
33
34        ctx = CFMachPortGetContext(port)
35        self.failUnless(ctx is context)
36
37        cb = CFMachPortGetInvalidationCallBack(port)
38        self.failUnless(cb is None)
39
40        global didInvalidate
41        didInvalidate=False
42        def invalidate(port, info):
43            global didInvalidate
44            didInvalidate = True
45
46        CFMachPortSetInvalidationCallBack(port, invalidate)
47        cb = CFMachPortGetInvalidationCallBack(port)
48        self.failUnless(invalidate is cb)
49
50        rls = CFMachPortCreateRunLoopSource(None, port, 0)
51        self.failUnless(isinstance(rls, CFRunLoopSourceRef))
52
53        self.failUnless(CFMachPortIsValid(port))
54        CFMachPortInvalidate(port)
55        self.failIf(CFMachPortIsValid(port))
56        self.failUnless(didInvalidate)
57
58if __name__ == "__main__":
59    main()
60