diff --git a/android-json-form-wizard/jacoco.exec b/android-json-form-wizard/jacoco.exec index d2edd505c..05b52065d 100644 Binary files a/android-json-form-wizard/jacoco.exec and b/android-json-form-wizard/jacoco.exec differ 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 d81947b12..7d0f62265 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,16 +1,13 @@ 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.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; @@ -18,68 +15,57 @@ import org.junit.Test; import org.mockito.Mock; import org.mockito.Mockito; +import org.robolectric.Robolectric; import java.util.List; +import java.util.Set; public class BarcodeFactorTest extends FactoryTest { 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() { super.setUp(); 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); - - context.setTheme(R.style.NativeFormsAppTheme); - Mockito.doReturn(rootLayout).when(factorySpy).getRootLayout(context); - Assert.assertNotNull(rootLayout); - - Mockito.doReturn(resources).when(context).getResources(); - Assert.assertNotNull(resources); + 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()); - Mockito.doReturn(editText).when(rootLayout).findViewById(R.id.edit_text); - Assert.assertNotNull(editText); + RelativeLayout relativeLayout = (RelativeLayout) viewList.get(0); + Assert.assertNotNull(relativeLayout); - Mockito.doReturn(scanButton).when(rootLayout).findViewById(R.id.scan_button); - Assert.assertNotNull(scanButton); + MaterialEditText materialEditText = (MaterialEditText) relativeLayout.getChildAt(0); - Mockito.doReturn(jsonFormActivity).when(formFragment).getJsonApi(); + 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)); + } - 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); + @Test + public void testGetCustomTranslatableWidgetFields() { + Assert.assertNotNull(factory); + BarcodeFactory factorySpy = Mockito.spy(factory); + Assert.assertNotNull(factorySpy); + 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 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 958b02f5a..a93c4ee12 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,11 +1,8 @@ package com.vijay.jsonwizard.widgets; -import android.app.FragmentManager; -import android.content.res.Resources; -import android.support.v7.app.AppCompatActivity; +import android.os.Looper; import android.view.View; import android.widget.RelativeLayout; -import android.widget.TextView; import com.rengwuxian.materialedittext.MaterialEditText; import com.vijay.jsonwizard.BaseTest; @@ -15,124 +12,99 @@ import com.vijay.jsonwizard.fragments.JsonFormFragment; import com.vijay.jsonwizard.interfaces.CommonListener; import com.vijay.jsonwizard.shadow.ShadowDialogFragment; -import com.vijay.jsonwizard.utils.AppExecutors; +import com.vijay.jsonwizard.utils.FormUtils; import org.json.JSONObject; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentCaptor; +import org.mockito.ArgumentMatchers; import org.mockito.Captor; import org.mockito.Mock; import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; import org.powermock.reflect.Whitebox; import org.robolectric.Robolectric; -import org.robolectric.RuntimeEnvironment; +import org.robolectric.Shadows; import org.robolectric.annotation.Config; import java.text.SimpleDateFormat; import java.util.List; -import java.util.Locale; import java.util.Set; -import static android.os.Looper.getMainLooper; -import static org.junit.Assert.assertEquals; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -import static org.robolectric.Shadows.shadowOf; - public class DatePickerFactoryTest extends BaseTest { private DatePickerFactory factory; - @Mock - private JsonFormActivity formActivity; - + private FormUtils formUtils; + private JsonFormActivity jsonFormActivity; @Mock private JsonFormFragment formFragment; - - - private Resources resources = RuntimeEnvironment.application.getResources(); - @Mock private CommonListener listener; - - @Mock - private MaterialEditText editText; - - @Mock - private RelativeLayout rootLayout; - - @Mock - private TextView duration; - - @Mock - private FragmentManager fragmentManager; - @Captor private ArgumentCaptor viewArgumentCaptor; @Captor private ArgumentCaptor datePickerDialogArgumentCaptor; + @Before public void setUp() { - AppExecutors appExecutors = new AppExecutors(); - when(formActivity.getAppExecutors()).thenReturn(appExecutors); - factory = spy(new DatePickerFactory()); - Mockito.doReturn(Locale.ENGLISH).when(factory).getCurrentLocale(formActivity); - Mockito.doReturn(Locale.ENGLISH).when(factory).getSetLanguage(formActivity); - Mockito.doReturn(resources).when(formActivity).getResources(); - Mockito.doReturn("12 Age").when(factory).getDurationText(formActivity, "12-05-2010", Locale.ENGLISH); + MockitoAnnotations.initMocks(this); + factory = new DatePickerFactory(); + formUtils = new FormUtils(); + jsonFormActivity = Robolectric.buildActivity(JsonFormActivity.class, getJsonFormActivityIntent()).create().get(); } @Test public void testDatePickerFactoryInstantiatesViewsCorrectly() throws Exception { - Mockito.doReturn(resources).when(formActivity).getResources(); - - formActivity.setTheme(R.style.NativeFormsAppTheme); - Mockito.doReturn(rootLayout).when(factory).getRelativeLayout(formActivity); - 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); 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 = factory.getViewsFromJson("RandomStepName", formActivity, formFragment, new JSONObject(datePicker), listener); - shadowOf(getMainLooper()).idle(); + Assert.assertNotNull(formUtils); + FormUtils formUtilsSpy = Mockito.spy(formUtils); + Assert.assertNotNull(formUtilsSpy); + Whitebox.setInternalState(factory, "formUtils", formUtilsSpy); + + List viewList = factory.getViewsFromJson("RandomStepName", jsonFormActivity, formFragment, new JSONObject(datePicker), listener); Assert.assertNotNull(viewList); - assertEquals(1, viewList.size()); + 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 public void testGetCustomTranslatableWidgetFields() { Assert.assertNotNull(factory); - DatePickerFactory factorySpy = spy(factory); + DatePickerFactory factorySpy = Mockito.spy(factory); Assert.assertNotNull(factorySpy); Set editableProperties = factorySpy.getCustomTranslatableWidgetFields(); - assertEquals(1, editableProperties.size()); - assertEquals("duration.label", editableProperties.iterator().next()); + Assert.assertEquals(1, editableProperties.size()); + Assert.assertEquals("duration.label", editableProperties.iterator().next()); } @Config(shadows = {ShadowDialogFragment.class}) @Test public void testShowDatePickerDialog() throws Exception { - when(formActivity.getFragmentManager()).thenReturn(fragmentManager); - AppCompatActivity appCompatActivity = Robolectric.buildActivity(AppCompatActivity.class).create().get(); - RelativeLayout view = (RelativeLayout) appCompatActivity.getLayoutInflater().inflate(factory.getLayout(), null); - Mockito.doReturn(view).when(factory).getRelativeLayout(formActivity); + JsonFormActivity jsonFormActivitySpy = Mockito.spy(jsonFormActivity); + DatePickerFactory datePickerFactory = Mockito.spy(factory); 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 = factory.getViewsFromJson("RandomStepName", formActivity, formFragment, new JSONObject(datePicker), listener); - shadowOf(getMainLooper()).idle(); - assertEquals(1, viewList.size()); - verify(formActivity).addFormDataView(viewArgumentCaptor.capture()); + List viewList = datePickerFactory.getViewsFromJson("RandomStepName", jsonFormActivitySpy, formFragment, new JSONObject(datePicker), listener); + Shadows.shadowOf(Looper.getMainLooper()).idle(); + Assert.assertEquals(1, viewList.size()); + Mockito.verify(jsonFormActivitySpy).addFormDataView(viewArgumentCaptor.capture()); viewArgumentCaptor.getValue().performClick(); - verify(factory).showDatePickerDialog(eq(formActivity), datePickerDialogArgumentCaptor.capture(), any(MaterialEditText.class)); - verify(fragmentManager).beginTransaction(); - verify(fragmentManager).executePendingTransactions(); + Mockito.verify(datePickerFactory).showDatePickerDialog(Mockito.eq(jsonFormActivitySpy), datePickerDialogArgumentCaptor.capture(), ArgumentMatchers.any(MaterialEditText.class)); DatePickerDialog datePickerDialog = datePickerDialogArgumentCaptor.getValue(); - assertEquals("12-05-2020", new SimpleDateFormat("dd-MM-yyyy").format(Whitebox.getInternalState(datePickerDialog, "date"))); + Assert.assertEquals("12-05-2020", new SimpleDateFormat("dd-MM-yyyy").format(Whitebox.getInternalState(datePickerDialog, "date"))); } 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..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 @@ -1,72 +1,68 @@ 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 static org.junit.Assert.assertEquals; +import java.util.List; +import java.util.Set; /** * 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 @@ -85,8 +81,18 @@ 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)); } } + + @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()); + } } 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..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 @@ -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,61 @@ 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; - @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()); } 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()); } } diff --git a/form_tester/build.gradle b/form_tester/build.gradle index 96afd02fc..2674898ff 100644 --- a/form_tester/build.gradle +++ b/form_tester/build.gradle @@ -61,4 +61,5 @@ dependencies { implementation 'com.android.support:support-v4:28.0.0' implementation 'com.android.support:recyclerview-v7:28.0.0' implementation 'com.google.code.gson:gson:2.8.6' + implementation 'org.smartregister:opensrp-client-utils:0.0.4-SNAPSHOT' }