package cgeo.geocaching.connector.gc;
import cgeo.geocaching.test.mock.MockedCache;
import cgeo.geocaching.utils.TextUtils;
import android.test.AndroidTestCase;
import android.text.Html;
public class GCConstantsTest extends AndroidTestCase {
// adapt the following after downloading new mock html files
public static final String MOCK_LOGIN_NAME = "JoSaMaJa";
public static final int MOCK_CACHES_FOUND = 484;
public static void testLocation() {
// GC37GFJ
assertEquals("Bretagne, France", parseLocation(" In Bretagne, France
"));
// GCV2R9
assertEquals("California, United States", parseLocation("In California, United States
"));
}
private static String parseLocation(final String html) {
return TextUtils.getMatch(html, GCConstants.PATTERN_LOCATION, true, "");
}
public static void testCacheCount() {
assertCacheCount(1510, " 1.510 Caches Found");
assertCacheCount(1510, " 1,510 Caches Found");
assertCacheCount(MOCK_CACHES_FOUND, MockedCache.readCachePage("GC2CJPF"));
}
private static void assertCacheCount(final int count, final String html) {
try {
assertEquals(count, Integer.parseInt(TextUtils.getMatch(html, GCConstants.PATTERN_CACHES_FOUND, true, "0").replaceAll("[,.]", "")));
} catch (NumberFormatException e) {
fail();
}
}
/**
* Test that we can parse the cache find count of the user.
*
* This test requires a real user name and password to be stored on the device or emulator. *
*/ public static void testCacheCountOnline() { Login.logout(); Login.setActualCachesFound(0); Login.login(); assertTrue(Login.getActualCachesFound() > 0); } public static void testConstants() { final String session = "userSession = new Groundspeak.Map.UserSession('aKWZ', userOptions:'XPTf', sessionToken:'123pNKwdktYGZL0xd-I7yqA6nm_JE1BDUtM4KcOkifin2TRCMutBd_PZE14Ohpffs2ZgkTnxTSnxYpBigK4hBA2', subscriberType: 3, enablePersonalization: true });"; assertEquals("aKWZ", TextUtils.getMatch(session, GCConstants.PATTERN_USERSESSION, "")); assertTrue(TextUtils.getMatch(session, GCConstants.PATTERN_SESSIONTOKEN, "").startsWith("123pNK")); } public static void testTBWithSpecialChar() { final String page = "\n"; assertEquals("Schlauchen&ravestorm", Html.fromHtml(TextUtils.getMatch(page, GCConstants.PATTERN_TRACKABLE_NAME, "")).toString()); } }