Merge "Create FakeVibrator to be used in service tests"

This commit is contained in:
Lais Andrade
2021-01-19 14:26:26 +00:00
committed by Android (Google) Code Review
4 changed files with 107 additions and 52 deletions

View File

@@ -64,6 +64,7 @@ import androidx.test.InstrumentationRegistry;
import com.android.internal.util.test.FakeSettingsProvider;
import com.android.internal.util.test.FakeSettingsProviderRule;
import com.android.server.vibrator.FakeVibrator;
import com.android.server.vibrator.FakeVibratorControllerProvider;
import com.android.server.vibrator.VibratorController;
@@ -114,8 +115,6 @@ public class VibratorServiceTest {
@Mock private PackageManagerInternal mPackageManagerInternalMock;
@Mock private PowerManagerInternal mPowerManagerInternalMock;
// TODO(b/131311651): replace with a FakeVibrator instead.
@Mock private Vibrator mVibratorMock;
@Mock private AppOpsManager mAppOpsManagerMock;
@Mock private IVibratorStateListener mVibratorStateListenerMock;
@Mock private IInputManager mIInputManagerMock;
@@ -124,26 +123,22 @@ public class VibratorServiceTest {
private TestLooper mTestLooper;
private ContextWrapper mContextSpy;
private PowerManagerInternal.LowPowerModeListener mRegisteredPowerModeListener;
private FakeVibrator mFakeVibrator;
private FakeVibratorControllerProvider mVibratorProvider;
@Before
public void setUp() throws Exception {
mTestLooper = new TestLooper();
mFakeVibrator = new FakeVibrator();
mVibratorProvider = new FakeVibratorControllerProvider(mTestLooper.getLooper());
mContextSpy = spy(new ContextWrapper(InstrumentationRegistry.getContext()));
InputManager inputManager = InputManager.resetInstance(mIInputManagerMock);
ContentResolver contentResolver = mSettingsProviderRule.mockContentResolver(mContextSpy);
when(mContextSpy.getContentResolver()).thenReturn(contentResolver);
when(mContextSpy.getSystemService(eq(Context.VIBRATOR_SERVICE))).thenReturn(mVibratorMock);
when(mContextSpy.getSystemService(eq(Context.VIBRATOR_SERVICE))).thenReturn(mFakeVibrator);
when(mContextSpy.getSystemService(eq(Context.INPUT_SERVICE))).thenReturn(inputManager);
when(mContextSpy.getSystemService(Context.APP_OPS_SERVICE)).thenReturn(mAppOpsManagerMock);
when(mVibratorMock.getDefaultHapticFeedbackIntensity())
.thenReturn(Vibrator.VIBRATION_INTENSITY_MEDIUM);
when(mVibratorMock.getDefaultNotificationVibrationIntensity())
.thenReturn(Vibrator.VIBRATION_INTENSITY_MEDIUM);
when(mVibratorMock.getDefaultRingVibrationIntensity())
.thenReturn(Vibrator.VIBRATION_INTENSITY_MEDIUM);
when(mVibratorStateListenerMock.asBinder()).thenReturn(mVibratorStateListenerBinderMock);
when(mPackageManagerInternalMock.getSystemUiServiceComponent())
.thenReturn(new ComponentName("", ""));
@@ -586,8 +581,7 @@ public class VibratorServiceTest {
@Test
public void scale_withOneShotAndWaveform_usesScaleLevelOnAmplitude() throws Exception {
when(mVibratorMock.getDefaultNotificationVibrationIntensity())
.thenReturn(Vibrator.VIBRATION_INTENSITY_LOW);
mFakeVibrator.setDefaultNotificationVibrationIntensity(Vibrator.VIBRATION_INTENSITY_LOW);
setUserSetting(Settings.System.NOTIFICATION_VIBRATION_INTENSITY,
Vibrator.VIBRATION_INTENSITY_HIGH);
setUserSetting(Settings.System.HAPTIC_FEEDBACK_INTENSITY,
@@ -617,8 +611,7 @@ public class VibratorServiceTest {
@Test
public void scale_withComposed_usesScaleLevelOnPrimitiveScaleValues() throws Exception {
when(mVibratorMock.getDefaultNotificationVibrationIntensity())
.thenReturn(Vibrator.VIBRATION_INTENSITY_LOW);
mFakeVibrator.setDefaultNotificationVibrationIntensity(Vibrator.VIBRATION_INTENSITY_LOW);
setUserSetting(Settings.System.NOTIFICATION_VIBRATION_INTENSITY,
Vibrator.VIBRATION_INTENSITY_HIGH);
setUserSetting(Settings.System.HAPTIC_FEEDBACK_INTENSITY,

View File

@@ -0,0 +1,79 @@
/*
* 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.server.vibrator;
import android.os.VibrationAttributes;
import android.os.VibrationEffect;
import android.os.Vibrator;
import androidx.annotation.NonNull;
/** Fake implementation of {@link Vibrator} for service tests. */
public final class FakeVibrator extends Vibrator {
private int mDefaultHapticFeedbackIntensity = Vibrator.VIBRATION_INTENSITY_MEDIUM;
private int mDefaultNotificationIntensity = Vibrator.VIBRATION_INTENSITY_MEDIUM;
private int mDefaultRingIntensity = Vibrator.VIBRATION_INTENSITY_MEDIUM;
@Override
public int getDefaultHapticFeedbackIntensity() {
return mDefaultHapticFeedbackIntensity;
}
@Override
public int getDefaultNotificationVibrationIntensity() {
return mDefaultNotificationIntensity;
}
@Override
public int getDefaultRingVibrationIntensity() {
return mDefaultRingIntensity;
}
public void setDefaultHapticFeedbackIntensity(
@VibrationIntensity int defaultHapticFeedbackIntensity) {
mDefaultHapticFeedbackIntensity = defaultHapticFeedbackIntensity;
}
public void setDefaultNotificationVibrationIntensity(
@VibrationIntensity int defaultNotificationIntensity) {
mDefaultNotificationIntensity = defaultNotificationIntensity;
}
public void setDefaultRingVibrationIntensity(@VibrationIntensity int defaultRingIntensity) {
mDefaultRingIntensity = defaultRingIntensity;
}
@Override
public boolean hasVibrator() {
return true;
}
@Override
public boolean hasAmplitudeControl() {
return true;
}
@Override
public void vibrate(int uid, String opPkg, @NonNull VibrationEffect vibe, String reason,
@NonNull VibrationAttributes attributes) {
}
@Override
public void cancel() {
}
}

View File

@@ -64,23 +64,23 @@ public class VibrationScalerTest {
@Rule public MockitoRule mMockitoRule = MockitoJUnit.rule();
@Rule public FakeSettingsProviderRule mSettingsProviderRule = FakeSettingsProvider.rule();
// TODO(b/131311651): replace with a FakeVibrator instead.
@Mock private Vibrator mVibratorMock;
@Mock private PowerManagerInternal mPowerManagerInternalMock;
private TestLooper mTestLooper;
private ContextWrapper mContextSpy;
private FakeVibrator mFakeVibrator;
private VibrationSettings mVibrationSettings;
private VibrationScaler mVibrationScaler;
@Before
public void setUp() throws Exception {
mTestLooper = new TestLooper();
mFakeVibrator = new FakeVibrator();
mContextSpy = spy(new ContextWrapper(InstrumentationRegistry.getContext()));
ContentResolver contentResolver = mSettingsProviderRule.mockContentResolver(mContextSpy);
when(mContextSpy.getContentResolver()).thenReturn(contentResolver);
when(mContextSpy.getSystemService(eq(Context.VIBRATOR_SERVICE))).thenReturn(mVibratorMock);
when(mContextSpy.getSystemService(eq(Context.VIBRATOR_SERVICE))).thenReturn(mFakeVibrator);
LocalServices.removeServiceForTest(PowerManagerInternal.class);
LocalServices.addService(PowerManagerInternal.class, mPowerManagerInternalMock);
@@ -97,8 +97,7 @@ public class VibrationScalerTest {
@Test
public void testGetExternalVibrationScale() {
when(mVibratorMock.getDefaultHapticFeedbackIntensity())
.thenReturn(Vibrator.VIBRATION_INTENSITY_LOW);
mFakeVibrator.setDefaultHapticFeedbackIntensity(Vibrator.VIBRATION_INTENSITY_LOW);
setUserSetting(Settings.System.HAPTIC_FEEDBACK_INTENSITY,
Vibrator.VIBRATION_INTENSITY_HIGH);
assertEquals(IExternalVibratorService.SCALE_VERY_HIGH,
@@ -114,13 +113,11 @@ public class VibrationScalerTest {
assertEquals(IExternalVibratorService.SCALE_NONE,
mVibrationScaler.getExternalVibrationScale(VibrationAttributes.USAGE_TOUCH));
when(mVibratorMock.getDefaultHapticFeedbackIntensity())
.thenReturn(Vibrator.VIBRATION_INTENSITY_MEDIUM);
mFakeVibrator.setDefaultHapticFeedbackIntensity(Vibrator.VIBRATION_INTENSITY_MEDIUM);
assertEquals(IExternalVibratorService.SCALE_LOW,
mVibrationScaler.getExternalVibrationScale(VibrationAttributes.USAGE_TOUCH));
when(mVibratorMock.getDefaultHapticFeedbackIntensity())
.thenReturn(Vibrator.VIBRATION_INTENSITY_HIGH);
mFakeVibrator.setDefaultHapticFeedbackIntensity(Vibrator.VIBRATION_INTENSITY_HIGH);
assertEquals(IExternalVibratorService.SCALE_VERY_LOW,
mVibrationScaler.getExternalVibrationScale(VibrationAttributes.USAGE_TOUCH));
@@ -222,8 +219,7 @@ public class VibrationScalerTest {
@Test
public void scale_withOneShotAndWaveform_resolvesAmplitude() {
// No scale, default amplitude still resolved
when(mVibratorMock.getDefaultRingVibrationIntensity())
.thenReturn(Vibrator.VIBRATION_INTENSITY_LOW);
mFakeVibrator.setDefaultRingVibrationIntensity(Vibrator.VIBRATION_INTENSITY_LOW);
setUserSetting(Settings.System.RING_VIBRATION_INTENSITY,
Vibrator.VIBRATION_INTENSITY_LOW);
@@ -241,16 +237,13 @@ public class VibrationScalerTest {
@Test
public void scale_withOneShotWaveform_scalesAmplitude() {
when(mVibratorMock.getDefaultRingVibrationIntensity())
.thenReturn(Vibrator.VIBRATION_INTENSITY_LOW);
mFakeVibrator.setDefaultRingVibrationIntensity(Vibrator.VIBRATION_INTENSITY_LOW);
setUserSetting(Settings.System.RING_VIBRATION_INTENSITY,
Vibrator.VIBRATION_INTENSITY_HIGH);
when(mVibratorMock.getDefaultNotificationVibrationIntensity())
.thenReturn(Vibrator.VIBRATION_INTENSITY_HIGH);
mFakeVibrator.setDefaultNotificationVibrationIntensity(Vibrator.VIBRATION_INTENSITY_HIGH);
setUserSetting(Settings.System.NOTIFICATION_VIBRATION_INTENSITY,
Vibrator.VIBRATION_INTENSITY_LOW);
when(mVibratorMock.getDefaultHapticFeedbackIntensity())
.thenReturn(Vibrator.VIBRATION_INTENSITY_MEDIUM);
mFakeVibrator.setDefaultHapticFeedbackIntensity(Vibrator.VIBRATION_INTENSITY_MEDIUM);
setUserSetting(Settings.System.HAPTIC_FEEDBACK_INTENSITY,
Vibrator.VIBRATION_INTENSITY_MEDIUM);
@@ -273,16 +266,13 @@ public class VibrationScalerTest {
@Test
public void scale_withComposed_scalesPrimitives() {
when(mVibratorMock.getDefaultRingVibrationIntensity())
.thenReturn(Vibrator.VIBRATION_INTENSITY_LOW);
mFakeVibrator.setDefaultRingVibrationIntensity(Vibrator.VIBRATION_INTENSITY_LOW);
setUserSetting(Settings.System.RING_VIBRATION_INTENSITY,
Vibrator.VIBRATION_INTENSITY_HIGH);
when(mVibratorMock.getDefaultNotificationVibrationIntensity())
.thenReturn(Vibrator.VIBRATION_INTENSITY_HIGH);
mFakeVibrator.setDefaultNotificationVibrationIntensity(Vibrator.VIBRATION_INTENSITY_HIGH);
setUserSetting(Settings.System.NOTIFICATION_VIBRATION_INTENSITY,
Vibrator.VIBRATION_INTENSITY_LOW);
when(mVibratorMock.getDefaultHapticFeedbackIntensity())
.thenReturn(Vibrator.VIBRATION_INTENSITY_MEDIUM);
mFakeVibrator.setDefaultHapticFeedbackIntensity(Vibrator.VIBRATION_INTENSITY_MEDIUM);
setUserSetting(Settings.System.HAPTIC_FEEDBACK_INTENSITY,
Vibrator.VIBRATION_INTENSITY_MEDIUM);

View File

@@ -76,26 +76,25 @@ public class VibrationSettingsTest {
@Rule public MockitoRule mMockitoRule = MockitoJUnit.rule();
@Rule public FakeSettingsProviderRule mSettingsProviderRule = FakeSettingsProvider.rule();
// TODO(b/131311651): replace with a FakeVibrator instead.
@Mock private Vibrator mVibratorMock;
@Mock private VibrationSettings.OnVibratorSettingsChanged mListenerMock;
@Mock private PowerManagerInternal mPowerManagerInternalMock;
private TestLooper mTestLooper;
private ContextWrapper mContextSpy;
private AudioManager mAudioManager;
private FakeVibrator mFakeVibrator;
private VibrationSettings mVibrationSettings;
private PowerManagerInternal.LowPowerModeListener mRegisteredPowerModeListener;
@Before
public void setUp() throws Exception {
mTestLooper = new TestLooper();
mFakeVibrator = new FakeVibrator();
mContextSpy = spy(new ContextWrapper(InstrumentationRegistry.getContext()));
ContentResolver contentResolver = mSettingsProviderRule.mockContentResolver(mContextSpy);
when(mContextSpy.getContentResolver()).thenReturn(contentResolver);
when(mContextSpy.getSystemService(eq(Context.VIBRATOR_SERVICE))).thenReturn(mVibratorMock);
when(mVibratorMock.hasVibrator()).thenReturn(true);
when(mContextSpy.getSystemService(eq(Context.VIBRATOR_SERVICE))).thenReturn(mFakeVibrator);
doAnswer(invocation -> {
mRegisteredPowerModeListener = invocation.getArgument(0);
return null;
@@ -305,12 +304,9 @@ public class VibrationSettingsTest {
@Test
public void getDefaultIntensity_returnsIntensityFromVibratorService() {
when(mVibratorMock.getDefaultHapticFeedbackIntensity())
.thenReturn(Vibrator.VIBRATION_INTENSITY_HIGH);
when(mVibratorMock.getDefaultNotificationVibrationIntensity())
.thenReturn(Vibrator.VIBRATION_INTENSITY_MEDIUM);
when(mVibratorMock.getDefaultRingVibrationIntensity())
.thenReturn(Vibrator.VIBRATION_INTENSITY_LOW);
mFakeVibrator.setDefaultHapticFeedbackIntensity(Vibrator.VIBRATION_INTENSITY_HIGH);
mFakeVibrator.setDefaultNotificationVibrationIntensity(Vibrator.VIBRATION_INTENSITY_MEDIUM);
mFakeVibrator.setDefaultRingVibrationIntensity(Vibrator.VIBRATION_INTENSITY_LOW);
setUserSetting(Settings.System.NOTIFICATION_VIBRATION_INTENSITY,
Vibrator.VIBRATION_INTENSITY_OFF);
@@ -336,12 +332,9 @@ public class VibrationSettingsTest {
@Test
public void getCurrentIntensity_returnsIntensityFromSettings() {
when(mVibratorMock.getDefaultHapticFeedbackIntensity())
.thenReturn(Vibrator.VIBRATION_INTENSITY_OFF);
when(mVibratorMock.getDefaultNotificationVibrationIntensity())
.thenReturn(Vibrator.VIBRATION_INTENSITY_OFF);
when(mVibratorMock.getDefaultRingVibrationIntensity())
.thenReturn(Vibrator.VIBRATION_INTENSITY_OFF);
mFakeVibrator.setDefaultHapticFeedbackIntensity(Vibrator.VIBRATION_INTENSITY_OFF);
mFakeVibrator.setDefaultNotificationVibrationIntensity(Vibrator.VIBRATION_INTENSITY_OFF);
mFakeVibrator.setDefaultRingVibrationIntensity(Vibrator.VIBRATION_INTENSITY_OFF);
setUserSetting(Settings.System.HAPTIC_FEEDBACK_INTENSITY,
Vibrator.VIBRATION_INTENSITY_HIGH);