some strings and tests broken

This commit is contained in:
Roumen Georgiev 2019-12-10 12:34:27 +02:00
parent cfdc59c983
commit 55e6246646
3 changed files with 65 additions and 8 deletions

View file

@ -68,7 +68,6 @@ public class TriggerLocation extends Trigger {
@Override @Override
public synchronized boolean shouldRun() { public synchronized boolean shouldRun() {
Location location = LocationService.getLastLocation(); Location location = LocationService.getLastLocation();
log.debug("Entered shouldRun()");
if (location == null) if (location == null)
return false; return false;
@ -203,4 +202,21 @@ public class TriggerLocation extends Trigger {
else else
return OUTSIDE; return OUTSIDE;
} }
double calculateDistance() {
Location location = LocationService.getLastLocation();
if (location == null)
return -1d;
Location a = new Location("Trigger");
a.setLatitude(latitude.getValue());
a.setLongitude(longitude.getValue());
return location.distanceTo(a);
}
String getLocation(){
Location location = LocationService.getLastLocation();
if (location == null)
return "No service!";
return location.toString();
}
} }

View file

@ -1410,12 +1410,12 @@
<string name="longitude_short">Lon:</string> <string name="longitude_short">Lon:</string>
<string name="distance_short">Dist [m]:</string> <string name="distance_short">Dist [m]:</string>
<string name="name_short">Name:</string> <string name="name_short">Name:</string>
<string name="locationis">Location is %1$s %2$s</string> <string name="locationis">%1$s %2$s</string>
<string name="location_mode">In/Out of defined area</string> <string name="location_mode">When </string>
<string name="location_inside">inside</string> <string name="location_inside">When you are inside the area</string>
<string name="location_outside">outside</string> <string name="location_outside">When you are inside the area</string>
<string name="location_going_in">when you enter the area named</string> <string name="location_going_in">When you enter the area named</string>
<string name="location_going_out">when you leave the area named</string> <string name="location_going_out">When you leave the area named</string>
<string name="lastboluslabel">Last bolus ago</string> <string name="lastboluslabel">Last bolus ago</string>
<string name="lastboluscompared">Last bolus time %1$s %2$s min ago</string> <string name="lastboluscompared">Last bolus time %1$s %2$s min ago</string>
<string name="triggercoblabel">COB</string> <string name="triggercoblabel">COB</string>

View file

@ -91,6 +91,33 @@ public class TriggerLocationTest {
t = new TriggerLocation(); t = new TriggerLocation();
t.distance.setValue(-500); t.distance.setValue(-500);
Assert.assertFalse(t.shouldRun()); Assert.assertFalse(t.shouldRun());
//Test of GOING_IN - last mode should be OUTSIDE, and current mode should be INSIDE
t = new TriggerLocation();
t.distance.setValue(50);
t.lastMode = t.currentMode(55d);
PowerMockito.when(LocationService.getLastLocation()).thenReturn(null);
PowerMockito.when(LocationService.getLastLocation()).thenReturn(mockedLocationOut());
t.modeSelected.setValue(InputLocationMode.Mode.GOING_IN);
Assert.assertEquals(t.lastMode, InputLocationMode.Mode.OUTSIDE);
Assert.assertEquals(t.currentMode(5d), InputLocationMode.Mode.INSIDE);
Assert.assertTrue(t.shouldRun());
//Test of GOING_OUT - last mode should be INSIDE, and current mode should be OUTSIDE
t = new TriggerLocation();
t.latitude.setValue(213);
t.longitude.setValue(212);
t.distance.setValue(2d);
t.lastMode = t.currentMode(1d);
PowerMockito.when(LocationService.getLastLocation()).thenReturn(mockedLocation());
t.modeSelected.setValue(InputLocationMode.Mode.GOING_OUT);
Assert.assertEquals(t.lastMode, InputLocationMode.Mode.INSIDE);
Assert.assertEquals(t.currentMode(55d), InputLocationMode.Mode.OUTSIDE);
PowerMockito.when(LocationService.getLastLocation()).thenReturn(null);
PowerMockito.when(LocationService.getLastLocation()).thenReturn(mockedLocationOut());
Assert.assertEquals(-1d, t.calculateDistance(), 0d);
Assert.assertTrue(t.shouldRun());
} }
String locationJson = "{\"data\":{\"mode\":\"OUTSIDE\",\"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\"}";
@ -111,7 +138,6 @@ 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); t.modeSelected.setValue(InputLocationMode.Mode.INSIDE);
TriggerLocation t2 = (TriggerLocation) Trigger.instantiate(new JSONObject(t.toJSON())); TriggerLocation t2 = (TriggerLocation) Trigger.instantiate(new JSONObject(t.toJSON()));
@ -172,6 +198,13 @@ public class TriggerLocationTest {
Assert.assertEquals(t.lastRun, 1514766900000L, 0d); Assert.assertEquals(t.lastRun, 1514766900000L, 0d);
} }
@Test
public void getLocationTest() {
PowerMockito.when(LocationService.getLastLocation()).thenReturn(mockedLocationOut());
TriggerLocation t = new TriggerLocation();
Assert.assertEquals("", t.getLocation());
}
public Location mockedLocation() { public Location mockedLocation() {
Location newLocation = new Location("test"); Location newLocation = new Location("test");
newLocation.setLatitude(10); newLocation.setLatitude(10);
@ -179,4 +212,12 @@ public class TriggerLocationTest {
newLocation.setAccuracy(1f); newLocation.setAccuracy(1f);
return newLocation; return newLocation;
} }
public Location mockedLocationOut() {
Location newLocation = new Location("test");
newLocation.setLatitude(12f);
newLocation.setLongitude(13f);
newLocation.setAccuracy(1f);
return newLocation;
}
} }