Merge branch 'dev' into smsrefactor

This commit is contained in:
Milos Kozak 2019-03-11 23:11:33 +01:00
commit f25b629fbc
2 changed files with 31 additions and 5 deletions

View file

@ -104,7 +104,6 @@ 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.diskfull)).thenReturn("Free at least 200Mb from internal storage! Loop disabled!");
}
public static MainApp mockMainApp() {

View file

@ -1,4 +1,7 @@
package info.nightscout.androidaps.plugins.constraints.storage;
import android.os.Environment;
import android.os.StatFs;
import junit.framework.Assert;
import org.junit.Before;
@ -8,6 +11,8 @@ import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.io.File;
import info.AAPSMocker;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
@ -15,16 +20,21 @@ import info.nightscout.androidaps.interfaces.Constraint;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.mock;
import static org.powermock.api.mockito.PowerMockito.whenNew;
import static org.mockito.ArgumentMatchers.any;
/**
* Created by Rumen on 06.03.2019.
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({MainApp.class, StorageConstraintPlugin.class})
@PrepareForTest({MainApp.class, StorageConstraintPlugin.class, StatFs.class, Environment.class})
public class StorageConstraintPluginTest extends StorageConstraintPlugin{
StorageConstraintPlugin storageConstraintPlugin;
private File mockedFile;
private static final String path = "/data";
private StatFs mockedStatFs;
@Test
public void isLoopInvocationAllowedTest(){
@ -33,21 +43,38 @@ public class StorageConstraintPluginTest extends StorageConstraintPlugin{
when(StorageConstraintPlugin.getAvailableInternalMemorySize()).thenReturn(150L);
Constraint<Boolean> c = new Constraint<>(true);
c = storageConstraintPlugin.isClosedLoopAllowed(c);
Assert.assertEquals(true, c.getReasons().contains(MainApp.gs(R.string.diskfull)));
Assert.assertEquals(Boolean.FALSE, c.value());
// Set free space over 200(Mb) to enable loop
when(StorageConstraintPlugin.getAvailableInternalMemorySize()).thenReturn(300L);
Constraint<Boolean> c2 = new Constraint<>(true);
c2 = storageConstraintPlugin.isClosedLoopAllowed(c2);
Assert.assertEquals(false, c2.getReasons().contains(MainApp.gs(R.string.diskfull)));
Assert.assertEquals(Boolean.TRUE, c2.value());
}
@Test
public void getAvailableInternalMemorySizeTest() throws Exception {
PowerMockito.mockStatic(Environment.class);
PowerMockito.when(Environment.getDataDirectory()).thenReturn(mockedFile);
when(mockedFile.getPath()).thenReturn(path);
when(mockedFile.exists()).thenReturn(true);
whenNew(StatFs.class).withArguments(any()).thenReturn(mockedStatFs);
when(mockedStatFs.getBlockSizeLong()).thenReturn(1024L);
when(mockedStatFs.getAvailableBlocksLong()).thenReturn(150l*1024);
long freeSpaceInMb = storageConstraintPlugin.getAvailableInternalMemorySize();
Assert.assertEquals(150L, freeSpaceInMb);
}
@Before
public void prepareMock() {
AAPSMocker.mockMainApp();
AAPSMocker.mockStrings();
// PowerMockito.mockStatic(Environment.class);
AAPSMocker.mockBus();
mockedFile = mock(File.class);
mockedStatFs = mock(StatFs.class);
storageConstraintPlugin = StorageConstraintPlugin.getPlugin();
}
}