fix tests, use location form service
This commit is contained in:
parent
77d7f1ca65
commit
e539dbc112
|
@ -46,8 +46,6 @@ object AutomationPlugin : PluginBase(PluginDescription()
|
||||||
private const val key_AUTOMATION_EVENTS = "AUTOMATION_EVENTS"
|
private const val key_AUTOMATION_EVENTS = "AUTOMATION_EVENTS"
|
||||||
|
|
||||||
val automationEvents = ArrayList<AutomationEvent>()
|
val automationEvents = ArrayList<AutomationEvent>()
|
||||||
var eventLocationChange: EventLocationChange? = null
|
|
||||||
private set
|
|
||||||
var executionLog: MutableList<String> = ArrayList()
|
var executionLog: MutableList<String> = ArrayList()
|
||||||
|
|
||||||
private val loopHandler = Handler()
|
private val loopHandler = Handler()
|
||||||
|
@ -91,10 +89,10 @@ object AutomationPlugin : PluginBase(PluginDescription()
|
||||||
.toObservable(EventLocationChange::class.java)
|
.toObservable(EventLocationChange::class.java)
|
||||||
.observeOn(Schedulers.io())
|
.observeOn(Schedulers.io())
|
||||||
.subscribe({ e ->
|
.subscribe({ e ->
|
||||||
eventLocationChange = e
|
e?.let {
|
||||||
if (e != null)
|
log.debug("Grabbed location: $it.location.latitude $it.location.longitude Provider: $it.location.provider")
|
||||||
log.debug("Grabbed location: $e.location.latitude $e.location.longitude Provider: $e.location.provider")
|
|
||||||
processActions()
|
processActions()
|
||||||
|
}
|
||||||
}, {})
|
}, {})
|
||||||
disposable += RxBus
|
disposable += RxBus
|
||||||
.toObservable(EventChargingState::class.java)
|
.toObservable(EventChargingState::class.java)
|
||||||
|
|
|
@ -16,15 +16,14 @@ import java.text.DecimalFormat;
|
||||||
|
|
||||||
import info.nightscout.androidaps.MainApp;
|
import info.nightscout.androidaps.MainApp;
|
||||||
import info.nightscout.androidaps.R;
|
import info.nightscout.androidaps.R;
|
||||||
import info.nightscout.androidaps.events.EventLocationChange;
|
|
||||||
import info.nightscout.androidaps.logging.L;
|
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.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.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;
|
||||||
import info.nightscout.androidaps.plugins.general.automation.elements.StaticLabel;
|
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.DateUtil;
|
||||||
import info.nightscout.androidaps.utils.JsonHelper;
|
import info.nightscout.androidaps.utils.JsonHelper;
|
||||||
import info.nightscout.androidaps.utils.T;
|
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"));
|
InputDouble distance = new InputDouble(200d, 0, 100000, 10d, new DecimalFormat("0"));
|
||||||
InputString name = new InputString();
|
InputString name = new InputString();
|
||||||
|
|
||||||
Runnable buttonAction = () -> {
|
private Runnable buttonAction = () -> {
|
||||||
EventLocationChange event = AutomationPlugin.INSTANCE.getEventLocationChange();
|
Location location = LocationService.getLastLocation();
|
||||||
if (event != null) {
|
if (location != null) {
|
||||||
latitude.setValue(event.location.getLatitude());
|
latitude.setValue(location.getLatitude());
|
||||||
longitude.setValue(event.location.getLongitude());
|
longitude.setValue(location.getLongitude());
|
||||||
log.debug(String.format("Grabbed location: %f %f", latitude.getValue(), longitude.getValue()));
|
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() {
|
public TriggerLocation() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
@ -62,8 +59,8 @@ public class TriggerLocation extends Trigger {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized boolean shouldRun() {
|
public synchronized boolean shouldRun() {
|
||||||
EventLocationChange eventLocationChange = AutomationPlugin.INSTANCE.getEventLocationChange();
|
Location location = LocationService.getLastLocation();
|
||||||
if (eventLocationChange == null)
|
if (location == null)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (lastRun > DateUtil.now() - T.mins(5).msecs())
|
if (lastRun > DateUtil.now() - T.mins(5).msecs())
|
||||||
|
@ -72,7 +69,7 @@ public class TriggerLocation extends Trigger {
|
||||||
Location a = new Location("Trigger");
|
Location a = new Location("Trigger");
|
||||||
a.setLatitude(latitude.getValue());
|
a.setLatitude(latitude.getValue());
|
||||||
a.setLongitude(longitude.getValue());
|
a.setLongitude(longitude.getValue());
|
||||||
double calculatedDistance = eventLocationChange.location.distanceTo(a);
|
double calculatedDistance = location.distanceTo(a);
|
||||||
|
|
||||||
if (calculatedDistance < distance.getValue()) {
|
if (calculatedDistance < distance.getValue()) {
|
||||||
if (L.isEnabled(L.AUTOMATION))
|
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.latitude_short), "", latitude))
|
||||||
.add(new LabelWithElement(MainApp.gs(R.string.longitude_short), "", longitude))
|
.add(new LabelWithElement(MainApp.gs(R.string.longitude_short), "", longitude))
|
||||||
.add(new LabelWithElement(MainApp.gs(R.string.distance_short), "", distance))
|
.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);
|
.build(root);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,15 +8,16 @@ import android.location.Location;
|
||||||
import android.location.LocationManager;
|
import android.location.LocationManager;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
|
|
||||||
import androidx.core.app.ActivityCompat;
|
import androidx.core.app.ActivityCompat;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import info.nightscout.androidaps.MainApp;
|
|
||||||
import info.nightscout.androidaps.R;
|
import info.nightscout.androidaps.R;
|
||||||
import info.nightscout.androidaps.events.EventLocationChange;
|
import info.nightscout.androidaps.events.EventLocationChange;
|
||||||
import info.nightscout.androidaps.logging.L;
|
import info.nightscout.androidaps.logging.L;
|
||||||
|
import info.nightscout.androidaps.plugins.bus.RxBus;
|
||||||
import info.nightscout.androidaps.utils.SP;
|
import info.nightscout.androidaps.utils.SP;
|
||||||
import info.nightscout.androidaps.utils.T;
|
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_ACTIVE = T.mins(5).msecs();
|
||||||
private static final long LOCATION_INTERVAL_PASSIVE = T.mins(1).msecs(); // this doesn't cost more power
|
private static final long LOCATION_INTERVAL_PASSIVE = T.mins(1).msecs(); // this doesn't cost more power
|
||||||
|
|
||||||
public LocationService() {
|
private static Location mLastLocation;
|
||||||
MainApp.bus().register(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
private class LocationListener implements android.location.LocationListener {
|
private class LocationListener implements android.location.LocationListener {
|
||||||
Location mLastLocation;
|
|
||||||
|
|
||||||
LocationListener(String provider) {
|
LocationListener(String provider) {
|
||||||
if (L.isEnabled(L.LOCATION))
|
if (L.isEnabled(L.LOCATION))
|
||||||
|
@ -47,7 +45,7 @@ public class LocationService extends Service {
|
||||||
if (L.isEnabled(L.LOCATION))
|
if (L.isEnabled(L.LOCATION))
|
||||||
log.debug("onLocationChanged: " + location);
|
log.debug("onLocationChanged: " + location);
|
||||||
mLastLocation.set(location);
|
mLastLocation.set(location);
|
||||||
MainApp.bus().post(new EventLocationChange(location));
|
RxBus.INSTANCE.send(new EventLocationChange(location));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -126,7 +124,6 @@ public class LocationService extends Service {
|
||||||
if (L.isEnabled(L.LOCATION))
|
if (L.isEnabled(L.LOCATION))
|
||||||
log.debug("onDestroy");
|
log.debug("onDestroy");
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
MainApp.bus().unregister(this);
|
|
||||||
if (mLocationManager != null) {
|
if (mLocationManager != null) {
|
||||||
try {
|
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) {
|
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);
|
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Location getLastLocation() {
|
||||||
|
return mLastLocation;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,6 @@ 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.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
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;
|
||||||
|
@ -20,15 +19,14 @@ import org.powermock.modules.junit4.PowerMockRunner;
|
||||||
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.events.EventLocationChange;
|
|
||||||
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
|
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 info.nightscout.androidaps.utils.DateUtil;
|
||||||
|
|
||||||
import static org.powermock.api.mockito.PowerMockito.when;
|
import static org.powermock.api.mockito.PowerMockito.when;
|
||||||
|
|
||||||
@RunWith(PowerMockRunner.class)
|
@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 {
|
public class TriggerLocationTest {
|
||||||
|
|
||||||
|
@ -41,12 +39,9 @@ public class TriggerLocationTest {
|
||||||
AAPSMocker.mockApplicationContext();
|
AAPSMocker.mockApplicationContext();
|
||||||
|
|
||||||
PowerMockito.mockStatic(DateUtil.class);
|
PowerMockito.mockStatic(DateUtil.class);
|
||||||
PowerMockito.mockStatic(AutomationPlugin.class);
|
PowerMockito.mockStatic(LocationService.class);
|
||||||
AutomationPlugin plugin = Mockito.mock(AutomationPlugin.class);
|
|
||||||
PowerMockito.when(AutomationPlugin.getPlugin()).thenReturn(plugin);
|
|
||||||
when(DateUtil.now()).thenReturn(now);
|
when(DateUtil.now()).thenReturn(now);
|
||||||
PowerMockito.when(AutomationPlugin.getPlugin().getEventLocationChange()).thenReturn(new EventLocationChange(mockedLocation()));
|
PowerMockito.when(LocationService.getLastLocation()).thenReturn(mockedLocation());
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
|
@ -73,11 +68,11 @@ 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);
|
||||||
PowerMockito.when(AutomationPlugin.getPlugin().getEventLocationChange()).thenReturn(null);
|
PowerMockito.when(LocationService.getLastLocation()).thenReturn(null);
|
||||||
Assert.assertFalse(t.shouldRun());
|
Assert.assertFalse(t.shouldRun());
|
||||||
PowerMockito.when(AutomationPlugin.getPlugin().getEventLocationChange()).thenReturn(new EventLocationChange(mockedLocation()));
|
PowerMockito.when(LocationService.getLastLocation()).thenReturn(mockedLocation());
|
||||||
Assert.assertTrue(t.shouldRun());
|
Assert.assertTrue(t.shouldRun());
|
||||||
t.lastRun(now-1);
|
t.lastRun(now - 1);
|
||||||
Assert.assertFalse(t.shouldRun());
|
Assert.assertFalse(t.shouldRun());
|
||||||
|
|
||||||
t = new TriggerLocation();
|
t = new TriggerLocation();
|
||||||
|
@ -153,8 +148,7 @@ public class TriggerLocationTest {
|
||||||
Assert.assertEquals(t.lastRun, 1514766900000L, 0d);
|
Assert.assertEquals(t.lastRun, 1514766900000L, 0d);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Location mockedLocation() {
|
||||||
public Location mockedLocation(){
|
|
||||||
Location newLocation = new Location("test");
|
Location newLocation = new Location("test");
|
||||||
newLocation.setLatitude(10);
|
newLocation.setLatitude(10);
|
||||||
newLocation.setLongitude(11);
|
newLocation.setLongitude(11);
|
||||||
|
|
Loading…
Reference in a new issue