diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/res/raw/gcvote.xml | 13 | ||||
| -rw-r--r-- | tests/src/cgeo/geocaching/CgeoApplicationTest.java | 6 | ||||
| -rw-r--r-- | tests/src/cgeo/geocaching/GeocacheTest.java | 2 | ||||
| -rw-r--r-- | tests/src/cgeo/geocaching/export/GpxSerializerTest.java | 2 | ||||
| -rw-r--r-- | tests/src/cgeo/geocaching/gcvote/GCVoteTest.java | 196 | ||||
| -rw-r--r-- | tests/src/cgeo/geocaching/sensors/SensorsTest.java | 3 | ||||
| -rw-r--r-- | tests/src/cgeo/geocaching/test/AbstractResourceInstrumentationTestCase.java | 4 | ||||
| -rw-r--r-- | tests/src/cgeo/geocaching/utils/HtmlUtilsTest.java | 5 | ||||
| -rw-r--r-- | tests/src/cgeo/geocaching/utils/TextUtilsTest.java | 7 | ||||
| -rw-r--r-- | tests/tests.iml | 1 |
10 files changed, 231 insertions, 8 deletions
diff --git a/tests/res/raw/gcvote.xml b/tests/res/raw/gcvote.xml new file mode 100644 index 0000000..c58100a --- /dev/null +++ b/tests/res/raw/gcvote.xml @@ -0,0 +1,13 @@ +<votes userName='' currentVersion='3.2' securityState='locked' loggedIn='false'> +<vote userName='' cacheId='120f6a89-94db-4897-9813-4d79d59ffb09' voteMedian='2.5' voteAvg='2.9' voteCnt='5' voteUser='0' waypoint='GC3MVFN' vote1='0' vote2='3' vote3='1' vote4='1' vote5='0' rawVotes='(2.5:3)(3.0:1)(4.0:1)'/> +<vote userName='' cacheId='3683c154-4a2a-45cd-a20e-ae503ee8975c' voteMedian='2.5' voteAvg='2.75' voteCnt='6' voteUser='0' waypoint='GC3JCQG' vote1='0' vote2='4' vote3='1' vote4='1' vote5='0' rawVotes='(2.0:1)(2.5:3)(3.0:1)(4.0:1)'/> +<vote userName='' cacheId='411fa2e5-7f03-458e-a2e0-f3eda8fd7140' voteMedian='4' voteAvg='4' voteCnt='1' voteUser='0' waypoint='GC2X3X2' vote1='0' vote2='0' vote3='0' vote4='1' vote5='0' rawVotes='(4.0:1)'/> +<vote userName='' cacheId='44f9a8c1-97ac-451a-9269-4b7dc1322db5' voteMedian='3' voteAvg='3.3333333333333' voteCnt='3' voteUser='0' waypoint='GC30X77' vote1='0' vote2='0' vote3='2' vote4='1' vote5='0' rawVotes='(3.0:2)(4.0:1)'/> +<vote userName='' cacheId='5520c33b-3941-45ca-9056-ea655dbaadf7' voteMedian='3.75' voteAvg='3.75' voteCnt='2' voteUser='0' waypoint='GC1WEVZ' vote1='0' vote2='0' vote3='1' vote4='1' vote5='0' rawVotes='(3.5:1)(4.0:1)'/> +<vote userName='' cacheId='5be5cbe3-9094-4a48-b534-dbc52b337315' voteMedian='3.5' voteAvg='3.5' voteCnt='2' voteUser='0' waypoint='GC3JCY7' vote1='0' vote2='0' vote3='1' vote4='1' vote5='0' rawVotes='(3.0:1)(4.0:1)'/> +<vote userName='' cacheId='735334dd-e284-4a7b-bc78-f3534f52a46d' voteMedian='2.5' voteAvg='2.6666666666667' voteCnt='3' voteUser='0' waypoint='GC3QD6N' vote1='0' vote2='2' vote3='1' vote4='0' vote5='0' rawVotes='(2.5:2)(3.0:1)'/> +<vote userName='' cacheId='92a611f9-9ff3-41ac-ae4e-d2927c86e836' voteMedian='3.25' voteAvg='3.5625' voteCnt='8' voteUser='0' waypoint='GCKF13' vote1='0' vote2='0' vote3='5' vote4='2' vote5='1' rawVotes='(3.0:4)(3.5:1)(4.0:2)(5.0:1)'/> +<vote userName='' cacheId='a02894bb-4a08-4c09-a73c-25939894ba15' voteMedian='3' voteAvg='3.1666666666667' voteCnt='9' voteUser='0' waypoint='GC2R3X3' vote1='0' vote2='2' vote3='5' vote4='2' vote5='0' rawVotes='(2.0:1)(2.5:1)(3.0:3)(3.5:2)(4.0:2)'/> +<vote userName='' cacheId='a1711706-09c4-44de-bda8-36df7e53d57c' voteMedian='3.5' voteAvg='3.5' voteCnt='4' voteUser='0' waypoint='GC3MVN6' vote1='0' vote2='0' vote3='2' vote4='2' vote5='0' rawVotes='(3.0:2)(4.0:2)'/> +<errorstring></errorstring> +</votes> diff --git a/tests/src/cgeo/geocaching/CgeoApplicationTest.java b/tests/src/cgeo/geocaching/CgeoApplicationTest.java index de3f4ba..5344742 100644 --- a/tests/src/cgeo/geocaching/CgeoApplicationTest.java +++ b/tests/src/cgeo/geocaching/CgeoApplicationTest.java @@ -165,7 +165,7 @@ public class CgeoApplicationTest extends CGeoTestCase { deleteCacheFromDBAndLogout(cache.getGeocode()); - SearchResult search = Geocache.searchByGeocode(cache.getGeocode(), null, StoredList.TEMPORARY_LIST_ID, true, null); + SearchResult search = Geocache.searchByGeocode(cache.getGeocode(), null, StoredList.TEMPORARY_LIST.id, true, null); assertThat(search).isNotNull(); assertThat(search.getGeocodes()).hasSize(1); assertThat(search.getGeocodes().contains(cache.getGeocode())).isTrue(); @@ -180,7 +180,7 @@ public class CgeoApplicationTest extends CGeoTestCase { deleteCacheFromDBAndLogout(cache.getGeocode()); - search = Geocache.searchByGeocode(cache.getGeocode(), null, StoredList.TEMPORARY_LIST_ID, true, null); + search = Geocache.searchByGeocode(cache.getGeocode(), null, StoredList.TEMPORARY_LIST.id, true, null); assertThat(search).isNotNull(); assertThat(search.getGeocodes()).isEmpty(); } @@ -201,7 +201,7 @@ public class CgeoApplicationTest extends CGeoTestCase { deleteCacheFromDBAndLogout(cache.getGeocode()); - final SearchResult search = Geocache.searchByGeocode(cache.getGeocode(), null, StoredList.TEMPORARY_LIST_ID, true, null); + final SearchResult search = Geocache.searchByGeocode(cache.getGeocode(), null, StoredList.TEMPORARY_LIST.id, true, null); assertThat(search).isNotNull(); assertThat(search.getGeocodes()).isEmpty(); } diff --git a/tests/src/cgeo/geocaching/GeocacheTest.java b/tests/src/cgeo/geocaching/GeocacheTest.java index b3ec6be..722e1cf 100644 --- a/tests/src/cgeo/geocaching/GeocacheTest.java +++ b/tests/src/cgeo/geocaching/GeocacheTest.java @@ -86,7 +86,7 @@ public class GeocacheTest extends CGeoTestCase { assertThat(waypoint.getCoords()).isEqualTo(new Geopoint("N51 13.888 E007 03.444")); // assertThat(waypoint.getNote()).isEqualTo("Test"); assertThat(waypoint.getName()).isEqualTo(CgeoApplication.getInstance().getString(R.string.cache_personal_note) + " 1"); - cache.store(StoredList.TEMPORARY_LIST_ID, null); + cache.store(StoredList.TEMPORARY_LIST.id, null); } removeCacheCompletely(geocode); } finally { diff --git a/tests/src/cgeo/geocaching/export/GpxSerializerTest.java b/tests/src/cgeo/geocaching/export/GpxSerializerTest.java index 809c121..c43ad38 100644 --- a/tests/src/cgeo/geocaching/export/GpxSerializerTest.java +++ b/tests/src/cgeo/geocaching/export/GpxSerializerTest.java @@ -67,7 +67,7 @@ public class GpxSerializerTest extends AbstractResourceInstrumentationTestCase { assertThat(gpxFirst.length() > 0).isTrue(); - final GPX10Parser parser = new GPX10Parser(StoredList.TEMPORARY_LIST_ID); + final GPX10Parser parser = new GPX10Parser(StoredList.TEMPORARY_LIST.id); final InputStream stream = new ByteArrayInputStream(gpxFirst.getBytes(CharEncoding.UTF_8)); Collection<Geocache> caches = parser.parse(stream, null); diff --git a/tests/src/cgeo/geocaching/gcvote/GCVoteTest.java b/tests/src/cgeo/geocaching/gcvote/GCVoteTest.java new file mode 100644 index 0000000..243fc99 --- /dev/null +++ b/tests/src/cgeo/geocaching/gcvote/GCVoteTest.java @@ -0,0 +1,196 @@ +package cgeo.geocaching.gcvote; + +import static org.assertj.core.api.Assertions.assertThat; + +import cgeo.geocaching.test.AbstractResourceInstrumentationTestCase; +import cgeo.geocaching.test.R; +import cgeo.geocaching.utils.LeastRecentlyUsedMap; +import cgeo.geocaching.utils.Log; +import cgeo.geocaching.utils.MatcherWrapper; + +import org.apache.commons.lang3.StringUtils; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.regex.Pattern; + +public class GCVoteTest extends AbstractResourceInstrumentationTestCase { + + private String response; + private InputStream responseStream; + + @Override + protected void setUp() throws Exception { + super.setUp(); + response = getFileContent(R.raw.gcvote); + responseStream = new ByteArrayInputStream(response.getBytes()); + responseStream.mark(response.getBytes().length + 1); + } + + private InputStream responseStream() { + try { + responseStream.reset(); + } catch (final IOException ignored) { + // Cannot happen + } + return responseStream; + } + + public void testGetRatingsByGeocode() { + final Map<String, GCVoteRating> ratings = GCVote.getRatingsFromXMLResponse(responseStream(), false); + assertThat(ratings).hasSize(10); + assertThat(ratings).containsKey("GCKF13"); + assertThat(ratings.get("GC1WEVZ")).isEqualToComparingFieldByField(new GCVoteRating(3.75f, 2, 0)); + } + + public void testGetRatingsByGuid() { + final Map<String, GCVoteRating> ratings = GCVote.getRatingsFromXMLResponse(responseStream(), true); + assertThat(ratings).hasSize(10); + assertThat(ratings).containsKey("a02894bb-4a08-4c09-a73c-25939894ba15"); + assertThat(ratings.get("5520c33b-3941-45ca-9056-ea655dbaadf7")).isEqualToComparingFieldByField(new GCVoteRating(3.75f, 2, 0)); + } + + public void testBenchmark() { + benchmarkXML(1000); + benchmarkRegex(1000); + } + + public void testCompareResults() { + for (int i = 0; i < 2; i++) { + final boolean requestByGuids = i == 0; + final Map<String, GCVoteRating> xmlRatings = GCVote.getRatingsFromXMLResponse(responseStream(), requestByGuids); + final Map<String, GCVoteRating> regexRatings = getRatingsRegex(response, requestByGuids); + assertThat(xmlRatings.keySet()).containsExactlyElementsOf(regexRatings.keySet()); + for (final Entry<String, GCVoteRating> entry: xmlRatings.entrySet()) { + assertThat(entry.getValue()).isEqualToComparingFieldByField(regexRatings.get(entry.getKey())); + } + } + } + + private void benchmarkXML(final int occurrences) { + final long start = System.currentTimeMillis(); + for (int i = 0; i < occurrences; i++) { + GCVote.getRatingsFromXMLResponse(responseStream(), false); + } + Log.d("XML GCVote parsing (current) in ms (" + occurrences + " times): " + (System.currentTimeMillis() - start)); + } + + private void benchmarkRegex(final int occurrences) { + final long start = System.currentTimeMillis(); + for (int i = 0; i < occurrences; i++) { + getRatingsRegex(response, false); + } + Log.d("Regex GCVote parsing (old) in ms (" + occurrences + " times): " + (System.currentTimeMillis() - start)); + } + + public static final float NO_RATING = 0; + private static final Pattern PATTERN_LOG_IN = Pattern.compile("loggedIn='([^']+)'", Pattern.CASE_INSENSITIVE); + private static final Pattern PATTERN_GUID = Pattern.compile("cacheId='([^']+)'", Pattern.CASE_INSENSITIVE); + private static final Pattern PATTERN_WAYPOINT = Pattern.compile("waypoint='([^']+)'", Pattern.CASE_INSENSITIVE); + private static final Pattern PATTERN_RATING = Pattern.compile("voteAvg='([0-9.]+)'", Pattern.CASE_INSENSITIVE); + private static final Pattern PATTERN_VOTES = Pattern.compile("voteCnt='([0-9]+)'", Pattern.CASE_INSENSITIVE); + private static final Pattern PATTERN_VOTE = Pattern.compile("voteUser='([0-9.]+)'", Pattern.CASE_INSENSITIVE); + private static final Pattern PATTERN_VOTE_ELEMENT = Pattern.compile("<vote ([^>]+)>", Pattern.CASE_INSENSITIVE); + + private static final int MAX_CACHED_RATINGS = 1000; + private static final LeastRecentlyUsedMap<String, GCVoteRating> RATINGS_CACHE = new LeastRecentlyUsedMap.LruCache<>(MAX_CACHED_RATINGS); + + private static Map<String, GCVoteRating> getRatingsRegex(final String page, final boolean requestByGuids) { + final Map<String, GCVoteRating> ratings = new HashMap<>(); + + try { + final MatcherWrapper matcherVoteElement = new MatcherWrapper(PATTERN_VOTE_ELEMENT, page); + while (matcherVoteElement.find()) { + String voteData = matcherVoteElement.group(1); + if (voteData == null) { + continue; + } + + String id = null; + String guid = null; + final MatcherWrapper matcherGuid = new MatcherWrapper(PATTERN_GUID, voteData); + if (matcherGuid.find()) { + if (matcherGuid.groupCount() > 0) { + guid = matcherGuid.group(1); + if (requestByGuids) { + id = guid; + } + } + } + if (!requestByGuids) { + final MatcherWrapper matcherWp = new MatcherWrapper(PATTERN_WAYPOINT, voteData); + if (matcherWp.find()) { + if (matcherWp.groupCount() > 0) { + id = matcherWp.group(1); + } + } + } + if (id == null) { + continue; + } + + boolean loggedIn = false; + final MatcherWrapper matcherLoggedIn = new MatcherWrapper(PATTERN_LOG_IN, page); + if (matcherLoggedIn.find()) { + if (matcherLoggedIn.groupCount() > 0) { + if (matcherLoggedIn.group(1).equalsIgnoreCase("true")) { + loggedIn = true; + } + } + } + + float rating = NO_RATING; + try { + final MatcherWrapper matcherRating = new MatcherWrapper(PATTERN_RATING, voteData); + if (matcherRating.find()) { + rating = Float.parseFloat(matcherRating.group(1)); + } + } catch (NumberFormatException e) { + Log.w("GCVote.getRating: Failed to parse rating", e); + } + if (!GCVote.isValidRating(rating)) { + continue; + } + + int votes = -1; + try { + final MatcherWrapper matcherVotes = new MatcherWrapper(PATTERN_VOTES, voteData); + if (matcherVotes.find()) { + votes = Integer.parseInt(matcherVotes.group(1)); + } + } catch (NumberFormatException e) { + Log.w("GCVote.getRating: Failed to parse vote count", e); + } + if (votes < 0) { + continue; + } + + float myVote = NO_RATING; + if (loggedIn) { + try { + final MatcherWrapper matcherVote = new MatcherWrapper(PATTERN_VOTE, voteData); + if (matcherVote.find()) { + myVote = Float.parseFloat(matcherVote.group(1)); + } + } catch (NumberFormatException e) { + Log.w("GCVote.getRating: Failed to parse user's vote", e); + } + } + + if (StringUtils.isNotBlank(id)) { + GCVoteRating gcvoteRating = new GCVoteRating(rating, votes, myVote); + ratings.put(id, gcvoteRating); + RATINGS_CACHE.put(guid, gcvoteRating); + } + } + } catch (RuntimeException e) { + Log.e("GCVote.getRating", e); + } + + return ratings; + } +} diff --git a/tests/src/cgeo/geocaching/sensors/SensorsTest.java b/tests/src/cgeo/geocaching/sensors/SensorsTest.java index c4f70aa..b54cd13 100644 --- a/tests/src/cgeo/geocaching/sensors/SensorsTest.java +++ b/tests/src/cgeo/geocaching/sensors/SensorsTest.java @@ -3,6 +3,7 @@ package cgeo.geocaching.sensors; import static org.assertj.core.api.Assertions.assertThat; import cgeo.geocaching.MainActivity; +import cgeo.geocaching.utils.AngleUtils; import android.test.ActivityInstrumentationTestCase2; @@ -18,7 +19,7 @@ public class SensorsTest extends ActivityInstrumentationTestCase2<MainActivity> } public static void testGetDirectionNow() { - final float angle = DirectionProvider.getDirectionNow(1.0f); + final float angle = AngleUtils.getDirectionNow(1.0f); assertThat(angle == 1.0f || angle == 91.0f || angle == 181.0f || angle == 271.0f).isTrue(); } diff --git a/tests/src/cgeo/geocaching/test/AbstractResourceInstrumentationTestCase.java b/tests/src/cgeo/geocaching/test/AbstractResourceInstrumentationTestCase.java index eaac181..2a22895 100644 --- a/tests/src/cgeo/geocaching/test/AbstractResourceInstrumentationTestCase.java +++ b/tests/src/cgeo/geocaching/test/AbstractResourceInstrumentationTestCase.java @@ -75,7 +75,7 @@ public abstract class AbstractResourceInstrumentationTestCase extends Instrument protected void setUp() throws Exception { super.setUp(); temporaryListId = DataStore.createList("Temporary unit testing"); - assertThat(temporaryListId != StoredList.TEMPORARY_LIST_ID).isTrue(); + assertThat(temporaryListId != StoredList.TEMPORARY_LIST.id).isTrue(); assertThat(temporaryListId != StoredList.STANDARD_LIST_ID).isTrue(); } @@ -95,7 +95,7 @@ public abstract class AbstractResourceInstrumentationTestCase extends Instrument final protected Geocache loadCacheFromResource(int resourceId) throws IOException, ParserException { final InputStream instream = getResourceStream(resourceId); try { - GPX10Parser parser = new GPX10Parser(StoredList.TEMPORARY_LIST_ID); + GPX10Parser parser = new GPX10Parser(StoredList.TEMPORARY_LIST.id); Collection<Geocache> caches = parser.parse(instream, null); assertThat(caches).isNotNull(); assertThat(caches.isEmpty()).isFalse(); diff --git a/tests/src/cgeo/geocaching/utils/HtmlUtilsTest.java b/tests/src/cgeo/geocaching/utils/HtmlUtilsTest.java index 65e86b8..e75c2e6 100644 --- a/tests/src/cgeo/geocaching/utils/HtmlUtilsTest.java +++ b/tests/src/cgeo/geocaching/utils/HtmlUtilsTest.java @@ -15,4 +15,9 @@ public class HtmlUtilsTest extends TestCase { assertThat(HtmlUtils.extractText("<b>bold</b>")).isEqualTo("bold"); } + public static void testRemoveExtraParagraph() { + assertThat(HtmlUtils.removeExtraParagraph("<p></p>")).isEqualTo(""); + assertThat(HtmlUtils.removeExtraParagraph("<p>Test</p>")).isEqualTo("Test"); + assertThat(HtmlUtils.removeExtraParagraph("<p>1</p><p>2</p>")).isEqualTo("<p>1</p><p>2</p>"); + } } diff --git a/tests/src/cgeo/geocaching/utils/TextUtilsTest.java b/tests/src/cgeo/geocaching/utils/TextUtilsTest.java index 29c4864..709fcc4 100644 --- a/tests/src/cgeo/geocaching/utils/TextUtilsTest.java +++ b/tests/src/cgeo/geocaching/utils/TextUtilsTest.java @@ -26,4 +26,11 @@ public class TextUtilsTest extends AndroidTestCase { assertThat(TextUtils.getMatch("some" + "\u001C" + "control" + (char) 0x1D + "characters removed", patternAll, "")).isEqualTo("some control characters removed"); assertThat(TextUtils.getMatch("newline\nalso\nremoved", patternAll, "")).isEqualTo("newline also removed"); } + + public static void testGetMatch() { + final Pattern patternAll = Pattern.compile("foo(...)"); + final String text = "abc-foobar-def-fooxyz-ghi-foobaz-jkl"; + assertThat(TextUtils.getMatch(text, patternAll, false, 1, null, false)).isEqualTo("bar"); + assertThat(TextUtils.getMatch(text, patternAll, false, 1, null, true)).isEqualTo("baz"); + } } diff --git a/tests/tests.iml b/tests/tests.iml index 6a6c905..ac5381c 100644 --- a/tests/tests.iml +++ b/tests/tests.iml @@ -4,6 +4,7 @@ <facet type="android" name="Android"> <configuration> <option name="PACK_TEST_CODE" value="true" /> + <option name="UPDATE_PROPERTY_FILES" value="false" /> <includeAssetsFromLibraries>true</includeAssetsFromLibraries> </configuration> </facet> |
