Files
MapLibreParticleLab/platform/ios/Integration Tests/Annotation Tests/MGLAnnotationViewIntegrationTests.m
Nadia Barbosa 7b243392e3 [ios] Add delegate method to specify the user location annotation’s position
Update method name

More API drafting

Add deprecation flag

Add Swift delegate integration test

Update method name and documentation

Update deprecation notices

Update method name

Offset anchor point relative to contentFrame

Update docs

Only run through switch statement if delegate is unimplemented

Account for content inset + refactor logic

Adjust edgePaddingForFollowing

Fix Swift delegate integration test

Set up integration test

Set up test location manager

.

Remove unused file reference from test

Return CGPoint value from delegate method within integration test setup

Test anchor points

Make updateUserLocationAnnotationView public

Refactor test

Update test location manager

Changelog entry

Doc fixes
2018-10-02 18:28:53 -04:00

135 lines
5.4 KiB
Objective-C

#import "MGLMapViewIntegrationTest.h"
#import "MGLTestUtility.h"
#import "MGLMapAccessibilityElement.h"
#import "MGLTestLocationManager.h"
@interface MGLMapView (Tests)
- (MGLAnnotationTag)annotationTagAtPoint:(CGPoint)point persistingResults:(BOOL)persist;
@end
@interface MGLAnnotationViewIntegrationTests : MGLMapViewIntegrationTest
@end
@implementation MGLAnnotationViewIntegrationTests
- (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";
CGFloat epsilon = 0.0000001;
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, epsilon);
XCTAssertEqualWithAccuracy(annotationPoint.y, size.height/2.0, epsilon);
// 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);
}
- (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];
}
@end