Cleanup: fix various issues in tests.

This commit is contained in:
Dennis Guse
2021-12-27 12:59:25 +01:00
parent 9af213fd12
commit c7bbaddc06
6 changed files with 26 additions and 29 deletions
@@ -15,6 +15,13 @@
*/ */
package de.dennisguse.opentracks.content.provider; package de.dennisguse.opentracks.content.provider;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.ContentUris; import android.content.ContentUris;
import android.content.ContentValues; import android.content.ContentValues;
@@ -57,13 +64,6 @@ import de.dennisguse.opentracks.stats.TrackStatistics;
import de.dennisguse.opentracks.util.FileUtils; import de.dennisguse.opentracks.util.FileUtils;
import de.dennisguse.opentracks.util.UUIDUtils; import de.dennisguse.opentracks.util.UUIDUtils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
/** /**
* A unit test for {@link ContentProviderUtils}. * A unit test for {@link ContentProviderUtils}.
* *
@@ -220,6 +220,8 @@ public class CustomContentProviderUtilsTest {
assertEquals(0, tracksPointsCursor.getCount()); assertEquals(0, tracksPointsCursor.getCount());
markerCursor = contentResolver.query(MarkerColumns.CONTENT_URI, null, null, null, MarkerColumns._ID); markerCursor = contentResolver.query(MarkerColumns.CONTENT_URI, null, null, null, MarkerColumns._ID);
assertEquals(0, markerCursor.getCount()); assertEquals(0, markerCursor.getCount());
//TODO Close all cursors
} }
/** /**
@@ -111,11 +111,6 @@ public class TrackRecordingServiceTest {
@Before @Before
public void setUp() { public void setUp() {
// Set up the mock content resolver
ContentProvider customContentProvider = new CustomContentProvider() {
};
customContentProvider.attachInfo(context, null);
contentProviderUtils = new ContentProviderUtils(context); contentProviderUtils = new ContentProviderUtils(context);
// Let's use default values. // Let's use default values.
@@ -249,6 +244,7 @@ public class TrackRecordingServiceTest {
// when // when
trackPointCreator.setClock(Clock.fixed(Instant.parse("2020-02-02T02:02:03Z"), ZoneId.of("CET"))); trackPointCreator.setClock(Clock.fixed(Instant.parse("2020-02-02T02:02:03Z"), ZoneId.of("CET")));
service.pauseCurrentTrack(); service.pauseCurrentTrack();
service.stopUpdateRecordingData();
// then // then
assertEquals(2, contentProviderUtils.getTrackPointCursor(trackId, null).getCount()); assertEquals(2, contentProviderUtils.getTrackPointCursor(trackId, null).getCount());
@@ -256,6 +252,7 @@ public class TrackRecordingServiceTest {
//when //when
trackPointCreator.setClock(Clock.fixed(Instant.parse("2020-02-02T02:02:04Z"), ZoneId.of("CET"))); trackPointCreator.setClock(Clock.fixed(Instant.parse("2020-02-02T02:02:04Z"), ZoneId.of("CET")));
service.resumeTrack(trackId); service.resumeTrack(trackId);
service.stopUpdateRecordingData();
// then // then
assertTrue(service.isRecording()); assertTrue(service.isRecording());
@@ -2,7 +2,6 @@ package de.dennisguse.opentracks.services;
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.os.Looper; import android.os.Looper;
@@ -31,7 +30,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.content.sensor.SensorDataHeartRate; import de.dennisguse.opentracks.content.sensor.SensorDataHeartRate;
import de.dennisguse.opentracks.content.sensor.SensorDataRunning; import de.dennisguse.opentracks.content.sensor.SensorDataRunning;
import de.dennisguse.opentracks.content.sensor.SensorDataSet; import de.dennisguse.opentracks.content.sensor.SensorDataSet;
@@ -79,11 +77,6 @@ public class TrackRecordingServiceTestLocation {
@Before @Before
public void setUp() throws TimeoutException { public void setUp() throws TimeoutException {
// Set up the mock content resolver
ContentProvider customContentProvider = new CustomContentProvider() {
};
customContentProvider.attachInfo(context, null);
contentProviderUtils = new ContentProviderUtils(context); contentProviderUtils = new ContentProviderUtils(context);
tearDown(); tearDown();
@@ -44,6 +44,7 @@ import de.dennisguse.opentracks.stats.TrackStatistics;
* *
* @author Bartlomiej Niechwiej * @author Bartlomiej Niechwiej
*/ */
//TODO Check that those tests are really testing something!
@RunWith(AndroidJUnit4.class) @RunWith(AndroidJUnit4.class)
public class TrackRecordingServiceTestLooper { public class TrackRecordingServiceTestLooper {
@@ -67,11 +68,6 @@ public class TrackRecordingServiceTestLooper {
@Before @Before
public void setUp() { public void setUp() {
// Set up the mock content resolver
ContentProvider customContentProvider = new CustomContentProvider() {
};
customContentProvider.attachInfo(context, null);
contentProviderUtils = new ContentProviderUtils(context); contentProviderUtils = new ContentProviderUtils(context);
// Let's use default values. // Let's use default values.
@@ -81,11 +81,6 @@ public class TrackRecordingServiceTestStatistics {
@Before @Before
public void setUp() throws TimeoutException { public void setUp() throws TimeoutException {
// Set up the mock content resolver
ContentProvider customContentProvider = new CustomContentProvider() {
};
customContentProvider.attachInfo(context, null);
contentProviderUtils = new ContentProviderUtils(context); contentProviderUtils = new ContentProviderUtils(context);
tearDown(); tearDown();
@@ -161,13 +161,16 @@ public class TrackRecordingService extends Service implements TrackPointCreator.
@Override @Override
public void onDestroy() { public void onDestroy() {
handler.removeCallbacksAndMessages(null); //Some tests do not finish the recording completely
handler = null; handler = null;
trackPointCreator.stop(); trackPointCreator.stop();
trackPointCreator = null; trackPointCreator = null;
trackRecordingManager = null;
// Reverse order from onCreate // Reverse order from onCreate
showNotification(false); //TODO Why? showNotification(false); //TODO Why?
notificationManager = null;
PreferencesUtils.unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener); PreferencesUtils.unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
@@ -180,6 +183,7 @@ public class TrackRecordingService extends Service implements TrackPointCreator.
// This should be the next to last operation // This should be the next to last operation
wakeLock = SystemUtils.releaseWakeLock(wakeLock); wakeLock = SystemUtils.releaseWakeLock(wakeLock);
updateRecordingStatus(STATUS_DEFAULT);
recordingStatusObservable = null; recordingStatusObservable = null;
gpsStatusObservable = null; gpsStatusObservable = null;
recordingDataObservable = null; recordingDataObservable = null;
@@ -187,6 +191,16 @@ public class TrackRecordingService extends Service implements TrackPointCreator.
super.onDestroy(); super.onDestroy();
} }
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public Binder onBind(Intent intent) {
return binder;
}
public boolean isRecording() { public boolean isRecording() {
return recordingStatus.isRecording(); return recordingStatus.isRecording();
} }