QuickWizardTest part1

This commit is contained in:
Milos Kozak 2018-04-09 00:37:23 +02:00
parent 05591cd762
commit 82bb65560b
3 changed files with 130 additions and 4 deletions

View file

@ -10,6 +10,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.MainApp;
import info.nightscout.utils.SP;
/**
* Created by mike on 12.10.2016.
@ -25,10 +26,7 @@ public class QuickWizard {
}
public void save() {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putString("QuickWizard", storage.toString());
editor.apply();
SP.putString("QuickWizard", storage.toString());
}
public int size() {

View file

@ -0,0 +1,45 @@
package info;
import org.junit.Assert;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito;
import java.util.HashMap;
import info.nightscout.utils.SP;
import static org.powermock.api.mockito.PowerMockito.when;
public class SPMocker {
static HashMap<String, Object> data = new HashMap<>();
public static void prepareMock() {
PowerMockito.mockStatic(SP.class);
try {
PowerMockito.when(SP.class, "putString", ArgumentMatchers.anyString(), ArgumentMatchers.anyString()).then(invocation -> {
String key = invocation.getArgument(0);
String value = invocation.getArgument(1);
data.put(key,value);
return null;
});
PowerMockito.when(SP.class, "getString", ArgumentMatchers.anyString(), ArgumentMatchers.anyString()).then(invocation -> {
String key = invocation.getArgument(0);
String def = invocation.getArgument(1);
String value = (String) data.get(key);
if (value == null) value = def;
return value;
});
} catch (Exception e) {
Assert.fail("Unable to mock the construction of "
+ "the SP object");
}
}
}

View file

@ -0,0 +1,83 @@
package info.nightscout.androidaps.data;
import junit.framework.Assert;
import org.json.JSONArray;
import org.json.JSONException;
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.SPMocker;
import info.nightscout.androidaps.MainApp;
import info.nightscout.utils.SP;
@RunWith(PowerMockRunner.class)
@PrepareForTest({SP.class, MainApp.class})
public class QuickWizardTest {
String data1 = "{\"buttonText\":\"Meal\",\"carbs\":36,\"validFrom\":0,\"validTo\":18000," +
"\"useBG\":0,\"useCOB\":0,\"useBolusIOB\":0,\"useBasalIOB\":0,\"useTrend\":0,\"useSuperBolus\":0,\"useTemptarget\":0}";
String data2 = "{\"buttonText\":\"Lunch\",\"carbs\":18,\"validFrom\":36000,\"validTo\":39600," +
"\"useBG\":0,\"useCOB\":0,\"useBolusIOB\":1,\"useBasalIOB\":2,\"useTrend\":0,\"useSuperBolus\":0,\"useTemptarget\":0}";
JSONArray array;
QuickWizard qv = new QuickWizard();
public QuickWizardTest() {
try {
array = new JSONArray("[" + data1 + "," + data2 + "]");
} catch (JSONException e) {
e.printStackTrace();
}
}
@Before
public void mock() {
AAPSMocker.mockMainApp();
SPMocker.prepareMock();
}
@Test
public void setDataTest() {
qv.setData(array);
Assert.assertEquals(2, qv.size());
}
@Test
public void saveTest() {
qv.setData(array);
qv.save();
Assert.assertEquals("[{\"useBolusIOB\":0,\"buttonText\":\"Meal\",\"useTrend\":0,\"carbs\":36,\"useCOB\":0,\"useBasalIOB\":0,\"useTemptarget\":0,\"useBG\":0,\"validFrom\":0,\"useSuperBolus\":0,\"validTo\":18000},{\"useBolusIOB\":1,\"buttonText\":\"Lunch\",\"useTrend\":0,\"carbs\":18,\"useCOB\":0,\"useBasalIOB\":2,\"useTemptarget\":0,\"useBG\":0,\"validFrom\":36000,\"useSuperBolus\":0,\"validTo\":39600}]", SP.getString("QuickWizard", "d"));
}
@Test
public void getTest() {
qv.setData(array);
Assert.assertEquals("Lunch", qv.get(1).buttonText());
}
@Test
public void isActive() {
}
@Test
public void getActive() {
}
@Test
public void newEmptyItemTest() {
Assert.assertNotNull(qv.newEmptyItem());
}
@Test
public void addOrUpdate() {
}
@Test
public void remove() {
}
}