aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src/cgeo/geocaching/location
diff options
context:
space:
mode:
Diffstat (limited to 'tests/src/cgeo/geocaching/location')
-rw-r--r--tests/src/cgeo/geocaching/location/DistanceParserTest.java32
-rw-r--r--tests/src/cgeo/geocaching/location/GeoPointFormatterTest.java35
-rw-r--r--tests/src/cgeo/geocaching/location/GeoPointParserTest.java121
-rw-r--r--tests/src/cgeo/geocaching/location/GeopointTest.java308
-rw-r--r--tests/src/cgeo/geocaching/location/UnitsTest.java74
-rw-r--r--tests/src/cgeo/geocaching/location/ViewportTest.java109
6 files changed, 679 insertions, 0 deletions
diff --git a/tests/src/cgeo/geocaching/location/DistanceParserTest.java b/tests/src/cgeo/geocaching/location/DistanceParserTest.java
new file mode 100644
index 0000000..4d9e5a5
--- /dev/null
+++ b/tests/src/cgeo/geocaching/location/DistanceParserTest.java
@@ -0,0 +1,32 @@
+package cgeo.geocaching.location;
+
+import cgeo.geocaching.location.DistanceParser;
+
+import android.test.AndroidTestCase;
+
+public class DistanceParserTest extends AndroidTestCase {
+
+ static private final double MM = 1e-6; // 1mm, in kilometers
+
+ public static void testFormats() {
+ assertEquals(1.2, DistanceParser.parseDistance("1200 m", true), MM);
+ assertEquals(1.2, DistanceParser.parseDistance("1.2 km", true), MM);
+ assertEquals(0.36576, DistanceParser.parseDistance("1200 ft", true), MM);
+ assertEquals(1.09728, DistanceParser.parseDistance("1200 yd", true), MM);
+ assertEquals(1.9312128, DistanceParser.parseDistance("1.2 mi", true), MM);
+ }
+
+ public static void testImplicit() {
+ assertEquals(1.2, DistanceParser.parseDistance("1200", true), MM);
+ assertEquals(0.36576, DistanceParser.parseDistance("1200", false), MM);
+ }
+
+ public static void testComma() {
+ assertEquals(1.2, DistanceParser.parseDistance("1,2km", true), MM);
+ }
+
+ public static void testCase() {
+ assertEquals(0.36576, DistanceParser.parseDistance("1200 FT", true), MM);
+ }
+
+} \ No newline at end of file
diff --git a/tests/src/cgeo/geocaching/location/GeoPointFormatterTest.java b/tests/src/cgeo/geocaching/location/GeoPointFormatterTest.java
new file mode 100644
index 0000000..9ae54fa
--- /dev/null
+++ b/tests/src/cgeo/geocaching/location/GeoPointFormatterTest.java
@@ -0,0 +1,35 @@
+package cgeo.geocaching.location;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import cgeo.geocaching.location.Geopoint;
+import cgeo.geocaching.location.GeopointFormatter;
+import cgeo.geocaching.utils.Formatter;
+
+import android.test.AndroidTestCase;
+
+public class GeoPointFormatterTest extends AndroidTestCase {
+
+ public static void testConfluence() {
+ // From issue #2624: coordinate is wrong near to a confluence point
+ final Geopoint point = new Geopoint(49.9999999999999, 5.0);
+ final String format = GeopointFormatter.format(GeopointFormatter.Format.LAT_LON_DECDEGREE_COMMA, point);
+ assertThat(format).isEqualTo("50.000000,5.000000");
+ final String formatMinute = GeopointFormatter.format(GeopointFormatter.Format.LAT_LON_DECMINUTE_RAW, point);
+ assertThat(formatMinute).isEqualTo("N 50° 00.000 E 005° 00.000");
+ final String formatSecond = GeopointFormatter.format(GeopointFormatter.Format.LAT_LON_DECSECOND, point).replaceAll(",", ".");
+ assertEquals(formatSecond, "N 50° 00' 00.000\"" + Formatter.SEPARATOR + "E 005° 00' 00.000\"", formatSecond);
+ }
+
+ public static void testFormat() {
+ // taken from GC30R6G
+ final Geopoint point = new Geopoint("N 51° 21.104 E 010° 15.369");
+ final String format = GeopointFormatter.format(GeopointFormatter.Format.LAT_LON_DECDEGREE_COMMA, point);
+ assertEquals(format, "51.351733,10.256150", format);
+ final String formatMinute = GeopointFormatter.format(GeopointFormatter.Format.LAT_LON_DECMINUTE_RAW, point);
+ assertEquals(formatMinute, "N 51° 21.104 E 010° 15.369", formatMinute);
+ final String formatSecond = GeopointFormatter.format(GeopointFormatter.Format.LAT_LON_DECSECOND, point).replaceAll(",", ".");
+ assertEquals(formatSecond, "N 51° 21' 06.240\"" + Formatter.SEPARATOR + "E 010° 15' 22.140\"", formatSecond);
+ }
+
+}
diff --git a/tests/src/cgeo/geocaching/location/GeoPointParserTest.java b/tests/src/cgeo/geocaching/location/GeoPointParserTest.java
new file mode 100644
index 0000000..e9e002d
--- /dev/null
+++ b/tests/src/cgeo/geocaching/location/GeoPointParserTest.java
@@ -0,0 +1,121 @@
+package cgeo.geocaching.location;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import cgeo.geocaching.location.Geopoint;
+import cgeo.geocaching.location.GeopointParser;
+import cgeo.geocaching.utils.Formatter;
+
+import android.test.AndroidTestCase;
+
+public class GeoPointParserTest extends AndroidTestCase {
+
+ private static final double refLongitude = 8.0 + 38.564 / 60.0;
+ private static final double refLatitude = 49.0 + 56.031 / 60.0;
+
+ public static void testParseLatitude() {
+ assertEquals(refLatitude, GeopointParser.parseLatitude("N 49° 56.031"), 1e-8);
+ }
+
+ public static void testParseLongitude() {
+ assertEquals(refLongitude, GeopointParser.parseLongitude("E 8° 38.564"), 1e-8);
+ }
+
+ public static void testFullCoordinates() {
+ final Geopoint goal = new Geopoint(refLatitude, refLongitude);
+ assertEquals(goal, GeopointParser.parse("N 49° 56.031 | E 8° 38.564"), 1e-6);
+ }
+
+ private static void assertEquals(final Geopoint expected, Geopoint actual, double tolerance) {
+ assertThat(expected).isNotNull();
+ assertThat(actual).isNotNull();
+ assertThat(expected.distanceTo(actual) <= tolerance).isTrue();
+ }
+
+ public static void testCoordinateMissingPart() {
+ // we are trying to parse a _point_, but have only a latitude. Don't accept the numerical part as longitude!
+ Geopoint point = null;
+ try {
+ point = GeopointParser.parse("N 49° 56.031");
+ } catch (Geopoint.ParseException e) {
+ // expected
+ }
+ assertThat(point).isNull();
+ }
+
+ public static void testSouth() {
+ assertEquals(-refLatitude, GeopointParser.parseLatitude("S 49° 56.031"), 1e-8);
+ }
+
+ public static void testWest() {
+ assertEquals(-refLongitude, GeopointParser.parseLongitude("W 8° 38.564"), 1e-8);
+ }
+
+ public static void testLowerCase() {
+ assertEquals(refLongitude, GeopointParser.parseLongitude("e 8° 38.564"), 1e-8);
+ }
+
+ public static void testVariousFormats() {
+ final Geopoint goal1 = GeopointParser.parse("N 49° 43' 57\" | E 2 12' 35");
+ final Geopoint goal2 = GeopointParser.parse("N 49 43.95 E2°12.5833333333");
+ assertEquals(goal1, goal2, 1e-6);
+ }
+
+ public static void testParseOurOwnSeparator() {
+ final Geopoint separator = GeopointParser.parse("N 49° 43' 57\"" + Formatter.SEPARATOR + "E 2 12' 35");
+ final Geopoint noSeparator = GeopointParser.parse("N 49 43.95 E2°12.5833333333");
+ assertEquals(separator, noSeparator, 1e-6);
+ }
+
+ public static void testInSentence() {
+ final Geopoint p1 = GeopointParser.parse("Station3: N51 21.523 / E07 02.680");
+ final Geopoint p2 = GeopointParser.parse("N51 21.523 E07 02.680");
+ assertThat(p1).isNotNull();
+ assertThat(p2).isNotNull();
+ assertThat(p2).isEqualTo(p1);
+ }
+
+ public static void testUnrelatedParts() {
+ Geopoint point = null;
+ try {
+ point = GeopointParser.parse("N51 21.523 and some words in between, so there is no relation E07 02.680");
+ } catch (Geopoint.ParseException e) {
+ // expected
+ }
+ assertThat(point).isNull();
+ }
+
+ public static void testComma() {
+ final Geopoint pointComma = GeopointParser.parse("N 46° 27' 55,65''\n" +
+ "E 15° 53' 41,68''");
+ final Geopoint pointDot = GeopointParser.parse("N 46° 27' 55.65''\n" +
+ "E 15° 53' 41.68''");
+ assertThat(pointComma).isNotNull();
+ assertThat(pointDot).isNotNull();
+ assertThat(pointDot).isEqualTo(pointComma);
+ }
+
+ public static void testBlankAddedByAutocorrectionDot() {
+ assertEquals(refLatitude, GeopointParser.parseLatitude("N 49° 56. 031"), 1e-8);
+ }
+
+ public static void testBlankAddedByAutocorrectionComma() {
+ assertEquals(refLatitude, GeopointParser.parseLatitude("N 49° 56, 031"), 1e-8);
+ }
+
+ public static void testNonTrimmed() {
+ assertEquals(refLatitude, GeopointParser.parseLatitude(" N 49° 56, 031 "), 1e-8);
+ }
+
+ public static void testEquatorGC53() {
+ assertEquals(new Geopoint(0, 36), GeopointParser.parse("00° 00.000 E 036° 00.000"));
+ }
+
+ public static void testMeridian() {
+ assertEquals(new Geopoint(23, 0), GeopointParser.parse("N 23° 00.000 00° 00.000"));
+ }
+
+ public static void testEquatorMeridian() {
+ assertThat(GeopointParser.parse("00° 00.000 00° 00.000")).isEqualTo(Geopoint.ZERO);
+ }
+}
diff --git a/tests/src/cgeo/geocaching/location/GeopointTest.java b/tests/src/cgeo/geocaching/location/GeopointTest.java
new file mode 100644
index 0000000..6f5dd78
--- /dev/null
+++ b/tests/src/cgeo/geocaching/location/GeopointTest.java
@@ -0,0 +1,308 @@
+package cgeo.geocaching.location;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.offset;
+
+import cgeo.geocaching.location.Geopoint;
+
+import android.os.Build;
+import android.os.Bundle;
+import android.test.AndroidTestCase;
+
+public class GeopointTest extends AndroidTestCase {
+
+ public static void testCreation() {
+ final Geopoint gp = new Geopoint(48.2, 3.5);
+ assertThat(gp.getLatitude()).isEqualTo(48.2, offset(1e-8));
+ assertThat(gp.getLongitude()).isEqualTo(3.5, offset(1e-8));
+ }
+
+ public static void testCreationWithParsing() {
+ final Geopoint gp = new Geopoint("N 52° 25,111 E 009° 39,111");
+ assertThat(gp.getLatitude()).isEqualTo(52.41852, offset(1e-4));
+ assertThat(gp.getLongitude()).isEqualTo(9.65185, offset(1e-4));
+ }
+
+ public static void testCreationAtLimit() {
+ // No exception should be raised.
+ final Geopoint gp1 = new Geopoint(90.0, 10.0);
+ assertThat(gp1.getLatitude()).isEqualTo(90, offset(1e-8));
+
+ final Geopoint gp2 = new Geopoint(-90.0, 10.0);
+ assertThat(gp2.getLatitude()).isEqualTo(-90, offset(1e-8));
+
+ final Geopoint gp3 = new Geopoint(10.0, 180.0);
+ assertThat(gp3.getLongitude()).isEqualTo(180, offset(1e-8));
+ }
+
+ public static void testEqual() {
+ final Geopoint gp1 = new Geopoint(48.2, 2.31);
+ assertThat(gp1.equals(gp1)).isTrue();
+ final Geopoint gp2 = new Geopoint(48.3, 2.31);
+ assertThat(gp1.equals(gp2)).isFalse();
+ }
+
+ public static void testGetE6() {
+ final Geopoint gp = new Geopoint(41.2, -3.4);
+ assertThat((double) gp.getLatitudeE6()).isEqualTo(41200000.0, offset(1e-6));
+ assertThat((double) gp.getLongitudeE6()).isEqualTo(-3400000.0, offset(1e-6));
+ }
+
+ public static void testBearingDistance() {
+ final Geopoint gp1 = new Geopoint(-30.4, -1.2);
+ final Geopoint gp2 = new Geopoint(-30.1, -2.3);
+
+ final float d12 = gp1.distanceTo(gp2);
+
+ // broken distance calculation in 4.2.1
+ if (Build.VERSION.SDK_INT == 17) {
+ assertThat((double) d12).isEqualTo(110.83107, offset(1e-6));
+ }
+ else {
+ assertThat((double) d12).isEqualTo(110.967995, offset(1e-6));
+ }
+
+ assertThat((double) gp2.distanceTo(gp1)).isEqualTo(d12, offset(1e-6));
+
+ // Bearing in both directions cannot be added, as this is
+ // the initial bearing of the path in both cases.
+ assertThat((double) gp1.bearingTo(gp2)).isEqualTo(287.162, offset(1e-3));
+ assertThat((double) gp2.bearingTo(gp1)).isEqualTo(107.715, offset(1e-3));
+ }
+
+ public static void testParcelable() {
+ final Geopoint gp = new Geopoint(1.2, 3.4);
+ final String KEY = "geopoint";
+ final Bundle bundle = new Bundle();
+ bundle.putParcelable(KEY, gp);
+ assertThat(bundle.getParcelable(KEY)).isEqualTo(gp);
+ }
+
+ public static void testDDD() {
+ // case 1
+ final Geopoint gp1 = new Geopoint(51.3d, 13.8d);
+
+ checkDDD(gp1, 'N', 51, 30000, 'E', 13, 80000);
+
+ final Geopoint gp1a = new Geopoint(String.valueOf(gp1.getLatDir()), String.valueOf(gp1.getLatDeg()), String.valueOf(gp1.getLatDegFrac()),
+ String.valueOf(gp1.getLonDir()), String.valueOf(gp1.getLonDeg()), String.valueOf(gp1.getLonDegFrac()));
+
+ assertThat(gp1a).isEqualTo(gp1);
+
+ // case 2
+ final Geopoint gp2 = new Geopoint(51.34567d, 13.87654d);
+
+ checkDDD(gp2, 'N', 51, 34567, 'E', 13, 87654);
+
+ final Geopoint gp2a = new Geopoint(String.valueOf(gp2.getLatDir()), String.valueOf(gp2.getLatDeg()), String.valueOf(gp2.getLatDegFrac()),
+ String.valueOf(gp2.getLonDir()), String.valueOf(gp2.getLonDeg()), String.valueOf(gp2.getLonDegFrac()));
+
+ assertThat(gp2a).isEqualTo(gp2);
+
+ // case 3
+ final Geopoint gp3 = new Geopoint(51.29999833333333d, 13.8d);
+
+ checkDDD(gp3, 'N', 51, 30000, 'E', 13, 80000);
+
+ final Geopoint gp3a = new Geopoint(String.valueOf(gp3.getLatDir()), String.valueOf(gp3.getLatDeg()), String.valueOf(gp3.getLatDegFrac()),
+ String.valueOf(gp3.getLonDir()), String.valueOf(gp3.getLonDeg()), String.valueOf(gp3.getLonDegFrac()));
+
+ checkTolerance(gp3, gp3a, 5e-5);
+
+ // case 4
+ final Geopoint gp4 = new Geopoint(51.00012d, 13.00089d);
+
+ checkDDD(gp4, 'N', 51, 12, 'E', 13, 89);
+
+ final Geopoint gp4a = new Geopoint(String.valueOf(gp4.getLatDir()), String.valueOf(gp4.getLatDeg()), String.valueOf(gp4.getLatDegFrac()),
+ String.valueOf(gp4.getLonDir()), String.valueOf(gp4.getLonDeg()), String.valueOf(gp4.getLonDegFrac()));
+
+ checkTolerance(gp4, gp4a, 5e-5);
+ }
+
+ private static void checkDDD(Geopoint gp, char latDir, int latDeg, int latDegFrac, char lonDir, int lonDeg, int lonDegFrac) {
+ assertThat(gp.getLatDir()).isEqualTo(latDir);
+ assertThat(gp.getLatDeg()).isEqualTo(latDeg);
+ assertThat(gp.getLatDegFrac()).isEqualTo(latDegFrac);
+ assertThat(gp.getLonDir()).isEqualTo(lonDir);
+ assertThat(gp.getLonDeg()).isEqualTo(lonDeg);
+ assertThat(gp.getLonDegFrac()).isEqualTo(lonDegFrac);
+ }
+
+ private static void checkTolerance(Geopoint gp1, Geopoint gp2, double tolerance) {
+ assertThat(Math.abs(gp1.getLatitude() - gp2.getLatitude()) <= tolerance).isTrue();
+ assertThat(Math.abs(gp1.getLongitude() - gp2.getLongitude()) <= tolerance).isTrue();
+ }
+
+ public static void testDMM() {
+ // case 1
+ final Geopoint gp1 = new Geopoint(51.3d, 13.8d);
+
+ checkDMM(gp1, 'N', 51, 18, 0, 'E', 13, 48, 0);
+
+ final Geopoint gp1a = new Geopoint(String.valueOf(gp1.getLatDir()), String.valueOf(gp1.getLatDeg()), String.valueOf(gp1.getLatMin()), String.valueOf(gp1.getLatMinFrac()),
+ String.valueOf(gp1.getLonDir()), String.valueOf(gp1.getLonDeg()), String.valueOf(gp1.getLonMin()), String.valueOf(gp1.getLonMinFrac()));
+
+ assertThat(gp1a).isEqualTo(gp1);
+
+ // case 2
+ final Geopoint gp2 = new Geopoint(51.34567d, 13.87654d);
+
+ checkDMM(gp2, 'N', 51, 20, 740, 'E', 13, 52, 592);
+
+ final Geopoint gp2a = new Geopoint(String.valueOf(gp2.getLatDir()), String.valueOf(gp2.getLatDeg()), String.valueOf(gp2.getLatMin()), String.valueOf(gp2.getLatMinFrac()),
+ String.valueOf(gp2.getLonDir()), String.valueOf(gp2.getLonDeg()), String.valueOf(gp2.getLonMin()), String.valueOf(gp2.getLonMinFrac()));
+
+ checkTolerance(gp2, gp2a, 5e-5);
+
+ // case 3
+ final Geopoint gp3 = new Geopoint(51.3d, 13.8d);
+
+ checkDMM(gp3, 'N', 51, 18, 0, 'E', 13, 48, 0);
+
+ final Geopoint gp3a = new Geopoint(String.valueOf(gp3.getLatDir()), String.valueOf(gp3.getLatDeg()), String.valueOf(gp3.getLatMin()), String.valueOf(gp3.getLatMinFrac()),
+ String.valueOf(gp3.getLonDir()), String.valueOf(gp3.getLonDeg()), String.valueOf(gp3.getLonMin()), String.valueOf(gp3.getLonMinFrac()));
+
+ checkTolerance(gp3, gp3a, 5e-5);
+
+ // case 4
+ final Geopoint gp4 = new Geopoint(51.00012d, 13.00089d);
+
+ checkDMM(gp4, 'N', 51, 0, 7, 'E', 13, 0, 53);
+
+ final Geopoint gp4a = new Geopoint(String.valueOf(gp4.getLatDir()), String.valueOf(gp4.getLatDeg()), String.valueOf(gp4.getLatMin()), String.valueOf(gp4.getLatMinFrac()),
+ String.valueOf(gp4.getLonDir()), String.valueOf(gp4.getLonDeg()), String.valueOf(gp4.getLonMin()), String.valueOf(gp4.getLonMinFrac()));
+
+ checkTolerance(gp4, gp4a, 5e-5);
+ }
+
+ private static void checkDMM(Geopoint gp, char latDir, int latDeg, int latMin, int latMinFrac, char lonDir, int lonDeg, int lonMin, int lonMinFrac) {
+ assertThat(gp.getLatDir()).isEqualTo(latDir);
+ assertThat(gp.getLatDeg()).isEqualTo(latDeg);
+ assertThat(gp.getLatMin()).isEqualTo(latMin);
+ assertThat(gp.getLatMinFrac()).isEqualTo(latMinFrac);
+ assertThat(gp.getLonDir()).isEqualTo(lonDir);
+ assertThat(gp.getLonDeg()).isEqualTo(lonDeg);
+ assertThat(gp.getLonMin()).isEqualTo(lonMin);
+ assertThat(gp.getLonMinFrac()).isEqualTo(lonMinFrac);
+ }
+
+ public static void testDMS() {
+ // case 1
+ final Geopoint gp1 = new Geopoint(51.3d, 13.8d);
+
+ checkDMS(gp1, 'N', 51, 18, 0, 0, 'E', 13, 48, 0, 0);
+
+ final Geopoint gp1a = new Geopoint(String.valueOf(gp1.getLatDir()), String.valueOf(gp1.getLatDeg()), String.valueOf(gp1.getLatMin()), String.valueOf(gp1.getLatSec()), String.valueOf(gp1.getLatSecFrac()),
+ String.valueOf(gp1.getLonDir()), String.valueOf(gp1.getLonDeg()), String.valueOf(gp1.getLonMin()), String.valueOf(gp1.getLonSec()), String.valueOf(gp1.getLonSecFrac()));
+
+ assertThat(gp1a).isEqualTo(gp1);
+
+ // case 2
+ final Geopoint gp2 = new Geopoint(51.34567d, 13.87654d);
+
+ checkDMS(gp2, 'N', 51, 20, 44, 412, 'E', 13, 52, 35, 544);
+
+ final Geopoint gp2a = new Geopoint(String.valueOf(gp2.getLatDir()), String.valueOf(gp2.getLatDeg()), String.valueOf(gp2.getLatMin()), String.valueOf(gp2.getLatSec()), String.valueOf(gp2.getLatSecFrac()),
+ String.valueOf(gp2.getLonDir()), String.valueOf(gp2.getLonDeg()), String.valueOf(gp2.getLonMin()), String.valueOf(gp2.getLonSec()), String.valueOf(gp2.getLonSecFrac()));
+
+ checkTolerance(gp2, gp2a, 5e-6);
+
+ // case 3
+ final Geopoint gp3 = new Geopoint(51.29999833333333d, 13.8d);
+
+ checkDMS(gp3, 'N', 51, 17, 59, 994, 'E', 13, 48, 0, 0);
+
+ final Geopoint gp3a = new Geopoint(String.valueOf(gp3.getLatDir()), String.valueOf(gp3.getLatDeg()), String.valueOf(gp3.getLatMin()), String.valueOf(gp3.getLatSec()), String.valueOf(gp3.getLatSecFrac()),
+ String.valueOf(gp3.getLonDir()), String.valueOf(gp3.getLonDeg()), String.valueOf(gp3.getLonMin()), String.valueOf(gp3.getLonSec()), String.valueOf(gp3.getLonSecFrac()));
+
+ checkTolerance(gp3, gp3a, 5e-6);
+
+ // case 4
+ final Geopoint gp4 = new Geopoint(51.00012d, 13.00089d);
+
+ checkDMS(gp4, 'N', 51, 0, 0, 432, 'E', 13, 0, 3, 204);
+
+ final Geopoint gp4a = new Geopoint(String.valueOf(gp4.getLatDir()), String.valueOf(gp4.getLatDeg()), String.valueOf(gp4.getLatMin()), String.valueOf(gp4.getLatSec()), String.valueOf(gp4.getLatSecFrac()),
+ String.valueOf(gp4.getLonDir()), String.valueOf(gp4.getLonDeg()), String.valueOf(gp4.getLonMin()), String.valueOf(gp4.getLonSec()), String.valueOf(gp4.getLonSecFrac()));
+
+ checkTolerance(gp4, gp4a, 5e-6);
+ }
+
+ private static void checkDMS(Geopoint gp, char latDir, int latDeg, int latMin, int latSec, int latSecFrac, char lonDir, int lonDeg, int lonMin, int lonSec, int lonSecFrac) {
+ assertThat(gp.getLatDir()).isEqualTo(latDir);
+ assertThat(gp.getLatDeg()).isEqualTo(latDeg);
+ assertThat(gp.getLatMin()).isEqualTo(latMin);
+ assertThat(gp.getLatSec()).isEqualTo(latSec);
+ assertThat(gp.getLatSecFrac()).isEqualTo(latSecFrac);
+ assertThat(gp.getLonDir()).isEqualTo(lonDir);
+ assertThat(gp.getLonDeg()).isEqualTo(lonDeg);
+ assertThat(gp.getLonMin()).isEqualTo(lonMin);
+ assertThat(gp.getLonSec()).isEqualTo(lonSec);
+ assertThat(gp.getLonSecFrac()).isEqualTo(lonSecFrac);
+ }
+
+ private static void assertParseException(Runnable runnable) {
+ try {
+ runnable.run();
+ fail("Should have thrown Geopoint.ParseException");
+ } catch (Geopoint.ParseException e) {
+ //success
+ }
+ }
+
+ public static void testParseParam1() {
+ assertParseException(new Runnable() {
+
+ @SuppressWarnings("unused")
+ @Override
+ public void run() {
+ new Geopoint("some nonsense text");
+ }
+ });
+ }
+
+ public static void testParseParam2() {
+ assertParseException(new Runnable() {
+
+ @SuppressWarnings("unused")
+ @Override
+ public void run() {
+ new Geopoint("latitude", "longitude");
+ }
+ });
+ }
+
+ public static void testParseParam6() {
+ assertParseException(new Runnable() {
+
+ @SuppressWarnings("unused")
+ @Override
+ public void run() {
+ new Geopoint("latDir", "latDeg", "latDegFrac", "lonDir", "lonDeg", "lonDegFrac");
+ }
+ });
+ }
+
+ public static void testParseParam8() {
+ assertParseException(new Runnable() {
+
+ @SuppressWarnings("unused")
+ @Override
+ public void run() {
+ new Geopoint("latDir", "latDeg", "latMin", "latMinFrac", "lonDir", "lonDeg", "lonMin", "lonMinFrac");
+ }
+ });
+ }
+
+ public static void testParseParam10() {
+ assertParseException(new Runnable() {
+
+ @SuppressWarnings("unused")
+ @Override
+ public void run() {
+ new Geopoint("latDir", "latDeg", "latMin", "latSec", "latSecFrac", "lonDir", "lonDeg", "lonMin", "lonSec", "lonSecFrac");
+ }
+ });
+ }
+}
diff --git a/tests/src/cgeo/geocaching/location/UnitsTest.java b/tests/src/cgeo/geocaching/location/UnitsTest.java
new file mode 100644
index 0000000..f03f6f0
--- /dev/null
+++ b/tests/src/cgeo/geocaching/location/UnitsTest.java
@@ -0,0 +1,74 @@
+package cgeo.geocaching.location;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import cgeo.CGeoTestCase;
+import cgeo.geocaching.location.Units;
+import cgeo.geocaching.settings.Settings;
+import cgeo.geocaching.settings.TestSettings;
+
+import org.apache.commons.lang3.StringUtils;
+
+public class UnitsTest extends CGeoTestCase {
+
+ private static void assertDistance(final String expected, final float distance) {
+ final String actual = Units.getDistanceFromKilometers(distance);
+ if (!StringUtils.equals(expected, actual.replace(',', '.'))) { // make 1.2 the same as 1,2
+ fail("getHumanDistance(" + distance +
+ ") [metric: " + (!Settings.useImperialUnits() ? "yes" : "no") +
+ "] fails to match " + expected + ": " + actual);
+ }
+ }
+
+ // Make method non-static so that Settings is initialized
+ @SuppressWarnings("static-method")
+ public void testDistance() {
+ assertThat(Units.getDistanceFromKilometers(null)).isEqualTo("?");
+ final boolean savedImperial = Settings.useImperialUnits();
+ try {
+ TestSettings.setUseImperialUnits(false);
+ assertDistance("123 km", 122.782f);
+ assertDistance("123 km", 123.456f);
+ assertDistance("12.3 km", 12.3456f);
+ assertDistance("1.23 km", 1.23456f);
+ assertDistance("123 m", 0.123456f);
+ TestSettings.setUseImperialUnits(true);
+ assertDistance("76.7 mi", 123.456f);
+ assertDistance("7.67 mi", 12.3456f);
+ assertDistance("0.77 mi", 1.23456f);
+ assertDistance("405 ft", 0.123456f);
+ assertDistance("40.5 ft", 0.0123456f);
+ } finally {
+ TestSettings.setUseImperialUnits(savedImperial);
+ }
+ }
+
+ // Make method non-static so that Settings is initialized
+ @SuppressWarnings("static-method")
+ public void testSpeed() {
+ assertThat(Units.getDistanceFromKilometers(null)).isEqualTo("?");
+ final boolean savedImperial = Settings.useImperialUnits();
+ try {
+ TestSettings.setUseImperialUnits(false);
+ assertSpeed("123 km/h", 122.782f);
+ assertSpeed("123 km/h", 123.456f);
+ assertSpeed("12 km/h", 12.3456f);
+ assertSpeed("1 km/h", 1.23456f);
+ assertSpeed("0 km/h", 0.123456f);
+ TestSettings.setUseImperialUnits(true);
+ assertSpeed("77 mph", 123.456f);
+ assertSpeed("8 mph", 12.3456f);
+ assertSpeed("1 mph", 1.23456f);
+ } finally {
+ TestSettings.setUseImperialUnits(savedImperial);
+ }
+ }
+
+ private static void assertSpeed(final String expected, final float kilometersPerHour) {
+ final String actual = Units.getSpeed(kilometersPerHour);
+ if (!StringUtils.equals(expected, actual.replace(',', '.'))) {
+ fail("speed " + actual + " does not match expected " + expected);
+ }
+ }
+
+}
diff --git a/tests/src/cgeo/geocaching/location/ViewportTest.java b/tests/src/cgeo/geocaching/location/ViewportTest.java
new file mode 100644
index 0000000..be15458
--- /dev/null
+++ b/tests/src/cgeo/geocaching/location/ViewportTest.java
@@ -0,0 +1,109 @@
+package cgeo.geocaching.location;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import cgeo.geocaching.ICoordinates;
+import cgeo.geocaching.location.Geopoint;
+import cgeo.geocaching.location.Viewport;
+
+import org.eclipse.jdt.annotation.NonNull;
+
+import android.annotation.SuppressLint;
+import android.test.AndroidTestCase;
+
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Locale;
+import java.util.Set;
+
+public class ViewportTest extends AndroidTestCase {
+
+ final private static @NonNull
+ Viewport vpRef = new Viewport(new Geopoint(-1.0, -2.0), new Geopoint(3.0, 4.0));
+
+ public static void assertBounds(final Viewport vp) {
+ assertEquals(new Geopoint(1.0, 1.0), vp.center);
+ assertEquals(new Geopoint(3.0, 4.0), vp.topRight);
+ assertEquals(new Geopoint(-1.0, -2.0), vp.bottomLeft);
+ }
+
+ public static void testCreationBounds() {
+ assertBounds(new Viewport(new Geopoint(-1.0, -2.0), new Geopoint(3.0, 4.0)));
+ assertBounds(new Viewport(new Geopoint(3.0, 4.0), new Geopoint(-1.0, -2.0)));
+ assertBounds(new Viewport(new Geopoint(-1.0, 4.0), new Geopoint(3.0, -2.0)));
+ assertBounds(new Viewport(new Geopoint(3.0, -2.0), new Geopoint(-1.0, 4.0)));
+ }
+
+ public static void testCreationCenter() {
+ assertBounds(new Viewport(new Geopoint(1.0, 1.0), 4.0, 6.0));
+ }
+
+ public static void testCreationSeparate() {
+ assertBounds(vpRef);
+ }
+
+ public static void testMinMax() {
+ assertThat(vpRef.getLatitudeMin()).isEqualTo(-1.0);
+ assertThat(vpRef.getLatitudeMax()).isEqualTo(3.0);
+ assertThat(vpRef.getLongitudeMin()).isEqualTo(-2.0);
+ assertThat(vpRef.getLongitudeMax()).isEqualTo(4.0);
+ }
+
+ public static void testSpans() {
+ assertThat(vpRef.getLatitudeSpan()).isEqualTo(4.0);
+ assertThat(vpRef.getLongitudeSpan()).isEqualTo(6.0);
+ }
+
+ public static void testInViewport() {
+ assertThat(vpRef.contains(new Geopoint(-2.0, -2.0))).isFalse();
+ assertThat(vpRef.contains(new Geopoint(4.0, 4.0))).isFalse();
+ assertThat(vpRef.contains(Geopoint.ZERO)).isTrue();
+ assertThat(vpRef.contains(new Geopoint(-1.0, -2.0))).isTrue();
+ assertThat(vpRef.contains(new Geopoint(3.0, 4.0))).isTrue();
+ }
+
+ @SuppressLint("DefaultLocale")
+ public static void testSqlWhere() {
+ assertThat(vpRef.sqlWhere(null).toString()).isEqualTo("latitude >= -1.0 and latitude <= 3.0 and longitude >= -2.0 and longitude <= 4.0");
+ assertThat(vpRef.sqlWhere("t").toString()).isEqualTo("t.latitude >= -1.0 and t.latitude <= 3.0 and t.longitude >= -2.0 and t.longitude <= 4.0");
+ Locale current = null;
+ try {
+ current = Locale.getDefault();
+ Locale.setDefault(Locale.FRENCH);
+ assertThat(String.format("%.2g", 1.0d)).isEqualTo("1,0"); // Control that we are in a locale with comma separator
+ assertThat(vpRef.sqlWhere("t").toString()).isEqualTo("t.latitude >= -1.0 and t.latitude <= 3.0 and t.longitude >= -2.0 and t.longitude <= 4.0");
+ } finally {
+ Locale.setDefault(current);
+ }
+ }
+
+ public static void testEquals() {
+ assertThat(vpRef).isEqualTo(vpRef);
+ assertEquals(vpRef, new Viewport(vpRef.bottomLeft, vpRef.topRight));
+ assertThat(vpRef.equals(new Viewport(new Geopoint(0.0, 0.0), 1.0, 1.0))).isFalse();
+ }
+
+ public static void testResize() {
+ assertThat(vpRef.resize(1.0)).isEqualTo(vpRef);
+ assertEquals(new Viewport(new Geopoint(-3.0, -5.0), new Geopoint(5.0, 7.0)), vpRef.resize(2.0));
+ assertEquals(new Viewport(new Geopoint(0.0, -0.5), new Geopoint(2.0, 2.5)), vpRef.resize(0.5));
+ }
+
+ public static void testIncludes() {
+ assertThat(vpRef.includes(vpRef)).isTrue();
+ assertThat(vpRef.includes(vpRef.resize(0.5))).isTrue();
+ assertThat(vpRef.includes(vpRef.resize(2.0))).isFalse();
+ }
+
+ public static void testContaining() {
+ assertThat(Viewport.containing(Collections.singleton((ICoordinates) null))).isNull();
+ final Set<Geopoint> points = new HashSet<Geopoint>();
+ points.add(vpRef.bottomLeft);
+ assertEquals(new Viewport(vpRef.bottomLeft, vpRef.bottomLeft), Viewport.containing(points));
+ points.add(vpRef.topRight);
+ assertThat(Viewport.containing(points)).isEqualTo(vpRef);
+ points.add(vpRef.center);
+ assertThat(Viewport.containing(points)).isEqualTo(vpRef);
+ }
+
+}