Merge "Update Settings to use intensity settings as main preference keys"

This commit is contained in:
Lais Andrade
2022-01-13 02:45:41 +00:00
committed by Android (Google) Code Review
40 changed files with 1543 additions and 1703 deletions

View File

@@ -52,7 +52,6 @@ import androidx.test.core.app.ApplicationProvider;
import com.android.internal.content.PackageMonitor;
import com.android.settings.R;
import com.android.settings.testutils.XmlTestUtils;
import com.android.settings.testutils.shadow.ShadowDeviceConfig;
import com.android.settings.testutils.shadow.ShadowFragment;
import com.android.settings.testutils.shadow.ShadowUserManager;
import com.android.settingslib.RestrictedPreference;
@@ -143,22 +142,6 @@ public class AccessibilitySettingsTest {
assertThat(indexableRawList).isNull();
}
@Test
@Config(shadows = {ShadowDeviceConfig.class})
public void isRampingRingerEnabled_settingsFlagOn_Enabled() {
Settings.System.putInt(
mContext.getContentResolver(), Settings.System.APPLY_RAMPING_RINGER, ON);
assertThat(AccessibilitySettings.isRampingRingerEnabled(mContext)).isTrue();
}
@Test
@Config(shadows = {ShadowDeviceConfig.class})
public void isRampingRingerEnabled_settingsFlagOff_Disabled() {
Settings.System.putInt(
mContext.getContentResolver(), Settings.System.APPLY_RAMPING_RINGER, OFF);
assertThat(AccessibilitySettings.isRampingRingerEnabled(mContext)).isFalse();
}
@Test
public void getServiceSummary_serviceCrash_showsStopped() {
mServiceInfo.crashed = true;

View File

@@ -18,42 +18,136 @@ package com.android.settings.accessibility;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.os.VibrationAttributes;
import android.os.Vibrator;
import android.provider.Settings;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.widget.SeekBarPreference;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class HapticFeedbackIntensityPreferenceControllerTest {
private static final String PREFERENCE_KEY = "preference_key";
private static final int OFF = 0;
private static final int ON = 1;
@Mock
private PreferenceScreen mScreen;
private LifecycleOwner mLifecycleOwner;
private Lifecycle mLifecycle;
private Context mContext;
private Vibrator mVibrator;
private HapticFeedbackIntensityPreferenceController mController;
private SeekBarPreference mPreference;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mLifecycleOwner = () -> mLifecycle;
mLifecycle = new Lifecycle(mLifecycleOwner);
mContext = RuntimeEnvironment.application;
mController = new HapticFeedbackIntensityPreferenceController(mContext);
mContext = spy(ApplicationProvider.getApplicationContext());
mVibrator = mContext.getSystemService(Vibrator.class);
mController = new HapticFeedbackIntensityPreferenceController(mContext, PREFERENCE_KEY);
mLifecycle.addObserver(mController);
mPreference = new SeekBarPreference(mContext);
mPreference.setSummary("Test summary");
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
showPreference();
}
@Test
public void verifyConstants() {
assertThat(mController.getPreferenceKey())
.isEqualTo(HapticFeedbackIntensityPreferenceController.PREF_KEY);
assertThat(mController.getPreferenceKey()).isEqualTo(PREFERENCE_KEY);
assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.AVAILABLE);
assertThat(mController.getMin()).isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF);
assertThat(mController.getMax()).isEqualTo(Vibrator.VIBRATION_INTENSITY_HIGH);
}
@Test
public void missingSetting_shouldReturnDefault() {
Settings.System.putString(mContext.getContentResolver(),
Settings.System.HAPTIC_FEEDBACK_INTENSITY, /* value= */ null);
mController.updateState(mPreference);
assertThat(mPreference.getProgress())
.isEqualTo(mVibrator.getDefaultVibrationIntensity(VibrationAttributes.USAGE_TOUCH));
}
@Test
public void updateState_shouldDisplayIntensityInSliderPosition() {
updateSetting(Settings.System.HAPTIC_FEEDBACK_INTENSITY, Vibrator.VIBRATION_INTENSITY_HIGH);
mController.updateState(mPreference);
assertThat(mPreference.getProgress()).isEqualTo(Vibrator.VIBRATION_INTENSITY_HIGH);
updateSetting(Settings.System.HAPTIC_FEEDBACK_INTENSITY,
Vibrator.VIBRATION_INTENSITY_MEDIUM);
mController.updateState(mPreference);
assertThat(mPreference.getProgress()).isEqualTo(Vibrator.VIBRATION_INTENSITY_MEDIUM);
updateSetting(Settings.System.HAPTIC_FEEDBACK_INTENSITY, Vibrator.VIBRATION_INTENSITY_LOW);
mController.updateState(mPreference);
assertThat(mPreference.getProgress()).isEqualTo(Vibrator.VIBRATION_INTENSITY_LOW);
updateSetting(Settings.System.HAPTIC_FEEDBACK_INTENSITY, Vibrator.VIBRATION_INTENSITY_OFF);
mController.updateState(mPreference);
assertThat(mPreference.getProgress()).isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF);
}
@Test
public void setProgress_updatesIntensityAndDependentSettings() throws Exception {
mPreference.setProgress(Vibrator.VIBRATION_INTENSITY_OFF);
assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_INTENSITY))
.isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF);
assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_ENABLED)).isEqualTo(OFF);
mPreference.setProgress(Vibrator.VIBRATION_INTENSITY_LOW);
assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_INTENSITY))
.isEqualTo(Vibrator.VIBRATION_INTENSITY_LOW);
assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_ENABLED)).isEqualTo(ON);
mPreference.setProgress(Vibrator.VIBRATION_INTENSITY_MEDIUM);
assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_INTENSITY))
.isEqualTo(Vibrator.VIBRATION_INTENSITY_MEDIUM);
assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_ENABLED)).isEqualTo(ON);
mPreference.setProgress(Vibrator.VIBRATION_INTENSITY_HIGH);
assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_INTENSITY))
.isEqualTo(Vibrator.VIBRATION_INTENSITY_HIGH);
assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_ENABLED)).isEqualTo(ON);
mPreference.setProgress(Vibrator.VIBRATION_INTENSITY_OFF);
assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_INTENSITY))
.isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF);
assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_ENABLED)).isEqualTo(OFF);
}
private void updateSetting(String key, int value) {
Settings.System.putInt(mContext.getContentResolver(), key, value);
}
private int readSetting(String settingKey) throws Settings.SettingNotFoundException {
return Settings.System.getInt(mContext.getContentResolver(), settingKey);
}
private void showPreference() {
mController.displayPreference(mScreen);
}
}

View File

@@ -0,0 +1,139 @@
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.accessibility;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.os.VibrationAttributes;
import android.os.Vibrator;
import android.provider.Settings;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
public class HapticFeedbackTogglePreferenceControllerTest {
private static final String PREFERENCE_KEY = "preference_key";
private static final int OFF = 0;
private static final int ON = 1;
@Mock
private PreferenceScreen mScreen;
private LifecycleOwner mLifecycleOwner;
private Lifecycle mLifecycle;
private Context mContext;
private Vibrator mVibrator;
private HapticFeedbackTogglePreferenceController mController;
private SwitchPreference mPreference;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mLifecycleOwner = () -> mLifecycle;
mLifecycle = new Lifecycle(mLifecycleOwner);
mContext = spy(ApplicationProvider.getApplicationContext());
mVibrator = mContext.getSystemService(Vibrator.class);
mController = new HapticFeedbackTogglePreferenceController(mContext, PREFERENCE_KEY);
mLifecycle.addObserver(mController);
mPreference = new SwitchPreference(mContext);
mPreference.setSummary("Test summary");
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
showPreference();
}
@Test
public void verifyConstants() {
assertThat(mController.getPreferenceKey()).isEqualTo(PREFERENCE_KEY);
assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.AVAILABLE);
}
@Test
public void missingSetting_shouldReturnDefault() {
Settings.System.putString(mContext.getContentResolver(),
Settings.System.HAPTIC_FEEDBACK_INTENSITY, /* value= */ null);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isTrue();
}
@Test
public void updateState_shouldDisplayOnOffState() {
updateSetting(Settings.System.HAPTIC_FEEDBACK_INTENSITY, Vibrator.VIBRATION_INTENSITY_HIGH);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isTrue();
updateSetting(Settings.System.HAPTIC_FEEDBACK_INTENSITY,
Vibrator.VIBRATION_INTENSITY_MEDIUM);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isTrue();
updateSetting(Settings.System.HAPTIC_FEEDBACK_INTENSITY, Vibrator.VIBRATION_INTENSITY_LOW);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isTrue();
updateSetting(Settings.System.HAPTIC_FEEDBACK_INTENSITY, Vibrator.VIBRATION_INTENSITY_OFF);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isFalse();
}
@Test
public void setChecked_updatesIntensityAndDependentSettings() throws Exception {
updateSetting(Settings.System.HAPTIC_FEEDBACK_INTENSITY, Vibrator.VIBRATION_INTENSITY_OFF);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isFalse();
mPreference.setChecked(true);
assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_INTENSITY))
.isEqualTo(mVibrator.getDefaultVibrationIntensity(VibrationAttributes.USAGE_TOUCH));
assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_ENABLED)).isEqualTo(ON);
mPreference.setChecked(false);
assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_INTENSITY))
.isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF);
assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_ENABLED)).isEqualTo(OFF);
}
private void updateSetting(String key, int value) {
Settings.System.putInt(mContext.getContentResolver(), key, value);
}
private int readSetting(String settingKey) throws Settings.SettingNotFoundException {
return Settings.System.getInt(mContext.getContentResolver(), settingKey);
}
private void showPreference() {
mController.displayPreference(mScreen);
}
}

View File

@@ -16,24 +16,22 @@
package com.android.settings.accessibility;
import static android.provider.Settings.System.NOTIFICATION_VIBRATION_INTENSITY;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.res.Resources;
import android.os.VibrationAttributes;
import android.os.Vibrator;
import android.provider.Settings;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.widget.SeekBarPreference;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
@@ -42,115 +40,105 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class NotificationVibrationIntensityPreferenceControllerTest {
private static final String PREFERENCE_KEY = "preference_key";
@Mock
private PreferenceScreen mScreen;
private LifecycleOwner mLifecycleOwner;
private Lifecycle mLifecycle;
private Context mContext;
private Resources mResources;
private Vibrator mVibrator;
private NotificationVibrationIntensityPreferenceController mController;
private Preference mPreference;
private SeekBarPreference mPreference;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mLifecycleOwner = () -> mLifecycle;
mLifecycle = new Lifecycle(mLifecycleOwner);
mContext = spy(RuntimeEnvironment.application);
mResources = spy(mContext.getResources());
when(mContext.getResources()).thenReturn(mResources);
when(mResources.getBoolean(R.bool.config_vibration_supports_multiple_intensities))
.thenReturn(true);
mController = new NotificationVibrationIntensityPreferenceController(mContext) {
@Override
protected int getDefaultIntensity() {
return 10;
}
};
mContext = spy(ApplicationProvider.getApplicationContext());
mVibrator = mContext.getSystemService(Vibrator.class);
mController = new NotificationVibrationIntensityPreferenceController(mContext,
PREFERENCE_KEY);
mLifecycle.addObserver(mController);
mPreference = new Preference(mContext);
mPreference.setSummary("Test");
when(mScreen.findPreference(mController.getPreferenceKey()))
.thenReturn(mPreference);
mPreference = new SeekBarPreference(mContext);
mPreference.setSummary("Test summary");
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
showPreference();
}
@Test
public void verifyConstants() {
assertThat(mController.getPreferenceKey())
.isEqualTo(NotificationVibrationIntensityPreferenceController.PREF_KEY);
assertThat(mController.getPreferenceKey()).isEqualTo(PREFERENCE_KEY);
assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.AVAILABLE);
assertThat(mController.getMin()).isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF);
assertThat(mController.getMax()).isEqualTo(Vibrator.VIBRATION_INTENSITY_HIGH);
}
@Test
public void updateState_withMultipleIntensitySuport_shouldRefreshSummary() {
setSupportsMultipleIntensities(true);
showPreference();
Settings.System.putInt(mContext.getContentResolver(),
NOTIFICATION_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_LOW);
public void missingSetting_shouldReturnDefault() {
Settings.System.putString(mContext.getContentResolver(),
Settings.System.NOTIFICATION_VIBRATION_INTENSITY, /* value= */ null);
mController.updateState(mPreference);
assertThat(mPreference.getSummary())
.isEqualTo(mContext.getString(R.string.accessibility_vibration_intensity_low));
Settings.System.putInt(mContext.getContentResolver(),
NOTIFICATION_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_HIGH);
mController.updateState(mPreference);
assertThat(mPreference.getSummary())
.isEqualTo(mContext.getString(R.string.accessibility_vibration_intensity_high));
Settings.System.putInt(mContext.getContentResolver(),
NOTIFICATION_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_MEDIUM);
mController.updateState(mPreference);
assertThat(mPreference.getSummary())
.isEqualTo(mContext.getString(R.string.accessibility_vibration_intensity_medium));
Settings.System.putInt(mContext.getContentResolver(),
NOTIFICATION_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_OFF);
mController.updateState(mPreference);
assertThat(mPreference.getSummary())
.isEqualTo(mContext.getString(R.string.accessibility_vibration_intensity_off));
assertThat(mPreference.getProgress()).isEqualTo(
mVibrator.getDefaultVibrationIntensity(VibrationAttributes.USAGE_NOTIFICATION));
}
@Test
public void updateState_withoutMultipleIntensitySupport_shouldRefreshSummary() {
setSupportsMultipleIntensities(false);
showPreference();
Settings.System.putInt(mContext.getContentResolver(),
NOTIFICATION_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_LOW);
public void updateState_shouldDisplayIntensityInSliderPosition() {
updateSetting(Settings.System.NOTIFICATION_VIBRATION_INTENSITY,
Vibrator.VIBRATION_INTENSITY_HIGH);
mController.updateState(mPreference);
assertThat(mPreference.getSummary())
.isEqualTo(mContext.getString(R.string.switch_on_text));
assertThat(mPreference.getProgress()).isEqualTo(Vibrator.VIBRATION_INTENSITY_HIGH);
Settings.System.putInt(mContext.getContentResolver(),
NOTIFICATION_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_HIGH);
updateSetting(Settings.System.NOTIFICATION_VIBRATION_INTENSITY,
Vibrator.VIBRATION_INTENSITY_MEDIUM);
mController.updateState(mPreference);
assertThat(mPreference.getSummary())
.isEqualTo(mContext.getString(R.string.switch_on_text));
assertThat(mPreference.getProgress()).isEqualTo(Vibrator.VIBRATION_INTENSITY_MEDIUM);
Settings.System.putInt(mContext.getContentResolver(),
NOTIFICATION_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_MEDIUM);
updateSetting(Settings.System.NOTIFICATION_VIBRATION_INTENSITY,
Vibrator.VIBRATION_INTENSITY_LOW);
mController.updateState(mPreference);
assertThat(mPreference.getSummary())
.isEqualTo(mContext.getString(R.string.switch_on_text));
assertThat(mPreference.getProgress()).isEqualTo(Vibrator.VIBRATION_INTENSITY_LOW);
Settings.System.putInt(mContext.getContentResolver(),
NOTIFICATION_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_OFF);
updateSetting(Settings.System.NOTIFICATION_VIBRATION_INTENSITY,
Vibrator.VIBRATION_INTENSITY_OFF);
mController.updateState(mPreference);
assertThat(mPreference.getSummary())
.isEqualTo(mContext.getString(R.string.switch_off_text));
assertThat(mPreference.getProgress()).isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF);
}
private void setSupportsMultipleIntensities(boolean hasSupport) {
when(mResources.getBoolean(R.bool.config_vibration_supports_multiple_intensities))
.thenReturn(hasSupport);
@Test
public void setProgress_updatesIntensitySetting() throws Exception {
mPreference.setProgress(Vibrator.VIBRATION_INTENSITY_OFF);
assertThat(readSetting(Settings.System.NOTIFICATION_VIBRATION_INTENSITY))
.isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF);
mPreference.setProgress(Vibrator.VIBRATION_INTENSITY_LOW);
assertThat(readSetting(Settings.System.NOTIFICATION_VIBRATION_INTENSITY))
.isEqualTo(Vibrator.VIBRATION_INTENSITY_LOW);
mPreference.setProgress(Vibrator.VIBRATION_INTENSITY_MEDIUM);
assertThat(readSetting(Settings.System.NOTIFICATION_VIBRATION_INTENSITY))
.isEqualTo(Vibrator.VIBRATION_INTENSITY_MEDIUM);
mPreference.setProgress(Vibrator.VIBRATION_INTENSITY_HIGH);
assertThat(readSetting(Settings.System.NOTIFICATION_VIBRATION_INTENSITY))
.isEqualTo(Vibrator.VIBRATION_INTENSITY_HIGH);
}
private void updateSetting(String key, int value) {
Settings.System.putInt(mContext.getContentResolver(), key, value);
}
private int readSetting(String settingKey) throws Settings.SettingNotFoundException {
return Settings.System.getInt(mContext.getContentResolver(), settingKey);
}
private void showPreference() {

View File

@@ -0,0 +1,139 @@
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.accessibility;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.os.VibrationAttributes;
import android.os.Vibrator;
import android.provider.Settings;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
public class NotificationVibrationTogglePreferenceControllerTest {
private static final String PREFERENCE_KEY = "preference_key";
@Mock
private PreferenceScreen mScreen;
private LifecycleOwner mLifecycleOwner;
private Lifecycle mLifecycle;
private Context mContext;
private Vibrator mVibrator;
private NotificationVibrationTogglePreferenceController mController;
private SwitchPreference mPreference;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mLifecycleOwner = () -> mLifecycle;
mLifecycle = new Lifecycle(mLifecycleOwner);
mContext = spy(ApplicationProvider.getApplicationContext());
mVibrator = mContext.getSystemService(Vibrator.class);
mController = new NotificationVibrationTogglePreferenceController(mContext, PREFERENCE_KEY);
mLifecycle.addObserver(mController);
mPreference = new SwitchPreference(mContext);
mPreference.setSummary("Test summary");
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
showPreference();
}
@Test
public void verifyConstants() {
assertThat(mController.getPreferenceKey()).isEqualTo(PREFERENCE_KEY);
assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.AVAILABLE);
}
@Test
public void missingSetting_shouldReturnDefault() {
Settings.System.putString(mContext.getContentResolver(),
Settings.System.NOTIFICATION_VIBRATION_INTENSITY, /* value= */ null);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isTrue();
}
@Test
public void updateState_shouldDisplayOnOffState() {
updateSetting(Settings.System.NOTIFICATION_VIBRATION_INTENSITY,
Vibrator.VIBRATION_INTENSITY_HIGH);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isTrue();
updateSetting(Settings.System.NOTIFICATION_VIBRATION_INTENSITY,
Vibrator.VIBRATION_INTENSITY_MEDIUM);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isTrue();
updateSetting(Settings.System.NOTIFICATION_VIBRATION_INTENSITY,
Vibrator.VIBRATION_INTENSITY_LOW);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isTrue();
updateSetting(Settings.System.NOTIFICATION_VIBRATION_INTENSITY,
Vibrator.VIBRATION_INTENSITY_OFF);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isFalse();
}
@Test
public void setChecked_updatesIntensityAndDependentSettings() throws Exception {
updateSetting(Settings.System.NOTIFICATION_VIBRATION_INTENSITY,
Vibrator.VIBRATION_INTENSITY_OFF);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isFalse();
mPreference.setChecked(true);
assertThat(readSetting(Settings.System.NOTIFICATION_VIBRATION_INTENSITY)).isEqualTo(
mVibrator.getDefaultVibrationIntensity(VibrationAttributes.USAGE_NOTIFICATION));
mPreference.setChecked(false);
assertThat(readSetting(Settings.System.NOTIFICATION_VIBRATION_INTENSITY))
.isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF);
}
private void updateSetting(String key, int value) {
Settings.System.putInt(mContext.getContentResolver(), key, value);
}
private int readSetting(String settingKey) throws Settings.SettingNotFoundException {
return Settings.System.getInt(mContext.getContentResolver(), settingKey);
}
private void showPreference() {
mController.displayPreference(mScreen);
}
}

View File

@@ -16,20 +16,24 @@
package com.android.settings.accessibility;
import static com.android.settings.core.BasePreferenceController.AVAILABLE;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.os.VibrationAttributes;
import android.os.Vibrator;
import android.provider.Settings;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.widget.SeekBarPreference;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -40,30 +44,112 @@ import org.robolectric.RobolectricTestRunner;
/** Tests for {@link RingVibrationIntensityPreferenceController}. */
@RunWith(RobolectricTestRunner.class)
public class RingVibrationIntensityPreferenceControllerTest {
@Mock
private Vibrator mVibrator;
private static final String PREFERENCE_KEY = "preference_key";
private static final int OFF = 0;
private static final int ON = 1;
@Mock
private PreferenceScreen mScreen;
private LifecycleOwner mLifecycleOwner;
private Lifecycle mLifecycle;
private Context mContext;
private Vibrator mVibrator;
private RingVibrationIntensityPreferenceController mController;
private SeekBarPreference mPreference;
@Before
public void setUp() throws Exception {
public void setUp() {
MockitoAnnotations.initMocks(this);
final Context mContext = spy(ApplicationProvider.getApplicationContext());
doReturn(mVibrator).when(mContext).getSystemService(Vibrator.class);
mController = new RingVibrationIntensityPreferenceController(mContext);
mLifecycleOwner = () -> mLifecycle;
mLifecycle = new Lifecycle(mLifecycleOwner);
mContext = spy(ApplicationProvider.getApplicationContext());
mVibrator = mContext.getSystemService(Vibrator.class);
mController = new RingVibrationIntensityPreferenceController(mContext, PREFERENCE_KEY);
mLifecycle.addObserver(mController);
mPreference = new SeekBarPreference(mContext);
mPreference.setSummary("Test summary");
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
showPreference();
}
@Test
public void getAvailabilityStatus_available() {
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
public void verifyConstants() {
assertThat(mController.getPreferenceKey()).isEqualTo(PREFERENCE_KEY);
assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.AVAILABLE);
assertThat(mController.getMin()).isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF);
assertThat(mController.getMax()).isEqualTo(Vibrator.VIBRATION_INTENSITY_HIGH);
}
@Test
public void getDefaultIntensity_success() {
doReturn(/* toBeReturned= */ 5).when(mVibrator).getDefaultVibrationIntensity(
eq(VibrationAttributes.USAGE_RINGTONE));
public void missingSetting_shouldReturnDefault() {
Settings.System.putString(mContext.getContentResolver(),
Settings.System.RING_VIBRATION_INTENSITY, /* value= */ null);
mController.updateState(mPreference);
assertThat(mPreference.getProgress()).isEqualTo(
mVibrator.getDefaultVibrationIntensity(VibrationAttributes.USAGE_RINGTONE));
}
assertThat(mController.getDefaultIntensity()).isEqualTo(/* expected= */ 5);
@Test
public void updateState_shouldDisplayIntensityInSliderPosition() {
updateSetting(Settings.System.RING_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_HIGH);
mController.updateState(mPreference);
assertThat(mPreference.getProgress()).isEqualTo(Vibrator.VIBRATION_INTENSITY_HIGH);
updateSetting(Settings.System.RING_VIBRATION_INTENSITY,
Vibrator.VIBRATION_INTENSITY_MEDIUM);
mController.updateState(mPreference);
assertThat(mPreference.getProgress()).isEqualTo(Vibrator.VIBRATION_INTENSITY_MEDIUM);
updateSetting(Settings.System.RING_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_LOW);
mController.updateState(mPreference);
assertThat(mPreference.getProgress()).isEqualTo(Vibrator.VIBRATION_INTENSITY_LOW);
updateSetting(Settings.System.RING_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_OFF);
mController.updateState(mPreference);
assertThat(mPreference.getProgress()).isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF);
}
@Test
public void setProgress_updatesIntensityAndDependentSettings() throws Exception {
mPreference.setProgress(Vibrator.VIBRATION_INTENSITY_OFF);
assertThat(readSetting(Settings.System.RING_VIBRATION_INTENSITY))
.isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF);
assertThat(readSetting(Settings.System.VIBRATE_WHEN_RINGING)).isEqualTo(OFF);
mPreference.setProgress(Vibrator.VIBRATION_INTENSITY_LOW);
assertThat(readSetting(Settings.System.RING_VIBRATION_INTENSITY))
.isEqualTo(Vibrator.VIBRATION_INTENSITY_LOW);
assertThat(readSetting(Settings.System.VIBRATE_WHEN_RINGING)).isEqualTo(ON);
mPreference.setProgress(Vibrator.VIBRATION_INTENSITY_MEDIUM);
assertThat(readSetting(Settings.System.RING_VIBRATION_INTENSITY))
.isEqualTo(Vibrator.VIBRATION_INTENSITY_MEDIUM);
assertThat(readSetting(Settings.System.VIBRATE_WHEN_RINGING)).isEqualTo(ON);
mPreference.setProgress(Vibrator.VIBRATION_INTENSITY_HIGH);
assertThat(readSetting(Settings.System.RING_VIBRATION_INTENSITY))
.isEqualTo(Vibrator.VIBRATION_INTENSITY_HIGH);
assertThat(readSetting(Settings.System.VIBRATE_WHEN_RINGING)).isEqualTo(ON);
mPreference.setProgress(Vibrator.VIBRATION_INTENSITY_OFF);
assertThat(readSetting(Settings.System.RING_VIBRATION_INTENSITY))
.isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF);
assertThat(readSetting(Settings.System.VIBRATE_WHEN_RINGING)).isEqualTo(OFF);
}
private void updateSetting(String key, int value) {
Settings.System.putInt(mContext.getContentResolver(), key, value);
}
private int readSetting(String settingKey) throws Settings.SettingNotFoundException {
return Settings.System.getInt(mContext.getContentResolver(), settingKey);
}
private void showPreference() {
mController.displayPreference(mScreen);
}
}

View File

@@ -1,67 +0,0 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.accessibility;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import android.content.Context;
import android.provider.Settings;
import com.android.settings.testutils.shadow.ShadowDeviceConfig;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
@RunWith(RobolectricTestRunner.class)
public class RingVibrationPreferenceFragmentTest {
private Context mContext;
private RingVibrationPreferenceFragment mFragment;
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mFragment = spy(new RingVibrationPreferenceFragment());
doReturn(mContext).when(mFragment).getContext();
}
@Test
@Config(shadows = {ShadowDeviceConfig.class})
public void getVibrationEnabledSetting_rampingRingerEnabled_returnApplyRampingRinger() {
// Turn on both flags to enable ramping ringer.
Settings.System.putInt(
mContext.getContentResolver(), Settings.System.APPLY_RAMPING_RINGER, 1 /* ON */);
assertThat(mFragment.getVibrationEnabledSetting()).isEqualTo(
Settings.System.APPLY_RAMPING_RINGER);
}
@Test
public void getVibrationEnabledSetting_rampingRingerDisabled_returnVibrationWhenRinging() {
// Turn off Settings.System.APPLY_RAMPING_RINGER to disable ramping ringer.
Settings.System.putInt(
mContext.getContentResolver(), Settings.System.APPLY_RAMPING_RINGER, 0 /* OFF */);
assertThat(mFragment.getVibrationEnabledSetting()).isEqualTo(
Settings.System.VIBRATE_WHEN_RINGING);
}
}

View File

@@ -0,0 +1,139 @@
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.accessibility;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.os.VibrationAttributes;
import android.os.Vibrator;
import android.provider.Settings;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
public class RingVibrationTogglePreferenceControllerTest {
private static final String PREFERENCE_KEY = "preference_key";
private static final int OFF = 0;
private static final int ON = 1;
@Mock
private PreferenceScreen mScreen;
private LifecycleOwner mLifecycleOwner;
private Lifecycle mLifecycle;
private Context mContext;
private Vibrator mVibrator;
private RingVibrationTogglePreferenceController mController;
private SwitchPreference mPreference;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mLifecycleOwner = () -> mLifecycle;
mLifecycle = new Lifecycle(mLifecycleOwner);
mContext = spy(ApplicationProvider.getApplicationContext());
mVibrator = mContext.getSystemService(Vibrator.class);
mController = new RingVibrationTogglePreferenceController(mContext, PREFERENCE_KEY);
mLifecycle.addObserver(mController);
mPreference = new SwitchPreference(mContext);
mPreference.setSummary("Test summary");
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
showPreference();
}
@Test
public void verifyConstants() {
assertThat(mController.getPreferenceKey()).isEqualTo(PREFERENCE_KEY);
assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.AVAILABLE);
}
@Test
public void missingSetting_shouldReturnDefault() {
Settings.System.putString(mContext.getContentResolver(),
Settings.System.RING_VIBRATION_INTENSITY, /* value= */ null);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isTrue();
}
@Test
public void updateState_shouldDisplayOnOffState() {
updateSetting(Settings.System.RING_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_HIGH);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isTrue();
updateSetting(Settings.System.RING_VIBRATION_INTENSITY,
Vibrator.VIBRATION_INTENSITY_MEDIUM);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isTrue();
updateSetting(Settings.System.RING_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_LOW);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isTrue();
updateSetting(Settings.System.RING_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_OFF);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isFalse();
}
@Test
public void setChecked_updatesIntensityAndDependentSettings() throws Exception {
updateSetting(Settings.System.RING_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_OFF);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isFalse();
mPreference.setChecked(true);
assertThat(readSetting(Settings.System.RING_VIBRATION_INTENSITY)).isEqualTo(
mVibrator.getDefaultVibrationIntensity(VibrationAttributes.USAGE_RINGTONE));
assertThat(readSetting(Settings.System.VIBRATE_WHEN_RINGING)).isEqualTo(ON);
mPreference.setChecked(false);
assertThat(readSetting(Settings.System.RING_VIBRATION_INTENSITY))
.isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF);
assertThat(readSetting(Settings.System.VIBRATE_WHEN_RINGING)).isEqualTo(OFF);
}
private void updateSetting(String key, int value) {
Settings.System.putInt(mContext.getContentResolver(), key, value);
}
private int readSetting(String settingKey) throws Settings.SettingNotFoundException {
return Settings.System.getInt(mContext.getContentResolver(), settingKey);
}
private void showPreference() {
mController.displayPreference(mScreen);
}
}

View File

@@ -1,87 +0,0 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.accessibility;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import android.os.Vibrator;
import android.provider.Settings;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class VibrationPreferenceControllerTest {
private static final String VIBRATION_ON = "On";
private static final String VIBRATION_OFF = "Off";
private Context mContext;
private VibrationPreferenceController mController;
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mController = new VibrationPreferenceController(mContext, "vibration_pref");
}
@Test
public void getAvailabilityStatus_byDefault_shouldReturnAvailable() {
assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.AVAILABLE);
}
@Test
public void getSummary_disabledVibration_shouldReturnOffSummary() {
Settings.System.putInt(mContext.getContentResolver(),
Settings.System.RING_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_OFF);
Settings.System.putInt(mContext.getContentResolver(),
Settings.System.NOTIFICATION_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_OFF);
Settings.System.putInt(mContext.getContentResolver(),
Settings.System.HAPTIC_FEEDBACK_INTENSITY, Vibrator.VIBRATION_INTENSITY_OFF);
final String expectedResult = mContext.getString(R.string.switch_off_text);
assertThat(mController.getSummary()).isEqualTo(expectedResult);
}
@Test
public void getSummary_enabledSomeVibration_shouldReturnVibrationOnSummary() {
Settings.System.putInt(mContext.getContentResolver(),
Settings.System.RING_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_MEDIUM);
Settings.System.putInt(mContext.getContentResolver(),
Settings.System.VIBRATE_WHEN_RINGING, 1);
Settings.System.putInt(mContext.getContentResolver(),
Settings.System.NOTIFICATION_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_OFF);
Settings.System.putInt(mContext.getContentResolver(),
Settings.System.HAPTIC_FEEDBACK_INTENSITY, Vibrator.VIBRATION_INTENSITY_MEDIUM);
Settings.System.putInt(mContext.getContentResolver(),
Settings.System.HAPTIC_FEEDBACK_ENABLED, 1);
final String expectedResult = mContext.getString(R.string.accessibility_vibration_summary,
VIBRATION_ON /* ring */,
VIBRATION_OFF /* notification */,
VIBRATION_ON /* touch */);
assertThat(mController.getSummary()).isEqualTo(expectedResult);
}
}

View File

@@ -1,178 +0,0 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.accessibility;
import static com.android.settings.accessibility.VibrationPreferenceFragment.KEY_INTENSITY_HIGH;
import static com.android.settings.accessibility.VibrationPreferenceFragment.KEY_INTENSITY_LOW;
import static com.android.settings.accessibility.VibrationPreferenceFragment.KEY_INTENSITY_MEDIUM;
import static com.android.settings.accessibility.VibrationPreferenceFragment.KEY_INTENSITY_OFF;
import static com.android.settings.accessibility.VibrationPreferenceFragment.KEY_INTENSITY_ON;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.res.Resources;
import android.os.UserManager;
import android.os.Vibrator;
import android.provider.Settings;
import com.android.settings.R;
import com.android.settings.accessibility.VibrationPreferenceFragment
.VibrationIntensityCandidateInfo;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settingslib.widget.CandidateInfo;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RunWith(RobolectricTestRunner.class)
public class VibrationPreferenceFragmentTest {
private static final Map<Integer, String> INTENSITY_TO_KEY = new HashMap<>(4);
static {
INTENSITY_TO_KEY.put(Vibrator.VIBRATION_INTENSITY_OFF, KEY_INTENSITY_OFF);
INTENSITY_TO_KEY.put(Vibrator.VIBRATION_INTENSITY_LOW, KEY_INTENSITY_LOW);
INTENSITY_TO_KEY.put(Vibrator.VIBRATION_INTENSITY_MEDIUM, KEY_INTENSITY_MEDIUM);
INTENSITY_TO_KEY.put(Vibrator.VIBRATION_INTENSITY_HIGH, KEY_INTENSITY_HIGH);
}
@Mock
private UserManager mUserManager;
private Context mContext;
private Resources mResources;
private TestVibrationPreferenceFragment mFragment;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
FakeFeatureFactory.setupForTest();
mContext = spy(RuntimeEnvironment.application);
mResources = spy(mContext.getResources());
when(mContext.getResources()).thenReturn(mResources);
mFragment = spy(new TestVibrationPreferenceFragment());
when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
}
@Test
public void changeIntensitySetting_shouldResultInCorrespondingKey() {
setSupportsMultipleIntensities(true);
mFragment.onAttach(mContext);
for (Map.Entry<Integer, String> entry : INTENSITY_TO_KEY.entrySet()) {
Settings.System.putInt(mContext.getContentResolver(),
Settings.System.HAPTIC_FEEDBACK_INTENSITY, entry.getKey());
assertThat(mFragment.getDefaultKey()).isEqualTo(entry.getValue());
}
}
@Test
public void changeIntensitySetting_WithoutMultipleIntensitySupport_shouldResultInOn() {
setSupportsMultipleIntensities(false);
mFragment.onAttach(mContext);
for (int intensity : INTENSITY_TO_KEY.keySet()) {
Settings.System.putInt(mContext.getContentResolver(),
Settings.System.HAPTIC_FEEDBACK_INTENSITY, intensity);
final String expectedKey = intensity == Vibrator.VIBRATION_INTENSITY_OFF
? KEY_INTENSITY_OFF
: KEY_INTENSITY_ON;
assertThat(mFragment.getDefaultKey()).isEqualTo(expectedKey);
}
}
@Test
public void initialDefaultKey_shouldBeMedium() {
setSupportsMultipleIntensities(true);
mFragment.onAttach(mContext);
assertThat(mFragment.getDefaultKey()).isEqualTo(KEY_INTENSITY_MEDIUM);
}
@Test
public void initialDefaultKey_WithoutMultipleIntensitySupport_shouldBeOn() {
setSupportsMultipleIntensities(false);
mFragment.onAttach(mContext);
assertThat(mFragment.getDefaultKey()).isEqualTo(KEY_INTENSITY_ON);
}
@Test
public void candidates_shouldBeSortedByIntensity() {
setSupportsMultipleIntensities(true);
mFragment.onAttach(mContext);
final List<? extends CandidateInfo> candidates = mFragment.getCandidates();
assertThat(candidates.size()).isEqualTo(INTENSITY_TO_KEY.size());
VibrationIntensityCandidateInfo prevCandidate =
(VibrationIntensityCandidateInfo) candidates.get(0);
for (int i = 1; i < candidates.size(); i++) {
VibrationIntensityCandidateInfo candidate =
(VibrationIntensityCandidateInfo) candidates.get(i);
assertThat(candidate.getIntensity()).isLessThan(prevCandidate.getIntensity());
}
}
private void setSupportsMultipleIntensities(boolean hasSupport) {
when(mResources.getBoolean(R.bool.config_vibration_supports_multiple_intensities))
.thenReturn(hasSupport);
}
private class TestVibrationPreferenceFragment extends VibrationPreferenceFragment {
@Override
protected int getPreferenceScreenResId() {
return 0;
}
@Override
public int getMetricsCategory() {
return 0;
}
/**
* Get the setting string of the vibration intensity setting this preference is dealing with.
*/
@Override
protected String getVibrationIntensitySetting() {
return Settings.System.HAPTIC_FEEDBACK_INTENSITY;
}
@Override
protected String getVibrationEnabledSetting() {
return "";
}
@Override
protected int getDefaultVibrationIntensity() {
return Vibrator.VIBRATION_INTENSITY_MEDIUM;
}
@Override
public Context getContext() {
return mContext;
}
}
}

View File

@@ -0,0 +1,169 @@
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.accessibility;
import static com.android.settings.core.BasePreferenceController.AVAILABLE;
import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.media.AudioManager;
import android.os.Vibrator;
import android.provider.DeviceConfig;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
import androidx.test.core.app.ApplicationProvider;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
public class VibrationRampingRingerTogglePreferenceControllerTest {
private static final String PREFERENCE_KEY = "preference_key";
@Mock
private PreferenceScreen mScreen;
@Mock
private TelephonyManager mTelephonyManager;
@Mock
private AudioManager mAudioManager;
private LifecycleOwner mLifecycleOwner;
private Lifecycle mLifecycle;
private Context mContext;
private VibrationRampingRingerTogglePreferenceController mController;
private SwitchPreference mPreference;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mLifecycleOwner = () -> mLifecycle;
mLifecycle = new Lifecycle(mLifecycleOwner);
mContext = spy(ApplicationProvider.getApplicationContext());
when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
when(mContext.getSystemService(Context.AUDIO_SERVICE)).thenReturn(mAudioManager);
mController = new VibrationRampingRingerTogglePreferenceController(mContext,
PREFERENCE_KEY);
mLifecycle.addObserver(mController);
mPreference = new SwitchPreference(mContext);
mPreference.setSummary("Test summary");
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
showPreference();
}
@Test
public void verifyConstants() {
assertThat(mController.getPreferenceKey()).isEqualTo(PREFERENCE_KEY);
}
@Test
public void getAvailabilityStatus_notVoiceCapable_returnUnsupportedOnDevice() {
when(mTelephonyManager.isVoiceCapable()).thenReturn(false);
DeviceConfig.setProperty(DeviceConfig.NAMESPACE_TELEPHONY,
VibrationRampingRingerTogglePreferenceController.DEVICE_CONFIG_KEY, "false", false);
assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
}
@Test
public void getAvailabilityStatus_rampingRingerEnabled_returnUnsupportedOnDevice() {
when(mTelephonyManager.isVoiceCapable()).thenReturn(true);
DeviceConfig.setProperty(DeviceConfig.NAMESPACE_TELEPHONY,
VibrationRampingRingerTogglePreferenceController.DEVICE_CONFIG_KEY, "true", false);
assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
}
@Test
public void getAvailabilityStatus_voiceCapableAndRampingRingerDisabled_returnAvailable() {
when(mTelephonyManager.isVoiceCapable()).thenReturn(true);
DeviceConfig.setProperty(DeviceConfig.NAMESPACE_TELEPHONY,
VibrationRampingRingerTogglePreferenceController.DEVICE_CONFIG_KEY, "false", false);
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
}
@Test
public void updateState_withRingDisabled_shouldReturnFalseForCheckedAndEnabled() {
updateSetting(Settings.System.RING_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_OFF);
when(mAudioManager.isRampingRingerEnabled()).thenReturn(true);
mController.updateState(mPreference);
assertThat(mPreference.isEnabled()).isFalse();
assertThat(mPreference.isChecked()).isFalse();
}
@Test
public void updateState_withRingEnabled_shouldReturnTheSettingStateAndAlwaysEnabled() {
updateSetting(Settings.System.RING_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_HIGH);
when(mAudioManager.isRampingRingerEnabled()).thenReturn(true, false);
mController.updateState(mPreference);
assertThat(mPreference.isEnabled()).isTrue();
assertThat(mPreference.isChecked()).isTrue();
mController.updateState(mPreference);
assertThat(mPreference.isEnabled()).isTrue();
assertThat(mPreference.isChecked()).isFalse();
}
@Test
public void setChecked_withRingDisabled_ignoresUpdates() {
updateSetting(Settings.System.RING_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_OFF);
mPreference.setChecked(true);
mPreference.setChecked(false);
verify(mAudioManager, never()).setRampingRingerEnabled(anyBoolean());
}
@Test
public void setChecked_withRingEnabled_updatesSetting() {
updateSetting(Settings.System.RING_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_HIGH);
mPreference.setChecked(true);
verify(mAudioManager).setRampingRingerEnabled(true);
mPreference.setChecked(false);
verify(mAudioManager).setRampingRingerEnabled(false);
}
private void updateSetting(String key, int value) {
Settings.System.putInt(mContext.getContentResolver(), key, value);
}
private void showPreference() {
mController.displayPreference(mScreen);
}
}

View File

@@ -1,120 +0,0 @@
/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.sound;
import static com.android.settings.core.BasePreferenceController.AVAILABLE;
import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.content.ContentResolver;
import android.content.Context;
import android.provider.DeviceConfig;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import com.android.settings.R;
import com.android.settings.testutils.shadow.ShadowDeviceConfig;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = {ShadowDeviceConfig.class})
public class VibrateForCallsPreferenceControllerTest {
private static final int OFF = 0;
private static final int ON = 1;
private Context mContext;
private ContentResolver mContentResolver;
@Mock
private TelephonyManager mTelephonyManager;
private VibrateForCallsPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
mContentResolver = mContext.getContentResolver();
when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
mController = new VibrateForCallsPreferenceController(
mContext, VibrateForCallsPreferenceController.RAMPING_RINGER_ENABLED);
}
@Test
public void getAvailabilityStatus_notVoiceCapable_returnUnsupportedOnDevice() {
when(mTelephonyManager.isVoiceCapable()).thenReturn(false);
DeviceConfig.setProperty(DeviceConfig.NAMESPACE_TELEPHONY,
VibrateForCallsPreferenceController.RAMPING_RINGER_ENABLED, "false", false);
assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
}
@Test
public void getAvailabilityStatus_rampingRingerEnabled_returnUnsupportedOnDevice() {
when(mTelephonyManager.isVoiceCapable()).thenReturn(true);
DeviceConfig.setProperty(DeviceConfig.NAMESPACE_TELEPHONY,
VibrateForCallsPreferenceController.RAMPING_RINGER_ENABLED, "true", false);
assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
}
@Test
public void getAvailabilityStatus_voiceCapableAndRampingRingerDisabled_returnAvailable() {
when(mTelephonyManager.isVoiceCapable()).thenReturn(true);
DeviceConfig.setProperty(DeviceConfig.NAMESPACE_TELEPHONY,
VibrateForCallsPreferenceController.RAMPING_RINGER_ENABLED, "false", false);
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
}
@Test
public void getSummary_applyRampingRinger_rampingRingerSummary() {
Settings.System.putInt(mContentResolver, Settings.System.VIBRATE_WHEN_RINGING, OFF);
Settings.System.putInt(mContentResolver, Settings.System.APPLY_RAMPING_RINGER, ON);
assertThat(mController.getSummary()).isEqualTo(
mContext.getText(R.string.vibrate_when_ringing_option_ramping_ringer));
}
@Test
public void getSummary_enableVibrateWhenRinging_alwaysVibrateSummary() {
Settings.System.putInt(mContentResolver, Settings.System.VIBRATE_WHEN_RINGING, ON);
Settings.System.putInt(mContentResolver, Settings.System.APPLY_RAMPING_RINGER, OFF);
assertThat(mController.getSummary()).isEqualTo(
mContext.getText(R.string.vibrate_when_ringing_option_always_vibrate));
}
@Test
public void getSummary_notApplyRampingRingerDisableVibrateWhenRinging_neverVibrateSummary() {
Settings.System.putInt(mContentResolver, Settings.System.VIBRATE_WHEN_RINGING, OFF);
Settings.System.putInt(mContentResolver, Settings.System.APPLY_RAMPING_RINGER, OFF);
assertThat(mController.getSummary()).isEqualTo(
mContext.getText(R.string.vibrate_when_ringing_option_never_vibrate));
}
}

View File

@@ -1,108 +0,0 @@
/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.sound;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class VibrateForCallsPreferenceFragmentTest {
private static final int OFF = 0;
private static final int ON = 1;
private Context mContext;
private ContentResolver mContentResolver;
private VibrateForCallsPreferenceFragment mFragment;
@Before
public void setUp() {
mContext = spy(RuntimeEnvironment.application);
mContentResolver = mContext.getContentResolver();
mFragment = spy(new VibrateForCallsPreferenceFragment());
doReturn(mContext).when(mFragment).getContext();
mFragment.onAttach(mContext);
}
@Test
public void getDefaultKey_applyRampingRinger_keyRampingRinger() {
Settings.System.putInt(mContentResolver, Settings.System.VIBRATE_WHEN_RINGING, OFF);
Settings.System.putInt(mContentResolver, Settings.System.APPLY_RAMPING_RINGER, ON);
assertThat(mFragment.getDefaultKey()).isEqualTo(
VibrateForCallsPreferenceFragment.KEY_RAMPING_RINGER);
}
@Test
public void getDefaultKey_enableVibrateWhenRinging_keyAlwaysVibrate() {
Settings.System.putInt(mContentResolver, Settings.System.VIBRATE_WHEN_RINGING, ON);
Settings.System.putInt(mContentResolver, Settings.System.APPLY_RAMPING_RINGER, OFF);
assertThat(mFragment.getDefaultKey()).isEqualTo(
VibrateForCallsPreferenceFragment.KEY_ALWAYS_VIBRATE);
}
@Test
public void getDefaultKey_notApplyRampingRingerDisableVibrateWhenRinging_keyNeverVibrate() {
Settings.System.putInt(mContentResolver, Settings.System.VIBRATE_WHEN_RINGING, OFF);
Settings.System.putInt(mContentResolver, Settings.System.APPLY_RAMPING_RINGER, OFF);
assertThat(mFragment.getDefaultKey()).isEqualTo(
VibrateForCallsPreferenceFragment.KEY_NEVER_VIBRATE);
}
@Test
public void setDefaultKey_keyRampingRinger_applyRampingRingerDisableVibrateWhenRinging() {
mFragment.setDefaultKey(VibrateForCallsPreferenceFragment.KEY_RAMPING_RINGER);
assertThat(Settings.System.getInt(
mContentResolver, Settings.System.APPLY_RAMPING_RINGER, OFF)).isEqualTo(ON);
assertThat(Settings.System.getInt(
mContentResolver, Settings.System.VIBRATE_WHEN_RINGING, OFF)).isEqualTo(OFF);
}
@Test
public void setDefaultKey_keyAlwaysVibrate_notApplyRampingRingerEnableVibrateWhenRinging() {
mFragment.setDefaultKey(VibrateForCallsPreferenceFragment.KEY_ALWAYS_VIBRATE);
assertThat(Settings.System.getInt(
mContentResolver, Settings.System.APPLY_RAMPING_RINGER, OFF)).isEqualTo(OFF);
assertThat(Settings.System.getInt(
mContentResolver, Settings.System.VIBRATE_WHEN_RINGING, OFF)).isEqualTo(ON);
}
@Test
public void setDefaultKey_keyNeverVibrate_notApplyRampingRingerDisableVibrateWhenRinging() {
mFragment.setDefaultKey(VibrateForCallsPreferenceFragment.KEY_NEVER_VIBRATE);
assertThat(Settings.System.getInt(
mContentResolver, Settings.System.APPLY_RAMPING_RINGER, OFF)).isEqualTo(OFF);
assertThat(Settings.System.getInt(
mContentResolver, Settings.System.VIBRATE_WHEN_RINGING, OFF)).isEqualTo(OFF);
}
}