1 2from PyObjCTools.TestSupport import * 3from Quartz.CoreGraphics import * 4from Quartz import CoreGraphics 5 6class TestCGPath (TestCase): 7 def testTypes(self): 8 self.assertIsCFType(CGPathRef) 9 self.failIf(hasattr(CoreGraphics, 'CGMutablePathRef')) 10 11 def testFunctions(self): 12 self.assertIsInstance(CGPathGetTypeID(), (int, long)) 13 14 self.assertResultIsCFRetained(CGPathCreateMutable) 15 path = CGPathCreateMutable() 16 self.assertIsInstance(path, CGPathRef) 17 18 self.assertResultIsCFRetained(CGPathCreateCopy) 19 v = CGPathCreateCopy(path) 20 self.assertIsInstance(v, CGPathRef) 21 22 self.assertResultIsCFRetained(CGPathCreateMutableCopy) 23 v = CGPathCreateMutableCopy(path) 24 self.assertIsInstance(v, CGPathRef) 25 26 v = CGPathRetain(path) 27 self.assertTrue(v is path) 28 CGPathRelease(path) 29 30 self.assertResultHasType(CGPathEqualToPath, objc._C_BOOL) 31 v = CGPathEqualToPath(path, path) 32 self.assertTrue(v is True) 33 34 transform = CGAffineTransformIdentity 35 self.assertArgIsIn(CGPathMoveToPoint, 1) 36 CGPathMoveToPoint(path, transform, 10, 30) 37 38 self.assertArgIsIn(CGPathAddLineToPoint, 1) 39 CGPathAddLineToPoint(path, transform, 10, 30) 40 41 self.assertArgIsIn(CGPathAddQuadCurveToPoint, 1) 42 CGPathAddQuadCurveToPoint(path, transform, 10, 30, 90, 90) 43 44 self.assertArgIsIn(CGPathAddCurveToPoint, 1) 45 CGPathAddCurveToPoint(path, transform, 10, 30, 90, 90, 140, 140) 46 47 CGPathCloseSubpath(path) 48 49 self.assertArgIsIn(CGPathAddRect, 1) 50 CGPathAddRect(path, transform, CGRectMake(50, 60, 90, 10)) 51 52 self.assertArgIsIn(CGPathAddRects, 1) 53 self.assertArgIsIn(CGPathAddRects, 2) 54 CGPathAddRects(path, transform, [ CGRectMake(50, 60, 90, 10), CGRectMake(90, 50, 10, 10)], 2) 55 self.assertRaises(ValueError, CGPathAddRects, path, transform, [ CGRectMake(50, 60, 90, 10), CGRectMake(90, 50, 10, 10)], 3) 56 57 58 self.assertArgIsIn(CGPathAddLines, 1) 59 self.assertArgIsIn(CGPathAddLines, 2) 60 CGPathAddLines(path, transform, [ CGPoint(50, 60), CGPoint(90, 50)], 2) 61 self.assertRaises(ValueError, CGPathAddLines, path, transform, [ CGPoint(50, 60), CGPoint(90, 50)], 3) 62 63 self.assertArgIsIn(CGPathAddEllipseInRect, 1) 64 CGPathAddEllipseInRect(path, transform, CGRectMake(50, 60, 20, 20)) 65 66 self.assertArgIsIn(CGPathAddArc, 1) 67 self.assertArgHasType(CGPathAddArc, 7, objc._C_BOOL) 68 CGPathAddArc(path, transform, 50, 60, 30, 2.0, 2.5, True) 69 70 self.assertArgIsIn(CGPathAddArcToPoint, 1) 71 CGPathAddArcToPoint(path, transform, 50, 60, 30, 30, 40) 72 73 path2 = CGPathCreateMutable() 74 self.assertArgIsIn(CGPathAddPath, 1) 75 CGPathAddPath(path, transform, path2) 76 77 self.assertResultHasType(CGPathIsEmpty, objc._C_BOOL) 78 self.assertTrue(CGPathIsEmpty(path2) is True) 79 self.assertTrue(CGPathIsEmpty(path) is False) 80 81 self.assertResultHasType(CGPathIsRect, objc._C_BOOL) 82 v1, v2 = CGPathIsRect(path, None) 83 self.assertTrue(v1 is False) 84 self.assertIsInstance(v2, CGRect) 85 86 v = CGPathGetCurrentPoint(path) 87 self.assertIsInstance(v, CGPoint) 88 self.assertIsInstance(v.x, float) 89 self.assertIsInstance(v.y, float) 90 91 v = CGPathGetBoundingBox(path) 92 self.assertIsInstance(v, CGRect) 93 box = v 94 95 self.assertResultHasType(CGPathContainsPoint, objc._C_BOOL) 96 self.assertArgHasType(CGPathContainsPoint, 3, objc._C_BOOL) 97 self.assertArgIsIn(CGPathContainsPoint, 1) 98 v = CGPathContainsPoint(path, transform, ( 99 CGRectGetMidX(box), 100 CGRectGetMidY(box)), True) 101 self.assertTrue(v is True) 102 103 v = CGPathContainsPoint(path, transform, ( 104 box.origin.x - 1, 105 box.origin.y - 1), True) 106 self.assertTrue(v is False) 107 108 109 110 l = [0] 111 info = object() 112 def applier(ctx, element): 113 l[0] += 1 114 self.assertTrue(ctx is info) 115 self.assertIsInstance(element, CGPathElement) 116 self.assertIsInstance(element.type, (int, long)) 117 self.assertIsInstance(element.points, objc.varlist) 118 self.assertIsInstance(element.points[0], CGPoint) 119 120 CGPathApply(path, info, applier) 121 self.failIfEqual(l[0], 0) 122 123 124 125 126 def testConstants(self): 127 self.assertEqual(kCGPathElementMoveToPoint, 0) 128 self.assertEqual(kCGPathElementAddLineToPoint, 1) 129 self.assertEqual(kCGPathElementAddQuadCurveToPoint, 2) 130 self.assertEqual(kCGPathElementAddCurveToPoint, 3) 131 self.assertEqual(kCGPathElementCloseSubpath, 4) 132 133 def testStructs(self): 134 v = CGPathElement() 135 self.assertTrue(hasattr(CGPathElement, 'type')) 136 self.assertTrue(hasattr(CGPathElement, 'points')) 137 138 139 140 141if __name__ == "__main__": 142 main() 143