From ecf9a141a017217928ecebc38bbc5bf6ecb45876 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20Nguye=CC=82=CC=83n?= Date: Mon, 23 Mar 2020 16:55:01 -0700 Subject: [PATCH] [ios, macos] Unit-test snapshotter --- platform/darwin/test/MGLMapSnapshotterTests.m | 112 ++++++++++++++++++ platform/ios/ios.xcodeproj/project.pbxproj | 4 + .../macos/macos.xcodeproj/project.pbxproj | 4 + 3 files changed, 120 insertions(+) create mode 100644 platform/darwin/test/MGLMapSnapshotterTests.m diff --git a/platform/darwin/test/MGLMapSnapshotterTests.m b/platform/darwin/test/MGLMapSnapshotterTests.m new file mode 100644 index 000000000..a692b95c4 --- /dev/null +++ b/platform/darwin/test/MGLMapSnapshotterTests.m @@ -0,0 +1,112 @@ +#import +#import + +@interface MGLMapSnapshotterTests : XCTestCase + +@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 diff --git a/platform/ios/ios.xcodeproj/project.pbxproj b/platform/ios/ios.xcodeproj/project.pbxproj index 7c7381fb7..4fd3aa517 100644 --- a/platform/ios/ios.xcodeproj/project.pbxproj +++ b/platform/ios/ios.xcodeproj/project.pbxproj @@ -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 = ""; }; DAA32CBC1E4C4F5D006F8D24 /* vi */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = vi; path = vi.lproj/Localizable.strings; sourceTree = ""; }; DAA32CBD1E4C4F62006F8D24 /* vi */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = vi; path = vi.lproj/Foundation.strings; sourceTree = ""; }; + DAA4227D2429771D008457DB /* MGLMapSnapshotterTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = MGLMapSnapshotterTests.m; path = ../../darwin/test/MGLMapSnapshotterTests.m; sourceTree = ""; }; 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 = ""; }; DAAF72291DA903C700312FA4 /* MGLStyleValue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MGLStyleValue.h; sourceTree = ""; }; @@ -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 */, diff --git a/platform/macos/macos.xcodeproj/project.pbxproj b/platform/macos/macos.xcodeproj/project.pbxproj index 03e18743e..18b5d8a8a 100644 --- a/platform/macos/macos.xcodeproj/project.pbxproj +++ b/platform/macos/macos.xcodeproj/project.pbxproj @@ -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 = ""; }; DAA32CC01E4C4F89006F8D24 /* vi */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = vi; path = vi.lproj/Foundation.strings; sourceTree = ""; }; DAA32CC11E4C4F93006F8D24 /* vi */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.strings; name = vi; path = vi.lproj/Localizable.strings; sourceTree = ""; }; + DAA4227B24294137008457DB /* MGLMapSnapshotterTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = MGLMapSnapshotterTests.m; path = ../../darwin/test/MGLMapSnapshotterTests.m; sourceTree = ""; }; DAA48EFB1D6A4731006A7E36 /* StyleLayerIconTransformer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StyleLayerIconTransformer.h; sourceTree = ""; }; DAA48EFC1D6A4731006A7E36 /* StyleLayerIconTransformer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = StyleLayerIconTransformer.m; sourceTree = ""; }; DAA998F91E9F545C002E6EA6 /* MGLFillExtrusionStyleLayer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MGLFillExtrusionStyleLayer.h; sourceTree = ""; }; @@ -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 */,