1
2from PyObjCTools.TestSupport import *
3from Quartz import *
4import Quartz
5import os
6
7class TestCGFunction (TestCase):
8    def testFunctions(self):
9        values = []
10        def evaluate(info, input, output):
11            values.append(input)
12            return input * 4
13
14        self.assertIsInstance(CGFunctionGetTypeID(), (int, long))
15
16        myInfo = object()
17        func = CGFunctionCreate(myInfo, 1, [0, 1], 4, [0, 1, 0, 1, 0, 1, 0, 1], evaluate)
18        self.assertIsInstance(func, CGFunctionRef)
19
20        v = CGFunctionRetain(func)
21        self.assertTrue(v is func)
22        CGFunctionRelease(func)
23
24
25        # It is not possible to "call" a CGFunction object directly, use a
26        # shading object to check that the function is actually called.
27
28        shading = CGShadingCreateAxial(CGColorSpaceCreateDeviceRGB(), (0, 0), (50, 50), func, True, True)
29        self.assertIsInstance(shading, CGShadingRef)
30
31        url = CFURLCreateWithFileSystemPath(None,
32                "/tmp/pyobjc.test.pdf", kCFURLPOSIXPathStyle, False)
33        self.assertIsInstance(url, CFURLRef)
34        context = CGPDFContextCreateWithURL(url,
35                ((0, 0), (1000, 1000)), None)
36        self.assertIsInstance(context, CGContextRef)
37        try:
38            CGContextBeginPage(context, objc.NULL)
39
40            CGContextDrawShading(context, shading)
41        finally:
42            CGContextEndPage(context)
43            if hasattr(Quartz, 'CGPDFContextClose'): CGPDFContextClose(context)
44            if os.path.exists("/tmp/pyobjc.test.pdf"):
45                os.unlink("/tmp/pyobjc.test.pdf")
46
47        # Drawing is done, check that the shading function is actually used
48        self.assertNotEqual(len(values), 0)
49        for item in values:
50            self.assertIsInstance(item, tuple)
51            self.assertEqual(len(item), 1)
52            self.assertIsInstance(item[0], float)
53
54        del func
55
56
57
58
59
60
61if __name__ == "__main__":
62    main()
63