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

View file

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

View file

@ -9,6 +9,7 @@ import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
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.SP;
import static org.mockito.ArgumentMatchers.anyDouble;
import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(PowerMockRunner.class)
@ -33,6 +35,8 @@ public class TriggerAutosensValueTest {
@Test
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());
TriggerAutosensValue t = new TriggerAutosensValue().setValue(110).comparator(Comparator.Compare.IS_EQUAL);
Assert.assertEquals(110, t.getValue(), 0.01d);
@ -69,9 +73,11 @@ public class TriggerAutosensValueTest {
@Test
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 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());
}
@ -82,21 +88,25 @@ public class TriggerAutosensValueTest {
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
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);
Assert.assertEquals(ASJson, t.toJSON());
}
@Test
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 t2 = (TriggerAutosensValue) Trigger.instantiate(new JSONObject(t.toJSON()));
Assert.assertEquals(Comparator.Compare.IS_EQUAL, t2.getComparator().getValue());
Assert.assertEquals(410, t2.getValue(), 0.01d);
Assert.assertEquals(120, t2.getValue(), 0.01d);
}
@Test