diff options
Diffstat (limited to 'tests/src/cgeo/geocaching/cgeoApplicationTest.java')
| -rw-r--r-- | tests/src/cgeo/geocaching/cgeoApplicationTest.java | 230 |
1 files changed, 121 insertions, 109 deletions
diff --git a/tests/src/cgeo/geocaching/cgeoApplicationTest.java b/tests/src/cgeo/geocaching/cgeoApplicationTest.java index f7d3158..9c882a5 100644 --- a/tests/src/cgeo/geocaching/cgeoApplicationTest.java +++ b/tests/src/cgeo/geocaching/cgeoApplicationTest.java @@ -56,7 +56,7 @@ public class cgeoApplicationTest extends CGeoTestCase { */ @MediumTest public static void testSearchTrackableNotExisting() { - cgTrackable tb = GCParser.searchTrackable("123456", null, null); + final Trackable tb = GCParser.searchTrackable("123456", null, null); assertNull(tb); } @@ -65,7 +65,7 @@ public class cgeoApplicationTest extends CGeoTestCase { */ @MediumTest public static void testSearchTrackable() { - final cgTrackable tb = GCParser.searchTrackable("TB2J1VZ", null, null); + final Trackable tb = GCParser.searchTrackable("TB2J1VZ", null, null); // fix data assertEquals("aefffb86-099f-444f-b132-605436163aa8", tb.getGuid()); assertEquals("TB2J1VZ", tb.getGeocode()); @@ -82,7 +82,7 @@ public class cgeoApplicationTest extends CGeoTestCase { // Following data can change over time assertTrue(tb.getDistance() >= 10617.8f); assertTrue(tb.getLogs().size() >= 10); - assertTrue(cgTrackable.SPOTTED_CACHE == tb.getSpottedType() || cgTrackable.SPOTTED_USER == tb.getSpottedType()); + assertTrue(Trackable.SPOTTED_CACHE == tb.getSpottedType() || Trackable.SPOTTED_USER == tb.getSpottedType()); // no assumption possible: assertEquals("faa2d47d-19ea-422f-bec8-318fc82c8063", tb.getSpottedGuid()); // no assumption possible: assertEquals("Nice place for a break cache", tb.getSpottedName()); @@ -103,13 +103,13 @@ public class cgeoApplicationTest extends CGeoTestCase { * Test {@link cgBase#searchByGeocode(String, String, int, boolean, CancellableHandler)} */ @MediumTest - public static cgCache testSearchByGeocode(final String geocode) { - final SearchResult search = cgCache.searchByGeocode(geocode, null, 0, true, null); + public static Geocache testSearchByGeocode(final String geocode) { + final SearchResult search = Geocache.searchByGeocode(geocode, null, 0, true, null); assertNotNull(search); if (Settings.isPremiumMember() || search.getError() == null) { assertEquals(1, search.getGeocodes().size()); assertTrue(search.getGeocodes().contains(geocode)); - return cgeoapplication.getInstance().loadCache(geocode, LoadFlags.LOAD_CACHE_OR_DB); + return cgData.loadCache(geocode, LoadFlags.LOAD_CACHE_OR_DB); } assertEquals(0, search.getGeocodes().size()); return null; @@ -120,7 +120,7 @@ public class cgeoApplicationTest extends CGeoTestCase { */ @MediumTest public static void testSearchByGeocodeNotExisting() { - final SearchResult search = cgCache.searchByGeocode("GC123456", null, 0, true, null); + final SearchResult search = Geocache.searchByGeocode("GC123456", null, 0, true, null); assertNotNull(search); assertEquals(StatusCode.UNPUBLISHED_CACHE, search.getError()); } @@ -130,8 +130,8 @@ public class cgeoApplicationTest extends CGeoTestCase { */ @MediumTest public static void testSearchByGeocodeNotLoggedIn() { - ImmutablePair<String, String> login = Settings.getLogin(); - String memberStatus = Settings.getMemberStatus(); + final ImmutablePair<String, String> login = Settings.getLogin(); + final String memberStatus = Settings.getMemberStatus(); try { // non premium cache @@ -139,11 +139,11 @@ public class cgeoApplicationTest extends CGeoTestCase { deleteCacheFromDBAndLogout(cache.getGeocode()); - SearchResult search = cgCache.searchByGeocode(cache.getGeocode(), null, StoredList.TEMPORARY_LIST_ID, true, null); + 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())); - cgCache searchedCache = search.getFirstCacheFromResult(LoadFlags.LOAD_CACHE_OR_DB); + final Geocache searchedCache = search.getFirstCacheFromResult(LoadFlags.LOAD_CACHE_OR_DB); // coords must be null if the user is not logged in assertNull(searchedCache.getCoords()); @@ -152,7 +152,7 @@ public class cgeoApplicationTest extends CGeoTestCase { deleteCacheFromDBAndLogout(cache.getGeocode()); - search = cgCache.searchByGeocode(cache.getGeocode(), null, StoredList.TEMPORARY_LIST_ID, true, null); + search = Geocache.searchByGeocode(cache.getGeocode(), null, StoredList.TEMPORARY_LIST_ID, true, null); assertNotNull(search); assertEquals(0, search.getGeocodes().size()); @@ -169,16 +169,16 @@ public class cgeoApplicationTest extends CGeoTestCase { */ @MediumTest public static void testSearchErrorOccured() { - ImmutablePair<String, String> login = Settings.getLogin(); - String memberStatus = Settings.getMemberStatus(); + final ImmutablePair<String, String> login = Settings.getLogin(); + final String memberStatus = Settings.getMemberStatus(); try { // non premium cache - MockedCache cache = new GC1ZXX2(); + final MockedCache cache = new GC1ZXX2(); deleteCacheFromDBAndLogout(cache.getGeocode()); - SearchResult search = cgCache.searchByGeocode(cache.getGeocode(), null, StoredList.TEMPORARY_LIST_ID, true, null); + final SearchResult search = Geocache.searchByGeocode(cache.getGeocode(), null, StoredList.TEMPORARY_LIST_ID, true, null); assertNotNull(search); assertEquals(0, search.getGeocodes().size()); @@ -191,46 +191,60 @@ public class cgeoApplicationTest extends CGeoTestCase { } /** - * Test {@link cgBase#searchByCoords(AbstractSearchThread, Geopoint, String, int, boolean)} + * mock the "exclude disabled caches" and "exclude my caches" options for the execution of the runnable + * + * @param runnable */ - @MediumTest - public static void testSearchByCoords() { + private static void withMockedFilters(Runnable runnable) { // backup user settings final boolean excludeMine = Settings.isExcludeMyCaches(); + final boolean excludeDisabled = Settings.isExcludeDisabledCaches(); try { // set up settings required for test Settings.setExcludeMine(false); + Settings.setExcludeDisabledCaches(false); + + runnable.run(); - final SearchResult search = GCParser.searchByCoords(new Geopoint("N 52° 24.972 E 009° 35.647"), CacheType.MYSTERY, false); - assertNotNull(search); - assertTrue(20 <= search.getGeocodes().size()); - assertTrue(search.getGeocodes().contains("GC1RMM2")); } finally { // restore user settings Settings.setExcludeMine(excludeMine); + Settings.setExcludeDisabledCaches(excludeDisabled); } } /** + * Test {@link cgBase#searchByCoords(AbstractSearchThread, Geopoint, String, int, boolean)} + */ + @MediumTest + public static void testSearchByCoords() { + withMockedFilters(new Runnable() { + + @Override + public void run() { + final SearchResult search = GCParser.searchByCoords(new Geopoint("N 52° 24.972 E 009° 35.647"), CacheType.MYSTERY, false); + assertNotNull(search); + assertTrue(20 <= search.getGeocodes().size()); + assertTrue(search.getGeocodes().contains("GC1RMM2")); + } + }); + } + + /** * Test {@link cgBase#searchByOwner(String, String, int, boolean, CancellableHandler)} */ @MediumTest public static void testSearchByOwner() { - // backup user settings - final boolean excludeMine = Settings.isExcludeMyCaches(); - try { - // set up settings required for test - Settings.setExcludeMine(false); - - final SearchResult search = GCParser.searchByOwner("blafoo", CacheType.MYSTERY, false); - assertNotNull(search); - assertEquals(3, search.getGeocodes().size()); - assertTrue(search.getGeocodes().contains("GC36RT6")); - - } finally { - // restore user settings - Settings.setExcludeMine(excludeMine); - } + withMockedFilters(new Runnable() { + + @Override + public void run() { + final SearchResult search = GCParser.searchByOwner("blafoo", CacheType.MYSTERY, false); + assertNotNull(search); + assertEquals(3, search.getGeocodes().size()); + assertTrue(search.getGeocodes().contains("GC36RT6")); + } + }); } /** @@ -238,21 +252,16 @@ public class cgeoApplicationTest extends CGeoTestCase { */ @MediumTest public static void testSearchByUsername() { - // backup user settings - final boolean excludeMine = Settings.isExcludeMyCaches(); - try { - // set up settings required for test - Settings.setExcludeMine(false); - - final SearchResult search = GCParser.searchByUsername("blafoo", CacheType.WEBCAM, false); - assertNotNull(search); - assertEquals(3, search.getTotal()); - assertTrue(search.getGeocodes().contains("GCP0A9")); - - } finally { - // restore user settings - Settings.setExcludeMine(excludeMine); - } + withMockedFilters(new Runnable() { + + @Override + public void run() { + final SearchResult search = GCParser.searchByUsername("blafoo", CacheType.WEBCAM, false); + assertNotNull(search); + assertEquals(3, search.getTotal()); + assertTrue(search.getGeocodes().contains("GCP0A9")); + } + }); } /** @@ -260,49 +269,54 @@ public class cgeoApplicationTest extends CGeoTestCase { */ @MediumTest public static void testSearchByViewport() { - // backup user settings - final boolean excludeMine = Settings.isExcludeMyCaches(); - final Strategy strategy = Settings.getLiveMapStrategy(); - - try { - // set up settings required for test - Settings.setExcludeMine(false); - - final GC2CJPF mockedCache = new GC2CJPF(); - deleteCacheFromDB(mockedCache.getGeocode()); - - final String[] tokens = Login.getMapTokens(); - final Viewport viewport = new Viewport(mockedCache, 0.003, 0.003); - - // check coords for DETAILED - Settings.setLiveMapStrategy(Strategy.DETAILED); - SearchResult search = ConnectorFactory.searchByViewport(viewport, tokens); - assertNotNull(search); - assertTrue(search.getGeocodes().contains(mockedCache.getGeocode())); - - cgCache parsedCache = cgeoapplication.getInstance().loadCache(mockedCache.getGeocode(), LoadFlags.LOAD_CACHE_OR_DB); - - assertEquals(Settings.isPremiumMember(), mockedCache.getCoords().equals(parsedCache.getCoords())); - assertEquals(Settings.isPremiumMember(), parsedCache.isReliableLatLon()); - - // check update after switch strategy to FAST - Settings.setLiveMapStrategy(Strategy.FAST); - Tile.Cache.removeFromTileCache(mockedCache); - - search = ConnectorFactory.searchByViewport(viewport, tokens); - assertNotNull(search); - assertTrue(search.getGeocodes().contains(mockedCache.getGeocode())); - - parsedCache = cgeoapplication.getInstance().loadCache(mockedCache.getGeocode(), LoadFlags.LOAD_CACHE_OR_DB); - - assertEquals(Settings.isPremiumMember(), mockedCache.getCoords().equals(parsedCache.getCoords())); - assertEquals(Settings.isPremiumMember(), parsedCache.isReliableLatLon()); - - } finally { - // restore user settings - Settings.setExcludeMine(excludeMine); - Settings.setLiveMapStrategy(strategy); - } + withMockedFilters(new Runnable() { + + @Override + public void run() { + // backup user settings + final Strategy strategy = Settings.getLiveMapStrategy(); + final CacheType cacheType = Settings.getCacheType(); + + try { + // set up settings required for test + Settings.setExcludeMine(false); + Settings.setCacheType(CacheType.ALL); + + final GC2CJPF mockedCache = new GC2CJPF(); + deleteCacheFromDB(mockedCache.getGeocode()); + + final String[] tokens = Login.getMapTokens(); + final Viewport viewport = new Viewport(mockedCache, 0.003, 0.003); + + // check coords for DETAILED + Settings.setLiveMapStrategy(Strategy.DETAILED); + 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); + + assertEquals(Settings.isPremiumMember(), mockedCache.getCoords().equals(parsedCache.getCoords())); + assertEquals(Settings.isPremiumMember(), parsedCache.isReliableLatLon()); + + // check update after switch strategy to FAST + Settings.setLiveMapStrategy(Strategy.FAST); + Tile.Cache.removeFromTileCache(mockedCache); + + search = ConnectorFactory.searchByViewport(viewport, tokens); + assertNotNull(search); + assertTrue(search.getGeocodes().contains(mockedCache.getGeocode())); + parsedCache = cgData.loadCache(mockedCache.getGeocode(), LoadFlags.LOAD_CACHE_OR_DB); + + assertEquals(Settings.isPremiumMember(), mockedCache.getCoords().equals(parsedCache.getCoords())); + assertEquals(Settings.isPremiumMember(), parsedCache.isReliableLatLon()); + + } finally { + // restore user settings + Settings.setLiveMapStrategy(strategy); + Settings.setCacheType(cacheType); + } + } + }); } /** @@ -311,11 +325,12 @@ public class cgeoApplicationTest extends CGeoTestCase { @MediumTest public static void testSearchByViewportNotLoggedIn() { - ImmutablePair<String, String> login = Settings.getLogin(); - String memberStatus = Settings.getMemberStatus(); - Strategy strategy = Settings.getLiveMapStrategy(); - Strategy testStrategy = Strategy.FAST; // FASTEST, FAST or DETAILED for tests + final ImmutablePair<String, String> login = Settings.getLogin(); + 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 { @@ -325,6 +340,7 @@ public class cgeoApplicationTest extends CGeoTestCase { MockedCache cache = new GC2CJPF(); deleteCacheFromDBAndLogout(cache.getGeocode()); Tile.Cache.removeFromTileCache(cache); + Settings.setCacheType(CacheType.ALL); Viewport viewport = new Viewport(cache, 0.003, 0.003); SearchResult search = ConnectorFactory.searchByViewport(viewport, tokens); @@ -332,7 +348,7 @@ public class cgeoApplicationTest extends CGeoTestCase { assertNotNull(search); assertTrue(search.getGeocodes().contains(cache.getGeocode())); // coords differ - cgCache cacheFromViewport = cgeoapplication.getInstance().loadCache(cache.getGeocode(), LoadFlags.LOAD_CACHE_OR_DB); + 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)); @@ -356,6 +372,7 @@ public class cgeoApplicationTest extends CGeoTestCase { Settings.setMemberStatus(memberStatus); Login.login(); Settings.setLiveMapStrategy(strategy); + Settings.setCacheType(cacheType); } } @@ -367,7 +384,7 @@ public class cgeoApplicationTest extends CGeoTestCase { String oldUser = mockedCache.getMockedDataUser(); try { mockedCache.setMockedDataUser(Settings.getUsername()); - cgCache parsedCache = cgeoApplicationTest.testSearchByGeocode(mockedCache.getGeocode()); + Geocache parsedCache = cgeoApplicationTest.testSearchByGeocode(mockedCache.getGeocode()); if (null != parsedCache) { Compare.assertCompareCaches(mockedCache, parsedCache, true); } @@ -381,19 +398,14 @@ public class cgeoApplicationTest extends CGeoTestCase { * Caches that are good test cases */ public static void testSearchByGeocodeSpecialties() { - cgCache GCV2R9 = cgeoApplicationTest.testSearchByGeocode("GCV2R9"); + final Geocache GCV2R9 = cgeoApplicationTest.testSearchByGeocode("GCV2R9"); Assert.assertEquals("California, United States", GCV2R9.getLocation()); - cgCache GC1ZXEZ = cgeoApplicationTest.testSearchByGeocode("GC1ZXEZ"); + final Geocache GC1ZXEZ = cgeoApplicationTest.testSearchByGeocode("GC1ZXEZ"); Assert.assertEquals("Ms.Marple/Mr.Stringer", GC1ZXEZ.getOwnerUserId()); } /** Remove cache from DB and cache to ensure that the cache is not loaded from the database */ - private static void deleteCacheFromDB(String geocode) { - cgeoapplication.getInstance().removeCache(geocode, LoadFlags.REMOVE_ALL); - } - - /** Remove cache from DB and cache to ensure that the cache is not loaded from the database */ private static void deleteCacheFromDBAndLogout(String geocode) { deleteCacheFromDB(geocode); |
