Fix ExternalVibrationTest for parcelable serialization

Use real instances of IBinder to create an IExternalVibrationController
before testing the writeToParcel/createFromParcel methods, as they rely
on the implementation of Parcel.writeStrongBinder that doesn't work on
mocks.

Also update the tests to assert on all fields from the restored
ExternalVibration, since the equals implementation only relies on the
client token.

Fix: 291713224
Test: ExternalVibrationTest
Change-Id: Ia36cd0cc4dfcabf16af9023fcb266fdcbff21fb0
This commit is contained in:
Lais Andrade
2023-07-25 18:28:59 +01:00
parent 19dae7cd51
commit b77103f756
2 changed files with 27 additions and 18 deletions

View File

@@ -23,6 +23,8 @@ import android.util.Slog;
import com.android.internal.util.Preconditions;
import java.util.NoSuchElementException;
/**
* An ExternalVibration represents an on-going vibration being controlled by something other than
* the core vibrator service.
@@ -149,7 +151,7 @@ public class ExternalVibration implements Parcelable {
try {
mToken.linkToDeath(recipient, 0);
} catch (RemoteException e) {
return;
Slog.wtf(TAG, "Failed to link to token death: " + this, e);
}
}
@@ -157,7 +159,11 @@ public class ExternalVibration implements Parcelable {
* Unlinks a recipient to death against this external vibration token
*/
public void unlinkToDeath(IBinder.DeathRecipient recipient) {
mToken.unlinkToDeath(recipient, 0);
try {
mToken.unlinkToDeath(recipient, 0);
} catch (NoSuchElementException e) {
Slog.wtf(TAG, "Failed to unlink to token death", e);
}
}
@Override
@@ -184,12 +190,12 @@ public class ExternalVibration implements Parcelable {
public void writeToParcel(Parcel out, int flags) {
out.writeInt(mUid);
out.writeString(mPkg);
writeAudioAttributes(mAttrs, out, flags);
writeAudioAttributes(mAttrs, out);
out.writeStrongBinder(mController.asBinder());
out.writeStrongBinder(mToken);
}
private static void writeAudioAttributes(AudioAttributes attrs, Parcel out, int flags) {
private static void writeAudioAttributes(AudioAttributes attrs, Parcel out) {
out.writeInt(attrs.getUsage());
out.writeInt(attrs.getContentType());
out.writeInt(attrs.getCapturePreset());

View File

@@ -16,34 +16,37 @@
package android.os;
import static junit.framework.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static com.google.common.truth.Truth.assertThat;
import android.media.AudioAttributes;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import org.junit.runners.JUnit4;
@RunWith(MockitoJUnitRunner.class)
@RunWith(JUnit4.class)
public class ExternalVibrationTest {
@Ignore("b/291713224")
@Test
public void testSerialization() {
AudioAttributes audio = new AudioAttributes.Builder().build();
IExternalVibrationController controller = mock(IExternalVibrationController.class);
ExternalVibration original = new ExternalVibration(
123, // uid
/* uid= */ 123,
"pkg",
audio,
controller);
new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setFlags(AudioAttributes.FLAG_BYPASS_MUTE)
.build(),
IExternalVibrationController.Stub.asInterface(new Binder()));
Parcel p = Parcel.obtain();
original.writeToParcel(p, 0);
p.setDataPosition(0);
ExternalVibration restored = ExternalVibration.CREATOR.createFromParcel(p);
assertEquals(original, restored);
assertThat(restored).isEqualTo(original);
// ExternalVibration.equals relies on the binder token only, check other attributes as well
assertThat(restored.getUid()).isEqualTo(original.getUid());
assertThat(restored.getPackage()).isEqualTo(original.getPackage());
assertThat(restored.getAudioAttributes()).isEqualTo(original.getAudioAttributes());
assertThat(restored.getToken()).isEqualTo(original.getToken());
}
}