Skip to content

Commit

Permalink
Merge pull request #5533 from grzesiek2010/COLLECT-5532
Browse files Browse the repository at this point in the history
Improved handling invalid geo answers
  • Loading branch information
seadowg authored Mar 31, 2023
2 parents 39e7310 + a125b4f commit 15b2688
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,31 +60,34 @@ protected View onCreateAnswerView(Context context, FormEntryPrompt prompt, int a
binding.simpleButton.setOnClickListener(v -> geoDataRequester.requestGeoPoint(prompt, answerText, waitingForDataRegistry));

answerText = prompt.getAnswerText();
binding.geoAnswerText.setText(GeoWidgetUtils.getGeoPointAnswerToDisplay(getContext(), answerText));
String answerToDisplay = GeoWidgetUtils.getGeoPointAnswerToDisplay(getContext(), answerText);

boolean dataAvailable = answerText != null && !answerText.isEmpty();
if (getFormEntryPrompt().isReadOnly()) {
if (dataAvailable) {
binding.simpleButton.setText(R.string.geopoint_view_read_only);
} else {
if (answerToDisplay.isEmpty()) {
if (getFormEntryPrompt().isReadOnly()) {
binding.simpleButton.setVisibility(View.GONE);
} else {
binding.simpleButton.setText(R.string.get_point);
}
answerText = null;
} else {
if (dataAvailable) {
binding.simpleButton.setText(R.string.view_change_location);
if (getFormEntryPrompt().isReadOnly()) {
binding.simpleButton.setText(R.string.geopoint_view_read_only);
} else {
binding.simpleButton.setText(R.string.get_point);
binding.simpleButton.setText(R.string.view_change_location);
}

binding.geoAnswerText.setText(answerToDisplay);
}

return binding.getRoot();
}

@Override
public IAnswerData getAnswer() {
return answerText == null || answerText.isEmpty()
double[] parsedGeometryPoint = GeoWidgetUtils.parseGeometryPoint(answerText);
return parsedGeometryPoint == null
? null
: new GeoPointData(GeoWidgetUtils.parseGeometryPoint(answerText));
: new GeoPointData(parsedGeometryPoint);
}

@Override
Expand All @@ -110,9 +113,16 @@ public void cancelLongPress() {

@Override
public void setData(Object answer) {
answerText = answer.toString();
binding.geoAnswerText.setText(GeoWidgetUtils.getGeoPointAnswerToDisplay(getContext(), answerText));
binding.simpleButton.setText(answerText == null || answerText.isEmpty() ? R.string.get_point : R.string.view_change_location);
String answerToDisplay = GeoWidgetUtils.getGeoPointAnswerToDisplay(getContext(), answer.toString());
if (answerToDisplay.isEmpty()) {
answerText = null;
binding.geoAnswerText.setText("");
binding.simpleButton.setText(R.string.get_point);
} else {
answerText = answer.toString();
binding.geoAnswerText.setText(answerToDisplay);
binding.simpleButton.setText(R.string.view_change_location);
}
widgetValueChanged();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,24 @@ protected View onCreateAnswerView(Context context, FormEntryPrompt prompt, int a

answerText = prompt.getAnswerText();

if (answerText != null && !answerText.isEmpty()) {
binding.geoAnswerText.setText(GeoWidgetUtils.getGeoPointAnswerToDisplay(getContext(), answerText));
binding.simpleButton.setText(R.string.change_location);
} else {
String answerToDisplay = GeoWidgetUtils.getGeoPointAnswerToDisplay(getContext(), answerText);
if (answerToDisplay.isEmpty()) {
binding.simpleButton.setText(R.string.get_point);
answerText = null;
} else {
binding.geoAnswerText.setText(answerToDisplay);
binding.simpleButton.setText(R.string.change_location);
}

return binding.getRoot();
}

@Override
public IAnswerData getAnswer() {
return answerText == null || answerText.isEmpty()
double[] parsedGeometryPoint = GeoWidgetUtils.parseGeometryPoint(answerText);
return parsedGeometryPoint == null
? null
: new GeoPointData(GeoWidgetUtils.parseGeometryPoint(answerText));
: new GeoPointData(parsedGeometryPoint);
}

@Override
Expand All @@ -103,9 +106,16 @@ public void cancelLongPress() {

@Override
public void setData(Object answer) {
answerText = answer.toString();
binding.geoAnswerText.setText(GeoWidgetUtils.getGeoPointAnswerToDisplay(getContext(), answerText));
binding.simpleButton.setText(answerText == null || answerText.isEmpty() ? R.string.get_point : R.string.change_location);
String answerToDisplay = GeoWidgetUtils.getGeoPointAnswerToDisplay(getContext(), answer.toString());
if (answerToDisplay.isEmpty()) {
answerText = null;
binding.geoAnswerText.setText("");
binding.simpleButton.setText(R.string.get_point);
} else {
answerText = answer.toString();
binding.geoAnswerText.setText(answerToDisplay);
binding.simpleButton.setText(R.string.change_location);
}
widgetValueChanged();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ class ActivityGeoDataRequester(
waitingForDataRegistry.waitForData(prompt.index)

val bundle = Bundle().also {
if (!answerText.isNullOrEmpty()) {
val parsedGeometry = GeoWidgetUtils.parseGeometry(answerText)
if (parsedGeometry.isNotEmpty()) {
it.putParcelable(
GeoPointMapActivity.EXTRA_LOCATION,
GeoWidgetUtils.parseGeometry(answerText)[0],
parsedGeometry[0],
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.javarosa.core.model.data.GeoPointData;
import org.javarosa.core.model.data.StringData;
import org.javarosa.form.api.FormEntryPrompt;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -53,6 +54,19 @@ public void getAnswer_whenPromptHasAnswer_returnsPromptAnswer() {
new GeoPointData(GeoWidgetUtils.parseGeometryPoint(answer.getDisplayText())).getDisplayText());
}

@Test
public void getAnswer_whenPromptHasInvalidAnswer_returnsNull() {
GeoPointMapWidget widget = createWidget(promptWithAnswer(new StringData("blah")));
assertNull(widget.getAnswer());
}

@Test
public void creatingWidgetWithInvalidValue_doesNotUpdateWidgetDisplayedAnswer() {
GeoPointMapWidget widget = createWidget(promptWithAnswer(new StringData("blah")));
assertEquals(widget.binding.geoAnswerText.getText(), "");
assertEquals(widget.binding.simpleButton.getText(), widget.getContext().getString(R.string.get_point));
}

@Test
public void whenPromptHasAnswer_answerTextViewShowsCorrectAnswer() {
GeoPointMapWidget widget = createWidget(promptWithAnswer(answer));
Expand Down Expand Up @@ -108,6 +122,12 @@ public void setData_updatesWidgetAnswer() {
assertEquals(widget.getAnswer().getDisplayText(), answer.getDisplayText());
}

@Test
public void setDataWithInvalidValue_doesNotUpdateWidgetAnswer() {
GeoPointMapWidget widget = createWidget(promptWithAnswer(null));
widget.setData("blah");
assertEquals(widget.getAnswer(), null);
}

@Test
public void setData_updatesWidgetDisplayedAnswer() {
Expand All @@ -116,6 +136,14 @@ public void setData_updatesWidgetDisplayedAnswer() {
assertEquals(widget.binding.geoAnswerText.getText(), GeoWidgetUtils.getGeoPointAnswerToDisplay(widget.getContext(), answer.getDisplayText()));
}

@Test
public void setDataWithInvalidValue_doesNotUpdateWidgetDisplayedAnswer() {
GeoPointMapWidget widget = createWidget(promptWithAnswer(null));
widget.setData("blah");
assertEquals(widget.binding.geoAnswerText.getText(), "");
assertEquals(widget.binding.simpleButton.getText(), widget.getContext().getString(R.string.get_point));
}

@Test
public void setData_whenDataIsNull_updatesButtonLabel() {
GeoPointMapWidget widget = createWidget(promptWithAnswer(answer));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.javarosa.core.model.data.GeoPointData;
import org.javarosa.core.model.data.StringData;
import org.javarosa.form.api.FormEntryPrompt;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -57,6 +58,19 @@ public void getAnswer_whenPromptHasAnswer_returnsAnswer() {
assertEquals(widget.getAnswer().getDisplayText(), answer.getDisplayText());
}

@Test
public void getAnswer_whenPromptHasInvalidAnswer_returnsNull() {
GeoPointWidget widget = createWidget(promptWithAnswer(new StringData("blah")));
assertNull(widget.getAnswer());
}

@Test
public void creatingWidgetWithInvalidValue_doesNotUpdateWidgetDisplayedAnswer() {
GeoPointWidget widget = createWidget(promptWithAnswer(new StringData("blah")));
assertEquals(widget.binding.geoAnswerText.getText(), "");
assertEquals(widget.binding.simpleButton.getText(), widget.getContext().getString(R.string.get_point));
}

@Test
public void answerTextViewShouldShowCorrectAnswer() {
GeoPointWidget widget = createWidget(promptWithAnswer(answer));
Expand Down Expand Up @@ -113,13 +127,28 @@ public void setData_updatesWidgetAnswer() {
assertEquals(widget.getAnswer().getDisplayText(), answer.getDisplayText());
}

@Test
public void setDataWithInvalidValue_doesNotUpdateWidgetAnswer() {
GeoPointWidget widget = createWidget(promptWithAnswer(null));
widget.setData("blah");
assertEquals(widget.getAnswer(), null);
}

@Test
public void setData_updatesWidgetDisplayedAnswer() {
GeoPointWidget widget = createWidget(promptWithAnswer(null));
widget.setData(answer.getDisplayText());
assertEquals(widget.binding.geoAnswerText.getText(), GeoWidgetUtils.getGeoPointAnswerToDisplay(widget.getContext(), answer.getDisplayText()));
}

@Test
public void setDataWithInvalidValue_doesNotUpdateWidgetDisplayedAnswer() {
GeoPointWidget widget = createWidget(promptWithAnswer(null));
widget.setData("blah");
assertEquals(widget.binding.geoAnswerText.getText(), "");
assertEquals(widget.binding.simpleButton.getText(), widget.getContext().getString(R.string.get_point));
}

@Test
public void setData_whenDataIsNull_updatesButtonLabel() {
GeoPointWidget widget = createWidget(promptWithAnswer(answer));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,18 @@ public void requestGeoPoint_whenAnswerIsPresent_addsToIntent() {
assertThat(bundle.getParcelable(GeoPointMapActivity.EXTRA_LOCATION), equalTo(new MapPoint(1.0, 2.0, 3, 4)));
}

@Test
public void requestGeoPoint_whenAnswerIsPresentButInvalid_doesNotAddToIntent() {
activityGeoDataRequester.requestGeoPoint(prompt, "something", waitingForDataRegistry);
Intent startedIntent = shadowActivity.getNextStartedActivity();

assertEquals(startedIntent.getComponent(), new ComponentName(testActivity, GeoPointActivity.class));
assertEquals(shadowActivity.getNextStartedActivityForResult().requestCode, LOCATION_CAPTURE);

Bundle bundle = startedIntent.getExtras();
assertThat(bundle.getParcelable(GeoPointMapActivity.EXTRA_LOCATION), equalTo(null));
}

@Test
public void whenWidgetHasAccuracyValue_requestGeoPoint_launchesCorrectIntent() {
when(questionDef.getAdditionalAttribute(null, "accuracyThreshold")).thenReturn("10");
Expand Down

0 comments on commit 15b2688

Please sign in to comment.