Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8df49bca authored by Wilson Wu's avatar Wilson Wu
Browse files

Clean up flag override mechanisms for vibrator tests

For scroll_feedback_api testing, replace ViewFeatureFlag
with the SetFlagsRule test rule and remove the ViewFeatureFlag
argument from HapticFeedbackVibrationProvider.

Bug: 305618021
Test: atest VibratorManagerServiceTest
Test: atest HapticFeedbackVibrationProviderTest
Change-Id: I95a8ee79520e8bdffb2f2fb4f4d371f53decd8a7
parent 557d3096
Loading
Loading
Loading
Loading
+5 −13
Original line number Diff line number Diff line
@@ -26,8 +26,6 @@ import android.os.vibrator.Flags;
import android.util.Slog;
import android.util.SparseArray;
import android.view.HapticFeedbackConstants;
import android.view.flags.FeatureFlags;
import android.view.flags.FeatureFlagsImpl;

import com.android.internal.annotations.VisibleForTesting;

@@ -57,7 +55,6 @@ public final class HapticFeedbackVibrationProvider {
    // If present and valid, a vibration here will be used for an effect.
    // Otherwise, the system's default vibration will be used.
    @Nullable private final SparseArray<VibrationEffect> mHapticCustomizations;
    private final FeatureFlags mViewFeatureFlags;

    private float mKeyboardVibrationFixedAmplitude;

@@ -68,16 +65,14 @@ public final class HapticFeedbackVibrationProvider {

    /** @hide */
    public HapticFeedbackVibrationProvider(Resources res, VibratorInfo vibratorInfo) {
        this(res, vibratorInfo, loadHapticCustomizations(res, vibratorInfo),
                new FeatureFlagsImpl());
        this(res, vibratorInfo, loadHapticCustomizations(res, vibratorInfo));
    }

    /** @hide */
    @VisibleForTesting HapticFeedbackVibrationProvider(
            Resources res,
            VibratorInfo vibratorInfo,
            @Nullable SparseArray<VibrationEffect> hapticCustomizations,
            FeatureFlags viewFeatureFlags) {
            @Nullable SparseArray<VibrationEffect> hapticCustomizations) {
        mVibratorInfo = vibratorInfo;
        mHapticTextHandleEnabled = res.getBoolean(
                com.android.internal.R.bool.config_enableHapticTextHandle);
@@ -86,8 +81,6 @@ public final class HapticFeedbackVibrationProvider {
            hapticCustomizations = null;
        }
        mHapticCustomizations = hapticCustomizations;
        mViewFeatureFlags = viewFeatureFlags;

        mSafeModeEnabledVibrationEffect =
                effectHasCustomization(HapticFeedbackConstants.SAFE_MODE_ENABLED)
                        ? mHapticCustomizations.get(HapticFeedbackConstants.SAFE_MODE_ENABLED)
@@ -226,7 +219,7 @@ public final class HapticFeedbackVibrationProvider {
        if (bypassVibrationIntensitySetting) {
            flags |= VibrationAttributes.FLAG_BYPASS_USER_VIBRATION_INTENSITY_OFF;
        }
        if (shouldBypassInterruptionPolicy(effectId, mViewFeatureFlags)) {
        if (shouldBypassInterruptionPolicy(effectId)) {
            flags |= VibrationAttributes.FLAG_BYPASS_INTERRUPTION_POLICY;
        }
        if (shouldBypassIntensityScale(effectId)) {
@@ -381,8 +374,7 @@ public final class HapticFeedbackVibrationProvider {
        }
    }

    private static boolean shouldBypassInterruptionPolicy(
            int effectId, FeatureFlags viewFeatureFlags) {
    private static boolean shouldBypassInterruptionPolicy(int effectId) {
        switch (effectId) {
            case HapticFeedbackConstants.SCROLL_TICK:
            case HapticFeedbackConstants.SCROLL_ITEM_FOCUS:
@@ -390,7 +382,7 @@ public final class HapticFeedbackVibrationProvider {
                // The SCROLL_* constants should bypass interruption filter, so that scroll haptics
                // can play regardless of focus modes like DND. Guard this behavior by the feature
                // flag controlling the general scroll feedback APIs.
                return viewFeatureFlags.scrollFeedbackApi();
                return android.view.flags.Flags.scrollFeedbackApi();
            default:
                return false;
        }
+3 −8
Original line number Diff line number Diff line
@@ -50,7 +50,6 @@ import android.os.vibrator.Flags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.util.AtomicFile;
import android.util.SparseArray;
import android.view.flags.FeatureFlags;

import androidx.test.InstrumentationRegistry;

@@ -87,9 +86,6 @@ public class HapticFeedbackVibrationProviderTest {

    @Mock private Resources mResourcesMock;

    // TODO(305618021): Clean up the FeatureFlags with SetFlagsRule
    @Mock private FeatureFlags mViewFeatureFlags;

    @Test
    public void testNonExistentCustomization_useDefault() throws Exception {
        // No customization file is set.
@@ -306,7 +302,7 @@ public class HapticFeedbackVibrationProviderTest {

    @Test
    public void testVibrationAttribute_scrollFeedback_scrollApiFlagOn_bypassInterruptPolicy() {
        when(mViewFeatureFlags.scrollFeedbackApi()).thenReturn(true);
        mSetFlagsRule.enableFlags(android.view.flags.Flags.FLAG_SCROLL_FEEDBACK_API);
        HapticFeedbackVibrationProvider hapticProvider = createProviderWithDefaultCustomizations();

        for (int effectId : SCROLL_FEEDBACK_CONSTANTS) {
@@ -319,7 +315,7 @@ public class HapticFeedbackVibrationProviderTest {

    @Test
    public void testVibrationAttribute_scrollFeedback_scrollApiFlagOff_noBypassInterruptPolicy() {
        when(mViewFeatureFlags.scrollFeedbackApi()).thenReturn(false);
        mSetFlagsRule.disableFlags(android.view.flags.Flags.FLAG_SCROLL_FEEDBACK_API);
        HapticFeedbackVibrationProvider hapticProvider = createProviderWithDefaultCustomizations();

        for (int effectId : SCROLL_FEEDBACK_CONSTANTS) {
@@ -394,8 +390,7 @@ public class HapticFeedbackVibrationProviderTest {

    private HapticFeedbackVibrationProvider createProvider(
            SparseArray<VibrationEffect> customizations) {
        return new HapticFeedbackVibrationProvider(
            mResourcesMock, mVibratorInfo, customizations, mViewFeatureFlags);
        return new HapticFeedbackVibrationProvider(mResourcesMock, mVibratorInfo, customizations);
    }

    private void mockVibratorPrimitiveSupport(int... supportedPrimitives) {
+6 −6
Original line number Diff line number Diff line
@@ -82,6 +82,7 @@ import android.os.vibrator.PrimitiveSegment;
import android.os.vibrator.StepSegment;
import android.os.vibrator.VibrationConfig;
import android.os.vibrator.VibrationEffectSegment;
import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.Settings;
import android.util.ArraySet;
import android.util.SparseArray;
@@ -89,7 +90,7 @@ import android.util.SparseBooleanArray;
import android.view.Display;
import android.view.HapticFeedbackConstants;
import android.view.InputDevice;
import android.view.flags.FeatureFlags;
import android.view.flags.Flags;

import androidx.test.InstrumentationRegistry;
import androidx.test.filters.FlakyTest;
@@ -155,6 +156,8 @@ public class VibratorManagerServiceTest {
    @Rule
    public FakeSettingsProviderRule mSettingsProviderRule = FakeSettingsProvider.rule();

    @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();

    @Mock
    private VibratorManagerService.NativeWrapper mNativeWrapperMock;
    @Mock
@@ -175,8 +178,6 @@ public class VibratorManagerServiceTest {
    private VirtualDeviceManagerInternal mVirtualDeviceManagerInternalMock;
    @Mock
    private AudioManager mAudioManagerMock;
    @Mock
    private FeatureFlags mViewFeatureFlags;

    private final Map<Integer, FakeVibratorControllerProvider> mVibratorProviders = new HashMap<>();

@@ -326,8 +327,7 @@ public class VibratorManagerServiceTest {
                    HapticFeedbackVibrationProvider createHapticFeedbackVibrationProvider(
                            Resources resources, VibratorInfo vibratorInfo) {
                        return new HapticFeedbackVibrationProvider(
                                resources, vibratorInfo, mHapticFeedbackVibrationMap,
                                mViewFeatureFlags);
                                resources, vibratorInfo, mHapticFeedbackVibrationMap);
                    }
                });
        return mService;
@@ -1354,7 +1354,7 @@ public class VibratorManagerServiceTest {
        denyPermission(android.Manifest.permission.MODIFY_PHONE_STATE);
        denyPermission(android.Manifest.permission.MODIFY_AUDIO_ROUTING);
        // Flag override to enable the scroll feedack constants to bypass interruption policies.
        when(mViewFeatureFlags.scrollFeedbackApi()).thenReturn(true);
        mSetFlagsRule.enableFlags(Flags.FLAG_SCROLL_FEEDBACK_API);
        mHapticFeedbackVibrationMap.put(
                HapticFeedbackConstants.SCROLL_TICK,
                VibrationEffect.createPredefined(VibrationEffect.EFFECT_CLICK));