From 99226acb798b87b2ff3e3a181184bfb61272f343 Mon Sep 17 00:00:00 2001 From: danesfeder Date: Thu, 19 Jul 2018 11:51:06 -0400 Subject: [PATCH] Fix IllegalArgumentException when updating InstructionList --- .../list/InstructionListPresenter.java | 9 +++++--- .../list/InstructionListPresenterTest.java | 23 +++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/summary/list/InstructionListPresenter.java b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/summary/list/InstructionListPresenter.java index b28d820c7c5..007967da1c1 100644 --- a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/summary/list/InstructionListPresenter.java +++ b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/summary/list/InstructionListPresenter.java @@ -122,7 +122,7 @@ private boolean addBannerInstructions(RouteProgress routeProgress) { List steps = currentLeg.steps(); for (LegStep step : steps) { List bannerInstructions = step.bannerInstructions(); - if (bannerInstructions != null) { + if (bannerInstructions != null && !bannerInstructions.isEmpty()) { instructions.addAll(bannerInstructions); didAddInstructions = true; } @@ -136,6 +136,9 @@ private boolean isNewLeg(RouteProgress routeProgress) { } private boolean updateInstructionList(RouteProgress routeProgress) { + if (instructions.isEmpty()) { + return false; + } RouteLegProgress legProgress = routeProgress.currentLegProgress(); LegStep currentStep = legProgress.currentStep(); double stepDistanceRemaining = legProgress.currentStepProgress().distanceRemaining(); @@ -150,8 +153,8 @@ private boolean removeInstructionsFrom(int currentInstructionIndex) { if (currentInstructionIndex == FIRST_INSTRUCTION_INDEX) { instructions.remove(FIRST_INSTRUCTION_INDEX); return true; - } else if (currentInstructionIndex < instructions.size() - 1) { - instructions.subList(0, currentInstructionIndex).clear(); + } else if (currentInstructionIndex <= instructions.size()) { + instructions.subList(FIRST_INSTRUCTION_INDEX, currentInstructionIndex).clear(); return true; } return false; diff --git a/libandroid-navigation-ui/src/test/java/com/mapbox/services/android/navigation/ui/v5/summary/list/InstructionListPresenterTest.java b/libandroid-navigation-ui/src/test/java/com/mapbox/services/android/navigation/ui/v5/summary/list/InstructionListPresenterTest.java index cae6445d73d..938db620fbf 100644 --- a/libandroid-navigation-ui/src/test/java/com/mapbox/services/android/navigation/ui/v5/summary/list/InstructionListPresenterTest.java +++ b/libandroid-navigation-ui/src/test/java/com/mapbox/services/android/navigation/ui/v5/summary/list/InstructionListPresenterTest.java @@ -18,6 +18,7 @@ import java.util.List; import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertFalse; import static junit.framework.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyDouble; @@ -107,6 +108,19 @@ public void updateBannerListWith_instructionListIsPopulated() throws Exception { assertTrue(didUpdate); } + @Test + public void updateBannerListWith_emptyInstructionsReturnFalse() throws Exception { + RouteProgress routeProgress = buildRouteProgress(); + RouteUtils routeUtils = buildRouteUtils(routeProgress); + clearInstructions(routeProgress); + DistanceFormatter distanceFormatter = mock(DistanceFormatter.class); + InstructionListPresenter presenter = new InstructionListPresenter(routeUtils, distanceFormatter); + + boolean didUpdate = presenter.updateBannerListWith(routeProgress); + + assertFalse(didUpdate); + } + @NonNull private RouteProgress buildRouteProgress() throws Exception { DirectionsRoute route = buildTestDirectionsRoute(); @@ -140,4 +154,13 @@ private int retrieveInstructionSizeFrom(RouteLeg routeLeg) { } return instructions.size() - 1; } + + private void clearInstructions(RouteProgress routeProgress) { + for (LegStep step : routeProgress.currentLeg().steps()) { + List instructions = step.bannerInstructions(); + if (instructions != null) { + instructions.clear(); + } + } + } } \ No newline at end of file