aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src/cgeo/geocaching/StaticMapsProviderTest.java
blob: cbace10b40903df2b41e77a3c1065eaf00046559 (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
package cgeo.geocaching;

import cgeo.geocaching.enumerations.WaypointType;
import cgeo.geocaching.files.LocalStorage;
import cgeo.geocaching.geopoint.Geopoint;

import java.io.File;

import junit.framework.TestCase;

public class StaticMapsProviderTest extends TestCase {

    public static void testDownloadStaticMaps() {
        final double lat = 52.354176d;
        final double lon = 9.745685d;
        String geocode = "GCTEST1";

        boolean backupStore = Settings.isStoreOfflineMaps();
        boolean backupStoreWP = Settings.isStoreOfflineWpMaps();
        Settings.setStoreOfflineMaps(true);
        Settings.setStoreOfflineWpMaps(true);
        try {
            Geopoint gp = new Geopoint(lat + 0.25d, lon + 0.25d);
            Geocache cache = new Geocache();
            cache.setGeocode(geocode);
            cache.setCoords(gp);
            cache.setCacheId(String.valueOf(1));

            Waypoint theFinal = new Waypoint("Final", WaypointType.FINAL, false);
            Geopoint finalGp = new Geopoint(lat + 0.25d + 1, lon + 0.25d + 1);
            theFinal.setCoords(finalGp);
            theFinal.setId(1);
            cache.addOrChangeWaypoint(theFinal, false);

            Waypoint trailhead = new Waypoint("Trail head", WaypointType.TRAILHEAD, false);
            Geopoint trailheadGp = new Geopoint(lat + 0.25d + 2, lon + 0.25d + 2);
            trailhead.setCoords(trailheadGp);
            trailhead.setId(2);
            cache.addOrChangeWaypoint(trailhead, false);

            // make sure we don't have stale downloads
            deleteCacheDirectory(geocode);
            assertFalse(StaticMapsProvider.hasStaticMap(cache));
            assertFalse(StaticMapsProvider.hasStaticMapForWaypoint(geocode, theFinal));
            assertFalse(StaticMapsProvider.hasStaticMapForWaypoint(geocode, trailhead));

            // download
            StaticMapsProvider.downloadMaps(cache);

            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                fail();
            }

            // check download
            assertTrue(StaticMapsProvider.hasStaticMap(cache));
            assertTrue(StaticMapsProvider.hasStaticMapForWaypoint(geocode, theFinal));
            assertTrue(StaticMapsProvider.hasStaticMapForWaypoint(geocode, trailhead));

            // waypoint static maps hashcode dependent
            trailhead.setCoords(new Geopoint(lat + 0.24d + 2, lon + 0.25d + 2));
            assertFalse(StaticMapsProvider.hasStaticMapForWaypoint(geocode, trailhead));
        } finally {
            Settings.setStoreOfflineWpMaps(backupStoreWP);
            Settings.setStoreOfflineMaps(backupStore);
            deleteCacheDirectory(geocode);
        }
    }

    private static void deleteCacheDirectory(String geocode) {
        File cacheDir = LocalStorage.getStorageDir(geocode);
        LocalStorage.deleteDirectory(cacheDir);
    }

}