Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not support the masked appearance in numeric questions #6187

Merged
merged 1 commit into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,8 @@ object QuestionAnswerProcessor {

if (!fep.answerText.isNullOrBlank() &&
Appearances.isMasked(fep) &&
fep.controlType == Constants.CONTROL_INPUT && (
fep.dataType == Constants.DATATYPE_TEXT ||
fep.dataType == Constants.DATATYPE_INTEGER ||
fep.dataType == Constants.DATATYPE_DECIMAL
)
fep.controlType == Constants.CONTROL_INPUT &&
fep.dataType == Constants.DATATYPE_TEXT
) {
return "••••••••••"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.odk.collect.android.utilities

import android.content.res.Configuration
import org.javarosa.core.model.Constants
import org.javarosa.form.api.FormEntryPrompt
import org.odk.collect.android.dynamicpreload.ExternalDataUtil
import org.odk.collect.androidshared.utils.ScreenUtils
Expand Down Expand Up @@ -195,6 +196,6 @@ object Appearances {
@JvmStatic
fun isMasked(prompt: FormEntryPrompt): Boolean {
val appearance = getSanitizedAppearanceHint(prompt)
return appearance.contains(MASKED)
return appearance.contains(MASKED) && prompt.dataType == Constants.DATATYPE_TEXT
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,73 +27,57 @@ class QuestionAnswerProcessorTest {
}

@Test
fun noAnswerShouldBeDisplayedIfItDoesNotExistAndMaskedAppearanceIsUsedForTextAndNumberDataTypes() {
listOf(
Constants.DATATYPE_TEXT,
Constants.DATATYPE_INTEGER,
Constants.DATATYPE_DECIMAL
).forEach {
val question = mock<QuestionDef>()
val prompt = MockFormEntryPromptBuilder()
.withQuestion(question)
.withDataType(it)
.withControlType(Constants.CONTROL_INPUT)
.withAppearance(Appearances.MASKED)
.build()
fun noAnswerShouldBeDisplayedIfItDoesNotExistAndMaskedAppearanceIsUsedForTextDataTypes() {
val question = mock<QuestionDef>()
val prompt = MockFormEntryPromptBuilder()
.withQuestion(question)
.withDataType(Constants.DATATYPE_TEXT)
.withControlType(Constants.CONTROL_INPUT)
.withAppearance(Appearances.MASKED)
.build()

val answer = QuestionAnswerProcessor.getQuestionAnswer(prompt, mock(), mock())
val answer = QuestionAnswerProcessor.getQuestionAnswer(prompt, mock(), mock())

assertThat(answer, equalTo(""))
}
assertThat(answer, equalTo(""))
}

@Test
fun noAnswerShouldBeDisplayedIfItIsEmptyAndMaskedAppearanceIsUsedForTextAndNumberDataTypes() {
listOf(
Constants.DATATYPE_TEXT,
Constants.DATATYPE_INTEGER,
Constants.DATATYPE_DECIMAL
).forEach {
val question = mock<QuestionDef>()
val prompt = MockFormEntryPromptBuilder()
.withQuestion(question)
.withAnswerDisplayText("")
.withAppearance(Appearances.MASKED)
.withControlType(Constants.CONTROL_INPUT)
.withDataType(it)
.build()
fun noAnswerShouldBeDisplayedIfItIsEmptyAndMaskedAppearanceIsUsedForTextDataTypes() {
val question = mock<QuestionDef>()
val prompt = MockFormEntryPromptBuilder()
.withQuestion(question)
.withAnswerDisplayText("")
.withAppearance(Appearances.MASKED)
.withControlType(Constants.CONTROL_INPUT)
.withDataType(Constants.DATATYPE_TEXT)
.build()

val answer = QuestionAnswerProcessor.getQuestionAnswer(prompt, mock(), mock())
val answer = QuestionAnswerProcessor.getQuestionAnswer(prompt, mock(), mock())

assertThat(answer, equalTo(""))
}
assertThat(answer, equalTo(""))
}

@Test
fun maskedAnswerShouldBeDisplayedIfItExistAndMaskedAppearanceIsUsedForTextAndNumberDataTypes() {
listOf(
Constants.DATATYPE_TEXT,
Constants.DATATYPE_INTEGER,
Constants.DATATYPE_DECIMAL
).forEach {
val question = mock<QuestionDef>()
val prompt = MockFormEntryPromptBuilder()
.withQuestion(question)
.withAnswerDisplayText("blah")
.withAppearance(Appearances.MASKED)
.withControlType(Constants.CONTROL_INPUT)
.withDataType(it)
.build()
fun maskedAnswerShouldBeDisplayedIfItExistAndMaskedAppearanceIsUsedForTextDataTypes() {
val question = mock<QuestionDef>()
val prompt = MockFormEntryPromptBuilder()
.withQuestion(question)
.withAnswerDisplayText("blah")
.withAppearance(Appearances.MASKED)
.withControlType(Constants.CONTROL_INPUT)
.withDataType(Constants.DATATYPE_TEXT)
.build()

val answer = QuestionAnswerProcessor.getQuestionAnswer(prompt, mock(), mock())
val answer = QuestionAnswerProcessor.getQuestionAnswer(prompt, mock(), mock())

assertThat(answer, equalTo("••••••••••"))
}
assertThat(answer, equalTo("••••••••••"))
}

@Test
fun originalAnswerShouldBeDisplayedIfItExistAndMaskedAppearanceIsUsedForDataTypesOtherThanTextAndNumber() {
fun originalAnswerShouldBeDisplayedIfItExistAndMaskedAppearanceIsUsedForDataTypesOtherThanText() {
listOf(
Constants.DATATYPE_INTEGER,
Constants.DATATYPE_DECIMAL,
Constants.DATATYPE_DATE_TIME,
Constants.DATATYPE_DATE,
Constants.DATATYPE_TIME,
Expand All @@ -120,7 +104,7 @@ class QuestionAnswerProcessorTest {
}

@Test
fun originalAnswerShouldBeDisplayedIfItExistAndMaskedAppearanceIsUsedButControlTypeIsOtherThanInput() {
fun originalAnswerShouldBeDisplayedIfItExistAndMaskedAppearanceIsUsedForDataTypesAndControlTypeOtherThanInput() {
listOf(
Constants.CONTROL_RANGE,
Constants.CONTROL_RANK,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import android.content.res.Configuration
import junit.framework.TestCase.assertEquals
import junit.framework.TestCase.assertFalse
import junit.framework.TestCase.assertTrue
import org.javarosa.core.model.Constants
import org.javarosa.form.api.FormEntryPrompt
import org.junit.Test
import org.mockito.kotlin.mock
Expand Down Expand Up @@ -233,7 +234,8 @@ class AppearancesTest {
}

@Test
fun `useThousandSeparator returns false when 'thousands-sep' appearance is found but mixed with 'masked'`() {
fun `useThousandSeparator returns false when 'thousands-sep' appearance is found but mixed with 'masked' for text questions`() {
whenever(formEntryPrompt.dataType).thenReturn(Constants.DATATYPE_TEXT)
whenever(formEntryPrompt.appearanceHint).thenReturn("thousands-sep masked")
assertFalse(Appearances.useThousandSeparator(formEntryPrompt))
}
Expand Down Expand Up @@ -369,6 +371,7 @@ class AppearancesTest {

@Test
fun `isMasked returns false when there is no appearance`() {
whenever(formEntryPrompt.dataType).thenReturn(Constants.DATATYPE_TEXT)
assertFalse(Appearances.isMasked(formEntryPrompt))

whenever(formEntryPrompt.appearanceHint).thenReturn("")
Expand All @@ -377,13 +380,25 @@ class AppearancesTest {

@Test
fun `isMasked returns false when non of supported appearances is found`() {
whenever(formEntryPrompt.dataType).thenReturn(Constants.DATATYPE_TEXT)
whenever(formEntryPrompt.appearanceHint).thenReturn("blah")
assertFalse(Appearances.isMasked(formEntryPrompt))
}

@Test
fun `isMasked returns true when 'masked' appearance is found`() {
fun `isMasked returns true when 'masked' appearance is found for text questions`() {
whenever(formEntryPrompt.dataType).thenReturn(Constants.DATATYPE_TEXT)
whenever(formEntryPrompt.appearanceHint).thenReturn("masked")
assertTrue(Appearances.isMasked(formEntryPrompt))
}

@Test
fun `isMasked returns false when 'masked' appearance is found for numeric questions`() {
whenever(formEntryPrompt.dataType).thenReturn(Constants.DATATYPE_INTEGER)
whenever(formEntryPrompt.appearanceHint).thenReturn("masked")
assertFalse(Appearances.isMasked(formEntryPrompt))

whenever(formEntryPrompt.dataType).thenReturn(Constants.DATATYPE_DECIMAL)
assertFalse(Appearances.isMasked(formEntryPrompt))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,28 @@

import androidx.annotation.NonNull;

import org.javarosa.core.model.Constants;
import org.javarosa.core.model.QuestionDef;
import org.javarosa.core.model.data.DecimalData;
import org.javarosa.core.model.data.IAnswerData;
import org.junit.Test;
import org.mockito.Mock;
import org.odk.collect.android.formentry.questions.QuestionDetails;
import org.odk.collect.android.utilities.Appearances;
import org.odk.collect.android.widgets.base.GeneralStringWidgetTest;

import java.text.NumberFormat;
import java.util.Locale;
import java.util.Random;

import static junit.framework.TestCase.assertEquals;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.core.Is.is;
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;
Expand All @@ -40,6 +43,7 @@ public class DecimalWidgetTest extends GeneralStringWidgetTest<DecimalWidget, De
@NonNull
@Override
public DecimalWidget createWidget() {
when(formEntryPrompt.getDataType()).thenReturn(Constants.DATATYPE_DECIMAL);
return new DecimalWidget(activity, new QuestionDetails(formEntryPrompt, readOnlyOverride));
}

Expand Down Expand Up @@ -219,12 +223,11 @@ public void verifyInputType() {
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));
public void answersShouldNotBeMaskedIfMaskedAppearanceIsUsed() {
when(formEntryPrompt.getAppearanceHint()).thenReturn(Appearances.MASKED);

assertThat(getSpyWidget().widgetAnswerText.getBinding().editText.getTransformationMethod(), is(not(instanceOf(PasswordTransformationMethod.class))));
assertThat(getSpyWidget().widgetAnswerText.getBinding().textView.getTransformationMethod(), is(not(instanceOf(PasswordTransformationMethod.class))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import androidx.annotation.NonNull;

import org.javarosa.core.model.Constants;
import org.javarosa.core.model.data.DecimalData;
import org.odk.collect.android.formentry.questions.QuestionDetails;
import org.javarosa.core.model.data.IAnswerData;
import org.junit.Test;
import org.mockito.Mock;
import org.odk.collect.android.utilities.Appearances;
import org.odk.collect.android.widgets.base.GeneralExStringWidgetTest;
import org.odk.collect.android.widgets.support.FakeWaitingForDataRegistry;
import org.odk.collect.android.widgets.utilities.StringRequester;
Expand All @@ -16,11 +18,12 @@

import static junit.framework.TestCase.assertEquals;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.core.Is.is;
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;
Expand All @@ -42,6 +45,7 @@ public class ExDecimalWidgetTest extends GeneralExStringWidgetTest<ExDecimalWidg
@NonNull
@Override
public ExDecimalWidget createWidget() {
when(formEntryPrompt.getDataType()).thenReturn(Constants.DATATYPE_DECIMAL);
return new ExDecimalWidget(activity, new QuestionDetails(formEntryPrompt), new FakeWaitingForDataRegistry(), stringRequester);
}

Expand Down Expand Up @@ -103,13 +107,11 @@ public void verifyInputType() {
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));
public void answersShouldNotBeMaskedIfMaskedAppearanceIsUsed() {
when(formEntryPrompt.getAppearanceHint()).thenReturn(Appearances.MASKED);

assertThat(getSpyWidget().binding.widgetAnswerText.getBinding().editText.getTransformationMethod(), is(not(instanceOf(PasswordTransformationMethod.class))));
assertThat(getSpyWidget().binding.widgetAnswerText.getBinding().textView.getTransformationMethod(), is(not(instanceOf(PasswordTransformationMethod.class))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@

import androidx.annotation.NonNull;

import org.javarosa.core.model.Constants;
import org.javarosa.core.model.data.IntegerData;
import org.mockito.Mock;
import org.odk.collect.android.formentry.questions.QuestionDetails;
import org.junit.Test;
import org.odk.collect.android.utilities.Appearances;
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 junit.framework.TestCase.assertEquals;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
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;
Expand All @@ -33,6 +37,7 @@ public class ExIntegerWidgetTest extends GeneralExStringWidgetTest<ExIntegerWidg
@NonNull
@Override
public ExIntegerWidget createWidget() {
when(formEntryPrompt.getDataType()).thenReturn(Constants.DATATYPE_INTEGER);
return new ExIntegerWidget(activity, new QuestionDetails(formEntryPrompt), new FakeWaitingForDataRegistry(), stringRequester);
}

Expand Down Expand Up @@ -77,13 +82,11 @@ public void verifyInputType() {
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));
public void answersShouldNotBeMaskedIfMaskedAppearanceIsUsed() {
when(formEntryPrompt.getAppearanceHint()).thenReturn(Appearances.MASKED);

assertThat(getSpyWidget().binding.widgetAnswerText.getBinding().editText.getTransformationMethod(), is(not(instanceOf(PasswordTransformationMethod.class))));
assertThat(getSpyWidget().binding.widgetAnswerText.getBinding().textView.getTransformationMethod(), is(not(instanceOf(PasswordTransformationMethod.class))));
}
}
Loading