[android] #3754 - newLatLngBounds: unit tests for LatLng and LatLngBounds, builder pattern + refactoring.

[android] #3754 - Working version using the underlying VisibleCoorindateBounds

[android] #3754 - refactor Camera api inside maps package, correctly use factory pattern, LatLngBounds hooks into camera API

[android] #3754 - cleanup old API
This commit is contained in:
Tobrun
2016-02-02 11:21:47 +01:00
parent a699690c16
commit 6dff169f2b
34 changed files with 690 additions and 768 deletions

View File

@@ -17,7 +17,7 @@ import android.widget.Spinner;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.maps.CameraUpdateFactory;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;

View File

@@ -12,7 +12,7 @@ import android.widget.Toast;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.maps.CameraUpdateFactory;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;

View File

@@ -10,8 +10,7 @@ import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.maps.CameraUpdateFactory;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapboxMap;

View File

@@ -17,7 +17,7 @@ import com.mapbox.directions.service.models.Waypoint;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.annotations.PolylineOptions;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.maps.CameraUpdateFactory;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;

View File

@@ -12,7 +12,7 @@ import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.maps.CameraUpdateFactory;
import com.mapbox.mapboxsdk.constants.MyLocationTracking;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.maps.MapView;

View File

@@ -10,7 +10,7 @@ import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.maps.CameraUpdateFactory;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;

View File

@@ -9,7 +9,7 @@ import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.maps.CameraUpdateFactory;
import com.mapbox.mapboxsdk.maps.MapFragment;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.LatLng;

View File

@@ -9,7 +9,7 @@ import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.maps.CameraUpdateFactory;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;

View File

@@ -9,7 +9,7 @@ import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.maps.CameraUpdateFactory;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapView;

View File

@@ -15,7 +15,7 @@ import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.maps.CameraUpdateFactory;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.constants.MyBearingTracking;
import com.mapbox.mapboxsdk.constants.MyLocationTracking;

View File

@@ -9,7 +9,7 @@ import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.maps.CameraUpdateFactory;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapboxMap;

View File

@@ -8,7 +8,7 @@ import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.maps.CameraUpdateFactory;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapboxMap;

View File

@@ -1,19 +1,15 @@
package com.mapbox.mapboxsdk.testapp;
import android.graphics.RectF;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.maps.CameraUpdateFactory;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.CoordinateBounds;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.maps.MapboxMap;
@@ -63,16 +59,17 @@ public class VisibleCoordinateBoundsActivity extends AppCompatActivity {
.snippet("City Hall")
.position(NEW_YORK));
Snackbar.make(findViewById(android.R.id.content), R.string.action_visible_bounds_explanation, Snackbar.LENGTH_INDEFINITE)
.setAction(android.R.string.ok, new View.OnClickListener() {
@Override
public void onClick(View v) {
// Reposition coordinate bounds
LatLngBounds bounds = new LatLngBounds(NEW_YORK, LOS_ANGELES);
int padding = (int) getResources().getDimension(R.dimen.coordinatebounds_margin);
mapboxMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, padding));
}
}).show();
// Create Bounds
final LatLngBounds bounds = new LatLngBounds.Builder()
.include(NEW_YORK)
.include(LOS_ANGELES)
.build();
// And padding
int padding = (int) getResources().getDimension(R.dimen.coordinatebounds_margin);
// Animate camera
mapboxMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, padding));
}
});

View File

@@ -0,0 +1,160 @@
package com.mapbox.mapboxsdk.geometry;
import com.mapbox.mapboxsdk.annotations.Marker;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.exceptions.InvalidLatLngBoundsException;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
public class LatLngBoundsTest {
private static final double DELTA = 1e-15;
private LatLngBounds mLatLngBounds;
private static final LatLng LAT_LNG_NULL_ISLAND = new LatLng(0, 0);
private static final LatLng LAT_LNG_NOT_NULL_ISLAND = new LatLng(2, 2);
@Before
public void beforeTest() {
mLatLngBounds = new LatLngBounds.Builder()
.include(LAT_LNG_NULL_ISLAND)
.include(LAT_LNG_NOT_NULL_ISLAND)
.build();
}
@Test
public void testSanity() {
LatLngBounds.Builder latLngBoundsBuilder = new LatLngBounds.Builder();
latLngBoundsBuilder.include(LAT_LNG_NULL_ISLAND).include(LAT_LNG_NOT_NULL_ISLAND);
assertNotNull("latLng should not be null", latLngBoundsBuilder.build());
}
@Test(expected = InvalidLatLngBoundsException.class)
public void testNoLatLngs() {
new LatLngBounds.Builder().build();
}
@Test(expected = InvalidLatLngBoundsException.class)
public void testOneLatLngs() {
new LatLngBounds.Builder().include(LAT_LNG_NULL_ISLAND).build();
}
@Test
public void testLatitiudeSpan() {
assertEquals("Span should be the same", 2, mLatLngBounds.getLatitudeSpan(), DELTA);
}
@Test
public void testLongitudeSpan() {
assertEquals("Span should be the same", 2, mLatLngBounds.getLongitudeSpan(), DELTA);
}
@Test
public void testCoordinateSpan() {
CoordinateSpan coordinateSpan = mLatLngBounds.getSpan();
assertEquals("CoordinateSpan should be the same", new CoordinateSpan(2, 2), coordinateSpan);
}
@Test
public void testCenter() {
LatLng center = mLatLngBounds.getCenter();
assertEquals("Center should match", new LatLng(1, 1), center);
}
@Test
public void testEmptySpan() {
mLatLngBounds = new LatLngBounds.Builder()
.include(LAT_LNG_NOT_NULL_ISLAND)
.include(LAT_LNG_NOT_NULL_ISLAND)
.build();
assertTrue("Should be empty", mLatLngBounds.isEmptySpan());
}
@Test
public void testNotEmptySpan() {
mLatLngBounds = new LatLngBounds.Builder()
.include(LAT_LNG_NOT_NULL_ISLAND)
.include(LAT_LNG_NULL_ISLAND)
.build();
assertFalse("Should not be empty", mLatLngBounds.isEmptySpan());
}
@Test
public void testIncluding() {
assertTrue("LatLng should be included", mLatLngBounds.including(new LatLng(1, 1)));
}
@Test
public void testNoIncluding() {
assertFalse("LatLng should not be included", mLatLngBounds.including(new LatLng(3, 1)));
}
@Test
public void testHashCode() {
assertEquals(2147483647, mLatLngBounds.hashCode(), -1946419200);
}
@Test
public void testEquality() {
LatLngBounds latLngBounds = new LatLngBounds.Builder()
.include(LAT_LNG_NULL_ISLAND)
.include(LAT_LNG_NOT_NULL_ISLAND)
.build();
assertEquals("equality should match", mLatLngBounds, latLngBounds);
}
@Test
public void testToString() {
assertEquals(mLatLngBounds.toString(), "N:2.0; E:2.0; S:0.0; W:0.0");
}
@Test
public void testIntersect() {
LatLngBounds latLngBounds = new LatLngBounds.Builder()
.include(new LatLng(1, 1))
.include(LAT_LNG_NULL_ISLAND)
.build();
assertEquals("intersect should match", latLngBounds, latLngBounds.intersect(mLatLngBounds.getLatNorth(), mLatLngBounds.getLonEast(), mLatLngBounds.getLatSouth(), mLatLngBounds.getLonWest()));
}
@Test
public void testNoIntersect() {
LatLngBounds latLngBounds = new LatLngBounds.Builder()
.include(new LatLng(10, 10))
.include(new LatLng(9, 8))
.build();
assertNull(latLngBounds.intersect(mLatLngBounds));
}
@Test
public void testInnerUnion() {
LatLngBounds latLngBounds = new LatLngBounds.Builder()
.include(new LatLng(1, 1))
.include(LAT_LNG_NULL_ISLAND)
.build();
assertEquals("union should match", latLngBounds, latLngBounds.intersect(mLatLngBounds));
}
@Test
public void testOuterUnion() {
LatLngBounds latLngBounds = new LatLngBounds.Builder()
.include(new LatLng(10, 10))
.include(new LatLng(9, 8))
.build();
assertEquals("outer union should match",
latLngBounds.union(mLatLngBounds),
new LatLngBounds.Builder()
.include(new LatLng(10, 10))
.include(LAT_LNG_NULL_ISLAND)
.build());
}
}

View File

@@ -1,6 +1,7 @@
package com.mapbox.mapboxsdk.maps;
package com.mapbox.mapboxsdk.geometry;
import android.location.Location;
import android.os.Parcel;
import com.mapbox.mapboxsdk.geometry.LatLng;
@@ -46,6 +47,34 @@ public class LatLngTest {
assertEquals("altitude default value", latLng2.getAltitude(), altitude, DELTA);
}
@Test
public void testLatitudeSetter() {
LatLng latLng = new LatLng(1.2, 3.4);
latLng.setLatitude(3);
assertEquals("latitude should match", 3, latLng.getLatitude(), DELTA);
}
@Test
public void testLongitudeSetter() {
LatLng latLng = new LatLng(1.2, 3.4);
latLng.setLongitude(3);
assertEquals("longitude should match", 3, latLng.getLongitude(), DELTA);
}
@Test
public void testAltitudeSetter() {
LatLng latLng = new LatLng(1.2, 3.4);
latLng.setAltitude(3);
assertEquals("altitude should match", 3, latLng.getAltitude(), DELTA);
}
@Test
public void testLatLngConstructor() {
LatLng latLng1 = new LatLng(1.2, 3.4);
LatLng latLng2 = new LatLng(latLng1);
assertEquals("latLng should match", latLng1, latLng2);
}
@Test
public void testDistanceTo() {
LatLng latLng1 = new LatLng(0.0, 0.0);
@@ -63,10 +92,6 @@ public class LatLngTest {
assertEquals("distance should match", 0.0, distance, DELTA);
}
/*
* A sample test where Mockito is necessary
*/
@Test
public void testLocationProvider() {
double latitude = 1.2;

View File

@@ -1,60 +0,0 @@
package com.mapbox.mapboxsdk.maps;
import com.mapbox.mapboxsdk.geometry.CoordinateBounds;
import com.mapbox.mapboxsdk.geometry.LatLng;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public class CoordinateBoundsTest {
@Test
public void testSanity() {
CoordinateBounds coordinateBounds = new CoordinateBounds(null, null);
assertNotNull("coordinateBounds should not be null", coordinateBounds);
}
@Test
public void testSouthWestConstructor() {
LatLng southWest = new LatLng(12, 12);
CoordinateBounds coordinateBounds = new CoordinateBounds(southWest, null);
assertEquals("southWest should match", southWest, coordinateBounds.getSouthWest());
}
@Test
public void testNorthEastConstructor() {
LatLng northEast = new LatLng(12, 12);
CoordinateBounds coordinateBounds = new CoordinateBounds(null, northEast);
assertEquals("northEast should match", northEast, coordinateBounds.getNorthEast());
}
@Test
public void testHashCode() {
LatLng northEast = new LatLng(60, 60);
LatLng southWest = new LatLng(43, 26);
CoordinateBounds coordinateBounds = new CoordinateBounds(northEast, southWest);
assertEquals("hash code should match", coordinateBounds.hashCode(), -1515487232);
}
@Test
public void testToString() {
LatLng northEast = new LatLng(60, 60);
LatLng southWest = new LatLng(43, 26);
CoordinateBounds coordinateBounds = new CoordinateBounds(northEast, southWest);
assertEquals("string should match",
coordinateBounds.toString(),
"CoordinateBounds [mNorthEast[" + coordinateBounds.getNorthEast() + "], mSouthWest[]" + coordinateBounds.getSouthWest() + "]");
}
@Test
public void testEquals() {
LatLng northEast = new LatLng(60, 60);
LatLng southWest = new LatLng(43, 26);
CoordinateBounds firstBounds = new CoordinateBounds(northEast, southWest);
CoordinateBounds sameBounds = new CoordinateBounds(northEast, southWest);
assertTrue(firstBounds.equals(sameBounds));
}
}

View File

@@ -1,56 +0,0 @@
package com.mapbox.mapboxsdk.maps;
import com.mapbox.mapboxsdk.geometry.CoordinateRegion;
import com.mapbox.mapboxsdk.geometry.CoordinateSpan;
import com.mapbox.mapboxsdk.geometry.LatLng;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
public class CoordinateRegionTest {
@Test
public void testSanity() {
LatLng center = new LatLng(1.2, 3.4, 5.6);
CoordinateSpan span = new CoordinateSpan(9.8, 7.6);
CoordinateRegion coordinateRegion = new CoordinateRegion(center, span);
assertNotNull("coordinateRegion should not be null", coordinateRegion);
}
@Test
public void testCenterConstructor() {
LatLng center = new LatLng(1.2, 3.4, 5.6);
CoordinateSpan span = new CoordinateSpan(9.8, 7.6);
CoordinateRegion coordinateRegion = new CoordinateRegion(center, span);
assertEquals("center should match", coordinateRegion.getCenter(), center);
}
@Test
public void testSpanConstructor() {
LatLng center = new LatLng(1.2, 3.4, 5.6);
CoordinateSpan span = new CoordinateSpan(9.8, 7.6);
CoordinateRegion coordinateRegion = new CoordinateRegion(center, span);
assertEquals("span should match", coordinateRegion.getSpan(), span);
}
@Test
public void testCenterMethod() {
LatLng center = new LatLng(1.2, 3.4, 5.6);
CoordinateSpan span = new CoordinateSpan(9.8, 7.6);
CoordinateRegion coordinateRegion = new CoordinateRegion(null, span);
coordinateRegion.setCenter(center);
assertEquals("center should match", coordinateRegion.getCenter(), center);
}
@Test
public void testSpanMethod() {
LatLng center = new LatLng(1.2, 3.4, 5.6);
CoordinateSpan span = new CoordinateSpan(9.8, 7.6);
CoordinateRegion coordinateRegion = new CoordinateRegion(center, null);
coordinateRegion.setSpan(span);
assertEquals("span should match", coordinateRegion.getSpan(), span);
}
}

View File

@@ -6,7 +6,6 @@ import com.mapbox.mapboxsdk.annotations.Marker;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.LatLng;
@@ -359,7 +358,6 @@ public class MapboxMapTest {
assertTrue("Zoomlevel should be same", 12 == mMapboxMap.getCameraPosition().zoom);
}
//
// OnMarkerClick interface
//