Skip to content

Commit

Permalink
Merge pull request #6088 from grzesiek2010/COLLECT-6073
Browse files Browse the repository at this point in the history
Fixed issues caused by the new 'masked' appearance
  • Loading branch information
seadowg authored Apr 26, 2024
2 parents ac23a4f + bfad220 commit bf66ba3
Show file tree
Hide file tree
Showing 12 changed files with 197 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ object Appearances {

@JvmStatic
fun useThousandSeparator(prompt: FormEntryPrompt): Boolean {
return getSanitizedAppearanceHint(prompt).contains(THOUSANDS_SEP)
return getSanitizedAppearanceHint(prompt).contains(THOUSANDS_SEP) && !isMasked(prompt)
}

@JvmStatic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ class WidgetAnswerText(context: Context, attrs: AttributeSet?) : FrameLayout(con

val binding = WidgetAnswerTextBinding.inflate(LayoutInflater.from(context), this, true)

private var isMasked: Boolean = false

fun init(fontSize: Float, readOnly: Boolean, numberOfRows: Int?, isMasked: Boolean, afterTextChanged: Runnable) {
binding.editText.id = generateViewId()
binding.textView.id = generateViewId()
Expand All @@ -61,9 +59,11 @@ class WidgetAnswerText(context: Context, attrs: AttributeSet?) : FrameLayout(con
}
})
if (isMasked) {
this.isMasked = isMasked
binding.editText.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
binding.editText.transformationMethod = PasswordTransformationMethod.getInstance()
binding.textView.transformationMethod = PasswordTransformationMethod.getInstance()
} else {
binding.editText.inputType = InputType.TYPE_CLASS_TEXT
}
}

Expand All @@ -83,7 +83,6 @@ class WidgetAnswerText(context: Context, attrs: AttributeSet?) : FrameLayout(con
binding.editText.addTextChangedListener(ThousandsSeparatorTextWatcher(binding.editText))
}

binding.editText.inputType = InputType.TYPE_NUMBER_FLAG_SIGNED
binding.editText.keyListener = DigitsKeyListener(true, false) // only allows numbers and no periods

// ints can only hold 2,147,483,648. we allow 999,999,999
Expand All @@ -98,19 +97,13 @@ class WidgetAnswerText(context: Context, attrs: AttributeSet?) : FrameLayout(con
if (answer != null) {
setAnswer(String.format(Locale.US, "%d", answer))
}

if (isMasked) {
binding.editText.transformationMethod = PasswordTransformationMethod.getInstance()
binding.textView.transformationMethod = PasswordTransformationMethod.getInstance()
}
}

fun setStringNumberType(useThousandSeparator: Boolean, answer: String?) {
if (useThousandSeparator) {
binding.editText.addTextChangedListener(ThousandsSeparatorTextWatcher(binding.editText))
}

binding.editText.inputType = InputType.TYPE_NUMBER_FLAG_SIGNED
binding.editText.keyListener = object : DigitsKeyListener() {
override fun getAcceptedChars(): CharArray {
return charArrayOf(
Expand All @@ -122,19 +115,13 @@ class WidgetAnswerText(context: Context, attrs: AttributeSet?) : FrameLayout(con
if (answer != null) {
setAnswer(answer)
}

if (isMasked) {
binding.editText.transformationMethod = PasswordTransformationMethod.getInstance()
binding.textView.transformationMethod = PasswordTransformationMethod.getInstance()
}
}

fun setDecimalType(useThousandSeparator: Boolean, answer: Double?) {
if (useThousandSeparator) {
binding.editText.addTextChangedListener(ThousandsSeparatorTextWatcher(binding.editText))
}

binding.editText.inputType = InputType.TYPE_NUMBER_FLAG_DECIMAL
binding.editText.keyListener = DigitsKeyListener(true, true) // only numbers are allowed

// only 15 characters allowed
Expand All @@ -154,11 +141,6 @@ class WidgetAnswerText(context: Context, attrs: AttributeSet?) : FrameLayout(con
val formattedValue: String = nf.format(answer)
setAnswer(formattedValue)
}

if (isMasked) {
binding.editText.transformationMethod = PasswordTransformationMethod.getInstance()
binding.textView.transformationMethod = PasswordTransformationMethod.getInstance()
}
}

fun getAnswer(): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,12 @@ class AppearancesTest {
assertFalse(Appearances.useThousandSeparator(formEntryPrompt))
}

@Test
fun `useThousandSeparator returns false when 'thousands-sep' appearance is found but mixed with 'masked'`() {
whenever(formEntryPrompt.appearanceHint).thenReturn("thousands-sep masked")
assertFalse(Appearances.useThousandSeparator(formEntryPrompt))
}

@Test
fun `useThousandSeparator returns true when 'thousands-sep' appearance is found`() {
whenever(formEntryPrompt.appearanceHint).thenReturn("THOUSANDS-SEP")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.when;
import static org.odk.collect.android.utilities.Appearances.MASKED;
import static org.odk.collect.android.utilities.Appearances.THOUSANDS_SEP;

import android.text.InputType;
import android.text.method.PasswordTransformationMethod;
import android.text.method.SingleLineTransformationMethod;

public class DecimalWidgetTest extends GeneralStringWidgetTest<DecimalWidget, DecimalData> {

private final Random random = new Random();
Expand Down Expand Up @@ -205,4 +210,21 @@ public void separatorsShouldBeAddedWhenEnabled() {
assertEquals("123,456,789.54", getWidget().widgetAnswerText.getBinding().editText.getText().toString());
assertEquals("123,456,789.54", getWidget().widgetAnswerText.getBinding().textView.getText().toString());
}

@Override
@Test
public void verifyInputType() {
DecimalWidget widget = getWidget();
assertThat(widget.widgetAnswerText.getBinding().editText.getInputType(), equalTo(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_NUMBER_FLAG_DECIMAL));
assertThat(widget.widgetAnswerText.getBinding().editText.getTransformationMethod().getClass(), equalTo(SingleLineTransformationMethod.class));
}

@Override
@Test
public void verifyInputTypeWithMaskedAppearance() {
when(formEntryPrompt.getAppearanceHint()).thenReturn(MASKED);
DecimalWidget widget = getWidget();
assertThat(widget.widgetAnswerText.getBinding().editText.getInputType(), equalTo(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_NUMBER_FLAG_DECIMAL));
assertThat(widget.widgetAnswerText.getBinding().editText.getTransformationMethod().getClass(), equalTo(PasswordTransformationMethod.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.when;
import static org.odk.collect.android.utilities.Appearances.MASKED;
import static org.odk.collect.android.utilities.Appearances.THOUSANDS_SEP;

import android.text.InputType;
import android.text.method.PasswordTransformationMethod;
import android.text.method.SingleLineTransformationMethod;

/**
* @author James Knight
*/
Expand Down Expand Up @@ -88,4 +93,23 @@ public void separatorsShouldBeAddedWhenEnabled() {
assertEquals("123,456,789.54", getWidget().binding.widgetAnswerText.getBinding().editText.getText().toString());
assertEquals("123,456,789.54", getWidget().binding.widgetAnswerText.getBinding().textView.getText().toString());
}

@Override
@Test
public void verifyInputType() {
ExDecimalWidget widget = getWidget();
assertThat(widget.binding.widgetAnswerText.getBinding().editText.getInputType(), equalTo(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_NUMBER_FLAG_DECIMAL));
assertThat(widget.binding.widgetAnswerText.getBinding().editText.getTransformationMethod().getClass(), equalTo(SingleLineTransformationMethod.class));
assertThat(widget.binding.widgetAnswerText.getBinding().textView.getTransformationMethod(), equalTo(null));
}

@Override
@Test
public void verifyInputTypeWithMaskedAppearance() {
when(formEntryPrompt.getAppearanceHint()).thenReturn(MASKED);
ExDecimalWidget widget = getWidget();
assertThat(widget.binding.widgetAnswerText.getBinding().editText.getInputType(), equalTo(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_NUMBER_FLAG_DECIMAL));
assertThat(widget.binding.widgetAnswerText.getBinding().editText.getTransformationMethod().getClass(), equalTo(PasswordTransformationMethod.class));
assertThat(widget.binding.widgetAnswerText.getBinding().textView.getTransformationMethod().getClass(), equalTo(PasswordTransformationMethod.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,16 @@
import org.odk.collect.android.widgets.utilities.StringRequester;

import static junit.framework.TestCase.assertEquals;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.when;
import static org.odk.collect.android.utilities.Appearances.MASKED;
import static org.odk.collect.android.utilities.Appearances.THOUSANDS_SEP;

import android.text.InputType;
import android.text.method.PasswordTransformationMethod;
import android.text.method.SingleLineTransformationMethod;

/**
* @author James Knight
*/
Expand Down Expand Up @@ -60,4 +67,23 @@ public void separatorsShouldBeAddedWhenEnabled() {
assertEquals("123,456,789", getWidget().binding.widgetAnswerText.getBinding().editText.getText().toString());
assertEquals("123,456,789", getWidget().binding.widgetAnswerText.getBinding().textView.getText().toString());
}

@Override
@Test
public void verifyInputType() {
ExIntegerWidget widget = getWidget();
assertThat(widget.binding.widgetAnswerText.getBinding().editText.getInputType(), equalTo(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED));
assertThat(widget.binding.widgetAnswerText.getBinding().editText.getTransformationMethod().getClass(), equalTo(SingleLineTransformationMethod.class));
assertThat(widget.binding.widgetAnswerText.getBinding().textView.getTransformationMethod(), equalTo(null));
}

@Override
@Test
public void verifyInputTypeWithMaskedAppearance() {
when(formEntryPrompt.getAppearanceHint()).thenReturn(MASKED);
ExIntegerWidget widget = getWidget();
assertThat(widget.binding.widgetAnswerText.getBinding().editText.getInputType(), equalTo(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED));
assertThat(widget.binding.widgetAnswerText.getBinding().editText.getTransformationMethod().getClass(), equalTo(PasswordTransformationMethod.class));
assertThat(widget.binding.widgetAnswerText.getBinding().textView.getTransformationMethod().getClass(), equalTo(PasswordTransformationMethod.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@
import net.bytebuddy.utility.RandomString;

import org.javarosa.core.model.data.StringData;
import org.junit.Test;
import org.mockito.Mock;
import org.odk.collect.android.formentry.questions.QuestionDetails;
import org.odk.collect.android.widgets.base.GeneralExStringWidgetTest;
import org.odk.collect.android.widgets.support.FakeWaitingForDataRegistry;
import org.odk.collect.android.widgets.utilities.StringRequester;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.Mockito.when;
import static org.odk.collect.android.utilities.Appearances.MASKED;

import android.text.InputType;
import android.text.method.PasswordTransformationMethod;
import android.text.method.SingleLineTransformationMethod;

/**
* @author James Knight
Expand Down Expand Up @@ -39,4 +47,23 @@ public void setUp() throws Exception {
super.setUp();
when(formEntryPrompt.getAppearanceHint()).thenReturn("");
}

@Override
@Test
public void verifyInputType() {
ExStringWidget widget = getWidget();
assertThat(widget.binding.widgetAnswerText.getBinding().editText.getInputType(), equalTo(InputType.TYPE_CLASS_TEXT));
assertThat(widget.binding.widgetAnswerText.getBinding().editText.getTransformationMethod().getClass(), equalTo(SingleLineTransformationMethod.class));
assertThat(widget.binding.widgetAnswerText.getBinding().textView.getTransformationMethod(), equalTo(null));
}

@Override
@Test
public void verifyInputTypeWithMaskedAppearance() {
when(formEntryPrompt.getAppearanceHint()).thenReturn(MASKED);
ExStringWidget widget = getWidget();
assertThat(widget.binding.widgetAnswerText.getBinding().editText.getInputType(), equalTo(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD));
assertThat(widget.binding.widgetAnswerText.getBinding().editText.getTransformationMethod().getClass(), equalTo(PasswordTransformationMethod.class));
assertThat(widget.binding.widgetAnswerText.getBinding().textView.getTransformationMethod().getClass(), equalTo(PasswordTransformationMethod.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@
import org.odk.collect.android.widgets.base.GeneralStringWidgetTest;

import static junit.framework.TestCase.assertEquals;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.when;
import static org.odk.collect.android.utilities.Appearances.MASKED;
import static org.odk.collect.android.utilities.Appearances.THOUSANDS_SEP;

import android.text.InputType;
import android.text.method.PasswordTransformationMethod;
import android.text.method.SingleLineTransformationMethod;

/**
* @author James Knight
*/
Expand Down Expand Up @@ -47,4 +54,21 @@ public void separatorsShouldBeAddedWhenEnabled() {
assertEquals("123,456,789", getWidget().widgetAnswerText.getBinding().editText.getText().toString());
assertEquals("123,456,789", getWidget().widgetAnswerText.getBinding().textView.getText().toString());
}

@Override
@Test
public void verifyInputType() {
IntegerWidget widget = getWidget();
assertThat(widget.widgetAnswerText.getBinding().editText.getInputType(), equalTo(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED));
assertThat(widget.widgetAnswerText.getBinding().editText.getTransformationMethod().getClass(), equalTo(SingleLineTransformationMethod.class));
}

@Override
@Test
public void verifyInputTypeWithMaskedAppearance() {
when(formEntryPrompt.getAppearanceHint()).thenReturn(MASKED);
IntegerWidget widget = getWidget();
assertThat(widget.widgetAnswerText.getBinding().editText.getInputType(), equalTo(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED));
assertThat(widget.widgetAnswerText.getBinding().editText.getTransformationMethod().getClass(), equalTo(PasswordTransformationMethod.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@
import org.odk.collect.android.widgets.base.GeneralStringWidgetTest;

import static junit.framework.TestCase.assertEquals;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.Mockito.when;
import static org.odk.collect.android.utilities.Appearances.MASKED;
import static org.odk.collect.android.utilities.Appearances.THOUSANDS_SEP;

import android.text.InputType;
import android.text.method.PasswordTransformationMethod;
import android.text.method.SingleLineTransformationMethod;

/**
* @author James Knight
*/
Expand Down Expand Up @@ -45,4 +52,21 @@ public void separatorsShouldBeAddedWhenEnabled() {
assertEquals("123,456,789,123,456,789,123,456,789,123,456,789", getWidget().widgetAnswerText.getBinding().editText.getText().toString());
assertEquals("123,456,789,123,456,789,123,456,789,123,456,789", getWidget().widgetAnswerText.getBinding().textView.getText().toString());
}

@Override
@Test
public void verifyInputType() {
StringNumberWidget widget = getWidget();
assertThat(widget.widgetAnswerText.getBinding().editText.getInputType(), equalTo(InputType.TYPE_CLASS_NUMBER));
assertThat(widget.widgetAnswerText.getBinding().editText.getTransformationMethod().getClass(), equalTo(SingleLineTransformationMethod.class));
}

@Override
@Test
public void verifyInputTypeWithMaskedAppearance() {
when(formEntryPrompt.getAppearanceHint()).thenReturn(MASKED);
StringNumberWidget widget = getWidget();
assertThat(widget.widgetAnswerText.getBinding().editText.getInputType(), equalTo(InputType.TYPE_CLASS_NUMBER));
assertThat(widget.widgetAnswerText.getBinding().editText.getTransformationMethod().getClass(), equalTo(PasswordTransformationMethod.class));
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
package org.odk.collect.android.widgets;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.Mockito.when;
import static org.odk.collect.android.utilities.Appearances.MASKED;

import android.text.InputType;
import android.text.method.PasswordTransformationMethod;
import android.text.method.SingleLineTransformationMethod;

import androidx.annotation.NonNull;

import net.bytebuddy.utility.RandomString;

import org.javarosa.core.model.data.StringData;
import org.junit.Test;
import org.odk.collect.android.formentry.questions.QuestionDetails;
import org.odk.collect.android.widgets.base.GeneralStringWidgetTest;

Expand All @@ -24,4 +34,22 @@ public StringWidget createWidget() {
public StringData getNextAnswer() {
return new StringData(RandomString.make());
}

@Override
@Test
public void verifyInputType() {
StringWidget widget = getWidget();
assertThat(widget.widgetAnswerText.getBinding().editText.getInputType(), equalTo(InputType.TYPE_CLASS_TEXT));
assertThat(widget.widgetAnswerText.getBinding().editText.getTransformationMethod().getClass(), equalTo(SingleLineTransformationMethod.class));
assertThat(widget.widgetAnswerText.getBinding().textView.getTransformationMethod(), equalTo(null));
}

@Override
@Test
public void verifyInputTypeWithMaskedAppearance() {
when(formEntryPrompt.getAppearanceHint()).thenReturn(MASKED);
StringWidget widget = getWidget();
assertThat(widget.widgetAnswerText.getBinding().editText.getInputType(), equalTo(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD));
assertThat(widget.widgetAnswerText.getBinding().editText.getTransformationMethod().getClass(), equalTo(PasswordTransformationMethod.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,10 @@ public void answersShouldBeMaskedIfMaskedAppearanceIsUsed() {
assertThat(getSpyWidget().binding.widgetAnswerText.getBinding().editText.getTransformationMethod(), is(instanceOf(PasswordTransformationMethod.class)));
assertThat(getSpyWidget().binding.widgetAnswerText.getBinding().textView.getTransformationMethod(), is(instanceOf(PasswordTransformationMethod.class)));
}

@Test
public abstract void verifyInputType();

@Test
public abstract void verifyInputTypeWithMaskedAppearance();
}
Loading

0 comments on commit bf66ba3

Please sign in to comment.