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

Fixed clicking on the min value when there is no value yet #6524

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions androidtest/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@ dependencies {
implementation(Dependencies.androidx_test_espresso_core)
implementation(Dependencies.androidx_appcompat)
implementation(Dependencies.androidx_test_espresso_intents)
implementation(Dependencies.android_material)
implementation(Dependencies.timber)
}
34 changes: 34 additions & 0 deletions androidtest/src/main/java/org/odk/collect/androidtest/SliderExt.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.odk.collect.androidtest

import android.view.MotionEvent
import com.google.android.material.slider.Slider

fun Slider.clickOnStep(step: Float) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should probably live in testshared instead as it's not the kind of thing that I feel should be androidx.test:core (which is what androidtest is really there for).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

val xPosition: Float = step * (width / valueTo)
val yPosition: Float = height / 2f

val currentTime = System.currentTimeMillis()

val downEvent = MotionEvent.obtain(
currentTime,
currentTime,
MotionEvent.ACTION_DOWN,
xPosition,
yPosition,
0
)
dispatchTouchEvent(downEvent)

val upEvent = MotionEvent.obtain(
currentTime,
currentTime,
MotionEvent.ACTION_UP,
xPosition,
yPosition,
0
)
dispatchTouchEvent(upEvent)

downEvent.recycle()
upEvent.recycle()
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
public class TrackingTouchSlider extends Slider implements Slider.OnSliderTouchListener {

private boolean trackingTouch;
private boolean enabled;

private OnMinValueChangedListener onMinValueChangedListener;

public interface OnMinValueChangedListener {
void onFirstValueChanged();
}

@SuppressLint("ClickableViewAccessibility")
public TrackingTouchSlider(@NonNull Context context, @Nullable AttributeSet attrs) {
Expand All @@ -29,6 +36,9 @@ public TrackingTouchSlider(@NonNull Context context, @Nullable AttributeSet attr
break;
case MotionEvent.ACTION_UP:
v.getParent().requestDisallowInterceptTouchEvent(false);
if (!enabled) {
onMinValueChangedListener.onFirstValueChanged();
}
break;
}
v.onTouchEvent(event);
Expand All @@ -51,4 +61,16 @@ public void onStopTrackingTouch(@NotNull Slider slider) {
public void onStartTrackingTouch(@NotNull Slider slider) {
trackingTouch = true;
}

public void enable() {
enabled = true;
}

public void disable() {
enabled = false;
}

public void setOnFirstValueChanged(OnMinValueChangedListener onMinValueChangedListener) {
this.onMinValueChangedListener = onMinValueChangedListener;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import java.math.BigDecimal;

@SuppressLint("ViewConstructor")
public class RangeDecimalWidget extends QuestionWidget implements Slider.OnChangeListener {
public class RangeDecimalWidget extends QuestionWidget implements Slider.OnChangeListener, TrackingTouchSlider.OnMinValueChangedListener {
TrackingTouchSlider slider;
TextView currentValue;

Expand All @@ -63,6 +63,7 @@ protected View onCreateAnswerView(Context context, FormEntryPrompt prompt, int a

setUpActualValueLabel(RangeWidgetUtils.setUpSlider(prompt, slider, false));

slider.setOnFirstValueChanged(this);
if (slider.isEnabled()) {
slider.addOnChangeListener(this);
}
Expand Down Expand Up @@ -90,6 +91,11 @@ public void clearAnswer() {
widgetValueChanged();
}

@Override
public void onFirstValueChanged() {
setUpActualValueLabel(BigDecimal.valueOf(slider.getValueFrom()));
}

@SuppressLint("RestrictedApi")
@Override
public void onValueChange(@NonNull Slider slider, float value, boolean fromUser) {
Expand All @@ -106,12 +112,14 @@ private void setUpActualValueLabel(BigDecimal actualValue) {
slider.setTickActiveTintList(defaultTickActiveTintList);
slider.setThumbWidth(defaultThumbWidth);
slider.setThumbTrackGapSize(defaultThumbTrackGapSize);
slider.enable();
} else {
slider.setValue(slider.getValueFrom());
slider.setTickActiveTintList(ColorStateList.valueOf(MaterialColors.getColor(this, com.google.android.material.R.attr.colorPrimary)));
slider.setThumbWidth(0);
slider.setThumbTrackGapSize(0);
currentValue.setText("");
slider.disable();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import java.math.BigDecimal;

@SuppressLint("ViewConstructor")
public class RangeIntegerWidget extends QuestionWidget implements Slider.OnChangeListener {
public class RangeIntegerWidget extends QuestionWidget implements Slider.OnChangeListener, TrackingTouchSlider.OnMinValueChangedListener {
TrackingTouchSlider slider;
TextView currentValue;

Expand All @@ -63,6 +63,7 @@ protected View onCreateAnswerView(Context context, FormEntryPrompt prompt, int a

setUpActualValueLabel(RangeWidgetUtils.setUpSlider(prompt, slider, true));

slider.setOnFirstValueChanged(this);
if (slider.isEnabled()) {
slider.addOnChangeListener(this);
}
Expand Down Expand Up @@ -90,6 +91,11 @@ public void clearAnswer() {
widgetValueChanged();
}

@Override
public void onFirstValueChanged() {
setUpActualValueLabel(BigDecimal.valueOf(slider.getValueFrom()));
}

@SuppressLint("RestrictedApi")
@Override
public void onValueChange(@NonNull Slider slider, float value, boolean fromUser) {
Expand All @@ -106,12 +112,14 @@ private void setUpActualValueLabel(BigDecimal actualValue) {
slider.setTickActiveTintList(defaultTickActiveTintList);
slider.setThumbWidth(defaultThumbWidth);
slider.setThumbTrackGapSize(defaultThumbTrackGapSize);
slider.enable();
} else {
slider.setValue(slider.getValueFrom());
slider.setTickActiveTintList(ColorStateList.valueOf(MaterialColors.getColor(this, com.google.android.material.R.attr.colorPrimary)));
slider.setThumbWidth(0);
slider.setThumbTrackGapSize(0);
currentValue.setText("");
slider.disable();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.odk.collect.android.formentry.questions.QuestionDetails;
import org.odk.collect.android.listeners.WidgetValueChangedListener;
import org.odk.collect.android.support.MockFormEntryPromptBuilder;
import org.odk.collect.androidtest.SliderExtKt;

import java.math.BigDecimal;

Expand Down Expand Up @@ -202,6 +203,17 @@ public void everyTriggerWidgetShouldHaveCheckboxWithUniqueID() {
assertThat(widget1.slider.getId(), not(equalTo(widget2.slider.getId())));
}

@Test
public void changingSliderValueToTheMinOneWhenSliderHasNoValue_setsTheValue() {
RangeDecimalWidget widget = createWidget(promptWithQuestionDefAndAnswer(rangeQuestion, null));
widget.slider.measure(100, 10);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could add a helper for the measure/layout boilerplate to SliderExt as well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It turns out widget.slider.measure(100, 10); is not needed so I removed it.

widget.slider.layout(0, 0, 100, 10);

SliderExtKt.clickOnStep(widget.slider, 1);

assertThat(widget.currentValue.getText(), equalTo("1.5"));
}

private RangeDecimalWidget createWidget(FormEntryPrompt prompt) {
return new RangeDecimalWidget(widgetTestActivity(), new QuestionDetails(prompt));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.odk.collect.android.formentry.questions.QuestionDetails;
import org.odk.collect.android.listeners.WidgetValueChangedListener;
import org.odk.collect.android.support.MockFormEntryPromptBuilder;
import org.odk.collect.androidtest.SliderExtKt;

import java.math.BigDecimal;

Expand Down Expand Up @@ -205,6 +206,17 @@ public void everyTriggerWidgetShouldHaveCheckboxWithUniqueID() {
assertThat(widget1.slider.getId(), not(equalTo(widget2.slider.getId())));
}

@Test
public void changingSliderValueToTheMinOneWhenSliderHasNoValue_setsTheValue() {
RangeIntegerWidget widget = createWidget(promptWithQuestionDefAndAnswer(rangeQuestion, null));
widget.slider.measure(100, 10);
widget.slider.layout(0, 0, 100, 10);

SliderExtKt.clickOnStep(widget.slider, 1);

assertThat(widget.currentValue.getText(), equalTo("1"));
}

private RangeIntegerWidget createWidget(FormEntryPrompt prompt) {
return new RangeIntegerWidget(widgetTestActivity(), new QuestionDetails(prompt));
}
Expand Down