diff options
Diffstat (limited to 'platform/darwin/test')
-rw-r--r-- | platform/darwin/test/MGLAttributionInfoTests.m | 2 | ||||
-rw-r--r-- | platform/darwin/test/MGLFeatureTests.mm | 2 | ||||
-rw-r--r-- | platform/darwin/test/MGLFilterTests.mm | 6 | ||||
-rw-r--r-- | platform/darwin/test/MGLGeometryTests.mm | 45 | ||||
-rw-r--r-- | platform/darwin/test/MGLShapeSourceTests.mm | 37 | ||||
-rw-r--r-- | platform/darwin/test/MGLStyleTests.mm | 10 | ||||
-rw-r--r-- | platform/darwin/test/MGLTileSetTests.mm | 103 |
7 files changed, 140 insertions, 65 deletions
diff --git a/platform/darwin/test/MGLAttributionInfoTests.m b/platform/darwin/test/MGLAttributionInfoTests.m index 003637bf1b..3cdb7adffb 100644 --- a/platform/darwin/test/MGLAttributionInfoTests.m +++ b/platform/darwin/test/MGLAttributionInfoTests.m @@ -1,7 +1,7 @@ #import <Mapbox/Mapbox.h> #import <XCTest/XCTest.h> -#import "MGLAttributionInfo.h" +#import "MGLAttributionInfo_Private.h" @interface MGLAttributionInfoTests : XCTestCase diff --git a/platform/darwin/test/MGLFeatureTests.mm b/platform/darwin/test/MGLFeatureTests.mm index 7f464aaab1..33c146e723 100644 --- a/platform/darwin/test/MGLFeatureTests.mm +++ b/platform/darwin/test/MGLFeatureTests.mm @@ -159,7 +159,7 @@ } - (void)testPointFeatureGeoJSONDictionary { - MGLPointFeature<MGLFeaturePrivate> *pointFeature = (MGLPointFeature<MGLFeaturePrivate> *)[[MGLPointFeature alloc] init]; + MGLPointFeature *pointFeature = [[MGLPointFeature alloc] init]; CLLocationCoordinate2D coordinate = { 10, 10 }; pointFeature.coordinate = coordinate; diff --git a/platform/darwin/test/MGLFilterTests.mm b/platform/darwin/test/MGLFilterTests.mm index 4b393fd1dc..e688d50583 100644 --- a/platform/darwin/test/MGLFilterTests.mm +++ b/platform/darwin/test/MGLFilterTests.mm @@ -18,7 +18,11 @@ NSString *filePath = [[NSBundle bundleForClass:self.class] pathForResource:@"amsterdam" ofType:@"geojson"]; NSURL *url = [NSURL fileURLWithPath:filePath]; NSData *geoJSONData = [NSData dataWithContentsOfURL:url]; - source = [[MGLShapeSource alloc] initWithIdentifier:@"test-source" geoJSONData:geoJSONData options:nil]; + NSError *error; + MGLShape *shape = [MGLShape shapeWithData:geoJSONData encoding:NSUTF8StringEncoding error:&error]; + XCTAssertNil(error); + XCTAssertNotNil(shape); + source = [[MGLShapeSource alloc] initWithIdentifier:@"test-source" shape:shape options:nil]; [self.mapView.style addSource:source]; layer = [[MGLLineStyleLayer alloc] initWithIdentifier:@"test-layer" source:source]; } diff --git a/platform/darwin/test/MGLGeometryTests.mm b/platform/darwin/test/MGLGeometryTests.mm index b15916e6fa..0ffc27b29e 100644 --- a/platform/darwin/test/MGLGeometryTests.mm +++ b/platform/darwin/test/MGLGeometryTests.mm @@ -99,4 +99,49 @@ XCTAssertFalse(MGLCoordinateInCoordinateBounds(kCLLocationCoordinate2DInvalid, wyoming)); } +- (void)testGeoJSONDeserialization { + NSData *data = [@"{\"type\": \"Feature\", \"geometry\": {\"type\": \"Point\", \"coordinates\": [0, 0]}, \"properties\": {}}" dataUsingEncoding:NSUTF8StringEncoding]; + NSError *error; + MGLPointFeature *feature = (MGLPointFeature *)[MGLShape shapeWithData:data encoding:NSUTF8StringEncoding error:&error]; + XCTAssertNil(error, @"Valid GeoJSON data should produce no error on deserialization."); + XCTAssertNotNil(feature, @"Valid GeoJSON data should produce an object on deserialization."); + XCTAssertTrue([feature isKindOfClass:[MGLPointFeature class]], @"Valid GeoJSON point feature data should produce an MGLPointFeature."); + XCTAssertEqual(feature.attributes.count, 0); + XCTAssertEqual(feature.coordinate.latitude, 0); + XCTAssertEqual(feature.coordinate.longitude, 0); + + data = [@"{\"type\": \"Feature\", \"feature\": {\"type\": \"Point\", \"coordinates\": [0, 0]}}" dataUsingEncoding:NSUTF8StringEncoding]; + error = nil; + MGLShape *shape = [MGLShape shapeWithData:data encoding:NSUTF8StringEncoding error:&error]; + XCTAssertNotNil(error, @"Invalid GeoJSON data should produce an error on deserialization."); + XCTAssertNil(shape, @"Invalid GeoJSON data should produce no object on deserialization."); +} + +- (void)testGeoJSONSerialization { + MGLPointFeature *feature = [[MGLPointFeature alloc] init]; + feature.identifier = @504; + feature.coordinate = CLLocationCoordinate2DMake(29.95, -90.066667); + + NSData *data = [feature geoJSONDataUsingEncoding:NSUTF8StringEncoding]; + XCTAssertNotNil(data, @"MGLPointFeature should serialize as an UTF-8 string data object."); + NSError *error; + NSDictionary *serializedGeoJSON = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; + XCTAssertNil(error, @"Serialized GeoJSON data should be deserializable JSON."); + XCTAssertNotNil(serializedGeoJSON, @"Serialized GeoJSON data should be valid JSON."); + XCTAssertTrue([serializedGeoJSON isKindOfClass:[NSDictionary class]], @"Serialized GeoJSON data should be a JSON object."); + NSDictionary *geoJSON = @{ + @"type": @"Feature", + @"id": @504, + @"geometry": @{ + @"type": @"Point", + @"coordinates": @[ + @(-90.066667), + @29.95, + ], + }, + @"properties": @{}, + }; + XCTAssertEqualObjects(serializedGeoJSON, geoJSON, @"MGLPointFeature should serialize as a GeoJSON point feature."); +} + @end diff --git a/platform/darwin/test/MGLShapeSourceTests.mm b/platform/darwin/test/MGLShapeSourceTests.mm index df90ea74d7..efff4b393e 100644 --- a/platform/darwin/test/MGLShapeSourceTests.mm +++ b/platform/darwin/test/MGLShapeSourceTests.mm @@ -12,18 +12,15 @@ @implementation MGLShapeSourceTests -- (void)testMGLShapeSourceWithOptions { - NSURL *url = [NSURL URLWithString:@"http://www.mapbox.com/source"]; - +- (void)testGeoJSONOptionsFromDictionary { NSDictionary *options = @{MGLShapeSourceOptionClustered: @YES, MGLShapeSourceOptionClusterRadius: @42, MGLShapeSourceOptionMaximumZoomLevelForClustering: @98, MGLShapeSourceOptionMaximumZoomLevel: @99, MGLShapeSourceOptionBuffer: @1976, MGLShapeSourceOptionSimplificationTolerance: @0.42}; - MGLShapeSource *source = [[MGLShapeSource alloc] initWithIdentifier:@"source-id" URL:url options:options]; - auto mbglOptions = [source geoJSONOptions]; + auto mbglOptions = MGLGeoJSONOptionsFromDictionary(options); XCTAssertTrue(mbglOptions.cluster); XCTAssertEqual(mbglOptions.clusterRadius, 42); XCTAssertEqual(mbglOptions.clusterMaxZoom, 98); @@ -32,7 +29,7 @@ XCTAssertEqual(mbglOptions.tolerance, 0.42); options = @{MGLShapeSourceOptionClustered: @"number 1"}; - XCTAssertThrows([[MGLShapeSource alloc] initWithIdentifier:@"source-id" URL:url options:options]); + XCTAssertThrows(MGLGeoJSONOptionsFromDictionary(options)); } - (void)testNilShape { @@ -45,7 +42,11 @@ NSString *geoJSON = @"{\"type\": \"FeatureCollection\",\"features\": [{\"type\": \"Feature\",\"properties\": {},\"geometry\": {\"type\": \"LineString\",\"coordinates\": [[-107.75390625,40.329795743702064],[-104.34814453125,37.64903402157866]]}}]}"; NSData *data = [geoJSON dataUsingEncoding:NSUTF8StringEncoding]; - MGLShapeSource *source = [[MGLShapeSource alloc] initWithIdentifier:@"source-id" geoJSONData:data options:nil]; + NSError *error; + MGLShape *shape = [MGLShape shapeWithData:data encoding:NSUTF8StringEncoding error:&error]; + XCTAssertNil(error); + XCTAssertNotNil(shape); + MGLShapeSource *source = [[MGLShapeSource alloc] initWithIdentifier:@"source-id" shape:shape options:nil]; MGLShapeCollection *collection = (MGLShapeCollection *)source.shape; XCTAssertNotNil(collection); @@ -54,18 +55,24 @@ } - (void)testMGLShapeSourceWithSingleGeometry { - MGLShapeSource *source = [[MGLShapeSource alloc] initWithIdentifier:@"geojson" - geoJSONData:[@"{\"type\": \"Point\", \"coordinates\": [0, 0]}" dataUsingEncoding:NSUTF8StringEncoding] - options:nil]; + NSData *data = [@"{\"type\": \"Point\", \"coordinates\": [0, 0]}" dataUsingEncoding:NSUTF8StringEncoding]; + NSError *error; + MGLShape *shape = [MGLShape shapeWithData:data encoding:NSUTF8StringEncoding error:&error]; + XCTAssertNil(error); + XCTAssertNotNil(shape); + MGLShapeSource *source = [[MGLShapeSource alloc] initWithIdentifier:@"geojson" shape:shape options:nil]; XCTAssertNotNil(source.shape); - XCTAssert([source.shape isKindOfClass:[MGLPointFeature class]]); + XCTAssert([source.shape isKindOfClass:[MGLPointAnnotation class]]); } - (void)testMGLGeoJSONSourceWithSingleFeature { NSString *geoJSON = @"{\"type\": \"Feature\", \"properties\": {\"color\": \"green\"}, \"geometry\": { \"type\": \"Point\", \"coordinates\": [ -114.06847000122069, 51.050459433092655 ] }}"; - MGLShapeSource *source = [[MGLShapeSource alloc] initWithIdentifier:@"geojson" - geoJSONData:[geoJSON dataUsingEncoding:NSUTF8StringEncoding] - options:nil]; + NSData *data = [geoJSON dataUsingEncoding:NSUTF8StringEncoding]; + NSError *error; + MGLShape *shape = [MGLShape shapeWithData:data encoding:NSUTF8StringEncoding error:&error]; + XCTAssertNil(error); + XCTAssertNotNil(shape); + MGLShapeSource *source = [[MGLShapeSource alloc] initWithIdentifier:@"geojson" shape:shape options:nil]; XCTAssertNotNil(source.shape); XCTAssert([source.shape isKindOfClass:[MGLPointFeature class]]); MGLPointFeature *feature = (MGLPointFeature *)source.shape; @@ -90,7 +97,7 @@ CLLocationCoordinate2DMake(100.0, 1.0), CLLocationCoordinate2DMake(100.0, 0.0)}; - MGLPolygonFeature<MGLFeaturePrivate> *polygonFeature = (MGLPolygonFeature<MGLFeaturePrivate> *)[MGLPolygonFeature polygonWithCoordinates:coordinates count:5]; + MGLPolygonFeature *polygonFeature = [MGLPolygonFeature polygonWithCoordinates:coordinates count:5]; polygonFeature.identifier = @"feature-id"; NSString *stringAttribute = @"string"; NSNumber *boolAttribute = [NSNumber numberWithBool:YES]; diff --git a/platform/darwin/test/MGLStyleTests.mm b/platform/darwin/test/MGLStyleTests.mm index 7abc88eebc..c67497640b 100644 --- a/platform/darwin/test/MGLStyleTests.mm +++ b/platform/darwin/test/MGLStyleTests.mm @@ -126,18 +126,18 @@ [self.style addSource:shapeSource]; XCTAssertThrowsSpecificNamed([self.style addSource:shapeSource], NSException, @"MGLRedundantSourceException"); - MGLRasterSource *rasterSource = [[MGLRasterSource alloc] initWithIdentifier:@"rasterSource" URL:[NSURL new] tileSize:42]; + MGLRasterSource *rasterSource = [[MGLRasterSource alloc] initWithIdentifier:@"rasterSource" configurationURL:[NSURL URLWithString:@".json"] tileSize:42]; [self.style addSource:rasterSource]; XCTAssertThrowsSpecificNamed([self.style addSource:rasterSource], NSException, @"MGLRedundantSourceException"); - MGLVectorSource *vectorSource = [[MGLVectorSource alloc] initWithIdentifier:@"vectorSource" URL:[NSURL new]]; + MGLVectorSource *vectorSource = [[MGLVectorSource alloc] initWithIdentifier:@"vectorSource" configurationURL:[NSURL URLWithString:@".json"]]; [self.style addSource:vectorSource]; XCTAssertThrowsSpecificNamed([self.style addSource:vectorSource], NSException, @"MGLRedundantSourceException"); } - (void)testAddingSourcesWithDuplicateIdentifiers { - MGLVectorSource *source1 = [[MGLVectorSource alloc] initWithIdentifier:@"my-source" URL:[NSURL URLWithString:@"mapbox://mapbox.mapbox-terrain-v2"]]; - MGLVectorSource *source2 = [[MGLVectorSource alloc] initWithIdentifier:@"my-source" URL:[NSURL URLWithString:@"mapbox://mapbox.mapbox-terrain-v2"]]; + MGLVectorSource *source1 = [[MGLVectorSource alloc] initWithIdentifier:@"my-source" configurationURL:[NSURL URLWithString:@"mapbox://mapbox.mapbox-terrain-v2"]]; + MGLVectorSource *source2 = [[MGLVectorSource alloc] initWithIdentifier:@"my-source" configurationURL:[NSURL URLWithString:@"mapbox://mapbox.mapbox-terrain-v2"]]; [self.style addSource: source1]; XCTAssertThrowsSpecificNamed([self.style addSource: source2], NSException, @"MGLRedundantSourceIdentifierException"); @@ -173,7 +173,7 @@ - (void)testAddingLayersWithDuplicateIdentifiers { //Just some source - MGLVectorSource *source = [[MGLVectorSource alloc] initWithIdentifier:@"my-source" URL:[NSURL URLWithString:@"mapbox://mapbox.mapbox-terrain-v2"]]; + MGLVectorSource *source = [[MGLVectorSource alloc] initWithIdentifier:@"my-source" configurationURL:[NSURL URLWithString:@"mapbox://mapbox.mapbox-terrain-v2"]]; [self.mapView.style addSource: source]; //Add initial layer diff --git a/platform/darwin/test/MGLTileSetTests.mm b/platform/darwin/test/MGLTileSetTests.mm index d77046928c..06901a0e96 100644 --- a/platform/darwin/test/MGLTileSetTests.mm +++ b/platform/darwin/test/MGLTileSetTests.mm @@ -1,7 +1,7 @@ #import <XCTest/XCTest.h> #import <Mapbox/Mapbox.h> -#import "MGLTileSet_Private.h" +#import "MGLTileSource_Private.h" #include <mbgl/util/tileset.hpp> @@ -11,72 +11,91 @@ @implementation MGLTileSetTests -- (void)testTileSet { +- (void)testTileSetFromTileURLTemplates { // a tile set that provides an mbgl tile set - MGLTileSet *tileSet = [[MGLTileSet alloc] initWithTileURLTemplates:@[@"tile.1", - @"tile.2", - @"tile.3"]]; - mbgl::Tileset mbglTileset = [tileSet mbglTileset]; + NSArray *tileURLTemplates = @[@"tile.1", @"tile.2", @"tile.3"]; + mbgl::Tileset tileSet = MGLTileSetFromTileURLTemplates(tileURLTemplates, nil); // has the correct URL templates - XCTAssertEqual(mbglTileset.tiles.size(), 3); - XCTAssertEqual(mbglTileset.tiles[0], "tile.1"); - XCTAssertEqual(mbglTileset.tiles[1], "tile.2"); - XCTAssertEqual(mbglTileset.tiles[2], "tile.3"); + XCTAssertEqual(tileSet.tiles.size(), 3); + XCTAssertEqual(tileSet.tiles[0], "tile.1"); + XCTAssertEqual(tileSet.tiles[1], "tile.2"); + XCTAssertEqual(tileSet.tiles[2], "tile.3"); // has the default scheme - XCTAssertEqual(mbglTileset.scheme, mbgl::Tileset::Scheme::XYZ); + XCTAssertEqual(tileSet.scheme, mbgl::Tileset::Scheme::XYZ); // when the tile set has no min or max zoom level set - tileSet.minimumZoomLevel = nil; - tileSet.maximumZoomLevel = nil; - // the mbgl object has default values for min and max zoom level - XCTAssertEqual([tileSet mbglTileset].zoomRange.min, 0); - XCTAssertEqual([tileSet mbglTileset].zoomRange.max, 22); + XCTAssertEqual(tileSet.zoomRange.min, 0); + XCTAssertEqual(tileSet.zoomRange.max, 22); // when the tile set has min and/or max zoom level set - tileSet.minimumZoomLevel = @(1); - tileSet.maximumZoomLevel = @(2); + tileSet = MGLTileSetFromTileURLTemplates(@[@"tile.1"], @{ + MGLTileSourceOptionMinimumZoomLevel: @1, + MGLTileSourceOptionMaximumZoomLevel: @2, + }); // the mbgl object reflects the set values for min and max zoom level - XCTAssertEqual([tileSet mbglTileset].zoomRange.min, 1); - XCTAssertEqual([tileSet mbglTileset].zoomRange.max, 2); + XCTAssertEqual(tileSet.zoomRange.min, 1); + XCTAssertEqual(tileSet.zoomRange.max, 2); // when the tile set has an attribution - tileSet.attribution = @"my tileset © ©️🎈"; + NSString *attribution = @"my tileset © ©️🎈"; + tileSet = MGLTileSetFromTileURLTemplates(tileURLTemplates, @{ + MGLTileSourceOptionAttributionHTMLString: attribution, + }); // the attribution is reflected by the mbgl tileset - XCTAssertEqual([tileSet mbglTileset].attribution, tileSet.attribution.UTF8String); + XCTAssertEqual(tileSet.attribution, attribution.UTF8String); + + // when the tile set has attribution infos + MGLAttributionInfo *mapboxInfo = [[MGLAttributionInfo alloc] initWithTitle:[[NSAttributedString alloc] initWithString:@"Mapbox"] + URL:[NSURL URLWithString:@"https://www.mapbox.com/"]]; + NSAttributedString *gl = [[NSAttributedString alloc] initWithString:@"GL" attributes:@{ + NSBackgroundColorAttributeName: [MGLColor redColor], + }]; + MGLAttributionInfo *glInfo = [[MGLAttributionInfo alloc] initWithTitle:gl URL:nil]; + tileSet = MGLTileSetFromTileURLTemplates(tileURLTemplates, @{ + MGLTileSourceOptionAttributionInfos: @[mapboxInfo, glInfo], + }); - // when the scheme is changed - tileSet.scheme = MGLTileSetSchemeTMS; + // the attribution is reflected by the mbgl tileset +#if TARGET_OS_IPHONE + NSString *html = (@"<font style=\"font-family: 'Helvetica'; font-weight: normal; font-style: normal; font-size: 12.00pt\">" + @"<a href=\"https://www.mapbox.com/\">Mapbox</a> </font>" + @"<font style=\"font-family: 'Helvetica'; font-weight: normal; font-style: normal; font-size: 12.00pt; background-color: #ff0000\">GL</font>\n"); +#else + NSString *html = (@"<font face=\"Helvetica\" size=\"3\" style=\"font: 12.0px Helvetica\">" + @"<a href=\"https://www.mapbox.com/\">Mapbox</a> </font>" + @"<font face=\"Helvetica\" size=\"3\" style=\"font: 12.0px Helvetica; background-color: #ff2600\">GL</font>\n"); +#endif + XCTAssertEqualObjects(@(tileSet.attribution.c_str()), html); + + // when the tile coordinate system is changed using an NSNumber + tileSet = MGLTileSetFromTileURLTemplates(tileURLTemplates, @{ + MGLTileSourceOptionTileCoordinateSystem: @(MGLTileCoordinateSystemTMS), + }); // the scheme is reflected by the mbgl tileset - XCTAssertEqual([tileSet mbglTileset].scheme , mbgl::Tileset::Scheme::TMS); + XCTAssertEqual(tileSet.scheme, mbgl::Tileset::Scheme::TMS); - // a tile set that provides an mbgl tile set and minimum and maximum zoom levels - tileSet = [[MGLTileSet alloc] initWithTileURLTemplates:@[@"tile.1"] minimumZoomLevel:15 maximumZoomLevel:20]; + // when the tile coordinate system is changed using an NSValue + MGLTileCoordinateSystem tms = MGLTileCoordinateSystemTMS; + tileSet = MGLTileSetFromTileURLTemplates(tileURLTemplates, @{ + MGLTileSourceOptionTileCoordinateSystem: [NSValue value:&tms withObjCType:@encode(MGLTileCoordinateSystem)], + }); - // the zoom levels are reflected by the mbgl tileset - XCTAssertEqual([tileSet mbglTileset].zoomRange.min, 15); - XCTAssertEqual([tileSet mbglTileset].zoomRange.max, 20); + // the scheme is reflected by the mbgl tileset + XCTAssertEqual(tileSet.scheme, mbgl::Tileset::Scheme::TMS); } - (void)testInvalidTileSet { // a tile set that provides an mbgl tile set and invalid (crossed) minimum and maximum zoom levels throws an exception - XCTAssertThrowsSpecificNamed([[MGLTileSet alloc] initWithTileURLTemplates:@[@"tile.1"] minimumZoomLevel:10 maximumZoomLevel:9], NSException, @"Invalid minimumZoomLevel"); - - // a tile set that provides an mbgl tile set - MGLTileSet *tileSet = [[MGLTileSet alloc] initWithTileURLTemplates:@[@"tile.1"]]; - tileSet.maximumZoomLevel = @(10); - - // when the minimum zoom level is set higher than the maximum zoom level - XCTAssertThrowsSpecificNamed(tileSet.minimumZoomLevel = @(11), NSException, @"Invalid minimumZoomLevel"); - - // when the maximum zoom level is set lower than the minimum zoom level - tileSet.minimumZoomLevel = @(5); - XCTAssertThrowsSpecificNamed(tileSet.maximumZoomLevel = @(4), NSException, @"Invalid minimumZoomLevel"); + XCTAssertThrowsSpecificNamed(MGLTileSetFromTileURLTemplates(@[@"tile.1"], @{ + MGLTileSourceOptionMinimumZoomLevel: @10, + MGLTileSourceOptionMaximumZoomLevel: @9, + }), NSException, NSInvalidArgumentException); } @end |