aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/src/cgeo/geocaching/cgBaseTest.java1
-rw-r--r--tests/src/cgeo/geocaching/cgeoApplicationTest.java128
-rw-r--r--tests/src/cgeo/geocaching/test/mock/MockedCache.java4
3 files changed, 130 insertions, 3 deletions
diff --git a/tests/src/cgeo/geocaching/cgBaseTest.java b/tests/src/cgeo/geocaching/cgBaseTest.java
index f772ead..506a1f4 100644
--- a/tests/src/cgeo/geocaching/cgBaseTest.java
+++ b/tests/src/cgeo/geocaching/cgBaseTest.java
@@ -32,6 +32,7 @@ public class cgBaseTest extends AndroidTestCase {
Assert.assertEquals(expected.getTerrain(), actual.getTerrain());
Assert.assertEquals(expected.getLatitude(), actual.getLatitude());
Assert.assertEquals(expected.getLongitude(), actual.getLongitude());
+ assertTrue(actual.isReliableLatLon());
Assert.assertEquals(expected.isDisabled(), actual.isDisabled());
Assert.assertEquals(expected.isOwn(), actual.isOwn());
Assert.assertEquals(expected.isArchived(), actual.isArchived());
diff --git a/tests/src/cgeo/geocaching/cgeoApplicationTest.java b/tests/src/cgeo/geocaching/cgeoApplicationTest.java
index 0e5731e..a1aa415 100644
--- a/tests/src/cgeo/geocaching/cgeoApplicationTest.java
+++ b/tests/src/cgeo/geocaching/cgeoApplicationTest.java
@@ -3,14 +3,23 @@ package cgeo.geocaching;
import cgeo.geocaching.enumerations.CacheType;
import cgeo.geocaching.enumerations.StatusCode;
import cgeo.geocaching.geopoint.Geopoint;
+import cgeo.geocaching.geopoint.Viewport;
import cgeo.geocaching.test.RegExPerformanceTest;
+import cgeo.geocaching.test.mock.GC1ZXX2;
+import cgeo.geocaching.test.mock.GC2CJPF;
+import cgeo.geocaching.test.mock.GC2JVEH;
import cgeo.geocaching.test.mock.MockedCache;
import cgeo.geocaching.utils.CancellableHandler;
+import org.apache.commons.lang3.tuple.ImmutablePair;
+
+import android.os.Handler;
import android.test.ApplicationTestCase;
import android.test.suitebuilder.annotation.MediumTest;
import android.test.suitebuilder.annotation.SmallTest;
+import android.util.Log;
+import java.util.Arrays;
import java.util.Date;
import junit.framework.Assert;
@@ -40,9 +49,10 @@ public class cgeoApplicationTest extends ApplicationTestCase<cgeoapplication> {
* any and all failures in other tests. This is not guaranteed to run before
* other tests, as junit uses reflection to find the tests.
*/
+ @SuppressWarnings("static-method")
@SmallTest
public void testPreconditions() {
- // intentionally left blank
+ assertEquals(StatusCode.NO_ERROR, cgBase.login());
}
/**
@@ -108,6 +118,43 @@ public class cgeoApplicationTest extends ApplicationTestCase<cgeoapplication> {
}
/**
+ * Test {@link cgBase#searchByGeocode(String, String, int, boolean, CancellableHandler)}
+ */
+ @MediumTest
+ public static void testSearchByGeocodeNotLoggedIn() {
+ ImmutablePair<String, String> login = Settings.getLogin();
+
+ try {
+ // non premium cache
+ MockedCache cache = new GC1ZXX2();
+
+ deleteCacheFromDBAndLogout(cache.getGeocode());
+
+ ParseResult search = cgBase.searchByGeocode(cache.getGeocode(), null, 0, true, null);
+ assertNotNull(search);
+ assertEquals(1, search.getGeocodes().size());
+ assertTrue(search.getGeocodes().contains(cache.getGeocode()));
+ cgCache searchedCache = search.cacheList.get(0);
+ // coords must be null if the user is not logged in
+ assertNull(searchedCache.getCoords());
+
+ // premium cache. Not visible to guests
+ cache = new GC2JVEH();
+
+ deleteCacheFromDBAndLogout(cache.getGeocode());
+
+ search = cgBase.searchByGeocode(cache.getGeocode(), null, 0, true, null);
+ assertNotNull(search);
+ assertEquals(0, search.getGeocodes().size());
+
+ } finally {
+ // restore user and password
+ Settings.setLogin(login.left, login.right);
+ cgBase.login();
+ }
+ }
+
+ /**
* Test {@link cgBase#searchByCoords(cgSearchThread, Geopoint, String, int, boolean)}
*/
@MediumTest
@@ -141,9 +188,74 @@ public class cgeoApplicationTest extends ApplicationTestCase<cgeoapplication> {
}
/**
+ * Test {@link cgBase#searchByViewport(String, Viewport)}
+ */
+ @MediumTest
+ public static void testSearchByViewport() {
+ GC2JVEH cache = new GC2JVEH();
+
+ final String token = cgBase.getMapUserToken(new Handler());
+ final Viewport viewport = new Viewport(cache.getCoords(), 0.003, 0.003);
+ final ParseResult search = cgBase.searchByViewport(token, viewport);
+
+ if (Settings.isPremiumMember()) {
+ assertNotNull(search);
+ assertTrue(search.cacheList.size() >= 1);
+ assertTrue(search.getGeocodes().contains(cache.getGeocode()));
+ Log.d(Settings.tag, "cgeoApplicationTest.testSearchByViewport: Coords = " + cache.getCoords());
+ assertTrue(cache.getCoords().toString().compareTo(cgeoapplication.getInstance().getCacheByGeocode(cache.getGeocode()).getCoords().toString()) == 0);
+ assertFalse(cgeoapplication.getInstance().getCacheByGeocode(cache.getGeocode()).isReliableLatLon());
+ }
+ }
+
+ /**
+ * Test {@link cgBase#searchByViewport(String, Viewport)}
+ */
+ @MediumTest
+ public static void testSearchByViewportNotLoggedIn() {
+ ImmutablePair<String, String> login = Settings.getLogin();
+
+ try {
+
+ final String token = null; // without a valid token we are "logged off"
+
+ // non premium cache
+ MockedCache cache = new GC2CJPF();
+ deleteCacheFromDBAndLogout(cache.getGeocode());
+
+ Viewport viewport = new Viewport(cache.getCoords(), 0.003, 0.003);
+ ParseResult search = cgBase.searchByViewport(token, viewport);
+
+ assertNotNull(search);
+ assertTrue(search.getGeocodes().contains(cache.getGeocode()));
+ // coords differ
+ Log.d(Settings.tag, "cgeoApplicationTest.testSearchByViewportNotLoggedIn: Coords expected = " + cache.getCoords());
+ Log.d(Settings.tag, "cgeoApplicationTest.testSearchByViewportNotLoggedIn: Coords actual = " + cgeoapplication.getInstance().getCacheByGeocode(cache.getGeocode()).getCoords());
+ assertFalse(cache.getCoords().isEqualTo(cgeoapplication.getInstance().getCacheByGeocode(cache.getGeocode()).getCoords()));
+ assertFalse(cgeoapplication.getInstance().getCacheByGeocode(cache.getGeocode()).isReliableLatLon());
+
+ // premium cache
+ cache = new GC2JVEH();
+ deleteCacheFromDBAndLogout(cache.getGeocode());
+
+ viewport = new Viewport(cache.getCoords(), 0.003, 0.003);
+ search = cgBase.searchByViewport(token, viewport);
+
+ assertNotNull(search);
+ // It's a premium member cache only and thus not visible to guests
+ assertFalse(search.getGeocodes().contains(cache.getGeocode()));
+
+ } finally {
+ // restore user and password
+ Settings.setLogin(login.left, login.right);
+ cgBase.login();
+ }
+ }
+
+ /**
* Test cache parsing. Esp. useful after a GC.com update
*/
- public static void testCacheBasics() {
+ public static void testSearchByGeocodeBasis() {
for (MockedCache mockedCache : RegExPerformanceTest.MOCKED_CACHES) {
mockedCache.setMockedDataUser(Settings.getUsername());
cgCache parsedCache = cgeoApplicationTest.testSearchByGeocode(mockedCache.getGeocode());
@@ -156,7 +268,7 @@ public class cgeoApplicationTest extends ApplicationTestCase<cgeoapplication> {
/**
* Caches that are good test cases
*/
- public static void testCacheSpecialties() {
+ public static void testSearchByGeocodeSpecialties() {
cgCache GCV2R9 = cgeoApplicationTest.testSearchByGeocode("GCV2R9");
Assert.assertEquals("California, United States", GCV2R9.getLocation());
@@ -164,5 +276,15 @@ public class cgeoApplicationTest extends ApplicationTestCase<cgeoapplication> {
Assert.assertEquals("Ms.Marple/Mr.Stringer", GC1ZXEZ.getOwnerReal());
}
+ /** Remove cache from DB and cache to ensure that the cache is not loaded from the database */
+ private static void deleteCacheFromDBAndLogout(String geocode) {
+ cgeoapplication.getInstance().dropCaches(Arrays.asList(geocode));
+ cgeoapplication.getInstance().removeCacheFromCache(geocode);
+
+ cgBase.logout();
+ // Modify login data to avoid an automatic login again
+ Settings.setLogin("c:geo", "c:geo");
+ }
+
}
diff --git a/tests/src/cgeo/geocaching/test/mock/MockedCache.java b/tests/src/cgeo/geocaching/test/mock/MockedCache.java
index 38061e0..d089ee3 100644
--- a/tests/src/cgeo/geocaching/test/mock/MockedCache.java
+++ b/tests/src/cgeo/geocaching/test/mock/MockedCache.java
@@ -148,4 +148,8 @@ public abstract class MockedCache implements ICache {
public String getNameForSorting() {
return getName();
}
+
+ public Geopoint getCoords() {
+ return coords;
+ }
}