Merge branch 'automation' of https://github.com/MilosKozak/AndroidAPS into automation

This commit is contained in:
Milos Kozak 2019-07-22 16:28:57 +02:00
commit 77d7f1ca65
3 changed files with 199 additions and 3 deletions

View file

@ -0,0 +1,164 @@
package info.nightscout.androidaps.plugins.general.automation.triggers;
import android.location.Location;
import com.google.common.base.Optional;
import com.squareup.otto.Bus;
import org.json.JSONException;
import org.json.JSONObject;
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;
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.utils.DateUtil;
import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(PowerMockRunner.class)
@PrepareForTest({MainApp.class, Bus.class, ProfileFunctions.class, DateUtil.class, AutomationPlugin.class})
public class TriggerLocationTest {
long now = 1514766900000L;
@Before
public void mock() {
AAPSMocker.mockMainApp();
AAPSMocker.mockBus();
AAPSMocker.mockApplicationContext();
PowerMockito.mockStatic(DateUtil.class);
PowerMockito.mockStatic(AutomationPlugin.class);
AutomationPlugin plugin = Mockito.mock(AutomationPlugin.class);
PowerMockito.when(AutomationPlugin.getPlugin()).thenReturn(plugin);
when(DateUtil.now()).thenReturn(now);
PowerMockito.when(AutomationPlugin.getPlugin().getEventLocationChange()).thenReturn(new EventLocationChange(mockedLocation()));
MockitoAnnotations.initMocks(this);
}
@Test
public void copyConstructorTest() {
TriggerLocation t = new TriggerLocation();
t.latitude.setValue(213);
t.longitude.setValue(212);
t.distance.setValue(2);
TriggerLocation t1 = (TriggerLocation) t.duplicate();
Assert.assertEquals(213d, t.latitude.getValue(), 0.01d);
Assert.assertEquals(212d, t.longitude.getValue(), 0.01d);
Assert.assertEquals(2d, t.distance.getValue(), 0.01d);
}
@Test
public void shouldRunTest() {
TriggerLocation t = new TriggerLocation();
t.latitude.setValue(213);
t.longitude.setValue(212);
t.distance.setValue(2);
PowerMockito.when(AutomationPlugin.getPlugin().getEventLocationChange()).thenReturn(null);
Assert.assertFalse(t.shouldRun());
PowerMockito.when(AutomationPlugin.getPlugin().getEventLocationChange()).thenReturn(new EventLocationChange(mockedLocation()));
Assert.assertTrue(t.shouldRun());
t.lastRun(now-1);
Assert.assertFalse(t.shouldRun());
t = new TriggerLocation();
t.distance.setValue(-500);
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\"}";
@Test
public void toJSONTest() {
TriggerLocation t = new TriggerLocation();
t.latitude.setValue(213);
t.longitude.setValue(212);
t.distance.setValue(2);
Assert.assertEquals(locationJson, t.toJSON());
}
@Test
public void fromJSONTest() throws JSONException {
TriggerLocation t = new TriggerLocation();
t.latitude.setValue(213);
t.longitude.setValue(212);
t.distance.setValue(2);
TriggerLocation t2 = (TriggerLocation) Trigger.instantiate(new JSONObject(t.toJSON()));
Assert.assertEquals(t.latitude.getValue(), t2.latitude.getValue(), 0.01d);
Assert.assertEquals(t.longitude.getValue(), t2.longitude.getValue(), 0.01d);
Assert.assertEquals(t.distance.getValue(), t2.distance.getValue(), 0.01d);
}
@Test
public void friendlyNameTest() {
Assert.assertEquals(R.string.location, new TriggerLocation().friendlyName());
}
@Test
public void friendlyDescriptionTest() {
Assert.assertEquals(null, new TriggerLocation().friendlyDescription()); //not mocked }
}
@Test
public void iconTest() {
Assert.assertEquals(Optional.of(R.drawable.ic_location_on), new TriggerLocation().icon());
}
@Test
public void setLatitudeTest() {
TriggerLocation t = new TriggerLocation();
t.setLatitude(212);
Assert.assertEquals(t.latitude.getValue(), 212, 0d);
}
@Test
public void setLongitudeTest() {
TriggerLocation t = new TriggerLocation();
t.setLongitude(213);
Assert.assertEquals(t.longitude.getValue(), 213, 0d);
}
@Test
public void setdistanceTest() {
TriggerLocation t = new TriggerLocation();
t.setdistance(2);
Assert.assertEquals(t.distance.getValue(), 2, 0d);
}
@Test
public void lastRunTest() {
TriggerLocation t = new TriggerLocation();
t.lastRun(now);
Assert.assertEquals(t.lastRun, 1514766900000L, 0d);
}
public Location mockedLocation(){
Location newLocation = new Location("test");
newLocation.setLatitude(10);
newLocation.setLongitude(11);
newLocation.setAccuracy(1f);
return newLocation;
}
}

View file

@ -47,15 +47,20 @@ public class TriggerRecurringTimeTest {
}
String timeJson = "{\"data\":{\"runAt\":1514766840000,\"THURSDAY\":false,\"lastRun\":0,\"SUNDAY\":false,\"recurring\":false,\"TUESDAY\":false,\"FRIDAY\":false,\"minute\":0,\"WEDNESDAY\":false,\"MONDAY\":false,\"hour\":0,\"SATURDAY\":false,\"validTo\":0},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerRecurringTime\"}";
String timeJson = "{\"data\":{\"WEDNESDAY\":false,\"MONDAY\":false,\"THURSDAY\":false,\"lastRun\":1514766840000,\"SUNDAY\":false,\"hour\":0,\"TUESDAY\":false,\"FRIDAY\":false,\"SATURDAY\":false,\"minute\":0,\"validTo\":0},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerRecurringTime\"}";
@Test
public void toJSONTest() {
public void toJSONTest() throws JSONException {
TriggerRecurringTime t = new TriggerRecurringTime().lastRun(now - T.mins(1).msecs());
Assert.assertEquals(timeJson, t.toJSON());
}
@Test
public void fromJSONTest() throws JSONException {
}
TriggerRecurringTime t = new TriggerRecurringTime().lastRun(now - T.mins(1).msecs());
TriggerRecurringTime t2 = (TriggerRecurringTime) Trigger.instantiate(new JSONObject(t.toJSON()));
Assert.assertEquals(now - T.mins(1).msecs(), t2.lastRun); }
@Before
public void mock() {

View file

@ -1,5 +1,6 @@
package info.nightscout.androidaps.plugins.general.automation.triggers;
import com.google.common.base.Optional;
import com.squareup.otto.Bus;
import org.json.JSONException;
@ -16,6 +17,7 @@ import java.util.GregorianCalendar;
import info.AAPSMocker;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.T;
@ -60,6 +62,31 @@ public class TriggerTimeTest {
Assert.assertEquals(now - T.mins(1).msecs(), t2.getRunAt());
}
@Test
public void copyConstructorTest() {
TriggerTime t = new TriggerTime();
t.runAt(now);
TriggerTime t1 = (TriggerTime) t.duplicate();
Assert.assertEquals(now, t1.getRunAt(), 0.01d);
}
@Test
public void friendlyNameTest() {
Assert.assertEquals(R.string.time, new TriggerTime().friendlyName());
}
@Test
public void friendlyDescriptionTest() {
Assert.assertEquals(null, new TriggerTime().friendlyDescription()); //not mocked }
}
@Test
public void iconTest() {
Assert.assertEquals(Optional.of(R.drawable.ic_access_alarm_24dp), new TriggerTime().icon());
}
@Before
public void mock() {
AAPSMocker.mockMainApp();