Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] #4009 - add tests for tracking settings, optimise architect…
Browse files Browse the repository at this point in the history
…ure to make it testable through unit tests
  • Loading branch information
tobrun committed Feb 18, 2016
1 parent dd7bbc0 commit e4fa181
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -363,10 +363,11 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
// User did not accept location permissions
}

TrackingSettings trackingSettings = mMapboxMap.getTrackingSettings();
//noinspection ResourceType
setMyLocationTrackingMode(savedInstanceState.getInt(MapboxConstants.STATE_MY_LOCATION_TRACKING_MODE, MyLocationTracking.TRACKING_NONE));
trackingSettings.setMyLocationTrackingMode(savedInstanceState.getInt(MapboxConstants.STATE_MY_LOCATION_TRACKING_MODE, MyLocationTracking.TRACKING_NONE));
//noinspection ResourceType
setMyBearingTrackingMode(savedInstanceState.getInt(MapboxConstants.STATE_MY_BEARING_TRACKING_MODE, MyBearingTracking.NONE));
trackingSettings.setMyBearingTrackingMode(savedInstanceState.getInt(MapboxConstants.STATE_MY_BEARING_TRACKING_MODE, MyBearingTracking.NONE));
} else {
// Force a check for Telemetry
validateTelemetryServiceConfigured();
Expand Down Expand Up @@ -411,8 +412,12 @@ public void onSaveInstanceState(@NonNull Bundle outState) {
outState.putString(MapboxConstants.STATE_ACCESS_TOKEN, mMapboxMap.getAccessToken());
outState.putLong(MapboxConstants.STATE_DEFAULT_TRANSITION_DURATION, mNativeMapView.getDefaultTransitionDuration());
outState.putBoolean(MapboxConstants.STATE_MY_LOCATION_ENABLED, mMapboxMap.isMyLocationEnabled());
outState.putInt(MapboxConstants.STATE_MY_LOCATION_TRACKING_MODE, getMyLocationTrackingMode());
outState.putInt(MapboxConstants.STATE_MY_BEARING_TRACKING_MODE, getMyBearingTrackingMode());


// TrackingSettings
TrackingSettings trackingSettings = mMapboxMap.getTrackingSettings();
outState.putInt(MapboxConstants.STATE_MY_LOCATION_TRACKING_MODE, trackingSettings.getMyLocationTrackingMode());
outState.putInt(MapboxConstants.STATE_MY_BEARING_TRACKING_MODE, trackingSettings.getMyBearingTrackingMode());

// UiSettings
UiSettings uiSettings = mMapboxMap.getUiSettings();
Expand Down Expand Up @@ -1148,7 +1153,7 @@ int getTopOffsetPixelsForIcon(Icon icon) {
/**
* Sets the distance from the edges of the map view’s frame to the edges of the map
* view’s logical viewport.
* <p>
* <p/>
* When the value of this property is equal to {0,0,0,0}, viewport
* properties such as `centerCoordinate` assume a viewport that matches the map
* view’s frame. Otherwise, those properties are inset, excluding part of the
Expand Down Expand Up @@ -1416,7 +1421,7 @@ void setBearing(float bearing) {

/**
* Sets Bearing in degrees
* <p>
* <p/>
* NOTE: Used by UserLocationView
*
* @param bearing Bearing in degrees
Expand Down Expand Up @@ -1550,7 +1555,7 @@ public boolean onDoubleTapEvent(MotionEvent e) {
}

// Single finger double tap
if (mUserLocationView.getMyLocationTrackingMode() == MyLocationTracking.TRACKING_NONE) {
if (mMapboxMap.getTrackingSettings().isLocationTrackingDisabled()) {
// Zoom in on gesture
zoom(true, e.getX(), e.getY());
} else {
Expand Down Expand Up @@ -1775,7 +1780,7 @@ public boolean onScale(ScaleGestureDetector detector) {
mQuickZoom = !mTwoTap;

// Scale the map
if (mMapboxMap.getUiSettings().isScrollGesturesEnabled() && !mQuickZoom && mUserLocationView.getMyLocationTrackingMode() == MyLocationTracking.TRACKING_NONE) {
if (mMapboxMap.getUiSettings().isScrollGesturesEnabled() && !mQuickZoom && mMapboxMap.getTrackingSettings().isLocationTrackingDisabled()) {
// around gesture
mNativeMapView.scaleBy(detector.getScaleFactor(), detector.getFocusX() / mScreenDensity, detector.getFocusY() / mScreenDensity);
} else {
Expand Down Expand Up @@ -1853,7 +1858,7 @@ public boolean onRotate(RotateGestureDetector detector) {
bearing += detector.getRotationDegreesDelta();

// Rotate the map
if (mUserLocationView.getMyLocationTrackingMode() == MyLocationTracking.TRACKING_NONE) {
if (mMapboxMap.getTrackingSettings().isLocationTrackingDisabled()) {
// around gesture
mNativeMapView.setBearing(bearing,
detector.getFocusX() / mScreenDensity,
Expand Down Expand Up @@ -2379,27 +2384,13 @@ void setMyLocationTrackingMode(@MyLocationTracking.Mode int myLocationTrackingMo
if (myLocationTrackingMode != MyLocationTracking.TRACKING_NONE && !mMapboxMap.isMyLocationEnabled()) {
mMapboxMap.setMyLocationEnabled(true);
}

mUserLocationView.setMyLocationTrackingMode(myLocationTrackingMode);
MapboxMap.OnMyLocationTrackingModeChangeListener listener = mMapboxMap.getOnMyLocationTrackingModeChangeListener();
if (listener != null) {
listener.onMyLocationTrackingModeChange(myLocationTrackingMode);
}
}

/**
* Returns the current user location tracking mode.
*
* @return The current user location tracking mode.
* One of the values from {@link MyLocationTracking.Mode}.
* @see MyLocationTracking.Mode
*/
@UiThread
@MyLocationTracking.Mode
int getMyLocationTrackingMode() {
return mUserLocationView.getMyLocationTrackingMode();
}

/**
* <p>
* Set the current my bearing tracking mode.
Expand All @@ -2426,26 +2417,11 @@ void setMyBearingTrackingMode(@MyBearingTracking.Mode int myBearingTrackingMode)
}
}

/**
* Returns the current user bearing tracking mode.
* See {@link MyBearingTracking} for possible return values.
*
* @return the current user bearing tracking mode.
* @see MyBearingTracking
*/
@UiThread
@MyLocationTracking.Mode
int getMyBearingTrackingMode() {
//noinspection ResourceType
return mUserLocationView.getMyBearingTrackingMode();
}

private void resetTrackingModes() {
try {
//noinspection ResourceType
setMyLocationTrackingMode(MyLocationTracking.TRACKING_NONE);
//noinspection ResourceType
setMyBearingTrackingMode(MyBearingTracking.NONE);
TrackingSettings trackingSettings = mMapboxMap.getTrackingSettings();
trackingSettings.setMyLocationTrackingMode(MyLocationTracking.TRACKING_NONE);
trackingSettings.setMyBearingTrackingMode(MyBearingTracking.NONE);
} catch (SecurityException ignore) {
// User did not accept location permissions
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.mapbox.mapboxsdk.maps;

import android.Manifest;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresPermission;
import android.support.annotation.UiThread;

import com.mapbox.mapboxsdk.constants.MyBearingTracking;
Expand All @@ -14,6 +12,12 @@ public class TrackingSettings {
private UiSettings uiSettings;
private boolean dismissTrackingOnGesture = true;

@MyLocationTracking.Mode
private int mMyLocationTrackingMode;

@MyBearingTracking.Mode
private int mMyBearingTrackingMode;

TrackingSettings(@NonNull MapView mapView, UiSettings uiSettings) {
this.mapView = mapView;
this.uiSettings = uiSettings;
Expand All @@ -33,10 +37,8 @@ public class TrackingSettings {
* @see MyLocationTracking
*/
@UiThread
@RequiresPermission(anyOf = {
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION})
public void setMyLocationTrackingMode(@MyLocationTracking.Mode int myLocationTrackingMode) {
mMyLocationTrackingMode = myLocationTrackingMode;
mapView.setMyLocationTrackingMode(myLocationTrackingMode);
validateGesturesForTrackingModes();
}
Expand All @@ -51,7 +53,7 @@ public void setMyLocationTrackingMode(@MyLocationTracking.Mode int myLocationTra
@UiThread
@MyLocationTracking.Mode
public int getMyLocationTrackingMode() {
return mapView.getMyLocationTrackingMode();
return mMyLocationTrackingMode;
}

/**
Expand All @@ -70,10 +72,8 @@ public int getMyLocationTrackingMode() {
* @see MyBearingTracking
*/
@UiThread
@RequiresPermission(anyOf = {
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION})
public void setMyBearingTrackingMode(@MyBearingTracking.Mode int myBearingTrackingMode) {
mMyBearingTrackingMode = myBearingTrackingMode;
mapView.setMyBearingTrackingMode(myBearingTrackingMode);
}

Expand All @@ -87,7 +87,7 @@ public void setMyBearingTrackingMode(@MyBearingTracking.Mode int myBearingTracki
@UiThread
@MyLocationTracking.Mode
public int getMyBearingTrackingMode() {
return mapView.getMyBearingTrackingMode();
return mMyBearingTrackingMode;
}

public boolean isDismissTrackingOnGesture() {
Expand All @@ -100,7 +100,7 @@ public void setDismissTrackingOnGesture(boolean dismissTrackingOnGesture) {
}

private void validateGesturesForTrackingModes() {
if(!dismissTrackingOnGesture) {
if (!dismissTrackingOnGesture) {
int myLocationTrackingMode = getMyLocationTrackingMode();
int myBearingTrackingMode = getMyBearingTrackingMode();

Expand All @@ -114,4 +114,8 @@ private void validateGesturesForTrackingModes() {
}
}
}

public boolean isLocationTrackingDisabled(){
return mMyLocationTrackingMode == MyLocationTracking.TRACKING_NONE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,6 @@ public void setMyLocationTrackingMode(@MyLocationTracking.Mode int myLocationTra
}
}

@MyLocationTracking.Mode
public int getMyLocationTrackingMode() {
return mMyLocationTrackingMode;
}

@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
Expand Down Expand Up @@ -412,11 +407,6 @@ public void setMyBearingTrackingMode(@MyBearingTracking.Mode int myBearingTracki
update();
}

@MyBearingTracking.Mode
public int getMyBearingTrackingMode() {
return mMyBearingTrackingMode;
}

private class MyBearingListener implements SensorEventListener {

private SensorManager mSensorManager;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.mapbox.mapboxsdk.maps;

import com.mapbox.mapboxsdk.constants.MyBearingTracking;
import com.mapbox.mapboxsdk.constants.MyLocationTracking;

import org.junit.Test;
import org.mockito.InjectMocks;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;

public class TrackingSettingsTest {

@InjectMocks
MapView mMapView = mock(MapView.class);

@Test
public void testSanity() {
TrackingSettings trackingSettings = new TrackingSettings(mMapView, new UiSettings(mMapView));
assertNotNull("trackingsettings should not be null", trackingSettings);
}

@Test
public void testMyLocationTrackingMode() {
TrackingSettings trackingSettings = new TrackingSettings(mMapView, new UiSettings(mMapView));
trackingSettings.setMyLocationTrackingMode(MyLocationTracking.TRACKING_FOLLOW);
assertEquals("MyLocationTrackingMode should match", MyLocationTracking.TRACKING_FOLLOW, trackingSettings.getMyLocationTrackingMode());
}

@Test
public void testMyBearingTrackingMode() {
TrackingSettings trackingSettings = new TrackingSettings(mMapView, new UiSettings(mMapView));
trackingSettings.setMyBearingTrackingMode(MyBearingTracking.COMPASS);
assertEquals("MyLocationTrackingMode should match", MyBearingTracking.COMPASS, trackingSettings.getMyBearingTrackingMode());
}

@Test
public void testDismissTrackingModesOnGesture() {
TrackingSettings trackingSettings = new TrackingSettings(mMapView, new UiSettings(mMapView));
trackingSettings.setDismissTrackingOnGesture(false);
assertFalse("DismissTrackingOnGesture should be false", trackingSettings.isDismissTrackingOnGesture());
}

@Test
public void testValidateGesturesForTrackingModes(){
TrackingSettings trackingSettings = new TrackingSettings(mMapView, new UiSettings(mMapView));
trackingSettings.setDismissTrackingOnGesture(false);
trackingSettings.setMyLocationTrackingMode(MyLocationTracking.TRACKING_FOLLOW);
assertFalse("DismissTrackingOnGesture should be false", trackingSettings.isDismissTrackingOnGesture());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,11 @@ public void testAllGesturesDisabled() {
assertEquals("Scroll gesture should be disabled", false, uiSettings.isScrollGesturesEnabled());
}


@Test
public void testInvalidate() {
UiSettings uiSettings = new UiSettings(mMapView);
uiSettings.invalidate();
}

}

0 comments on commit e4fa181

Please sign in to comment.