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