From a678c6509c769b14090ca2ea46141fd90a5f6cf8 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 22 Apr 2019 22:47:59 +0200 Subject: [PATCH] TriggerWifiSsid, UI tweaking --- .../automation/actions/ActionLoopSuspend.java | 4 +- .../actions/ActionNotification.java | 4 +- .../actions/ActionProfileSwitchPercent.java | 6 +- .../actions/ActionStartTempTarget.java | 6 +- .../dialogs/ChooseTriggerDialog.java | 2 + .../{Label.java => LabelWithElement.java} | 4 +- .../automation/elements/StaticLabel.java | 36 +++++ .../automation/triggers/TriggerBg.java | 6 +- .../automation/triggers/TriggerIob.java | 6 +- .../triggers/TriggerProfilePercent.java | 6 +- .../triggers/TriggerTempTarget.java | 5 +- .../automation/triggers/TriggerWifiSsid.java | 148 ++++++++++++++++++ app/src/main/res/values/strings.xml | 1 + ...belTest.java => LabelWithElementTest.java} | 6 +- .../automation/elements/StaticLabelTest.java | 32 ++++ .../triggers/TriggerWifiSsidTest.java | 116 ++++++++++++++ 16 files changed, 363 insertions(+), 25 deletions(-) rename app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/{Label.java => LabelWithElement.java} (94%) create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/StaticLabel.java create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerWifiSsid.java rename app/src/test/java/info/nightscout/androidaps/plugins/general/automation/elements/{LabelTest.java => LabelWithElementTest.java} (79%) create mode 100644 app/src/test/java/info/nightscout/androidaps/plugins/general/automation/elements/StaticLabelTest.java create mode 100644 app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerWifiSsidTest.java diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionLoopSuspend.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionLoopSuspend.java index 72e29ec76b..b8bc64ee0a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionLoopSuspend.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionLoopSuspend.java @@ -13,7 +13,7 @@ import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.events.EventRefreshOverview; 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.Label; +import info.nightscout.androidaps.plugins.general.automation.elements.LabelWithElement; import info.nightscout.androidaps.plugins.general.automation.elements.LayoutBuilder; import info.nightscout.androidaps.queue.Callback; import info.nightscout.androidaps.utils.JsonHelper; @@ -83,7 +83,7 @@ public class ActionLoopSuspend extends Action { public void generateDialog(LinearLayout root) { 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); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionNotification.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionNotification.java index 2f8b233574..d176f0b10f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionNotification.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionNotification.java @@ -12,7 +12,7 @@ import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.events.EventRefreshOverview; 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.nsclient.NSUpload; import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification; @@ -83,7 +83,7 @@ public class ActionNotification extends Action { public void generateDialog(LinearLayout root) { new LayoutBuilder() - .add(new Label(MainApp.gs(R.string.message_short), "", text)) + .add(new LabelWithElement(MainApp.gs(R.string.message_short), "", text)) .build(root); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionProfileSwitchPercent.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionProfileSwitchPercent.java index 1c6dcf56d5..9a2f9ac04d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionProfileSwitchPercent.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionProfileSwitchPercent.java @@ -13,7 +13,7 @@ import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; 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.Label; +import info.nightscout.androidaps.plugins.general.automation.elements.LabelWithElement; import info.nightscout.androidaps.plugins.general.automation.elements.LayoutBuilder; import info.nightscout.androidaps.queue.Callback; import info.nightscout.androidaps.utils.JsonHelper; @@ -42,8 +42,8 @@ public class ActionProfileSwitchPercent extends Action { @Override public void generateDialog(LinearLayout root) { new LayoutBuilder() - .add(new Label(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.percent_u), "", pct)) + .add(new LabelWithElement(MainApp.gs(R.string.careportal_newnstreatment_duration_min_label), "", duration)) .build(root); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionStartTempTarget.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionStartTempTarget.java index d74ceef78e..83a47fe288 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionStartTempTarget.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionStartTempTarget.java @@ -16,7 +16,7 @@ import info.nightscout.androidaps.db.Source; import info.nightscout.androidaps.db.TempTarget; 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.Label; +import info.nightscout.androidaps.plugins.general.automation.elements.LabelWithElement; import info.nightscout.androidaps.plugins.general.automation.elements.LayoutBuilder; import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; 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; new LayoutBuilder() - .add(new Label(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_temporarytarget), MainApp.gs(unitResId), value)) + .add(new LabelWithElement(MainApp.gs(R.string.careportal_newnstreatment_duration_min_label), "", duration)) .build(root); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseTriggerDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseTriggerDialog.java index ccf4ba2069..db532ab39b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseTriggerDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseTriggerDialog.java @@ -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.TriggerTempTarget; import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerTime; +import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerWifiSsid; public class ChooseTriggerDialog extends DialogFragment { @@ -38,6 +39,7 @@ public class ChooseTriggerDialog extends DialogFragment { add(new TriggerIob()); add(new TriggerProfilePercent()); add(new TriggerTempTarget()); + add(new TriggerWifiSsid()); }}; private Unbinder mUnbinder; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/Label.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/LabelWithElement.java similarity index 94% rename from app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/Label.java rename to app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/LabelWithElement.java index 95630ecfaa..2531d56e6c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/Label.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/LabelWithElement.java @@ -7,12 +7,12 @@ import android.widget.TextView; import info.nightscout.androidaps.MainApp; -public class Label extends Element { +public class LabelWithElement extends Element { final Element element; final String textPre; final String textPost; - public Label(String textPre, String textPost, Element element) { + public LabelWithElement(String textPre, String textPost, Element element) { this.element = element; this.textPre = textPre; this.textPost = textPost; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/StaticLabel.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/StaticLabel.java new file mode 100644 index 0000000000..3c8cf38074 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/StaticLabel.java @@ -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); + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBg.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBg.java index 2fb307f6a9..63f35f5b00 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBg.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBg.java @@ -17,8 +17,9 @@ import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.logging.L; 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.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.StaticLabel; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus; import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.JsonHelper; @@ -159,8 +160,9 @@ public class TriggerBg extends Trigger { @Override public void generateDialog(LinearLayout root, FragmentManager fragmentManager) { new LayoutBuilder() + .add(new StaticLabel(R.string.glucose)) .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); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerIob.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerIob.java index c0b2840331..9db7b87b7d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerIob.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerIob.java @@ -18,8 +18,9 @@ import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; 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.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.StaticLabel; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin; import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.JsonHelper; @@ -136,8 +137,9 @@ public class TriggerIob extends Trigger { @Override public void generateDialog(LinearLayout root, FragmentManager fragmentManager) { new LayoutBuilder() + .add(new StaticLabel(R.string.iob)) .add(comparator) - .add(new Label(MainApp.gs(R.string.iob_u), "", insulin)) + .add(new LabelWithElement(MainApp.gs(R.string.iob_u), "", insulin)) .build(root); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerProfilePercent.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerProfilePercent.java index cb5717d93f..694ca403c0 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerProfilePercent.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerProfilePercent.java @@ -17,8 +17,9 @@ import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; 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.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.StaticLabel; import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.JsonHelper; import info.nightscout.androidaps.utils.T; @@ -142,8 +143,9 @@ public class TriggerProfilePercent extends Trigger { @Override public void generateDialog(LinearLayout root, FragmentManager fragmentManager) { new LayoutBuilder() + .add(new StaticLabel(R.string.profilepercentage)) .add(comparator) - .add(new Label(MainApp.gs(R.string.percent_u), "", pct)) + .add(new LabelWithElement(MainApp.gs(R.string.percent_u), "", pct)) .build(root); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTempTarget.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTempTarget.java index d6b66e6ea7..6cb3d60470 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTempTarget.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTempTarget.java @@ -14,11 +14,9 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.db.TempTarget; 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.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.StaticLabel; import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.JsonHelper; @@ -125,6 +123,7 @@ public class TriggerTempTarget extends Trigger { @Override public void generateDialog(LinearLayout root, FragmentManager fragmentManager) { new LayoutBuilder() + .add(new StaticLabel(R.string.temptarget)) .add(comparator) .build(root); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerWifiSsid.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerWifiSsid.java new file mode 100644 index 0000000000..d768c4379a --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerWifiSsid.java @@ -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 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); + } +} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f2a9add5cd..24d9f1f2d9 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1399,6 +1399,7 @@ exists not exists Temp target %1$s + WiFi SSID %1$s %2$s %1$d day diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/elements/LabelTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/elements/LabelWithElementTest.java similarity index 79% rename from app/src/test/java/info/nightscout/androidaps/plugins/general/automation/elements/LabelTest.java rename to app/src/test/java/info/nightscout/androidaps/plugins/general/automation/elements/LabelWithElementTest.java index 55d59e61c0..e3e85d8ae4 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/elements/LabelTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/elements/LabelWithElementTest.java @@ -5,14 +5,12 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.modules.junit4.PowerMockRunner; -import static org.junit.Assert.*; - @RunWith(PowerMockRunner.class) -public class LabelTest { +public class LabelWithElementTest { @Test 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("B", l.textPost); Assert.assertEquals(InputInsulin.class, l.element.getClass()); diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/elements/StaticLabelTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/elements/StaticLabelTest.java new file mode 100644 index 0000000000..669e5f8982 --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/elements/StaticLabelTest.java @@ -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(); + } +} \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerWifiSsidTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerWifiSsidTest.java new file mode 100644 index 0000000000..e0ee555753 --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerWifiSsidTest.java @@ -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); + } + +}