Introduce alarm and media vibration intensity settings

Introduce toggles and sliders to configure the alarm and media
vibrations in the "Vibration & haptics" settings app.

Also update the multiple intensities configuration flag into a integer,
where the device can specify how many distinct levels are supported.
Follow existing implementation to map the intensities to higher setting
values.

Bug: 198346559
Bug: 207477604
Test: [Alarm|Media]Vibration[Intensity|Toggle]PreferenceControllerTest
Change-Id: Ie3d570b72ba1229e613ecf0c45fac81233529e32
This commit is contained in:
Lais Andrade
2022-01-07 20:12:19 +00:00
parent 8e7a57e8fa
commit eaaf5331d6
26 changed files with 1102 additions and 146 deletions

View File

@@ -197,11 +197,24 @@
<!-- Whether or not to show the night light suggestion. --> <!-- Whether or not to show the night light suggestion. -->
<bool name="config_night_light_suggestion_enabled">true</bool> <bool name="config_night_light_suggestion_enabled">true</bool>
<!-- Whether or not the device is capable of multiple levels of vibration intensity. <!-- The number of vibration intensity levels supported by the device.
Note that this is different from whether it can control the vibration amplitude as some
devices will be able to vary their amplitude but do not possess enough dynamic range to Note that this should correspond to the ability to vary the vibration amplitude, with
have distinct intensity levels --> enough dynamic range to have at least as many distinct intensity levels as defined here.
<bool name="config_vibration_supports_multiple_intensities">false</bool>
Supported values are 1, 2, 3. If '1', the settings app will use a toggle for the settings,
otherwise a slider. If '3', the settings app maps intensities directly to the levels
supported by the Vibrator HAL APIs. If '2', then the levels will be mapped to
VIBRATION_INTENSITY_LOW and VIBRATION_INTENSITY_HIGH, which gives the most range for
scaling vibrations. The medium intensity will be skipped.
The default intensity values are configured at
frameworks/base/core/res/res/values/config.xml's config_default[type]VibrationIntensity.
Make sure that each default intensity value is consistent with the supported levels set
here. If this settings supports only 2 levels, for example, then the default intensity
should be either LOW (1) or HIGH (3).
-->
<integer name="config_vibration_supported_intensity_levels">1</integer>
<!-- <!--
Whether or not the homepage should be powered by legacy suggestion (versus contextual cards) Whether or not the homepage should be powered by legacy suggestion (versus contextual cards)

View File

@@ -5457,6 +5457,10 @@
<string name="accessibility_notification_alarm_vibration_category_title">Notifications and alarms</string> <string name="accessibility_notification_alarm_vibration_category_title">Notifications and alarms</string>
<!-- Title for the category of preferences to configure device vibrations triggered by user interaction with the device. [CHAR LIMIT=NONE] --> <!-- Title for the category of preferences to configure device vibrations triggered by user interaction with the device. [CHAR LIMIT=NONE] -->
<string name="accessibility_interactive_haptics_category_title">Interactive haptics</string> <string name="accessibility_interactive_haptics_category_title">Interactive haptics</string>
<!-- Title for preference for configuring alarm vibrations. [CHAR LIMIT=NONE] -->
<string name="accessibility_alarm_vibration_title">Alarm vibration</string>
<!-- Title for preference for configuring media vibrations (e.g. vibrations played together with animations, music, videos, etc). [CHAR LIMIT=NONE] -->
<string name="accessibility_media_vibration_title">Media vibration</string>
<!-- Title for accessibility preference for configuring ring vibrations. [CHAR LIMIT=NONE] --> <!-- Title for accessibility preference for configuring ring vibrations. [CHAR LIMIT=NONE] -->
<string name="accessibility_ring_vibration_title">Ring vibration</string> <string name="accessibility_ring_vibration_title">Ring vibration</string>
<!-- Title for accessibility preference for configuring notification vibrations. --> <!-- Title for accessibility preference for configuring notification vibrations. -->
@@ -8238,7 +8242,11 @@
<!-- List of synonyms for ring vibration setting (changes whether your phone vibrates when it rings), used to match in settings search [CHAR LIMIT=NONE] --> <!-- List of synonyms for ring vibration setting (changes whether your phone vibrates when it rings), used to match in settings search [CHAR LIMIT=NONE] -->
<string name="keywords_ramping_ringer_vibration">haptics, vibrate, phone, call, ring, gradually</string> <string name="keywords_ramping_ringer_vibration">haptics, vibrate, phone, call, ring, gradually</string>
<!-- List of synonyms for notification vibration setting (changes whether your phone vibrates when it shows a notification), used to match in settings search [CHAR LIMIT=NONE] --> <!-- List of synonyms for notification vibration setting (changes whether your phone vibrates when it shows a notification), used to match in settings search [CHAR LIMIT=NONE] -->
<string name="keywords_notification_vibration">haptics, vibrate, sensitivity</string> <string name="keywords_notification_vibration">haptics, vibrate, sensitivity, notification</string>
<!-- List of synonyms for alarm vibration setting (changes whether your phone vibrates when an alarm goes off), used to match in settings search [CHAR LIMIT=NONE] -->
<string name="keywords_alarm_vibration">haptics, vibrate, sensitivity, alarm</string>
<!-- List of synonyms for media vibration setting (changes whether your phone vibrates as part of a music, animation, video, etc), used to match in settings search [CHAR LIMIT=NONE] -->
<string name="keywords_media_vibration">haptics, vibrate, sensitivity, media</string>
<!-- List of synonyms for vibration and haptics setting, used to match in settings search [CHAR LIMIT=NONE] --> <!-- List of synonyms for vibration and haptics setting, used to match in settings search [CHAR LIMIT=NONE] -->
<string name="keywords_vibration">haptics, vibrate, vibration</string> <string name="keywords_vibration">haptics, vibrate, vibration</string>
<!-- Battery Saver: Search terms for sticky battery saver preference [CHAR_LIMIT=NONE] --> <!-- Battery Saver: Search terms for sticky battery saver preference [CHAR_LIMIT=NONE] -->

View File

@@ -47,6 +47,12 @@
app:keywords="@string/keywords_notification_vibration" app:keywords="@string/keywords_notification_vibration"
app:controller="com.android.settings.accessibility.NotificationVibrationIntensityPreferenceController" /> app:controller="com.android.settings.accessibility.NotificationVibrationIntensityPreferenceController" />
<com.android.settings.widget.SeekBarPreference
android:key="alarm_vibration_preference_screen"
android:title="@string/accessibility_alarm_vibration_title"
app:keywords="@string/keywords_alarm_vibration"
app:controller="com.android.settings.accessibility.AlarmVibrationIntensityPreferenceController" />
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory <PreferenceCategory
@@ -59,6 +65,12 @@
app:keywords="@string/keywords_touch_vibration" app:keywords="@string/keywords_touch_vibration"
app:controller="com.android.settings.accessibility.HapticFeedbackIntensityPreferenceController" /> app:controller="com.android.settings.accessibility.HapticFeedbackIntensityPreferenceController" />
<com.android.settings.widget.SeekBarPreference
android:key="media_vibration_preference_screen"
android:title="@string/accessibility_media_vibration_title"
app:keywords="@string/keywords_media_vibration"
app:controller="com.android.settings.accessibility.MediaVibrationIntensityPreferenceController" />
</PreferenceCategory> </PreferenceCategory>
</PreferenceScreen> </PreferenceScreen>

View File

@@ -47,6 +47,12 @@
app:keywords="@string/keywords_notification_vibration" app:keywords="@string/keywords_notification_vibration"
app:controller="com.android.settings.accessibility.NotificationVibrationTogglePreferenceController" /> app:controller="com.android.settings.accessibility.NotificationVibrationTogglePreferenceController" />
<SwitchPreference
android:key="alarm_vibration_preference_screen"
android:title="@string/accessibility_alarm_vibration_title"
app:keywords="@string/keywords_alarm_vibration"
app:controller="com.android.settings.accessibility.AlarmVibrationTogglePreferenceController" />
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory <PreferenceCategory
@@ -59,6 +65,12 @@
app:keywords="@string/keywords_touch_vibration" app:keywords="@string/keywords_touch_vibration"
app:controller="com.android.settings.accessibility.HapticFeedbackTogglePreferenceController" /> app:controller="com.android.settings.accessibility.HapticFeedbackTogglePreferenceController" />
<SwitchPreference
android:key="media_vibration_preference_screen"
android:title="@string/accessibility_media_vibration_title"
app:keywords="@string/keywords_media_vibration"
app:controller="com.android.settings.accessibility.MediaVibrationTogglePreferenceController" />
</PreferenceCategory> </PreferenceCategory>
</PreferenceScreen> </PreferenceScreen>

View File

@@ -0,0 +1,50 @@
/*
* 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 android.content.Context;
import android.os.VibrationAttributes;
import android.provider.Settings;
/** Preference controller for alarm vibration intensity */
public class AlarmVibrationIntensityPreferenceController
extends VibrationIntensityPreferenceController {
/** General configuration for alarm vibration intensity settings. */
public static final class AlarmVibrationPreferenceConfig extends VibrationPreferenceConfig {
public AlarmVibrationPreferenceConfig(Context context) {
super(context, Settings.System.ALARM_VIBRATION_INTENSITY,
VibrationAttributes.USAGE_ALARM);
}
}
public AlarmVibrationIntensityPreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey, new AlarmVibrationPreferenceConfig(context));
}
protected AlarmVibrationIntensityPreferenceController(Context context, String preferenceKey,
int supportedIntensityLevels) {
super(context, preferenceKey, new AlarmVibrationPreferenceConfig(context),
supportedIntensityLevels);
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
}

View File

@@ -0,0 +1,34 @@
/*
* 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 android.content.Context;
import com.android.settings.accessibility.AlarmVibrationIntensityPreferenceController.AlarmVibrationPreferenceConfig;
/** Preference controller for alarm vibration with only a toggle for on/off states. */
public class AlarmVibrationTogglePreferenceController extends VibrationTogglePreferenceController {
public AlarmVibrationTogglePreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey, new AlarmVibrationPreferenceConfig(context));
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
}

View File

@@ -74,6 +74,12 @@ public class HapticFeedbackIntensityPreferenceController
super(context, preferenceKey, new HapticFeedbackVibrationPreferenceConfig(context)); super(context, preferenceKey, new HapticFeedbackVibrationPreferenceConfig(context));
} }
protected HapticFeedbackIntensityPreferenceController(Context context, String preferenceKey,
int supportedIntensityLevels) {
super(context, preferenceKey, new HapticFeedbackVibrationPreferenceConfig(context),
supportedIntensityLevels);
}
@Override @Override
public int getAvailabilityStatus() { public int getAvailabilityStatus() {
return AVAILABLE; return AVAILABLE;

View File

@@ -0,0 +1,51 @@
/*
* 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 android.content.Context;
import android.os.VibrationAttributes;
import android.provider.Settings;
/** Preference controller for am vibration intensity */
public class MediaVibrationIntensityPreferenceController
extends VibrationIntensityPreferenceController {
/** General configuration for alarm vibration intensity settings. */
public static final class MediaVibrationPreferenceConfig extends VibrationPreferenceConfig {
public MediaVibrationPreferenceConfig(Context context) {
super(context, Settings.System.MEDIA_VIBRATION_INTENSITY,
VibrationAttributes.USAGE_MEDIA);
}
}
public MediaVibrationIntensityPreferenceController(Context context,
String preferenceKey) {
super(context, preferenceKey, new MediaVibrationPreferenceConfig(context));
}
protected MediaVibrationIntensityPreferenceController(Context context, String preferenceKey,
int supportedIntensityLevels) {
super(context, preferenceKey, new MediaVibrationPreferenceConfig(context),
supportedIntensityLevels);
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
}

View File

@@ -0,0 +1,34 @@
/*
* 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 android.content.Context;
import com.android.settings.accessibility.MediaVibrationIntensityPreferenceController.MediaVibrationPreferenceConfig;
/** Preference controller for alarm vibration with only a toggle for on/off states. */
public class MediaVibrationTogglePreferenceController extends VibrationTogglePreferenceController {
public MediaVibrationTogglePreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey, new MediaVibrationPreferenceConfig(context));
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
}

View File

@@ -39,6 +39,12 @@ public class NotificationVibrationIntensityPreferenceController
super(context, preferenceKey, new NotificationVibrationPreferenceConfig(context)); super(context, preferenceKey, new NotificationVibrationPreferenceConfig(context));
} }
protected NotificationVibrationIntensityPreferenceController(Context context,
String preferenceKey, int supportedIntensityLevels) {
super(context, preferenceKey, new NotificationVibrationPreferenceConfig(context),
supportedIntensityLevels);
}
@Override @Override
public int getAvailabilityStatus() { public int getAvailabilityStatus() {
return AVAILABLE; return AVAILABLE;

View File

@@ -71,6 +71,12 @@ public class RingVibrationIntensityPreferenceController
super(context, preferenceKey, new RingVibrationPreferenceConfig(context)); super(context, preferenceKey, new RingVibrationPreferenceConfig(context));
} }
protected RingVibrationIntensityPreferenceController(Context context, String preferenceKey,
int supportedIntensityLevels) {
super(context, preferenceKey, new RingVibrationPreferenceConfig(context),
supportedIntensityLevels);
}
@Override @Override
public int getAvailabilityStatus() { public int getAvailabilityStatus() {
return AVAILABLE; return AVAILABLE;

View File

@@ -21,6 +21,7 @@ import android.os.Vibrator;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.core.SliderPreferenceController; import com.android.settings.core.SliderPreferenceController;
import com.android.settings.widget.SeekBarPreference; import com.android.settings.widget.SeekBarPreference;
import com.android.settingslib.core.lifecycle.LifecycleObserver; import com.android.settingslib.core.lifecycle.LifecycleObserver;
@@ -36,13 +37,22 @@ public abstract class VibrationIntensityPreferenceController extends SliderPrefe
protected final VibrationPreferenceConfig mPreferenceConfig; protected final VibrationPreferenceConfig mPreferenceConfig;
private final VibrationPreferenceConfig.SettingObserver mSettingsContentObserver; private final VibrationPreferenceConfig.SettingObserver mSettingsContentObserver;
private final int mMaxIntensity;
protected VibrationIntensityPreferenceController(Context context, String prefkey, protected VibrationIntensityPreferenceController(Context context, String prefkey,
VibrationPreferenceConfig preferenceConfig) { VibrationPreferenceConfig preferenceConfig) {
this(context, prefkey, preferenceConfig,
context.getResources().getInteger(
R.integer.config_vibration_supported_intensity_levels));
}
protected VibrationIntensityPreferenceController(Context context, String prefkey,
VibrationPreferenceConfig preferenceConfig, int supportedIntensityLevels) {
super(context, prefkey); super(context, prefkey);
mPreferenceConfig = preferenceConfig; mPreferenceConfig = preferenceConfig;
mSettingsContentObserver = new VibrationPreferenceConfig.SettingObserver( mSettingsContentObserver = new VibrationPreferenceConfig.SettingObserver(
preferenceConfig); preferenceConfig);
mMaxIntensity = Math.min(Vibrator.VIBRATION_INTENSITY_HIGH, supportedIntensityLevels);
} }
@Override @Override
@@ -74,7 +84,7 @@ public abstract class VibrationIntensityPreferenceController extends SliderPrefe
@Override @Override
public int getMax() { public int getMax() {
return Vibrator.VIBRATION_INTENSITY_HIGH; return mMaxIntensity;
} }
@Override @Override
@@ -85,7 +95,8 @@ public abstract class VibrationIntensityPreferenceController extends SliderPrefe
@Override @Override
public boolean setSliderPosition(int position) { public boolean setSliderPosition(int position) {
final boolean success = mPreferenceConfig.updateIntensity(position); final int intensity = calculateVibrationIntensity(position);
final boolean success = mPreferenceConfig.updateIntensity(intensity);
if (success && (position != Vibrator.VIBRATION_INTENSITY_OFF)) { if (success && (position != Vibrator.VIBRATION_INTENSITY_OFF)) {
mPreferenceConfig.playVibrationPreview(); mPreferenceConfig.playVibrationPreview();
@@ -93,4 +104,19 @@ public abstract class VibrationIntensityPreferenceController extends SliderPrefe
return success; return success;
} }
private int calculateVibrationIntensity(int position) {
int maxPosition = getMax();
if (position >= maxPosition) {
if (maxPosition == 1) {
// If there is only one intensity available besides OFF, then use the device default
// intensity to ensure no scaling will ever happen in the platform.
return mPreferenceConfig.getDefaultIntensity();
}
// If the settings granularity is lower than the platform's then map the max position to
// the highest vibration intensity, skipping intermediate values in the scale.
return Vibrator.VIBRATION_INTENSITY_HIGH;
}
return position;
}
} }

View File

@@ -21,7 +21,6 @@ import android.database.ContentObserver;
import android.media.AudioManager; import android.media.AudioManager;
import android.net.Uri; import android.net.Uri;
import android.os.Handler; import android.os.Handler;
import android.os.Looper;
import android.os.VibrationAttributes; import android.os.VibrationAttributes;
import android.os.Vibrator; import android.os.Vibrator;
import android.provider.DeviceConfig; import android.provider.DeviceConfig;
@@ -37,8 +36,6 @@ import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnStart; import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.core.lifecycle.events.OnStop; import com.android.settingslib.core.lifecycle.events.OnStop;
import com.google.common.annotations.VisibleForTesting;
/** /**
* Preference controller for the ramping ringer setting key, controlled via {@link AudioManager}. * Preference controller for the ramping ringer setting key, controlled via {@link AudioManager}.
* *
@@ -50,9 +47,15 @@ import com.google.common.annotations.VisibleForTesting;
public class VibrationRampingRingerTogglePreferenceController public class VibrationRampingRingerTogglePreferenceController
extends TogglePreferenceController implements LifecycleObserver, OnStart, OnStop { extends TogglePreferenceController implements LifecycleObserver, OnStart, OnStop {
@VisibleForTesting /** Wrapper around static {@link DeviceConfig} accessor for testing. */
static final String DEVICE_CONFIG_KEY = "ramping_ringer_enabled"; protected static class DeviceConfigProvider {
public boolean isRampingRingerEnabledOnTelephonyConfig() {
return DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_TELEPHONY,
"ramping_ringer_enabled", false);
}
}
private final DeviceConfigProvider mDeviceConfigProvider;
private final ContentObserver mSettingObserver; private final ContentObserver mSettingObserver;
private final Vibrator mVibrator; private final Vibrator mVibrator;
private final AudioManager mAudioManager; private final AudioManager mAudioManager;
@@ -60,10 +63,16 @@ public class VibrationRampingRingerTogglePreferenceController
private Preference mPreference; private Preference mPreference;
public VibrationRampingRingerTogglePreferenceController(Context context, String preferenceKey) { public VibrationRampingRingerTogglePreferenceController(Context context, String preferenceKey) {
this(context, preferenceKey, new DeviceConfigProvider());
}
protected VibrationRampingRingerTogglePreferenceController(Context context,
String preferenceKey, DeviceConfigProvider deviceConfigProvider) {
super(context, preferenceKey); super(context, preferenceKey);
mDeviceConfigProvider = deviceConfigProvider;
mVibrator = context.getSystemService(Vibrator.class); mVibrator = context.getSystemService(Vibrator.class);
mAudioManager = context.getSystemService(AudioManager.class); mAudioManager = context.getSystemService(AudioManager.class);
mSettingObserver = new ContentObserver(new Handler(Looper.getMainLooper())) { mSettingObserver = new ContentObserver(new Handler(/* async= */ true)) {
@Override @Override
public void onChange(boolean selfChange, Uri uri) { public void onChange(boolean selfChange, Uri uri) {
updateState(mPreference); updateState(mPreference);
@@ -74,7 +83,7 @@ public class VibrationRampingRingerTogglePreferenceController
@Override @Override
public int getAvailabilityStatus() { public int getAvailabilityStatus() {
final boolean rampingRingerEnabledOnTelephonyConfig = final boolean rampingRingerEnabledOnTelephonyConfig =
DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_TELEPHONY, DEVICE_CONFIG_KEY, false); mDeviceConfigProvider.isRampingRingerEnabledOnTelephonyConfig();
return (Utils.isVoiceCapable(mContext) && !rampingRingerEnabledOnTelephonyConfig) return (Utils.isVoiceCapable(mContext) && !rampingRingerEnabledOnTelephonyConfig)
? AVAILABLE ? AVAILABLE
: UNSUPPORTED_ON_DEVICE; : UNSUPPORTED_ON_DEVICE;

View File

@@ -41,9 +41,9 @@ public class VibrationSettings extends DashboardFragment {
@Override @Override
protected int getPreferenceScreenResId() { protected int getPreferenceScreenResId() {
final boolean supportsMultipleIntensities = getContext().getResources().getBoolean( final int supportedIntensities = getContext().getResources().getInteger(
R.bool.config_vibration_supports_multiple_intensities); R.integer.config_vibration_supported_intensity_levels);
return supportsMultipleIntensities return supportedIntensities > 1
? R.xml.accessibility_vibration_intensity_settings ? R.xml.accessibility_vibration_intensity_settings
: R.xml.accessibility_vibration_settings; : R.xml.accessibility_vibration_settings;
} }

View File

@@ -0,0 +1,137 @@
/*
* 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.when;
import android.content.Context;
import android.os.VibrationAttributes;
import android.os.Vibrator;
import android.provider.Settings;
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;
@RunWith(RobolectricTestRunner.class)
public class AlarmVibrationIntensityPreferenceControllerTest {
private static final String PREFERENCE_KEY = "preference_key";
@Mock private PreferenceScreen mScreen;
private Lifecycle mLifecycle;
private Context mContext;
private Vibrator mVibrator;
private AlarmVibrationIntensityPreferenceController mController;
private SeekBarPreference mPreference;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mLifecycle = new Lifecycle(() -> mLifecycle);
mContext = ApplicationProvider.getApplicationContext();
mVibrator = mContext.getSystemService(Vibrator.class);
mController = new AlarmVibrationIntensityPreferenceController(mContext, PREFERENCE_KEY,
Vibrator.VIBRATION_INTENSITY_HIGH);
mLifecycle.addObserver(mController);
mPreference = new SeekBarPreference(mContext);
mPreference.setSummary("Test summary");
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
mController.displayPreference(mScreen);
}
@Test
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 missingSetting_shouldReturnDefault() {
Settings.System.putString(mContext.getContentResolver(),
Settings.System.ALARM_VIBRATION_INTENSITY, /* value= */ null);
mController.updateState(mPreference);
assertThat(mPreference.getProgress()).isEqualTo(
mVibrator.getDefaultVibrationIntensity(VibrationAttributes.USAGE_ALARM));
}
@Test
public void updateState_shouldDisplayIntensityInSliderPosition() {
updateSetting(Settings.System.ALARM_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_HIGH);
mController.updateState(mPreference);
assertThat(mPreference.getProgress()).isEqualTo(Vibrator.VIBRATION_INTENSITY_HIGH);
updateSetting(Settings.System.ALARM_VIBRATION_INTENSITY,
Vibrator.VIBRATION_INTENSITY_MEDIUM);
mController.updateState(mPreference);
assertThat(mPreference.getProgress()).isEqualTo(Vibrator.VIBRATION_INTENSITY_MEDIUM);
updateSetting(Settings.System.ALARM_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_LOW);
mController.updateState(mPreference);
assertThat(mPreference.getProgress()).isEqualTo(Vibrator.VIBRATION_INTENSITY_LOW);
updateSetting(Settings.System.ALARM_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_OFF);
mController.updateState(mPreference);
assertThat(mPreference.getProgress()).isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF);
}
@Test
public void setProgress_updatesIntensitySetting() throws Exception {
mController.setSliderPosition(Vibrator.VIBRATION_INTENSITY_OFF);
assertThat(readSetting(Settings.System.ALARM_VIBRATION_INTENSITY))
.isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF);
mController.setSliderPosition(Vibrator.VIBRATION_INTENSITY_LOW);
assertThat(readSetting(Settings.System.ALARM_VIBRATION_INTENSITY))
.isEqualTo(Vibrator.VIBRATION_INTENSITY_LOW);
mController.setSliderPosition(Vibrator.VIBRATION_INTENSITY_MEDIUM);
assertThat(readSetting(Settings.System.ALARM_VIBRATION_INTENSITY))
.isEqualTo(Vibrator.VIBRATION_INTENSITY_MEDIUM);
mController.setSliderPosition(Vibrator.VIBRATION_INTENSITY_HIGH);
assertThat(readSetting(Settings.System.ALARM_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);
}
}

View File

@@ -0,0 +1,128 @@
/*
* 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.when;
import android.content.Context;
import android.os.VibrationAttributes;
import android.os.Vibrator;
import android.provider.Settings;
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 AlarmVibrationTogglePreferenceControllerTest {
private static final String PREFERENCE_KEY = "preference_key";
@Mock private PreferenceScreen mScreen;
private Lifecycle mLifecycle;
private Context mContext;
private Vibrator mVibrator;
private AlarmVibrationTogglePreferenceController mController;
private SwitchPreference mPreference;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mLifecycle = new Lifecycle(() -> mLifecycle);
mContext = ApplicationProvider.getApplicationContext();
mVibrator = mContext.getSystemService(Vibrator.class);
mController = new AlarmVibrationTogglePreferenceController(mContext, PREFERENCE_KEY);
mLifecycle.addObserver(mController);
mPreference = new SwitchPreference(mContext);
mPreference.setSummary("Test summary");
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
mController.displayPreference(mScreen);
}
@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.ALARM_VIBRATION_INTENSITY, /* value= */ null);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isTrue();
}
@Test
public void updateState_shouldDisplayOnOffState() {
updateSetting(Settings.System.ALARM_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_HIGH);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isTrue();
updateSetting(Settings.System.ALARM_VIBRATION_INTENSITY,
Vibrator.VIBRATION_INTENSITY_MEDIUM);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isTrue();
updateSetting(Settings.System.ALARM_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_LOW);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isTrue();
updateSetting(Settings.System.ALARM_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_OFF);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isFalse();
}
@Test
public void setChecked_updatesIntensityAndDependentSettings() throws Exception {
updateSetting(Settings.System.ALARM_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_OFF);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isFalse();
mController.setChecked(true);
assertThat(readSetting(Settings.System.ALARM_VIBRATION_INTENSITY)).isEqualTo(
mVibrator.getDefaultVibrationIntensity(VibrationAttributes.USAGE_ALARM));
mController.setChecked(false);
assertThat(readSetting(Settings.System.ALARM_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);
}
}

View File

@@ -18,7 +18,6 @@ package com.android.settings.accessibility;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import android.content.Context; import android.content.Context;
@@ -26,7 +25,6 @@ import android.os.VibrationAttributes;
import android.os.Vibrator; import android.os.Vibrator;
import android.provider.Settings; import android.provider.Settings;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider; import androidx.test.core.app.ApplicationProvider;
@@ -48,10 +46,8 @@ public class HapticFeedbackIntensityPreferenceControllerTest {
private static final int OFF = 0; private static final int OFF = 0;
private static final int ON = 1; private static final int ON = 1;
@Mock @Mock private PreferenceScreen mScreen;
private PreferenceScreen mScreen;
private LifecycleOwner mLifecycleOwner;
private Lifecycle mLifecycle; private Lifecycle mLifecycle;
private Context mContext; private Context mContext;
private Vibrator mVibrator; private Vibrator mVibrator;
@@ -61,16 +57,16 @@ public class HapticFeedbackIntensityPreferenceControllerTest {
@Before @Before
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
mLifecycleOwner = () -> mLifecycle; mLifecycle = new Lifecycle(() -> mLifecycle);
mLifecycle = new Lifecycle(mLifecycleOwner); mContext = ApplicationProvider.getApplicationContext();
mContext = spy(ApplicationProvider.getApplicationContext());
mVibrator = mContext.getSystemService(Vibrator.class); mVibrator = mContext.getSystemService(Vibrator.class);
mController = new HapticFeedbackIntensityPreferenceController(mContext, PREFERENCE_KEY); mController = new HapticFeedbackIntensityPreferenceController(mContext, PREFERENCE_KEY,
Vibrator.VIBRATION_INTENSITY_HIGH);
mLifecycle.addObserver(mController); mLifecycle.addObserver(mController);
mPreference = new SeekBarPreference(mContext); mPreference = new SeekBarPreference(mContext);
mPreference.setSummary("Test summary"); mPreference.setSummary("Test summary");
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
showPreference(); mController.displayPreference(mScreen);
} }
@Test @Test
@@ -113,27 +109,27 @@ public class HapticFeedbackIntensityPreferenceControllerTest {
@Test @Test
public void setProgress_updatesIntensityAndDependentSettings() throws Exception { public void setProgress_updatesIntensityAndDependentSettings() throws Exception {
mPreference.setProgress(Vibrator.VIBRATION_INTENSITY_OFF); mController.setSliderPosition(Vibrator.VIBRATION_INTENSITY_OFF);
assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_INTENSITY)) assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_INTENSITY))
.isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF); .isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF);
assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_ENABLED)).isEqualTo(OFF); assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_ENABLED)).isEqualTo(OFF);
mPreference.setProgress(Vibrator.VIBRATION_INTENSITY_LOW); mController.setSliderPosition(Vibrator.VIBRATION_INTENSITY_LOW);
assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_INTENSITY)) assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_INTENSITY))
.isEqualTo(Vibrator.VIBRATION_INTENSITY_LOW); .isEqualTo(Vibrator.VIBRATION_INTENSITY_LOW);
assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_ENABLED)).isEqualTo(ON); assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_ENABLED)).isEqualTo(ON);
mPreference.setProgress(Vibrator.VIBRATION_INTENSITY_MEDIUM); mController.setSliderPosition(Vibrator.VIBRATION_INTENSITY_MEDIUM);
assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_INTENSITY)) assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_INTENSITY))
.isEqualTo(Vibrator.VIBRATION_INTENSITY_MEDIUM); .isEqualTo(Vibrator.VIBRATION_INTENSITY_MEDIUM);
assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_ENABLED)).isEqualTo(ON); assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_ENABLED)).isEqualTo(ON);
mPreference.setProgress(Vibrator.VIBRATION_INTENSITY_HIGH); mController.setSliderPosition(Vibrator.VIBRATION_INTENSITY_HIGH);
assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_INTENSITY)) assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_INTENSITY))
.isEqualTo(Vibrator.VIBRATION_INTENSITY_HIGH); .isEqualTo(Vibrator.VIBRATION_INTENSITY_HIGH);
assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_ENABLED)).isEqualTo(ON); assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_ENABLED)).isEqualTo(ON);
mPreference.setProgress(Vibrator.VIBRATION_INTENSITY_OFF); mController.setSliderPosition(Vibrator.VIBRATION_INTENSITY_OFF);
assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_INTENSITY)) assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_INTENSITY))
.isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF); .isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF);
assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_ENABLED)).isEqualTo(OFF); assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_ENABLED)).isEqualTo(OFF);
@@ -146,8 +142,4 @@ public class HapticFeedbackIntensityPreferenceControllerTest {
private int readSetting(String settingKey) throws Settings.SettingNotFoundException { private int readSetting(String settingKey) throws Settings.SettingNotFoundException {
return Settings.System.getInt(mContext.getContentResolver(), settingKey); return Settings.System.getInt(mContext.getContentResolver(), settingKey);
} }
private void showPreference() {
mController.displayPreference(mScreen);
}
} }

View File

@@ -18,7 +18,6 @@ package com.android.settings.accessibility;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import android.content.Context; import android.content.Context;
@@ -26,7 +25,6 @@ import android.os.VibrationAttributes;
import android.os.Vibrator; import android.os.Vibrator;
import android.provider.Settings; import android.provider.Settings;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference; import androidx.preference.SwitchPreference;
import androidx.test.core.app.ApplicationProvider; import androidx.test.core.app.ApplicationProvider;
@@ -48,10 +46,8 @@ public class HapticFeedbackTogglePreferenceControllerTest {
private static final int OFF = 0; private static final int OFF = 0;
private static final int ON = 1; private static final int ON = 1;
@Mock @Mock private PreferenceScreen mScreen;
private PreferenceScreen mScreen;
private LifecycleOwner mLifecycleOwner;
private Lifecycle mLifecycle; private Lifecycle mLifecycle;
private Context mContext; private Context mContext;
private Vibrator mVibrator; private Vibrator mVibrator;
@@ -61,16 +57,15 @@ public class HapticFeedbackTogglePreferenceControllerTest {
@Before @Before
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
mLifecycleOwner = () -> mLifecycle; mLifecycle = new Lifecycle(() -> mLifecycle);
mLifecycle = new Lifecycle(mLifecycleOwner); mContext = ApplicationProvider.getApplicationContext();
mContext = spy(ApplicationProvider.getApplicationContext());
mVibrator = mContext.getSystemService(Vibrator.class); mVibrator = mContext.getSystemService(Vibrator.class);
mController = new HapticFeedbackTogglePreferenceController(mContext, PREFERENCE_KEY); mController = new HapticFeedbackTogglePreferenceController(mContext, PREFERENCE_KEY);
mLifecycle.addObserver(mController); mLifecycle.addObserver(mController);
mPreference = new SwitchPreference(mContext); mPreference = new SwitchPreference(mContext);
mPreference.setSummary("Test summary"); mPreference.setSummary("Test summary");
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
showPreference(); mController.displayPreference(mScreen);
} }
@Test @Test
@@ -114,12 +109,12 @@ public class HapticFeedbackTogglePreferenceControllerTest {
mController.updateState(mPreference); mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isFalse(); assertThat(mPreference.isChecked()).isFalse();
mPreference.setChecked(true); mController.setChecked(true);
assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_INTENSITY)) assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_INTENSITY))
.isEqualTo(mVibrator.getDefaultVibrationIntensity(VibrationAttributes.USAGE_TOUCH)); .isEqualTo(mVibrator.getDefaultVibrationIntensity(VibrationAttributes.USAGE_TOUCH));
assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_ENABLED)).isEqualTo(ON); assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_ENABLED)).isEqualTo(ON);
mPreference.setChecked(false); mController.setChecked(false);
assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_INTENSITY)) assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_INTENSITY))
.isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF); .isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF);
assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_ENABLED)).isEqualTo(OFF); assertThat(readSetting(Settings.System.HAPTIC_FEEDBACK_ENABLED)).isEqualTo(OFF);
@@ -132,8 +127,4 @@ public class HapticFeedbackTogglePreferenceControllerTest {
private int readSetting(String settingKey) throws Settings.SettingNotFoundException { private int readSetting(String settingKey) throws Settings.SettingNotFoundException {
return Settings.System.getInt(mContext.getContentResolver(), settingKey); return Settings.System.getInt(mContext.getContentResolver(), settingKey);
} }
private void showPreference() {
mController.displayPreference(mScreen);
}
} }

View File

@@ -0,0 +1,138 @@
/*
* 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.when;
import android.content.Context;
import android.os.VibrationAttributes;
import android.os.Vibrator;
import android.provider.Settings;
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;
/** Test for {@link MediaVibrationIntensityPreferenceController}. */
@RunWith(RobolectricTestRunner.class)
public class MediaVibrationIntensityPreferenceControllerTest {
private static final String PREFERENCE_KEY = "preference_key";
@Mock private PreferenceScreen mScreen;
private Lifecycle mLifecycle;
private Context mContext;
private Vibrator mVibrator;
private MediaVibrationIntensityPreferenceController mController;
private SeekBarPreference mPreference;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mLifecycle = new Lifecycle(() -> mLifecycle);
mContext = ApplicationProvider.getApplicationContext();
mVibrator = mContext.getSystemService(Vibrator.class);
mController = new MediaVibrationIntensityPreferenceController(mContext, PREFERENCE_KEY,
Vibrator.VIBRATION_INTENSITY_HIGH);
mLifecycle.addObserver(mController);
mPreference = new SeekBarPreference(mContext);
mPreference.setSummary("Test summary");
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
mController.displayPreference(mScreen);
}
@Test
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 missingSetting_shouldReturnDefault() {
Settings.System.putString(mContext.getContentResolver(),
Settings.System.MEDIA_VIBRATION_INTENSITY, /* value= */ null);
mController.updateState(mPreference);
assertThat(mPreference.getProgress()).isEqualTo(
mVibrator.getDefaultVibrationIntensity(VibrationAttributes.USAGE_MEDIA));
}
@Test
public void updateState_shouldDisplayIntensityInSliderPosition() {
updateSetting(Settings.System.MEDIA_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_HIGH);
mController.updateState(mPreference);
assertThat(mPreference.getProgress()).isEqualTo(Vibrator.VIBRATION_INTENSITY_HIGH);
updateSetting(Settings.System.MEDIA_VIBRATION_INTENSITY,
Vibrator.VIBRATION_INTENSITY_MEDIUM);
mController.updateState(mPreference);
assertThat(mPreference.getProgress()).isEqualTo(Vibrator.VIBRATION_INTENSITY_MEDIUM);
updateSetting(Settings.System.MEDIA_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_LOW);
mController.updateState(mPreference);
assertThat(mPreference.getProgress()).isEqualTo(Vibrator.VIBRATION_INTENSITY_LOW);
updateSetting(Settings.System.MEDIA_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_OFF);
mController.updateState(mPreference);
assertThat(mPreference.getProgress()).isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF);
}
@Test
public void setProgress_updatesIntensitySetting() throws Exception {
mController.setSliderPosition(Vibrator.VIBRATION_INTENSITY_OFF);
assertThat(readSetting(Settings.System.MEDIA_VIBRATION_INTENSITY))
.isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF);
mController.setSliderPosition(Vibrator.VIBRATION_INTENSITY_LOW);
assertThat(readSetting(Settings.System.MEDIA_VIBRATION_INTENSITY))
.isEqualTo(Vibrator.VIBRATION_INTENSITY_LOW);
mController.setSliderPosition(Vibrator.VIBRATION_INTENSITY_MEDIUM);
assertThat(readSetting(Settings.System.MEDIA_VIBRATION_INTENSITY))
.isEqualTo(Vibrator.VIBRATION_INTENSITY_MEDIUM);
mController.setSliderPosition(Vibrator.VIBRATION_INTENSITY_HIGH);
assertThat(readSetting(Settings.System.MEDIA_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);
}
}

View File

@@ -0,0 +1,129 @@
/*
* 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.when;
import android.content.Context;
import android.os.VibrationAttributes;
import android.os.Vibrator;
import android.provider.Settings;
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;
/** Test for {@link MediaVibrationIntensityPreferenceController}. */
@RunWith(RobolectricTestRunner.class)
public class MediaVibrationTogglePreferenceControllerTest {
private static final String PREFERENCE_KEY = "preference_key";
@Mock private PreferenceScreen mScreen;
private Lifecycle mLifecycle;
private Context mContext;
private Vibrator mVibrator;
private MediaVibrationTogglePreferenceController mController;
private SwitchPreference mPreference;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mLifecycle = new Lifecycle(() -> mLifecycle);
mContext = ApplicationProvider.getApplicationContext();
mVibrator = mContext.getSystemService(Vibrator.class);
mController = new MediaVibrationTogglePreferenceController(mContext, PREFERENCE_KEY);
mLifecycle.addObserver(mController);
mPreference = new SwitchPreference(mContext);
mPreference.setSummary("Test summary");
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
mController.displayPreference(mScreen);
}
@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.MEDIA_VIBRATION_INTENSITY, /* value= */ null);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isTrue();
}
@Test
public void updateState_shouldDisplayOnOffState() {
updateSetting(Settings.System.MEDIA_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_HIGH);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isTrue();
updateSetting(Settings.System.MEDIA_VIBRATION_INTENSITY,
Vibrator.VIBRATION_INTENSITY_MEDIUM);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isTrue();
updateSetting(Settings.System.MEDIA_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_LOW);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isTrue();
updateSetting(Settings.System.MEDIA_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_OFF);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isFalse();
}
@Test
public void setChecked_updatesIntensityAndDependentSettings() throws Exception {
updateSetting(Settings.System.MEDIA_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_OFF);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isFalse();
mController.setChecked(true);
assertThat(readSetting(Settings.System.MEDIA_VIBRATION_INTENSITY)).isEqualTo(
mVibrator.getDefaultVibrationIntensity(VibrationAttributes.USAGE_MEDIA));
mController.setChecked(false);
assertThat(readSetting(Settings.System.MEDIA_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);
}
}

View File

@@ -18,7 +18,6 @@ package com.android.settings.accessibility;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import android.content.Context; import android.content.Context;
@@ -26,7 +25,6 @@ import android.os.VibrationAttributes;
import android.os.Vibrator; import android.os.Vibrator;
import android.provider.Settings; import android.provider.Settings;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider; import androidx.test.core.app.ApplicationProvider;
@@ -46,10 +44,8 @@ public class NotificationVibrationIntensityPreferenceControllerTest {
private static final String PREFERENCE_KEY = "preference_key"; private static final String PREFERENCE_KEY = "preference_key";
@Mock @Mock private PreferenceScreen mScreen;
private PreferenceScreen mScreen;
private LifecycleOwner mLifecycleOwner;
private Lifecycle mLifecycle; private Lifecycle mLifecycle;
private Context mContext; private Context mContext;
private Vibrator mVibrator; private Vibrator mVibrator;
@@ -59,17 +55,16 @@ public class NotificationVibrationIntensityPreferenceControllerTest {
@Before @Before
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
mLifecycleOwner = () -> mLifecycle; mLifecycle = new Lifecycle(() -> mLifecycle);
mLifecycle = new Lifecycle(mLifecycleOwner); mContext = ApplicationProvider.getApplicationContext();
mContext = spy(ApplicationProvider.getApplicationContext());
mVibrator = mContext.getSystemService(Vibrator.class); mVibrator = mContext.getSystemService(Vibrator.class);
mController = new NotificationVibrationIntensityPreferenceController(mContext, mController = new NotificationVibrationIntensityPreferenceController(mContext,
PREFERENCE_KEY); PREFERENCE_KEY, Vibrator.VIBRATION_INTENSITY_HIGH);
mLifecycle.addObserver(mController); mLifecycle.addObserver(mController);
mPreference = new SeekBarPreference(mContext); mPreference = new SeekBarPreference(mContext);
mPreference.setSummary("Test summary"); mPreference.setSummary("Test summary");
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
showPreference(); mController.displayPreference(mScreen);
} }
@Test @Test
@@ -116,19 +111,19 @@ public class NotificationVibrationIntensityPreferenceControllerTest {
@Test @Test
public void setProgress_updatesIntensitySetting() throws Exception { public void setProgress_updatesIntensitySetting() throws Exception {
mPreference.setProgress(Vibrator.VIBRATION_INTENSITY_OFF); mController.setSliderPosition(Vibrator.VIBRATION_INTENSITY_OFF);
assertThat(readSetting(Settings.System.NOTIFICATION_VIBRATION_INTENSITY)) assertThat(readSetting(Settings.System.NOTIFICATION_VIBRATION_INTENSITY))
.isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF); .isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF);
mPreference.setProgress(Vibrator.VIBRATION_INTENSITY_LOW); mController.setSliderPosition(Vibrator.VIBRATION_INTENSITY_LOW);
assertThat(readSetting(Settings.System.NOTIFICATION_VIBRATION_INTENSITY)) assertThat(readSetting(Settings.System.NOTIFICATION_VIBRATION_INTENSITY))
.isEqualTo(Vibrator.VIBRATION_INTENSITY_LOW); .isEqualTo(Vibrator.VIBRATION_INTENSITY_LOW);
mPreference.setProgress(Vibrator.VIBRATION_INTENSITY_MEDIUM); mController.setSliderPosition(Vibrator.VIBRATION_INTENSITY_MEDIUM);
assertThat(readSetting(Settings.System.NOTIFICATION_VIBRATION_INTENSITY)) assertThat(readSetting(Settings.System.NOTIFICATION_VIBRATION_INTENSITY))
.isEqualTo(Vibrator.VIBRATION_INTENSITY_MEDIUM); .isEqualTo(Vibrator.VIBRATION_INTENSITY_MEDIUM);
mPreference.setProgress(Vibrator.VIBRATION_INTENSITY_HIGH); mController.setSliderPosition(Vibrator.VIBRATION_INTENSITY_HIGH);
assertThat(readSetting(Settings.System.NOTIFICATION_VIBRATION_INTENSITY)) assertThat(readSetting(Settings.System.NOTIFICATION_VIBRATION_INTENSITY))
.isEqualTo(Vibrator.VIBRATION_INTENSITY_HIGH); .isEqualTo(Vibrator.VIBRATION_INTENSITY_HIGH);
} }
@@ -140,8 +135,4 @@ public class NotificationVibrationIntensityPreferenceControllerTest {
private int readSetting(String settingKey) throws Settings.SettingNotFoundException { private int readSetting(String settingKey) throws Settings.SettingNotFoundException {
return Settings.System.getInt(mContext.getContentResolver(), settingKey); return Settings.System.getInt(mContext.getContentResolver(), settingKey);
} }
private void showPreference() {
mController.displayPreference(mScreen);
}
} }

View File

@@ -18,7 +18,6 @@ package com.android.settings.accessibility;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import android.content.Context; import android.content.Context;
@@ -26,7 +25,6 @@ import android.os.VibrationAttributes;
import android.os.Vibrator; import android.os.Vibrator;
import android.provider.Settings; import android.provider.Settings;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference; import androidx.preference.SwitchPreference;
import androidx.test.core.app.ApplicationProvider; import androidx.test.core.app.ApplicationProvider;
@@ -46,10 +44,8 @@ public class NotificationVibrationTogglePreferenceControllerTest {
private static final String PREFERENCE_KEY = "preference_key"; private static final String PREFERENCE_KEY = "preference_key";
@Mock @Mock private PreferenceScreen mScreen;
private PreferenceScreen mScreen;
private LifecycleOwner mLifecycleOwner;
private Lifecycle mLifecycle; private Lifecycle mLifecycle;
private Context mContext; private Context mContext;
private Vibrator mVibrator; private Vibrator mVibrator;
@@ -59,16 +55,15 @@ public class NotificationVibrationTogglePreferenceControllerTest {
@Before @Before
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
mLifecycleOwner = () -> mLifecycle; mLifecycle = new Lifecycle(() -> mLifecycle);
mLifecycle = new Lifecycle(mLifecycleOwner); mContext = ApplicationProvider.getApplicationContext();
mContext = spy(ApplicationProvider.getApplicationContext());
mVibrator = mContext.getSystemService(Vibrator.class); mVibrator = mContext.getSystemService(Vibrator.class);
mController = new NotificationVibrationTogglePreferenceController(mContext, PREFERENCE_KEY); mController = new NotificationVibrationTogglePreferenceController(mContext, PREFERENCE_KEY);
mLifecycle.addObserver(mController); mLifecycle.addObserver(mController);
mPreference = new SwitchPreference(mContext); mPreference = new SwitchPreference(mContext);
mPreference.setSummary("Test summary"); mPreference.setSummary("Test summary");
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
showPreference(); mController.displayPreference(mScreen);
} }
@Test @Test
@@ -116,11 +111,11 @@ public class NotificationVibrationTogglePreferenceControllerTest {
mController.updateState(mPreference); mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isFalse(); assertThat(mPreference.isChecked()).isFalse();
mPreference.setChecked(true); mController.setChecked(true);
assertThat(readSetting(Settings.System.NOTIFICATION_VIBRATION_INTENSITY)).isEqualTo( assertThat(readSetting(Settings.System.NOTIFICATION_VIBRATION_INTENSITY)).isEqualTo(
mVibrator.getDefaultVibrationIntensity(VibrationAttributes.USAGE_NOTIFICATION)); mVibrator.getDefaultVibrationIntensity(VibrationAttributes.USAGE_NOTIFICATION));
mPreference.setChecked(false); mController.setChecked(false);
assertThat(readSetting(Settings.System.NOTIFICATION_VIBRATION_INTENSITY)) assertThat(readSetting(Settings.System.NOTIFICATION_VIBRATION_INTENSITY))
.isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF); .isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF);
} }
@@ -132,8 +127,4 @@ public class NotificationVibrationTogglePreferenceControllerTest {
private int readSetting(String settingKey) throws Settings.SettingNotFoundException { private int readSetting(String settingKey) throws Settings.SettingNotFoundException {
return Settings.System.getInt(mContext.getContentResolver(), settingKey); return Settings.System.getInt(mContext.getContentResolver(), settingKey);
} }
private void showPreference() {
mController.displayPreference(mScreen);
}
} }

View File

@@ -18,7 +18,6 @@ package com.android.settings.accessibility;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import android.content.Context; import android.content.Context;
@@ -26,7 +25,6 @@ import android.os.VibrationAttributes;
import android.os.Vibrator; import android.os.Vibrator;
import android.provider.Settings; import android.provider.Settings;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider; import androidx.test.core.app.ApplicationProvider;
@@ -49,10 +47,8 @@ public class RingVibrationIntensityPreferenceControllerTest {
private static final int OFF = 0; private static final int OFF = 0;
private static final int ON = 1; private static final int ON = 1;
@Mock @Mock private PreferenceScreen mScreen;
private PreferenceScreen mScreen;
private LifecycleOwner mLifecycleOwner;
private Lifecycle mLifecycle; private Lifecycle mLifecycle;
private Context mContext; private Context mContext;
private Vibrator mVibrator; private Vibrator mVibrator;
@@ -62,16 +58,16 @@ public class RingVibrationIntensityPreferenceControllerTest {
@Before @Before
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
mLifecycleOwner = () -> mLifecycle; mLifecycle = new Lifecycle(() -> mLifecycle);
mLifecycle = new Lifecycle(mLifecycleOwner); mContext = ApplicationProvider.getApplicationContext();
mContext = spy(ApplicationProvider.getApplicationContext());
mVibrator = mContext.getSystemService(Vibrator.class); mVibrator = mContext.getSystemService(Vibrator.class);
mController = new RingVibrationIntensityPreferenceController(mContext, PREFERENCE_KEY); mController = new RingVibrationIntensityPreferenceController(mContext, PREFERENCE_KEY,
Vibrator.VIBRATION_INTENSITY_HIGH);
mLifecycle.addObserver(mController); mLifecycle.addObserver(mController);
mPreference = new SeekBarPreference(mContext); mPreference = new SeekBarPreference(mContext);
mPreference.setSummary("Test summary"); mPreference.setSummary("Test summary");
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
showPreference(); mController.displayPreference(mScreen);
} }
@Test @Test
@@ -115,27 +111,27 @@ public class RingVibrationIntensityPreferenceControllerTest {
@Test @Test
public void setProgress_updatesIntensityAndDependentSettings() throws Exception { public void setProgress_updatesIntensityAndDependentSettings() throws Exception {
mPreference.setProgress(Vibrator.VIBRATION_INTENSITY_OFF); mController.setSliderPosition(Vibrator.VIBRATION_INTENSITY_OFF);
assertThat(readSetting(Settings.System.RING_VIBRATION_INTENSITY)) assertThat(readSetting(Settings.System.RING_VIBRATION_INTENSITY))
.isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF); .isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF);
assertThat(readSetting(Settings.System.VIBRATE_WHEN_RINGING)).isEqualTo(OFF); assertThat(readSetting(Settings.System.VIBRATE_WHEN_RINGING)).isEqualTo(OFF);
mPreference.setProgress(Vibrator.VIBRATION_INTENSITY_LOW); mController.setSliderPosition(Vibrator.VIBRATION_INTENSITY_LOW);
assertThat(readSetting(Settings.System.RING_VIBRATION_INTENSITY)) assertThat(readSetting(Settings.System.RING_VIBRATION_INTENSITY))
.isEqualTo(Vibrator.VIBRATION_INTENSITY_LOW); .isEqualTo(Vibrator.VIBRATION_INTENSITY_LOW);
assertThat(readSetting(Settings.System.VIBRATE_WHEN_RINGING)).isEqualTo(ON); assertThat(readSetting(Settings.System.VIBRATE_WHEN_RINGING)).isEqualTo(ON);
mPreference.setProgress(Vibrator.VIBRATION_INTENSITY_MEDIUM); mController.setSliderPosition(Vibrator.VIBRATION_INTENSITY_MEDIUM);
assertThat(readSetting(Settings.System.RING_VIBRATION_INTENSITY)) assertThat(readSetting(Settings.System.RING_VIBRATION_INTENSITY))
.isEqualTo(Vibrator.VIBRATION_INTENSITY_MEDIUM); .isEqualTo(Vibrator.VIBRATION_INTENSITY_MEDIUM);
assertThat(readSetting(Settings.System.VIBRATE_WHEN_RINGING)).isEqualTo(ON); assertThat(readSetting(Settings.System.VIBRATE_WHEN_RINGING)).isEqualTo(ON);
mPreference.setProgress(Vibrator.VIBRATION_INTENSITY_HIGH); mController.setSliderPosition(Vibrator.VIBRATION_INTENSITY_HIGH);
assertThat(readSetting(Settings.System.RING_VIBRATION_INTENSITY)) assertThat(readSetting(Settings.System.RING_VIBRATION_INTENSITY))
.isEqualTo(Vibrator.VIBRATION_INTENSITY_HIGH); .isEqualTo(Vibrator.VIBRATION_INTENSITY_HIGH);
assertThat(readSetting(Settings.System.VIBRATE_WHEN_RINGING)).isEqualTo(ON); assertThat(readSetting(Settings.System.VIBRATE_WHEN_RINGING)).isEqualTo(ON);
mPreference.setProgress(Vibrator.VIBRATION_INTENSITY_OFF); mController.setSliderPosition(Vibrator.VIBRATION_INTENSITY_OFF);
assertThat(readSetting(Settings.System.RING_VIBRATION_INTENSITY)) assertThat(readSetting(Settings.System.RING_VIBRATION_INTENSITY))
.isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF); .isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF);
assertThat(readSetting(Settings.System.VIBRATE_WHEN_RINGING)).isEqualTo(OFF); assertThat(readSetting(Settings.System.VIBRATE_WHEN_RINGING)).isEqualTo(OFF);
@@ -148,8 +144,4 @@ public class RingVibrationIntensityPreferenceControllerTest {
private int readSetting(String settingKey) throws Settings.SettingNotFoundException { private int readSetting(String settingKey) throws Settings.SettingNotFoundException {
return Settings.System.getInt(mContext.getContentResolver(), settingKey); return Settings.System.getInt(mContext.getContentResolver(), settingKey);
} }
private void showPreference() {
mController.displayPreference(mScreen);
}
} }

View File

@@ -18,7 +18,6 @@ package com.android.settings.accessibility;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import android.content.Context; import android.content.Context;
@@ -26,7 +25,6 @@ import android.os.VibrationAttributes;
import android.os.Vibrator; import android.os.Vibrator;
import android.provider.Settings; import android.provider.Settings;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference; import androidx.preference.SwitchPreference;
import androidx.test.core.app.ApplicationProvider; import androidx.test.core.app.ApplicationProvider;
@@ -48,10 +46,8 @@ public class RingVibrationTogglePreferenceControllerTest {
private static final int OFF = 0; private static final int OFF = 0;
private static final int ON = 1; private static final int ON = 1;
@Mock @Mock private PreferenceScreen mScreen;
private PreferenceScreen mScreen;
private LifecycleOwner mLifecycleOwner;
private Lifecycle mLifecycle; private Lifecycle mLifecycle;
private Context mContext; private Context mContext;
private Vibrator mVibrator; private Vibrator mVibrator;
@@ -61,16 +57,15 @@ public class RingVibrationTogglePreferenceControllerTest {
@Before @Before
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
mLifecycleOwner = () -> mLifecycle; mLifecycle = new Lifecycle(() -> mLifecycle);
mLifecycle = new Lifecycle(mLifecycleOwner); mContext = ApplicationProvider.getApplicationContext();
mContext = spy(ApplicationProvider.getApplicationContext());
mVibrator = mContext.getSystemService(Vibrator.class); mVibrator = mContext.getSystemService(Vibrator.class);
mController = new RingVibrationTogglePreferenceController(mContext, PREFERENCE_KEY); mController = new RingVibrationTogglePreferenceController(mContext, PREFERENCE_KEY);
mLifecycle.addObserver(mController); mLifecycle.addObserver(mController);
mPreference = new SwitchPreference(mContext); mPreference = new SwitchPreference(mContext);
mPreference.setSummary("Test summary"); mPreference.setSummary("Test summary");
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
showPreference(); mController.displayPreference(mScreen);
} }
@Test @Test
@@ -114,12 +109,12 @@ public class RingVibrationTogglePreferenceControllerTest {
mController.updateState(mPreference); mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isFalse(); assertThat(mPreference.isChecked()).isFalse();
mPreference.setChecked(true); mController.setChecked(true);
assertThat(readSetting(Settings.System.RING_VIBRATION_INTENSITY)).isEqualTo( assertThat(readSetting(Settings.System.RING_VIBRATION_INTENSITY)).isEqualTo(
mVibrator.getDefaultVibrationIntensity(VibrationAttributes.USAGE_RINGTONE)); mVibrator.getDefaultVibrationIntensity(VibrationAttributes.USAGE_RINGTONE));
assertThat(readSetting(Settings.System.VIBRATE_WHEN_RINGING)).isEqualTo(ON); assertThat(readSetting(Settings.System.VIBRATE_WHEN_RINGING)).isEqualTo(ON);
mPreference.setChecked(false); mController.setChecked(false);
assertThat(readSetting(Settings.System.RING_VIBRATION_INTENSITY)) assertThat(readSetting(Settings.System.RING_VIBRATION_INTENSITY))
.isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF); .isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF);
assertThat(readSetting(Settings.System.VIBRATE_WHEN_RINGING)).isEqualTo(OFF); assertThat(readSetting(Settings.System.VIBRATE_WHEN_RINGING)).isEqualTo(OFF);
@@ -132,8 +127,4 @@ public class RingVibrationTogglePreferenceControllerTest {
private int readSetting(String settingKey) throws Settings.SettingNotFoundException { private int readSetting(String settingKey) throws Settings.SettingNotFoundException {
return Settings.System.getInt(mContext.getContentResolver(), settingKey); return Settings.System.getInt(mContext.getContentResolver(), settingKey);
} }
private void showPreference() {
mController.displayPreference(mScreen);
}
} }

View File

@@ -0,0 +1,221 @@
/*
* 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.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.os.VibrationAttributes;
import android.os.Vibrator;
import android.provider.Settings;
import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
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;
@RunWith(RobolectricTestRunner.class)
public class VibrationIntensityPreferenceControllerTest {
private static final String SETTING_KEY = Settings.System.NOTIFICATION_VIBRATION_INTENSITY;
private static final int VIBRATION_USAGE = VibrationAttributes.USAGE_NOTIFICATION;
/** Basic implementation of preference controller to test generic behavior. */
private static class TestPreferenceController extends VibrationIntensityPreferenceController {
TestPreferenceController(Context context, int supportedIntensityLevels) {
super(context, "preference_key",
new VibrationPreferenceConfig(context, SETTING_KEY, VIBRATION_USAGE) {},
supportedIntensityLevels);
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
}
@Mock private PreferenceScreen mScreen;
private Lifecycle mLifecycle;
private Context mContext;
private Vibrator mVibrator;
private SeekBarPreference mPreference;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mLifecycle = new Lifecycle(() -> mLifecycle);
mContext = ApplicationProvider.getApplicationContext();
mVibrator = mContext.getSystemService(Vibrator.class);
}
@Test
public void missingSetting_shouldReturnDefault() {
VibrationIntensityPreferenceController controller = createPreferenceController(3);
Settings.System.putString(mContext.getContentResolver(), SETTING_KEY, null);
controller.updateState(mPreference);
assertThat(mPreference.getProgress())
.isEqualTo(mVibrator.getDefaultVibrationIntensity(VIBRATION_USAGE));
}
@Test
public void updateState_allLevelsSupported_shouldDisplayIntensityInSliderPosition() {
VibrationIntensityPreferenceController controller = createPreferenceController(3);
updateSetting(SETTING_KEY, Vibrator.VIBRATION_INTENSITY_HIGH);
controller.updateState(mPreference);
assertThat(mPreference.getProgress()).isEqualTo(Vibrator.VIBRATION_INTENSITY_HIGH);
updateSetting(SETTING_KEY, Vibrator.VIBRATION_INTENSITY_MEDIUM);
controller.updateState(mPreference);
assertThat(mPreference.getProgress()).isEqualTo(Vibrator.VIBRATION_INTENSITY_MEDIUM);
updateSetting(SETTING_KEY, Vibrator.VIBRATION_INTENSITY_LOW);
controller.updateState(mPreference);
assertThat(mPreference.getProgress()).isEqualTo(Vibrator.VIBRATION_INTENSITY_LOW);
updateSetting(SETTING_KEY, Vibrator.VIBRATION_INTENSITY_OFF);
controller.updateState(mPreference);
assertThat(mPreference.getProgress()).isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF);
}
@Test
public void updateState_twoLevelsSupported_shouldDisplayMediumAndHighAtLastPosition() {
VibrationIntensityPreferenceController controller = createPreferenceController(2);
updateSetting(SETTING_KEY, Vibrator.VIBRATION_INTENSITY_HIGH);
controller.updateState(mPreference);
assertThat(mPreference.getProgress()).isEqualTo(Vibrator.VIBRATION_INTENSITY_MEDIUM);
updateSetting(SETTING_KEY, Vibrator.VIBRATION_INTENSITY_MEDIUM);
controller.updateState(mPreference);
assertThat(mPreference.getProgress()).isEqualTo(Vibrator.VIBRATION_INTENSITY_MEDIUM);
updateSetting(SETTING_KEY, Vibrator.VIBRATION_INTENSITY_LOW);
controller.updateState(mPreference);
assertThat(mPreference.getProgress()).isEqualTo(Vibrator.VIBRATION_INTENSITY_LOW);
updateSetting(SETTING_KEY, Vibrator.VIBRATION_INTENSITY_OFF);
controller.updateState(mPreference);
assertThat(mPreference.getProgress()).isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF);
}
@Test
public void updateState_oneLevelSupported_shouldDisplayAllAtLastPosition() {
VibrationIntensityPreferenceController controller = createPreferenceController(1);
updateSetting(SETTING_KEY, Vibrator.VIBRATION_INTENSITY_HIGH);
controller.updateState(mPreference);
assertThat(mPreference.getProgress()).isEqualTo(Vibrator.VIBRATION_INTENSITY_LOW);
updateSetting(SETTING_KEY, Vibrator.VIBRATION_INTENSITY_MEDIUM);
controller.updateState(mPreference);
assertThat(mPreference.getProgress()).isEqualTo(Vibrator.VIBRATION_INTENSITY_LOW);
updateSetting(SETTING_KEY, Vibrator.VIBRATION_INTENSITY_LOW);
controller.updateState(mPreference);
assertThat(mPreference.getProgress()).isEqualTo(Vibrator.VIBRATION_INTENSITY_LOW);
updateSetting(SETTING_KEY, Vibrator.VIBRATION_INTENSITY_OFF);
controller.updateState(mPreference);
assertThat(mPreference.getProgress()).isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF);
}
@Test
public void setProgress_allSupportedPositions_updatesIntensitySetting() throws Exception {
VibrationIntensityPreferenceController controller = createPreferenceController(3);
controller.setSliderPosition(Vibrator.VIBRATION_INTENSITY_OFF);
assertThat(readSetting(SETTING_KEY)).isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF);
controller.setSliderPosition(Vibrator.VIBRATION_INTENSITY_LOW);
assertThat(readSetting(SETTING_KEY)).isEqualTo(Vibrator.VIBRATION_INTENSITY_LOW);
controller.setSliderPosition(Vibrator.VIBRATION_INTENSITY_MEDIUM);
assertThat(readSetting(SETTING_KEY)).isEqualTo(Vibrator.VIBRATION_INTENSITY_MEDIUM);
controller.setSliderPosition(Vibrator.VIBRATION_INTENSITY_HIGH);
assertThat(readSetting(SETTING_KEY)).isEqualTo(Vibrator.VIBRATION_INTENSITY_HIGH);
}
@Test
public void setProgress_twoSupportedPositions_updatesMediumPositionToHigh() throws Exception {
VibrationIntensityPreferenceController controller = createPreferenceController(2);
controller.setSliderPosition(Vibrator.VIBRATION_INTENSITY_OFF);
assertThat(readSetting(SETTING_KEY)).isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF);
controller.setSliderPosition(Vibrator.VIBRATION_INTENSITY_LOW);
assertThat(readSetting(SETTING_KEY)).isEqualTo(Vibrator.VIBRATION_INTENSITY_LOW);
controller.setSliderPosition(Vibrator.VIBRATION_INTENSITY_MEDIUM);
assertThat(readSetting(SETTING_KEY)).isEqualTo(Vibrator.VIBRATION_INTENSITY_HIGH);
controller.setSliderPosition(Vibrator.VIBRATION_INTENSITY_HIGH);
assertThat(readSetting(SETTING_KEY)).isEqualTo(Vibrator.VIBRATION_INTENSITY_HIGH);
}
@Test
public void setProgress_oneSupportedPosition_updatesOnPositionsToDeviceDefault()
throws Exception {
int defaultIntensity = mVibrator.getDefaultVibrationIntensity(VIBRATION_USAGE);
VibrationIntensityPreferenceController controller = createPreferenceController(1);
controller.setSliderPosition(Vibrator.VIBRATION_INTENSITY_OFF);
assertThat(readSetting(SETTING_KEY)).isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF);
controller.setSliderPosition(Vibrator.VIBRATION_INTENSITY_LOW);
assertThat(readSetting(SETTING_KEY)).isEqualTo(defaultIntensity);
controller.setSliderPosition(Vibrator.VIBRATION_INTENSITY_MEDIUM);
assertThat(readSetting(SETTING_KEY)).isEqualTo(defaultIntensity);
controller.setSliderPosition(Vibrator.VIBRATION_INTENSITY_HIGH);
assertThat(readSetting(SETTING_KEY)).isEqualTo(defaultIntensity);
}
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 VibrationIntensityPreferenceController createPreferenceController(
int supportedIntensityLevels) {
VibrationIntensityPreferenceController controller =
new TestPreferenceController(mContext, supportedIntensityLevels);
mLifecycle.addObserver(controller);
mPreference = new SeekBarPreference(mContext);
mPreference.setSummary("Test summary");
when(mScreen.findPreference(controller.getPreferenceKey())).thenReturn(mPreference);
controller.displayPreference(mScreen);
return controller;
}
}

View File

@@ -30,11 +30,9 @@ import static org.mockito.Mockito.when;
import android.content.Context; import android.content.Context;
import android.media.AudioManager; import android.media.AudioManager;
import android.os.Vibrator; import android.os.Vibrator;
import android.provider.DeviceConfig;
import android.provider.Settings; import android.provider.Settings;
import android.telephony.TelephonyManager; import android.telephony.TelephonyManager;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference; import androidx.preference.SwitchPreference;
import androidx.test.core.app.ApplicationProvider; import androidx.test.core.app.ApplicationProvider;
@@ -53,14 +51,12 @@ public class VibrationRampingRingerTogglePreferenceControllerTest {
private static final String PREFERENCE_KEY = "preference_key"; private static final String PREFERENCE_KEY = "preference_key";
@Mock @Mock private PreferenceScreen mScreen;
private PreferenceScreen mScreen; @Mock private TelephonyManager mTelephonyManager;
@Mock @Mock private AudioManager mAudioManager;
private TelephonyManager mTelephonyManager; @Mock private VibrationRampingRingerTogglePreferenceController.DeviceConfigProvider
@Mock mDeviceConfigProvider;
private AudioManager mAudioManager;
private LifecycleOwner mLifecycleOwner;
private Lifecycle mLifecycle; private Lifecycle mLifecycle;
private Context mContext; private Context mContext;
private VibrationRampingRingerTogglePreferenceController mController; private VibrationRampingRingerTogglePreferenceController mController;
@@ -69,18 +65,17 @@ public class VibrationRampingRingerTogglePreferenceControllerTest {
@Before @Before
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
mLifecycleOwner = () -> mLifecycle; mLifecycle = new Lifecycle(() -> mLifecycle);
mLifecycle = new Lifecycle(mLifecycleOwner);
mContext = spy(ApplicationProvider.getApplicationContext()); mContext = spy(ApplicationProvider.getApplicationContext());
when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager); when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
when(mContext.getSystemService(Context.AUDIO_SERVICE)).thenReturn(mAudioManager); when(mContext.getSystemService(Context.AUDIO_SERVICE)).thenReturn(mAudioManager);
mController = new VibrationRampingRingerTogglePreferenceController(mContext, mController = new VibrationRampingRingerTogglePreferenceController(mContext,
PREFERENCE_KEY); PREFERENCE_KEY, mDeviceConfigProvider);
mLifecycle.addObserver(mController); mLifecycle.addObserver(mController);
mPreference = new SwitchPreference(mContext); mPreference = new SwitchPreference(mContext);
mPreference.setSummary("Test summary"); mPreference.setSummary("Test summary");
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
showPreference(); mController.displayPreference(mScreen);
} }
@Test @Test
@@ -91,8 +86,7 @@ public class VibrationRampingRingerTogglePreferenceControllerTest {
@Test @Test
public void getAvailabilityStatus_notVoiceCapable_returnUnsupportedOnDevice() { public void getAvailabilityStatus_notVoiceCapable_returnUnsupportedOnDevice() {
when(mTelephonyManager.isVoiceCapable()).thenReturn(false); when(mTelephonyManager.isVoiceCapable()).thenReturn(false);
DeviceConfig.setProperty(DeviceConfig.NAMESPACE_TELEPHONY, when(mDeviceConfigProvider.isRampingRingerEnabledOnTelephonyConfig()).thenReturn(false);
VibrationRampingRingerTogglePreferenceController.DEVICE_CONFIG_KEY, "false", false);
assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE); assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
} }
@@ -100,8 +94,7 @@ public class VibrationRampingRingerTogglePreferenceControllerTest {
@Test @Test
public void getAvailabilityStatus_rampingRingerEnabled_returnUnsupportedOnDevice() { public void getAvailabilityStatus_rampingRingerEnabled_returnUnsupportedOnDevice() {
when(mTelephonyManager.isVoiceCapable()).thenReturn(true); when(mTelephonyManager.isVoiceCapable()).thenReturn(true);
DeviceConfig.setProperty(DeviceConfig.NAMESPACE_TELEPHONY, when(mDeviceConfigProvider.isRampingRingerEnabledOnTelephonyConfig()).thenReturn(true);
VibrationRampingRingerTogglePreferenceController.DEVICE_CONFIG_KEY, "true", false);
assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE); assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
} }
@@ -109,8 +102,7 @@ public class VibrationRampingRingerTogglePreferenceControllerTest {
@Test @Test
public void getAvailabilityStatus_voiceCapableAndRampingRingerDisabled_returnAvailable() { public void getAvailabilityStatus_voiceCapableAndRampingRingerDisabled_returnAvailable() {
when(mTelephonyManager.isVoiceCapable()).thenReturn(true); when(mTelephonyManager.isVoiceCapable()).thenReturn(true);
DeviceConfig.setProperty(DeviceConfig.NAMESPACE_TELEPHONY, when(mDeviceConfigProvider.isRampingRingerEnabledOnTelephonyConfig()).thenReturn(false);
VibrationRampingRingerTogglePreferenceController.DEVICE_CONFIG_KEY, "false", false);
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
} }
@@ -143,8 +135,8 @@ public class VibrationRampingRingerTogglePreferenceControllerTest {
public void setChecked_withRingDisabled_ignoresUpdates() { public void setChecked_withRingDisabled_ignoresUpdates() {
updateSetting(Settings.System.RING_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_OFF); updateSetting(Settings.System.RING_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_OFF);
mPreference.setChecked(true); mController.setChecked(true);
mPreference.setChecked(false); mController.setChecked(false);
verify(mAudioManager, never()).setRampingRingerEnabled(anyBoolean()); verify(mAudioManager, never()).setRampingRingerEnabled(anyBoolean());
} }
@@ -152,18 +144,14 @@ public class VibrationRampingRingerTogglePreferenceControllerTest {
public void setChecked_withRingEnabled_updatesSetting() { public void setChecked_withRingEnabled_updatesSetting() {
updateSetting(Settings.System.RING_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_HIGH); updateSetting(Settings.System.RING_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_HIGH);
mPreference.setChecked(true); mController.setChecked(true);
verify(mAudioManager).setRampingRingerEnabled(true); verify(mAudioManager).setRampingRingerEnabled(true);
mPreference.setChecked(false); mController.setChecked(false);
verify(mAudioManager).setRampingRingerEnabled(false); verify(mAudioManager).setRampingRingerEnabled(false);
} }
private void updateSetting(String key, int value) { private void updateSetting(String key, int value) {
Settings.System.putInt(mContext.getContentResolver(), key, value); Settings.System.putInt(mContext.getContentResolver(), key, value);
} }
private void showPreference() {
mController.displayPreference(mScreen);
}
} }