1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-24 09:35:50 +01:00

Add tests and a couple of fixes

This commit is contained in:
Martin.JM 2024-10-28 19:46:14 +01:00 committed by José Rebelo
parent f0ffc0e165
commit 126102aa05
2 changed files with 74 additions and 1 deletions

View File

@ -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<HuaweiActivityS
protected List<HuaweiActivitySample> getGBActivitySamplesHighRes(int timestamp_from, int timestamp_to) {
List<HuaweiActivitySample> 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<HuaweiActivityS
int stateModifier = ActivitySample.NOT_MEASURED;
for (HuaweiActivitySample activitySample : activitySamples) {
// Ignore the end markers
if (activitySample.getTimestamp() > 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

View File

@ -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 extends AbstractActivitySample> T createSample(SampleProvider<T> 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<MiBandActivitySample> 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<HuaweiActivitySample> 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
}
}