Skip to content

Commit

Permalink
Add unit test for multileg route
Browse files Browse the repository at this point in the history
  • Loading branch information
frederoni committed Feb 21, 2019
1 parent 6217a5f commit dc27dca
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 4 deletions.
6 changes: 4 additions & 2 deletions MapboxCoreNavigation/RouteController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,10 @@ open class RouteController: NSObject {
if willChangeVisualIndex || isFirstLocation {
let currentStepProgress = routeProgress.currentLegProgress.currentStepProgress
currentStepProgress.visualInstructionIndex = Int(status.bannerInstruction?.index ?? 0)
let instruction = currentStepProgress.currentVisualInstruction
announcePassage(of: instruction!, routeProgress: routeProgress)

if let instruction = currentStepProgress.currentVisualInstruction {
announcePassage(of: instruction, routeProgress: routeProgress)
}
}
}

Expand Down
26 changes: 24 additions & 2 deletions MapboxCoreNavigationTests/NavigationServiceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ class NavigationServiceTests: XCTestCase {
let navigation = dependencies.navigationService

let now = Date()
let trace = Fixture.generateTrace(for: route).enumerated().map { $0.element.shifted(to: now + $0.offset) }
let trace = Fixture.generateTrace(for: route).shiftedToPresent()
trace.forEach { navigation.router!.locationManager!(navigation.locationManager, didUpdateLocations: [$0]) }

// TODO: Verify why we need a second location update when routeState == .complete to trigger `MMEEventTypeNavigationArrive`
Expand Down Expand Up @@ -328,7 +328,7 @@ class NavigationServiceTests: XCTestCase {

// MARK: When navigation begins with a location update
let now = Date()
let trace = Fixture.generateTrace(for: route).enumerated().map { $0.element.shifted(to: now + $0.offset) }
let trace = Fixture.generateTrace(for: route).shiftedToPresent()

trace.forEach { navigation.router.locationManager!(navigation.locationManager, didUpdateLocations: [$0]) }

Expand Down Expand Up @@ -405,4 +405,26 @@ class NavigationServiceTests: XCTestCase {
subject.poorGPSPatience = 5.0
XCTAssert(subject.poorGPSTimer.countdownInterval == .milliseconds(5000), "Timer should now have a countdown interval of 5000 millseconds.")
}

func testMultiLegRoute() {
let route = Fixture.route(from: "multileg-route")
let trace = Fixture.generateTrace(for: route).shiftedToPresent().qualified()
let service = dependencies.navigationService

let routeController = service.router as! RouteController
routeController.route = route

for (index, location) in trace.enumerated() {

service.locationManager!(service.locationManager, didUpdateLocations: [location])

if index < 33 {
XCTAssert(routeController.routeProgress.legIndex == 0)
} else {
XCTAssert(routeController.routeProgress.legIndex == 1)
}
}

XCTAssertTrue(delegate.recentMessages.contains("navigationService(_:didArriveAt:)"))
}
}
4 changes: 4 additions & 0 deletions MapboxNavigation.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
35726EE81F0856E900AFA1B6 /* DayStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35726EE71F0856E900AFA1B6 /* DayStyle.swift */; };
3573EA71215A5A9F009899D7 /* RouteControllerSnapshotTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3573EA70215A5A9F009899D7 /* RouteControllerSnapshotTests.swift */; };
3577B878214FF35800094294 /* FavoritesList.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3577B877214FF35800094294 /* FavoritesList.swift */; };
357DEC44221AEE150019BAEC /* multileg-route.json in Resources */ = {isa = PBXBuildFile; fileRef = 357DEC43221AEE150019BAEC /* multileg-route.json */; };
3582A25020EEC46B0029C5DE /* Router.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3582A24F20EEC46B0029C5DE /* Router.swift */; };
3582A25220EFA9680029C5DE /* RouterDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3582A25120EFA9680029C5DE /* RouterDelegate.swift */; };
358D14661E5E3B7700ADE590 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 358D14651E5E3B7700ADE590 /* AppDelegate.swift */; };
Expand Down Expand Up @@ -685,6 +686,7 @@
35726EE71F0856E900AFA1B6 /* DayStyle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DayStyle.swift; sourceTree = "<group>"; };
3573EA70215A5A9F009899D7 /* RouteControllerSnapshotTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RouteControllerSnapshotTests.swift; sourceTree = "<group>"; };
3577B877214FF35800094294 /* FavoritesList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = FavoritesList.swift; path = Example/FavoritesList.swift; sourceTree = "<group>"; };
357DEC43221AEE150019BAEC /* multileg-route.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "multileg-route.json"; sourceTree = "<group>"; };
357F0DF01EB9D99F00A0B53C /* sv */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.strings; name = sv; path = sv.lproj/Localizable.strings; sourceTree = "<group>"; };
357F0DF11EB9DAB400A0B53C /* vi */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.strings; name = vi; path = vi.lproj/Localizable.strings; sourceTree = "<group>"; };
3582A24F20EEC46B0029C5DE /* Router.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Router.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1142,6 +1144,7 @@
35CDA8882190F5100072B675 /* sthlm-double-back-replay.json */,
35CDA8852190F50A0072B675 /* sthlm-double-back.json */,
C5EF397420599120009A2C50 /* straight-line.json */,
357DEC43221AEE150019BAEC /* multileg-route.json */,
);
path = Fixtures;
sourceTree = "<group>";
Expand Down Expand Up @@ -2141,6 +2144,7 @@
35CDA8972190F6980072B675 /* sthlm-double-back.json in Resources */,
35CDA88F2190F6980072B675 /* EmptyStyle.json in Resources */,
35CDA8992190F6980072B675 /* UnionSquare-to-GGPark.route in Resources */,
357DEC44221AEE150019BAEC /* multileg-route.json in Resources */,
35C8DBF6219194380053328C /* routeWithTunnels_9thStreetDC.json in Resources */,
35C8DBF52191940C0053328C /* straight-line.json in Resources */,
35C8DC0F2191DE940053328C /* DCA-Arboretum.trace.json in Resources */,
Expand Down
26 changes: 26 additions & 0 deletions TestHelper/CoreLocation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,29 @@ extension Date {
return formatter
}()
}

extension Array where Element == CLLocation {

// Shifts the [CLLocation]’s first location to now and offsets the remaining locations by one second after the prior.
public func shiftedToPresent() -> [CLLocation] {
let now = Date()
return enumerated().map { CLLocation(coordinate: $0.element.coordinate,
altitude: $0.element.altitude,
horizontalAccuracy: $0.element.horizontalAccuracy,
verticalAccuracy: $0.element.verticalAccuracy,
course: $0.element.course,
speed: $0.element.speed,
timestamp: now + $0.offset) }
}

// Returns a [CLLocation] with course and accuracies qualified for navigation native.
public func qualified() -> [CLLocation] {
return enumerated().map { CLLocation(coordinate: $0.element.coordinate,
altitude: -1,
horizontalAccuracy: 10,
verticalAccuracy: -1,
course: -1,
speed: 10,
timestamp: $0.element.timestamp) }
}
}
Loading

0 comments on commit dc27dca

Please sign in to comment.