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 android.provider.Settings.Secure.VOLUME_HUSH_GESTURE;
20 import static android.provider.Settings.Secure.VOLUME_HUSH_MUTE;
21 import static android.provider.Settings.Secure.VOLUME_HUSH_OFF;
22 import static android.provider.Settings.Secure.VOLUME_HUSH_VIBRATE;
23 
24 import static com.android.settings.core.BasePreferenceController.AVAILABLE;
25 import static com.android.settings.core.BasePreferenceController.DISABLED_DEPENDENT_SETTING;
26 import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE;
27 
28 import static com.google.common.truth.Truth.assertThat;
29 
30 import static org.mockito.Mockito.spy;
31 import static org.mockito.Mockito.when;
32 
33 import android.content.Context;
34 import android.content.res.Resources;
35 import android.provider.Settings;
36 
37 import androidx.preference.Preference;
38 import androidx.preference.PreferenceScreen;
39 
40 import com.android.settings.R;
41 import com.android.settingslib.PrimarySwitchPreference;
42 
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.mockito.Mock;
47 import org.mockito.MockitoAnnotations;
48 import org.robolectric.RobolectricTestRunner;
49 import org.robolectric.RuntimeEnvironment;
50 
51 @RunWith(RobolectricTestRunner.class)
52 public class PreventRingingParentPreferenceControllerTest {
53 
54     @Mock
55     private Resources mResources;
56 
57     @Mock
58     PreferenceScreen mScreen;
59 
60     private Context mContext;
61     private PreventRingingParentPreferenceController mController;
62     private Preference mPreference;
63 
64     @Before
setUp()65     public void setUp() {
66         MockitoAnnotations.initMocks(this);
67         mContext = spy(RuntimeEnvironment.application.getApplicationContext());
68         when(mContext.getResources()).thenReturn(mResources);
69         when(mResources.getInteger(
70                 com.android.internal.R.integer.config_keyChordPowerVolumeUp)).thenReturn(
71                 PreventRingingParentPreferenceController.KEY_CHORD_POWER_VOLUME_UP_MUTE_TOGGLE);
72         mController = new PreventRingingParentPreferenceController(mContext, "test_key");
73         mPreference = new PrimarySwitchPreference(mContext);
74         when(mScreen.findPreference("test_key")).thenReturn(mPreference);
75         mController.displayPreference(mScreen);
76     }
77 
78     @Test
isAvailable_configIsTrueAndKeyChordMute_shouldAvailableUnSearchable()79     public void isAvailable_configIsTrueAndKeyChordMute_shouldAvailableUnSearchable() {
80         when(mResources.getBoolean(
81                 com.android.internal.R.bool.config_volumeHushGestureEnabled)).thenReturn(true);
82         assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
83     }
84 
85     @Test
getAvailabilityStatus_configIsTrueAndKeyNotMute_shouldReturnDisabledDependent()86     public void getAvailabilityStatus_configIsTrueAndKeyNotMute_shouldReturnDisabledDependent() {
87         when(mContext.getResources()).thenReturn(mResources);
88         when(mResources.getBoolean(
89                 com.android.internal.R.bool.config_volumeHushGestureEnabled)).thenReturn(true);
90         when(mResources.getBoolean(
91                 com.android.internal.R.bool.config_longPressOnPowerForAssistantSettingAvailable))
92                 .thenReturn(true);
93         when(mResources.getInteger(
94                 com.android.internal.R.integer.config_keyChordPowerVolumeUp)).thenReturn(2);
95 
96         assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_DEPENDENT_SETTING);
97     }
98 
99     @Test
getAvailabilityStatus_configIsTrueLppDisabled_shouldReturnUnsupportedOnDevice()100     public void getAvailabilityStatus_configIsTrueLppDisabled_shouldReturnUnsupportedOnDevice() {
101         when(mContext.getResources()).thenReturn(mResources);
102         when(mResources.getBoolean(
103                 com.android.internal.R.bool.config_volumeHushGestureEnabled)).thenReturn(true);
104         when(mResources.getBoolean(
105                 com.android.internal.R.bool.config_longPressOnPowerForAssistantSettingAvailable))
106                 .thenReturn(false);
107         when(mResources.getInteger(
108                 com.android.internal.R.integer.config_keyChordPowerVolumeUp)).thenReturn(2);
109 
110         assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
111     }
112 
113     @Test
isAvailable_configIsFalse_shouldReturnFalse()114     public void isAvailable_configIsFalse_shouldReturnFalse() {
115         when(mContext.getResources()).thenReturn(mResources);
116         when(mResources.getBoolean(
117                 com.android.internal.R.bool.config_volumeHushGestureEnabled)).thenReturn(false);
118 
119         assertThat(mController.isAvailable()).isFalse();
120     }
121 
122     @Test
updateState_summaryUpdated()123     public void updateState_summaryUpdated() {
124         Settings.Secure.putInt(mContext.getContentResolver(), VOLUME_HUSH_GESTURE,
125                 VOLUME_HUSH_MUTE);
126         mController.updateState(mPreference);
127         assertThat(mPreference.getSummary()).isEqualTo(mContext.getResources().getText(
128                 R.string.prevent_ringing_option_mute_summary));
129 
130         Settings.Secure.putInt(mContext.getContentResolver(), VOLUME_HUSH_GESTURE,
131                 VOLUME_HUSH_VIBRATE);
132         mController.updateState(mPreference);
133         assertThat(mPreference.getSummary()).isEqualTo(mContext.getResources().getText(
134                 R.string.prevent_ringing_option_vibrate_summary));
135 
136         Settings.Secure.putInt(mContext.getContentResolver(), VOLUME_HUSH_GESTURE,
137                 VOLUME_HUSH_OFF);
138         mController.updateState(mPreference);
139         assertThat(mPreference.getSummary()).isEqualTo(mContext.getResources().getText(
140                 R.string.switch_off_text));
141     }
142 
143     @Test
updateState_keyChordDisabled_summaryUpdated()144     public void updateState_keyChordDisabled_summaryUpdated() {
145         when(mResources.getInteger(
146                 com.android.internal.R.integer.config_keyChordPowerVolumeUp)).thenReturn(2);
147         // Ensure that the state displays unchecked even if the underlying field is set.
148         Settings.Secure.putInt(mContext.getContentResolver(), VOLUME_HUSH_GESTURE,
149                 VOLUME_HUSH_MUTE);
150         mController.updateState(mPreference);
151 
152         assertThat(mPreference.isEnabled()).isFalse();
153         assertThat(mPreference.getSummary()).isEqualTo(mContext.getResources().getText(
154                 R.string.prevent_ringing_option_unavailable_lpp_summary));
155         assertThat(mController.isChecked()).isFalse();
156     }
157 
158     @Test
isChecked_vibrate_shouldReturnTrue()159     public void isChecked_vibrate_shouldReturnTrue() {
160         Settings.Secure.putInt(mContext.getContentResolver(), VOLUME_HUSH_GESTURE,
161                 VOLUME_HUSH_VIBRATE);
162 
163         assertThat(mController.isChecked()).isTrue();
164     }
165 
166     @Test
isChecked_mute_shouldReturnTrue()167     public void isChecked_mute_shouldReturnTrue() {
168         Settings.Secure.putInt(mContext.getContentResolver(), VOLUME_HUSH_GESTURE,
169                 VOLUME_HUSH_MUTE);
170 
171         assertThat(mController.isChecked()).isTrue();
172     }
173 
174     @Test
isChecked_off_shouldReturnFalse()175     public void isChecked_off_shouldReturnFalse() {
176         Settings.Secure.putInt(mContext.getContentResolver(), VOLUME_HUSH_GESTURE,
177                 VOLUME_HUSH_OFF);
178 
179         assertThat(mController.isChecked()).isFalse();
180     }
181 }
182