Skip to content

Commit

Permalink
Review updates
Browse files Browse the repository at this point in the history
  • Loading branch information
danesfeder committed Apr 6, 2018
1 parent 09afc6e commit f4b1c46
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ public class NavigationCamera {
private RouteInformation currentRouteInformation;
private boolean trackingEnabled = true;
private long locationUpdateTimestamp;
private ProgressChangeListener progressChangeListener = new ProgressChangeListener() {
@Override
public void onProgressChange(Location location, RouteProgress routeProgress) {
if (trackingEnabled) {
currentRouteInformation = buildRouteInformationFromLocation(location, routeProgress);
animateCameraFromLocation(currentRouteInformation);
}
}
};

/**
* Creates an instance of {@link NavigationCamera}.
Expand All @@ -47,6 +56,15 @@ public NavigationCamera(@NonNull MapboxMap mapboxMap, @NonNull MapboxNavigation
initialize();
}

/**
* Used for testing only.
*/
NavigationCamera(MapboxMap mapboxMap, MapboxNavigation navigation, ProgressChangeListener progressChangeListener) {
this.mapboxMap = mapboxMap;
this.navigation = navigation;
this.progressChangeListener = progressChangeListener;
}

/**
* Called when beginning navigation with a route.
* <p>
Expand Down Expand Up @@ -121,25 +139,6 @@ public void resetCameraPosition() {
}
}

/**
* Used to update the camera position.
* <p>
* {@link Location} is also stored in case the user scrolls the map and the camera
* will eventually need to return to that last location update.
*
* @param location used to update the camera position
* @param routeProgress ignored in this scenario
*/
ProgressChangeListener progressChangeListener = new ProgressChangeListener() {
@Override
public void onProgressChange(Location location, RouteProgress routeProgress) {
if (trackingEnabled) {
currentRouteInformation = buildRouteInformationFromLocation(location, routeProgress);
animateCameraFromLocation(currentRouteInformation);
}
}
};

private void initialize() {
mapboxMap.setMinZoomPreference(7d);
navigation.setCameraEngine(new DynamicCamera(mapboxMap));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,30 @@

import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.services.android.navigation.v5.navigation.MapboxNavigation;
import com.mapbox.services.android.navigation.v5.routeprogress.ProgressChangeListener;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

public class NavigationCameraTest {

@Mock
private MapboxNavigation navigation;

@Mock
private MapboxMap mapboxMap;

private NavigationCamera camera;

@Before
public void setup() throws Exception {
MockitoAnnotations.initMocks(this);
camera = new NavigationCamera(mapboxMap, navigation);
}

@Test
public void sanity() throws Exception {
NavigationCamera camera = buildCamera();

assertNotNull(camera);
}

@Test
public void setTrackingEnabled_trackingIsEnabled() throws Exception {
NavigationCamera camera = buildCamera();

camera.setCameraTrackingLocation(false);
camera.setCameraTrackingLocation(true);

Expand All @@ -45,6 +34,8 @@ public void setTrackingEnabled_trackingIsEnabled() throws Exception {

@Test
public void setTrackingDisabled_trackingIsDisabled() throws Exception {
NavigationCamera camera = buildCamera();

camera.setCameraTrackingLocation(true);
camera.setCameraTrackingLocation(false);

Expand All @@ -53,6 +44,8 @@ public void setTrackingDisabled_trackingIsDisabled() throws Exception {

@Test
public void onResetCamera_trackingIsResumed() throws Exception {
NavigationCamera camera = buildCamera();

camera.setCameraTrackingLocation(false);
camera.resetCameraPosition();

Expand All @@ -61,15 +54,31 @@ public void onResetCamera_trackingIsResumed() throws Exception {

@Test
public void onStartWithNullRoute_progressListenerIsAdded() throws Exception {
MapboxNavigation navigation = mock(MapboxNavigation.class);
ProgressChangeListener listener = mock(ProgressChangeListener.class);
NavigationCamera camera = buildCamera(navigation, listener);

camera.start(null);

verify(navigation, times(1)).addProgressChangeListener(camera.progressChangeListener);
verify(navigation, times(1)).addProgressChangeListener(listener);
}

@Test
public void onResumeWithNullLocation_progressListenerIsAdded() throws Exception {
MapboxNavigation navigation = mock(MapboxNavigation.class);
ProgressChangeListener listener = mock(ProgressChangeListener.class);
NavigationCamera camera = buildCamera(navigation, listener);

camera.resume(null);

verify(navigation, times(1)).addProgressChangeListener(camera.progressChangeListener);
verify(navigation, times(1)).addProgressChangeListener(listener);
}

private NavigationCamera buildCamera() {
return new NavigationCamera(mock(MapboxMap.class), mock(MapboxNavigation.class));
}

private NavigationCamera buildCamera(MapboxNavigation navigation, ProgressChangeListener listener) {
return new NavigationCamera(mock(MapboxMap.class), navigation, listener);
}
}

0 comments on commit f4b1c46

Please sign in to comment.