Merge branch 'dev' into omnipod_eros_dev_dagger
This commit is contained in:
commit
037d9e88df
17 changed files with 100 additions and 113 deletions
|
@ -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> 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<Boolean> 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;
|
||||
|
|
|
@ -98,7 +98,7 @@ public class SafetyPlugin extends PluginBase implements ConstraintsInterface {
|
|||
@NonNull @Override
|
||||
public Constraint<Boolean> isClosedLoopAllowed(@NonNull Constraint<Boolean> 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<Double> applyMaxIOBConstraints(@NonNull Constraint<Double> 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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,10 +4,12 @@
|
|||
<string-array name="aps_modeArray">
|
||||
<item>@string/closedloop</item>
|
||||
<item>@string/openloop</item>
|
||||
<item>@string/lowglucosesuspend</item>
|
||||
</string-array>
|
||||
<string-array name="aps_modeValues" translatable="false">
|
||||
<item>closed</item>
|
||||
<item>open</item>
|
||||
<item>lgs</item>
|
||||
</string-array>
|
||||
|
||||
<string-array name="unitsArray">
|
||||
|
|
|
@ -86,6 +86,7 @@
|
|||
<color name="ribbonCritical">#ff0400</color>
|
||||
<color name="ribbonTextDefault">#FFFFFF</color>
|
||||
<color name="ribbonTextWarning">#303030</color>
|
||||
<color name="ribbonUnusual">#01017A</color>
|
||||
<color name="ribbonTextCritical">#FFFFFF</color>
|
||||
|
||||
<color name="splashBackground">#2E2E2E</color>
|
||||
|
|
|
@ -187,10 +187,12 @@
|
|||
|
||||
<string name="closedloop">Closed Loop</string>
|
||||
<string name="openloop">Open Loop</string>
|
||||
<string name="lowglucosesuspend">Low Glucose Suspend</string>
|
||||
<string name="disabledloop">Loop Disabled</string>
|
||||
<string name="disableloop">Disable loop</string>
|
||||
<string name="enableloop">Enable loop</string>
|
||||
|
||||
<string name="lgs">LGS</string>
|
||||
<string name="openloop_newsuggestion">New suggestion available</string>
|
||||
<string name="unsupportednsversion">Unsupported version of Nightscout</string>
|
||||
<string name="loopdisabled">LOOP DISABLED BY CONSTRAINTS</string>
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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")
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<String, SharedPreferences> 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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue