Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public class NavigationMapRoute implements MapView.OnMapChangedListener,
private static final String WAYPOINT_LAYER_ID = "mapbox-navigation-waypoint-layer";
private static final String ID_FORMAT = "%s-%d";
private static final String GENERIC_ROUTE_SHIELD_LAYER_ID = "mapbox-navigation-route-shield-layer";
private static final int TWO_MANEUVERS = 2;
private static final int TWO_POINTS = 2;
private static final int THIRTY = 30;
private static final String ARROW_BEARING = "mapbox-navigation-arrow-bearing";
private static final String ARROW_SHAFT_SOURCE_ID = "mapbox-navigation-arrow-shaft-source";
Expand Down Expand Up @@ -445,7 +445,10 @@ private void addDirectionWaypoints() {
}

private void addUpcomingManeuverArrow(RouteProgress routeProgress) {
if (routeProgress.upcomingStepPoints() == null || routeProgress.upcomingStepPoints().size() < TWO_MANEUVERS) {
boolean invalidUpcomingStepPoints = routeProgress.upcomingStepPoints() == null
|| routeProgress.upcomingStepPoints().size() < TWO_POINTS;
boolean invalidCurrentStepPoints = routeProgress.currentStepPoints().size() < TWO_POINTS;
if (invalidUpcomingStepPoints || invalidCurrentStepPoints) {
updateArrowLayersVisibilityTo(false);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class OffRouteDetector extends OffRoute {
private Point lastReroutePoint;
private OffRouteCallback callback;
private RingBuffer<Integer> distancesAwayFromManeuver = new RingBuffer<>(3);
private static final int TWO_POINTS = 2;
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

This is duplicated now in OffRouteDetector and NavigationMapRoute, but since it's two different packages and the duplication is rather small, I prefered to duplicate it.


/**
* Method in charge of running a series of test based on the device current location
Expand Down Expand Up @@ -208,8 +209,9 @@ private static boolean movingAwayFromManeuver(RouteProgress routeProgress,
RingBuffer<Integer> distancesAwayFromManeuver,
List<Point> stepPoints,
Point currentPoint) {

if (routeProgress.currentLegProgress().upComingStep() == null || stepPoints.isEmpty()) {
boolean invalidUpcomingStep = routeProgress.currentLegProgress().upComingStep() == null;
boolean invalidStepPointSize = stepPoints.size() < TWO_POINTS;
if (invalidUpcomingStep || invalidStepPointSize) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,21 @@ public void isUserOffRoute_AssertTrueWhenTooFarFromStep() throws Exception {
assertTrue(isUserOffRoute);
}

@Test
public void isUserOffRoute_StepPointSize() throws Exception {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good point, I thought I had to delete the points, after the first off route check, it turns out, it's ok to do it before as well. Changed the order.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

NIT - You can also remove some "unnecessary" new lines so that the 3 parts of the test (Arrange, Act and Assert) are visually 👌 i.e.

@Test
public void test() {
  RouteProgress routeProgress = buildDefaultTestRouteProgress();
  // ...
  
  boolean isUserOffRoute = offRouteDetector.isUserOffRoute(secondUpdate, routeProgress, options);
  
  assertFalse(isUserOffRoute);
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Sure that's not a problem.

RouteProgress routeProgress = buildDefaultTestRouteProgress();
Point stepManeuverPoint = routeProgress.directionsRoute().legs().get(0).steps().get(0).maneuver().location();
removeAllButOneStepPoints(routeProgress);
Location firstUpdate = buildDefaultLocationUpdate(-77.0339782574523, 38.89993519985637);
offRouteDetector.isUserOffRoute(firstUpdate, routeProgress, options);
Point offRoutePoint = buildPointAwayFromPoint(stepManeuverPoint, 50, 90);
Location secondUpdate = buildDefaultLocationUpdate(offRoutePoint.longitude(), offRoutePoint.latitude());

boolean isUserOffRoute = offRouteDetector.isUserOffRoute(secondUpdate, routeProgress, options);

assertFalse(isUserOffRoute);
}

@Test
public void isUserOffRoute_AssertFalseWhenOnStep() throws Exception {
RouteProgress routeProgress = buildDefaultTestRouteProgress();
Expand Down Expand Up @@ -225,4 +240,10 @@ public void isUserOffRoute_assertTrueWhenRouteDistanceRemainingIsZero() {

assertTrue(isOffRoute);
}

private void removeAllButOneStepPoints(RouteProgress routeProgress) {
for (int i = routeProgress.currentStepPoints().size() - 2; i >= 0; i--) {
routeProgress.currentStepPoints().remove(i);
}
}
}