TrackRecordingService: add integration tests.

This commit is contained in:
Dennis Guse
2022-01-09 13:40:07 +01:00
parent ab07d3b130
commit cd7bf36aa9
7 changed files with 475 additions and 445 deletions
@@ -8,7 +8,6 @@ import de.dennisguse.opentracks.content.data.TrackPoint;
public class TrackPointAssert { public class TrackPointAssert {
private boolean assertTime = true;
private boolean assertAccuracy = true; private boolean assertAccuracy = true;
private double delta = 0.001; private double delta = 0.001;
@@ -17,10 +16,7 @@ public class TrackPointAssert {
} }
public void assertEquals(TrackPoint expected, TrackPoint actual) { public void assertEquals(TrackPoint expected, TrackPoint actual) {
Assert.assertNotNull(actual.getTime()); Assert.assertEquals(expected.getTime(), actual.getTime());
if (assertTime) {
Assert.assertEquals(expected.getTime(), actual.getTime());
}
Assert.assertEquals(expected.getType(), actual.getType()); Assert.assertEquals(expected.getType(), actual.getType());
@@ -100,12 +96,6 @@ public class TrackPointAssert {
} }
} }
@Deprecated //Try to mock time instead.
public TrackPointAssert ignoreTime() {
this.assertTime = false;
return this;
}
public TrackPointAssert noAccuracy() { public TrackPointAssert noAccuracy() {
this.assertAccuracy = false; this.assertAccuracy = false;
return this; return this;
@@ -2,16 +2,14 @@ package de.dennisguse.opentracks.services;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.location.Location;
import androidx.test.rule.ServiceTestRule; import androidx.test.rule.ServiceTestRule;
import java.time.Clock; import java.time.Clock;
import java.time.Instant;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import de.dennisguse.opentracks.content.data.Distance; import de.dennisguse.opentracks.services.handlers.TrackPointCreator;
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.services.sensors.BluetoothRemoteSensorManager;
import de.dennisguse.opentracks.settings.PreferencesUtils; import de.dennisguse.opentracks.settings.PreferencesUtils;
@@ -35,18 +33,15 @@ public class TrackRecordingServiceTestUtils {
service.sharedPreferenceChangeListener.onSharedPreferenceChanged(null, null); service.sharedPreferenceChangeListener.onSharedPreferenceChanged(null, null);
} }
static void newTrackPoint(TrackRecordingService trackRecordingService, double latitude, double longitude, float accuracy, long speed) { static void sendGPSLocation(TrackPointCreator trackPointCreator, String time, double latitude, double longitude, float accuracy, long speed) {
newTrackPoint(trackRecordingService, latitude, longitude, accuracy, speed, System.currentTimeMillis()); Location location = new Location("mock");
} location.setTime(1L); // Should be ignored anyhow.
location.setLatitude(latitude);
location.setLongitude(longitude);
location.setAccuracy(accuracy);
location.setSpeed(speed);
static void newTrackPoint(TrackRecordingService trackRecordingService, double latitude, double longitude, float accuracy, long speed, long time) { trackPointCreator.setClock(time);
TrackPoint trackPoint = new TrackPoint(TrackPoint.Type.TRACKPOINT, Instant.ofEpochMilli(time)) trackPointCreator.getGpsHandler().onLocationChanged(location);
.setLongitude(longitude)
.setLatitude(latitude)
.setHorizontalAccuracy(Distance.of(accuracy))
.setSpeed(Speed.of(speed))
.setBearing(3.0f);
trackRecordingService.getTrackPointCreator().onNewTrackPoint(trackPoint);
} }
} }
@@ -415,27 +415,25 @@ public class TrackPoint {
return this; return this;
} }
@NonNull
@Override @Override
public String toString() { public String toString() {
String result = "time=" + getTime() + " (type=" + getType() + ")"; return "TrackPoint{" +
if (hasLocation()) { "id=" + id +
result += ": lat=" + getLatitude() + " lng=" + getLongitude() + " alt=" + getAltitude(); ", time=" + time +
} ", latitude=" + latitude +
if (hasHorizontalAccuracy()) { ", longitude=" + longitude +
result += " acc=" + getHorizontalAccuracy(); ", horizontalAccuracy=" + horizontalAccuracy +
} ", altitude=" + altitude +
if (hasSensorDistance()) { ", speed=" + speed +
result += " distance=" + getSensorDistance(); ", bearing=" + bearing +
} ", sensorDistance=" + sensorDistance +
if (hasAltitudeGain()) { ", type=" + type +
result += " altitudeGain= " + getAltitudeGain(); ", heartRate_bpm=" + heartRate_bpm +
} ", cadence_rpm=" + cadence_rpm +
if (hasAltitudeLoss()) { ", power=" + power +
result += " altitudeLoss= " + getAltitudeLoss(); ", altitudeGain_m=" + altitudeGain_m +
} ", altitudeLoss_m=" + altitudeLoss_m +
'}';
return result;
} }
public static class Id { public static class Id {
@@ -178,6 +178,12 @@ public class TrackPointCreator implements BluetoothRemoteSensorManager.SensorDat
this.altitudeSumManager = altitudeSumManager; this.altitudeSumManager = altitudeSumManager;
} }
@Deprecated
@VisibleForTesting
public BluetoothRemoteSensorManager getRemoteSensorManager() {
return remoteSensorManager;
}
@Deprecated @Deprecated
@VisibleForTesting @VisibleForTesting
public void setRemoteSensorManager(BluetoothRemoteSensorManager remoteSensorManager) { public void setRemoteSensorManager(BluetoothRemoteSensorManager remoteSensorManager) {
@@ -168,6 +168,11 @@ public class PreferencesUtils {
editor.apply(); editor.apply();
} }
@VisibleForTesting
public static void setString(int keyId, int valueId) {
setString(keyId, resources.getString(valueId));
}
static void setInt(int keyId, int value) { static void setInt(int keyId, int value) {
Editor editor = sharedPreferences.edit(); Editor editor = sharedPreferences.edit();
editor.putInt(getKey(keyId), value); editor.putInt(getKey(keyId), value);
@@ -78,7 +78,7 @@ public class TrackStatistics {
} }
@VisibleForTesting @VisibleForTesting
public TrackStatistics(String startTime, String stopTime, int totalDistance_m, int totalTime_s, int movingTime_s, float maxSpeed_mps, Float totalAltitudeGain_m, Float totalAltitudeLoss_m) { public TrackStatistics(String startTime, String stopTime, double totalDistance_m, int totalTime_s, int movingTime_s, float maxSpeed_mps, Float totalAltitudeGain_m, Float totalAltitudeLoss_m) {
this.startTime = Instant.parse(startTime); this.startTime = Instant.parse(startTime);
this.stopTime = Instant.parse(stopTime); this.stopTime = Instant.parse(stopTime);
this.totalDistance = Distance.of(totalDistance_m); this.totalDistance = Distance.of(totalDistance_m);