Skip to content

Commit

Permalink
Move test doubles to TestHelper framework
Browse files Browse the repository at this point in the history
  • Loading branch information
frederoni committed Nov 8, 2018
1 parent 0dbd8c3 commit 36baf32
Show file tree
Hide file tree
Showing 16 changed files with 266 additions and 156 deletions.
Expand Up @@ -2,6 +2,7 @@ import XCTest
import Quick
import Nimble
import MapboxDirections
import TestHelper
@testable import MapboxCoreNavigation

class MapboxNavigationServiceSpec: QuickSpec {
Expand Down
2 changes: 2 additions & 0 deletions MapboxCoreNavigationTests/NavigationEventsManagerTests.swift
@@ -1,6 +1,8 @@
import XCTest
@testable import TestHelper
@testable import MapboxCoreNavigation


class NavigationEventsManagerTests: XCTestCase {

func testMobileEventsManagerIsInitializedImmediately() {
Expand Down
2 changes: 1 addition & 1 deletion MapboxCoreNavigationTests/NavigationServiceTests.swift
Expand Up @@ -2,7 +2,7 @@ import XCTest
import MapboxDirections
import Turf
import MapboxMobileEvents
import TestHelper
@testable import TestHelper
@testable import MapboxCoreNavigation

fileprivate let mbTestHeading: CLLocationDirection = 50
Expand Down
11 changes: 0 additions & 11 deletions MapboxCoreNavigationTests/Support/DummyURLSessionDataTask.swift

This file was deleted.

This file was deleted.

113 changes: 89 additions & 24 deletions MapboxNavigation.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>classNames</key>
<dict>
<key>BenchTests</key>
<dict>
<key>testControlRoute1()</key>
<dict>
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
<dict>
<key>baselineAverage</key>
<real>0.736</real>
<key>baselineIntegrationDisplayName</key>
<string>Local Baseline</string>
</dict>
</dict>
</dict>
</dict>
</dict>
</plist>
21 changes: 0 additions & 21 deletions MapboxNavigationTests/MapboxVoiceControllerTests.swift
Expand Up @@ -97,24 +97,3 @@ class MockMapboxVoiceController: MapboxVoiceController {
}
}

fileprivate extension Notification.Name {
enum MapboxVoiceTests {
static let prepareToPlay = NSNotification.Name("MapboxVoiceTests.prepareToPlay")
static let play = NSNotification.Name("MapboxVoiceTests.play")
}
}
class AudioPlayerDummy: AVAudioPlayer {
public let sound = NSDataAsset(name: "reroute-sound", bundle: .mapboxNavigation)!

lazy var notifier: NotificationCenter = .default
fileprivate typealias Note = Notification.Name.MapboxVoiceTests

override func prepareToPlay() -> Bool {
notifier.post(name: Note.prepareToPlay, object: self)
return true
}
override func play() -> Bool {
notifier.post(name: Note.play, object: self)
return true
}
}
32 changes: 0 additions & 32 deletions MapboxNavigationTests/Support/SpeechAPISpy.swift

This file was deleted.

1 change: 1 addition & 0 deletions TestHelper/DCA-Arboretum-Tunnels-1.trace.json

Large diffs are not rendered by default.

Expand Up @@ -2,21 +2,21 @@ import Foundation
import MapboxDirections

@objc(MBDirectionsSpy)
class DirectionsSpy: Directions {
public class DirectionsSpy: Directions {

var lastCalculateOptionsCompletion: RouteCompletionHandler?
public var lastCalculateOptionsCompletion: RouteCompletionHandler?

override func calculate(_ options: MatchOptions, completionHandler: @escaping Directions.MatchCompletionHandler) -> URLSessionDataTask {
override public func calculate(_ options: MatchOptions, completionHandler: @escaping Directions.MatchCompletionHandler) -> URLSessionDataTask {
assert(false, "Not ready to handle \(#function)")
return DummyURLSessionDataTask()
}

override func calculate(_ options: RouteOptions, completionHandler: @escaping Directions.RouteCompletionHandler) -> URLSessionDataTask {
override public func calculate(_ options: RouteOptions, completionHandler: @escaping Directions.RouteCompletionHandler) -> URLSessionDataTask {
lastCalculateOptionsCompletion = completionHandler
return DummyURLSessionDataTask()
}

override func calculateRoutes(matching options: MatchOptions, completionHandler: @escaping Directions.RouteCompletionHandler) -> URLSessionDataTask {
override public func calculateRoutes(matching options: MatchOptions, completionHandler: @escaping Directions.RouteCompletionHandler) -> URLSessionDataTask {
assert(false, "Not ready to handle \(#function)")
return DummyURLSessionDataTask()
}
Expand All @@ -26,7 +26,7 @@ class DirectionsSpy: Directions {
assert(false, "Can't fire a completion handler which doesn't exist!")
return
}

lastCalculateOptionsCompletion(waypoints, routes, error)
}

Expand Down
11 changes: 11 additions & 0 deletions TestHelper/DummyURLSessionDataTask.swift
@@ -0,0 +1,11 @@
import Foundation

public class DummyURLSessionDataTask: URLSessionDataTask {
override public func resume() {
//
}

override public func cancel() {
//
}
}
15 changes: 15 additions & 0 deletions TestHelper/Fixture.swift
Expand Up @@ -58,6 +58,21 @@ public class Fixture: NSObject {
return URL(fileURLWithPath: path!)
}

public class func locations(from name: String) -> [CLLocation] {
guard let path = Bundle(for: Fixture.self).path(forResource: name, ofType: "json") else {
assert(false, "Fixture \(name) not found.")
return []
}
guard let data = NSData(contentsOfFile: path) as Data? else {
assert(false, "No data found at \(path).")
return []
}

let locations = try! JSONDecoder().decode([Location].self, from: data)

return locations.map { CLLocation($0) }
}

public class func route(from jsonFile: String) -> Route {
let response = JSONFromFileNamed(name: jsonFile)
let waypoints = Fixture.waypoints(from: jsonFile)
Expand Down
Expand Up @@ -3,7 +3,7 @@ import MapboxMobileEvents
@testable import MapboxCoreNavigation
import MapboxDirections

class NavigationEventsManagerSpy: NavigationEventsManager {
public class NavigationEventsManagerSpy: NavigationEventsManager {

private var mobileEventsManagerSpy: MMEEventsManagerSpy!

Expand Down
60 changes: 60 additions & 0 deletions TestHelper/NavigationServiceTestDoubles.swift
@@ -0,0 +1,60 @@
import Foundation
import MapboxCoreNavigation
import MapboxDirections

class RouteControllerDataSourceFake: RouterDataSource {

let manager = NavigationLocationManager()

var location: CLLocation? {
return manager.location
}

var locationProvider: NavigationLocationManager.Type {
return type(of: manager)
}
}

public class NavigationServiceDelegateSpy: NavigationServiceDelegate {
private(set) var recentMessages: [String] = []

public func reset() {
recentMessages.removeAll()
}

public func navigationService(_ service: NavigationService, shouldRerouteFrom location: CLLocation) -> Bool {
recentMessages.append(#function)
return true
}

public func navigationService(_ service: NavigationService, willRerouteFrom location: CLLocation) {
recentMessages.append(#function)
}

public func navigationService(_ service: NavigationService, shouldDiscard location: CLLocation) -> Bool {
recentMessages.append(#function)
return true
}

public func navigationService(_ service: NavigationService, didRerouteAlong route: Route, at location: CLLocation?, proactive: Bool) {
recentMessages.append(#function)
}

public func navigationService(_ service: NavigationService, didFailToRerouteWith error: Error) {
recentMessages.append(#function)
}

public func navigationService(_ service: NavigationService, didUpdate progress: RouteProgress, with location: CLLocation, rawLocation: CLLocation) {
recentMessages.append(#function)
}

public func navigationService(_ service: NavigationService, didArriveAt waypoint: Waypoint) -> Bool {
recentMessages.append(#function)
return true
}

public func navigationService(_ service: NavigationService, shouldPreventReroutesWhenArrivingAt waypoint: Waypoint) -> Bool {
recentMessages.append(#function)
return true
}
}
57 changes: 57 additions & 0 deletions TestHelper/SpeechAPISpy.swift
@@ -0,0 +1,57 @@
import Foundation
import MapboxSpeech
import AVKit
@testable import MapboxNavigation
/**
* This class can be used as a substitute for SpeechSynthesizer under test, in order to verify whether expected calls were made.
*/


public class SpeechAPISpy: SpeechSynthesizer {
public struct AudioDataCall {
static let sound = NSDataAsset(name: "reroute-sound", bundle: .mapboxNavigation)!

let options: MapboxSpeech.SpeechOptions
let completion: SpeechSynthesizer.CompletionHandler

func fulfill() {
completion(AudioDataCall.sound.data, nil)
}
}

public var audioDataCalls: [AudioDataCall] = []

override public func audioData(with options: MapboxSpeech.SpeechOptions, completionHandler: @escaping MapboxSpeech.SpeechSynthesizer.CompletionHandler) -> URLSessionDataTask {
let call = AudioDataCall(options: options, completion: completionHandler)
audioDataCalls.append(call)
return DummyURLSessionDataTask()
}

public func reset() {
audioDataCalls.removeAll()
}
}

public class AudioPlayerDummy: AVAudioPlayer {
public let sound = NSDataAsset(name: "reroute-sound", bundle: .mapboxNavigation)!

lazy var notifier: NotificationCenter = .default
fileprivate typealias Note = Notification.Name.MapboxVoiceTests

override public func prepareToPlay() -> Bool {
notifier.post(name: Note.prepareToPlay, object: self)
return true
}

override public func play() -> Bool {
notifier.post(name: Note.play, object: self)
return true
}
}

extension Notification.Name {
enum MapboxVoiceTests {
static let prepareToPlay = NSNotification.Name("MapboxVoiceTests.prepareToPlay")
static let play = NSNotification.Name("MapboxVoiceTests.play")
}
}

0 comments on commit 36baf32

Please sign in to comment.