fix tests, use location form service

This commit is contained in:
Milos Kozak 2019-07-22 16:58:25 +02:00
parent 77d7f1ca65
commit e539dbc112
4 changed files with 31 additions and 41 deletions

View file

@ -46,8 +46,6 @@ object AutomationPlugin : PluginBase(PluginDescription()
private const val key_AUTOMATION_EVENTS = "AUTOMATION_EVENTS"
val automationEvents = ArrayList<AutomationEvent>()
var eventLocationChange: EventLocationChange? = null
private set
var executionLog: MutableList<String> = ArrayList()
private val loopHandler = Handler()
@ -91,10 +89,10 @@ object AutomationPlugin : PluginBase(PluginDescription()
.toObservable(EventLocationChange::class.java)
.observeOn(Schedulers.io())
.subscribe({ e ->
eventLocationChange = e
if (e != null)
log.debug("Grabbed location: $e.location.latitude $e.location.longitude Provider: $e.location.provider")
processActions()
e?.let {
log.debug("Grabbed location: $it.location.latitude $it.location.longitude Provider: $it.location.provider")
processActions()
}
}, {})
disposable += RxBus
.toObservable(EventChargingState::class.java)

View file

@ -16,15 +16,14 @@ import java.text.DecimalFormat;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.events.EventLocationChange;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.general.automation.AutomationPlugin;
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.InputString;
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.StaticLabel;
import info.nightscout.androidaps.services.LocationService;
import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.JsonHelper;
import info.nightscout.androidaps.utils.T;
@ -37,17 +36,15 @@ public class TriggerLocation extends Trigger {
InputDouble distance = new InputDouble(200d, 0, 100000, 10d, new DecimalFormat("0"));
InputString name = new InputString();
Runnable buttonAction = () -> {
EventLocationChange event = AutomationPlugin.INSTANCE.getEventLocationChange();
if (event != null) {
latitude.setValue(event.location.getLatitude());
longitude.setValue(event.location.getLongitude());
private Runnable buttonAction = () -> {
Location location = LocationService.getLastLocation();
if (location != null) {
latitude.setValue(location.getLatitude());
longitude.setValue(location.getLongitude());
log.debug(String.format("Grabbed location: %f %f", latitude.getValue(), longitude.getValue()));
}
};
private InputButton button = new InputButton(MainApp.gs(R.string.currentlocation), buttonAction);
public TriggerLocation() {
super();
}
@ -62,8 +59,8 @@ public class TriggerLocation extends Trigger {
@Override
public synchronized boolean shouldRun() {
EventLocationChange eventLocationChange = AutomationPlugin.INSTANCE.getEventLocationChange();
if (eventLocationChange == null)
Location location = LocationService.getLastLocation();
if (location == null)
return false;
if (lastRun > DateUtil.now() - T.mins(5).msecs())
@ -72,7 +69,7 @@ public class TriggerLocation extends Trigger {
Location a = new Location("Trigger");
a.setLatitude(latitude.getValue());
a.setLongitude(longitude.getValue());
double calculatedDistance = eventLocationChange.location.distanceTo(a);
double calculatedDistance = location.distanceTo(a);
if (calculatedDistance < distance.getValue()) {
if (L.isEnabled(L.AUTOMATION))
@ -164,7 +161,7 @@ public class TriggerLocation extends Trigger {
.add(new LabelWithElement(MainApp.gs(R.string.latitude_short), "", latitude))
.add(new LabelWithElement(MainApp.gs(R.string.longitude_short), "", longitude))
.add(new LabelWithElement(MainApp.gs(R.string.distance_short), "", distance))
.add(new InputButton(MainApp.gs(R.string.currentlocation), buttonAction), AutomationPlugin.INSTANCE.getEventLocationChange() != null)
.add(new InputButton(MainApp.gs(R.string.currentlocation), buttonAction), LocationService.getLastLocation() != null)
.build(root);
}
}

View file

@ -8,15 +8,16 @@ import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import androidx.core.app.ActivityCompat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.events.EventLocationChange;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.bus.RxBus;
import info.nightscout.androidaps.utils.SP;
import info.nightscout.androidaps.utils.T;
@ -29,12 +30,9 @@ public class LocationService extends Service {
private static final long LOCATION_INTERVAL_ACTIVE = T.mins(5).msecs();
private static final long LOCATION_INTERVAL_PASSIVE = T.mins(1).msecs(); // this doesn't cost more power
public LocationService() {
MainApp.bus().register(this);
}
private static Location mLastLocation;
private class LocationListener implements android.location.LocationListener {
Location mLastLocation;
LocationListener(String provider) {
if (L.isEnabled(L.LOCATION))
@ -47,7 +45,7 @@ public class LocationService extends Service {
if (L.isEnabled(L.LOCATION))
log.debug("onLocationChanged: " + location);
mLastLocation.set(location);
MainApp.bus().post(new EventLocationChange(location));
RxBus.INSTANCE.send(new EventLocationChange(location));
}
@Override
@ -126,7 +124,6 @@ public class LocationService extends Service {
if (L.isEnabled(L.LOCATION))
log.debug("onDestroy");
super.onDestroy();
MainApp.bus().unregister(this);
if (mLocationManager != null) {
try {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
@ -146,4 +143,8 @@ public class LocationService extends Service {
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
}
}
public static Location getLastLocation() {
return mLastLocation;
}
}

View file

@ -11,7 +11,6 @@ import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
@ -20,15 +19,14 @@ import org.powermock.modules.junit4.PowerMockRunner;
import info.AAPSMocker;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.events.EventLocationChange;
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
import info.nightscout.androidaps.plugins.general.automation.AutomationPlugin;
import info.nightscout.androidaps.services.LocationService;
import info.nightscout.androidaps.utils.DateUtil;
import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(PowerMockRunner.class)
@PrepareForTest({MainApp.class, Bus.class, ProfileFunctions.class, DateUtil.class, AutomationPlugin.class})
@PrepareForTest({MainApp.class, Bus.class, ProfileFunctions.class, DateUtil.class, LocationService.class})
public class TriggerLocationTest {
@ -41,12 +39,9 @@ public class TriggerLocationTest {
AAPSMocker.mockApplicationContext();
PowerMockito.mockStatic(DateUtil.class);
PowerMockito.mockStatic(AutomationPlugin.class);
AutomationPlugin plugin = Mockito.mock(AutomationPlugin.class);
PowerMockito.when(AutomationPlugin.getPlugin()).thenReturn(plugin);
PowerMockito.mockStatic(LocationService.class);
when(DateUtil.now()).thenReturn(now);
PowerMockito.when(AutomationPlugin.getPlugin().getEventLocationChange()).thenReturn(new EventLocationChange(mockedLocation()));
PowerMockito.when(LocationService.getLastLocation()).thenReturn(mockedLocation());
MockitoAnnotations.initMocks(this);
@ -73,11 +68,11 @@ public class TriggerLocationTest {
t.latitude.setValue(213);
t.longitude.setValue(212);
t.distance.setValue(2);
PowerMockito.when(AutomationPlugin.getPlugin().getEventLocationChange()).thenReturn(null);
PowerMockito.when(LocationService.getLastLocation()).thenReturn(null);
Assert.assertFalse(t.shouldRun());
PowerMockito.when(AutomationPlugin.getPlugin().getEventLocationChange()).thenReturn(new EventLocationChange(mockedLocation()));
PowerMockito.when(LocationService.getLastLocation()).thenReturn(mockedLocation());
Assert.assertTrue(t.shouldRun());
t.lastRun(now-1);
t.lastRun(now - 1);
Assert.assertFalse(t.shouldRun());
t = new TriggerLocation();
@ -97,7 +92,7 @@ public class TriggerLocationTest {
}
@Test
public void fromJSONTest() throws JSONException {
public void fromJSONTest() throws JSONException {
TriggerLocation t = new TriggerLocation();
t.latitude.setValue(213);
t.longitude.setValue(212);
@ -153,8 +148,7 @@ public class TriggerLocationTest {
Assert.assertEquals(t.lastRun, 1514766900000L, 0d);
}
public Location mockedLocation(){
public Location mockedLocation() {
Location newLocation = new Location("test");
newLocation.setLatitude(10);
newLocation.setLongitude(11);