diff options
Diffstat (limited to 'tests/src/cgeo/geocaching/cgeoApplicationTest.java')
| -rw-r--r-- | tests/src/cgeo/geocaching/cgeoApplicationTest.java | 178 |
1 files changed, 92 insertions, 86 deletions
diff --git a/tests/src/cgeo/geocaching/cgeoApplicationTest.java b/tests/src/cgeo/geocaching/cgeoApplicationTest.java index 83bf28d..b2537a3 100644 --- a/tests/src/cgeo/geocaching/cgeoApplicationTest.java +++ b/tests/src/cgeo/geocaching/cgeoApplicationTest.java @@ -12,6 +12,7 @@ import cgeo.geocaching.enumerations.LogType; import cgeo.geocaching.enumerations.StatusCode; import cgeo.geocaching.geopoint.Geopoint; import cgeo.geocaching.geopoint.Viewport; +import cgeo.geocaching.list.StoredList; import cgeo.geocaching.loaders.RecaptchaReceiver; import cgeo.geocaching.settings.Settings; import cgeo.geocaching.settings.TestSettings; @@ -111,7 +112,7 @@ public class cgeoApplicationTest extends CGeoTestCase { if (Settings.isPremiumMember() || search.getError() == null) { assertEquals(1, search.getGeocodes().size()); assertTrue(search.getGeocodes().contains(geocode)); - return cgData.loadCache(geocode, LoadFlags.LOAD_CACHE_OR_DB); + return DataStore.loadCache(geocode, LoadFlags.LOAD_CACHE_OR_DB); } assertEquals(0, search.getGeocodes().size()); return null; @@ -128,36 +129,16 @@ public class cgeoApplicationTest extends CGeoTestCase { } /** - * Test {@link Geocache#searchByGeocode(String, String, int, boolean, CancellableHandler)} + * Set the login data to the cgeo login, run the given Runnable, and restore the login. + * + * @param runnable */ - @MediumTest - public static void testSearchByGeocodeNotLoggedIn() { + private static void withMockedLoginDo(final Runnable runnable) { final ImmutablePair<String, String> login = Settings.getGcLogin(); final String memberStatus = Settings.getMemberStatus(); try { - // non premium cache - MockedCache cache = new GC2CJPF(); - - deleteCacheFromDBAndLogout(cache.getGeocode()); - - SearchResult search = Geocache.searchByGeocode(cache.getGeocode(), null, StoredList.TEMPORARY_LIST_ID, true, null); - assertNotNull(search); - assertEquals(1, search.getGeocodes().size()); - assertTrue(search.getGeocodes().contains(cache.getGeocode())); - final Geocache searchedCache = search.getFirstCacheFromResult(LoadFlags.LOAD_CACHE_OR_DB); - // 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 = Geocache.searchByGeocode(cache.getGeocode(), null, StoredList.TEMPORARY_LIST_ID, true, null); - assertNotNull(search); - assertEquals(0, search.getGeocodes().size()); - + runnable.run(); } finally { // restore user and password TestSettings.setLogin(login.left, login.right); @@ -170,26 +151,53 @@ public class cgeoApplicationTest extends CGeoTestCase { * Test {@link Geocache#searchByGeocode(String, String, int, boolean, CancellableHandler)} */ @MediumTest - public static void testSearchErrorOccured() { - final ImmutablePair<String, String> login = Settings.getGcLogin(); - final String memberStatus = Settings.getMemberStatus(); + public static void testSearchByGeocodeNotLoggedIn() { + withMockedLoginDo(new Runnable() { - try { - // non premium cache - final MockedCache cache = new GC1ZXX2(); + public void run() { + // non premium cache + MockedCache cache = new GC2CJPF(); - deleteCacheFromDBAndLogout(cache.getGeocode()); + deleteCacheFromDBAndLogout(cache.getGeocode()); - final SearchResult search = Geocache.searchByGeocode(cache.getGeocode(), null, StoredList.TEMPORARY_LIST_ID, true, null); - assertNotNull(search); - assertEquals(0, search.getGeocodes().size()); + SearchResult search = Geocache.searchByGeocode(cache.getGeocode(), null, StoredList.TEMPORARY_LIST_ID, true, null); + assertNotNull(search); + assertEquals(1, search.getGeocodes().size()); + assertTrue(search.getGeocodes().contains(cache.getGeocode())); + final Geocache searchedCache = search.getFirstCacheFromResult(LoadFlags.LOAD_CACHE_OR_DB); + // coords must be null if the user is not logged in + assertNull(searchedCache.getCoords()); - } finally { - // restore user and password - TestSettings.setLogin(login.left, login.right); - Settings.setMemberStatus(memberStatus); - Login.login(); - } + // premium cache. Not visible to guests + cache = new GC2JVEH(); + + deleteCacheFromDBAndLogout(cache.getGeocode()); + + search = Geocache.searchByGeocode(cache.getGeocode(), null, StoredList.TEMPORARY_LIST_ID, true, null); + assertNotNull(search); + assertEquals(0, search.getGeocodes().size()); + } + }); + } + + /** + * Test {@link Geocache#searchByGeocode(String, String, int, boolean, CancellableHandler)} + */ + @MediumTest + public static void testSearchErrorOccured() { + withMockedLoginDo(new Runnable() { + + public void run() { + // non premium cache + final MockedCache cache = new GC1ZXX2(); + + deleteCacheFromDBAndLogout(cache.getGeocode()); + + final SearchResult search = Geocache.searchByGeocode(cache.getGeocode(), null, StoredList.TEMPORARY_LIST_ID, true, null); + assertNotNull(search); + assertEquals(0, search.getGeocodes().size()); + } + }); } /** @@ -295,7 +303,7 @@ public class cgeoApplicationTest extends CGeoTestCase { SearchResult search = ConnectorFactory.searchByViewport(viewport, tokens); assertNotNull(search); assertTrue(search.getGeocodes().contains(mockedCache.getGeocode())); - Geocache parsedCache = cgData.loadCache(mockedCache.getGeocode(), LoadFlags.LOAD_CACHE_OR_DB); + Geocache parsedCache = DataStore.loadCache(mockedCache.getGeocode(), LoadFlags.LOAD_CACHE_OR_DB); assertEquals(Settings.isPremiumMember(), mockedCache.getCoords().equals(parsedCache.getCoords())); assertEquals(Settings.isPremiumMember(), parsedCache.isReliableLatLon()); @@ -307,7 +315,7 @@ public class cgeoApplicationTest extends CGeoTestCase { search = ConnectorFactory.searchByViewport(viewport, tokens); assertNotNull(search); assertTrue(search.getGeocodes().contains(mockedCache.getGeocode())); - parsedCache = cgData.loadCache(mockedCache.getGeocode(), LoadFlags.LOAD_CACHE_OR_DB); + parsedCache = DataStore.loadCache(mockedCache.getGeocode(), LoadFlags.LOAD_CACHE_OR_DB); assertEquals(Settings.isPremiumMember(), mockedCache.getCoords().equals(parsedCache.getCoords())); assertEquals(Settings.isPremiumMember(), parsedCache.isReliableLatLon()); @@ -326,56 +334,54 @@ public class cgeoApplicationTest extends CGeoTestCase { */ @MediumTest public static void testSearchByViewportNotLoggedIn() { + withMockedLoginDo(new Runnable() { - final ImmutablePair<String, String> login = Settings.getGcLogin(); - final String memberStatus = Settings.getMemberStatus(); - final Strategy strategy = Settings.getLiveMapStrategy(); - final Strategy testStrategy = Strategy.FAST; // FASTEST, FAST or DETAILED for tests - Settings.setLiveMapStrategy(testStrategy); - final CacheType cacheType = Settings.getCacheType(); - - try { - - final String[] tokens = null; // without a valid token we are "logged off" + public void run() { + final Strategy strategy = Settings.getLiveMapStrategy(); + final Strategy testStrategy = Strategy.FAST; // FASTEST, FAST or DETAILED for tests + Settings.setLiveMapStrategy(testStrategy); + final CacheType cacheType = Settings.getCacheType(); - // non premium cache - MockedCache cache = new GC2CJPF(); - deleteCacheFromDBAndLogout(cache.getGeocode()); - Tile.Cache.removeFromTileCache(cache); - Settings.setCacheType(CacheType.ALL); + try { - Viewport viewport = new Viewport(cache, 0.003, 0.003); - SearchResult search = ConnectorFactory.searchByViewport(viewport, tokens); + final String[] tokens = null; // without a valid token we are "logged off" - assertNotNull(search); - assertTrue(search.getGeocodes().contains(cache.getGeocode())); - // coords differ - final Geocache cacheFromViewport = cgData.loadCache(cache.getGeocode(), LoadFlags.LOAD_CACHE_OR_DB); - Log.d("cgeoApplicationTest.testSearchByViewportNotLoggedIn: Coords expected = " + cache.getCoords()); - Log.d("cgeoApplicationTest.testSearchByViewportNotLoggedIn: Coords actual = " + cacheFromViewport.getCoords()); - assertFalse(cache.getCoords().isEqualTo(cacheFromViewport.getCoords(), 1e-3)); - // depending on the chosen strategy the coords can be reliable or not - assertEquals(testStrategy == Strategy.DETAILED, cacheFromViewport.isReliableLatLon()); + // non premium cache + MockedCache cache = new GC2CJPF(); + deleteCacheFromDBAndLogout(cache.getGeocode()); + Tile.Cache.removeFromTileCache(cache); + Settings.setCacheType(CacheType.ALL); - // premium cache - cache = new GC2JVEH(); - deleteCacheFromDBAndLogout(cache.getGeocode()); + Viewport viewport = new Viewport(cache, 0.003, 0.003); + SearchResult search = ConnectorFactory.searchByViewport(viewport, tokens); - viewport = new Viewport(cache, 0.003, 0.003); - search = ConnectorFactory.searchByViewport(viewport, tokens); + assertNotNull(search); + assertTrue(search.getGeocodes().contains(cache.getGeocode())); + // coords differ + final Geocache cacheFromViewport = DataStore.loadCache(cache.getGeocode(), LoadFlags.LOAD_CACHE_OR_DB); + Log.d("cgeoApplicationTest.testSearchByViewportNotLoggedIn: Coords expected = " + cache.getCoords()); + Log.d("cgeoApplicationTest.testSearchByViewportNotLoggedIn: Coords actual = " + cacheFromViewport.getCoords()); + assertFalse(cache.getCoords().isEqualTo(cacheFromViewport.getCoords(), 1e-3)); + // depending on the chosen strategy the coords can be reliable or not + assertEquals(testStrategy == Strategy.DETAILED, cacheFromViewport.isReliableLatLon()); + + // premium cache + cache = new GC2JVEH(); + deleteCacheFromDBAndLogout(cache.getGeocode()); + + viewport = new Viewport(cache, 0.003, 0.003); + search = ConnectorFactory.searchByViewport(viewport, tokens); - assertNotNull(search); - // depending on the chosen strategy the cache is part of the search or not - assertEquals(testStrategy == Strategy.DETAILED, search.getGeocodes().contains(cache.getGeocode())); + assertNotNull(search); + // depending on the chosen strategy the cache is part of the search or not + assertEquals(testStrategy == Strategy.DETAILED, search.getGeocodes().contains(cache.getGeocode())); - } finally { - // restore user and password - TestSettings.setLogin(login.left, login.right); - Settings.setMemberStatus(memberStatus); - Login.login(); - Settings.setLiveMapStrategy(strategy); - Settings.setCacheType(cacheType); - } + } finally { + Settings.setLiveMapStrategy(strategy); + Settings.setCacheType(cacheType); + } + } + }); } /** |
