ActionProfileSwitchPercent

This commit is contained in:
Milos Kozak 2019-04-21 23:48:05 +02:00
parent 42cd2ddaa3
commit 45312d9ffe
6 changed files with 178 additions and 2 deletions

View file

@ -0,0 +1,86 @@
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 info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
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.LayoutBuilder;
import info.nightscout.androidaps.queue.Callback;
import info.nightscout.androidaps.utils.JsonHelper;
public class ActionProfileSwitchPercent extends Action {
InputPercent pct = new InputPercent();
InputDuration duration = new InputDuration(0, InputDuration.TimeUnit.MINUTES);
@Override
public int friendlyName() {
return R.string.profilepercentage;
}
@Override
public String shortDescription() {
return MainApp.gs(R.string.startprofile, (int) pct.getValue(), (int) duration.getValue());
}
@Override
public void doAction(Callback callback) {
ProfileFunctions.doProfileSwitch((int) duration.getValue(), (int) pct.getValue(), 0);
if (callback != null)
callback.result(new PumpEnactResult().success(true).comment(R.string.ok)).run();
}
@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))
.build(root);
}
@Override
public boolean hasDialog() {
return true;
}
@Override
public String toJSON() {
JSONObject o = new JSONObject();
try {
o.put("type", ActionProfileSwitchPercent.class.getName());
JSONObject data = new JSONObject();
data.put("percentage", pct.getValue());
data.put("durationInMinutes", duration.getMinutes());
o.put("data", data);
} catch (JSONException e) {
e.printStackTrace();
}
return o.toString();
}
@Override
public Action fromJSON(String data) {
try {
JSONObject d = new JSONObject(data);
pct.setValue(JsonHelper.safeGetInt(d, "percentage"));
duration.setMinutes(JsonHelper.safeGetInt(d, "durationInMinutes"));
} catch (JSONException e) {
e.printStackTrace();
}
return this;
}
@Override
public Optional<Integer> icon() {
return Optional.of(R.drawable.remove);
}
}

View file

@ -66,7 +66,7 @@ public class ActionStartTempTarget extends Action {
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), "min", duration))
.add(new Label(MainApp.gs(R.string.careportal_newnstreatment_duration_min_label), "", duration))
.build(root);
}

View file

@ -23,6 +23,7 @@ import info.nightscout.androidaps.plugins.general.automation.actions.ActionLoopE
import info.nightscout.androidaps.plugins.general.automation.actions.ActionLoopResume;
import info.nightscout.androidaps.plugins.general.automation.actions.ActionLoopSuspend;
import info.nightscout.androidaps.plugins.general.automation.actions.ActionNotification;
import info.nightscout.androidaps.plugins.general.automation.actions.ActionProfileSwitchPercent;
import info.nightscout.androidaps.plugins.general.automation.actions.ActionStartTempTarget;
import info.nightscout.androidaps.plugins.general.automation.actions.ActionStopTempTarget;
@ -42,6 +43,7 @@ public class ChooseActionDialog extends DialogFragment {
add(new ActionStartTempTarget());
add(new ActionStopTempTarget());
add(new ActionNotification());
add(new ActionProfileSwitchPercent());
}};
private Unbinder mUnbinder;

View file

@ -1395,6 +1395,7 @@
<string name="message_short">Msg:</string>
<string name="profilepercentage">Profile percentage</string>
<string name="percent_u">Percent [%]:</string>
<string name="startprofile">Start profile %1$d%% for %2$d min</string>
<plurals name="objective_days">
<item quantity="one">%1$d day</item>

View file

@ -57,6 +57,7 @@ public class AAPSMocker {
public static CommandQueue queue;
public static ConfigBuilderPlugin configBuilderPlugin;
public static ProfileFunctions profileFunctions;
public static void mockStrings() {
Locale.setDefault(new Locale("en", "US"));
@ -278,7 +279,7 @@ public class AAPSMocker {
public static void mockProfileFunctions() {
PowerMockito.mockStatic(ProfileFunctions.class);
ProfileFunctions profileFunctions = PowerMockito.mock(ProfileFunctions.class);
profileFunctions = PowerMockito.mock(ProfileFunctions.class);
PowerMockito.when(ProfileFunctions.getInstance()).thenReturn(profileFunctions);
profile = getValidProfile();
PowerMockito.when(ProfileFunctions.getInstance().getProfile()).thenReturn(profile);

View file

@ -0,0 +1,86 @@
package info.nightscout.androidaps.plugins.general.automation.actions;
import com.google.common.base.Optional;
import com.squareup.otto.Bus;
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 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.queue.Callback;
import info.nightscout.androidaps.utils.SP;
@RunWith(PowerMockRunner.class)
@PrepareForTest({MainApp.class, SP.class, Bus.class, ProfileFunctions.class})
public class ActionProfileSwitchPercentTest {
private ActionProfileSwitchPercent actionProfileSwitchPercent;
@Test
public void friendlyNameTest() {
Assert.assertEquals(R.string.profilepercentage, actionProfileSwitchPercent.friendlyName());
}
@Test
public void shortDescriptionTest() {
actionProfileSwitchPercent = new ActionProfileSwitchPercent();
actionProfileSwitchPercent.pct = new InputPercent().setValue(100);
actionProfileSwitchPercent.duration = new InputDuration(30, InputDuration.TimeUnit.MINUTES);
Assert.assertNull(actionProfileSwitchPercent.shortDescription()); // not mocked
}
@Test
public void iconTest() {
Assert.assertEquals(Optional.of(R.drawable.remove), actionProfileSwitchPercent.icon());
}
@Test
public void doActionTest() {
actionProfileSwitchPercent.doAction(new Callback() {
@Override
public void run() {
Assert.assertTrue(result.success);
}
});
}
@Test
public void hasDialogTest() {
Assert.assertTrue(actionProfileSwitchPercent.hasDialog());
}
@Test
public void toJSONTest() {
actionProfileSwitchPercent = new ActionProfileSwitchPercent();
actionProfileSwitchPercent.pct = new InputPercent().setValue(100);
actionProfileSwitchPercent.duration = new InputDuration(30, InputDuration.TimeUnit.MINUTES);
Assert.assertEquals("{\"data\":{\"percentage\":100,\"durationInMinutes\":30},\"type\":\"info.nightscout.androidaps.plugins.general.automation.actions.ActionProfileSwitchPercent\"}", actionProfileSwitchPercent.toJSON());
}
@Test
public void fromJSONTest() {
actionProfileSwitchPercent = new ActionProfileSwitchPercent();
actionProfileSwitchPercent.fromJSON("{\"percentage\":100,\"durationInMinutes\":30}");
Assert.assertEquals(100, actionProfileSwitchPercent.pct.getValue(), 0.001d);
Assert.assertEquals(30, actionProfileSwitchPercent.duration.getMinutes(), 0.001);
}
@Before
public void prepareTest() {
AAPSMocker.mockMainApp();
AAPSMocker.mockSP();
AAPSMocker.mockBus();
AAPSMocker.mockStrings();
AAPSMocker.mockProfileFunctions();
actionProfileSwitchPercent = new ActionProfileSwitchPercent();
}
}