Skip to content

Commit

Permalink
Merge pull request #5824 from seadowg/fix-multi-select
Browse files Browse the repository at this point in the history
Fix multi select widget with no-buttons appearance
  • Loading branch information
grzesiek2010 authored Nov 16, 2023
2 parents 9e31df8 + acb8b9d commit 6967465
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ void onItemClick(Selection selection, View view) {
}
playAudio(selection.choice);
}

if (listener != null) {
listener.onItemClicked();
}
}

public void addItem(Selection item) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.TextView;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 6967465

Please sign in to comment.