From 64defd0246a2debf0692d49f76bb2f5b20f1e984 Mon Sep 17 00:00:00 2001 From: LZRS <12814349+LZRS@users.noreply.github.com> Date: Tue, 23 Nov 2021 14:23:12 +0300 Subject: [PATCH] v2.1.13-SNAPSHOT (#610) * Update to client-utils v0.0.6-SNAPSHOT * configurable visibility for form wizard prev/next icons * configurable greying of next/save menu item when form fields are invalid * configurable hiding of shown date selector dialog when a date form field is invalid --- android-json-form-wizard/build.gradle | 4 +- .../customviews/NativeEditText.java | 15 +++ .../fragments/JsonFormFragment.java | 63 +++++++++- .../fragments/JsonWizardFormFragment.java | 12 +- .../presenters/JsonFormFragmentPresenter.java | 64 ++++++++++ .../jsonwizard/widgets/CheckBoxFactory.java | 5 + .../widgets/NativeRadioButtonFactory.java | 6 + .../widgets/NumberSelectorFactory.java | 7 ++ .../customviews/NativeEditTextTest.java | 119 ++++++++++++++++++ ...FormFragmentPresenterRoboElectricTest.java | 22 ++++ .../widgets/CheckBoxFactoryTest.java | 40 ++++++ gradle.properties | 2 +- 12 files changed, 346 insertions(+), 13 deletions(-) create mode 100644 android-json-form-wizard/src/test/java/com/vijay/jsonwizard/customviews/NativeEditTextTest.java diff --git a/android-json-form-wizard/build.gradle b/android-json-form-wizard/build.gradle index cc3de8ea4..087c6eee6 100644 --- a/android-json-form-wizard/build.gradle +++ b/android-json-form-wizard/build.gradle @@ -113,7 +113,7 @@ dependencies { exclude group: 'com.android.support', module: 'recyclerview-v7' } - implementation 'org.smartregister:opensrp-client-utils:0.0.4-SNAPSHOT' + implementation 'org.smartregister:opensrp-client-utils:0.0.6-SNAPSHOT' implementation 'org.jeasy:easy-rules-core:3.3.0' implementation 'org.jeasy:easy-rules-mvel:3.3.0' @@ -146,7 +146,7 @@ dependencies { testImplementation "org.powermock:powermock-module-junit4-rule:$powerMockVersion" testImplementation "org.powermock:powermock-api-mockito2:$powerMockVersion" testImplementation "org.powermock:powermock-classloading-xstream:$powerMockVersion" - implementation('com.github.raihan-mpower:FancyAlertDialog-Android:0.3') { + implementation('com.github.raihan-mpower:FancyAlertDialog-Android:0.3@aar') { exclude group: 'com.android.support', module: 'appcompat-v7' } diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/customviews/NativeEditText.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/customviews/NativeEditText.java index 9f7186109..01345c54a 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/customviews/NativeEditText.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/customviews/NativeEditText.java @@ -145,6 +145,21 @@ public boolean validateWith(@NonNull METValidator validator) { return isValid; } + public boolean isFilledValidly(){ + boolean noValidators = validators == null || validators.isEmpty(); + if (noValidators) return true; + + boolean valid = true; + for (METValidator validator: validators){ + valid = valid && validator.isValid(getText(), isEmpty()); + } + return valid; + } + + public boolean isEmpty(){ + return getText().length() == 0; + } + /** * Check all validators, sets the error text if not *

diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/fragments/JsonFormFragment.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/fragments/JsonFormFragment.java index f7d815390..76d9e775f 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/fragments/JsonFormFragment.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/fragments/JsonFormFragment.java @@ -4,13 +4,19 @@ import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; +import android.graphics.Color; import android.os.Bundle; +import android.os.Handler; +import android.os.Message; import android.support.annotation.DrawableRes; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v7.app.ActionBar; import android.support.v7.widget.AppCompatRadioButton; import android.support.v7.widget.Toolbar; +import android.text.SpannableString; import android.text.TextUtils; +import android.text.style.ForegroundColorSpan; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; @@ -31,6 +37,7 @@ import com.vijay.jsonwizard.activities.JsonFormActivity; import com.vijay.jsonwizard.constants.JsonFormConstants; import com.vijay.jsonwizard.customviews.RadioButton; +import com.vijay.jsonwizard.domain.Form; import com.vijay.jsonwizard.interfaces.CommonListener; import com.vijay.jsonwizard.interfaces.JsonApi; import com.vijay.jsonwizard.interfaces.OnFieldsInvalid; @@ -42,6 +49,7 @@ import com.vijay.jsonwizard.viewstates.JsonFormFragmentViewState; import org.apache.commons.lang3.StringUtils; +import org.jetbrains.annotations.NotNull; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -59,8 +67,10 @@ * Created by vijay on 5/7/15. */ public class JsonFormFragment extends MvpFragment - implements CommonListener, JsonFormFragmentView { + implements CommonListener, JsonFormFragmentView, Handler.Callback { private static final String TAG = "JsonFormFragment"; + private static final int GRAY_OUT_ACTIVE_WHAT = 1212; + public OnFieldsInvalid onFieldsInvalid; protected LinearLayout mMainView; protected ScrollView mScrollView; @@ -76,6 +86,8 @@ public class JsonFormFragment extends MvpFragment() { + @Override + public Boolean answer(InvocationOnMock invocation) throws Throwable { + String test = invocation.getArgument(0).toString(); + return test.equalsIgnoreCase("test"); + } + }); + } + + @Test + public void testIsValid() { + String regex = ".*"; + boolean isValid = nativeEditText.isValid(regex); + Assert.assertTrue(isValid); + } + + @Test + public void testInitWatchers() throws Exception { + Whitebox.invokeMethod(nativeEditText, "initTextWatcher"); + nativeEditText.setText("new text"); + when(nativeEditText.getText()).thenCallRealMethod(); + Assert.assertEquals("new text", nativeEditText.getText().toString()); + } + + @Test + public void testValidateWith() { + METValidator isNotEmptyValidator = mock(METValidator.class); + when(isNotEmptyValidator.isValid(any(CharSequence.class), anyBoolean())).thenAnswer(new Answer() { + @Override + public Boolean answer(InvocationOnMock invocation) { + String text = invocation.getArgument(0).toString(); + return text.length() > 0; + } + }); + boolean isValid = nativeEditText.validateWith(isNotEmptyValidator); + verify(isNotEmptyValidator).isValid(eq(nativeEditText.getText()), anyBoolean()); + Assert.assertTrue(isValid); + } + + @Test + public void testClearValidators() { + Assert.assertFalse(nativeEditText.hasValidators()); + nativeEditText.addValidator(testValidator); + Assert.assertTrue(nativeEditText.hasValidators()); + + nativeEditText.clearValidators(); + Assert.assertFalse(nativeEditText.hasValidators()); + } + + + @Test + public void testIsFilledValidly() { + nativeEditText.addValidator(testValidator); + boolean isValid = nativeEditText.isFilledValidly(); + Assert.assertTrue(isValid); + } + + @Test + public void testValidate(){ + nativeEditText.addValidator(testValidator); + boolean isValid = nativeEditText.validate(); + Assert.assertTrue(isValid); + } + + @Test + public void testIsEmpty() { + Assert.assertEquals(nativeEditText.getText().length() == 0, nativeEditText.isEmpty()); + } + + + @Test + public void testAddValidator() { + Assert.assertFalse(nativeEditText.hasValidators()); + nativeEditText.addValidator(testValidator); + Assert.assertTrue(nativeEditText.hasValidators()); + Assert.assertNotNull(nativeEditText.getValidators()); + Assert.assertTrue(nativeEditText.getValidators().contains(testValidator)); + } +} \ No newline at end of file diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/presenters/JsonFormFragmentPresenterRoboElectricTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/presenters/JsonFormFragmentPresenterRoboElectricTest.java index bcbeb1f26..f60165844 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/presenters/JsonFormFragmentPresenterRoboElectricTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/presenters/JsonFormFragmentPresenterRoboElectricTest.java @@ -352,6 +352,21 @@ public void testValidateAndWriteValues() throws InterruptedException { verify(onFieldsInvalid, times(2)).passInvalidFields(presenter.getInvalidFields()); } + @Test + public void testAreFormViewsFilledValid() throws InterruptedException { + initWithActualForm(); + boolean valid = presenter.areFormViewsFilled(); + shadowOf(getMainLooper()).idle(); + assertFalse(valid); + + 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, false); + boolean newValid = presenter.areFormViewsFilled(); + assertTrue(newValid); + } + private void setTextValue(String address, String value) { TextView view = (TextView) formFragment.getJsonApi().getFormDataView(address); view.setTag(R.id.raw_value, value); @@ -713,4 +728,11 @@ public void testOnSpinnerEditBtnClickShouldResetWidgetReadOnly() throws JSONExce expectedField = FormUtils.getFieldFromForm(jsonForm, (String) view.getTag(R.id.key)); assertFalse(expectedField.optBoolean(JsonFormConstants.READ_ONLY)); } + + @Test + public void testCleanUp(){ + presenter.cleanUp(); + boolean cleanUpAndExit = Whitebox.getInternalState(presenter, "cleanupAndExit"); + assertTrue(cleanUpAndExit); + } } diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/CheckBoxFactoryTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/CheckBoxFactoryTest.java index 41f371fcb..26b499d13 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/CheckBoxFactoryTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/CheckBoxFactoryTest.java @@ -14,7 +14,9 @@ import com.vijay.jsonwizard.fragments.JsonFormFragment; import com.vijay.jsonwizard.interfaces.CommonListener; import com.vijay.jsonwizard.utils.FormUtils; +import com.vijay.jsonwizard.utils.ValidationStatus; import com.vijay.jsonwizard.views.CustomTextView; +import com.vijay.jsonwizard.views.JsonFormFragmentView; import org.json.JSONArray; import org.json.JSONObject; @@ -130,4 +132,42 @@ public void testPerformValidation() throws Exception { Assert.assertTrue(checked); } + + @Test + public void testIsValid() throws Exception { + Assert.assertNotNull(factory); + CheckBoxFactory factorySpy = Mockito.spy(factory); + Assert.assertNotNull(rootLayout); + LinearLayout rootLayoutSpy = Mockito.spy(rootLayout); + + LinearLayout checkboxOptionLayout = Mockito.mock(LinearLayout.class); + CheckBox currentCheckbox = Mockito.mock(CheckBox.class); + + Mockito.doReturn(2).when(rootLayoutSpy).getChildCount(); + Mockito.doReturn(checkboxOptionLayout).when(rootLayoutSpy).getChildAt(Mockito.anyInt()); + Mockito.doReturn(currentCheckbox).when(checkboxOptionLayout).getChildAt(0); + Mockito.doReturn(true).when(currentCheckbox).isChecked(); + + boolean isValid = factorySpy.isValid(rootLayoutSpy); + Assert.assertTrue(isValid); + } + + @Test + public void testValidate(){ + CheckBoxFactory factorySpy = Mockito.spy(factory); + LinearLayout rootLayoutSpy = Mockito.spy(rootLayout); + JsonFormFragmentView fragmentView = Mockito.mock(JsonFormFragmentView.class); + LinearLayout checkboxOptionLayout = Mockito.mock(LinearLayout.class); + CheckBox currentCheckbox = Mockito.mock(CheckBox.class); + Mockito.doReturn(2).when(rootLayoutSpy).getChildCount(); + Mockito.doReturn(checkboxOptionLayout).when(rootLayoutSpy).getChildAt(Mockito.anyInt()); + Mockito.doReturn(currentCheckbox).when(checkboxOptionLayout).getChildAt(0); + Mockito.doReturn(true).when(currentCheckbox).isChecked(); + Mockito.when(rootLayoutSpy.getTag(ArgumentMatchers.anyInt())).thenReturn("error"); + Mockito.doReturn("error").when(rootLayoutSpy).getTag(); + + ValidationStatus validationStatus = factorySpy.validate(fragmentView, rootLayoutSpy); + Assert.assertTrue(validationStatus.isValid()); + + } } diff --git a/gradle.properties b/gradle.properties index 2c3faa76c..02f37824f 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -VERSION_NAME=2.1.12-SNAPSHOT +VERSION_NAME=2.1.13-SNAPSHOT VERSION_CODE=1 GROUP=org.smartregister POM_SETTING_DESCRIPTION=OpenSRP Client Native Form Json Wizard