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

[ios, macos] Add style's text localization tests. #14430

Open
wants to merge 1 commit into
base: fabian-localize-14393
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 39 additions & 12 deletions platform/darwin/test/MGLStyleTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ - (void)setUp {
[super setUp];

[MGLAccountManager setAccessToken:@"pk.feedcafedeadbeefbadebede"];
NSURL *styleURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"one-liner" withExtension:@"json"];
NSURL *styleURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"basic-style" withExtension:@"json"];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unlike one-liner.json, basic-style.json seems to have network dependencies. We’d need to mock those dependencies to ensure stable tests.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One possibility - we could (for the time being) convert this to an integration test, which do hit the network...

self.mapView = [[MGLMapView alloc] initWithFrame:CGRectMake(0, 0, 100, 100) styleURL:styleURL];
self.mapView.delegate = self;
if (!self.mapView.style) {
Expand Down Expand Up @@ -140,11 +140,8 @@ - (void)testName {

- (void)testSources {
NSSet<MGLSource *> *initialSources = self.style.sources;
if ([initialSources.anyObject.identifier isEqualToString:@"com.mapbox.annotations"]) {
XCTAssertEqual(self.style.sources.count, 1UL);
} else {
XCTAssertEqual(self.style.sources.count, 0UL);
}
XCTAssertTrue(initialSources.count <= 2);

MGLShapeSource *shapeSource = [[MGLShapeSource alloc] initWithIdentifier:@"shapeSource" shape:nil options:nil];
[self.style addSource:shapeSource];
XCTAssertEqual(self.style.sources.count, initialSources.count + 1);
Expand Down Expand Up @@ -250,11 +247,9 @@ - (void)testRemovingSourceInUse {

- (void)testLayers {
NSArray<MGLStyleLayer *> *initialLayers = self.style.layers;
if ([initialLayers.firstObject.identifier isEqualToString:@"com.mapbox.annotations.points"]) {
XCTAssertEqual(self.style.layers.count, 1UL);
} else {
XCTAssertEqual(self.style.layers.count, 0UL);
}

XCTAssertTrue(initialLayers.count <= 2);

MGLShapeSource *shapeSource = [[MGLShapeSource alloc] initWithIdentifier:@"shapeSource" shape:nil options:nil];
[self.style addSource:shapeSource];
MGLFillStyleLayer *fillLayer = [[MGLFillStyleLayer alloc] initWithIdentifier:@"fillLayer" source:shapeSource];
Expand Down Expand Up @@ -396,6 +391,8 @@ - (void)testLayersOrder {
NSURL *url = [NSURL fileURLWithPath:filePath];
MGLShapeSource *source = [[MGLShapeSource alloc] initWithIdentifier:@"sourceID" URL:url options:nil];
[self.style addSource:source];

NSUInteger startIndex = self.style.layers.count;

MGLCircleStyleLayer *layer1 = [[MGLCircleStyleLayer alloc] initWithIdentifier:@"layer1" source:source];
[self.style addLayer:layer1];
Expand All @@ -413,7 +410,7 @@ - (void)testLayersOrder {
[self.style insertLayer:layer0 belowLayer:layer1];

NSArray<MGLStyleLayer *> *layers = [self.style layers];
NSUInteger startIndex = 0;

if ([layers.firstObject.identifier isEqualToString:@"com.mapbox.annotations.points"]) {
startIndex++;
}
Expand All @@ -427,6 +424,36 @@ - (void)testLayersOrder {

#pragma mark Localization tests

- (void)testLocalization {
MGLSymbolStyleLayer *countryLabel = (MGLSymbolStyleLayer *)[self.style layerWithIdentifier:@"country-label"];
{
NSLocale *locale = [NSLocale localeWithLocaleIdentifier:@"de_DE"];
[self.style localizeLabelsIntoLocale:locale];

NSArray * keypathArray = @[ [NSExpression expressionForKeyPath:@"name_de"],
[NSExpression expressionForKeyPath:@"name"]];
NSExpression *coalesceExpression = [NSExpression expressionWithFormat:@"mgl_coalesce:(%@)", @[ [NSExpression expressionWithFormat:@"mgl_coalesce:(%@)", keypathArray],
[NSExpression expressionWithFormat:@"mgl_coalesce:(%@)", keypathArray] ]];
MGLAttributedExpression *attributedExpression = [MGLAttributedExpression attributedExpression:coalesceExpression attributes:@{}];
NSExpression *localizedExpression = [NSExpression mgl_expressionForAttributedExpressions:@[ [NSExpression expressionForConstantValue:attributedExpression] ]];
XCTAssertEqualObjects(countryLabel.text, localizedExpression);
countryLabel.text = [NSExpression expressionWithFormat:@"mgl_coalesce({%K, %K})", @"name_en", @"name"];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it’s more important to test that -[NSExpression mgl_expressionLocalizedIntoLocale:] returns the right value than to test the full integration with -[MGLStyle localizeLabelsIntoLocale:], especially considering the difficulty of removing network dependencies.

}
{
NSLocale *locale = [NSLocale localeWithLocaleIdentifier:@"es"];
[self.style localizeLabelsIntoLocale:locale];

NSArray * keypathArray = @[ [NSExpression expressionForKeyPath:@"name_es"],
[NSExpression expressionForKeyPath:@"name"]];
NSExpression *coalesceExpression = [NSExpression expressionWithFormat:@"mgl_coalesce:(%@)", @[ [NSExpression expressionWithFormat:@"mgl_coalesce:(%@)", keypathArray],
[NSExpression expressionWithFormat:@"mgl_coalesce:(%@)", keypathArray] ]];
MGLAttributedExpression *attributedExpression = [MGLAttributedExpression attributedExpression:coalesceExpression attributes:@{}];
NSExpression *localizedExpression = [NSExpression mgl_expressionForAttributedExpressions:@[ [NSExpression expressionForConstantValue:attributedExpression] ]];
XCTAssertEqualObjects(countryLabel.text, localizedExpression);
countryLabel.text = [NSExpression expressionWithFormat:@"mgl_coalesce({%K, %K})", @"name_en", @"name"];
}
}

- (void)testLanguageMatching {
{
NSArray *preferences = @[@"en"];
Expand Down
53 changes: 53 additions & 0 deletions platform/darwin/test/basic-style.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"version": 8,
"sources": {
"composite": {
"url": "mapbox://mapbox.mapbox-streets-v8",
"type": "vector"
}
},
"layers": [
{
"id": "country-label",
"type": "symbol",
"source": "composite",
"source-layer": "place_label",
"minzoom": 3,
"maxzoom": 8,
"filter": ["==", ["get", "type"], "country"],
"layout": {
"text-field": ["coalesce", ["get", "name_en"], ["get", "name"]],
"text-max-width": [
"interpolate",
["linear"],
["zoom"],
0,
5,
3,
6
],
"text-font": [
"step",
["zoom"],
["literal", ["Roboto Medium", "Arial Unicode MS Regular"]],
4,
["literal", ["Roboto Bold", "Arial Unicode MS Bold"]]
],
"text-size": [
"interpolate",
["linear"],
["zoom"],
1,
["step", ["get", "symbolrank"], 12, 3, 10, 5, 9],
9,
["step", ["get", "symbolrank"], 35, 3, 27, 5, 22]
]
},
"paint": {
"text-halo-width": 1.5,
"text-halo-color": "hsla(0, 0%, 100%, 0.95)",
"text-color": "hsl(0, 0%, 0%)"
}
}
]
}
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":[]}
6 changes: 6 additions & 0 deletions platform/ios/ios.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@
1FF48588223710BE00F19727 /* MGLAttributedExpression.h in Headers */ = {isa = PBXBuildFile; fileRef = 1FF48585223710BE00F19727 /* MGLAttributedExpression.h */; settings = {ATTRIBUTES = (Public, ); }; };
1FF48589223710BE00F19727 /* MGLAttributedExpression.m in Sources */ = {isa = PBXBuildFile; fileRef = 1FF48586223710BE00F19727 /* MGLAttributedExpression.m */; };
1FF4858A223710BE00F19727 /* MGLAttributedExpression.m in Sources */ = {isa = PBXBuildFile; fileRef = 1FF48586223710BE00F19727 /* MGLAttributedExpression.m */; };
1FFAEDB822650A23009A1B2A /* basic-style.json in Resources */ = {isa = PBXBuildFile; fileRef = 1FFAEDB722650A23009A1B2A /* basic-style.json */; };
1FFAEDB922650A23009A1B2A /* basic-style.json in Resources */ = {isa = PBXBuildFile; fileRef = 1FFAEDB722650A23009A1B2A /* basic-style.json */; };
30E578171DAA85520050F07E /* UIImage+MGLAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = 30E578111DAA7D690050F07E /* UIImage+MGLAdditions.h */; };
30E578181DAA85520050F07E /* UIImage+MGLAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = 30E578111DAA7D690050F07E /* UIImage+MGLAdditions.h */; };
30E578191DAA855E0050F07E /* UIImage+MGLAdditions.mm in Sources */ = {isa = PBXBuildFile; fileRef = 30E578121DAA7D690050F07E /* UIImage+MGLAdditions.mm */; };
Expand Down Expand Up @@ -856,6 +858,7 @@
1FDB00CB21F8F15300D21389 /* cs */ = {isa = PBXFileReference; lastKnownFileType = text.plist.stringsdict; name = cs; path = cs.lproj/Localizable.stringsdict; sourceTree = "<group>"; };
1FF48585223710BE00F19727 /* MGLAttributedExpression.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MGLAttributedExpression.h; sourceTree = "<group>"; };
1FF48586223710BE00F19727 /* MGLAttributedExpression.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MGLAttributedExpression.m; sourceTree = "<group>"; };
1FFAEDB722650A23009A1B2A /* basic-style.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = "basic-style.json"; path = "../../darwin/test/basic-style.json"; sourceTree = "<group>"; };
20DABE861DF78148007AC5FF /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/Foundation.strings"; sourceTree = "<group>"; };
20DABE881DF78148007AC5FF /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/Localizable.strings"; sourceTree = "<group>"; };
30E578111DAA7D690050F07E /* UIImage+MGLAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "UIImage+MGLAdditions.h"; path = "src/UIImage+MGLAdditions.h"; sourceTree = SOURCE_ROOT; };
Expand Down Expand Up @@ -1925,6 +1928,7 @@
DA2E88551CC036F400F24E7B /* Info.plist */,
DA2784FB1DF02FF4001D5B8D /* Media.xcassets */,
DA35D0871E1A6309007DED41 /* one-liner.json */,
1FFAEDB722650A23009A1B2A /* basic-style.json */,
1F8A59F62165326C004DFE75 /* sideload_sat.db */,
);
name = "SDK Tests";
Expand Down Expand Up @@ -2852,6 +2856,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
1FFAEDB922650A23009A1B2A /* basic-style.json in Resources */,
16376B471FFDB92B0000563E /* one-liner.json in Resources */,
1F8A59F821653275004DFE75 /* sideload_sat.db in Resources */,
);
Expand Down Expand Up @@ -2895,6 +2900,7 @@
1F8A59F72165326D004DFE75 /* sideload_sat.db in Resources */,
353BAEF71D646370009A8DA9 /* amsterdam.geojson in Resources */,
DA35D0881E1A6309007DED41 /* one-liner.json in Resources */,
1FFAEDB822650A23009A1B2A /* basic-style.json in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
4 changes: 4 additions & 0 deletions platform/macos/macos.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
1FCCEC55222EF9FE00302E3B /* MGLSDKMetricsManager.h in Headers */ = {isa = PBXBuildFile; fileRef = 1FCCEC52222EF9FE00302E3B /* MGLSDKMetricsManager.h */; settings = {ATTRIBUTES = (Public, ); }; };
1FF4858D2237235300F19727 /* MGLAttributedExpression.m in Sources */ = {isa = PBXBuildFile; fileRef = 1FF4858B2237235200F19727 /* MGLAttributedExpression.m */; };
1FF4858E2237235300F19727 /* MGLAttributedExpression.h in Headers */ = {isa = PBXBuildFile; fileRef = 1FF4858C2237235200F19727 /* MGLAttributedExpression.h */; settings = {ATTRIBUTES = (Public, ); }; };
1FFAEDBB22650A7E009A1B2A /* basic-style.json in Resources */ = {isa = PBXBuildFile; fileRef = 1FFAEDBA22650A7D009A1B2A /* basic-style.json */; };
3508EC641D749D39009B0EE4 /* NSExpression+MGLAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = 3508EC621D749D39009B0EE4 /* NSExpression+MGLAdditions.h */; settings = {ATTRIBUTES = (Public, ); }; };
3508EC651D749D39009B0EE4 /* NSExpression+MGLAdditions.mm in Sources */ = {isa = PBXBuildFile; fileRef = 3508EC631D749D39009B0EE4 /* NSExpression+MGLAdditions.mm */; };
3526EABD1DF9B19800006B43 /* MGLCodingTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 3526EABC1DF9B19800006B43 /* MGLCodingTests.mm */; };
Expand Down Expand Up @@ -355,6 +356,7 @@
1FDB00CD21F8F1FF00D21389 /* cs */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = cs; path = cs.lproj/Localizable.strings; sourceTree = "<group>"; };
1FF4858B2237235200F19727 /* MGLAttributedExpression.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MGLAttributedExpression.m; sourceTree = "<group>"; };
1FF4858C2237235200F19727 /* MGLAttributedExpression.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MGLAttributedExpression.h; sourceTree = "<group>"; };
1FFAEDBA22650A7D009A1B2A /* basic-style.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = "basic-style.json"; path = "../../darwin/test/basic-style.json"; sourceTree = "<group>"; };
3508EC621D749D39009B0EE4 /* NSExpression+MGLAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSExpression+MGLAdditions.h"; sourceTree = "<group>"; };
3508EC631D749D39009B0EE4 /* NSExpression+MGLAdditions.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = "NSExpression+MGLAdditions.mm"; sourceTree = "<group>"; };
3526EABC1DF9B19800006B43 /* MGLCodingTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = MGLCodingTests.mm; path = ../../darwin/test/MGLCodingTests.mm; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1196,6 +1198,7 @@
DAE6C33A1CC30DB200DB3429 /* Info.plist */,
DA2784FD1DF03060001D5B8D /* Media.xcassets */,
DA35D0891E1A631B007DED41 /* one-liner.json */,
1FFAEDBA22650A7D009A1B2A /* basic-style.json */,
1F8A59F921653483004DFE75 /* sideload_sat.db */,
);
name = "SDK Tests";
Expand Down Expand Up @@ -1603,6 +1606,7 @@
DA2784FE1DF03060001D5B8D /* Media.xcassets in Resources */,
DA35D08A1E1A631B007DED41 /* one-liner.json in Resources */,
1F8A59FA21653483004DFE75 /* sideload_sat.db in Resources */,
1FFAEDBB22650A7E009A1B2A /* basic-style.json in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down