[ios, macos] Unit-test snapshotter

This commit is contained in:
Minh Nguyễn
2020-03-23 16:55:01 -07:00
committed by Minh Nguyễn
parent 63bf91125a
commit ecf9a141a0
3 changed files with 120 additions and 0 deletions

View File

@@ -0,0 +1,112 @@
#import <Mapbox/Mapbox.h>
#import <XCTest/XCTest.h>
@interface MGLMapSnapshotterTests : XCTestCase <MGLMapSnapshotterDelegate>
@property (nonatomic) XCTestExpectation *styleLoadingExpectation;
@end
@implementation MGLMapSnapshotterTests
- (void)setUp {
[super setUp];
[MGLAccountManager setAccessToken:@"pk.feedcafedeadbeefbadebede"];
// Initialize file system and run loop before snapshotting.
// https://github.com/mapbox/mapbox-gl-native-ios/issues/227
(void)[MGLOfflineStorage sharedOfflineStorage];
}
- (void)tearDown {
[MGLAccountManager setAccessToken:nil];
self.styleLoadingExpectation = nil;
[super tearDown];
}
- (void)testOverlayHandler {
self.styleLoadingExpectation = [self expectationWithDescription:@"Style should finish loading."];
XCTestExpectation *overlayExpectation = [self expectationWithDescription:@"Overlay handler should get called."];
XCTestExpectation *completionExpectation = [self expectationWithDescription:@"Completion handler should get called."];
#if TARGET_OS_IPHONE
CGRect rect = CGRectMake(0, 0, 500, 500);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, UIScreen.mainScreen.scale);
UIImage *blankImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
#else
NSImage *blankImage = [NSImage imageWithSize:NSMakeSize(500, 500) flipped:NO drawingHandler:^BOOL(NSRect dstRect) {
return YES;
}];
CGImageRef blankCGImage = [blankImage CGImageForProposedRect:nil context:nil hints:nil];
NSBitmapImageRep *blankImageRep = [[NSBitmapImageRep alloc] initWithCGImage:blankCGImage];
NSData *blankImageData = [blankImageRep representationUsingType:NSPNGFileType properties:@{}];
#endif
NSURL *styleURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"one-liner" withExtension:@"json"];
MGLMapCamera *camera = [MGLMapCamera camera];
MGLMapSnapshotOptions *options = [[MGLMapSnapshotOptions alloc] initWithStyleURL:styleURL camera:camera size:CGSizeMake(500, 500)];
MGLMapSnapshotter *snapshotter = [[MGLMapSnapshotter alloc] initWithOptions:options];
snapshotter.delegate = self;
[snapshotter startWithOverlayHandler:^(MGLMapSnapshotOverlay * _Nonnull snapshotOverlay) {
XCTAssertNotNil(snapshotOverlay);
if (snapshotOverlay) {
XCTAssertNotEqual(snapshotOverlay.context, NULL);
#if TARGET_OS_IPHONE
UIImage *imageFromContext = UIGraphicsGetImageFromCurrentImageContext();
XCTAssertEqualObjects(UIImagePNGRepresentation(blankImage), UIImagePNGRepresentation(imageFromContext), @"Base map in snapshot should be blank.");
#else
CGImageRef imageFromContext = CGBitmapContextCreateImage(snapshotOverlay.context);
NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] initWithCGImage:imageFromContext];
NSData *imageData = [imageRep representationUsingType:NSPNGFileType properties:@{}];
XCTAssertEqualObjects(blankImageData, imageData, @"Base map in snapshot should be blank.");
#endif
}
[overlayExpectation fulfill];
} completionHandler:^(MGLMapSnapshot * _Nullable snapshot, NSError * _Nullable error) {
XCTAssertNil(error);
XCTAssertNotNil(snapshot);
if (snapshot) {
XCTAssertEqual(snapshot.image.size.width, 500);
XCTAssertEqual(snapshot.image.size.height, 500);
}
[completionExpectation fulfill];
}];
[self waitForExpectations:@[self.styleLoadingExpectation, overlayExpectation, completionExpectation] timeout:2 enforceOrder:YES];
}
- (void)testDelegate {
self.styleLoadingExpectation = [self expectationWithDescription:@"Style should finish loading."];
XCTestExpectation *completionExpectation = [self expectationWithDescription:@"Completion handler should get called."];
NSURL *styleURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"one-liner" withExtension:@"json"];
MGLMapCamera *camera = [MGLMapCamera camera];
MGLMapSnapshotOptions *options = [[MGLMapSnapshotOptions alloc] initWithStyleURL:styleURL camera:camera size:CGSizeMake(500, 500)];
MGLMapSnapshotter *snapshotter = [[MGLMapSnapshotter alloc] initWithOptions:options];
snapshotter.delegate = self;
[snapshotter startWithCompletionHandler:^(MGLMapSnapshot * _Nullable snapshot, NSError * _Nullable error) {
XCTAssertNil(error);
XCTAssertNotNil(snapshot);
if (snapshot) {
XCTAssertEqual(snapshot.image.size.width, 500);
XCTAssertEqual(snapshot.image.size.height, 500);
}
[completionExpectation fulfill];
}];
[self waitForExpectations:@[self.styleLoadingExpectation, completionExpectation] timeout:2 enforceOrder:YES];
}
#pragma mark MGLMapSnapshotterDelegate methods
- (void)mapSnapshotter:(MGLMapSnapshotter *)snapshotter didFinishLoadingStyle:(MGLStyle *)style {
XCTAssertNotNil(snapshotter);
XCTAssertNotNil(style);
[self.styleLoadingExpectation fulfill];
}
@end

View File

@@ -577,6 +577,7 @@
DA8963391CC549A100684375 /* styles in Resources */ = {isa = PBXBuildFile; fileRef = DA8963351CC549A100684375 /* styles */; };
DA89633A1CC549A100684375 /* tiles in Resources */ = {isa = PBXBuildFile; fileRef = DA8963361CC549A100684375 /* tiles */; };
DAA32CC31E4C6B65006F8D24 /* MGLDistanceFormatter.m in Sources */ = {isa = PBXBuildFile; fileRef = 3557F7AF1E1D27D300CCA5E6 /* MGLDistanceFormatter.m */; };
DAA4227E2429771D008457DB /* MGLMapSnapshotterTests.m in Sources */ = {isa = PBXBuildFile; fileRef = DAA4227D2429771D008457DB /* MGLMapSnapshotterTests.m */; };
DAA4E4081CBB6C9500178DFB /* Mapbox.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DA8847D21CBAF91600AB86E3 /* Mapbox.framework */; };
DAA4E4091CBB6C9500178DFB /* Mapbox.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = DA8847D21CBAF91600AB86E3 /* Mapbox.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
DAA4E41C1CBB730400178DFB /* MGLAccountManager.m in Sources */ = {isa = PBXBuildFile; fileRef = DA8848001CBAFA6200AB86E3 /* MGLAccountManager.m */; };
@@ -1261,6 +1262,7 @@
DAA32CB71E4C4ED8006F8D24 /* sv */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.strings; name = sv; path = sv.lproj/Localizable.strings; sourceTree = "<group>"; };
DAA32CBC1E4C4F5D006F8D24 /* vi */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = vi; path = vi.lproj/Localizable.strings; sourceTree = "<group>"; };
DAA32CBD1E4C4F62006F8D24 /* vi */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = vi; path = vi.lproj/Foundation.strings; sourceTree = "<group>"; };
DAA4227D2429771D008457DB /* MGLMapSnapshotterTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = MGLMapSnapshotterTests.m; path = ../../darwin/test/MGLMapSnapshotterTests.m; sourceTree = "<group>"; };
DAA4E4131CBB71D400178DFB /* libMapbox.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libMapbox.a; sourceTree = BUILT_PRODUCTS_DIR; };
DAAE9B1C213A636B00F7D722 /* ar */ = {isa = PBXFileReference; lastKnownFileType = text.plist.stringsdict; name = ar; path = ar.lproj/Localizable.stringsdict; sourceTree = "<group>"; };
DAAF72291DA903C700312FA4 /* MGLStyleValue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MGLStyleValue.h; sourceTree = "<group>"; };
@@ -1863,6 +1865,7 @@
DA0E9F3E2411AC9B007C75D4 /* MGLImageTests.mm */,
DA5DB1291FABF1EE001C2326 /* MGLMapAccessibilityElementTests.m */,
DA695425215B1E75002041A4 /* MGLMapCameraTests.m */,
DAA4227D2429771D008457DB /* MGLMapSnapshotterTests.m */,
96E6145522CC135200109F14 /* MGLMapViewCompassViewTests.mm */,
1F0196A923174B0700F5C819 /* MGLMapViewContentInsetTests.m */,
96ED34DD22374C0900E9FCA9 /* MGLMapViewDirectionTests.mm */,
@@ -3010,6 +3013,7 @@
6407D6701E0085FD00F6A9C3 /* MGLDocumentationExampleTests.swift in Sources */,
DA2E88631CC0382C00F24E7B /* MGLOfflineRegionTests.m in Sources */,
409F43FD1E9E781C0048729D /* MGLMapViewDelegateIntegrationTests.swift in Sources */,
DAA4227E2429771D008457DB /* MGLMapSnapshotterTests.m in Sources */,
DA2E88651CC0382C00F24E7B /* MGLStyleTests.mm in Sources */,
DA2E88611CC0382C00F24E7B /* MGLGeometryTests.mm in Sources */,
170C437D2029D97900863DF0 /* MGLHeatmapStyleLayerTests.mm in Sources */,

View File

@@ -201,6 +201,7 @@
DA8F259C1D51CB000010E6B5 /* MGLStyleValue_Private.h in Headers */ = {isa = PBXBuildFile; fileRef = DA8F259B1D51CB000010E6B5 /* MGLStyleValue_Private.h */; };
DA8F25B21D51CB270010E6B5 /* NSValue+MGLStyleAttributeAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = DA8F25A61D51CB270010E6B5 /* NSValue+MGLStyleAttributeAdditions.h */; };
DA8F25B31D51CB270010E6B5 /* NSValue+MGLStyleAttributeAdditions.mm in Sources */ = {isa = PBXBuildFile; fileRef = DA8F25A71D51CB270010E6B5 /* NSValue+MGLStyleAttributeAdditions.mm */; };
DAA4227C24294137008457DB /* MGLMapSnapshotterTests.m in Sources */ = {isa = PBXBuildFile; fileRef = DAA4227B24294137008457DB /* MGLMapSnapshotterTests.m */; };
DAA48EFD1D6A4731006A7E36 /* StyleLayerIconTransformer.m in Sources */ = {isa = PBXBuildFile; fileRef = DAA48EFC1D6A4731006A7E36 /* StyleLayerIconTransformer.m */; };
DAA998FB1E9F545C002E6EA6 /* MGLFillExtrusionStyleLayer.h in Headers */ = {isa = PBXBuildFile; fileRef = DAA998F91E9F545C002E6EA6 /* MGLFillExtrusionStyleLayer.h */; settings = {ATTRIBUTES = (Public, ); }; };
DAA998FC1E9F545C002E6EA6 /* MGLFillExtrusionStyleLayer.mm in Sources */ = {isa = PBXBuildFile; fileRef = DAA998FA1E9F545C002E6EA6 /* MGLFillExtrusionStyleLayer.mm */; };
@@ -603,6 +604,7 @@
DAA32CBA1E4C4F10006F8D24 /* sv */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = sv; path = sv.lproj/Localizable.strings; sourceTree = "<group>"; };
DAA32CC01E4C4F89006F8D24 /* vi */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = vi; path = vi.lproj/Foundation.strings; sourceTree = "<group>"; };
DAA32CC11E4C4F93006F8D24 /* vi */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.strings; name = vi; path = vi.lproj/Localizable.strings; sourceTree = "<group>"; };
DAA4227B24294137008457DB /* MGLMapSnapshotterTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = MGLMapSnapshotterTests.m; path = ../../darwin/test/MGLMapSnapshotterTests.m; sourceTree = "<group>"; };
DAA48EFB1D6A4731006A7E36 /* StyleLayerIconTransformer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StyleLayerIconTransformer.h; sourceTree = "<group>"; };
DAA48EFC1D6A4731006A7E36 /* StyleLayerIconTransformer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = StyleLayerIconTransformer.m; sourceTree = "<group>"; };
DAA998F91E9F545C002E6EA6 /* MGLFillExtrusionStyleLayer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MGLFillExtrusionStyleLayer.h; sourceTree = "<group>"; };
@@ -1210,6 +1212,7 @@
DAE6C3C81CC34BD800DB3429 /* MGLGeometryTests.mm */,
DA0E9F3A24119F6B007C75D4 /* MGLImageTests.mm */,
DA695423215B1E6C002041A4 /* MGLMapCameraTests.m */,
DAA4227B24294137008457DB /* MGLMapSnapshotterTests.m */,
076171C4213A0DC200668A35 /* MGLMapViewTests.m */,
1F95931A1E6DE2B600D5B294 /* MGLNSDateAdditionsTests.mm */,
DAE7DEC31E24549F007505A6 /* MGLNSStringAdditionsTests.m */,
@@ -1789,6 +1792,7 @@
4031ACFC1E9EB3C100A3EA26 /* MGLMapViewDelegateIntegrationTests.swift in Sources */,
CA8FBC0D21A4A74300D1203C /* MGLRendererConfigurationTests.mm in Sources */,
4031AD031E9FD6AA00A3EA26 /* MGLSDKTestHelpers.swift in Sources */,
DAA4227C24294137008457DB /* MGLMapSnapshotterTests.m in Sources */,
DA87A9A71DCACC5000810D09 /* MGLBackgroundStyleLayerTests.mm in Sources */,
DAA999011E9F5EC5002E6EA6 /* MGLFillExtrusionStyleLayerTests.mm in Sources */,
DA29875A1E1A4290002299F5 /* MGLDocumentationExampleTests.swift in Sources */,