From 61ddb9473fbc21e198bab296d37ee686e3f5ed1f Mon Sep 17 00:00:00 2001 From: Lais Andrade Date: Tue, 17 Nov 2020 15:47:50 +0000 Subject: [PATCH] Create parcelable VibratorInfo to expose HAL profile This parcelable will replace the separated methods hasAmplitudeControl, areEffectsSupported and arePrimitiveSupported from VibratorService. VibratorManagerService will return this single parcelable, per vibator id, to represent the HAL profile of that individual vibrator and be used by Vibrator.java to explose some of that to the public API. Bug: 167946816 Test: atest VibratorInfoTest atest VibratorManagerServiceTest Change-Id: I09c191dd15a3cf38122ec6dc87b2d73f46c8cde6 --- .../android/os/IVibratorManagerService.aidl | 2 + core/java/android/os/IVibratorService.aidl | 4 +- core/java/android/os/SystemVibrator.java | 45 +++- core/java/android/os/VibrationEffect.java | 24 +- core/java/android/os/VibratorInfo.aidl | 19 ++ core/java/android/os/VibratorInfo.java | 229 ++++++++++++++++++ .../src/android/os/VibratorInfoTest.java | 112 +++++++++ .../src/android/os/VibratorTest.java | 20 ++ .../server/VibratorManagerService.java | 8 + .../com/android/server/VibratorService.java | 16 +- .../server/vibrator/VibratorController.java | 90 ++----- .../server/VibratorManagerServiceTest.java | 44 +++- .../android/server/VibratorServiceTest.java | 57 +---- .../vibrator/VibratorControllerTest.java | 57 +---- 14 files changed, 521 insertions(+), 206 deletions(-) create mode 100644 core/java/android/os/VibratorInfo.aidl create mode 100644 core/java/android/os/VibratorInfo.java create mode 100644 core/tests/coretests/src/android/os/VibratorInfoTest.java diff --git a/core/java/android/os/IVibratorManagerService.aidl b/core/java/android/os/IVibratorManagerService.aidl index b591f6016d14c..804dc102c3f6b 100644 --- a/core/java/android/os/IVibratorManagerService.aidl +++ b/core/java/android/os/IVibratorManagerService.aidl @@ -18,10 +18,12 @@ package android.os; import android.os.CombinedVibrationEffect; import android.os.VibrationAttributes; +import android.os.VibratorInfo; /** {@hide} */ interface IVibratorManagerService { int[] getVibratorIds(); + VibratorInfo getVibratorInfo(int vibratorId); boolean setAlwaysOnEffect(int uid, String opPkg, int alwaysOnId, in CombinedVibrationEffect effect, in VibrationAttributes attributes); void vibrate(int uid, String opPkg, in CombinedVibrationEffect effect, diff --git a/core/java/android/os/IVibratorService.aidl b/core/java/android/os/IVibratorService.aidl index 562ac16099ddc..1cd48dcf797b3 100644 --- a/core/java/android/os/IVibratorService.aidl +++ b/core/java/android/os/IVibratorService.aidl @@ -18,6 +18,7 @@ package android.os; import android.os.VibrationEffect; import android.os.VibrationAttributes; +import android.os.VibratorInfo; import android.os.IVibratorStateListener; /** {@hide} */ @@ -25,11 +26,10 @@ interface IVibratorService { boolean hasVibrator(); boolean isVibrating(); + VibratorInfo getVibratorInfo(); boolean registerVibratorStateListener(in IVibratorStateListener listener); boolean unregisterVibratorStateListener(in IVibratorStateListener listener); boolean hasAmplitudeControl(); - int[] areEffectsSupported(in int[] effectIds); - boolean[] arePrimitivesSupported(in int[] primitiveIds); void vibrate(int uid, String opPkg, in VibrationEffect effect, in VibrationAttributes attributes, String reason, IBinder token); void cancelVibrate(IBinder token); diff --git a/core/java/android/os/SystemVibrator.java b/core/java/android/os/SystemVibrator.java index b742ee2be43bf..0330500f09977 100644 --- a/core/java/android/os/SystemVibrator.java +++ b/core/java/android/os/SystemVibrator.java @@ -18,6 +18,7 @@ package android.os; import android.annotation.CallbackExecutor; import android.annotation.NonNull; +import android.annotation.Nullable; import android.compat.annotation.UnsupportedAppUsage; import android.content.Context; import android.media.AudioAttributes; @@ -39,8 +40,11 @@ public class SystemVibrator extends Vibrator { private final IVibratorService mService; private final IVibratorManagerService mManagerService; + private final Object mLock = new Object(); private final Binder mToken = new Binder(); private final Context mContext; + @GuardedBy("mLock") + private VibratorInfo mVibratorInfo; @GuardedBy("mDelegates") private final ArrayMap CREATOR = + new Creator() { + @Override + public VibratorInfo createFromParcel(Parcel in) { + return new VibratorInfo(in); + } + + @Override + public VibratorInfo[] newArray(int size) { + return new VibratorInfo[size]; + } + }; +} diff --git a/core/tests/coretests/src/android/os/VibratorInfoTest.java b/core/tests/coretests/src/android/os/VibratorInfoTest.java new file mode 100644 index 0000000000000..89411902bb6b8 --- /dev/null +++ b/core/tests/coretests/src/android/os/VibratorInfoTest.java @@ -0,0 +1,112 @@ +/* + * 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 android.os; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertFalse; +import static junit.framework.Assert.assertTrue; + +import android.platform.test.annotations.Presubmit; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@Presubmit +@RunWith(JUnit4.class) +public class VibratorInfoTest { + + @Test + public void testHasAmplitudeControl() { + assertFalse(createInfo(/* capabilities= */ 0).hasAmplitudeControl()); + assertTrue(createInfo(VibratorInfo.CAPABILITY_COMPOSE_EFFECTS + | VibratorInfo.CAPABILITY_AMPLITUDE_CONTROL).hasAmplitudeControl()); + } + + @Test + public void testHasCapabilities() { + assertTrue(createInfo(VibratorInfo.CAPABILITY_COMPOSE_EFFECTS) + .hasCapability(VibratorInfo.CAPABILITY_COMPOSE_EFFECTS)); + assertFalse(createInfo(VibratorInfo.CAPABILITY_COMPOSE_EFFECTS) + .hasCapability(VibratorInfo.CAPABILITY_AMPLITUDE_CONTROL)); + } + + @Test + public void testIsEffectSupported() { + VibratorInfo info = new VibratorInfo(/* id= */ 0, /* capabilities= */0, + new int[]{VibrationEffect.EFFECT_CLICK}, null); + assertEquals(Vibrator.VIBRATION_EFFECT_SUPPORT_UNKNOWN, + createInfo(/* capabilities= */ 0).isEffectSupported(VibrationEffect.EFFECT_CLICK)); + assertEquals(Vibrator.VIBRATION_EFFECT_SUPPORT_YES, + info.isEffectSupported(VibrationEffect.EFFECT_CLICK)); + assertEquals(Vibrator.VIBRATION_EFFECT_SUPPORT_NO, + info.isEffectSupported(VibrationEffect.EFFECT_TICK)); + } + + @Test + public void testIsPrimitiveSupported() { + VibratorInfo info = new VibratorInfo(/* id= */ 0, VibratorInfo.CAPABILITY_COMPOSE_EFFECTS, + null, new int[]{VibrationEffect.Composition.PRIMITIVE_CLICK}); + assertTrue(info.isPrimitiveSupported(VibrationEffect.Composition.PRIMITIVE_CLICK)); + assertFalse(info.isPrimitiveSupported(VibrationEffect.Composition.PRIMITIVE_TICK)); + + // Returns false when there is no compose capability. + info = new VibratorInfo(/* id= */ 0, /* capabilities= */ 0, + null, new int[]{VibrationEffect.Composition.PRIMITIVE_CLICK}); + assertFalse(info.isPrimitiveSupported(VibrationEffect.Composition.PRIMITIVE_CLICK)); + } + + @Test + public void testEquals() { + VibratorInfo empty = new VibratorInfo(1, 0, null, null); + VibratorInfo complete = new VibratorInfo(1, VibratorInfo.CAPABILITY_AMPLITUDE_CONTROL, + new int[]{VibrationEffect.EFFECT_CLICK}, + new int[]{VibrationEffect.Composition.PRIMITIVE_CLICK}); + + assertEquals(complete, complete); + assertEquals(complete, new VibratorInfo(1, VibratorInfo.CAPABILITY_AMPLITUDE_CONTROL, + new int[]{VibrationEffect.EFFECT_CLICK}, + new int[]{VibrationEffect.Composition.PRIMITIVE_CLICK})); + + assertFalse(empty.equals(new VibratorInfo(1, 0, new int[]{}, new int[]{}))); + assertFalse(complete.equals(new VibratorInfo(1, VibratorInfo.CAPABILITY_COMPOSE_EFFECTS, + new int[]{VibrationEffect.EFFECT_CLICK}, + new int[]{VibrationEffect.Composition.PRIMITIVE_CLICK}))); + assertFalse(complete.equals(new VibratorInfo(1, VibratorInfo.CAPABILITY_AMPLITUDE_CONTROL, + new int[]{}, new int[]{}))); + assertFalse(complete.equals(new VibratorInfo(1, VibratorInfo.CAPABILITY_AMPLITUDE_CONTROL, + null, new int[]{VibrationEffect.Composition.PRIMITIVE_CLICK}))); + assertFalse(complete.equals(new VibratorInfo(1, VibratorInfo.CAPABILITY_AMPLITUDE_CONTROL, + new int[]{VibrationEffect.EFFECT_CLICK}, null))); + } + + @Test + public void testSerialization() { + VibratorInfo original = new VibratorInfo(1, VibratorInfo.CAPABILITY_COMPOSE_EFFECTS, + new int[]{VibrationEffect.EFFECT_CLICK}, null); + + Parcel parcel = Parcel.obtain(); + original.writeToParcel(parcel, 0); + parcel.setDataPosition(0); + VibratorInfo restored = VibratorInfo.CREATOR.createFromParcel(parcel); + assertEquals(original, restored); + } + + private static VibratorInfo createInfo(long capabilities) { + return new VibratorInfo(/* id= */ 0, capabilities, null, null); + } +} diff --git a/core/tests/coretests/src/android/os/VibratorTest.java b/core/tests/coretests/src/android/os/VibratorTest.java index c3d84ecc2abfc..575a1be905f2a 100644 --- a/core/tests/coretests/src/android/os/VibratorTest.java +++ b/core/tests/coretests/src/android/os/VibratorTest.java @@ -54,6 +54,26 @@ public class VibratorTest { mVibratorSpy = spy(InstrumentationRegistry.getContext().getSystemService(Vibrator.class)); } + @Test + public void areEffectsSupported_returnsArrayOfSameSize() { + assertEquals(0, mVibratorSpy.areEffectsSupported(new int[0]).length); + assertEquals(1, + mVibratorSpy.areEffectsSupported(new int[]{VibrationEffect.EFFECT_CLICK}).length); + assertEquals(2, + mVibratorSpy.areEffectsSupported(new int[]{VibrationEffect.EFFECT_CLICK, + VibrationEffect.EFFECT_TICK}).length); + } + + @Test + public void arePrimitivesSupported_returnsArrayOfSameSize() { + assertEquals(0, mVibratorSpy.arePrimitivesSupported(new int[0]).length); + assertEquals(1, mVibratorSpy.arePrimitivesSupported( + new int[]{VibrationEffect.Composition.PRIMITIVE_CLICK}).length); + assertEquals(2, mVibratorSpy.arePrimitivesSupported( + new int[]{VibrationEffect.Composition.PRIMITIVE_CLICK, + VibrationEffect.Composition.PRIMITIVE_QUICK_RISE}).length); + } + @Test public void vibrate_withAudioAttributes_createsVibrationAttributesWithSameUsage() { VibrationEffect effect = VibrationEffect.get(VibrationEffect.EFFECT_CLICK); diff --git a/services/core/java/com/android/server/VibratorManagerService.java b/services/core/java/com/android/server/VibratorManagerService.java index 57ab0471b343c..4491ba777478f 100644 --- a/services/core/java/com/android/server/VibratorManagerService.java +++ b/services/core/java/com/android/server/VibratorManagerService.java @@ -37,6 +37,7 @@ import android.os.Trace; import android.os.VibrationAttributes; import android.os.VibrationEffect; import android.os.Vibrator; +import android.os.VibratorInfo; import android.util.Slog; import android.util.SparseArray; @@ -165,6 +166,13 @@ public class VibratorManagerService extends IVibratorManagerService.Stub { } } + @Override // Binder call + @Nullable + public VibratorInfo getVibratorInfo(int vibratorId) { + VibratorController controller = mVibrators.get(vibratorId); + return controller == null ? null : controller.getVibratorInfo(); + } + @Override // Binder call public int[] getVibratorIds() { return Arrays.copyOf(mVibratorIds, mVibratorIds.length); diff --git a/services/core/java/com/android/server/VibratorService.java b/services/core/java/com/android/server/VibratorService.java index bede1563655a1..0af6e26d0e997 100644 --- a/services/core/java/com/android/server/VibratorService.java +++ b/services/core/java/com/android/server/VibratorService.java @@ -51,6 +51,7 @@ import android.os.Trace; import android.os.VibrationAttributes; import android.os.VibrationEffect; import android.os.Vibrator; +import android.os.VibratorInfo; import android.os.WorkSource; import android.util.Slog; import android.util.SparseArray; @@ -343,6 +344,11 @@ public class VibratorService extends IVibratorService.Stub { return mVibratorController.isVibrating(); } + @Override // Binder call + public VibratorInfo getVibratorInfo() { + return mVibratorController.getVibratorInfo(); + } + @Override // Binder call public boolean registerVibratorStateListener(IVibratorStateListener listener) { if (!hasPermission(android.Manifest.permission.ACCESS_VIBRATOR_STATE)) { @@ -367,16 +373,6 @@ public class VibratorService extends IVibratorService.Stub { || mVibratorController.hasCapability(IVibrator.CAP_AMPLITUDE_CONTROL); } - @Override // Binder call - public int[] areEffectsSupported(int[] effectIds) { - return mVibratorController.areEffectsSupported(effectIds); - } - - @Override // Binder call - public boolean[] arePrimitivesSupported(int[] primitiveIds) { - return mVibratorController.arePrimitivesSupported(primitiveIds); - } - private void verifyIncomingUid(int uid) { if (uid == Binder.getCallingUid()) { return; diff --git a/services/core/java/com/android/server/vibrator/VibratorController.java b/services/core/java/com/android/server/vibrator/VibratorController.java index f76c1a1b2b9de..311c73bcb19ff 100644 --- a/services/core/java/com/android/server/vibrator/VibratorController.java +++ b/services/core/java/com/android/server/vibrator/VibratorController.java @@ -23,7 +23,7 @@ import android.os.IVibratorStateListener; import android.os.RemoteCallbackList; import android.os.RemoteException; import android.os.VibrationEffect; -import android.os.Vibrator; +import android.os.VibratorInfo; import android.util.Slog; import com.android.internal.annotations.GuardedBy; @@ -31,10 +31,6 @@ import com.android.internal.annotations.VisibleForTesting; import libcore.util.NativeAllocationRegistry; -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; - /** Controls a single vibrator. */ // TODO(b/159207608): Make this package-private once vibrator services are moved to this package public final class VibratorController { @@ -42,12 +38,7 @@ public final class VibratorController { private final Object mLock = new Object(); private final NativeWrapper mNativeWrapper; - private final int mVibratorId; - private final long mCapabilities; - @Nullable - private final Set mSupportedEffects; - @Nullable - private final Set mSupportedPrimitives; + private final VibratorInfo mVibratorInfo; @GuardedBy("mLock") private final RemoteCallbackList mVibratorStateListeners = @@ -115,13 +106,11 @@ public final class VibratorController { @VisibleForTesting public VibratorController(int vibratorId, OnVibrationCompleteListener listener, NativeWrapper nativeWrapper) { - mVibratorId = vibratorId; mNativeWrapper = nativeWrapper; + mNativeWrapper.init(vibratorId, listener); - nativeWrapper.init(vibratorId, listener); - mCapabilities = nativeWrapper.getCapabilities(); - mSupportedEffects = asSet(nativeWrapper.getSupportedEffects()); - mSupportedPrimitives = asSet(nativeWrapper.getSupportedPrimitives()); + mVibratorInfo = new VibratorInfo(vibratorId, nativeWrapper.getCapabilities(), + nativeWrapper.getSupportedEffects(), nativeWrapper.getSupportedPrimitives()); } /** Register state listener for this vibrator. */ @@ -153,9 +142,9 @@ public final class VibratorController { } } - /** Return the id of the vibrator controlled by this instance. */ - public int getVibratorId() { - return mVibratorId; + /** Return the {@link VibratorInfo} representing the vibrator controlled by this instance. */ + public VibratorInfo getVibratorInfo() { + return mVibratorInfo; } /** @@ -184,43 +173,7 @@ public final class VibratorController { * @return true if this vibrator has this capability, false otherwise */ public boolean hasCapability(long capability) { - return (mCapabilities & capability) == capability; - } - - /** - * Check against this vibrator supported effects. - * - * @param effectIds list of effects, one of VibrationEffect.EFFECT_* - * @return one entry per requested effectId, with one of Vibrator.VIBRATION_EFFECT_SUPPORT_* - */ - public int[] areEffectsSupported(int[] effectIds) { - int[] supported = new int[effectIds.length]; - if (mSupportedEffects == null) { - Arrays.fill(supported, Vibrator.VIBRATION_EFFECT_SUPPORT_UNKNOWN); - } else { - for (int i = 0; i < effectIds.length; i++) { - supported[i] = mSupportedEffects.contains(effectIds[i]) - ? Vibrator.VIBRATION_EFFECT_SUPPORT_YES - : Vibrator.VIBRATION_EFFECT_SUPPORT_NO; - } - } - return supported; - } - - /** - * Check against this vibrator supported primitives. - * - * @param primitiveIds list of primitives, one of VibrationEffect.Composition.EFFECT_* - * @return one entry per requested primitiveId, with true if it is supported - */ - public boolean[] arePrimitivesSupported(int[] primitiveIds) { - boolean[] supported = new boolean[primitiveIds.length]; - if (mSupportedPrimitives != null && hasCapability(IVibrator.CAP_COMPOSE_EFFECTS)) { - for (int i = 0; i < primitiveIds.length; i++) { - supported[i] = mSupportedPrimitives.contains(primitiveIds[i]); - } - } - return supported; + return mVibratorInfo.hasCapability(capability); } /** Return {@code true} if the underlying vibrator is currently available, false otherwise. */ @@ -234,7 +187,7 @@ public final class VibratorController { *

This will affect the state of {@link #isUnderExternalControl()}. */ public void setExternalControl(boolean externalControl) { - if (!hasCapability(IVibrator.CAP_EXTERNAL_CONTROL)) { + if (!mVibratorInfo.hasCapability(IVibrator.CAP_EXTERNAL_CONTROL)) { return; } synchronized (mLock) { @@ -248,7 +201,7 @@ public final class VibratorController { * if given {@code effect} is {@code null}. */ public void updateAlwaysOn(int id, @Nullable VibrationEffect.Prebaked effect) { - if (!hasCapability(IVibrator.CAP_ALWAYS_ON_CONTROL)) { + if (!mVibratorInfo.hasCapability(IVibrator.CAP_ALWAYS_ON_CONTROL)) { return; } synchronized (mLock) { @@ -263,7 +216,7 @@ public final class VibratorController { /** Set the vibration amplitude. This will NOT affect the state of {@link #isVibrating()}. */ public void setAmplitude(int amplitude) { synchronized (mLock) { - if (hasCapability(IVibrator.CAP_AMPLITUDE_CONTROL)) { + if (mVibratorInfo.hasCapability(IVibrator.CAP_AMPLITUDE_CONTROL)) { mNativeWrapper.setAmplitude(amplitude); } } @@ -306,7 +259,7 @@ public final class VibratorController { *

This will affect the state of {@link #isVibrating()}. */ public void on(VibrationEffect.Composed effect, long vibrationId) { - if (!hasCapability(IVibrator.CAP_COMPOSE_EFFECTS)) { + if (!mVibratorInfo.hasCapability(IVibrator.CAP_COMPOSE_EFFECTS)) { return; } synchronized (mLock) { @@ -327,10 +280,7 @@ public final class VibratorController { @Override public String toString() { return "VibratorController{" - + "mVibratorId=" + mVibratorId - + ", mCapabilities=" + mCapabilities - + ", mSupportedEffects=" + mSupportedEffects - + ", mSupportedPrimitives=" + mSupportedPrimitives + + "mVibratorInfo=" + mVibratorInfo + ", mIsVibrating=" + mIsVibrating + ", mIsUnderExternalControl=" + mIsUnderExternalControl + ", mVibratorStateListeners count=" @@ -375,18 +325,6 @@ public final class VibratorController { } } - @Nullable - private static Set asSet(int[] values) { - if (values == null) { - return null; - } - HashSet set = new HashSet<>(); - for (int value : values) { - set.add(value); - } - return set; - } - /** Wrapper around the static-native methods of {@link VibratorController} for tests. */ @VisibleForTesting public static class NativeWrapper { diff --git a/services/tests/servicestests/src/com/android/server/VibratorManagerServiceTest.java b/services/tests/servicestests/src/com/android/server/VibratorManagerServiceTest.java index 9c58daf412f8c..726536db859ed 100644 --- a/services/tests/servicestests/src/com/android/server/VibratorManagerServiceTest.java +++ b/services/tests/servicestests/src/com/android/server/VibratorManagerServiceTest.java @@ -19,7 +19,10 @@ package com.android.server; import static com.android.server.testutils.TestUtils.assertExpectException; import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.eq; @@ -38,6 +41,8 @@ import android.os.PowerSaveState; import android.os.Process; import android.os.VibrationAttributes; import android.os.VibrationEffect; +import android.os.Vibrator; +import android.os.VibratorInfo; import android.os.test.TestLooper; import android.platform.test.annotations.Presubmit; @@ -134,17 +139,48 @@ public class VibratorManagerServiceTest { @Test public void getVibratorIds_withNonEmptyResultFromNative_returnsSameArray() { - mNativeWrappers.put(1, mockVibrator(0)); - mNativeWrappers.put(2, mockVibrator(0)); + mNativeWrappers.put(1, mockVibrator(/* capabilities= */ 0)); + mNativeWrappers.put(2, mockVibrator(/* capabilities= */ 0)); when(mNativeWrapperMock.getVibratorIds()).thenReturn(new int[]{2, 1}); assertArrayEquals(new int[]{2, 1}, createService().getVibratorIds()); } + @Test + public void getVibratorInfo_withMissingVibratorId_returnsNull() { + mockVibrators(mockVibrator(/* capabilities= */ 0)); + assertNull(createService().getVibratorInfo(2)); + } + + @Test + public void getVibratorInfo_withExistingVibratorId_returnsHalInfoForVibrator() { + VibratorController.NativeWrapper vibratorMock = mockVibrator( + IVibrator.CAP_COMPOSE_EFFECTS | IVibrator.CAP_AMPLITUDE_CONTROL); + when(vibratorMock.getSupportedEffects()).thenReturn( + new int[]{VibrationEffect.EFFECT_CLICK}); + when(vibratorMock.getSupportedPrimitives()).thenReturn( + new int[]{VibrationEffect.Composition.PRIMITIVE_CLICK}); + mNativeWrappers.put(1, vibratorMock); + when(mNativeWrapperMock.getVibratorIds()).thenReturn(new int[]{1}); + VibratorInfo info = createService().getVibratorInfo(1); + + assertNotNull(info); + assertEquals(1, info.getId()); + assertTrue(info.hasAmplitudeControl()); + assertTrue(info.hasCapability(IVibrator.CAP_COMPOSE_EFFECTS)); + assertFalse(info.hasCapability(IVibrator.CAP_ON_CALLBACK)); + assertEquals(Vibrator.VIBRATION_EFFECT_SUPPORT_YES, + info.isEffectSupported(VibrationEffect.EFFECT_CLICK)); + assertEquals(Vibrator.VIBRATION_EFFECT_SUPPORT_NO, + info.isEffectSupported(VibrationEffect.EFFECT_TICK)); + assertTrue(info.isPrimitiveSupported(VibrationEffect.Composition.PRIMITIVE_CLICK)); + assertFalse(info.isPrimitiveSupported(VibrationEffect.Composition.PRIMITIVE_TICK)); + } + @Test public void setAlwaysOnEffect_withMono_enablesAlwaysOnEffectToAllVibratorsWithCapability() { - VibratorController.NativeWrapper[] vibratorMocks = new VibratorController.NativeWrapper[] { + VibratorController.NativeWrapper[] vibratorMocks = new VibratorController.NativeWrapper[]{ mockVibrator(IVibrator.CAP_ALWAYS_ON_CONTROL), - mockVibrator(0), + mockVibrator(/* capabilities= */ 0), mockVibrator(IVibrator.CAP_ALWAYS_ON_CONTROL), }; mockVibrators(vibratorMocks); diff --git a/services/tests/servicestests/src/com/android/server/VibratorServiceTest.java b/services/tests/servicestests/src/com/android/server/VibratorServiceTest.java index 83ba621de48d4..faa2ba9cd2544 100644 --- a/services/tests/servicestests/src/com/android/server/VibratorServiceTest.java +++ b/services/tests/servicestests/src/com/android/server/VibratorServiceTest.java @@ -16,7 +16,6 @@ package com.android.server; -import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -62,6 +61,7 @@ import android.os.UserHandle; import android.os.VibrationAttributes; import android.os.VibrationEffect; import android.os.Vibrator; +import android.os.VibratorInfo; import android.os.test.TestLooper; import android.platform.test.annotations.Presubmit; import android.provider.Settings; @@ -235,56 +235,21 @@ public class VibratorServiceTest { } @Test - public void areEffectsSupported_withNullResultFromNative_returnsSupportUnknown() { - when(mNativeWrapperMock.getSupportedEffects()).thenReturn(null); - assertArrayEquals(new int[]{Vibrator.VIBRATION_EFFECT_SUPPORT_UNKNOWN}, - createService().areEffectsSupported(new int[]{VibrationEffect.EFFECT_CLICK})); - } - - @Test - public void areEffectsSupported_withSomeEffectsSupported_returnsSupportYesAndNoForEffects() { - int[] effects = new int[]{VibrationEffect.EFFECT_CLICK, VibrationEffect.EFFECT_TICK}; - + public void getVibratorInfo_returnsSameInfoFromNative() { + mockVibratorCapabilities(IVibrator.CAP_COMPOSE_EFFECTS | IVibrator.CAP_AMPLITUDE_CONTROL); when(mNativeWrapperMock.getSupportedEffects()) .thenReturn(new int[]{VibrationEffect.EFFECT_CLICK}); - assertArrayEquals( - new int[]{Vibrator.VIBRATION_EFFECT_SUPPORT_YES, - Vibrator.VIBRATION_EFFECT_SUPPORT_NO}, - createService().areEffectsSupported(effects)); - } - - @Test - public void arePrimitivesSupported_withoutComposeCapability_returnsAlwaysFalse() { - assertArrayEquals(new boolean[]{false, false}, - createService().arePrimitivesSupported(new int[]{ - VibrationEffect.Composition.PRIMITIVE_CLICK, - VibrationEffect.Composition.PRIMITIVE_TICK - })); - } - - @Test - public void arePrimitivesSupported_withNullResultFromNative_returnsAlwaysFalse() { - mockVibratorCapabilities(IVibrator.CAP_COMPOSE_EFFECTS); - when(mNativeWrapperMock.getSupportedPrimitives()).thenReturn(null); - - assertArrayEquals(new boolean[]{false, false}, - createService().arePrimitivesSupported(new int[]{ - VibrationEffect.Composition.PRIMITIVE_CLICK, - VibrationEffect.Composition.PRIMITIVE_QUICK_RISE - })); - } - - @Test - public void arePrimitivesSupported_withSomeSupportedPrimitives_returnsBasedOnNativeResult() { - mockVibratorCapabilities(IVibrator.CAP_COMPOSE_EFFECTS); when(mNativeWrapperMock.getSupportedPrimitives()) .thenReturn(new int[]{VibrationEffect.Composition.PRIMITIVE_CLICK}); - assertArrayEquals(new boolean[]{true, false}, - createService().arePrimitivesSupported(new int[]{ - VibrationEffect.Composition.PRIMITIVE_CLICK, - VibrationEffect.Composition.PRIMITIVE_QUICK_RISE - })); + VibratorInfo info = createService().getVibratorInfo(); + assertTrue(info.hasAmplitudeControl()); + assertEquals(Vibrator.VIBRATION_EFFECT_SUPPORT_YES, + info.isEffectSupported(VibrationEffect.EFFECT_CLICK)); + assertEquals(Vibrator.VIBRATION_EFFECT_SUPPORT_NO, + info.isEffectSupported(VibrationEffect.EFFECT_TICK)); + assertTrue(info.isPrimitiveSupported(VibrationEffect.Composition.PRIMITIVE_CLICK)); + assertFalse(info.isPrimitiveSupported(VibrationEffect.Composition.PRIMITIVE_TICK)); } @Test diff --git a/services/tests/servicestests/src/com/android/server/vibrator/VibratorControllerTest.java b/services/tests/servicestests/src/com/android/server/vibrator/VibratorControllerTest.java index 1f163bd3282b1..815aa8ee66aef 100644 --- a/services/tests/servicestests/src/com/android/server/vibrator/VibratorControllerTest.java +++ b/services/tests/servicestests/src/com/android/server/vibrator/VibratorControllerTest.java @@ -16,7 +16,6 @@ package com.android.server.vibrator; -import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -38,7 +37,6 @@ import android.hardware.vibrator.IVibrator; import android.os.IBinder; import android.os.IVibratorStateListener; import android.os.VibrationEffect; -import android.os.Vibrator; import android.os.test.TestLooper; import android.platform.test.annotations.Presubmit; @@ -100,7 +98,7 @@ public class VibratorControllerTest { public void createController_initializesNativeWrapper() { int vibratorId = 13; VibratorController controller = createController(vibratorId); - assertEquals(vibratorId, controller.getVibratorId()); + assertEquals(vibratorId, controller.getVibratorInfo().getId()); verify(mNativeWrapperMock).init(eq(vibratorId), notNull()); } @@ -131,59 +129,6 @@ public class VibratorControllerTest { assertFalse(createController().hasCapability(IVibrator.CAP_ON_CALLBACK)); } - @Test - public void areEffectsSupported_withNullResultFromNative_returnsSupportUnknown() { - when(mNativeWrapperMock.getSupportedEffects()).thenReturn(null); - assertArrayEquals(new int[]{Vibrator.VIBRATION_EFFECT_SUPPORT_UNKNOWN}, - createController().areEffectsSupported(new int[]{VibrationEffect.EFFECT_CLICK})); - } - - @Test - public void areEffectsSupported_withSomeEffectsSupported_returnsSupportYesAndNoForEffects() { - int[] effects = new int[]{VibrationEffect.EFFECT_CLICK, VibrationEffect.EFFECT_TICK}; - - when(mNativeWrapperMock.getSupportedEffects()) - .thenReturn(new int[]{VibrationEffect.EFFECT_CLICK}); - assertArrayEquals( - new int[]{Vibrator.VIBRATION_EFFECT_SUPPORT_YES, - Vibrator.VIBRATION_EFFECT_SUPPORT_NO}, - createController().areEffectsSupported(effects)); - } - - @Test - public void arePrimitivesSupported_withoutComposeCapability_returnsAlwaysFalse() { - assertArrayEquals(new boolean[]{false, false}, - createController().arePrimitivesSupported(new int[]{ - VibrationEffect.Composition.PRIMITIVE_CLICK, - VibrationEffect.Composition.PRIMITIVE_TICK - })); - } - - @Test - public void arePrimitivesSupported_withNullResultFromNative_returnsAlwaysFalse() { - mockVibratorCapabilities(IVibrator.CAP_COMPOSE_EFFECTS); - when(mNativeWrapperMock.getSupportedPrimitives()).thenReturn(null); - - assertArrayEquals(new boolean[]{false, false}, - createController().arePrimitivesSupported(new int[]{ - VibrationEffect.Composition.PRIMITIVE_CLICK, - VibrationEffect.Composition.PRIMITIVE_QUICK_RISE - })); - } - - @Test - public void arePrimitivesSupported_withSomeSupportedPrimitives_returnsBasedOnNativeResult() { - mockVibratorCapabilities(IVibrator.CAP_COMPOSE_EFFECTS); - when(mNativeWrapperMock.getSupportedPrimitives()) - .thenReturn(new int[]{VibrationEffect.Composition.PRIMITIVE_CLICK}); - - assertArrayEquals(new boolean[]{true, false}, - createController().arePrimitivesSupported(new int[]{ - VibrationEffect.Composition.PRIMITIVE_CLICK, - VibrationEffect.Composition.PRIMITIVE_QUICK_RISE - })); - } - @Test public void setExternalControl_withCapability_enablesExternalControl() { mockVibratorCapabilities(IVibrator.CAP_EXTERNAL_CONTROL);