diff options
| -rw-r--r-- | main/src/cgeo/geocaching/cgBase.java | 38 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/cgCache.java | 3 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/cgData.java | 3 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/cgeoapplication.java | 7 | ||||
| -rw-r--r-- | tests/src/cgeo/geocaching/cgBaseTest.java | 1 | ||||
| -rw-r--r-- | tests/src/cgeo/geocaching/cgeoApplicationTest.java | 128 | ||||
| -rw-r--r-- | tests/src/cgeo/geocaching/test/mock/MockedCache.java | 4 |
7 files changed, 169 insertions, 15 deletions
diff --git a/main/src/cgeo/geocaching/cgBase.java b/main/src/cgeo/geocaching/cgBase.java index 8173ee0..9b4e09c 100644 --- a/main/src/cgeo/geocaching/cgBase.java +++ b/main/src/cgeo/geocaching/cgBase.java @@ -374,7 +374,13 @@ public class cgBase { return StatusCode.NO_ERROR; } - public static boolean checkLogin(String page) { + /** + * Check if the user has been logged in when he retrieved the data. + * + * @param page + * @return true = User has been logged in, false else + */ + private static boolean checkLogin(String page) { if (StringUtils.isBlank(page)) { Log.e(Settings.tag, "cgeoBase.checkLogin: No page given"); return false; @@ -913,7 +919,7 @@ public class cgBase { // on watchlist cache.setOnWatchlist(BaseUtils.matches(page, GCConstants.PATTERN_WATCHLIST)); - // latitude and longitude + // latitude and longitude. Can only be retrieved if user is logged in cache.setLatlon(BaseUtils.getMatch(page, GCConstants.PATTERN_LATLON, true, cache.getLatlon())); if (StringUtils.isNotEmpty(cache.getLatlon())) { try { @@ -1196,11 +1202,13 @@ public class cgBase { int position = 0; List<cgLog> allLogs = cache.getLogs(); List<cgLog> friendLogs = loadLogsFromDetails(page, cache, true); - for (cgLog log : friendLogs) { - if (allLogs.contains(log)) { - allLogs.get(allLogs.indexOf(log)).friend = true; - } else { - allLogs.add(position++, log); + if (friendLogs != null) { + for (cgLog log : friendLogs) { + if (allLogs.contains(log)) { + allLogs.get(allLogs.indexOf(log)).friend = true; + } else { + allLogs.add(position++, log); + } } } } @@ -2393,11 +2401,20 @@ public class cgBase { return getResponseDataOnError(response); } + /** + * POST HTTP request. Do the request a second time if the user is not logged in + * + * @param uri + * @return + */ public static String postRequestLogged(final String uri) { - final String data = getResponseData(postRequest(uri, null)); + HttpResponse response = postRequest(uri, null); + String data = getResponseData(response); + if (!checkLogin(data)) { if (login() == StatusCode.NO_ERROR) { - return getResponseData(postRequest(uri, null)); + response = postRequest(uri, null); + data = getResponseData(response); } else { Log.i(Settings.tag, "Working as guest."); } @@ -2406,7 +2423,7 @@ public class cgBase { } /** - * GET HTTP request + * GET HTTP request. Do the request a second time if the user is not logged in * * @param uri * @param params @@ -2427,7 +2444,6 @@ public class cgBase { Log.i(Settings.tag, "Working as guest."); } } - return data; } diff --git a/main/src/cgeo/geocaching/cgCache.java b/main/src/cgeo/geocaching/cgCache.java index 1bad99d..e7fee43 100644 --- a/main/src/cgeo/geocaching/cgCache.java +++ b/main/src/cgeo/geocaching/cgCache.java @@ -688,6 +688,9 @@ public class cgCache implements ICache { this.coords = coords; } + /** + * @return true if the coords are from the cache details page and the user has been logged in + */ public boolean isReliableLatLon() { return reliableLatLon; } diff --git a/main/src/cgeo/geocaching/cgData.java b/main/src/cgeo/geocaching/cgData.java index 206d3f3..df98fb0 100644 --- a/main/src/cgeo/geocaching/cgData.java +++ b/main/src/cgeo/geocaching/cgData.java @@ -2991,7 +2991,8 @@ public class cgData { * @param geocodes * list of geocodes to drop from cache */ - private void dropCaches(final List<String> geocodes) { + public void dropCaches(final List<String> geocodes) { + init(); // Drop caches from the database final ArrayList<String> quotedGeocodes = new ArrayList<String>(geocodes.size()); for (final String geocode : geocodes) { diff --git a/main/src/cgeo/geocaching/cgeoapplication.java b/main/src/cgeo/geocaching/cgeoapplication.java index 96fa5e0..99d894d 100644 --- a/main/src/cgeo/geocaching/cgeoapplication.java +++ b/main/src/cgeo/geocaching/cgeoapplication.java @@ -495,6 +495,13 @@ public class cgeoapplication extends Application { storage.dropStored(listId); } + /** + * {@link cgData#dropCaches(List)} + */ + public void dropCaches(final List<String> geocodes) { + storage.dropCaches(geocodes); + } + public List<cgTrackable> loadInventory(String geocode) { return storage.loadInventory(geocode); } 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; + } } |
