Class ShadowBinder

java.lang.Object
org.robolectric.shadows.ShadowBinder

@Implements(android.os.Binder.class) public class ShadowBinder extends Object
  • Constructor Details

    • ShadowBinder

      public ShadowBinder()
  • Method Details

    • transact

      @Implementation protected boolean transact(int code, Parcel data, Parcel reply, int flags) throws RemoteException
      Throws:
      RemoteException
    • linkToDeath

      @Implementation protected void linkToDeath(IBinder.DeathRecipient deathRecipient, int flags)
    • unlinkToDeath

      @Implementation protected boolean unlinkToDeath(IBinder.DeathRecipient deathRecipient, int flags)
    • getCallingPid

      @Implementation protected static int getCallingPid()
    • getCallingUid

      @Implementation protected static int getCallingUid()
    • getCallingUidOrThrow

      @Implementation(minSdk=29) protected static int getCallingUidOrThrow()
      See Binder.getCallingUidOrThrow(). Whether or not this returns a value is controlled by setCallingUid(int) (to set the value to be returned) or by reset() (to trigger the exception).
      Returns:
      the value set by setCallingUid(int)
      Throws:
      IllegalStateException - if no UID has been set
    • getCallingUserHandle

      @Implementation protected static UserHandle getCallingUserHandle()
    • getDeathRecipients

      public List<IBinder.DeathRecipient> getDeathRecipients()
    • setCallingPid

      public static void setCallingPid(int pid)
      Configures Binder.getCallingPid() to return the specified Linux PID to subsequent callers on *any* thread, for testing purposes.

      Warning: This is a global setting affecting all threads in your test process (very different than real Android). It can cause flakiness or unexpected behavior in multi-threaded tests. Prefer to configure your simulated Binder IPC callers one at a time using setCallingIdentityForCurrentThread(CallingIdentity) instead.

    • setCallingUid

      public static void setCallingUid(int uid)
      Configures Binder.getCallingUid() to return the specified Linux UID to subsequent callers on *any* thread, for testing purposes.

      Warning: This is a global setting affecting all threads in your test process (very different than real Android). It can cause flakiness or unexpected behavior in multi-threaded tests. Prefer to configure your simulated Binder IPC callers one at a time using setCallingIdentityForCurrentThread(CallingIdentity) instead.

    • setCallingUserHandle

      public static void setCallingUserHandle(UserHandle userHandle)
      Configures Binder.getCallingUserHandle() to return the specified UserHandle to subsequent callers on *any* thread, for testing purposes.

      Warning: This is a global setting affecting all threads in your test process (very different than real Android). It can cause flakiness or unexpected behavior in multi-threaded tests. Prefer to configure your simulated Binder IPC callers one at a time using setCallingIdentityForCurrentThread(CallingIdentity) instead.

    • setCallingIdentityForCurrentThread

      @Nullable public static ShadowBinder.CallingIdentity setCallingIdentityForCurrentThread(@Nullable ShadowBinder.CallingIdentity identity)
      Configures all the android.os.Binder#getCallingXXX methods to reflect the specified ShadowBinder.CallingIdentity when called from the current thread, for testing purposes.

      Use this method to prepare the current thread to deliver a Binder IPC transaction under a fake remote identity. You can configure this thread's apparent UID, PID and UserHandle. Call this method again (unconditionally, in a finally block, say) after the transaction completes to restore this thread's previous IPC identity (or clear it).

      Parameters:
      identity - the identity to set, or null to clear
      Returns:
      this thread's previous fake identity, or null if none was set.
    • reset

      @Resetter public static void reset()