diff --git a/GBDaoGenerator/src/nodomain/freeyourgadget/gadgetbridge/daogen/GBDaoGenerator.java b/GBDaoGenerator/src/nodomain/freeyourgadget/gadgetbridge/daogen/GBDaoGenerator.java index bc1eeefde..d47f698db 100644 --- a/GBDaoGenerator/src/nodomain/freeyourgadget/gadgetbridge/daogen/GBDaoGenerator.java +++ b/GBDaoGenerator/src/nodomain/freeyourgadget/gadgetbridge/daogen/GBDaoGenerator.java @@ -91,8 +91,8 @@ public class GBDaoGenerator { userAttributes.addIdProperty(); userAttributes.addIntProperty("heightCM").notNull(); userAttributes.addIntProperty("weightKG").notNull(); - userAttributes.addIntProperty("sleepGoalHPD"); - userAttributes.addIntProperty("stepsGoalSPD"); + userAttributes.addIntProperty("sleepGoalHPD").javaDocGetterAndSetter("Desired number of hours of sleep per day."); + userAttributes.addIntProperty("stepsGoalSPD").javaDocGetterAndSetter("Desired number of steps per day."); addDateValidityTo(userAttributes); return userAttributes; diff --git a/app/build.gradle b/app/build.gradle index 381f28e1c..afa03ee03 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -48,6 +48,7 @@ dependencies { // testCompile 'ch.qos.logback:logback-core:1.1.3' testCompile 'junit:junit:4.12' testCompile "org.mockito:mockito-core:1.9.5" + testCompile "org.robolectric:robolectric:3.1.2" compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:23.3.0' diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/ActivityUser.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/ActivityUser.java index 93358df2d..3ed594083 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/ActivityUser.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/ActivityUser.java @@ -14,6 +14,10 @@ import nodomain.freeyourgadget.gadgetbridge.util.Prefs; */ public class ActivityUser { + public static final int GENDER_FEMALE = 0; + public static final int GENDER_MALE = 1; + public static final int GENDER_OTHER = 2; + private String activityUserName; private int activityUserGender; private int activityUserYearOfBirth; @@ -23,7 +27,7 @@ public class ActivityUser { private int activityUserStepsGoal; private static final String defaultUserName = "gadgetbridge-user"; - public static final int defaultUserGender = 0; + public static final int defaultUserGender = GENDER_FEMALE; public static final int defaultUserYearOfBirth = 0; public static final int defaultUserAge = 0; public static final int defaultUserHeightCm = 175; @@ -51,6 +55,11 @@ public class ActivityUser { return activityUserWeightKg; } + /** + * @see #GENDER_FEMALE + * @see #GENDER_MALE + * @see #GENDER_OTHER + */ public int getGender() { return activityUserGender; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/FileUtils.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/FileUtils.java index 5fbb34f12..37f95f621 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/FileUtils.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/FileUtils.java @@ -135,7 +135,19 @@ public class FileUtils { @NonNull private static List getWritableExternalFilesDirs() throws IOException { Context context = GBApplication.getContext(); - File[] dirs = context.getExternalFilesDirs(null); + File[] dirs; + try { + dirs = context.getExternalFilesDirs(null); + } catch (NullPointerException ex) { + // workaround for robolectric 3.1.2 not implementinc getExternalFilesDirs() + // https://github.com/robolectric/robolectric/issues/2531 + File dir = context.getExternalFilesDir(null); + if (dir != null) { + dirs = new File[] { dir }; + } else { + throw ex; + } + } if (dirs == null) { throw new IOException("Unable to access external files dirs: null"); } diff --git a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/database/EntitiesTest.java b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/database/EntitiesTest.java new file mode 100644 index 000000000..c5baecfe6 --- /dev/null +++ b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/database/EntitiesTest.java @@ -0,0 +1,82 @@ +package nodomain.freeyourgadget.gadgetbridge.database; + +import android.database.sqlite.SQLiteDatabase; + +import org.junit.Before; +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 nodomain.freeyourgadget.gadgetbridge.BuildConfig; +import nodomain.freeyourgadget.gadgetbridge.entities.DaoMaster; +import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession; +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.model.ActivityUser; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNotNull; +import static junit.framework.Assert.assertNull; + +@RunWith(RobolectricTestRunner.class) +@Config(constants = BuildConfig.class, sdk = 19) // need sdk 19 because "WITHOUT ROWID" is not supported in robolectric/sqlite4java +public class EntitiesTest { + + private DaoSession daoSession; + private UserDao userDao; + private UserAttributesDao userAttributesDao; + + @Before + public void setUp() { + DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(RuntimeEnvironment.application, null, null); + SQLiteDatabase db = openHelper.getWritableDatabase(); + daoSession = new DaoMaster(db).newSession(); + userDao = daoSession.getUserDao(); + userAttributesDao = daoSession.getUserAttributesDao(); + } + + @Test + public void testUser() { + User user = new User(); + user.setName("Peter"); + user.setGender(ActivityUser.GENDER_FEMALE); + Calendar cal = GregorianCalendar.getInstance(); + cal.add(Calendar.YEAR, -20); + user.setBirthday(cal.getTime()); + UserAttributes attributes = new UserAttributes(); + attributes.setWeightKG(55); + attributes.setHeightCM(170); + attributes.setSleepGoalHPD(8); + attributes.setStepsGoalSPD(10000); + + daoSession.insert(user); + assertNotNull(user.getId()); + + attributes.setUserId(user.getId()); + daoSession.insert(attributes); + user.getUserAttributesList().add(attributes); + + assertNotNull(userDao.load(user.getId())); + assertEquals(1, userDao.count()); + assertEquals(1, daoSession.loadAll(User.class).size()); + + assertNotNull(userAttributesDao.load(attributes.getId())); + assertEquals(1, userAttributesDao.count()); + assertEquals(1, daoSession.loadAll(UserAttributes.class).size()); + + daoSession.update(user); + daoSession.delete(user); + daoSession.delete(attributes); + daoSession.delete(attributes); + assertNull(userDao.load(user.getId())); + } + + +} diff --git a/build.gradle b/build.gradle index 29aa94b67..14aa16c71 100644 --- a/build.gradle +++ b/build.gradle @@ -15,6 +15,9 @@ buildscript { allprojects { repositories { jcenter() + maven { + url "https://repo1.maven.org" + } maven { url "https://jitpack.io" }