Skip to content

Commit

Permalink
Merge pull request #545 from OpenSRP/tt-feb-2-2021
Browse files Browse the repository at this point in the history
Tt feb 2 2021
  • Loading branch information
bennsimon authored Feb 8, 2021
2 parents 52d7c84 + af09a18 commit 65472f8
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
import java.util.Set;

public class ExtendedRadioButtonWidgetFactory extends NativeRadioButtonFactory {

private FormUtils formUtils = new FormUtils();

@Override
protected List<View> attachJson(String stepName, Context context, JsonFormFragment formFragment, JSONObject jsonObject,
CommonListener commonListener, boolean popup) throws JSONException {
Expand Down Expand Up @@ -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();
}
}

Expand Down
1 change: 1 addition & 0 deletions android-json-form-wizard/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,6 @@
<string name="error_title">Error</string>
<string name="form_load_error">Sorry, a fatal error occurred when loading the form. Please contact a developer for further assistance.</string>
<string name="select_one_option">Please select one option</string>
<string name="extended_radio_info_text_ensure_options_set">Please make sure you have set the radio button options</string>

</resources>
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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\"}]");
Expand Down
Original file line number Diff line number Diff line change
@@ -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<View> 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());
}
}

0 comments on commit 65472f8

Please sign in to comment.