Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

Commit

Permalink
[ios, macos] Added snapshot test of locally rendered font
Browse files Browse the repository at this point in the history
Disabled on iOS for now due to actual differences in Core Text behavior. Disabled on macOS because the test is too strict without any tolerance.
  • Loading branch information
1ec5 committed Apr 25, 2020
1 parent 50d067f commit 288194c
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 8 deletions.
38 changes: 31 additions & 7 deletions platform/darwin/test/MGLMapSnapshotterTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ BOOL MGLEqualImages(MGLImage *leftImage, MGLImage *rightImage) {
#endif
}

@interface MGLMapSnapshotterTests : XCTestCase <MGLMapSnapshotterDelegate>
@interface MGLMapSnapshotterTests : XCTestCase <MGLMapSnapshotterDelegate, MGLOfflineStorageDelegate>

@property (nonatomic) XCTestExpectation *styleLoadingExpectation;
@property (nonatomic, copy, nullable) void (^runtimeStylingActions)(MGLStyle *style);
Expand All @@ -62,10 +62,13 @@ - (void)setUp {
[super setUp];

[MGLAccountManager setAccessToken:@"pk.feedcafedeadbeefbadebede"];

[MGLOfflineStorage sharedOfflineStorage].delegate = self;
}

- (void)tearDown {
[MGLAccountManager setAccessToken:nil];
[MGLOfflineStorage sharedOfflineStorage].delegate = nil;
self.styleLoadingExpectation = nil;
self.runtimeStylingActions = nil;
[super tearDown];
Expand Down Expand Up @@ -138,20 +141,26 @@ - (void)testDelegate {
}

- (void)testRuntimeStyling {
[self testRuntimeStylingActions:^(MGLStyle *style) {
[self testStyleURL:nil applyingRuntimeStylingActions:^(MGLStyle *style) {
MGLBackgroundStyleLayer *backgroundLayer = [[MGLBackgroundStyleLayer alloc] initWithIdentifier:@"background"];
backgroundLayer.backgroundColor = [NSExpression expressionForConstantValue:[MGLColor orangeColor]];
[style addLayer:backgroundLayer];
} expectedImageName:@"Fixtures/MGLMapSnapshotterTests/background"];
}

- (void)testLocalGlyphRendering {
[[NSUserDefaults standardUserDefaults] setObject:@[@"PingFang TC"] forKey:@"MGLIdeographicFontFamilyName"];
NSURL *styleURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"mixed" withExtension:@"json"];
[self testStyleURL:styleURL applyingRuntimeStylingActions:^(MGLStyle *style) {} expectedImageName:@"Fixtures/MGLMapSnapshotterTests/PingFang"];
}

/**
Tests that applying the given runtime styling actions on a blank style results in a snapshot image that matches the image with the given name in the asset catalog.
@param actions Runtime styling actions to apply to the blank style.
@param expectedImageName Name of the test fixture image in Media.xcassets.
*/
- (void)testRuntimeStylingActions:(void (^)(MGLStyle *style))actions expectedImageName:(NSString *)expectedImageName {
- (void)testStyleURL:(nullable NSURL *)styleURL applyingRuntimeStylingActions:(void (^)(MGLStyle *style))actions expectedImageName:(NSString *)expectedImageName {
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."];
Expand All @@ -163,9 +172,15 @@ - (void)testRuntimeStylingActions:(void (^)(MGLStyle *style))actions expectedIma
#endif
XCTAssertNotNil(expectedImage, @"Image fixture “%@” missing from Media.xcassets.", expectedImageName);

NSURL *styleURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"one-liner" withExtension:@"json"];
if (!styleURL) {
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)];
camera.centerCoordinate = kCLLocationCoordinate2DInvalid;
camera.heading = -1;
camera.pitch = -1;
MGLMapSnapshotOptions *options = [[MGLMapSnapshotOptions alloc] initWithStyleURL:styleURL camera:camera size:expectedImage.size];
options.zoomLevel = -1;

MGLMapSnapshotter *snapshotter = [[MGLMapSnapshotter alloc] initWithOptions:options];
snapshotter.delegate = self;
Expand All @@ -183,8 +198,8 @@ - (void)testRuntimeStylingActions:(void (^)(MGLStyle *style))actions expectedIma
XCTAssertNil(error);
XCTAssertNotNil(snapshot);
if (snapshot) {
XCTAssertEqual(snapshot.image.size.width, 500);
XCTAssertEqual(snapshot.image.size.height, 500);
XCTAssertEqual(snapshot.image.size.width, expectedImage.size.width);
XCTAssertEqual(snapshot.image.size.height, expectedImage.size.height);
}
[completionExpectation fulfill];
}];
Expand All @@ -205,4 +220,13 @@ - (void)mapSnapshotter:(MGLMapSnapshotter *)snapshotter didFinishLoadingStyle:(M
[self.styleLoadingExpectation fulfill];
}

#pragma mark MGLOfflineStorageDelegate methods

- (NSURL *)offlineStorage:(MGLOfflineStorage *)storage URLForResourceOfKind:(MGLResourceKind)kind withURL:(NSURL *)url {
if (kind == MGLResourceKindGlyphs && [url.scheme isEqualToString:@"local"]) {
return [[NSBundle bundleForClass:[self class]] URLForResource:@"glyphs" withExtension:@"pbf"];
}
return url;
}

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"images" : [
{
"filename" : "expected.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "actualImage.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion platform/darwin/test/one-liner.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":8,"sources":{},"layers":[]}
{"version":8,"sources":{},"layers":[],"center":[0,0],"zoom":0}
8 changes: 8 additions & 0 deletions platform/ios/ios.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,8 @@
DAC25FCD200FD83F009BE98E /* NSExpression+MGLPrivateAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = DAC25FCB200FD83E009BE98E /* NSExpression+MGLPrivateAdditions.h */; };
DAC49C5C1CD02BC9009E1AA3 /* Localizable.stringsdict in Resources */ = {isa = PBXBuildFile; fileRef = DAC49C5F1CD02BC9009E1AA3 /* Localizable.stringsdict */; };
DAC49C5D1CD02BC9009E1AA3 /* Localizable.stringsdict in Resources */ = {isa = PBXBuildFile; fileRef = DAC49C5F1CD02BC9009E1AA3 /* Localizable.stringsdict */; };
DAC8A48024527EDC00C795BB /* glyphs.pbf in Resources */ = {isa = PBXBuildFile; fileRef = DAC8A47F24527EDB00C795BB /* glyphs.pbf */; };
DAC8A4822452971B00C795BB /* mixed.json in Resources */ = {isa = PBXBuildFile; fileRef = DAC8A4812452971B00C795BB /* mixed.json */; };
DACA86262019218600E9693A /* MGLRasterDEMSource.h in Headers */ = {isa = PBXBuildFile; fileRef = DACA86242019218500E9693A /* MGLRasterDEMSource.h */; settings = {ATTRIBUTES = (Public, ); }; };
DACA86272019218600E9693A /* MGLRasterDEMSource.h in Headers */ = {isa = PBXBuildFile; fileRef = DACA86242019218500E9693A /* MGLRasterDEMSource.h */; settings = {ATTRIBUTES = (Public, ); }; };
DACA86282019218600E9693A /* MGLRasterDEMSource.mm in Sources */ = {isa = PBXBuildFile; fileRef = DACA86252019218500E9693A /* MGLRasterDEMSource.mm */; };
Expand Down Expand Up @@ -1281,6 +1283,8 @@
DAC49C621CD07D74009E1AA3 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.stringsdict; name = en; path = en.lproj/Localizable.stringsdict; sourceTree = "<group>"; };
DAC832642404AF9000A61BF8 /* darwin.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = darwin.xcconfig; path = ../darwin/darwin.xcconfig; sourceTree = "<group>"; };
DAC832672404AFBB00A61BF8 /* ci.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = ci.xcconfig; path = ../darwin/ci.xcconfig; sourceTree = "<group>"; };
DAC8A47F24527EDB00C795BB /* glyphs.pbf */ = {isa = PBXFileReference; lastKnownFileType = file; name = glyphs.pbf; path = "../../../vendor/mapbox-gl-native/test/fixtures/resources/glyphs.pbf"; sourceTree = "<group>"; };
DAC8A4812452971B00C795BB /* mixed.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = mixed.json; path = "../../../vendor/mapbox-gl-native/test/fixtures/local_glyphs/mixed.json"; sourceTree = "<group>"; };
DACA86242019218500E9693A /* MGLRasterDEMSource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MGLRasterDEMSource.h; sourceTree = "<group>"; };
DACA86252019218500E9693A /* MGLRasterDEMSource.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MGLRasterDEMSource.mm; sourceTree = "<group>"; };
DACBC60B20118ABE00C4D7E2 /* he */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = he; path = he.lproj/Localizable.strings; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1888,6 +1892,8 @@
DA2E88601CC0382C00F24E7B /* MGLStyleTests.mm */,
556660D71E1D085500E2C41B /* MGLVersionNumber.m */,
DA35D0871E1A6309007DED41 /* one-liner.json */,
DAC8A4812452971B00C795BB /* mixed.json */,
DAC8A47F24527EDB00C795BB /* glyphs.pbf */,
357579811D502AD4000B822E /* Styling */,
409F43FB1E9E77D10048729D /* Swift Integration */,
4031ACFD1E9FD26900A3EA26 /* Test Helpers */,
Expand Down Expand Up @@ -2858,6 +2864,8 @@
DA2784FC1DF02FF4001D5B8D /* Media.xcassets in Resources */,
353BAEF71D646370009A8DA9 /* amsterdam.geojson in Resources */,
DA35D0881E1A6309007DED41 /* one-liner.json in Resources */,
DAC8A4822452971B00C795BB /* mixed.json in Resources */,
DAC8A48024527EDC00C795BB /* glyphs.pbf in Resources */,
1F7839A6235E137000D4D606 /* sideload_sat.db in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
5 changes: 5 additions & 0 deletions platform/ios/ios.xcodeproj/xcshareddata/xcschemes/CI.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@
BlueprintName = "test"
ReferencedContainer = "container:ios.xcodeproj">
</BuildableReference>
<SkippedTests>
<Test
Identifier = "MGLMapSnapshotterTests/testLocalGlyphRendering">
</Test>
</SkippedTests>
</TestableReference>
</Testables>
</TestAction>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@
BlueprintName = "test"
ReferencedContainer = "container:ios.xcodeproj">
</BuildableReference>
<SkippedTests>
<Test
Identifier = "MGLMapSnapshotterTests/testLocalGlyphRendering">
</Test>
</SkippedTests>
</TestableReference>
</Testables>
</TestAction>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@
BlueprintName = "test"
ReferencedContainer = "container:ios.xcodeproj">
</BuildableReference>
<SkippedTests>
<Test
Identifier = "MGLMapSnapshotterTests/testLocalGlyphRendering">
</Test>
</SkippedTests>
</TestableReference>
</Testables>
</TestAction>
Expand Down
8 changes: 8 additions & 0 deletions platform/macos/macos.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@
DAC2ABC51CC6D343006D18C4 /* MGLAnnotationImage_Private.h in Headers */ = {isa = PBXBuildFile; fileRef = DAC2ABC41CC6D343006D18C4 /* MGLAnnotationImage_Private.h */; };
DAC8325F24049D8700A61BF8 /* libsqlite3.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = DAE6C3C61CC3499100DB3429 /* libsqlite3.tbd */; };
DAC8326024049D9C00A61BF8 /* libz.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 55D9B4B01D005D3900C1CCE2 /* libz.tbd */; };
DAC8A47E24527EBB00C795BB /* glyphs.pbf in Resources */ = {isa = PBXBuildFile; fileRef = DAC8A47D24527EBB00C795BB /* glyphs.pbf */; };
DAC8A4842452972F00C795BB /* mixed.json in Resources */ = {isa = PBXBuildFile; fileRef = DAC8A4832452972F00C795BB /* mixed.json */; };
DACA8622201920BE00E9693A /* MGLRasterDEMSource.h in Headers */ = {isa = PBXBuildFile; fileRef = DACA8620201920BE00E9693A /* MGLRasterDEMSource.h */; settings = {ATTRIBUTES = (Public, ); }; };
DACA8623201920BE00E9693A /* MGLRasterDEMSource.mm in Sources */ = {isa = PBXBuildFile; fileRef = DACA8621201920BE00E9693A /* MGLRasterDEMSource.mm */; };
DACB0C391E18DFFD005DDBEA /* MGLStyle+MBXAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = DACB0C381E18DFFD005DDBEA /* MGLStyle+MBXAdditions.m */; };
Expand Down Expand Up @@ -615,6 +617,8 @@
DAC832652404AF9E00A61BF8 /* Makefile */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.make; name = Makefile; path = ../../Makefile; sourceTree = "<group>"; };
DAC832662404AFB400A61BF8 /* ci.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = ci.xcconfig; path = ../darwin/ci.xcconfig; sourceTree = "<group>"; };
DAC832682404B26400A61BF8 /* jazzy.yml */ = {isa = PBXFileReference; lastKnownFileType = text.yaml; path = jazzy.yml; sourceTree = "<group>"; };
DAC8A47D24527EBB00C795BB /* glyphs.pbf */ = {isa = PBXFileReference; lastKnownFileType = file; name = glyphs.pbf; path = "../../../vendor/mapbox-gl-native/test/fixtures/resources/glyphs.pbf"; sourceTree = "<group>"; };
DAC8A4832452972F00C795BB /* mixed.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = mixed.json; path = "../../../vendor/mapbox-gl-native/test/fixtures/local_glyphs/mixed.json"; sourceTree = "<group>"; };
DACA8620201920BE00E9693A /* MGLRasterDEMSource.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MGLRasterDEMSource.h; sourceTree = "<group>"; };
DACA8621201920BE00E9693A /* MGLRasterDEMSource.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = MGLRasterDEMSource.mm; sourceTree = "<group>"; };
DACB0C371E18DFFD005DDBEA /* MGLStyle+MBXAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "MGLStyle+MBXAdditions.h"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1221,6 +1225,8 @@
DAE6C3CC1CC34BD800DB3429 /* MGLStyleTests.mm */,
556660D51E1D07E400E2C41B /* MGLVersionNumber.m */,
DA35D0891E1A631B007DED41 /* one-liner.json */,
DAC8A4832452972F00C795BB /* mixed.json */,
DAC8A47D24527EBB00C795BB /* glyphs.pbf */,
1F8A59F921653483004DFE75 /* sideload_sat.db */,
DA8F257D1D51C5F40010E6B5 /* Styling */,
4031ACFA1E9EB39A00A3EA26 /* Swift Integration */,
Expand Down Expand Up @@ -1643,6 +1649,8 @@
35724FC41D630502002A4AB4 /* amsterdam.geojson in Resources */,
DA2784FE1DF03060001D5B8D /* Media.xcassets in Resources */,
DA35D08A1E1A631B007DED41 /* one-liner.json in Resources */,
DAC8A4842452972F00C795BB /* mixed.json in Resources */,
DAC8A47E24527EBB00C795BB /* glyphs.pbf in Resources */,
1F8A59FA21653483004DFE75 /* sideload_sat.db in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@
BlueprintName = "test"
ReferencedContainer = "container:macos.xcodeproj">
</BuildableReference>
<SkippedTests>
<Test
Identifier = "MGLMapSnapshotterTests/testLocalGlyphRendering">
</Test>
</SkippedTests>
</TestableReference>
</Testables>
</TestAction>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@
BlueprintName = "test"
ReferencedContainer = "container:macos.xcodeproj">
</BuildableReference>
<SkippedTests>
<Test
Identifier = "MGLMapSnapshotterTests/testLocalGlyphRendering">
</Test>
</SkippedTests>
</TestableReference>
</Testables>
</TestAction>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
BlueprintName = "test"
ReferencedContainer = "container:macos.xcodeproj">
</BuildableReference>
<SkippedTests>
<Test
Identifier = "MGLMapSnapshotterTests/testLocalGlyphRendering">
</Test>
</SkippedTests>
</TestableReference>
</Testables>
</TestAction>
Expand Down

0 comments on commit 288194c

Please sign in to comment.