-
Notifications
You must be signed in to change notification settings - Fork 325
Expand file tree
/
Copy pathMapViewTests.swift
More file actions
327 lines (268 loc) · 14.1 KB
/
Copy pathMapViewTests.swift
File metadata and controls
327 lines (268 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import XCTest
import MapboxMaps
@testable import MapboxNavigation
import TestHelper
class MapViewTests: TestCase {
override func setUp() {
super.setUp()
}
override func tearDown() {
super.tearDown()
}
func testMapViewTileSetAndSourceIdentifiers() {
let resourceOptions = ResourceOptions(accessToken: "")
let mapInitOptions = MapInitOptions(resourceOptions: resourceOptions)
let mapView = MapView(frame: UIScreen.main.bounds, mapInitOptions: mapInitOptions)
let styleJSONObject: [String: Any] = [
"version": 8,
"center": [
-122.385563, 37.763330
],
"zoom": 15,
"sources": [
"composite": [
"url": "mapbox://mapbox.mapbox-streets-v8,mapbox.mapbox-terrain-v2",
"type": "vector"
],
"custom": [
"url": "http://api.example.com/tilejson.json",
"type": "raster"
]
],
"layers": []
]
let styleJSON: String = ValueConverter.toJson(forValue: styleJSONObject)
XCTAssertFalse(styleJSON.isEmpty, "ValueConverter should create valid JSON string.")
let mapLoadingErrorExpectation = expectation(description: "Map loading error expectation")
mapLoadingErrorExpectation.assertForOverFulfill = false
mapView.mapboxMap.onNext(.mapLoadingError, handler: { event in
mapLoadingErrorExpectation.fulfill()
})
mapView.mapboxMap.loadStyleJSON(styleJSON)
wait(for: [mapLoadingErrorExpectation], timeout: 1.0)
let tileSetIdentifiers = mapView.tileSetIdentifiers("composite")
let expectedTileSetIdentifiers = Set([
"mapbox.mapbox-streets-v8",
"mapbox.mapbox-terrain-v2"
])
XCTAssertEqual(tileSetIdentifiers.count,
expectedTileSetIdentifiers.count,
"There should be two tile set identifier.")
XCTAssertEqual(tileSetIdentifiers,
expectedTileSetIdentifiers,
"Tile set identifiers are not equal.")
let filteredTileSetIdentifiers = mapView.tileSetIdentifiers("custom", sourceType: "raster")
XCTAssertEqual(filteredTileSetIdentifiers.count,
0,
"Tile set identifiers array should be empty.")
// Verify whether `MapView.sourceIdentifiers(_:)` returns only source identifiers for
// Mapbox tile set identifiers.
let mapboxSourceIdentifiers = mapView.sourceIdentifiers(Set(["mapbox.mapbox-terrain-v2"]))
XCTAssertEqual(mapboxSourceIdentifiers.count,
1,
"There should be only one source identifier.")
XCTAssertEqual(mapboxSourceIdentifiers.first,
"composite",
"Source identifiers are not equal.")
}
func testMapViewShowsTrafficAndIncidents() {
let resourceOptions = ResourceOptions(accessToken: "")
let mapInitOptions = MapInitOptions(resourceOptions: resourceOptions)
let mapView = MapView(frame: UIScreen.main.bounds, mapInitOptions: mapInitOptions)
let styleJSONObject: [String: Any] = [
"version": 8,
"center": [
-122.385563, 37.763330
],
"zoom": 15,
"sources": [
"mapbox-traffic": [
"url": "mapbox://mapbox.mapbox-traffic-v1",
"type": "vector"
]
],
"layers": [
[
"id": "traffic",
"type": "line",
"source": "mapbox-traffic",
"source-layer": "traffic"
]
]
]
let styleJSON: String = ValueConverter.toJson(forValue: styleJSONObject)
XCTAssertFalse(styleJSON.isEmpty, "ValueConverter should create valid JSON string.")
let mapLoadingErrorExpectation = expectation(description: "Map loading error expectation")
mapLoadingErrorExpectation.assertForOverFulfill = false
mapView.mapboxMap.onNext(.mapLoadingError, handler: { event in
mapLoadingErrorExpectation.fulfill()
})
mapView.mapboxMap.loadStyleJSON(styleJSON)
wait(for: [mapLoadingErrorExpectation], timeout: 10.0)
XCTAssertEqual(mapView.mapboxMap.style.allSourceIdentifiers.count,
1,
"There should be one source.")
XCTAssertEqual(mapView.mapboxMap.style.allLayerIdentifiers.count,
1,
"There should be one layer.")
// It is expected that `showsTraffic` will be set to `false` after changing its visibility.
XCTAssertTrue(mapView.showsTraffic, "Traffic should be shown by default.")
mapView.showsTraffic = false
XCTAssertFalse(mapView.showsTraffic, "Traffic should not be shown after change.")
// Since there is no incidents layer `showsIncidents` modification will have no effect.
XCTAssertFalse(mapView.showsIncidents, "Incidents should not be shown by default.")
mapView.showsIncidents = false
XCTAssertFalse(mapView.showsIncidents, "Incidents should not be shown after change.")
}
func testLocalizingLabels() {
let resourceOptions = ResourceOptions(accessToken: "")
let mapInitOptions = MapInitOptions(resourceOptions: resourceOptions)
let mapView = MapView(frame: UIScreen.main.bounds, mapInitOptions: mapInitOptions)
let styleJSONObject: [String: Any] = [
"version": 8,
"center": [
-122.385563, 37.763330
],
"zoom": 15,
"sources": [
"composite": [
"url": "mapbox://mapbox.mapbox-streets-v8,mapbox.mapbox-terrain-v2",
"type": "vector",
]
],
"layers": [
[
"id": "road-labels",
"type": "symbol",
"source": "composite",
"source-layer": "road",
"layout": [
"text-field": ["coalesce", ["get", "name_en"], ["get", "name"]],
],
],
[
"id": "place-labels",
"type": "symbol",
"source": "composite",
"source-layer": "place",
"layout": [
"text-field": ["coalesce", ["get", "name_en"], ["get", "name"]],
],
],
],
]
let styleJSON: String = ValueConverter.toJson(forValue: styleJSONObject)
XCTAssertFalse(styleJSON.isEmpty, "ValueConverter should create valid JSON string.")
let mapLoadingErrorExpectation = expectation(description: "Map loading error expectation")
mapLoadingErrorExpectation.assertForOverFulfill = false
mapView.mapboxMap.onNext(.mapLoadingError, handler: { event in
mapLoadingErrorExpectation.fulfill()
})
mapView.mapboxMap.loadStyleJSON(styleJSON)
wait(for: [mapLoadingErrorExpectation], timeout: 10.0)
let style = mapView.mapboxMap.style
XCTAssertEqual(style.allSourceIdentifiers.count, 1)
XCTAssertEqual(style.allLayerIdentifiers.count, 2)
func textFieldExpression(layerIdentifier: String) -> Exp? {
let expressionArray = style.layerProperty(for: layerIdentifier, property: "text-field").value
var expressionData: Data? = nil
XCTAssertNoThrow(expressionData = try JSONSerialization.data(withJSONObject: expressionArray, options: []))
guard expressionData != nil else { return nil }
var expression: Exp? = nil
XCTAssertNoThrow(expression = try JSONDecoder().decode(Exp.self, from: expressionData!))
return expression
}
XCTAssertEqual(textFieldExpression(layerIdentifier: "road-labels"),
Exp(.format) {
Exp(.coalesce) { Exp(.get) { "name_en" }; Exp(.get) { "name" } }
FormatOptions()
},
"Road labels should be in English by default.")
XCTAssertEqual(textFieldExpression(layerIdentifier: "place-labels"),
Exp(.format) {
Exp(.coalesce) { Exp(.get) { "name_en" }; Exp(.get) { "name" } }
FormatOptions()
},
"Place labels should be in English by default.")
func assert(roadLabelProperty: String, placeLabelProperty: String) {
// TODO: Unlocalize road labels: https://github.com/mapbox/mapbox-maps-ios/issues/653
XCTAssertEqual(textFieldExpression(layerIdentifier: "road-labels"),
Exp(.format) {
Exp(.coalesce) { Exp(.get) { roadLabelProperty }; Exp(.get) { "name" } }
FormatOptions()
},
"Road labels should remain in English after localization.")
XCTAssertEqual(textFieldExpression(layerIdentifier: "place-labels"),
Exp(.format) {
Exp(.coalesce) { Exp(.get) { placeLabelProperty }; Exp(.get) { "name" } }
FormatOptions()
},
"Place labels should be localized after localization.")
}
mapView.localizeLabels(into: Locale(identifier: "en"))
assert(roadLabelProperty: "name_en", placeLabelProperty: "name_en")
mapView.localizeLabels(into: Locale(identifier: "es"))
assert(roadLabelProperty: "name_en", placeLabelProperty: "name_es")
mapView.localizeLabels(into: Locale(identifier: "zh-Hant-TW"))
assert(roadLabelProperty: "name_en", placeLabelProperty: "name_zh-Hant")
// Simplified Chinese is broken: https://github.com/mapbox/mapbox-maps-ios/issues/652
mapView.localizeLabels(into: Locale(identifier: "zh-Hans-CN"))
assert(roadLabelProperty: "name_en", placeLabelProperty: "name_zh")
XCTAssertNoThrow(mapView.localizeLabels(into: Locale(identifier: "tlh")))
}
func testPreferredMapboxStreetsLocale() {
// https://github.com/mapbox/mapbox-maps-ios/issues/653
XCTAssertNil(VectorSource.preferredMapboxStreetsLocale(for: Locale(identifier: "mul")),
"Local language not yet implemented.")
XCTAssertEqual(VectorSource.preferredMapboxStreetsLocale(for: Locale(identifier: "es")),
Locale(identifier: "es"),
"Exact match should be supported.")
XCTAssertEqual(VectorSource.preferredMapboxStreetsLocale(for: Locale(identifier: "en-US")),
Locale(identifier: "en"),
"Extraneous region codes should be removed.")
XCTAssertEqual(VectorSource.preferredMapboxStreetsLocale(for: Locale(identifier: "en-Latn")),
Locale(identifier: "en"),
"Extraneous script codes should be removed.")
XCTAssertEqual(VectorSource.preferredMapboxStreetsLocale(for: Locale(identifier: "zh-Hans-CN")),
Locale(identifier: "zh-Hans"),
"Extraneous region codes should be removed.")
XCTAssertEqual(VectorSource.preferredMapboxStreetsLocale(for: Locale(identifier: "zh-Hant-HK")),
Locale(identifier: "zh-Hant"),
"Extraneous region codes should be removed.")
XCTAssertNil(VectorSource.preferredMapboxStreetsLocale(for: Locale(identifier: "frm")),
"Middle French not supported despite sharing prefix with French.")
XCTAssertNil(VectorSource.preferredMapboxStreetsLocale(for: Locale(identifier: "tlh")),
"Klingon not yet implemented. 🖖")
}
func testMapViewPointForCoordinate() {
let resourceOptions = ResourceOptions(accessToken: "")
let mapInitOptions = MapInitOptions(resourceOptions: resourceOptions)
let mapView = MapView(frame: UIScreen.main.bounds, mapInitOptions: mapInitOptions)
let styleJSONObject: [String: Any] = [
"version": 8,
"center": [
-122.38556, 37.76333
],
"zoom": 15,
"sources": [],
"layers": []
]
let styleJSON: String = ValueConverter.toJson(forValue: styleJSONObject)
XCTAssertFalse(styleJSON.isEmpty, "ValueConverter should create valid JSON string.")
let styleLoadedExpectation = expectation(description: "Map loading error expectation")
styleLoadedExpectation.assertForOverFulfill = false
mapView.mapboxMap.onNext(.styleLoaded, handler: { _ in
styleLoadedExpectation.fulfill()
})
mapView.mapboxMap.loadStyleJSON(styleJSON)
wait(for: [styleLoadedExpectation], timeout: 1.0)
var coordinate = CLLocationCoordinate2D(latitude: 37.76333, longitude: -122.38556)
var expectedPoint = CGPoint(x: 187.5, y: 333.4999999980936)
var actualPoint = mapView.mapboxMap.point(for: coordinate)
XCTAssertEqual(actualPoint, expectedPoint, "Points should be equal.")
coordinate = CLLocationCoordinate2D(latitude: 37.42859, longitude: -122.12780)
expectedPoint = CGPoint(x: 187.5, y: 333.5000000009532)
actualPoint = mapView.mapboxMap.point(for: coordinate)
XCTAssertEqual(actualPoint, expectedPoint, "Points should be equal.")
}
}