1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-27 20:36:51 +01:00

Add robolectric dependency and addd a first EntitiesTest

Use KitKat (19) as target sdk since robolectric 3.1.2/sqlite4java
does not understand "WITHOUT ROWID" tables.

Also, add constants for user's gender and document some things.
This commit is contained in:
cpfeiffer 2016-08-24 22:54:32 +02:00
parent 02ac70e2a7
commit bfc0b4faaf
6 changed files with 111 additions and 4 deletions

View File

@ -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;

View File

@ -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'

View File

@ -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;
}

View File

@ -135,7 +135,19 @@ public class FileUtils {
@NonNull
private static List<File> 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");
}

View File

@ -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()));
}
}

View File

@ -15,6 +15,9 @@ buildscript {
allprojects {
repositories {
jcenter()
maven {
url "https://repo1.maven.org"
}
maven {
url "https://jitpack.io"
}