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

Commit a68aa78

Browse files
committed
[ios, macos] Add style's text localization tests.
1 parent cb4dfd9 commit a68aa78

File tree

5 files changed

+103
-13
lines changed

5 files changed

+103
-13
lines changed

platform/darwin/test/MGLStyleTests.mm

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ - (void)setUp {
2828
[super setUp];
2929

3030
[MGLAccountManager setAccessToken:@"pk.feedcafedeadbeefbadebede"];
31-
NSURL *styleURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"one-liner" withExtension:@"json"];
31+
NSURL *styleURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"basic-style" withExtension:@"json"];
3232
self.mapView = [[MGLMapView alloc] initWithFrame:CGRectMake(0, 0, 100, 100) styleURL:styleURL];
3333
self.mapView.delegate = self;
3434
if (!self.mapView.style) {
@@ -140,11 +140,8 @@ - (void)testName {
140140

141141
- (void)testSources {
142142
NSSet<MGLSource *> *initialSources = self.style.sources;
143-
if ([initialSources.anyObject.identifier isEqualToString:@"com.mapbox.annotations"]) {
144-
XCTAssertEqual(self.style.sources.count, 1UL);
145-
} else {
146-
XCTAssertEqual(self.style.sources.count, 0UL);
147-
}
143+
XCTAssertTrue(initialSources.count <= 2);
144+
148145
MGLShapeSource *shapeSource = [[MGLShapeSource alloc] initWithIdentifier:@"shapeSource" shape:nil options:nil];
149146
[self.style addSource:shapeSource];
150147
XCTAssertEqual(self.style.sources.count, initialSources.count + 1);
@@ -250,11 +247,9 @@ - (void)testRemovingSourceInUse {
250247

251248
- (void)testLayers {
252249
NSArray<MGLStyleLayer *> *initialLayers = self.style.layers;
253-
if ([initialLayers.firstObject.identifier isEqualToString:@"com.mapbox.annotations.points"]) {
254-
XCTAssertEqual(self.style.layers.count, 1UL);
255-
} else {
256-
XCTAssertEqual(self.style.layers.count, 0UL);
257-
}
250+
251+
XCTAssertTrue(initialLayers.count <= 2);
252+
258253
MGLShapeSource *shapeSource = [[MGLShapeSource alloc] initWithIdentifier:@"shapeSource" shape:nil options:nil];
259254
[self.style addSource:shapeSource];
260255
MGLFillStyleLayer *fillLayer = [[MGLFillStyleLayer alloc] initWithIdentifier:@"fillLayer" source:shapeSource];
@@ -396,6 +391,8 @@ - (void)testLayersOrder {
396391
NSURL *url = [NSURL fileURLWithPath:filePath];
397392
MGLShapeSource *source = [[MGLShapeSource alloc] initWithIdentifier:@"sourceID" URL:url options:nil];
398393
[self.style addSource:source];
394+
395+
NSUInteger startIndex = self.style.layers.count;
399396

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

415412
NSArray<MGLStyleLayer *> *layers = [self.style layers];
416-
NSUInteger startIndex = 0;
413+
417414
if ([layers.firstObject.identifier isEqualToString:@"com.mapbox.annotations.points"]) {
418415
startIndex++;
419416
}
@@ -427,6 +424,36 @@ - (void)testLayersOrder {
427424

428425
#pragma mark Localization tests
429426

427+
- (void)testLocalization {
428+
MGLSymbolStyleLayer *countryLabel = (MGLSymbolStyleLayer *)[self.style layerWithIdentifier:@"country-label"];
429+
{
430+
NSLocale *locale = [NSLocale localeWithLocaleIdentifier:@"de_DE"];
431+
[self.style localizeLabelsIntoLocale:locale];
432+
433+
NSArray * keypathArray = @[ [NSExpression expressionForKeyPath:@"name_de"],
434+
[NSExpression expressionForKeyPath:@"name"]];
435+
NSExpression *coalesceExpression = [NSExpression expressionWithFormat:@"mgl_coalesce:(%@)", @[ [NSExpression expressionWithFormat:@"mgl_coalesce:(%@)", keypathArray],
436+
[NSExpression expressionWithFormat:@"mgl_coalesce:(%@)", keypathArray] ]];
437+
MGLAttributedExpression *attributedExpression = [MGLAttributedExpression attributedExpression:coalesceExpression attributes:@{}];
438+
NSExpression *localizedExpression = [NSExpression mgl_expressionForAttributedExpressions:@[ [NSExpression expressionForConstantValue:attributedExpression] ]];
439+
XCTAssertEqualObjects(countryLabel.text, localizedExpression);
440+
countryLabel.text = [NSExpression expressionWithFormat:@"mgl_coalesce({%K, %K})", @"name_en", @"name"];
441+
}
442+
{
443+
NSLocale *locale = [NSLocale localeWithLocaleIdentifier:@"es"];
444+
[self.style localizeLabelsIntoLocale:locale];
445+
446+
NSArray * keypathArray = @[ [NSExpression expressionForKeyPath:@"name_es"],
447+
[NSExpression expressionForKeyPath:@"name"]];
448+
NSExpression *coalesceExpression = [NSExpression expressionWithFormat:@"mgl_coalesce:(%@)", @[ [NSExpression expressionWithFormat:@"mgl_coalesce:(%@)", keypathArray],
449+
[NSExpression expressionWithFormat:@"mgl_coalesce:(%@)", keypathArray] ]];
450+
MGLAttributedExpression *attributedExpression = [MGLAttributedExpression attributedExpression:coalesceExpression attributes:@{}];
451+
NSExpression *localizedExpression = [NSExpression mgl_expressionForAttributedExpressions:@[ [NSExpression expressionForConstantValue:attributedExpression] ]];
452+
XCTAssertEqualObjects(countryLabel.text, localizedExpression);
453+
countryLabel.text = [NSExpression expressionWithFormat:@"mgl_coalesce({%K, %K})", @"name_en", @"name"];
454+
}
455+
}
456+
430457
- (void)testLanguageMatching {
431458
{
432459
NSArray *preferences = @[@"en"];
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"version": 8,
3+
"sources": {
4+
"composite": {
5+
"url": "mapbox://mapbox.mapbox-streets-v8",
6+
"type": "vector"
7+
}
8+
},
9+
"layers": [
10+
{
11+
"id": "country-label",
12+
"type": "symbol",
13+
"source": "composite",
14+
"source-layer": "place_label",
15+
"minzoom": 3,
16+
"maxzoom": 8,
17+
"filter": ["==", ["get", "type"], "country"],
18+
"layout": {
19+
"text-field": ["coalesce", ["get", "name_en"], ["get", "name"]],
20+
"text-max-width": [
21+
"interpolate",
22+
["linear"],
23+
["zoom"],
24+
0,
25+
5,
26+
3,
27+
6
28+
],
29+
"text-font": [
30+
"step",
31+
["zoom"],
32+
["literal", ["Roboto Medium", "Arial Unicode MS Regular"]],
33+
4,
34+
["literal", ["Roboto Bold", "Arial Unicode MS Bold"]]
35+
],
36+
"text-size": [
37+
"interpolate",
38+
["linear"],
39+
["zoom"],
40+
1,
41+
["step", ["get", "symbolrank"], 12, 3, 10, 5, 9],
42+
9,
43+
["step", ["get", "symbolrank"], 35, 3, 27, 5, 22]
44+
]
45+
},
46+
"paint": {
47+
"text-halo-width": 1.5,
48+
"text-halo-color": "hsla(0, 0%, 100%, 0.95)",
49+
"text-color": "hsl(0, 0%, 0%)"
50+
}
51+
}
52+
]
53+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"version":8,"sources":{},"layers":[]}
1+
{"version":8,"sources":{},"layers":[]}

platform/ios/ios.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
1FF48588223710BE00F19727 /* MGLAttributedExpression.h in Headers */ = {isa = PBXBuildFile; fileRef = 1FF48585223710BE00F19727 /* MGLAttributedExpression.h */; settings = {ATTRIBUTES = (Public, ); }; };
7070
1FF48589223710BE00F19727 /* MGLAttributedExpression.m in Sources */ = {isa = PBXBuildFile; fileRef = 1FF48586223710BE00F19727 /* MGLAttributedExpression.m */; };
7171
1FF4858A223710BE00F19727 /* MGLAttributedExpression.m in Sources */ = {isa = PBXBuildFile; fileRef = 1FF48586223710BE00F19727 /* MGLAttributedExpression.m */; };
72+
1FFAEDB822650A23009A1B2A /* basic-style.json in Resources */ = {isa = PBXBuildFile; fileRef = 1FFAEDB722650A23009A1B2A /* basic-style.json */; };
73+
1FFAEDB922650A23009A1B2A /* basic-style.json in Resources */ = {isa = PBXBuildFile; fileRef = 1FFAEDB722650A23009A1B2A /* basic-style.json */; };
7274
30E578171DAA85520050F07E /* UIImage+MGLAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = 30E578111DAA7D690050F07E /* UIImage+MGLAdditions.h */; };
7375
30E578181DAA85520050F07E /* UIImage+MGLAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = 30E578111DAA7D690050F07E /* UIImage+MGLAdditions.h */; };
7476
30E578191DAA855E0050F07E /* UIImage+MGLAdditions.mm in Sources */ = {isa = PBXBuildFile; fileRef = 30E578121DAA7D690050F07E /* UIImage+MGLAdditions.mm */; };
@@ -856,6 +858,7 @@
856858
1FDB00CB21F8F15300D21389 /* cs */ = {isa = PBXFileReference; lastKnownFileType = text.plist.stringsdict; name = cs; path = cs.lproj/Localizable.stringsdict; sourceTree = "<group>"; };
857859
1FF48585223710BE00F19727 /* MGLAttributedExpression.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MGLAttributedExpression.h; sourceTree = "<group>"; };
858860
1FF48586223710BE00F19727 /* MGLAttributedExpression.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MGLAttributedExpression.m; sourceTree = "<group>"; };
861+
1FFAEDB722650A23009A1B2A /* basic-style.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = "basic-style.json"; path = "../../darwin/test/basic-style.json"; sourceTree = "<group>"; };
859862
20DABE861DF78148007AC5FF /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/Foundation.strings"; sourceTree = "<group>"; };
860863
20DABE881DF78148007AC5FF /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/Localizable.strings"; sourceTree = "<group>"; };
861864
30E578111DAA7D690050F07E /* UIImage+MGLAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "UIImage+MGLAdditions.h"; path = "src/UIImage+MGLAdditions.h"; sourceTree = SOURCE_ROOT; };
@@ -1925,6 +1928,7 @@
19251928
DA2E88551CC036F400F24E7B /* Info.plist */,
19261929
DA2784FB1DF02FF4001D5B8D /* Media.xcassets */,
19271930
DA35D0871E1A6309007DED41 /* one-liner.json */,
1931+
1FFAEDB722650A23009A1B2A /* basic-style.json */,
19281932
1F8A59F62165326C004DFE75 /* sideload_sat.db */,
19291933
);
19301934
name = "SDK Tests";
@@ -2852,6 +2856,7 @@
28522856
isa = PBXResourcesBuildPhase;
28532857
buildActionMask = 2147483647;
28542858
files = (
2859+
1FFAEDB922650A23009A1B2A /* basic-style.json in Resources */,
28552860
16376B471FFDB92B0000563E /* one-liner.json in Resources */,
28562861
1F8A59F821653275004DFE75 /* sideload_sat.db in Resources */,
28572862
);
@@ -2895,6 +2900,7 @@
28952900
1F8A59F72165326D004DFE75 /* sideload_sat.db in Resources */,
28962901
353BAEF71D646370009A8DA9 /* amsterdam.geojson in Resources */,
28972902
DA35D0881E1A6309007DED41 /* one-liner.json in Resources */,
2903+
1FFAEDB822650A23009A1B2A /* basic-style.json in Resources */,
28982904
);
28992905
runOnlyForDeploymentPostprocessing = 0;
29002906
};

platform/macos/macos.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
1FCCEC55222EF9FE00302E3B /* MGLSDKMetricsManager.h in Headers */ = {isa = PBXBuildFile; fileRef = 1FCCEC52222EF9FE00302E3B /* MGLSDKMetricsManager.h */; settings = {ATTRIBUTES = (Public, ); }; };
3636
1FF4858D2237235300F19727 /* MGLAttributedExpression.m in Sources */ = {isa = PBXBuildFile; fileRef = 1FF4858B2237235200F19727 /* MGLAttributedExpression.m */; };
3737
1FF4858E2237235300F19727 /* MGLAttributedExpression.h in Headers */ = {isa = PBXBuildFile; fileRef = 1FF4858C2237235200F19727 /* MGLAttributedExpression.h */; settings = {ATTRIBUTES = (Public, ); }; };
38+
1FFAEDBB22650A7E009A1B2A /* basic-style.json in Resources */ = {isa = PBXBuildFile; fileRef = 1FFAEDBA22650A7D009A1B2A /* basic-style.json */; };
3839
3508EC641D749D39009B0EE4 /* NSExpression+MGLAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = 3508EC621D749D39009B0EE4 /* NSExpression+MGLAdditions.h */; settings = {ATTRIBUTES = (Public, ); }; };
3940
3508EC651D749D39009B0EE4 /* NSExpression+MGLAdditions.mm in Sources */ = {isa = PBXBuildFile; fileRef = 3508EC631D749D39009B0EE4 /* NSExpression+MGLAdditions.mm */; };
4041
3526EABD1DF9B19800006B43 /* MGLCodingTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 3526EABC1DF9B19800006B43 /* MGLCodingTests.mm */; };
@@ -355,6 +356,7 @@
355356
1FDB00CD21F8F1FF00D21389 /* cs */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = cs; path = cs.lproj/Localizable.strings; sourceTree = "<group>"; };
356357
1FF4858B2237235200F19727 /* MGLAttributedExpression.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MGLAttributedExpression.m; sourceTree = "<group>"; };
357358
1FF4858C2237235200F19727 /* MGLAttributedExpression.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MGLAttributedExpression.h; sourceTree = "<group>"; };
359+
1FFAEDBA22650A7D009A1B2A /* basic-style.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = "basic-style.json"; path = "../../darwin/test/basic-style.json"; sourceTree = "<group>"; };
358360
3508EC621D749D39009B0EE4 /* NSExpression+MGLAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSExpression+MGLAdditions.h"; sourceTree = "<group>"; };
359361
3508EC631D749D39009B0EE4 /* NSExpression+MGLAdditions.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = "NSExpression+MGLAdditions.mm"; sourceTree = "<group>"; };
360362
3526EABC1DF9B19800006B43 /* MGLCodingTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = MGLCodingTests.mm; path = ../../darwin/test/MGLCodingTests.mm; sourceTree = "<group>"; };
@@ -1196,6 +1198,7 @@
11961198
DAE6C33A1CC30DB200DB3429 /* Info.plist */,
11971199
DA2784FD1DF03060001D5B8D /* Media.xcassets */,
11981200
DA35D0891E1A631B007DED41 /* one-liner.json */,
1201+
1FFAEDBA22650A7D009A1B2A /* basic-style.json */,
11991202
1F8A59F921653483004DFE75 /* sideload_sat.db */,
12001203
);
12011204
name = "SDK Tests";
@@ -1603,6 +1606,7 @@
16031606
DA2784FE1DF03060001D5B8D /* Media.xcassets in Resources */,
16041607
DA35D08A1E1A631B007DED41 /* one-liner.json in Resources */,
16051608
1F8A59FA21653483004DFE75 /* sideload_sat.db in Resources */,
1609+
1FFAEDBB22650A7E009A1B2A /* basic-style.json in Resources */,
16061610
);
16071611
runOnlyForDeploymentPostprocessing = 0;
16081612
};

0 commit comments

Comments
 (0)