diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Notify_Delivery_Complete.kt b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Notify_Delivery_Complete.kt index a0419deaaa..246f2c7fe5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Notify_Delivery_Complete.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Notify_Delivery_Complete.kt @@ -27,7 +27,7 @@ class DanaRS_Packet_Notify_Delivery_Complete( val deliveredInsulin = byteArrayToInt(getBytes(data, DATA_START, 2)) / 100.0 danaRSPlugin.bolusingTreatment.insulin = deliveredInsulin val bolusingEvent = EventOverviewBolusProgress - bolusingEvent.status = String.format(resourceHelper.gs(R.string.bolusdelivering), deliveredInsulin) + bolusingEvent.status = resourceHelper.gs(R.string.bolusdelivering, deliveredInsulin) bolusingEvent.t = danaRSPlugin.bolusingTreatment bolusingEvent.percent = min((deliveredInsulin / danaRSPlugin.bolusAmountToBeDelivered * 100).toInt(), 100) danaRSPlugin.bolusDone = true diff --git a/app/src/test/java/info/TestBase.kt b/app/src/test/java/info/TestBase.kt new file mode 100644 index 0000000000..235e470e65 --- /dev/null +++ b/app/src/test/java/info/TestBase.kt @@ -0,0 +1,24 @@ +package info + +import org.junit.Rule +import org.mockito.Mockito +import org.mockito.junit.MockitoJUnit +import org.mockito.junit.MockitoRule + +open class TestBase { + // Add a JUnit rule that will setup the @Mock annotated vars and log. + // Another possibility would be to add `MockitoAnnotations.initMocks(this) to the setup method. + @get:Rule + val mockitoRule: MockitoRule = MockitoJUnit.rule() + + // Workaround for Kotlin nullability. + // https://medium.com/@elye.project/befriending-kotlin-and-mockito-1c2e7b0ef791 + // https://stackoverflow.com/questions/30305217/is-it-possible-to-use-mockito-in-kotlin + fun anyObject(): T { + Mockito.any() + return uninitialized() + } + + @Suppress("Unchecked_Cast") + fun uninitialized(): T = null as T +} \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRSMessageHashTableTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRSMessageHashTableTest.java deleted file mode 100644 index b498fb8bbc..0000000000 --- a/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRSMessageHashTableTest.java +++ /dev/null @@ -1,36 +0,0 @@ -package info.nightscout.androidaps.plugins.pump.danaRS.comm; - - -import com.cozmo.danar.util.BleCommandUtil; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -import info.AAPSMocker; -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.logging.L; -import info.nightscout.androidaps.utils.SP; - -import static org.junit.Assert.assertEquals; - -/** - * Created by Rumen Georgiev on 8/9/2018. - */ -@RunWith(PowerMockRunner.class) -@PrepareForTest({MainApp.class, SP.class, L.class, DanaRSMessageHashTable.class}) -public class DanaRSMessageHashTableTest { - @Test - public void runTest() { - AAPSMocker.mockMainApp(); - AAPSMocker.mockApplicationContext(); - AAPSMocker.mockSP(); - AAPSMocker.mockL(); -// HashMap messages = new DanaRSMessageHashTable().messages; - DanaRS_Packet forTesting = new DanaRS_Packet_APS_Set_Event_History(); - DanaRS_Packet testPacket = DanaRSMessageHashTable.findMessage(forTesting.getCommand()); - assertEquals(BleCommandUtil.DANAR_PACKET__OPCODE__APS_SET_EVENT_HISTORY, testPacket.getOpCode()); - } - -} diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRSMessageHashTableTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRSMessageHashTableTest.kt new file mode 100644 index 0000000000..55a3539d8b --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRSMessageHashTableTest.kt @@ -0,0 +1,50 @@ +package info.nightscout.androidaps.plugins.pump.danaRS.comm + +import com.cozmo.danar.util.BleCommandUtil +import info.TestBase +import info.nightscout.androidaps.interfaces.ActivePluginProvider +import info.nightscout.androidaps.interfaces.Constraint +import info.nightscout.androidaps.logging.AAPSLogger +import info.nightscout.androidaps.plugins.bus.RxBusWrapper +import info.nightscout.androidaps.plugins.configBuilder.ConstraintChecker +import info.nightscout.androidaps.plugins.pump.danaR.DanaRPump +import info.nightscout.androidaps.plugins.pump.danaRS.DanaRSPlugin +import info.nightscout.androidaps.utils.resources.ResourceHelper +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.`when` +import org.powermock.core.classloader.annotations.PrepareForTest +import org.powermock.modules.junit4.PowerMockRunner + +@RunWith(PowerMockRunner::class) +@PrepareForTest(ConstraintChecker::class) +class DanaRSMessageHashTableTest : TestBase() { + + @Mock lateinit var aapsLogger: AAPSLogger + @Mock lateinit var rxBus: RxBusWrapper + @Mock lateinit var resourceHelper: ResourceHelper + @Mock lateinit var danaRSPlugin: DanaRSPlugin + @Mock lateinit var activePlugin: ActivePluginProvider + @Mock lateinit var constraintChecker: ConstraintChecker + @Mock lateinit var sp: SP + lateinit var danaRPump: DanaRPump + + @Test + fun runTest() { + `when`(constraintChecker.applyBolusConstraints(anyObject())).thenReturn(Constraint(0.0)) + + val danaRSMessageHashTable = DanaRSMessageHashTable(aapsLogger, rxBus, resourceHelper, danaRPump, danaRSPlugin, activePlugin, constraintChecker) + val forTesting: DanaRS_Packet = DanaRS_Packet_APS_Set_Event_History(aapsLogger, DanaRPump.CARBS, 0, 0, 0) + val testPacket: DanaRS_Packet = danaRSMessageHashTable.findMessage(forTesting.command) + Assert.assertEquals(BleCommandUtil.DANAR_PACKET__OPCODE__APS_SET_EVENT_HISTORY.toLong(), testPacket.getOpCode().toLong()) + } + + @Before + fun setup() { + danaRPump = DanaRPump(aapsLogger, sp) + } +} \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Bolus_Set_Step_Bolus_StartTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Bolus_Set_Step_Bolus_StartTest.java deleted file mode 100644 index 1889ff9611..0000000000 --- a/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Bolus_Set_Step_Bolus_StartTest.java +++ /dev/null @@ -1,46 +0,0 @@ -package info.nightscout.androidaps.plugins.pump.danaRS.comm; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -import info.AAPSMocker; -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.logging.L; -import info.nightscout.androidaps.utils.SP; - -import static org.junit.Assert.assertEquals; - -/** - * Created by Rumen on 31.07.2018. - */ - -@RunWith(PowerMockRunner.class) -@PrepareForTest({MainApp.class, SP.class, L.class}) -public class DanaRS_Packet_Bolus_Set_Step_Bolus_StartTest { - - @Test - public void runTest() { - AAPSMocker.mockMainApp(); - AAPSMocker.mockApplicationContext(); - AAPSMocker.mockSP(); - AAPSMocker.mockL(); - - DanaRS_Packet_Bolus_Set_Step_Bolus_Start packet = new DanaRS_Packet_Bolus_Set_Step_Bolus_Start(); - - // test params - byte[] testparams = packet.getRequestParams(); - assertEquals(0, testparams[0]); - assertEquals(0, testparams[2]); - - // test message decoding - packet.handleMessage(new byte[]{(byte) 0, (byte) 0, (byte) 0}); - assertEquals(false, packet.failed); - packet.handleMessage(new byte[]{(byte) 1, (byte) 1, (byte) 1, (byte) 1, (byte) 1, (byte) 1, (byte) 1, (byte) 1}); - assertEquals(true, packet.failed); - - assertEquals("BOLUS__SET_STEP_BOLUS_START", packet.getFriendlyName()); - } - -} diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Bolus_Set_Step_Bolus_StopTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Bolus_Set_Step_Bolus_StopTest.kt index 4ecf11ef16..ae68b1c123 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Bolus_Set_Step_Bolus_StopTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Bolus_Set_Step_Bolus_StopTest.kt @@ -2,30 +2,35 @@ package info.nightscout.androidaps.plugins.pump.danaRS.comm import dagger.android.AndroidInjector import dagger.android.HasAndroidInjector -import info.AAPSMocker -import info.nightscout.androidaps.MainApp -import info.nightscout.androidaps.logging.L +import info.TestBase +import info.nightscout.androidaps.logging.AAPSLogger +import info.nightscout.androidaps.plugins.bus.RxBusWrapper import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin import info.nightscout.androidaps.plugins.configBuilder.ProfileFunction +import info.nightscout.androidaps.plugins.pump.danaRS.DanaRSPlugin import info.nightscout.androidaps.plugins.treatments.Treatment import info.nightscout.androidaps.utils.DefaultValueHelper -import info.nightscout.androidaps.utils.SP import info.nightscout.androidaps.utils.resources.ResourceHelper import org.junit.Assert 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(MainApp::class, SP::class, L::class) -class DanaRS_Packet_Bolus_Set_Step_Bolus_StopTest : DanaRS_Packet_Bolus_Set_Step_Bolus_Stop() { +@PrepareForTest(RxBusWrapper::class) +class DanaRS_Packet_Bolus_Set_Step_Bolus_StopTest : TestBase() { @Mock lateinit var defaultValueHelper: DefaultValueHelper + @Mock lateinit var rxBus: RxBusWrapper + @Mock lateinit var aapsLogger: AAPSLogger @Mock lateinit var resourceHelper: ResourceHelper @Mock lateinit var profileFunction: ProfileFunction @Mock lateinit var configBuilderPlugin: ConfigBuilderPlugin + @Mock lateinit var danaRSPlugin: DanaRSPlugin private var treatmentInjector: HasAndroidInjector = HasAndroidInjector { AndroidInjector { @@ -39,17 +44,15 @@ class DanaRS_Packet_Bolus_Set_Step_Bolus_StopTest : DanaRS_Packet_Bolus_Set_Step } @Test fun runTest() { - AAPSMocker.mockMainApp() - AAPSMocker.mockApplicationContext() - AAPSMocker.mockStrings() - AAPSMocker.mockSP() - AAPSMocker.mockL() - val testPacket = DanaRS_Packet_Bolus_Set_Step_Bolus_Stop(1.0, Treatment(treatmentInjector)) + `when`(resourceHelper.gs(Mockito.anyInt())).thenReturn("SomeString") + + danaRSPlugin.bolusingTreatment = Treatment(treatmentInjector) + val testPacket = DanaRS_Packet_Bolus_Set_Step_Bolus_Stop(aapsLogger, rxBus, resourceHelper, danaRSPlugin) // test message decoding testPacket.handleMessage(byteArrayOf(0.toByte(), 0.toByte(), 0.toByte())) Assert.assertEquals(false, testPacket.failed) testPacket.handleMessage(byteArrayOf(1.toByte(), 1.toByte(), 1.toByte(), 1.toByte(), 1.toByte(), 1.toByte(), 1.toByte(), 1.toByte())) Assert.assertEquals(true, testPacket.failed) - Assert.assertEquals("BOLUS__SET_STEP_BOLUS_STOP", friendlyName) + Assert.assertEquals("BOLUS__SET_STEP_BOLUS_STOP", testPacket.friendlyName) } } \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Notify_Delivery_CompleteTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Notify_Delivery_CompleteTest.kt index 77863a83d8..501b8472c8 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Notify_Delivery_CompleteTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Notify_Delivery_CompleteTest.kt @@ -2,28 +2,34 @@ package info.nightscout.androidaps.plugins.pump.danaRS.comm import dagger.android.AndroidInjector import dagger.android.HasAndroidInjector -import info.AAPSMocker -import info.nightscout.androidaps.MainApp -import info.nightscout.androidaps.logging.L +import info.TestBase +import info.nightscout.androidaps.logging.AAPSLogger +import info.nightscout.androidaps.plugins.bus.RxBusWrapper import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin import info.nightscout.androidaps.plugins.configBuilder.ProfileFunction +import info.nightscout.androidaps.plugins.pump.danaRS.DanaRSPlugin import info.nightscout.androidaps.plugins.treatments.Treatment import info.nightscout.androidaps.utils.DefaultValueHelper -import info.nightscout.androidaps.utils.SP import info.nightscout.androidaps.utils.resources.ResourceHelper import org.junit.Assert import org.junit.Test import org.junit.runner.RunWith import org.mockito.Mock +import org.mockito.Mockito.`when` +import org.mockito.Mockito.anyDouble +import org.mockito.Mockito.anyInt import org.powermock.core.classloader.annotations.PrepareForTest import org.powermock.modules.junit4.PowerMockRunner @RunWith(PowerMockRunner::class) -@PrepareForTest(MainApp::class, SP::class, L::class) -class DanaRS_Packet_Notify_Delivery_CompleteTest { +@PrepareForTest(RxBusWrapper::class) +class DanaRS_Packet_Notify_Delivery_CompleteTest : TestBase() { @Mock lateinit var defaultValueHelper: DefaultValueHelper + @Mock lateinit var rxBus: RxBusWrapper + @Mock lateinit var aapsLogger: AAPSLogger @Mock lateinit var resourceHelper: ResourceHelper + @Mock lateinit var danaRSPlugin: DanaRSPlugin @Mock lateinit var profileFunction: ProfileFunction @Mock lateinit var configBuilderPlugin: ConfigBuilderPlugin @@ -39,17 +45,15 @@ class DanaRS_Packet_Notify_Delivery_CompleteTest { } @Test fun runTest() { - AAPSMocker.mockMainApp() - AAPSMocker.mockApplicationContext() - AAPSMocker.mockSP() - AAPSMocker.mockL() - AAPSMocker.mockStrings() - val packet = DanaRS_Packet_Notify_Delivery_Complete(0.5, Treatment(treatmentInjector)) + `when`(resourceHelper.gs(anyInt(), anyDouble())).thenReturn("SomeString") + + danaRSPlugin.bolusingTreatment = Treatment(treatmentInjector) + val packet = DanaRS_Packet_Notify_Delivery_Complete(aapsLogger, rxBus, resourceHelper, danaRSPlugin) // test params Assert.assertEquals(null, packet.requestParams) // test message decoding packet.handleMessage(createArray(17, 0.toByte())) - Assert.assertEquals(true, DanaRS_Packet_Notify_Delivery_Complete.done) + Assert.assertEquals(true, danaRSPlugin.bolusDone) Assert.assertEquals("NOTIFY__DELIVERY_COMPLETE", packet.friendlyName) }