diff --git a/android_app/app/src/androidTest/java/com/health/openscale/gui/AddMeasurementTest.java b/android_app/app/src/androidTest/java/com/health/openscale/gui/AddMeasurementTest.java new file mode 100644 index 00000000..e0b914ed --- /dev/null +++ b/android_app/app/src/androidTest/java/com/health/openscale/gui/AddMeasurementTest.java @@ -0,0 +1,106 @@ +/* Copyright (C) 2018 olie.xdev +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see +*/ +package com.health.openscale.gui; + +import android.content.Context; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; +import android.support.test.InstrumentationRegistry; +import android.support.test.espresso.ViewInteraction; +import android.support.test.espresso.contrib.PickerActions; +import android.support.test.rule.ActivityTestRule; +import android.support.test.runner.AndroidJUnit4; +import android.test.suitebuilder.annotation.LargeTest; +import android.view.View; +import android.view.ViewGroup; +import android.view.ViewParent; +import android.widget.DatePicker; + +import com.health.openscale.R; +import com.health.openscale.core.OpenScale; +import com.health.openscale.core.datatypes.ScaleMeasurement; +import com.health.openscale.core.datatypes.ScaleUser; +import com.health.openscale.gui.activities.BaseAppCompatActivity; + +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.Matchers; +import org.hamcrest.TypeSafeMatcher; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.util.Calendar; + +import static android.support.test.espresso.Espresso.onView; +import static android.support.test.espresso.action.ViewActions.click; +import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard; +import static android.support.test.espresso.action.ViewActions.replaceText; +import static android.support.test.espresso.action.ViewActions.scrollTo; +import static android.support.test.espresso.matcher.ViewMatchers.withClassName; +import static android.support.test.espresso.matcher.ViewMatchers.withId; +import static org.hamcrest.Matchers.allOf; +import static org.junit.Assert.assertEquals; + +@LargeTest +@RunWith(AndroidJUnit4.class) +public class AddMeasurementTest { + private static final double DELTA = 1e-15; + + private static Context context; + private static OpenScale openScale; + + private static final ScaleUser male = TestData.getMaleUser(); + private static final ScaleUser female = TestData.getFemaleUser(); + + @Rule + public ActivityTestRule mActivityTestRule = new ActivityTestRule<>(MainActivity.class, false, true); + + @BeforeClass + public static void initTest() { + context = InstrumentationRegistry.getTargetContext(); + + openScale = OpenScale.getInstance(); + + openScale.addScaleUser(male); + openScale.addScaleUser(female); + + // Set first start to true to get the user add dialog + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + prefs.edit() + .putBoolean("firstStart", false) + .putString(BaseAppCompatActivity.PREFERENCE_LANGUAGE, "en") + .putInt("selectedUserId", male.getId()) + .commit(); + } + + @AfterClass + public static void addMeasurementVerification() { + openScale.deleteScaleUser(male.getId()); + openScale.deleteScaleUser(female.getId()); + } + + @Test + public void addMeasurementTest() { + onView(withId(R.id.action_add_measurement)).perform(click()); + + ScaleMeasurement measurement = TestData.getMeasurement(1); + } +} diff --git a/android_app/app/src/androidTest/java/com/health/openscale/gui/UserAddTest.java b/android_app/app/src/androidTest/java/com/health/openscale/gui/AddUserTest.java similarity index 98% rename from android_app/app/src/androidTest/java/com/health/openscale/gui/UserAddTest.java rename to android_app/app/src/androidTest/java/com/health/openscale/gui/AddUserTest.java index f6e97f09..54a7089f 100644 --- a/android_app/app/src/androidTest/java/com/health/openscale/gui/UserAddTest.java +++ b/android_app/app/src/androidTest/java/com/health/openscale/gui/AddUserTest.java @@ -61,7 +61,7 @@ import static org.junit.Assert.assertEquals; @LargeTest @RunWith(AndroidJUnit4.class) -public class UserAddTest { +public class AddUserTest { private static final double DELTA = 1e-15; private Context context; @@ -82,7 +82,7 @@ public class UserAddTest { } @After - public void verifyUserAdd() { + public void addUserVerification() { ScaleUser user = OpenScale.getInstance().getSelectedScaleUser(); assertEquals("test", user.getUserName()); @@ -107,10 +107,12 @@ public class UserAddTest { goalDate.set(Calendar.HOUR_OF_DAY, 0); assertEquals(goalDate.getTime().getTime(), user.getGoalDate().getTime()); + + OpenScale.getInstance().deleteScaleUser(user.getId()); } @Test - public void userAddTest() { + public void addUserTest() { mActivityTestRule.launchActivity(null); ViewInteraction editText = onView( diff --git a/android_app/app/src/androidTest/java/com/health/openscale/gui/TestData.java b/android_app/app/src/androidTest/java/com/health/openscale/gui/TestData.java new file mode 100644 index 00000000..7c96be62 --- /dev/null +++ b/android_app/app/src/androidTest/java/com/health/openscale/gui/TestData.java @@ -0,0 +1,108 @@ +/* Copyright (C) 2018 olie.xdev + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + */ +package com.health.openscale.gui; + +import com.health.openscale.core.datatypes.ScaleMeasurement; +import com.health.openscale.core.datatypes.ScaleUser; +import com.health.openscale.core.utils.Converters; + +import java.util.Calendar; +import java.util.Date; +import java.util.Random; + +public class TestData { + private static Random rand = new Random(); + + public static ScaleUser getMaleUser() { + ScaleUser male = new ScaleUser(); + + male.setUserName("Bob"); + male.setGender(Converters.Gender.MALE); + male.setInitialWeight(80.0f); + male.setScaleUnit(Converters.WeightUnit.KG); + male.setActivityLevel(Converters.ActivityLevel.MILD); + male.setBodyHeight(180.0f); + male.setGoalWeight(60.0f); + male.setMeasureUnit(Converters.MeasureUnit.CM); + male.setBirthday(getDateFromYears(-20)); + male.setGoalDate(getDateFromYears(2)); + + return male; + } + + public static ScaleUser getFemaleUser() { + ScaleUser female = new ScaleUser(); + + female.setUserName("Alice"); + female.setGender(Converters.Gender.FEMALE); + female.setInitialWeight(70.0f); + female.setScaleUnit(Converters.WeightUnit.LB); + female.setActivityLevel(Converters.ActivityLevel.EXTREME); + female.setBodyHeight(160.0f); + female.setGoalWeight(50.0f); + female.setMeasureUnit(Converters.MeasureUnit.INCH); + female.setBirthday(getDateFromYears(-25)); + female.setGoalDate(getDateFromYears(1)); + + return female; + } + + public static ScaleMeasurement getMeasurement(int nr) { + ScaleMeasurement measurement = new ScaleMeasurement(); + + rand.setSeed(nr); + + measurement.setDateTime(getDateFromDays(nr)); + measurement.setWeight(100.0f + getRandNumberInRange(0,50)); + measurement.setFat(30.0f + getRandNumberInRange(0,30)); + measurement.setWater(50.0f + getRandNumberInRange(0,20)); + measurement.setMuscle(40.0f + getRandNumberInRange(0,15)); + measurement.setLbm(20.0f + getRandNumberInRange(0,10)); + measurement.setBone(8.0f + getRandNumberInRange(0,50)); + measurement.setWaist(50.0f + getRandNumberInRange(0,50)); + measurement.setHip(60.0f + getRandNumberInRange(0,50)); + measurement.setChest(80.0f + getRandNumberInRange(0,50)); + measurement.setThigh(40.0f + getRandNumberInRange(0,50)); + measurement.setBiceps(30.0f + getRandNumberInRange(0,50)); + measurement.setNeck(15.0f + getRandNumberInRange(0,50)); + measurement.setCaliper1(5.0f + getRandNumberInRange(0,10)); + measurement.setCaliper2(10.0f + getRandNumberInRange(0,10)); + measurement.setCaliper3(7.0f + getRandNumberInRange(0,10)); + measurement.setComment("my comment " + nr); + + return measurement; + } + + private static Date getDateFromYears(int years) { + Calendar currentTime = Calendar.getInstance(); + + currentTime.add(Calendar.YEAR, years); + + return currentTime.getTime(); + } + + private static Date getDateFromDays(int days) { + Calendar currentTime = Calendar.getInstance(); + + currentTime.add(Calendar.DAY_OF_YEAR, days); + + return currentTime.getTime(); + } + + private static float getRandNumberInRange(int min, int max) { + return (float)(rand.nextInt(max*10 - min*10) + min*10) / 10.0f; + } +}