summaryrefslogtreecommitdiff
path: root/platform/macos/test/MGLAnnotationTests.m
blob: 36a7aef9f038988d67842afb5ef13791dbd6c9c1 (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
#import <Mapbox/Mapbox.h>
#import <XCTest/XCTest.h>

@interface MGLAnnotationTests : XCTestCase <MGLMapViewDelegate>
@property (nonatomic) MGLMapView *mapView;
@property (nonatomic) BOOL centerCoordinateDidChange;
@end

@implementation MGLAnnotationTests

- (void)setUp
{
    [super setUp];
    _mapView = [[MGLMapView alloc] initWithFrame:CGRectMake(0, 0, 64, 64)];
    _mapView.delegate = self;
}

- (void)testSelectingOnscreenAnnotationThatHasNotBeenAdded {
    // See https://github.com/mapbox/mapbox-gl-native/issues/11476

    // This bug occurs under the following conditions:
    //
    // - There are content insets (e.g. navigation bar) for the compare against
    //      NSZeroRect

    self.mapView.contentInsets = NSEdgeInsetsMake(10.0, 10.0, 10.0, 10.0);

    // Create annotation
    MGLPointFeature *point = [[MGLPointFeature alloc] init];
    point.title = NSStringFromSelector(_cmd);
    point.coordinate = CLLocationCoordinate2DMake(0.0, 0.0);

    MGLCoordinateBounds coordinateBounds = [self.mapView convertRect:self.mapView.bounds toCoordinateBoundsFromView:self.mapView];
    XCTAssert(MGLCoordinateInCoordinateBounds(point.coordinate, coordinateBounds), @"The test point should be within the visible map view");

    [self.mapView addObserver:self forKeyPath:@"centerCoordinate" options:NSKeyValueObservingOptionNew context:_cmd];
    XCTAssertFalse(self.centerCoordinateDidChange, @"Center coordinate should not have changed at this point");

    // Select on screen annotation (DO NOT ADD FIRST).
    [self.mapView selectAnnotation:point];

    XCTAssertFalse(self.centerCoordinateDidChange, @"Center coordinate should not have changed after selecting a visible annotation");
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if ((context == @selector(testSelectingOnscreenAnnotationThatHasNotBeenAdded)) &&
        (object == self.mapView)) {
        self.centerCoordinateDidChange = YES;
    }
}

@end