1from PyObjCTools.TestSupport import *
2from CoreFoundation import *
3
4
5try:
6    long
7except NameError:
8    long = int
9
10
11class TestNotificationCenter (TestCase):
12
13    def testTypes(self):
14        self.assertIsCFType(CFNotificationCenterRef)
15
16    def testTypeID(self):
17        self.assertIsInstance(CFNotificationCenterGetTypeID(), (int, long))
18
19    def testGetting(self):
20        ref = CFNotificationCenterGetLocalCenter();
21        self.assertIsInstance(ref,CFNotificationCenterRef)
22        ref = CFNotificationCenterGetDistributedCenter();
23        self.assertIsInstance(ref,CFNotificationCenterRef)
24        ref = CFNotificationCenterGetDarwinNotifyCenter();
25        self.assertIsInstance(ref,CFNotificationCenterRef)
26
27    def testSending(self):
28        ref = CFNotificationCenterGetLocalCenter();
29        self.assertIsInstance(ref,CFNotificationCenterRef)
30        notifications = []
31        @objc.callbackFor(CFNotificationCenterAddObserver)
32        def observe(center, observer, name, object, userInfo):
33            notifications.append(( center, observer, name, object, userInfo ))
34
35        self.assertArgHasType(CFNotificationCenterAddObserver, 1, b'@')
36        self.assertArgIsFunction(CFNotificationCenterAddObserver, 2, b'v@@@@@', True)
37        self.assertArgHasType(CFNotificationCenterAddObserver, 4, b'@')
38
39        args = {}
40        args["object"] = b"object".decode('ascii')
41        args["pyobjc.test"] = b"pyobjc.test".decode('ascii')
42
43        CFNotificationCenterAddObserver(ref, args["object"], observe, args["pyobjc.test"], ref, CFNotificationSuspensionBehaviorDeliverImmediately)
44
45        CFNotificationCenterPostNotificationWithOptions(ref, b"pyobjc.test".decode('ascii'), ref, {b"name".decode('ascii'):b"value".decode('ascii')},  kCFNotificationPostToAllSessions)
46        self.assertEqual(len(notifications) , 1)
47        info = notifications[-1]
48        self.assertIs(info[0], ref)
49        self.assertEqual(info[1] , b"object".decode('ascii'))
50        self.assertEqual(info[2] , b"pyobjc.test".decode('ascii'))
51        self.assertIs(info[3], ref)
52        self.assertEqual(info[4] , {b"name".decode('ascii'):b"value".decode('ascii')})
53        CFNotificationCenterPostNotification(ref, b"pyobjc.test".decode('ascii'), ref, {b"name2".decode('ascii'):b"value2".decode('ascii')},  True)
54        self.assertEqual(len(notifications) , 2)
55        info = notifications[-1]
56        self.assertIs(info[0], ref)
57        self.assertEqual(info[1] , b"object".decode('ascii'))
58        self.assertEqual(info[2] , b"pyobjc.test".decode('ascii'))
59        self.assertIs(info[3], ref)
60        self.assertEqual(info[4] , {b"name2".decode('ascii'):b"value2".decode('ascii')})
61        self.assertArgHasType(CFNotificationCenterRemoveObserver, 1, b'@')
62        self.assertArgHasType(CFNotificationCenterRemoveObserver, 3, b'@')
63        CFNotificationCenterRemoveObserver(ref, args["object"], args["pyobjc.test"], ref)
64
65        self.assertArgHasType(CFNotificationCenterPostNotificationWithOptions, 2, b'@')
66        CFNotificationCenterPostNotificationWithOptions(ref, b"pyobjc.test".decode('ascii'), ref, {b"name".decode('ascii'):b"value".decode('ascii')},  kCFNotificationPostToAllSessions)
67        self.assertEqual(len(notifications) , 2)
68
69        CFNotificationCenterAddObserver(ref, args["object"], observe, args["pyobjc.test"], ref, CFNotificationSuspensionBehaviorDeliverImmediately)
70
71        self.assertArgHasType(CFNotificationCenterPostNotification, 2, b'@')
72        self.assertArgIsBOOL(CFNotificationCenterPostNotification, 4)
73        CFNotificationCenterPostNotification(ref, b"pyobjc.test".decode('ascii'), ref, {b"name2".decode('ascii'):b"value2".decode('ascii')},  True)
74        self.assertEqual(len(notifications) , 3)
75        CFNotificationCenterRemoveEveryObserver(ref, args["object"])
76        CFNotificationCenterPostNotification(ref, b"pyobjc.test".decode('ascii'), ref, {b"name2".decode('ascii'):b"value2".decode('ascii')},  True)
77        self.assertEqual(len(notifications) , 3)
78
79    def testConstants(self):
80        self.assertEqual(CFNotificationSuspensionBehaviorDrop, 1)
81        self.assertEqual(CFNotificationSuspensionBehaviorCoalesce, 2)
82        self.assertEqual(CFNotificationSuspensionBehaviorHold, 3)
83        self.assertEqual(CFNotificationSuspensionBehaviorDeliverImmediately, 4)
84
85        self.assertEqual(kCFNotificationDeliverImmediately, 1)
86        self.assertEqual(kCFNotificationPostToAllSessions, 2)
87
88if __name__ == "__main__":
89    main()
90