Skip to content

Commit

Permalink
Merge branch 'feature/translate_radio_button_values' of https://githu…
Browse files Browse the repository at this point in the history
…b.com/opensrp/opensrp-client-native-form into feature/translate_radio_button_values
  • Loading branch information
junaidwarsivd committed Jun 28, 2022
2 parents ce39719 + 3691207 commit a161404
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 113 deletions.
1 change: 0 additions & 1 deletion android-json-form-wizard/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ dependencies {
implementation 'com.jakewharton.timber:timber:4.7.1'
implementation 'org.greenrobot:eventbus:3.1.1'
implementation 'com.android.support:multidex:1.0.3'
testImplementation project(path: ':android-json-form-wizard')

// PowerMock
def powerMockVersion = '2.0.4'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.vijay.jsonwizard.validators.edittext;

import android.support.annotation.NonNull;

import com.rengwuxian.materialedittext.validation.METValidator;

/**
Expand All @@ -9,17 +11,22 @@ public class LengthValidator extends METValidator {

protected int minLength = 0;
protected int maxLength = Integer.MAX_VALUE;
protected boolean isRequired;

public LengthValidator(String errorMessage, int minLength, int maxLength) {
public LengthValidator(String errorMessage, int minLength, int maxLength, boolean isRequired) {
super(errorMessage);
this.minLength = minLength;
this.maxLength = maxLength;
this.isRequired = isRequired;
}

@Override
public boolean isValid(CharSequence charSequence, boolean isEmpty) {
return charSequence != null ? !isEmpty && charSequence.length() >= minLength && charSequence.length() <= maxLength && charSequence != null
: false;

public boolean isValid(@NonNull CharSequence charSequence, boolean isEmpty) {
if (isRequired && isEmpty)
return false;
else if (!isRequired && isEmpty)
return true;
else
return (charSequence != null && charSequence.length() >= minLength && charSequence.length() <= maxLength);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
public class MaxLengthValidator extends LengthValidator {

public MaxLengthValidator(String errorMessage, int maxLength) {
super(errorMessage, EditTextFactory.MIN_LENGTH, maxLength);
public MaxLengthValidator(String errorMessage, int maxLength, boolean isRequired) {
super(errorMessage, EditTextFactory.MIN_LENGTH, maxLength, isRequired);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
public class MinLengthValidator extends LengthValidator {

public MinLengthValidator(String errorMessage, int minLength) {
super(errorMessage, minLength, EditTextFactory.MAX_LENGTH);
public MinLengthValidator(String errorMessage, int minLength, boolean isRequired) {
super(errorMessage, minLength, EditTextFactory.MAX_LENGTH, isRequired);
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,16 @@ public Set<String> getCustomTranslatableWidgetFields() {
}


public void cleanUp() {
this.context = null;
this.formFragment = null;
this.radioButton = null;
this.extraInfoTextView = null;
this.specifyTextView = null;
this.reasonsTextView = null;

}

private class CustomTextViewClickListener implements View.OnClickListener {
@Override
public void onClick(View view) {
Expand All @@ -917,13 +927,4 @@ public void onClick(View view) {
radioButton.performClick();
}
}

public void cleanUp() {
this.context = null;
this.formFragment = null;
this.radioButton = null;
this.extraInfoTextView = null;
this.specifyTextView = null;
this.reasonsTextView = null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,32 @@
public class LengthValidatorTest extends BaseTest {

@Test
public void isValidShouldReturnFalseWhenIsEmptyTrue() {
public void isValidShouldReturnTrueWhenEditTextValueIsEmptyAndMinimumLengthIsZeroAndIsNotRequired() {
LengthValidator validator = new LengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_0, CONSTANT_INT_50, false);
Assert.assertEquals(true, validator.isValid("", true));
}

LengthValidator validator = new LengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_0, CONSTANT_INT_50);
Assert.assertEquals(false, validator.isValid(DEFAULT_TEST_MESSAGE, true));
@Test
public void isValidShouldReturnFalseWhenEditTextValueIsEmptyAndIsRequired() {
LengthValidator validator = new LengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_0, CONSTANT_INT_50, true);
Assert.assertEquals(false, validator.isValid("", true));
}

@Test
public void isValidShouldReturnTrueWhenEditTextValueIsNotEmptyAndIsLowerThanMaximumLength() {
LengthValidator validator = new LengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_0, CONSTANT_INT_50, false);
Assert.assertEquals(true, validator.isValid(DEFAULT_TEST_MESSAGE, false));
}

@Test
public void isValidShouldReturnTrueWhenIsEmptyFalse() {
public void isValidShouldReturnFalseWhenEditTextValueIsLongerThanMaximumLength() {
LengthValidator validator = new LengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_0, 20, false);
Assert.assertEquals(false, validator.isValid("This message is longer", false));
}

LengthValidator validator = new LengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_0, CONSTANT_INT_50);
@Test
public void isValidShouldReturnTrueWhenEditTextValueIsLongerThanMinimumLengthAndShorterThanMaximumLength() {
LengthValidator validator = new LengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_0, CONSTANT_INT_50, false);
Assert.assertEquals(true, validator.isValid(DEFAULT_TEST_MESSAGE, false));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,28 @@
import org.junit.Test;

public class MaxLengthValidatorTest extends BaseTest {

@Test
public void isValidShouldReturnFalseWhenStringLengthGreaterThanMaxSetValue() {

MaxLengthValidator validator = new MaxLengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_1);
MaxLengthValidator validator = new MaxLengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_1, false);
Assert.assertEquals(false, validator.isValid(DEFAULT_TEST_MESSAGE, false));

}

@Test
public void isValidShouldReturnTrueWhenStringLengthLessThanMaxSetValue() {

MaxLengthValidator validator = new MaxLengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_50);
MaxLengthValidator validator = new MaxLengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_50, false);
Assert.assertEquals(true, validator.isValid(DEFAULT_TEST_MESSAGE, false));

}

@Test
public void isValidShouldReturnTrueWhenStringLengthEqualToMaxSetValue() {
MaxLengthValidator validator = new MaxLengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_16);
MaxLengthValidator validator = new MaxLengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_16, false);
Assert.assertEquals(true, validator.isValid(DEFAULT_TEST_MESSAGE, false));

}

@Test
public void isValidShouldReturnTrueWhenTheStringIsNull() {
MaxLengthValidator validator = new MaxLengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_1);
MaxLengthValidator validator = new MaxLengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_1, false);
Assert.assertEquals(false, (validator.isValid(null, false)));
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,28 @@
import org.junit.Test;

public class MinLengthValidatorTest extends BaseTest {

@Test
public void isValidShouldReturnTrueWhenStringLengthIsGreaterThanMinSetValue() {

MinLengthValidator validator = new MinLengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_1);
MinLengthValidator validator = new MinLengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_1, false);
Assert.assertEquals(true, validator.isValid(DEFAULT_TEST_MESSAGE, false));

}

@Test
public void isValidShouldReturnFalseWhenStringLengthIsLessThanMinSetValue() {

MinLengthValidator validator = new MinLengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_50);
MinLengthValidator validator = new MinLengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_50, false);
Assert.assertEquals(false, validator.isValid(DEFAULT_TEST_MESSAGE, false));

}

@Test
public void isValidShouldReturnTrueWhenStringLengthIsEqualToMinSetValue() {

MinLengthValidator validator = new MinLengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_16);
MinLengthValidator validator = new MinLengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_16, false);
Assert.assertEquals(true, validator.isValid(DEFAULT_TEST_MESSAGE, false));

}

@Test
public void isValidShouldReturnTrueWhenTheStringIsNull() {
MinLengthValidator validator = new MinLengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_1);
MinLengthValidator validator = new MinLengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_1, false);
Assert.assertEquals(false, (validator.isValid(null, false)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ public void testSelectedButton() throws Exception {
radioButton = Mockito.mock(RadioButton.class);
Whitebox.invokeMethod(factory, "checkSelectedRadioButton", listener, radioButton, value, jsonObject);
ShadowLooper.runUiThreadTasksIncludingDelayedTasks();
Thread.sleep(TIMEOUT);
Mockito.verify(radioButton).setChecked(ArgumentMatchers.anyBoolean());
}

Expand All @@ -148,6 +149,7 @@ public void testSelectedTranslatedButton() throws Exception {
radioButton = Mockito.mock(RadioButton.class);
Whitebox.invokeMethod(factory, "checkSelectedRadioButton", listener, radioButton, value, jsonObject);
ShadowLooper.runUiThreadTasksIncludingDelayedTasks();
Thread.sleep(TIMEOUT);
Mockito.verify(radioButton).setChecked(ArgumentMatchers.anyBoolean());
}
}

0 comments on commit a161404

Please sign in to comment.