|
| 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 | +} |
0 commit comments