forked from upstream-mirrors/OpenTracks
Tests: ServiceTestRule does not (always) restart the service.
Thus, resetting it's status is not happening and then leading to failures in subsequent tests. Fixes #1058.
This commit is contained in:
+11
-15
@@ -55,6 +55,7 @@ import de.dennisguse.opentracks.content.sensor.SensorDataSet;
|
|||||||
import de.dennisguse.opentracks.io.file.TrackFileFormat;
|
import de.dennisguse.opentracks.io.file.TrackFileFormat;
|
||||||
import de.dennisguse.opentracks.io.file.exporter.TrackExporter;
|
import de.dennisguse.opentracks.io.file.exporter.TrackExporter;
|
||||||
import de.dennisguse.opentracks.services.TrackRecordingService;
|
import de.dennisguse.opentracks.services.TrackRecordingService;
|
||||||
|
import de.dennisguse.opentracks.services.TrackRecordingServiceTest;
|
||||||
import de.dennisguse.opentracks.services.handlers.TrackPointCreator;
|
import de.dennisguse.opentracks.services.handlers.TrackPointCreator;
|
||||||
import de.dennisguse.opentracks.services.sensors.AltitudeSumManager;
|
import de.dennisguse.opentracks.services.sensors.AltitudeSumManager;
|
||||||
import de.dennisguse.opentracks.services.sensors.BluetoothRemoteSensorManager;
|
import de.dennisguse.opentracks.services.sensors.BluetoothRemoteSensorManager;
|
||||||
@@ -98,21 +99,26 @@ public class ExportImportTest {
|
|||||||
private TrackImporter trackImporter;
|
private TrackImporter trackImporter;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void fileSetup() throws IOException {
|
public void fileSetup() throws IOException, TimeoutException {
|
||||||
|
TrackRecordingServiceTest.resetService(mServiceRule, context);
|
||||||
|
|
||||||
tmpFile = File.createTempFile("test", "test", context.getFilesDir());
|
tmpFile = File.createTempFile("test", "test", context.getFilesDir());
|
||||||
tmpFileUri = Uri.fromFile(tmpFile);
|
tmpFileUri = Uri.fromFile(tmpFile);
|
||||||
|
|
||||||
trackImporter = new TrackImporter(context, contentProviderUtils, Distance.of(10), Distance.of(200), true);
|
trackImporter = new TrackImporter(context, contentProviderUtils, Distance.of(10), Distance.of(200), true);
|
||||||
|
|
||||||
|
TrackRecordingServiceTest.resetService(mServiceRule, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void FileTearDown() throws TimeoutException {
|
public void tearDown() throws TimeoutException {
|
||||||
tmpFile.deleteOnExit();
|
tmpFile.deleteOnExit();
|
||||||
tmpFileUri = null;
|
tmpFileUri = null;
|
||||||
|
|
||||||
TrackRecordingService service = ((TrackRecordingService.Binder) mServiceRule.bindService(new Intent(context, TrackRecordingService.class)))
|
// Ensure that the database is empty after every test
|
||||||
.getService();
|
contentProviderUtils.deleteAllTracks(context);
|
||||||
service.getTrackPointCreator().setClock(Clock.systemUTC());
|
|
||||||
|
TrackRecordingServiceTest.resetService(mServiceRule, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setUp() throws TimeoutException {
|
public void setUp() throws TimeoutException {
|
||||||
@@ -175,16 +181,6 @@ public class ExportImportTest {
|
|||||||
assertEquals(2, markers.size());
|
assertEquals(2, markers.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
|
||||||
public void tearDown() {
|
|
||||||
if (trackId != null) {
|
|
||||||
contentProviderUtils.deleteTrack(context, trackId);
|
|
||||||
}
|
|
||||||
if (importTrackId != null) {
|
|
||||||
contentProviderUtils.deleteTrack(context, importTrackId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//TODO Does not test images
|
//TODO Does not test images
|
||||||
@LargeTest
|
@LargeTest
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
+18
-15
@@ -22,7 +22,6 @@ import static org.junit.Assert.assertNotNull;
|
|||||||
import static org.junit.Assert.assertNull;
|
import static org.junit.Assert.assertNull;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
import android.content.ContentProvider;
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
@@ -60,7 +59,6 @@ import de.dennisguse.opentracks.content.data.TestDataUtil;
|
|||||||
import de.dennisguse.opentracks.content.data.Track;
|
import de.dennisguse.opentracks.content.data.Track;
|
||||||
import de.dennisguse.opentracks.content.data.TrackPoint;
|
import de.dennisguse.opentracks.content.data.TrackPoint;
|
||||||
import de.dennisguse.opentracks.content.provider.ContentProviderUtils;
|
import de.dennisguse.opentracks.content.provider.ContentProviderUtils;
|
||||||
import de.dennisguse.opentracks.content.provider.CustomContentProvider;
|
|
||||||
import de.dennisguse.opentracks.io.file.importer.TrackPointAssert;
|
import de.dennisguse.opentracks.io.file.importer.TrackPointAssert;
|
||||||
import de.dennisguse.opentracks.services.handlers.TrackPointCreator;
|
import de.dennisguse.opentracks.services.handlers.TrackPointCreator;
|
||||||
import de.dennisguse.opentracks.services.sensors.AltitudeSumManager;
|
import de.dennisguse.opentracks.services.sensors.AltitudeSumManager;
|
||||||
@@ -110,25 +108,15 @@ public class TrackRecordingServiceTest {
|
|||||||
};
|
};
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() throws TimeoutException {
|
||||||
contentProviderUtils = new ContentProviderUtils(context);
|
contentProviderUtils = new ContentProviderUtils(context);
|
||||||
|
|
||||||
// Let's use default values.
|
tearDown();
|
||||||
PreferencesUtils.clear();
|
|
||||||
|
|
||||||
// Ensure that the database is empty before every test
|
|
||||||
contentProviderUtils.deleteAllTracks(context);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void tearDown() throws TimeoutException {
|
public void tearDown() throws TimeoutException {
|
||||||
// Reset service (if some previous test failed)
|
TrackRecordingServiceTest.resetService(mServiceRule, context);
|
||||||
TrackRecordingService service = ((TrackRecordingService.Binder) mServiceRule.bindService(createStartIntent(context)))
|
|
||||||
.getService();
|
|
||||||
if (service.isRecording() || service.isPaused()) {
|
|
||||||
service.endCurrentTrack();
|
|
||||||
}
|
|
||||||
service.getTrackPointCreator().setClock(Clock.systemUTC());
|
|
||||||
|
|
||||||
// Ensure that the database is empty after every test
|
// Ensure that the database is empty after every test
|
||||||
contentProviderUtils.deleteAllTracks(context);
|
contentProviderUtils.deleteAllTracks(context);
|
||||||
@@ -466,4 +454,19 @@ public class TrackRecordingServiceTest {
|
|||||||
|
|
||||||
trackRecordingService.getTrackPointCreator().onNewTrackPoint(trackPoint, Distance.of(50));
|
trackRecordingService.getTrackPointCreator().onNewTrackPoint(trackPoint, Distance.of(50));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//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.endCurrentTrack();
|
||||||
|
service.getTrackPointCreator().setClock(Clock.systemUTC());
|
||||||
|
service.sharedPreferenceChangeListener.onSharedPreferenceChanged(null, null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-5
@@ -36,7 +36,6 @@ import de.dennisguse.opentracks.content.sensor.SensorDataSet;
|
|||||||
import de.dennisguse.opentracks.io.file.importer.TrackPointAssert;
|
import de.dennisguse.opentracks.io.file.importer.TrackPointAssert;
|
||||||
import de.dennisguse.opentracks.services.sensors.AltitudeSumManager;
|
import de.dennisguse.opentracks.services.sensors.AltitudeSumManager;
|
||||||
import de.dennisguse.opentracks.services.sensors.BluetoothRemoteSensorManager;
|
import de.dennisguse.opentracks.services.sensors.BluetoothRemoteSensorManager;
|
||||||
import de.dennisguse.opentracks.settings.PreferencesUtils;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests insert location.
|
* Tests insert location.
|
||||||
@@ -80,16 +79,14 @@ public class TrackRecordingServiceTestLocation {
|
|||||||
contentProviderUtils = new ContentProviderUtils(context);
|
contentProviderUtils = new ContentProviderUtils(context);
|
||||||
tearDown();
|
tearDown();
|
||||||
|
|
||||||
// Let's use default values.
|
|
||||||
PreferencesUtils.clear();
|
|
||||||
|
|
||||||
service = ((TrackRecordingService.Binder) mServiceRule.bindService(TrackRecordingServiceTest.createStartIntent(context)))
|
service = ((TrackRecordingService.Binder) mServiceRule.bindService(TrackRecordingServiceTest.createStartIntent(context)))
|
||||||
.getService();
|
.getService();
|
||||||
service.getTrackPointCreator().stopGPS();
|
service.getTrackPointCreator().stopGPS();
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void tearDown() {
|
public void tearDown() throws TimeoutException {
|
||||||
|
TrackRecordingServiceTest.resetService(mServiceRule, context);
|
||||||
// Ensure that the database is empty after every test
|
// Ensure that the database is empty after every test
|
||||||
contentProviderUtils.deleteAllTracks(context);
|
contentProviderUtils.deleteAllTracks(context);
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-14
@@ -5,7 +5,6 @@ import static org.junit.Assert.assertFalse;
|
|||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
import android.content.ContentProvider;
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.os.Looper;
|
import android.os.Looper;
|
||||||
@@ -35,7 +34,6 @@ import de.dennisguse.opentracks.content.data.Speed;
|
|||||||
import de.dennisguse.opentracks.content.data.Track;
|
import de.dennisguse.opentracks.content.data.Track;
|
||||||
import de.dennisguse.opentracks.content.data.TrackPoint;
|
import de.dennisguse.opentracks.content.data.TrackPoint;
|
||||||
import de.dennisguse.opentracks.content.provider.ContentProviderUtils;
|
import de.dennisguse.opentracks.content.provider.ContentProviderUtils;
|
||||||
import de.dennisguse.opentracks.content.provider.CustomContentProvider;
|
|
||||||
import de.dennisguse.opentracks.settings.PreferencesUtils;
|
import de.dennisguse.opentracks.settings.PreferencesUtils;
|
||||||
import de.dennisguse.opentracks.stats.TrackStatistics;
|
import de.dennisguse.opentracks.stats.TrackStatistics;
|
||||||
|
|
||||||
@@ -67,24 +65,15 @@ public class TrackRecordingServiceTestLooper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() throws TimeoutException {
|
||||||
contentProviderUtils = new ContentProviderUtils(context);
|
contentProviderUtils = new ContentProviderUtils(context);
|
||||||
|
|
||||||
// Let's use default values.
|
tearDown();
|
||||||
PreferencesUtils.clear();
|
|
||||||
|
|
||||||
// Ensure that the database is empty before every test
|
|
||||||
contentProviderUtils.deleteAllTracks(context);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void tearDown() throws TimeoutException {
|
public void tearDown() throws TimeoutException {
|
||||||
// Reset service (if some previous test failed)
|
TrackRecordingServiceTest.resetService(mServiceRule, context);
|
||||||
TrackRecordingService service = ((TrackRecordingService.Binder) mServiceRule.bindService(TrackRecordingServiceTest.createStartIntent(context)))
|
|
||||||
.getService();
|
|
||||||
if (service.isRecording() || service.isPaused()) {
|
|
||||||
service.endCurrentTrack();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure that the database is empty after every test
|
// Ensure that the database is empty after every test
|
||||||
contentProviderUtils.deleteAllTracks(context);
|
contentProviderUtils.deleteAllTracks(context);
|
||||||
|
|||||||
+8
-22
@@ -3,10 +3,7 @@ package de.dennisguse.opentracks.services;
|
|||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
|
|
||||||
import android.content.ContentProvider;
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
|
||||||
import android.os.Build;
|
|
||||||
import android.os.Looper;
|
import android.os.Looper;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
@@ -18,7 +15,6 @@ import androidx.test.rule.ServiceTestRule;
|
|||||||
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.AfterClass;
|
import org.junit.AfterClass;
|
||||||
import org.junit.Assume;
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
@@ -38,9 +34,7 @@ import de.dennisguse.opentracks.content.data.TestDataUtil;
|
|||||||
import de.dennisguse.opentracks.content.data.Track;
|
import de.dennisguse.opentracks.content.data.Track;
|
||||||
import de.dennisguse.opentracks.content.data.TrackPoint;
|
import de.dennisguse.opentracks.content.data.TrackPoint;
|
||||||
import de.dennisguse.opentracks.content.provider.ContentProviderUtils;
|
import de.dennisguse.opentracks.content.provider.ContentProviderUtils;
|
||||||
import de.dennisguse.opentracks.content.provider.CustomContentProvider;
|
|
||||||
import de.dennisguse.opentracks.services.sensors.AltitudeSumManager;
|
import de.dennisguse.opentracks.services.sensors.AltitudeSumManager;
|
||||||
import de.dennisguse.opentracks.settings.PreferencesUtils;
|
|
||||||
import de.dennisguse.opentracks.stats.TrackStatistics;
|
import de.dennisguse.opentracks.stats.TrackStatistics;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -82,21 +76,14 @@ public class TrackRecordingServiceTestStatistics {
|
|||||||
@Before
|
@Before
|
||||||
public void setUp() throws TimeoutException {
|
public void setUp() throws TimeoutException {
|
||||||
contentProviderUtils = new ContentProviderUtils(context);
|
contentProviderUtils = new ContentProviderUtils(context);
|
||||||
|
|
||||||
tearDown();
|
tearDown();
|
||||||
|
|
||||||
// Let's use default values.
|
|
||||||
PreferencesUtils.clear();
|
|
||||||
|
|
||||||
service = ((TrackRecordingService.Binder) mServiceRule.bindService(TrackRecordingServiceTest.createStartIntent(context)))
|
|
||||||
.getService();
|
|
||||||
service.getTrackPointCreator().stopGPS();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void tearDown() throws TimeoutException {
|
public void tearDown() throws TimeoutException {
|
||||||
TrackRecordingService service = ((TrackRecordingService.Binder) mServiceRule.bindService(new Intent(context, TrackRecordingService.class)))
|
TrackRecordingServiceTest.resetService(mServiceRule, context);
|
||||||
.getService();
|
contentProviderUtils.deleteAllTracks(context);
|
||||||
service.getTrackPointCreator().setClock(Clock.systemUTC());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -104,13 +91,12 @@ public class TrackRecordingServiceTestStatistics {
|
|||||||
*/
|
*/
|
||||||
@MediumTest
|
@MediumTest
|
||||||
@Test
|
@Test
|
||||||
public void movingtime_with_pauses() {
|
public void movingtime_with_pauses() throws TimeoutException {
|
||||||
Assume.assumeTrue(
|
|
||||||
"Test fails on API23; reproducible on CI and some machines.",
|
|
||||||
Build.VERSION.SDK_INT > 23
|
|
||||||
);
|
|
||||||
|
|
||||||
// given
|
// given
|
||||||
|
TrackRecordingService service = ((TrackRecordingService.Binder) mServiceRule.bindService(TrackRecordingServiceTest.createStartIntent(context)))
|
||||||
|
.getService();
|
||||||
|
service.getTrackPointCreator().stopGPS();
|
||||||
|
|
||||||
service.getTrackPointCreator().setClock(Clock.fixed(Instant.ofEpochMilli(0), ZoneId.systemDefault()));
|
service.getTrackPointCreator().setClock(Clock.fixed(Instant.ofEpochMilli(0), ZoneId.systemDefault()));
|
||||||
Track.Id trackId = service.startNewTrack();
|
Track.Id trackId = service.startNewTrack();
|
||||||
service.stopUpdateRecordingData();
|
service.stopUpdateRecordingData();
|
||||||
|
|||||||
@@ -99,7 +99,10 @@ public class TrackRecordingService extends Service implements TrackPointCreator.
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
private final OnSharedPreferenceChangeListener sharedPreferenceChangeListener = new OnSharedPreferenceChangeListener() {
|
@Deprecated
|
||||||
|
//TODO Workaround as service is not stopped on API23; thus sharedpreferences are not reset between tests.
|
||||||
|
@VisibleForTesting
|
||||||
|
final OnSharedPreferenceChangeListener sharedPreferenceChangeListener = new OnSharedPreferenceChangeListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
||||||
if (PreferencesUtils.isKey(R.string.stats_units_key, key)) {
|
if (PreferencesUtils.isKey(R.string.stats_units_key, key)) {
|
||||||
@@ -149,21 +152,13 @@ public class TrackRecordingService extends Service implements TrackPointCreator.
|
|||||||
PreferencesUtils.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
|
PreferencesUtils.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
|
||||||
return START_STICKY;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Binder onBind(Intent intent) {
|
|
||||||
return binder;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
handler.removeCallbacksAndMessages(null); //Some tests do not finish the recording completely
|
handler.removeCallbacksAndMessages(null); //Some tests do not finish the recording completely
|
||||||
handler = null;
|
handler = null;
|
||||||
|
|
||||||
|
PreferencesUtils.unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
|
||||||
|
|
||||||
trackPointCreator.stop();
|
trackPointCreator.stop();
|
||||||
trackPointCreator = null;
|
trackPointCreator = null;
|
||||||
trackRecordingManager = null;
|
trackRecordingManager = null;
|
||||||
@@ -172,8 +167,6 @@ public class TrackRecordingService extends Service implements TrackPointCreator.
|
|||||||
showNotification(false); //TODO Why?
|
showNotification(false); //TODO Why?
|
||||||
notificationManager = null;
|
notificationManager = null;
|
||||||
|
|
||||||
PreferencesUtils.unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
voiceAnnouncementManager.shutdown();
|
voiceAnnouncementManager.shutdown();
|
||||||
} finally {
|
} finally {
|
||||||
@@ -435,6 +428,12 @@ public class TrackRecordingService extends Service implements TrackPointCreator.
|
|||||||
return trackPointCreator;
|
return trackPointCreator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
|
@VisibleForTesting
|
||||||
|
public TrackRecordingManager getTrackRecordingManager() {
|
||||||
|
return trackRecordingManager;
|
||||||
|
}
|
||||||
|
|
||||||
public LiveData<GpsStatusValue> getGpsStatusObservable() {
|
public LiveData<GpsStatusValue> getGpsStatusObservable() {
|
||||||
return gpsStatusObservable;
|
return gpsStatusObservable;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user