public PendingIntent getErrorPendingIntent(int errorCode, Context context, int requestCode) { if (errorCode == ConnectionResult.SUCCESS) { return null; } return PendingIntent.getActivity( context, requestCode, new Intent(), PendingIntent.FLAG_CANCEL_CURRENT); }
public static PendingIntent createPendingIntent(Context context, String referenceKey) { setEnabledBlocking(context, DisplayLeakActivity.class, true); Intent intent = new Intent(context, DisplayLeakActivity.class); intent.putExtra(SHOW_LEAK_EXTRA, referenceKey); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); return PendingIntent.getActivity(context, 1, intent, FLAG_UPDATE_CURRENT); }
public static PendingIntent createPendingIntent(Context context) { setEnabledBlocking(context, RequestStoragePermissionActivity.class, true); Intent intent = new Intent(context, RequestStoragePermissionActivity.class); intent.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TOP); return PendingIntent.getActivity(context, 1, intent, FLAG_UPDATE_CURRENT); }
@Test public void getActivity_withFlagNoCreate_shouldReturnNullIfRequestCodeIsUnmatched() { Intent intent = new Intent(); PendingIntent.getActivity(context, 99, intent, 0); assertThat(PendingIntent.getActivity(context, 98, intent, FLAG_NO_CREATE)).isNull(); }
@Test public void getActivity_withFlagNoCreate_shouldReturnNullIfNoPendingIntentExists() { Intent intent = new Intent(); assertThat(PendingIntent.getActivity(context, 99, intent, FLAG_NO_CREATE)).isNull(); }
@Test public void cancel_shouldRemovePendingIntentForActivity() { Intent intent = new Intent(); PendingIntent pendingIntent = PendingIntent.getActivity(context, 99, intent, 100); assertThat(pendingIntent).isNotNull(); pendingIntent.cancel(); assertThat(PendingIntent.getActivity(context, 99, intent, FLAG_NO_CREATE)).isNull(); }
@Test public void set_shouldReplaceAlarmsWithSameIntentReceiver() { alarmManager.set(AlarmManager.ELAPSED_REALTIME, 500, PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()), 0)); alarmManager.set(AlarmManager.ELAPSED_REALTIME, 1000, PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()), 0)); assertThat(shadowAlarmManager.getScheduledAlarms()).hasSize(1); }
@Test public void set_shouldReplaceDuplicates() { alarmManager.set(AlarmManager.ELAPSED_REALTIME, 0, PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()), 0)); alarmManager.set(AlarmManager.ELAPSED_REALTIME, 0, PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()), 0)); assertThat(shadowAlarmManager.getScheduledAlarms()).hasSize(1); }
@Test public void setRepeating_shouldReplaceDuplicates() { alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, 0, INTERVAL_HOUR, PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()), 0)); alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, 0, INTERVAL_HOUR, PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()), 0)); assertThat(shadowAlarmManager.getScheduledAlarms()).hasSize(1); }
@Test public void getActivity_withNoFlags_shouldReturnExistingIntent() { Intent intent = new Intent(); PendingIntent.getActivity(context, 99, intent, 100); Intent updated = new Intent(); PendingIntent saved = PendingIntent.getActivity(context, 99, updated, 0); assertThat(saved).isNotNull(); assertThat(intent).isSameAs(shadowOf(saved).getSavedIntent()); }
@Test public void testHashCode() { Context ctx = ApplicationProvider.getApplicationContext(); PendingIntent pendingIntent1 = PendingIntent.getActivity(ctx, 99, new Intent("activity"), 100); assertThat(pendingIntent1.hashCode()) .isEqualTo(PendingIntent.getActivity(ctx, 99, new Intent("activity"), 100).hashCode()); assertThat(pendingIntent1.hashCode()) .isNotEqualTo(PendingIntent.getActivity(ctx, 99, new Intent("activity2"), 100).hashCode()); assertThat(pendingIntent1.hashCode()) .isNotEqualTo(PendingIntent.getActivity(ctx, 999, new Intent("activity"), 100).hashCode()); } }
@Test @Config(minSdk = Build.VERSION_CODES.JELLY_BEAN_MR1) public void testGetCreatorPackage_nothingSet() { PendingIntent pendingIntent = PendingIntent.getActivity(context, 99, new Intent("activity"), 100); assertThat(pendingIntent.getCreatorPackage()).isEqualTo(context.getPackageName()); assertThat(pendingIntent.getTargetPackage()).isEqualTo(context.getPackageName()); }
@Test @Config(minSdk = M) public void setAndAllowWhileIdle_shouldRegisterAlarm() { assertThat(shadowAlarmManager.getNextScheduledAlarm()).isNull(); alarmManager.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME, 0, PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()), 0)); assertThat(shadowAlarmManager.getNextScheduledAlarm()).isNotNull(); }
@Test @Config(minSdk = KITKAT) public void setWindow_shouldRegisterAlarm_forApi19() { assertThat(shadowAlarmManager.getNextScheduledAlarm()).isNull(); alarmManager.setWindow(AlarmManager.ELAPSED_REALTIME, 0, 1, PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()), 0)); assertThat(shadowAlarmManager.getNextScheduledAlarm()).isNotNull(); }
@Test @Config(minSdk = M) public void setExactAndAllowWhileIdle_shouldRegisterAlarm() { assertThat(shadowAlarmManager.getNextScheduledAlarm()).isNull(); alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME, 0, PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()), 0)); assertThat(shadowAlarmManager.getNextScheduledAlarm()).isNotNull(); }
@Test @Config(minSdk = KITKAT) public void setExact_shouldRegisterAlarm_forApi19() { assertThat(shadowAlarmManager.getNextScheduledAlarm()).isNull(); alarmManager.setExact(AlarmManager.ELAPSED_REALTIME, 0, PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()), 0)); assertThat(shadowAlarmManager.getNextScheduledAlarm()).isNotNull(); }
@Test public void setRepeating_shouldRegisterAlarm() { assertThat(shadowAlarmManager.getNextScheduledAlarm()).isNull(); alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, 0, INTERVAL_HOUR, PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()), 0)); assertThat(shadowAlarmManager.getNextScheduledAlarm()).isNotNull(); }
@Test public void set_shouldRegisterAlarm() { assertThat(shadowAlarmManager.getNextScheduledAlarm()).isNull(); alarmManager.set(AlarmManager.ELAPSED_REALTIME, 0, PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()), 0)); assertThat(shadowAlarmManager.getNextScheduledAlarm()).isNotNull(); }
@Test public void getNextScheduledAlarm_shouldReturnRepeatingAlarms() { assertThat(shadowAlarmManager.getNextScheduledAlarm()).isNull(); long now = new Date().getTime(); Intent intent = new Intent(activity, activity.getClass()); PendingIntent pendingIntent = PendingIntent.getActivity(activity, 0, intent, 0); alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, now, INTERVAL_HOUR, pendingIntent); ShadowAlarmManager.ScheduledAlarm scheduledAlarm = shadowAlarmManager.getNextScheduledAlarm(); assertThat(shadowAlarmManager.getNextScheduledAlarm()).isNull(); assertRepeatingScheduledAlarm(now, INTERVAL_HOUR, pendingIntent, scheduledAlarm); }
@Test public void peekNextScheduledAlarm_shouldReturnNextAlarm() { assertThat(shadowAlarmManager.getNextScheduledAlarm()).isNull(); long now = new Date().getTime(); Intent intent = new Intent(activity, activity.getClass()); PendingIntent pendingIntent = PendingIntent.getActivity(activity, 0, intent, 0); alarmManager.set(AlarmManager.ELAPSED_REALTIME, now, pendingIntent); ShadowAlarmManager.ScheduledAlarm scheduledAlarm = shadowAlarmManager.peekNextScheduledAlarm(); assertThat(shadowAlarmManager.peekNextScheduledAlarm()).isNotNull(); assertScheduledAlarm(now, pendingIntent, scheduledAlarm); }