aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src/cgeo/CGeoTestCase.java
blob: 5759bc74625b807c5cf7e75c337d72d5c5c2cee6 (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
package cgeo;

import cgeo.geocaching.CgeoApplication;
import cgeo.geocaching.DataStore;
import cgeo.geocaching.enumerations.LoadFlags;
import cgeo.geocaching.enumerations.LoadFlags.RemoveFlag;
import cgeo.geocaching.settings.Settings;
import cgeo.geocaching.settings.TestSettings;

import android.test.ApplicationTestCase;

import java.util.EnumSet;

public abstract class CGeoTestCase extends ApplicationTestCase<CgeoApplication> {

    private boolean oldStoreMapsFlag;
    private boolean oldStoreWpMapsFlag;
    private boolean oldMapStoreFlagsRecorded = false;

    public CGeoTestCase() {
        super(CgeoApplication.class);
    }

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

    /** Remove cache from DB and cache to ensure that the cache is not loaded from the database */
    protected static void deleteCacheFromDB(String geocode) {
        DataStore.removeCache(geocode, LoadFlags.REMOVE_ALL);
    }

    /**
     * remove cache from database and file system
     *
     * @param geocode
     */
    protected static void removeCacheCompletely(final String geocode) {
        final EnumSet<RemoveFlag> flags = EnumSet.copyOf(LoadFlags.REMOVE_ALL);
        flags.add(RemoveFlag.OWN_WAYPOINTS_ONLY_FOR_TESTING);
        DataStore.removeCache(geocode, flags);
    }

    /**
     * must be called once before setting the flags
     * can be called again after restoring the flags
     */
    protected void recordMapStoreFlags() {
        if (oldMapStoreFlagsRecorded) {
            throw new IllegalStateException("MapStoreFlags already recorded!");
        }
        oldStoreMapsFlag = Settings.isStoreOfflineMaps();
        oldStoreWpMapsFlag = Settings.isStoreOfflineWpMaps();
        oldMapStoreFlagsRecorded = true;
    }

    /**
     * can be called after recordMapStoreFlags,
     * to set the flags for map storing as necessary
     * @param storeCacheMap
     * @param storeWpMaps
     */
    protected void setMapStoreFlags(boolean storeCacheMap, boolean storeWpMaps) {
        if (!oldMapStoreFlagsRecorded) {
            throw new IllegalStateException("Previous MapStoreFlags havn't been recorded! Setting not allowed");
        }

        TestSettings.setStoreOfflineMaps(storeCacheMap);
        TestSettings.setStoreOfflineWpMaps(storeWpMaps);
    }

    /**
     * has to be called after completion of the test (preferably in the finally part of a try statement)
     */
    protected void restoreMapStoreFlags() {
        if (!oldMapStoreFlagsRecorded) {
            throw new IllegalStateException("Previous MapStoreFlags havn't been recorded. Restore not possible");
        }

        TestSettings.setStoreOfflineMaps(oldStoreMapsFlag);
        TestSettings.setStoreOfflineWpMaps(oldStoreWpMapsFlag);
        oldMapStoreFlagsRecorded = false;
    }
}