summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMinh Nguyễn <mxn@1ec5.org>2017-12-21 00:28:19 -0800
committerMinh Nguyễn <mxn@1ec5.org>2017-12-21 12:02:20 -0800
commit1b407e85e9ee03124e24b71a856045c254775e6e (patch)
tree627cdb68cf1db38126e838fea73c19f87e383a8c
parentb9ef2727303bd2858b8fe1203adb0914a83ea0af (diff)
downloadqtlocation-mapboxgl-1b407e85e9ee03124e24b71a856045c254775e6e.tar.gz
[ios, macos] Fixed MGLMapSnapshotter test crash
Fixed a crash in MGLDocumentationExampleTests caused by an asynchronous snapshot call that the test wasn’t waiting on, compounded by the use of a style that requires an access token (as opposed to the one-liner style that uses no remote resources. Employed some Swiftisms to fulfill expectations without making the example code look weird.
-rw-r--r--platform/darwin/src/MGLMapSnapshotter.h8
-rw-r--r--platform/darwin/test/MGLDocumentationExampleTests.swift35
2 files changed, 33 insertions, 10 deletions
diff --git a/platform/darwin/src/MGLMapSnapshotter.h b/platform/darwin/src/MGLMapSnapshotter.h
index fc68204a7d..426ab1bb00 100644
--- a/platform/darwin/src/MGLMapSnapshotter.h
+++ b/platform/darwin/src/MGLMapSnapshotter.h
@@ -145,11 +145,11 @@ typedef void (^MGLMapSnapshotCompletionHandler)(MGLMapSnapshot* _Nullable snapsh
let snapshotter = MGLMapSnapshotter(options: options)
snapshotter.start { (snapshot, error) in
- if error != nil {
- // error handler
- } else {
- // image handler
+ if let error = error {
+ fatalError(error.localizedDescription)
}
+
+ image = snapshot?.image
}
```
*/
diff --git a/platform/darwin/test/MGLDocumentationExampleTests.swift b/platform/darwin/test/MGLDocumentationExampleTests.swift
index 8762af9ba4..668e5f57f8 100644
--- a/platform/darwin/test/MGLDocumentationExampleTests.swift
+++ b/platform/darwin/test/MGLDocumentationExampleTests.swift
@@ -27,11 +27,11 @@ import Mapbox
class MGLDocumentationExampleTests: XCTestCase, MGLMapViewDelegate {
var mapView: MGLMapView!
var styleLoadingExpectation: XCTestExpectation!
+ static let styleURL = Bundle(for: MGLDocumentationExampleTests.self).url(forResource: "one-liner", withExtension: "json")!
override func setUp() {
super.setUp()
- let styleURL = Bundle(for: MGLDocumentationExampleTests.self).url(forResource: "one-liner", withExtension: "json")
- mapView = MGLMapView(frame: CGRect(x: 0, y: 0, width: 256, height: 256), styleURL: styleURL)
+ mapView = MGLMapView(frame: CGRect(x: 0, y: 0, width: 256, height: 256), styleURL: MGLDocumentationExampleTests.styleURL)
mapView.delegate = self
styleLoadingExpectation = expectation(description: "Map view should finish loading style")
waitForExpectations(timeout: 1, handler: nil)
@@ -279,6 +279,27 @@ class MGLDocumentationExampleTests: XCTestCase, MGLMapViewDelegate {
}
func testMGLMapSnapshotter() {
+ let expectation = self.expectation(description: "MGLMapSnapshotter should produce a snapshot")
+ #if os(macOS)
+ var image: NSImage? {
+ didSet {
+ expectation.fulfill()
+ }
+ }
+ #else
+ var image: UIImage? {
+ didSet {
+ expectation.fulfill()
+ }
+ }
+ #endif
+
+ class MGLStyle {
+ static func satelliteStreetsStyleURL() -> URL {
+ return MGLDocumentationExampleTests.styleURL
+ }
+ }
+
//#-example-code
let camera = MGLMapCamera(lookingAtCenter: CLLocationCoordinate2D(latitude: 37.7184, longitude: -122.4365), fromDistance: 100, pitch: 20, heading: 0)
@@ -287,13 +308,15 @@ class MGLDocumentationExampleTests: XCTestCase, MGLMapViewDelegate {
let snapshotter = MGLMapSnapshotter(options: options)
snapshotter.start { (snapshot, error) in
- if error != nil {
- // error handler
- } else {
- // image handler
+ if let error = error {
+ fatalError(error.localizedDescription)
}
+
+ image = snapshot?.image
}
//#-end-example-code
+
+ wait(for: [expectation], timeout: 1)
}
// For testMGLMapView().