[ios, macos] handle duplicate layer error

This commit is contained in:
Ivo van Dongen
2016-12-06 11:48:36 +01:00
committed by Jesse Bounds
parent bb6973ae90
commit d3895b47ef
2 changed files with 49 additions and 7 deletions

View File

@@ -287,10 +287,18 @@ static NSURL *MGLStyleURL_emerald;
[NSException raise:NSRangeException
format:@"Cannot insert style layer at out-of-bounds index %lu.", (unsigned long)index];
} else if (index == 0) {
[styleLayer addToMapView:self.mapView belowLayer:nil];
try {
[styleLayer addToMapView:self.mapView belowLayer:nil];
} catch (const std::runtime_error & err) {
[NSException raise:@"MGLRedundantLayerIdentifierException" format:@"%s", err.what()];
}
} else {
MGLStyleLayer *sibling = [self layerFromMBGLLayer:layers.at(layers.size() - index)];
[styleLayer addToMapView:self.mapView belowLayer:sibling];
try {
MGLStyleLayer *sibling = [self layerFromMBGLLayer:layers.at(layers.size() - index)];
[styleLayer addToMapView:self.mapView belowLayer:sibling];
} catch (std::runtime_error & err) {
[NSException raise:@"MGLRedundantLayerIdentifierException" format:@"%s", err.what()];
}
}
}
@@ -374,7 +382,11 @@ static NSURL *MGLStyleURL_emerald;
layer];
}
[self willChangeValueForKey:@"layers"];
[layer addToMapView:self.mapView belowLayer:nil];
try {
[layer addToMapView:self.mapView belowLayer:nil];
} catch (std::runtime_error & err) {
[NSException raise:@"MGLRedundantLayerIdentifierException" format:@"%s", err.what()];
}
[self didChangeValueForKey:@"layers"];
}
@@ -399,7 +411,11 @@ static NSURL *MGLStyleURL_emerald;
sibling];
}
[self willChangeValueForKey:@"layers"];
[layer addToMapView:self.mapView belowLayer:sibling];
try {
[layer addToMapView:self.mapView belowLayer:sibling];
} catch (std::runtime_error & err) {
[NSException raise:@"MGLRedundantLayerIdentifierException" format:@"%s", err.what()];
}
[self didChangeValueForKey:@"layers"];
}
@@ -437,10 +453,18 @@ static NSURL *MGLStyleURL_emerald;
@"Make sure sibling was obtained using -[MGLStyle layerWithIdentifier:].",
sibling];
} else if (index + 1 == layers.size()) {
[layer addToMapView:self.mapView belowLayer:nil];
try {
[layer addToMapView:self.mapView belowLayer:nil];
} catch (std::runtime_error & err) {
[NSException raise:@"MGLRedundantLayerIdentifierException" format:@"%s", err.what()];
}
} else {
MGLStyleLayer *sibling = [self layerFromMBGLLayer:layers.at(index + 1)];
[layer addToMapView:self.mapView belowLayer:sibling];
try {
[layer addToMapView:self.mapView belowLayer:sibling];
} catch (std::runtime_error & err) {
[NSException raise:@"MGLRedundantLayerIdentifierException" format:@"%s", err.what()];
}
}
[self didChangeValueForKey:@"layers"];
}

View File

@@ -1,5 +1,6 @@
#import "MGLMapView.h"
#import "MGLStyle_Private.h"
#import "MGLOpenGLStyleLayer.h"
#import "MGLShapeSource.h"
#import "MGLRasterSource.h"
@@ -170,6 +171,23 @@
XCTAssertThrowsSpecificNamed([self.style addLayer:symbolLayer], NSException, @"MGLRedundantLayerException");
}
- (void)testAddingLayersWithDuplicateIdentifiers {
//Just some source
MGLVectorSource *source = [[MGLVectorSource alloc] initWithIdentifier:@"my-source" URL:[NSURL URLWithString:@"mapbox://mapbox.mapbox-terrain-v2"]];
[self.mapView.style addSource: source];
//Add initial layer
MGLFillStyleLayer *initial = [[MGLFillStyleLayer alloc] initWithIdentifier:@"my-layer" source:source];
[self.mapView.style addLayer:initial];
//Try to add the duplicate
XCTAssertThrowsSpecificNamed([self.mapView.style addLayer:[[MGLFillStyleLayer alloc] initWithIdentifier:@"my-layer" source:source]], NSException, @"MGLRedundantLayerIdentifierException");
XCTAssertThrowsSpecificNamed([self.mapView.style insertLayer:[[MGLFillStyleLayer alloc] initWithIdentifier:@"my-layer" source:source] belowLayer:initial],NSException, @"MGLRedundantLayerIdentifierException");
XCTAssertThrowsSpecificNamed([self.mapView.style insertLayer:[[MGLFillStyleLayer alloc] initWithIdentifier:@"my-layer" source:source] aboveLayer:initial], NSException, @"MGLRedundantLayerIdentifierException");
XCTAssertThrowsSpecificNamed([self.mapView.style insertLayer:[[MGLFillStyleLayer alloc] initWithIdentifier:@"my-layer" source:source] atIndex:0], NSException, @"MGLRedundantLayerIdentifierException");
XCTAssertThrowsSpecificNamed([self.mapView.style insertLayer:[[MGLOpenGLStyleLayer alloc] initWithIdentifier:@"my-layer"] atIndex:0], NSException, @"MGLRedundantLayerIdentifierException");
}
- (NSString *)stringWithContentsOfStyleHeader {
NSURL *styleHeaderURL = [[[NSBundle mgl_frameworkBundle].bundleURL
URLByAppendingPathComponent:@"Headers" isDirectory:YES]