From 49b8b9ebca7273189f352835a212be4218110376 Mon Sep 17 00:00:00 2001 From: cpfeiffer Date: Thu, 25 Aug 2016 00:00:53 +0200 Subject: [PATCH] More robolectric stuff - guard against multiple GBApplication.onCreate() invocations - test DBHelper.getDevice() for a start --- TODO.md | 1 + .../gadgetbridge/GBApplication.java | 5 +++ .../gadgetbridge/database/EntitiesTest.java | 37 ++++++++++++++++++- 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/TODO.md b/TODO.md index 0921e3a9..1f97d55d 100644 --- a/TODO.md +++ b/TODO.md @@ -3,6 +3,7 @@ TODO before 0.12.0 release: * ~~Support importing Pebble Health data from old database~~ DONE, needs check. * Fix user attribute table being spammed * Add onboarding activity on first startup (to merge old data) +* Add hwVersion / revision to Device Non blocking issues: diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java index 6ecca503..f03d0e01 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java @@ -99,6 +99,11 @@ public class GBApplication extends Application { public void onCreate() { super.onCreate(); + if (lockHandler != null) { + // guard against multiple invocations (robolectric) + return; + } + sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); prefs = new Prefs(sharedPrefs); gbPrefs = new GBPrefs(prefs); diff --git a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/database/EntitiesTest.java b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/database/EntitiesTest.java index c5baecfe..4f13f234 100644 --- a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/database/EntitiesTest.java +++ b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/database/EntitiesTest.java @@ -2,6 +2,7 @@ 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.runner.RunWith; @@ -13,13 +14,19 @@ import java.util.Calendar; import java.util.GregorianCalendar; import nodomain.freeyourgadget.gadgetbridge.BuildConfig; +import nodomain.freeyourgadget.gadgetbridge.GBApplication; +import nodomain.freeyourgadget.gadgetbridge.GBException; import nodomain.freeyourgadget.gadgetbridge.entities.DaoMaster; import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession; +import nodomain.freeyourgadget.gadgetbridge.entities.Device; +import nodomain.freeyourgadget.gadgetbridge.entities.DeviceAttributes; 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.model.ActivityUser; +import nodomain.freeyourgadget.gadgetbridge.model.DeviceType; import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertNotNull; @@ -32,16 +39,26 @@ public class EntitiesTest { private DaoSession daoSession; private UserDao userDao; private UserAttributesDao userAttributesDao; + private DBHandler dbHandler; + private GBApplication app = (GBApplication) RuntimeEnvironment.application; @Before - public void setUp() { - DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(RuntimeEnvironment.application, null, null); + public void setUp() throws GBException { +// dbHandler = GBApplication.acquireDB(); +// daoSession = dbHandler.getDaoSession(); + DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(app, null, null); SQLiteDatabase db = openHelper.getWritableDatabase(); daoSession = new DaoMaster(db).newSession(); userDao = daoSession.getUserDao(); userAttributesDao = daoSession.getUserAttributesDao(); } + @After + public void tearDown() { +// GBApplication.releaseDB(); + } + + @Test public void testUser() { User user = new User(); @@ -78,5 +95,21 @@ public class EntitiesTest { assertNull(userDao.load(user.getId())); } + @Test + public void testDBHelper() { +// DBHelper dbHelper = new DBHelper(RuntimeEnvironment.application); + GBDevice dummyGBDevice = new GBDevice("00:00:00:00:00", "Testie", DeviceType.TEST); + dummyGBDevice.setFirmwareVersion("1.2.3"); + dummyGBDevice.setHardwareVersion("4.0"); + Device device = DBHelper.getDevice(dummyGBDevice, daoSession); + assertNotNull(device); + assertEquals("00:00:00:00:00", device.getIdentifier()); + assertEquals("Testie", device.getName()); +// assertEquals("4.0", device.get()); + assertEquals(DeviceType.TEST.getKey(), device.getType()); + DeviceAttributes attributes = device.getDeviceAttributesList().get(0); + assertNotNull(attributes); + assertEquals("1.2.3", attributes.getFirmwareVersion1()); + } }