Skip to content

Commit 17aaa02

Browse files
committed
Test: Add test for GPX export with track segments support
1 parent 6eddc6a commit 17aaa02

File tree

3 files changed

+160
-3
lines changed

3 files changed

+160
-3
lines changed

app/src/main/java/net/osmtracker/gpx/ExportTrackTask.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ protected void exportTrackAsGpx(long trackId) throws ExportTrackException {
273273
* @param target Target GPX file
274274
* @throws IOException
275275
*/
276-
private void writeGpxFile(String trackName, String tags, String track_description, Cursor cTrackPoints, Cursor cWayPoints, File target) throws IOException {
276+
protected void writeGpxFile(String trackName, String tags, String track_description, Cursor cTrackPoints, Cursor cWayPoints, File target) throws IOException {
277277

278278
String accuracyOutput = PreferenceManager.getDefaultSharedPreferences(context).getString(
279279
OSMTracker.Preferences.KEY_OUTPUT_ACCURACY,
@@ -329,7 +329,7 @@ private void writeGpxFile(String trackName, String tags, String track_descriptio
329329
* @param compass Indicates if and how to write compass heading to the GPX ('none', 'comment', 'extension')
330330
* @throws IOException
331331
*/
332-
private void writeTrackPoints(String trackName, Writer fw, Cursor c, boolean fillHDOP, String compass) throws IOException {
332+
protected void writeTrackPoints(String trackName, Writer fw, Cursor c, boolean fillHDOP, String compass) throws IOException {
333333
// Update dialog every 1%
334334
int dialogUpdateThreshold = c.getCount() / 100;
335335
if (dialogUpdateThreshold == 0) {
@@ -419,7 +419,7 @@ private void writeTrackPoints(String trackName, Writer fw, Cursor c, boolean fil
419419
* @param compass Indicates if and how to write compass heading to the GPX ('none', 'comment', 'extension')
420420
* @throws IOException
421421
*/
422-
private void writeWayPoints(Writer fw, Cursor c, String accuracyInfo, boolean fillHDOP, String compass) throws IOException {
422+
protected void writeWayPoints(Writer fw, Cursor c, String accuracyInfo, boolean fillHDOP, String compass) throws IOException {
423423

424424
// Update dialog every 1%
425425
int dialogUpdateThreshold = c.getCount() / 100;
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
package net.osmtracker.gpx;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import android.content.ContentValues;
6+
import android.content.Context;
7+
import android.database.MatrixCursor;
8+
import androidx.preference.PreferenceManager;
9+
import androidx.test.core.app.ApplicationProvider;
10+
11+
import net.osmtracker.OSMTracker;
12+
import net.osmtracker.db.TrackContentProvider;
13+
14+
import org.apache.commons.io.FileUtils;
15+
import org.apache.commons.io.IOUtils;
16+
import org.junit.After;
17+
import org.junit.Before;
18+
import org.junit.Test;
19+
import org.junit.runner.RunWith;
20+
import org.robolectric.Robolectric;
21+
import org.robolectric.RobolectricTestRunner;
22+
import org.robolectric.android.controller.ContentProviderController;
23+
import org.robolectric.annotation.Config;
24+
import org.robolectric.shadows.ShadowContentResolver;
25+
26+
import java.io.File;
27+
import java.io.InputStream;
28+
import java.nio.charset.StandardCharsets;
29+
import java.util.Locale;
30+
31+
@RunWith(RobolectricTestRunner.class)
32+
@Config(sdk = 25, qualifiers = "es")
33+
public class ExportToTempFileTaskTest {
34+
35+
private Context context;
36+
private final long testTrackId = 123L;
37+
38+
private ContentProviderController<TrackContentProvider> providerController;
39+
40+
41+
/**
42+
* Concrete implementation of the abstract ExportToTempFileTask for testing.
43+
*/
44+
private static class TestTempExportTask extends ExportToTempFileTask {
45+
public TestTempExportTask(Context context, long trackId) {
46+
super(context, trackId);
47+
}
48+
@Override
49+
protected void executionCompleted() {
50+
// No-op for testing
51+
}
52+
}
53+
54+
@Before
55+
public void setUp() {
56+
context = ApplicationProvider.getApplicationContext();
57+
providerController = Robolectric.buildContentProvider(TrackContentProvider.class)
58+
.create(TrackContentProvider.AUTHORITY);
59+
60+
// Set up locale and preferences to to match gpx-test.gpx file
61+
Locale esLocale = new Locale("es");
62+
Locale.setDefault(esLocale);
63+
context.getResources().getConfiguration().setLocale(esLocale);
64+
PreferenceManager.getDefaultSharedPreferences(context).edit()
65+
.putBoolean(OSMTracker.Preferences.KEY_OUTPUT_GPX_HDOP_APPROXIMATION, true)
66+
.putString(OSMTracker.Preferences.KEY_OUTPUT_COMPASS, "extension")
67+
.apply();
68+
69+
// Initialize the DB record so the Task constructor doesn't fail
70+
ContentValues values = new ContentValues();
71+
values.put(TrackContentProvider.Schema.COL_ID, testTrackId);
72+
values.put(TrackContentProvider.Schema.COL_NAME, "2020-12-30_17-20-17");
73+
values.put(TrackContentProvider.Schema.COL_START_DATE, 990055225000L);
74+
values.put(TrackContentProvider.Schema.COL_TAGS, "osmtracker");
75+
context.getContentResolver().insert(TrackContentProvider.CONTENT_URI_TRACK, values);
76+
}
77+
78+
@After
79+
public void tearDown() {
80+
// Shut down the provider explicitly to close database
81+
if (providerController != null) {
82+
providerController.shutdown();
83+
}
84+
ShadowContentResolver.reset();
85+
}
86+
87+
@Test
88+
public void testExportMatchesGpxResource() throws Exception {
89+
try (
90+
MatrixCursor pointCursor = createPointCursor();
91+
MatrixCursor wptCursor = createWptCursor()
92+
) {
93+
TestTempExportTask task = new TestTempExportTask(context, testTrackId);
94+
File outputFile = task.getTmpFile();
95+
96+
// Load gpx-test.gpx file
97+
InputStream is = getClass().getClassLoader().getResourceAsStream("gpx/gpx-test.gpx");
98+
if (is == null) throw new RuntimeException("Resource gpx-test.gpx not found");
99+
String expectedXml = IOUtils.toString(is, StandardCharsets.UTF_8.name());
100+
101+
// Execute GPX writing logic
102+
task.writeGpxFile("2020-12-30_17-20-17","osmtracker",null,
103+
pointCursor,wptCursor,outputFile);
104+
String actualXml = FileUtils.readFileToString(outputFile, StandardCharsets.UTF_8.name());
105+
106+
assertEquals(expectedXml, actualXml);
107+
}
108+
}
109+
110+
// Helper method to create the point cursor
111+
private MatrixCursor createPointCursor() {
112+
String[] ptColumns = new String[] {
113+
TrackContentProvider.Schema.COL_LATITUDE,
114+
TrackContentProvider.Schema.COL_LONGITUDE,
115+
TrackContentProvider.Schema.COL_ELEVATION,
116+
TrackContentProvider.Schema.COL_TIMESTAMP,
117+
TrackContentProvider.Schema.COL_SEG_ID,
118+
TrackContentProvider.Schema.COL_ACCURACY,
119+
TrackContentProvider.Schema.COL_SPEED,
120+
TrackContentProvider.Schema.COL_COMPASS,
121+
TrackContentProvider.Schema.COL_COMPASS_ACCURACY,
122+
TrackContentProvider.Schema.COL_ATMOSPHERIC_PRESSURE,
123+
};
124+
125+
MatrixCursor pointCursor = new MatrixCursor(ptColumns);
126+
pointCursor.addRow(new Object[]{10.0375690436648, -84.2122886824885, 1015.13159253262,
127+
990055225000L, 0, 35.635440826416, 0.361938893795013, 204.690002441406, 2.0, null});
128+
pointCursor.addRow(new Object[]{10.0377106969496, -84.2123268390527, 1007.3209409276,
129+
990055226000L, 1, 52.0706405639648, 0.271533757448196, 204.360000610352, 2.0, null});
130+
return pointCursor;
131+
}
132+
133+
// Helper method to create the waypoint cursor
134+
private MatrixCursor createWptCursor() {
135+
String[] wptColumns = new String[]{
136+
TrackContentProvider.Schema.COL_LATITUDE,
137+
TrackContentProvider.Schema.COL_LONGITUDE,
138+
TrackContentProvider.Schema.COL_ELEVATION,
139+
TrackContentProvider.Schema.COL_TIMESTAMP,
140+
TrackContentProvider.Schema.COL_NAME,
141+
TrackContentProvider.Schema.COL_ACCURACY,
142+
TrackContentProvider.Schema.COL_COMPASS,
143+
TrackContentProvider.Schema.COL_COMPASS_ACCURACY,
144+
TrackContentProvider.Schema.COL_LINK,
145+
TrackContentProvider.Schema.COL_NBSATELLITES,
146+
TrackContentProvider.Schema.COL_ATMOSPHERIC_PRESSURE,
147+
};
148+
149+
MatrixCursor wptCursor = new MatrixCursor(wptColumns);
150+
wptCursor.addRow(new Object[]{10.0375634848231, -84.2123549868801, 1029.89940680377,
151+
990055228000L, "Punto de prueba", 24.0, 204.029998779297, 2.0, null, 0, null});
152+
return wptCursor;
153+
}
154+
155+
}

app/src/test/resources/gpx/gpx-test.gpx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
<compass_accuracy>2.0</compass_accuracy>
3030
</extensions>
3131
</trkpt>
32+
</trkseg>
33+
<trkseg>
3234
<trkpt lat="10.0377106969496" lon="-84.2123268390527">
3335
<ele>1007.3209409276</ele>
3436
<time>2001-05-16T23:20:26Z</time>

0 commit comments

Comments
 (0)