1#import "CPTAxisLabelTests.h"
2#import "CPTAxisLabel.h"
3#import "CPTMutableTextStyle.h"
4#import "CPTFill.h"
5#import "CPTBorderedLayer.h"
6#import "CPTColor.h"
7#import "CPTExceptions.h"
8#import <tgmath.h>
9
10static const double precision = 1.0e-6;
11
12@implementation CPTAxisLabelTests
13
14static CGPoint roundPoint(CGPoint position, CGSize contentSize, CGPoint anchor);
15
16static CGPoint roundPoint(CGPoint position, CGSize contentSize, CGPoint anchor)
17{
18	CGPoint newPosition = position;
19	newPosition.x = round(newPosition.x) - round(contentSize.width * anchor.x) + (contentSize.width * anchor.x);
20	newPosition.y = round(newPosition.y) - round(contentSize.height * anchor.y) + (contentSize.height * anchor.y);
21	return newPosition;
22}
23
24-(void)testPositionRelativeToViewPointRaisesForInvalidDirection
25{
26    CPTAxisLabel *label;
27
28    @try {
29        label = [[CPTAxisLabel alloc] initWithText:@"CPTAxisLabelTests-testPositionRelativeToViewPointRaisesForInvalidDirection" textStyle:[CPTTextStyle textStyle]];
30
31        STAssertThrowsSpecificNamed([label positionRelativeToViewPoint:CGPointZero forCoordinate:CPTCoordinateX inDirection:INT_MAX], NSException, NSInvalidArgumentException, @"Should raise NSInvalidArgumentException for invalid direction (type CPTSign)");
32
33    }
34    @finally {
35        [label release];
36    }
37}
38
39-(void)testPositionRelativeToViewPointPositionsForXCoordinate
40{
41    CPTAxisLabel *label;
42    CGFloat start = 100.0;
43
44    @try {
45        label = [[CPTAxisLabel alloc] initWithText:@"CPTAxisLabelTests-testPositionRelativeToViewPointPositionsForXCoordinate" textStyle:[CPTTextStyle textStyle]];
46		CPTLayer *contentLayer = label.contentLayer;
47		CGSize contentSize = contentLayer.bounds.size;
48        label.offset = 20.0;
49
50        CGPoint viewPoint = CGPointMake(start, start);
51
52        contentLayer.anchorPoint = CGPointZero;
53        contentLayer.position = CGPointZero;
54        [label positionRelativeToViewPoint:viewPoint
55                             forCoordinate:CPTCoordinateX
56                               inDirection:CPTSignNone];
57
58		CGPoint newPosition = roundPoint(CGPointMake(start-label.offset, start), contentSize, contentLayer.anchorPoint);
59
60        STAssertEquals(contentLayer.position, newPosition, @"Should add negative offset, %@ != %@", NSStringFromPoint(NSPointFromCGPoint(contentLayer.position)), NSStringFromPoint(NSPointFromCGPoint(newPosition)));
61        STAssertEqualsWithAccuracy(contentLayer.anchorPoint.x, (CGFloat)1.0, precision, @"Should anchor at (1.0,0.5)");
62        STAssertEqualsWithAccuracy(contentLayer.anchorPoint.y, (CGFloat)0.5, precision, @"Should anchor at (1.0,0.5)");
63
64        contentLayer.anchorPoint = CGPointZero;
65        contentLayer.position = CGPointZero;
66        [label positionRelativeToViewPoint:viewPoint
67                             forCoordinate:CPTCoordinateX
68                               inDirection:CPTSignNegative];
69
70		newPosition = roundPoint(CGPointMake(start-label.offset, start), contentSize, contentLayer.anchorPoint);
71
72        STAssertEquals(contentLayer.position, newPosition, @"Should add negative offset, %@ != %@", NSStringFromPoint(NSPointFromCGPoint(contentLayer.position)), NSStringFromPoint(NSPointFromCGPoint(newPosition)));
73        STAssertEqualsWithAccuracy(contentLayer.anchorPoint.x, (CGFloat)1.0, precision, @"Should anchor at (1.0,0.5)");
74        STAssertEqualsWithAccuracy(contentLayer.anchorPoint.y, (CGFloat)0.5, precision, @"Should anchor at (1.0,0.5)");
75
76        contentLayer.anchorPoint = CGPointZero;
77        contentLayer.position = CGPointZero;
78        [label positionRelativeToViewPoint:viewPoint
79                             forCoordinate:CPTCoordinateX
80                               inDirection:CPTSignPositive];
81
82		newPosition = roundPoint(CGPointMake(start+label.offset, start), contentSize, contentLayer.anchorPoint);
83
84        STAssertEquals(contentLayer.position, newPosition, @"Should add positive offset, %@ != %@", NSStringFromPoint(NSPointFromCGPoint(contentLayer.position)), NSStringFromPoint(NSPointFromCGPoint(newPosition)));
85        STAssertEqualsWithAccuracy(contentLayer.anchorPoint.x, (CGFloat)0.0, precision, @"Should anchor at (0.0,0.5)");
86        STAssertEqualsWithAccuracy(contentLayer.anchorPoint.y, (CGFloat)0.5, precision, @"Should anchor at (0.0,0.5)");
87    }
88    @finally {
89        [label release];
90    }
91}
92
93-(void)testPositionRelativeToViewPointPositionsForYCoordinate
94{
95    CPTAxisLabel *label;
96    CGFloat start = 100.0;
97
98    @try {
99        label = [[CPTAxisLabel alloc] initWithText:@"CPTAxisLabelTests-testPositionRelativeToViewPointPositionsForYCoordinate" textStyle:[CPTTextStyle textStyle]];
100		CPTLayer *contentLayer = label.contentLayer;
101		CGSize contentSize = contentLayer.bounds.size;
102		label.offset = 20.0;
103
104        CGPoint viewPoint = CGPointMake(start,start);
105
106        contentLayer.anchorPoint = CGPointZero;
107        contentLayer.position = CGPointZero;
108        [label positionRelativeToViewPoint:viewPoint
109                             forCoordinate:CPTCoordinateY
110                               inDirection:CPTSignNone];
111
112		CGPoint newPosition = roundPoint(CGPointMake(start, start-label.offset), contentSize, contentLayer.anchorPoint);
113
114        STAssertEquals(contentLayer.position, newPosition, @"Should add negative offset, %@ != %@", NSStringFromPoint(NSPointFromCGPoint(contentLayer.position)), NSStringFromPoint(NSPointFromCGPoint(newPosition)));
115        STAssertEqualsWithAccuracy(contentLayer.anchorPoint.x, (CGFloat)0.5, precision, @"Should anchor at (0.5,1.0)");
116        STAssertEqualsWithAccuracy(contentLayer.anchorPoint.y, (CGFloat)1.0, precision, @"Should anchor at (0.5,1.0)");
117
118        contentLayer.anchorPoint = CGPointZero;
119        contentLayer.position = CGPointZero;
120        [label positionRelativeToViewPoint:viewPoint
121                             forCoordinate:CPTCoordinateY
122                               inDirection:CPTSignNegative];
123
124		newPosition = roundPoint(CGPointMake(start, start-label.offset), contentSize, contentLayer.anchorPoint);
125
126        STAssertEquals(contentLayer.position, newPosition, @"Should add negative offset, %@ != %@", NSStringFromPoint(NSPointFromCGPoint(contentLayer.position)), NSStringFromPoint(NSPointFromCGPoint(newPosition)));
127        STAssertEqualsWithAccuracy(contentLayer.anchorPoint.x, (CGFloat)0.5, precision, @"Should anchor at (0.5,1.0)");
128        STAssertEqualsWithAccuracy(contentLayer.anchorPoint.y, (CGFloat)1.0, precision, @"Should anchor at (0.5,1.0)");
129
130        contentLayer.anchorPoint = CGPointZero;
131        contentLayer.position = CGPointZero;
132        [label positionRelativeToViewPoint:viewPoint
133                             forCoordinate:CPTCoordinateY
134                               inDirection:CPTSignPositive];
135
136		newPosition = roundPoint(CGPointMake(start, start+label.offset), contentSize, contentLayer.anchorPoint);
137
138        STAssertEquals(contentLayer.position, newPosition, @"Should add positive offset, %@ != %@", NSStringFromPoint(NSPointFromCGPoint(contentLayer.position)), NSStringFromPoint(NSPointFromCGPoint(newPosition)));
139        STAssertEqualsWithAccuracy(contentLayer.anchorPoint.x, (CGFloat)0.5, precision, @"Should anchor at (0.5,0)");
140        STAssertEqualsWithAccuracy(contentLayer.anchorPoint.y, (CGFloat)0.0, precision, @"Should anchor at (0.5,0)");
141    }
142    @finally {
143        [label release];
144    }
145}
146@end
147