1"""
2FIXME: None of these tests actually use the MachPort
3"""
4from PyObjCTools.TestSupport import *
5from CoreFoundation import *
6import Foundation
7
8
9try:
10    long
11except NameError:
12    long = int
13
14
15class TestMachPort (TestCase):
16    def testTypes(self):
17        self.assertIsCFType(CFMachPortRef)
18
19    def testTypeID(self):
20        self.assertIsInstance(CFMachPortGetTypeID(), (int, long))
21
22    @min_os_level('10.8')
23    @expectedFailure
24    def testCreate10_8(self):
25        class Context: pass
26        context = Context()
27
28        def callout(port, msg, size, info):
29            pass
30
31        port, shouldFree = CFMachPortCreate(None, callout, context, None)
32
33        # On OSX 10.7 or earlier this test passed, on OSX 10.8 it doesn't???
34        self.assertIsInstance(port, Foundation.NSMachPort)
35
36    def testCreate(self):
37
38        class Context: pass
39        context = Context()
40
41        def callout(port, msg, size, info):
42            pass
43
44        # XXX: This one cannot be tested without bindings to the low-level mach_port API's
45        #port, shouldFree = CFMachPortCreateWithPort(None, 1, callout, context, None)
46        #self.assertIsInstance(port, CFMachPortRef)
47        #self.assertTrue(shouldFree is True or shouldFree is False)
48
49        port, shouldFree = CFMachPortCreate(None, callout, context, None)
50
51        if os_release() < (10, 8):
52            self.assertIsInstance(port, Foundation.NSMachPort)
53        self.assertIsInstance(port, Foundation.NSPort)
54        self.assertTrue(shouldFree is True or shouldFree is False)
55        idx = CFMachPortGetPort(port)
56        self.assertIsInstance(idx, (int, long))
57        ctx = CFMachPortGetContext(port, None)
58        self.assertIs(ctx, context)
59        cb = CFMachPortGetInvalidationCallBack(port)
60        self.assertIs(cb, None)
61        global didInvalidate
62        didInvalidate=False
63        def invalidate(port, info):
64            global didInvalidate
65            didInvalidate = True
66
67        CFMachPortSetInvalidationCallBack(port, invalidate)
68        cb = CFMachPortGetInvalidationCallBack(port)
69        self.assertIs(invalidate, cb)
70        rls = CFMachPortCreateRunLoopSource(None, port, 0)
71        self.assertIsInstance(rls, CFRunLoopSourceRef)
72        self.assertTrue(CFMachPortIsValid(port))
73        CFMachPortInvalidate(port)
74        self.assertFalse(CFMachPortIsValid(port))
75        self.assertTrue(didInvalidate)
76
77if __name__ == "__main__":
78    main()
79