private void verifyAlarmCount(AlarmManager alarmManager, int count) throws NoSuchFieldException, IllegalAccessException { Field declaredField = alarmManager.getClass().getDeclaredField("__robo_data__"); declaredField.setAccessible(true); ShadowAlarmManager shadowAlarmManager = (ShadowAlarmManager) declaredField.get(alarmManager); assertThat(shadowAlarmManager.getScheduledAlarms()).hasSize(count); }
@Test public void cancel_removesMatchingPendingIntentsWithActions() { Intent newIntent = new Intent("someAction"); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, newIntent, 0); alarmManager.set(AlarmManager.RTC, 1337, pendingIntent); assertThat(shadowAlarmManager.getScheduledAlarms()).hasSize(1); alarmManager.cancel(PendingIntent.getBroadcast(context, 0, new Intent("anotherAction"), 0)); assertThat(shadowAlarmManager.getScheduledAlarms()).hasSize(1); alarmManager.cancel(PendingIntent.getBroadcast(context, 0, new Intent("someAction"), 0)); assertThat(shadowAlarmManager.getScheduledAlarms()).hasSize(0); }
@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 schedule_useRequestCodeToMatchExistingPendingIntents() throws Exception { Intent intent = new Intent("ACTION!"); PendingIntent pI = PendingIntent.getService(context, 1, intent, 0); alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 10, pI); PendingIntent pI2 = PendingIntent.getService(context, 2, intent, 0); alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 10, pI2); assertThat(shadowAlarmManager.getScheduledAlarms()).hasSize(2); }
@Test public void cancel_useRequestCodeToMatchExistingPendingIntents() throws Exception { Intent intent = new Intent("ACTION!"); PendingIntent pI = PendingIntent.getService(context, 1, intent, 0); alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 10, pI); PendingIntent pI2 = PendingIntent.getService(context, 2, intent, 0); alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 10, pI2); assertThat(shadowAlarmManager.getScheduledAlarms()).hasSize(2); alarmManager.cancel(pI); assertThat(shadowAlarmManager.getScheduledAlarms()).hasSize(1); assertThat(shadowAlarmManager.getNextScheduledAlarm().operation).isEqualTo(pI2); }
@Test public void cancel_removesMatchingPendingIntents() { Intent intent = new Intent(context, String.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, FLAG_UPDATE_CURRENT); alarmManager.set(AlarmManager.RTC, 1337, pendingIntent); Intent intent2 = new Intent(context, Integer.class); PendingIntent pendingIntent2 = PendingIntent.getBroadcast(context, 0, intent2, FLAG_UPDATE_CURRENT); alarmManager.set(AlarmManager.RTC, 1337, pendingIntent2); assertThat(shadowAlarmManager.getScheduledAlarms()).hasSize(2); Intent intent3 = new Intent(context, String.class); PendingIntent pendingIntent3 = PendingIntent.getBroadcast(context, 0, intent3, FLAG_UPDATE_CURRENT); alarmManager.cancel(pendingIntent3); assertThat(shadowAlarmManager.getScheduledAlarms()).hasSize(1); }
@Test @Config(minSdk = N) public void cancel_removesMatchingListeners() { Intent intent = new Intent("ACTION!"); PendingIntent pI = PendingIntent.getService(context, 1, intent, 0); OnAlarmListener listener1 = () -> {}; OnAlarmListener listener2 = () -> {}; Handler handler = new Handler(); alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 20, "tag", listener1, handler); alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 30, "tag", listener2, handler); alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 40, pI); assertThat(shadowAlarmManager.getScheduledAlarms()).hasSize(3); alarmManager.cancel(listener1); assertThat(shadowAlarmManager.getScheduledAlarms()).hasSize(2); assertThat(shadowAlarmManager.peekNextScheduledAlarm().onAlarmListener).isEqualTo(listener2); assertThat(shadowAlarmManager.peekNextScheduledAlarm().handler).isEqualTo(handler); }