Skip to content

Commit

Permalink
Pass the listener via constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
grzesiek2010 committed Jan 26, 2024
1 parent 2ac5582 commit 345da30
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.javarosa.form.api.FormEntryCaption;
import org.javarosa.form.api.FormEntryPrompt;
import org.odk.collect.android.R;
import org.odk.collect.android.activities.FormFillingActivity;
import org.odk.collect.android.application.Collect;
import org.odk.collect.android.audio.AudioHelper;
import org.odk.collect.android.exception.ExternalParamsException;
Expand Down Expand Up @@ -196,7 +197,8 @@ public ODKView(
viewLifecycle,
new FileRequesterImpl(intentLauncher, externalAppIntentProvider, formController),
new StringRequesterImpl(intentLauncher, externalAppIntentProvider, formController),
formController
formController,
(FormFillingActivity) context
);

widgets = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.odk.collect.android.formentry.questions.QuestionDetails;
import org.odk.collect.android.geo.MapConfiguratorProvider;
import org.odk.collect.android.javarosawrapper.FormController;
import org.odk.collect.android.listeners.AdvanceToNextListener;
import org.odk.collect.android.storage.StoragePathProvider;
import org.odk.collect.android.utilities.Appearances;
import org.odk.collect.android.utilities.ExternalWebPageHelper;
Expand Down Expand Up @@ -89,6 +90,7 @@ public class WidgetFactory {
private final FileRequester fileRequester;
private final StringRequester stringRequester;
private final FormController formController;
private final AdvanceToNextListener advanceToNextListener;

public WidgetFactory(Activity activity,
boolean readOnlyOverride,
Expand All @@ -103,7 +105,9 @@ public WidgetFactory(Activity activity,
LifecycleOwner viewLifecycle,
FileRequester fileRequester,
StringRequester stringRequester,
FormController formController) {
FormController formController,
AdvanceToNextListener advanceToNextListener
) {
this.activity = activity;
this.readOnlyOverride = readOnlyOverride;
this.useExternalRecorder = useExternalRecorder;
Expand All @@ -118,6 +122,7 @@ public WidgetFactory(Activity activity,
this.fileRequester = fileRequester;
this.stringRequester = stringRequester;
this.formController = formController;
this.advanceToNextListener = advanceToNextListener;
}

public QuestionWidget createWidgetFromPrompt(FormEntryPrompt prompt, PermissionsProvider permissionsProvider) {
Expand Down Expand Up @@ -315,7 +320,7 @@ private QuestionWidget getSelectOneWidget(String appearance, QuestionDetails que
} else if (appearance.contains(Appearances.IMAGE_MAP)) {
questionWidget = new SelectOneImageMapWidget(activity, questionDetails, isQuick, formEntryViewModel);
} else if (appearance.contains(Appearances.MAP)) {
questionWidget = new SelectOneFromMapWidget(activity, questionDetails, isQuick);
questionWidget = new SelectOneFromMapWidget(activity, questionDetails, isQuick, advanceToNextListener);
} else {
questionWidget = new SelectOneWidget(activity, questionDetails, isQuick, formController, formEntryViewModel);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,11 @@ import org.odk.collect.permissions.PermissionListener
class SelectOneFromMapWidget(
context: Context,
questionDetails: QuestionDetails,
private val autoAdvance: Boolean
private val autoAdvance: Boolean,
private val autoAdvanceListener: AdvanceToNextListener
) : QuestionWidget(context, questionDetails), WidgetDataReceiver {

lateinit var autoAdvanceListener: AdvanceToNextListener

init {
if (context is AdvanceToNextListener) {
autoAdvanceListener = context
}
render()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class WidgetFactoryTest {
@Before
public void setup() {
Activity activity = CollectHelpers.buildThemedActivity(WidgetTestActivity.class).get();
widgetFactory = new WidgetFactory(activity, false, false, null, null, null, null, mock(FormEntryViewModel.class), null, null, null, null, null, null);
widgetFactory = new WidgetFactory(activity, false, false, null, null, null, null, mock(FormEntryViewModel.class), null, null, null, null, null, null, mock());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ class SelectOneFromMapWidgetTest {
val widget = SelectOneFromMapWidget(
activityController.get(),
QuestionDetails(promptWithAnswer(null)),
false
false,
mock()
)

assertThat(
Expand All @@ -109,7 +110,7 @@ class SelectOneFromMapWidgetTest {

val prompt = promptWithAnswer(null)
val widget =
SelectOneFromMapWidget(activity, QuestionDetails(prompt), false)
SelectOneFromMapWidget(activity, QuestionDetails(prompt), false, mock())
whenever(formEntryViewModel.getQuestionPrompt(prompt.index)).doReturn(prompt)

widget.binding.button.performClick()
Expand All @@ -131,7 +132,8 @@ class SelectOneFromMapWidgetTest {
val widget = SelectOneFromMapWidget(
activityController.get(),
QuestionDetails(promptWithAnswer(null)),
false
false,
mock()
)

permissionsProvider.setPermissionGranted(false)
Expand All @@ -153,7 +155,7 @@ class SelectOneFromMapWidgetTest {
.withAnswer(SelectOneData(choices[0].selection()))
.build()

val widget = SelectOneFromMapWidget(activityController.get(), QuestionDetails(prompt), false)
val widget = SelectOneFromMapWidget(activityController.get(), QuestionDetails(prompt), false, mock())
assertThat(widget.binding.answer.text, equalTo("A"))
}

Expand All @@ -163,7 +165,8 @@ class SelectOneFromMapWidgetTest {
val widget = SelectOneFromMapWidget(
activityController.get(),
QuestionDetails(promptWithAnswer(null)),
false
false,
mock()
)

assertThat(
Expand All @@ -180,7 +183,8 @@ class SelectOneFromMapWidgetTest {
val widget = SelectOneFromMapWidget(
activityController.get(),
QuestionDetails(promptWithAnswer(answer)),
false
false,
mock()
)
assertThat(widget.answer, equalTo(answer))
}
Expand All @@ -193,7 +197,8 @@ class SelectOneFromMapWidgetTest {
val widget = SelectOneFromMapWidget(
activityController.get(),
QuestionDetails(promptWithAnswer(answer)),
false
false,
mock()
)
widget.clearAnswer()
assertThat(widget.answer, equalTo(null))
Expand All @@ -207,7 +212,8 @@ class SelectOneFromMapWidgetTest {
val widget = SelectOneFromMapWidget(
activityController.get(),
QuestionDetails(promptWithAnswer(answer)),
false
false,
mock()
)

val mockValueChangedListener = mockValueChangedListener(widget)
Expand All @@ -224,7 +230,7 @@ class SelectOneFromMapWidgetTest {
.withAnswer(SelectOneData(choices[0].selection()))
.build()

val widget = SelectOneFromMapWidget(activityController.get(), QuestionDetails(prompt), false)
val widget = SelectOneFromMapWidget(activityController.get(), QuestionDetails(prompt), false, mock())

widget.clearAnswer()
assertThat(widget.binding.answer.text, equalTo(""))
Expand All @@ -235,7 +241,8 @@ class SelectOneFromMapWidgetTest {
val widget = SelectOneFromMapWidget(
activityController.get(),
QuestionDetails(promptWithAnswer(null)),
false
false,
mock()
)

val selectChoice = selectChoice(value = "a", index = 101)
Expand All @@ -253,7 +260,7 @@ class SelectOneFromMapWidgetTest {
mapOf(choices[0] to "A", choices[1] to "B")
)
.build()
val widget = SelectOneFromMapWidget(activityController.get(), QuestionDetails(prompt), false)
val widget = SelectOneFromMapWidget(activityController.get(), QuestionDetails(prompt), false, mock())

widget.setData(SelectOneData(choices[1].selection()))
assertThat(widget.binding.answer.text, equalTo("B"))
Expand All @@ -267,7 +274,7 @@ class SelectOneFromMapWidgetTest {
.withSelectChoiceText(mapOf(choices[0] to "A"))
.build()

val widget = SelectOneFromMapWidget(activityController.get(), QuestionDetails(prompt), false)
val widget = SelectOneFromMapWidget(activityController.get(), QuestionDetails(prompt), false, mock())

val mockValueChangedListener = mockValueChangedListener(widget)
widget.setData(SelectOneData(choices[0].selection()))
Expand Down Expand Up @@ -295,7 +302,7 @@ class SelectOneFromMapWidgetTest {
.build()

val widget =
SelectOneFromMapWidget(activity, QuestionDetails(prompt), false)
SelectOneFromMapWidget(activity, QuestionDetails(prompt), false, mock())
widget.setData(SelectOneData(choices[1].selection()))

whenever(formEntryViewModel.getQuestionPrompt(prompt.index)).doReturn(prompt)
Expand All @@ -315,13 +322,13 @@ class SelectOneFromMapWidgetTest {

@Test
fun `setData calls AdvanceToNextListener when the 'quick' appearance is used`() {
val listener = mock<AdvanceToNextListener>()
val widget = SelectOneFromMapWidget(
activityController.get(),
QuestionDetails(promptWithAnswer(null)),
true
true,
listener
)
val listener = mock<AdvanceToNextListener>()
widget.autoAdvanceListener = listener

val selectChoice = selectChoice(value = "a", index = 101)
val answer = SelectOneData(selectChoice.selection())
Expand All @@ -331,13 +338,13 @@ class SelectOneFromMapWidgetTest {

@Test
fun `setData does not call AdvanceToNextListener when the 'quick' appearance is not used`() {
val listener = mock<AdvanceToNextListener>()
val widget = SelectOneFromMapWidget(
activityController.get(),
QuestionDetails(promptWithAnswer(null)),
false
false,
listener
)
val listener = mock<AdvanceToNextListener>()
widget.autoAdvanceListener = listener

val selectChoice = selectChoice(value = "a", index = 101)
val answer = SelectOneData(selectChoice.selection())
Expand Down

0 comments on commit 345da30

Please sign in to comment.