• 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.gestures;
18 
19 import static android.provider.Settings.Secure.VOLUME_HUSH_MUTE;
20 import static android.provider.Settings.Secure.VOLUME_HUSH_OFF;
21 import static android.provider.Settings.Secure.VOLUME_HUSH_VIBRATE;
22 
23 import static com.google.common.truth.Truth.assertThat;
24 
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.spy;
28 import static org.mockito.Mockito.times;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
31 
32 import android.content.Context;
33 import android.content.res.Resources;
34 import android.provider.Settings;
35 
36 import androidx.preference.Preference;
37 import androidx.preference.PreferenceScreen;
38 
39 import com.android.settings.widget.SwitchBar;
40 import com.android.settingslib.widget.LayoutPreference;
41 
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.robolectric.RobolectricTestRunner;
46 import org.robolectric.RuntimeEnvironment;
47 
48 @RunWith(RobolectricTestRunner.class)
49 public class PreventRingingSwitchPreferenceControllerTest {
50 
51     private static final int UNKNOWN = -1;
52 
53     private Context mContext;
54     private Resources mResources;
55     private PreventRingingSwitchPreferenceController mController;
56     private Preference mPreference = mock(Preference.class);
57 
58     @Before
setUp()59     public void setUp() {
60         mContext = spy(RuntimeEnvironment.application);
61         mResources = mock(Resources.class);
62         when(mContext.getResources()).thenReturn(mResources);
63         when(mResources.getBoolean(com.android.internal.R.bool.config_volumeHushGestureEnabled))
64                 .thenReturn(true);
65         mController = new PreventRingingSwitchPreferenceController(mContext);
66         mController.mSwitch = mock(SwitchBar.class);
67     }
68 
69     @Test
testIsAvailable_configIsTrue_shouldReturnTrue()70     public void testIsAvailable_configIsTrue_shouldReturnTrue() {
71         when(mResources.getBoolean(
72                 com.android.internal.R.bool.config_volumeHushGestureEnabled)).thenReturn(true);
73 
74         assertThat(mController.isAvailable()).isTrue();
75     }
76 
77     @Test
testIsAvailable_configIsFalse_shouldReturnFalse()78     public void testIsAvailable_configIsFalse_shouldReturnFalse() {
79         when(mResources.getBoolean(
80                 com.android.internal.R.bool.config_volumeHushGestureEnabled)).thenReturn(false);
81 
82         assertThat(mController.isAvailable()).isFalse();
83     }
84 
85     @Test
updateState_hushOff_uncheck()86     public void updateState_hushOff_uncheck() {
87         Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.VOLUME_HUSH_GESTURE,
88                 VOLUME_HUSH_OFF);
89 
90         mController.updateState(mPreference);
91 
92         verify(mController.mSwitch, times(1)).setChecked(false);
93     }
94 
95     @Test
updateState_hushVibrate_setChecked()96     public void updateState_hushVibrate_setChecked() {
97         Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.VOLUME_HUSH_GESTURE,
98                 VOLUME_HUSH_VIBRATE);
99 
100         mController.updateState(mPreference);
101 
102         verify(mController.mSwitch, times(1)).setChecked(true);
103     }
104 
105     @Test
updateState_hushMute_setChecked()106     public void updateState_hushMute_setChecked() {
107         Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.VOLUME_HUSH_GESTURE,
108                 VOLUME_HUSH_MUTE);
109 
110         mController.updateState(mPreference);
111 
112         verify(mController.mSwitch, times(1)).setChecked(true);
113     }
114 
115     @Test
onSwitchChanged_wasHushOff_checked_returnHushVibrate()116     public void onSwitchChanged_wasHushOff_checked_returnHushVibrate() {
117         Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.VOLUME_HUSH_GESTURE,
118                 VOLUME_HUSH_OFF);
119 
120         mController.onSwitchChanged(null, true);
121 
122         assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
123                 Settings.Secure.VOLUME_HUSH_GESTURE, UNKNOWN)).isEqualTo(VOLUME_HUSH_VIBRATE);
124     }
125 
126     @Test
onSwitchChanged_wasHushMute_unchecked_returnHushOff()127     public void onSwitchChanged_wasHushMute_unchecked_returnHushOff() {
128         Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.VOLUME_HUSH_GESTURE,
129                 VOLUME_HUSH_MUTE);
130 
131         mController.onSwitchChanged(null, false);
132 
133         assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
134                 Settings.Secure.VOLUME_HUSH_GESTURE, UNKNOWN)).isEqualTo(VOLUME_HUSH_OFF);
135     }
136 
137     @Test
onSwitchChanged_wasHushMute_checked_returnHushMute()138     public void onSwitchChanged_wasHushMute_checked_returnHushMute() {
139         // this is the case for the page open
140         Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.VOLUME_HUSH_GESTURE,
141                 VOLUME_HUSH_MUTE);
142 
143         mController.onSwitchChanged(null, true);
144 
145         assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
146                 Settings.Secure.VOLUME_HUSH_GESTURE, UNKNOWN)).isEqualTo(VOLUME_HUSH_MUTE);
147     }
148 
149     @Test
onSwitchChanged_wasHushVibrate_checked_returnHushVibrate()150     public void onSwitchChanged_wasHushVibrate_checked_returnHushVibrate() {
151         // this is the case for the page open
152         Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.VOLUME_HUSH_GESTURE,
153                 VOLUME_HUSH_VIBRATE);
154 
155         mController.onSwitchChanged(null, true);
156 
157         assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
158                 Settings.Secure.VOLUME_HUSH_GESTURE, UNKNOWN)).isEqualTo(VOLUME_HUSH_VIBRATE);
159     }
160 
161     @Test
testPreferenceClickListenerAttached()162     public void testPreferenceClickListenerAttached() {
163         PreferenceScreen preferenceScreen = mock(PreferenceScreen.class);
164         LayoutPreference mLayoutPreference = mock(LayoutPreference.class);
165         when(preferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn(
166                 mLayoutPreference);
167 
168         mController.displayPreference(preferenceScreen);
169 
170         verify(mLayoutPreference, times(1))
171                 .setOnPreferenceClickListener(any());
172     }
173 }
174