|
| 1 | +/* |
| 2 | + * Copyright (C) 2020 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.android.material.slider; |
| 18 | + |
| 19 | +import com.google.android.material.R; |
| 20 | + |
| 21 | +import static android.view.ViewGroup.LayoutParams.MATCH_PARENT; |
| 22 | +import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT; |
| 23 | +import static org.mockito.ArgumentMatchers.eq; |
| 24 | +import static org.mockito.Mockito.mock; |
| 25 | +import static org.mockito.Mockito.never; |
| 26 | +import static org.mockito.Mockito.times; |
| 27 | +import static org.mockito.Mockito.verify; |
| 28 | + |
| 29 | +import android.app.ActionBar.LayoutParams; |
| 30 | +import android.os.SystemClock; |
| 31 | +import androidx.appcompat.app.AppCompatActivity; |
| 32 | +import android.view.MotionEvent; |
| 33 | +import android.widget.LinearLayout; |
| 34 | +import androidx.test.core.app.ApplicationProvider; |
| 35 | +import com.google.android.material.slider.Slider.OnChangeListener; |
| 36 | +import org.junit.Before; |
| 37 | +import org.junit.Test; |
| 38 | +import org.junit.runner.RunWith; |
| 39 | +import org.mockito.AdditionalMatchers; |
| 40 | +import org.robolectric.Robolectric; |
| 41 | +import org.robolectric.RobolectricTestRunner; |
| 42 | + |
| 43 | +/** Tests for events of {@link com.google.android.material.slider.Slider} */ |
| 44 | +@RunWith(RobolectricTestRunner.class) |
| 45 | +public class SliderEventTest { |
| 46 | + |
| 47 | + private static final float FLOAT_ERROR = 1e-4f; |
| 48 | + |
| 49 | + private static final float SLIDER_VALUE_FROM = 0f; |
| 50 | + private static final float SLIDER_VALUE_TO = 100f; |
| 51 | + private static final float SLIDER_VALUE_RANGE = SLIDER_VALUE_TO - SLIDER_VALUE_FROM; |
| 52 | + private static final float SLIDER_STEP_VALUE = 1f; |
| 53 | + |
| 54 | + private Slider slider; |
| 55 | + private OnChangeListener mockOnChangeListener; |
| 56 | + |
| 57 | + @Before |
| 58 | + public void createSlider() { |
| 59 | + ApplicationProvider.getApplicationContext().setTheme(R.style.Theme_MaterialComponents_Bridge); |
| 60 | + AppCompatActivity activity = Robolectric.buildActivity(AppCompatActivity.class).setup().get(); |
| 61 | + |
| 62 | + // Creates slider and adds the listener. |
| 63 | + slider = new Slider(activity); |
| 64 | + slider.setValueFrom(SLIDER_VALUE_FROM); |
| 65 | + slider.setValueTo(SLIDER_VALUE_TO); |
| 66 | + |
| 67 | + mockOnChangeListener = mock(OnChangeListener.class); |
| 68 | + slider.addOnChangeListener(mockOnChangeListener); |
| 69 | + |
| 70 | + // Makes sure getParent() won't return null. |
| 71 | + LinearLayout container = new LinearLayout(activity); |
| 72 | + container.setPadding(50, 50, 50, 50); |
| 73 | + container.setOrientation(LinearLayout.VERTICAL); |
| 74 | + // Prevents getContentView() dead loop. |
| 75 | + container.setId(android.R.id.content); |
| 76 | + // Adds slider to layout, and adds layout to activity. |
| 77 | + container.addView(slider, new LayoutParams(MATCH_PARENT, WRAP_CONTENT)); |
| 78 | + activity.addContentView(container, new LayoutParams(MATCH_PARENT, WRAP_CONTENT)); |
| 79 | + |
| 80 | + // Changes the step size. |
| 81 | + slider.setStepSize(SLIDER_STEP_VALUE); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testSliderSingleClickCurrentPosition_ListenerShouldNotBeCalled() { |
| 86 | + float currentValue = slider.getValue(); |
| 87 | + // Click pressed. |
| 88 | + touchSliderAtValue(slider, currentValue, MotionEvent.ACTION_DOWN); |
| 89 | + // Click released. |
| 90 | + touchSliderAtValue(slider, currentValue, MotionEvent.ACTION_UP); |
| 91 | + // Listener should not be called since value is not changed. |
| 92 | + verify(mockOnChangeListener, never()) |
| 93 | + .onValueChange(eq(slider), eq(SLIDER_VALUE_FROM), eq(true)); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void testSliderSingleClickDifferentPosition_ListenerShouldBeCalled() { |
| 98 | + // Click pressed. |
| 99 | + touchSliderAtValue(slider, SLIDER_VALUE_FROM + SLIDER_VALUE_RANGE / 2, MotionEvent.ACTION_DOWN); |
| 100 | + // Click released. |
| 101 | + touchSliderAtValue(slider, SLIDER_VALUE_FROM + SLIDER_VALUE_RANGE / 2, MotionEvent.ACTION_UP); |
| 102 | + // Listener should be called once. |
| 103 | + verify(mockOnChangeListener, times(1)) |
| 104 | + .onValueChange( |
| 105 | + eq(slider), |
| 106 | + AdditionalMatchers.eq(SLIDER_VALUE_FROM + SLIDER_VALUE_RANGE / 2, FLOAT_ERROR), |
| 107 | + eq(true)); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void testSliderDrag_ListenerShouldBeCalled() { |
| 112 | + // Drag starts from one quarter to the left end. |
| 113 | + touchSliderAtValue(slider, SLIDER_VALUE_FROM + SLIDER_VALUE_RANGE / 4, MotionEvent.ACTION_DOWN); |
| 114 | + // Drags to the center. |
| 115 | + for (int incremental = 1; incremental <= 100; incremental++) { |
| 116 | + touchSliderAtValue( |
| 117 | + slider, |
| 118 | + SLIDER_VALUE_FROM + SLIDER_VALUE_RANGE * (25 + incremental / 4) / 100, |
| 119 | + MotionEvent.ACTION_MOVE); |
| 120 | + } |
| 121 | + // Drag released. |
| 122 | + touchSliderAtValue(slider, SLIDER_VALUE_TO / 2, MotionEvent.ACTION_UP); |
| 123 | + // Verifies listener calls. |
| 124 | + for (int value = 25; value <= 50; value++) { |
| 125 | + verify(mockOnChangeListener, times(1)) |
| 126 | + .onValueChange(eq(slider), AdditionalMatchers.eq((float) value, FLOAT_ERROR), eq(true)); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void testSliderSetValue_OnlySetOnce() { |
| 132 | + // Sets value twice. |
| 133 | + slider.setValue(SLIDER_VALUE_FROM + SLIDER_VALUE_RANGE / 2); |
| 134 | + slider.setValue(SLIDER_VALUE_FROM + SLIDER_VALUE_RANGE / 2); |
| 135 | + |
| 136 | + // Verifies listener calls. |
| 137 | + verify(mockOnChangeListener, times(1)) |
| 138 | + .onValueChange(eq(slider), eq(SLIDER_VALUE_FROM + SLIDER_VALUE_RANGE / 2), eq(false)); |
| 139 | + } |
| 140 | + |
| 141 | + private static void touchSliderAtValue(Slider s, float value, int motionEventType) { |
| 142 | + float x = |
| 143 | + s.getTrackSidePadding() |
| 144 | + + (value - s.getValueFrom()) / (s.getValueTo() - s.getValueFrom()) * s.getTrackWidth(); |
| 145 | + float y = s.getY() + s.getHeight() / 2; |
| 146 | + |
| 147 | + s.dispatchTouchEvent( |
| 148 | + MotionEvent.obtain( |
| 149 | + SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), motionEventType, x, y, 0)); |
| 150 | + } |
| 151 | +} |
0 commit comments