Copy&paste from ActionNotification

This commit is contained in:
Roumen Georgiev 2019-10-04 10:55:33 +03:00
parent 98f874d1fe
commit e12217ae69
4 changed files with 180 additions and 1 deletions

View file

@ -202,7 +202,8 @@ object AutomationPlugin : PluginBase(PluginDescription()
ActionStopTempTarget(),
ActionNotification(),
ActionProfileSwitchPercent(),
ActionProfileSwitch()
ActionProfileSwitch(),
ActionSendSMS()
)
}

View file

@ -0,0 +1,88 @@
package info.nightscout.androidaps.plugins.general.automation.actions;
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.data.PumpEnactResult;
import info.nightscout.androidaps.logging.L;
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.smsCommunicator.SmsCommunicatorPlugin;
import info.nightscout.androidaps.queue.Callback;
import info.nightscout.androidaps.utils.JsonHelper;
public class ActionSendSMS extends Action {
public InputString text = new InputString();
@Override
public int friendlyName() {
return R.string.sendsmsactiondescription;
}
@Override
public String shortDescription() {
return MainApp.gs(R.string.sendsmsactionlabel);
}
@Override
public void doAction(Callback callback) {
SmsCommunicatorPlugin.getPlugin().sendNotificationToAllNumbers(text.getValue());
if (callback != null)
callback.result(new PumpEnactResult().success(true).comment(R.string.ok)).run();
}
@Override
public Optional<Integer> icon() {
return Optional.of(R.drawable.ic_notifications);
}
@Override
public String toJSON() {
JSONObject o = new JSONObject();
JSONObject data = new JSONObject();
try {
data.put("text", text.getValue());
o.put("type", this.getClass().getName());
o.put("data", data);
} catch (JSONException e) {
e.printStackTrace();
}
return o.toString();
}
@Override
public Action fromJSON(String data) {
try {
JSONObject o = new JSONObject(data);
text.setValue(JsonHelper.safeGetString(o, "text"));
} catch (JSONException e) {
e.printStackTrace();
}
return this;
}
@Override
public boolean hasDialog() {
return true;
}
@Override
public void generateDialog(LinearLayout root) {
new LayoutBuilder()
.add(new LabelWithElement(MainApp.gs(R.string.sendsmsactiontext), "", text))
.build(root);
}
}

View file

@ -1574,6 +1574,9 @@
<string name="medtronic_cmd_desc_set_bolus">Set Bolus</string>
<string name="profilename">Change profile to</string>
<string name="changengetoprofilename">Change profile to %1$s</string>
<string name="sendsmsactionlabel">Send SMS</string>
<string name="sendsmsactiondescription">Send SMS to all numbers in preferences</string>
<string name="sendsmsactiontext">Send SMS with text</string>
<string name="insulinFromCob"><![CDATA[COB vs IOB: <font color=\'%1$s\'>%2$+.2fU</font>]]></string>

View file

@ -0,0 +1,87 @@
package info.nightscout.androidaps.plugins.general.automation.actions;
import android.telephony.SmsManager;
import com.google.common.base.Optional;
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.plugins.general.automation.elements.InputString;
import info.nightscout.androidaps.queue.Callback;
import info.nightscout.androidaps.utils.SP;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
@RunWith(PowerMockRunner.class)
@PrepareForTest({MainApp.class, SP.class, SmsManager.class})
public class ActionSendSMSTest {
private ActionSendSMS actionSendSMS;
@Test
public void friendlyNameTest() {
Assert.assertEquals(R.string.sendsmsactiondescription, actionSendSMS.friendlyName());
}
@Test
public void shortDescriptionTest() {
actionSendSMS = new ActionSendSMS();
Assert.assertEquals(null, actionSendSMS.shortDescription()); // not mocked
}
@Test
public void iconTest() {
Assert.assertEquals(Optional.of(R.drawable.ic_notifications), actionSendSMS.icon());
}
@Test
public void doActionTest() {
actionSendSMS.text = new InputString().setValue("Asd");
actionSendSMS.doAction(new Callback() {
@Override
public void run() {
Assert.assertTrue(result.success);
}
});
}
@Test
public void hasDialogTest() {
Assert.assertTrue(actionSendSMS.hasDialog());
}
@Test
public void toJSONTest() {
actionSendSMS = new ActionSendSMS();
actionSendSMS.text = new InputString().setValue("Asd");
Assert.assertEquals("{\"data\":{\"text\":\"Asd\"},\"type\":\"info.nightscout.androidaps.plugins.general.automation.actions.ActionSendSMS\"}", actionSendSMS.toJSON());
}
@Test
public void fromJSONTest() {
actionSendSMS = new ActionSendSMS();
actionSendSMS.fromJSON("{\"text\":\"Asd\"}");
Assert.assertEquals("Asd", actionSendSMS.text.getValue());
}
@Before
public void prepareTest() {
AAPSMocker.mockMainApp();
AAPSMocker.mockBus();
AAPSMocker.mockSP();
mockStatic(SmsManager.class);
SmsManager smsManager = mock(SmsManager.class);
PowerMockito.when(SmsManager.getDefault()).thenReturn(smsManager);
PowerMockito.when(SP.getString(R.string.key_smscommunicator_allowednumbers, "")).thenReturn("1234;5678");
actionSendSMS = new ActionSendSMS();
}
}