summaryrefslogtreecommitdiff
path: root/platform/darwin
diff options
context:
space:
mode:
authorJason Wray <friedbunny@users.noreply.github.com>2019-02-19 16:33:27 -0800
committerGitHub <noreply@github.com>2019-02-19 16:33:27 -0800
commitc00d44740fcf4bb868b39fd4d907958f6fe534bb (patch)
tree3f3dfccdce2631a053608740ed420d731ece40cd /platform/darwin
parent1215629591d0a3b26f6a6a12b3efa759498f1e08 (diff)
downloadqtlocation-mapboxgl-c00d44740fcf4bb868b39fd4d907958f6fe534bb.tar.gz
[ios] Mock MGLOfflineStorage in MGLDocumentationExampleTests
Otherwise we risk kicking-off long-running offline pack downloads that could interfere with other tests.
Diffstat (limited to 'platform/darwin')
-rw-r--r--platform/darwin/src/MGLOfflinePack.h12
-rw-r--r--platform/darwin/test/MGLDocumentationExampleTests.swift37
2 files changed, 32 insertions, 17 deletions
diff --git a/platform/darwin/src/MGLOfflinePack.h b/platform/darwin/src/MGLOfflinePack.h
index 7b241e0248..3dcb5b7abe 100644
--- a/platform/darwin/src/MGLOfflinePack.h
+++ b/platform/darwin/src/MGLOfflinePack.h
@@ -106,15 +106,15 @@ typedef struct __attribute__((objc_boxable)) MGLOfflinePackProgress {
### Example
```swift
- MGLOfflineStorage.shared.addPack(for: region, withContext: context!) { (pack, error) in
- guard error == nil else {
- // If download fails, log the error to the console
- print("Error: \(error?.localizedDescription ?? "unknown error")")
+ MGLOfflineStorage.shared.addPack(for: region, withContext: context) { (pack, error) in
+ guard let pack = pack else {
+ // If adding the pack fails, log an error to console.
+ print("Error:", error?.localizedDescription ?? "unknown error adding pack at \(#file)(\(#line)) in \(#function)")
return
}
-
+
// Start an MGLOfflinePack download
- pack!.resume()
+ pack.resume()
}
```
*/
diff --git a/platform/darwin/test/MGLDocumentationExampleTests.swift b/platform/darwin/test/MGLDocumentationExampleTests.swift
index 9d1d848868..91fb02dfd2 100644
--- a/platform/darwin/test/MGLDocumentationExampleTests.swift
+++ b/platform/darwin/test/MGLDocumentationExampleTests.swift
@@ -29,6 +29,17 @@ class MGLDocumentationExampleTests: XCTestCase, MGLMapViewDelegate {
var styleLoadingExpectation: XCTestExpectation!
static let styleURL = Bundle(for: MGLDocumentationExampleTests.self).url(forResource: "one-liner", withExtension: "json")!
+ // Mock MGLOfflineStorage singleton so that it doesn't start long-running tasks that could interfere with other tests.
+ fileprivate class MGLOfflineStorageMock {
+ static let shared = MGLOfflineStorageMock()
+ func addPack(for: MGLOfflineRegion, withContext: Data, completionHandler: MGLOfflinePackAdditionCompletionHandler? = nil) {
+ XCTAssert(MGLOfflineStorage.shared.responds(to: #selector(MGLOfflineStorage.shared.addPack(for:withContext:completionHandler:))))
+ if let completionHandler = completionHandler {
+ completionHandler(nil, NSError(domain: "MGLDocumentationExampleError", code: 0, userInfo: [NSLocalizedDescriptionKey: "\(#function) is mocked and not functional."]))
+ }
+ }
+ }
+
override func setUp() {
super.setUp()
mapView = MGLMapView(frame: CGRect(x: 0, y: 0, width: 256, height: 256), styleURL: MGLDocumentationExampleTests.styleURL)
@@ -66,6 +77,8 @@ class MGLDocumentationExampleTests: XCTestCase, MGLMapViewDelegate {
}
}
+ typealias MGLOfflineStorage = MGLOfflineStorageMock
+
//#-example-code
let northeast = CLLocationCoordinate2D(latitude: 40.989329, longitude: -102.062592)
let southwest = CLLocationCoordinate2D(latitude: 36.986207, longitude: -109.049896)
@@ -85,6 +98,8 @@ class MGLDocumentationExampleTests: XCTestCase, MGLMapViewDelegate {
return MGLDocumentationExampleTests.styleURL
}
}
+
+ typealias MGLOfflineStorage = MGLOfflineStorageMock
//#-example-code
var coordinates = [
@@ -103,26 +118,26 @@ class MGLDocumentationExampleTests: XCTestCase, MGLMapViewDelegate {
}
func testMGLOfflinePack() {
+ typealias MGLOfflineStorage = MGLOfflineStorageMock
+
let northeast = CLLocationCoordinate2D(latitude: 40.989329, longitude: -102.062592)
let southwest = CLLocationCoordinate2D(latitude: 36.986207, longitude: -109.049896)
let bbox = MGLCoordinateBounds(sw: southwest, ne: northeast)
- let region = MGLTilePyramidOfflineRegion(styleURL: MGLStyle.lightStyleURL, bounds: bbox, fromZoomLevel: 11, toZoomLevel: 14)
- let context = "Tile Pyramid Region".data(using: .utf8)
-
+ let region = MGLTilePyramidOfflineRegion(styleURL: MGLDocumentationExampleTests.styleURL, bounds: bbox, fromZoomLevel: 11, toZoomLevel: 14)
+ let context = "Tile Pyramid Region".data(using: .utf8)!
+
//#-example-code
- MGLOfflineStorage.shared.addPack(for: region, withContext: context!) { (pack, error) in
- guard error == nil else {
- // If download fails, log the error to the console
- print("Error: \(error?.localizedDescription ?? "unknown error")")
+ MGLOfflineStorage.shared.addPack(for: region, withContext: context) { (pack, error) in
+ guard let pack = pack else {
+ // If adding the pack fails, log an error to console.
+ print("Error:", error?.localizedDescription ?? "unknown error adding pack at \(#file)(\(#line)) in \(#function)")
return
}
-
+
// Start an MGLOfflinePack download
- pack!.resume()
+ pack.resume()
}
//#-end-example-code
-
- XCTAssertNotNil(region)
}
func testMGLShape$shapeWithData_encoding_error_() {