From e0553f812f6df6bb302103f2d261d7447efd8f1e Mon Sep 17 00:00:00 2001 From: Dennis Guse Date: Sun, 17 Jan 2021 14:42:19 +0100 Subject: [PATCH] Import: segments might contain no TrackPoints. #316. --- .../content/DescriptionGeneratorTest.java | 4 +-- .../opentracks/stats/TrackStatisticsTest.java | 27 +++++++++++++++++++ .../opentracks/stats/TrackStatistics.java | 27 ++++++++++++------- 3 files changed, 47 insertions(+), 11 deletions(-) diff --git a/src/androidTest/java/de/dennisguse/opentracks/content/DescriptionGeneratorTest.java b/src/androidTest/java/de/dennisguse/opentracks/content/DescriptionGeneratorTest.java index c654f911b..dbaea9222 100644 --- a/src/androidTest/java/de/dennisguse/opentracks/content/DescriptionGeneratorTest.java +++ b/src/androidTest/java/de/dennisguse/opentracks/content/DescriptionGeneratorTest.java @@ -67,7 +67,7 @@ public class DescriptionGeneratorTest { stats.setMaxElevation(550); stats.setMinElevation(-500); stats.setTotalElevationGain(6000f); - stats.setTotalElevationLoss(6000); + stats.setTotalElevationLoss(6000f); stats.setStartTime(START_TIME); track.setTrackStatistics(stats); track.setCategory("hiking"); @@ -105,7 +105,7 @@ public class DescriptionGeneratorTest { stats.setMaxElevation(Double.POSITIVE_INFINITY); stats.setMinElevation(Double.NEGATIVE_INFINITY); stats.setTotalElevationGain(6000f); - stats.setTotalElevationLoss(6000); + stats.setTotalElevationLoss(6000f); stats.setStartTime(START_TIME); track.setTrackStatistics(stats); track.setCategory("hiking"); diff --git a/src/androidTest/java/de/dennisguse/opentracks/stats/TrackStatisticsTest.java b/src/androidTest/java/de/dennisguse/opentracks/stats/TrackStatisticsTest.java index 4024e8e66..1875f1795 100644 --- a/src/androidTest/java/de/dennisguse/opentracks/stats/TrackStatisticsTest.java +++ b/src/androidTest/java/de/dennisguse/opentracks/stats/TrackStatisticsTest.java @@ -24,6 +24,7 @@ import java.time.Duration; import java.time.Instant; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; /** * Tests for {@link TrackStatistics}. @@ -41,8 +42,32 @@ public class TrackStatisticsTest { statistics = new TrackStatistics(); } + @Test + public void testMerge_no_data() { + // given + TrackStatistics statistics2 = new TrackStatistics(); + + // when + statistics.merge(statistics2); + + // then + assertNull(statistics.getStartTime()); + assertNull(statistics.getStopTime()); + assertEquals(Duration.ofSeconds(0), statistics.getMovingTime()); + assertEquals(Duration.ofSeconds(0), statistics.getTotalTime()); + + assertNull(statistics.getTotalElevationGain()); + assertNull(statistics.getTotalElevationLoss()); + assertEquals(Double.NEGATIVE_INFINITY, statistics.getMaxElevation(), 0.0); + assertEquals(Double.POSITIVE_INFINITY, statistics.getMinElevation(), 0.0); + assertEquals(0.0, statistics.getMaxSpeed(), 0.0); + assertEquals(0.0, statistics.getAverageSpeed(), 0.0); + assertEquals(0.0, statistics.getAverageMovingSpeed(), 0.0); + } + @Test public void testMerge() { + // given TrackStatistics statistics2 = new TrackStatistics(); statistics.setStartTime(Instant.ofEpochMilli(1000)); // Resulting start time statistics.setStopTime(Instant.ofEpochMilli(2500)); @@ -63,8 +88,10 @@ public class TrackStatisticsTest { statistics2.setMaxElevation(3575.0); // Resulting max elevation statistics2.setMinElevation(2800.0); + // when statistics.merge(statistics2); + // then assertEquals(Instant.ofEpochMilli(1000), statistics.getStartTime()); assertEquals(Instant.ofEpochMilli(4000), statistics.getStopTime()); assertEquals(Duration.ofMillis(2500), statistics.getTotalTime()); diff --git a/src/main/java/de/dennisguse/opentracks/stats/TrackStatistics.java b/src/main/java/de/dennisguse/opentracks/stats/TrackStatistics.java index d8387b419..c7816d3c6 100644 --- a/src/main/java/de/dennisguse/opentracks/stats/TrackStatistics.java +++ b/src/main/java/de/dennisguse/opentracks/stats/TrackStatistics.java @@ -48,8 +48,7 @@ public class TrackStatistics { // The maximum speed (meters/second) that we believe is valid. private double maxSpeed_mps; // The total elevation gained (meters). - @Nullable - private Float totalElevationGain_m; + private Float totalElevationGain_m = null; // The total elevation lost (meters). private Float totalElevationLoss_m = null; @@ -81,8 +80,17 @@ public class TrackStatistics { * @param other another statistics data object */ public void merge(TrackStatistics other) { - startTime = startTime.isBefore(other.startTime) ? startTime : other.startTime; - stopTime = stopTime.isAfter(other.stopTime) ? stopTime : other.stopTime; + if (startTime == null) { + startTime = other.startTime; + } else { + startTime = startTime.isBefore(other.startTime) ? startTime : other.startTime; + } + if (stopTime == null) { + stopTime = other.stopTime; + } else { + stopTime = stopTime.isAfter(other.stopTime) ? stopTime : other.stopTime; + } + totalDistance_m += other.totalDistance_m; totalTime = totalTime.plus(other.totalTime); movingTime = movingTime.plus(other.movingTime); @@ -120,6 +128,7 @@ public class TrackStatistics { setMovingTime(Duration.ofSeconds(0)); setMaxSpeed(0); setTotalElevationGain(null); + setTotalElevationLoss(null); } public void reset(Instant startTime) { @@ -279,8 +288,8 @@ public class TrackStatistics { return totalElevationGain_m != null; } - public @Nullable - Float getTotalElevationGain() { + @Nullable + public Float getTotalElevationGain() { return totalElevationGain_m; } @@ -299,12 +308,12 @@ public class TrackStatistics { return totalElevationLoss_m != null; } - public @Nullable - Float getTotalElevationLoss() { + @Nullable + public Float getTotalElevationLoss() { return totalElevationLoss_m; } - public void setTotalElevationLoss(float totalElevationLoss_m) { + public void setTotalElevationLoss(Float totalElevationLoss_m) { this.totalElevationLoss_m = totalElevationLoss_m; }