From d998ecec53c8af37a827a477973fe859fae3f897 Mon Sep 17 00:00:00 2001 From: Dan Nesfeder Date: Thu, 22 Mar 2018 12:31:01 -0400 Subject: [PATCH] Fix OffRoute detection disabled bug (#783) * Fix OffRoute detection disabled bug * Cleanup test --- .../v5/navigation/MapboxNavigation.java | 2 +- .../v5/navigation/NavigationHelper.java | 3 + .../v5/navigation/MapboxNavigationTest.java | 35 ++++++++- .../v5/navigation/NavigationHelperTest.java | 71 ++++++++++++------- 4 files changed, 83 insertions(+), 28 deletions(-) diff --git a/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/navigation/MapboxNavigation.java b/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/navigation/MapboxNavigation.java index 824839e9bf2..bca8145876d 100644 --- a/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/navigation/MapboxNavigation.java +++ b/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/navigation/MapboxNavigation.java @@ -428,8 +428,8 @@ public void endNavigation() { context.unbindService(this); isBound = false; cameraEngine = null; - navigationEventDispatcher.onNavigationEvent(false); } + navigationEventDispatcher.onNavigationEvent(false); } // Listeners diff --git a/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/navigation/NavigationHelper.java b/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/navigation/NavigationHelper.java index 71d20c81a97..b33fd344a17 100644 --- a/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/navigation/NavigationHelper.java +++ b/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/navigation/NavigationHelper.java @@ -199,6 +199,9 @@ static List checkMilestones(RouteProgress previousRouteProgress, static boolean isUserOffRoute(NewLocationModel newLocationModel, RouteProgress routeProgress, OffRouteCallback callback) { MapboxNavigationOptions options = newLocationModel.mapboxNavigation().options(); + if (!options.enableOffRouteDetection()) { + return false; + } Location location = newLocationModel.location(); OffRoute offRoute = newLocationModel.mapboxNavigation().getOffRouteEngine(); setOffRouteDetectorCallback(offRoute, callback); diff --git a/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/navigation/MapboxNavigationTest.java b/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/navigation/MapboxNavigationTest.java index 99c9c3326ae..dbcbb69cce8 100644 --- a/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/navigation/MapboxNavigationTest.java +++ b/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/navigation/MapboxNavigationTest.java @@ -13,7 +13,6 @@ import com.mapbox.services.android.telemetry.location.LocationEngine; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import static com.mapbox.services.android.navigation.v5.navigation.NavigationConstants.BANNER_INSTRUCTION_MILESTONE_ID; @@ -21,6 +20,7 @@ import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertNotNull; import static junit.framework.Assert.assertNotSame; +import static junit.framework.Assert.assertNull; import static junit.framework.Assert.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; @@ -31,7 +31,7 @@ public class MapboxNavigationTest extends BaseTest { private MapboxNavigation navigation; @Before - public void setUp() throws Exception { + public void before() throws Exception { navigation = new MapboxNavigation(mock(Context.class), ACCESS_TOKEN, mock(NavigationTelemetry.class), mock(LocationEngine.class)); } @@ -70,6 +70,36 @@ public void defaultEngines_didGetInitialized() throws Exception { assertNotNull(navigation.getOffRouteEngine()); } + @Test + public void offRouteEngine_doesNotGetInitializedWithFalseOption() throws Exception { + MapboxNavigationOptions options = MapboxNavigationOptions.builder() + .enableOffRouteDetection(false) + .build(); + navigation = new MapboxNavigation(mock(Context.class), ACCESS_TOKEN, options, mock(NavigationTelemetry.class), + mock(LocationEngine.class)); + assertNull(navigation.getOffRouteEngine()); + } + + @Test + public void snapToRouteEngine_doesNotGetInitializedWithFalseOption() throws Exception { + MapboxNavigationOptions options = MapboxNavigationOptions.builder() + .snapToRoute(false) + .build(); + navigation = new MapboxNavigation(mock(Context.class), ACCESS_TOKEN, options, mock(NavigationTelemetry.class), + mock(LocationEngine.class)); + assertNull(navigation.getSnapEngine()); + } + + @Test + public void fasterRouteEngine_doesNotGetInitializedWithFalseOption() throws Exception { + MapboxNavigationOptions options = MapboxNavigationOptions.builder() + .enableFasterRouteDetection(false) + .build(); + navigation = new MapboxNavigation(mock(Context.class), ACCESS_TOKEN, options, mock(NavigationTelemetry.class), + mock(LocationEngine.class)); + assertNull(navigation.getFasterRouteEngine()); + } + @Test public void addMilestone_milestoneDidGetAdded() throws Exception { Milestone milestone = new StepMilestone.Builder().build(); @@ -158,7 +188,6 @@ public void getLocationEngine_returnsCorrectLocationEngine() throws Exception { } @Test - @Ignore public void endNavigation_doesSendFalseToNavigationEvent() throws Exception { NavigationEventListener navigationEventListener = mock(NavigationEventListener.class); navigation.addNavigationEventListener(navigationEventListener); diff --git a/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/navigation/NavigationHelperTest.java b/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/navigation/NavigationHelperTest.java index bf096c97694..d2d91209e43 100644 --- a/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/navigation/NavigationHelperTest.java +++ b/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/navigation/NavigationHelperTest.java @@ -1,5 +1,6 @@ package com.mapbox.services.android.navigation.v5.navigation; +import android.content.Context; import android.location.Location; import com.google.gson.Gson; @@ -7,12 +8,18 @@ import com.mapbox.api.directions.v5.DirectionsAdapterFactory; import com.mapbox.api.directions.v5.models.DirectionsResponse; import com.mapbox.api.directions.v5.models.DirectionsRoute; +import com.mapbox.core.constants.Constants; import com.mapbox.geojson.Point; import com.mapbox.geojson.utils.PolylineUtils; import com.mapbox.services.android.navigation.BuildConfig; import com.mapbox.services.android.navigation.v5.BaseTest; +import com.mapbox.services.android.navigation.v5.milestone.Milestone; +import com.mapbox.services.android.navigation.v5.milestone.StepMilestone; +import com.mapbox.services.android.navigation.v5.milestone.Trigger; +import com.mapbox.services.android.navigation.v5.milestone.TriggerProperty; +import com.mapbox.services.android.navigation.v5.offroute.OffRouteCallback; import com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress; -import com.mapbox.core.constants.Constants; +import com.mapbox.services.android.telemetry.location.LocationEngine; import org.junit.Before; import org.junit.Test; @@ -22,8 +29,13 @@ import java.util.List; +import static com.mapbox.services.android.navigation.v5.navigation.NavigationHelper.checkMilestones; +import static com.mapbox.services.android.navigation.v5.navigation.NavigationHelper.isUserOffRoute; import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertFalse; +import static junit.framework.Assert.assertNotSame; import static junit.framework.Assert.assertTrue; +import static org.mockito.Mockito.mock; @RunWith(RobolectricTestRunner.class) @Config(constants = BuildConfig.class, sdk = 25) @@ -89,29 +101,40 @@ public void increaseIndex_stepIndexResetsOnLegIndexIncrease() throws Exception { assertEquals(0, newIndices.stepIndex()); } - // @Test - // public void checkMilestones_onlyTriggeredMilestonesGetReturned() throws Exception { - // RouteProgress routeProgress = routeProgressBuilder - // .legIndex(0) - // .stepIndex(0) - // .build(); - // MapboxNavigationOptions options = MapboxNavigationOptions.builder() - // .defaultMilestonesEnabled(false).build(); - // MapboxNavigation mapboxNavigation = new MapboxNavigation( - // RuntimeEnvironment.application.getApplicationContext(), "pk.XXX", options); - // mapboxNavigation.addMilestone(new StepMilestone.Builder() - // .setTrigger(Trigger.eq(TriggerProperty.STEP_INDEX, 0)) - // .setIdentifier(1001).build()); - // mapboxNavigation.addMilestone(new StepMilestone.Builder() - // .setTrigger(Trigger.eq(TriggerProperty.STEP_INDEX, 4)) - // .setIdentifier(1002).build()); - // - // List triggeredMilestones - // = checkMilestones(routeProgress, routeProgress, mapboxNavigation); - // assertEquals(1, triggeredMilestones.size()); - // assertEquals(1001, triggeredMilestones.get(0).getIdentifier()); - // assertNotSame(1002, triggeredMilestones.get(0).getIdentifier()); - // } + @Test + public void checkMilestones_onlyTriggeredMilestonesGetReturned() throws Exception { + RouteProgress routeProgress = routeProgressBuilder + .legIndex(0) + .stepIndex(0) + .build(); + MapboxNavigationOptions options = MapboxNavigationOptions.builder() + .defaultMilestonesEnabled(false).build(); + MapboxNavigation mapboxNavigation = new MapboxNavigation(mock(Context.class), ACCESS_TOKEN, options, + mock(NavigationTelemetry.class), mock(LocationEngine.class)); + mapboxNavigation.addMilestone(new StepMilestone.Builder() + .setTrigger(Trigger.eq(TriggerProperty.STEP_INDEX, 0)) + .setIdentifier(1001).build()); + mapboxNavigation.addMilestone(new StepMilestone.Builder() + .setTrigger(Trigger.eq(TriggerProperty.STEP_INDEX, 4)) + .setIdentifier(1002).build()); + + List triggeredMilestones + = checkMilestones(routeProgress, routeProgress, mapboxNavigation); + assertEquals(1, triggeredMilestones.size()); + assertEquals(1001, triggeredMilestones.get(0).getIdentifier()); + assertNotSame(1002, triggeredMilestones.get(0).getIdentifier()); + } + + @Test + public void offRouteDetectionDisabled_isOffRouteReturnsFalse() throws Exception { + MapboxNavigationOptions options = MapboxNavigationOptions.builder() + .enableOffRouteDetection(false) + .build(); + MapboxNavigation mapboxNavigation = new MapboxNavigation(mock(Context.class), ACCESS_TOKEN, options, + mock(NavigationTelemetry.class), mock(LocationEngine.class)); + NewLocationModel model = NewLocationModel.create(mock(Location.class), mapboxNavigation); + assertFalse(isUserOffRoute(model, mock(RouteProgress.class), mock(OffRouteCallback.class))); + } @Test public void stepDistanceRemaining_returnsZeroWhenPositionsEqualEachOther() throws Exception {