Skip to content

Commit

Permalink
Make ShadowPendingIntent implement is___ methods of PendingIntent
Browse files Browse the repository at this point in the history
The methods isActivity, isBroadcast, isForegroundService, isService
and isImmutable were added in API level 31. They currently throw
NullPointerException when called in the simulated Robolectric
environment.

This change implements these methods using the pre-existing
is___Intent methods of ShadowPendingIntent.

PiperOrigin-RevId: 443407199
  • Loading branch information
Googler authored and Copybara-Service committed Apr 25, 2022
1 parent fe6501f commit 3041878
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
Expand Up @@ -517,6 +517,80 @@ public void cancel_shouldRemovePendingIntentForForegroundService() {
assertThat(PendingIntent.getForegroundService(context, 99, intent, FLAG_NO_CREATE)).isNull();
}

@Test
@Config(minSdk = Build.VERSION_CODES.S)
public void isActivity_activityPendingIntent_returnsTrue() {
Intent intent = new Intent();
assertThat(PendingIntent.getActivity(context, 99, intent, 100).isActivity()).isTrue();
}

@Test
@Config(minSdk = Build.VERSION_CODES.S)
public void isActivity_broadcastPendingIntent_returnsFalse() {
Intent intent = new Intent();
assertThat(PendingIntent.getBroadcast(context, 99, intent, 100).isActivity()).isFalse();
}

@Test
@Config(minSdk = Build.VERSION_CODES.S)
public void isBroadcast_broadcastPendingIntent_returnsTrue() {
Intent intent = new Intent();
assertThat(PendingIntent.getBroadcast(context, 99, intent, 100).isBroadcast()).isTrue();
}

@Test
@Config(minSdk = Build.VERSION_CODES.S)
public void isBroadcast_activityPendingIntent_returnsFalse() {
Intent intent = new Intent();
assertThat(PendingIntent.getActivity(context, 99, intent, 100).isBroadcast()).isFalse();
}

@Test
@Config(minSdk = Build.VERSION_CODES.S)
public void isForegroundService_foregroundServicePendingIntent_returnsTrue() {
Intent intent = new Intent();
assertThat(PendingIntent.getForegroundService(context, 99, intent, 100).isForegroundService())
.isTrue();
}

@Test
@Config(minSdk = Build.VERSION_CODES.S)
public void isForegroundService_normalServicePendingIntent_returnsFalse() {
Intent intent = new Intent();
assertThat(PendingIntent.getService(context, 99, intent, 100).isForegroundService()).isFalse();
}

@Test
@Config(minSdk = Build.VERSION_CODES.S)
public void isService_servicePendingIntent_returnsTrue() {
Intent intent = new Intent();
assertThat(PendingIntent.getService(context, 99, intent, 100).isService()).isTrue();
}

@Test
@Config(minSdk = Build.VERSION_CODES.S)
public void isService_foregroundServicePendingIntent_returnsFalse() {
Intent intent = new Intent();
assertThat(PendingIntent.getForegroundService(context, 99, intent, 100).isService()).isFalse();
}

@Test
@Config(minSdk = Build.VERSION_CODES.S)
public void isImmutable_pendingIntentWithImmutableFlag_returnsTrue() {
Intent intent = new Intent();
assertThat(
PendingIntent.getActivity(context, 99, intent, PendingIntent.FLAG_IMMUTABLE)
.isImmutable())
.isTrue();
}

@Test
@Config(minSdk = Build.VERSION_CODES.S)
public void isImmutable_pendingIntentWithoutImmutableFlag_returnsFalse() {
Intent intent = new Intent();
assertThat(PendingIntent.getActivity(context, 99, intent, 0).isActivity()).isTrue();
}

@Test
public void send_canceledPendingIntent_throwsCanceledException() throws CanceledException {
Intent intent = new Intent().setPackage("dummy.package");
Expand Down
Expand Up @@ -9,6 +9,7 @@
import static android.os.Build.VERSION_CODES.M;
import static android.os.Build.VERSION_CODES.N;
import static android.os.Build.VERSION_CODES.O;
import static android.os.Build.VERSION_CODES.S;
import static org.robolectric.util.reflector.Reflector.reflector;

import android.annotation.NonNull;
Expand Down Expand Up @@ -240,6 +241,31 @@ protected IntentSender getIntentSender() {
return new RoboIntentSender(realPendingIntent);
}

@Implementation(minSdk = S)
public boolean isActivity() {
return isActivityIntent();
}

@Implementation(minSdk = S)
public boolean isBroadcast() {
return isBroadcastIntent();
}

@Implementation(minSdk = S)
public boolean isForegroundService() {
return isForegroundServiceIntent();
}

@Implementation(minSdk = S)
public boolean isService() {
return isServiceIntent();
}

@Implementation(minSdk = S)
public boolean isImmutable() {
return (flags & FLAG_IMMUTABLE) > 0;
}

/**
* @return {@code true} iff sending this PendingIntent will start an activity
*/
Expand Down

0 comments on commit 3041878

Please sign in to comment.