diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopPlugin.java index fefd50f132..93fcef0f0b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopPlugin.java @@ -68,6 +68,7 @@ import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.T; import info.nightscout.androidaps.utils.resources.ResourceHelper; import info.nightscout.androidaps.utils.sharedPreferences.SP; +import info.nightscout.androidaps.utils.HardLimits; import io.reactivex.disposables.CompositeDisposable; import io.reactivex.schedulers.Schedulers; @@ -88,6 +89,7 @@ public class LoopPlugin extends PluginBase { private final IobCobCalculatorPlugin iobCobCalculatorPlugin; private final ReceiverStatusStore receiverStatusStore; private final FabricPrivacy fabricPrivacy; + private final HardLimits hardLimits; private CompositeDisposable disposable = new CompositeDisposable(); @@ -132,7 +134,8 @@ public class LoopPlugin extends PluginBase { Lazy actionStringHandler, // TODO Adrian use RxBus instead of Lazy IobCobCalculatorPlugin iobCobCalculatorPlugin, ReceiverStatusStore receiverStatusStore, - FabricPrivacy fabricPrivacy + FabricPrivacy fabricPrivacy, + HardLimits hardLimits ) { super(new PluginDescription() .mainType(PluginType.LOOP) @@ -158,6 +161,7 @@ public class LoopPlugin extends PluginBase { this.iobCobCalculatorPlugin = iobCobCalculatorPlugin; this.receiverStatusStore = receiverStatusStore; this.fabricPrivacy = fabricPrivacy; + this.hardLimits = hardLimits; loopSuspendedTill = sp.getLong("loopSuspendedTill", 0L); isSuperBolus = sp.getBoolean("isSuperBolus", false); @@ -288,6 +292,21 @@ public class LoopPlugin extends PluginBase { return true; } + public boolean isLGS(){ + Constraint closedLoopEnabled = constraintChecker.isClosedLoopAllowed(); + Double MaxIOBallowed = constraintChecker.getMaxIOBAllowed().value(); + String APSmode = sp.getString(R.string.key_aps_mode, "open"); + PumpInterface pump = activePlugin.getActivePump(); + boolean isLGS = false; + + if (!isSuspended() && !pump.isSuspended()) + if (closedLoopEnabled.value()) + if ((MaxIOBallowed.equals(hardLimits.getMAXIOB_LGS())) || (APSmode.equals("lgs"))) + isLGS = true; + + return isLGS; + } + public boolean isSuperBolus() { if (loopSuspendedTill == 0) return false; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/constraints/safety/SafetyPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/safety/SafetyPlugin.java index a32b7233e1..ab325f1f0c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/constraints/safety/SafetyPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/safety/SafetyPlugin.java @@ -98,7 +98,7 @@ public class SafetyPlugin extends PluginBase implements ConstraintsInterface { @NonNull @Override public Constraint isClosedLoopAllowed(@NonNull Constraint value) { String mode = sp.getString(R.string.key_aps_mode, "open"); - if (!mode.equals("closed")) + if ((mode.equals("open"))) value.set(getAapsLogger(), false, getResourceHelper().gs(R.string.closedmodedisabledinpreferences), this); if (!buildHelper.isEngineeringModeOrRelease()) { @@ -266,6 +266,7 @@ public class SafetyPlugin extends PluginBase implements ConstraintsInterface { @NonNull @Override public Constraint applyMaxIOBConstraints(@NonNull Constraint maxIob) { double maxIobPref; + String apsmode = sp.getString(R.string.key_aps_mode, "open"); if (openAPSSMBPlugin.isEnabled(PluginType.APS)) maxIobPref = sp.getDouble(R.string.key_openapssmb_max_iob, 3d); else @@ -276,6 +277,9 @@ public class SafetyPlugin extends PluginBase implements ConstraintsInterface { maxIob.setIfSmaller(getAapsLogger(), hardLimits.maxIobAMA(), String.format(getResourceHelper().gs(R.string.limitingiob), hardLimits.maxIobAMA(), getResourceHelper().gs(R.string.hardlimit)), this); if (openAPSSMBPlugin.isEnabled(PluginType.APS)) maxIob.setIfSmaller(getAapsLogger(), hardLimits.maxIobSMB(), String.format(getResourceHelper().gs(R.string.limitingiob), hardLimits.maxIobSMB(), getResourceHelper().gs(R.string.hardlimit)), this); + if ((apsmode.equals("lgs"))) + maxIob.setIfSmaller(getAapsLogger(), hardLimits.getMAXIOB_LGS(), String.format(getResourceHelper().gs(R.string.limitingiob), hardLimits.getMAXIOB_LGS(), getResourceHelper().gs(R.string.lowglucosesuspend)), this); + return maxIob; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewFragment.kt index b1b4233b35..72fd50a10f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewFragment.kt @@ -622,8 +622,17 @@ class OverviewFragment : DaggerFragment(), View.OnClickListener, OnLongClickList } loopPlugin.isEnabled(PluginType.LOOP) -> { - overview_apsmode?.text = if (closedLoopEnabled.value()) resourceHelper.gs(R.string.closedloop) else resourceHelper.gs(R.string.openloop) - overview_apsmode?.setBackgroundColor(resourceHelper.gc(R.color.ribbonDefault)) + val isLGS = loopPlugin.isLGS + overview_apsmode?.text = + if (closedLoopEnabled.value()) + if (isLGS) + resourceHelper.gs(R.string.lgs) + else + resourceHelper.gs(R.string.closedloop) + else + resourceHelper.gs(R.string.openloop) + + overview_apsmode?.setBackgroundColor(if (isLGS) resourceHelper.gc(R.color.ribbonUnusual) else resourceHelper.gc(R.color.ribbonDefault)) overview_apsmode?.setTextColor(resourceHelper.gc(R.color.ribbonTextDefault)) } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/StatusLightHandler.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/StatusLightHandler.kt index 89f500a758..f988fd3832 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/StatusLightHandler.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/StatusLightHandler.kt @@ -28,17 +28,17 @@ class StatusLightHandler @Inject constructor( */ fun updateStatusLights(careportal_canulaage: TextView?, careportal_insulinage: TextView?, careportal_reservoirlevel: TextView?, careportal_sensorage: TextView?, careportal_pbage: TextView?, careportal_batterylevel: TextView?) { val pump = activePlugin.activePump - handleAge(careportal_canulaage, "cage", CareportalEvent.SITECHANGE, R.string.key_statuslights_cage_warning, 48.0, R.string.key_statuslights_cage_critical, 72.0) - handleAge(careportal_insulinage, "iage", CareportalEvent.INSULINCHANGE, R.string.key_statuslights_iage_warning, 72.0, R.string.key_statuslights_iage_critical, 144.0) - handleAge(careportal_sensorage, "sage", CareportalEvent.SENSORCHANGE, R.string.key_statuslights_sage_warning, 216.0, R.string.key_statuslights_sage_critical, 240.0) - handleAge(careportal_pbage, "bage", CareportalEvent.PUMPBATTERYCHANGE, R.string.key_statuslights_bage_warning, 216.0, R.string.key_statuslights_bage_critical, 240.0) + handleAge(careportal_canulaage, CareportalEvent.SITECHANGE, R.string.key_statuslights_cage_warning, 48.0, R.string.key_statuslights_cage_critical, 72.0) + handleAge(careportal_insulinage, CareportalEvent.INSULINCHANGE, R.string.key_statuslights_iage_warning, 72.0, R.string.key_statuslights_iage_critical, 144.0) + handleAge(careportal_sensorage, CareportalEvent.SENSORCHANGE, R.string.key_statuslights_sage_warning, 216.0, R.string.key_statuslights_sage_critical, 240.0) + handleAge(careportal_pbage, CareportalEvent.PUMPBATTERYCHANGE, R.string.key_statuslights_bage_warning, 216.0, R.string.key_statuslights_bage_critical, 240.0) if (!Config.NSCLIENT) - handleLevel(careportal_reservoirlevel, R.string.key_statuslights_res_critical, 10.0, R.string.key_statuslights_res_warning, 80.0, pump.reservoirLevel) + handleLevel(careportal_reservoirlevel, R.string.key_statuslights_res_critical, 10.0, R.string.key_statuslights_res_warning, 80.0, pump.reservoirLevel, "U") if (!Config.NSCLIENT && pump.model() != PumpType.AccuChekCombo) - handleLevel(careportal_batterylevel, R.string.key_statuslights_bat_critical, 26.0, R.string.key_statuslights_bat_warning, 51.0, pump.batteryLevel.toDouble()) + handleLevel(careportal_batterylevel, R.string.key_statuslights_bat_critical, 26.0, R.string.key_statuslights_bat_warning, 51.0, pump.batteryLevel.toDouble(), "%") } - private fun handleAge(view: TextView?, nsSettingPlugin: String, eventName: String, @StringRes warnSettings: Int, defaultWarnThreshold: Double, @StringRes urgentSettings: Int, defaultUrgentThreshold: Double) { + private fun handleAge(view: TextView?, eventName: String, @StringRes warnSettings: Int, defaultWarnThreshold: Double, @StringRes urgentSettings: Int, defaultUrgentThreshold: Double) { val warn = sp.getDouble(warnSettings, defaultWarnThreshold) val urgent = sp.getDouble(urgentSettings, defaultUrgentThreshold) val careportalEvent = MainApp.getDbHelper().getLastCareportalEvent(eventName) @@ -50,11 +50,11 @@ class StatusLightHandler @Inject constructor( } } - private fun handleLevel(view: TextView?, criticalSetting: Int, criticalDefaultValue: Double, warnSetting: Int, warnDefaultValue: Double, level: Double) { + private fun handleLevel(view: TextView?, criticalSetting: Int, criticalDefaultValue: Double, warnSetting: Int, warnDefaultValue: Double, level: Double, units: String) { val resUrgent = sp.getDouble(criticalSetting, criticalDefaultValue) val resWarn = sp.getDouble(warnSetting, warnDefaultValue) @Suppress("SetTextI18n") - view?.text = " " + DecimalFormatter.to0Decimal(level) + view?.text = " " + DecimalFormatter.to0Decimal(level) + units warnColors.setColorInverse(view, level, resWarn, resUrgent) } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/source/RandomBgPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/source/RandomBgPlugin.kt index 27f3fd4b4c..bb008b383b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/source/RandomBgPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/source/RandomBgPlugin.kt @@ -79,7 +79,7 @@ class RandomBgPlugin @Inject constructor( val cal = GregorianCalendar() val currentMinute = cal.get(Calendar.MINUTE) + (cal.get(Calendar.HOUR_OF_DAY) % 2) * 60 - val bgMgdl = min + (max - min) + (max - min) * sin(currentMinute / 120.0 * 2 * PI) + val bgMgdl = min + ((max - min) + (max - min) * sin(currentMinute / 120.0 * 2 * PI))/2 val bgReading = BgReading() bgReading.value = bgMgdl diff --git a/app/src/main/java/info/nightscout/androidaps/utils/HardLimits.kt b/app/src/main/java/info/nightscout/androidaps/utils/HardLimits.kt index 48eba49112..c9e2835ceb 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/HardLimits.kt +++ b/app/src/main/java/info/nightscout/androidaps/utils/HardLimits.kt @@ -45,6 +45,10 @@ class HardLimits @Inject constructor( val MAXIOB_SMB = doubleArrayOf(3.0, 7.0, 12.0, 25.0) val MAXBASAL = doubleArrayOf(2.0, 5.0, 10.0, 12.0) + //LGS Hard limits + //No IOB at all + val MAXIOB_LGS = 0.0 + private fun loadAge(): Int { val sp_age = sp.getString(R.string.key_age, "") val age: Int diff --git a/app/src/main/java/info/nightscout/androidaps/utils/textValidator/ValidatingEditTextPreference.kt b/app/src/main/java/info/nightscout/androidaps/utils/textValidator/ValidatingEditTextPreference.kt index 190a3bea73..b4a49c42de 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/textValidator/ValidatingEditTextPreference.kt +++ b/app/src/main/java/info/nightscout/androidaps/utils/textValidator/ValidatingEditTextPreference.kt @@ -2,7 +2,6 @@ package info.nightscout.androidaps.utils.textValidator import android.content.Context import android.util.AttributeSet -import androidx.core.content.res.TypedArrayUtils import androidx.preference.EditTextPreference import androidx.preference.EditTextPreference.OnBindEditTextListener import androidx.preference.PreferenceViewHolder @@ -23,8 +22,7 @@ class ValidatingEditTextPreference(ctx: Context, attrs: AttributeSet, defStyleAt : this(ctx, attrs, defStyle, 0) constructor(ctx: Context, attrs: AttributeSet) - : this(ctx, attrs, TypedArrayUtils.getAttr(ctx, R.attr.editTextPreferenceStyle, - R.attr.editTextPreferenceStyle)) + : this(ctx, attrs, R.attr.editTextPreferenceStyle) private lateinit var editTextValidator: EditTextValidator @@ -33,4 +31,4 @@ class ValidatingEditTextPreference(ctx: Context, attrs: AttributeSet, defStyleAt holder?.isDividerAllowedAbove = false holder?.isDividerAllowedBelow = false } -} \ No newline at end of file +} diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index 04d78d3406..d11a14eefd 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -4,10 +4,12 @@ @string/closedloop @string/openloop + @string/lowglucosesuspend closed open + lgs diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 8d1b977fea..d30a19a76e 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -86,6 +86,7 @@ #ff0400 #FFFFFF #303030 + #01017A #FFFFFF #2E2E2E diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 976bb33764..2a648432fe 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -187,10 +187,12 @@ Closed Loop Open Loop + Low Glucose Suspend Loop Disabled Disable loop Enable loop + LGS New suggestion available Unsupported version of Nightscout LOOP DISABLED BY CONSTRAINTS diff --git a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.kt b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.kt index cd090dbde4..7698cc6844 100644 --- a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.kt @@ -310,6 +310,7 @@ class ConstraintsCheckerTest : TestBaseWithProfile() { @Test fun iobAMAShouldBeLimited() { // No limit by default + `when`(sp.getString(R.string.key_aps_mode, "open")).thenReturn("closed") `when`(sp.getDouble(R.string.key_openapsma_max_iob, 1.5)).thenReturn(1.5) `when`(sp.getString(R.string.key_age, "")).thenReturn("teenage") openAPSAMAPlugin.setPluginEnabled(PluginType.APS, true) @@ -325,6 +326,7 @@ class ConstraintsCheckerTest : TestBaseWithProfile() { @Test fun iobSMBShouldBeLimited() { // No limit by default + `when`(sp.getString(R.string.key_aps_mode, "open")).thenReturn("closed") `when`(sp.getDouble(R.string.key_openapssmb_max_iob, 3.0)).thenReturn(3.0) `when`(sp.getString(R.string.key_age, "")).thenReturn("teenage") openAPSSMBPlugin.setPluginEnabled(PluginType.APS, true) diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/aps/loop/LoopPluginTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/aps/loop/LoopPluginTest.kt index dbfa209d11..40495880a3 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/aps/loop/LoopPluginTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/aps/loop/LoopPluginTest.kt @@ -19,6 +19,7 @@ import info.nightscout.androidaps.plugins.pump.virtual.VirtualPumpPlugin import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin import info.nightscout.androidaps.receivers.ReceiverStatusStore import info.nightscout.androidaps.utils.FabricPrivacy +import info.nightscout.androidaps.utils.HardLimits import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.sharedPreferences.SP import org.junit.Assert @@ -49,11 +50,15 @@ class LoopPluginTest : TestBase() { @Mock lateinit var fabricPrivacy: FabricPrivacy @Mock lateinit var receiverStatusStore: ReceiverStatusStore + private lateinit var hardLimits: HardLimits + lateinit var loopPlugin: LoopPlugin val injector = HasAndroidInjector { AndroidInjector { } } @Before fun prepareMock() { - loopPlugin = LoopPlugin(injector, aapsLogger, rxBus, sp, constraintChecker, resourceHelper, profileFunction, context, commandQueue, activePlugin, treatmentsPlugin, virtualPumpPlugin, actionStringHandler, iobCobCalculatorPlugin, receiverStatusStore, fabricPrivacy) + hardLimits = HardLimits(aapsLogger, rxBus, sp, resourceHelper, context) + + loopPlugin = LoopPlugin(injector, aapsLogger, rxBus, sp, constraintChecker, resourceHelper, profileFunction, context, commandQueue, activePlugin, treatmentsPlugin, virtualPumpPlugin, actionStringHandler, iobCobCalculatorPlugin, receiverStatusStore, fabricPrivacy, hardLimits) `when`(activePlugin.getActivePump()).thenReturn(virtualPumpPlugin) } @@ -61,6 +66,7 @@ class LoopPluginTest : TestBase() { fun testPluginInterface() { `when`(resourceHelper.gs(R.string.loop)).thenReturn("Loop") `when`(resourceHelper.gs(R.string.loop_shortname)).thenReturn("LOOP") + `when`(sp.getString(R.string.key_aps_mode, "open")).thenReturn("closed") val pumpDescription = PumpDescription() `when`(virtualPumpPlugin.pumpDescription).thenReturn(pumpDescription) Assert.assertEquals(LoopFragment::class.java.name, loopPlugin.pluginDescription.fragmentClass) diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/constraints/safety/SafetyPluginTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/constraints/safety/SafetyPluginTest.kt index 9da162a301..3e36b8bca0 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/constraints/safety/SafetyPluginTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/constraints/safety/SafetyPluginTest.kt @@ -230,6 +230,7 @@ class SafetyPluginTest : TestBaseWithProfile() { } @Test fun iobShouldBeLimited() { + `when`(sp.getString(R.string.key_aps_mode, "open")).thenReturn("closed") `when`(sp.getDouble(R.string.key_openapsma_max_iob, 1.5)).thenReturn(1.5) `when`(sp.getString(R.string.key_age, "")).thenReturn("teenage") diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/maintenance/ClassicPrefsFormatTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/general/maintenance/ClassicPrefsFormatTest.kt index 0755190d2b..f0a5c07491 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/maintenance/ClassicPrefsFormatTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/maintenance/ClassicPrefsFormatTest.kt @@ -1,45 +1,36 @@ package info.nightscout.androidaps.plugins.general.maintenance -import info.nightscout.androidaps.MainApp import info.nightscout.androidaps.TestBase import info.nightscout.androidaps.plugins.general.maintenance.formats.ClassicPrefsFormat import info.nightscout.androidaps.plugins.general.maintenance.formats.Prefs -import info.nightscout.androidaps.testing.mockers.AAPSMocker import info.nightscout.androidaps.testing.utils.SingleStringStorage import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.sharedPreferences.SP import org.hamcrest.CoreMatchers import org.junit.Assert -import org.junit.Before import org.junit.Test import org.junit.runner.RunWith -import org.mockito.ArgumentMatchers.anyInt import org.mockito.Mock +import org.mockito.Mockito import org.mockito.Mockito.`when` import org.powermock.core.classloader.annotations.PrepareForTest import org.powermock.modules.junit4.PowerMockRunner import java.io.File @RunWith(PowerMockRunner::class) -@PrepareForTest(AAPSMocker::class, MainApp::class, File::class) +@PrepareForTest(File::class) class ClassicPrefsFormatTest : TestBase() { @Mock lateinit var resourceHelper: ResourceHelper @Mock lateinit var sp: SP - @Before - fun mock() { - AAPSMocker.prepareMock() - AAPSMocker.resetMockedSharedPrefs() - } - @Test fun preferenceLoadingTest() { val test = "key1::val1\nkeyB::valB" val classicFormat = ClassicPrefsFormat(resourceHelper, SingleStringStorage(test)) - val prefs = classicFormat.loadPreferences(AAPSMocker.getMockedFile(), "") + val prefs = classicFormat.loadPreferences(getMockedFile(), "") Assert.assertThat(prefs.values.size, CoreMatchers.`is`(2)) Assert.assertThat(prefs.values["key1"], CoreMatchers.`is`("val1")) @@ -59,7 +50,14 @@ class ClassicPrefsFormatTest : TestBase() { mapOf() ) - classicFormat.savePreferences(AAPSMocker.getMockedFile(), prefs) + classicFormat.savePreferences(getMockedFile(), prefs) } + private fun getMockedFile(): File { + val file = Mockito.mock(File::class.java) + `when`(file.exists()).thenReturn(true) + `when`(file.canRead()).thenReturn(true) + `when`(file.canWrite()).thenReturn(true) + return file + } } diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/maintenance/EncryptedPrefsFormatTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/general/maintenance/EncryptedPrefsFormatTest.kt index 4537783ce4..fa52651e50 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/maintenance/EncryptedPrefsFormatTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/maintenance/EncryptedPrefsFormatTest.kt @@ -1,9 +1,7 @@ package info.nightscout.androidaps.plugins.general.maintenance -import info.nightscout.androidaps.MainApp import info.nightscout.androidaps.TestBase import info.nightscout.androidaps.plugins.general.maintenance.formats.* -import info.nightscout.androidaps.testing.mockers.AAPSMocker import info.nightscout.androidaps.testing.utils.SingleStringStorage import info.nightscout.androidaps.utils.CryptoUtil import info.nightscout.androidaps.utils.assumeAES256isSupported @@ -24,19 +22,17 @@ import java.io.File @PowerMockIgnore("javax.crypto.*") @RunWith(PowerMockRunner::class) -@PrepareForTest(AAPSMocker::class, MainApp::class, File::class, ResourceHelper::class) +@PrepareForTest(File::class) class EncryptedPrefsFormatTest : TestBase() { @Mock lateinit var resourceHelper: ResourceHelper @Mock lateinit var sp: SP - var cryptoUtil: CryptoUtil = CryptoUtil(aapsLogger) + private var cryptoUtil: CryptoUtil = CryptoUtil(aapsLogger) @Before fun mock() { - AAPSMocker.prepareMock() - AAPSMocker.resetMockedSharedPrefs() Mockito.`when`(resourceHelper.gs(ArgumentMatchers.anyInt())).thenReturn("mock translation") } @@ -56,7 +52,7 @@ class EncryptedPrefsFormatTest : TestBase() { val storage = SingleStringStorage(frozenPrefs) val encryptedFormat = EncryptedPrefsFormat(resourceHelper, cryptoUtil, storage) - val prefs = encryptedFormat.loadPreferences(AAPSMocker.getMockedFile(), "sikret") + val prefs = encryptedFormat.loadPreferences(getMockedFile(), "sikret") assumeAES256isSupported(cryptoUtil) @@ -82,7 +78,7 @@ class EncryptedPrefsFormatTest : TestBase() { PrefsMetadataKey.ENCRYPTION to PrefMetadata(EncryptedPrefsFormat.FORMAT_KEY_ENC, PrefsStatus.OK) ) ) - encryptedFormat.savePreferences(AAPSMocker.getMockedFile(), prefs, "sikret") + encryptedFormat.savePreferences(getMockedFile(), prefs, "sikret") aapsLogger.debug(storage.contents) } @@ -99,8 +95,8 @@ class EncryptedPrefsFormatTest : TestBase() { PrefsMetadataKey.ENCRYPTION to PrefMetadata(EncryptedPrefsFormat.FORMAT_KEY_ENC, PrefsStatus.OK) ) ) - encryptedFormat.savePreferences(AAPSMocker.getMockedFile(), prefsIn, "tajemnica") - val prefsOut = encryptedFormat.loadPreferences(AAPSMocker.getMockedFile(), "tajemnica") + encryptedFormat.savePreferences(getMockedFile(), prefsIn, "tajemnica") + val prefsOut = encryptedFormat.loadPreferences(getMockedFile(), "tajemnica") assumeAES256isSupported(cryptoUtil) @@ -129,7 +125,7 @@ class EncryptedPrefsFormatTest : TestBase() { val storage = SingleStringStorage(frozenPrefs) val encryptedFormat = EncryptedPrefsFormat(resourceHelper, cryptoUtil, storage) - val prefs = encryptedFormat.loadPreferences(AAPSMocker.getMockedFile(), "it-is-NOT-right-secret") + val prefs = encryptedFormat.loadPreferences(getMockedFile(), "it-is-NOT-right-secret") Assert.assertThat(prefs.values.size, CoreMatchers.`is`(0)) @@ -156,7 +152,7 @@ class EncryptedPrefsFormatTest : TestBase() { val storage = SingleStringStorage(frozenPrefs) val encryptedFormat = EncryptedPrefsFormat(resourceHelper, cryptoUtil, storage) - val prefs = encryptedFormat.loadPreferences(AAPSMocker.getMockedFile(), "sikret") + val prefs = encryptedFormat.loadPreferences(getMockedFile(), "sikret") assumeAES256isSupported(cryptoUtil) @@ -183,7 +179,7 @@ class EncryptedPrefsFormatTest : TestBase() { val storage = SingleStringStorage(frozenPrefs) val encryptedFormat = EncryptedPrefsFormat(resourceHelper, cryptoUtil, storage) - val prefs = encryptedFormat.loadPreferences(AAPSMocker.getMockedFile(), "sikret") + val prefs = encryptedFormat.loadPreferences(getMockedFile(), "sikret") Assert.assertThat(prefs.values.size, CoreMatchers.`is`(0)) Assert.assertThat(prefs.metadata[PrefsMetadataKey.ENCRYPTION]?.status, CoreMatchers.`is`(PrefsStatus.ERROR)) @@ -198,7 +194,7 @@ class EncryptedPrefsFormatTest : TestBase() { val storage = SingleStringStorage(frozenPrefs) val encryptedFormat = EncryptedPrefsFormat(resourceHelper, cryptoUtil, storage) - val prefs = encryptedFormat.loadPreferences(AAPSMocker.getMockedFile(), "sikret") + val prefs = encryptedFormat.loadPreferences(getMockedFile(), "sikret") Assert.assertThat(prefs.values.size, CoreMatchers.`is`(0)) Assert.assertThat(prefs.metadata[PrefsMetadataKey.FILE_FORMAT]?.status, CoreMatchers.`is`(PrefsStatus.ERROR)) @@ -210,7 +206,7 @@ class EncryptedPrefsFormatTest : TestBase() { val storage = SingleStringStorage(frozenPrefs) val encryptedFormat = EncryptedPrefsFormat(resourceHelper, cryptoUtil, storage) - encryptedFormat.loadPreferences(AAPSMocker.getMockedFile(), "sikret") + encryptedFormat.loadPreferences(getMockedFile(), "sikret") } @Test(expected = PrefFormatError::class) @@ -229,7 +225,14 @@ class EncryptedPrefsFormatTest : TestBase() { val storage = SingleStringStorage(frozenPrefs) val encryptedFormat = EncryptedPrefsFormat(resourceHelper, cryptoUtil, storage) - encryptedFormat.loadPreferences(AAPSMocker.getMockedFile(), "sikret") + encryptedFormat.loadPreferences(getMockedFile(), "sikret") } + private fun getMockedFile(): File { + val file = Mockito.mock(File::class.java) + Mockito.`when`(file.exists()).thenReturn(true) + Mockito.`when`(file.canRead()).thenReturn(true) + Mockito.`when`(file.canWrite()).thenReturn(true) + return file + } } diff --git a/app/src/test/java/info/nightscout/androidaps/testing/mockers/AAPSMocker.java b/app/src/test/java/info/nightscout/androidaps/testing/mockers/AAPSMocker.java deleted file mode 100644 index 3b495af147..0000000000 --- a/app/src/test/java/info/nightscout/androidaps/testing/mockers/AAPSMocker.java +++ /dev/null @@ -1,56 +0,0 @@ -package info.nightscout.androidaps.testing.mockers; - -import android.content.Context; -import android.content.SharedPreferences; - -import org.mockito.ArgumentMatchers; -import org.mockito.invocation.InvocationOnMock; -import org.powermock.api.mockito.PowerMockito; - -import java.io.File; -import java.util.HashMap; -import java.util.Map; - -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.testing.mocks.SharedPreferencesMock; - -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import static org.powermock.api.mockito.PowerMockito.mockStatic; - -public class AAPSMocker { - - private static final Map mockedSharedPrefs = new HashMap<>(); - - public static void prepareMock() throws Exception { - Context mockedContext = mock(Context.class); - mockStatic(MainApp.class, InvocationOnMock::callRealMethod); - - PowerMockito.when(mockedContext, "getSharedPreferences", ArgumentMatchers.anyString(), ArgumentMatchers.anyInt()).thenAnswer(invocation -> { - - final String key = invocation.getArgument(0); - if (mockedSharedPrefs.containsKey(key)) { - return mockedSharedPrefs.get(key); - } else { - SharedPreferencesMock newPrefs = new SharedPreferencesMock(); - mockedSharedPrefs.put(key, newPrefs); - return newPrefs; - } - }); - - resetMockedSharedPrefs(); - } - - public static void resetMockedSharedPrefs() { - mockedSharedPrefs.clear(); - } - - public static File getMockedFile() { - File file = mock(File.class); - when(file.exists()).thenReturn(true); - when(file.canRead()).thenReturn(true); - when(file.canWrite()).thenReturn(true); - return file; - } - -} diff --git a/app/src/test/java/info/nightscout/androidaps/testing/utils/SingleStringStorage.kt b/app/src/test/java/info/nightscout/androidaps/testing/utils/SingleStringStorage.kt index 8a3b06d56e..9991779066 100644 --- a/app/src/test/java/info/nightscout/androidaps/testing/utils/SingleStringStorage.kt +++ b/app/src/test/java/info/nightscout/androidaps/testing/utils/SingleStringStorage.kt @@ -3,20 +3,14 @@ package info.nightscout.androidaps.testing.utils import info.nightscout.androidaps.utils.storage.Storage import java.io.File -class SingleStringStorage : Storage { - - var contents: String = "" - - constructor(contents: String) { - this.contents = contents - } +class SingleStringStorage(var contents: String) : Storage { override fun getFileContents(file: File): String { return contents } - override fun putFileContents(file: File, putContents: String) { - contents = putContents + override fun putFileContents(file: File, contents: String) { + this.contents = contents } override fun toString(): String {