TriggerWifiSsid, UI tweaking

This commit is contained in:
Milos Kozak 2019-04-22 22:47:59 +02:00
parent 1840ddabb3
commit a678c6509c
16 changed files with 363 additions and 25 deletions

View file

@ -13,7 +13,7 @@ import info.nightscout.androidaps.data.PumpEnactResult;
import info.nightscout.androidaps.events.EventRefreshOverview; import info.nightscout.androidaps.events.EventRefreshOverview;
import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin; import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin;
import info.nightscout.androidaps.plugins.general.automation.elements.InputDuration; import info.nightscout.androidaps.plugins.general.automation.elements.InputDuration;
import info.nightscout.androidaps.plugins.general.automation.elements.Label; 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.queue.Callback; import info.nightscout.androidaps.queue.Callback;
import info.nightscout.androidaps.utils.JsonHelper; import info.nightscout.androidaps.utils.JsonHelper;
@ -83,7 +83,7 @@ public class ActionLoopSuspend extends Action {
public void generateDialog(LinearLayout root) { public void generateDialog(LinearLayout root) {
new LayoutBuilder() new LayoutBuilder()
.add(new Label(MainApp.gs(R.string.careportal_newnstreatment_duration_min_label), "", minutes)) .add(new LabelWithElement(MainApp.gs(R.string.careportal_newnstreatment_duration_min_label), "", minutes))
.build(root); .build(root);
} }

View file

@ -12,7 +12,7 @@ import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.data.PumpEnactResult;
import info.nightscout.androidaps.events.EventRefreshOverview; import info.nightscout.androidaps.events.EventRefreshOverview;
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.Label; 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.nsclient.NSUpload; import info.nightscout.androidaps.plugins.general.nsclient.NSUpload;
import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification;
@ -83,7 +83,7 @@ public class ActionNotification extends Action {
public void generateDialog(LinearLayout root) { public void generateDialog(LinearLayout root) {
new LayoutBuilder() new LayoutBuilder()
.add(new Label(MainApp.gs(R.string.message_short), "", text)) .add(new LabelWithElement(MainApp.gs(R.string.message_short), "", text))
.build(root); .build(root);
} }

View file

@ -13,7 +13,7 @@ import info.nightscout.androidaps.data.PumpEnactResult;
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
import info.nightscout.androidaps.plugins.general.automation.elements.InputDuration; import info.nightscout.androidaps.plugins.general.automation.elements.InputDuration;
import info.nightscout.androidaps.plugins.general.automation.elements.InputPercent; import info.nightscout.androidaps.plugins.general.automation.elements.InputPercent;
import info.nightscout.androidaps.plugins.general.automation.elements.Label; 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.queue.Callback; import info.nightscout.androidaps.queue.Callback;
import info.nightscout.androidaps.utils.JsonHelper; import info.nightscout.androidaps.utils.JsonHelper;
@ -42,8 +42,8 @@ public class ActionProfileSwitchPercent extends Action {
@Override @Override
public void generateDialog(LinearLayout root) { public void generateDialog(LinearLayout root) {
new LayoutBuilder() new LayoutBuilder()
.add(new Label(MainApp.gs(R.string.percent_u), "", pct)) .add(new LabelWithElement(MainApp.gs(R.string.percent_u), "", pct))
.add(new Label(MainApp.gs(R.string.careportal_newnstreatment_duration_min_label), "", duration)) .add(new LabelWithElement(MainApp.gs(R.string.careportal_newnstreatment_duration_min_label), "", duration))
.build(root); .build(root);
} }

View file

@ -16,7 +16,7 @@ import info.nightscout.androidaps.db.Source;
import info.nightscout.androidaps.db.TempTarget; import info.nightscout.androidaps.db.TempTarget;
import info.nightscout.androidaps.plugins.general.automation.elements.InputBg; import info.nightscout.androidaps.plugins.general.automation.elements.InputBg;
import info.nightscout.androidaps.plugins.general.automation.elements.InputDuration; import info.nightscout.androidaps.plugins.general.automation.elements.InputDuration;
import info.nightscout.androidaps.plugins.general.automation.elements.Label; 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.treatments.TreatmentsPlugin; import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin;
import info.nightscout.androidaps.queue.Callback; import info.nightscout.androidaps.queue.Callback;
@ -65,8 +65,8 @@ public class ActionStartTempTarget extends Action {
int unitResId = value.getUnits().equals(Constants.MGDL) ? R.string.mgdl : R.string.mmol; int unitResId = value.getUnits().equals(Constants.MGDL) ? R.string.mgdl : R.string.mmol;
new LayoutBuilder() new LayoutBuilder()
.add(new Label(MainApp.gs(R.string.careportal_temporarytarget), MainApp.gs(unitResId), value)) .add(new LabelWithElement(MainApp.gs(R.string.careportal_temporarytarget), MainApp.gs(unitResId), value))
.add(new Label(MainApp.gs(R.string.careportal_newnstreatment_duration_min_label), "", duration)) .add(new LabelWithElement(MainApp.gs(R.string.careportal_newnstreatment_duration_min_label), "", duration))
.build(root); .build(root);
} }

View file

@ -24,6 +24,7 @@ import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerPro
import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerRecurringTime; import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerRecurringTime;
import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerTempTarget; import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerTempTarget;
import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerTime; import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerTime;
import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerWifiSsid;
public class ChooseTriggerDialog extends DialogFragment { public class ChooseTriggerDialog extends DialogFragment {
@ -38,6 +39,7 @@ public class ChooseTriggerDialog extends DialogFragment {
add(new TriggerIob()); add(new TriggerIob());
add(new TriggerProfilePercent()); add(new TriggerProfilePercent());
add(new TriggerTempTarget()); add(new TriggerTempTarget());
add(new TriggerWifiSsid());
}}; }};
private Unbinder mUnbinder; private Unbinder mUnbinder;

View file

@ -7,12 +7,12 @@ import android.widget.TextView;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
public class Label extends Element { public class LabelWithElement extends Element {
final Element element; final Element element;
final String textPre; final String textPre;
final String textPost; final String textPost;
public Label(String textPre, String textPost, Element element) { public LabelWithElement(String textPre, String textPost, Element element) {
this.element = element; this.element = element;
this.textPre = textPre; this.textPre = textPre;
this.textPost = textPost; this.textPost = textPost;

View file

@ -0,0 +1,36 @@
package info.nightscout.androidaps.plugins.general.automation.elements;
import android.graphics.Typeface;
import android.widget.LinearLayout;
import android.widget.TextView;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
public class StaticLabel extends Element {
String label;
public StaticLabel(String label) {
super();
this.label = label;
}
public StaticLabel(int resourceId) {
super();
this.label = MainApp.gs(resourceId);
}
@Override
public void addToLayout(LinearLayout root) {
// text view pre element
int px = MainApp.dpToPx(10);
TextView textView = new TextView(root.getContext());
textView.setText(label);
// textViewPre.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
textView.setPadding(px, px, px, px);
textView.setTypeface(textView.getTypeface(), Typeface.BOLD);
textView.setBackgroundColor(MainApp.gc(R.color.mdtp_line_dark));
// add element to layout
root.addView(textView);
}
}

View file

@ -17,8 +17,9 @@ import info.nightscout.androidaps.data.Profile;
import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.general.automation.elements.Comparator; import info.nightscout.androidaps.plugins.general.automation.elements.Comparator;
import info.nightscout.androidaps.plugins.general.automation.elements.InputBg; import info.nightscout.androidaps.plugins.general.automation.elements.InputBg;
import info.nightscout.androidaps.plugins.general.automation.elements.Label; 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.iob.iobCobCalculator.GlucoseStatus; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus;
import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.JsonHelper; import info.nightscout.androidaps.utils.JsonHelper;
@ -159,8 +160,9 @@ public class TriggerBg extends Trigger {
@Override @Override
public void generateDialog(LinearLayout root, FragmentManager fragmentManager) { public void generateDialog(LinearLayout root, FragmentManager fragmentManager) {
new LayoutBuilder() new LayoutBuilder()
.add(new StaticLabel(R.string.glucose))
.add(comparator) .add(comparator)
.add(new Label(MainApp.gs(R.string.glucose_u, bg.getUnits()), "", bg)) .add(new LabelWithElement(MainApp.gs(R.string.glucose_u, bg.getUnits()), "", bg))
.build(root); .build(root);
} }
} }

View file

@ -18,8 +18,9 @@ import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
import info.nightscout.androidaps.plugins.general.automation.elements.Comparator; import info.nightscout.androidaps.plugins.general.automation.elements.Comparator;
import info.nightscout.androidaps.plugins.general.automation.elements.InputInsulin; import info.nightscout.androidaps.plugins.general.automation.elements.InputInsulin;
import info.nightscout.androidaps.plugins.general.automation.elements.Label; 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.iob.iobCobCalculator.IobCobCalculatorPlugin; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin;
import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.JsonHelper; import info.nightscout.androidaps.utils.JsonHelper;
@ -136,8 +137,9 @@ public class TriggerIob extends Trigger {
@Override @Override
public void generateDialog(LinearLayout root, FragmentManager fragmentManager) { public void generateDialog(LinearLayout root, FragmentManager fragmentManager) {
new LayoutBuilder() new LayoutBuilder()
.add(new StaticLabel(R.string.iob))
.add(comparator) .add(comparator)
.add(new Label(MainApp.gs(R.string.iob_u), "", insulin)) .add(new LabelWithElement(MainApp.gs(R.string.iob_u), "", insulin))
.build(root); .build(root);
} }
} }

View file

@ -17,8 +17,9 @@ import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
import info.nightscout.androidaps.plugins.general.automation.elements.Comparator; import info.nightscout.androidaps.plugins.general.automation.elements.Comparator;
import info.nightscout.androidaps.plugins.general.automation.elements.InputPercent; import info.nightscout.androidaps.plugins.general.automation.elements.InputPercent;
import info.nightscout.androidaps.plugins.general.automation.elements.Label; 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.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;
@ -142,8 +143,9 @@ public class TriggerProfilePercent extends Trigger {
@Override @Override
public void generateDialog(LinearLayout root, FragmentManager fragmentManager) { public void generateDialog(LinearLayout root, FragmentManager fragmentManager) {
new LayoutBuilder() new LayoutBuilder()
.add(new StaticLabel(R.string.profilepercentage))
.add(comparator) .add(comparator)
.add(new Label(MainApp.gs(R.string.percent_u), "", pct)) .add(new LabelWithElement(MainApp.gs(R.string.percent_u), "", pct))
.build(root); .build(root);
} }
} }

View file

@ -14,11 +14,9 @@ import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.db.TempTarget; import info.nightscout.androidaps.db.TempTarget;
import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.general.automation.elements.Comparator;
import info.nightscout.androidaps.plugins.general.automation.elements.ComparatorExists; import info.nightscout.androidaps.plugins.general.automation.elements.ComparatorExists;
import info.nightscout.androidaps.plugins.general.automation.elements.InputInsulin;
import info.nightscout.androidaps.plugins.general.automation.elements.Label;
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.treatments.TreatmentsPlugin; import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin;
import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.JsonHelper; import info.nightscout.androidaps.utils.JsonHelper;
@ -125,6 +123,7 @@ public class TriggerTempTarget extends Trigger {
@Override @Override
public void generateDialog(LinearLayout root, FragmentManager fragmentManager) { public void generateDialog(LinearLayout root, FragmentManager fragmentManager) {
new LayoutBuilder() new LayoutBuilder()
.add(new StaticLabel(R.string.temptarget))
.add(comparator) .add(comparator)
.build(root); .build(root);
} }

View file

@ -0,0 +1,148 @@
package info.nightscout.androidaps.plugins.general.automation.triggers;
import android.support.v4.app.FragmentManager;
import android.widget.LinearLayout;
import com.google.common.base.Optional;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.events.EventNetworkChange;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.general.automation.AutomationPlugin;
import info.nightscout.androidaps.plugins.general.automation.elements.Comparator;
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.utils.DateUtil;
import info.nightscout.androidaps.utils.JsonHelper;
import info.nightscout.androidaps.utils.T;
public class TriggerWifiSsid extends Trigger {
private static Logger log = LoggerFactory.getLogger(L.AUTOMATION);
private InputString ssid = new InputString();
private Comparator comparator = new Comparator();
public TriggerWifiSsid() {
super();
}
private TriggerWifiSsid(TriggerWifiSsid triggerWifiSsid) {
super();
ssid = new InputString(triggerWifiSsid.ssid);
comparator = new Comparator(triggerWifiSsid.comparator);
lastRun = triggerWifiSsid.lastRun;
}
public String getValue() {
return ssid.getValue();
}
public Comparator getComparator() {
return comparator;
}
@Override
public synchronized boolean shouldRun() {
EventNetworkChange eventNetworkChange = AutomationPlugin.getPlugin().getEventNetworkChange();
if (eventNetworkChange == null)
return false;
if (lastRun > DateUtil.now() - T.mins(5).msecs())
return false;
if (!eventNetworkChange.wifiConnected && comparator.getValue() == Comparator.Compare.IS_NOT_AVAILABLE) {
if (L.isEnabled(L.AUTOMATION))
log.debug("Ready for execution: " + friendlyDescription());
return true;
}
boolean doRun = eventNetworkChange.wifiConnected && comparator.getValue().check(eventNetworkChange.ssid, getValue());
if (doRun) {
if (L.isEnabled(L.AUTOMATION))
log.debug("Ready for execution: " + friendlyDescription());
return true;
}
return false;
}
@Override
public synchronized String toJSON() {
JSONObject o = new JSONObject();
try {
o.put("type", TriggerWifiSsid.class.getName());
JSONObject data = new JSONObject();
data.put("ssid", getValue());
data.put("lastRun", lastRun);
data.put("comparator", comparator.getValue().toString());
o.put("data", data);
} catch (JSONException e) {
e.printStackTrace();
}
return o.toString();
}
@Override
Trigger fromJSON(String data) {
try {
JSONObject d = new JSONObject(data);
ssid.setValue(JsonHelper.safeGetString(d, "ssid"));
lastRun = JsonHelper.safeGetLong(d, "lastRun");
comparator.setValue(Comparator.Compare.valueOf(JsonHelper.safeGetString(d, "comparator")));
} catch (Exception e) {
e.printStackTrace();
}
return this;
}
@Override
public int friendlyName() {
return R.string.ns_wifi_ssids;
}
@Override
public String friendlyDescription() {
return MainApp.gs(R.string.wifissidcompared, MainApp.gs(comparator.getValue().getStringRes()), getValue());
}
@Override
public Optional<Integer> icon() {
return Optional.of(R.drawable.remove); // TODO icon
}
@Override
public Trigger duplicate() {
return new TriggerWifiSsid(this);
}
TriggerWifiSsid setValue(String value) {
ssid.setValue(value);
return this;
}
TriggerWifiSsid lastRun(long lastRun) {
this.lastRun = lastRun;
return this;
}
TriggerWifiSsid comparator(Comparator.Compare compare) {
this.comparator = new Comparator().setValue(compare);
return this;
}
@Override
public void generateDialog(LinearLayout root, FragmentManager fragmentManager) {
new LayoutBuilder()
.add(new StaticLabel(R.string.ns_wifi_ssids))
.add(comparator)
.add(new LabelWithElement(MainApp.gs(R.string.ns_wifi_ssids) + ": ", "", ssid))
.build(root);
}
}

View file

@ -1399,6 +1399,7 @@
<string name="exists">exists</string> <string name="exists">exists</string>
<string name="notexists">not exists</string> <string name="notexists">not exists</string>
<string name="temptargetcompared">Temp target %1$s</string> <string name="temptargetcompared">Temp target %1$s</string>
<string name="wifissidcompared">WiFi SSID %1$s %2$s</string>
<plurals name="objective_days"> <plurals name="objective_days">
<item quantity="one">%1$d day</item> <item quantity="one">%1$d day</item>

View file

@ -5,14 +5,12 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.modules.junit4.PowerMockRunner;
import static org.junit.Assert.*;
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
public class LabelTest { public class LabelWithElementTest {
@Test @Test
public void constructorTest() { public void constructorTest() {
Label l = new Label("A", "B", new InputInsulin()); LabelWithElement l = new LabelWithElement("A", "B", new InputInsulin());
Assert.assertEquals("A", l.textPre); Assert.assertEquals("A", l.textPre);
Assert.assertEquals("B", l.textPost); Assert.assertEquals("B", l.textPost);
Assert.assertEquals(InputInsulin.class, l.element.getClass()); Assert.assertEquals(InputInsulin.class, l.element.getClass());

View file

@ -0,0 +1,32 @@
package info.nightscout.androidaps.plugins.general.automation.elements;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
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;
@RunWith(PowerMockRunner.class)
@PrepareForTest({MainApp.class})
public class StaticLabelTest {
@Test
public void constructor() {
StaticLabel sl = new StaticLabel("any");
Assert.assertEquals("any", sl.label);
sl = new StaticLabel(R.string.pumplimit);
Assert.assertEquals(MainApp.gs(R.string.pumplimit), sl.label);
}
@Before
public void prepareTest() {
AAPSMocker.mockMainApp();
AAPSMocker.mockStrings();
}
}

View file

@ -0,0 +1,116 @@
package info.nightscout.androidaps.plugins.general.automation.triggers;
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.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.EventNetworkChange;
import info.nightscout.androidaps.plugins.general.automation.AutomationPlugin;
import info.nightscout.androidaps.plugins.general.automation.elements.Comparator;
import info.nightscout.androidaps.utils.DateUtil;
import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(PowerMockRunner.class)
@PrepareForTest({MainApp.class, Bus.class, AutomationPlugin.class, DateUtil.class})
public class TriggerWifiSsidTest {
AutomationPlugin automationPlugin;
long now = 1514766900000L;
@Test
public void shouldRunTest() {
EventNetworkChange e = new EventNetworkChange();
when(automationPlugin.getEventNetworkChange()).thenReturn(e);
TriggerWifiSsid t = new TriggerWifiSsid().setValue("aSSID").comparator(Comparator.Compare.IS_EQUAL);
e.wifiConnected = false;
Assert.assertFalse(t.shouldRun());
e.wifiConnected = true;
e.ssid = "otherSSID";
Assert.assertFalse(t.shouldRun());
e.wifiConnected = true;
e.ssid = "aSSID";
Assert.assertTrue(t.shouldRun());
t.lastRun(now - 1);
Assert.assertFalse(t.shouldRun());
t = new TriggerWifiSsid().setValue("aSSID").comparator(Comparator.Compare.IS_NOT_AVAILABLE);
e.wifiConnected = false;
Assert.assertTrue(t.shouldRun());
// no network data
when(automationPlugin.getEventNetworkChange()).thenReturn(null);
Assert.assertFalse(t.shouldRun());
}
@Test
public void copyConstructorTest() {
TriggerWifiSsid t = new TriggerWifiSsid().setValue("aSSID").comparator(Comparator.Compare.IS_EQUAL_OR_LESSER);
TriggerWifiSsid t1 = (TriggerWifiSsid) t.duplicate();
Assert.assertEquals("aSSID", t1.getValue());
Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.getComparator().getValue());
}
String json = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"lastRun\":0,\"ssid\":\"aSSID\"},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerWifiSsid\"}";
@Test
public void toJSONTest() {
TriggerWifiSsid t = new TriggerWifiSsid().setValue("aSSID").comparator(Comparator.Compare.IS_EQUAL);
Assert.assertEquals(json, t.toJSON());
}
@Test
public void fromJSONTest() throws JSONException {
TriggerWifiSsid t = new TriggerWifiSsid().setValue("aSSID").comparator(Comparator.Compare.IS_EQUAL);
TriggerWifiSsid t2 = (TriggerWifiSsid) Trigger.instantiate(new JSONObject(t.toJSON()));
Assert.assertEquals(Comparator.Compare.IS_EQUAL, t2.getComparator().getValue());
Assert.assertEquals("aSSID", t2.getValue());
}
@Test
public void iconTest() {
Assert.assertEquals(Optional.of(R.drawable.remove), new TriggerWifiSsid().icon());
}
@Test
public void friendlyNameTest() {
Assert.assertEquals(R.string.ns_wifi_ssids, new TriggerWifiSsid().friendlyName());
}
@Test
public void friendlyDescriptionTest() {
Assert.assertEquals(null, new TriggerWifiSsid().friendlyDescription()); //not mocked
}
@Before
public void mock() {
AAPSMocker.mockMainApp();
AAPSMocker.mockBus();
PowerMockito.mockStatic(AutomationPlugin.class);
automationPlugin = PowerMockito.mock(AutomationPlugin.class);
when(AutomationPlugin.getPlugin()).thenReturn(automationPlugin);
PowerMockito.mockStatic(DateUtil.class);
when(DateUtil.now()).thenReturn(now);
}
}