QuickWizardTest

This commit is contained in:
Milos Kozak 2020-03-19 20:38:25 +01:00
parent 4f295fec4b
commit 7ace6fe3ac
5 changed files with 120 additions and 112 deletions

View file

@ -16,6 +16,7 @@ import java.util.Objects;
import javax.inject.Inject; import javax.inject.Inject;
import dagger.android.HasAndroidInjector; import dagger.android.HasAndroidInjector;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.data.Profile;
import info.nightscout.androidaps.interfaces.Interval; import info.nightscout.androidaps.interfaces.Interval;
@ -74,6 +75,11 @@ public class ProfileSwitch implements Interval, DataPointWithLabelInterface {
@Inject public RxBusWrapper rxBus; @Inject public RxBusWrapper rxBus;
@Inject public ResourceHelper resourceHelper; @Inject public ResourceHelper resourceHelper;
public ProfileSwitch() {
this.injector = MainApp.instance().injector;
injector.androidInjector().inject(this);
}
public ProfileSwitch(HasAndroidInjector injector) { public ProfileSwitch(HasAndroidInjector injector) {
injector.androidInjector().inject(this); injector.androidInjector().inject(this);
this.injector = injector; this.injector = injector;

View file

@ -1,6 +1,6 @@
package info.nightscout.androidaps.utils.wizard package info.nightscout.androidaps.utils.wizard
import info.nightscout.androidaps.MainApp import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.utils.sharedPreferences.SP import info.nightscout.androidaps.utils.sharedPreferences.SP
import org.json.JSONArray import org.json.JSONArray
import org.json.JSONObject import org.json.JSONObject
@ -10,8 +10,9 @@ import javax.inject.Singleton
@Singleton @Singleton
class QuickWizard @Inject constructor( class QuickWizard @Inject constructor(
private val sp: SP, private val sp: SP,
private val mainApp: MainApp private val injector: HasAndroidInjector
){ ) {
private var storage = JSONArray() private var storage = JSONArray()
init { init {
@ -20,7 +21,7 @@ class QuickWizard @Inject constructor(
fun getActive(): QuickWizardEntry? { fun getActive(): QuickWizardEntry? {
for (i in 0 until storage.length()) { for (i in 0 until storage.length()) {
val entry = QuickWizardEntry(mainApp).from(storage.get(i) as JSONObject, i) val entry = QuickWizardEntry(injector).from(storage.get(i) as JSONObject, i)
if (entry.isActive()) return entry if (entry.isActive()) return entry
} }
return null return null
@ -37,11 +38,10 @@ class QuickWizard @Inject constructor(
fun size(): Int = storage.length() fun size(): Int = storage.length()
operator fun get(position: Int): QuickWizardEntry = operator fun get(position: Int): QuickWizardEntry =
QuickWizardEntry(mainApp).from(storage.get(position) as JSONObject, position) QuickWizardEntry(injector).from(storage.get(position) as JSONObject, position)
fun newEmptyItem(): QuickWizardEntry { fun newEmptyItem(): QuickWizardEntry {
return QuickWizardEntry(mainApp) return QuickWizardEntry(injector)
} }
fun addOrUpdate(newItem: QuickWizardEntry) { fun addOrUpdate(newItem: QuickWizardEntry) {

View file

@ -1,7 +1,6 @@
package info.nightscout.androidaps.utils.wizard package info.nightscout.androidaps.utils.wizard
import dagger.android.HasAndroidInjector import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.R import info.nightscout.androidaps.R
import info.nightscout.androidaps.data.Profile import info.nightscout.androidaps.data.Profile
import info.nightscout.androidaps.db.BgReading import info.nightscout.androidaps.db.BgReading
@ -20,9 +19,8 @@ import org.json.JSONObject
import java.util.* import java.util.*
import javax.inject.Inject import javax.inject.Inject
class QuickWizardEntry @Inject constructor(private val mainApp: MainApp) { class QuickWizardEntry @Inject constructor(private val injector: HasAndroidInjector) {
@Inject lateinit var injector: HasAndroidInjector
@Inject lateinit var aapsLogger: AAPSLogger @Inject lateinit var aapsLogger: AAPSLogger
@Inject lateinit var sp: SP @Inject lateinit var sp: SP
@Inject lateinit var profileFunction: ProfileFunction @Inject lateinit var profileFunction: ProfileFunction
@ -41,7 +39,7 @@ class QuickWizardEntry @Inject constructor(private val mainApp: MainApp) {
} }
init { init {
mainApp.androidInjector().inject(this) injector.androidInjector().inject(this)
val emptyData = "{\"buttonText\":\"\",\"carbs\":0,\"validFrom\":0,\"validTo\":86340}" val emptyData = "{\"buttonText\":\"\",\"carbs\":0,\"validFrom\":0,\"validTo\":86340}"
try { try {
storage = JSONObject(emptyData) storage = JSONObject(emptyData)
@ -119,7 +117,7 @@ class QuickWizardEntry @Inject constructor(private val mainApp: MainApp) {
trend = true trend = true
} }
val percentage = sp.getDouble(R.string.key_boluswizard_percentage, 100.0) val percentage = sp.getDouble(R.string.key_boluswizard_percentage, 100.0)
return BolusWizard(mainApp).doCalc(profile, profileName, tempTarget, carbs(), cob, bg, 0.0, percentage, true, useCOB() == YES, bolusIOB, basalIOB, superBolus, useTempTarget() == YES, trend, "QuickWizard") return BolusWizard(injector).doCalc(profile, profileName, tempTarget, carbs(), cob, bg, 0.0, percentage, true, useCOB() == YES, bolusIOB, basalIOB, superBolus, useTempTarget() == YES, trend, "QuickWizard")
} }
fun buttonText(): String = safeGetString(storage, "buttonText", "") fun buttonText(): String = safeGetString(storage, "buttonText", "")

View file

@ -1,100 +0,0 @@
package info.nightscout.androidaps.data;
import org.json.JSONArray;
import org.json.JSONException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import info.AAPSMocker;
import info.SPMocker;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.utils.SP;
import info.nightscout.androidaps.utils.wizard.QuickWizard;
import info.nightscout.androidaps.utils.wizard.QuickWizardEntry;
@RunWith(PowerMockRunner.class)
@PrepareForTest({SP.class, MainApp.class, Profile.class})
public class QuickWizardTest {
private String data1 = "{\"buttonText\":\"Meal\",\"carbs\":36,\"validFrom\":0,\"validTo\":18000," +
"\"useBG\":0,\"useCOB\":0,\"useBolusIOB\":0,\"useBasalIOB\":0,\"useTrend\":0,\"useSuperBolus\":0,\"useTemptarget\":0}";
private String data2 = "{\"buttonText\":\"Lunch\",\"carbs\":18,\"validFrom\":36000,\"validTo\":39600," +
"\"useBG\":0,\"useCOB\":0,\"useBolusIOB\":1,\"useBasalIOB\":2,\"useTrend\":0,\"useSuperBolus\":0,\"useTemptarget\":0}";
private JSONArray array;
public QuickWizardTest() {
try {
array = new JSONArray("[" + data1 + "," + data2 + "]");
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
@Before
public void mock() throws Exception {
AAPSMocker.mockMainApp();
SPMocker.prepareMock();
PowerMockito.mockStatic(Profile.class);
PowerMockito.when(Profile.class, "secondsFromMidnight").thenReturn(0);
}
@Test
public void setDataTest() {
QuickWizard.INSTANCE.setData(array);
Assert.assertEquals(2, QuickWizard.INSTANCE.size());
}
@Test
public void saveTest() {
QuickWizard.INSTANCE.setData(array);
QuickWizard.INSTANCE.save();
Assert.assertEquals("[{\"useBolusIOB\":0,\"buttonText\":\"Meal\",\"useTrend\":0,\"carbs\":36,\"useCOB\":0,\"useBasalIOB\":0,\"useTemptarget\":0,\"useBG\":0,\"validFrom\":0,\"useSuperBolus\":0,\"validTo\":18000},{\"useBolusIOB\":1,\"buttonText\":\"Lunch\",\"useTrend\":0,\"carbs\":18,\"useCOB\":0,\"useBasalIOB\":2,\"useTemptarget\":0,\"useBG\":0,\"validFrom\":36000,\"useSuperBolus\":0,\"validTo\":39600}]", SP.getString("QuickWizard", "d"));
}
@Test
public void getTest() {
QuickWizard.INSTANCE.setData(array);
Assert.assertEquals("Lunch", QuickWizard.INSTANCE.get(1).buttonText());
}
@Test
public void getActive() {
QuickWizard.INSTANCE.setData(array);
QuickWizardEntry e = QuickWizard.INSTANCE.getActive();
Assert.assertEquals(36d, e.carbs(), 0.01d);
QuickWizard.INSTANCE.remove(0);
QuickWizard.INSTANCE.remove(0);
Assert.assertNull(QuickWizard.INSTANCE.getActive());
}
@Test
public void newEmptyItemTest() {
Assert.assertNotNull(QuickWizard.INSTANCE.newEmptyItem());
}
@Test
public void addOrUpdate() {
QuickWizard.INSTANCE.setData(array);
Assert.assertEquals(2, QuickWizard.INSTANCE.size());
QuickWizard.INSTANCE.addOrUpdate(QuickWizard.INSTANCE.newEmptyItem());
Assert.assertEquals(3, QuickWizard.INSTANCE.size());
QuickWizardEntry q = QuickWizard.INSTANCE.newEmptyItem();
q.position = 0;
QuickWizard.INSTANCE.addOrUpdate(q);
Assert.assertEquals(3, QuickWizard.INSTANCE.size());
}
@Test
public void remove() {
QuickWizard.INSTANCE.setData(array);
QuickWizard.INSTANCE.remove(0);
Assert.assertEquals(1, QuickWizard.INSTANCE.size());
}
}

View file

@ -0,0 +1,104 @@
package info.nightscout.androidaps.data
import dagger.android.AndroidInjector
import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.TestBase
import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunction
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin
import info.nightscout.androidaps.utils.sharedPreferences.SP
import info.nightscout.androidaps.utils.wizard.QuickWizard
import info.nightscout.androidaps.utils.wizard.QuickWizardEntry
import org.json.JSONArray
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito.`when`
import org.powermock.api.mockito.PowerMockito
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.PowerMockRunner
@RunWith(PowerMockRunner::class)
@PrepareForTest(Profile::class)
class QuickWizardTest : TestBase() {
@Mock lateinit var aapsLogger: AAPSLogger
@Mock lateinit var sp: SP
@Mock lateinit var profileFunction: ProfileFunction
@Mock lateinit var treatmentsPlugin: TreatmentsPlugin
@Mock lateinit var loopPlugin: LoopPlugin
@Mock lateinit var iobCobCalculatorPlugin: IobCobCalculatorPlugin
private val data1 = "{\"buttonText\":\"Meal\",\"carbs\":36,\"validFrom\":0,\"validTo\":18000," +
"\"useBG\":0,\"useCOB\":0,\"useBolusIOB\":0,\"useBasalIOB\":0,\"useTrend\":0,\"useSuperBolus\":0,\"useTemptarget\":0}"
private val data2 = "{\"buttonText\":\"Lunch\",\"carbs\":18,\"validFrom\":36000,\"validTo\":39600," +
"\"useBG\":0,\"useCOB\":0,\"useBolusIOB\":1,\"useBasalIOB\":2,\"useTrend\":0,\"useSuperBolus\":0,\"useTemptarget\":0}"
var array: JSONArray = JSONArray("[$data1,$data2]")
val injector = HasAndroidInjector {
AndroidInjector {
if (it is QuickWizardEntry) {
it.aapsLogger = aapsLogger
it.sp = sp
it.profileFunction = profileFunction
it.treatmentsPlugin = treatmentsPlugin
it.loopPlugin = loopPlugin
it.iobCobCalculatorPlugin = iobCobCalculatorPlugin
}
}
}
private lateinit var quickWizard : QuickWizard
@Before
fun mock() {
PowerMockito.mockStatic(Profile::class.java)
PowerMockito.`when`<Any>(Profile::class.java, "secondsFromMidnight").thenReturn(0)
`when`(sp.getString("QuickWizard", "[]")).thenReturn("[]")
quickWizard = QuickWizard(sp, injector)
}
@Test fun setDataTest() {
quickWizard.setData(array)
Assert.assertEquals(2, quickWizard.size())
}
@Test fun test() {
quickWizard.setData(array)
Assert.assertEquals("Lunch", quickWizard.get(1).buttonText())
}
@Test fun active() {
quickWizard.setData(array)
val e: QuickWizardEntry = quickWizard.getActive()!!
Assert.assertEquals(36.0, e.carbs().toDouble(), 0.01)
quickWizard.remove(0)
quickWizard.remove(0)
Assert.assertNull(quickWizard.getActive())
}
@Test fun newEmptyItemTest() {
Assert.assertNotNull(quickWizard.newEmptyItem())
}
@Test fun addOrUpdate() {
quickWizard.setData(array)
Assert.assertEquals(2, quickWizard.size())
quickWizard.addOrUpdate(quickWizard.newEmptyItem())
Assert.assertEquals(3, quickWizard.size())
val q: QuickWizardEntry = quickWizard.newEmptyItem()
q.position = 0
quickWizard.addOrUpdate(q)
Assert.assertEquals(3, quickWizard.size())
}
@Test fun remove() {
quickWizard.setData(array)
quickWizard.remove(0)
Assert.assertEquals(1, quickWizard.size())
}
}