fix for maxValue and tests

This commit is contained in:
Roumen Georgiev 2019-05-15 17:11:47 +03:00
parent 31224f894d
commit 3280f220ad
3 changed files with 24 additions and 13 deletions

View file

@ -11,8 +11,8 @@ import info.nightscout.androidaps.utils.SP;
public class InputAutosens extends Element { public class InputAutosens extends Element {
private int value; private int value;
int minValue = (int) (SP.getDouble("openapsama_autosens_min", 0.7d) * 100); public int minValue = (int) (SP.getDouble("openapsama_autosens_min", 0.7d) * 100);
int maxValue = (int) (SP.getDouble("openapsama_autosens_max", 1.2d) * 100); public int maxValue = (int) (SP.getDouble("openapsama_autosens_max", 1.2d) * 100);
private double step = 1; private double step = 1;
private DecimalFormat decimalFormat = new DecimalFormat("1");; private DecimalFormat decimalFormat = new DecimalFormat("1");;
@ -44,7 +44,7 @@ public class InputAutosens extends Element {
super(); super();
this.value = (int) value; this.value = (int) value;
this.minValue = (int) ( minValue * 100 ); this.minValue = (int) ( minValue * 100 );
this.maxValue = (int) (maxValue * 100 ); this.maxValue = (int) ( maxValue * 100 );
this.step = step; this.step = step;
this.decimalFormat = decimalFormat; this.decimalFormat = decimalFormat;
} }
@ -63,10 +63,7 @@ public class InputAutosens extends Element {
public void addToLayout(LinearLayout root) { public void addToLayout(LinearLayout root) {
minValue = (int) (SP.getDouble("openapsama_autosens_min", 0.7d) * 100); minValue = (int) (SP.getDouble("openapsama_autosens_min", 0.7d) * 100);
maxValue = (int) (SP.getDouble("openapsama_autosens_max", 1.2d) * 100); maxValue = (int) (SP.getDouble("openapsama_autosens_max", 1.2d) * 100);
if (value > maxValue) value = minValue & maxValue;
value = Math.max(value, this.maxValue);
if (value < minValue)
value = minValue;
numberPicker = new NumberPicker(root.getContext(), null); numberPicker = new NumberPicker(root.getContext(), null);
numberPicker.setParams((double) value, (double) minValue, (double) maxValue, step, decimalFormat, true, textWatcher); numberPicker.setParams((double) value, (double) minValue, (double) maxValue, step, decimalFormat, true, textWatcher);
numberPicker.setOnValueChangedListener(value -> this.value = (int) value); numberPicker.setOnValueChangedListener(value -> this.value = (int) value);
@ -77,7 +74,7 @@ public class InputAutosens extends Element {
minValue = (int) (SP.getDouble("openapsama_autosens_min", 0.7d) * 100); minValue = (int) (SP.getDouble("openapsama_autosens_min", 0.7d) * 100);
maxValue = (int) (SP.getDouble("openapsama_autosens_max", 1.2d) * 100); maxValue = (int) (SP.getDouble("openapsama_autosens_max", 1.2d) * 100);
if (value > maxValue) if (value > maxValue)
value = Math.max(value, this.maxValue); value = maxValue;
if (value < minValue) if (value < minValue)
value = minValue; value = minValue;
this.value = value; this.value = value;

View file

@ -4,6 +4,7 @@ import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.modules.junit4.PowerMockRunner;
@ -22,6 +23,8 @@ public class InputAutosensTest {
@Test @Test
public void textWatcherTest() { public void textWatcherTest() {
when(SP.getDouble(Mockito.eq("openapsama_autosens_max"), anyDouble())).thenReturn(1.2d);
when(SP.getDouble(Mockito.eq("openapsama_autosens_min"), anyDouble())).thenReturn(0.7d);
InputAutosens t = new InputAutosens().setValue(100); InputAutosens t = new InputAutosens().setValue(100);
t.textWatcher.beforeTextChanged(null, 0, 0, 0); t.textWatcher.beforeTextChanged(null, 0, 0, 0);
t.textWatcher.onTextChanged(null, 0, 0, 0); t.textWatcher.onTextChanged(null, 0, 0, 0);
@ -30,14 +33,15 @@ public class InputAutosensTest {
t = new InputAutosens().setValue(200); t = new InputAutosens().setValue(200);
t.textWatcher.afterTextChanged(null); t.textWatcher.afterTextChanged(null);
Assert.assertEquals(200, t.getValue(), 0.01d); Assert.assertEquals(120, t.getValue(), 0.01d);
} }
@Test @Test
public void getSetValueTest() { public void getSetValueTest() {
when(SP.getDouble(Mockito.eq("openapsama_autosens_max"), anyDouble())).thenReturn(1.2d);
when(SP.getDouble(anyInt(), anyDouble())).thenReturn(0.7d); when(SP.getDouble(anyInt(), anyDouble())).thenReturn(0.7d);
InputAutosens i = new InputAutosens().setValue(500); InputAutosens i = new InputAutosens().setValue(500);
Assert.assertEquals(500, i.getValue(), 0.01d); Assert.assertEquals(120, i.getValue(), 0.01d);
Assert.assertEquals(0, i.minValue, 0.01d); Assert.assertEquals(0, i.minValue, 0.01d);
i = new InputAutosens().setValue(110); i = new InputAutosens().setValue(110);
Assert.assertEquals(110, i.getValue(), 0.01d); Assert.assertEquals(110, i.getValue(), 0.01d);

View file

@ -9,6 +9,7 @@ import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito; import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.modules.junit4.PowerMockRunner;
@ -23,6 +24,7 @@ import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorP
import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.SP; import info.nightscout.androidaps.utils.SP;
import static org.mockito.ArgumentMatchers.anyDouble;
import static org.powermock.api.mockito.PowerMockito.when; import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@ -33,6 +35,8 @@ public class TriggerAutosensValueTest {
@Test @Test
public void shouldRunTest() { public void shouldRunTest() {
Mockito.when(SP.getDouble(Mockito.eq("openapsama_autosens_max"), anyDouble())).thenReturn(1.2d);
Mockito.when(SP.getDouble(Mockito.eq("openapsama_autosens_min"), anyDouble())).thenReturn(0.7d);
when(IobCobCalculatorPlugin.getPlugin().getLastAutosensData("Automation trigger")).thenReturn(generateAutosensData()); when(IobCobCalculatorPlugin.getPlugin().getLastAutosensData("Automation trigger")).thenReturn(generateAutosensData());
TriggerAutosensValue t = new TriggerAutosensValue().setValue(110).comparator(Comparator.Compare.IS_EQUAL); TriggerAutosensValue t = new TriggerAutosensValue().setValue(110).comparator(Comparator.Compare.IS_EQUAL);
Assert.assertEquals(110, t.getValue(), 0.01d); Assert.assertEquals(110, t.getValue(), 0.01d);
@ -69,9 +73,11 @@ public class TriggerAutosensValueTest {
@Test @Test
public void copyConstructorTest() { public void copyConstructorTest() {
Mockito.when(SP.getDouble(Mockito.eq("openapsama_autosens_max"), anyDouble())).thenReturn(1.2d);
Mockito.when(SP.getDouble(Mockito.eq("openapsama_autosens_min"), anyDouble())).thenReturn(0.7d);
TriggerAutosensValue t = new TriggerAutosensValue().setValue(213).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); TriggerAutosensValue t = new TriggerAutosensValue().setValue(213).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER);
TriggerAutosensValue t1 = (TriggerAutosensValue) t.duplicate(); TriggerAutosensValue t1 = (TriggerAutosensValue) t.duplicate();
Assert.assertEquals(213, t1.getValue(), 0.01d); Assert.assertEquals(120, t1.getValue(), 0.01d);
Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.getComparator().getValue()); Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.getComparator().getValue());
} }
@ -82,21 +88,25 @@ public class TriggerAutosensValueTest {
Assert.assertEquals(1l, t.getLastRun()); Assert.assertEquals(1l, t.getLastRun());
} }
String ASJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"lastRun\":0,\"value\":410},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerAutosensValue\"}"; String ASJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"lastRun\":0,\"value\":120},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerAutosensValue\"}";
@Test @Test
public void toJSONTest() { public void toJSONTest() {
Mockito.when(SP.getDouble(Mockito.eq("openapsama_autosens_max"), anyDouble())).thenReturn(1.2d);
Mockito.when(SP.getDouble(Mockito.eq("openapsama_autosens_min"), anyDouble())).thenReturn(0.7d);
TriggerAutosensValue t = new TriggerAutosensValue().setValue(410).comparator(Comparator.Compare.IS_EQUAL); TriggerAutosensValue t = new TriggerAutosensValue().setValue(410).comparator(Comparator.Compare.IS_EQUAL);
Assert.assertEquals(ASJson, t.toJSON()); Assert.assertEquals(ASJson, t.toJSON());
} }
@Test @Test
public void fromJSONTest() throws JSONException { public void fromJSONTest() throws JSONException {
Mockito.when(SP.getDouble(Mockito.eq("openapsama_autosens_max"), anyDouble())).thenReturn(1.2d);
Mockito.when(SP.getDouble(Mockito.eq("openapsama_autosens_min"), anyDouble())).thenReturn(0.7d);
TriggerAutosensValue t = new TriggerAutosensValue().setValue(410).comparator(Comparator.Compare.IS_EQUAL); TriggerAutosensValue t = new TriggerAutosensValue().setValue(410).comparator(Comparator.Compare.IS_EQUAL);
TriggerAutosensValue t2 = (TriggerAutosensValue) Trigger.instantiate(new JSONObject(t.toJSON())); TriggerAutosensValue t2 = (TriggerAutosensValue) Trigger.instantiate(new JSONObject(t.toJSON()));
Assert.assertEquals(Comparator.Compare.IS_EQUAL, t2.getComparator().getValue()); Assert.assertEquals(Comparator.Compare.IS_EQUAL, t2.getComparator().getValue());
Assert.assertEquals(410, t2.getValue(), 0.01d); Assert.assertEquals(120, t2.getValue(), 0.01d);
} }
@Test @Test