From 065192f11ee5eabdfc32c14f08288ec8194c76c0 Mon Sep 17 00:00:00 2001 From: Roumen Georgiev Date: Thu, 7 Mar 2019 14:49:07 +0200 Subject: [PATCH] added test for getAvailableInternalMemorySize() it took me some time, but it's OK now --- .../storage/StorageConstraintPluginTest.java | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/constraints/storage/StorageConstraintPluginTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/constraints/storage/StorageConstraintPluginTest.java index c9b2914b1c..5bc5cfacbe 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/constraints/storage/StorageConstraintPluginTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/constraints/storage/StorageConstraintPluginTest.java @@ -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(){ @@ -43,11 +53,29 @@ public class StorageConstraintPluginTest extends StorageConstraintPlugin{ 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); + mockedFile = mock(File.class); + mockedStatFs = mock(StatFs.class); storageConstraintPlugin = StorageConstraintPlugin.getPlugin(); } }