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

Commit d7d0aa79 authored by Juan Sebastian Martinez's avatar Juan Sebastian Martinez
Browse files

Testing deliverOnProgressChangedHaptics in VolumeRow

The method now returns true if it can deliver haptics when there is a
progress change and false otherwise. This simplifies the tests related
to the method and avoids flakiness introduced from animator rules and
complicated handling of the dialog's runnables.

Test: SystemUITests:VolumeDialogImplTest
Flag: NONE
Bug: 329099861
Bug: 338979732
Change-Id: I7f07c5f240c9e374509309753a94ede92fb6f330
parent 321860fa
Loading
Loading
Loading
Loading
+17 −13
Original line number Original line Diff line number Diff line
@@ -174,9 +174,6 @@ public class VolumeDialogImpl implements VolumeDialog, Dumpable,
    private static final String TYPE_DISMISS = "dismiss";
    private static final String TYPE_DISMISS = "dismiss";
    /** Volume dialog slider animation. */
    /** Volume dialog slider animation. */
    private static final String TYPE_UPDATE = "update";
    private static final String TYPE_UPDATE = "update";
    static final int PROGRESS_HAPTICS_DISABLED = 0;
    static final int PROGRESS_HAPTICS_EAGER = 1;
    static final int PROGRESS_HAPTICS_ANIMATED = 2;


    /**
    /**
     *  TODO(b/290612381): remove lingering animations or tolerate them
     *  TODO(b/290612381): remove lingering animations or tolerate them
@@ -2112,7 +2109,7 @@ public class VolumeDialogImpl implements VolumeDialog, Dumpable,
                    row.anim.setIntValues(progress, newProgress);
                    row.anim.setIntValues(progress, newProgress);
                    // The animator can't keep up with the volume changes so haptics need to be
                    // The animator can't keep up with the volume changes so haptics need to be
                    // triggered here. This happens when the volume keys are continuously pressed.
                    // triggered here. This happens when the volume keys are continuously pressed.
                    row.deliverOnProgressChangedHaptics(false, newProgress, PROGRESS_HAPTICS_EAGER);
                    row.deliverOnProgressChangedHaptics(false, newProgress);
                }
                }
                row.animTargetProgress = newProgress;
                row.animTargetProgress = newProgress;
                row.anim.setDuration(UPDATE_ANIMATION_DURATION);
                row.anim.setDuration(UPDATE_ANIMATION_DURATION);
@@ -2127,13 +2124,14 @@ public class VolumeDialogImpl implements VolumeDialog, Dumpable,
        }
        }
    }
    }


    @VisibleForTesting int progressHapticsForStream(int stream) {
    @VisibleForTesting
    boolean canDeliverProgressHapticsToStream(int stream, boolean fromUser, int progress) {
        for (VolumeRow row: mRows) {
        for (VolumeRow row: mRows) {
            if (row.stream == stream) {
            if (row.stream == stream) {
                return row.mProgressHapticsType;
                return row.deliverOnProgressChangedHaptics(fromUser, progress);
            }
            }
        }
        }
        return PROGRESS_HAPTICS_DISABLED;
        return false;
    }
    }


    private void recheckH(VolumeRow row) {
    private void recheckH(VolumeRow row) {
@@ -2527,8 +2525,7 @@ public class VolumeDialogImpl implements VolumeDialog, Dumpable,
                if (fromUser || mRow.animTargetProgress == progress) {
                if (fromUser || mRow.animTargetProgress == progress) {
                    // Deliver user-generated slider haptics immediately, or when the animation
                    // Deliver user-generated slider haptics immediately, or when the animation
                    // completes
                    // completes
                    mRow.deliverOnProgressChangedHaptics(
                    mRow.deliverOnProgressChangedHaptics(fromUser, progress);
                            fromUser, progress, PROGRESS_HAPTICS_ANIMATED);
                }
                }
            }
            }
            if (D.BUG) Log.d(TAG, AudioSystem.streamToString(mRow.stream)
            if (D.BUG) Log.d(TAG, AudioSystem.streamToString(mRow.stream)
@@ -2641,7 +2638,6 @@ public class VolumeDialogImpl implements VolumeDialog, Dumpable,
        private int animTargetProgress;
        private int animTargetProgress;
        private int lastAudibleLevel = 1;
        private int lastAudibleLevel = 1;
        private SeekbarHapticPlugin mHapticPlugin;
        private SeekbarHapticPlugin mHapticPlugin;
        private int mProgressHapticsType = PROGRESS_HAPTICS_DISABLED;


        void setIcon(int iconRes, Resources.Theme theme) {
        void setIcon(int iconRes, Resources.Theme theme) {
            if (icon != null) {
            if (icon != null) {
@@ -2683,15 +2679,23 @@ public class VolumeDialogImpl implements VolumeDialog, Dumpable,
            slider.setOnTouchListener(null);
            slider.setOnTouchListener(null);
        }
        }


        void deliverOnProgressChangedHaptics(boolean fromUser, int progress, int hapticsType) {
        /**
            if (mHapticPlugin == null) return;
         * Deliver haptics when the progress of the slider has changed.
         *
         * @param fromUser True if the progress changed was caused by the user.
         * @param progress The progress value of the slider.
         * @return True if haptics were successfully delivered. False otherwise. This will happen
         *   if mHapticPlugin is null
         */
        boolean deliverOnProgressChangedHaptics(boolean fromUser, int progress) {
            if (mHapticPlugin == null) return false;


            mHapticPlugin.onProgressChanged(slider, progress, fromUser);
            mHapticPlugin.onProgressChanged(slider, progress, fromUser);
            if (!fromUser) {
            if (!fromUser) {
                // Consider a change from program as the volume key being continuously pressed
                // Consider a change from program as the volume key being continuously pressed
                mHapticPlugin.onKeyDown();
                mHapticPlugin.onKeyDown();
            }
            }
            mProgressHapticsType = hapticsType;
            return true;
        }
        }
    }
    }


+16 −41
Original line number Original line Diff line number Diff line
@@ -65,7 +65,6 @@ import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.SeekBar;


import androidx.test.core.view.MotionEventBuilder;
import androidx.test.core.view.MotionEventBuilder;
import androidx.test.filters.FlakyTest;
import androidx.test.filters.SmallTest;
import androidx.test.filters.SmallTest;


import com.android.internal.jank.InteractionJankMonitor;
import com.android.internal.jank.InteractionJankMonitor;
@@ -273,54 +272,30 @@ public class VolumeDialogImplTest extends SysuiTestCase {


    @Test
    @Test
    @DisableFlags(FLAG_HAPTIC_VOLUME_SLIDER)
    @DisableFlags(FLAG_HAPTIC_VOLUME_SLIDER)
    public void testVolumeChange_noSliderHaptics_doesNotDeliverOnProgressChangedHaptics() {
    public void addSliderHaptics_withHapticsDisabled_doesNotDeliverOnProgressChangedHaptics() {
        final State shellState = createShellState();
        // GIVEN that the slider haptics flag is disabled and we try to add haptics to volume rows
        VolumeDialogController.StreamState musicStreamState =
        mDialog.addSliderHapticsToRows();
                shellState.states.get(AudioSystem.STREAM_MUSIC);

        mDialog.show(SHOW_REASON_UNKNOWN);
        mTestableLooper.processMessages(1); //Only the SHOW message
        mDialog.removeDismissMessages(); // Temporarily remove the rescheduled DISMISS

        // Change the volume two times
        musicStreamState.level += 10;
        mDialog.onStateChangedH(shellState);
        musicStreamState.level += 10;
        mDialog.onStateChangedH(shellState);


        // expected: the type of the latest progress haptics for the stream should be DISABLED
        // WHEN haptics try to be delivered to a volume stream
        int type = mDialog.progressHapticsForStream(AudioSystem.STREAM_MUSIC);
        boolean canDeliverHaptics =
        assertEquals(VolumeDialogImpl.PROGRESS_HAPTICS_DISABLED, type);
                mDialog.canDeliverProgressHapticsToStream(AudioSystem.STREAM_MUSIC, true, 50);


        mDialog.dismiss(DISMISS_REASON_UNKNOWN); // Dismiss
        // THEN the result is that haptics are not successfully delivered
        mTestableLooper.processAllMessages();
        assertFalse(canDeliverHaptics);
    }
    }


    @Test @FlakyTest(bugId = 329099861)
    @Test
    @EnableFlags(FLAG_HAPTIC_VOLUME_SLIDER)
    @EnableFlags(FLAG_HAPTIC_VOLUME_SLIDER)
    public void testVolumeChange_withSliderHaptics_deliversOnProgressChangedHapticsEagerly() {
    public void addSliderHaptics_withHapticsEnabled_canDeliverOnProgressChangedHaptics() {
        // create haptic plugins on the rows with the flag enabled
        // GIVEN that the slider haptics flag is enabled and we try to add haptics to volume rows
        mDialog.addSliderHapticsToRows();
        mDialog.addSliderHapticsToRows();
        final State shellState = createShellState();
        VolumeDialogController.StreamState musicStreamState =
                shellState.states.get(AudioSystem.STREAM_MUSIC);

        mDialog.show(SHOW_REASON_UNKNOWN);
        mTestableLooper.processMessages(1); //Only the SHOW message
        mDialog.removeDismissMessages(); // Temporarily remove the rescheduled DISMISS


        // Change the volume two times
        // WHEN haptics try to be delivered to a volume stream
        musicStreamState.level += 10;
        boolean canDeliverHaptics =
        mDialog.onStateChangedH(shellState);
                mDialog.canDeliverProgressHapticsToStream(AudioSystem.STREAM_MUSIC, true, 50);
        musicStreamState.level += 10;
        mDialog.onStateChangedH(shellState);


        // expected: the type of the latest progress haptics for the stream should be EAGER
        // THEN the result is that haptics are successfully delivered
        int type = mDialog.progressHapticsForStream(AudioSystem.STREAM_MUSIC);
        assertTrue(canDeliverHaptics);
        assertEquals(VolumeDialogImpl.PROGRESS_HAPTICS_EAGER, type);

        mDialog.dismiss(DISMISS_REASON_UNKNOWN); // Dismiss
        mTestableLooper.processAllMessages();
    }
    }


    @Test
    @Test