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

import cgeo.geocaching.SearchResult;
import cgeo.geocaching.Settings;
import cgeo.geocaching.cgCache;
import cgeo.geocaching.cgeoapplication;
import cgeo.geocaching.test.AbstractResourceInstrumentationTestCase;
import cgeo.geocaching.test.R;
import cgeo.geocaching.utils.CancellableHandler;

import android.net.Uri;
import android.os.Message;
import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class GPXImporterTest extends AbstractResourceInstrumentationTestCase {
    private TestHandler importStepHandler = new TestHandler();
    private TestHandler progressHandler = new TestHandler();
    private int listId;
    private File tempDir;

    public static void testGetWaypointsFileNameForGpxFileName() {
        assertEquals("1234567-wpts.gpx", GPXImporter.getWaypointsFileNameForGpxFileName("1234567.gpx"));
        assertEquals("/mnt/sdcard/1234567-wpts.gpx", GPXImporter.getWaypointsFileNameForGpxFileName("/mnt/sdcard/1234567.gpx"));
        assertEquals("/mnt/sdcard/1-wpts.gpx", GPXImporter.getWaypointsFileNameForGpxFileName("/mnt/sdcard/1.gpx"));
        assertEquals("/mnt/sd.card/1-wpts.gpx", GPXImporter.getWaypointsFileNameForGpxFileName("/mnt/sd.card/1.gpx"));
        assertEquals("1234567.9-wpts.gpx", GPXImporter.getWaypointsFileNameForGpxFileName("1234567.9.gpx"));
        assertEquals("1234567-wpts.gpx", GPXImporter.getWaypointsFileNameForGpxFileName("1234567.GPX"));
        assertEquals("gpx.gpx-wpts.gpx", GPXImporter.getWaypointsFileNameForGpxFileName("gpx.gpx.gpx"));
        assertEquals("/mnt/sdcard/-wpts.gpx", GPXImporter.getWaypointsFileNameForGpxFileName("/mnt/sdcard/.gpx"));
        assertEquals("1234567_query-wpts.gpx", GPXImporter.getWaypointsFileNameForGpxFileName("1234567_query.gpx"));
        assertNull(GPXImporter.getWaypointsFileNameForGpxFileName("123.gpy"));
        assertNull(GPXImporter.getWaypointsFileNameForGpxFileName("gpx"));
        assertNull(GPXImporter.getWaypointsFileNameForGpxFileName(".gpx"));
    }

    public void testImportGpx() throws IOException {
        File gc31j2h = new File(tempDir, "gc31j2h.gpx");
        copyResourceToFile(R.raw.gc31j2h, gc31j2h);

        GPXImporter.ImportGpxFileThread importThread = new GPXImporter.ImportGpxFileThread(gc31j2h, listId, importStepHandler, progressHandler);
        runImportThread(importThread);

        assertEquals(4, importStepHandler.messages.size());
        Iterator<Message> iMsg = importStepHandler.messages.iterator();
        assertEquals(GPXImporter.IMPORT_STEP_START, iMsg.next().what);
        assertEquals(GPXImporter.IMPORT_STEP_READ_FILE, iMsg.next().what);
        assertEquals(GPXImporter.IMPORT_STEP_STORE_CACHES, iMsg.next().what);
        assertEquals(GPXImporter.IMPORT_STEP_FINISHED, iMsg.next().what);
        SearchResult search = (SearchResult) importStepHandler.messages.get(3).obj;
        assertEquals(Collections.singletonList("GC31J2H"), new ArrayList<String>(search.getGeocodes()));

        cgCache cache = cgeoapplication.getInstance().getCacheByGeocode("GC31J2H");
        assertCacheProperties(cache);

        // can't assert, for whatever reason the waypoints are remembered in DB
        //        assertNull(cache.waypoints);
    }

    private void runImportThread(GPXImporter.ImportThread importThread) {
        importThread.start();
        try {
            importThread.join();
        } catch (InterruptedException e) {
            Log.e(Settings.tag, "GPXImporterTest.runImportThread", e);
        }
        importStepHandler.waitForCompletion();
    }

    public void testImportGpxWithWaypoints() throws IOException {
        File gc31j2h = new File(tempDir, "gc31j2h.gpx");
        copyResourceToFile(R.raw.gc31j2h, gc31j2h);
        copyResourceToFile(R.raw.gc31j2h_wpts, new File(tempDir, "gc31j2h-wpts.gpx"));

        GPXImporter.ImportGpxFileThread importThread = new GPXImporter.ImportGpxFileThread(gc31j2h, listId, importStepHandler, progressHandler);
        runImportThread(importThread);

        assertImportStepMessages(GPXImporter.IMPORT_STEP_START, GPXImporter.IMPORT_STEP_READ_FILE, GPXImporter.IMPORT_STEP_READ_WPT_FILE, GPXImporter.IMPORT_STEP_STORE_CACHES, GPXImporter.IMPORT_STEP_FINISHED);
        SearchResult search = (SearchResult) importStepHandler.messages.get(4).obj;
        assertEquals(Collections.singletonList("GC31J2H"), new ArrayList<String>(search.getGeocodes()));

        cgCache cache = cgeoapplication.getInstance().getCacheByGeocode("GC31J2H");
        assertCacheProperties(cache);
        assertEquals(2, cache.getWaypoints().size());
    }

    private void assertImportStepMessages(int... importSteps) {
        assertEquals(importSteps.length, importStepHandler.messages.size());
        for (int i = 0; i < importSteps.length; i++) {
            assertEquals(importSteps[i], importStepHandler.messages.get(i).what);
        }
    }

    public void testImportLoc() throws IOException {
        File oc5952 = new File(tempDir, "oc5952.loc");
        copyResourceToFile(R.raw.oc5952_loc, oc5952);

        GPXImporter.ImportLocFileThread importThread = new GPXImporter.ImportLocFileThread(oc5952, listId, importStepHandler, progressHandler);
        runImportThread(importThread);

        assertImportStepMessages(GPXImporter.IMPORT_STEP_START, GPXImporter.IMPORT_STEP_READ_FILE, GPXImporter.IMPORT_STEP_STORE_CACHES, GPXImporter.IMPORT_STEP_FINISHED);
        SearchResult search = (SearchResult) importStepHandler.messages.get(3).obj;
        assertEquals(Collections.singletonList("OC5952"), new ArrayList<String>(search.getGeocodes()));

        cgCache cache = cgeoapplication.getInstance().getCacheByGeocode("OC5952");
        assertCacheProperties(cache);
    }

    private static void assertCacheProperties(cgCache cache) {
        assertNotNull(cache);
        assertFalse(cache.getLocation().startsWith(","));
    }

    public void testImportGpxError() throws IOException {
        File gc31j2h = new File(tempDir, "gc31j2h.gpx");
        copyResourceToFile(R.raw.gc31j2h_err, gc31j2h);

        GPXImporter.ImportGpxFileThread importThread = new GPXImporter.ImportGpxFileThread(gc31j2h, listId, importStepHandler, progressHandler);
        runImportThread(importThread);

        assertImportStepMessages(GPXImporter.IMPORT_STEP_START, GPXImporter.IMPORT_STEP_READ_FILE, GPXImporter.IMPORT_STEP_READ_FILE, GPXImporter.IMPORT_STEP_FINISHED_WITH_ERROR);
    }

    public void testImportGpxCancel() throws IOException {
        File gc31j2h = new File(tempDir, "gc31j2h.gpx");
        copyResourceToFile(R.raw.gc31j2h, gc31j2h);

        progressHandler.cancel();
        GPXImporter.ImportGpxFileThread importThread = new GPXImporter.ImportGpxFileThread(gc31j2h, listId, importStepHandler, progressHandler);
        runImportThread(importThread);

        assertImportStepMessages(GPXImporter.IMPORT_STEP_START, GPXImporter.IMPORT_STEP_READ_FILE, GPXImporter.IMPORT_STEP_CANCELED);
    }

    public void testImportGpxAttachment() {
        Uri uri = Uri.parse("android.resource://cgeo.geocaching.test/raw/gc31j2h");

        GPXImporter.ImportGpxAttachmentThread importThread = new GPXImporter.ImportGpxAttachmentThread(uri, getInstrumentation().getContext().getContentResolver(), listId, importStepHandler, progressHandler);
        runImportThread(importThread);

        assertImportStepMessages(GPXImporter.IMPORT_STEP_START, GPXImporter.IMPORT_STEP_READ_FILE, GPXImporter.IMPORT_STEP_STORE_CACHES, GPXImporter.IMPORT_STEP_FINISHED);
        SearchResult search = (SearchResult) importStepHandler.messages.get(3).obj;
        assertEquals(Collections.singletonList("GC31J2H"), new ArrayList<String>(search.getGeocodes()));

        cgCache cache = cgeoapplication.getInstance().getCacheByGeocode("GC31J2H");
        assertCacheProperties(cache);

        // can't assert, for whatever reason the waypoints are remembered in DB
        //        assertNull(cache.waypoints);
    }

    public void testImportGpxZip() throws IOException {
        File pq7545915 = new File(tempDir, "7545915.zip");
        copyResourceToFile(R.raw.pq7545915, pq7545915);

        GPXImporter.ImportGpxZipFileThread importThread = new GPXImporter.ImportGpxZipFileThread(pq7545915, listId, importStepHandler, progressHandler);
        runImportThread(importThread);

        assertImportStepMessages(GPXImporter.IMPORT_STEP_START, GPXImporter.IMPORT_STEP_READ_FILE, GPXImporter.IMPORT_STEP_READ_WPT_FILE, GPXImporter.IMPORT_STEP_STORE_CACHES, GPXImporter.IMPORT_STEP_FINISHED);
        SearchResult search = (SearchResult) importStepHandler.messages.get(4).obj;
        assertEquals(Collections.singletonList("GC31J2H"), new ArrayList<String>(search.getGeocodes()));

        cgCache cache = cgeoapplication.getInstance().getCacheByGeocode("GC31J2H");
        assertCacheProperties(cache);
        assertEquals(1, cache.getWaypoints().size()); // this is the original pocket query result without test waypoint
    }

    public void testImportGpxZipErr() throws IOException {
        File pqError = new File(tempDir, "pq_error.zip");
        copyResourceToFile(R.raw.pq_error, pqError);

        GPXImporter.ImportGpxZipFileThread importThread = new GPXImporter.ImportGpxZipFileThread(pqError, listId, importStepHandler, progressHandler);
        runImportThread(importThread);

        assertImportStepMessages(GPXImporter.IMPORT_STEP_START, GPXImporter.IMPORT_STEP_FINISHED_WITH_ERROR);
    }

    public void testImportGpxZipAttachment() {
        Uri uri = Uri.parse("android.resource://cgeo.geocaching.test/raw/pq7545915");

        GPXImporter.ImportGpxZipAttachmentThread importThread = new GPXImporter.ImportGpxZipAttachmentThread(uri, getInstrumentation().getContext().getContentResolver(), listId, importStepHandler, progressHandler);
        runImportThread(importThread);

        assertImportStepMessages(GPXImporter.IMPORT_STEP_START, GPXImporter.IMPORT_STEP_READ_FILE, GPXImporter.IMPORT_STEP_READ_WPT_FILE, GPXImporter.IMPORT_STEP_STORE_CACHES, GPXImporter.IMPORT_STEP_FINISHED);
        SearchResult search = (SearchResult) importStepHandler.messages.get(4).obj;
        assertEquals(Collections.singletonList("GC31J2H"), new ArrayList<String>(search.getGeocodes()));

        cgCache cache = cgeoapplication.getInstance().getCacheByGeocode("GC31J2H");
        assertCacheProperties(cache);
        assertEquals(1, cache.getWaypoints().size()); // this is the original pocket query result without test waypoint
    }

    static class TestHandler extends CancellableHandler {
        private final List<Message> messages = new ArrayList<Message>();
        private long lastMessage = System.currentTimeMillis();

        @Override
        public synchronized void handleRegularMessage(Message msg) {
            final Message msg1 = new Message();
            msg1.copyFrom(msg);
            messages.add(msg1);
            lastMessage = System.currentTimeMillis();
            notify();
        }

        public synchronized void waitForCompletion(final long milliseconds, final int maxMessages) {
            try {
                while (System.currentTimeMillis() - lastMessage <= milliseconds && messages.size() <= maxMessages) {
                    wait(milliseconds);
                }
            } catch (InterruptedException e) {
                // intentionally left blank
            }
        }

        public void waitForCompletion() {
            // Use reasonable defaults
            waitForCompletion(1000, 10);
        }
    }

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

        tempDir = new File(System.getProperty("java.io.tmpdir"), "cgeogpxesTest");
        tempDir.mkdir();

        // workaround to get storage initialized
        cgeoapplication.getInstance().getAllHistoricCachesCount();
        listId = cgeoapplication.getInstance().createList("cgeogpxesTest");
    }

    @Override
    protected void tearDown() throws Exception {
        cgeoapplication.getInstance().dropStored(listId);
        cgeoapplication.getInstance().removeList(listId);
        deleteDirectory(tempDir);
        super.tearDown();
    }

    private void deleteDirectory(File dir) {
        for (File f : dir.listFiles()) {
            if (f.isFile()) {
                f.delete();
            } else if (f.isDirectory()) {
                deleteDirectory(f);
            }
        }
        dir.delete();
    }

}