1 /*
2  * Copyright (C) 2018 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 com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.spy;
23 import static org.mockito.Mockito.when;
24 
25 import android.content.Context;
26 import android.content.res.Resources;
27 import android.provider.Settings;
28 
29 import androidx.preference.Preference;
30 import androidx.preference.PreferenceCategory;
31 
32 import com.android.settingslib.widget.RadioButtonPreference;
33 
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 import org.robolectric.RobolectricTestRunner;
40 import org.robolectric.RuntimeEnvironment;
41 
42 @RunWith(RobolectricTestRunner.class)
43 public class PreventRingingGesturePreferenceControllerTest {
44     private Context mContext;
45     private Resources mResources;
46     private PreventRingingGesturePreferenceController mController;
47 
48     @Mock
49     private Preference mPreference;
50 
51     @Before
setUp()52     public void setUp() {
53         MockitoAnnotations.initMocks(this);
54         mContext = spy(RuntimeEnvironment.application);
55         mResources = mock(Resources.class);
56         when(mContext.getResources()).thenReturn(mResources);
57         when(mResources.getBoolean(com.android.internal.R.bool.config_volumeHushGestureEnabled))
58                 .thenReturn(true);
59         mController = new PreventRingingGesturePreferenceController(mContext, null);
60         mController.mPreferenceCategory = new PreferenceCategory(mContext);
61         mController.mVibratePref = new RadioButtonPreference(mContext);
62         mController.mMutePref = new RadioButtonPreference(mContext);
63     }
64 
65     @Test
testIsAvailable_configIsTrue_shouldReturnTrue()66     public void testIsAvailable_configIsTrue_shouldReturnTrue() {
67         when(mResources.getBoolean(
68                 com.android.internal.R.bool.config_volumeHushGestureEnabled)).thenReturn(true);
69 
70         assertThat(mController.isAvailable()).isTrue();
71     }
72 
73     @Test
testIsAvailable_configIsFalse_shouldReturnFalse()74     public void testIsAvailable_configIsFalse_shouldReturnFalse() {
75         when(mResources.getBoolean(
76                 com.android.internal.R.bool.config_volumeHushGestureEnabled)).thenReturn(false);
77 
78         assertThat(mController.isAvailable()).isFalse();
79     }
80 
81     @Test
testUpdateState_mute()82     public void testUpdateState_mute() {
83         Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.VOLUME_HUSH_GESTURE,
84                 Settings.Secure.VOLUME_HUSH_MUTE);
85         mController.updateState(mPreference);
86         assertThat(mController.mVibratePref.isEnabled()).isTrue();
87         assertThat(mController.mMutePref.isEnabled()).isTrue();
88         assertThat(mController.mVibratePref.isChecked()).isFalse();
89         assertThat(mController.mMutePref.isChecked()).isTrue();
90     }
91 
92     @Test
testUpdateState_vibrate()93     public void testUpdateState_vibrate() {
94         Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.VOLUME_HUSH_GESTURE,
95                 Settings.Secure.VOLUME_HUSH_VIBRATE);
96         mController.updateState(mPreference);
97         assertThat(mController.mVibratePref.isEnabled()).isTrue();
98         assertThat(mController.mMutePref.isEnabled()).isTrue();
99         assertThat(mController.mVibratePref.isChecked()).isTrue();
100         assertThat(mController.mMutePref.isChecked()).isFalse();
101     }
102 
103     @Test
testUpdateState_off()104     public void testUpdateState_off() {
105         Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.VOLUME_HUSH_GESTURE,
106                 Settings.Secure.VOLUME_HUSH_OFF);
107         mController.updateState(mPreference);
108         assertThat(mController.mVibratePref.isEnabled()).isFalse();
109         assertThat(mController.mMutePref.isEnabled()).isFalse();
110         assertThat(mController.mVibratePref.isChecked()).isFalse();
111         assertThat(mController.mMutePref.isChecked()).isFalse();
112     }
113 
114     @Test
testUpdateState_other()115     public void testUpdateState_other() {
116         Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.VOLUME_HUSH_GESTURE,
117                 7);
118         mController.updateState(mPreference);
119         assertThat(mController.mVibratePref.isChecked()).isFalse();
120         assertThat(mController.mMutePref.isChecked()).isFalse();
121     }
122 
123     @Test
testRadioButtonClicked_mute()124     public void testRadioButtonClicked_mute() {
125         RadioButtonPreference rbPref = new RadioButtonPreference(mContext);
126         rbPref.setKey(PreventRingingGesturePreferenceController.KEY_MUTE);
127 
128         Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.VOLUME_HUSH_GESTURE,
129                 Settings.Secure.VOLUME_HUSH_OFF);
130         mController.onRadioButtonClicked(rbPref);
131 
132         assertThat(Settings.Secure.VOLUME_HUSH_MUTE).isEqualTo(
133                 Settings.Secure.getInt(mContext.getContentResolver(),
134                         Settings.Secure.VOLUME_HUSH_GESTURE, Settings.Secure.VOLUME_HUSH_OFF));
135     }
136 
137     @Test
testRadioButtonClicked_vibrate()138     public void testRadioButtonClicked_vibrate() {
139         RadioButtonPreference rbPref = new RadioButtonPreference(mContext);
140         rbPref.setKey(PreventRingingGesturePreferenceController.KEY_VIBRATE);
141 
142         Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.VOLUME_HUSH_GESTURE,
143                 Settings.Secure.VOLUME_HUSH_OFF);
144         mController.onRadioButtonClicked(rbPref);
145 
146         assertThat(Settings.Secure.VOLUME_HUSH_VIBRATE).isEqualTo(
147                 Settings.Secure.getInt(mContext.getContentResolver(),
148                         Settings.Secure.VOLUME_HUSH_GESTURE, Settings.Secure.VOLUME_HUSH_OFF));
149     }
150 }
151