Action tests

This commit is contained in:
Milos Kozak 2020-03-01 22:59:47 +01:00
parent 09176a1b9e
commit 15ffb5b596
3 changed files with 106 additions and 98 deletions

View file

@ -78,7 +78,7 @@ public class LoopPlugin extends PluginBase {
private final ConstraintChecker constraintChecker;
private final ResourceHelper resourceHelper;
private final ProfileFunction profileFunction;
private final MainApp mainApp;
private final Context context;
private final CommandQueueProvider commandQueue;
private final ConfigBuilderPlugin configBuilderPlugin;
private final TreatmentsPlugin treatmentsPlugin;
@ -120,7 +120,7 @@ public class LoopPlugin extends PluginBase {
ConstraintChecker constraintChecker,
ResourceHelper resourceHelper,
ProfileFunction profileFunction,
MainApp mainApp,
Context context,
CommandQueueProvider commandQueue,
ConfigBuilderPlugin configBuilderPlugin,
TreatmentsPlugin treatmentsPlugin,
@ -142,7 +142,7 @@ public class LoopPlugin extends PluginBase {
this.constraintChecker = constraintChecker;
this.resourceHelper = resourceHelper;
this.profileFunction = profileFunction;
this.mainApp = mainApp;
this.context = context;
this.configBuilderPlugin = configBuilderPlugin;
this.commandQueue = commandQueue;
this.treatmentsPlugin = treatmentsPlugin;
@ -194,7 +194,7 @@ public class LoopPlugin extends PluginBase {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager mNotificationManager =
(NotificationManager) mainApp.getSystemService(Context.NOTIFICATION_SERVICE);
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
@SuppressLint("WrongConstant") NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
CHANNEL_ID,
NotificationManager.IMPORTANCE_HIGH);
@ -448,7 +448,7 @@ public class LoopPlugin extends PluginBase {
} else {
if (resultAfterConstraints.isChangeRequested() && allowNotification) {
NotificationCompat.Builder builder =
new NotificationCompat.Builder(mainApp, CHANNEL_ID);
new NotificationCompat.Builder(context, CHANNEL_ID);
builder.setSmallIcon(R.drawable.notif_icon)
.setContentTitle(resourceHelper.gs(R.string.openloop_newsuggestion))
.setContentText(resultAfterConstraints.toString())
@ -461,13 +461,13 @@ public class LoopPlugin extends PluginBase {
}
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(mainApp, MainActivity.class);
Intent resultIntent = new Intent(context, MainActivity.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(mainApp);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
@ -476,7 +476,7 @@ public class LoopPlugin extends PluginBase {
builder.setContentIntent(resultPendingIntent);
builder.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
NotificationManager mNotificationManager =
(NotificationManager) mainApp.getSystemService(Context.NOTIFICATION_SERVICE);
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(Constants.notificationID, builder.build());
rxBus.send(new EventNewOpenLoopNotification());
@ -486,7 +486,7 @@ public class LoopPlugin extends PluginBase {
} else if (allowNotification) {
// dismiss notifications
NotificationManager notificationManager =
(NotificationManager) mainApp.getSystemService(Context.NOTIFICATION_SERVICE);
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(Constants.notificationID);
actionStringHandler.get().handleInitiate("cancelChangeRequest");
}
@ -680,12 +680,12 @@ public class LoopPlugin extends PluginBase {
@Override
public void run() {
if (!result.success) {
Intent i = new Intent(mainApp, ErrorHelperActivity.class);
Intent i = new Intent(context, ErrorHelperActivity.class);
i.putExtra("soundid", R.raw.boluserror);
i.putExtra("status", result.comment);
i.putExtra("title", resourceHelper.gs(R.string.tempbasaldeliveryerror));
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mainApp.startActivity(i);
context.startActivity(i);
}
}
});
@ -694,12 +694,12 @@ public class LoopPlugin extends PluginBase {
@Override
public void run() {
if (!result.success) {
Intent i = new Intent(mainApp, ErrorHelperActivity.class);
Intent i = new Intent(context, ErrorHelperActivity.class);
i.putExtra("soundid", R.raw.boluserror);
i.putExtra("status", result.comment);
i.putExtra("title", resourceHelper.gs(R.string.tempbasaldeliveryerror));
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mainApp.startActivity(i);
context.startActivity(i);
}
}
});
@ -710,12 +710,12 @@ public class LoopPlugin extends PluginBase {
@Override
public void run() {
if (!result.success) {
Intent i = new Intent(mainApp, ErrorHelperActivity.class);
Intent i = new Intent(context, ErrorHelperActivity.class);
i.putExtra("soundid", R.raw.boluserror);
i.putExtra("status", result.comment);
i.putExtra("title", resourceHelper.gs(R.string.extendedbolusdeliveryerror));
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mainApp.startActivity(i);
context.startActivity(i);
}
}
});
@ -729,12 +729,12 @@ public class LoopPlugin extends PluginBase {
@Override
public void run() {
if (!result.success) {
Intent i = new Intent(mainApp, ErrorHelperActivity.class);
Intent i = new Intent(context, ErrorHelperActivity.class);
i.putExtra("soundid", R.raw.boluserror);
i.putExtra("status", result.comment);
i.putExtra("title", resourceHelper.gs(R.string.tempbasaldeliveryerror));
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mainApp.startActivity(i);
context.startActivity(i);
}
}
});

View file

@ -1,81 +0,0 @@
package info.nightscout.androidaps.plugins.general.automation.actions;
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.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.interfaces.PluginType;
import info.nightscout.androidaps.interfaces.PumpDescription;
import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin;
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.pump.virtual.VirtualPumpPlugin;
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.when;
@RunWith(PowerMockRunner.class)
@PrepareForTest({MainApp.class, ConfigBuilderPlugin.class, SP.class})
public class ActionLoopDisableTest {
ActionLoopDisable actionLoopDisable = new ActionLoopDisable();
@Test
public void friendlyNameTest() {
Assert.assertEquals(R.string.disableloop, actionLoopDisable.friendlyName());
}
@Test
public void shortDescriptionTest() {
Assert.assertEquals("Disable loop", actionLoopDisable.shortDescription());
}
@Test
public void iconTest() {
Assert.assertEquals(Optional.of(R.drawable.ic_stop_24dp), actionLoopDisable.icon());
}
@Test
public void doActionTest() {
LoopPlugin.getPlugin().setPluginEnabled(PluginType.LOOP, true);
actionLoopDisable.doAction(new Callback() {
@Override
public void run() {
}
});
Assert.assertEquals(false, LoopPlugin.getPlugin().isEnabled(PluginType.LOOP));
// another call should keep it disabled
actionLoopDisable.doAction(new Callback() {
@Override
public void run() {
}
});
Assert.assertEquals(false, LoopPlugin.getPlugin().isEnabled(PluginType.LOOP));
}
@Before
public void prepareTest() {
AAPSMocker.mockMainApp();
AAPSMocker.mockApplicationContext();
AAPSMocker.mockSP();
AAPSMocker.mockConfigBuilder();
AAPSMocker.mockCommandQueue();
AAPSMocker.mockStrings();
VirtualPumpPlugin pump = mock(VirtualPumpPlugin.class);
when(pump.specialEnableCondition()).thenReturn(true);
PumpDescription pumpDescription = new PumpDescription();
pumpDescription.isTempBasalCapable = true;
when(pump.getPumpDescription()).thenReturn(pumpDescription);
when(AAPSMocker.configBuilderPlugin.getActivePump()).thenReturn(pump);
}
}

View file

@ -0,0 +1,89 @@
package info.nightscout.androidaps.plugins.general.automation.actions
import android.content.Context
import com.google.common.base.Optional
import dagger.Lazy
import info.AAPSMocker
import info.nightscout.androidaps.R
import info.nightscout.androidaps.interfaces.CommandQueueProvider
import info.nightscout.androidaps.interfaces.PluginType
import info.nightscout.androidaps.interfaces.PumpDescription
import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin
import info.nightscout.androidaps.plugins.configBuilder.ConstraintChecker
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunction
import info.nightscout.androidaps.plugins.general.wear.ActionStringHandler
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin
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.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.sharedPreferences.SP
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.powermock.api.mockito.PowerMockito
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.PowerMockRunner
@RunWith(PowerMockRunner::class)
@PrepareForTest(ConstraintChecker::class, VirtualPumpPlugin::class)
class ActionLoopDisableTest : ActionTestBase() {
@Mock lateinit var rxBus: RxBusWrapper
@Mock lateinit var sp: SP
@Mock lateinit var constraintChecker: ConstraintChecker
@Mock lateinit var resourceHelper: ResourceHelper
@Mock lateinit var profileFunction: ProfileFunction
@Mock lateinit var context: Context
@Mock lateinit var commandQueue: CommandQueueProvider
@Mock lateinit var configBuilderPlugin: ConfigBuilderPlugin
@Mock lateinit var treatmentsPlugin: TreatmentsPlugin
@Mock lateinit var virtualPumpPlugin: VirtualPumpPlugin
@Mock lateinit var lazyActionStringHandler: Lazy<ActionStringHandler>
@Mock lateinit var iobCobCalculatorPlugin: IobCobCalculatorPlugin
lateinit var loopPlugin: LoopPlugin
lateinit var actionLoopDisable : ActionLoopDisable
@Test fun friendlyNameTest() {
Assert.assertEquals(R.string.disableloop.toLong(), actionLoopDisable.friendlyName().toLong())
}
@Test fun shortDescriptionTest() {
Assert.assertEquals("Disable loop", actionLoopDisable.shortDescription())
}
@Test fun iconTest() {
Assert.assertEquals(Optional.of(R.drawable.ic_stop_24dp), actionLoopDisable.icon())
}
@Test fun doActionTest() {
loopPlugin.setPluginEnabled(PluginType.LOOP, true)
actionLoopDisable.doAction(object : Callback() {
override fun run() {}
})
Assert.assertEquals(false, loopPlugin.isEnabled(PluginType.LOOP))
// another call should keep it disabled
actionLoopDisable.doAction(object : Callback() {
override fun run() {}
})
Assert.assertEquals(false, loopPlugin.isEnabled(PluginType.LOOP))
}
@Before fun prepareTest() {
actionLoopDisable = ActionLoopDisable(actionInjector)
val pump = PowerMockito.mock(VirtualPumpPlugin::class.java)
PowerMockito.`when`(pump.specialEnableCondition()).thenReturn(true)
val pumpDescription = PumpDescription()
pumpDescription.isTempBasalCapable = true
PowerMockito.`when`(pump.pumpDescription).thenReturn(pumpDescription)
PowerMockito.`when`(configBuilderPlugin.activePump).thenReturn(pump)
loopPlugin = LoopPlugin(aapsLogger, rxBus, sp, constraintChecker, resourceHelper, profileFunction, context, commandQueue, configBuilderPlugin, treatmentsPlugin, virtualPumpPlugin, lazyActionStringHandler, iobCobCalculatorPlugin)
}
}