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

Commit 4bcf419a authored by Lais Andrade's avatar Lais Andrade
Browse files

Fix VibrationEffect creation from parcel

Fix creators for concrete VibrationEffect types to skip the int token
used to identify the different implementation while reading from parcel.

Add unit tests to the individial CREATOR instances to make sure they're
able to create an instance when they're triggered by Bundle.getParcelable.

Fix: 356352552
Test: android.os.VibrationEffectTest
Flag: android.os.vibrator.vendor_vibration_effects
Change-Id: If3a42c8c49f045ce699c7d571ee53e278fcfad36
parent 3adb62b1
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -1024,6 +1024,7 @@ public abstract class VibrationEffect implements Parcelable {
                new Creator<Composed>() {
                    @Override
                    public Composed createFromParcel(Parcel in) {
                        in.readInt(); // Skip the parcel type token
                        return new Composed(in);
                    }

@@ -1298,6 +1299,7 @@ public abstract class VibrationEffect implements Parcelable {
                new Creator<VendorEffect>() {
                    @Override
                    public VendorEffect createFromParcel(Parcel in) {
                        in.readInt(); // Skip the parcel type token
                        return new VendorEffect(in);
                    }

+21 −0
Original line number Diff line number Diff line
@@ -1071,6 +1071,27 @@ public class VibrationEffectTest {
                .isHapticFeedbackCandidate());
    }

    @Test
    public void testParcelingComposed() {
        Parcel p = Parcel.obtain();
        VibrationEffect effect = VibrationEffect.createPredefined(VibrationEffect.EFFECT_CLICK);
        effect.writeToParcel(p, 0);
        p.setDataPosition(0);
        VibrationEffect parceledEffect = VibrationEffect.Composed.CREATOR.createFromParcel(p);
        assertThat(parceledEffect).isEqualTo(effect);
    }

    @Test
    @RequiresFlagsEnabled(android.os.vibrator.Flags.FLAG_VENDOR_VIBRATION_EFFECTS)
    public void testParcelingVendorEffect() {
        Parcel p = Parcel.obtain();
        VibrationEffect effect = VibrationEffect.createVendorEffect(createNonEmptyBundle());
        effect.writeToParcel(p, 0);
        p.setDataPosition(0);
        VibrationEffect parceledEffect = VibrationEffect.VendorEffect.CREATOR.createFromParcel(p);
        assertThat(parceledEffect).isEqualTo(effect);
    }

    private void assertArrayEq(long[] expected, long[] actual) {
        assertTrue(
                String.format("Expected pattern %s, but was %s",