Merge pull request #1521 from MilosKozak/smsrefactor

Smsrefactor
This commit is contained in:
Milos Kozak 2019-03-25 08:41:09 +01:00 committed by GitHub
commit 0ee121d916
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 1984 additions and 511 deletions

View file

@ -50,6 +50,10 @@ public class ConstraintChecker implements ConstraintsInterface {
return isAdvancedFilteringEnabled(new Constraint<>(true));
}
public Constraint<Boolean> isSuperBolusEnabled() {
return isSuperBolusEnabled(new Constraint<>(true));
}
public Constraint<Double> getMaxBasalAllowed(Profile profile) {
return applyBasalConstraints(new Constraint<>(Constants.REALLYHIGHBASALRATE), profile);
}
@ -157,6 +161,17 @@ public class ConstraintChecker implements ConstraintsInterface {
return value;
}
@Override
public Constraint<Boolean> isSuperBolusEnabled(Constraint<Boolean> value) {
ArrayList<PluginBase> constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class);
for (PluginBase p : constraintsPlugins) {
ConstraintsInterface constraint = (ConstraintsInterface) p;
if (!p.isEnabled(PluginType.CONSTRAINTS)) continue;
constraint.isSuperBolusEnabled(value);
}
return value;
}
@Override
public Constraint<Double> applyBasalConstraints(Constraint<Double> absoluteRate, Profile profile) {
ArrayList<PluginBase> constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class);

View file

@ -35,6 +35,10 @@ public interface ConstraintsInterface {
return value;
}
default Constraint<Boolean> isSuperBolusEnabled(Constraint<Boolean> value) {
return value;
}
default Constraint<Double> applyBasalConstraints(Constraint<Double> absoluteRate, Profile profile) {
return absoluteRate;
}

View file

@ -93,6 +93,8 @@ public abstract class PluginBase {
return state == State.ENABLED && specialEnableCondition();
if (type == PluginType.CONSTRAINTS && pluginDescription.mainType == PluginType.PUMP && isEnabled(PluginType.PUMP))
return true;
if (type == PluginType.CONSTRAINTS && pluginDescription.mainType == PluginType.APS && isEnabled(PluginType.APS))
return true;
if (type == PluginType.PROFILE && pluginDescription.mainType == PluginType.PUMP)
return isProfileInterfaceEnabled;
return false;

View file

@ -95,6 +95,7 @@ public class L {
public static final String PROFILE = "PROFILE";
public static final String CONFIGBUILDER = "CONFIGBUILDER";
public static final String UI = "UI";
public static final String SMS = "SMS";
private static void initialize() {
logElements = new ArrayList<>();
@ -117,6 +118,7 @@ public class L {
logElements.add(new LogElement(PUMPBTCOMM, false));
logElements.add(new LogElement(PUMPCOMM, true));
logElements.add(new LogElement(PUMPQUEUE, true));
logElements.add(new LogElement(SMS, true));
logElements.add(new LogElement(UI, true));
}

View file

@ -13,6 +13,7 @@ import info.nightscout.androidaps.data.Profile;
import info.nightscout.androidaps.db.TempTarget;
import info.nightscout.androidaps.interfaces.APSInterface;
import info.nightscout.androidaps.interfaces.Constraint;
import info.nightscout.androidaps.interfaces.ConstraintsInterface;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PluginDescription;
import info.nightscout.androidaps.interfaces.PluginType;
@ -38,7 +39,7 @@ import info.nightscout.androidaps.utils.ToastUtils;
/**
* Created by mike on 05.08.2016.
*/
public class OpenAPSSMBPlugin extends PluginBase implements APSInterface {
public class OpenAPSSMBPlugin extends PluginBase implements APSInterface, ConstraintsInterface {
private static Logger log = LoggerFactory.getLogger(L.APS);
private static OpenAPSSMBPlugin openAPSSMBPlugin;
@ -268,4 +269,9 @@ public class OpenAPSSMBPlugin extends PluginBase implements APSInterface {
return newvalue;
}
public Constraint<Boolean> isSuperBolusEnabled(Constraint<Boolean> value) {
value.set(false);
return value;
}
}

View file

@ -16,6 +16,7 @@ import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.Profile;
import info.nightscout.androidaps.data.ProfileStore;
import info.nightscout.androidaps.db.ProfileSwitch;
import info.nightscout.androidaps.db.Source;
import info.nightscout.androidaps.events.EventNewBasalProfile;
import info.nightscout.androidaps.events.EventProfileSwitchChange;
import info.nightscout.androidaps.interfaces.ProfileInterface;
@ -139,4 +140,44 @@ public class ProfileFunctions {
return null;
}
public static ProfileSwitch prepareProfileSwitch(final ProfileStore profileStore, final String profileName, final int duration, final int percentage, final int timeshift, long date) {
ProfileSwitch profileSwitch = new ProfileSwitch();
profileSwitch.date = date;
profileSwitch.source = Source.USER;
profileSwitch.profileName = profileName;
profileSwitch.profileJson = profileStore.getSpecificProfile(profileName).getData().toString();
profileSwitch.profilePlugin = ConfigBuilderPlugin.getPlugin().getActiveProfileInterface().getClass().getName();
profileSwitch.durationInMinutes = duration;
profileSwitch.isCPP = percentage != 100 || timeshift != 0;
profileSwitch.timeshift = timeshift;
profileSwitch.percentage = percentage;
return profileSwitch;
}
public static void doProfileSwitch(final ProfileStore profileStore, final String profileName, final int duration, final int percentage, final int timeshift) {
ProfileSwitch profileSwitch = prepareProfileSwitch(profileStore, profileName, duration, percentage, timeshift, System.currentTimeMillis());
TreatmentsPlugin.getPlugin().addToHistoryProfileSwitch(profileSwitch);
FabricPrivacy.getInstance().logCustom(new CustomEvent("ProfileSwitch"));
}
public static void doProfileSwitch(final int duration, final int percentage, final int timeshift) {
ProfileSwitch profileSwitch = TreatmentsPlugin.getPlugin().getProfileSwitchFromHistory(System.currentTimeMillis());
if (profileSwitch != null) {
profileSwitch = new ProfileSwitch();
profileSwitch.date = System.currentTimeMillis();
profileSwitch.source = Source.USER;
profileSwitch.profileName = getInstance().getProfileName(System.currentTimeMillis(), false);
profileSwitch.profileJson = getInstance().getProfile().getData().toString();
profileSwitch.profilePlugin = ConfigBuilderPlugin.getPlugin().getActiveProfileInterface().getClass().getName();
profileSwitch.durationInMinutes = duration;
profileSwitch.isCPP = percentage != 100 || timeshift != 0;
profileSwitch.timeshift = timeshift;
profileSwitch.percentage = percentage;
TreatmentsPlugin.getPlugin().addToHistoryProfileSwitch(profileSwitch);
FabricPrivacy.getInstance().logCustom(new CustomEvent("ProfileSwitch"));
} else {
log.error("No profile switch existing");
}
}
}

View file

@ -722,7 +722,7 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick
public void createNSTreatment(JSONObject data) {
if (options.executeProfileSwitch) {
if (data.has("profile")) {
doProfileSwitch(profileStore, JsonHelper.safeGetString(data, "profile"), JsonHelper.safeGetInt(data, "duration"), JsonHelper.safeGetInt(data, "percentage"), JsonHelper.safeGetInt(data, "timeshift"));
ProfileFunctions.doProfileSwitch(profileStore, JsonHelper.safeGetString(data, "profile"), JsonHelper.safeGetInt(data, "duration"), JsonHelper.safeGetInt(data, "percentage"), JsonHelper.safeGetInt(data, "timeshift"));
}
} else if (options.executeTempTarget) {
final int duration = JsonHelper.safeGetInt(data, "duration");
@ -746,7 +746,7 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick
}
} else {
if (JsonHelper.safeGetString(data, "eventType").equals(CareportalEvent.PROFILESWITCH)) {
ProfileSwitch profileSwitch = prepareProfileSwitch(
ProfileSwitch profileSwitch = ProfileFunctions.prepareProfileSwitch(
profileStore,
JsonHelper.safeGetString(data, "profile"),
JsonHelper.safeGetInt(data, "duration"),
@ -762,46 +762,6 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick
}
}
public static ProfileSwitch prepareProfileSwitch(final ProfileStore profileStore, final String profileName, final int duration, final int percentage, final int timeshift, long date) {
ProfileSwitch profileSwitch = new ProfileSwitch();
profileSwitch.date = date;
profileSwitch.source = Source.USER;
profileSwitch.profileName = profileName;
profileSwitch.profileJson = profileStore.getSpecificProfile(profileName).getData().toString();
profileSwitch.profilePlugin = ConfigBuilderPlugin.getPlugin().getActiveProfileInterface().getClass().getName();
profileSwitch.durationInMinutes = duration;
profileSwitch.isCPP = percentage != 100 || timeshift != 0;
profileSwitch.timeshift = timeshift;
profileSwitch.percentage = percentage;
return profileSwitch;
}
public static void doProfileSwitch(final ProfileStore profileStore, final String profileName, final int duration, final int percentage, final int timeshift) {
ProfileSwitch profileSwitch = prepareProfileSwitch(profileStore, profileName, duration, percentage, timeshift, System.currentTimeMillis());
TreatmentsPlugin.getPlugin().addToHistoryProfileSwitch(profileSwitch);
FabricPrivacy.getInstance().logCustom(new CustomEvent("ProfileSwitch"));
}
public static void doProfileSwitch(final int duration, final int percentage, final int timeshift) {
ProfileSwitch profileSwitch = TreatmentsPlugin.getPlugin().getProfileSwitchFromHistory(System.currentTimeMillis());
if (profileSwitch != null) {
profileSwitch = new ProfileSwitch();
profileSwitch.date = System.currentTimeMillis();
profileSwitch.source = Source.USER;
profileSwitch.profileName = ProfileFunctions.getInstance().getProfileName(System.currentTimeMillis(), false);
profileSwitch.profileJson = ProfileFunctions.getInstance().getProfile().getData().toString();
profileSwitch.profilePlugin = ConfigBuilderPlugin.getPlugin().getActiveProfileInterface().getClass().getName();
profileSwitch.durationInMinutes = duration;
profileSwitch.isCPP = percentage != 100 || timeshift != 0;
profileSwitch.timeshift = timeshift;
profileSwitch.percentage = percentage;
TreatmentsPlugin.getPlugin().addToHistoryProfileSwitch(profileSwitch);
FabricPrivacy.getInstance().logCustom(new CustomEvent("ProfileSwitch"));
} else {
log.error("No profile switch existing");
}
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putString("notesEdit", notesEdit.getText().toString());

View file

@ -0,0 +1,59 @@
package info.nightscout.androidaps.plugins.general.smsCommunicator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.utils.DateUtil;
class AuthRequest {
private static Logger log = LoggerFactory.getLogger(L.SMS);
Sms requester;
String confirmCode;
private Runnable action;
private long date;
private boolean processed;
private SmsCommunicatorPlugin plugin;
AuthRequest(SmsCommunicatorPlugin plugin, Sms requester, String requestText, String confirmCode, SmsAction action) {
this.requester = requester;
this.confirmCode = confirmCode;
this.action = action;
this.plugin = plugin;
this.date = DateUtil.now();
plugin.sendSMS(new Sms(requester.phoneNumber, requestText));
}
void action(String codeReceived) {
if (processed) {
if (L.isEnabled(L.SMS))
log.debug("Already processed");
return;
}
if (!confirmCode.equals(codeReceived)) {
processed = true;
if (L.isEnabled(L.SMS))
log.debug("Wrong code");
plugin.sendSMS(new Sms(requester.phoneNumber, R.string.sms_wrongcode));
return;
}
if (DateUtil.now() - date < Constants.SMS_CONFIRM_TIMEOUT) {
processed = true;
if (L.isEnabled(L.SMS))
log.debug("Processing confirmed SMS: " + requester.text);
if (action != null)
action.run();
return;
}
if (L.isEnabled(L.SMS))
log.debug("Timed out SMS: " + requester.text);
}
}

View file

@ -0,0 +1,42 @@
package info.nightscout.androidaps.plugins.general.smsCommunicator;
import android.telephony.SmsMessage;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.utils.DateUtil;
class Sms {
String phoneNumber;
String text;
long date;
boolean received = false;
boolean sent = false;
boolean processed = false;
boolean ignored = false;
Sms(SmsMessage message) {
phoneNumber = message.getOriginatingAddress();
text = message.getMessageBody();
date = message.getTimestampMillis();
received = true;
}
Sms(String phoneNumber, String text) {
this.phoneNumber = phoneNumber;
this.text = text;
this.date = DateUtil.now();
sent = true;
}
Sms(String phoneNumber, int textId) {
this.phoneNumber = phoneNumber;
this.text = MainApp.gs(textId);
this.date = DateUtil.now();
sent = true;
}
public String toString() {
return "SMS from " + phoneNumber + ": " + text;
}
}

View file

@ -0,0 +1,33 @@
package info.nightscout.androidaps.plugins.general.smsCommunicator;
abstract class SmsAction implements Runnable {
Double aDouble;
Integer anInteger;
Integer secondInteger;
String aString;
SmsAction() {}
SmsAction(Double aDouble) {
this.aDouble = aDouble;
}
SmsAction(Double aDouble, Integer secondInteger) {
this.aDouble = aDouble;
this.secondInteger = secondInteger;
}
SmsAction(String aString, Integer secondInteger) {
this.aString = aString;
this.secondInteger = secondInteger;
}
SmsAction(Integer anInteger) {
this.anInteger = anInteger;
}
SmsAction(Integer anInteger, Integer secondInteger) {
this.anInteger = anInteger;
this.secondInteger = secondInteger;
}
}

View file

@ -3,7 +3,6 @@ package info.nightscout.androidaps.plugins.general.smsCommunicator;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
@ -12,9 +11,6 @@ import android.widget.TextView;
import com.squareup.otto.Subscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collections;
import java.util.Comparator;
@ -22,14 +18,8 @@ import info.nightscout.androidaps.R;
import info.nightscout.androidaps.plugins.common.SubscriberFragment;
import info.nightscout.androidaps.plugins.general.smsCommunicator.events.EventSmsCommunicatorUpdateGui;
import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.FabricPrivacy;
/**
* A simple {@link Fragment} subclass.
*/
public class SmsCommunicatorFragment extends SubscriberFragment {
private static Logger log = LoggerFactory.getLogger(SmsCommunicatorFragment.class);
TextView logView;
public SmsCommunicatorFragment() {
@ -39,18 +29,11 @@ public class SmsCommunicatorFragment extends SubscriberFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
try {
View view = inflater.inflate(R.layout.smscommunicator_fragment, container, false);
View view = inflater.inflate(R.layout.smscommunicator_fragment, container, false);
logView = (TextView) view.findViewById(R.id.smscommunicator_log);
logView = (TextView) view.findViewById(R.id.smscommunicator_log);
updateGUI();
return view;
} catch (Exception e) {
FabricPrivacy.logException(e);
}
return null;
return view;
}
@Subscribe
@ -58,35 +41,33 @@ public class SmsCommunicatorFragment extends SubscriberFragment {
updateGUI();
}
@Override
protected void updateGUI() {
Activity activity = getActivity();
if (activity != null)
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
class CustomComparator implements Comparator<SmsCommunicatorPlugin.Sms> {
public int compare(SmsCommunicatorPlugin.Sms object1, SmsCommunicatorPlugin.Sms object2) {
return (int) (object1.date - object2.date);
}
activity.runOnUiThread(() -> {
class CustomComparator implements Comparator<Sms> {
public int compare(Sms object1, Sms object2) {
return (int) (object1.date - object2.date);
}
Collections.sort(SmsCommunicatorPlugin.getPlugin().messages, new CustomComparator());
int messagesToShow = 40;
int start = Math.max(0, SmsCommunicatorPlugin.getPlugin().messages.size() - messagesToShow);
String logText = "";
for (int x = start; x < SmsCommunicatorPlugin.getPlugin().messages.size(); x++) {
SmsCommunicatorPlugin.Sms sms = SmsCommunicatorPlugin.getPlugin().messages.get(x);
if (sms.received) {
logText += DateUtil.timeString(sms.date) + " &lt;&lt;&lt; " + (sms.processed ? "" : "") + sms.phoneNumber + " <b>" + sms.text + "</b><br>";
} else if (sms.sent) {
logText += DateUtil.timeString(sms.date) + " &gt;&gt;&gt; " + (sms.processed ? "" : "") + sms.phoneNumber + " <b>" + sms.text + "</b><br>";
}
}
logView.setText(Html.fromHtml(logText));
}
Collections.sort(SmsCommunicatorPlugin.getPlugin().messages, new CustomComparator());
int messagesToShow = 40;
int start = Math.max(0, SmsCommunicatorPlugin.getPlugin().messages.size() - messagesToShow);
String logText = "";
for (int x = start; x < SmsCommunicatorPlugin.getPlugin().messages.size(); x++) {
Sms sms = SmsCommunicatorPlugin.getPlugin().messages.get(x);
if (sms.ignored) {
logText += DateUtil.timeString(sms.date) + " &lt;&lt;&lt; " + "" + sms.phoneNumber + " <b>" + sms.text + "</b><br>";
} else if (sms.received) {
logText += DateUtil.timeString(sms.date) + " &lt;&lt;&lt; " + (sms.processed ? "" : "") + sms.phoneNumber + " <b>" + sms.text + "</b><br>";
} else if (sms.sent) {
logText += DateUtil.timeString(sms.date) + " &gt;&gt;&gt; " + (sms.processed ? "" : "") + sms.phoneNumber + " <b>" + sms.text + "</b><br>";
}
}
logView.setText(Html.fromHtml(logText));
});
}
}

View file

@ -693,7 +693,7 @@ public class ActionStringHandler {
//send profile to pumpe
new NewNSTreatmentDialog(); //init
NewNSTreatmentDialog.doProfileSwitch(0, percentage, timeshift);
ProfileFunctions.doProfileSwitch(0, percentage, timeshift);
}
private static void generateTempTarget(int duration, double low, double high) {

View file

@ -22,7 +22,6 @@ import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.Profile;
import info.nightscout.androidaps.data.ProfileStore;
import info.nightscout.androidaps.plugins.general.careportal.Dialogs.NewNSTreatmentDialog;
import info.nightscout.androidaps.plugins.common.SubscriberFragment;
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
import info.nightscout.androidaps.plugins.profile.ns.events.EventNSProfileUpdateGUI;
@ -157,7 +156,7 @@ public class NSProfileFragment extends SubscriberFragment {
Profile profile = store.getSpecificProfile(name);
if (profile != null) {
OKDialog.showConfirmation(getActivity(), MainApp.gs(R.string.activate_profile) + ": " + name + " ?", () ->
NewNSTreatmentDialog.doProfileSwitch(store, name, 0, 100, 0)
ProfileFunctions.doProfileSwitch(store, name, 0, 100, 0)
);
}
}

View file

@ -294,12 +294,11 @@
<string name="smscommunicator_bolusreplywithcode">To deliver bolus %1$.2fU reply with code %2$s</string>
<string name="smscommunicator_calibrationreplywithcode">To send calibration %1$.2f reply with code %2$s</string>
<string name="smscommunicator_bolusfailed">Bolus failed</string>
<string name="bolusdelivered" formatted="false">Bolus %.2fU delivered successfully</string>
<string name="bolusrequested" formatted="false">Going to deliver %.2fU</string>
<string name="smscommunicator_bolusdelivered" formatted="false">Bolus %.2fU delivered successfully</string>
<string name="bolusdelivering" formatted="false">Delivering %.2fU</string>
<string name="bolusdelivered">Bolus %1$.2fU delivered successfully</string>
<string name="bolusrequested">Going to deliver %1$.2fU</string>
<string name="smscommunicator_bolusdelivered">Bolus %1$.2fU delivered successfully</string>
<string name="bolusdelivering">Delivering %1$.2fU</string>
<string name="smscommunicator_remotecommandsallowed">Allow remote commands via SMS</string>
<string name="smscommunicator_remotebolusnotallowed">Remote bolus not allowed</string>
<string name="glucosetype_finger">Finger</string>
<string name="glucosetype_sensor">Sensor</string>
<string name="manual">Manual</string>
@ -367,16 +366,25 @@
<string name="smscommunicator_loopisenabled">Loop is enabled</string>
<string name="valuelimitedto">%1$.2f limited to %2$.2f</string>
<string name="valueoutofrange" formatted="false">Value %s is out of hard limits</string>
<string name="smscommunicator_remotebasalnotallowed">Remote basal setting is not allowed</string>
<string name="smscommunicator_remotecommandnotallowed">Remote command is not allowed</string>
<string name="smscommunicator_basalreplywithcode">To start basal %1$.2fU/h reply with code %2$s</string>
<string name="smscommunicator_remotebolusnotallowed">Remote bolus not available. Try again later.</string>
<string name="smscommunicator_basalreplywithcode">To start basal %1$.2fU/h for %2$d min reply with code %3$s</string>
<string name="smscommunicator_profilereplywithcode">To switch profile to %1$s %2$d%% reply with code %3$s</string>
<string name="smscommunicator_extendedreplywithcode">To start extended bolus %1$.2fU for %2$d min reply with code %3$s</string>
<string name="smscommunicator_basalpctreplywithcode">To start basal %1$d%% for %2$d min reply with code %3$s</string>
<string name="smscommunicator_suspendreplywithcode">To suspend loop for %1$d minutes reply with code %2$s</string>
<string name="smscommunicator_tempbasalset">Temp basal %1$.2fU/h for %2$d min started successfully</string>
<string name="smscommunicator_extendedset">Extended bolus %1$.2fU for %2$d min started successfully</string>
<string name="smscommunicator_tempbasalset_percent">Temp basal %1$d%% for %2$d min started successfully</string>
<string name="smscommunicator_tempbasalfailed">Temp basal start failed</string>
<string name="smscommunicator_basalstopreplywithcode" formatted="false">To stop temp basal reply with code %s</string>
<string name="smscommunicator_extendedfailed">Extended bolus start failed</string>
<string name="smscommunicator_basalstopreplywithcode">To stop temp basal reply with code %1$s</string>
<string name="smscommunicator_extendedstopreplywithcode">To stop extended bolus reply with code %1$s</string>
<string name="smscommunicator_tempbasalcanceled">Temp basal canceled</string>
<string name="smscommunicator_extendedcanceled">Extended bolus canceled</string>
<string name="smscommunicator_tempbasalcancelfailed">Canceling temp basal failed</string>
<string name="smscommunicator_unknowncommand">Uknown command or wrong reply</string>
<string name="smscommunicator_extendedcancelfailed">Canceling extended bolus failed</string>
<string name="smscommunicator_unknowncommand">Unknown command or wrong reply</string>
<string name="quickwizard">QuickWizard</string>
<string name="quickwizardsettings">QuickWizard settings</string>
@ -509,7 +517,6 @@
<string name="send_calibration" formatted="false">Send calibration %.1f to xDrip?</string>
<string name="xdripnotinstalled">xDrip+ not installed</string>
<string name="calibrationsent">Calibration sent to xDrip</string>
<string name="smscommunicator_remotecalibrationnotallowed">Remote calibration not allowed</string>
<string name="smscommunicator_calibrationsent">Calibration sent. Receiving must be enabled in xDrip.</string>
<string name="smscommunicator_calibrationfailed">xDrip is not receiving calibrations</string>
<string name="pumpsuspended">Pump suspended</string>
@ -1315,6 +1322,10 @@
<string name="dst_loop_disabled_warning">Dayligh Saving time change in less than 3 hours - Closed loop diabled</string>
<string name="storage">internal storage constraint</string>
<string name="diskfull">Free at least %1$d MB from internal storage! Loop disabled!</string>
<string name="wrongformat">Wrong format</string>
<string name="sms_wrongcode">Wrong code. Command cancelled.</string>
<string name="notconfigured">Not configured</string>
<string name="profileswitchcreated">Profile switch created</string>
<plurals name="objective_days">
<item quantity="one">%1$d day</item>
<item quantity="other">%1$d days</item>

View file

@ -2,6 +2,7 @@ package info;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import com.squareup.otto.Bus;
@ -9,6 +10,7 @@ import com.squareup.otto.Bus;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Assert;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito;
import java.util.Locale;
@ -17,6 +19,7 @@ import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.ConstraintChecker;
import info.nightscout.androidaps.data.IobTotal;
import info.nightscout.androidaps.data.Profile;
import info.nightscout.androidaps.data.ProfileStore;
import info.nightscout.androidaps.db.DatabaseHelper;
@ -24,11 +27,14 @@ import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
import info.nightscout.androidaps.plugins.general.nsclient.NSUpload;
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.CobInfo;
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin;
import info.nightscout.androidaps.plugins.pump.danaR.DanaRPlugin;
import info.nightscout.androidaps.plugins.pump.danaRKorean.DanaRKoreanPlugin;
import info.nightscout.androidaps.plugins.pump.danaRv2.DanaRv2Plugin;
import info.nightscout.androidaps.plugins.treatments.TreatmentService;
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin;
import info.nightscout.androidaps.plugins.pump.danaR.DanaRPlugin;
import info.nightscout.androidaps.plugins.pump.danaRKorean.DanaRKoreanPlugin;
import info.nightscout.androidaps.queue.Callback;
import info.nightscout.androidaps.queue.CommandQueue;
import info.nightscout.androidaps.utils.SP;
@ -36,6 +42,7 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ -51,6 +58,8 @@ public class AAPSMocker {
public static Intent intentSent = null;
public static CommandQueue queue;
public static void mockStrings() {
Locale.setDefault(new Locale("en", "US"));
@ -104,6 +113,43 @@ public class AAPSMocker {
when(MainApp.gs(R.string.profile_per_unit)).thenReturn("/U");
when(MainApp.gs(R.string.profile_carbs_per_unit)).thenReturn("g/U");
when(MainApp.gs(R.string.profile_ins_units_per_hout)).thenReturn("U/h");
when(MainApp.gs(R.string.sms_wrongcode)).thenReturn("Wrong code. Command cancelled.");
when(MainApp.gs(R.string.sms_iob)).thenReturn("IOB:");
when(MainApp.gs(R.string.sms_lastbg)).thenReturn("Last BG:");
when(MainApp.gs(R.string.sms_minago)).thenReturn("%1$dmin ago");
when(MainApp.gs(R.string.smscommunicator_remotecommandnotallowed)).thenReturn("Remote command is not allowed");
when(MainApp.gs(R.string.loopsuspendedfor)).thenReturn("Suspended (%1$d m)");
when(MainApp.gs(R.string.smscommunicator_loopisdisabled)).thenReturn("Loop is disabled");
when(MainApp.gs(R.string.smscommunicator_loopisenabled)).thenReturn("Loop is enabled");
when(MainApp.gs(R.string.wrongformat)).thenReturn("Wrong format");
when(MainApp.gs(R.string.smscommunicator_loophasbeendisabled)).thenReturn("Loop has been disabled");
when(MainApp.gs(R.string.smscommunicator_loophasbeenenabled)).thenReturn("Loop has been enabled");
when(MainApp.gs(R.string.smscommunicator_tempbasalcanceled)).thenReturn("Temp basal canceled");
when(MainApp.gs(R.string.smscommunicator_loopresumed)).thenReturn("Loop resumed");
when(MainApp.gs(R.string.smscommunicator_wrongduration)).thenReturn("Wrong duration");
when(MainApp.gs(R.string.smscommunicator_suspendreplywithcode)).thenReturn("To suspend loop for %1$d minutes reply with code %2$s");
when(MainApp.gs(R.string.smscommunicator_loopsuspended)).thenReturn("Loop suspended");
when(MainApp.gs(R.string.smscommunicator_unknowncommand)).thenReturn("Unknown command or wrong reply");
when(MainApp.gs(R.string.notconfigured)).thenReturn("Not configured");
when(MainApp.gs(R.string.smscommunicator_profilereplywithcode)).thenReturn("To switch profile to %1$s %2$d%% reply with code %3$s");
when(MainApp.gs(R.string.profileswitchcreated)).thenReturn("Profile switch created");
when(MainApp.gs(R.string.smscommunicator_basalstopreplywithcode)).thenReturn("To stop temp basal reply with code %1$s");
when(MainApp.gs(R.string.smscommunicator_basalpctreplywithcode)).thenReturn("To start basal %1$d%% for %2$d min reply with code %3$s");
when(MainApp.gs(R.string.smscommunicator_tempbasalset_percent)).thenReturn("Temp basal %1$d%% for %2$d min started successfully");
when(MainApp.gs(R.string.smscommunicator_basalreplywithcode)).thenReturn("To start basal %1$.2fU/h for %2$d min reply with code %3$s");
when(MainApp.gs(R.string.smscommunicator_tempbasalset)).thenReturn("Temp basal %1$.2fU/h for %2$d min started successfully");
when(MainApp.gs(R.string.smscommunicator_extendedstopreplywithcode)).thenReturn("To stop extended bolus reply with code %1$s");
when(MainApp.gs(R.string.smscommunicator_extendedcanceled)).thenReturn("Extended bolus canceled");
when(MainApp.gs(R.string.smscommunicator_extendedreplywithcode)).thenReturn("To start extended bolus %1$.2fU for %2$d min reply with code %3$s");
when(MainApp.gs(R.string.smscommunicator_extendedset)).thenReturn("Extended bolus %1$.2fU for %2$d min started successfully");
when(MainApp.gs(R.string.smscommunicator_bolusreplywithcode)).thenReturn("To deliver bolus %1$.2fU reply with code %2$s");
when(MainApp.gs(R.string.smscommunicator_bolusdelivered)).thenReturn("Bolus %1$.2fU delivered successfully");
when(MainApp.gs(R.string.smscommunicator_remotebolusnotallowed)).thenReturn("Remote bolus not available. Try again later.");
when(MainApp.gs(R.string.smscommunicator_calibrationreplywithcode)).thenReturn("To send calibration %1$.2f reply with code %2$s");
when(MainApp.gs(R.string.smscommunicator_calibrationsent)).thenReturn("Calibration sent. Receiving must be enabled in xDrip.");
when(MainApp.gs(R.string.pumpsuspended)).thenReturn("Pump suspended");
when(MainApp.gs(R.string.cob)).thenReturn("COB");
when(MainApp.gs(R.string.value_unavailable_short)).thenReturn("n/a");
}
public static MainApp mockMainApp() {
@ -143,7 +189,7 @@ public class AAPSMocker {
when(L.isEnabled(any())).thenReturn(true);
}
public static void mockNSUpload(){
public static void mockNSUpload() {
PowerMockito.mockStatic(NSUpload.class);
}
@ -152,6 +198,8 @@ public class AAPSMocker {
Resources mResources = mock(Resources.class);
when(MainApp.instance().getApplicationContext()).thenReturn(mockedContext);
when(mockedContext.getResources()).thenReturn(mResources);
PackageManager packageManager = mock(PackageManager.class);
when(mockedContext.getPackageManager()).thenReturn(packageManager);
}
public static DatabaseHelper mockDatabaseHelper() {
@ -161,7 +209,7 @@ public class AAPSMocker {
}
public static void mockCommandQueue() {
CommandQueue queue = mock(CommandQueue.class);
queue = mock(CommandQueue.class);
when(ConfigBuilderPlugin.getPlugin().getCommandQueue()).thenReturn(queue);
}
@ -169,12 +217,21 @@ public class AAPSMocker {
PowerMockito.mockStatic(TreatmentsPlugin.class);
TreatmentsPlugin treatmentsPlugin = PowerMockito.mock(TreatmentsPlugin.class);
when(TreatmentsPlugin.getPlugin()).thenReturn(treatmentsPlugin);
when(treatmentsPlugin.getLastCalculationTreatments()).thenReturn(new IobTotal(0));
when(treatmentsPlugin.getLastCalculationTempBasals()).thenReturn(new IobTotal(0));
TreatmentService treatmentService = PowerMockito.mock(TreatmentService.class);
when(treatmentsPlugin.getService()).thenReturn(treatmentService);
return treatmentsPlugin;
}
public static void mockTreatmentService() throws Exception {
public static void mockTreatmentService() {
TreatmentService treatmentService = PowerMockito.mock(TreatmentService.class);
PowerMockito.whenNew(TreatmentService.class).withNoArguments().thenReturn(treatmentService);
try {
PowerMockito.whenNew(TreatmentService.class).withNoArguments().thenReturn(treatmentService);
} catch (Exception e) {
}
}
public static DanaRPlugin mockDanaRPlugin() {
@ -221,6 +278,14 @@ public class AAPSMocker {
PowerMockito.when(ProfileFunctions.getInstance()).thenReturn(profileFunctions);
profile = getValidProfile();
PowerMockito.when(ProfileFunctions.getInstance().getProfile()).thenReturn(profile);
PowerMockito.when(ProfileFunctions.getInstance().getProfileUnits()).thenReturn(Constants.MGDL);
PowerMockito.when(ProfileFunctions.getInstance().getProfileName()).thenReturn(TESTPROFILENAME);
}
public static void mockIobCobCalculatorPlugin() {
PowerMockito.mockStatic(IobCobCalculatorPlugin.class);
IobCobCalculatorPlugin iobCobCalculatorPlugin = PowerMockito.mock(IobCobCalculatorPlugin.class);
PowerMockito.when(IobCobCalculatorPlugin.getPlugin()).thenReturn(iobCobCalculatorPlugin);
}
private static MockedBus bus = new MockedBus();

View file

@ -2,6 +2,7 @@ package info.nightscout.androidaps.interfaces;
import junit.framework.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
@ -9,6 +10,7 @@ import org.powermock.modules.junit4.PowerMockRunner;
import info.AAPSMocker;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.utils.SP;
/**
@ -16,15 +18,11 @@ import info.nightscout.androidaps.utils.SP;
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({MainApp.class, SP.class})
@PrepareForTest({MainApp.class, SP.class, L.class})
public class ConstraintTest {
@Test
public void doTests() {
AAPSMocker.mockMainApp();
AAPSMocker.mockApplicationContext();
AAPSMocker.mockSP();
Constraint<Boolean> b = new Constraint<>(true);
Assert.assertEquals(Boolean.TRUE, b.value());
Assert.assertEquals("", b.getReasons());
@ -56,5 +54,18 @@ public class ConstraintTest {
Assert.assertEquals("ConstraintTest: Set 5d\nConstraintTest: Set 6d\nConstraintTest: Set 4d", d.getReasons());
Assert.assertEquals("ConstraintTest: Set 4d", d.getMostLimitedReasons());
Assert.assertEquals(10d, d.originalValue());
d.setIfDifferent(7d, "Set 7d", this);
Assert.assertEquals(7d, d.value());
Assert.assertEquals("ConstraintTest: Set 5d\nConstraintTest: Set 6d\nConstraintTest: Set 4d\nConstraintTest: Set 7d", d.getReasons());
Assert.assertEquals("ConstraintTest: Set 4d\nConstraintTest: Set 7d", d.getMostLimitedReasons());
Assert.assertEquals(10d, d.originalValue());
}
@Before
public void prepareMock() {
AAPSMocker.mockMainApp();
AAPSMocker.mockApplicationContext();
AAPSMocker.mockSP();
AAPSMocker.mockL();
}
}

View file

@ -54,6 +54,7 @@ public class ConstraintsCheckerTest {
DanaRPlugin danaRPlugin;
DanaRSPlugin danaRSPlugin;
LocalInsightPlugin insightPlugin;
OpenAPSSMBPlugin openAPSSMBPlugin;
boolean notificationSent = false;
@ -119,6 +120,14 @@ public class ConstraintsCheckerTest {
Assert.assertEquals(Boolean.FALSE, c.value());
}
@Test
public void isSuperBolusEnabledTest() throws Exception {
OpenAPSSMBPlugin.getPlugin().setPluginEnabled(PluginType.APS, true);
Constraint<Boolean> c = constraintChecker.isSuperBolusEnabled();
Assert.assertEquals(Boolean.FALSE, c.value()); // SMB should limit
}
@Test
public void isSMBModeEnabledTest() throws Exception {
objectivesPlugin.objectives.get(7).setStartedOn(null);
@ -288,6 +297,7 @@ public class ConstraintsCheckerTest {
danaRPlugin = DanaRPlugin.getPlugin();
danaRSPlugin = DanaRSPlugin.getPlugin();
insightPlugin = LocalInsightPlugin.getPlugin();
openAPSSMBPlugin = OpenAPSSMBPlugin.getPlugin();
ArrayList<PluginBase> constraintsPluginsList = new ArrayList<>();
constraintsPluginsList.add(safetyPlugin);
constraintsPluginsList.add(objectivesPlugin);
@ -295,6 +305,7 @@ public class ConstraintsCheckerTest {
constraintsPluginsList.add(danaRPlugin);
constraintsPluginsList.add(danaRSPlugin);
constraintsPluginsList.add(insightPlugin);
constraintsPluginsList.add(openAPSSMBPlugin);
when(mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class)).thenReturn(constraintsPluginsList);
}

View file

@ -0,0 +1,94 @@
package info.nightscout.androidaps.plugins.general.smsCommunicator;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.stubbing.Answer;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.util.Date;
import info.AAPSMocker;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.SP;
import info.nightscout.androidaps.utils.T;
import static org.mockito.ArgumentMatchers.any;
import static org.powermock.api.mockito.PowerMockito.doAnswer;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(PowerMockRunner.class)
@PrepareForTest({SmsCommunicatorPlugin.class, L.class, SP.class, MainApp.class, DateUtil.class})
public class AuthRequestTest {
SmsCommunicatorPlugin smsCommunicatorPlugin;
Sms sentSms;
boolean actionCalled = false;
@Test
public void doTests() {
Sms requester = new Sms("aNumber", "aText");
SmsAction action = new SmsAction() {
@Override
public void run() {
actionCalled = true;
}
};
// Check if SMS requesting code is sent
AuthRequest authRequest = new AuthRequest(smsCommunicatorPlugin, requester, "Request text", "ABC", action);
Assert.assertEquals(sentSms.phoneNumber, "aNumber");
Assert.assertEquals(sentSms.text, "Request text");
// wrong reply
actionCalled = false;
authRequest.action("EFG");
Assert.assertEquals(sentSms.phoneNumber, "aNumber");
Assert.assertEquals(sentSms.text, "Wrong code. Command cancelled.");
Assert.assertFalse(actionCalled);
// correct reply
authRequest = new AuthRequest(smsCommunicatorPlugin, requester, "Request text", "ABC", action);
actionCalled = false;
authRequest.action("ABC");
Assert.assertTrue(actionCalled);
// second time action should not be called
actionCalled = false;
authRequest.action("ABC");
Assert.assertFalse(actionCalled);
// test timed out message
long now = 10000;
when(DateUtil.now()).thenReturn(now);
authRequest = new AuthRequest(smsCommunicatorPlugin, requester, "Request text", "ABC", action);
actionCalled = false;
when(DateUtil.now()).thenReturn(now + T.mins(Constants.SMS_CONFIRM_TIMEOUT).msecs() + 1);
authRequest.action("ABC");
Assert.assertFalse(actionCalled);
}
@Before
public void prepareTests() {
smsCommunicatorPlugin = mock(SmsCommunicatorPlugin.class);
doAnswer((Answer) invocation -> {
sentSms = invocation.getArgument(0);
return null;
}).when(smsCommunicatorPlugin).sendSMS(any(Sms.class));
AAPSMocker.mockMainApp();
AAPSMocker.mockApplicationContext();
AAPSMocker.mockSP();
AAPSMocker.mockL();
AAPSMocker.mockStrings();
mockStatic(DateUtil.class);
}
}

View file

@ -0,0 +1,90 @@
package info.nightscout.androidaps.plugins.general.smsCommunicator;
import android.telephony.SmsMessage;
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;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(PowerMockRunner.class)
public class SmsActionTest {
String result = "";
@Test
public void doTests() {
SmsAction smsAction = new SmsAction() {
@Override
public void run() {
result = "A";
}
};
smsAction.run();
Assert.assertEquals(result, "A");
smsAction = new SmsAction(1d) {
@Override
public void run() {
result = "B";
}
};
smsAction.run();
Assert.assertEquals(result, "B");
Assert.assertEquals(smsAction.aDouble, 1d, 0.000001d);
smsAction = new SmsAction(1d, 2) {
@Override
public void run() {
result = "C";
}
};
smsAction.run();
Assert.assertEquals(result, "C");
Assert.assertEquals(smsAction.aDouble, 1d, 0.000001d);
Assert.assertEquals(smsAction.secondInteger.intValue(), 2);
smsAction = new SmsAction("aString", 3) {
@Override
public void run() {
result = "D";
}
};
smsAction.run();
Assert.assertEquals(result, "D");
Assert.assertEquals(smsAction.aString, "aString");
Assert.assertEquals(smsAction.secondInteger.intValue(), 3);
smsAction = new SmsAction(4) {
@Override
public void run() {
result = "E";
}
};
smsAction.run();
Assert.assertEquals(result, "E");
Assert.assertEquals(smsAction.anInteger.intValue(), 4);
smsAction = new SmsAction(5, 6) {
@Override
public void run() {
result = "F";
}
};
smsAction.run();
Assert.assertEquals(result, "F");
Assert.assertEquals(smsAction.anInteger.intValue(), 5);
Assert.assertEquals(smsAction.secondInteger.intValue(), 6);
}
}

View file

@ -0,0 +1,804 @@
package info.nightscout.androidaps.plugins.general.smsCommunicator;
import android.telephony.SmsManager;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.util.ArrayList;
import java.util.List;
import info.AAPSMocker;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.DetailedBolusInfo;
import info.nightscout.androidaps.data.PumpEnactResult;
import info.nightscout.androidaps.db.BgReading;
import info.nightscout.androidaps.interfaces.Constraint;
import info.nightscout.androidaps.interfaces.PluginType;
import info.nightscout.androidaps.interfaces.ProfileInterface;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin;
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
import info.nightscout.androidaps.plugins.general.nsclient.NSUpload;
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.CobInfo;
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin;
import info.nightscout.androidaps.plugins.profile.simple.SimpleProfilePlugin;
import info.nightscout.androidaps.plugins.pump.virtual.VirtualPumpPlugin;
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin;
import info.nightscout.androidaps.queue.Callback;
import info.nightscout.androidaps.queue.CommandQueue;
import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.SP;
import info.nightscout.androidaps.utils.XdripCalibrations;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyDouble;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.powermock.api.mockito.PowerMockito.doAnswer;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(PowerMockRunner.class)
@PrepareForTest({
L.class, SP.class, MainApp.class, DateUtil.class, ProfileFunctions.class,
TreatmentsPlugin.class, SmsManager.class, IobCobCalculatorPlugin.class,
CommandQueue.class, ConfigBuilderPlugin.class, NSUpload.class, ProfileInterface.class,
SimpleProfilePlugin.class, XdripCalibrations.class, VirtualPumpPlugin.class
})
public class SmsCommunicatorPluginTest {
private SmsCommunicatorPlugin smsCommunicatorPlugin;
private LoopPlugin loopPlugin;
private boolean hasBeenRun = false;
@Test
public void processSettingsTest() {
// called from constructor
Assert.assertEquals("1234", smsCommunicatorPlugin.allowedNumbers.get(0));
Assert.assertEquals("5678", smsCommunicatorPlugin.allowedNumbers.get(1));
Assert.assertEquals(2, smsCommunicatorPlugin.allowedNumbers.size());
}
@Test
public void isCommandTest() {
Assert.assertTrue(smsCommunicatorPlugin.isCommand("BOLUS", ""));
smsCommunicatorPlugin.messageToConfirm = null;
Assert.assertFalse(smsCommunicatorPlugin.isCommand("BLB", ""));
smsCommunicatorPlugin.messageToConfirm = new AuthRequest(smsCommunicatorPlugin, new Sms("1234", "ddd"), "RequestText", "ccode", new SmsAction() {
@Override
public void run() {
}
});
Assert.assertTrue(smsCommunicatorPlugin.isCommand("BLB", "1234"));
Assert.assertFalse(smsCommunicatorPlugin.isCommand("BLB", "2345"));
smsCommunicatorPlugin.messageToConfirm = null;
}
@Test
public void isAllowedNumberTest() {
Assert.assertTrue(smsCommunicatorPlugin.isAllowedNumber("5678"));
Assert.assertFalse(smsCommunicatorPlugin.isAllowedNumber("56"));
}
@Test
public void processSmsTest() {
Sms sms;
// SMS from not allowed number should be ignored
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("12", "aText");
smsCommunicatorPlugin.processSms(sms);
Assert.assertTrue(sms.ignored);
Assert.assertEquals("aText", smsCommunicatorPlugin.messages.get(0).text);
//UNKNOWN
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "UNKNOWN");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("UNKNOWN", smsCommunicatorPlugin.messages.get(0).text);
//BG
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "BG");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("BG", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertTrue(smsCommunicatorPlugin.messages.get(1).text.contains("IOB:"));
Assert.assertTrue(smsCommunicatorPlugin.messages.get(1).text.contains("Last BG: 100"));
Assert.assertTrue(smsCommunicatorPlugin.messages.get(1).text.contains("COB: 10(2)g"));
// LOOP : test remote control disabled
when(SP.getBoolean(R.string.key_smscommunicator_remotecommandsallowed, false)).thenReturn(false);
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "LOOP STATUS");
smsCommunicatorPlugin.processSms(sms);
Assert.assertFalse(sms.ignored);
Assert.assertEquals("LOOP STATUS", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertTrue(smsCommunicatorPlugin.messages.get(1).text.contains("Remote command is not allowed"));
when(SP.getBoolean(R.string.key_smscommunicator_remotecommandsallowed, false)).thenReturn(true);
//LOOP STATUS : disabled
when(loopPlugin.isEnabled(PluginType.LOOP)).thenReturn(false);
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "LOOP STATUS");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("LOOP STATUS", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Loop is disabled", smsCommunicatorPlugin.messages.get(1).text);
//LOOP STATUS : suspended
when(loopPlugin.minutesToEndOfSuspend()).thenReturn(10);
when(loopPlugin.isEnabled(PluginType.LOOP)).thenReturn(true);
when(loopPlugin.isSuspended()).thenReturn(true);
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "LOOP STATUS");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("LOOP STATUS", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Suspended (10 m)", smsCommunicatorPlugin.messages.get(1).text);
//LOOP STATUS : enabled
when(loopPlugin.isEnabled(PluginType.LOOP)).thenReturn(true);
when(loopPlugin.isSuspended()).thenReturn(false);
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "LOOP STATUS");
smsCommunicatorPlugin.processSms(sms);
Assert.assertFalse(sms.ignored);
Assert.assertEquals("LOOP STATUS", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Loop is enabled", smsCommunicatorPlugin.messages.get(1).text);
//LOOP : wrong format
when(loopPlugin.isEnabled(PluginType.LOOP)).thenReturn(true);
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "LOOP");
smsCommunicatorPlugin.processSms(sms);
Assert.assertFalse(sms.ignored);
Assert.assertEquals("LOOP", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Wrong format", smsCommunicatorPlugin.messages.get(1).text);
//LOOP DISABLE : already disabled
when(loopPlugin.isEnabled(PluginType.LOOP)).thenReturn(false);
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "LOOP DISABLE");
smsCommunicatorPlugin.processSms(sms);
Assert.assertFalse(sms.ignored);
Assert.assertEquals("LOOP DISABLE", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Loop is disabled", smsCommunicatorPlugin.messages.get(1).text);
//LOOP DISABLE : from enabled
hasBeenRun = false;
when(loopPlugin.isEnabled(PluginType.LOOP)).thenReturn(true);
doAnswer((Answer) invocation -> {
hasBeenRun = true;
return null;
}).when(loopPlugin).setPluginEnabled(PluginType.LOOP, false);
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "LOOP DISABLE");
smsCommunicatorPlugin.processSms(sms);
Assert.assertFalse(sms.ignored);
Assert.assertEquals("LOOP DISABLE", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Loop has been disabled Temp basal canceled", smsCommunicatorPlugin.messages.get(1).text);
Assert.assertTrue(hasBeenRun);
//LOOP ENABLE : already enabled
when(loopPlugin.isEnabled(PluginType.LOOP)).thenReturn(true);
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "LOOP ENABLE");
smsCommunicatorPlugin.processSms(sms);
Assert.assertFalse(sms.ignored);
Assert.assertEquals("LOOP ENABLE", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Loop is enabled", smsCommunicatorPlugin.messages.get(1).text);
//LOOP ENABLE : from disabled
hasBeenRun = false;
when(loopPlugin.isEnabled(PluginType.LOOP)).thenReturn(false);
doAnswer((Answer) invocation -> {
hasBeenRun = true;
return null;
}).when(loopPlugin).setPluginEnabled(PluginType.LOOP, true);
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "LOOP ENABLE");
smsCommunicatorPlugin.processSms(sms);
Assert.assertFalse(sms.ignored);
Assert.assertEquals("LOOP ENABLE", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Loop has been enabled", smsCommunicatorPlugin.messages.get(1).text);
Assert.assertTrue(hasBeenRun);
//LOOP RESUME : already enabled
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "LOOP RESUME");
smsCommunicatorPlugin.processSms(sms);
Assert.assertFalse(sms.ignored);
Assert.assertEquals("LOOP RESUME", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Loop resumed", smsCommunicatorPlugin.messages.get(1).text);
//LOOP SUSPEND 1 2: wrong format
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "LOOP SUSPEND 1 2");
smsCommunicatorPlugin.processSms(sms);
Assert.assertFalse(sms.ignored);
Assert.assertEquals("LOOP SUSPEND 1 2", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Wrong format", smsCommunicatorPlugin.messages.get(1).text);
//LOOP SUSPEND 0 : wrong duration
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "LOOP SUSPEND 0");
smsCommunicatorPlugin.processSms(sms);
Assert.assertFalse(sms.ignored);
Assert.assertEquals("LOOP SUSPEND 0", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Wrong duration", smsCommunicatorPlugin.messages.get(1).text);
//LOOP SUSPEND 100 : suspend for 100 min + correct answer
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "LOOP SUSPEND 100");
smsCommunicatorPlugin.processSms(sms);
Assert.assertFalse(sms.ignored);
Assert.assertEquals("LOOP SUSPEND 100", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertTrue(smsCommunicatorPlugin.messages.get(1).text.contains("To suspend loop for 100 minutes reply with code "));
String passCode = smsCommunicatorPlugin.messageToConfirm.confirmCode;
smsCommunicatorPlugin.processSms(new Sms("1234", passCode));
Assert.assertEquals(passCode, smsCommunicatorPlugin.messages.get(2).text);
Assert.assertEquals("Loop suspended Temp basal canceled", smsCommunicatorPlugin.messages.get(3).text);
//LOOP SUSPEND 200 : limit to 180 min + wrong answer
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "LOOP SUSPEND 200");
smsCommunicatorPlugin.processSms(sms);
Assert.assertFalse(sms.ignored);
Assert.assertEquals("LOOP SUSPEND 200", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertTrue(smsCommunicatorPlugin.messages.get(1).text.contains("To suspend loop for 180 minutes reply with code "));
passCode = smsCommunicatorPlugin.messageToConfirm.confirmCode;
// ignore from other number
smsCommunicatorPlugin.processSms(new Sms("5678", passCode));
smsCommunicatorPlugin.processSms(new Sms("1234", "XXXX"));
Assert.assertEquals("XXXX", smsCommunicatorPlugin.messages.get(3).text);
Assert.assertEquals("Wrong code. Command cancelled.", smsCommunicatorPlugin.messages.get(4).text);
//then correct code should not work
smsCommunicatorPlugin.processSms(new Sms("1234", passCode));
Assert.assertEquals(passCode, smsCommunicatorPlugin.messages.get(5).text);
Assert.assertEquals(6, smsCommunicatorPlugin.messages.size()); // processed as common message
//LOOP BLABLA
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "LOOP BLABLA");
smsCommunicatorPlugin.processSms(sms);
Assert.assertFalse(sms.ignored);
Assert.assertEquals("LOOP BLABLA", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Wrong format", smsCommunicatorPlugin.messages.get(1).text);
//TREATMENTS REFRESH
when(loopPlugin.isEnabled(PluginType.LOOP)).thenReturn(true);
when(loopPlugin.isSuspended()).thenReturn(false);
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "TREATMENTS REFRESH");
smsCommunicatorPlugin.processSms(sms);
Assert.assertFalse(sms.ignored);
Assert.assertEquals("TREATMENTS REFRESH", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertTrue(smsCommunicatorPlugin.messages.get(1).text.contains("TREATMENTS REFRESH"));
//TREATMENTS BLA BLA
when(loopPlugin.isEnabled(PluginType.LOOP)).thenReturn(true);
when(loopPlugin.isSuspended()).thenReturn(false);
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "TREATMENTS BLA BLA");
smsCommunicatorPlugin.processSms(sms);
Assert.assertFalse(sms.ignored);
Assert.assertEquals("TREATMENTS BLA BLA", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Wrong format", smsCommunicatorPlugin.messages.get(1).text);
//TREATMENTS BLABLA
when(loopPlugin.isEnabled(PluginType.LOOP)).thenReturn(true);
when(loopPlugin.isSuspended()).thenReturn(false);
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "TREATMENTS BLABLA");
smsCommunicatorPlugin.processSms(sms);
Assert.assertFalse(sms.ignored);
Assert.assertEquals("TREATMENTS BLABLA", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Wrong format", smsCommunicatorPlugin.messages.get(1).text);
//NSCLIENT RESTART
when(loopPlugin.isEnabled(PluginType.LOOP)).thenReturn(true);
when(loopPlugin.isSuspended()).thenReturn(false);
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "NSCLIENT RESTART");
smsCommunicatorPlugin.processSms(sms);
Assert.assertFalse(sms.ignored);
Assert.assertEquals("NSCLIENT RESTART", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertTrue(smsCommunicatorPlugin.messages.get(1).text.contains("NSCLIENT RESTART"));
//NSCLIENT BLA BLA
when(loopPlugin.isEnabled(PluginType.LOOP)).thenReturn(true);
when(loopPlugin.isSuspended()).thenReturn(false);
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "NSCLIENT BLA BLA");
smsCommunicatorPlugin.processSms(sms);
Assert.assertFalse(sms.ignored);
Assert.assertEquals("NSCLIENT BLA BLA", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Wrong format", smsCommunicatorPlugin.messages.get(1).text);
//NSCLIENT BLABLA
when(loopPlugin.isEnabled(PluginType.LOOP)).thenReturn(true);
when(loopPlugin.isSuspended()).thenReturn(false);
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "NSCLIENT BLABLA");
smsCommunicatorPlugin.processSms(sms);
Assert.assertFalse(sms.ignored);
Assert.assertEquals("NSCLIENT BLABLA", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Wrong format", smsCommunicatorPlugin.messages.get(1).text);
//PUMP
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "PUMP");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("PUMP", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Virtual Pump", smsCommunicatorPlugin.messages.get(1).text);
}
@Test
public void processProfileTest() {
Sms sms;
//PROFILE
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "PROFILE");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("PROFILE", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Remote command is not allowed", smsCommunicatorPlugin.messages.get(1).text);
when(SP.getBoolean(R.string.key_smscommunicator_remotecommandsallowed, false)).thenReturn(true);
//PROFILE
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "PROFILE");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("PROFILE", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Wrong format", smsCommunicatorPlugin.messages.get(1).text);
//PROFILE LIST (no profile interface)
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "PROFILE LIST");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("PROFILE LIST", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Not configured", smsCommunicatorPlugin.messages.get(1).text);
ProfileInterface profileInterface = mock(SimpleProfilePlugin.class);
when(ConfigBuilderPlugin.getPlugin().getActiveProfileInterface()).thenReturn(profileInterface);
//PROFILE LIST (no profile defined)
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "PROFILE LIST");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("PROFILE LIST", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Not configured", smsCommunicatorPlugin.messages.get(1).text);
when(profileInterface.getProfile()).thenReturn(AAPSMocker.getValidProfileStore());
//PROFILE STATUS
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "PROFILE STATUS");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("PROFILE STATUS", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals(AAPSMocker.TESTPROFILENAME, smsCommunicatorPlugin.messages.get(1).text);
//PROFILE LIST
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "PROFILE LIST");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("PROFILE LIST", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("1. " + AAPSMocker.TESTPROFILENAME, smsCommunicatorPlugin.messages.get(1).text);
//PROFILE 2 (non existing)
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "PROFILE 2");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("PROFILE 2", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Wrong format", smsCommunicatorPlugin.messages.get(1).text);
//PROFILE 1 0(wrong percentage)
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "PROFILE 1 0");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("PROFILE 1 0", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Wrong format", smsCommunicatorPlugin.messages.get(1).text);
//PROFILE 0(wrong index)
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "PROFILE 0");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("PROFILE 0", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Wrong format", smsCommunicatorPlugin.messages.get(1).text);
//PROFILE 1(OK)
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "PROFILE 1");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("PROFILE 1", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertTrue(smsCommunicatorPlugin.messages.get(1).text.contains("To switch profile to someProfile 100% reply with code"));
//PROFILE 1 90(OK)
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "PROFILE 1 90");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("PROFILE 1 90", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertTrue(smsCommunicatorPlugin.messages.get(1).text.contains("To switch profile to someProfile 90% reply with code"));
String passCode = smsCommunicatorPlugin.messageToConfirm.confirmCode;
smsCommunicatorPlugin.processSms(new Sms("1234", passCode));
Assert.assertEquals(passCode, smsCommunicatorPlugin.messages.get(2).text);
Assert.assertEquals("Profile switch created", smsCommunicatorPlugin.messages.get(3).text);
}
@Test
public void processBasalTest() {
Sms sms;
//BASAL
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "BASAL");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("BASAL", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Remote command is not allowed", smsCommunicatorPlugin.messages.get(1).text);
when(SP.getBoolean(R.string.key_smscommunicator_remotecommandsallowed, false)).thenReturn(true);
//BASAL
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "BASAL");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("BASAL", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Wrong format", smsCommunicatorPlugin.messages.get(1).text);
//BASAL CANCEL
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "BASAL CANCEL");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("BASAL CANCEL", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertTrue(smsCommunicatorPlugin.messages.get(1).text.contains("To stop temp basal reply with code"));
String passCode = smsCommunicatorPlugin.messageToConfirm.confirmCode;
smsCommunicatorPlugin.processSms(new Sms("1234", passCode));
Assert.assertEquals(passCode, smsCommunicatorPlugin.messages.get(2).text);
Assert.assertEquals("Temp basal canceled\nVirtual Pump", smsCommunicatorPlugin.messages.get(3).text);
//BASAL a%
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "BASAL a%");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("BASAL a%", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Wrong format", smsCommunicatorPlugin.messages.get(1).text);
//BASAL 10% 0
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "BASAL 10% 0");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("BASAL 10% 0", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Wrong format", smsCommunicatorPlugin.messages.get(1).text);
when(MainApp.getConstraintChecker().applyBasalPercentConstraints(any(), any())).thenReturn(new Constraint<>(20));
//BASAL 20% 20
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "BASAL 20% 20");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("BASAL 20% 20", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertTrue(smsCommunicatorPlugin.messages.get(1).text.contains("To start basal 20% for 20 min reply with code"));
passCode = smsCommunicatorPlugin.messageToConfirm.confirmCode;
smsCommunicatorPlugin.processSms(new Sms("1234", passCode));
Assert.assertEquals(passCode, smsCommunicatorPlugin.messages.get(2).text);
Assert.assertEquals("Temp basal 20% for 20 min started successfully\nVirtual Pump", smsCommunicatorPlugin.messages.get(3).text);
//BASAL a
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "BASAL a");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("BASAL a", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Wrong format", smsCommunicatorPlugin.messages.get(1).text);
//BASAL 1 0
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "BASAL 1 0");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("BASAL 1 0", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Wrong format", smsCommunicatorPlugin.messages.get(1).text);
when(MainApp.getConstraintChecker().applyBasalConstraints(any(), any())).thenReturn(new Constraint<>(1d));
//BASAL 1 20
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "BASAL 1 20");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("BASAL 1 20", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertTrue(smsCommunicatorPlugin.messages.get(1).text.contains("To start basal 1.00U/h for 20 min reply with code"));
passCode = smsCommunicatorPlugin.messageToConfirm.confirmCode;
smsCommunicatorPlugin.processSms(new Sms("1234", passCode));
Assert.assertEquals(passCode, smsCommunicatorPlugin.messages.get(2).text);
Assert.assertEquals("Temp basal 1.00U/h for 20 min started successfully\nVirtual Pump", smsCommunicatorPlugin.messages.get(3).text);
}
@Test
public void processExtendedTest() {
Sms sms;
//EXTENDED
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "EXTENDED");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("EXTENDED", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Remote command is not allowed", smsCommunicatorPlugin.messages.get(1).text);
when(SP.getBoolean(R.string.key_smscommunicator_remotecommandsallowed, false)).thenReturn(true);
//EXTENDED
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "EXTENDED");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("EXTENDED", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Wrong format", smsCommunicatorPlugin.messages.get(1).text);
//EXTENDED CANCEL
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "EXTENDED CANCEL");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("EXTENDED CANCEL", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertTrue(smsCommunicatorPlugin.messages.get(1).text.contains("To stop extended bolus reply with code"));
String passCode = smsCommunicatorPlugin.messageToConfirm.confirmCode;
smsCommunicatorPlugin.processSms(new Sms("1234", passCode));
Assert.assertEquals(passCode, smsCommunicatorPlugin.messages.get(2).text);
Assert.assertEquals("Extended bolus canceled\nVirtual Pump", smsCommunicatorPlugin.messages.get(3).text);
//EXTENDED a%
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "EXTENDED a%");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("EXTENDED a%", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Wrong format", smsCommunicatorPlugin.messages.get(1).text);
when(MainApp.getConstraintChecker().applyExtendedBolusConstraints(any())).thenReturn(new Constraint<>(1d));
//EXTENDED 1 0
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "EXTENDED 1 0");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("EXTENDED 1 0", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Wrong format", smsCommunicatorPlugin.messages.get(1).text);
//EXTENDED 1 20
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "EXTENDED 1 20");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("EXTENDED 1 20", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertTrue(smsCommunicatorPlugin.messages.get(1).text.contains("To start extended bolus 1.00U for 20 min reply with code"));
passCode = smsCommunicatorPlugin.messageToConfirm.confirmCode;
smsCommunicatorPlugin.processSms(new Sms("1234", passCode));
Assert.assertEquals(passCode, smsCommunicatorPlugin.messages.get(2).text);
Assert.assertEquals("Extended bolus 1.00U for 20 min started successfully\nVirtual Pump", smsCommunicatorPlugin.messages.get(3).text);
}
@Test
public void processBolusTest() {
Sms sms;
//BOLUS
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "BOLUS");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("BOLUS", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Remote command is not allowed", smsCommunicatorPlugin.messages.get(1).text);
when(SP.getBoolean(R.string.key_smscommunicator_remotecommandsallowed, false)).thenReturn(true);
//BOLUS
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "BOLUS");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("BOLUS", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Wrong format", smsCommunicatorPlugin.messages.get(1).text);
when(MainApp.getConstraintChecker().applyBolusConstraints(any())).thenReturn(new Constraint<>(1d));
when(DateUtil.now()).thenReturn(1000L);
//BOLUS 1
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "BOLUS 1");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("BOLUS 1", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Remote bolus not available. Try again later.", smsCommunicatorPlugin.messages.get(1).text);
when(MainApp.getConstraintChecker().applyBolusConstraints(any())).thenReturn(new Constraint<>(0d));
when(DateUtil.now()).thenReturn(Constants.remoteBolusMinDistance + 1002L);
//BOLUS 0
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "BOLUS 0");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("BOLUS 0", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Wrong format", smsCommunicatorPlugin.messages.get(1).text);
//BOLUS a
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "BOLUS a");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("BOLUS a", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Wrong format", smsCommunicatorPlugin.messages.get(1).text);
when(MainApp.getConstraintChecker().applyExtendedBolusConstraints(any())).thenReturn(new Constraint<>(1d));
when(MainApp.getConstraintChecker().applyBolusConstraints(any())).thenReturn(new Constraint<>(1d));
//BOLUS 1
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "BOLUS 1");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("BOLUS 1", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertTrue(smsCommunicatorPlugin.messages.get(1).text.contains("To deliver bolus 1.00U reply with code"));
String passCode = smsCommunicatorPlugin.messageToConfirm.confirmCode;
smsCommunicatorPlugin.processSms(new Sms("1234", passCode));
Assert.assertEquals(passCode, smsCommunicatorPlugin.messages.get(2).text);
Assert.assertEquals("Bolus 1.00U delivered successfully\nVirtual Pump", smsCommunicatorPlugin.messages.get(3).text);
//BOLUS 1 (Suspended pump)
smsCommunicatorPlugin.lastRemoteBolusTime = 0;
PumpInterface pump = mock(VirtualPumpPlugin.class);
when(ConfigBuilderPlugin.getPlugin().getActivePump()).thenReturn(pump);
when(pump.isSuspended()).thenReturn(true);
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "BOLUS 1");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("BOLUS 1", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Pump suspended", smsCommunicatorPlugin.messages.get(1).text);
when(pump.isSuspended()).thenReturn(false);
}
@Test
public void processCalTest() {
Sms sms;
//CAL
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "CAL");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("CAL", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Remote command is not allowed", smsCommunicatorPlugin.messages.get(1).text);
when(SP.getBoolean(R.string.key_smscommunicator_remotecommandsallowed, false)).thenReturn(true);
//CAL
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "CAL");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("CAL", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Wrong format", smsCommunicatorPlugin.messages.get(1).text);
//CAL 0
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "CAL 0");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("CAL 0", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Wrong format", smsCommunicatorPlugin.messages.get(1).text);
when(XdripCalibrations.sendIntent(any())).thenReturn(true);
//CAL 1
smsCommunicatorPlugin.messages = new ArrayList<>();
sms = new Sms("1234", "CAL 1");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("CAL 1", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertTrue(smsCommunicatorPlugin.messages.get(1).text.contains("To send calibration 1.00 reply with code"));
String passCode = smsCommunicatorPlugin.messageToConfirm.confirmCode;
smsCommunicatorPlugin.processSms(new Sms("1234", passCode));
Assert.assertEquals(passCode, smsCommunicatorPlugin.messages.get(2).text);
Assert.assertEquals("Calibration sent. Receiving must be enabled in xDrip.", smsCommunicatorPlugin.messages.get(3).text);
}
@Test
public void sendNotificationToAllNumbers() {
smsCommunicatorPlugin.messages = new ArrayList<>();
smsCommunicatorPlugin.sendNotificationToAllNumbers("abc");
Assert.assertEquals("abc", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("abc", smsCommunicatorPlugin.messages.get(1).text);
}
@Before
public void prepareTests() {
AAPSMocker.mockMainApp();
AAPSMocker.mockApplicationContext();
AAPSMocker.mockSP();
AAPSMocker.mockL();
AAPSMocker.mockStrings();
AAPSMocker.mockBus();
AAPSMocker.mockProfileFunctions();
AAPSMocker.mockTreatmentPlugin();
AAPSMocker.mockTreatmentService();
AAPSMocker.mockIobCobCalculatorPlugin();
AAPSMocker.mockConfigBuilder();
AAPSMocker.mockCommandQueue();
AAPSMocker.mockNSUpload();
AAPSMocker.mockConstraintsChecker();
BgReading reading = new BgReading();
reading.value = 100;
List<BgReading> bgList = new ArrayList<>();
bgList.add(reading);
PowerMockito.when(IobCobCalculatorPlugin.getPlugin().getBgReadings()).thenReturn(bgList);
PowerMockito.when(IobCobCalculatorPlugin.getPlugin().getCobInfo(false, "SMS COB")).thenReturn(new CobInfo(10d, 2d));
mockStatic(XdripCalibrations.class);
mockStatic(DateUtil.class);
mockStatic(SmsManager.class);
SmsManager smsManager = mock(SmsManager.class);
when(SmsManager.getDefault()).thenReturn(smsManager);
when(SP.getString(R.string.key_smscommunicator_allowednumbers, "")).thenReturn("1234;5678");
smsCommunicatorPlugin = SmsCommunicatorPlugin.getPlugin();
smsCommunicatorPlugin.setPluginEnabled(PluginType.GENERAL, true);
loopPlugin = mock(LoopPlugin.class);
when(MainApp.getSpecificPlugin(LoopPlugin.class)).thenReturn(loopPlugin);
Mockito.doAnswer(invocation -> {
Callback callback = invocation.getArgument(1);
callback.result = new PumpEnactResult().success(true);
callback.run();
return null;
}).when(AAPSMocker.queue).cancelTempBasal(anyBoolean(), any(Callback.class));
Mockito.doAnswer(invocation -> {
Callback callback = invocation.getArgument(0);
callback.result = new PumpEnactResult().success(true);
callback.run();
return null;
}).when(AAPSMocker.queue).cancelExtended(any(Callback.class));
Mockito.doAnswer(invocation -> {
Callback callback = invocation.getArgument(1);
callback.result = new PumpEnactResult().success(true);
callback.run();
return null;
}).when(AAPSMocker.queue).readStatus(anyString(), any(Callback.class));
Mockito.doAnswer(invocation -> {
Callback callback = invocation.getArgument(1);
callback.result = new PumpEnactResult().success(true).bolusDelivered(1);
callback.run();
return null;
}).when(AAPSMocker.queue).bolus(any(DetailedBolusInfo.class), any(Callback.class));
Mockito.doAnswer(invocation -> {
Callback callback = invocation.getArgument(4);
callback.result = new PumpEnactResult().success(true).isPercent(true).percent(invocation.getArgument(0)).duration(invocation.getArgument(1));
callback.run();
return null;
}).when(AAPSMocker.queue).tempBasalPercent(anyInt(), anyInt(), anyBoolean(), any(), any(Callback.class));
Mockito.doAnswer(invocation -> {
Callback callback = invocation.getArgument(4);
callback.result = new PumpEnactResult().success(true).isPercent(false).absolute(invocation.getArgument(0)).duration(invocation.getArgument(1));
callback.run();
return null;
}).when(AAPSMocker.queue).tempBasalAbsolute(anyDouble(), anyInt(), anyBoolean(), any(), any(Callback.class));
Mockito.doAnswer(invocation -> {
Callback callback = invocation.getArgument(2);
callback.result = new PumpEnactResult().success(true).isPercent(false).absolute(invocation.getArgument(0)).duration(invocation.getArgument(1));
callback.run();
return null;
}).when(AAPSMocker.queue).extendedBolus(anyDouble(), anyInt(), any(Callback.class));
VirtualPumpPlugin virtualPumpPlugin = VirtualPumpPlugin.getPlugin();
when(ConfigBuilderPlugin.getPlugin().getActivePump()).thenReturn(virtualPumpPlugin);
}
}

View file

@ -0,0 +1,53 @@
package info.nightscout.androidaps.plugins.general.smsCommunicator;
import android.telephony.SmsMessage;
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;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(PowerMockRunner.class)
@PrepareForTest({SmsMessage.class, MainApp.class})
public class SmsTest {
@Test
public void doTests() {
SmsMessage smsMessage = mock(SmsMessage.class);
when(smsMessage.getOriginatingAddress()).thenReturn("aNumber");
when(smsMessage.getMessageBody()).thenReturn("aBody");
Sms sms = new Sms(smsMessage);
Assert.assertEquals(sms.phoneNumber, "aNumber");
Assert.assertEquals(sms.text, "aBody");
Assert.assertTrue(sms.received);
sms = new Sms("aNumber", "aBody");
Assert.assertEquals(sms.phoneNumber, "aNumber");
Assert.assertEquals(sms.text, "aBody");
Assert.assertTrue(sms.sent);
sms = new Sms("aNumber", R.string.insulin_unit_shortname);
Assert.assertEquals(sms.phoneNumber, "aNumber");
Assert.assertEquals(sms.text, MainApp.gs(R.string.insulin_unit_shortname));
Assert.assertTrue(sms.sent);
Assert.assertEquals(sms.toString(), "SMS from aNumber: U");
}
@Before
public void prepareTests() {
AAPSMocker.mockMainApp();
AAPSMocker.mockStrings();
}
}

View file

@ -1,4 +1,4 @@
package info.nightscout.androidaps.plugins.IobCobCalculatorPlugin;
package info.nightscout.androidaps.plugins.iob.iobCobCalculatorPlugin;
import android.content.Context;