aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src/cgeo/geocaching/cgeoApplicationTest.java
blob: 4a1fcb00543cf3ada7414a306dfc00f1c996ef20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package cgeo.geocaching;

import cgeo.geocaching.connector.ConnectorFactory;
import cgeo.geocaching.connector.gc.GCBase;
import cgeo.geocaching.connector.gc.GCParser;
import cgeo.geocaching.connector.gc.Login;
import cgeo.geocaching.enumerations.CacheType;
import cgeo.geocaching.enumerations.LiveMapStrategy.Strategy;
import cgeo.geocaching.enumerations.LoadFlags;
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 cgeo.geocaching.utils.Log;
import cgeo.test.Compare;

import org.apache.commons.lang3.tuple.ImmutablePair;

import android.test.ApplicationTestCase;
import android.test.suitebuilder.annotation.MediumTest;
import android.test.suitebuilder.annotation.SmallTest;

import java.util.Date;

import junit.framework.Assert;

/**
 * The c:geo application test. It can be used for tests that require an
 * application and/or context.
 */

public class cgeoApplicationTest extends ApplicationTestCase<cgeoapplication> {

    public cgeoApplicationTest() {
        super(cgeoapplication.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        // init environment
        createApplication();
    }

    /**
     * The name 'test preconditions' is a convention to signal that if this test
     * doesn't pass, the test case was not set up properly and it might explain
     * 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() {
        assertEquals(StatusCode.NO_ERROR, Login.login());
    }

    /**
     * Test {@link cgBase#searchTrackable(String, String, String)}
     */
    @MediumTest
    public static void testSearchTrackableNotExisting() {
        cgTrackable tb = GCParser.searchTrackable("123456", null, null);
        assertNotNull(tb);
    }

    /**
     * Test {@link cgBase#searchTrackable(String, String, String)}
     */
    @MediumTest
    public static void testSearchTrackable() {
        cgTrackable tb = GCParser.searchTrackable("TB2J1VZ", null, null);
        // fix data
        assertEquals("aefffb86-099f-444f-b132-605436163aa8", tb.getGuid());
        assertEquals("TB2J1VZ", tb.getGeocode());
        assertEquals("http://www.geocaching.com/images/wpttypes/21.gif", tb.getIconUrl());
        assertEquals("blafoo's Children Music CD", tb.getName());
        assertEquals("Travel Bug Dog Tag", tb.getType());
        assertEquals(new Date(2009 - 1900, 8 - 1, 24), tb.getReleased());
        assertEquals("Niedersachsen, Germany", tb.getOrigin());
        assertEquals("blafoo", tb.getOwner());
        assertEquals("0564a940-8311-40ee-8e76-7e91b2cf6284", tb.getOwnerGuid());
        assertEquals("Kinder erfreuen.<br /><br />Make children happy.", tb.getGoal());
        assertTrue(tb.getDetails().startsWith("Auf der CD sind"));
        assertEquals("http://img.geocaching.com/track/display/38382780-87a7-4393-8393-78841678ee8c.jpg", tb.getImage());
        // 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());
        // no assumption possible: assertEquals("faa2d47d-19ea-422f-bec8-318fc82c8063", tb.getSpottedGuid());
        // no assumption possible: assertEquals("Nice place for a break cache", tb.getSpottedName());
    }

    /**
     * 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);
        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);
        }
        assertEquals(0, search.getGeocodes().size());
        return null;
    }

    /**
     * Test {@link cgBase#searchByGeocode(String, String, int, boolean, CancellableHandler)}
     */
    @MediumTest
    public static void testSearchByGeocodeNotExisting() {
        final SearchResult search = cgCache.searchByGeocode("GC123456", null, 0, true, null);
        assertNotNull(search);
        assertEquals(StatusCode.UNPUBLISHED_CACHE, search.getError());
    }

    /**
     * Test {@link cgBase#searchByGeocode(String, String, int, boolean, CancellableHandler)}
     */
    @MediumTest
    public static void testSearchByGeocodeNotLoggedIn() {
        ImmutablePair<String, String> login = Settings.getLogin();
        String memberStatus = Settings.getMemberStatus();

        try {
            // non premium cache
            MockedCache cache = new GC2CJPF();

            deleteCacheFromDBAndLogout(cache.getGeocode());

            SearchResult search = cgCache.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);
            // 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 = cgCache.searchByGeocode(cache.getGeocode(), null, StoredList.TEMPORARY_LIST_ID, true, null);
            assertNotNull(search);
            assertEquals(0, search.getGeocodes().size());

        } finally {
            // restore user and password
            Settings.setLogin(login.left, login.right);
            Settings.setMemberStatus(memberStatus);
            Login.login();
        }
    }

    /**
     * Test {@link cgBase#searchByGeocode(String, String, int, boolean, CancellableHandler)}
     */
    @MediumTest
    public static void testSearchErrorOccured() {
        ImmutablePair<String, String> login = Settings.getLogin();
        String memberStatus = Settings.getMemberStatus();

        try {
            // non premium cache
            MockedCache cache = new GC1ZXX2();

            deleteCacheFromDBAndLogout(cache.getGeocode());

            SearchResult search = cgCache.searchByGeocode(cache.getGeocode(), null, StoredList.TEMPORARY_LIST_ID, true, null);
            assertNotNull(search);
            assertEquals(0, search.getGeocodes().size());

        } finally {
            // restore user and password
            Settings.setLogin(login.left, login.right);
            Settings.setMemberStatus(memberStatus);
            Login.login();
        }
    }

    /**
     * Test {@link cgBase#searchByCoords(cgSearchThread, Geopoint, String, int, boolean)}
     */
    @MediumTest
    public static void testSearchByCoords() {
        final SearchResult search = GCParser.searchByCoords(null, new Geopoint("N 52° 24.972 E 009° 35.647"), CacheType.MYSTERY, false);
        assertNotNull(search);
        assertTrue(18 <= search.getGeocodes().size());
        assertTrue(search.getGeocodes().contains("GC1RMM2"));
    }

    /**
     * Test {@link cgBase#searchByOwner(String, String, int, boolean, CancellableHandler)}
     */
    @MediumTest
    public static void testSearchByOwner() {
        final SearchResult search = GCParser.searchByOwner(null, "blafoo", CacheType.MYSTERY, false);
        assertNotNull(search);
        assertEquals(3, search.getGeocodes().size());
        assertTrue(search.getGeocodes().contains("GC36RT6"));
    }

    /**
     * Test {@link cgBase#searchByUsername(String, String, int, boolean, CancellableHandler)}
     */
    @MediumTest
    public static void testSearchByUsername() {
        final SearchResult search = GCParser.searchByUsername(null, "blafoo", CacheType.WEBCAM, false);
        assertNotNull(search);
        assertEquals(3, search.getTotal());
        assertTrue(search.getGeocodes().contains("GCP0A9"));
    }

    /**
     * Test {@link cgBase#searchByViewport(String, Viewport)}
     */
    @MediumTest
    public static void testSearchByViewport() {

        Strategy strategy = Settings.getLiveMapStrategy();

        try {
            GC2CJPF mockedCache = new GC2CJPF();
            deleteCacheFromDB(mockedCache.getGeocode());

            final String[] tokens = GCBase.getTokens();
            final Viewport viewport = new Viewport(mockedCache.getCoords(), 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);
            GCBase.removeFromTileCache(mockedCache.getCoords());

            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 {
            Settings.setLiveMapStrategy(strategy);
        }
    }

    /**
     * Test {@link cgBase#searchByViewport(String, Viewport)}
     */
    @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
        Settings.setLiveMapStrategy(testStrategy);

        try {

            final String[] tokens = null; // without a valid token we are "logged off"

            // non premium cache
            MockedCache cache = new GC2CJPF();
            deleteCacheFromDBAndLogout(cache.getGeocode());
            GCBase.removeFromTileCache(cache.getCoords());

            Viewport viewport = new Viewport(cache.getCoords(), 0.003, 0.003);
            SearchResult search = ConnectorFactory.searchByViewport(viewport, tokens);

            assertNotNull(search);
            assertTrue(search.getGeocodes().contains(cache.getGeocode()));
            // coords differ
            cgCache cacheFromViewport = cgeoapplication.getInstance().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.getCoords(), 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()));

        } finally {
            // restore user and password
            Settings.setLogin(login.left, login.right);
            Settings.setMemberStatus(memberStatus);
            Login.login();
            Settings.setLiveMapStrategy(strategy);
        }
    }

    /**
     * Test cache parsing. Esp. useful after a GC.com update
     */
    public static void testSearchByGeocodeBasis() {
        for (MockedCache mockedCache : RegExPerformanceTest.MOCKED_CACHES) {
            mockedCache.setMockedDataUser(Settings.getUsername());
            cgCache parsedCache = cgeoApplicationTest.testSearchByGeocode(mockedCache.getGeocode());
            if (null != parsedCache) {
                Compare.assertCompareCaches(mockedCache, parsedCache, true);
            }
        }
    }

    /**
     * Caches that are good test cases
     */
    public static void testSearchByGeocodeSpecialties() {
        cgCache GCV2R9 = cgeoApplicationTest.testSearchByGeocode("GCV2R9");
        Assert.assertEquals("California, United States", GCV2R9.getLocation());

        cgCache GC1ZXEZ = cgeoApplicationTest.testSearchByGeocode("GC1ZXEZ");
        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 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);

        Login.logout();
        // Modify login data to avoid an automatic login again
        Settings.setLogin("c:geo", "c:geo");
        Settings.setMemberStatus("Basic member");
    }

}