diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/ExtendedRadioButtonWidgetFactory.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/ExtendedRadioButtonWidgetFactory.java index b3c75c325..2295c8938 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/ExtendedRadioButtonWidgetFactory.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/ExtendedRadioButtonWidgetFactory.java @@ -30,7 +30,9 @@ import java.util.Set; public class ExtendedRadioButtonWidgetFactory extends NativeRadioButtonFactory { + private FormUtils formUtils = new FormUtils(); + @Override protected List attachJson(String stepName, Context context, JsonFormFragment formFragment, JSONObject jsonObject, CommonListener commonListener, boolean popup) throws JSONException { @@ -162,7 +164,7 @@ private void addRadioButtons(String stepName, Context context, JSONObject jsonOb rootLayout.addView(radioButton); } } else { - Toast.makeText(context, "Please make sure you have set the radio button options", Toast.LENGTH_SHORT).show(); + Toast.makeText(context, R.string.extended_radio_info_text_ensure_options_set, Toast.LENGTH_SHORT).show(); } } diff --git a/android-json-form-wizard/src/main/res/values/strings.xml b/android-json-form-wizard/src/main/res/values/strings.xml index 4d25a6df5..54509d95e 100644 --- a/android-json-form-wizard/src/main/res/values/strings.xml +++ b/android-json-form-wizard/src/main/res/values/strings.xml @@ -114,5 +114,6 @@ Error Sorry, a fatal error occurred when loading the form. Please contact a developer for further assistance. Please select one option + Please make sure you have set the radio button options \ No newline at end of file diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/FormUtilsTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/FormUtilsTest.java index d64cd8090..01d9e7e63 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/FormUtilsTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/FormUtilsTest.java @@ -1,14 +1,21 @@ package com.vijay.jsonwizard.utils; +import android.app.Activity; +import android.app.DialogFragment; +import android.app.FragmentTransaction; import android.content.Context; import android.content.res.Configuration; import android.content.res.Resources; +import android.widget.Button; +import android.widget.LinearLayout; import com.vijay.jsonwizard.BaseTest; +import com.vijay.jsonwizard.R; import com.vijay.jsonwizard.constants.JsonFormConstants; import com.vijay.jsonwizard.domain.ExpansionPanelItemModel; import com.vijay.jsonwizard.domain.ExpansionPanelValuesModel; import com.vijay.jsonwizard.interfaces.OnFormFetchedCallback; +import com.vijay.jsonwizard.views.CustomTextView; import com.vijay.jsonwizard.model.DynamicLabelInfo; import org.apache.commons.lang3.StringUtils; @@ -20,9 +27,11 @@ import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentCaptor; +import org.mockito.ArgumentMatchers; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.robolectric.RuntimeEnvironment; +import org.robolectric.util.ReflectionHelpers; import org.smartregister.client.utils.contract.ClientFormContract; import java.text.SimpleDateFormat; @@ -660,6 +669,33 @@ public void testGetCheckBoxResultsWithNoIsRuleCheck() throws JSONException { Assert.assertEquals(4, facts.asMap().size()); } + @Test + public void testShowGenericDialogShouldInvokeExpectedMethods() { + formUtils = Mockito.spy(formUtils); + + FragmentTransaction mockFragmentTransaction = Mockito.mock(FragmentTransaction.class); + Utils utils = Mockito.spy(new Utils()); + ReflectionHelpers.setField(formUtils, "utils", utils); + Mockito.doReturn(mockFragmentTransaction).when(utils).getFragmentTransaction(ArgumentMatchers.any(Activity.class)); + + Activity mockActivity = Mockito.mock(Activity.class); + LinearLayout mainLayout = Mockito.spy(new LinearLayout(RuntimeEnvironment.application)); + Mockito.doReturn(mainLayout).when(mockActivity).findViewById(R.id.main_layout); + + Button button = new Button(RuntimeEnvironment.application); + button.setTag(R.id.specify_context, mockActivity); + button.setTag(R.id.type, JsonFormConstants.EXPANSION_PANEL); + button.setTag(R.id.specify_content, "user_native_sub_form"); + button.setTag(R.id.specify_content_form, ""); + button.setTag(R.id.specify_textview, new CustomTextView(button.getContext())); + button.setTag(R.id.specify_reasons_textview, new CustomTextView(button.getContext())); + + formUtils.showGenericDialog(button); + + Mockito.verify(mainLayout, Mockito.only()).clearFocus(); + Mockito.verify(mockFragmentTransaction).add(ArgumentMatchers.any(DialogFragment.class), ArgumentMatchers.eq("GenericPopup")); + } + @Test public void testGetDynamicLabelInfoList() throws JSONException { JSONArray jsonArray = new JSONArray("[{\"dynamic_label_title\": \"sample title\",\"dynamic_label_text\": \"sample text\",\"dynamic_label_image_src\": \"img/img.png\"}]"); diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/ExtendedRadioButtonWidgetFactoryTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/ExtendedRadioButtonWidgetFactoryTest.java new file mode 100644 index 000000000..bdbd25f1d --- /dev/null +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/ExtendedRadioButtonWidgetFactoryTest.java @@ -0,0 +1,56 @@ +package com.vijay.jsonwizard.widgets; + +import android.support.constraint.ConstraintLayout; +import android.view.View; +import android.widget.LinearLayout; +import android.widget.RadioGroup; + +import com.vijay.jsonwizard.constants.JsonFormConstants; +import com.vijay.jsonwizard.fragments.JsonFormFragment; +import com.vijay.jsonwizard.interfaces.CommonListener; + +import org.json.JSONException; +import org.json.JSONObject; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; + +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.spy; + +public class ExtendedRadioButtonWidgetFactoryTest extends FactoryTest { + + private ExtendedRadioButtonWidgetFactory extendedRadioButtonWidgetFactory; + + @Mock + private JsonFormFragment jsonFormFragment; + + @Mock + private CommonListener commonListener; + + @Before + public void setUp() { + super.setUp(); + extendedRadioButtonWidgetFactory = spy(new ExtendedRadioButtonWidgetFactory()); + } + + @Test + public void testFactoryShouldInitializeCorrectly() throws JSONException { + String strJson = "{\"key\":\"blood_type_test_status\",\"openmrs_entity_parent\":\"300AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"163725AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"label\":\"Blood type test\",\"label_text_style\":\"bold\",\"text_color\":\"#000000\",\"type\":\"extended_radio_button\",\"options\":[{\"key\":\"done_today\",\"text\":\"Done today\",\"type\":\"done_today\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"165383AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"},{\"key\":\"done_earlier\",\"text\":\"Done earlier\",\"type\":\"done_earlier\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"165385AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"},{\"key\":\"ordered\",\"text\":\"Ordered\",\"type\":\"ordered\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"165384AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"},{\"key\":\"not_done\",\"text\":\"Not done\",\"type\":\"not_done\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"1118AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"}],\"v_required\":{\"value\":true,\"err\":\"Blood type status is required\"}}"; + JSONObject fieldJsonObject = new JSONObject(strJson); + String stepName = JsonFormConstants.STEP1; + List views = extendedRadioButtonWidgetFactory.attachJson(stepName, jsonFormActivity, jsonFormFragment, fieldJsonObject, commonListener, true); + + assertNotNull(views); + assertTrue(views.get(0) instanceof LinearLayout); + + LinearLayout linearLayout = (LinearLayout) views.get(0); + assertTrue(linearLayout.getChildAt(0) instanceof ConstraintLayout); + assertTrue(linearLayout.getChildAt(1) instanceof RadioGroup); + assertEquals(4, ((RadioGroup) linearLayout.getChildAt(1)).getChildCount()); + } +} \ No newline at end of file