Test SampleProvider (MiBand for a start)
This commit is contained in:
parent
2e91246a45
commit
ce175c2952
|
@ -1,163 +1,173 @@
|
||||||
package nodomain.freeyourgadget.gadgetbridge.database;
|
package nodomain.freeyourgadget.gadgetbridge.database;
|
||||||
|
|
||||||
import android.database.sqlite.SQLiteDatabase;
|
|
||||||
|
|
||||||
import org.junit.After;
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.robolectric.RobolectricTestRunner;
|
|
||||||
import org.robolectric.RuntimeEnvironment;
|
|
||||||
import org.robolectric.annotation.Config;
|
|
||||||
|
|
||||||
import java.util.Calendar;
|
|
||||||
import java.util.GregorianCalendar;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import nodomain.freeyourgadget.gadgetbridge.BuildConfig;
|
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandSampleProvider;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.GBException;
|
import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.entities.ActivityDescription;
|
|
||||||
import nodomain.freeyourgadget.gadgetbridge.entities.ActivityDescriptionDao;
|
|
||||||
import nodomain.freeyourgadget.gadgetbridge.entities.DaoMaster;
|
|
||||||
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
|
|
||||||
import nodomain.freeyourgadget.gadgetbridge.entities.Device;
|
import nodomain.freeyourgadget.gadgetbridge.entities.Device;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.entities.DeviceAttributes;
|
import nodomain.freeyourgadget.gadgetbridge.entities.MiBandActivitySample;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.entities.Tag;
|
|
||||||
import nodomain.freeyourgadget.gadgetbridge.entities.User;
|
import nodomain.freeyourgadget.gadgetbridge.entities.User;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.entities.UserAttributes;
|
|
||||||
import nodomain.freeyourgadget.gadgetbridge.entities.UserAttributesDao;
|
|
||||||
import nodomain.freeyourgadget.gadgetbridge.entities.UserDao;
|
|
||||||
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser;
|
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
|
|
||||||
import nodomain.freeyourgadget.gadgetbridge.test.TestBase;
|
import nodomain.freeyourgadget.gadgetbridge.test.TestBase;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertNull;
|
import static org.junit.Assert.assertSame;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
public class SampleProviderTest extends TestBase {
|
public class SampleProviderTest extends TestBase {
|
||||||
|
|
||||||
@Test
|
private GBDevice dummyGBDevice;
|
||||||
public void testDBHelper() {
|
|
||||||
GBDevice dummyGBDevice = createDummyGDevice("00:00:00:00:01");
|
@Override
|
||||||
Device device = DBHelper.getDevice(dummyGBDevice, daoSession);
|
public void setUp() throws Exception {
|
||||||
assertNotNull(device);
|
super.setUp();
|
||||||
assertEquals("00:00:00:00:01", device.getIdentifier());
|
dummyGBDevice = createDummyGDevice("00:00:00:00:10");
|
||||||
assertEquals("Testie", device.getName());
|
|
||||||
assertEquals("4.0", device.getModel());
|
|
||||||
assertEquals(DeviceType.TEST.getKey(), device.getType());
|
|
||||||
DeviceAttributes attributes = device.getDeviceAttributesList().get(0);
|
|
||||||
assertNotNull(attributes);
|
|
||||||
assertEquals("1.2.3", attributes.getFirmwareVersion1());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testActivityDescription() {
|
public void testBasics() {
|
||||||
|
MiBandSampleProvider sampleProvider = new MiBandSampleProvider(dummyGBDevice, daoSession);
|
||||||
|
assertNotNull(sampleProvider.getDevice());
|
||||||
|
assertSame(dummyGBDevice, sampleProvider.getDevice());
|
||||||
|
assertNotNull(sampleProvider.getSampleDao());
|
||||||
|
assertNotNull(sampleProvider.createActivitySample());
|
||||||
|
float intensity = sampleProvider.normalizeIntensity(50);
|
||||||
|
assertTrue(intensity > 0);
|
||||||
|
assertTrue(intensity < 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testActivityKind() {
|
||||||
|
MiBandSampleProvider sampleProvider = new MiBandSampleProvider(dummyGBDevice, daoSession);
|
||||||
|
int type = sampleProvider.normalizeType(MiBandSampleProvider.TYPE_ACTIVITY);
|
||||||
|
assertEquals(ActivityKind.TYPE_ACTIVITY, type);
|
||||||
|
|
||||||
|
type = sampleProvider.normalizeType(MiBandSampleProvider.TYPE_DEEP_SLEEP);
|
||||||
|
assertEquals(ActivityKind.TYPE_DEEP_SLEEP, type);
|
||||||
|
|
||||||
|
type = sampleProvider.normalizeType(MiBandSampleProvider.TYPE_LIGHT_SLEEP);
|
||||||
|
assertEquals(ActivityKind.TYPE_LIGHT_SLEEP, type);
|
||||||
|
|
||||||
|
type = sampleProvider.normalizeType(MiBandSampleProvider.TYPE_NONWEAR);
|
||||||
|
assertEquals(ActivityKind.TYPE_NOT_WORN, type);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNoSamples() {
|
||||||
|
MiBandSampleProvider sampleProvider = new MiBandSampleProvider(dummyGBDevice, daoSession);
|
||||||
|
List<MiBandActivitySample> samples = sampleProvider.getAllActivitySamples(0, 0);
|
||||||
|
assertEquals(0, samples.size());
|
||||||
|
|
||||||
|
samples = sampleProvider.getAllActivitySamples(-1, 1);
|
||||||
|
assertEquals(0, samples.size());
|
||||||
|
|
||||||
|
samples = sampleProvider.getAllActivitySamples(1, -1);
|
||||||
|
assertEquals(0, samples.size());
|
||||||
|
|
||||||
|
// now specific activity kinds
|
||||||
|
samples = sampleProvider.getActivitySamples(0, 0);
|
||||||
|
assertEquals(0, samples.size());
|
||||||
|
|
||||||
|
samples = sampleProvider.getActivitySamples(-1, 1);
|
||||||
|
assertEquals(0, samples.size());
|
||||||
|
|
||||||
|
samples = sampleProvider.getActivitySamples(1, -1);
|
||||||
|
assertEquals(0, samples.size());
|
||||||
|
|
||||||
|
// and sleep
|
||||||
|
samples = sampleProvider.getSleepSamples(0, 0);
|
||||||
|
assertEquals(0, samples.size());
|
||||||
|
|
||||||
|
samples = sampleProvider.getSleepSamples(-1, 1);
|
||||||
|
assertEquals(0, samples.size());
|
||||||
|
|
||||||
|
samples = sampleProvider.getSleepSamples(1, -1);
|
||||||
|
assertEquals(0, samples.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
private <T extends AbstractActivitySample> T createSample(SampleProvider<T> sampleProvider, int rawKind, int timestamp, int rawIntensity, int heartRate, int steps, User user, Device device) {
|
||||||
|
T sample = sampleProvider.createActivitySample();
|
||||||
|
sample.setProvider(sampleProvider);
|
||||||
|
sample.setRawKind(rawKind);
|
||||||
|
sample.setTimestamp(timestamp);
|
||||||
|
sample.setRawIntensity(rawIntensity);
|
||||||
|
sample.setHeartRate(heartRate);
|
||||||
|
sample.setSteps(steps);
|
||||||
|
sample.setUserId(user.getId());
|
||||||
|
sample.setDeviceId(device.getId());
|
||||||
|
|
||||||
|
return sample;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSamples() {
|
||||||
|
MiBandSampleProvider sampleProvider = new MiBandSampleProvider(dummyGBDevice, daoSession);
|
||||||
User user = DBHelper.getUser(daoSession);
|
User user = DBHelper.getUser(daoSession);
|
||||||
assertNotNull(user);
|
assertNotNull(user);
|
||||||
|
assertNotNull(user.getId());
|
||||||
|
Device device = DBHelper.getDevice(dummyGBDevice, daoSession);
|
||||||
|
assertNotNull(device);
|
||||||
|
|
||||||
ActivityDescriptionDao descDao = daoSession.getActivityDescriptionDao();
|
MiBandActivitySample s1 = createSample(sampleProvider, MiBandSampleProvider.TYPE_ACTIVITY, 100, 10, 70, 1000, user, device);
|
||||||
assertEquals(0, descDao.count());
|
sampleProvider.addGBActivitySample(s1);
|
||||||
|
sampleProvider.addGBActivitySample(s1); // add again, should not throw or fail
|
||||||
|
|
||||||
List<ActivityDescription> list = DBHelper.findActivityDecriptions(user, 10, 100, daoSession);
|
MiBandActivitySample s2 = createSample(sampleProvider, MiBandSampleProvider.TYPE_ACTIVITY, 200, 20, 80, 1030, user, device);
|
||||||
assertTrue(list.isEmpty());
|
sampleProvider.addGBActivitySample(s2);
|
||||||
|
|
||||||
ActivityDescription desc = DBHelper.createActivityDescription(user, 10, 100, daoSession);
|
MiBandActivitySample s3 = createSample(sampleProvider, MiBandSampleProvider.TYPE_DEEP_SLEEP, 1200, 10, 62, 4030, user, device);
|
||||||
assertNotNull(desc);
|
MiBandActivitySample s4 = createSample(sampleProvider, MiBandSampleProvider.TYPE_LIGHT_SLEEP, 2000, 10, 60, 4030, user, device);
|
||||||
assertEquals(user, desc.getUser());
|
sampleProvider.addGBActivitySamples(new MiBandActivitySample[] { s3, s4 });
|
||||||
assertEquals(10, desc.getTimestampFrom());
|
|
||||||
assertEquals(100, desc.getTimestampTo());
|
|
||||||
List<Tag> tagList = desc.getTagList();
|
|
||||||
assertEquals(0, tagList.size());
|
|
||||||
|
|
||||||
Tag t1 = DBHelper.getTag(user, "Table Tennis", daoSession);
|
// first checks for irrelevant timestamps => no samples
|
||||||
assertNotNull(t1);
|
List<MiBandActivitySample> samples = sampleProvider.getAllActivitySamples(0, 0);
|
||||||
assertEquals("Table Tennis", t1.getName());
|
assertEquals(0, samples.size());
|
||||||
t1.setDescription("Table tennis training for Olympia");
|
|
||||||
tagList.add(t1);
|
|
||||||
|
|
||||||
list = DBHelper.findActivityDecriptions(user, 10, 100, daoSession);
|
samples = sampleProvider.getAllActivitySamples(-1, 1);
|
||||||
assertEquals(1, list.size());
|
assertEquals(0, samples.size());
|
||||||
ActivityDescription desc1 = list.get(0);
|
|
||||||
assertEquals(desc, desc1);
|
|
||||||
assertEquals(1, desc1.getTagList().size());
|
|
||||||
|
|
||||||
// check for partial range overlaps
|
samples = sampleProvider.getAllActivitySamples(1, -1);
|
||||||
list = DBHelper.findActivityDecriptions(user, 20, 80, daoSession);
|
assertEquals(0, samples.size());
|
||||||
assertEquals(1, list.size());
|
|
||||||
|
|
||||||
list = DBHelper.findActivityDecriptions(user, 5, 120, daoSession);
|
// now specific activity kinds
|
||||||
assertEquals(1, list.size());
|
samples = sampleProvider.getActivitySamples(0, 0);
|
||||||
|
assertEquals(0, samples.size());
|
||||||
|
|
||||||
list = DBHelper.findActivityDecriptions(user, 20, 120, daoSession);
|
samples = sampleProvider.getActivitySamples(-1, 1);
|
||||||
assertEquals(1, list.size());
|
assertEquals(0, samples.size());
|
||||||
|
|
||||||
list = DBHelper.findActivityDecriptions(user, 5, 80, daoSession);
|
samples = sampleProvider.getActivitySamples(1, -1);
|
||||||
assertEquals(1, list.size());
|
assertEquals(0, samples.size());
|
||||||
|
|
||||||
// Now with a second, adjacent ActivityDescription
|
// and sleep
|
||||||
ActivityDescription desc2 = DBHelper.createActivityDescription(user, 101, 200, daoSession);
|
samples = sampleProvider.getSleepSamples(0, 0);
|
||||||
|
assertEquals(0, samples.size());
|
||||||
|
|
||||||
list = DBHelper.findActivityDecriptions(user, 10, 100, daoSession);
|
samples = sampleProvider.getSleepSamples(-1, 1);
|
||||||
assertEquals(1, list.size());
|
assertEquals(0, samples.size());
|
||||||
|
|
||||||
list = DBHelper.findActivityDecriptions(user, 20, 80, daoSession);
|
samples = sampleProvider.getSleepSamples(1, -1);
|
||||||
assertEquals(1, list.size());
|
assertEquals(0, samples.size());
|
||||||
|
|
||||||
list = DBHelper.findActivityDecriptions(user, 5, 120, daoSession);
|
// finally checks for existing timestamps
|
||||||
assertEquals(2, list.size());
|
List<MiBandActivitySample> allSamples = sampleProvider.getAllActivitySamples(0, 10000);
|
||||||
|
assertEquals(4, allSamples.size());
|
||||||
|
List<MiBandActivitySample> activitySamples = sampleProvider.getActivitySamples(0, 10000);
|
||||||
|
assertEquals(2, activitySamples.size());
|
||||||
|
List<MiBandActivitySample> sleepSamples = sampleProvider.getSleepSamples(0, 10000);
|
||||||
|
assertEquals(2, sleepSamples.size());
|
||||||
|
|
||||||
list = DBHelper.findActivityDecriptions(user, 20, 120, daoSession);
|
// now with more strict time ranges
|
||||||
assertEquals(2, list.size());
|
allSamples = sampleProvider.getAllActivitySamples(0, 1300);
|
||||||
|
assertEquals(3, allSamples.size());
|
||||||
list = DBHelper.findActivityDecriptions(user, 5, 80, daoSession);
|
activitySamples = sampleProvider.getActivitySamples(10, 150);
|
||||||
assertEquals(1, list.size());
|
assertEquals(1, activitySamples.size());
|
||||||
|
sleepSamples = sampleProvider.getSleepSamples(1500, 2500);
|
||||||
// Now with a third, partially overlapping ActivityDescription
|
assertEquals(1, sleepSamples.size());
|
||||||
ActivityDescription desc3 = DBHelper.createActivityDescription(user, 5, 15, daoSession);
|
|
||||||
|
|
||||||
list = DBHelper.findActivityDecriptions(user, 10, 100, daoSession);
|
|
||||||
assertEquals(2, list.size());
|
|
||||||
|
|
||||||
list = DBHelper.findActivityDecriptions(user, 20, 80, daoSession);
|
|
||||||
assertEquals(1, list.size());
|
|
||||||
|
|
||||||
list = DBHelper.findActivityDecriptions(user, 5, 120, daoSession);
|
|
||||||
assertEquals(3, list.size());
|
|
||||||
|
|
||||||
list = DBHelper.findActivityDecriptions(user, 20, 120, daoSession);
|
|
||||||
assertEquals(2, list.size());
|
|
||||||
|
|
||||||
list = DBHelper.findActivityDecriptions(user, 5, 80, daoSession);
|
|
||||||
assertEquals(2, list.size());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testDeviceAttributes() throws Exception {
|
|
||||||
GBDevice dummyGBDevice = createDummyGDevice("00:00:00:00:02");
|
|
||||||
dummyGBDevice.setFirmwareVersion("1.0");
|
|
||||||
Device deviceOld = DBHelper.getDevice(dummyGBDevice, daoSession);
|
|
||||||
assertNotNull(deviceOld);
|
|
||||||
|
|
||||||
List<DeviceAttributes> attrListOld = deviceOld.getDeviceAttributesList();
|
|
||||||
assertEquals(1, attrListOld.size());
|
|
||||||
assertEquals("1.0", attrListOld.get(0).getFirmwareVersion1());
|
|
||||||
assertEquals("1.0", DBHelper.getDeviceAttributes(deviceOld).getFirmwareVersion1());
|
|
||||||
|
|
||||||
// some time passes, firmware update occurs
|
|
||||||
Thread.sleep(2 * 1000);
|
|
||||||
dummyGBDevice.setFirmwareVersion("2.0");
|
|
||||||
|
|
||||||
Device deviceNew = DBHelper.getDevice(dummyGBDevice, daoSession);
|
|
||||||
assertNotNull(deviceNew);
|
|
||||||
List<DeviceAttributes> attrListNew = deviceNew.getDeviceAttributesList();
|
|
||||||
assertEquals(2, attrListNew.size());
|
|
||||||
assertEquals("2.0", attrListNew.get(0).getFirmwareVersion1());
|
|
||||||
assertEquals("1.0", attrListNew.get(1).getFirmwareVersion1());
|
|
||||||
|
|
||||||
assertEquals("2.0", DBHelper.getDeviceAttributes(deviceNew).getFirmwareVersion1());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue