Created enum for mode & tests

This commit is contained in:
Roumen Georgiev 2019-12-04 16:42:47 +02:00
parent aae2e21bef
commit 14dc7395e0
4 changed files with 96 additions and 84 deletions

View file

@ -5,42 +5,68 @@ import android.widget.ArrayAdapter;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.Spinner; import android.widget.Spinner;
import androidx.annotation.StringRes;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
public class InputSelect extends Element { public class InputLocationMode extends Element {
public enum Mode {
INSIDE,
OUTSIDE,
GOING_IN,
GOING_OUT;
public @StringRes
int getStringRes() {
switch (this) {
case INSIDE:
return R.string.location_inside;
case OUTSIDE:
return R.string.location_outside;
default:
return R.string.unknown;
}
}
public static List<String> labels() {
List<String> list = new ArrayList<>();
for (Mode c : Mode.values()) {
list.add(MainApp.gs(c.getStringRes()));
}
return list;
}
public Mode fromString(String wanted){
for (Mode c : Mode.values()) {
if(c.toString() == wanted)
return c;
}
return null;
}
}
private Mode mode;
private InputOption selected; private InputOption selected;
private ArrayList<InputOption> options;
public InputSelect() { public InputLocationMode() {
super(); super();
mode = Mode.INSIDE;
} }
public InputSelect(ArrayList<InputOption> options) { public InputLocationMode(InputLocationMode another) {
super(); super();
this.options = options; this.mode = another.mode;
}
public InputSelect(ArrayList<InputOption> options, InputOption selected) {
super();
this.options = options;
this.selected = selected;
}
public InputSelect(InputSelect another) {
super();
options = another.options;
selected = another.selected;
} }
@Override @Override
public void addToLayout(LinearLayout root) { public void addToLayout(LinearLayout root) {
ArrayAdapter<String> adapter = new ArrayAdapter<>(root.getContext(), ArrayAdapter<String> adapter = new ArrayAdapter<>(root.getContext(),
R.layout.spinner_centered, labels()); R.layout.spinner_centered, Mode.labels());
Spinner spinner = new Spinner(root.getContext()); Spinner spinner = new Spinner(root.getContext());
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter); spinner.setAdapter(adapter);
@ -53,41 +79,27 @@ public class InputSelect extends Element {
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override @Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
setValue(options.get(position)); // mode = Mode.values()[position];
setValue(Mode.values()[position]);
} }
@Override @Override
public void onNothingSelected(AdapterView<?> parent) { public void onNothingSelected(AdapterView<?> parent) {
} }
}); });
spinner.setSelection(selected == null ? 0 : options.indexOf(selected)); spinner.setSelection(this.getValue().ordinal());
root.addView(spinner); root.addView(spinner);
} }
public String getValue() { public Mode getValue() {
return selected == null ? null : selected.getValue(); return mode;
} }
public InputSelect setValue(InputOption selected) { public InputLocationMode setValue(Mode mode) {
this.selected = selected; this.mode = mode;
return this; return this;
} }
public InputSelect setValue(String selected) {
for (InputOption o: options) {
if (o.getValue().equals(selected)) {
this.selected = o;
break;
}
}
return this;
}
private List<String> labels() {
List<String> list = new ArrayList<>();
for (InputOption o : options) {
list.add(MainApp.gs(o.getStringRes()));
}
return list;
}
} }

View file

@ -6,6 +6,8 @@ public class InputOption {
public InputOption(int stringRes, String value) { public InputOption(int stringRes, String value) {
this.stringRes = stringRes; this.stringRes = stringRes;
if (value == null)
this.value = "";
this.value = value; this.value = value;
} }

View file

@ -3,7 +3,6 @@ package info.nightscout.androidaps.plugins.general.automation.triggers;
import android.location.Location; import android.location.Location;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import com.google.common.base.Optional; import com.google.common.base.Optional;
@ -14,16 +13,13 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.general.automation.elements.InputButton; import info.nightscout.androidaps.plugins.general.automation.elements.InputButton;
import info.nightscout.androidaps.plugins.general.automation.elements.InputDouble; import info.nightscout.androidaps.plugins.general.automation.elements.InputDouble;
import info.nightscout.androidaps.plugins.general.automation.elements.InputOption; import info.nightscout.androidaps.plugins.general.automation.elements.InputLocationMode;
import info.nightscout.androidaps.plugins.general.automation.elements.InputSelect;
import info.nightscout.androidaps.plugins.general.automation.elements.InputString; import info.nightscout.androidaps.plugins.general.automation.elements.InputString;
import info.nightscout.androidaps.plugins.general.automation.elements.LabelWithElement; import info.nightscout.androidaps.plugins.general.automation.elements.LabelWithElement;
import info.nightscout.androidaps.plugins.general.automation.elements.LayoutBuilder; import info.nightscout.androidaps.plugins.general.automation.elements.LayoutBuilder;
@ -36,16 +32,11 @@ import info.nightscout.androidaps.utils.T;
public class TriggerLocation extends Trigger { public class TriggerLocation extends Trigger {
private static Logger log = LoggerFactory.getLogger(L.AUTOMATION); private static Logger log = LoggerFactory.getLogger(L.AUTOMATION);
private static final ArrayList<InputOption> modes = new ArrayList<>(Arrays.asList(
new InputOption(R.string.location_inside, MainApp.gs(R.string.location_inside)),
new InputOption(R.string.location_outside, MainApp.gs(R.string.location_outside))
));
InputDouble latitude = new InputDouble(0d, -90d, +90d, 0.000001d, new DecimalFormat("0.000000")); InputDouble latitude = new InputDouble(0d, -90d, +90d, 0.000001d, new DecimalFormat("0.000000"));
InputDouble longitude = new InputDouble(0d, -180d, +180d, 0.000001d, new DecimalFormat("0.000000")); InputDouble longitude = new InputDouble(0d, -180d, +180d, 0.000001d, new DecimalFormat("0.000000"));
InputDouble distance = new InputDouble(200d, 0, 100000, 10d, new DecimalFormat("0")); InputDouble distance = new InputDouble(200d, 0, 100000, 10d, new DecimalFormat("0"));
// Default mode selected is 1 - inside area // Default mode selected is 0 - inside area
InputSelect modeSelected = new InputSelect(modes); InputLocationMode modeSelected = new InputLocationMode();
InputString name = new InputString(); InputString name = new InputString();
@ -67,7 +58,7 @@ public class TriggerLocation extends Trigger {
latitude = new InputDouble(triggerLocation.latitude); latitude = new InputDouble(triggerLocation.latitude);
longitude = new InputDouble(triggerLocation.longitude); longitude = new InputDouble(triggerLocation.longitude);
distance = new InputDouble(triggerLocation.distance); distance = new InputDouble(triggerLocation.distance);
modeSelected = new InputSelect(modes); modeSelected = new InputLocationMode(triggerLocation.modeSelected);
lastRun = triggerLocation.lastRun; lastRun = triggerLocation.lastRun;
name = triggerLocation.name; name = triggerLocation.name;
} }
@ -85,8 +76,8 @@ public class TriggerLocation extends Trigger {
a.setLatitude(latitude.getValue()); a.setLatitude(latitude.getValue());
a.setLongitude(longitude.getValue()); a.setLongitude(longitude.getValue());
double calculatedDistance = location.distanceTo(a); double calculatedDistance = location.distanceTo(a);
if (((stringToMode(modeSelected.getValue()) == 1d) && (calculatedDistance < distance.getValue())) || if (((modeSelected.getValue().ordinal()) == 1d) && (calculatedDistance < distance.getValue()) ||
((stringToMode(modeSelected.getValue()) == 2d) && (calculatedDistance > distance.getValue()))) { ((modeSelected.getValue().ordinal() == 2d) && (calculatedDistance > distance.getValue()))) {
if (L.isEnabled(L.AUTOMATION)) if (L.isEnabled(L.AUTOMATION))
log.debug("Ready for execution: " + friendlyDescription()); log.debug("Ready for execution: " + friendlyDescription());
return true; return true;
@ -104,12 +95,13 @@ public class TriggerLocation extends Trigger {
data.put("longitude", longitude.getValue()); data.put("longitude", longitude.getValue());
data.put("distance", distance.getValue()); data.put("distance", distance.getValue());
data.put("name", name.getValue()); data.put("name", name.getValue());
data.put("mode", stringToMode(modeSelected.getValue())); data.put("mode", modeSelected.getValue());
data.put("lastRun", lastRun); data.put("lastRun", lastRun);
o.put("data", data); o.put("data", data);
} catch (JSONException e) { } catch (JSONException e) {
log.error("Unhandled exception", e); log.error("Unhandled exception", e);
} }
log.debug("JSON "+o.toString());
return o.toString(); return o.toString();
} }
@ -121,7 +113,7 @@ public class TriggerLocation extends Trigger {
longitude.setValue(JsonHelper.safeGetDouble(d, "longitude")); longitude.setValue(JsonHelper.safeGetDouble(d, "longitude"));
distance.setValue(JsonHelper.safeGetDouble(d, "distance")); distance.setValue(JsonHelper.safeGetDouble(d, "distance"));
name.setValue(JsonHelper.safeGetString(d, "name")); name.setValue(JsonHelper.safeGetString(d, "name"));
modeSelected.setValue(modeToString(JsonHelper.safeGetDouble(d, "mode"))); modeSelected.setValue(InputLocationMode.Mode.valueOf(JsonHelper.safeGetString(d, "mode")));
lastRun = JsonHelper.safeGetLong(d, "lastRun"); lastRun = JsonHelper.safeGetLong(d, "lastRun");
} catch (Exception e) { } catch (Exception e) {
log.error("Unhandled exception", e); log.error("Unhandled exception", e);
@ -170,8 +162,9 @@ public class TriggerLocation extends Trigger {
return this; return this;
} }
TriggerLocation setMode(double value) { TriggerLocation setMode(InputLocationMode.Mode value) {
modeSelected.setValue(modeToString(value));
modeSelected.setValue(value);
return this; return this;
} }
@ -188,25 +181,4 @@ public class TriggerLocation extends Trigger {
.build(root); .build(root);
} }
public double stringToMode(String selected) {
if (selected == null)
return 1d;
if (selected.equals(MainApp.gs(R.string.location_inside)))
return 1d;
else if (selected.equals(MainApp.gs(R.string.location_outside)))
return 2d;
else return 0d;
}
public String modeToString(double selectedMode) {
if (selectedMode == 1d)
return MainApp.gs(R.string.location_inside);
else if (selectedMode == 2d)
return MainApp.gs(R.string.location_outside);
else
return MainApp.gs(R.string.location_mode);
}
} }

View file

@ -15,10 +15,15 @@ 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;
import java.util.ArrayList;
import java.util.Arrays;
import info.AAPSMocker; import info.AAPSMocker;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
import info.nightscout.androidaps.plugins.general.automation.elements.InputOption;
import info.nightscout.androidaps.plugins.general.automation.elements.InputLocationMode;
import info.nightscout.androidaps.services.LocationService; import info.nightscout.androidaps.services.LocationService;
import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.DateUtil;
@ -31,6 +36,11 @@ public class TriggerLocationTest {
long now = 1514766900000L; long now = 1514766900000L;
private static final ArrayList<InputOption> modes = new ArrayList<>(Arrays.asList(
new InputOption(1, "inside"),
new InputOption(2, "outside")
));
@Before @Before
public void mock() { public void mock() {
AAPSMocker.mockMainApp(); AAPSMocker.mockMainApp();
@ -53,11 +63,14 @@ public class TriggerLocationTest {
t.latitude.setValue(213); t.latitude.setValue(213);
t.longitude.setValue(212); t.longitude.setValue(212);
t.distance.setValue(2); t.distance.setValue(2);
t.modeSelected.setValue(InputLocationMode.Mode.INSIDE);
TriggerLocation t1 = (TriggerLocation) t.duplicate(); TriggerLocation t1 = (TriggerLocation) t.duplicate();
Assert.assertEquals(213d, t.latitude.getValue(), 0.01d); Assert.assertEquals(213d, t1.latitude.getValue(), 0.01d);
Assert.assertEquals(212d, t.longitude.getValue(), 0.01d); Assert.assertEquals(212d, t1.longitude.getValue(), 0.01d);
Assert.assertEquals(2d, t.distance.getValue(), 0.01d); Assert.assertEquals(2d, t1.distance.getValue(), 0.01d);
Assert.assertEquals(InputLocationMode.Mode.INSIDE, t1.modeSelected.getValue());
} }
@Test @Test
@ -66,6 +79,8 @@ public class TriggerLocationTest {
t.latitude.setValue(213); t.latitude.setValue(213);
t.longitude.setValue(212); t.longitude.setValue(212);
t.distance.setValue(2); t.distance.setValue(2);
t.modeSelected.setValue(InputLocationMode.Mode.OUTSIDE);
PowerMockito.when(LocationService.getLastLocation()).thenReturn(null); PowerMockito.when(LocationService.getLastLocation()).thenReturn(null);
Assert.assertFalse(t.shouldRun()); Assert.assertFalse(t.shouldRun());
PowerMockito.when(LocationService.getLastLocation()).thenReturn(mockedLocation()); PowerMockito.when(LocationService.getLastLocation()).thenReturn(mockedLocation());
@ -78,7 +93,7 @@ public class TriggerLocationTest {
Assert.assertFalse(t.shouldRun()); Assert.assertFalse(t.shouldRun());
} }
String locationJson = "{\"data\":{\"distance\":2,\"lastRun\":0,\"latitude\":213,\"name\":\"\",\"longitude\":212},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerLocation\"}"; String locationJson = "{\"data\":{\"mode\":\"OUTSIDE\",\"distance\":2,\"lastRun\":0,\"latitude\":213,\"name\":\"\",\"longitude\":212},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerLocation\"}";
@Test @Test
public void toJSONTest() { public void toJSONTest() {
@ -86,6 +101,7 @@ public class TriggerLocationTest {
t.latitude.setValue(213); t.latitude.setValue(213);
t.longitude.setValue(212); t.longitude.setValue(212);
t.distance.setValue(2); t.distance.setValue(2);
t.modeSelected = t.modeSelected.setValue(InputLocationMode.Mode.OUTSIDE);
Assert.assertEquals(locationJson, t.toJSON()); Assert.assertEquals(locationJson, t.toJSON());
} }
@ -95,11 +111,14 @@ public class TriggerLocationTest {
t.latitude.setValue(213); t.latitude.setValue(213);
t.longitude.setValue(212); t.longitude.setValue(212);
t.distance.setValue(2); t.distance.setValue(2);
// t.setMode(t.stringToMode(new InputSelect(modes).getValue()));
t.modeSelected.setValue(InputLocationMode.Mode.INSIDE);
TriggerLocation t2 = (TriggerLocation) Trigger.instantiate(new JSONObject(t.toJSON())); TriggerLocation t2 = (TriggerLocation) Trigger.instantiate(new JSONObject(t.toJSON()));
Assert.assertEquals(t.latitude.getValue(), t2.latitude.getValue(), 0.01d); Assert.assertEquals(t.latitude.getValue(), t2.latitude.getValue(), 0.01d);
Assert.assertEquals(t.longitude.getValue(), t2.longitude.getValue(), 0.01d); Assert.assertEquals(t.longitude.getValue(), t2.longitude.getValue(), 0.01d);
Assert.assertEquals(t.distance.getValue(), t2.distance.getValue(), 0.01d); Assert.assertEquals(t.distance.getValue(), t2.distance.getValue(), 0.01d);
Assert.assertEquals(t.modeSelected.getValue(), t2.modeSelected.getValue());
} }
@Test @Test
@ -139,6 +158,13 @@ public class TriggerLocationTest {
Assert.assertEquals(t.distance.getValue(), 2, 0d); Assert.assertEquals(t.distance.getValue(), 2, 0d);
} }
@Test
public void setModeTest() {
TriggerLocation t = new TriggerLocation();
t.setMode(InputLocationMode.Mode.INSIDE);
Assert.assertEquals(t.modeSelected.getValue(), InputLocationMode.Mode.INSIDE);
}
@Test @Test
public void lastRunTest() { public void lastRunTest() {
TriggerLocation t = new TriggerLocation(); TriggerLocation t = new TriggerLocation();