This commit is contained in:
Milos Kozak 2020-05-02 10:49:55 +02:00
parent 585b45fb97
commit 9efbbd3c17
5 changed files with 42 additions and 103 deletions

View file

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

View file

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

View 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
}
}

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

View 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 {