1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2025-01-12 18:57:36 +01:00

More robolectric stuff

- guard against multiple GBApplication.onCreate() invocations
- test DBHelper.getDevice() for a start
This commit is contained in:
cpfeiffer 2016-08-25 00:00:53 +02:00
parent 38c4be4379
commit 49b8b9ebca
3 changed files with 41 additions and 2 deletions

View File

@ -3,6 +3,7 @@ TODO before 0.12.0 release:
* ~~Support importing Pebble Health data from old database~~ DONE, needs check. * ~~Support importing Pebble Health data from old database~~ DONE, needs check.
* Fix user attribute table being spammed * Fix user attribute table being spammed
* Add onboarding activity on first startup (to merge old data) * Add onboarding activity on first startup (to merge old data)
* Add hwVersion / revision to Device
Non blocking issues: Non blocking issues:

View File

@ -99,6 +99,11 @@ public class GBApplication extends Application {
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
if (lockHandler != null) {
// guard against multiple invocations (robolectric)
return;
}
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs = new Prefs(sharedPrefs); prefs = new Prefs(sharedPrefs);
gbPrefs = new GBPrefs(prefs); gbPrefs = new GBPrefs(prefs);

View File

@ -2,6 +2,7 @@ package nodomain.freeyourgadget.gadgetbridge.database;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -13,13 +14,19 @@ import java.util.Calendar;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import nodomain.freeyourgadget.gadgetbridge.BuildConfig; 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.DaoMaster;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession; 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.User;
import nodomain.freeyourgadget.gadgetbridge.entities.UserAttributes; import nodomain.freeyourgadget.gadgetbridge.entities.UserAttributes;
import nodomain.freeyourgadget.gadgetbridge.entities.UserAttributesDao; import nodomain.freeyourgadget.gadgetbridge.entities.UserAttributesDao;
import nodomain.freeyourgadget.gadgetbridge.entities.UserDao; import nodomain.freeyourgadget.gadgetbridge.entities.UserDao;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser; import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull; import static junit.framework.Assert.assertNotNull;
@ -32,16 +39,26 @@ public class EntitiesTest {
private DaoSession daoSession; private DaoSession daoSession;
private UserDao userDao; private UserDao userDao;
private UserAttributesDao userAttributesDao; private UserAttributesDao userAttributesDao;
private DBHandler dbHandler;
private GBApplication app = (GBApplication) RuntimeEnvironment.application;
@Before @Before
public void setUp() { public void setUp() throws GBException {
DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(RuntimeEnvironment.application, null, null); // dbHandler = GBApplication.acquireDB();
// daoSession = dbHandler.getDaoSession();
DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(app, null, null);
SQLiteDatabase db = openHelper.getWritableDatabase(); SQLiteDatabase db = openHelper.getWritableDatabase();
daoSession = new DaoMaster(db).newSession(); daoSession = new DaoMaster(db).newSession();
userDao = daoSession.getUserDao(); userDao = daoSession.getUserDao();
userAttributesDao = daoSession.getUserAttributesDao(); userAttributesDao = daoSession.getUserAttributesDao();
} }
@After
public void tearDown() {
// GBApplication.releaseDB();
}
@Test @Test
public void testUser() { public void testUser() {
User user = new User(); User user = new User();
@ -78,5 +95,21 @@ public class EntitiesTest {
assertNull(userDao.load(user.getId())); 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());
}
} }