Skip to content

Commit

Permalink
Fix IllegalArgumentException when updating InstructionList
Browse files Browse the repository at this point in the history
  • Loading branch information
danesfeder committed Jul 19, 2018
1 parent 73b16a9 commit 99226ac
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private boolean addBannerInstructions(RouteProgress routeProgress) {
List<LegStep> steps = currentLeg.steps();
for (LegStep step : steps) {
List<BannerInstructions> bannerInstructions = step.bannerInstructions();
if (bannerInstructions != null) {
if (bannerInstructions != null && !bannerInstructions.isEmpty()) {
instructions.addAll(bannerInstructions);
didAddInstructions = true;
}
Expand All @@ -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();
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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<BannerInstructions> instructions = step.bannerInstructions();
if (instructions != null) {
instructions.clear();
}
}
}
}

0 comments on commit 99226ac

Please sign in to comment.