From e8819897f3504fcfca0ed49de4c9ada39502354c Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Thu, 19 Mar 2020 21:37:57 +0100 Subject: [PATCH] ObjectivesPluginTest --- .../objectives/ObjectivesPlugin.kt | 12 +-- .../objectives/objectives/Objective.java | 2 +- .../objectives/ObjectivesPluginTest.java | 88 ------------------- .../objectives/ObjectivesPluginTest.kt | 87 ++++++++++++++++++ 4 files changed, 94 insertions(+), 95 deletions(-) delete mode 100644 app/src/test/java/info/nightscout/androidaps/plugins/constraints/objectives/ObjectivesPluginTest.java create mode 100644 app/src/test/java/info/nightscout/androidaps/plugins/constraints/objectives/ObjectivesPluginTest.kt diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/ObjectivesPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/ObjectivesPlugin.kt index 95abd25df1..e357a1b57d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/ObjectivesPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/ObjectivesPlugin.kt @@ -146,37 +146,37 @@ class ObjectivesPlugin @Inject constructor( */ override fun isLoopInvocationAllowed(value: Constraint): Constraint { if (!objectives[FIRST_OBJECTIVE].isStarted) - value.set(aapsLogger, false, resourceHelper.gs(R.string.objectivenotstarted, FIRST_OBJECTIVE + 1), this) + value.set(aapsLogger, false, String.format(resourceHelper.gs(R.string.objectivenotstarted), FIRST_OBJECTIVE + 1), this) return value } override fun isClosedLoopAllowed(value: Constraint): Constraint { if (!objectives[MAXIOB_ZERO_CL_OBJECTIVE].isStarted) - value.set(aapsLogger, false, resourceHelper.gs(R.string.objectivenotstarted, MAXIOB_ZERO_CL_OBJECTIVE + 1), this) + value.set(aapsLogger, false, String.format(resourceHelper.gs(R.string.objectivenotstarted), MAXIOB_ZERO_CL_OBJECTIVE + 1), this) return value } override fun isAutosensModeEnabled(value: Constraint): Constraint { if (!objectives[AUTOSENS_OBJECTIVE].isStarted) - value.set(aapsLogger, false, resourceHelper.gs(R.string.objectivenotstarted, AUTOSENS_OBJECTIVE + 1), this) + value.set(aapsLogger, false, String.format(resourceHelper.gs(R.string.objectivenotstarted), AUTOSENS_OBJECTIVE + 1), this) return value } override fun isAMAModeEnabled(value: Constraint): Constraint { if (!objectives[AMA_OBJECTIVE].isStarted) - value.set(aapsLogger, false, resourceHelper.gs(R.string.objectivenotstarted, AMA_OBJECTIVE + 1), this) + value.set(aapsLogger, false, String.format(resourceHelper.gs(R.string.objectivenotstarted), AMA_OBJECTIVE + 1), this) return value } override fun isSMBModeEnabled(value: Constraint): Constraint { if (!objectives[SMB_OBJECTIVE].isStarted) - value.set(aapsLogger, false, resourceHelper.gs(R.string.objectivenotstarted, SMB_OBJECTIVE + 1), this) + value.set(aapsLogger, false, String.format(resourceHelper.gs(R.string.objectivenotstarted), SMB_OBJECTIVE + 1), this) return value } override fun applyMaxIOBConstraints(maxIob: Constraint): Constraint { if (objectives[MAXIOB_ZERO_CL_OBJECTIVE].isStarted && !objectives[MAXIOB_ZERO_CL_OBJECTIVE].isAccomplished) - maxIob.set(aapsLogger, 0.0, resourceHelper.gs(R.string.objectivenotfinished, MAXIOB_ZERO_CL_OBJECTIVE + 1), this) + maxIob.set(aapsLogger, 0.0, String.format(resourceHelper.gs(R.string.objectivenotfinished), MAXIOB_ZERO_CL_OBJECTIVE + 1), this) return maxIob } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/objectives/Objective.java b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/objectives/Objective.java index c37bf22a23..aaa21d59dc 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/objectives/Objective.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/objectives/Objective.java @@ -24,7 +24,7 @@ import info.nightscout.androidaps.utils.sharedPreferences.SP; public abstract class Objective { @Inject public SP sp; - @Inject ResourceHelper resourceHelper; + @Inject public ResourceHelper resourceHelper; private String spName; @StringRes private int objective; diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/constraints/objectives/ObjectivesPluginTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/constraints/objectives/ObjectivesPluginTest.java deleted file mode 100644 index 371e970d23..0000000000 --- a/app/src/test/java/info/nightscout/androidaps/plugins/constraints/objectives/ObjectivesPluginTest.java +++ /dev/null @@ -1,88 +0,0 @@ -package info.nightscout.androidaps.plugins.constraints.objectives; - -import junit.framework.Assert; - -import org.junit.Before; -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.interfaces.Constraint; -import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; -import info.nightscout.androidaps.utils.DateUtil; -import info.nightscout.androidaps.utils.SP; - -/** - * Created by mike on 23.03.2018. - */ - -@RunWith(PowerMockRunner.class) -@PrepareForTest({MainApp.class, ConfigBuilderPlugin.class, SP.class}) -public class ObjectivesPluginTest { - - ObjectivesPlugin objectivesPlugin; - - @Test - public void notStartedObjectivesShouldLimitLoopInvocation() { - objectivesPlugin.getObjectives().get(ObjectivesPlugin.INSTANCE.getFIRST_OBJECTIVE()).setStartedOn(0); - - Constraint c = new Constraint<>(true); - c = objectivesPlugin.isLoopInvocationAllowed(c); - Assert.assertEquals("Objectives: Objective 1 not started", c.getReasons()); - Assert.assertEquals(Boolean.FALSE, c.value()); - objectivesPlugin.getObjectives().get(ObjectivesPlugin.INSTANCE.getFIRST_OBJECTIVE()).setStartedOn(DateUtil.now()); - } - - @Test - public void notStartedObjective6ShouldLimitClosedLoop() { - objectivesPlugin.getObjectives().get(ObjectivesPlugin.INSTANCE.getMAXIOB_ZERO_CL_OBJECTIVE()).setStartedOn(0); - - Constraint c = new Constraint<>(true); - c = objectivesPlugin.isClosedLoopAllowed(c); - Assert.assertEquals(true, c.getReasons().contains("Objective 6 not started")); - Assert.assertEquals(Boolean.FALSE, c.value()); - } - - @Test - public void notStartedObjective8ShouldLimitAutosensMode() { - objectivesPlugin.getObjectives().get(ObjectivesPlugin.INSTANCE.getAUTOSENS_OBJECTIVE()).setStartedOn(0); - - Constraint c = new Constraint<>(true); - c = objectivesPlugin.isAutosensModeEnabled(c); - Assert.assertEquals(true, c.getReasons().contains("Objective 8 not started")); - Assert.assertEquals(Boolean.FALSE, c.value()); - } - - @Test - public void notStartedObjective9ShouldLimitAMAMode() { - objectivesPlugin.getObjectives().get(ObjectivesPlugin.INSTANCE.getAMA_OBJECTIVE()).setStartedOn(0); - - Constraint c = new Constraint<>(true); - c = objectivesPlugin.isAMAModeEnabled(c); - Assert.assertEquals(true, c.getReasons().contains("Objective 9 not started")); - Assert.assertEquals(Boolean.FALSE, c.value()); - } - - @Test - public void notStartedObjective10ShouldLimitSMBMode() { - objectivesPlugin.getObjectives().get(ObjectivesPlugin.INSTANCE.getSMB_OBJECTIVE()).setStartedOn(0); - - Constraint c = new Constraint<>(true); - c = objectivesPlugin.isSMBModeEnabled(c); - Assert.assertEquals(true, c.getReasons().contains("Objective 10 not started")); - Assert.assertEquals(Boolean.FALSE, c.value()); - } - - @Before - public void prepareMock() { - AAPSMocker.mockMainApp(); - AAPSMocker.mockConfigBuilder(); - AAPSMocker.mockSP(); - AAPSMocker.mockStrings(); - - objectivesPlugin = ObjectivesPlugin.INSTANCE; - } -} diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/constraints/objectives/ObjectivesPluginTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/constraints/objectives/ObjectivesPluginTest.kt new file mode 100644 index 0000000000..7417fd5ec8 --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/constraints/objectives/ObjectivesPluginTest.kt @@ -0,0 +1,87 @@ +package info.nightscout.androidaps.plugins.constraints.objectives + +import dagger.android.AndroidInjector +import dagger.android.HasAndroidInjector +import info.nightscout.androidaps.R +import info.nightscout.androidaps.TestBase +import info.nightscout.androidaps.interfaces.ActivePluginProvider +import info.nightscout.androidaps.interfaces.Constraint +import info.nightscout.androidaps.logging.AAPSLogger +import info.nightscout.androidaps.plugins.constraints.objectives.objectives.Objective +import info.nightscout.androidaps.utils.DateUtil +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.modules.junit4.PowerMockRunner + +@RunWith(PowerMockRunner::class) +class ObjectivesPluginTest : TestBase() { + + @Mock lateinit var aapsLogger: AAPSLogger + @Mock lateinit var resourceHelper: ResourceHelper + @Mock lateinit var activePlugin: ActivePluginProvider + @Mock lateinit var sp: SP + + private lateinit var objectivesPlugin: ObjectivesPlugin + + val injector = HasAndroidInjector { + AndroidInjector { + if (it is Objective) { + it.sp = sp + it.resourceHelper = resourceHelper + } + } + } + + @Before fun prepareMock() { + objectivesPlugin = ObjectivesPlugin(injector, aapsLogger, resourceHelper, activePlugin, sp) + objectivesPlugin.onStart() + `when`(resourceHelper.gs(R.string.objectivenotstarted)).thenReturn("Objective %1\$d not started") + } + + @Test fun notStartedObjectivesShouldLimitLoopInvocation() { + objectivesPlugin.objectives[ObjectivesPlugin.FIRST_OBJECTIVE].startedOn = 0 + var c = Constraint(true) + c = objectivesPlugin.isLoopInvocationAllowed(c) + Assert.assertEquals("Objectives: Objective 1 not started", c.getReasons(aapsLogger)) + Assert.assertEquals(false, c.value()) + objectivesPlugin.objectives[ObjectivesPlugin.FIRST_OBJECTIVE].startedOn = DateUtil.now() + } + + @Test fun notStartedObjective6ShouldLimitClosedLoop() { + objectivesPlugin.objectives[ObjectivesPlugin.MAXIOB_ZERO_CL_OBJECTIVE].startedOn = 0 + var c = Constraint(true) + c = objectivesPlugin.isClosedLoopAllowed(c) + Assert.assertEquals(true, c.getReasons(aapsLogger).contains("Objective 6 not started")) + Assert.assertEquals(false, c.value()) + } + + @Test fun notStartedObjective8ShouldLimitAutosensMode() { + objectivesPlugin.objectives[ObjectivesPlugin.AUTOSENS_OBJECTIVE].startedOn = 0 + var c = Constraint(true) + c = objectivesPlugin.isAutosensModeEnabled(c) + Assert.assertEquals(true, c.getReasons(aapsLogger).contains("Objective 8 not started")) + Assert.assertEquals(false, c.value()) + } + + @Test fun notStartedObjective9ShouldLimitAMAMode() { + objectivesPlugin.objectives[ObjectivesPlugin.AMA_OBJECTIVE].startedOn = 0 + var c = Constraint(true) + c = objectivesPlugin.isAMAModeEnabled(c) + Assert.assertEquals(true, c.getReasons(aapsLogger).contains("Objective 9 not started")) + Assert.assertEquals(false, c.value()) + } + + @Test fun notStartedObjective10ShouldLimitSMBMode() { + objectivesPlugin.objectives[ObjectivesPlugin.SMB_OBJECTIVE].startedOn = 0 + var c = Constraint(true) + c = objectivesPlugin.isSMBModeEnabled(c) + Assert.assertEquals(true, c.getReasons(aapsLogger).contains("Objective 10 not started")) + Assert.assertEquals(false, c.value()) + } +} \ No newline at end of file