Skip to content

Commit

Permalink
Fix OffRoute detection disabled bug (#783)
Browse files Browse the repository at this point in the history
* Fix OffRoute detection disabled bug

* Cleanup test
  • Loading branch information
danesfeder authored Mar 22, 2018
1 parent e1dd605 commit d998ece
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,8 @@ public void endNavigation() {
context.unbindService(this);
isBound = false;
cameraEngine = null;
navigationEventDispatcher.onNavigationEvent(false);
}
navigationEventDispatcher.onNavigationEvent(false);
}

// Listeners
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ static List<Milestone> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
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;
import static com.mapbox.services.android.navigation.v5.navigation.NavigationConstants.VOICE_INSTRUCTION_MILESTONE_ID;
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;
Expand All @@ -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));
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
package com.mapbox.services.android.navigation.v5.navigation;

import android.content.Context;
import android.location.Location;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
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;
Expand All @@ -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)
Expand Down Expand Up @@ -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<Milestone> 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<Milestone> 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 {
Expand Down

0 comments on commit d998ece

Please sign in to comment.