From 49ca6dd72d1b859b930a618d7b7d690248fddc6b Mon Sep 17 00:00:00 2001 From: Bram Jongebloet Date: Thu, 20 Feb 2020 11:06:50 +0100 Subject: [PATCH 1/4] version name change --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 6dcfb3906..a80db6c4c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ # Project-wide Gradle settings. -VERSION_NAME=0.20.0-SNAPSHOT +VERSION_NAME=1.0.2 GROUP=com.mapbox.mapboxsdk POM_URL=https://github.com/mapbox/mapbox-navigation-android From 615b14fc8a97e53c591c68965bc92fc46999c154 Mon Sep 17 00:00:00 2001 From: Gino Biervliet Date: Fri, 10 Jul 2020 11:26:54 +0200 Subject: [PATCH 2/4] FMANDROID-433 updated robolectric --- gradle/dependencies.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle/dependencies.gradle b/gradle/dependencies.gradle index fb05d5970..1742a87a2 100644 --- a/gradle/dependencies.gradle +++ b/gradle/dependencies.gradle @@ -26,7 +26,7 @@ ext { espressoVersion : '3.0.2', spoonRunner : '1.6.2', commonsIO : '2.6', - robolectric : '3.8', + robolectric : '4.0', lifecycle : '1.1.1', picasso : '2.71828', gmsLocation : '15.0.1' From ee3de53c3b8d159a984ed7e136a802726980c92c Mon Sep 17 00:00:00 2001 From: Gino Biervliet Date: Fri, 10 Jul 2020 13:50:40 +0200 Subject: [PATCH 3/4] FMANDROID-433 bugfix --- .../v5/location/replay/ReplayRouteLocationConverter.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/location/replay/ReplayRouteLocationConverter.java b/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/location/replay/ReplayRouteLocationConverter.java index 4974a84ca..d921cc44c 100644 --- a/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/location/replay/ReplayRouteLocationConverter.java +++ b/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/location/replay/ReplayRouteLocationConverter.java @@ -62,6 +62,10 @@ void initializeTime() { * @return list of sliced {@link Point}s. */ List sliceRoute(LineString lineString) { + if (lineString == null || lineString.coordinates().isEmpty()) { + return Collections.emptyList(); + } + double distanceMeters = TurfMeasurement.length(lineString, TurfConstants.UNIT_METERS); if (distanceMeters <= 0) { return Collections.emptyList(); From 028c76e11e4ff03f2044fbe6616207a0fdcc4f26 Mon Sep 17 00:00:00 2001 From: Gino Biervliet Date: Fri, 10 Jul 2020 14:08:14 +0200 Subject: [PATCH 4/4] FMANDROID-433 created tests and mayor cleanup making tests run again --- .../v5/navigation/NavigationHelper.java | 4 + .../v5/TestRouteProgressBuilder.java | 3 +- .../ReplayRouteLocationConverterTest.java | 28 + .../v5/milestone/TriggerPropertyTest.java | 222 +++-- .../navigation/v5/milestone/TriggerTest.java | 699 +++++++------- .../navigation/FasterRouteDetectorTest.java | 4 +- .../v5/navigation/MapboxNavigationTest.java | 6 +- .../NavigationEventDispatcherTest.java | 707 +++++++-------- .../v5/navigation/NavigationHelperTest.java | 857 +++++++++--------- .../NavigationLocationEngineTest.java | 66 -- .../NavigationLocationEngineUpdaterTest.java | 75 -- .../NavigationRouteProcessorTest.java | 178 ---- .../navigation/v5/snap/SnapToRouteTest.java | 29 +- 13 files changed, 1286 insertions(+), 1592 deletions(-) create mode 100644 libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/location/replay/ReplayRouteLocationConverterTest.java delete mode 100644 libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/navigation/NavigationLocationEngineTest.java delete mode 100644 libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/navigation/NavigationLocationEngineUpdaterTest.java delete mode 100644 libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/navigation/NavigationRouteProcessorTest.java 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 d740cb03f..11ea394d4 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 @@ -342,6 +342,8 @@ public static StepIntersection findCurrentIntersection(@NonNull List> measuredIntersections, double stepDistanceTraveled) { for (Pair measuredIntersection : measuredIntersections) { + if (measuredIntersection.first == null) + return intersections.get(0); double intersectionDistance = measuredIntersection.second; int intersectionIndex = measuredIntersections.indexOf(measuredIntersection); int nextIntersectionIndex = intersectionIndex + ONE_INDEX; @@ -486,6 +488,8 @@ static boolean isUserOffRoute(NavigationLocationUpdate navigationLocationUpdate, static boolean shouldCheckFasterRoute(NavigationLocationUpdate navigationLocationUpdate, RouteProgress routeProgress) { + if(navigationLocationUpdate == null) + return false; FasterRoute fasterRoute = navigationLocationUpdate.mapboxNavigation().getFasterRouteEngine(); return fasterRoute.shouldCheckFasterRoute(navigationLocationUpdate.location(), routeProgress); } diff --git a/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/TestRouteProgressBuilder.java b/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/TestRouteProgressBuilder.java index b1631e017..143f30de3 100644 --- a/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/TestRouteProgressBuilder.java +++ b/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/TestRouteProgressBuilder.java @@ -1,7 +1,8 @@ package com.mapbox.services.android.navigation.v5; +import android.util.Pair; + import androidx.annotation.NonNull; -import androidx.core.util.Pair; import com.mapbox.api.directions.v5.models.DirectionsRoute; import com.mapbox.api.directions.v5.models.LegStep; diff --git a/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/location/replay/ReplayRouteLocationConverterTest.java b/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/location/replay/ReplayRouteLocationConverterTest.java new file mode 100644 index 000000000..036cf07b9 --- /dev/null +++ b/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/location/replay/ReplayRouteLocationConverterTest.java @@ -0,0 +1,28 @@ +package com.mapbox.services.android.navigation.v5.location.replay; + +import com.mapbox.geojson.LineString; +import com.mapbox.geojson.Point; + +import org.junit.Test; + +import java.util.List; + +public class ReplayRouteLocationConverterTest { + + + @Test + public void testSliceRouteWithEmptyLineString() { + ReplayRouteLocationConverter replayRouteLocationConverter = new ReplayRouteLocationConverter(null, 100, 1); + List result = replayRouteLocationConverter.sliceRoute(LineString.fromJson("")); + + assert (result.isEmpty()); + } + + @Test + public void testSliceRouteWithNullLineString() { + ReplayRouteLocationConverter replayRouteLocationConverter = new ReplayRouteLocationConverter(null, 100, 1); + List result = replayRouteLocationConverter.sliceRoute(null); + + assert (result.isEmpty()); + } +} \ No newline at end of file diff --git a/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/milestone/TriggerPropertyTest.java b/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/milestone/TriggerPropertyTest.java index 9bf5e22bd..1a1e39812 100644 --- a/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/milestone/TriggerPropertyTest.java +++ b/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/milestone/TriggerPropertyTest.java @@ -5,7 +5,6 @@ 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.services.android.navigation.BuildConfig; import com.mapbox.services.android.navigation.v5.BaseTest; import com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress; @@ -14,125 +13,122 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RobolectricTestRunner; -import org.robolectric.annotation.Config; @RunWith(RobolectricTestRunner.class) -@Config(constants = BuildConfig.class) public class TriggerPropertyTest extends BaseTest { + private static final String ROUTE_FIXTURE = "directions_v5_precision_6.json"; + + @Test + public void stepDurationRemainingProperty_onlyPassesValidationWhenEqual() throws Exception { + RouteProgress routeProgress = buildTestRouteProgressForTrigger(); + double stepDuration = routeProgress.currentLegProgress().currentStepProgress().durationRemaining(); + + for (int i = 10; i > 0; i--) { + Milestone milestone = new StepMilestone.Builder() + .setTrigger( + Trigger.eq(TriggerProperty.STEP_DURATION_REMAINING_SECONDS, (stepDuration / i)) + ).build(); + + boolean result = milestone.isOccurring(routeProgress, routeProgress); + if ((stepDuration / i) == stepDuration) { + Assert.assertTrue(result); + } else { + Assert.assertFalse(result); + } + } + } - private static final String ROUTE_FIXTURE = "directions_v5_precision_6.json"; - - @Test - public void stepDurationRemainingProperty_onlyPassesValidationWhenEqual() throws Exception { - RouteProgress routeProgress = buildTestRouteProgressForTrigger(); - double stepDuration = routeProgress.currentLegProgress().currentStepProgress().durationRemaining(); - - for (int i = 10; i > 0; i--) { - Milestone milestone = new StepMilestone.Builder() - .setTrigger( - Trigger.eq(TriggerProperty.STEP_DURATION_REMAINING_SECONDS, (stepDuration / i)) - ).build(); - - boolean result = milestone.isOccurring(routeProgress, routeProgress); - if ((stepDuration / i) == stepDuration) { - Assert.assertTrue(result); - } else { - Assert.assertFalse(result); - } + @Test + public void stepDistanceRemainingProperty_onlyPassesValidationWhenEqual() throws Exception { + RouteProgress routeProgress = buildTestRouteProgressForTrigger(); + double stepDistance = routeProgress.currentLegProgress().currentStepProgress().distanceRemaining(); + + for (int i = 10; i > 0; i--) { + Milestone milestone = new StepMilestone.Builder() + .setTrigger( + Trigger.eq(TriggerProperty.STEP_DISTANCE_REMAINING_METERS, (stepDistance / i)) + ).build(); + + boolean result = milestone.isOccurring(routeProgress, routeProgress); + if ((stepDistance / i) == stepDistance) { + Assert.assertTrue(result); + } else { + Assert.assertFalse(result); + } + } } - } - - @Test - public void stepDistanceRemainingProperty_onlyPassesValidationWhenEqual() throws Exception { - RouteProgress routeProgress = buildTestRouteProgressForTrigger(); - double stepDistance = routeProgress.currentLegProgress().currentStepProgress().distanceRemaining(); - - for (int i = 10; i > 0; i--) { - Milestone milestone = new StepMilestone.Builder() - .setTrigger( - Trigger.eq(TriggerProperty.STEP_DISTANCE_REMAINING_METERS, (stepDistance / i)) - ).build(); - - boolean result = milestone.isOccurring(routeProgress, routeProgress); - if ((stepDistance / i) == stepDistance) { - Assert.assertTrue(result); - } else { - Assert.assertFalse(result); - } + + @Test + public void stepDistanceTotalProperty_onlyPassesValidationWhenEqual() throws Exception { + RouteProgress routeProgress = buildTestRouteProgressForTrigger(); + double stepDistanceTotal = routeProgress.currentLegProgress().currentStep().distance(); + + for (int i = 10; i > 0; i--) { + Milestone milestone = new StepMilestone.Builder() + .setTrigger( + Trigger.eq(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, (stepDistanceTotal / i)) + ).build(); + + boolean result = milestone.isOccurring(routeProgress, routeProgress); + if ((stepDistanceTotal / i) == stepDistanceTotal) { + Assert.assertTrue(result); + } else { + Assert.assertFalse(result); + } + } } - } - - @Test - public void stepDistanceTotalProperty_onlyPassesValidationWhenEqual() throws Exception { - RouteProgress routeProgress = buildTestRouteProgressForTrigger(); - double stepDistanceTotal = routeProgress.currentLegProgress().currentStep().distance(); - - for (int i = 10; i > 0; i--) { - Milestone milestone = new StepMilestone.Builder() - .setTrigger( - Trigger.eq(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, (stepDistanceTotal / i)) - ).build(); - - boolean result = milestone.isOccurring(routeProgress, routeProgress); - if ((stepDistanceTotal / i) == stepDistanceTotal) { - Assert.assertTrue(result); - } else { - Assert.assertFalse(result); - } + + @Test + public void stepDurationTotalProperty_onlyPassesValidationWhenEqual() throws Exception { + RouteProgress routeProgress = buildTestRouteProgressForTrigger(); + double stepDurationTotal = routeProgress.currentLegProgress().currentStep().duration(); + + for (int i = 10; i > 0; i--) { + Milestone milestone = new StepMilestone.Builder() + .setTrigger( + Trigger.eq(TriggerProperty.STEP_DURATION_TOTAL_SECONDS, (stepDurationTotal / i)) + ).build(); + + boolean result = milestone.isOccurring(routeProgress, routeProgress); + if ((stepDurationTotal / i) == stepDurationTotal) { + Assert.assertTrue(result); + } else { + Assert.assertFalse(result); + } + } } - } - - @Test - public void stepDurationTotalProperty_onlyPassesValidationWhenEqual() throws Exception { - RouteProgress routeProgress = buildTestRouteProgressForTrigger(); - double stepDurationTotal = routeProgress.currentLegProgress().currentStep().duration(); - - for (int i = 10; i > 0; i--) { - Milestone milestone = new StepMilestone.Builder() - .setTrigger( - Trigger.eq(TriggerProperty.STEP_DURATION_TOTAL_SECONDS, (stepDurationTotal / i)) - ).build(); - - boolean result = milestone.isOccurring(routeProgress, routeProgress); - if ((stepDurationTotal / i) == stepDurationTotal) { - Assert.assertTrue(result); - } else { - Assert.assertFalse(result); - } + + @Test + public void stepIndexProperty_onlyPassesValidationWhenEqual() throws Exception { + RouteProgress routeProgress = buildTestRouteProgressForTrigger(); + int stepIndex = routeProgress.currentLegProgress().stepIndex(); + + for (int i = 10; i > 0; i--) { + Milestone milestone = new StepMilestone.Builder() + .setTrigger( + Trigger.eq(TriggerProperty.STEP_INDEX, Math.abs(stepIndex - i)) + ).build(); + + boolean result = milestone.isOccurring(routeProgress, routeProgress); + if (Math.abs(stepIndex - i) == stepIndex) { + Assert.assertTrue(result); + } else { + Assert.assertFalse(result); + } + } } - } - - @Test - public void stepIndexProperty_onlyPassesValidationWhenEqual() throws Exception { - RouteProgress routeProgress = buildTestRouteProgressForTrigger(); - int stepIndex = routeProgress.currentLegProgress().stepIndex(); - - for (int i = 10; i > 0; i--) { - Milestone milestone = new StepMilestone.Builder() - .setTrigger( - Trigger.eq(TriggerProperty.STEP_INDEX, Math.abs(stepIndex - i)) - ).build(); - - boolean result = milestone.isOccurring(routeProgress, routeProgress); - if (Math.abs(stepIndex - i) == stepIndex) { - Assert.assertTrue(result); - } else { - Assert.assertFalse(result); - } + + private RouteProgress buildTestRouteProgressForTrigger() throws Exception { + Gson gson = new GsonBuilder() + .registerTypeAdapterFactory(DirectionsAdapterFactory.create()).create(); + String body = loadJsonFixture(ROUTE_FIXTURE); + DirectionsResponse response = gson.fromJson(body, DirectionsResponse.class); + + DirectionsRoute route = response.routes().get(0); + double distanceRemaining = route.distance(); + double legDistanceRemaining = route.legs().get(0).distance(); + double stepDistanceRemaining = route.legs().get(0).steps().get(0).distance(); + return buildTestRouteProgress(route, stepDistanceRemaining, + legDistanceRemaining, distanceRemaining, 1, 0); } - } - - private RouteProgress buildTestRouteProgressForTrigger() throws Exception { - Gson gson = new GsonBuilder() - .registerTypeAdapterFactory(DirectionsAdapterFactory.create()).create(); - String body = loadJsonFixture(ROUTE_FIXTURE); - DirectionsResponse response = gson.fromJson(body, DirectionsResponse.class); - - DirectionsRoute route = response.routes().get(0); - double distanceRemaining = route.distance(); - double legDistanceRemaining = route.legs().get(0).distance(); - double stepDistanceRemaining = route.legs().get(0).steps().get(0).distance(); - return buildTestRouteProgress(route, stepDistanceRemaining, - legDistanceRemaining, distanceRemaining, 1, 0); - } } diff --git a/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/milestone/TriggerTest.java b/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/milestone/TriggerTest.java index 6941e0c07..347edae21 100644 --- a/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/milestone/TriggerTest.java +++ b/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/milestone/TriggerTest.java @@ -5,7 +5,6 @@ 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.services.android.navigation.BuildConfig; import com.mapbox.services.android.navigation.v5.BaseTest; import com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress; @@ -14,358 +13,356 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RobolectricTestRunner; -import org.robolectric.annotation.Config; @RunWith(RobolectricTestRunner.class) -@Config(constants = BuildConfig.class) public class TriggerTest extends BaseTest { - private static final String ROUTE_FIXTURE = "directions_v5_precision_6.json"; - - @Test - public void triggerAll_noStatementsProvidedResultsInTrue() throws Exception { - RouteProgress routeProgress = buildTriggerRouteProgress(); - Milestone milestone = new StepMilestone.Builder() - .setTrigger(Trigger.all()) - .build(); - - boolean result = milestone.isOccurring(routeProgress, routeProgress); - - Assert.assertTrue(result); - } - - @Test - public void triggerAll_validatesAllStatements() throws Exception { - RouteProgress routeProgress = buildTriggerRouteProgress(); - Milestone milestone = new StepMilestone.Builder() - .setTrigger(Trigger.all( - Trigger.gt(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, 100d), - Trigger.eq(TriggerProperty.STEP_INDEX, 1) - )) - .build(); - - boolean result = milestone.isOccurring(routeProgress, routeProgress); - - Assert.assertTrue(result); - } - - @Test - public void triggerAll_oneConditionsFalse() throws Exception { - RouteProgress routeProgress = buildTriggerRouteProgress(); - Milestone milestone = new StepMilestone.Builder() - .setTrigger(Trigger.all( - Trigger.gt(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, 100d), - Trigger.eq(TriggerProperty.NEW_STEP, TriggerProperty.FALSE) - )) - .build(); - - boolean result = milestone.isOccurring(routeProgress, routeProgress); - - Assert.assertFalse(result); - } - - @Test - public void triggerAny_noConditionsAreTrue() throws Exception { - RouteProgress routeProgress = buildTriggerRouteProgress(); - Milestone milestone = new StepMilestone.Builder() - .setTrigger(Trigger.any( - Trigger.gt(TriggerProperty.STEP_DURATION_REMAINING_SECONDS, 200d), - Trigger.lt(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, 100d) - )) - .build(); - - boolean result = milestone.isOccurring(routeProgress, routeProgress); - - Assert.assertFalse(result); - } - - @Test - public void triggerAny_validatesAllStatementsTillOnesTrue() throws Exception { - RouteProgress routeProgress = buildTriggerRouteProgress(); - Milestone milestone = new StepMilestone.Builder() - .setTrigger(Trigger.any( - Trigger.eq(TriggerProperty.STEP_INDEX, 1), - Trigger.gt(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, 100d), - Trigger.eq(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, 100d) - )) - .build(); - - boolean result = milestone.isOccurring(routeProgress, routeProgress); - - Assert.assertTrue(result); - } - - @Test - public void triggerAny_oneConditionsTrue() throws Exception { - RouteProgress routeProgress = buildTriggerRouteProgress(); - Milestone milestone = new StepMilestone.Builder() - .setTrigger(Trigger.any( - Trigger.gt(TriggerProperty.STEP_DURATION_REMAINING_SECONDS, 100d), - Trigger.gt(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, 100d) - )) - .build(); - - boolean result = milestone.isOccurring(routeProgress, routeProgress); - - Assert.assertTrue(result); - } - - - @Test - public void triggerNone_noConditionsAreTrue() throws Exception { - RouteProgress routeProgress = buildTriggerRouteProgress(); - Milestone milestone = new StepMilestone.Builder() - .setTrigger(Trigger.none( - Trigger.gt(TriggerProperty.STEP_DURATION_REMAINING_SECONDS, 200d), - Trigger.lt(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, 100d) - )) - .build(); - - boolean result = milestone.isOccurring(routeProgress, routeProgress); - - Assert.assertTrue(result); - } - - @Test - public void triggerNone_validatesAllStatementsTillOnesTrue() throws Exception { - RouteProgress routeProgress = buildTriggerRouteProgress(); - Milestone milestone = new StepMilestone.Builder() - .setTrigger(Trigger.none( - Trigger.neq(TriggerProperty.STEP_INDEX, 1), - Trigger.lt(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, 100d) - )) - .build(); - - boolean result = milestone.isOccurring(routeProgress, routeProgress); - - Assert.assertTrue(result); - } - - @Test - public void triggerNone_onoConditionsTrue() throws Exception { - RouteProgress routeProgress = buildTriggerRouteProgress(); - Milestone milestone = new StepMilestone.Builder() - .setTrigger(Trigger.none( - Trigger.gt(TriggerProperty.STEP_DURATION_REMAINING_SECONDS, 100d), - Trigger.gt(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, 100d) - )) - .build(); - - boolean result = milestone.isOccurring(routeProgress, routeProgress); - - Assert.assertFalse(result); - } - - @Test - public void greaterThan_validatesToTrue() throws Exception { - RouteProgress routeProgress = buildTriggerRouteProgress(); - Milestone milestone = new StepMilestone.Builder() - .setTrigger( - Trigger.gt(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, 100d) - ) - .build(); - - boolean result = milestone.isOccurring(routeProgress, routeProgress); - - Assert.assertTrue(result); - } - - @Test - public void greaterThan_validatesToFalse() throws Exception { - RouteProgress routeProgress = buildTriggerRouteProgress(); - Milestone milestone = new StepMilestone.Builder() - .setTrigger( - Trigger.gt(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, 10000d) - ) - .build(); - - boolean result = milestone.isOccurring(routeProgress, routeProgress); - - Assert.assertFalse(result); - } - - @Test - public void greaterThanEqual_validatesToTrue() throws Exception { - RouteProgress routeProgress = buildTriggerRouteProgress(); - Milestone milestone = new StepMilestone.Builder() - .setTrigger( - Trigger.gte(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, 100d) - ) - .build(); - - boolean result = milestone.isOccurring(routeProgress, routeProgress); - - Assert.assertTrue(result); - } - - @Test - public void greaterThanEqual_equalStillValidatesToTrue() throws Exception { - RouteProgress routeProgress = buildTriggerRouteProgress(); - Milestone milestone = new StepMilestone.Builder() - .setTrigger( - Trigger.gte(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, - routeProgress.currentLegProgress().currentStep().distance()) - ) - .build(); - - boolean result = milestone.isOccurring(routeProgress, routeProgress); - - Assert.assertTrue(result); - } - - @Test - public void greaterThanEqual_validatesToFalse() throws Exception { - RouteProgress routeProgress = buildTriggerRouteProgress(); - Milestone milestone = new StepMilestone.Builder() - .setTrigger( - Trigger.gte(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, 10000d) - ) - .build(); - - boolean result = milestone.isOccurring(routeProgress, routeProgress); - - Assert.assertFalse(result); - } - - @Test - public void lessThan_validatesToTrue() throws Exception { - RouteProgress routeProgress = buildTriggerRouteProgress(); - Milestone milestone = new StepMilestone.Builder() - .setTrigger( - Trigger.lt(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, 10000d) - ) - .build(); - - boolean result = milestone.isOccurring(routeProgress, routeProgress); - - Assert.assertTrue(result); - } - - @Test - public void lessThan_validatesToFalse() throws Exception { - RouteProgress routeProgress = buildTriggerRouteProgress(); - Milestone milestone = new StepMilestone.Builder() - .setTrigger( - Trigger.lt(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, 100d) - ) - .build(); - - boolean result = milestone.isOccurring(routeProgress, routeProgress); - - Assert.assertFalse(result); - } - - @Test - public void lessThanEqual_validatesToTrue() throws Exception { - RouteProgress routeProgress = buildTriggerRouteProgress(); - Milestone milestone = new StepMilestone.Builder() - .setTrigger( - Trigger.lte(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, 10000d) - ) - .build(); - - boolean result = milestone.isOccurring(routeProgress, routeProgress); - - Assert.assertTrue(result); - } - - @Test - public void lessThanEqual_equalStillValidatesToTrue() throws Exception { - RouteProgress routeProgress = buildTriggerRouteProgress(); - Milestone milestone = new StepMilestone.Builder() - .setTrigger( - Trigger.lte(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, - routeProgress.currentLegProgress().currentStep().distance()) - ) - .build(); - - boolean result = milestone.isOccurring(routeProgress, routeProgress); - - Assert.assertTrue(result); - } - - @Test - public void lessThanEqual_validatesToFalse() throws Exception { - RouteProgress routeProgress = buildTriggerRouteProgress(); - Milestone milestone = new StepMilestone.Builder() - .setTrigger( - Trigger.lte(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, 100d) - ) - .build(); - - boolean result = milestone.isOccurring(routeProgress, routeProgress); - - Assert.assertFalse(result); - } - - @Test - public void equal_validatesToFalse() throws Exception { - RouteProgress routeProgress = buildTriggerRouteProgress(); - Milestone milestone = new StepMilestone.Builder() - .setTrigger( - Trigger.eq(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, 100d) - ) - .build(); - - boolean result = milestone.isOccurring(routeProgress, routeProgress); - - Assert.assertFalse(result); - } - - @Test - public void equal_validatesToTrue() throws Exception { - RouteProgress routeProgress = buildTriggerRouteProgress(); - Milestone milestone = new StepMilestone.Builder() - .setTrigger( - Trigger.eq(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, - routeProgress.currentLegProgress().currentStep().distance()) - ) - .build(); - - boolean result = milestone.isOccurring(routeProgress, routeProgress); - - Assert.assertTrue(result); - } - - @Test - public void notEqual_validatesToFalse() throws Exception { - RouteProgress routeProgress = buildTriggerRouteProgress(); - Milestone milestone = new StepMilestone.Builder() - .setTrigger( - Trigger.neq(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, - routeProgress.currentLegProgress().currentStep().distance()) - ) - .build(); - - boolean result = milestone.isOccurring(routeProgress, routeProgress); - - Assert.assertFalse(result); - } - - @Test - public void notEqual_validatesToTrue() throws Exception { - RouteProgress routeProgress = buildTriggerRouteProgress(); - Milestone milestone = new StepMilestone.Builder() - .setTrigger( - Trigger.neq(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, 100d) - ) - .build(); - - boolean result = milestone.isOccurring(routeProgress, routeProgress); - - Assert.assertTrue(result); - } - - private RouteProgress buildTriggerRouteProgress() throws Exception { - Gson gson = new GsonBuilder() - .registerTypeAdapterFactory(DirectionsAdapterFactory.create()).create(); - String body = loadJsonFixture(ROUTE_FIXTURE); - DirectionsResponse response = gson.fromJson(body, DirectionsResponse.class); - DirectionsRoute route = response.routes().get(0); - int stepDistanceRemaining = (int) route.legs().get(0).steps().get(0).distance(); - int legDistanceRemaining = route.legs().get(0).distance().intValue(); - int routeDistance = route.distance().intValue(); - return buildTestRouteProgress(route, stepDistanceRemaining, legDistanceRemaining, - routeDistance, 1, 0); - } + private static final String ROUTE_FIXTURE = "directions_v5_precision_6.json"; + + @Test + public void triggerAll_noStatementsProvidedResultsInTrue() throws Exception { + RouteProgress routeProgress = buildTriggerRouteProgress(); + Milestone milestone = new StepMilestone.Builder() + .setTrigger(Trigger.all()) + .build(); + + boolean result = milestone.isOccurring(routeProgress, routeProgress); + + Assert.assertTrue(result); + } + + @Test + public void triggerAll_validatesAllStatements() throws Exception { + RouteProgress routeProgress = buildTriggerRouteProgress(); + Milestone milestone = new StepMilestone.Builder() + .setTrigger(Trigger.all( + Trigger.gt(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, 100d), + Trigger.eq(TriggerProperty.STEP_INDEX, 1) + )) + .build(); + + boolean result = milestone.isOccurring(routeProgress, routeProgress); + + Assert.assertTrue(result); + } + + @Test + public void triggerAll_oneConditionsFalse() throws Exception { + RouteProgress routeProgress = buildTriggerRouteProgress(); + Milestone milestone = new StepMilestone.Builder() + .setTrigger(Trigger.all( + Trigger.gt(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, 100d), + Trigger.eq(TriggerProperty.NEW_STEP, TriggerProperty.FALSE) + )) + .build(); + + boolean result = milestone.isOccurring(routeProgress, routeProgress); + + Assert.assertFalse(result); + } + + @Test + public void triggerAny_noConditionsAreTrue() throws Exception { + RouteProgress routeProgress = buildTriggerRouteProgress(); + Milestone milestone = new StepMilestone.Builder() + .setTrigger(Trigger.any( + Trigger.gt(TriggerProperty.STEP_DURATION_REMAINING_SECONDS, 200d), + Trigger.lt(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, 100d) + )) + .build(); + + boolean result = milestone.isOccurring(routeProgress, routeProgress); + + Assert.assertFalse(result); + } + + @Test + public void triggerAny_validatesAllStatementsTillOnesTrue() throws Exception { + RouteProgress routeProgress = buildTriggerRouteProgress(); + Milestone milestone = new StepMilestone.Builder() + .setTrigger(Trigger.any( + Trigger.eq(TriggerProperty.STEP_INDEX, 1), + Trigger.gt(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, 100d), + Trigger.eq(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, 100d) + )) + .build(); + + boolean result = milestone.isOccurring(routeProgress, routeProgress); + + Assert.assertTrue(result); + } + + @Test + public void triggerAny_oneConditionsTrue() throws Exception { + RouteProgress routeProgress = buildTriggerRouteProgress(); + Milestone milestone = new StepMilestone.Builder() + .setTrigger(Trigger.any( + Trigger.gt(TriggerProperty.STEP_DURATION_REMAINING_SECONDS, 100d), + Trigger.gt(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, 100d) + )) + .build(); + + boolean result = milestone.isOccurring(routeProgress, routeProgress); + + Assert.assertTrue(result); + } + + + @Test + public void triggerNone_noConditionsAreTrue() throws Exception { + RouteProgress routeProgress = buildTriggerRouteProgress(); + Milestone milestone = new StepMilestone.Builder() + .setTrigger(Trigger.none( + Trigger.gt(TriggerProperty.STEP_DURATION_REMAINING_SECONDS, 200d), + Trigger.lt(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, 100d) + )) + .build(); + + boolean result = milestone.isOccurring(routeProgress, routeProgress); + + Assert.assertTrue(result); + } + + @Test + public void triggerNone_validatesAllStatementsTillOnesTrue() throws Exception { + RouteProgress routeProgress = buildTriggerRouteProgress(); + Milestone milestone = new StepMilestone.Builder() + .setTrigger(Trigger.none( + Trigger.neq(TriggerProperty.STEP_INDEX, 1), + Trigger.lt(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, 100d) + )) + .build(); + + boolean result = milestone.isOccurring(routeProgress, routeProgress); + + Assert.assertTrue(result); + } + + @Test + public void triggerNone_onoConditionsTrue() throws Exception { + RouteProgress routeProgress = buildTriggerRouteProgress(); + Milestone milestone = new StepMilestone.Builder() + .setTrigger(Trigger.none( + Trigger.gt(TriggerProperty.STEP_DURATION_REMAINING_SECONDS, 100d), + Trigger.gt(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, 100d) + )) + .build(); + + boolean result = milestone.isOccurring(routeProgress, routeProgress); + + Assert.assertFalse(result); + } + + @Test + public void greaterThan_validatesToTrue() throws Exception { + RouteProgress routeProgress = buildTriggerRouteProgress(); + Milestone milestone = new StepMilestone.Builder() + .setTrigger( + Trigger.gt(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, 100d) + ) + .build(); + + boolean result = milestone.isOccurring(routeProgress, routeProgress); + + Assert.assertTrue(result); + } + + @Test + public void greaterThan_validatesToFalse() throws Exception { + RouteProgress routeProgress = buildTriggerRouteProgress(); + Milestone milestone = new StepMilestone.Builder() + .setTrigger( + Trigger.gt(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, 10000d) + ) + .build(); + + boolean result = milestone.isOccurring(routeProgress, routeProgress); + + Assert.assertFalse(result); + } + + @Test + public void greaterThanEqual_validatesToTrue() throws Exception { + RouteProgress routeProgress = buildTriggerRouteProgress(); + Milestone milestone = new StepMilestone.Builder() + .setTrigger( + Trigger.gte(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, 100d) + ) + .build(); + + boolean result = milestone.isOccurring(routeProgress, routeProgress); + + Assert.assertTrue(result); + } + + @Test + public void greaterThanEqual_equalStillValidatesToTrue() throws Exception { + RouteProgress routeProgress = buildTriggerRouteProgress(); + Milestone milestone = new StepMilestone.Builder() + .setTrigger( + Trigger.gte(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, + routeProgress.currentLegProgress().currentStep().distance()) + ) + .build(); + + boolean result = milestone.isOccurring(routeProgress, routeProgress); + + Assert.assertTrue(result); + } + + @Test + public void greaterThanEqual_validatesToFalse() throws Exception { + RouteProgress routeProgress = buildTriggerRouteProgress(); + Milestone milestone = new StepMilestone.Builder() + .setTrigger( + Trigger.gte(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, 10000d) + ) + .build(); + + boolean result = milestone.isOccurring(routeProgress, routeProgress); + + Assert.assertFalse(result); + } + + @Test + public void lessThan_validatesToTrue() throws Exception { + RouteProgress routeProgress = buildTriggerRouteProgress(); + Milestone milestone = new StepMilestone.Builder() + .setTrigger( + Trigger.lt(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, 10000d) + ) + .build(); + + boolean result = milestone.isOccurring(routeProgress, routeProgress); + + Assert.assertTrue(result); + } + + @Test + public void lessThan_validatesToFalse() throws Exception { + RouteProgress routeProgress = buildTriggerRouteProgress(); + Milestone milestone = new StepMilestone.Builder() + .setTrigger( + Trigger.lt(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, 100d) + ) + .build(); + + boolean result = milestone.isOccurring(routeProgress, routeProgress); + + Assert.assertFalse(result); + } + + @Test + public void lessThanEqual_validatesToTrue() throws Exception { + RouteProgress routeProgress = buildTriggerRouteProgress(); + Milestone milestone = new StepMilestone.Builder() + .setTrigger( + Trigger.lte(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, 10000d) + ) + .build(); + + boolean result = milestone.isOccurring(routeProgress, routeProgress); + + Assert.assertTrue(result); + } + + @Test + public void lessThanEqual_equalStillValidatesToTrue() throws Exception { + RouteProgress routeProgress = buildTriggerRouteProgress(); + Milestone milestone = new StepMilestone.Builder() + .setTrigger( + Trigger.lte(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, + routeProgress.currentLegProgress().currentStep().distance()) + ) + .build(); + + boolean result = milestone.isOccurring(routeProgress, routeProgress); + + Assert.assertTrue(result); + } + + @Test + public void lessThanEqual_validatesToFalse() throws Exception { + RouteProgress routeProgress = buildTriggerRouteProgress(); + Milestone milestone = new StepMilestone.Builder() + .setTrigger( + Trigger.lte(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, 100d) + ) + .build(); + + boolean result = milestone.isOccurring(routeProgress, routeProgress); + + Assert.assertFalse(result); + } + + @Test + public void equal_validatesToFalse() throws Exception { + RouteProgress routeProgress = buildTriggerRouteProgress(); + Milestone milestone = new StepMilestone.Builder() + .setTrigger( + Trigger.eq(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, 100d) + ) + .build(); + + boolean result = milestone.isOccurring(routeProgress, routeProgress); + + Assert.assertFalse(result); + } + + @Test + public void equal_validatesToTrue() throws Exception { + RouteProgress routeProgress = buildTriggerRouteProgress(); + Milestone milestone = new StepMilestone.Builder() + .setTrigger( + Trigger.eq(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, + routeProgress.currentLegProgress().currentStep().distance()) + ) + .build(); + + boolean result = milestone.isOccurring(routeProgress, routeProgress); + + Assert.assertTrue(result); + } + + @Test + public void notEqual_validatesToFalse() throws Exception { + RouteProgress routeProgress = buildTriggerRouteProgress(); + Milestone milestone = new StepMilestone.Builder() + .setTrigger( + Trigger.neq(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, + routeProgress.currentLegProgress().currentStep().distance()) + ) + .build(); + + boolean result = milestone.isOccurring(routeProgress, routeProgress); + + Assert.assertFalse(result); + } + + @Test + public void notEqual_validatesToTrue() throws Exception { + RouteProgress routeProgress = buildTriggerRouteProgress(); + Milestone milestone = new StepMilestone.Builder() + .setTrigger( + Trigger.neq(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, 100d) + ) + .build(); + + boolean result = milestone.isOccurring(routeProgress, routeProgress); + + Assert.assertTrue(result); + } + + private RouteProgress buildTriggerRouteProgress() throws Exception { + Gson gson = new GsonBuilder() + .registerTypeAdapterFactory(DirectionsAdapterFactory.create()).create(); + String body = loadJsonFixture(ROUTE_FIXTURE); + DirectionsResponse response = gson.fromJson(body, DirectionsResponse.class); + DirectionsRoute route = response.routes().get(0); + int stepDistanceRemaining = (int) route.legs().get(0).steps().get(0).distance(); + int legDistanceRemaining = route.legs().get(0).distance().intValue(); + int routeDistance = route.distance().intValue(); + return buildTestRouteProgress(route, stepDistanceRemaining, legDistanceRemaining, + routeDistance, 1, 0); + } } diff --git a/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/navigation/FasterRouteDetectorTest.java b/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/navigation/FasterRouteDetectorTest.java index 5f94894bf..2e78db81f 100644 --- a/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/navigation/FasterRouteDetectorTest.java +++ b/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/navigation/FasterRouteDetectorTest.java @@ -115,8 +115,7 @@ private MapboxNavigation buildNavigationWithFasterRouteEnabled() { .build(); Context context = mock(Context.class); when(context.getApplicationContext()).thenReturn(mock(Context.class)); - return new MapboxNavigation(context, ACCESS_TOKEN, options, mock(NavigationTelemetry.class), - mock(LocationEngine.class)); + return new MapboxNavigation(context, ACCESS_TOKEN, options, mock(LocationEngine.class)); } private RouteProgress obtainDefaultRouteProgress() throws Exception { @@ -130,6 +129,7 @@ private DirectionsRoute obtainADirectionsRoute() throws IOException { String body = loadJsonFixture(PRECISION_6); DirectionsResponse response = gson.fromJson(body, DirectionsResponse.class); DirectionsRoute aRoute = response.routes().get(0); + return aRoute; } 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 1f1bc6ab4..a9a91799b 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 @@ -269,15 +269,13 @@ public void getCameraEngine_returnsSimpleCameraWhenNull() throws Exception { private MapboxNavigation buildMapboxNavigation() { Context context = mock(Context.class); when(context.getApplicationContext()).thenReturn(context); - return new MapboxNavigation(context, ACCESS_TOKEN, mock(NavigationTelemetry.class), - mock(LocationEngine.class)); + return new MapboxNavigation(context, ACCESS_TOKEN, mock(LocationEngine.class)); } private MapboxNavigation buildMapboxNavigationWithOptions(MapboxNavigationOptions options) { Context context = mock(Context.class); when(context.getApplicationContext()).thenReturn(context); - return new MapboxNavigation(context, ACCESS_TOKEN, options, mock(NavigationTelemetry.class), - mock(LocationEngine.class)); + return new MapboxNavigation(context, ACCESS_TOKEN, options, mock(LocationEngine.class)); } private int searchForVoiceInstructionMilestone(MapboxNavigation navigation) { diff --git a/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/navigation/NavigationEventDispatcherTest.java b/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/navigation/NavigationEventDispatcherTest.java index a53c256e4..d39a8c6d1 100644 --- a/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/navigation/NavigationEventDispatcherTest.java +++ b/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/navigation/NavigationEventDispatcherTest.java @@ -2,6 +2,7 @@ import android.content.Context; import android.location.Location; + import androidx.annotation.NonNull; import com.google.gson.Gson; @@ -10,7 +11,6 @@ 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.services.android.navigation.BuildConfig; import com.mapbox.services.android.navigation.v5.BaseTest; import com.mapbox.services.android.navigation.v5.milestone.BannerInstructionMilestone; import com.mapbox.services.android.navigation.v5.milestone.Milestone; @@ -28,7 +28,6 @@ import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.robolectric.RobolectricTestRunner; -import org.robolectric.annotation.Config; import static org.junit.Assert.assertNotNull; import static org.mockito.Mockito.mock; @@ -37,359 +36,357 @@ import static org.mockito.Mockito.when; @RunWith(RobolectricTestRunner.class) -@Config(constants = BuildConfig.class, manifest = Config.DEFAULT_MANIFEST_NAME) public class NavigationEventDispatcherTest extends BaseTest { - private static final String PRECISION_6 = "directions_v5_precision_6.json"; - - @Mock - MilestoneEventListener milestoneEventListener; - @Mock - ProgressChangeListener progressChangeListener; - @Mock - NavigationMetricListener metricEventListener; - @Mock - OffRouteListener offRouteListener; - @Mock - NavigationEventListener navigationEventListener; - @Mock - FasterRouteListener fasterRouteListener; - @Mock - Location location; - @Mock - Milestone milestone; - - private NavigationEventDispatcher navigationEventDispatcher; - private MapboxNavigation navigation; - private DirectionsRoute route; - private RouteProgress routeProgress; - - @Before - public void setup() throws Exception { - MockitoAnnotations.initMocks(this); - Context context = mock(Context.class); - when(context.getApplicationContext()).thenReturn(mock(Context.class)); - navigation = new MapboxNavigation(context, ACCESS_TOKEN, mock(NavigationTelemetry.class), - mock(LocationEngine.class)); - navigationEventDispatcher = navigation.getEventDispatcher(); - - Gson gson = new GsonBuilder() - .registerTypeAdapterFactory(DirectionsAdapterFactory.create()).create(); - String body = loadJsonFixture(PRECISION_6); - DirectionsResponse response = gson.fromJson(body, DirectionsResponse.class); - route = response.routes().get(0); - - routeProgress = buildTestRouteProgress(route, 100, 100, 100, 0, 0); - } - - @Test - public void sanity() throws Exception { - NavigationEventDispatcher navigationEventDispatcher = new NavigationEventDispatcher(); - assertNotNull(navigationEventDispatcher); - } - - @Test - public void addMilestoneEventListener_didAddListener() throws Exception { - navigationEventDispatcher.onMilestoneEvent(routeProgress, "", milestone); - verify(milestoneEventListener, times(0)).onMilestoneEvent(routeProgress, "", milestone); - - navigation.addMilestoneEventListener(milestoneEventListener); - navigationEventDispatcher.onMilestoneEvent(routeProgress, "", milestone); - verify(milestoneEventListener, times(1)).onMilestoneEvent(routeProgress, "", milestone); - } - - @Test - public void addMilestoneEventListener_onlyAddsListenerOnce() throws Exception { - navigationEventDispatcher.onMilestoneEvent(routeProgress, "", milestone); - verify(milestoneEventListener, times(0)).onMilestoneEvent(routeProgress, "", milestone); - - navigation.addMilestoneEventListener(milestoneEventListener); - navigation.addMilestoneEventListener(milestoneEventListener); - navigation.addMilestoneEventListener(milestoneEventListener); - navigationEventDispatcher.onMilestoneEvent(routeProgress, "", milestone); - verify(milestoneEventListener, times(1)).onMilestoneEvent(routeProgress, "", milestone); - } - - @Test - public void removeMilestoneEventListener_didRemoveListener() throws Exception { - navigation.addMilestoneEventListener(milestoneEventListener); - navigation.removeMilestoneEventListener(milestoneEventListener); - navigationEventDispatcher.onMilestoneEvent(routeProgress, "", milestone); - verify(milestoneEventListener, times(0)).onMilestoneEvent(routeProgress, "", milestone); - } - - @Test - public void removeMilestoneEventListener_nullRemovesAllListeners() throws Exception { - navigation.addMilestoneEventListener(milestoneEventListener); - navigation.addMilestoneEventListener(mock(MilestoneEventListener.class)); - navigation.addMilestoneEventListener(mock(MilestoneEventListener.class)); - navigation.addMilestoneEventListener(mock(MilestoneEventListener.class)); - - navigation.removeMilestoneEventListener(null); - navigationEventDispatcher.onMilestoneEvent(routeProgress, "", milestone); - verify(milestoneEventListener, times(0)).onMilestoneEvent(routeProgress, "", milestone); - } - - @Test - public void addProgressChangeListener_didAddListener() throws Exception { - navigationEventDispatcher.onProgressChange(location, routeProgress); - verify(progressChangeListener, times(0)).onProgressChange(location, routeProgress); - - navigation.addProgressChangeListener(progressChangeListener); - navigationEventDispatcher.onProgressChange(location, routeProgress); - verify(progressChangeListener, times(1)).onProgressChange(location, routeProgress); - } - - @Test - public void addProgressChangeListener_onlyAddsListenerOnce() throws Exception { - navigationEventDispatcher.onProgressChange(location, routeProgress); - verify(progressChangeListener, times(0)).onProgressChange(location, routeProgress); - - navigation.addProgressChangeListener(progressChangeListener); - navigation.addProgressChangeListener(progressChangeListener); - navigation.addProgressChangeListener(progressChangeListener); - navigationEventDispatcher.onProgressChange(location, routeProgress); - verify(progressChangeListener, times(1)).onProgressChange(location, routeProgress); - } - - @Test - public void removeProgressChangeListener_didRemoveListener() throws Exception { - navigation.addProgressChangeListener(progressChangeListener); - navigation.removeProgressChangeListener(progressChangeListener); - navigationEventDispatcher.onProgressChange(location, routeProgress); - verify(progressChangeListener, times(0)).onProgressChange(location, routeProgress); - } - - @Test - public void removeProgressChangeListener_nullRemovesAllListeners() throws Exception { - navigation.addProgressChangeListener(progressChangeListener); - navigation.addProgressChangeListener(mock(ProgressChangeListener.class)); - navigation.addProgressChangeListener(mock(ProgressChangeListener.class)); - navigation.addProgressChangeListener(mock(ProgressChangeListener.class)); - - navigation.removeProgressChangeListener(null); - navigationEventDispatcher.onProgressChange(location, routeProgress); - verify(progressChangeListener, times(0)).onProgressChange(location, routeProgress); - } - - @Test - public void addOffRouteListener_didAddListener() throws Exception { - navigationEventDispatcher.onUserOffRoute(location); - verify(offRouteListener, times(0)).userOffRoute(location); - - navigation.addOffRouteListener(offRouteListener); - navigationEventDispatcher.onUserOffRoute(location); - verify(offRouteListener, times(1)).userOffRoute(location); - } - - @Test - public void addOffRouteListener_onlyAddsListenerOnce() throws Exception { - navigationEventDispatcher.onUserOffRoute(location); - verify(offRouteListener, times(0)).userOffRoute(location); - - navigation.addOffRouteListener(offRouteListener); - navigation.addOffRouteListener(offRouteListener); - navigation.addOffRouteListener(offRouteListener); - navigationEventDispatcher.onUserOffRoute(location); - verify(offRouteListener, times(1)).userOffRoute(location); - } - - @Test - public void removeOffRouteListener_didRemoveListener() throws Exception { - navigation.addOffRouteListener(offRouteListener); - navigation.removeOffRouteListener(offRouteListener); - navigationEventDispatcher.onUserOffRoute(location); - verify(offRouteListener, times(0)).userOffRoute(location); - } - - @Test - public void removeOffRouteListener_nullRemovesAllListeners() throws Exception { - navigation.addOffRouteListener(offRouteListener); - navigation.addOffRouteListener(mock(OffRouteListener.class)); - navigation.addOffRouteListener(mock(OffRouteListener.class)); - navigation.addOffRouteListener(mock(OffRouteListener.class)); - navigation.addOffRouteListener(mock(OffRouteListener.class)); - - navigation.removeOffRouteListener(null); - navigationEventDispatcher.onUserOffRoute(location); - verify(offRouteListener, times(0)).userOffRoute(location); - } - - @Test - public void addNavigationEventListener_didAddListener() throws Exception { - navigationEventDispatcher.onNavigationEvent(true); - verify(navigationEventListener, times(0)).onRunning(true); - - navigation.addNavigationEventListener(navigationEventListener); - navigationEventDispatcher.onNavigationEvent(true); - verify(navigationEventListener, times(1)).onRunning(true); - } - - @Test - public void addNavigationEventListener_onlyAddsListenerOnce() throws Exception { - navigationEventDispatcher.onNavigationEvent(true); - verify(navigationEventListener, times(0)).onRunning(true); - - navigation.addNavigationEventListener(navigationEventListener); - navigation.addNavigationEventListener(navigationEventListener); - navigation.addNavigationEventListener(navigationEventListener); - navigationEventDispatcher.onNavigationEvent(true); - verify(navigationEventListener, times(1)).onRunning(true); - } - - @Test - public void removeNavigationEventListener_didRemoveListener() throws Exception { - navigation.addNavigationEventListener(navigationEventListener); - navigation.removeNavigationEventListener(navigationEventListener); - navigationEventDispatcher.onNavigationEvent(true); - verify(navigationEventListener, times(0)).onRunning(true); - } - - @Test - public void removeNavigationEventListener_nullRemovesAllListeners() throws Exception { - navigation.addNavigationEventListener(navigationEventListener); - navigation.addNavigationEventListener(mock(NavigationEventListener.class)); - navigation.addNavigationEventListener(mock(NavigationEventListener.class)); - navigation.addNavigationEventListener(mock(NavigationEventListener.class)); - navigation.addNavigationEventListener(mock(NavigationEventListener.class)); - - navigation.removeNavigationEventListener(null); - navigationEventDispatcher.onNavigationEvent(true); - verify(navigationEventListener, times(0)).onRunning(true); - } - - @Test - public void addFasterRouteListener_didAddListener() throws Exception { - navigationEventDispatcher.onFasterRouteEvent(route); - verify(fasterRouteListener, times(0)).fasterRouteFound(route); - - navigation.addFasterRouteListener(fasterRouteListener); - navigationEventDispatcher.onFasterRouteEvent(route); - verify(fasterRouteListener, times(1)).fasterRouteFound(route); - } - - @Test - public void addFasterRouteListener_onlyAddsListenerOnce() throws Exception { - navigationEventDispatcher.onFasterRouteEvent(route); - verify(fasterRouteListener, times(0)).fasterRouteFound(route); - - navigation.addFasterRouteListener(fasterRouteListener); - navigation.addFasterRouteListener(fasterRouteListener); - navigation.addFasterRouteListener(fasterRouteListener); - navigationEventDispatcher.onFasterRouteEvent(route); - verify(fasterRouteListener, times(1)).fasterRouteFound(route); - } - - @Test - public void removeFasterRouteListener_didRemoveListener() throws Exception { - navigation.addFasterRouteListener(fasterRouteListener); - navigation.removeFasterRouteListener(fasterRouteListener); - navigationEventDispatcher.onFasterRouteEvent(route); - verify(fasterRouteListener, times(0)).fasterRouteFound(route); - } - - @Test - public void removeFasterRouteListener_nullRemovesAllListeners() throws Exception { - navigation.addFasterRouteListener(fasterRouteListener); - navigation.addFasterRouteListener(mock(FasterRouteListener.class)); - navigation.addFasterRouteListener(mock(FasterRouteListener.class)); - navigation.addFasterRouteListener(mock(FasterRouteListener.class)); - navigation.addFasterRouteListener(mock(FasterRouteListener.class)); - - navigation.removeFasterRouteListener(null); - navigationEventDispatcher.onFasterRouteEvent(route); - verify(fasterRouteListener, times(0)).fasterRouteFound(route); - } - - - @Test - public void setNavigationMetricListener_didGetSet() throws Exception { - navigationEventDispatcher.addMetricEventListeners(metricEventListener); - navigationEventDispatcher.onProgressChange(location, routeProgress); - - verify(metricEventListener, times(1)).onRouteProgressUpdate(routeProgress); - } - - @Test - public void setNavigationMetricListener_didNotGetTriggeredBeforeArrival() throws Exception { - String instruction = ""; - BannerInstructionMilestone milestone = mock(BannerInstructionMilestone.class); - RouteUtils routeUtils = mock(RouteUtils.class); - when(routeUtils.isArrivalEvent(routeProgress, milestone)).thenReturn(false); - NavigationEventDispatcher navigationEventDispatcher = new NavigationEventDispatcher(routeUtils); - navigationEventDispatcher.addMetricEventListeners(metricEventListener); - - navigationEventDispatcher.onMilestoneEvent(routeProgress, instruction, milestone); - - verify(metricEventListener, times(0)).onArrival(routeProgress); - } - - @Test - public void setNavigationMetricListener_doesGetTriggeredUponArrival() throws Exception { - String instruction = ""; - BannerInstructionMilestone milestone = mock(BannerInstructionMilestone.class); - RouteUtils routeUtils = mock(RouteUtils.class); - when(routeUtils.isArrivalEvent(routeProgress, milestone)).thenReturn(true); - NavigationEventDispatcher navigationEventDispatcher = new NavigationEventDispatcher(routeUtils); - navigationEventDispatcher.addMetricEventListeners(metricEventListener); - - navigationEventDispatcher.onMilestoneEvent(routeProgress, instruction, milestone); - - verify(metricEventListener, times(1)).onArrival(routeProgress); - } - - @Test - public void onArrivalDuringLastLeg_offRouteListenerIsRemoved() { - String instruction = ""; - Location location = mock(Location.class); - BannerInstructionMilestone milestone = mock(BannerInstructionMilestone.class); - RouteUtils routeUtils = mock(RouteUtils.class); - when(routeUtils.isArrivalEvent(routeProgress, milestone)).thenReturn(true); - when(routeUtils.isLastLeg(routeProgress)).thenReturn(true); - NavigationEventDispatcher navigationEventDispatcher = buildEventDispatcherHasArrived(instruction, routeUtils, milestone); - - navigationEventDispatcher.onUserOffRoute(location); - - verify(offRouteListener, times(0)).userOffRoute(location); - } - - @Test - public void onArrivalDuringLastLeg_metricEventListenerIsRemoved() { - String instruction = ""; - Location location = mock(Location.class); - BannerInstructionMilestone milestone = mock(BannerInstructionMilestone.class); - RouteUtils routeUtils = mock(RouteUtils.class); - when(routeUtils.isArrivalEvent(routeProgress, milestone)).thenReturn(true); - when(routeUtils.isLastLeg(routeProgress)).thenReturn(true); - NavigationEventDispatcher dispatcher = buildEventDispatcherHasArrived(instruction, routeUtils, milestone); - - dispatcher.onUserOffRoute(location); - - verify(metricEventListener, times(0)).onOffRouteEvent(location); - } - - @Test - public void onArrivalAlreadyOccurred_arrivalCheckIsIgnored() { - String instruction = ""; - Location location = mock(Location.class); - BannerInstructionMilestone milestone = mock(BannerInstructionMilestone.class); - RouteUtils routeUtils = mock(RouteUtils.class); - when(routeUtils.isArrivalEvent(routeProgress, milestone)).thenReturn(true); - when(routeUtils.isLastLeg(routeProgress)).thenReturn(true); - NavigationEventDispatcher dispatcher = buildEventDispatcherHasArrived(instruction, routeUtils, milestone); - - dispatcher.onMilestoneEvent(routeProgress, instruction, milestone); - - verify(metricEventListener, times(0)).onOffRouteEvent(location); - } - - @NonNull - private NavigationEventDispatcher buildEventDispatcherHasArrived(String instruction, RouteUtils routeUtils, - Milestone milestone) { - NavigationEventDispatcher navigationEventDispatcher = new NavigationEventDispatcher(routeUtils); - navigationEventDispatcher.addOffRouteListener(offRouteListener); - navigationEventDispatcher.addMetricEventListeners(metricEventListener); - navigationEventDispatcher.onMilestoneEvent(routeProgress, instruction, milestone); - return navigationEventDispatcher; - } + private static final String PRECISION_6 = "directions_v5_precision_6.json"; + + @Mock + MilestoneEventListener milestoneEventListener; + @Mock + ProgressChangeListener progressChangeListener; + @Mock + NavigationMetricListener metricEventListener; + @Mock + OffRouteListener offRouteListener; + @Mock + NavigationEventListener navigationEventListener; + @Mock + FasterRouteListener fasterRouteListener; + @Mock + Location location; + @Mock + Milestone milestone; + + private NavigationEventDispatcher navigationEventDispatcher; + private MapboxNavigation navigation; + private DirectionsRoute route; + private RouteProgress routeProgress; + + @Before + public void setup() throws Exception { + MockitoAnnotations.initMocks(this); + Context context = mock(Context.class); + when(context.getApplicationContext()).thenReturn(mock(Context.class)); + navigation = new MapboxNavigation(context, ACCESS_TOKEN, mock(LocationEngine.class)); + navigationEventDispatcher = navigation.getEventDispatcher(); + + Gson gson = new GsonBuilder() + .registerTypeAdapterFactory(DirectionsAdapterFactory.create()).create(); + String body = loadJsonFixture(PRECISION_6); + DirectionsResponse response = gson.fromJson(body, DirectionsResponse.class); + route = response.routes().get(0); + + routeProgress = buildTestRouteProgress(route, 100, 100, 100, 0, 0); + } + + @Test + public void sanity() throws Exception { + NavigationEventDispatcher navigationEventDispatcher = new NavigationEventDispatcher(); + assertNotNull(navigationEventDispatcher); + } + + @Test + public void addMilestoneEventListener_didAddListener() throws Exception { + navigationEventDispatcher.onMilestoneEvent(routeProgress, "", milestone); + verify(milestoneEventListener, times(0)).onMilestoneEvent(routeProgress, "", milestone); + + navigation.addMilestoneEventListener(milestoneEventListener); + navigationEventDispatcher.onMilestoneEvent(routeProgress, "", milestone); + verify(milestoneEventListener, times(1)).onMilestoneEvent(routeProgress, "", milestone); + } + + @Test + public void addMilestoneEventListener_onlyAddsListenerOnce() throws Exception { + navigationEventDispatcher.onMilestoneEvent(routeProgress, "", milestone); + verify(milestoneEventListener, times(0)).onMilestoneEvent(routeProgress, "", milestone); + + navigation.addMilestoneEventListener(milestoneEventListener); + navigation.addMilestoneEventListener(milestoneEventListener); + navigation.addMilestoneEventListener(milestoneEventListener); + navigationEventDispatcher.onMilestoneEvent(routeProgress, "", milestone); + verify(milestoneEventListener, times(1)).onMilestoneEvent(routeProgress, "", milestone); + } + + @Test + public void removeMilestoneEventListener_didRemoveListener() throws Exception { + navigation.addMilestoneEventListener(milestoneEventListener); + navigation.removeMilestoneEventListener(milestoneEventListener); + navigationEventDispatcher.onMilestoneEvent(routeProgress, "", milestone); + verify(milestoneEventListener, times(0)).onMilestoneEvent(routeProgress, "", milestone); + } + + @Test + public void removeMilestoneEventListener_nullRemovesAllListeners() throws Exception { + navigation.addMilestoneEventListener(milestoneEventListener); + navigation.addMilestoneEventListener(mock(MilestoneEventListener.class)); + navigation.addMilestoneEventListener(mock(MilestoneEventListener.class)); + navigation.addMilestoneEventListener(mock(MilestoneEventListener.class)); + + navigation.removeMilestoneEventListener(null); + navigationEventDispatcher.onMilestoneEvent(routeProgress, "", milestone); + verify(milestoneEventListener, times(0)).onMilestoneEvent(routeProgress, "", milestone); + } + + @Test + public void addProgressChangeListener_didAddListener() throws Exception { + navigationEventDispatcher.onProgressChange(location, routeProgress); + verify(progressChangeListener, times(0)).onProgressChange(location, routeProgress); + + navigation.addProgressChangeListener(progressChangeListener); + navigationEventDispatcher.onProgressChange(location, routeProgress); + verify(progressChangeListener, times(1)).onProgressChange(location, routeProgress); + } + + @Test + public void addProgressChangeListener_onlyAddsListenerOnce() throws Exception { + navigationEventDispatcher.onProgressChange(location, routeProgress); + verify(progressChangeListener, times(0)).onProgressChange(location, routeProgress); + + navigation.addProgressChangeListener(progressChangeListener); + navigation.addProgressChangeListener(progressChangeListener); + navigation.addProgressChangeListener(progressChangeListener); + navigationEventDispatcher.onProgressChange(location, routeProgress); + verify(progressChangeListener, times(1)).onProgressChange(location, routeProgress); + } + + @Test + public void removeProgressChangeListener_didRemoveListener() throws Exception { + navigation.addProgressChangeListener(progressChangeListener); + navigation.removeProgressChangeListener(progressChangeListener); + navigationEventDispatcher.onProgressChange(location, routeProgress); + verify(progressChangeListener, times(0)).onProgressChange(location, routeProgress); + } + + @Test + public void removeProgressChangeListener_nullRemovesAllListeners() throws Exception { + navigation.addProgressChangeListener(progressChangeListener); + navigation.addProgressChangeListener(mock(ProgressChangeListener.class)); + navigation.addProgressChangeListener(mock(ProgressChangeListener.class)); + navigation.addProgressChangeListener(mock(ProgressChangeListener.class)); + + navigation.removeProgressChangeListener(null); + navigationEventDispatcher.onProgressChange(location, routeProgress); + verify(progressChangeListener, times(0)).onProgressChange(location, routeProgress); + } + + @Test + public void addOffRouteListener_didAddListener() throws Exception { + navigationEventDispatcher.onUserOffRoute(location); + verify(offRouteListener, times(0)).userOffRoute(location); + + navigation.addOffRouteListener(offRouteListener); + navigationEventDispatcher.onUserOffRoute(location); + verify(offRouteListener, times(1)).userOffRoute(location); + } + + @Test + public void addOffRouteListener_onlyAddsListenerOnce() throws Exception { + navigationEventDispatcher.onUserOffRoute(location); + verify(offRouteListener, times(0)).userOffRoute(location); + + navigation.addOffRouteListener(offRouteListener); + navigation.addOffRouteListener(offRouteListener); + navigation.addOffRouteListener(offRouteListener); + navigationEventDispatcher.onUserOffRoute(location); + verify(offRouteListener, times(1)).userOffRoute(location); + } + + @Test + public void removeOffRouteListener_didRemoveListener() throws Exception { + navigation.addOffRouteListener(offRouteListener); + navigation.removeOffRouteListener(offRouteListener); + navigationEventDispatcher.onUserOffRoute(location); + verify(offRouteListener, times(0)).userOffRoute(location); + } + + @Test + public void removeOffRouteListener_nullRemovesAllListeners() throws Exception { + navigation.addOffRouteListener(offRouteListener); + navigation.addOffRouteListener(mock(OffRouteListener.class)); + navigation.addOffRouteListener(mock(OffRouteListener.class)); + navigation.addOffRouteListener(mock(OffRouteListener.class)); + navigation.addOffRouteListener(mock(OffRouteListener.class)); + + navigation.removeOffRouteListener(null); + navigationEventDispatcher.onUserOffRoute(location); + verify(offRouteListener, times(0)).userOffRoute(location); + } + + @Test + public void addNavigationEventListener_didAddListener() throws Exception { + navigationEventDispatcher.onNavigationEvent(true); + verify(navigationEventListener, times(0)).onRunning(true); + + navigation.addNavigationEventListener(navigationEventListener); + navigationEventDispatcher.onNavigationEvent(true); + verify(navigationEventListener, times(1)).onRunning(true); + } + + @Test + public void addNavigationEventListener_onlyAddsListenerOnce() throws Exception { + navigationEventDispatcher.onNavigationEvent(true); + verify(navigationEventListener, times(0)).onRunning(true); + + navigation.addNavigationEventListener(navigationEventListener); + navigation.addNavigationEventListener(navigationEventListener); + navigation.addNavigationEventListener(navigationEventListener); + navigationEventDispatcher.onNavigationEvent(true); + verify(navigationEventListener, times(1)).onRunning(true); + } + + @Test + public void removeNavigationEventListener_didRemoveListener() throws Exception { + navigation.addNavigationEventListener(navigationEventListener); + navigation.removeNavigationEventListener(navigationEventListener); + navigationEventDispatcher.onNavigationEvent(true); + verify(navigationEventListener, times(0)).onRunning(true); + } + + @Test + public void removeNavigationEventListener_nullRemovesAllListeners() throws Exception { + navigation.addNavigationEventListener(navigationEventListener); + navigation.addNavigationEventListener(mock(NavigationEventListener.class)); + navigation.addNavigationEventListener(mock(NavigationEventListener.class)); + navigation.addNavigationEventListener(mock(NavigationEventListener.class)); + navigation.addNavigationEventListener(mock(NavigationEventListener.class)); + + navigation.removeNavigationEventListener(null); + navigationEventDispatcher.onNavigationEvent(true); + verify(navigationEventListener, times(0)).onRunning(true); + } + + @Test + public void addFasterRouteListener_didAddListener() throws Exception { + navigationEventDispatcher.onFasterRouteEvent(route); + verify(fasterRouteListener, times(0)).fasterRouteFound(route); + + navigation.addFasterRouteListener(fasterRouteListener); + navigationEventDispatcher.onFasterRouteEvent(route); + verify(fasterRouteListener, times(1)).fasterRouteFound(route); + } + + @Test + public void addFasterRouteListener_onlyAddsListenerOnce() throws Exception { + navigationEventDispatcher.onFasterRouteEvent(route); + verify(fasterRouteListener, times(0)).fasterRouteFound(route); + + navigation.addFasterRouteListener(fasterRouteListener); + navigation.addFasterRouteListener(fasterRouteListener); + navigation.addFasterRouteListener(fasterRouteListener); + navigationEventDispatcher.onFasterRouteEvent(route); + verify(fasterRouteListener, times(1)).fasterRouteFound(route); + } + + @Test + public void removeFasterRouteListener_didRemoveListener() throws Exception { + navigation.addFasterRouteListener(fasterRouteListener); + navigation.removeFasterRouteListener(fasterRouteListener); + navigationEventDispatcher.onFasterRouteEvent(route); + verify(fasterRouteListener, times(0)).fasterRouteFound(route); + } + + @Test + public void removeFasterRouteListener_nullRemovesAllListeners() throws Exception { + navigation.addFasterRouteListener(fasterRouteListener); + navigation.addFasterRouteListener(mock(FasterRouteListener.class)); + navigation.addFasterRouteListener(mock(FasterRouteListener.class)); + navigation.addFasterRouteListener(mock(FasterRouteListener.class)); + navigation.addFasterRouteListener(mock(FasterRouteListener.class)); + + navigation.removeFasterRouteListener(null); + navigationEventDispatcher.onFasterRouteEvent(route); + verify(fasterRouteListener, times(0)).fasterRouteFound(route); + } + + + @Test + public void setNavigationMetricListener_didGetSet() throws Exception { + navigationEventDispatcher.addMetricEventListeners(metricEventListener); + navigationEventDispatcher.onProgressChange(location, routeProgress); + + verify(metricEventListener, times(1)).onRouteProgressUpdate(routeProgress); + } + + @Test + public void setNavigationMetricListener_didNotGetTriggeredBeforeArrival() throws Exception { + String instruction = ""; + BannerInstructionMilestone milestone = mock(BannerInstructionMilestone.class); + RouteUtils routeUtils = mock(RouteUtils.class); + when(routeUtils.isArrivalEvent(routeProgress, milestone)).thenReturn(false); + NavigationEventDispatcher navigationEventDispatcher = new NavigationEventDispatcher(routeUtils); + navigationEventDispatcher.addMetricEventListeners(metricEventListener); + + navigationEventDispatcher.onMilestoneEvent(routeProgress, instruction, milestone); + + verify(metricEventListener, times(0)).onArrival(routeProgress); + } + + @Test + public void setNavigationMetricListener_doesGetTriggeredUponArrival() throws Exception { + String instruction = ""; + BannerInstructionMilestone milestone = mock(BannerInstructionMilestone.class); + RouteUtils routeUtils = mock(RouteUtils.class); + when(routeUtils.isArrivalEvent(routeProgress, milestone)).thenReturn(true); + NavigationEventDispatcher navigationEventDispatcher = new NavigationEventDispatcher(routeUtils); + navigationEventDispatcher.addMetricEventListeners(metricEventListener); + + navigationEventDispatcher.onMilestoneEvent(routeProgress, instruction, milestone); + + verify(metricEventListener, times(1)).onArrival(routeProgress); + } + + @Test + public void onArrivalDuringLastLeg_offRouteListenerIsRemoved() { + String instruction = ""; + Location location = mock(Location.class); + BannerInstructionMilestone milestone = mock(BannerInstructionMilestone.class); + RouteUtils routeUtils = mock(RouteUtils.class); + when(routeUtils.isArrivalEvent(routeProgress, milestone)).thenReturn(true); + when(routeUtils.isLastLeg(routeProgress)).thenReturn(true); + NavigationEventDispatcher navigationEventDispatcher = buildEventDispatcherHasArrived(instruction, routeUtils, milestone); + + navigationEventDispatcher.onUserOffRoute(location); + + verify(offRouteListener, times(0)).userOffRoute(location); + } + + @Test + public void onArrivalDuringLastLeg_metricEventListenerIsRemoved() { + String instruction = ""; + Location location = mock(Location.class); + BannerInstructionMilestone milestone = mock(BannerInstructionMilestone.class); + RouteUtils routeUtils = mock(RouteUtils.class); + when(routeUtils.isArrivalEvent(routeProgress, milestone)).thenReturn(true); + when(routeUtils.isLastLeg(routeProgress)).thenReturn(true); + NavigationEventDispatcher dispatcher = buildEventDispatcherHasArrived(instruction, routeUtils, milestone); + + dispatcher.onUserOffRoute(location); + + verify(metricEventListener, times(0)).onOffRouteEvent(location); + } + + @Test + public void onArrivalAlreadyOccurred_arrivalCheckIsIgnored() { + String instruction = ""; + Location location = mock(Location.class); + BannerInstructionMilestone milestone = mock(BannerInstructionMilestone.class); + RouteUtils routeUtils = mock(RouteUtils.class); + when(routeUtils.isArrivalEvent(routeProgress, milestone)).thenReturn(true); + when(routeUtils.isLastLeg(routeProgress)).thenReturn(true); + NavigationEventDispatcher dispatcher = buildEventDispatcherHasArrived(instruction, routeUtils, milestone); + + dispatcher.onMilestoneEvent(routeProgress, instruction, milestone); + + verify(metricEventListener, times(0)).onOffRouteEvent(location); + } + + @NonNull + private NavigationEventDispatcher buildEventDispatcherHasArrived(String instruction, RouteUtils routeUtils, + Milestone milestone) { + NavigationEventDispatcher navigationEventDispatcher = new NavigationEventDispatcher(routeUtils); + navigationEventDispatcher.addOffRouteListener(offRouteListener); + navigationEventDispatcher.addMetricEventListeners(metricEventListener); + navigationEventDispatcher.onMilestoneEvent(routeProgress, instruction, milestone); + return navigationEventDispatcher; + } } 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 fce723a92..56cc67cb3 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 @@ -2,7 +2,7 @@ import android.content.Context; import android.location.Location; -import androidx.core.util.Pair; +import android.util.Pair; import com.google.gson.Gson; import com.google.gson.GsonBuilder; @@ -17,7 +17,6 @@ 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; @@ -32,7 +31,6 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RobolectricTestRunner; -import org.robolectric.annotation.Config; import java.io.IOException; import java.util.ArrayList; @@ -48,434 +46,431 @@ import static org.mockito.Mockito.when; @RunWith(RobolectricTestRunner.class) -@Config(constants = BuildConfig.class, manifest = Config.DEFAULT_MANIFEST_NAME) public class NavigationHelperTest extends BaseTest { - private static final String MULTI_LEG_ROUTE_FIXTURE = "directions_two_leg_route.json"; - private static final String ANNOTATED_DISTANCE_CONGESTION_ROUTE_FIXTURE = "directions_distance_congestion_annotation.json"; - - @Test - public void increaseIndex_increasesStepByOne() throws Exception { - RouteProgress routeProgress = buildMultiLegRouteProgress(); - NavigationIndices previousIndices = NavigationIndices.create(0, 0); - - NavigationIndices newIndices = NavigationHelper.increaseIndex(routeProgress, previousIndices); - - assertEquals(0, newIndices.legIndex()); - assertEquals(1, newIndices.stepIndex()); - } - - @Test - public void increaseIndex_increasesLegIndex() throws Exception { - RouteProgress multiLegRouteProgress = buildMultiLegRouteProgress(); - RouteProgress routeProgress = multiLegRouteProgress.toBuilder() - .legIndex(0) - .stepIndex(21) - .build(); - NavigationIndices previousIndices = NavigationIndices.create(0, 21); - - NavigationIndices newIndices = NavigationHelper.increaseIndex(routeProgress, previousIndices); - - assertEquals(1, newIndices.legIndex()); - } - - @Test - public void increaseIndex_stepIndexResetsOnLegIndexIncrease() throws Exception { - RouteProgress multiLegRouteProgress = buildMultiLegRouteProgress(); - RouteProgress routeProgress = multiLegRouteProgress.toBuilder() - .legIndex(0) - .stepIndex(21) - .build(); - NavigationIndices previousIndices = NavigationIndices.create(0, 21); - - NavigationIndices newIndices = NavigationHelper.increaseIndex(routeProgress, previousIndices); - - assertEquals(0, newIndices.stepIndex()); - } - - @Test - public void checkMilestones_onlyTriggeredMilestonesGetReturned() throws Exception { - RouteProgress routeProgress = buildMultiLegRouteProgress(); - MapboxNavigationOptions options = MapboxNavigationOptions.builder() - .defaultMilestonesEnabled(false).build(); - Context context = mock(Context.class); - when(context.getApplicationContext()).thenReturn(mock(Context.class)); - MapboxNavigation mapboxNavigation = new MapboxNavigation(context, 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(); - Context context = mock(Context.class); - when(context.getApplicationContext()).thenReturn(mock(Context.class)); - MapboxNavigation mapboxNavigation = new MapboxNavigation(context, ACCESS_TOKEN, options, - mock(NavigationTelemetry.class), mock(LocationEngine.class)); - NavigationLocationUpdate model = NavigationLocationUpdate.create(mock(Location.class), mapboxNavigation); - - boolean userOffRoute = isUserOffRoute(model, mock(RouteProgress.class), mock(OffRouteCallback.class)); - - assertFalse(userOffRoute); - } - - @Test - public void stepDistanceRemaining_returnsZeroWhenPositionsEqualEachOther() throws Exception { - DirectionsRoute route = buildMultiLegRoute(); - Point snappedPoint = Point.fromLngLat(-77.062996, 38.798405); - List coordinates = PolylineUtils.decode( - route.legs().get(0).steps().get(1).geometry(), Constants.PRECISION_6 - ); - - double distance = NavigationHelper.stepDistanceRemaining(snappedPoint, 0, 1, route, coordinates); - - assertEquals(0.0, distance); - } - - @Test - public void nextManeuverPosition_correctlyReturnsNextManeuverPosition() throws Exception { - DirectionsRoute route = buildMultiLegRoute(); - List coordinates = PolylineUtils.decode( - route.legs().get(0).steps().get(0).geometry(), Constants.PRECISION_6 - ); - - Point nextManeuver = NavigationHelper.nextManeuverPosition(0, - route.legs().get(0).steps(), coordinates); - - assertTrue(nextManeuver.equals(route.legs().get(0).steps().get(1).maneuver().location())); - } - - @Test - public void nextManeuverPosition_correctlyReturnsNextManeuverPositionInNextLeg() throws Exception { - DirectionsRoute route = buildMultiLegRoute(); - int stepIndex = route.legs().get(0).steps().size() - 1; - List coordinates = PolylineUtils.decode( - route.legs().get(0).steps().get(stepIndex).geometry(), Constants.PRECISION_6); - - Point nextManeuver = NavigationHelper.nextManeuverPosition(stepIndex, - route.legs().get(0).steps(), coordinates); - - assertTrue(nextManeuver.equals(route.legs().get(1).steps().get(0).maneuver().location())); - } - - @Test - public void createIntersectionList_returnsCompleteIntersectionList() throws Exception { - RouteProgress routeProgress = buildMultiLegRouteProgress(); - LegStep currentStep = routeProgress.currentLegProgress().currentStep(); - LegStep upcomingStep = routeProgress.currentLegProgress().upComingStep(); - - List intersections = NavigationHelper.createIntersectionsList(currentStep, upcomingStep); - int correctListSize = currentStep.intersections().size() + 1; - - assertTrue(correctListSize == intersections.size()); - } - - @Test - public void createIntersectionList_upcomingStepNull_returnsCurrentStepIntersectionList() throws Exception { - RouteProgress routeProgress = buildMultiLegRouteProgress(); - LegStep currentStep = routeProgress.currentLegProgress().currentStep(); - LegStep upcomingStep = null; - - List intersections = NavigationHelper.createIntersectionsList(currentStep, upcomingStep); - int correctListSize = currentStep.intersections().size() + 1; - - assertFalse(correctListSize == intersections.size()); - } - - @Test - public void createIntersectionDistanceList_samePointsForDistanceCalculationsEqualZero() throws Exception { - RouteProgress routeProgress = buildMultiLegRouteProgress(); - LegStep currentStep = routeProgress.currentLegProgress().currentStep(); - List currentStepPoints = PolylineUtils.decode(currentStep.geometry(), Constants.PRECISION_6); - List currentStepIntersections = currentStep.intersections(); - - List> intersectionDistances = NavigationHelper.createDistancesToIntersections( - currentStepPoints, currentStepIntersections - ); - - assertTrue(intersectionDistances.get(0).second == 0); - } - - @Test - public void createIntersectionDistanceList_intersectionListSizeEqualsDistanceListSize() throws Exception { - RouteProgress routeProgress = buildMultiLegRouteProgress(); - LegStep currentStep = routeProgress.currentLegProgress().currentStep(); - List currentStepPoints = PolylineUtils.decode(currentStep.geometry(), Constants.PRECISION_6); - List currentStepIntersections = currentStep.intersections(); - - List> intersectionDistances = NavigationHelper.createDistancesToIntersections( - currentStepPoints, currentStepIntersections - ); - - assertTrue(currentStepIntersections.size() == intersectionDistances.size()); - } - - @Test - public void createIntersectionDistanceList_emptyStepPointsReturnsEmptyList() throws Exception { - RouteProgress routeProgress = buildMultiLegRouteProgress(); - LegStep currentStep = routeProgress.currentLegProgress().currentStep(); - List currentStepPoints = new ArrayList<>(); - List currentStepIntersections = currentStep.intersections(); - - List> intersectionDistances = NavigationHelper.createDistancesToIntersections( - currentStepPoints, currentStepIntersections - ); - - assertTrue(intersectionDistances.isEmpty()); - } - - @Test - public void createIntersectionDistanceList_oneStepPointReturnsEmptyList() throws Exception { - RouteProgress routeProgress = buildMultiLegRouteProgress(); - LegStep currentStep = routeProgress.currentLegProgress().currentStep(); - List currentStepPoints = new ArrayList<>(); - currentStepPoints.add(Point.fromLngLat(1d, 1d)); - List currentStepIntersections = currentStep.intersections(); - - List> intersectionDistances = NavigationHelper.createDistancesToIntersections( - currentStepPoints, currentStepIntersections - ); - - assertTrue(intersectionDistances.isEmpty()); - } - - @Test - public void createIntersectionDistanceList_emptyStepIntersectionsReturnsEmptyList() throws Exception { - RouteProgress routeProgress = buildMultiLegRouteProgress(); - LegStep currentStep = routeProgress.currentLegProgress().currentStep(); - List currentStepPoints = PolylineUtils.decode(currentStep.geometry(), Constants.PRECISION_6); - List currentStepIntersections = new ArrayList<>(); - - List> intersectionDistances = NavigationHelper.createDistancesToIntersections( - currentStepPoints, currentStepIntersections - ); - - assertTrue(intersectionDistances.isEmpty()); - } - - @Test - public void findCurrentIntersection_beginningOfStepReturnsFirstIntersection() throws Exception { - RouteProgress routeProgress = buildMultiLegRouteProgress(); - RouteLegProgress legProgress = routeProgress.currentLegProgress(); - RouteStepProgress stepProgress = legProgress.currentStepProgress(); - List intersections = stepProgress.intersections(); - List> intersectionDistances = stepProgress.intersectionDistancesAlongStep(); - - StepIntersection currentIntersection = NavigationHelper.findCurrentIntersection( - intersections, intersectionDistances, 0 - ); - - assertTrue(currentIntersection.equals(intersections.get(0))); - } - - @Test - public void findCurrentIntersection_endOfStepReturnsLastIntersection() throws Exception { - RouteProgress routeProgress = buildMultiLegRouteProgress(); - RouteLegProgress legProgress = routeProgress.currentLegProgress(); - RouteStepProgress stepProgress = legProgress.currentStepProgress(); - List intersections = stepProgress.intersections(); - List> intersectionDistances = stepProgress.intersectionDistancesAlongStep(); - - StepIntersection currentIntersection = NavigationHelper.findCurrentIntersection( - intersections, intersectionDistances, legProgress.currentStep().distance() - ); - - assertTrue(currentIntersection.equals(intersections.get(intersections.size() - 1))); - } - - @Test - public void findCurrentIntersection_middleOfStepReturnsCorrectIntersection() throws Exception { - RouteProgress routeProgress = buildMultiLegRouteProgress(100, 0, 0, 2, 0); - RouteLegProgress legProgress = routeProgress.currentLegProgress(); - RouteStepProgress stepProgress = legProgress.currentStepProgress(); - List intersections = stepProgress.intersections(); - List> intersectionDistances = stepProgress.intersectionDistancesAlongStep(); - - StepIntersection currentIntersection = NavigationHelper.findCurrentIntersection( - intersections, intersectionDistances, 130 - ); - - assertTrue(currentIntersection.equals(intersections.get(1))); - } - - @Test - public void findUpcomingIntersection_beginningOfStepReturnsSecondIntersection() throws Exception { - RouteProgress routeProgress = buildMultiLegRouteProgress(); - RouteLegProgress legProgress = routeProgress.currentLegProgress(); - RouteStepProgress stepProgress = legProgress.currentStepProgress(); - List intersections = stepProgress.intersections(); - - StepIntersection upcomingIntersection = NavigationHelper.findUpcomingIntersection( - intersections, legProgress.upComingStep(), stepProgress.currentIntersection() - ); - - assertTrue(upcomingIntersection.equals(intersections.get(1))); - } - - @Test - public void findUpcomingIntersection_endOfStepReturnsUpcomingStepFirstIntersection() throws Exception { - RouteProgress routeProgress = buildMultiLegRouteProgress(); - RouteLegProgress legProgress = routeProgress.currentLegProgress(); - RouteStepProgress stepProgress = legProgress.currentStepProgress(); - List intersections = stepProgress.intersections(); - List> intersectionDistances = stepProgress.intersectionDistancesAlongStep(); - StepIntersection currentIntersection = NavigationHelper.findCurrentIntersection( - intersections, intersectionDistances, legProgress.currentStep().distance() - ); - - StepIntersection upcomingIntersection = NavigationHelper.findUpcomingIntersection( - intersections, legProgress.upComingStep(), currentIntersection - ); - - assertEquals(legProgress.upComingStep().intersections().get(0), upcomingIntersection); - } - - @Test - public void findUpcomingIntersection_endOfLegReturnsNullIntersection() throws Exception { - int stepIndex = buildMultiLegRoute().legs().get(1).steps().size() - 1; - RouteProgress routeProgress = buildMultiLegRouteProgress(0, 0, 0, stepIndex, 1); - RouteLegProgress legProgress = routeProgress.currentLegProgress(); - RouteStepProgress stepProgress = legProgress.currentStepProgress(); - List intersections = stepProgress.intersections(); - List> intersectionDistances = stepProgress.intersectionDistancesAlongStep(); - StepIntersection currentIntersection = NavigationHelper.findCurrentIntersection( - intersections, intersectionDistances, legProgress.currentStep().distance() - ); - - StepIntersection upcomingIntersection = NavigationHelper.findUpcomingIntersection( - intersections, legProgress.upComingStep(), currentIntersection - ); - - assertEquals(null, upcomingIntersection); - } - - @Test - public void createCurrentAnnotation_nullAnnotationReturnsNull() throws Exception { - CurrentLegAnnotation currentLegAnnotation = NavigationHelper.createCurrentAnnotation( - null, mock(RouteLeg.class), 0 - ); - - assertEquals(null, currentLegAnnotation); - } - - @Test - public void createCurrentAnnotation_emptyDistanceArrayReturnsNull() throws Exception { - CurrentLegAnnotation currentLegAnnotation = buildCurrentAnnotation(); - RouteLeg routeLeg = buildRouteLegWithAnnotation(); - - CurrentLegAnnotation newLegAnnotation = NavigationHelper.createCurrentAnnotation( - currentLegAnnotation, routeLeg, 0 - ); - - assertEquals(null, newLegAnnotation); - } - - @Test - public void createCurrentAnnotation_beginningOfStep_correctAnnotationIsReturned() throws Exception { - RouteProgress routeProgress = buildDistanceCongestionAnnotationRouteProgress(0, 0, 0, 0, 0); - Double legDistanceRemaining = routeProgress.currentLeg().distance(); - - CurrentLegAnnotation newLegAnnotation = NavigationHelper.createCurrentAnnotation( - null, routeProgress.currentLeg(), legDistanceRemaining - ); - - assertEquals("moderate", newLegAnnotation.congestion()); - } - - @Test - public void createCurrentAnnotation_midStep_correctAnnotationIsReturned() throws Exception { - RouteProgress routeProgress = buildDistanceCongestionAnnotationRouteProgress(0, 0, 0, 0, 0); - Double legDistanceRemaining = routeProgress.currentLeg().distance() / 2; - - CurrentLegAnnotation newLegAnnotation = NavigationHelper.createCurrentAnnotation( - null, routeProgress.currentLeg(), legDistanceRemaining - ); - - assertTrue(newLegAnnotation.distanceToAnnotation() < legDistanceRemaining); - assertEquals("heavy", newLegAnnotation.congestion()); - } - - @Test - public void createCurrentAnnotation_usesCurrentLegAnnotationForPriorDistanceTraveled() throws Exception { - RouteProgress routeProgress = buildDistanceCongestionAnnotationRouteProgress(0, 0, 0, 0, 0); - Double legDistanceRemaining = routeProgress.currentLeg().distance() / 2; - Double previousAnnotationDistance = routeProgress.currentLeg().distance() / 3; - CurrentLegAnnotation currentLegAnnotation = CurrentLegAnnotation.builder() - .distance(100d) - .distanceToAnnotation(previousAnnotationDistance) - .index(0) - .build(); - - CurrentLegAnnotation newLegAnnotation = NavigationHelper.createCurrentAnnotation( - currentLegAnnotation, routeProgress.currentLeg(), legDistanceRemaining - ); - - assertEquals(11, newLegAnnotation.index()); - } - - private RouteProgress buildMultiLegRouteProgress(double stepDistanceRemaining, double legDistanceRemaining, - double distanceRemaining, int stepIndex, int legIndex) throws Exception { - DirectionsRoute multiLegRoute = buildMultiLegRoute(); - return buildTestRouteProgress(multiLegRoute, stepDistanceRemaining, - legDistanceRemaining, distanceRemaining, stepIndex, legIndex); - } - - private RouteProgress buildDistanceCongestionAnnotationRouteProgress(double stepDistanceRemaining, double legDistanceRemaining, - double distanceRemaining, int stepIndex, int legIndex) throws Exception { - DirectionsRoute annotatedRoute = buildDistanceCongestionAnnotationRoute(); - return buildTestRouteProgress(annotatedRoute, stepDistanceRemaining, - legDistanceRemaining, distanceRemaining, stepIndex, legIndex); - } - - private RouteProgress buildMultiLegRouteProgress() throws Exception { - DirectionsRoute multiLegRoute = buildMultiLegRoute(); - return buildTestRouteProgress(multiLegRoute, 1000, 1000, 1000, 0, 0); - } - - private DirectionsRoute buildMultiLegRoute() throws IOException { - Gson gson = new GsonBuilder() - .registerTypeAdapterFactory(DirectionsAdapterFactory.create()).create(); - String body = loadJsonFixture(MULTI_LEG_ROUTE_FIXTURE); - DirectionsResponse response = gson.fromJson(body, DirectionsResponse.class); - return response.routes().get(0); - } - - private DirectionsRoute buildDistanceCongestionAnnotationRoute() throws IOException { - Gson gson = new GsonBuilder() - .registerTypeAdapterFactory(DirectionsAdapterFactory.create()).create(); - String body = loadJsonFixture(ANNOTATED_DISTANCE_CONGESTION_ROUTE_FIXTURE); - DirectionsResponse response = gson.fromJson(body, DirectionsResponse.class); - return response.routes().get(0); - } - - private CurrentLegAnnotation buildCurrentAnnotation() { - return CurrentLegAnnotation.builder() - .distance(54d) - .distanceToAnnotation(100) - .congestion("severe") - .index(1) - .build(); - } - - private RouteLeg buildRouteLegWithAnnotation() { - RouteLeg routeLeg = mock(RouteLeg.class); - LegAnnotation legAnnotation = LegAnnotation.builder() - .distance(new ArrayList()) - .build(); - when(routeLeg.annotation()).thenReturn(legAnnotation); - return routeLeg; - } + private static final String MULTI_LEG_ROUTE_FIXTURE = "directions_two_leg_route.json"; + private static final String ANNOTATED_DISTANCE_CONGESTION_ROUTE_FIXTURE = "directions_distance_congestion_annotation.json"; + + @Test + public void increaseIndex_increasesStepByOne() throws Exception { + RouteProgress routeProgress = buildMultiLegRouteProgress(); + NavigationIndices previousIndices = NavigationIndices.create(0, 0); + + NavigationIndices newIndices = NavigationHelper.increaseIndex(routeProgress, previousIndices); + + assertEquals(0, newIndices.legIndex()); + assertEquals(1, newIndices.stepIndex()); + } + + @Test + public void increaseIndex_increasesLegIndex() throws Exception { + RouteProgress multiLegRouteProgress = buildMultiLegRouteProgress(); + RouteProgress routeProgress = multiLegRouteProgress.toBuilder() + .legIndex(0) + .stepIndex(21) + .build(); + NavigationIndices previousIndices = NavigationIndices.create(0, 21); + + NavigationIndices newIndices = NavigationHelper.increaseIndex(routeProgress, previousIndices); + + assertEquals(1, newIndices.legIndex()); + } + + @Test + public void increaseIndex_stepIndexResetsOnLegIndexIncrease() throws Exception { + RouteProgress multiLegRouteProgress = buildMultiLegRouteProgress(); + RouteProgress routeProgress = multiLegRouteProgress.toBuilder() + .legIndex(0) + .stepIndex(21) + .build(); + NavigationIndices previousIndices = NavigationIndices.create(0, 21); + + NavigationIndices newIndices = NavigationHelper.increaseIndex(routeProgress, previousIndices); + + assertEquals(0, newIndices.stepIndex()); + } + + @Test + public void checkMilestones_onlyTriggeredMilestonesGetReturned() throws Exception { + RouteProgress routeProgress = buildMultiLegRouteProgress(); + MapboxNavigationOptions options = MapboxNavigationOptions.builder() + .defaultMilestonesEnabled(false).build(); + Context context = mock(Context.class); + when(context.getApplicationContext()).thenReturn(mock(Context.class)); + MapboxNavigation mapboxNavigation = new MapboxNavigation(context, ACCESS_TOKEN, options, 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(); + Context context = mock(Context.class); + when(context.getApplicationContext()).thenReturn(mock(Context.class)); + MapboxNavigation mapboxNavigation = new MapboxNavigation(context, ACCESS_TOKEN, options, mock(LocationEngine.class)); + NavigationLocationUpdate model = NavigationLocationUpdate.create(mock(Location.class), mapboxNavigation); + + boolean userOffRoute = isUserOffRoute(model, mock(RouteProgress.class), mock(OffRouteCallback.class)); + + assertFalse(userOffRoute); + } + + @Test + public void stepDistanceRemaining_returnsZeroWhenPositionsEqualEachOther() throws Exception { + DirectionsRoute route = buildMultiLegRoute(); + Point snappedPoint = Point.fromLngLat(-77.062996, 38.798405); + List coordinates = PolylineUtils.decode( + route.legs().get(0).steps().get(1).geometry(), Constants.PRECISION_6 + ); + + double distance = NavigationHelper.stepDistanceRemaining(snappedPoint, 0, 1, route, coordinates); + + assertEquals(0.0, distance); + } + + @Test + public void nextManeuverPosition_correctlyReturnsNextManeuverPosition() throws Exception { + DirectionsRoute route = buildMultiLegRoute(); + List coordinates = PolylineUtils.decode( + route.legs().get(0).steps().get(0).geometry(), Constants.PRECISION_6 + ); + + Point nextManeuver = NavigationHelper.nextManeuverPosition(0, + route.legs().get(0).steps(), coordinates); + + assertTrue(nextManeuver.equals(route.legs().get(0).steps().get(1).maneuver().location())); + } + + @Test + public void nextManeuverPosition_correctlyReturnsNextManeuverPositionInNextLeg() throws Exception { + DirectionsRoute route = buildMultiLegRoute(); + int stepIndex = route.legs().get(0).steps().size() - 1; + List coordinates = PolylineUtils.decode( + route.legs().get(0).steps().get(stepIndex).geometry(), Constants.PRECISION_6); + + Point nextManeuver = NavigationHelper.nextManeuverPosition(stepIndex, + route.legs().get(0).steps(), coordinates); + + assertTrue(nextManeuver.equals(route.legs().get(1).steps().get(0).maneuver().location())); + } + + @Test + public void createIntersectionList_returnsCompleteIntersectionList() throws Exception { + RouteProgress routeProgress = buildMultiLegRouteProgress(); + LegStep currentStep = routeProgress.currentLegProgress().currentStep(); + LegStep upcomingStep = routeProgress.currentLegProgress().upComingStep(); + + List intersections = NavigationHelper.createIntersectionsList(currentStep, upcomingStep); + int correctListSize = currentStep.intersections().size() + 1; + + assertTrue(correctListSize == intersections.size()); + } + + @Test + public void createIntersectionList_upcomingStepNull_returnsCurrentStepIntersectionList() throws Exception { + RouteProgress routeProgress = buildMultiLegRouteProgress(); + LegStep currentStep = routeProgress.currentLegProgress().currentStep(); + LegStep upcomingStep = null; + + List intersections = NavigationHelper.createIntersectionsList(currentStep, upcomingStep); + int correctListSize = currentStep.intersections().size() + 1; + + assertFalse(correctListSize == intersections.size()); + } + + @Test + public void createIntersectionDistanceList_samePointsForDistanceCalculationsEqualZero() throws Exception { + RouteProgress routeProgress = buildMultiLegRouteProgress(); + LegStep currentStep = routeProgress.currentLegProgress().currentStep(); + List currentStepPoints = PolylineUtils.decode(currentStep.geometry(), Constants.PRECISION_6); + List currentStepIntersections = currentStep.intersections(); + + List> intersectionDistances = NavigationHelper.createDistancesToIntersections( + currentStepPoints, currentStepIntersections + ); + + assertTrue(intersectionDistances.get(0).second == 0); + } + + @Test + public void createIntersectionDistanceList_intersectionListSizeEqualsDistanceListSize() throws Exception { + RouteProgress routeProgress = buildMultiLegRouteProgress(); + LegStep currentStep = routeProgress.currentLegProgress().currentStep(); + List currentStepPoints = PolylineUtils.decode(currentStep.geometry(), Constants.PRECISION_6); + List currentStepIntersections = currentStep.intersections(); + + List> intersectionDistances = NavigationHelper.createDistancesToIntersections( + currentStepPoints, currentStepIntersections + ); + + assertTrue(currentStepIntersections.size() == intersectionDistances.size()); + } + + @Test + public void createIntersectionDistanceList_emptyStepPointsReturnsEmptyList() throws Exception { + RouteProgress routeProgress = buildMultiLegRouteProgress(); + LegStep currentStep = routeProgress.currentLegProgress().currentStep(); + List currentStepPoints = new ArrayList<>(); + List currentStepIntersections = currentStep.intersections(); + + List> intersectionDistances = NavigationHelper.createDistancesToIntersections( + currentStepPoints, currentStepIntersections + ); + + assertTrue(intersectionDistances.isEmpty()); + } + + @Test + public void createIntersectionDistanceList_oneStepPointReturnsEmptyList() throws Exception { + RouteProgress routeProgress = buildMultiLegRouteProgress(); + LegStep currentStep = routeProgress.currentLegProgress().currentStep(); + List currentStepPoints = new ArrayList<>(); + currentStepPoints.add(Point.fromLngLat(1d, 1d)); + List currentStepIntersections = currentStep.intersections(); + + List> intersectionDistances = NavigationHelper.createDistancesToIntersections( + currentStepPoints, currentStepIntersections + ); + + assertTrue(intersectionDistances.isEmpty()); + } + + @Test + public void createIntersectionDistanceList_emptyStepIntersectionsReturnsEmptyList() throws Exception { + RouteProgress routeProgress = buildMultiLegRouteProgress(); + LegStep currentStep = routeProgress.currentLegProgress().currentStep(); + List currentStepPoints = PolylineUtils.decode(currentStep.geometry(), Constants.PRECISION_6); + List currentStepIntersections = new ArrayList<>(); + + List> intersectionDistances = NavigationHelper.createDistancesToIntersections( + currentStepPoints, currentStepIntersections + ); + + assertTrue(intersectionDistances.isEmpty()); + } + + @Test + public void findCurrentIntersection_beginningOfStepReturnsFirstIntersection() throws Exception { + RouteProgress routeProgress = buildMultiLegRouteProgress(); + RouteLegProgress legProgress = routeProgress.currentLegProgress(); + RouteStepProgress stepProgress = legProgress.currentStepProgress(); + List intersections = stepProgress.intersections(); + List> intersectionDistances = stepProgress.intersectionDistancesAlongStep(); + + StepIntersection currentIntersection = NavigationHelper.findCurrentIntersection( + intersections, intersectionDistances, 0 + ); + + assertTrue(currentIntersection.equals(intersections.get(0))); + } + + @Test + public void findCurrentIntersection_endOfStepReturnsLastIntersection() throws Exception { + RouteProgress routeProgress = buildMultiLegRouteProgress(); + RouteLegProgress legProgress = routeProgress.currentLegProgress(); + RouteStepProgress stepProgress = legProgress.currentStepProgress(); + List intersections = stepProgress.intersections(); + List> intersectionDistances = stepProgress.intersectionDistancesAlongStep(); + + StepIntersection currentIntersection = NavigationHelper.findCurrentIntersection( + intersections, intersectionDistances, legProgress.currentStep().distance() + ); + + assertTrue(currentIntersection.equals(intersections.get(intersections.size() - 1))); + } + + @Test + public void findCurrentIntersection_middleOfStepReturnsCorrectIntersection() throws Exception { + RouteProgress routeProgress = buildMultiLegRouteProgress(100, 0, 0, 2, 0); + RouteLegProgress legProgress = routeProgress.currentLegProgress(); + RouteStepProgress stepProgress = legProgress.currentStepProgress(); + List intersections = stepProgress.intersections(); + List> intersectionDistances = stepProgress.intersectionDistancesAlongStep(); + + StepIntersection currentIntersection = NavigationHelper.findCurrentIntersection( + intersections, intersectionDistances, 130 + ); + + assertTrue(currentIntersection.equals(intersections.get(1))); + } + + @Test + public void findUpcomingIntersection_beginningOfStepReturnsSecondIntersection() throws Exception { + RouteProgress routeProgress = buildMultiLegRouteProgress(); + RouteLegProgress legProgress = routeProgress.currentLegProgress(); + RouteStepProgress stepProgress = legProgress.currentStepProgress(); + List intersections = stepProgress.intersections(); + + StepIntersection upcomingIntersection = NavigationHelper.findUpcomingIntersection( + intersections, legProgress.upComingStep(), stepProgress.currentIntersection() + ); + + assertTrue(upcomingIntersection.equals(intersections.get(1))); + } + + @Test + public void findUpcomingIntersection_endOfStepReturnsUpcomingStepFirstIntersection() throws Exception { + RouteProgress routeProgress = buildMultiLegRouteProgress(); + RouteLegProgress legProgress = routeProgress.currentLegProgress(); + RouteStepProgress stepProgress = legProgress.currentStepProgress(); + List intersections = stepProgress.intersections(); + List> intersectionDistances = stepProgress.intersectionDistancesAlongStep(); + StepIntersection currentIntersection = NavigationHelper.findCurrentIntersection( + intersections, intersectionDistances, legProgress.currentStep().distance() + ); + + StepIntersection upcomingIntersection = NavigationHelper.findUpcomingIntersection( + intersections, legProgress.upComingStep(), currentIntersection + ); + + assertEquals(legProgress.upComingStep().intersections().get(0), upcomingIntersection); + } + + @Test + public void findUpcomingIntersection_endOfLegReturnsNullIntersection() throws Exception { + int stepIndex = buildMultiLegRoute().legs().get(1).steps().size() - 1; + RouteProgress routeProgress = buildMultiLegRouteProgress(0, 0, 0, stepIndex, 1); + RouteLegProgress legProgress = routeProgress.currentLegProgress(); + RouteStepProgress stepProgress = legProgress.currentStepProgress(); + List intersections = stepProgress.intersections(); + List> intersectionDistances = stepProgress.intersectionDistancesAlongStep(); + StepIntersection currentIntersection = NavigationHelper.findCurrentIntersection( + intersections, intersectionDistances, legProgress.currentStep().distance() + ); + + StepIntersection upcomingIntersection = NavigationHelper.findUpcomingIntersection( + intersections, legProgress.upComingStep(), currentIntersection + ); + + assertEquals(null, upcomingIntersection); + } + + @Test + public void createCurrentAnnotation_nullAnnotationReturnsNull() throws Exception { + CurrentLegAnnotation currentLegAnnotation = NavigationHelper.createCurrentAnnotation( + null, mock(RouteLeg.class), 0 + ); + + assertEquals(null, currentLegAnnotation); + } + + @Test + public void createCurrentAnnotation_emptyDistanceArrayReturnsNull() throws Exception { + CurrentLegAnnotation currentLegAnnotation = buildCurrentAnnotation(); + RouteLeg routeLeg = buildRouteLegWithAnnotation(); + + CurrentLegAnnotation newLegAnnotation = NavigationHelper.createCurrentAnnotation( + currentLegAnnotation, routeLeg, 0 + ); + + assertEquals(null, newLegAnnotation); + } + + @Test + public void createCurrentAnnotation_beginningOfStep_correctAnnotationIsReturned() throws Exception { + RouteProgress routeProgress = buildDistanceCongestionAnnotationRouteProgress(0, 0, 0, 0, 0); + Double legDistanceRemaining = routeProgress.currentLeg().distance(); + + CurrentLegAnnotation newLegAnnotation = NavigationHelper.createCurrentAnnotation( + null, routeProgress.currentLeg(), legDistanceRemaining + ); + + assertEquals("moderate", newLegAnnotation.congestion()); + } + + @Test + public void createCurrentAnnotation_midStep_correctAnnotationIsReturned() throws Exception { + RouteProgress routeProgress = buildDistanceCongestionAnnotationRouteProgress(0, 0, 0, 0, 0); + Double legDistanceRemaining = routeProgress.currentLeg().distance() / 2; + + CurrentLegAnnotation newLegAnnotation = NavigationHelper.createCurrentAnnotation( + null, routeProgress.currentLeg(), legDistanceRemaining + ); + + assertTrue(newLegAnnotation.distanceToAnnotation() < legDistanceRemaining); + assertEquals("heavy", newLegAnnotation.congestion()); + } + + @Test + public void createCurrentAnnotation_usesCurrentLegAnnotationForPriorDistanceTraveled() throws Exception { + RouteProgress routeProgress = buildDistanceCongestionAnnotationRouteProgress(0, 0, 0, 0, 0); + Double legDistanceRemaining = routeProgress.currentLeg().distance() / 2; + Double previousAnnotationDistance = routeProgress.currentLeg().distance() / 3; + CurrentLegAnnotation currentLegAnnotation = CurrentLegAnnotation.builder() + .distance(100d) + .distanceToAnnotation(previousAnnotationDistance) + .index(0) + .build(); + + CurrentLegAnnotation newLegAnnotation = NavigationHelper.createCurrentAnnotation( + currentLegAnnotation, routeProgress.currentLeg(), legDistanceRemaining + ); + + assertEquals(11, newLegAnnotation.index()); + } + + private RouteProgress buildMultiLegRouteProgress(double stepDistanceRemaining, double legDistanceRemaining, + double distanceRemaining, int stepIndex, int legIndex) throws Exception { + DirectionsRoute multiLegRoute = buildMultiLegRoute(); + return buildTestRouteProgress(multiLegRoute, stepDistanceRemaining, + legDistanceRemaining, distanceRemaining, stepIndex, legIndex); + } + + private RouteProgress buildDistanceCongestionAnnotationRouteProgress(double stepDistanceRemaining, double legDistanceRemaining, + double distanceRemaining, int stepIndex, int legIndex) throws Exception { + DirectionsRoute annotatedRoute = buildDistanceCongestionAnnotationRoute(); + return buildTestRouteProgress(annotatedRoute, stepDistanceRemaining, + legDistanceRemaining, distanceRemaining, stepIndex, legIndex); + } + + private RouteProgress buildMultiLegRouteProgress() throws Exception { + DirectionsRoute multiLegRoute = buildMultiLegRoute(); + return buildTestRouteProgress(multiLegRoute, 1000, 1000, 1000, 0, 0); + } + + private DirectionsRoute buildMultiLegRoute() throws IOException { + Gson gson = new GsonBuilder() + .registerTypeAdapterFactory(DirectionsAdapterFactory.create()).create(); + String body = loadJsonFixture(MULTI_LEG_ROUTE_FIXTURE); + DirectionsResponse response = gson.fromJson(body, DirectionsResponse.class); + return response.routes().get(0); + } + + private DirectionsRoute buildDistanceCongestionAnnotationRoute() throws IOException { + Gson gson = new GsonBuilder() + .registerTypeAdapterFactory(DirectionsAdapterFactory.create()).create(); + String body = loadJsonFixture(ANNOTATED_DISTANCE_CONGESTION_ROUTE_FIXTURE); + DirectionsResponse response = gson.fromJson(body, DirectionsResponse.class); + return response.routes().get(0); + } + + private CurrentLegAnnotation buildCurrentAnnotation() { + return CurrentLegAnnotation.builder() + .distance(54d) + .distanceToAnnotation(100) + .congestion("severe") + .index(1) + .build(); + } + + private RouteLeg buildRouteLegWithAnnotation() { + RouteLeg routeLeg = mock(RouteLeg.class); + LegAnnotation legAnnotation = LegAnnotation.builder() + .distance(new ArrayList()) + .build(); + when(routeLeg.annotation()).thenReturn(legAnnotation); + return routeLeg; + } } \ No newline at end of file diff --git a/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/navigation/NavigationLocationEngineTest.java b/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/navigation/NavigationLocationEngineTest.java deleted file mode 100644 index 5e3654945..000000000 --- a/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/navigation/NavigationLocationEngineTest.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.mapbox.services.android.navigation.v5.navigation; - -import android.location.Location; - -import com.mapbox.android.core.location.LocationEngine; -import com.mapbox.services.android.navigation.v5.location.LocationValidator; - -import org.junit.Test; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyZeroInteractions; -import static org.mockito.Mockito.when; - -public class NavigationLocationEngineTest { - - @Test - public void onConnected_engineRequestsUpdates() { - LocationEngine locationEngine = mock(LocationEngine.class); - NavigationLocationEngine listener = buildListener(locationEngine); - - listener.onConnected(); - - verify(locationEngine).requestLocationUpdates(); - } - - @Test - public void queueValidLocationUpdate_threadReceivesUpdate() { - RouteProcessorBackgroundThread thread = mock(RouteProcessorBackgroundThread.class); - LocationValidator validator = mock(LocationValidator.class); - when(validator.isValidUpdate(any(Location.class))).thenReturn(true); - NavigationLocationEngine listener = buildListener(thread, validator); - - listener.onLocationChanged(mock(Location.class)); - - verify(thread).queueUpdate(any(NavigationLocationUpdate.class)); - } - - @Test - public void queueInvalidLocationUpdate_threadDoesNotReceiveUpdate() { - RouteProcessorBackgroundThread thread = mock(RouteProcessorBackgroundThread.class); - LocationValidator validator = mock(LocationValidator.class); - when(validator.isValidUpdate(any(Location.class))).thenReturn(false); - NavigationLocationEngine listener = buildListener(thread, validator); - - listener.onLocationChanged(mock(Location.class)); - - verifyZeroInteractions(thread); - } - - private NavigationLocationEngine buildListener(RouteProcessorBackgroundThread thread, - LocationValidator validator) { - MapboxNavigation mapboxNavigation = mock(MapboxNavigation.class); - when(mapboxNavigation.options()).thenReturn(MapboxNavigationOptions.builder().build()); - return new NavigationLocationEngine(thread, mapboxNavigation, - mock(LocationEngine.class), validator); - } - - private NavigationLocationEngine buildListener(LocationEngine locationEngine) { - MapboxNavigation mapboxNavigation = mock(MapboxNavigation.class); - when(mapboxNavigation.options()).thenReturn(MapboxNavigationOptions.builder().build()); - return new NavigationLocationEngine(mock(RouteProcessorBackgroundThread.class), - mapboxNavigation, locationEngine, mock(LocationValidator.class)); - } -} \ No newline at end of file diff --git a/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/navigation/NavigationLocationEngineUpdaterTest.java b/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/navigation/NavigationLocationEngineUpdaterTest.java deleted file mode 100644 index 02b1eef40..000000000 --- a/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/navigation/NavigationLocationEngineUpdaterTest.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.mapbox.services.android.navigation.v5.navigation; - -import android.location.Location; - -import com.mapbox.android.core.location.LocationEngine; -import com.mapbox.api.directions.v5.models.DirectionsRoute; - -import org.junit.Test; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -public class NavigationLocationEngineUpdaterTest { - - @Test - public void onInitialization_engineListenerIsSet() { - LocationEngine locationEngine = mock(LocationEngine.class); - NavigationLocationEngine listener = mock(NavigationLocationEngine.class); - - new NavigationLocationEngineUpdater(locationEngine, listener); - - verify(locationEngine).addLocationEngineListener(eq(listener)); - } - - @Test - public void updateLocationEngine_engineListenerIsAdded() { - LocationEngine locationEngine = mock(LocationEngine.class); - NavigationLocationEngine listener = mock(NavigationLocationEngine.class); - NavigationLocationEngineUpdater provider = new NavigationLocationEngineUpdater(locationEngine, listener); - LocationEngine newLocationEngine = mock(LocationEngine.class); - - provider.updateLocationEngine(newLocationEngine); - - verify(newLocationEngine).addLocationEngineListener(eq(listener)); - } - - @Test - public void updateLocationEngine_oldEngineListenerIsRemoved() { - LocationEngine locationEngine = mock(LocationEngine.class); - NavigationLocationEngine listener = mock(NavigationLocationEngine.class); - NavigationLocationEngineUpdater provider = new NavigationLocationEngineUpdater(locationEngine, listener); - LocationEngine newLocationEngine = mock(LocationEngine.class); - - provider.updateLocationEngine(newLocationEngine); - - verify(locationEngine).removeLocationEngineListener(eq(listener)); - } - - @Test - public void forceLocationUpdate_nonNullLastLocationIsSent() { - LocationEngine locationEngine = mock(LocationEngine.class); - when(locationEngine.getLastLocation()).thenReturn(mock(Location.class)); - NavigationLocationEngine listener = mock(NavigationLocationEngine.class); - when(listener.isValidLocationUpdate(any(Location.class))).thenReturn(true); - NavigationLocationEngineUpdater provider = new NavigationLocationEngineUpdater(locationEngine, listener); - - provider.forceLocationUpdate(mock(DirectionsRoute.class)); - - verify(listener).queueLocationUpdate(any(Location.class)); - } - - @Test - public void removeLocationEngineListener() { - LocationEngine locationEngine = mock(LocationEngine.class); - NavigationLocationEngine listener = mock(NavigationLocationEngine.class); - NavigationLocationEngineUpdater provider = new NavigationLocationEngineUpdater(locationEngine, listener); - - provider.removeLocationEngineListener(); - - verify(locationEngine).removeLocationEngineListener(eq(listener)); - } -} \ No newline at end of file diff --git a/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/navigation/NavigationRouteProcessorTest.java b/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/navigation/NavigationRouteProcessorTest.java deleted file mode 100644 index 7c6797319..000000000 --- a/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/navigation/NavigationRouteProcessorTest.java +++ /dev/null @@ -1,178 +0,0 @@ -package com.mapbox.services.android.navigation.v5.navigation; - -import android.content.Context; -import android.location.Location; - -import com.mapbox.android.core.location.LocationEngine; -import com.mapbox.geojson.Point; -import com.mapbox.services.android.navigation.v5.BaseTest; -import com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress; - -import org.junit.Before; -import org.junit.Test; - -import java.util.List; - -import static com.mapbox.services.android.navigation.v5.navigation.NavigationHelper.buildSnappedLocation; -import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertNotNull; -import static junit.framework.Assert.assertTrue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -public class NavigationRouteProcessorTest extends BaseTest { - - private NavigationRouteProcessor routeProcessor; - private MapboxNavigation navigation; - - @Before - public void before() throws Exception { - routeProcessor = new NavigationRouteProcessor(); - MapboxNavigationOptions options = MapboxNavigationOptions.builder().build(); - Context context = mock(Context.class); - when(context.getApplicationContext()).thenReturn(context); - navigation = new MapboxNavigation(context, ACCESS_TOKEN, options, mock(NavigationTelemetry.class), - mock(LocationEngine.class)); - navigation.startNavigation(buildTestDirectionsRoute()); - } - - @Test - public void sanity() throws Exception { - assertNotNull(routeProcessor); - } - - @Test - public void onFirstRouteProgressBuilt_newRouteIsDecoded() throws Exception { - RouteProgress progress = routeProcessor.buildNewRouteProgress(navigation, mock(Location.class)); - assertEquals(0, progress.legIndex()); - assertEquals(0, progress.currentLegProgress().stepIndex()); - } - - @Test - public void onShouldIncreaseStepIndex_indexIsIncreased() throws Exception { - RouteProgress progress = routeProcessor.buildNewRouteProgress(navigation, mock(Location.class)); - int currentStepIndex = progress.currentLegProgress().stepIndex(); - routeProcessor.onShouldIncreaseIndex(); - routeProcessor.checkIncreaseIndex(navigation); - - RouteProgress secondProgress = routeProcessor.buildNewRouteProgress(navigation, mock(Location.class)); - int secondStepIndex = secondProgress.currentLegProgress().stepIndex(); - - assertTrue(currentStepIndex != secondStepIndex); - } - - @Test - public void onSnapToRouteEnabledAndUserOnRoute_snappedLocationReturns() throws Exception { - RouteProgress progress = routeProcessor.buildNewRouteProgress(navigation, mock(Location.class)); - boolean snapEnabled = true; - boolean userOffRoute = false; - List coordinates = createCoordinatesFromCurrentStep(progress); - Point lastPointInCurrentStep = coordinates.remove(coordinates.size() - 1); - Location rawLocation = buildDefaultLocationUpdate( - lastPointInCurrentStep.longitude(), lastPointInCurrentStep.latitude() - ); - - Location snappedLocation = buildSnappedLocation( - navigation, snapEnabled, rawLocation, progress, userOffRoute - ); - - assertTrue(!rawLocation.equals(snappedLocation)); - } - - @Test - public void onSnapToRouteDisabledAndUserOnRoute_rawLocationReturns() throws Exception { - RouteProgress progress = routeProcessor.buildNewRouteProgress(navigation, mock(Location.class)); - boolean snapEnabled = false; - boolean userOffRoute = false; - List coordinates = createCoordinatesFromCurrentStep(progress); - Point lastPointInCurrentStep = coordinates.remove(coordinates.size() - 1); - Location rawLocation = buildDefaultLocationUpdate( - lastPointInCurrentStep.longitude(), lastPointInCurrentStep.latitude() - ); - - Location snappedLocation = buildSnappedLocation( - navigation, snapEnabled, rawLocation, progress, userOffRoute - ); - - assertTrue(rawLocation.equals(snappedLocation)); - } - - @Test - public void onSnapToRouteEnabledAndUserOffRoute_rawLocationReturns() throws Exception { - RouteProgress progress = routeProcessor.buildNewRouteProgress(navigation, mock(Location.class)); - boolean snapEnabled = false; - boolean userOffRoute = false; - List coordinates = createCoordinatesFromCurrentStep(progress); - Point lastPointInCurrentStep = coordinates.remove(coordinates.size() - 1); - Location rawLocation = buildDefaultLocationUpdate( - lastPointInCurrentStep.longitude(), lastPointInCurrentStep.latitude() - ); - - Location snappedLocation = buildSnappedLocation( - navigation, snapEnabled, rawLocation, progress, userOffRoute - ); - - assertTrue(rawLocation.equals(snappedLocation)); - } - - @Test - public void onStepDistanceRemainingZeroAndNoBearingMatch_stepIndexForceIncreased() throws Exception { - RouteProgress firstProgress = routeProcessor.buildNewRouteProgress(navigation, mock(Location.class)); - int firstProgressIndex = firstProgress.currentLegProgress().stepIndex(); - List coordinates = createCoordinatesFromCurrentStep(firstProgress); - Point lastPointInCurrentStep = coordinates.remove(coordinates.size() - 1); - Location rawLocation = buildDefaultLocationUpdate( - lastPointInCurrentStep.longitude(), lastPointInCurrentStep.latitude() - ); - - RouteProgress secondProgress = routeProcessor.buildNewRouteProgress(navigation, rawLocation); - int secondProgressIndex = secondProgress.currentLegProgress().stepIndex(); - - assertTrue(firstProgressIndex != secondProgressIndex); - } - - @Test - public void onInvalidNextLeg_indexIsNotIncreased() throws Exception { - routeProcessor.buildNewRouteProgress(navigation, mock(Location.class)); - int legSize = navigation.getRoute().legs().size(); - - for (int i = 0; i < legSize; i++) { - routeProcessor.onShouldIncreaseIndex(); - routeProcessor.checkIncreaseIndex(navigation); - } - RouteProgress progress = routeProcessor.buildNewRouteProgress(navigation, mock(Location.class)); - - assertTrue(progress.legIndex() == legSize - 1); - } - - @Test - public void onInvalidNextStep_indexIsNotIncreased() throws Exception { - routeProcessor.buildNewRouteProgress(navigation, mock(Location.class)); - int stepSize = navigation.getRoute().legs().get(0).steps().size(); - - for (int i = 0; i < stepSize; i++) { - routeProcessor.onShouldIncreaseIndex(); - routeProcessor.checkIncreaseIndex(navigation); - } - RouteProgress progress = routeProcessor.buildNewRouteProgress(navigation, mock(Location.class)); - - assertTrue(progress.currentLegProgress().stepIndex() == stepSize - 1); - } - - @Test - public void withinManeuverRadiusAndBearingMatches_stepIndexIsIncreased() throws Exception { - RouteProgress firstProgress = routeProcessor.buildNewRouteProgress(navigation, mock(Location.class)); - int firstProgressIndex = firstProgress.currentLegProgress().stepIndex(); - List coordinates = createCoordinatesFromCurrentStep(firstProgress); - Point lastPointInCurrentStep = coordinates.remove(coordinates.size() - 1); - Location rawLocation = buildDefaultLocationUpdate( - lastPointInCurrentStep.longitude(), lastPointInCurrentStep.latitude() - ); - when(rawLocation.getBearing()).thenReturn(145f); - - RouteProgress secondProgress = routeProcessor.buildNewRouteProgress(navigation, rawLocation); - int secondProgressIndex = secondProgress.currentLegProgress().stepIndex(); - - assertTrue(firstProgressIndex != secondProgressIndex); - } -} diff --git a/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/snap/SnapToRouteTest.java b/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/snap/SnapToRouteTest.java index a872875b2..31993ba08 100644 --- a/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/snap/SnapToRouteTest.java +++ b/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/snap/SnapToRouteTest.java @@ -2,37 +2,34 @@ import android.location.Location; -import com.mapbox.services.android.navigation.BuildConfig; import com.mapbox.services.android.navigation.v5.BaseTest; import com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RobolectricTestRunner; -import org.robolectric.annotation.Config; import static junit.framework.Assert.assertNotNull; import static junit.framework.Assert.assertTrue; @RunWith(RobolectricTestRunner.class) -@Config(constants = BuildConfig.class) public class SnapToRouteTest extends BaseTest { - @Test - public void sanity() throws Exception { - Snap snap = new SnapToRoute(); + @Test + public void sanity() throws Exception { + Snap snap = new SnapToRoute(); - assertNotNull(snap); - } + assertNotNull(snap); + } - @Test - public void getSnappedLocation_returnsProviderNameCorrectly() throws Exception { - RouteProgress routeProgress = buildDefaultTestRouteProgress(); - Snap snap = new SnapToRoute(); - Location location = new Location("test"); + @Test + public void getSnappedLocation_returnsProviderNameCorrectly() throws Exception { + RouteProgress routeProgress = buildDefaultTestRouteProgress(); + Snap snap = new SnapToRoute(); + Location location = new Location("test"); - Location snappedLocation = snap.getSnappedLocation(location, routeProgress); + Location snappedLocation = snap.getSnappedLocation(location, routeProgress); - assertTrue(snappedLocation.getProvider().equals("test")); - } + assertTrue(snappedLocation.getProvider().equals("test")); + } }