summaryrefslogtreecommitdiff
path: root/platform/ios/test/MGLMapViewContentInsetTests.m
blob: 185baf4a0502b88a2a78df10c8d2829b2bf01d10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#import <Mapbox/Mapbox.h>
#import <XCTest/XCTest.h>

@interface MGLMapViewContentInsetTests : XCTestCase <MGLMapViewDelegate>

@property (nonatomic) MGLMapView *mapView;
@property (nonatomic) UIWindow *window;
@property (nonatomic) UIViewController *viewController;
@property (nonatomic) XCTestExpectation *styleLoadingExpectation;
@property (assign) CGRect screenBounds;

@end

@implementation MGLMapViewContentInsetTests

- (void)setUp {
    [super setUp];
    
    [MGLAccountManager setAccessToken:@"pk.feedcafedeadbeefbadebede"];
    NSURL *styleURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"one-liner" withExtension:@"json"];
    self.screenBounds = UIScreen.mainScreen.bounds;
    self.mapView = [[MGLMapView alloc] initWithFrame:self.screenBounds styleURL:styleURL];
    self.mapView.zoomLevel = 16;
    self.mapView.delegate = self;
    
    self.viewController = [[UIViewController alloc] init];
    self.viewController.view = [[UIView alloc] initWithFrame:self.screenBounds];
    [self.viewController.view addSubview:self.mapView];
    self.window = [[UIWindow alloc] initWithFrame:self.screenBounds];
    [self.window addSubview:self.viewController.view];
    [self.window makeKeyAndVisible];
    
    if (!self.mapView.style) {
        _styleLoadingExpectation = [self expectationWithDescription:@"Map view should finish loading style."];
        [self waitForExpectationsWithTimeout:10 handler:nil];
    }
}

- (void)mapView:(MGLMapView *)mapView didFinishLoadingStyle:(MGLStyle *)style {
    XCTAssertNotNil(mapView.style);
    XCTAssertEqual(mapView.style, style);
    
    [_styleLoadingExpectation fulfill];
}

- (void)tearDown {
    self.mapView = nil;
    [MGLAccountManager setAccessToken:nil];
    [super tearDown];
}

- (void)testContentInsetCenter {
    CLLocationCoordinate2D center = CLLocationCoordinate2DMake(1.0, 5.0);
    self.mapView.centerCoordinate = center;
    XCTAssertEqualWithAccuracy(self.mapView.centerCoordinate.latitude, center.latitude, 0.01);
    XCTAssertEqualWithAccuracy(self.mapView.centerCoordinate.longitude, center.longitude, 0.01);
    
    CGPoint centerPoint = [self.mapView convertCoordinate:center toPointToView:self.mapView];

    XCTAssertEqualWithAccuracy(centerPoint.x, self.screenBounds.size.width/2, 0.01);
    XCTAssertEqualWithAccuracy(centerPoint.y, self.screenBounds.size.height/2, 0.01);
    
    // shifting contentInset should keep the same centerCoordinate but shift the screen
    // center point accordingly
    UIEdgeInsets contentInset = UIEdgeInsetsMake(50.0, 10.0, 10.0, 30.0);
    self.mapView.contentInset = contentInset;
    XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(self.mapView.contentInset, contentInset));
    XCTAssertEqualWithAccuracy(self.mapView.centerCoordinate.latitude, center.latitude, 0.01);
    XCTAssertEqualWithAccuracy(self.mapView.centerCoordinate.longitude, center.longitude, 0.01);
    CGPoint shiftedPoint = [self.mapView convertCoordinate:center toPointToView:self.mapView];
    CGPoint expectedShiftedPoint = CGPointMake((self.screenBounds.size.width/2) + ((contentInset.left - contentInset.right) / 2 ),
                                               (self.screenBounds.size.height/2) + ((contentInset.top - contentInset.bottom) / 2));
    XCTAssertEqualWithAccuracy(shiftedPoint.x, expectedShiftedPoint.x, 0.01);
    XCTAssertEqualWithAccuracy(shiftedPoint.y, expectedShiftedPoint.y, 0.01);
    
  
}

- (void)testContentInsetOrnaments {
    CGFloat margin = 8;
    self.mapView.contentInset = UIEdgeInsetsZero;
    UIView *scaleBar = self.mapView.scaleBar;
    CGPoint expectedScaleBarOrigin = CGPointMake(margin, margin);
    XCTAssertTrue(CGPointEqualToPoint(scaleBar.frame.origin, expectedScaleBarOrigin));
    
    UIView *compassView = self.mapView.compassView;
    CGFloat x = self.screenBounds.size.width - compassView.bounds.size.width - margin;
    CGPoint expectedCompassOrigin = CGPointMake(x, margin);
    XCTAssertTrue(CGPointEqualToPoint(compassView.frame.origin, expectedCompassOrigin));
                  
    UIView *logoView = self.mapView.logoView;
    CGFloat y = self.screenBounds.size.height - logoView.bounds.size.height - margin;
    CGPoint expectedLogoOrigin = CGPointMake(margin, y);
    XCTAssertTrue(CGPointEqualToPoint(logoView.frame.origin, expectedLogoOrigin));
    
    UIView *attributionView = self.mapView.attributionButton;
    x = self.screenBounds.size.width - attributionView.bounds.size.width - margin;
    y = self.screenBounds.size.height - attributionView.bounds.size.height - margin;
    CGPoint expectedAttributionOrigin = CGPointMake(x, y);
    XCTAssertTrue(CGPointEqualToPoint(attributionView.frame.origin, expectedAttributionOrigin));
    
    UIEdgeInsets insets = UIEdgeInsetsMake(15, 10, 20, 5);
    self.viewController.automaticallyAdjustsScrollViewInsets = NO;
    self.mapView.contentInset = insets;
    
    [self.mapView setNeedsLayout];
    [self.mapView layoutIfNeeded];
    
    expectedScaleBarOrigin = CGPointMake(insets.left + self.mapView.scaleBarMargins.x, insets.top  + self.mapView.scaleBarMargins.y);
    XCTAssertTrue(CGPointEqualToPoint(scaleBar.frame.origin, expectedScaleBarOrigin));
    
    x = self.screenBounds.size.width - compassView.bounds.size.width - insets.right - self.mapView.compassViewMargins.x;
    expectedCompassOrigin = CGPointMake(x, insets.top + self.mapView.compassViewMargins.y);
    XCTAssertTrue(CGPointEqualToPoint(compassView.frame.origin, expectedCompassOrigin));
    
    y = self.screenBounds.size.height - logoView.bounds.size.height - insets.bottom - self.mapView.logoViewMargins.y;
    expectedLogoOrigin = CGPointMake(insets.left + self.mapView.logoViewMargins.x, y);
    XCTAssertTrue(CGPointEqualToPoint(logoView.frame.origin, expectedLogoOrigin));
    
    x = self.screenBounds.size.width - attributionView.bounds.size.width - insets.right - self.mapView.attributionButtonMargins.x;
    y = self.screenBounds.size.height - attributionView.bounds.size.height - insets.bottom - self.mapView.attributionButtonMargins.y;
    expectedAttributionOrigin = CGPointMake(x, y);
    XCTAssertTrue(CGPointEqualToPoint(attributionView.frame.origin, expectedAttributionOrigin));
    
    // tests that passing negative values result in a 0 inset value
    insets = UIEdgeInsetsMake(-100, -100, -100, -100);
    self.mapView.contentInset = insets;
    
    [self.mapView setNeedsLayout];
    [self.mapView layoutIfNeeded];
    
    expectedScaleBarOrigin = CGPointMake(margin, margin);
    XCTAssertTrue(CGPointEqualToPoint(scaleBar.frame.origin, expectedScaleBarOrigin));
    
    x = self.screenBounds.size.width - compassView.bounds.size.width - margin;
    expectedCompassOrigin = CGPointMake(x, margin);
    XCTAssertTrue(CGPointEqualToPoint(compassView.frame.origin, expectedCompassOrigin));
    
    y = self.screenBounds.size.height - logoView.bounds.size.height - margin;
    expectedLogoOrigin = CGPointMake(margin, y);
    XCTAssertTrue(CGPointEqualToPoint(logoView.frame.origin, expectedLogoOrigin));
    
    x = self.screenBounds.size.width - attributionView.bounds.size.width - margin;
    y = self.screenBounds.size.height - attributionView.bounds.size.height - margin;
    expectedAttributionOrigin = CGPointMake(x, y);
    XCTAssertTrue(CGPointEqualToPoint(attributionView.frame.origin, expectedAttributionOrigin));
    
    self.mapView.automaticallyAdjustsContentInset = YES;
    insets = UIEdgeInsetsMake(100, 100, 100, 100);
    self.mapView.contentInset = insets;
    XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(self.mapView.contentInset, insets));
    
    [self.mapView setNeedsLayout];
    [self.mapView layoutIfNeeded];
    
    // when automaticallyAdjustsContentInset = YES the content insets should be overwriten
    XCTAssertFalse(UIEdgeInsetsEqualToEdgeInsets(self.mapView.contentInset, insets));
    
    expectedScaleBarOrigin = CGPointMake(margin, margin);
    XCTAssertTrue(CGPointEqualToPoint(scaleBar.frame.origin, expectedScaleBarOrigin));
    
    x = self.screenBounds.size.width - compassView.bounds.size.width - margin;
    expectedCompassOrigin = CGPointMake(x, margin);
    XCTAssertTrue(CGPointEqualToPoint(compassView.frame.origin, expectedCompassOrigin));
    
    y = self.screenBounds.size.height - logoView.bounds.size.height - margin;
    expectedLogoOrigin = CGPointMake(margin, y);
    XCTAssertTrue(CGPointEqualToPoint(logoView.frame.origin, expectedLogoOrigin));
    
    x = self.screenBounds.size.width - attributionView.bounds.size.width - margin;
    y = self.screenBounds.size.height - attributionView.bounds.size.height - margin;
    expectedAttributionOrigin = CGPointMake(x, y);
    XCTAssertTrue(CGPointEqualToPoint(attributionView.frame.origin, expectedAttributionOrigin));
    
}

@end