diff --git a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/camera/NavigationCamera.java b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/camera/NavigationCamera.java index 7aba5691227..b034c84af83 100644 --- a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/camera/NavigationCamera.java +++ b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/camera/NavigationCamera.java @@ -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}. @@ -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. *
@@ -121,25 +139,6 @@ public void resetCameraPosition() { } } - /** - * Used to update the camera position. - *
- * {@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)); diff --git a/libandroid-navigation-ui/src/test/java/com/mapbox/services/android/navigation/ui/v5/camera/NavigationCameraTest.java b/libandroid-navigation-ui/src/test/java/com/mapbox/services/android/navigation/ui/v5/camera/NavigationCameraTest.java index 4d47e553835..0185ac5044d 100644 --- a/libandroid-navigation-ui/src/test/java/com/mapbox/services/android/navigation/ui/v5/camera/NavigationCameraTest.java +++ b/libandroid-navigation-ui/src/test/java/com/mapbox/services/android/navigation/ui/v5/camera/NavigationCameraTest.java @@ -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); @@ -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); @@ -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(); @@ -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); } }