1 /* 2 * Copyright (C) 2019 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.android.settings.accessibility; 18 19 import static android.view.HapticFeedbackConstants.CLOCK_TICK; 20 import static android.view.HapticFeedbackConstants.CONTEXT_CLICK; 21 22 import static com.google.common.truth.Truth.assertThat; 23 24 import static org.mockito.Mockito.eq; 25 import static org.mockito.Mockito.mock; 26 import static org.mockito.Mockito.times; 27 import static org.mockito.Mockito.verify; 28 import static org.robolectric.Shadows.shadowOf; 29 30 import android.content.Context; 31 import android.util.AttributeSet; 32 import android.widget.SeekBar; 33 34 import com.android.settings.R; 35 import com.android.settings.Utils; 36 import com.android.settings.testutils.shadow.ShadowSystemSettings; 37 38 import org.junit.Before; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 import org.robolectric.RobolectricTestRunner; 42 import org.robolectric.RuntimeEnvironment; 43 import org.robolectric.annotation.Config; 44 45 import java.util.Locale; 46 47 @RunWith(RobolectricTestRunner.class) 48 @Config(shadows = { 49 ShadowSystemSettings.class, 50 }) 51 public class BalanceSeekBarTest { 52 // Fix the maximum process value to 200 for testing the BalanceSeekBar. 53 // It affects the SeekBar value of center(100) and snapThreshold(200 * SNAP_TO_PERCENTAGE). 54 private static final int MAX_PROGRESS_VALUE = 200; 55 56 private Context mContext; 57 private AttributeSet mAttrs; 58 private BalanceSeekBar mSeekBar; 59 private BalanceSeekBar.OnSeekBarChangeListener mProxySeekBarListener; 60 private SeekBar.OnSeekBarChangeListener mockSeekBarChangeListener; 61 62 @Before setUp()63 public void setUp() { 64 mContext = RuntimeEnvironment.application; 65 mSeekBar = new BalanceSeekBar(mContext, mAttrs); 66 mProxySeekBarListener = shadowOf(mSeekBar).getOnSeekBarChangeListener(); 67 mockSeekBarChangeListener = mock(SeekBar.OnSeekBarChangeListener.class); 68 mSeekBar.setOnSeekBarChangeListener(mockSeekBarChangeListener); 69 } 70 71 @Test onStartTrackingTouch_shouldInvokeMethod()72 public void onStartTrackingTouch_shouldInvokeMethod() { 73 mProxySeekBarListener.onStartTrackingTouch(mSeekBar); 74 75 verify(mockSeekBarChangeListener, times(1)).onStartTrackingTouch(mSeekBar); 76 } 77 78 @Test onStopTrackingTouch_shouldInvokeMethod()79 public void onStopTrackingTouch_shouldInvokeMethod() { 80 mProxySeekBarListener.onStopTrackingTouch(mSeekBar); 81 82 verify(mockSeekBarChangeListener, times(1)).onStopTrackingTouch(mSeekBar); 83 } 84 85 @Test onProgressChanged_shouldInvokeMethod()86 public void onProgressChanged_shouldInvokeMethod() { 87 // Assign the test value of SeekBar progress 88 mProxySeekBarListener.onProgressChanged(mSeekBar, MAX_PROGRESS_VALUE, true); 89 90 verify(mockSeekBarChangeListener, times(1)).onProgressChanged(eq(mSeekBar), 91 eq(MAX_PROGRESS_VALUE), eq(true)); 92 } 93 94 @Test onProgressChanged_minimumValue_clockTickFeedbackPerformed()95 public void onProgressChanged_minimumValue_clockTickFeedbackPerformed() { 96 mSeekBar.performHapticFeedback(CONTEXT_CLICK); 97 mProxySeekBarListener.onProgressChanged(mSeekBar, 0, true); 98 99 assertThat(shadowOf(mSeekBar).lastHapticFeedbackPerformed()).isEqualTo(CLOCK_TICK); 100 } 101 102 @Test onProgressChanged_centerValue_clockTickFeedbackPerformed()103 public void onProgressChanged_centerValue_clockTickFeedbackPerformed() { 104 mSeekBar.performHapticFeedback(CONTEXT_CLICK); 105 mProxySeekBarListener.onProgressChanged(mSeekBar, MAX_PROGRESS_VALUE / 2, true); 106 107 assertThat(shadowOf(mSeekBar).lastHapticFeedbackPerformed()).isEqualTo(CLOCK_TICK); 108 } 109 110 @Test onProgressChanged_maximumValue_clockTickFeedbackPerformed()111 public void onProgressChanged_maximumValue_clockTickFeedbackPerformed() { 112 mSeekBar.setMax(MAX_PROGRESS_VALUE); 113 mSeekBar.performHapticFeedback(CONTEXT_CLICK); 114 mProxySeekBarListener.onProgressChanged(mSeekBar, MAX_PROGRESS_VALUE, true); 115 116 assertThat(shadowOf(mSeekBar).lastHapticFeedbackPerformed()).isEqualTo(CLOCK_TICK); 117 } 118 119 @Test setMaxTest_shouldSetValue()120 public void setMaxTest_shouldSetValue() { 121 mSeekBar.setMax(MAX_PROGRESS_VALUE); 122 123 assertThat(getBalanceSeekBarCenter(mSeekBar)).isEqualTo(MAX_PROGRESS_VALUE / 2); 124 assertThat(getBalanceSeekBarSnapThreshold(mSeekBar)).isEqualTo( 125 MAX_PROGRESS_VALUE * BalanceSeekBar.SNAP_TO_PERCENTAGE); 126 } 127 128 @Test setProgressTest_shouldSnapToCenter()129 public void setProgressTest_shouldSnapToCenter() { 130 // Assign the test value of SeekBar progress within the threshold (94-106 in this case). 131 final int progressWithinThreshold = 102; 132 mSeekBar.setMax(MAX_PROGRESS_VALUE); 133 mSeekBar.setProgress(progressWithinThreshold + 10); //set progress which is over threshold. 134 mProxySeekBarListener.onProgressChanged(mSeekBar, progressWithinThreshold, true); 135 136 assertThat(mSeekBar.getProgress()).isEqualTo(getBalanceSeekBarCenter(mSeekBar)); 137 } 138 139 @Test setProgressTest_shouldMaintainInputValue()140 public void setProgressTest_shouldMaintainInputValue() { 141 // Assign the test value of SeekBar progress without the threshold. 142 final int progressWithoutThreshold = 107; 143 mSeekBar.setMax(MAX_PROGRESS_VALUE); 144 mSeekBar.setProgress(progressWithoutThreshold); 145 mProxySeekBarListener.onProgressChanged(mSeekBar, progressWithoutThreshold, true); 146 147 assertThat(mSeekBar.getProgress()).isEqualTo(progressWithoutThreshold); 148 } 149 150 @Test onProgressChanged_getStateDescription_centered_leftFirst()151 public void onProgressChanged_getStateDescription_centered_leftFirst() { 152 // Seek bar centered 153 int progress = (int) (0.50f * MAX_PROGRESS_VALUE); 154 mSeekBar.setMax(MAX_PROGRESS_VALUE); 155 156 mProxySeekBarListener.onProgressChanged(mSeekBar, progress, true); 157 158 assertThat(mSeekBar.getStateDescription()).isEqualTo( 159 mContext.getString(R.string.audio_seek_bar_state_left_first, 160 Utils.formatPercentage(50), Utils.formatPercentage(50))); 161 } 162 163 @Test onProgressChanged_getStateDescription_centered_rtl_rightFirst()164 public void onProgressChanged_getStateDescription_centered_rtl_rightFirst() { 165 // RTL layout 166 mContext.getResources().getConfiguration().setLayoutDirection(new Locale("iw", "IL")); 167 // Seek bar centered 168 int progress = (int) (0.50f * MAX_PROGRESS_VALUE); 169 mSeekBar.setMax(MAX_PROGRESS_VALUE); 170 171 mProxySeekBarListener.onProgressChanged(mSeekBar, progress, true); 172 173 assertThat(mSeekBar.getStateDescription()).isEqualTo( 174 mContext.getString(R.string.audio_seek_bar_state_right_first, 175 Utils.formatPercentage(50), Utils.formatPercentage(50))); 176 } 177 178 @Test onProgressChanged_getStateDescription_25percent_leftFirst()179 public void onProgressChanged_getStateDescription_25percent_leftFirst() { 180 // Seek bar 3/4th toward the left 181 int progress = (int) (0.25f * MAX_PROGRESS_VALUE); 182 mSeekBar.setMax(MAX_PROGRESS_VALUE); 183 mProxySeekBarListener.onProgressChanged(mSeekBar, progress, true); 184 185 assertThat(mSeekBar.getStateDescription()).isEqualTo( 186 mContext.getString(R.string.audio_seek_bar_state_left_first, 187 Utils.formatPercentage(75), Utils.formatPercentage(25))); 188 } 189 190 @Test onProgressChanged_getStateDescription_75percent_rightFirst()191 public void onProgressChanged_getStateDescription_75percent_rightFirst() { 192 // Seek bar 3/4th toward the right 193 int progress = (int) (0.75f * MAX_PROGRESS_VALUE); 194 mSeekBar.setMax(MAX_PROGRESS_VALUE); 195 mProxySeekBarListener.onProgressChanged(mSeekBar, progress, true); 196 197 assertThat(mSeekBar.getStateDescription()).isEqualTo( 198 mContext.getString(R.string.audio_seek_bar_state_right_first, 199 Utils.formatPercentage(75), Utils.formatPercentage(25))); 200 } 201 202 // method to get the center from BalanceSeekBar for testing setMax(). getBalanceSeekBarCenter(BalanceSeekBar seekBar)203 private int getBalanceSeekBarCenter(BalanceSeekBar seekBar) { 204 return seekBar.getMax() / 2; 205 } 206 207 // method to get the snapThreshold from BalanceSeekBar for testing setMax(). getBalanceSeekBarSnapThreshold(BalanceSeekBar seekBar)208 private float getBalanceSeekBarSnapThreshold(BalanceSeekBar seekBar) { 209 return seekBar.getMax() * BalanceSeekBar.SNAP_TO_PERCENTAGE; 210 } 211 } 212