summaryrefslogtreecommitdiff
path: root/platform/ios/Integration Tests/Annotation Tests/MGLAnnotationViewIntegrationTests.m
blob: 777afe338030d94e8901049e4919cd6f5d4fc114 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
#import "MGLMapViewIntegrationTest.h"
#import "MGLTestUtility.h"
#import "MGLMapAccessibilityElement.h"
#import "MGLTestLocationManager.h"
#import "MGLCompactCalloutView.h"

@interface MGLTestCalloutView : MGLCompactCalloutView
@property (nonatomic) BOOL implementsMarginHints;
@end

@implementation MGLTestCalloutView
- (BOOL)respondsToSelector:(SEL)aSelector {
    if (!self.implementsMarginHints &&
        (aSelector == @selector(marginInsetsHintForPresentationFromRect:))) {
        return NO;
    }
    return [super respondsToSelector:aSelector];
}
@end


@interface MGLMapView (Tests)
- (MGLAnnotationTag)annotationTagAtPoint:(CGPoint)point persistingResults:(BOOL)persist;
@property (nonatomic) UIView<MGLCalloutView> *calloutViewForSelectedAnnotation;
@end

@interface MGLAnnotationViewIntegrationTests : MGLMapViewIntegrationTest
@end

@implementation MGLAnnotationViewIntegrationTests

#pragma mark - Offscreen/panning selection tests

typedef struct PanTestData {
    CGPoint relativeCoord;
    BOOL showsCallout;
    BOOL implementsMargins;
    BOOL moveIntoView;
    BOOL expectMapToHavePanned;
    BOOL calloutOnScreen;
} PanTestData;

#define PAN_TEST_TERMINATOR {{FLT_MAX, FLT_MAX}, NO, NO, NO, NO, NO}
static const CGPoint kAnnotationRelativeScale = { 0.05f, 0.125f };

- (void)internalTestOffscreenSelectionTitle:(NSString*)title withTestData:(PanTestData)test animateSelection:(BOOL)animateSelection {
    
    CGPoint relativeCoordinate          = test.relativeCoord;
    BOOL showsCallout                   = test.showsCallout;
    BOOL calloutImplementsMarginHints   = test.implementsMargins;
    BOOL moveIntoView                   = test.moveIntoView;
    BOOL expectMapToHavePanned          = test.expectMapToHavePanned;
    BOOL expectCalloutToBeFullyOnscreen = test.calloutOnScreen;
    
    // Reset the map to a consistent state - want the map to be zoomed in, so that
    // it's free to be panned without hitting boundaries.
    [self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(0, 0) zoomLevel:14 animated:NO];
    [self waitForMapViewToBeRenderedWithTimeout:1.0];
    
    XCTAssert(self.mapView.annotations.count == 0);
    
    NSString * const MGLTestAnnotationReuseIdentifer = @"MGLTestAnnotationReuseIdentifer";
    CGSize size = self.mapView.bounds.size;
    CGSize annotationSize = CGSizeMake(floor(size.width*kAnnotationRelativeScale.x), floor(size.height*kAnnotationRelativeScale.y));
    
    self.viewForAnnotation = ^MGLAnnotationView*(MGLMapView *view, id<MGLAnnotation> annotation) {
        
        if (![annotation isKindOfClass:[MGLPointAnnotation class]]) {
            return nil;
        }
        
        // No dequeue
        MGLAnnotationView *annotationView = [[MGLAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:MGLTestAnnotationReuseIdentifer];
        annotationView.bounds             = (CGRect){ .origin = CGPointZero, .size = annotationSize };
        annotationView.backgroundColor    = UIColor.redColor;
        annotationView.enabled            = YES;
        
        return annotationView;
    };
    
    // Coordinate for annotation screen coordinate
    CGPoint annotationPoint = CGPointMake(floor(relativeCoordinate.x * size.width), floor(relativeCoordinate.y * size.height)   );
    CLLocationCoordinate2D coordinate = [self.mapView convertPoint:annotationPoint toCoordinateFromView:self.mapView];
    
    MGLPointAnnotation *point = [[MGLPointAnnotation alloc] init];
    point.title = title;
    point.coordinate = coordinate;
    
    self.mapViewAnnotationCanShowCalloutForAnnotation = ^BOOL(MGLMapView *mapView, id<MGLAnnotation> annotation) {
        return showsCallout;
    };
    
    self.mapViewCalloutViewForAnnotation = ^id<MGLCalloutView>(MGLMapView *mapView, id<MGLAnnotation> annotation) {
        if (!showsCallout)
            return nil;
        
        MGLTestCalloutView *calloutView = [[MGLTestCalloutView alloc] init];
        calloutView.representedObject = annotation;
        calloutView.implementsMarginHints = calloutImplementsMarginHints;
        return calloutView;
    };
    
    [self.mapView addAnnotation:point];

    // Check assumptions before selection
    UIView *annotationViewBeforeSelection =  [self.mapView viewForAnnotation:point];
    XCTAssertNotNil(annotationViewBeforeSelection);

    CLLocationCoordinate2D mapCenterBeforeSelection = self.mapView.centerCoordinate;
    XCTAssert(CLLocationCoordinate2DIsValid(mapCenterBeforeSelection));

    // Also note, that at this point, the internal mechanism that determines if
    // an annotation view is offscreen and should be put back in the reuse queue
    // will have run, and `viewForAnnotation` may return nil
    
    [self.mapView selectAnnotation:point moveIntoView:moveIntoView animateSelection:animateSelection];

    // Animated selection takes MGLAnimationDuration (0.3 seconds), so wait a little
    // longer. We don't need to wait as long if we're not animated (but we do
    // want the runloop to tick over)
    [self waitFor:animateSelection ? 0.4: 0.05];

    UIView *annotationViewAfterSelection =  [self.mapView viewForAnnotation:point];
    CLLocationCoordinate2D mapCenterAfterSelection = self.mapView.centerCoordinate;
    XCTAssert(CLLocationCoordinate2DIsValid(mapCenterAfterSelection));

    // If the annotation is still "offscreen" at this point, then the above annotation view
    // may be nil, which is expected.
    BOOL (^CGRectContainsRectWithAccuracy)(CGRect, CGRect, CGFloat) = ^(CGRect rect1, CGRect rect2, CGFloat accuracy) {
        CGRect expandedRect1 = CGRectInset(rect1, -accuracy, -accuracy);
        return (BOOL)CGRectContainsRect(expandedRect1, rect2);
    };
    
    CGFloat epsilon = 0.00001;
    if (expectMapToHavePanned) {
        CLLocationDegrees latitudeDelta = fabs(mapCenterAfterSelection.latitude - mapCenterBeforeSelection.latitude);
        CLLocationDegrees longitudeDelta = fabs(mapCenterAfterSelection.longitude - mapCenterBeforeSelection.longitude);
        
        XCTAssert( (latitudeDelta > epsilon) || (longitudeDelta > epsilon), @"Deltas: lat=%f, long=%f", latitudeDelta, longitudeDelta); // One of them should have moved

        // If the map panned - the intention is that the annotation is on-screen,
        // and so should have an annotation view and that it is fully on screen
        CGRect annotationFrameAfterSelection = annotationViewAfterSelection.frame;

        XCTAssertNotNil(annotationViewAfterSelection);
        
        XCTAssert(CGRectContainsRectWithAccuracy(self.mapView.bounds, annotationFrameAfterSelection, 0.25), @"Mapview:%@ frame:%@", NSStringFromCGRect(self.mapView.bounds), NSStringFromCGRect(annotationFrameAfterSelection));
        
        // Check the callout
        if (showsCallout) {
            UIView *calloutView = self.mapView.calloutViewForSelectedAnnotation;
            XCTAssertNotNil(calloutView);
            
            // This can fail if the callout view's width is < the annotations. This is really a warning, so
            // if you need this NOT to fail the tests, consider replacing with MGLTestWarning
            XCTAssert(expectCalloutToBeFullyOnscreen == CGRectContainsRectWithAccuracy(self.mapView.bounds, calloutView.frame, 0.25),
                      @"Expect contains:%d, Mapview:%@ annotation:%@ callout:%@",
                      expectCalloutToBeFullyOnscreen,
                      NSStringFromCGRect(self.mapView.bounds),
                      NSStringFromCGRect(annotationFrameAfterSelection),
                      NSStringFromCGRect(calloutView.frame));
        }
    }
    else {
        // The map shouldn't have moved, so use equality (rather than an error check)
        XCTAssertEqual(mapCenterBeforeSelection.latitude, mapCenterAfterSelection.latitude);
        XCTAssertEqual(mapCenterBeforeSelection.longitude, mapCenterAfterSelection.longitude);
        
        // Annotation shouldn't have moved
        CGPoint annotationPoint2 = [self.mapView convertCoordinate:point.coordinate toPointToView:self.mapView];
        CGFloat xDelta = fabs(annotationPoint2.x - annotationPoint.x);
        CGFloat yDelta = fabs(annotationPoint2.y - annotationPoint.y);
        
        XCTAssert((xDelta < epsilon) && (yDelta < epsilon));
        
        if (showsCallout) {
            UIView *calloutView = self.mapView.calloutViewForSelectedAnnotation;
            
            if (annotationViewAfterSelection) {
                XCTAssertNotNil(calloutView);

                // If kAnnotationScale == 0.25, then the following assert can fail.
                // This is really a warning (see https://github.com/mapbox/mapbox-gl-native/issues/13744 )
                // If you need this NOT to fail the tests, consider replacing with MGLTestWarning
                XCTAssert((expectCalloutToBeFullyOnscreen == CGRectContainsRectWithAccuracy(self.mapView.bounds, calloutView.frame, 0.25)),
                          @"Mapview:%@ annotation:%@ callout:%@",
                          NSStringFromCGRect(self.mapView.bounds),
                          NSStringFromCGRect(annotationViewAfterSelection.frame),
                          NSStringFromCGRect(calloutView.frame));
            }
            else {
                // If there's no annotation view, should we expect a callout?
                XCTAssertNil(calloutView);
                XCTAssertFalse(expectCalloutToBeFullyOnscreen);
            }
        }
    }
   
    // Remove the annotation
    [self.mapView removeAnnotation:point];
    
    XCTAssert(self.mapView.annotations.count == 0);
}

// See https://github.com/mapbox/mapbox-gl-native/pull/13727#issuecomment-454028698
// What follows are tests based on this table.
// This is not a full-set of possible combinations, just the most important/likely
// ones
- (void)internalRunTests:(PanTestData*)testData
{
    // Test both animated and not-animated.
    for (int i = 0; i<2; i++) {
        int row = 0;
        PanTestData *test = testData;
        while (test->relativeCoord.x != FLT_MAX) {
            NSString *activityTitle = [NSString stringWithFormat:@"Row %d/%d", row, i];
            [XCTContext runActivityNamed:activityTitle
                                   block:^(id<XCTActivity>  _Nonnull activity) {
                                       [self internalTestOffscreenSelectionTitle:activityTitle
                                                                    withTestData:*test
                                                                animateSelection:!i];
                                   }];
            ++test;
            ++row;
        }
    }
}

- (void)testBasicSelection  {
    // Tests moveIntoView:NO
    // WITHOUT a callout
    
    PanTestData tests[] = {
        //  Coord           showsCallout    impl margins?   moveIntoView    expectMapToPan  calloutOnScreen
        //  Offscreen
        {   {-1.0f, 0.5f},  NO,             NO,             NO,             NO,             NO },
        {   { 2.0f, 0.5f},  NO,             NO,             NO,             NO,             NO },
        {   { 0.5f,-1.0f},  NO,             NO,             NO,             NO,             NO },
        {   { 0.5f, 2.0f},  NO,             NO,             NO,             NO,             NO },
        
        //  Partial
        {   { 0.0f, 0.5f},  NO,             NO,             NO,             NO,             NO },
        {   { 1.0f, 0.5f},  NO,             NO,             NO,             NO,             NO },
        {   { 0.5f, 0.0f},  NO,             NO,             NO,             NO,             NO },
        {   { 0.5f, 1.0f},  NO,             NO,             NO,             NO,             NO },
        
        //  Onscreen
        {   { 0.5f, 0.5f},  NO,             NO,             NO,             NO,             NO },
        
        PAN_TEST_TERMINATOR
    };
    
    [self internalRunTests:tests];
}

- (void)testBasicSelectionWithCallout  {
    // Tests moveIntoView:NO
    // WITH the default callout (implements marginshint)
    
    PanTestData tests[] = {
        //  Coord           showsCallout    impl margins?   moveIntoView    expectMapToPan  calloutOnScreen
        {   {-1.0f, 0.5f},  YES,            YES,            NO,             NO,             NO },
        {   { 0.0f, 0.5f},  YES,            YES,            NO,             NO,             NO },
        {   { 0.5f, 1.0f},  YES,            YES,            NO,             NO,             YES }, // Because annotation was off the bottom of screen, and callout is above annotation
        {   { 0.5f, 0.5f},  YES,            YES,            NO,             NO,             YES },
        
        PAN_TEST_TERMINATOR
    };
    
    [self internalRunTests:tests];
}

- (void)testSelectionMoveIntoView  {
    // Tests moveIntoView:YES
    // without a callout
    
    // From https://github.com/mapbox/mapbox-gl-native/pull/13727#issuecomment-454028698
    //
    // | Annotation position | Has callout? | Callout implements `marginInsets...`? | Map pans when selected with moveIntoView=YES? |
    // |---------------------|--------------|---------------------------------------|-----------------------------------------------|
    // | Offscreen           | No           | n/a                                   | Yes (no margins)                              |
    // | Partially           | No           | n/a                                   | No                                            |
    // | Onscreen            | No           | n/a                                   | No                                            |
    //
    
    PanTestData tests[] = {
        //  Coord           showsCallout    impl margins?   moveIntoView     expectMapToPan  calloutOnScreen
        //  Offscreen
        {   {-1.0f, 0.5f},  NO,             NO,             YES,            YES,            NO },
        {   { 2.0f, 0.5f},  NO,             NO,             YES,            YES,            NO },
        {   { 0.5f,-1.0f},  NO,             NO,             YES,            YES,            NO },
        {   { 0.5f, 2.0f},  NO,             NO,             YES,            YES,            NO },
        
        //  Partial
        {   { 0.0f, 0.5f},  NO,             NO,             YES,            NO,             NO },
        {   { 1.0f, 0.5f},  NO,             NO,             YES,            NO,             NO },
        {   { 0.5f, 0.0f},  NO,             NO,             YES,            NO,             NO },
        {   { 0.5f, 1.0f},  NO,             NO,             YES,            NO,             NO },
        
        //  Onscreen
        {   { 0.5f, 0.5f},  NO,             NO,             YES,            NO,             NO },
        
        PAN_TEST_TERMINATOR
    };
    
    [self internalRunTests:tests];
}

- (void)testSelectionMoveIntoViewWithCallout  {
    // Tests moveIntoView:YES
    // WITH the default callout (implements marginshint)
    
    // From https://github.com/mapbox/mapbox-gl-native/pull/13727#issuecomment-454028698
    //
    // | Annotation position | Has callout? | Callout implements `marginInsets...`? | Map pans when selected with moveIntoView=YES? |
    // |---------------------|--------------|---------------------------------------|-----------------------------------------------|
    // | Offscreen           | Yes          | Yes                                   | Yes to ensure callout is fully visible        |
    // | Partially           | Yes          | Yes                                   | Yes to ensure callout is fully visible        |
    // | Onscreen            | Yes          | Yes                                   | Yes, but *only* to ensure callout is fully visible |
    //

    CGFloat offset = kAnnotationRelativeScale.x * 0.5f;
    
    PanTestData tests[] = {
        //  Coord           showsCallout    impl margins?   moveIntoView    expectMapToPan  calloutOnScreen
        //  Offscreen
        {   {-1.0f, 0.5f},  YES,            YES,            YES,            YES,            YES },
        {   { 2.0f, 0.5f},  YES,            YES,            YES,            YES,            YES },
        {   { 0.5f,-1.0f},  YES,            YES,            YES,            YES,            YES },
        {   { 0.5f, 2.0f},  YES,            YES,            YES,            YES,            YES },
        
        //  Partial
        {   { 0.0f, 0.5f},  YES,            YES,            YES,            YES,            YES },
        {   { 1.0f, 0.5f},  YES,            YES,            YES,            YES,            YES },
        {   { 0.5f, 0.0f},  YES,            YES,            YES,            YES,            YES },
        {   { 0.5f, 1.0f},  YES,            YES,            YES,            YES,            YES },
        
        //  Onscreen
        {   { 0.5f, 0.5f},  YES,            YES,            YES,            NO,             YES },
        
        //  Just at the edge of the screen.
        //  Expects to move, because although onscreen, callout would not be.
        //  However, if the scale is 0.25, then expectToPan should be NO, because
        //  of the width of the annotation

        //  Coord                   showsCallout  impl margins?   moveIntoView    expectMapToPan    calloutOnScreen
        {   {offset, 0.5f},         YES,          YES,            YES,            YES,              YES },
        {   {1.0 - offset, 0.5f},   YES,          YES,            YES,            YES,              YES },

        PAN_TEST_TERMINATOR
    };
    
    [self internalRunTests:tests];
}

- (void)testSelectionMoveIntoViewWithBasicCallout  {
    // Tests moveIntoView:YES
    // WITH a callout that DOES NOT implement marginshint
    
    // From https://github.com/mapbox/mapbox-gl-native/pull/13727#issuecomment-454028698
    //
    // | Annotation position | Has callout? | Callout implements `marginInsets...`? | Map pans when selected with moveIntoView=YES? |
    // |---------------------|--------------|---------------------------------------|-----------------------------------------------|
    // | Offscreen           | Yes          | No                                    | Yes, but only to show annotation (not callout) with no margins |
    // | Partially           | Yes          | No                                    | No                                            |
    // | Onscreen            | Yes          | No                                    | No                                            |
    //

    PanTestData tests[] = {
        //  Coord           showsCallout    impl margins?   moveIntoView    expectMapToPan  calloutOnScreen
        //  Offscreen
        {   {-1.0f, 0.5f},  YES,            NO,             YES,            YES,            NO },
        {   { 2.0f, 0.5f},  YES,            NO,             YES,            YES,            NO },
        {   { 0.5f,-1.0f},  YES,            NO,             YES,            YES,            NO },
        {   { 0.5f, 2.0f},  YES,            NO,             YES,            YES,            YES },   // Because annotation was off the bottom of screen, and callout is above annotation
        {   { 2.0f, 2.0f},  YES,            NO,             YES,            YES,            NO },

        //  Partial
        {   { 0.0f, 0.5f},  YES,            NO,             YES,            NO,             NO },
        {   { 1.0f, 0.5f},  YES,            NO,             YES,            NO,             NO },
        {   { 0.5f, 0.0f},  YES,            NO,             YES,            NO,             NO },
        {   { 0.5f, 1.0f},  YES,            NO,             YES,            NO,             YES }, // Because annotation was off the bottom of screen, and callout is above annotation
        {   { 1.0f, 1.0f},  YES,            NO,             YES,            NO,             NO },

        //  Onscreen
        {   { 0.5f, 0.5f},  YES,            NO,             YES,            NO,             YES },

        PAN_TEST_TERMINATOR
    };
    
    [self internalRunTests:tests];
}

#pragma mark - Selection with an offset

- (void)testSelectingAnnotationWithCenterOffset {

    for (CGFloat dx = -100.0; dx <= 100.0; dx += 100.0 ) {
        for (CGFloat dy = -100.0; dy <= 100.0; dy += 100.0 ) {
            CGVector offset = CGVectorMake(dx, dy);
            [self internalTestSelectingAnnotationWithCenterOffsetWithOffset:offset];
        }
    }
}

- (void)internalTestSelectingAnnotationWithCenterOffsetWithOffset:(CGVector)offset {

    NSString * const MGLTestAnnotationReuseIdentifer = @"MGLTestAnnotationReuseIdentifer";

    CGSize size = self.mapView.bounds.size;

    CGSize annotationSize = CGSizeMake(40.0, 40.0);

    self.viewForAnnotation = ^MGLAnnotationView*(MGLMapView *view, id<MGLAnnotation> annotation) {

        if (![annotation isKindOfClass:[MGLPointAnnotation class]]) {
            return nil;
        }

        // No dequeue
        MGLAnnotationView *annotationView = [[MGLAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:MGLTestAnnotationReuseIdentifer];
        annotationView.bounds             = (CGRect){ .origin = CGPointZero, .size = annotationSize };
        annotationView.backgroundColor    = UIColor.redColor;
        annotationView.enabled            = YES;
        annotationView.centerOffset       = offset;

        return annotationView;
    };

    MGLPointAnnotation *point = [[MGLPointAnnotation alloc] init];
    point.title = NSStringFromSelector(_cmd);
    point.coordinate = CLLocationCoordinate2DMake(0.0, 0.0);
    [self.mapView addAnnotation:point];

    // From https://github.com/mapbox/mapbox-gl-native/issues/12259#issuecomment-401414168
    //
    //      queryRenderedFeatures depends on collision detection having been run
    //      before it shows results [...]. Collision detection runs asynchronously
    //      (at least every 300ms, sometimes more often), and therefore the results
    //      of queryRenderedFeatures are similarly asynchronous.
    //
    // So, we need to wait before `annotationTagAtPoint:persistingResults:` will
    // return out newly added annotation

    [self waitForCollisionDetectionToRun];

    // Check that the annotation is in the center of the view
    CGPoint annotationPoint = [self.mapView convertCoordinate:point.coordinate toPointToView:self.mapView];
    XCTAssertEqualWithAccuracy(annotationPoint.x, size.width/2.0, 0.25);
    XCTAssertEqualWithAccuracy(annotationPoint.y, size.height/2.0, 0.25);

    // Now test taps around the annotation
    CGPoint tapPoint = CGPointMake(annotationPoint.x + offset.dx, annotationPoint.y + offset.dy);

    MGLAnnotationTag tagAtPoint = [self.mapView annotationTagAtPoint:tapPoint persistingResults:YES];
    XCTAssert(tagAtPoint != UINT32_MAX, @"Should have tapped on annotation");

    CGPoint testPoints[] = {
        { tapPoint.x - annotationSize.width, tapPoint.y },
        { tapPoint.x + annotationSize.width, tapPoint.y },
        { tapPoint.x, tapPoint.y - annotationSize.height },
        { tapPoint.x, tapPoint.y + annotationSize.height },
        CGPointZero
    };

    CGPoint *testPoint = testPoints;

    while (!CGPointEqualToPoint(*testPoint, CGPointZero)) {
        tagAtPoint = [self.mapView annotationTagAtPoint:*testPoints persistingResults:YES];
        XCTAssert(tagAtPoint == UINT32_MAX, @"Tap should to the side of the annotation");
        testPoint++;
    }
}

- (void)testUserLocationWithOffsetAnchorPoint {
    [self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(37.787357, -122.39899)];
    MGLTestLocationManager *locationManager = [[MGLTestLocationManager alloc] init];
    self.mapView.locationManager = locationManager;

    [self.mapView setUserTrackingMode:MGLUserTrackingModeFollow animated:NO];
    CGRect originalFrame = [self.mapView viewForAnnotation:self.mapView.userLocation].frame;
    
    // Temporarily disable location tracking so we can save the value of
    // the originalFrame in memory
    [self.mapView setUserTrackingMode:MGLUserTrackingModeNone animated:NO];
    
    CGPoint offset = CGPointMake(20, 20);
    
    self.mapViewUserLocationAnchorPoint = ^CGPoint (MGLMapView *mapView) {
        return offset;;
    };
    
    [self.mapView setUserTrackingMode:MGLUserTrackingModeFollow animated:NO];
    CGRect offsetFrame = [self.mapView viewForAnnotation:self.mapView.userLocation].frame;
    
    XCTAssertEqual(originalFrame.origin.x + offset.x, offsetFrame.origin.x);
    XCTAssertEqual(originalFrame.origin.y + offset.y, offsetFrame.origin.y);
}

#pragma mark - Utilities

- (void)runRunLoop {
    [[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}

- (void)waitFor:(NSTimeInterval)seconds {
    XCTestExpectation *timerExpired = [self expectationWithDescription:@"Timer expires"];
    
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.1
                                                      target:self
                                                    selector:@selector(runRunLoop)
                                                    userInfo:nil
                                                     repeats:YES];

    double duration = seconds * (double)NSEC_PER_SEC;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)duration), dispatch_get_main_queue(), ^{
        [timerExpired fulfill];
    });
    
    [self waitForExpectations:@[timerExpired] timeout:seconds + 1.0];
    [timer invalidate];
}

- (void)waitForCollisionDetectionToRun {
    XCTAssertNil(self.renderFinishedExpectation, @"Incorrect test setup");

    self.renderFinishedExpectation = [self expectationWithDescription:@"Map view should be rendered"];
    XCTestExpectation *timerExpired = [self expectationWithDescription:@"Timer expires"];

    // Wait 1/2 second
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(NSEC_PER_SEC >> 1)), dispatch_get_main_queue(), ^{
        [timerExpired fulfill];
    });

    [self waitForExpectations:@[timerExpired, self.renderFinishedExpectation] timeout:5];
    
    self.renderFinishedExpectation = nil;
}

@end