From 126102aa050481dbcfee8caf6c2227d450900b52 Mon Sep 17 00:00:00 2001 From: "Martin.JM" Date: Mon, 28 Oct 2024 19:46:14 +0100 Subject: [PATCH] Add tests and a couple of fixes --- .../devices/huawei/HuaweiSampleProvider.java | 8 ++- .../database/SampleProviderTest.java | 67 +++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiSampleProvider.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiSampleProvider.java index aa4594748..c1e8eaf22 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiSampleProvider.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiSampleProvider.java @@ -22,6 +22,7 @@ import androidx.annotation.Nullable; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; import de.greenrobot.dao.AbstractDao; import de.greenrobot.dao.Property; @@ -309,7 +310,8 @@ public class HuaweiSampleProvider extends AbstractSampleProvider getGBActivitySamplesHighRes(int timestamp_from, int timestamp_to) { List processedSamples = getRawOrderedActivitySamples(timestamp_from, timestamp_to); addWorkoutSamples(processedSamples, timestamp_from, timestamp_to); - return processedSamples; + // Filter out the end markers before returning + return processedSamples.stream().filter(sample -> sample.getTimestamp() <= sample.getOtherTimestamp()).collect(Collectors.toList()); } @Override @@ -357,6 +359,10 @@ public class HuaweiSampleProvider extends AbstractSampleProvider activitySample.getOtherTimestamp()) + continue; + // Skip the processed samples that are before this activity sample while (activitySample.getTimestamp() > processedSamples.get(currentIndex).getTimestamp()) { // Add data to current index sample diff --git a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/database/SampleProviderTest.java b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/database/SampleProviderTest.java index 8b37091ea..d3cc0fd70 100644 --- a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/database/SampleProviderTest.java +++ b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/database/SampleProviderTest.java @@ -5,9 +5,11 @@ import org.junit.Test; import java.util.List; import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider; +import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiSampleProvider; import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandSampleProvider; import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample; import nodomain.freeyourgadget.gadgetbridge.entities.Device; +import nodomain.freeyourgadget.gadgetbridge.entities.HuaweiActivitySample; import nodomain.freeyourgadget.gadgetbridge.entities.MiBandActivitySample; import nodomain.freeyourgadget.gadgetbridge.entities.User; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; @@ -78,6 +80,16 @@ public class SampleProviderTest extends TestBase { samples = sampleProvider.getActivitySamples(1, -1); assertEquals(0, samples.size()); + + // Now high res data + samples = sampleProvider.getAllActivitySamplesHighRes(0, 0); + assertTrue(samples.isEmpty()); + + samples = sampleProvider.getAllActivitySamplesHighRes(-1, 1); + assertTrue(samples.isEmpty()); + + samples = sampleProvider.getAllActivitySamplesHighRes(1, -1); + assertTrue(samples.isEmpty()); } private T createSample(SampleProvider sampleProvider, int rawKind, int timestamp, int rawIntensity, int heartRate, int steps, User user, Device device) { @@ -134,6 +146,16 @@ public class SampleProviderTest extends TestBase { samples = sampleProvider.getActivitySamples(1, -1); assertEquals(0, samples.size()); + // Now high res data + samples = sampleProvider.getAllActivitySamplesHighRes(0, 0); + assertTrue(samples.isEmpty()); + + samples = sampleProvider.getAllActivitySamplesHighRes(-1, 1); + assertTrue(samples.isEmpty()); + + samples = sampleProvider.getAllActivitySamplesHighRes(1, -1); + assertTrue(samples.isEmpty()); + // finally checks for existing timestamps List allSamples = sampleProvider.getAllActivitySamples(0, 10000); assertEquals(4, allSamples.size()); @@ -146,4 +168,49 @@ public class SampleProviderTest extends TestBase { // FIXME activitySamples = sampleProvider.getActivitySamples(10, 150); // FIXME assertEquals(1, activitySamples.size()); } + + @Test + public void testHighResSamples() { + // Mi Band sample provider does not support this at the moment, so we use the Huawei sample provider + HuaweiSampleProvider sampleProvider = new HuaweiSampleProvider(dummyGBDevice, daoSession); + User user = DBHelper.getUser(daoSession); + assertNotNull(user); + assertNotNull(user.getId()); + Device device = DBHelper.getDevice(dummyGBDevice, daoSession); + assertNotNull(device); + + HuaweiActivitySample s1 = createSample(sampleProvider, MiBandSampleProvider.TYPE_ACTIVITY, 100, 10, 70, 1000, user, device); + s1.setOtherTimestamp(110); // Necessary for Huawei samples + sampleProvider.addGBActivitySample(s1); + sampleProvider.addGBActivitySample(s1); // add again, should not throw or fail + + HuaweiActivitySample s2 = createSample(sampleProvider, MiBandSampleProvider.TYPE_ACTIVITY, 110, 20, 80, 1030, user, device); + s2.setOtherTimestamp(120); + sampleProvider.addGBActivitySample(s2); + + HuaweiActivitySample s3 = createSample(sampleProvider, MiBandSampleProvider.TYPE_DEEP_SLEEP, 120, 10, 62, 4030, user, device); + s3.setOtherTimestamp(200); + HuaweiActivitySample s4 = createSample(sampleProvider, MiBandSampleProvider.TYPE_LIGHT_SLEEP, 200, 10, 60, 4030, user, device); + s4.setOtherTimestamp(220); + sampleProvider.addGBActivitySamples(new HuaweiActivitySample[] { s3, s4 }); + + List samples = sampleProvider.getAllActivitySamples(0, 1); + assertEquals(1, samples.size()); // It generates a sample for every 60 seconds that is requested + + samples = sampleProvider.getAllActivitySamplesHighRes(0, 1); + assertTrue(samples.isEmpty()); + + samples = sampleProvider.getAllActivitySamples(100, 150); + assertEquals(1, samples.size()); + assertEquals(100, samples.get(0).getTimestamp()); + + samples = sampleProvider.getAllActivitySamplesHighRes(100, 115); + assertEquals(2, samples.size()); + + samples = sampleProvider.getAllActivitySamples(100, 200); + assertEquals(2, samples.size()); // First three are combined + + samples = sampleProvider.getAllActivitySamplesHighRes(100, 200); + assertEquals(4, samples.size()); // No combining takes place for the high res + } }