Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix OffRoute detection disabled bug #783

Merged
merged 2 commits into from
Mar 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally is good to structure the tests following the triple A 👀 https://speakerdeck.com/guardiola31337/elegant-unit-testing-mobilization-2016?slide=40 http://wiki.c2.com/?ArrangeActAssert
If the Arrange part gets too long we should create factory or builder methods.

This applies to other tests included in MapboxNavigationTest.

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());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally is preferable having only 1 assertion per test 👀 https://speakerdeck.com/guardiola31337/elegant-unit-testing-mobilization-2016?slide=57

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