From d3895b47efc28aa63b941de4341c4cb6fd6fb477 Mon Sep 17 00:00:00 2001 From: Ivo van Dongen Date: Tue, 6 Dec 2016 11:48:36 +0100 Subject: [ios, macos] handle duplicate layer error --- platform/darwin/src/MGLStyle.mm | 38 ++++++++++++++++++++++++++++------- platform/darwin/test/MGLStyleTests.mm | 18 +++++++++++++++++ 2 files changed, 49 insertions(+), 7 deletions(-) (limited to 'platform') diff --git a/platform/darwin/src/MGLStyle.mm b/platform/darwin/src/MGLStyle.mm index c84782dba9..1e60b956c7 100644 --- a/platform/darwin/src/MGLStyle.mm +++ b/platform/darwin/src/MGLStyle.mm @@ -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"]; } diff --git a/platform/darwin/test/MGLStyleTests.mm b/platform/darwin/test/MGLStyleTests.mm index 0dd6e98c5d..7abc88eebc 100644 --- a/platform/darwin/test/MGLStyleTests.mm +++ b/platform/darwin/test/MGLStyleTests.mm @@ -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] -- cgit v1.2.1