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