diff options
author | Jesse Bounds <jesse@rebounds.net> | 2016-12-01 21:20:53 -0800 |
---|---|---|
committer | Jesse Bounds <jesse@rebounds.net> | 2016-12-02 14:21:05 -0800 |
commit | 1db0a96927d53bc0216d48cf5add1254ae6cab89 (patch) | |
tree | 43625af30467a433cde87ed03ee76768cfd3fd95 /platform/darwin/test/MGLStyleTests.mm | |
parent | 2db31be8bc0130ac2f02bc2245b1ff21103e9baf (diff) | |
download | qtlocation-mapboxgl-1db0a96927d53bc0216d48cf5add1254ae6cab89.tar.gz |
[ios, macos] Raise NSException if layer or source added more than once
When MGLSource and MGLLayer instances are added to the style, they lose
ownership of their std::unique_ptr<T> after it is moved to the mbgl
level. Subsequent attempts to add such instances result in a C++
exception.
This adds logic in the Darwin platform to raise a NSException if the
source and layer addToMapView methods are called more than once and the
pointer is invalid. In addition, the documentation in MGLStyle for
addSource: and addLayer: has been update to warn developers to avoid
adding the same instance twice and, for that matter, instances with the
same identifier.
Diffstat (limited to 'platform/darwin/test/MGLStyleTests.mm')
-rw-r--r-- | platform/darwin/test/MGLStyleTests.mm | 61 |
1 files changed, 60 insertions, 1 deletions
diff --git a/platform/darwin/test/MGLStyleTests.mm b/platform/darwin/test/MGLStyleTests.mm index 07e012edc6..6a6a4a827f 100644 --- a/platform/darwin/test/MGLStyleTests.mm +++ b/platform/darwin/test/MGLStyleTests.mm @@ -1,4 +1,16 @@ -#import "MGLStyle.h" +#import "MGLMapView.h" +#import "MGLStyle_Private.h" + +#import "MGLGeoJSONSource.h" +#import "MGLRasterSource.h" +#import "MGLVectorSource.h" + +#import "MGLBackgroundStyleLayer.h" +#import "MGLCircleStyleLayer.h" +#import "MGLFillStyleLayer.h" +#import "MGLLineStyleLayer.h" +#import "MGLRasterStyleLayer.h" +#import "MGLSymbolStyleLayer.h" #import "NSBundle+MGLAdditions.h" @@ -92,6 +104,53 @@ }]; } +- (void)testAddingSourcesTwice { + MGLMapView *mapView = [[MGLMapView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; + MGLStyle *style = [[MGLStyle alloc] initWithMapView:mapView]; + + MGLGeoJSONSource *geoJSONSource = [[MGLGeoJSONSource alloc] initWithIdentifier:@"geoJSONSource" features:@[] options:nil]; + [style addSource:geoJSONSource]; + XCTAssertThrowsSpecificNamed([style addSource:geoJSONSource], NSException, @"MGLRedundantSourceException"); + + MGLRasterSource *rasterSource = [[MGLRasterSource alloc] initWithIdentifier:@"rasterSource" URL:[NSURL new] tileSize:42]; + [style addSource:rasterSource]; + XCTAssertThrowsSpecificNamed([style addSource:rasterSource], NSException, @"MGLRedundantSourceException"); + + MGLVectorSource *vectorSource = [[MGLVectorSource alloc] initWithIdentifier:@"vectorSource" URL:[NSURL new]]; + [style addSource:vectorSource]; + XCTAssertThrowsSpecificNamed([style addSource:vectorSource], NSException, @"MGLRedundantSourceException"); +} + +- (void)testAddingLayersTwice { + MGLMapView *mapView = [[MGLMapView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; + MGLStyle *style = [[MGLStyle alloc] initWithMapView:mapView]; + MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithIdentifier:@"geoJSONSource" features:@[] options:nil]; + + MGLBackgroundStyleLayer *backgroundLayer = [[MGLBackgroundStyleLayer alloc] initWithIdentifier:@"backgroundLayer"]; + [style addLayer:backgroundLayer]; + XCTAssertThrowsSpecificNamed([style addLayer:backgroundLayer], NSException, @"MGLRedundantLayerException"); + + MGLCircleStyleLayer *circleLayer = [[MGLCircleStyleLayer alloc] initWithIdentifier:@"circleLayer" source:source]; + [style addLayer:circleLayer]; + XCTAssertThrowsSpecificNamed([style addLayer:circleLayer], NSException, @"MGLRedundantLayerException"); + + MGLFillStyleLayer *fillLayer = [[MGLFillStyleLayer alloc] initWithIdentifier:@"fillLayer" source:source]; + [style addLayer:fillLayer]; + XCTAssertThrowsSpecificNamed([style addLayer:fillLayer], NSException, @"MGLRedundantLayerException"); + + MGLLineStyleLayer *lineLayer = [[MGLLineStyleLayer alloc] initWithIdentifier:@"lineLayer" source:source]; + [style addLayer:lineLayer]; + XCTAssertThrowsSpecificNamed([style addLayer:lineLayer], NSException, @"MGLRedundantLayerException"); + + MGLRasterStyleLayer *rasterLayer = [[MGLRasterStyleLayer alloc] initWithIdentifier:@"rasterLayer" source:source]; + [style addLayer:rasterLayer]; + XCTAssertThrowsSpecificNamed([style addLayer:rasterLayer], NSException, @"MGLRedundantLayerException"); + + MGLSymbolStyleLayer *symbolLayer = [[MGLSymbolStyleLayer alloc] initWithIdentifier:@"symbolLayer" source:source]; + [style addLayer:symbolLayer]; + XCTAssertThrowsSpecificNamed([style addLayer:symbolLayer], NSException, @"MGLRedundantLayerException"); +} + - (NSString *)stringWithContentsOfStyleHeader { NSURL *styleHeaderURL = [[[NSBundle mgl_frameworkBundle].bundleURL URLByAppendingPathComponent:@"Headers" isDirectory:YES] |