From ef15632d7be0abd8587bbee2a478d95265356179 Mon Sep 17 00:00:00 2001 From: Dennis Guse Date: Sat, 8 Jan 2022 00:17:35 +0100 Subject: [PATCH] TrackRecordingService: refactored and unified tests. --- .../io/file/importer/ExportImportTest.java | 8 +- .../services/TrackRecordingServiceTest.java | 377 ------------------ .../TrackRecordingServiceTestMarker.java | 160 ++++++++ .../TrackRecordingServiceTestRecording.java | 221 ++++++++-- ...TrackRecordingServiceTestStateMachine.java | 30 +- .../TrackRecordingServiceTestStatistics.java | 46 ++- .../TrackRecordingServiceTestUtils.java | 52 +++ 7 files changed, 431 insertions(+), 463 deletions(-) delete mode 100644 src/androidTest/java/de/dennisguse/opentracks/services/TrackRecordingServiceTest.java create mode 100644 src/androidTest/java/de/dennisguse/opentracks/services/TrackRecordingServiceTestMarker.java create mode 100644 src/androidTest/java/de/dennisguse/opentracks/services/TrackRecordingServiceTestUtils.java 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 e24bf65ee..04cc70663 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 @@ -54,7 +54,7 @@ import de.dennisguse.opentracks.content.sensor.SensorDataSet; import de.dennisguse.opentracks.io.file.TrackFileFormat; import de.dennisguse.opentracks.io.file.exporter.TrackExporter; import de.dennisguse.opentracks.services.TrackRecordingService; -import de.dennisguse.opentracks.services.TrackRecordingServiceTest; +import de.dennisguse.opentracks.services.TrackRecordingServiceTestUtils; import de.dennisguse.opentracks.services.handlers.TrackPointCreator; import de.dennisguse.opentracks.services.sensors.AltitudeSumManager; import de.dennisguse.opentracks.services.sensors.BluetoothRemoteSensorManager; @@ -99,14 +99,14 @@ public class ExportImportTest { @Before public void fileSetup() throws IOException, TimeoutException { - TrackRecordingServiceTest.resetService(mServiceRule, context); + TrackRecordingServiceTestUtils.resetService(mServiceRule, context); tmpFile = File.createTempFile("test", "test", context.getFilesDir()); tmpFileUri = Uri.fromFile(tmpFile); trackImporter = new TrackImporter(context, contentProviderUtils, Distance.of(10), Distance.of(200), true); - TrackRecordingServiceTest.resetService(mServiceRule, context); + TrackRecordingServiceTestUtils.resetService(mServiceRule, context); } @After @@ -117,7 +117,7 @@ public class ExportImportTest { // Ensure that the database is empty after every test contentProviderUtils.deleteAllTracks(context); - TrackRecordingServiceTest.resetService(mServiceRule, context); + TrackRecordingServiceTestUtils.resetService(mServiceRule, context); } public void setUp() throws TimeoutException { diff --git a/src/androidTest/java/de/dennisguse/opentracks/services/TrackRecordingServiceTest.java b/src/androidTest/java/de/dennisguse/opentracks/services/TrackRecordingServiceTest.java deleted file mode 100644 index 97632442f..000000000 --- a/src/androidTest/java/de/dennisguse/opentracks/services/TrackRecordingServiceTest.java +++ /dev/null @@ -1,377 +0,0 @@ -/* - * Copyright 2010 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package de.dennisguse.opentracks.services; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - -import android.content.Context; -import android.content.Intent; -import android.os.Looper; - -import androidx.annotation.NonNull; -import androidx.test.core.app.ApplicationProvider; -import androidx.test.ext.junit.runners.AndroidJUnit4; -import androidx.test.filters.MediumTest; -import androidx.test.rule.GrantPermissionRule; -import androidx.test.rule.ServiceTestRule; - -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; - -import java.time.Clock; -import java.time.Instant; -import java.time.ZoneId; -import java.util.List; -import java.util.Random; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; - -import de.dennisguse.opentracks.R; -import de.dennisguse.opentracks.content.data.Distance; -import de.dennisguse.opentracks.content.data.Marker; -import de.dennisguse.opentracks.content.data.Speed; -import de.dennisguse.opentracks.content.data.TestDataUtil; -import de.dennisguse.opentracks.content.data.Track; -import de.dennisguse.opentracks.content.data.TrackPoint; -import de.dennisguse.opentracks.content.provider.ContentProviderUtils; -import de.dennisguse.opentracks.io.file.importer.TrackPointAssert; -import de.dennisguse.opentracks.services.handlers.TrackPointCreator; -import de.dennisguse.opentracks.services.sensors.AltitudeSumManager; -import de.dennisguse.opentracks.services.sensors.BluetoothRemoteSensorManager; -import de.dennisguse.opentracks.settings.PreferencesUtils; -import de.dennisguse.opentracks.stats.TrackStatistics; - -/** - * Tests for the track recording service. - * - * @author Bartlomiej Niechwiej - */ -@RunWith(AndroidJUnit4.class) -public class TrackRecordingServiceTest { - - @Rule - public final ServiceTestRule mServiceRule = ServiceTestRule.withTimeout(5, TimeUnit.SECONDS); - - @Rule - public GrantPermissionRule mRuntimePermissionRule = GrantPermissionRule.grant(android.Manifest.permission.ACCESS_FINE_LOCATION); - - @BeforeClass - public static void preSetUp() { - // Prepare looper for Android's message queue - if (Looper.myLooper() == null) Looper.prepare(); - } - - @AfterClass - public static void finalTearDown() { - if (Looper.myLooper() != null) Looper.myLooper().quit(); - } - - private final Context context = ApplicationProvider.getApplicationContext(); - private ContentProviderUtils contentProviderUtils; - - private final Track.Id trackId = new Track.Id(Math.abs(new Random().nextLong())); - - static Intent createStartIntent(Context context) { - return new Intent(context, TrackRecordingService.class); - } - - private final AltitudeSumManager altitudeSumManager = new AltitudeSumManager() { - @Override - public void fill(@NonNull TrackPoint trackPoint) { - trackPoint.setAltitudeGain(0f); - trackPoint.setAltitudeLoss(0f); - } - }; - - @Before - public void setUp() throws TimeoutException { - contentProviderUtils = new ContentProviderUtils(context); - - tearDown(); - } - - @After - public void tearDown() throws TimeoutException { - TrackRecordingServiceTest.resetService(mServiceRule, context); - - // Ensure that the database is empty after every test - contentProviderUtils.deleteAllTracks(context); - } - - @MediumTest - @Test - public void testRecording_stop() throws TimeoutException { - // given - TrackRecordingService service = ((TrackRecordingService.Binder) mServiceRule.bindService(createStartIntent(context))) - .getService(); - - TrackPointCreator trackPointCreator = service.getTrackPointCreator(); - - trackPointCreator.setClock(Clock.fixed(Instant.parse("2020-02-02T02:02:02Z"), ZoneId.of("CET"))); - Track.Id trackId = service.startNewTrack(); - service.stopUpdateRecordingData(); - trackPointCreator.stopGPS(); - trackPointCreator.setAltitudeSumManager(altitudeSumManager); - - // when - trackPointCreator.setClock(Clock.fixed(Instant.parse("2020-02-02T02:02:03Z"), ZoneId.of("CET"))); - service.endCurrentTrack(); - - // then - List trackPoints = TestDataUtil.getTrackPoints(contentProviderUtils, trackId); - - TrackPointAssert a = new TrackPointAssert(); - a.assertEquals(List.of( - new TrackPoint(TrackPoint.Type.SEGMENT_START_MANUAL, Instant.parse("2020-02-02T02:02:02Z")), - new TrackPoint(TrackPoint.Type.SEGMENT_END_MANUAL, Instant.parse("2020-02-02T02:02:03Z")) - .setAltitudeGain(0f) - .setAltitudeLoss(0f) - ), trackPoints); - } - - @MediumTest - @Test - public void testRecording_pauseAndResume() throws TimeoutException { - // given - TrackRecordingService service = ((TrackRecordingService.Binder) mServiceRule.bindService(createStartIntent(context))) - .getService(); - - TrackPointCreator trackPointCreator = service.getTrackPointCreator(); - - trackPointCreator.setClock(Clock.fixed(Instant.parse("2020-02-02T02:02:02Z"), ZoneId.of("CET"))); - Track.Id trackId = service.startNewTrack(); - service.stopUpdateRecordingData(); - trackPointCreator.stopGPS(); - trackPointCreator.setAltitudeSumManager(altitudeSumManager); - - // when - trackPointCreator.setClock(Clock.fixed(Instant.parse("2020-02-02T02:02:03Z"), ZoneId.of("CET"))); - service.pauseCurrentTrack(); - service.stopUpdateRecordingData(); - - // then - assertEquals(2, contentProviderUtils.getTrackPointCursor(trackId, null).getCount()); - - //when - trackPointCreator.setClock(Clock.fixed(Instant.parse("2020-02-02T02:02:04Z"), ZoneId.of("CET"))); - service.resumeTrack(trackId); - service.stopUpdateRecordingData(); - - // then - assertTrue(service.isRecording()); - - List trackPoints = TestDataUtil.getTrackPoints(contentProviderUtils, trackId); - TrackPointAssert a = new TrackPointAssert(); - a.assertEquals(List.of( - new TrackPoint(TrackPoint.Type.SEGMENT_START_MANUAL, Instant.parse("2020-02-02T02:02:02Z")), - new TrackPoint(TrackPoint.Type.SEGMENT_END_MANUAL, Instant.parse("2020-02-02T02:02:03Z")) - .setAltitudeGain(0f) - .setAltitudeLoss(0f), - new TrackPoint(TrackPoint.Type.SEGMENT_START_MANUAL, Instant.parse("2020-02-02T02:02:04Z")) - ), trackPoints); - } - - @MediumTest - @Test - public void testRecording_resumeStoppedTrack() throws TimeoutException { - // given - TrackRecordingService service = ((TrackRecordingService.Binder) mServiceRule.bindService(createStartIntent(context))) - .getService(); - - TrackPointCreator trackPointCreator = service.getTrackPointCreator(); - - trackPointCreator.setClock(Clock.fixed(Instant.parse("2020-02-02T02:02:02Z"), ZoneId.of("CET"))); - Track.Id trackId = service.startNewTrack(); - service.stopUpdateRecordingData(); - trackPointCreator.stopGPS(); - trackPointCreator.setAltitudeSumManager(altitudeSumManager); - - trackPointCreator.setClock(Clock.fixed(Instant.parse("2020-02-02T02:02:03Z"), ZoneId.of("CET"))); - service.endCurrentTrack(); - - // when - trackPointCreator.setClock(Clock.fixed(Instant.parse("2020-02-02T02:02:04Z"), ZoneId.of("CET"))); - service.resumeTrack(trackId); - trackPointCreator.stopGPS(); - trackPointCreator.setAltitudeSumManager(altitudeSumManager); - - trackPointCreator.onNewTrackPoint(new TrackPoint(TrackPoint.Type.TRACKPOINT, Instant.parse("2020-02-02T02:02:05Z"))); - - // then - assertTrue(service.isRecording()); - - List trackPoints = TestDataUtil.getTrackPoints(contentProviderUtils, trackId); - TrackPointAssert a = new TrackPointAssert(); - a.assertEquals(List.of( - new TrackPoint(TrackPoint.Type.SEGMENT_START_MANUAL, Instant.parse("2020-02-02T02:02:02Z")), - new TrackPoint(TrackPoint.Type.SEGMENT_END_MANUAL, Instant.parse("2020-02-02T02:02:03Z")) - .setAltitudeGain(0f) - .setAltitudeLoss(0f), - new TrackPoint(TrackPoint.Type.SEGMENT_START_MANUAL, Instant.parse("2020-02-02T02:02:04Z")), - new TrackPoint(TrackPoint.Type.TRACKPOINT, Instant.parse("2020-02-02T02:02:05Z")) - .setAltitudeGain(0f) - .setAltitudeLoss(0f) - ), trackPoints); - } - - @MediumTest - @Test - public void testRecording_stopPausedTrack() throws TimeoutException { - // given - TrackRecordingService service = ((TrackRecordingService.Binder) mServiceRule.bindService(createStartIntent(context))) - .getService(); - - TrackPointCreator trackPointCreator = service.getTrackPointCreator(); - - trackPointCreator.setClock(Clock.fixed(Instant.parse("2020-02-02T02:02:02Z"), ZoneId.of("CET"))); - Track.Id trackId = service.startNewTrack(); - service.stopUpdateRecordingData(); - trackPointCreator.stopGPS(); - trackPointCreator.setAltitudeSumManager(altitudeSumManager); - - trackPointCreator.setClock(Clock.fixed(Instant.parse("2020-02-02T02:02:03Z"), ZoneId.of("CET"))); - service.pauseCurrentTrack(); - - // when - trackPointCreator.setClock(Clock.fixed(Instant.parse("2020-02-02T02:02:04Z"), ZoneId.of("CET"))); - service.endCurrentTrack(); - - // then - assertFalse(service.isRecording()); - - List trackPoints = TestDataUtil.getTrackPoints(contentProviderUtils, trackId); - TrackPointAssert a = new TrackPointAssert(); - a.assertEquals(List.of( - new TrackPoint(TrackPoint.Type.SEGMENT_START_MANUAL, Instant.parse("2020-02-02T02:02:02Z")), - new TrackPoint(TrackPoint.Type.SEGMENT_END_MANUAL, Instant.parse("2020-02-02T02:02:03Z")) - .setAltitudeGain(0f) - .setAltitudeLoss(0f) - ), trackPoints); - } - - - @MediumTest - @Test - public void testInsertWaypointMarker_noRecordingTrack() throws TimeoutException { - // given - TrackRecordingService service = ((TrackRecordingService.Binder) mServiceRule.bindService(createStartIntent(context))) - .getService(); - assertFalse(service.isRecording()); - - // when - Marker.Id markerId = service.insertMarker(null, null, null, null); - - // then - assertNull(markerId); - } - - @MediumTest - @Test - public void testInsertWaypointMarker_validWaypoint() throws TimeoutException { - // given - TrackRecordingService service = ((TrackRecordingService.Binder) mServiceRule.bindService(createStartIntent(context))) - .getService(); - - TrackPointCreator trackPointCreator = service.getTrackPointCreator(); - trackPointCreator.stopGPS(); - - trackPointCreator.setClock(Clock.fixed(Instant.parse("2020-02-02T02:02:02Z"), ZoneId.of("CET"))); - Track.Id trackId = service.startNewTrack(); - service.stopUpdateRecordingData(); - - assertTrue(service.isRecording()); - trackPointCreator.onNewTrackPoint( - new TrackPoint(TrackPoint.Type.TRACKPOINT, Instant.parse("2020-02-02T02:02:03Z")) - .setLatitude(10) - .setLongitude(10) - ); - - // when - Marker.Id markerId = service.insertMarker(null, null, null, null); - - // then - assertNotEquals(new Marker.Id(-1L), markerId); - Marker wpt = contentProviderUtils.getMarker(markerId); - assertEquals(context.getString(R.string.marker_icon_url), wpt.getIcon()); - assertEquals(context.getString(R.string.marker_name_format, 1), wpt.getName()); - assertEquals(trackId, wpt.getTrackId()); - assertEquals(0.0, wpt.getLength().toM(), 0.01); - assertNotNull(wpt.getLocation()); - - service.endCurrentTrack(); - } - - private void addTrack(Track track) { - assertNotNull(track.getId()); - contentProviderUtils.insertTrack(track); - assertEquals(track.getId(), contentProviderUtils.getTrack(track.getId()).getId()); - } - - // NOTE: Do not use to create a track that is currently recording. - private void createDummyTrack(Track.Id id) { - Track dummyTrack = new Track(); - dummyTrack.setId(id); - dummyTrack.setName("Dummy Track"); - TrackStatistics trackStatistics = new TrackStatistics(); - Instant now = Instant.now(); - trackStatistics.setStartTime(now.minusSeconds(5L)); - trackStatistics.setStopTime(now.minusSeconds(1L)); - dummyTrack.setTrackStatistics(trackStatistics); - addTrack(dummyTrack); - } - - static void newTrackPoint(TrackRecordingService trackRecordingService, double latitude, double longitude, float accuracy, long speed) { - newTrackPoint(trackRecordingService, latitude, longitude, accuracy, speed, System.currentTimeMillis()); - } - - static void newTrackPoint(TrackRecordingService trackRecordingService, double latitude, double longitude, float accuracy, long speed, long time) { - TrackPoint trackPoint = new TrackPoint(TrackPoint.Type.TRACKPOINT, Instant.ofEpochMilli(time)) - .setLongitude(longitude) - .setLatitude(latitude) - .setHorizontalAccuracy(Distance.of(accuracy)) - .setSpeed(Speed.of(speed)) - .setBearing(3.0f); - - trackRecordingService.getTrackPointCreator().onNewTrackPoint(trackPoint); - } - - //TODO Workaround as service is not stopped on API23; thus sharedpreferences are not reset between tests. - //TODO Anyhow, the service should re-create all it's resources if a recording starts and makes sure that there is no leftovers from previous recordings. - @Deprecated - public static void resetService(ServiceTestRule mServiceRule, Context context) throws TimeoutException { - // Let's use default values. - PreferencesUtils.clear(); - - // Reset service (if some previous test failed) - TrackRecordingService service = ((TrackRecordingService.Binder) mServiceRule.bindService(new Intent(context, TrackRecordingService.class))) - .getService(); - - service.getTrackPointCreator().setRemoteSensorManager(new BluetoothRemoteSensorManager(context, service.getTrackPointCreator())); - service.getTrackPointCreator().setClock(Clock.systemUTC()); - service.endCurrentTrack(); - service.sharedPreferenceChangeListener.onSharedPreferenceChanged(null, null); - } -} diff --git a/src/androidTest/java/de/dennisguse/opentracks/services/TrackRecordingServiceTestMarker.java b/src/androidTest/java/de/dennisguse/opentracks/services/TrackRecordingServiceTestMarker.java new file mode 100644 index 000000000..4180dc90d --- /dev/null +++ b/src/androidTest/java/de/dennisguse/opentracks/services/TrackRecordingServiceTestMarker.java @@ -0,0 +1,160 @@ +/* + * Copyright 2010 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package de.dennisguse.opentracks.services; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import android.content.Context; +import android.content.Intent; +import android.os.Looper; + +import androidx.test.core.app.ApplicationProvider; +import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.filters.MediumTest; +import androidx.test.rule.GrantPermissionRule; +import androidx.test.rule.ServiceTestRule; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.time.Clock; +import java.time.Instant; +import java.time.ZoneId; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import de.dennisguse.opentracks.R; +import de.dennisguse.opentracks.content.data.Marker; +import de.dennisguse.opentracks.content.data.Track; +import de.dennisguse.opentracks.content.data.TrackPoint; +import de.dennisguse.opentracks.content.provider.ContentProviderUtils; +import de.dennisguse.opentracks.services.handlers.TrackPointCreator; + +@RunWith(AndroidJUnit4.class) +public class TrackRecordingServiceTestMarker { + + @Rule + public final ServiceTestRule mServiceRule = ServiceTestRule.withTimeout(5, TimeUnit.SECONDS); + + @Rule + public GrantPermissionRule mRuntimePermissionRule = GrantPermissionRule.grant(android.Manifest.permission.ACCESS_FINE_LOCATION); + + private final Context context = ApplicationProvider.getApplicationContext(); + private ContentProviderUtils contentProviderUtils; + + private TrackRecordingService service; + + @BeforeClass + public static void preSetUp() { + // Prepare looper for Android's message queue + if (Looper.myLooper() == null) Looper.prepare(); + } + + @AfterClass + public static void finalTearDown() { + if (Looper.myLooper() != null) Looper.myLooper().quit(); + } + + private TrackRecordingService startService() throws TimeoutException { + Intent startIntent = new Intent(context, TrackRecordingService.class); + return ((TrackRecordingService.Binder) mServiceRule.bindService(startIntent)) + .getService(); + } + + @Before + public void setUp() throws TimeoutException { + contentProviderUtils = new ContentProviderUtils(context); + service = startService(); + tearDown(); + } + + @After + public void tearDown() throws TimeoutException { + TrackRecordingServiceTestUtils.resetService(mServiceRule, context); + + // Ensure that the database is empty after every test + contentProviderUtils.deleteAllTracks(context); + } + + @MediumTest + @Test + public void notRecording_testInsertMarker() { + // given + assertFalse(service.isRecording()); + + // when + Marker.Id markerId = service.insertMarker(null, null, null, null); + + // then + assertNull(markerId); + } + + @MediumTest + @Test + public void recording_noGPSfix_cannotCreateMarker() { + // given + service.startNewTrack(); + + // when + Marker.Id markerId = service.insertMarker(null, null, null, null); + + // then + assertNull(markerId); + } + + @MediumTest + @Test + public void recording_GPSfix_createsMarker() { + // given + TrackPointCreator trackPointCreator = service.getTrackPointCreator(); + trackPointCreator.stopGPS(); + + trackPointCreator.setClock(Clock.fixed(Instant.parse("2020-02-02T02:02:02Z"), ZoneId.of("CET"))); + Track.Id trackId = service.startNewTrack(); + service.stopUpdateRecordingData(); + + assertTrue(service.isRecording()); + trackPointCreator.onNewTrackPoint( + new TrackPoint(TrackPoint.Type.TRACKPOINT, Instant.parse("2020-02-02T02:02:03Z")) + .setLatitude(10) + .setLongitude(10) + ); + + // when + Marker.Id markerId = service.insertMarker(null, null, null, null); + + // then + assertNotEquals(new Marker.Id(-1L), markerId); + Marker wpt = contentProviderUtils.getMarker(markerId); + assertEquals(context.getString(R.string.marker_icon_url), wpt.getIcon()); + assertEquals(context.getString(R.string.marker_name_format, 1), wpt.getName()); + assertEquals(trackId, wpt.getTrackId()); + assertEquals(0.0, wpt.getLength().toM(), 0.01); + assertNotNull(wpt.getLocation()); + + service.endCurrentTrack(); + } +} diff --git a/src/androidTest/java/de/dennisguse/opentracks/services/TrackRecordingServiceTestRecording.java b/src/androidTest/java/de/dennisguse/opentracks/services/TrackRecordingServiceTestRecording.java index f3ae9dc1d..f35155613 100644 --- a/src/androidTest/java/de/dennisguse/opentracks/services/TrackRecordingServiceTestRecording.java +++ b/src/androidTest/java/de/dennisguse/opentracks/services/TrackRecordingServiceTestRecording.java @@ -1,6 +1,8 @@ package de.dennisguse.opentracks.services; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import android.content.Context; import android.content.Intent; @@ -21,6 +23,9 @@ import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; +import java.time.Clock; +import java.time.Instant; +import java.time.ZoneId; import java.util.List; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; @@ -35,6 +40,7 @@ import de.dennisguse.opentracks.content.sensor.SensorDataHeartRate; import de.dennisguse.opentracks.content.sensor.SensorDataRunning; import de.dennisguse.opentracks.content.sensor.SensorDataSet; import de.dennisguse.opentracks.io.file.importer.TrackPointAssert; +import de.dennisguse.opentracks.services.handlers.TrackPointCreator; import de.dennisguse.opentracks.services.sensors.AltitudeSumManager; import de.dennisguse.opentracks.services.sensors.BluetoothRemoteSensorManager; @@ -91,11 +97,150 @@ public class TrackRecordingServiceTestRecording { @After public void tearDown() throws TimeoutException { - TrackRecordingServiceTest.resetService(mServiceRule, context); + TrackRecordingServiceTestUtils.resetService(mServiceRule, context); // Ensure that the database is empty after every test contentProviderUtils.deleteAllTracks(context); } + @MediumTest + @Test + public void testRecording_stop() { + // given + TrackPointCreator trackPointCreator = service.getTrackPointCreator(); + + trackPointCreator.setClock(Clock.fixed(Instant.parse("2020-02-02T02:02:02Z"), ZoneId.of("CET"))); + Track.Id trackId = service.startNewTrack(); + service.stopUpdateRecordingData(); + trackPointCreator.stopGPS(); + trackPointCreator.setAltitudeSumManager(altitudeSumManager); + + // when + trackPointCreator.setClock(Clock.fixed(Instant.parse("2020-02-02T02:02:03Z"), ZoneId.of("CET"))); + service.endCurrentTrack(); + + // then + List trackPoints = TestDataUtil.getTrackPoints(contentProviderUtils, trackId); + + TrackPointAssert a = new TrackPointAssert(); + a.assertEquals(List.of( + new TrackPoint(TrackPoint.Type.SEGMENT_START_MANUAL, Instant.parse("2020-02-02T02:02:02Z")), + new TrackPoint(TrackPoint.Type.SEGMENT_END_MANUAL, Instant.parse("2020-02-02T02:02:03Z")) + .setAltitudeGain(0f) + .setAltitudeLoss(0f) + ), trackPoints); + } + + @MediumTest + @Test + public void testRecording_pauseAndResume() { + // given + TrackPointCreator trackPointCreator = service.getTrackPointCreator(); + + trackPointCreator.setClock(Clock.fixed(Instant.parse("2020-02-02T02:02:02Z"), ZoneId.of("CET"))); + Track.Id trackId = service.startNewTrack(); + service.stopUpdateRecordingData(); + trackPointCreator.stopGPS(); + trackPointCreator.setAltitudeSumManager(altitudeSumManager); + + // when + trackPointCreator.setClock(Clock.fixed(Instant.parse("2020-02-02T02:02:03Z"), ZoneId.of("CET"))); + service.pauseCurrentTrack(); + service.stopUpdateRecordingData(); + + // then + assertEquals(2, contentProviderUtils.getTrackPointCursor(trackId, null).getCount()); + + //when + trackPointCreator.setClock(Clock.fixed(Instant.parse("2020-02-02T02:02:04Z"), ZoneId.of("CET"))); + service.resumeTrack(trackId); + service.stopUpdateRecordingData(); + + // then + assertTrue(service.isRecording()); + + List trackPoints = TestDataUtil.getTrackPoints(contentProviderUtils, trackId); + TrackPointAssert a = new TrackPointAssert(); + a.assertEquals(List.of( + new TrackPoint(TrackPoint.Type.SEGMENT_START_MANUAL, Instant.parse("2020-02-02T02:02:02Z")), + new TrackPoint(TrackPoint.Type.SEGMENT_END_MANUAL, Instant.parse("2020-02-02T02:02:03Z")) + .setAltitudeGain(0f) + .setAltitudeLoss(0f), + new TrackPoint(TrackPoint.Type.SEGMENT_START_MANUAL, Instant.parse("2020-02-02T02:02:04Z")) + ), trackPoints); + } + + @MediumTest + @Test + public void testRecording_resumeStoppedTrack() { + // given + TrackPointCreator trackPointCreator = service.getTrackPointCreator(); + + trackPointCreator.setClock(Clock.fixed(Instant.parse("2020-02-02T02:02:02Z"), ZoneId.of("CET"))); + Track.Id trackId = service.startNewTrack(); + service.stopUpdateRecordingData(); + trackPointCreator.stopGPS(); + trackPointCreator.setAltitudeSumManager(altitudeSumManager); + + trackPointCreator.setClock(Clock.fixed(Instant.parse("2020-02-02T02:02:03Z"), ZoneId.of("CET"))); + service.endCurrentTrack(); + + // when + trackPointCreator.setClock(Clock.fixed(Instant.parse("2020-02-02T02:02:04Z"), ZoneId.of("CET"))); + service.resumeTrack(trackId); + trackPointCreator.stopGPS(); + trackPointCreator.setAltitudeSumManager(altitudeSumManager); + + trackPointCreator.onNewTrackPoint(new TrackPoint(TrackPoint.Type.TRACKPOINT, Instant.parse("2020-02-02T02:02:05Z"))); + + // then + assertTrue(service.isRecording()); + + List trackPoints = TestDataUtil.getTrackPoints(contentProviderUtils, trackId); + TrackPointAssert a = new TrackPointAssert(); + a.assertEquals(List.of( + new TrackPoint(TrackPoint.Type.SEGMENT_START_MANUAL, Instant.parse("2020-02-02T02:02:02Z")), + new TrackPoint(TrackPoint.Type.SEGMENT_END_MANUAL, Instant.parse("2020-02-02T02:02:03Z")) + .setAltitudeGain(0f) + .setAltitudeLoss(0f), + new TrackPoint(TrackPoint.Type.SEGMENT_START_MANUAL, Instant.parse("2020-02-02T02:02:04Z")), + new TrackPoint(TrackPoint.Type.TRACKPOINT, Instant.parse("2020-02-02T02:02:05Z")) + .setAltitudeGain(0f) + .setAltitudeLoss(0f) + ), trackPoints); + } + + @MediumTest + @Test + public void testRecording_stopPausedTrack() { + // given + TrackPointCreator trackPointCreator = service.getTrackPointCreator(); + + trackPointCreator.setClock(Clock.fixed(Instant.parse("2020-02-02T02:02:02Z"), ZoneId.of("CET"))); + Track.Id trackId = service.startNewTrack(); + service.stopUpdateRecordingData(); + trackPointCreator.stopGPS(); + trackPointCreator.setAltitudeSumManager(altitudeSumManager); + + trackPointCreator.setClock(Clock.fixed(Instant.parse("2020-02-02T02:02:03Z"), ZoneId.of("CET"))); + service.pauseCurrentTrack(); + + // when + trackPointCreator.setClock(Clock.fixed(Instant.parse("2020-02-02T02:02:04Z"), ZoneId.of("CET"))); + service.endCurrentTrack(); + + // then + assertFalse(service.isRecording()); + + List trackPoints = TestDataUtil.getTrackPoints(contentProviderUtils, trackId); + TrackPointAssert a = new TrackPointAssert(); + a.assertEquals(List.of( + new TrackPoint(TrackPoint.Type.SEGMENT_START_MANUAL, Instant.parse("2020-02-02T02:02:02Z")), + new TrackPoint(TrackPoint.Type.SEGMENT_END_MANUAL, Instant.parse("2020-02-02T02:02:03Z")) + .setAltitudeGain(0f) + .setAltitudeLoss(0f) + ), trackPoints); + } + @MediumTest @Test public void testOnLocationChangedAsync_movingAccurate() { @@ -105,12 +250,12 @@ public class TrackRecordingServiceTestRecording { service.getTrackPointCreator().setAltitudeSumManager(altitudeSumManager); // when - TrackRecordingServiceTest.newTrackPoint(service, 45.0, 35.0, 1, 15); - TrackRecordingServiceTest.newTrackPoint(service, 45.0001, 35.0, 2, 15); - TrackRecordingServiceTest.newTrackPoint(service, 45.0002, 35.0, 3, 15); - TrackRecordingServiceTest.newTrackPoint(service, 45.0003, 35.0, 4, 15); - TrackRecordingServiceTest.newTrackPoint(service, 45.0004, 35.0, 5, 15); - TrackRecordingServiceTest.newTrackPoint(service, 45.0005, 35.0, 6, 15); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0, 35.0, 1, 15); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0001, 35.0, 2, 15); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0002, 35.0, 3, 15); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0003, 35.0, 4, 15); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0004, 35.0, 5, 15); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0005, 35.0, 6, 15); service.endCurrentTrack(); @@ -179,12 +324,12 @@ public class TrackRecordingServiceTestRecording { service.getTrackPointCreator().setAltitudeSumManager(altitudeSumManager); // when - TrackRecordingServiceTest.newTrackPoint(service, 45.0, 35.0, 1, 15); - TrackRecordingServiceTest.newTrackPoint(service, 45.000001, 35.0, 2, 15); - TrackRecordingServiceTest.newTrackPoint(service, 45.000002, 35.0, 3, 15); - TrackRecordingServiceTest.newTrackPoint(service, 45.000003, 35.0, 4, 15); - TrackRecordingServiceTest.newTrackPoint(service, 45.000004, 35.0, 5, 15); - TrackRecordingServiceTest.newTrackPoint(service, 45.000005, 35.0, 6, 15); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0, 35.0, 1, 15); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.000001, 35.0, 2, 15); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.000002, 35.0, 3, 15); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.000003, 35.0, 4, 15); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.000004, 35.0, 5, 15); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.000005, 35.0, 6, 15); service.endCurrentTrack(); @@ -225,12 +370,12 @@ public class TrackRecordingServiceTestRecording { service.getTrackPointCreator().setAltitudeSumManager(altitudeSumManager); // when - TrackRecordingServiceTest.newTrackPoint(service, 45.0, 35.0, 1, 0); - TrackRecordingServiceTest.newTrackPoint(service, 45.0, 35.0, 2, 0); - TrackRecordingServiceTest.newTrackPoint(service, 45.0, 35.0, 3, 0); - TrackRecordingServiceTest.newTrackPoint(service, 45.0, 35.0, 4, 0); - TrackRecordingServiceTest.newTrackPoint(service, 45.0, 35.0, 5, 0); - TrackRecordingServiceTest.newTrackPoint(service, 45.0, 35.0, 6, 0); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0, 35.0, 1, 0); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0, 35.0, 2, 0); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0, 35.0, 3, 0); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0, 35.0, 4, 0); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0, 35.0, 5, 0); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0, 35.0, 6, 0); service.endCurrentTrack(); @@ -272,12 +417,12 @@ public class TrackRecordingServiceTestRecording { service.getTrackPointCreator().stopGPS(); // when - TrackRecordingServiceTest.newTrackPoint(service, 45.0, 35.0, 1, 15); - TrackRecordingServiceTest.newTrackPoint(service, 45.0, 35.0, 2, 0); - TrackRecordingServiceTest.newTrackPoint(service, 45.0, 35.0, 3, 0); // will be ignored - TrackRecordingServiceTest.newTrackPoint(service, 45.0, 35.0, 4, 0); // will be ignored - TrackRecordingServiceTest.newTrackPoint(service, 45.0, 35.0, 5, 0); - TrackRecordingServiceTest.newTrackPoint(service, 45.0, 35.0, 6, 15); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0, 35.0, 1, 15); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0, 35.0, 2, 0); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0, 35.0, 3, 0); // will be ignored + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0, 35.0, 4, 0); // will be ignored + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0, 35.0, 5, 0); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0, 35.0, 6, 15); service.endCurrentTrack(); @@ -347,12 +492,12 @@ public class TrackRecordingServiceTestRecording { }); // when - TrackRecordingServiceTest.newTrackPoint(service, 45.0, 35.0, 1, 0); - TrackRecordingServiceTest.newTrackPoint(service, 45.0, 35.0, 2, 0); - TrackRecordingServiceTest.newTrackPoint(service, 45.0, 35.0, 3, 0); - TrackRecordingServiceTest.newTrackPoint(service, 45.0, 35.0, 4, 0); - TrackRecordingServiceTest.newTrackPoint(service, 45.0, 35.0, 5, 0); - TrackRecordingServiceTest.newTrackPoint(service, 45.0, 35.0, 6, 0); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0, 35.0, 1, 0); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0, 35.0, 2, 0); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0, 35.0, 3, 0); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0, 35.0, 4, 0); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0, 35.0, 5, 0); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0, 35.0, 6, 0); service.endCurrentTrack(); @@ -413,7 +558,7 @@ public class TrackRecordingServiceTestRecording { altitudeSumManager.addAltitudeLoss_m(6f); remoteSensorManager.onChanged(new SensorDataRunning("", "", Speed.of(5), null, Distance.of(0))); //Should be ignored remoteSensorManager.onChanged(new SensorDataRunning("", "", Speed.of(5), null, Distance.of(2))); //TODO Should be ignored; distance will be added to TrackPoint - TrackRecordingServiceTest.newTrackPoint(service, 45.0, 35.0, 1, 15); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0, 35.0, 1, 15); remoteSensorManager.onChanged(new SensorDataRunning("", "", Speed.of(5), null, Distance.of(12))); @@ -422,7 +567,7 @@ public class TrackRecordingServiceTestRecording { altitudeSumManager.addAltitudeGain_m(6f); altitudeSumManager.addAltitudeLoss_m(6f); remoteSensorManager.onChanged(new SensorDataRunning("", "", Speed.of(5), null, Distance.of(14))); //Should be ignored - TrackRecordingServiceTest.newTrackPoint(service, 45.0, 35.0, 4, 15); //Should be ignored + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0, 35.0, 4, 15); //Should be ignored altitudeSumManager.addAltitudeGain_m(6f); altitudeSumManager.addAltitudeLoss_m(6f); @@ -477,11 +622,11 @@ public class TrackRecordingServiceTestRecording { service.getTrackPointCreator().setAltitudeSumManager(altitudeSumManager); // when - TrackRecordingServiceTest.newTrackPoint(service, 45.0, 35.0, 1, 0); - TrackRecordingServiceTest.newTrackPoint(service, 45.1, 35.0, 2, 0); - TrackRecordingServiceTest.newTrackPoint(service, 45.1, 35.0, 3, 0); - TrackRecordingServiceTest.newTrackPoint(service, 45.2, 35.0, 4, 0); - TrackRecordingServiceTest.newTrackPoint(service, 45.2, 35.0, 5, 0); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0, 35.0, 1, 0); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.1, 35.0, 2, 0); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.1, 35.0, 3, 0); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.2, 35.0, 4, 0); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.2, 35.0, 5, 0); service.endCurrentTrack(); diff --git a/src/androidTest/java/de/dennisguse/opentracks/services/TrackRecordingServiceTestStateMachine.java b/src/androidTest/java/de/dennisguse/opentracks/services/TrackRecordingServiceTestStateMachine.java index 6739ad044..2bf8fbf3f 100644 --- a/src/androidTest/java/de/dennisguse/opentracks/services/TrackRecordingServiceTestStateMachine.java +++ b/src/androidTest/java/de/dennisguse/opentracks/services/TrackRecordingServiceTestStateMachine.java @@ -41,7 +41,6 @@ import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; -import java.time.Clock; import java.util.List; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; @@ -49,8 +48,6 @@ import java.util.concurrent.TimeoutException; import de.dennisguse.opentracks.content.data.Track; import de.dennisguse.opentracks.content.provider.ContentProviderUtils; import de.dennisguse.opentracks.services.handlers.GpsStatusValue; -import de.dennisguse.opentracks.services.sensors.BluetoothRemoteSensorManager; -import de.dennisguse.opentracks.settings.PreferencesUtils; /** * Testing the states of TrackRecordingService. @@ -65,6 +62,11 @@ public class TrackRecordingServiceTestStateMachine { @Rule public GrantPermissionRule mRuntimePermissionRule = GrantPermissionRule.grant(android.Manifest.permission.ACCESS_FINE_LOCATION); + private final Context context = ApplicationProvider.getApplicationContext(); + private ContentProviderUtils contentProviderUtils; + + private TrackRecordingService service; + @BeforeClass public static void preSetUp() { // Prepare looper for Android's message queue @@ -76,9 +78,6 @@ public class TrackRecordingServiceTestStateMachine { if (Looper.myLooper() != null) Looper.myLooper().quit(); } - private TrackRecordingService service; - private final Context context = ApplicationProvider.getApplicationContext(); - private ContentProviderUtils contentProviderUtils; private TrackRecordingService startService() throws TimeoutException { Intent startIntent = new Intent(context, TrackRecordingService.class); @@ -95,7 +94,7 @@ public class TrackRecordingServiceTestStateMachine { @After public void tearDown() throws TimeoutException { - TrackRecordingServiceTestStateMachine.resetService(mServiceRule, context); + TrackRecordingServiceTestUtils.resetService(mServiceRule, context); // Ensure that the database is empty after every test contentProviderUtils.deleteAllTracks(context); } @@ -280,21 +279,4 @@ public class TrackRecordingServiceTestStateMachine { assertNotNull(trackId); assertNull(newTrackId); } - - //TODO Workaround as service is not stopped on API23; thus sharedpreferences are not reset between tests. - //TODO Anyhow, the service should re-create all it's resources if a recording starts and makes sure that there is no leftovers from previous recordings. - @Deprecated - public static void resetService(ServiceTestRule mServiceRule, Context context) throws TimeoutException { - // Let's use default values. - PreferencesUtils.clear(); - - // Reset service (if some previous test failed) - TrackRecordingService service = ((TrackRecordingService.Binder) mServiceRule.bindService(new Intent(context, TrackRecordingService.class))) - .getService(); - - service.getTrackPointCreator().setRemoteSensorManager(new BluetoothRemoteSensorManager(context, service.getTrackPointCreator())); - service.getTrackPointCreator().setClock(Clock.systemUTC()); - service.endCurrentTrack(); - service.sharedPreferenceChangeListener.onSharedPreferenceChanged(null, null); - } } diff --git a/src/androidTest/java/de/dennisguse/opentracks/services/TrackRecordingServiceTestStatistics.java b/src/androidTest/java/de/dennisguse/opentracks/services/TrackRecordingServiceTestStatistics.java index f366fa924..b410b995f 100644 --- a/src/androidTest/java/de/dennisguse/opentracks/services/TrackRecordingServiceTestStatistics.java +++ b/src/androidTest/java/de/dennisguse/opentracks/services/TrackRecordingServiceTestStatistics.java @@ -4,6 +4,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import android.content.Context; +import android.content.Intent; import android.os.Looper; import androidx.annotation.NonNull; @@ -73,16 +74,23 @@ public class TrackRecordingServiceTestStatistics { if (Looper.myLooper() != null) Looper.myLooper().quit(); } + private TrackRecordingService startService() throws TimeoutException { + Intent startIntent = new Intent(context, TrackRecordingService.class); + return ((TrackRecordingService.Binder) mServiceRule.bindService(startIntent)) + .getService(); + } + @Before public void setUp() throws TimeoutException { contentProviderUtils = new ContentProviderUtils(context); + service = startService(); tearDown(); } @After public void tearDown() throws TimeoutException { - TrackRecordingServiceTest.resetService(mServiceRule, context); + TrackRecordingServiceTestUtils.resetService(mServiceRule, context); contentProviderUtils.deleteAllTracks(context); } @@ -93,8 +101,6 @@ public class TrackRecordingServiceTestStatistics { @Test public void movingtime_with_pauses() throws TimeoutException { // given - TrackRecordingService service = ((TrackRecordingService.Binder) mServiceRule.bindService(TrackRecordingServiceTest.createStartIntent(context))) - .getService(); service.getTrackPointCreator().stopGPS(); service.getTrackPointCreator().setClock(Clock.fixed(Instant.ofEpochMilli(0), ZoneId.systemDefault())); @@ -117,37 +123,37 @@ public class TrackRecordingServiceTestStatistics { // when / then int movingtime_s = 0; - TrackRecordingServiceTest.newTrackPoint(service, 45.0, 35.0, 1, 15, 5 * 60000); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0, 35.0, 1, 15, 5 * 60000); assertMovingTime.apply(movingtime_s); - TrackRecordingServiceTest.newTrackPoint(service, 45.0001, 35.0, 2, 15, 6 * 60000); - TrackRecordingServiceTest.newTrackPoint(service, 45.0002, 35.0, 2, 15, (long) (6.5 * 60000)); - TrackRecordingServiceTest.newTrackPoint(service, 45.0003, 35.0, 2, 15, 7 * 60000); - TrackRecordingServiceTest.newTrackPoint(service, 45.0004, 35.0, 2, 15, 8 * 60000); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0001, 35.0, 2, 15, 6 * 60000); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0002, 35.0, 2, 15, (long) (6.5 * 60000)); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0003, 35.0, 2, 15, 7 * 60000); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0004, 35.0, 2, 15, 8 * 60000); movingtime_s += 3 * 60; assertMovingTime.apply(movingtime_s); - TrackRecordingServiceTest.newTrackPoint(service, 45.0004, 35.0, 2, 0, 9 * 60000); //will be ignored - TrackRecordingServiceTest.newTrackPoint(service, 45.0004, 35.0, 2, 0, 10 * 60000); //will be ignored - TrackRecordingServiceTest.newTrackPoint(service, 45.0004, 35.0, 2, 0, 11 * 60000); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0004, 35.0, 2, 0, 9 * 60000); //will be ignored + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0004, 35.0, 2, 0, 10 * 60000); //will be ignored + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0004, 35.0, 2, 0, 11 * 60000); assertMovingTime.apply(movingtime_s); - TrackRecordingServiceTest.newTrackPoint(service, 45.0004, 35.0, 2, 15, 13 * 60000); - TrackRecordingServiceTest.newTrackPoint(service, 45.0004, 35.0, 2, 15, (long) (13.5 * 60000)); //will be ignored - TrackRecordingServiceTest.newTrackPoint(service, 45.0005, 35.0, 2, 15, 14 * 60000); - TrackRecordingServiceTest.newTrackPoint(service, 45.0006, 35.0, 2, 15, 15 * 60000); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0004, 35.0, 2, 15, 13 * 60000); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0004, 35.0, 2, 15, (long) (13.5 * 60000)); //will be ignored + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0005, 35.0, 2, 15, 14 * 60000); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0006, 35.0, 2, 15, 15 * 60000); movingtime_s += 2 * 60; assertMovingTime.apply(movingtime_s); assertTotalTime.apply(15 * 60); - TrackRecordingServiceTest.newTrackPoint(service, 45.0006, 35.0, 2, 0, 16 * 60000); //will be ignored + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0006, 35.0, 2, 0, 16 * 60000); //will be ignored assertMovingTime.apply(movingtime_s); - TrackRecordingServiceTest.newTrackPoint(service, 45.0015, 35.0, 2, 0, 17 * 60000); - TrackRecordingServiceTest.newTrackPoint(service, 45.0016, 35.0, 2, 15, 18 * 60000); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0015, 35.0, 2, 0, 17 * 60000); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0016, 35.0, 2, 15, 18 * 60000); assertMovingTime.apply(movingtime_s); - TrackRecordingServiceTest.newTrackPoint(service, 45.0016, 35.0, 2, 0, 19 * 60000); //TODO we could ignore this TrackPoint + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0016, 35.0, 2, 0, 19 * 60000); //TODO we could ignore this TrackPoint assertMovingTime.apply(movingtime_s); assertTotalTime.apply(19 * 60); @@ -158,7 +164,7 @@ public class TrackRecordingServiceTestStatistics { service.getTrackPointCreator().setClock(Clock.fixed(Instant.ofEpochSecond(41 * 60), ZoneId.systemDefault())); service.resumeCurrentTrack(); - TrackRecordingServiceTest.newTrackPoint(service, 45.0016, 35.0, 2, 15, 42 * 60000); + TrackRecordingServiceTestUtils.newTrackPoint(service, 45.0016, 35.0, 2, 15, 42 * 60000); assertMovingTime.apply(movingtime_s); assertTotalTime.apply(41 * 60); diff --git a/src/androidTest/java/de/dennisguse/opentracks/services/TrackRecordingServiceTestUtils.java b/src/androidTest/java/de/dennisguse/opentracks/services/TrackRecordingServiceTestUtils.java new file mode 100644 index 000000000..798006d43 --- /dev/null +++ b/src/androidTest/java/de/dennisguse/opentracks/services/TrackRecordingServiceTestUtils.java @@ -0,0 +1,52 @@ +package de.dennisguse.opentracks.services; + +import android.content.Context; +import android.content.Intent; + +import androidx.test.rule.ServiceTestRule; + +import java.time.Clock; +import java.time.Instant; +import java.util.concurrent.TimeoutException; + +import de.dennisguse.opentracks.content.data.Distance; +import de.dennisguse.opentracks.content.data.Speed; +import de.dennisguse.opentracks.content.data.TrackPoint; +import de.dennisguse.opentracks.services.sensors.BluetoothRemoteSensorManager; +import de.dennisguse.opentracks.settings.PreferencesUtils; + +public class TrackRecordingServiceTestUtils { + + + //TODO Workaround as service is not stopped on API23; thus sharedpreferences are not reset between tests. + //TODO Anyhow, the service should re-create all it's resources if a recording starts and makes sure that there is no leftovers from previous recordings. + @Deprecated + public static void resetService(ServiceTestRule mServiceRule, Context context) throws TimeoutException { + // Let's use default values. + PreferencesUtils.clear(); + + // Reset service (if some previous test failed) + TrackRecordingService service = ((TrackRecordingService.Binder) mServiceRule.bindService(new Intent(context, TrackRecordingService.class))) + .getService(); + + service.getTrackPointCreator().setRemoteSensorManager(new BluetoothRemoteSensorManager(context, service.getTrackPointCreator())); + service.getTrackPointCreator().setClock(Clock.systemUTC()); + service.endCurrentTrack(); + service.sharedPreferenceChangeListener.onSharedPreferenceChanged(null, null); + } + + static void newTrackPoint(TrackRecordingService trackRecordingService, double latitude, double longitude, float accuracy, long speed) { + newTrackPoint(trackRecordingService, latitude, longitude, accuracy, speed, System.currentTimeMillis()); + } + + static void newTrackPoint(TrackRecordingService trackRecordingService, double latitude, double longitude, float accuracy, long speed, long time) { + TrackPoint trackPoint = new TrackPoint(TrackPoint.Type.TRACKPOINT, Instant.ofEpochMilli(time)) + .setLongitude(longitude) + .setLatitude(latitude) + .setHorizontalAccuracy(Distance.of(accuracy)) + .setSpeed(Speed.of(speed)) + .setBearing(3.0f); + + trackRecordingService.getTrackPointCreator().onNewTrackPoint(trackPoint); + } +}