Skip to content

Commit

Permalink
add check to avoid array index out of bounds exceptions from map rout…
Browse files Browse the repository at this point in the history
…e line draw way points (#1951)
  • Loading branch information
Guardiola31337 authored May 29, 2019
1 parent 92ab4eb commit d447d41
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Mapbox welcomes participation and contributions from everyone.

### v0.39.0 - May 29, 2019

* Add check to avoid ArrayIndexOutOfBoundsExceptions from MapRouteLine#drawWayPoints [#1951](https://github.com/mapbox/mapbox-navigation-android/pull/1951)
* Fix way name truncating too soon [#1947](https://github.com/mapbox/mapbox-navigation-android/pull/1947)
* Fix instruction icon mismatch in between banner and notification [#1946](https://github.com/mapbox/mapbox-navigation-android/pull/1946)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private void findClickedRoute(@NonNull LatLng point, HashMap<LineString, Directi
}

private void calculateClickDistances(HashMap<Double, DirectionsRoute> routeDistancesAwayFromClick,
Point clickPoint, HashMap<LineString, DirectionsRoute> routeLineStrings) {
Point clickPoint, HashMap<LineString, DirectionsRoute> routeLineStrings) {
for (LineString lineString : routeLineStrings.keySet()) {
Point pointOnLine = findPointOnLine(clickPoint, lineString);
if (pointOnLine == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ List<DirectionsRoute> retrieveDirectionsRoutes() {

boolean updatePrimaryRouteIndex(int primaryRouteIndex) {
boolean isNewIndex = this.primaryRouteIndex != primaryRouteIndex
&& primaryRouteIndex < directionsRoutes.size();
&& primaryRouteIndex < directionsRoutes.size() && primaryRouteIndex >= 0;
if (isNewIndex) {
this.primaryRouteIndex = primaryRouteIndex;
updateRoutesFor(primaryRouteIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import edu.emory.mathcs.backport.java.util.Collections;

import static com.mapbox.services.android.navigation.ui.v5.route.RouteConstants.ROUTE_LAYER_ID;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyFloat;
import static org.mockito.ArgumentMatchers.anyInt;
Expand Down Expand Up @@ -110,6 +113,44 @@ public void updatePrimaryIndex_routeLineSourceIsSet() throws IOException {
verify(routeLineSource, times(4)).setGeoJson(any(FeatureCollection.class));
}

@Test
public void updatePrimaryIndex_newPrimaryRouteIndexUpdated() throws IOException {
GeoJsonSource mockedRouteLineSource = mock(GeoJsonSource.class);
GeoJsonSource mockedWayPointSource = mock(GeoJsonSource.class);
List<Layer> anyRouteLayers = buildMockLayers();
List<DirectionsRoute> routes = new ArrayList<>();
routes.add(buildTestDirectionsRoute());
routes.add(buildTestDirectionsRoute());
routes.add(buildTestDirectionsRoute());
routes.add(buildTestDirectionsRoute());
MapRouteLine routeLine = new MapRouteLine(mockedRouteLineSource, mockedWayPointSource, anyRouteLayers);
routeLine.draw(routes);

boolean isNewIndex = routeLine.updatePrimaryRouteIndex(3);

assertTrue(isNewIndex);
assertEquals(3, routeLine.retrievePrimaryRouteIndex());
}

@Test
public void updatePrimaryIndex_newPrimaryRouteIndexIsNotUpdated() throws IOException {
GeoJsonSource mockedRouteLineSource = mock(GeoJsonSource.class);
GeoJsonSource mockedWayPointSource = mock(GeoJsonSource.class);
List<Layer> anyRouteLayers = buildMockLayers();
List<DirectionsRoute> routes = new ArrayList<>();
routes.add(buildTestDirectionsRoute());
routes.add(buildTestDirectionsRoute());
routes.add(buildTestDirectionsRoute());
routes.add(buildTestDirectionsRoute());
MapRouteLine routeLine = new MapRouteLine(mockedRouteLineSource, mockedWayPointSource, anyRouteLayers);
routeLine.draw(routes);

boolean isNewIndex = routeLine.updatePrimaryRouteIndex(-1);

assertFalse(isNewIndex);
assertEquals(0, routeLine.retrievePrimaryRouteIndex());
}

@Test
public void routeLineCap_defaultIsSet() {
Context context = mock(Context.class);
Expand Down

0 comments on commit d447d41

Please sign in to comment.