From 12883a407eea5c485f9eb2edde00fea26070b2cd Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Thu, 2 Aug 2018 15:53:36 +0200 Subject: [PATCH 1/3] fix test coverage --- ...ket_APS_Basal_Set_Temporary_BasalTest.java | 83 +++++++++---------- 1 file changed, 40 insertions(+), 43 deletions(-) diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_APS_Basal_Set_Temporary_BasalTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_APS_Basal_Set_Temporary_BasalTest.java index 6831f889d7..af68836b6e 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_APS_Basal_Set_Temporary_BasalTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_APS_Basal_Set_Temporary_BasalTest.java @@ -1,8 +1,6 @@ package info.nightscout.androidaps.plugins.PumpDanaRS.comm; -import android.content.Context; - import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.core.classloader.annotations.PrepareForTest; @@ -10,9 +8,7 @@ import org.powermock.modules.junit4.PowerMockRunner; import info.AAPSMocker; import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.utils.SP; -import info.nightscout.utils.ToastUtils; import static org.junit.Assert.assertEquals; @@ -22,7 +18,7 @@ import static org.junit.Assert.assertEquals; @RunWith(PowerMockRunner.class) @PrepareForTest({MainApp.class, SP.class}) -public class DanaRS_Packet_APS_Basal_Set_Temporary_BasalTest extends DanaRS_Packet_APS_Basal_Set_Temporary_Basal { +public class DanaRS_Packet_APS_Basal_Set_Temporary_BasalTest { @Test public void runTest() { @@ -30,57 +26,58 @@ public class DanaRS_Packet_APS_Basal_Set_Temporary_BasalTest extends DanaRS_Pack AAPSMocker.mockApplicationContext(); AAPSMocker.mockSP(); // under 100% should last 30 min - setParams(0); - assertEquals(0, temporaryBasalRatio); - assertEquals(PARAM30MIN, temporaryBasalDuration); + DanaRS_Packet_APS_Basal_Set_Temporary_Basal packet = new DanaRS_Packet_APS_Basal_Set_Temporary_Basal(); + packet.setParams(0); + assertEquals(0, packet.temporaryBasalRatio); + assertEquals(packet.PARAM30MIN, packet.temporaryBasalDuration); // over 100% should last 15 min - setParams(150); - assertEquals(150, temporaryBasalRatio); - assertEquals(PARAM15MIN, temporaryBasalDuration); + packet.setParams(150); + assertEquals(150, packet.temporaryBasalRatio); + assertEquals(packet.PARAM15MIN, packet.temporaryBasalDuration); // test low hard limit - setParams(-1); - assertEquals(0, temporaryBasalRatio); - assertEquals(PARAM30MIN, temporaryBasalDuration); + packet.setParams(-1); + assertEquals(0, packet.temporaryBasalRatio); + assertEquals(packet.PARAM30MIN, packet.temporaryBasalDuration); // test high hard limit - setParams(550); - assertEquals(500, temporaryBasalRatio); - assertEquals(PARAM15MIN, temporaryBasalDuration); + packet.setParams(550); + assertEquals(500, packet.temporaryBasalRatio); + assertEquals(packet.PARAM15MIN, packet.temporaryBasalDuration); // test setting 15 min - setParams(50, true, false); - assertEquals(50, temporaryBasalRatio); - assertEquals(PARAM15MIN, temporaryBasalDuration); + packet.setParams(50, true, false); + assertEquals(50, packet.temporaryBasalRatio); + assertEquals(packet.PARAM15MIN, packet.temporaryBasalDuration); // test setting 30 min - setParams(50, false, true); - assertEquals(50, temporaryBasalRatio); - assertEquals(PARAM30MIN, temporaryBasalDuration); + packet.setParams(50, false, true); + assertEquals(50, packet.temporaryBasalRatio); + assertEquals(packet.PARAM30MIN, packet.temporaryBasalDuration); // over 200% set always 15 min - setParams(250, false, true); - assertEquals(250, temporaryBasalRatio); - assertEquals(PARAM15MIN, temporaryBasalDuration); + packet.setParams(250, false, true); + assertEquals(250, packet.temporaryBasalRatio); + assertEquals(packet.PARAM15MIN, packet.temporaryBasalDuration); // test low hard limit - setParams(-1, false, true); - assertEquals(0, temporaryBasalRatio); - assertEquals(PARAM30MIN, temporaryBasalDuration); + packet.setParams(-1, false, true); + assertEquals(0, packet.temporaryBasalRatio); + assertEquals(packet.PARAM30MIN, packet.temporaryBasalDuration); // test high hard limit - setParams(550, false,true); - assertEquals(500, temporaryBasalRatio); - assertEquals(PARAM15MIN, temporaryBasalDuration); + packet.setParams(550, false, true); + assertEquals(500, packet.temporaryBasalRatio); + assertEquals(packet.PARAM15MIN, packet.temporaryBasalDuration); // test message generation - setParams(260, true, false); - byte[] generatedCode = getRequestParams(); - assertEquals(3 , generatedCode.length); - assertEquals((byte)4 , generatedCode[0]); - assertEquals((byte)1 , generatedCode[1]); - assertEquals((byte)PARAM15MIN, generatedCode[2]); + packet.setParams(260, true, false); + byte[] generatedCode = packet.getRequestParams(); + assertEquals(3, generatedCode.length); + assertEquals((byte) 4, generatedCode[0]); + assertEquals((byte) 1, generatedCode[1]); + assertEquals((byte) packet.PARAM15MIN, generatedCode[2]); // test message decoding - handleMessage(new byte[]{(byte) 0, (byte) 0, (byte) 0}); - assertEquals(false, failed); - handleMessage(new byte[]{(byte) 0, (byte) 0, (byte) 1}); - assertEquals(true, failed); + packet.handleMessage(new byte[]{(byte) 0, (byte) 0, (byte) 0}); + assertEquals(false, packet.failed); + packet.handleMessage(new byte[]{(byte) 0, (byte) 0, (byte) 1}); + assertEquals(true, packet.failed); - assertEquals("BASAL__APS_SET_TEMPORARY_BASAL", getFriendlyName()); + assertEquals("BASAL__APS_SET_TEMPORARY_BASAL", packet.getFriendlyName()); } } From 5d7bebbbdc552913b0cd28bb5dc3618478757b28 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Thu, 2 Aug 2018 16:11:30 +0200 Subject: [PATCH 2/3] L.class mock --- app/src/test/java/info/AAPSMocker.java | 7 +++++++ ...anaRS_Packet_APS_Basal_Set_Temporary_BasalTest.java | 10 +++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/app/src/test/java/info/AAPSMocker.java b/app/src/test/java/info/AAPSMocker.java index c02c69cd07..0bb6890054 100644 --- a/app/src/test/java/info/AAPSMocker.java +++ b/app/src/test/java/info/AAPSMocker.java @@ -19,11 +19,13 @@ import info.nightscout.androidaps.data.ConstraintChecker; import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.data.ProfileStore; import info.nightscout.androidaps.db.DatabaseHelper; +import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.Treatments.TreatmentService; import info.nightscout.androidaps.queue.CommandQueue; import info.nightscout.utils.SP; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyLong; @@ -122,6 +124,11 @@ public class AAPSMocker { when(SP.getInt(anyInt(), anyInt())).thenReturn(0); } + public static void mockL() { + PowerMockito.mockStatic(L.class); + when(L.isEnabled(any())).thenReturn(true); + } + public static void mockApplicationContext() { Context context = mock(Context.class); when(MainApp.instance().getApplicationContext()).thenReturn(context); diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_APS_Basal_Set_Temporary_BasalTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_APS_Basal_Set_Temporary_BasalTest.java index af68836b6e..3c52db56a4 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_APS_Basal_Set_Temporary_BasalTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_APS_Basal_Set_Temporary_BasalTest.java @@ -8,16 +8,18 @@ import org.powermock.modules.junit4.PowerMockRunner; import info.AAPSMocker; import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.logging.L; import info.nightscout.utils.SP; import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; /** * Created by mike on 20.11.2017. */ @RunWith(PowerMockRunner.class) -@PrepareForTest({MainApp.class, SP.class}) +@PrepareForTest({MainApp.class, SP.class, L.class}) public class DanaRS_Packet_APS_Basal_Set_Temporary_BasalTest { @Test @@ -25,11 +27,17 @@ public class DanaRS_Packet_APS_Basal_Set_Temporary_BasalTest { AAPSMocker.mockMainApp(); AAPSMocker.mockApplicationContext(); AAPSMocker.mockSP(); + AAPSMocker.mockL(); + // under 100% should last 30 min DanaRS_Packet_APS_Basal_Set_Temporary_Basal packet = new DanaRS_Packet_APS_Basal_Set_Temporary_Basal(); packet.setParams(0); assertEquals(0, packet.temporaryBasalRatio); assertEquals(packet.PARAM30MIN, packet.temporaryBasalDuration); + //constructor with param + packet = new DanaRS_Packet_APS_Basal_Set_Temporary_Basal(10); + assertEquals(10, packet.temporaryBasalRatio); + assertEquals(packet.PARAM30MIN, packet.temporaryBasalDuration); // over 100% should last 15 min packet.setParams(150); assertEquals(150, packet.temporaryBasalRatio); From 1ae885cb17991dc882b5f66ea5c93cf8d80820b1 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Thu, 2 Aug 2018 16:22:22 +0200 Subject: [PATCH 3/3] DanaRS_Packet_APS_Basal_Set_Temporary_BasalTest update --- .../comm/DanaRS_Packet_APS_Basal_Set_Temporary_BasalTest.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_APS_Basal_Set_Temporary_BasalTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_APS_Basal_Set_Temporary_BasalTest.java index 3c52db56a4..74c5038b71 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_APS_Basal_Set_Temporary_BasalTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_APS_Basal_Set_Temporary_BasalTest.java @@ -38,6 +38,9 @@ public class DanaRS_Packet_APS_Basal_Set_Temporary_BasalTest { packet = new DanaRS_Packet_APS_Basal_Set_Temporary_Basal(10); assertEquals(10, packet.temporaryBasalRatio); assertEquals(packet.PARAM30MIN, packet.temporaryBasalDuration); + packet = new DanaRS_Packet_APS_Basal_Set_Temporary_Basal(20, true, false); + assertEquals(20, packet.temporaryBasalRatio); + assertEquals(packet.PARAM15MIN, packet.temporaryBasalDuration); // over 100% should last 15 min packet.setParams(150); assertEquals(150, packet.temporaryBasalRatio);