• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.robolectric.RobolectricTestRunner;
38 import org.robolectric.RuntimeEnvironment;
39 
40 @RunWith(RobolectricTestRunner.class)
41 public class BalanceSeekBarTest {
42     // Fix the maximum process value to 200 for testing the BalanceSeekBar.
43     // It affects the SeekBar value of center(100) and snapThreshold(200 * SNAP_TO_PERCENTAGE).
44     private static final int MAX_PROGRESS_VALUE = 200;
45 
46     private Context mContext;
47     private AttributeSet mAttrs;
48     private BalanceSeekBar mSeekBar;
49     private BalanceSeekBar.OnSeekBarChangeListener mProxySeekBarListener;
50     private SeekBar.OnSeekBarChangeListener mockSeekBarChangeListener;
51 
52     @Before
setUp()53     public void setUp() {
54         mContext = RuntimeEnvironment.application;
55         mSeekBar = new BalanceSeekBar(mContext, mAttrs);
56         mProxySeekBarListener = shadowOf(mSeekBar).getOnSeekBarChangeListener();
57         mockSeekBarChangeListener = mock(SeekBar.OnSeekBarChangeListener.class);
58         mSeekBar.setOnSeekBarChangeListener(mockSeekBarChangeListener);
59     }
60 
61     @Test
onStartTrackingTouch_shouldInvokeMethod()62     public void onStartTrackingTouch_shouldInvokeMethod() {
63         mProxySeekBarListener.onStartTrackingTouch(mSeekBar);
64 
65         verify(mockSeekBarChangeListener, times(1)).onStartTrackingTouch(mSeekBar);
66     }
67 
68     @Test
onStopTrackingTouch_shouldInvokeMethod()69     public void onStopTrackingTouch_shouldInvokeMethod() {
70         mProxySeekBarListener.onStopTrackingTouch(mSeekBar);
71 
72         verify(mockSeekBarChangeListener, times(1)).onStopTrackingTouch(mSeekBar);
73     }
74 
75     @Test
onProgressChanged_shouldInvokeMethod()76     public void onProgressChanged_shouldInvokeMethod() {
77         // Assign the test value of SeekBar progress
78         mProxySeekBarListener.onProgressChanged(mSeekBar, MAX_PROGRESS_VALUE, true);
79 
80         verify(mockSeekBarChangeListener, times(1)).onProgressChanged(eq(mSeekBar),
81                 eq(MAX_PROGRESS_VALUE), eq(true));
82     }
83 
84     @Test
onProgressChanged_minimumValue_clockTickFeedbackPerformed()85     public void onProgressChanged_minimumValue_clockTickFeedbackPerformed() {
86         mSeekBar.performHapticFeedback(CONTEXT_CLICK);
87         mProxySeekBarListener.onProgressChanged(mSeekBar, 0, true);
88 
89         assertThat(shadowOf(mSeekBar).lastHapticFeedbackPerformed()).isEqualTo(CLOCK_TICK);
90     }
91 
92     @Test
onProgressChanged_centerValue_clockTickFeedbackPerformed()93     public void onProgressChanged_centerValue_clockTickFeedbackPerformed() {
94         mSeekBar.performHapticFeedback(CONTEXT_CLICK);
95         mProxySeekBarListener.onProgressChanged(mSeekBar, MAX_PROGRESS_VALUE / 2, true);
96 
97         assertThat(shadowOf(mSeekBar).lastHapticFeedbackPerformed()).isEqualTo(CLOCK_TICK);
98     }
99 
100     @Test
onProgressChanged_maximumValue_clockTickFeedbackPerformed()101     public void onProgressChanged_maximumValue_clockTickFeedbackPerformed() {
102         mSeekBar.setMax(MAX_PROGRESS_VALUE);
103         mSeekBar.performHapticFeedback(CONTEXT_CLICK);
104         mProxySeekBarListener.onProgressChanged(mSeekBar, MAX_PROGRESS_VALUE, true);
105 
106         assertThat(shadowOf(mSeekBar).lastHapticFeedbackPerformed()).isEqualTo(CLOCK_TICK);
107     }
108 
109     @Test
setMaxTest_shouldSetValue()110     public void setMaxTest_shouldSetValue() {
111         mSeekBar.setMax(MAX_PROGRESS_VALUE);
112 
113         assertThat(getBalanceSeekBarCenter(mSeekBar)).isEqualTo(MAX_PROGRESS_VALUE / 2);
114         assertThat(getBalanceSeekBarSnapThreshold(mSeekBar)).isEqualTo(
115                 MAX_PROGRESS_VALUE * BalanceSeekBar.SNAP_TO_PERCENTAGE);
116     }
117 
118     @Test
setProgressTest_shouldSnapToCenter()119     public void setProgressTest_shouldSnapToCenter() {
120         // Assign the test value of SeekBar progress within the threshold (94-106 in this case).
121         final int progressWithinThreshold = 102;
122         mSeekBar.setMax(MAX_PROGRESS_VALUE);
123         mSeekBar.setProgress(progressWithinThreshold + 10); //set progress which is over threshold.
124         mProxySeekBarListener.onProgressChanged(mSeekBar, progressWithinThreshold, true);
125 
126         assertThat(mSeekBar.getProgress()).isEqualTo(getBalanceSeekBarCenter(mSeekBar));
127     }
128 
129     @Test
setProgressTest_shouldMaintainInputValue()130     public void setProgressTest_shouldMaintainInputValue() {
131         // Assign the test value of SeekBar progress without the threshold.
132         final int progressWithoutThreshold = 107;
133         mSeekBar.setMax(MAX_PROGRESS_VALUE);
134         mSeekBar.setProgress(progressWithoutThreshold);
135         mProxySeekBarListener.onProgressChanged(mSeekBar, progressWithoutThreshold, true);
136 
137         assertThat(mSeekBar.getProgress()).isEqualTo(progressWithoutThreshold);
138     }
139 
140     // method to get the center from BalanceSeekBar for testing setMax().
getBalanceSeekBarCenter(BalanceSeekBar seekBar)141     private int getBalanceSeekBarCenter(BalanceSeekBar seekBar) {
142         return seekBar.getMax() / 2;
143     }
144 
145     // method to get the snapThreshold from BalanceSeekBar for testing setMax().
getBalanceSeekBarSnapThreshold(BalanceSeekBar seekBar)146     private float getBalanceSeekBarSnapThreshold(BalanceSeekBar seekBar) {
147         return seekBar.getMax() * BalanceSeekBar.SNAP_TO_PERCENTAGE;
148     }
149 }
150