Skip to content

Commit

Permalink
Merge pull request #379 from OpenSRP/tt-wk3-JsonFormFragmentPresenter
Browse files Browse the repository at this point in the history
TT wk JsonFormFragmentPresenter tests
  • Loading branch information
githengi authored Mar 10, 2020
2 parents 4b7f52f + e824d76 commit 0895e9c
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ public class TestConstants {
public static final String TYPE_ARRAY = "array";

public static final String PAOT_TEST_FORM="{\"count\":\"1\",\"encounter_type\":\"PAOT\",\"entity_id\":\"\",\"metadata\":{\"start\":{\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_data_type\":\"start\",\"openmrs_entity_id\":\"163137AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"},\"end\":{\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_data_type\":\"end\",\"openmrs_entity_id\":\"163138AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"},\"today\":{\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"encounter\",\"openmrs_entity_id\":\"encounter_date\"},\"deviceid\":{\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_data_type\":\"deviceid\",\"openmrs_entity_id\":\"163149AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"},\"subscriberid\":{\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_data_type\":\"subscriberid\",\"openmrs_entity_id\":\"163150AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"},\"simserial\":{\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_data_type\":\"simserial\",\"openmrs_entity_id\":\"163151AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"},\"phonenumber\":{\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_data_type\":\"phonenumber\",\"openmrs_entity_id\":\"163152AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"},\"encounter_location\":\"\"},\"step1\":{\"title\":\"Potential Area of Transmission\",\"display_back_button\":\"true\",\"fields\":[{\"key\":\"paotStatus\",\"label\":\"Status\",\"type\":\"native_radio\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"options\":[{\"key\":\"Active\",\"text\":\"Active\"},{\"key\":\"Inactive\",\"text\":\"Inactive\"},{\"key\":\"Not_Eligible\",\"text\":\"Not Eligible\"}],\"v_required\":{\"value\":true,\"err\":\"Please specify status of area of transmission\"}},{\"key\":\"paotComments\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"type\":\"edit_text\",\"hint\":\"Comments\"},{\"key\":\"lastUpdatedDate\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"type\":\"date_picker\",\"hint\":\"Last Updated\",\"max_date\":\"today\",\"v_required\":{\"value\":true,\"err\":\"Please specify the last updated date\"}},{\"key\":\"business_status\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"type\":\"hidden\",\"value\":\"Complete\"}]}}";


public static final String BASIC_FORM="{\"count\":\"3\",\"encounter_type\":\"Test\",\"entity_id\":\"\",\"relational_id\":\"\",\"validate_on_submit\":true,\"show_errors_on_submit\":true,\"metadata\":{\"start\":{\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_data_type\":\"start\",\"openmrs_entity_id\":\"163137AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"},\"end\":{\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_data_type\":\"end\",\"openmrs_entity_id\":\"163138AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"},\"today\":{\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"encounter\",\"openmrs_entity_id\":\"encounter_date\"},\"deviceid\":{\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_data_type\":\"deviceid\",\"openmrs_entity_id\":\"163149AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"},\"subscriberid\":{\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_data_type\":\"subscriberid\",\"openmrs_entity_id\":\"163150AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"},\"simserial\":{\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_data_type\":\"simserial\",\"openmrs_entity_id\":\"163151AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"},\"phonenumber\":{\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_data_type\":\"phonenumber\",\"openmrs_entity_id\":\"163152AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"},\"encounter_location\":\"\",\"look_up\":{\"entity_id\":\"\",\"value\":\"\"}},\"step1\":{\"title\":\"Basic Form One\",\"next\":\"step2\",\"fields\":[{\"key\":\"user_image\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"type\":\"choose_image\",\"uploadButtonText\":\"Take a photo of the child\"},{\"key\":\"thumbnail\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"type\":\"image_view\",\"uploadButtonText\":\"Take a photo of the child\"},{\"key\":\"user_first_name\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"type\":\"edit_text\",\"hint\":\"User First name\",\"edit_type\":\"name\",\"v_required\":{\"value\":\"true\",\"err\":\"Please enter the first name\"},\"v_regex\":{\"value\":\"[A-Za-z\\\\s\\\\.\\\\-]*\",\"err\":\"Please enter a valid name\"}},{\"key\":\"user_last_name\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"type\":\"normal_edit_text\",\"hint\":\"User Last name\",\"edit_type\":\"name\",\"v_required\":{\"value\":\"true\",\"err\":\"Please enter the last name\"},\"v_regex\":{\"value\":\"[A-Za-z\\\\s\\\\.\\\\-]*\",\"err\":\"Please enter a valid name\"}},{\"key\":\"user_qr_code\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"type\":\"barcode\",\"barcode_type\":\"qrcode\",\"hint\":\"User ID\",\"scanButtonText\":\"Scan QR Code\",\"v_numeric\":{\"value\":\"true\",\"err\":\"Please enter a valid ID\"},\"v_required\":{\"value\":false,\"err\":\"Please enter the user ID\"}},{\"key\":\"user_age\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"type\":\"edit_text\",\"hint\":\"User age\",\"edit_type\":\"number\",\"v_required\":{\"value\":\"true\",\"err\":\"Please enter user age\"}},{\"key\":\"user_gps\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"openmrs_data_type\":\"text\",\"type\":\"gps\"},{\"key\":\"user_calculation_one\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"165260AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"type\":\"hidden\"},{\"key\":\"user_spinner\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"type\":\"spinner\",\"hint\":\"User Spinners\",\"values\":[\"User Option One\",\"User Option Two\"],\"keys\":[\"user_option_one\",\"user_option_two\"],\"v_required\":{\"value\":\"true\",\"err\":\"Please enter the sex\"},\"openmrs_choice_ids\":{\"user_one\":\"1107AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"user_two\":\"1713AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"}},{\"key\":\"user_check_box\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"openmrs_data_type\":\"select one\",\"type\":\"check_box\",\"label\":\"Do want to select any checkbox?\",\"label_text_style\":\"bold\",\"options\":[{\"key\":\"None\",\"text\":\"None\",\"value\":false,\"openmrs_choice_id\":\"\"},{\"key\":\"yes\",\"text\":\"Yes\",\"value\":false,\"openmrs_choice_id\":\"\"},{\"key\":\"no\",\"text\":\"No\",\"value\":false,\"openmrs_choice_id\":\"\"},{\"key\":\"other\",\"text\":\"Other\",\"value\":false,\"openmrs_choice_id\":\"\"}],\"v_required\":{\"value\":\"true\"}}]},\"step2\":{\"title\":\"Basic Form Two\",\"next\":\"step3\",\"fields\":[{\"key\":\"user_select\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"type\":\"native_radio\",\"label\":\"Do you want to select anything\",\"label_text_style\":\"bold\",\"text_color\":\"#000000\",\"label_info_text\":\"You can select every thing you want.\",\"label_info_title\":\"User selection\",\"options\":[{\"key\":\"yes\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"text\":\"Yes\"},{\"key\":\"no\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"text\":\"No\"}]},{\"key\":\"user_normal_edit_text\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"type\":\"normal_edit_text\",\"hint\":\"User Text \",\"edit_text_style\":\"bordered\"},{\"key\":\"user_normal_edit_number\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"type\":\"normal_edit_text\",\"hint\":\"User number\",\"edit_text_style\":\"bordered\",\"edit_type\":\"number\"},{\"key\":\"user_toaster_problem\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"type\":\"toaster_notes\",\"text\":\"User problem toaster\",\"toaster_info_text\":\"Procedure:\\n- This shows the user the problems.\\n- Problem toaster.\",\"toaster_type\":\"problem\"},{\"key\":\"user_toaster_warning\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"type\":\"toaster_notes\",\"text\":\"User warning toaster\",\"toaster_info_text\":\"Procedure:\\n- This shows the user the warning.\\n- Warning toaster.\",\"toaster_type\":\"warning\"},{\"key\":\"user_toaster_positive\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"type\":\"toaster_notes\",\"text\":\"User positive toaster\",\"toaster_info_text\":\"Procedure:\\n- This shows the user the positive.\\n- Positive toaster.\",\"toaster_type\":\"positive\"},{\"key\":\"user_toaster_info\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"type\":\"toaster_notes\",\"text\":\"User Info toaster\",\"toaster_info_text\":\"Procedure:\\n- This shows the user the info.\\n- Info toaster.\",\"toaster_type\":\"info\"}]},\"step3\":{\"title\":\"Basic Form Threes\",\"fields\":[{\"key\":\"user_date\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"type\":\"date_picker\",\"hint\":\"User DOB\",\"expanded\":false,\"duration\":{\"label\":\"Age\"},\"default\":\"12-12-2015\",\"min_date\":\"today-5y\",\"max_date\":\"today\",\"v_required\":{\"value\":\"true\",\"err\":\"Please enter the date of birth\"}},{\"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\"}},{\"key\":\"user_spinner\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"type\":\"spinner\",\"hint\":\"User Spinners\",\"values\":[\"User Option One\",\"User Option Two\"],\"keys\":[\"user_option_one\",\"user_option_two\"],\"v_required\":{\"value\":\"true\",\"err\":\"Please enter the sex\"},\"openmrs_choice_ids\":{\"user_one\":\"1107AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"user_two\":\"1713AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"}},{\"key\":\"spacer\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"spacer\",\"type\":\"spacer\",\"spacer_height\":\"20dp\"},{\"key\":\"user_form_labels\",\"type\":\"label\",\"label_text_style\":\"bold\",\"text\":\"Number of labels\",\"text_color\":\"#000000\",\"v_required\":{\"value\":true}},{\"key\":\"user_form\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"type\":\"numbers_selector\",\"number_of_selectors\":\"5\",\"start_number\":\"1\",\"max_value\":\"15\",\"text_size\":\"16px\",\"text_color\":\"#000000\",\"selected_text_color\":\"#ffffff\",\"v_required\":{\"value\":true}},{\"key\":\"spacer\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"spacer\",\"type\":\"spacer\",\"spacer_height\":\"20dp\"},{\"key\":\"user_sub_form\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"type\":\"native_radio\",\"label\":\"User sub forms?\",\"label_text_style\":\"bold\",\"text_color\":\"#000000\",\"extra_rel\":true,\"has_extra_rel\":\"yes\",\"options\":[{\"key\":\"yes\",\"text\":\"Yes\",\"value\":false,\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"specify_info\":\"User sub specify...\",\"specify_widget\":\"normal_edit_text\",\"specify_info_color\":\"#8C8C8C\",\"secondary_suffix\":\"bpm\",\"content_form\":\"user_native_sub_form\"},{\"key\":\"no\",\"text\":\"No\",\"value\":false,\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\"}],\"v_required\":{\"value\":true,\"err\":\"Please specify user native form.\"}},{\"key\":\"user_check_box\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"openmrs_data_type\":\"select one\",\"type\":\"check_box\",\"label\":\"Do want to select any checkbox?\",\"label_text_style\":\"bold\",\"options\":[{\"key\":\"None\",\"text\":\"None\",\"value\":false,\"openmrs_choice_id\":\"\"},{\"key\":\"yes\",\"text\":\"Yes\",\"value\":false,\"openmrs_choice_id\":\"\"},{\"key\":\"no\",\"text\":\"No\",\"value\":false,\"openmrs_choice_id\":\"\"},{\"key\":\"other\",\"text\":\"Other\",\"value\":false,\"openmrs_choice_id\":\"\"}],\"v_required\":{\"value\":\"false\"},\"value\":\"[yes]\"}]}}";
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.vijay.jsonwizard.presenters;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.AppCompatSpinner;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.vijay.jsonwizard.BaseTest;
import com.vijay.jsonwizard.R;
import com.vijay.jsonwizard.TestConstants;
import com.vijay.jsonwizard.activities.JsonFormActivity;
import com.vijay.jsonwizard.constants.JsonFormConstants;
Expand All @@ -26,7 +30,9 @@
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.powermock.reflect.Whitebox;
import org.robolectric.Robolectric;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowToast;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
Expand All @@ -37,11 +43,14 @@
import static com.vijay.jsonwizard.constants.JsonFormConstants.STEP1;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -91,6 +100,16 @@ public void setUp() throws JSONException {
when(jsonFormActivity.getmJSONObject()).thenReturn(jsonForm);
}

private void initWithActualForm() {
Intent intent = new Intent();
intent.putExtra("json", TestConstants.BASIC_FORM);
jsonFormActivity = spy(Robolectric.buildActivity(JsonFormActivity.class, intent).create().resume().get());
formFragment = spy(JsonFormFragment.getFormFragment("step1"));
jsonFormActivity.getSupportFragmentManager().beginTransaction().add(formFragment, null).commit();
formFragment.onFieldsInvalid = this.onFieldsInvalid;
presenter = formFragment.getPresenter();
}

@Test
public void testAddFormElements() {
Bundle bundle = new Bundle();
Expand Down Expand Up @@ -174,4 +193,65 @@ public void testOnNextClickReturnsFalseIfFormIsInvalid() {
assertFalse(presenter.onNextClick(null));
}


@Test
public void testValidateAndWriteValuesWithInvalidFields() {
initWithActualForm();
presenter.validateAndWriteValues();
assertEquals(4, presenter.getInvalidFields().size());
assertEquals("Please enter the last name", presenter.getInvalidFields().get("step1#Basic Form One:user_last_name").getErrorMessage());
assertEquals("Please enter user age", presenter.getInvalidFields().get("step1#Basic Form One:user_age").getErrorMessage());
assertEquals("Please enter the first name", presenter.getInvalidFields().get("step1#Basic Form One:user_first_name").getErrorMessage());
assertEquals("Please enter the sex", presenter.getInvalidFields().get("step1#Basic Form One:user_spinner").getErrorMessage());
verify(formFragment, times(6)).writeValue(anyString(), anyString(), anyString(), anyString(), anyString(),
anyString(), anyBoolean());
verify(onFieldsInvalid).passInvalidFields(presenter.getInvalidFields());
}


@Test
public void testValidateAndWriteValues() {
initWithActualForm();
presenter.validateAndWriteValues();
assertEquals(4, presenter.getInvalidFields().size());


setTextValue("step1:user_last_name", "Doe");
setTextValue("step1:user_first_name", "John");
setTextValue("step1:user_age", "21");
((AppCompatSpinner) formFragment.getJsonApi().getFormDataView("step1:user_spinner")).setSelection(1);
presenter.validateAndWriteValues();
assertEquals(0, presenter.getInvalidFields().size());
verify(formFragment, times(13)).writeValue(anyString(), anyString(), anyString(), anyString(), anyString(),
anyString(), anyBoolean());
verify(onFieldsInvalid,times(2)).passInvalidFields(presenter.getInvalidFields());
}

private void setTextValue(String address, String value) {
TextView view = (TextView) formFragment.getJsonApi().getFormDataView(address);
view.setTag(R.id.raw_value, value);
view.setText(value);

}


@Test
public void testOnSaveClickDisplaysErrorFragmentAndDisplaysToast() {
initWithActualForm();
formFragment.getMainView().setTag(R.id.skip_validation, false);
presenter.onSaveClick(formFragment.getMainView());
assertEquals(4, presenter.getInvalidFields().size());
assertEquals("Please enter the last name", presenter.getInvalidFields().get("step1#Basic Form One:user_last_name").getErrorMessage());
assertEquals("Please enter user age", presenter.getInvalidFields().get("step1#Basic Form One:user_age").getErrorMessage());
assertEquals("Please enter the first name", presenter.getInvalidFields().get("step1#Basic Form One:user_first_name").getErrorMessage());
assertEquals("Please enter the sex", presenter.getInvalidFields().get("step1#Basic Form One:user_spinner").getErrorMessage());
verify(formFragment, times(6)).writeValue(anyString(), anyString(), anyString(), anyString(), anyString(),
anyString(), anyBoolean());
assertTrue(presenter.getErrorFragment().isVisible());
Toast toast = ShadowToast.getLatestToast();
assertEquals(Toast.LENGTH_SHORT, toast.getDuration());
assertEquals(context.getString(R.string.json_form_error_msg, 4), ShadowToast.getTextOfLatestToast());
}


}

0 comments on commit 0895e9c

Please sign in to comment.