From 1622b9cd39183c3088999bf463c93b79bfafe8da Mon Sep 17 00:00:00 2001 From: Benjamin Mwalimu Date: Tue, 8 Sep 2020 11:53:11 +0300 Subject: [PATCH 1/6] :ok_hand: Adding unit tests for the date picker & time picker factory --- .../widgets/DatePickerFactoryTest.java | 62 ++++++------------ .../widgets/TimePickerFactoryTest.java | 63 ++++++++----------- 2 files changed, 45 insertions(+), 80 deletions(-) diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/DatePickerFactoryTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/DatePickerFactoryTest.java index 9083e0ec0..f255164f9 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/DatePickerFactoryTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/DatePickerFactoryTest.java @@ -1,9 +1,7 @@ package com.vijay.jsonwizard.widgets; -import android.content.res.Resources; import android.view.View; import android.widget.RelativeLayout; -import android.widget.TextView; import com.rengwuxian.materialedittext.MaterialEditText; import com.vijay.jsonwizard.BaseTest; @@ -17,73 +15,53 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import org.mockito.ArgumentMatchers; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; +import org.powermock.reflect.Whitebox; +import org.robolectric.Robolectric; import java.util.List; -import java.util.Locale; import java.util.Set; public class DatePickerFactoryTest extends BaseTest { private DatePickerFactory factory; - @Mock - private JsonFormActivity context; - + private FormUtils formUtils; + private JsonFormActivity jsonFormActivity; @Mock private JsonFormFragment formFragment; - - @Mock - private Resources resources; - @Mock private CommonListener listener; - @Mock - private RelativeLayout rootLayout; - - @Mock - private MaterialEditText editText; - - @Mock - private TextView duration; - @Before public void setUp() { MockitoAnnotations.initMocks(this); factory = new DatePickerFactory(); + formUtils = new FormUtils(); + jsonFormActivity = Robolectric.buildActivity(JsonFormActivity.class, getJsonFormActivityIntent()).create().get(); } @Test public void testDatePickerFactoryInstantiatesViewsCorrectly() throws Exception { - Assert.assertNotNull(factory); - DatePickerFactory factorySpy = Mockito.spy(factory); - Assert.assertNotNull(factorySpy); - - FormUtils formUtils = new FormUtils(); + String datePicker = "{\"key\":\"First_Health_Facility_Contact\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"163260AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"openmrs_data_type\":\"text\",\"type\":\"date_picker\",\"hint\":\"Date first seen *\",\"expanded\":false,\"min_date\":\"today-5y\",\"max_date\":\"today\",\"v_required\":{\"value\":\"true\",\"err\":\"Enter the date that the child was first seen at a health facility for immunization services\"},\"constraints\":{\"type\":\"date\",\"ex\":\"greaterThanEqualTo(., step1:Date_Birth)\",\"err\":\"Date first seen can't occur before date of birth\"},\"relevance\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"sample-relevance-rules.yml\"}}},\"calculation\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"sample-calculation-rules.yml\"}}},\"value\":\"12-05-2020\",\"read_only\":true,\"label_info_text\":\"Just testing\",\"label_info_title\":\"Just testing\",\"duration\":{\"label\":\"AGE\"}}"; + Assert.assertNotNull(formUtils); FormUtils formUtilsSpy = Mockito.spy(formUtils); Assert.assertNotNull(formUtilsSpy); + Whitebox.setInternalState(factory, "formUtils", formUtilsSpy); - Mockito.doReturn(resources).when(context).getResources(); - Assert.assertNotNull(resources); - - context.setTheme(R.style.NativeFormsAppTheme); - Mockito.doReturn(rootLayout).when(factorySpy).getRelativeLayout(context); - Mockito.doReturn(editText).when(rootLayout).findViewById(R.id.edit_text); - Mockito.doReturn(duration).when(rootLayout).findViewById(R.id.duration); - Mockito.doReturn(duration).when(rootLayout).findViewById(R.id.duration); - Mockito.doReturn(Locale.ENGLISH).when(factorySpy).getCurrentLocale(context); - Mockito.doReturn("Age").when(duration).getTag(ArgumentMatchers.anyInt()); - Mockito.doReturn(Locale.ENGLISH).when(factorySpy).getSetLanguage(context); - Mockito.doReturn("12 Age").when(factorySpy).getDurationText(context, "12-05-2010", Locale.ENGLISH); - Mockito.doReturn(resources).when(context).getResources(); - Mockito.doReturn("%1$dw").when(resources).getString(ArgumentMatchers.anyInt()); - - String datePicker = "{\"key\":\"First_Health_Facility_Contact\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"163260AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"openmrs_data_type\":\"text\",\"type\":\"date_picker\",\"hint\":\"Date first seen *\",\"expanded\":false,\"min_date\":\"today-5y\",\"max_date\":\"today\",\"v_required\":{\"value\":\"true\",\"err\":\"Enter the date that the child was first seen at a health facility for immunization services\"},\"constraints\":{\"type\":\"date\",\"ex\":\"greaterThanEqualTo(., step1:Date_Birth)\",\"err\":\"Date first seen can't occur before date of birth\"},\"relevance\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"sample-relevance-rules.yml\"}}},\"calculation\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"sample-calculation-rules.yml\"}}},\"value\":\"12-05-2020\",\"read_only\":true,\"label_info_text\":\"Just testing\",\"label_info_title\":\"Just testing\",\"duration\":{\"label\":\"AGE\"}}"; - List viewList = factorySpy.getViewsFromJson("RandomStepName", context, formFragment, new JSONObject(datePicker), listener); + List viewList = factory.getViewsFromJson("RandomStepName", jsonFormActivity, formFragment, new JSONObject(datePicker), listener); Assert.assertNotNull(viewList); Assert.assertEquals(1, viewList.size()); + + View rootLayout = viewList.get(0); + Assert.assertEquals(3, ((RelativeLayout) rootLayout).getChildCount()); + + MaterialEditText materialEditText = (MaterialEditText) ((RelativeLayout) rootLayout).getChildAt(0); + + Assert.assertEquals("First_Health_Facility_Contact", materialEditText.getTag(R.id.key)); + Assert.assertEquals("", materialEditText.getTag(R.id.openmrs_entity_parent)); + Assert.assertEquals("concept", materialEditText.getTag(R.id.openmrs_entity)); + Assert.assertEquals("163260AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", materialEditText.getTag(R.id.openmrs_entity_id)); } @Test diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/TimePickerFactoryTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/TimePickerFactoryTest.java index c1da4ac2e..428567cd9 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/TimePickerFactoryTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/TimePickerFactoryTest.java @@ -1,10 +1,8 @@ package com.vijay.jsonwizard.widgets; -import android.content.res.Resources; import android.text.Editable; import android.view.View; import android.widget.RelativeLayout; -import android.widget.TextView; import com.rengwuxian.materialedittext.MaterialEditText; import com.vijay.jsonwizard.BaseTest; @@ -17,75 +15,64 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import org.mockito.ArgumentMatchers; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.powermock.reflect.Whitebox; +import org.robolectric.Robolectric; import java.util.List; -import java.util.Locale; import java.util.Set; public class TimePickerFactoryTest extends BaseTest { private TimePickerFactory factory; - @Mock - private JsonFormActivity context; + private JsonFormActivity jsonFormActivity; @Mock private JsonFormFragment formFragment; - - @Mock - private Resources resources; - @Mock private CommonListener listener; - - @Mock - private RelativeLayout rootLayout; - - @Mock - private MaterialEditText editText; - @Mock - private TextView duration; + private View view; @Before public void setUp() { MockitoAnnotations.initMocks(this); factory = new TimePickerFactory(); + jsonFormActivity = Robolectric.buildActivity(JsonFormActivity.class, getJsonFormActivityIntent()).create().get(); } @Test public void testTimePickerFactoryInstantiatesViewsCorrectly() throws Exception { - Assert.assertNotNull(factory); - TimePickerFactory factorySpy = Mockito.spy(factory); - - Mockito.doReturn(resources).when(context).getResources(); - Assert.assertNotNull(resources); - - context.setTheme(R.style.NativeFormsAppTheme); - Mockito.doReturn(rootLayout).when(factorySpy).getRelativeLayout(context); - Mockito.doReturn(editText).when(rootLayout).findViewById(R.id.edit_text); - Mockito.doReturn(duration).when(rootLayout).findViewById(R.id.duration); - Mockito.doReturn(Locale.ENGLISH).when(factorySpy).getCurrentLocale(context); - Mockito.doReturn("hrs").when(duration).getTag(ArgumentMatchers.anyInt()); - String timePickerWidget = "{\"key\":\"user_time\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"type\":\"time_picker\",\"hint\":\"Birth Time\",\"expanded\":false,\"duration\":{\"label\":\"Birth Time\"},\"v_required\":{\"value\":true,\"err\":\"Please enter the time of birth\"},\"relevance\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"sample-relevance-rules.yml\"}}},\"constraints\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"sample-constraints-rules.yml\"}}},\"calculation\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"sample-calculation-rules.yml\"}}},\"value\":\"22:03\",\"read_only\":true,\"label_info_text\":\"Just testing\",\"label_info_title\":\"Just testing\"}"; - List viewList = factorySpy.getViewsFromJson("RandomStepName", context, formFragment, new JSONObject(timePickerWidget), listener); + List viewList = factory.getViewsFromJson("RandomStepName", jsonFormActivity, formFragment, new JSONObject(timePickerWidget), listener); Assert.assertNotNull(viewList); Assert.assertEquals(1, viewList.size()); + + View rootLayout = viewList.get(0); + Assert.assertEquals(2, ((RelativeLayout) rootLayout).getChildCount()); + + MaterialEditText materialEditText = (MaterialEditText) ((RelativeLayout) rootLayout).getChildAt(0); + + Assert.assertEquals("user_time", materialEditText.getTag(R.id.key)); + Assert.assertEquals("", materialEditText.getTag(R.id.openmrs_entity_parent)); + Assert.assertEquals("", materialEditText.getTag(R.id.openmrs_entity)); + Assert.assertEquals("", materialEditText.getTag(R.id.openmrs_entity_id)); } @Test public void testUpdateTimeText() throws Exception { - Assert.assertNotNull(factory); - TimePickerFactory factorySpy = Mockito.spy(factory); - Mockito.doReturn(rootLayout).when(factorySpy).getRelativeLayout(context); - Mockito.doReturn(editText).when(rootLayout).findViewById(R.id.edit_text); + String timePickerWidget = "{\"key\":\"user_time\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"type\":\"time_picker\",\"hint\":\"Birth Time\",\"expanded\":false,\"duration\":{\"label\":\"Birth Time\"},\"v_required\":{\"value\":true,\"err\":\"Please enter the time of birth\"},\"relevance\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"sample-relevance-rules.yml\"}}},\"constraints\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"sample-constraints-rules.yml\"}}},\"calculation\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"sample-calculation-rules.yml\"}}},\"value\":\"22:03\",\"read_only\":true,\"label_info_text\":\"Just testing\",\"label_info_title\":\"Just testing\"}"; + List viewList = factory.getViewsFromJson("RandomStepName", jsonFormActivity, formFragment, new JSONObject(timePickerWidget), listener); + Assert.assertNotNull(viewList); + + View rootLayout = viewList.get(0); + Assert.assertEquals(2, ((RelativeLayout) rootLayout).getChildCount()); + + MaterialEditText materialEditText = (MaterialEditText) ((RelativeLayout) rootLayout).getChildAt(0); + Mockito.doReturn(jsonFormActivity).when(formFragment).getContext(); Editable editable = new Editable.Factory().newEditable("23:03"); - Whitebox.invokeMethod(factorySpy, "updateTimeText", editText, 22, 3); - Mockito.doReturn(editable).when(editText).getText(); + Whitebox.invokeMethod(factory, "updateTimeText", materialEditText, 22, 3); Assert.assertEquals("23:03", editable.toString()); } From 2543491c24db862a900f73e25f48973825f3053e Mon Sep 17 00:00:00 2001 From: Benjamin Mwalimu Date: Tue, 8 Sep 2020 12:04:09 +0300 Subject: [PATCH 2/6] :ok_hand: fix code standards requests --- .../com/vijay/jsonwizard/widgets/TimePickerFactoryTest.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/TimePickerFactoryTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/TimePickerFactoryTest.java index 428567cd9..c6a6d5d84 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/TimePickerFactoryTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/TimePickerFactoryTest.java @@ -27,13 +27,10 @@ public class TimePickerFactoryTest extends BaseTest { private TimePickerFactory factory; private JsonFormActivity jsonFormActivity; - @Mock private JsonFormFragment formFragment; @Mock private CommonListener listener; - @Mock - private View view; @Before public void setUp() { From 556042d6bb213ab4d7520cc632f16835401231c2 Mon Sep 17 00:00:00 2001 From: Benjamin Mwalimu Date: Tue, 29 Sep 2020 11:31:13 +0300 Subject: [PATCH 3/6] :ok_hand: add factory initialiazation tests, add translatable fields test --- .../widgets/SpinnerFactoryTest.java | 92 ++++++++++--------- 1 file changed, 50 insertions(+), 42 deletions(-) diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/SpinnerFactoryTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/SpinnerFactoryTest.java index a1d616fc8..3061c1721 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/SpinnerFactoryTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/SpinnerFactoryTest.java @@ -1,16 +1,30 @@ package com.vijay.jsonwizard.widgets; import android.util.Pair; +import android.view.View; +import android.widget.RelativeLayout; import com.vijay.jsonwizard.BaseTest; +import com.vijay.jsonwizard.R; +import com.vijay.jsonwizard.activities.JsonFormActivity; import com.vijay.jsonwizard.constants.JsonFormConstants; +import com.vijay.jsonwizard.customviews.MaterialSpinner; +import com.vijay.jsonwizard.fragments.JsonFormFragment; +import com.vijay.jsonwizard.interfaces.CommonListener; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; import org.powermock.reflect.Whitebox; +import org.robolectric.Robolectric; + +import java.util.List; +import java.util.Set; import static org.junit.Assert.assertEquals; @@ -18,55 +32,39 @@ * Created by Vincent Karuri on 03/04/2020 */ public class SpinnerFactoryTest extends BaseTest { - - private final String spinnerWidgetString = "{\n" + - " \"key\": \"response_spinner_with_options\",\n" + - " \"openmrs_entity_parent\": \"\",\n" + - " \"openmrs_entity\": \"\",\n" + - " \"openmrs_entity_id\": \"\",\n" + - " \"type\": \"spinner\",\n" + - " \"hint\": \"Response Spinners\",\n" + - " \"options\": [\n" + - " {\n" + - " \"key\": \"yes\",\n" + - " \"text\": \"Yes\",\n" + - " \"value\": false,\n" + - " \"openmrs_entity\": \"\",\n" + - " \"openmrs_entity_id\": \"\"\n" + - " },\n" + - " {\n" + - " \"key\": \"no\",\n" + - " \"text\": \"No\",\n" + - " \"value\": false,\n" + - " \"openmrs_entity\": \"\",\n" + - " \"openmrs_entity_id\": \"\"\n" + - " },\n" + - " {\n" + - " \"key\": \"maybe\",\n" + - " \"text\": \"Maybe\",\n" + - " \"value\": false,\n" + - " \"openmrs_entity\": \"\",\n" + - " \"openmrs_entity_id\": \"\"\n" + - " }\n" + - " ],\n" + - " \"v_required\": {\n" + - " \"value\": \"true\",\n" + - " \"err\": \"Please enter response\"\n" + - " },\n" + - " \"openmrs_choice_ids\": {\n" + - " \"user_one\": \"1107AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\n" + - " \"user_two\": \"1713AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"\n" + - " }\n" + - " }"; - private SpinnerFactory spinnerFactory; private JSONArray spinnerWidgetOptions; + private JsonFormActivity jsonFormActivity; + private JSONObject spinnerWidget; + @Mock + private JsonFormFragment formFragment; + @Mock + private CommonListener listener; @Before public void setUp() throws JSONException { spinnerFactory = new SpinnerFactory(); - JSONObject spinnerWidget = new JSONObject(spinnerWidgetString); + String spinnerWidgetString = "{\"key\":\"response_spinner_with_options\",\"openmrs_entity_parent\":\"test_parent_concept\",\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"concept_id\",\"type\":\"spinner\",\"hint\":\"Response Spinners\",\"options\":[{\"key\":\"yes\",\"text\":\"Yes\",\"value\":false,\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\"},{\"key\":\"no\",\"text\":\"No\",\"value\":false,\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\"},{\"key\":\"maybe\",\"text\":\"Maybe\",\"value\":false,\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\"}],\"relevance\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"sample-relevance-rules.yml\"}}},\"constraints\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"sample-constraints-rules.yml\"}}},\"calculation\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"sample-calculation-rules.yml\"}}},\"v_required\":{\"value\":\"true\",\"err\":\"Please enter response\"},\"openmrs_choice_ids\":{\"user_one\":\"1107AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"user_two\":\"1713AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"}}"; + spinnerWidget = new JSONObject(spinnerWidgetString); spinnerWidgetOptions = spinnerWidget.getJSONArray(JsonFormConstants.OPTIONS_FIELD_NAME); + jsonFormActivity = Robolectric.buildActivity(JsonFormActivity.class, getJsonFormActivityIntent()).create().get(); + } + + @Test + public void testSpinnerFactoryInstantiatesViewsCorrectly() throws Exception { + List viewList = spinnerFactory.getViewsFromJson("RandomStepName", jsonFormActivity, formFragment, spinnerWidget, listener); + Assert.assertNotNull(viewList); + Assert.assertEquals(1, viewList.size()); + + View rootLayout = viewList.get(0); + Assert.assertEquals(3, ((RelativeLayout) rootLayout).getChildCount()); + + MaterialSpinner materialSpinner = (MaterialSpinner) ((RelativeLayout) rootLayout).getChildAt(0); + + Assert.assertEquals("response_spinner_with_options", materialSpinner.getTag(R.id.key)); + Assert.assertEquals("test_parent_concept", materialSpinner.getTag(R.id.openmrs_entity_parent)); + Assert.assertEquals("concept", materialSpinner.getTag(R.id.openmrs_entity)); + Assert.assertEquals("concept_id", materialSpinner.getTag(R.id.openmrs_entity_id)); } @Test @@ -89,4 +87,14 @@ public void testGetOptionsKeyValPairsShouldExtractCorrectPairs() throws Exceptio assertEquals(actualVals.getString(i), expectedVals.getString(i)); } } + + @Test + public void testGetCustomTranslatableWidgetFields() { + Assert.assertNotNull(spinnerFactory); + SpinnerFactory factorySpy = Mockito.spy(spinnerFactory); + + Set editableProperties = factorySpy.getCustomTranslatableWidgetFields(); + Assert.assertEquals(1, editableProperties.size()); + Assert.assertEquals("options.text", editableProperties.iterator().next()); + } } From 30ac9f82077acc14d04db9cc4074948e5afad630 Mon Sep 17 00:00:00 2001 From: Benjamin Mwalimu Date: Tue, 29 Sep 2020 11:46:58 +0300 Subject: [PATCH 4/6] :ok_hand: remove unused imports --- .../com/vijay/jsonwizard/widgets/SpinnerFactoryTest.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/SpinnerFactoryTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/SpinnerFactoryTest.java index 3061c1721..a7e329898 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/SpinnerFactoryTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/SpinnerFactoryTest.java @@ -26,8 +26,6 @@ import java.util.List; import java.util.Set; -import static org.junit.Assert.assertEquals; - /** * Created by Vincent Karuri on 03/04/2020 */ @@ -83,8 +81,8 @@ public void testGetOptionsKeyValPairsShouldExtractCorrectPairs() throws Exceptio expectedVals.put("No"); expectedVals.put("Maybe"); for (int i = 0; i < actualKeys.length(); i++) { - assertEquals(actualKeys.getString(i), expectedKeys.getString(i)); - assertEquals(actualVals.getString(i), expectedVals.getString(i)); + Assert.assertEquals(actualKeys.getString(i), expectedKeys.getString(i)); + Assert.assertEquals(actualVals.getString(i), expectedVals.getString(i)); } } From 00eaa536e465cd511f0b9b2e9f2bae3fdb1a4639 Mon Sep 17 00:00:00 2001 From: Benjamin Mwalimu Date: Tue, 27 Oct 2020 11:32:28 +0300 Subject: [PATCH 5/6] :ok_hand: add toaster notes tests --- .../widgets/ToasterNotesFactoryTest.java | 142 +++--------------- 1 file changed, 19 insertions(+), 123 deletions(-) diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/ToasterNotesFactoryTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/ToasterNotesFactoryTest.java index 80d5c2ff9..538202fff 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/ToasterNotesFactoryTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/ToasterNotesFactoryTest.java @@ -1,15 +1,9 @@ package com.vijay.jsonwizard.widgets; -import android.content.res.Resources; import android.view.View; -import android.widget.ImageView; -import android.widget.RelativeLayout; -import android.widget.TextView; import com.vijay.jsonwizard.BaseTest; -import com.vijay.jsonwizard.R; import com.vijay.jsonwizard.activities.JsonFormActivity; -import com.vijay.jsonwizard.customviews.ToasterLinearLayout; import com.vijay.jsonwizard.fragments.JsonFormFragment; import com.vijay.jsonwizard.interfaces.CommonListener; @@ -18,164 +12,66 @@ import org.junit.Before; import org.junit.Test; import org.mockito.Mock; -import org.mockito.Mockito; import org.mockito.MockitoAnnotations; +import org.robolectric.Robolectric; import java.util.List; +import java.util.Set; public class ToasterNotesFactoryTest extends BaseTest { - private ToasterNotesFactory factory; - @Mock - private JsonFormActivity context; + private JsonFormActivity jsonFormActivity; @Mock private JsonFormFragment formFragment; - @Mock private CommonListener listener; - @Mock - private ToasterLinearLayout rootLayout; - - @Mock - private Resources resources; - - @Mock - private RelativeLayout toasterRelativeLayout; - - @Mock - private ImageView toasterNoteImageView; - - @Mock - private ImageView toasterNoteInfo; - - @Mock - private TextView toasterNotesTextView; - @Before public void setUp() { MockitoAnnotations.initMocks(this); factory = new ToasterNotesFactory(); + jsonFormActivity = Robolectric.buildActivity(JsonFormActivity.class, getJsonFormActivityIntent()).create().get(); } @Test public void testInfoToasterInstantiatesViewsCorrectly() throws Exception { - Assert.assertNotNull(factory); - ToasterNotesFactory factorySpy = Mockito.spy(factory); - Assert.assertNotNull(factorySpy); - - Mockito.doReturn(rootLayout).when(factorySpy).getToasterLinearLayout(context); - Assert.assertNotNull(rootLayout); - - Mockito.doReturn(toasterRelativeLayout).when(rootLayout).findViewById(R.id.toaster_notes_layout); - Assert.assertNotNull(toasterRelativeLayout); - - Mockito.doReturn(resources).when(context).getResources(); - Assert.assertNotNull(resources); - - Mockito.doReturn(toasterNoteImageView).when(rootLayout).findViewById(R.id.toaster_notes_image); - Assert.assertNotNull(toasterNoteImageView); - - Mockito.doReturn(toasterNoteInfo).when(rootLayout).findViewById(R.id.toaster_notes_info); - Assert.assertNotNull(toasterNoteInfo); - - Mockito.doReturn(toasterNotesTextView).when(rootLayout).findViewById(R.id.toaster_notes_text); - Assert.assertNotNull(toasterNotesTextView); - String labelString = "{\"key\":\"ultrasound_info_toaster\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"type\":\"toaster_notes\",\"text\":\"Test toaster notes\",\"toaster_type\":\"info\",\"relevance\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"ultrasound_sub_form_relevance_rules.yml\"}}},\"calculation\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"ultrasound_sub_form_calculation_rules.yml\"}}},\"constraints\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"ultrasound_sub_form_constraints_rules.yml\"}}}}"; - List viewList = factorySpy.getViewsFromJson("RandomStepName", context, formFragment, new JSONObject(labelString), listener); + List viewList = factory.getViewsFromJson("RandomStepName", jsonFormActivity, formFragment, new JSONObject(labelString), listener); Assert.assertNotNull(viewList); - Assert.assertEquals(1,viewList.size()); + Assert.assertEquals(1, viewList.size()); } @Test public void testWarningToasterInstantiatesViewsCorrectly() throws Exception { - Assert.assertNotNull(factory); - ToasterNotesFactory factorySpy = Mockito.spy(factory); - Assert.assertNotNull(factorySpy); - - Mockito.doReturn(rootLayout).when(factorySpy).getToasterLinearLayout(context); - Assert.assertNotNull(rootLayout); - - Mockito.doReturn(toasterRelativeLayout).when(rootLayout).findViewById(R.id.toaster_notes_layout); - Assert.assertNotNull(toasterRelativeLayout); - - Mockito.doReturn(resources).when(context).getResources(); - Assert.assertNotNull(resources); - - Mockito.doReturn(toasterNoteImageView).when(rootLayout).findViewById(R.id.toaster_notes_image); - Assert.assertNotNull(toasterNoteImageView); - - Mockito.doReturn(toasterNoteInfo).when(rootLayout).findViewById(R.id.toaster_notes_info); - Assert.assertNotNull(toasterNoteInfo); - - Mockito.doReturn(toasterNotesTextView).when(rootLayout).findViewById(R.id.toaster_notes_text); - Assert.assertNotNull(toasterNotesTextView); String labelString = "{\"key\":\"ultrasound_info_toaster\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"type\":\"toaster_notes\",\"text\":\"Test toaster notes\",\"toaster_type\":\"warning\",\"relevance\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"ultrasound_sub_form_relevance_rules.yml\"}}},\"calculation\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"ultrasound_sub_form_calculation_rules.yml\"}}},\"constraints\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"ultrasound_sub_form_constraints_rules.yml\"}}}}"; - List viewList = factorySpy.getViewsFromJson("RandomStepName", context, formFragment, new JSONObject(labelString), listener); + List viewList = factory.getViewsFromJson("RandomStepName", jsonFormActivity, formFragment, new JSONObject(labelString), listener); Assert.assertNotNull(viewList); - Assert.assertEquals(1,viewList.size()); + Assert.assertEquals(1, viewList.size()); } @Test public void testProblemToasterInstantiatesViewsCorrectly() throws Exception { - Assert.assertNotNull(factory); - ToasterNotesFactory factorySpy = Mockito.spy(factory); - Assert.assertNotNull(factorySpy); - - Mockito.doReturn(rootLayout).when(factorySpy).getToasterLinearLayout(context); - Assert.assertNotNull(rootLayout); - - Mockito.doReturn(toasterRelativeLayout).when(rootLayout).findViewById(R.id.toaster_notes_layout); - Assert.assertNotNull(toasterRelativeLayout); - - Mockito.doReturn(resources).when(context).getResources(); - Assert.assertNotNull(resources); - - Mockito.doReturn(toasterNoteImageView).when(rootLayout).findViewById(R.id.toaster_notes_image); - Assert.assertNotNull(toasterNoteImageView); - - Mockito.doReturn(toasterNoteInfo).when(rootLayout).findViewById(R.id.toaster_notes_info); - Assert.assertNotNull(toasterNoteInfo); - - Mockito.doReturn(toasterNotesTextView).when(rootLayout).findViewById(R.id.toaster_notes_text); - Assert.assertNotNull(toasterNotesTextView); String labelString = "{\"key\":\"ultrasound_info_toaster\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"type\":\"toaster_notes\",\"text\":\"Test toaster notes\",\"toaster_type\":\"problem\",\"toaster_info_text\":\"Toaster text\",\"toaster_info_title\":\"Toaster titles\",\"relevance\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"ultrasound_sub_form_relevance_rules.yml\"}}},\"calculation\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"ultrasound_sub_form_calculation_rules.yml\"}}},\"constraints\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"ultrasound_sub_form_constraints_rules.yml\"}}}}"; - List viewList = factorySpy.getViewsFromJson("RandomStepName", context, formFragment, new JSONObject(labelString), listener); + List viewList = factory.getViewsFromJson("RandomStepName", jsonFormActivity, formFragment, new JSONObject(labelString), listener); Assert.assertNotNull(viewList); - Assert.assertEquals(1,viewList.size()); + Assert.assertEquals(1, viewList.size()); } @Test public void testPositiveToasterInstantiatesViewsCorrectly() throws Exception { - Assert.assertNotNull(factory); - ToasterNotesFactory factorySpy = Mockito.spy(factory); - Assert.assertNotNull(factorySpy); - - Mockito.doReturn(rootLayout).when(factorySpy).getToasterLinearLayout(context); - Assert.assertNotNull(rootLayout); - - Mockito.doReturn(toasterRelativeLayout).when(rootLayout).findViewById(R.id.toaster_notes_layout); - Assert.assertNotNull(toasterRelativeLayout); - - Mockito.doReturn(resources).when(context).getResources(); - Assert.assertNotNull(resources); - - Mockito.doReturn(toasterNoteImageView).when(rootLayout).findViewById(R.id.toaster_notes_image); - Assert.assertNotNull(toasterNoteImageView); - - Mockito.doReturn(toasterNoteInfo).when(rootLayout).findViewById(R.id.toaster_notes_info); - Assert.assertNotNull(toasterNoteInfo); - - Mockito.doReturn(toasterNotesTextView).when(rootLayout).findViewById(R.id.toaster_notes_text); - Assert.assertNotNull(toasterNotesTextView); - String labelString = "{\"key\":\"ultrasound_info_toaster\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"type\":\"toaster_notes\",\"text\":\"Test toaster notes\",\"toaster_type\":\"positive\",\"relevance\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"ultrasound_sub_form_relevance_rules.yml\"}}},\"calculation\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"ultrasound_sub_form_calculation_rules.yml\"}}},\"constraints\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"ultrasound_sub_form_constraints_rules.yml\"}}}}"; - List viewList = factorySpy.getViewsFromJson("RandomStepName", context, formFragment, new JSONObject(labelString), listener); + List viewList = factory.getViewsFromJson("RandomStepName", jsonFormActivity, formFragment, new JSONObject(labelString), listener); Assert.assertNotNull(viewList); - Assert.assertEquals(1,viewList.size()); + Assert.assertEquals(1, viewList.size()); + } + + @Test + public void testGetCustomTranslatableWidgetFields() { + Set editableProperties = factory.getCustomTranslatableWidgetFields(); + Assert.assertEquals(2, editableProperties.size()); + Assert.assertEquals("toaster_info_text", editableProperties.iterator().next()); } } From 5e58b570dddaaad042e1aa8d96bc7bffcda36b88 Mon Sep 17 00:00:00 2001 From: Benjamin Mwalimu Date: Tue, 3 Nov 2020 11:58:15 +0300 Subject: [PATCH 6/6] :ok_hand: Updated the barcode tests --- .../jsonwizard/widgets/BarcodeFactorTest.java | 61 ++++++++----------- .../jsonwizard/widgets/ButtonFactoryTest.java | 13 +++- 2 files changed, 37 insertions(+), 37 deletions(-) diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/BarcodeFactorTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/BarcodeFactorTest.java index 660fef025..a72039cb0 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/BarcodeFactorTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/BarcodeFactorTest.java @@ -1,17 +1,14 @@ package com.vijay.jsonwizard.widgets; -import android.content.res.Resources; import android.view.View; import android.widget.RelativeLayout; import com.rengwuxian.materialedittext.MaterialEditText; -import com.rey.material.widget.Button; import com.vijay.jsonwizard.BaseTest; import com.vijay.jsonwizard.R; import com.vijay.jsonwizard.activities.JsonFormActivity; import com.vijay.jsonwizard.fragments.JsonFormFragment; import com.vijay.jsonwizard.interfaces.CommonListener; -import com.vijay.jsonwizard.utils.FormUtils; import org.json.JSONObject; import org.junit.Assert; @@ -20,64 +17,56 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; +import org.robolectric.Robolectric; import java.util.List; +import java.util.Set; public class BarcodeFactorTest extends BaseTest { private BarcodeFactory factory; @Mock - private JsonFormActivity context; + private JsonFormActivity jsonFormActivity; @Mock private JsonFormFragment formFragment; - @Mock - private Resources resources; - @Mock private CommonListener listener; - @Mock - private RelativeLayout rootLayout; - - @Mock - private MaterialEditText editText; - - @Mock - private Button scanButton; - @Before public void setUp() { MockitoAnnotations.initMocks(this); factory = new BarcodeFactory(); + jsonFormActivity = Robolectric.buildActivity(JsonFormActivity.class, getJsonFormActivityIntent()).create().get(); } @Test public void testBarCodeFactoryInstantiatesViewsCorrectly() throws Exception { - Assert.assertNotNull(factory); - BarcodeFactory factorySpy = Mockito.spy(factory); - Assert.assertNotNull(factorySpy); - - FormUtils formUtils = new FormUtils(); - FormUtils formUtilsSpy = Mockito.spy(formUtils); - Assert.assertNotNull(formUtilsSpy); + String gpsString = "{\"key\":\"user_qr_code\",\"openmrs_entity_parent\":\"no_parent\",\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"user_qr_code_id\",\"type\":\"barcode\",\"barcode_type\":\"qrcode\",\"hint\":\"User ID\",\"read_only\":true,\"scanButtonText\":\"Scan QR Code\",\"v_numeric\":{\"value\":\"true\",\"err\":\"Please enter a valid ID\"},\"value\":\"123455646\",\"v_required\":{\"value\":true,\"err\":\"Please enter the user ID\"},\"relevance\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"sample-relevance-rules.yml\"}}},\"calculation\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"sample-calculation-rules.yml\"}}},\"constraints\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"sample-constraints-rules.yml\"}}}}"; + List viewList = factory.getViewsFromJson("RandomStepName", jsonFormActivity, formFragment, new JSONObject(gpsString), listener); + Assert.assertNotNull(viewList); + Assert.assertTrue(viewList.size() > 0); + Assert.assertEquals(1, viewList.size()); - context.setTheme(R.style.NativeFormsAppTheme); - Mockito.doReturn(rootLayout).when(factorySpy).getRootLayout(context); - Assert.assertNotNull(rootLayout); + RelativeLayout relativeLayout = (RelativeLayout) viewList.get(0); + Assert.assertNotNull(relativeLayout); - Mockito.doReturn(resources).when(context).getResources(); - Assert.assertNotNull(resources); + MaterialEditText materialEditText = (MaterialEditText) relativeLayout.getChildAt(0); - Mockito.doReturn(editText).when(rootLayout).findViewById(R.id.edit_text); - Assert.assertNotNull(editText); + Assert.assertEquals("user_qr_code", materialEditText.getTag(R.id.key)); + Assert.assertEquals("no_parent", materialEditText.getTag(R.id.openmrs_entity_parent)); + Assert.assertEquals("concept", materialEditText.getTag(R.id.openmrs_entity)); + Assert.assertEquals("user_qr_code_id", materialEditText.getTag(R.id.openmrs_entity_id)); + } - Mockito.doReturn(scanButton).when(rootLayout).findViewById(R.id.scan_button); - Assert.assertNotNull(scanButton); + @Test + public void testGetCustomTranslatableWidgetFields() { + Assert.assertNotNull(factory); + BarcodeFactory factorySpy = Mockito.spy(factory); + Assert.assertNotNull(factorySpy); - String gpsString = "{\"key\":\"user_qr_code\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"type\":\"barcode\",\"barcode_type\":\"qrcode\",\"hint\":\"User ID\",\"read_only\":true,\"scanButtonText\":\"Scan QR Code\",\"v_numeric\":{\"value\":\"true\",\"err\":\"Please enter a valid ID\"},\"value\":\"123455646\",\"v_required\":{\"value\":true,\"err\":\"Please enter the user ID\"},\"relevance\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"sample-relevance-rules.yml\"}}},\"calculation\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"sample-calculation-rules.yml\"}}},\"constraints\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"sample-constraints-rules.yml\"}}}}"; - List viewList = factorySpy.getViewsFromJson("RandomStepName", context, formFragment, new JSONObject(gpsString), listener); - Assert.assertNotNull(viewList); - Assert.assertTrue(viewList.size() > 0); + Set editableProperties = factorySpy.getCustomTranslatableWidgetFields(); + Assert.assertEquals(1, editableProperties.size()); + Assert.assertEquals("scanButtonText", editableProperties.iterator().next()); } } diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/ButtonFactoryTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/ButtonFactoryTest.java index 30b86c18f..acbe22694 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/ButtonFactoryTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/ButtonFactoryTest.java @@ -15,13 +15,14 @@ import org.mockito.Mock; import org.mockito.Mockito; +import java.util.Set; + import static com.vijay.jsonwizard.constants.JsonFormConstants.STEP1; /** * Created by Vincent Karuri on 25/08/2020 */ public class ButtonFactoryTest extends FactoryTest { - @Mock private JsonFormFragment jsonFormFragment; @Mock @@ -96,4 +97,14 @@ private JSONObject getJsonObject() throws JSONException { jsonObject.put(JsonFormConstants.ACTION, action); return jsonObject; } + + @Test + public void testGetCustomTranslatableWidgetFields() { + Assert.assertNotNull(buttonFactory); + ButtonFactory factorySpy = Mockito.spy(buttonFactory); + Assert.assertNotNull(factorySpy); + + Set editableProperties = factorySpy.getCustomTranslatableWidgetFields(); + Assert.assertEquals(0, editableProperties.size()); + } } \ No newline at end of file