From df29ed894a5644d94156073af915bca89e6c2678 Mon Sep 17 00:00:00 2001 From: Jason Wray Date: Tue, 19 Feb 2019 16:33:27 -0800 Subject: [ios] Mock MGLOfflineStorage in MGLDocumentationExampleTests Otherwise we risk kicking-off long-running offline pack downloads that could interfere with other tests. --- platform/darwin/src/MGLOfflinePack.h | 12 +++---- .../darwin/test/MGLDocumentationExampleTests.swift | 37 +++++++++++++++------- 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_() { -- cgit v1.2.1