From 6250bdf2dd708574836c3983ae69e948465d5275 Mon Sep 17 00:00:00 2001 From: Callum Stott Date: Mon, 6 Nov 2023 14:36:38 +0000 Subject: [PATCH 1/2] Call widgetValueChanged() when selecting item in multi select --- .../adapters/SelectMultipleListAdapter.java | 4 + .../widgets/items/SelectMultiWidgetTest.java | 83 ++++++++++++++----- 2 files changed, 66 insertions(+), 21 deletions(-) diff --git a/collect_app/src/main/java/org/odk/collect/android/adapters/SelectMultipleListAdapter.java b/collect_app/src/main/java/org/odk/collect/android/adapters/SelectMultipleListAdapter.java index 73acc2f7682..46ca8f752aa 100644 --- a/collect_app/src/main/java/org/odk/collect/android/adapters/SelectMultipleListAdapter.java +++ b/collect_app/src/main/java/org/odk/collect/android/adapters/SelectMultipleListAdapter.java @@ -137,6 +137,10 @@ void onItemClick(Selection selection, View view) { } playAudio(selection.choice); } + + if (listener != null) { + listener.onItemClicked(); + } } public void addItem(Selection item) { diff --git a/collect_app/src/test/java/org/odk/collect/android/widgets/items/SelectMultiWidgetTest.java b/collect_app/src/test/java/org/odk/collect/android/widgets/items/SelectMultiWidgetTest.java index 7d725d5b57b..8d025d0edc4 100644 --- a/collect_app/src/test/java/org/odk/collect/android/widgets/items/SelectMultiWidgetTest.java +++ b/collect_app/src/test/java/org/odk/collect/android/widgets/items/SelectMultiWidgetTest.java @@ -2,6 +2,7 @@ import android.view.View; import android.view.ViewGroup; +import android.widget.CheckBox; import android.widget.FrameLayout; import android.widget.TextView; @@ -30,6 +31,7 @@ import org.odk.collect.android.formentry.questions.QuestionDetails; import org.odk.collect.android.formentry.questions.QuestionTextSizeHelper; import org.odk.collect.android.injection.config.AppDependencyModule; +import org.odk.collect.android.listeners.WidgetValueChangedListener; import org.odk.collect.android.support.CollectHelpers; import org.odk.collect.android.support.MockFormEntryPromptBuilder; import org.odk.collect.android.utilities.Appearances; @@ -160,27 +162,6 @@ public void whenChoicesHaveAudio_audioButtonUsesIndexAsClipID() throws Exception verify(audioHelper).setAudio(any(AudioButton.class), eq(new Clip("i am index 1", REFERENCES.get(1).second))); } - private void overrideDependencyModule() throws Exception { - ReferenceManager referenceManager = setupFakeReferenceManager(REFERENCES); - CollectHelpers.overrideAppDependencyModule(new AppDependencyModule() { - - @Override - public ReferenceManager providesReferenceManager() { - return referenceManager; - } - - @Override - public AudioHelperFactory providesAudioHelperFactory(Scheduler scheduler) { - return context -> audioHelper; - } - - @Override - public SoftKeyboardController provideSoftKeyboardController() { - return mock(SoftKeyboardController.class); - } - }); - } - @Test public void usingReadOnlyOptionShouldMakeAllClickableElementsDisabled() { // No appearance @@ -226,6 +207,66 @@ public void whenSpacesInUnderlyingValuesExist_shouldAppropriateWarningBeDisplaye assertThat(warningTv.getText(), is("Warning: underlying values a a, b b have spaces")); } + @Test + public void checkingCheckboxOnItem_callsValueChangedListener() { + formEntryPrompt = new MockFormEntryPromptBuilder() + .withIndex("i am index") + .withSelectChoices(asList( + new SelectChoice("1", "1"), + new SelectChoice("2", "2") + )) + .build(); + + SelectMultiWidget widget = getWidget(); + WidgetValueChangedListener valueChangedListener = mock(); + widget.setValueChangedListener(valueChangedListener); + populateRecyclerView(widget); + + ((CheckBox) ((AudioVideoImageTextLabel) getWidget().binding.choicesRecyclerView.getChildAt(0)).getLabelTextView()).setChecked(true); + verify(valueChangedListener).widgetValueChanged(widget); + } + + @Test + public void whenPromptHasNoButtonsAppearance_clickingItem_callsValueChangedListener() { + formEntryPrompt = new MockFormEntryPromptBuilder() + .withIndex("i am index") + .withAppearance("no-buttons") + .withSelectChoices(asList( + new SelectChoice("1", "1"), + new SelectChoice("2", "2") + )) + .build(); + + SelectMultiWidget widget = getWidget(); + WidgetValueChangedListener valueChangedListener = mock(); + widget.setValueChangedListener(valueChangedListener); + populateRecyclerView(widget); + + getWidget().binding.choicesRecyclerView.getChildAt(0).performClick(); + verify(valueChangedListener).widgetValueChanged(widget); + } + + private void overrideDependencyModule() throws Exception { + ReferenceManager referenceManager = setupFakeReferenceManager(REFERENCES); + CollectHelpers.overrideAppDependencyModule(new AppDependencyModule() { + + @Override + public ReferenceManager providesReferenceManager() { + return referenceManager; + } + + @Override + public AudioHelperFactory providesAudioHelperFactory(Scheduler scheduler) { + return context -> audioHelper; + } + + @Override + public SoftKeyboardController provideSoftKeyboardController() { + return mock(SoftKeyboardController.class); + } + }); + } + private ViewGroup getChoiceView(SelectMultiWidget widget, int index) { return (ViewGroup) widget.binding.choicesRecyclerView.getChildAt(index); } From acb8b9d46fade07bfe80db8eda772c6ed94fe177 Mon Sep 17 00:00:00 2001 From: Callum Stott Date: Tue, 7 Nov 2023 10:04:55 +0000 Subject: [PATCH 2/2] Backfill widgetValueChanged tests for SelectOneWidget --- .../widgets/items/SelectOneWidgetTest.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/collect_app/src/test/java/org/odk/collect/android/widgets/items/SelectOneWidgetTest.java b/collect_app/src/test/java/org/odk/collect/android/widgets/items/SelectOneWidgetTest.java index 537f593d59a..29f947b11ad 100644 --- a/collect_app/src/test/java/org/odk/collect/android/widgets/items/SelectOneWidgetTest.java +++ b/collect_app/src/test/java/org/odk/collect/android/widgets/items/SelectOneWidgetTest.java @@ -43,6 +43,7 @@ import org.odk.collect.android.formentry.questions.QuestionTextSizeHelper; import org.odk.collect.android.injection.config.AppDependencyModule; import org.odk.collect.android.listeners.AdvanceToNextListener; +import org.odk.collect.android.listeners.WidgetValueChangedListener; import org.odk.collect.android.support.CollectHelpers; import org.odk.collect.android.support.MockFormEntryPromptBuilder; import org.odk.collect.android.utilities.Appearances; @@ -287,6 +288,45 @@ public void usingReadOnlyOptionShouldMakeAllClickableElementsDisabled() { assertThat(view.isEnabled(), is(Boolean.FALSE)); } + @Test + public void clickingItem_callsValueChangedListener() { + formEntryPrompt = new MockFormEntryPromptBuilder() + .withIndex("i am index") + .withSelectChoices(asList( + new SelectChoice("1", "1"), + new SelectChoice("2", "2") + )) + .build(); + + SelectOneWidget widget = getWidget(); + WidgetValueChangedListener valueChangedListener = mock(); + widget.setValueChangedListener(valueChangedListener); + populateRecyclerView(widget); + + ((AudioVideoImageTextLabel) getWidget().binding.choicesRecyclerView.getChildAt(0)).getLabelTextView().performClick(); + verify(valueChangedListener).widgetValueChanged(widget); + } + + @Test + public void whenPromptHasNoButtonsAppearance_clickingItem_callsValueChangedListener() { + formEntryPrompt = new MockFormEntryPromptBuilder() + .withIndex("i am index") + .withAppearance("no-buttons") + .withSelectChoices(asList( + new SelectChoice("1", "1"), + new SelectChoice("2", "2") + )) + .build(); + + SelectOneWidget widget = getWidget(); + WidgetValueChangedListener valueChangedListener = mock(); + widget.setValueChangedListener(valueChangedListener); + populateRecyclerView(widget); + + getWidget().binding.choicesRecyclerView.getChildAt(0).performClick(); + verify(valueChangedListener).widgetValueChanged(widget); + } + private void overrideDependencyModule() throws Exception { ReferenceManager referenceManager = setupFakeReferenceManager(REFERENCES); CollectHelpers.overrideAppDependencyModule(new AppDependencyModule() {