From 8799c30b8e83379f23e922366bb1e305fa3372a1 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Fri, 6 Mar 2020 23:43:29 +0100 Subject: [PATCH] ActionLoopDisableTest --- .../androidaps/data/PumpEnactResult.java | 84 +++++++++---------- .../dependencyInjection/AppComponent.kt | 2 + .../dependencyInjection/AppModule.kt | 3 + .../actions/ActionLoopDisableTest.kt | 27 +++--- 4 files changed, 63 insertions(+), 53 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/data/PumpEnactResult.java b/app/src/main/java/info/nightscout/androidaps/data/PumpEnactResult.java index 8563bfbe17..edfe7cfc35 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/PumpEnactResult.java +++ b/app/src/main/java/info/nightscout/androidaps/data/PumpEnactResult.java @@ -2,18 +2,18 @@ package info.nightscout.androidaps.data; import org.json.JSONException; import org.json.JSONObject; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import info.nightscout.androidaps.MainApp; +import javax.inject.Inject; + import info.nightscout.androidaps.R; -import info.nightscout.androidaps.logging.L; -import info.nightscout.androidaps.logging.StacktraceLoggerWrapper; +import info.nightscout.androidaps.logging.AAPSLogger; import info.nightscout.androidaps.utils.DecimalFormatter; import info.nightscout.androidaps.utils.Round; +import info.nightscout.androidaps.utils.resources.ResourceHelper; public class PumpEnactResult { - private static Logger log = StacktraceLoggerWrapper.getLogger(L.APS); + @Inject AAPSLogger aapsLogger; + @Inject ResourceHelper resourceHelper; public boolean success = false; // request was processed successfully (but possible no change was needed) public boolean enacted = false; // request was processed successfully and change has been made @@ -47,7 +47,7 @@ public class PumpEnactResult { } public PumpEnactResult comment(int comment) { - this.comment = MainApp.gs(comment); + this.comment = resourceHelper.gs(comment); return this; } @@ -106,66 +106,66 @@ public class PumpEnactResult { } public String toString() { - String ret = MainApp.gs(R.string.success) + ": " + success; + String ret = resourceHelper.gs(R.string.success) + ": " + success; if (enacted) { if (bolusDelivered > 0) { - ret += "\n" + MainApp.gs(R.string.enacted) + ": " + enacted; - ret += "\n" + MainApp.gs(R.string.comment) + ": " + comment; - ret += "\n" + MainApp.gs(R.string.configbuilder_insulin) - + ": " + bolusDelivered + " " + MainApp.gs(R.string.insulin_unit_shortname); + ret += "\n" + resourceHelper.gs(R.string.enacted) + ": " + enacted; + ret += "\n" + resourceHelper.gs(R.string.comment) + ": " + comment; + ret += "\n" + resourceHelper.gs(R.string.configbuilder_insulin) + + ": " + bolusDelivered + " " + resourceHelper.gs(R.string.insulin_unit_shortname); } else if (isTempCancel) { - ret += "\n" + MainApp.gs(R.string.enacted) + ": " + enacted; + ret += "\n" + resourceHelper.gs(R.string.enacted) + ": " + enacted; if (!comment.isEmpty()) - ret += "\n" + MainApp.gs(R.string.comment) + ": " + comment; - ret += "\n" + MainApp.gs(R.string.canceltemp); + ret += "\n" + resourceHelper.gs(R.string.comment) + ": " + comment; + ret += "\n" + resourceHelper.gs(R.string.canceltemp); } else if (isPercent) { - ret += "\n" + MainApp.gs(R.string.enacted) + ": " + enacted; + ret += "\n" + resourceHelper.gs(R.string.enacted) + ": " + enacted; if (!comment.isEmpty()) - ret += "\n" + MainApp.gs(R.string.comment) + ": " + comment; - ret += "\n" + MainApp.gs(R.string.duration) + ": " + duration + " min"; - ret += "\n" + MainApp.gs(R.string.percent) + ": " + percent + "%"; + ret += "\n" + resourceHelper.gs(R.string.comment) + ": " + comment; + ret += "\n" + resourceHelper.gs(R.string.duration) + ": " + duration + " min"; + ret += "\n" + resourceHelper.gs(R.string.percent) + ": " + percent + "%"; } else { - ret += "\n" + MainApp.gs(R.string.enacted) + ": " + enacted; + ret += "\n" + resourceHelper.gs(R.string.enacted) + ": " + enacted; if (!comment.isEmpty()) - ret += "\n" + MainApp.gs(R.string.comment) + ": " + comment; - ret += "\n" + MainApp.gs(R.string.duration) + ": " + duration + " min"; - ret += "\n" + MainApp.gs(R.string.absolute) + ": " + absolute + " U/h"; + ret += "\n" + resourceHelper.gs(R.string.comment) + ": " + comment; + ret += "\n" + resourceHelper.gs(R.string.duration) + ": " + duration + " min"; + ret += "\n" + resourceHelper.gs(R.string.absolute) + ": " + absolute + " U/h"; } } else { - ret += "\n" + MainApp.gs(R.string.comment) + ": " + comment; + ret += "\n" + resourceHelper.gs(R.string.comment) + ": " + comment; } return ret; } public String toHtml() { - String ret = "" + MainApp.gs(R.string.success) + ": " + success; + String ret = "" + resourceHelper.gs(R.string.success) + ": " + success; if (queued) { - ret = MainApp.gs(R.string.waitingforpumpresult); + ret = resourceHelper.gs(R.string.waitingforpumpresult); } else if (enacted) { if (bolusDelivered > 0) { - ret += "
" + MainApp.gs(R.string.enacted) + ": " + enacted; + ret += "
" + resourceHelper.gs(R.string.enacted) + ": " + enacted; if (!comment.isEmpty()) - ret += "
" + MainApp.gs(R.string.comment) + ": " + comment; - ret += "
" + MainApp.gs(R.string.smb_shortname) + ": " + bolusDelivered + " " + MainApp.gs(R.string.insulin_unit_shortname); + ret += "
" + resourceHelper.gs(R.string.comment) + ": " + comment; + ret += "
" + resourceHelper.gs(R.string.smb_shortname) + ": " + bolusDelivered + " " + resourceHelper.gs(R.string.insulin_unit_shortname); } else if (isTempCancel) { - ret += "
" + MainApp.gs(R.string.enacted) + ": " + enacted; - ret += "
" + MainApp.gs(R.string.comment) + ": " + comment + - "
" + MainApp.gs(R.string.canceltemp); + ret += "
" + resourceHelper.gs(R.string.enacted) + ": " + enacted; + ret += "
" + resourceHelper.gs(R.string.comment) + ": " + comment + + "
" + resourceHelper.gs(R.string.canceltemp); } else if (isPercent && percent != -1) { - ret += "
" + MainApp.gs(R.string.enacted) + ": " + enacted; + ret += "
" + resourceHelper.gs(R.string.enacted) + ": " + enacted; if (!comment.isEmpty()) - ret += "
" + MainApp.gs(R.string.comment) + ": " + comment; - ret += "
" + MainApp.gs(R.string.duration) + ": " + duration + " min"; - ret += "
" + MainApp.gs(R.string.percent) + ": " + percent + "%"; + ret += "
" + resourceHelper.gs(R.string.comment) + ": " + comment; + ret += "
" + resourceHelper.gs(R.string.duration) + ": " + duration + " min"; + ret += "
" + resourceHelper.gs(R.string.percent) + ": " + percent + "%"; } else if (absolute != -1) { - ret += "
" + MainApp.gs(R.string.enacted) + ": " + enacted; + ret += "
" + resourceHelper.gs(R.string.enacted) + ": " + enacted; if (!comment.isEmpty()) - ret += "
" + MainApp.gs(R.string.comment) + ": " + comment; - ret += "
" + MainApp.gs(R.string.duration) + ": " + duration + " min"; - ret += "
" + MainApp.gs(R.string.absolute) + ": " + DecimalFormatter.to2Decimal(absolute) + " U/h"; + ret += "
" + resourceHelper.gs(R.string.comment) + ": " + comment; + ret += "
" + resourceHelper.gs(R.string.duration) + ": " + duration + " min"; + ret += "
" + resourceHelper.gs(R.string.absolute) + ": " + DecimalFormatter.to2Decimal(absolute) + " U/h"; } } else { - ret += "
" + MainApp.gs(R.string.comment) + ": " + comment; + ret += "
" + resourceHelper.gs(R.string.comment) + ": " + comment; } return ret; } @@ -188,7 +188,7 @@ public class PumpEnactResult { result.put("duration", duration); } } catch (JSONException e) { - log.error("Unhandled exception", e); + aapsLogger.error("Unhandled exception", e); } return result; } diff --git a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppComponent.kt b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppComponent.kt index e9a8590cfe..1bf03a66e9 100644 --- a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppComponent.kt +++ b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppComponent.kt @@ -6,6 +6,7 @@ import dagger.android.AndroidInjectionModule import dagger.android.AndroidInjector import info.nightscout.androidaps.MainApp import info.nightscout.androidaps.data.ProfileStore +import info.nightscout.androidaps.data.PumpEnactResult import info.nightscout.androidaps.db.BgReading import info.nightscout.androidaps.plugins.aps.openAPSMA.LoggerCallback import info.nightscout.androidaps.plugins.constraints.objectives.objectives.* @@ -38,6 +39,7 @@ import javax.inject.Singleton interface AppComponent : AndroidInjector { fun injectProfileStore(profileStore: ProfileStore) + fun injectPumpEnactResult(pumpEnactResult: PumpEnactResult) fun injectCommandQueue(commandQueue: CommandQueue) fun injectCommandBolus(commandBolus: CommandBolus) diff --git a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppModule.kt b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppModule.kt index 047123fe5f..fdcf2ad72b 100644 --- a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppModule.kt +++ b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppModule.kt @@ -10,6 +10,7 @@ import dagger.android.HasAndroidInjector import info.nightscout.androidaps.BuildConfig import info.nightscout.androidaps.MainApp import info.nightscout.androidaps.data.ProfileStore +import info.nightscout.androidaps.data.PumpEnactResult import info.nightscout.androidaps.db.BgReading import info.nightscout.androidaps.interfaces.ActivePluginProvider import info.nightscout.androidaps.interfaces.CommandQueueProvider @@ -78,6 +79,8 @@ open class AppModule { @ContributesAndroidInjector fun profileStoreInjector(): ProfileStore + @ContributesAndroidInjector fun pumpEnactResultInjector(): PumpEnactResult + @ContributesAndroidInjector fun commandQueueInjector(): CommandQueue @ContributesAndroidInjector fun commandBolusInjector(): CommandBolus @ContributesAndroidInjector diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionLoopDisableTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionLoopDisableTest.kt index 5860823475..a96800345b 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionLoopDisableTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionLoopDisableTest.kt @@ -25,24 +25,23 @@ import info.nightscout.androidaps.utils.sharedPreferences.SP 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 import org.mockito.Mockito.`when` +import org.powermock.core.classloader.annotations.PrepareForTest +import org.powermock.modules.junit4.PowerMockRunner +@RunWith(PowerMockRunner::class) +@PrepareForTest(VirtualPumpPlugin::class, RxBusWrapper::class) class ActionLoopDisableTest : TestBase() { @Mock lateinit var rxBus: RxBusWrapper @Mock lateinit var sp: SP - @Mock lateinit var constraintChecker: ConstraintChecker @Mock lateinit var resourceHelper: ResourceHelper - @Mock lateinit var profileFunction: ProfileFunction - @Mock lateinit var context: Context @Mock lateinit var commandQueue: CommandQueueProvider @Mock lateinit var configBuilderPlugin: ConfigBuilderPlugin - @Mock lateinit var treatmentsPlugin: TreatmentsPlugin @Mock lateinit var virtualPumpPlugin: VirtualPumpPlugin - @Mock lateinit var lazyActionStringHandler: Lazy - @Mock lateinit var iobCobCalculatorPlugin: IobCobCalculatorPlugin @Mock lateinit var loopPlugin: LoopPlugin lateinit var sut: ActionLoopDisable @@ -54,6 +53,8 @@ class ActionLoopDisableTest : TestBase() { val pumpDescription = PumpDescription().apply { isTempBasalCapable = true } `when`(virtualPumpPlugin.pumpDescription).thenReturn(pumpDescription) `when`(configBuilderPlugin.activePump).thenReturn(virtualPumpPlugin) + `when`(resourceHelper.gs(R.string.disableloop)).thenReturn("Disable loop") + `when`(resourceHelper.gs(R.string.alreadydisabled)).thenReturn("Disable loop") sut = ActionLoopDisable(HasAndroidInjector { AndroidInjector { Unit } }) // do nothing injector .also { // inject the mocks @@ -65,19 +66,23 @@ class ActionLoopDisableTest : TestBase() { } } - @Test fun friendlyNameTest() { + @Test + fun friendlyNameTest() { Assert.assertEquals(R.string.disableloop.toLong(), sut.friendlyName().toLong()) } - @Test fun shortDescriptionTest() { + @Test + fun shortDescriptionTest() { Assert.assertEquals("Disable loop", sut.shortDescription()) } - @Test fun iconTest() { - Assert.assertEquals(Optional.of(R.drawable.ic_stop_24dp), sut.icon()) + @Test + fun iconTest() { + Assert.assertEquals(R.drawable.ic_stop_24dp, sut.icon()) } - @Test fun doActionTest() { + @Test + fun doActionTest() { sut.doAction(object : Callback() { override fun run() {} })