[android] #3758 - Projection / Visible Region implementation.

[android] #3758 - add VisibleRegion + unit tests, removed boundingbox, cleanup jni, cleanup test app, renamed CoordinateSpan to LatLng.
This commit is contained in:
Tobrun
2016-02-03 17:26:09 +01:00
parent bc1f4b1fc0
commit aa05cba0df
24 changed files with 460 additions and 686 deletions

View File

@@ -10,17 +10,17 @@ import org.junit.Test;
import org.junit.runner.RunWith;
/**
* Tests on VisibleCoordinateBoundsActivity
* Tests on LatLngBoundsActivity
*/
@RunWith(AndroidJUnit4.class)
@LargeTest
public class VisibleCoordinateBoundsActivityTest extends BaseTest {
@Rule
public ActivityTestRule<VisibleCoordinateBoundsActivity> mActivityRule = new ActivityTestRule<>(
VisibleCoordinateBoundsActivity.class);
public ActivityTestRule<LatLngBoundsActivity> mActivityRule = new ActivityTestRule<>(
LatLngBoundsActivity.class);
private VisibleCoordinateBoundsActivity mActivity = null;
private LatLngBoundsActivity mActivity = null;
@Before
public void setActivity() {

View File

@@ -27,7 +27,7 @@
android:name=".MapboxMapActivity"
android:label="@string/action_mapboxmap" />
<activity
android:name=".VisibleCoordinateBoundsActivity"
android:name=".LatLngBoundsActivity"
android:label="@string/activity_visible_coordinate_bounds" />
<activity
android:name=".InfoWindowAdapterActivity"

View File

@@ -5,9 +5,11 @@ import android.support.annotation.NonNull;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.maps.CameraUpdateFactory;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.LatLng;
@@ -18,12 +20,13 @@ import com.mapbox.mapboxsdk.maps.UiSettings;
import com.mapbox.mapboxsdk.utils.ApiAccess;
import com.mapbox.mapboxsdk.maps.MapView;
public class VisibleCoordinateBoundsActivity extends AppCompatActivity {
public class LatLngBoundsActivity extends AppCompatActivity {
private MapView mMapView;
private static final LatLng LOS_ANGELES = new LatLng(34.053940, -118.242622);
private static final LatLng NEW_YORK = new LatLng(40.712730, -74.005953);
private MapView mMapView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -68,8 +71,11 @@ public class VisibleCoordinateBoundsActivity extends AppCompatActivity {
// And padding
int padding = (int) getResources().getDimension(R.dimen.coordinatebounds_margin);
// Animate camera
mapboxMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, padding));
// Move camera to the bounds with padding
mapboxMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, padding));
// Call mapboxMap.getProjection().getVisibleRegion().latLngBounds to retrieve the bounds
Log.v(MapboxConstants.TAG, mapboxMap.getProjection().getVisibleRegion().latLngBounds.toString());
}
});

View File

@@ -38,7 +38,6 @@ import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.layers.CustomLayer;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.maps.SupportMapFragment;
import com.mapbox.mapboxsdk.maps.UiSettings;
import com.mapbox.mapboxsdk.testapp.layers.ExampleCustomLayer;
import com.mapbox.mapboxsdk.testapp.utils.GeoParseUtil;
@@ -403,7 +402,7 @@ public class MainActivity extends AppCompatActivity {
return true;
case R.id.action_visible_bounds:
startActivity(new Intent(getApplicationContext(), VisibleCoordinateBoundsActivity.class));
startActivity(new Intent(getApplicationContext(), LatLngBoundsActivity.class));
return true;
case R.id.action_user_tracking_mode:

View File

@@ -83,7 +83,7 @@ public class PressForMarkerActivity extends AppCompatActivity implements MapboxM
@Override
public void onMapLongClick(@NonNull LatLng point) {
PointF pixel = mMapboxMap.toScreenLocation(point);
PointF pixel = mMapboxMap.getProjection().toScreenLocation(point);
String title = LAT_LON_FORMATTER.format(point.getLatitude()) + ", " + LAT_LON_FORMATTER.format(point.getLongitude());
String snippet = "X = " + (int) pixel.x + ", Y = " + (int) pixel.y;

View File

@@ -1,7 +1,5 @@
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;
@@ -58,8 +56,8 @@ public class LatLngBoundsTest {
@Test
public void testCoordinateSpan() {
CoordinateSpan coordinateSpan = mLatLngBounds.getSpan();
assertEquals("CoordinateSpan should be the same", new CoordinateSpan(2, 2), coordinateSpan);
LatLngSpan latLngSpan = mLatLngBounds.getSpan();
assertEquals("LatLngSpan should be the same", new LatLngSpan(2, 2), latLngSpan);
}
@Test

View File

@@ -0,0 +1,50 @@
package com.mapbox.mapboxsdk.geometry;
import com.mapbox.mapboxsdk.geometry.LatLngSpan;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
public class LatLngSpanTest {
private static final double DELTA = 1e-15;
@Test
public void testSanity() {
LatLngSpan latLngSpan = new LatLngSpan(0.0, 0.0);
assertNotNull("latLngSpan should not be null", latLngSpan);
}
@Test
public void testLatitudeConstructor() {
double latitude = 1.23;
LatLngSpan latLngSpan = new LatLngSpan(latitude, 0.0);
assertEquals("latitude in constructor", latLngSpan.getLatitudeSpan(), latitude, DELTA);
}
@Test
public void testLongitudeConstructor() {
double longitude = 1.23;
LatLngSpan latLngSpan = new LatLngSpan(0.0, longitude);
assertEquals("latitude in constructor", latLngSpan.getLongitudeSpan(), longitude, DELTA);
}
@Test
public void testLatitudeMethod() {
double latitude = 1.23;
LatLngSpan latLngSpan = new LatLngSpan(0.0, 0.0);
latLngSpan.setLatitudeSpan(latitude);
assertEquals("latitude in constructor", latLngSpan.getLatitudeSpan(), latitude, DELTA);
}
@Test
public void testLongitudeMethod() {
double longitude = 1.23;
LatLngSpan latLngSpan = new LatLngSpan(0.0, 0.0);
latLngSpan.setLongitudeSpan(longitude);
assertEquals("latitude in constructor", latLngSpan.getLongitudeSpan(), longitude, DELTA);
}
}

View File

@@ -1,4 +1,4 @@
package com.mapbox.mapboxsdk.maps;
package com.mapbox.mapboxsdk.geometry;
import com.mapbox.mapboxsdk.geometry.ProjectedMeters;

View File

@@ -0,0 +1,71 @@
package com.mapbox.mapboxsdk.geometry;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
public class VisibleRegionTest {
private static final LatLng FAR_LEFT = new LatLng(52, -12);
private static final LatLng NEAR_LEFT = new LatLng(34, -12);
private static final LatLng FAR_RIGHT = new LatLng(52, 26);
private static final LatLng NEAR_RIGHT = new LatLng(34, 26);
private static final LatLngBounds BOUNDS = new LatLngBounds.Builder().include(FAR_LEFT).include(FAR_RIGHT).include(NEAR_LEFT).include(NEAR_RIGHT).build();
@Test
public void testSanity() {
VisibleRegion region = new VisibleRegion(FAR_LEFT, FAR_RIGHT, NEAR_LEFT, NEAR_RIGHT, BOUNDS);
assertNotNull("region should not be null", region);
}
@Test
public void testFarLeftConstructor() {
VisibleRegion region = new VisibleRegion(FAR_LEFT, FAR_RIGHT, NEAR_LEFT, NEAR_RIGHT, BOUNDS);
assertEquals("LatLng should match", region.farLeft, FAR_LEFT);
}
@Test
public void testNearLeftConstructor() {
VisibleRegion region = new VisibleRegion(FAR_LEFT, FAR_RIGHT, NEAR_LEFT, NEAR_RIGHT, BOUNDS);
assertEquals("LatLng should match", region.nearLeft, NEAR_LEFT);
}
@Test
public void testFarRightConstructor() {
VisibleRegion region = new VisibleRegion(FAR_LEFT, FAR_RIGHT, NEAR_LEFT, NEAR_RIGHT, BOUNDS);
assertEquals("LatLng should match", region.farRight, FAR_RIGHT);
}
@Test
public void testNearRightConstructor() {
VisibleRegion region = new VisibleRegion(FAR_LEFT, FAR_RIGHT, NEAR_LEFT, NEAR_RIGHT, BOUNDS);
assertEquals("LatLng should match", region.nearRight, NEAR_RIGHT);
}
@Test
public void testLatLngBoundsConstructor() {
VisibleRegion region = new VisibleRegion(FAR_LEFT, FAR_RIGHT, NEAR_LEFT, NEAR_RIGHT, BOUNDS);
assertEquals("LatLngBounds should match", region.latLngBounds, BOUNDS);
}
@Test
public void testEquals() {
VisibleRegion regionLeft = new VisibleRegion(FAR_LEFT, FAR_RIGHT, NEAR_LEFT, NEAR_RIGHT, BOUNDS);
VisibleRegion regionRight = new VisibleRegion(FAR_LEFT, FAR_RIGHT, NEAR_LEFT, NEAR_RIGHT, BOUNDS);
assertEquals("VisibleRegions should match", regionLeft, regionRight);
}
@Test
public void testToString() {
VisibleRegion region = new VisibleRegion(FAR_LEFT, FAR_RIGHT, NEAR_LEFT, NEAR_RIGHT, BOUNDS);
assertEquals("string should match",
"[farLeft [LatLng [longitude=-12.0, latitude=52.0, altitude=0.0]], " +
"farRight [LatLng [longitude=26.0, latitude=52.0, altitude=0.0]], " +
"nearLeft [LatLng [longitude=-12.0, latitude=34.0, altitude=0.0]], " +
"nearRight [LatLng [longitude=26.0, latitude=34.0, altitude=0.0]], " +
"latLngBounds [N:52.0; E:26.0; S:34.0; W:-12.0]]"
, region.toString());
}
}

View File

@@ -1,170 +0,0 @@
package com.mapbox.mapboxsdk.maps;
import com.mapbox.mapboxsdk.geometry.BoundingBox;
import com.mapbox.mapboxsdk.geometry.CoordinateSpan;
import com.mapbox.mapboxsdk.geometry.LatLng;
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 BoundingBoxTest {
private BoundingBox mBoundingBox;
@Before
public void setup() {
mBoundingBox = new BoundingBox(20.0, 10.0, 0.0, 0.0);
}
@Test
public void testSanity() {
BoundingBox boundingBox = new BoundingBox(0.0, 0.0, 0.0, 0.0);
assertNotNull("boundingBox should not be null", boundingBox);
}
@Test
public void testEqualityConstructors() {
BoundingBox cornerBoundingBox = new BoundingBox(new LatLng(20.0, 10.0), new LatLng(0.0, 0.0));
BoundingBox cloneBoundingBox = new BoundingBox(mBoundingBox);
assertEquals("boundingBoxes should match", mBoundingBox, cornerBoundingBox);
assertEquals("boundingBoxes should match", mBoundingBox, cloneBoundingBox);
}
@Test
public void testValidBox() {
assertTrue(mBoundingBox.isValid());
}
@Test
public void testInvalidLatitudeSpanBox() {
mBoundingBox = new BoundingBox(0.0, 10.0, 10.0, 0.0);
assertFalse(mBoundingBox.isValid());
}
@Test
public void testInvalidLongitudeSpanBox() {
mBoundingBox = new BoundingBox(10.0, 0.0, 0.0, 20.0);
assertFalse(mBoundingBox.isValid());
}
@Test
public void testCenterCoordinate() {
LatLng center = new LatLng(10.0, 5.0);
assertEquals("boundingBox center should match", center, mBoundingBox.getCenter());
}
@Test
public void testUnionSameBox() {
assertEquals(mBoundingBox, mBoundingBox.union(mBoundingBox));
}
@Test
public void testUnionInnerBox() {
BoundingBox innerBox = new BoundingBox(2.0, 2.0, 1.0, 1.0);
assertEquals(mBoundingBox.union(innerBox), mBoundingBox);
}
@Test
public void testUnionOverlapBox() {
BoundingBox resultBox = new BoundingBox(22.0, 12.0, 0.0, 0.0);
BoundingBox overlapBox = new BoundingBox(22.0, 12.0, 1.0, 1.0);
assertEquals(resultBox, mBoundingBox.union(overlapBox));
}
@Test
public void testUnionOuterBox() {
BoundingBox resultBox = new BoundingBox(40.0, 40.0, 0.0, 0.0);
BoundingBox outerBox = new BoundingBox(40.0, 40.0, 0.0, 0.0);
assertEquals(resultBox, mBoundingBox.union(outerBox));
}
@Test
public void testIntersectSameBox() {
assertEquals(mBoundingBox, mBoundingBox.intersect(mBoundingBox));
}
@Test
public void testIntersectInnerBox() {
BoundingBox innerBox = new BoundingBox(2.0, 2.0, 1.0, 1.0);
assertEquals(innerBox, mBoundingBox.intersect(innerBox));
}
@Test
public void testIntersectOuterBox() {
BoundingBox outerBox = new BoundingBox(40.0, 40.0, 0.0, 0.0);
assertEquals(mBoundingBox, mBoundingBox.intersect(outerBox));
}
@Test
public void testIntersectOverlapBox() {
BoundingBox resultBox = new BoundingBox(20.0, 10.0, 1.0, 1.0);
BoundingBox overlapBox = new BoundingBox(22.0, 12.0, 1.0, 1.0);
assertEquals(resultBox, mBoundingBox.intersect(overlapBox));
}
@Test
public void testIntersectNoneBox() {
BoundingBox outsideBox = new BoundingBox(100.0, 100.0, 50.0, 50.0);
assertNull(mBoundingBox.intersect(outsideBox));
}
@Test
public void testContainsCoordinate() {
LatLng coordinate = new LatLng(1.0, 1.0);
assertTrue(mBoundingBox.contains(coordinate));
}
@Test
public void testContainsCoordinateOnBoundary() {
LatLng coordinate = new LatLng(20.0, 9.0);
assertFalse(mBoundingBox.contains(coordinate));
}
@Test
public void testContainsNoCoordinate() {
LatLng coordinate = new LatLng(100.0, 100.0);
assertFalse(mBoundingBox.contains(coordinate));
}
@Test
public void testSpan() {
CoordinateSpan span = new CoordinateSpan(20.0, 10.0);
assertTrue(mBoundingBox.getSpan().equals(span));
}
@Test
public void testLatitudeSpan() {
double height = 20.0;
assertTrue(mBoundingBox.getLatitudeSpan() == height);
}
@Test
public void testLongitudeSpan() {
double width = 10.0;
assertTrue(mBoundingBox.getLongitudeSpan() == width);
}
@Test
public void testEmptySpanEmptyNotEmptyBox() {
assertFalse(mBoundingBox.isEmpty());
}
@Test
public void testEmptySpanEmptyLatitude() {
mBoundingBox = new BoundingBox(1.0, 2.0, 0.0, 2.0);
assertTrue(mBoundingBox.isEmpty());
}
@Test
public void testEmptySpanEmptyLongitude() {
mBoundingBox = new BoundingBox(0.0, 3.0, 0.0, 1.0);
assertTrue(mBoundingBox.isEmpty());
}
}

View File

@@ -1,50 +0,0 @@
package com.mapbox.mapboxsdk.maps;
import com.mapbox.mapboxsdk.geometry.CoordinateSpan;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
public class CoordinateSpanTest {
private static final double DELTA = 1e-15;
@Test
public void testSanity() {
CoordinateSpan coordinateSpan = new CoordinateSpan(0.0, 0.0);
assertNotNull("coordinateSpan should not be null", coordinateSpan);
}
@Test
public void testLatitudeConstructor() {
double latitude = 1.23;
CoordinateSpan coordinateSpan = new CoordinateSpan(latitude, 0.0);
assertEquals("latitude in constructor", coordinateSpan.getLatitudeSpan(), latitude, DELTA);
}
@Test
public void testLongitudeConstructor() {
double longitude = 1.23;
CoordinateSpan coordinateSpan = new CoordinateSpan(0.0, longitude);
assertEquals("latitude in constructor", coordinateSpan.getLongitudeSpan(), longitude, DELTA);
}
@Test
public void testLatitudeMethod() {
double latitude = 1.23;
CoordinateSpan coordinateSpan = new CoordinateSpan(0.0, 0.0);
coordinateSpan.setLatitudeSpan(latitude);
assertEquals("latitude in constructor", coordinateSpan.getLatitudeSpan(), latitude, DELTA);
}
@Test
public void testLongitudeMethod() {
double longitude = 1.23;
CoordinateSpan coordinateSpan = new CoordinateSpan(0.0, 0.0);
coordinateSpan.setLongitudeSpan(longitude);
assertEquals("latitude in constructor", coordinateSpan.getLongitudeSpan(), longitude, DELTA);
}
}

View File

@@ -47,7 +47,7 @@ public class MapboxMapTest {
}
//
// Test UiSettings
// UiSettings
//
@Test
@@ -55,6 +55,16 @@ public class MapboxMapTest {
assertNotNull("UiSettings should not be null", mMapboxMap.getUiSettings());
}
//
// Projection
//
@Test
public void testProjection(){
assertNotNull("Projection should not be null",mMapboxMap.getProjection());
}
//
// InfoWindow

View File

@@ -0,0 +1,26 @@
package com.mapbox.mapboxsdk.maps;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.MockitoAnnotations;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
public class ProjectionTest {
@InjectMocks
MapView mMapView = mock(MapView.class);
@Before
public void beforeTest() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testSanity() {
Projection projection = new Projection(mMapView);
assertNotNull("projection should not be null", projection);
}
}