diff options
author | Samuel Tardieu <sam@rfc1149.net> | 2015-01-07 16:06:42 +0100 |
---|---|---|
committer | Samuel Tardieu <sam@rfc1149.net> | 2015-01-07 16:27:52 +0100 |
commit | d7c0c6ec079b6b7a9ee262f9561b1357ca6fbddd (patch) | |
tree | 61d2725060883b15024d3e6c14a431dfae651d07 /tests | |
parent | 0babda54fcec1b01c18d5e0a755c9afce18984b5 (diff) | |
download | cgeo-d7c0c6ec079b6b7a9ee262f9561b1357ca6fbddd.zip cgeo-d7c0c6ec079b6b7a9ee262f9561b1357ca6fbddd.tar.gz cgeo-d7c0c6ec079b6b7a9ee262f9561b1357ca6fbddd.tar.bz2 |
Use MapQuest reverse geocoder if the Android one fails
Diffstat (limited to 'tests')
-rw-r--r-- | tests/src/cgeo/geocaching/location/GeocoderTest.java | 32 |
1 files changed, 22 insertions, 10 deletions
diff --git a/tests/src/cgeo/geocaching/location/GeocoderTest.java b/tests/src/cgeo/geocaching/location/GeocoderTest.java index f53c074..0cc1a79 100644 --- a/tests/src/cgeo/geocaching/location/GeocoderTest.java +++ b/tests/src/cgeo/geocaching/location/GeocoderTest.java @@ -4,45 +4,57 @@ import static org.assertj.core.api.Assertions.assertThat; import cgeo.CGeoTestCase; import cgeo.geocaching.CgeoApplication; +import cgeo.geocaching.utils.Log; import org.apache.commons.lang3.StringUtils; import org.assertj.core.data.Offset; import rx.Observable; -import android.annotation.TargetApi; import android.location.Address; import android.location.Geocoder; -import android.os.Build; public class GeocoderTest extends CGeoTestCase { private static final String TEST_ADDRESS = "46 rue Barrault, Paris, France"; private static final double TEST_LATITUDE = 48.82677; private static final double TEST_LONGITUDE = 2.34644; + private static final Geopoint TEST_COORDS = new Geopoint(TEST_LATITUDE, TEST_LONGITUDE); private static final Offset<Double> TEST_OFFSET = Offset.offset(0.00050); - @TargetApi(Build.VERSION_CODES.GINGERBREAD) public static void testAndroidGeocoder() { // Some emulators don't have access to Google Android geocoder if (Geocoder.isPresent()) { - testGeocoder(new AndroidGeocoder(CgeoApplication.getInstance()).getFromLocationName(TEST_ADDRESS), "Android"); + final AndroidGeocoder geocoder = new AndroidGeocoder(CgeoApplication.getInstance()); + testGeocoder(geocoder.getFromLocationName(TEST_ADDRESS), "Android", true); + testGeocoder(geocoder.getFromLocation(TEST_COORDS), "Android reverse", true); + } else { + Log.i("not testing absent Android geocoder"); } } public static void testGCGeocoder() { - testGeocoder(GCGeocoder.getFromLocationName(TEST_ADDRESS), "GC"); + testGeocoder(GCGeocoder.getFromLocationName(TEST_ADDRESS), "GC", false); } public static void testMapQuestGeocoder() { - testGeocoder(MapQuestGeocoder.getFromLocationName(TEST_ADDRESS), "MapQuest"); + testGeocoder(MapQuestGeocoder.getFromLocationName(TEST_ADDRESS), "MapQuest", true); + testGeocoder(MapQuestGeocoder.getFromLocation(TEST_COORDS), "MapQuest reverse", true); } - public static void testGeocoder(final Observable<Address> addressObservable, final String geocoder) { + public static void testGeocoder(final Observable<Address> addressObservable, final String geocoder, final boolean withAddress) { final Address address = addressObservable.toBlocking().first(); - assertThat(address.getLatitude()).as("latitude for " + geocoder + " geocoder").isCloseTo(TEST_LATITUDE, TEST_OFFSET); - assertThat(address.getLongitude()).as("longitude for " + geocoder + " geocoder").isCloseTo(TEST_LONGITUDE, TEST_OFFSET); - assertThat(StringUtils.lowerCase(address.getAddressLine(0))).as("street address for " + geocoder + " geocoder").startsWith("46 rue barrault"); + assertThat(address.getLatitude()).as(describe("latitude", geocoder)).isCloseTo(TEST_LATITUDE, TEST_OFFSET); + assertThat(address.getLongitude()).as(describe("longitude", geocoder)).isCloseTo(TEST_LONGITUDE, TEST_OFFSET); + if (withAddress) { + assertThat(StringUtils.lowerCase(address.getAddressLine(0))).as(describe("street address", geocoder)).startsWith("46 rue barrault"); + assertThat(address.getLocality()).as(describe("locality", geocoder)).isEqualTo("Paris"); + assertThat(address.getCountryName()).as(describe("country name", geocoder)).isEqualTo("France"); + } + } + + private static String describe(final String field, final String geocoder) { + return new StringBuilder(field).append(" for ").append(geocoder).append(" .geocoder").toString(); } } |