-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for mapbox designed route shields (#644)
* Add support for mapbox shields. * Incorporate feedback. * Update changelog. Rename mapboxShield to shield for consistency.
- Loading branch information
1 parent
0f20e9a
commit 1e5310d
Showing
3 changed files
with
177 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,32 +23,126 @@ class VisualInstructionComponentTests: XCTestCase { | |
} | ||
|
||
func testImageComponent() { | ||
let componentJSON = [ | ||
"text": "US 42", | ||
let componentJSON: [String : Any] = [ | ||
"text": "I 95", | ||
"type": "icon", | ||
"imageBaseURL": "https://s3.amazonaws.com/mapbox/shields/v3/i-95", | ||
"mapbox_shield": [ | ||
"base_url": "https://api.mapbox.com/styles/v1/", | ||
"name": "us-interstate", | ||
"text_color": "white", | ||
"display_ref": "95" | ||
] | ||
] | ||
let componentData = try! JSONSerialization.data(withJSONObject: componentJSON, options: []) | ||
var component: VisualInstruction.Component? | ||
XCTAssertNoThrow(component = try JSONDecoder().decode(VisualInstruction.Component.self, from: componentData)) | ||
XCTAssertNotNil(component) | ||
if let component = component { | ||
switch component { | ||
case .image(let image, let alternativeText, let shield): | ||
XCTAssertEqual(image.imageBaseURL?.absoluteString, "https://s3.amazonaws.com/mapbox/shields/v3/i-95") | ||
XCTAssertEqual(image.imageURL(scale: 1, format: .svg)?.absoluteString, "https://s3.amazonaws.com/mapbox/shields/v3/[email protected]") | ||
XCTAssertEqual(image.imageURL(scale: 3, format: .svg)?.absoluteString, "https://s3.amazonaws.com/mapbox/shields/v3/[email protected]") | ||
XCTAssertEqual(image.imageURL(scale: 3, format: .png)?.absoluteString, "https://s3.amazonaws.com/mapbox/shields/v3/[email protected]") | ||
XCTAssertEqual(alternativeText.text, "I 95") | ||
XCTAssertNil(alternativeText.abbreviation) | ||
XCTAssertNil(alternativeText.abbreviationPriority) | ||
XCTAssertEqual(shield?.baseURL, URL(string: "https://api.mapbox.com/styles/v1/")!) | ||
XCTAssertEqual(shield?.name, "us-interstate") | ||
XCTAssertEqual(shield?.textColor, "white") | ||
XCTAssertEqual(shield?.text, "95") | ||
default: | ||
XCTFail("Image component should not be decoded as any other kind of component.") | ||
} | ||
} | ||
|
||
component = .image(image: .init(imageBaseURL: URL(string: "https://s3.amazonaws.com/mapbox/shields/v3/i-95")!), | ||
alternativeText: .init(text: "I 95", abbreviation: nil, abbreviationPriority: nil), | ||
shield: .init(baseURL: URL(string: "https://api.mapbox.com/styles/v1/")!, name: "us-interstate", textColor: "white", text: "95")) | ||
let encoder = JSONEncoder() | ||
var encodedData: Data? | ||
XCTAssertNoThrow(encodedData = try encoder.encode(component)) | ||
XCTAssertNotNil(encodedData) | ||
|
||
if let encodedData = encodedData { | ||
var encodedComponentJSON: [String: Any?]? | ||
XCTAssertNoThrow(encodedComponentJSON = try JSONSerialization.jsonObject(with: encodedData, options: []) as? [String: Any?]) | ||
XCTAssertNotNil(encodedComponentJSON) | ||
|
||
XCTAssert(JSONSerialization.objectsAreEqual(componentJSON, encodedComponentJSON, approximate: false)) | ||
} | ||
} | ||
|
||
func testShield() { | ||
let shieldJSON = [ | ||
"base_url": "https://api.mapbox.com/styles/v1/", | ||
"name": "us-interstate", | ||
"text_color": "white", | ||
"display_ref": "95", | ||
] | ||
|
||
let shieldData = try! JSONSerialization.data(withJSONObject: shieldJSON, options: []) | ||
var shield: VisualInstruction.Component.ShieldRepresentation? | ||
XCTAssertNoThrow(shield = try JSONDecoder().decode(VisualInstruction.Component.ShieldRepresentation.self, from: shieldData)) | ||
XCTAssertNotNil(shield) | ||
let url = URL(string: "https://api.mapbox.com/styles/v1/") | ||
if let shield = shield { | ||
XCTAssertEqual(shield.baseURL, url) | ||
XCTAssertEqual(shield.name, "us-interstate") | ||
XCTAssertEqual(shield.textColor, "white") | ||
XCTAssertEqual(shield.text, "95") | ||
} | ||
shield = .init(baseURL: url!, name: "us-interstate", textColor: "white", text: "95") | ||
|
||
let encoder = JSONEncoder() | ||
var encodedData: Data? | ||
XCTAssertNoThrow(encodedData = try encoder.encode(shield)) | ||
XCTAssertNotNil(encodedData) | ||
|
||
if let encodedData = encodedData { | ||
var encodedShieldJSON: [String: Any]? | ||
XCTAssertNoThrow(encodedShieldJSON = try JSONSerialization.jsonObject(with: encodedData, options: []) as? [String: Any]) | ||
XCTAssertNotNil(encodedShieldJSON) | ||
|
||
XCTAssert(JSONSerialization.objectsAreEqual(shieldJSON, encodedShieldJSON, approximate: false)) | ||
} | ||
} | ||
|
||
func testShieldImageComponent() { | ||
let componentJSON: [String : Any] = [ | ||
"text": "I 95", | ||
"type": "icon", | ||
"imageBaseURL": "https://s3.amazonaws.com/mapbox/shields/v3/us-42", | ||
"mapbox_shield": [ | ||
"base_url": "https://api.mapbox.com/styles/v1/", | ||
"name": "us-interstate", | ||
"text_color": "white", | ||
"display_ref": "95" | ||
] | ||
] | ||
let componentData = try! JSONSerialization.data(withJSONObject: componentJSON, options: []) | ||
var component: VisualInstruction.Component? | ||
XCTAssertNoThrow(component = try JSONDecoder().decode(VisualInstruction.Component.self, from: componentData)) | ||
XCTAssertNotNil(component) | ||
if let component = component { | ||
switch component { | ||
case .image(let image, let alternativeText): | ||
XCTAssertEqual(image.imageBaseURL?.absoluteString, "https://s3.amazonaws.com/mapbox/shields/v3/us-42") | ||
XCTAssertEqual(image.imageURL(scale: 1, format: .svg)?.absoluteString, "https://s3.amazonaws.com/mapbox/shields/v3/[email protected]") | ||
XCTAssertEqual(image.imageURL(scale: 3, format: .svg)?.absoluteString, "https://s3.amazonaws.com/mapbox/shields/v3/[email protected]") | ||
XCTAssertEqual(image.imageURL(scale: 3, format: .png)?.absoluteString, "https://s3.amazonaws.com/mapbox/shields/v3/[email protected]") | ||
XCTAssertEqual(alternativeText.text, "US 42") | ||
case .image(let image, let alternativeText, let shield): | ||
XCTAssertNil(image.imageBaseURL?.absoluteString) | ||
XCTAssertEqual(alternativeText.text, "I 95") | ||
XCTAssertNil(alternativeText.abbreviation) | ||
XCTAssertNil(alternativeText.abbreviationPriority) | ||
XCTAssertEqual(shield?.baseURL, URL(string: "https://api.mapbox.com/styles/v1/")!) | ||
XCTAssertEqual(shield?.name, "us-interstate") | ||
XCTAssertEqual(shield?.textColor, "white") | ||
XCTAssertEqual(shield?.text, "95") | ||
default: | ||
XCTFail("Image component should not be decoded as any other kind of component.") | ||
} | ||
} | ||
|
||
component = .image(image: .init(imageBaseURL: URL(string: "https://s3.amazonaws.com/mapbox/shields/v3/us-42")!), | ||
alternativeText: .init(text: "US 42", abbreviation: nil, abbreviationPriority: nil)) | ||
component = .image(image: .init(imageBaseURL: nil), | ||
alternativeText: .init(text: "I 95", abbreviation: nil, abbreviationPriority: nil), | ||
shield: .init(baseURL: URL(string: "https://api.mapbox.com/styles/v1/")!, name: "us-interstate", textColor: "white", text: "95")) | ||
let encoder = JSONEncoder() | ||
var encodedData: Data? | ||
XCTAssertNoThrow(encodedData = try encoder.encode(component)) | ||
|