From 5ac72092fb8f185ad027dea6dd54c7fb76803bfa Mon Sep 17 00:00:00 2001 From: Dennis Guse Date: Sun, 22 Dec 2019 18:50:54 +0100 Subject: [PATCH] Test: export and import tracks via KML and GPX; now includes waypoints. TripStatistics is not tested. --- .../CustomContentProviderUtilsTest.java | 117 ++++----------- .../opentracks/content/data/TestDataUtil.java | 44 ++++++ .../io/file/importer/ExportImportTest.java | 139 +++++++++++++++--- .../services/TrackRecordingServiceTest.java | 2 +- .../opentracks/MarkerListActivity.java | 11 +- .../content/ContentProviderUtils.java | 62 +++----- .../io/file/exporter/FileTrackExporter.java | 1 - .../importer/AbstractFileTrackImporter.java | 3 + .../opentracks/util/LocationUtils.java | 1 + 9 files changed, 221 insertions(+), 159 deletions(-) create mode 100644 src/androidTest/java/de/dennisguse/opentracks/content/data/TestDataUtil.java diff --git a/src/androidTest/java/de/dennisguse/opentracks/content/CustomContentProviderUtilsTest.java b/src/androidTest/java/de/dennisguse/opentracks/content/CustomContentProviderUtilsTest.java index e77525207..6dff5425f 100644 --- a/src/androidTest/java/de/dennisguse/opentracks/content/CustomContentProviderUtilsTest.java +++ b/src/androidTest/java/de/dennisguse/opentracks/content/CustomContentProviderUtilsTest.java @@ -35,6 +35,7 @@ import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; +import de.dennisguse.opentracks.content.data.TestDataUtil; import de.dennisguse.opentracks.content.data.Track; import de.dennisguse.opentracks.content.data.TrackPointsColumns; import de.dennisguse.opentracks.content.data.TracksColumns; @@ -56,9 +57,6 @@ public class CustomContentProviderUtilsTest { private static final String MOCK_DESC = "Mock Next Waypoint Desc!"; private static final String TEST_DESC = "Test Desc!"; private static final String TEST_DESC_NEW = "Test Desc new!"; - private static final double INITIAL_LATITUDE = 37.0; - private static final double INITIAL_LONGITUDE = -57.0; - private static final double ALTITUDE_INTERVAL = 2.5; private Context context = ApplicationProvider.getApplicationContext(); private ContentProviderUtils contentProviderUtils; @@ -224,7 +222,7 @@ public class CustomContentProviderUtilsTest { public void testDeleteAllTracks() { // Insert track, points and waypoint at first. long trackId = System.currentTimeMillis(); - Track track = getTrack(trackId, 10); + Track track = TestDataUtil.getTrack(trackId, 10); insertTrackWithLocations(track); Waypoint waypoint = new Waypoint(); contentProviderUtils.insertWaypoint(waypoint); @@ -253,11 +251,11 @@ public class CustomContentProviderUtilsTest { public void testDeleteTrack() { // Insert three tracks, points of two tracks and way point of one track. long trackId = System.currentTimeMillis(); - Track track = getTrack(trackId, 10); + Track track = TestDataUtil.getTrack(trackId, 10); contentProviderUtils.insertTrack(track); - insertTrackWithLocations(getTrack(trackId + 1, 10)); - insertTrackWithLocations(getTrack(trackId + 2, 10)); + insertTrackWithLocations(TestDataUtil.getTrack(trackId + 1, 10)); + insertTrackWithLocations(TestDataUtil.getTrack(trackId + 2, 10)); Waypoint waypoint = new Waypoint(); waypoint.setTrackId(trackId); @@ -288,7 +286,7 @@ public class CustomContentProviderUtilsTest { public void testGetAllTracks() { int initialTrackNumber = contentProviderUtils.getAllTracks().size(); long trackId = System.currentTimeMillis(); - contentProviderUtils.insertTrack(getTrack(trackId, 0)); + contentProviderUtils.insertTrack(TestDataUtil.getTrack(trackId, 0)); List allTracks = contentProviderUtils.getAllTracks(); Assert.assertEquals(initialTrackNumber + 1, allTracks.size()); Assert.assertEquals(trackId, allTracks.get(allTracks.size() - 1).getId()); @@ -300,7 +298,7 @@ public class CustomContentProviderUtilsTest { @Test public void testGetLastTrack() { long trackId = System.currentTimeMillis(); - contentProviderUtils.insertTrack(getTrack(trackId, 0)); + contentProviderUtils.insertTrack(TestDataUtil.getTrack(trackId, 0)); Assert.assertEquals(trackId, contentProviderUtils.getLastTrack().getId()); } @@ -310,7 +308,7 @@ public class CustomContentProviderUtilsTest { @Test public void testGetTrack() { long trackId = System.currentTimeMillis(); - contentProviderUtils.insertTrack(getTrack(trackId, 0)); + contentProviderUtils.insertTrack(TestDataUtil.getTrack(trackId, 0)); Assert.assertNotNull(contentProviderUtils.getTrack(trackId)); } @@ -320,7 +318,7 @@ public class CustomContentProviderUtilsTest { @Test public void testUpdateTrack() { long trackId = System.currentTimeMillis(); - Track track = getTrack(trackId, 0); + Track track = TestDataUtil.getTrack(trackId, 0); String nameOld = "name1"; String nameNew = "name2"; track.setName(nameOld); @@ -337,7 +335,7 @@ public class CustomContentProviderUtilsTest { @Test public void testCreateContentValues_waypoint() { long trackId = System.currentTimeMillis(); - Track track = getTrack(trackId, 10); + Track track = TestDataUtil.getTrack(trackId, 10); contentProviderUtils.insertTrack(track); // Bottom long startTime = 1000L; @@ -423,7 +421,7 @@ public class CustomContentProviderUtilsTest { @Test public void testDeleteWaypoint_onlyOneWayPoint() { long trackId = System.currentTimeMillis(); - Track track = getTrack(trackId, 10); + Track track = TestDataUtil.getTrack(trackId, 10); contentProviderUtils.insertTrack(track); // Insert at first. @@ -446,7 +444,7 @@ public class CustomContentProviderUtilsTest { @Test public void testDeleteWaypoint_hasNextWayPoint() { long trackId = System.currentTimeMillis(); - Track track = getTrack(trackId, 10); + Track track = TestDataUtil.getTrack(trackId, 10); contentProviderUtils.insertTrack(track); TripStatistics statistics = new TripStatistics(); @@ -484,35 +482,13 @@ public class CustomContentProviderUtilsTest { Assert.assertEquals(MOCK_DESC, contentProviderUtils.getWaypoint(waypoint2Id).getDescription()); } - /** - * Tests the method {@link ContentProviderUtils#getFirstWaypointId(long)}. - */ - @Test - public void testGetFirstWaypointId() { - long trackId = System.currentTimeMillis(); - Track track = getTrack(trackId, 10); - contentProviderUtils.insertTrack(track); - - Waypoint waypoint1 = new Waypoint(); - waypoint1.setTrackId(trackId); - long waypoint1id = ContentUris.parseId(contentProviderUtils.insertWaypoint(waypoint1)); - - Waypoint waypoint2 = new Waypoint(); - waypoint2.setTrackId(trackId); - long waypoint2id = ContentUris.parseId(contentProviderUtils.insertWaypoint(waypoint2)); - - Assert.assertEquals(-1L, contentProviderUtils.getFirstWaypointId(-1)); - Assert.assertEquals(waypoint1id, contentProviderUtils.getFirstWaypointId(trackId)); - Assert.assertEquals(waypoint2id, contentProviderUtils.getLastWaypoint(trackId).getId()); - } - /** * Tests the method {@link ContentProviderUtils#getNextWaypointNumber(long)}. */ @Test public void testGetNextWaypointNumber() { long trackId = System.currentTimeMillis(); - Track track = getTrack(trackId, 10); + Track track = TestDataUtil.getTrack(trackId, 10); contentProviderUtils.insertTrack(track); Waypoint waypoint1 = new Waypoint(); @@ -538,7 +514,7 @@ public class CustomContentProviderUtilsTest { @Test public void testGetLastWaypoint() { long trackId = System.currentTimeMillis(); - Track track = getTrack(trackId, 10); + Track track = TestDataUtil.getTrack(trackId, 10); contentProviderUtils.insertTrack(track); Waypoint waypoint1 = new Waypoint(); @@ -564,7 +540,7 @@ public class CustomContentProviderUtilsTest { @Test public void testInsertAndGetWaypoint() { long trackId = System.currentTimeMillis(); - Track track = getTrack(trackId, 10); + Track track = TestDataUtil.getTrack(trackId, 10); contentProviderUtils.insertTrack(track); Waypoint waypoint = new Waypoint(); @@ -581,7 +557,7 @@ public class CustomContentProviderUtilsTest { @Test public void testUpdateWaypoint() { long trackId = System.currentTimeMillis(); - Track track = getTrack(trackId, 10); + Track track = TestDataUtil.getTrack(trackId, 10); contentProviderUtils.insertTrack(track); // Insert at first. Waypoint waypoint = new Waypoint(); @@ -604,7 +580,7 @@ public class CustomContentProviderUtilsTest { public void testBulkInsertTrackPoint() { // Insert track, point at first. long trackId = System.currentTimeMillis(); - Track track = getTrack(trackId, 10); + Track track = TestDataUtil.getTrack(trackId, 10); insertTrackWithLocations(track); contentProviderUtils.bulkInsertTrackPoint(track.getLocations().toArray(new Location[0]), -1, trackId); @@ -680,10 +656,10 @@ public class CustomContentProviderUtilsTest { public void testInsertTrackPoint() { // Insert track, point at first. long trackId = System.currentTimeMillis(); - Track track = getTrack(trackId, 10); + Track track = TestDataUtil.getTrack(trackId, 10); insertTrackWithLocations(track); - contentProviderUtils.insertTrackPoint(createLocation(22), trackId); + contentProviderUtils.insertTrackPoint(TestDataUtil.createLocation(22), trackId); Assert.assertEquals(11, contentProviderUtils.getTrackPointCursor(trackId, -1L, 1000, false).getCount()); } @@ -694,7 +670,7 @@ public class CustomContentProviderUtilsTest { public void testGetFirstTrackPointId() { // Insert track, point at first. long trackId = System.currentTimeMillis(); - Track track = getTrack(trackId, 10); + Track track = TestDataUtil.getTrack(trackId, 10); insertTrackWithLocations(track); Assert.assertNotEquals(-1L, contentProviderUtils.getFirstTrackPointId(trackId)); @@ -707,7 +683,7 @@ public class CustomContentProviderUtilsTest { public void testGetLastTrackPointId() { // Insert track, point at first. long trackId = System.currentTimeMillis(); - Track track = getTrack(trackId, 10); + Track track = TestDataUtil.getTrack(trackId, 10); insertTrackWithLocations(track); Assert.assertTrue(contentProviderUtils.getFirstTrackPointId(trackId) < contentProviderUtils.getLastTrackPointId(trackId)); @@ -720,7 +696,7 @@ public class CustomContentProviderUtilsTest { public void testGetLastValidTrackPoint() { // Insert track, points at first. long trackId = System.currentTimeMillis(); - Track track = getTrack(trackId, 10); + Track track = TestDataUtil.getTrack(trackId, 10); insertTrackWithLocations(track); Location lastLocation = contentProviderUtils.getLastValidTrackPoint(trackId); @@ -734,7 +710,7 @@ public class CustomContentProviderUtilsTest { public void testGetTrackPointCursor_desc() { // Insert track, points at first. long trackId = System.currentTimeMillis(); - Track track = getTrack(trackId, 10); + Track track = TestDataUtil.getTrack(trackId, 10); contentProviderUtils.insertTrack(track); long[] trackpointIds = new long[track.getNumberOfPoints()]; @@ -753,7 +729,7 @@ public class CustomContentProviderUtilsTest { public void testGetTrackPointCursor_asc() { // Insert track, points at first. long trackId = System.currentTimeMillis(); - Track track = getTrack(trackId, 10); + Track track = TestDataUtil.getTrack(trackId, 10); contentProviderUtils.insertTrack(track); long[] trackpointIds = new long[track.getNumberOfPoints()]; @@ -772,7 +748,7 @@ public class CustomContentProviderUtilsTest { public void testGetTrackPointLocationIterator_desc() { // Insert track, points at first. long trackId = System.currentTimeMillis(); - Track track = getTrack(trackId, 10); + Track track = TestDataUtil.getTrack(trackId, 10); contentProviderUtils.insertTrack(track); long[] trackpointIds = new long[track.getNumberOfPoints()]; @@ -799,7 +775,7 @@ public class CustomContentProviderUtilsTest { public void testGetTrackPointLocationIterator_asc() { // Insert track, point at first. long trackId = System.currentTimeMillis(); - Track track = getTrack(trackId, 10); + Track track = TestDataUtil.getTrack(trackId, 10); contentProviderUtils.insertTrack(track); long[] trackpointIds = new long[track.getNumberOfPoints()]; @@ -820,50 +796,17 @@ public class CustomContentProviderUtilsTest { Assert.assertFalse(locationIterator.hasNext()); } - /** - * Simulates a track which is used for testing. - * - * @param id the id of the track - * @param numPoints the location number in the track - * @return the simulated track - */ - public static Track getTrack(long id, int numPoints) { - Track track = new Track(); - track.setId(id); - track.setName("Test: " + id); - track.setNumberOfPoints(numPoints); - for (int i = 0; i < numPoints; i++) { - track.addLocation(createLocation(i)); - } - return track; - } - - /** - * Creates a location. - * - * @param i the index to set the value of location. - * @return created location - */ - private static Location createLocation(int i) { - Location loc = new Location("test"); - loc.setLatitude(INITIAL_LATITUDE + (double) i / 10000.0); - loc.setLongitude(INITIAL_LONGITUDE - (double) i / 10000.0); - loc.setAccuracy((float) i / 100.0f); - loc.setAltitude(i * ALTITUDE_INTERVAL); - return loc; - } - /** * Checks the value of a location. * - * @param i the index of this location which created in the method {@link CustomContentProviderUtilsTest#getTrack(long, int)} + * @param i the index of this location which created in the method {@link TestDataUtil#getTrack(long, int)} * @param location the location to be checked */ private void checkLocation(int i, Location location) { - Assert.assertEquals(INITIAL_LATITUDE + (double) i / 10000.0, location.getLatitude(), 0.01); - Assert.assertEquals(INITIAL_LONGITUDE - (double) i / 10000.0, location.getLongitude(), 0.01); + Assert.assertEquals(TestDataUtil.INITIAL_LATITUDE + (double) i / 10000.0, location.getLatitude(), 0.01); + Assert.assertEquals(TestDataUtil.INITIAL_LONGITUDE - (double) i / 10000.0, location.getLongitude(), 0.01); Assert.assertEquals((float) i / 100.0f, location.getAccuracy(), 0.01); - Assert.assertEquals(i * ALTITUDE_INTERVAL, location.getAltitude(), 0.01); + Assert.assertEquals(i * TestDataUtil.ALTITUDE_INTERVAL, location.getAltitude(), 0.01); } /** diff --git a/src/androidTest/java/de/dennisguse/opentracks/content/data/TestDataUtil.java b/src/androidTest/java/de/dennisguse/opentracks/content/data/TestDataUtil.java new file mode 100644 index 000000000..44ca74a9a --- /dev/null +++ b/src/androidTest/java/de/dennisguse/opentracks/content/data/TestDataUtil.java @@ -0,0 +1,44 @@ +package de.dennisguse.opentracks.content.data; + +import android.location.Location; + +public class TestDataUtil { + + public static final double INITIAL_LATITUDE = 37.0; + public static final double INITIAL_LONGITUDE = -57.0; + public static final double ALTITUDE_INTERVAL = 2.5; + + /** + * Simulates a track which is used for testing. + * + * @param id the id of the track + * @param numPoints the location number in the track + * @return the simulated track + */ + public static Track getTrack(long id, int numPoints) { + Track track = new Track(); + track.setId(id); + track.setName("Test: " + id); + track.setNumberOfPoints(numPoints); + for (int i = 0; i < numPoints; i++) { + track.addLocation(createLocation(i)); + } + return track; + } + + /** + * Creates a location. + * + * @param i the index to set the value of location. + * @return created location + */ + public static Location createLocation(int i) { + Location loc = new Location("test"); + loc.setLatitude(INITIAL_LATITUDE + (double) i / 10000.0); + loc.setLongitude(INITIAL_LONGITUDE - (double) i / 10000.0); + loc.setAccuracy((float) i / 100.0f); + loc.setAltitude(i * ALTITUDE_INTERVAL); + loc.setTime(i + 1); + return loc; + } +} diff --git a/src/androidTest/java/de/dennisguse/opentracks/io/file/importer/ExportImportTest.java b/src/androidTest/java/de/dennisguse/opentracks/io/file/importer/ExportImportTest.java index 0de25837b..97dab6cf9 100644 --- a/src/androidTest/java/de/dennisguse/opentracks/io/file/importer/ExportImportTest.java +++ b/src/androidTest/java/de/dennisguse/opentracks/io/file/importer/ExportImportTest.java @@ -2,8 +2,10 @@ package de.dennisguse.opentracks.io.file.importer; import android.content.Context; import android.location.Location; +import android.util.Log; import androidx.test.core.app.ApplicationProvider; +import androidx.test.filters.LargeTest; import org.junit.After; import org.junit.Before; @@ -14,19 +16,29 @@ import org.junit.runners.JUnit4; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; import de.dennisguse.opentracks.content.ContentProviderUtils; -import de.dennisguse.opentracks.content.CustomContentProviderUtilsTest; +import de.dennisguse.opentracks.content.data.TestDataUtil; import de.dennisguse.opentracks.content.data.Track; +import de.dennisguse.opentracks.content.data.Waypoint; import de.dennisguse.opentracks.io.file.TrackFileFormat; import de.dennisguse.opentracks.io.file.exporter.TrackExporter; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +/** + * Export a track to {@link TrackFileFormat} and verify that the import is identical. + *

+ * TODO: test ignores {@link de.dennisguse.opentracks.stats.TripStatistics} for now. + */ @RunWith(JUnit4.class) public class ExportImportTest { + private static final String TAG = ExportImportTest.class.getSimpleName(); + private Context context = ApplicationProvider.getApplicationContext(); private ContentProviderUtils contentProviderUtils = new ContentProviderUtils(context); @@ -34,68 +46,131 @@ public class ExportImportTest { private static final String TRACK_ICON = "the track icon"; private static final String TRACK_CATEGORY = "the category"; private static final String TRACK_DESCRIPTION = "the description"; - private long exportTrackId = System.currentTimeMillis(); + private final List waypoints = new ArrayList<>(); private long importTrackId; + private long trackId = System.currentTimeMillis(); @Before public void setUp() { - Track track = CustomContentProviderUtilsTest.getTrack(exportTrackId, 150); + Track track = TestDataUtil.getTrack(trackId, 10); track.setIcon(TRACK_ICON); track.setCategory(TRACK_CATEGORY); track.setDescription(TRACK_DESCRIPTION); contentProviderUtils.insertTrack(track); contentProviderUtils.bulkInsertTrackPoint(track.getLocations().toArray(new Location[0]), track.getLocations().size(), track.getId()); + + for (int i = 0; i < 3; i++) { + Waypoint waypoint = new Waypoint(); + waypoint.setName("the waypoint " + i); + waypoint.setDescription("the waypoint description " + i); + waypoint.setCategory("the waypoint category" + i); + waypoint.setIcon("the waypoing icon" + i); + waypoint.setPhotoUrl("the photo url" + i); + waypoint.setTrackId(trackId); + waypoint.setLocation(track.getLocations().get(i)); + contentProviderUtils.insertWaypoint(waypoint); + + waypoints.add(waypoint); + } + + assertEquals(waypoints.size(), contentProviderUtils.getWaypointCount(trackId)); } @After public void tearDown() { - contentProviderUtils.deleteTrack(context, exportTrackId); + contentProviderUtils.deleteTrack(context, trackId); contentProviderUtils.deleteTrack(context, importTrackId); } + @LargeTest + @Test + public void kml_only_track() { + // TODO + Log.e(TAG, "Test not implemented."); + } + + @LargeTest @Test public void kml_with_trackdetail() { // given - Track track = contentProviderUtils.getTrack(exportTrackId); + Track track = contentProviderUtils.getTrack(trackId); TrackFileFormat trackFileFormat = TrackFileFormat.KML_WITH_TRACKDETAIL; TrackExporter trackExporter = trackFileFormat.newTrackExporter(context, new Track[]{track}, null); // when - // 1. export ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); trackExporter.writeTrack(context, outputStream); + System.out.println(outputStream.toString()); + // 2. import InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); AbstractFileTrackImporter trackImporter = new KmlFileTrackImporter(context, -1L); importTrackId = trackImporter.importFile(inputStream); // then - Track trackImported = contentProviderUtils.getTrack(importTrackId); - assertNotNull(trackImported); - assertEquals(track.getLocations(), trackImported.getLocations()); - assertEquals(track.getCategory(), trackImported.getCategory()); - assertEquals(track.getDescription(), trackImported.getDescription()); - assertEquals(track.getName(), trackImported.getName()); - assertEquals(track.getIcon(), trackImported.getIcon()); + // 1. track + Track importedTrack = contentProviderUtils.getTrack(importTrackId); + assertNotNull(importedTrack); + assertEquals(track.getLocations(), importedTrack.getLocations()); + assertEquals(track.getCategory(), importedTrack.getCategory()); + assertEquals(track.getDescription(), importedTrack.getDescription()); + assertEquals(track.getName(), importedTrack.getName()); + assertEquals(track.getIcon(), importedTrack.getIcon()); - //TODO Check (relative/absolute) time of trackpoints - //TODO Check marker/waypoints - //TODO Check tripstatistics + // 2. waypoints + assertWaypoints(); + + //TODO Check absolute time of trackpoints } + @LargeTest + @Test + public void kml_with_trackdetail_and_sensordata() { + // TODO + Log.e(TAG, "Test not implemented."); + } + + @LargeTest + @Test + public void kmz_only_track() { + // TODO + Log.e(TAG, "Test not implemented."); + } + + @LargeTest + @Test + public void kmz_with_trackdetail() { + // TODO + Log.e(TAG, "Test not implemented."); + } + + @LargeTest + @Test + public void kmz_with_trackdetail_and_sensordata() { + // TODO + Log.e(TAG, "Test not implemented."); + } + + @LargeTest + @Test + public void kmz_with_trackdetail_and_sensordata_and_pictures() { + // TODO + Log.e(TAG, "Test not implemented."); + } + + @LargeTest @Test public void gpx() { // given - Track track = contentProviderUtils.getTrack(exportTrackId); + Track track = contentProviderUtils.getTrack(trackId); TrackFileFormat trackFileFormat = TrackFileFormat.GPX; TrackExporter trackExporter = trackFileFormat.newTrackExporter(context, new Track[]{track}, null); // when - // 1. export ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); trackExporter.writeTrack(context, outputStream); @@ -106,6 +181,7 @@ public class ExportImportTest { importTrackId = trackImporter.importFile(inputStream); // then + // 1. track Track trackImported = contentProviderUtils.getTrack(importTrackId); assertNotNull(trackImported); assertEquals(track.getLocations(), trackImported.getLocations()); @@ -115,8 +191,29 @@ public class ExportImportTest { //TODO exporting and importing a track icon is not yet supported by GpxTrackWriter. //assertEquals(track.getIcon(), trackImported.getIcon()); - //TODO Check (relative/absolute) time of trackpoints - //TODO Check marker/waypoints - //TODO Check tripstatistics + + // 2. waypoints + assertWaypoints(); + + //TODO Check absolute time of trackpoints + } + + private void assertWaypoints() { + assertEquals(waypoints.size(), contentProviderUtils.getWaypointCount(importTrackId)); + + List importedWaypoints = contentProviderUtils.getWaypoints(importTrackId); + for (int i = 0; i < waypoints.size(); i++) { + Waypoint waypoint = waypoints.get(i); + Waypoint importedWaypoint = importedWaypoints.get(i); + assertEquals(waypoint.getCategory(), importedWaypoint.getCategory()); + assertEquals(waypoint.getDescription(), importedWaypoint.getDescription()); + // assertEquals(waypoint.getIcon(), importedWaypoint.getIcon()); // TODO for KML + assertEquals(waypoint.getName(), importedWaypoint.getName()); + assertEquals("", importedWaypoint.getPhotoUrl()); + + assertEquals(waypoint.getLocation().getLatitude(), importedWaypoint.getLocation().getLatitude(), 0.001); + assertEquals(waypoint.getLocation().getLongitude(), importedWaypoint.getLocation().getLongitude(), 0.001); + assertEquals(waypoint.getLocation().getAltitude(), importedWaypoint.getLocation().getAltitude(), 0.001); + } } } \ No newline at end of file diff --git a/src/androidTest/java/de/dennisguse/opentracks/services/TrackRecordingServiceTest.java b/src/androidTest/java/de/dennisguse/opentracks/services/TrackRecordingServiceTest.java index 12c8615a9..9e49afb5d 100644 --- a/src/androidTest/java/de/dennisguse/opentracks/services/TrackRecordingServiceTest.java +++ b/src/androidTest/java/de/dennisguse/opentracks/services/TrackRecordingServiceTest.java @@ -55,7 +55,7 @@ import de.dennisguse.opentracks.util.PreferencesUtils; * * @author Bartlomiej Niechwiej *

- * ATTENTION: This tests deletes all stored tracks in the database. + * //TODO ATTENTION: This tests deletes all stored tracks in the database. * So, if it is executed on a real device, data might be lost. */ @RunWith(AndroidJUnit4.class) diff --git a/src/main/java/de/dennisguse/opentracks/MarkerListActivity.java b/src/main/java/de/dennisguse/opentracks/MarkerListActivity.java index b39a82852..ffb12c6c2 100644 --- a/src/main/java/de/dennisguse/opentracks/MarkerListActivity.java +++ b/src/main/java/de/dennisguse/opentracks/MarkerListActivity.java @@ -126,17 +126,14 @@ public class MarkerListActivity extends AbstractActivity implements DeleteMarker @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - + setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL); + long trackId = getIntent().getLongExtra(EXTRA_TRACK_ID, -1L); recordingTrackPaused = PreferencesUtils.isRecordingTrackPausedDefault(this); contentProviderUtils = new ContentProviderUtils(this); sharedPreferences = PreferencesUtils.getSharedPreferences(this); - long trackId = getIntent().getLongExtra(EXTRA_TRACK_ID, -1L); track = trackId != -1L ? contentProviderUtils.getTrack(trackId) : null; - final long trackFirstWaypointId = trackId != -1 ? contentProviderUtils.getFirstWaypointId(trackId) : -1; - - setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL); listView = findViewById(R.id.marker_list); listView.setEmptyView(findViewById(R.id.marker_list_empty)); @@ -181,8 +178,8 @@ public class MarkerListActivity extends AbstractActivity implements DeleteMarker public Loader onCreateLoader(int arg0, Bundle arg1) { if (track != null) { return new CursorLoader(MarkerListActivity.this, WaypointsColumns.CONTENT_URI, PROJECTION, - WaypointsColumns.TRACKID + "=? AND " + WaypointsColumns._ID + "!=?", - new String[]{String.valueOf(track.getId()), String.valueOf(trackFirstWaypointId)}, null); + WaypointsColumns.TRACKID + "=?", + new String[]{String.valueOf(track.getId())}, null); } else { return new CursorLoader(MarkerListActivity.this, WaypointsColumns.CONTENT_URI, PROJECTION, WaypointsColumns.STARTTIME + " IS NULL", null, null); diff --git a/src/main/java/de/dennisguse/opentracks/content/ContentProviderUtils.java b/src/main/java/de/dennisguse/opentracks/content/ContentProviderUtils.java index 94b252723..b0b97c5a6 100644 --- a/src/main/java/de/dennisguse/opentracks/content/ContentProviderUtils.java +++ b/src/main/java/de/dennisguse/opentracks/content/ContentProviderUtils.java @@ -539,7 +539,7 @@ public class ContentProviderUtils { * If deleting a statistics waypoint, this will also correct the next statistics waypoint after the deleted one to reflect the deletion. * The generator is used to update the next statistics waypoint. * - * @param waypointId the waypoint id + * @param waypointId the waypoint id */ public void deleteWaypoint(Context context, long waypointId) { @@ -559,29 +559,10 @@ public class ContentProviderUtils { contentResolver.delete(WaypointsColumns.CONTENT_URI, WaypointsColumns._ID + "=?", new String[]{Long.toString(waypointId)}); } - /** - * Gets the first waypoint id for a track. - * The first waypoint is special as it contains the stats for the track. - * Returns -1L if it doesn't exist. - * - * @param trackId the track id - */ - public long getFirstWaypointId(long trackId) { - if (trackId < 0) { - return -1L; - } - try (Cursor cursor = getWaypointCursor(new String[]{WaypointsColumns._ID}, WaypointsColumns.TRACKID + "=?", new String[]{Long.toString(trackId)}, WaypointsColumns._ID, 1)) { - if (cursor != null && cursor.moveToFirst()) { - return cursor.getLong(cursor.getColumnIndexOrThrow(WaypointsColumns._ID)); - } - } - return -1L; - } - /** * Gets the last waypoint for a type. Returns null if it doesn't exist. * - * @param trackId the track id + * @param trackId the track id */ public Waypoint getLastWaypoint(long trackId) { if (trackId < 0) { @@ -602,7 +583,7 @@ public class ContentProviderUtils { * Gets the next waypoint number for a type. * Returns -1 if not able to get the next waypoint number. * - * @param trackId the track id + * @param trackId the track id */ public int getNextWaypointNumber(long trackId) { if (trackId < 0) { @@ -677,6 +658,19 @@ public class ContentProviderUtils { return getWaypointCursor(null, selection, selectionArgs, WaypointsColumns._ID, maxWaypoints); } + @VisibleForTesting + public List getWaypoints(long trackId) { + ArrayList waypoints = new ArrayList<>(); + try (Cursor cursor = getWaypointCursor(trackId, -1L, -1)) { + if (cursor.moveToFirst()) { + do { + waypoints.add(createWaypoint(cursor)); + } while (cursor.moveToNext()); + } + } + return waypoints; + } + /** * Gets the number of waypoints for a track. * @@ -695,8 +689,7 @@ public class ContentProviderUtils { cursor.moveToFirst(); int count = cursor.getInt(0); cursor.close(); - // not count the first waypoint - return count > 0 ? count - 1 : 0; + return count; } /** @@ -894,23 +887,6 @@ public class ContentProviderUtils { return -1L; } - /** - * Gets the first valid location for a track. - * Returns null if it doesn't exist. - * - * @param trackId the track id - */ - public Location getFirstValidTrackPoint(long trackId) { - if (trackId < 0) { - return null; - } - String selection = TrackPointsColumns._ID + "=(select min(" + TrackPointsColumns._ID + ") from " - + TrackPointsColumns.TABLE_NAME + " WHERE " + TrackPointsColumns.TRACKID + "=? AND " - + TrackPointsColumns.LATITUDE + "<=" + MAX_LATITUDE + ")"; - String[] selectionArgs = new String[]{Long.toString(trackId)}; - return findTrackPointBy(selection, selectionArgs); - } - /** * Gets the last valid location for a track. * Returns null if it doesn't exist. @@ -1086,9 +1062,11 @@ public class ContentProviderUtils { values.put(TrackPointsColumns.LONGITUDE, (int) (location.getLongitude() * 1E6)); values.put(TrackPointsColumns.LATITUDE, (int) (location.getLatitude() * 1E6)); - // Hack for Samsung phones that don't properly populate the time field long time = location.getTime(); + // TODO: Check if this is really necessary! + // Hack for Samsung phones that don't properly populate the time field if (time == 0) { + Log.w(TAG, "location has no timestamp; setting current time"); time = System.currentTimeMillis(); } values.put(TrackPointsColumns.TIME, time); diff --git a/src/main/java/de/dennisguse/opentracks/io/file/exporter/FileTrackExporter.java b/src/main/java/de/dennisguse/opentracks/io/file/exporter/FileTrackExporter.java index c17731bf1..1d23dcaab 100644 --- a/src/main/java/de/dennisguse/opentracks/io/file/exporter/FileTrackExporter.java +++ b/src/main/java/de/dennisguse/opentracks/io/file/exporter/FileTrackExporter.java @@ -99,7 +99,6 @@ public class FileTrackExporter implements TrackExporter { boolean hasWaypoints = false; try (Cursor cursor = contentProviderUtils.getWaypointCursor(track.getId(), -1L, ContentProviderUtils.MAX_LOADED_WAYPOINTS_POINTS)) { if (cursor != null && cursor.moveToFirst()) { - // Intentionally skip first waypoint (contains statistics). for (int i = 0; i < cursor.getCount(); i++) { if (Thread.interrupted()) { throw new InterruptedException(); diff --git a/src/main/java/de/dennisguse/opentracks/io/file/importer/AbstractFileTrackImporter.java b/src/main/java/de/dennisguse/opentracks/io/file/importer/AbstractFileTrackImporter.java index 44c2d8225..b27f6782e 100644 --- a/src/main/java/de/dennisguse/opentracks/io/file/importer/AbstractFileTrackImporter.java +++ b/src/main/java/de/dennisguse/opentracks/io/file/importer/AbstractFileTrackImporter.java @@ -174,6 +174,7 @@ abstract class AbstractFileTrackImporter extends DefaultHandler implements Track return; } } + if (location == null) { if (!locationIterator.hasNext()) { // No more track points. Ignore the rest of the waypoints. @@ -183,9 +184,11 @@ abstract class AbstractFileTrackImporter extends DefaultHandler implements Track trackTripStatisticstrackUpdater.addLocation(location, recordingDistanceInterval); markerTripStatisticsUpdater.addLocation(location, recordingDistanceInterval); } + if (waypoint.getLocation().getTime() > location.getTime()) { location = null; } else if (waypoint.getLocation().getTime() < location.getTime()) { + Log.w(TAG, "Ignoring waypoint: current location was after waypoint."); waypoint = null; } else { // The waypoint location time matches the track point time diff --git a/src/main/java/de/dennisguse/opentracks/util/LocationUtils.java b/src/main/java/de/dennisguse/opentracks/util/LocationUtils.java index 72540c01a..43598a07c 100644 --- a/src/main/java/de/dennisguse/opentracks/util/LocationUtils.java +++ b/src/main/java/de/dennisguse/opentracks/util/LocationUtils.java @@ -164,6 +164,7 @@ public class LocationUtils { * @param location the location to test * @return true if the location is a valid location. */ + //TODO Still needed? public static boolean isValidLocation(Location location) { return location != null && Math.abs(location.getLatitude()) <= 90