From 82bb65560bc3dc4ad62e1cd1613b4ae0ccf037ca Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 9 Apr 2018 00:37:23 +0200 Subject: [PATCH] QuickWizardTest part1 --- .../androidaps/data/QuickWizard.java | 6 +- app/src/test/java/info/SPMocker.java | 45 ++++++++++ .../androidaps/data/QuickWizardTest.java | 83 +++++++++++++++++++ 3 files changed, 130 insertions(+), 4 deletions(-) create mode 100644 app/src/test/java/info/SPMocker.java create mode 100644 app/src/test/java/info/nightscout/androidaps/data/QuickWizardTest.java diff --git a/app/src/main/java/info/nightscout/androidaps/data/QuickWizard.java b/app/src/main/java/info/nightscout/androidaps/data/QuickWizard.java index 587d7f86a2..25c768cdda 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/QuickWizard.java +++ b/app/src/main/java/info/nightscout/androidaps/data/QuickWizard.java @@ -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() { diff --git a/app/src/test/java/info/SPMocker.java b/app/src/test/java/info/SPMocker.java new file mode 100644 index 0000000000..29fbdaa230 --- /dev/null +++ b/app/src/test/java/info/SPMocker.java @@ -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 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"); + } + + } + +} diff --git a/app/src/test/java/info/nightscout/androidaps/data/QuickWizardTest.java b/app/src/test/java/info/nightscout/androidaps/data/QuickWizardTest.java new file mode 100644 index 0000000000..9a12a2a255 --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/data/QuickWizardTest.java @@ -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() { + } +} \ No newline at end of file