@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) @Test public void testSetTimeout() { final Context ctx = RuntimeEnvironment.application.getApplicationContext(); final AlarmManager alarmManager = (AlarmManager) RuntimeEnvironment.application.getSystemService(Context.ALARM_SERVICE); final ShadowAlarmManager shadowAlarmManager = shadowOf(alarmManager); final long whenExpected = System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(TIMEOUT_MINUTES); RegisteringTimeoutController.setTimeout(ctx, TEST_PROVIDER); final List<ShadowAlarmManager.ScheduledAlarm> alarms = shadowAlarmManager.getScheduledAlarms(); for (ShadowAlarmManager.ScheduledAlarm alarm : alarms) { final PendingIntent operation = alarm.operation; final int creatorUid = operation.getCreatorUid(); if (creatorUid == ctx.getApplicationInfo().uid) { Assert.assertTrue(Math.abs(whenExpected - alarm.triggerAtTime) < TIME_DELTA_MILLISECONDS); return; } } Assert.fail(); }
@Test public void shouldScheduleForContinuousAlertOnActivationAndGoToHomeScreen() { panicAlert = getPanicAlert(new TestExecutorService()); when(mockCurrentLocationProvider.getLocation()).thenReturn(mockLocation); panicAlert.activate(); Map<PendingIntent, String> requestLocationUpdates = shadowLocationManager.getRequestLocationUdpateProviderPendingIntents(); List<ShadowAlarmManager.ScheduledAlarm> scheduledAlarms = shadowAlarmManager.getScheduledAlarms(); ShadowAlarmManager.ScheduledAlarm alarm = scheduledAlarms.get(0); assertEquals(2, requestLocationUpdates.size()); assertTrue(requestLocationUpdates.values().containsAll(Arrays.asList(NETWORK_PROVIDER, GPS_PROVIDER))); assertEquals(1, scheduledAlarms.size()); assertEquals(AppConstants.DEFAULT_ALARM_INTERVAL * 1000 * 60, alarm.interval); assertEquals(AlarmManager.ELAPSED_REALTIME_WAKEUP, alarm.type); Intent startedIntent = shadowOf(context).peekNextStartedActivity(); List<String> categories = new ArrayList<String>(startedIntent.getCategories()); assertEquals(categories.get(0), Intent.CATEGORY_HOME); assertEquals(startedIntent.getPackage(), "com.test.package.Class1"); }
@Test public void setAddsTheAlarmIntent() { repo.set(alarm); ShadowAlarmManager.ScheduledAlarm nextAlarm = shadowAlarmManager.getNextScheduledAlarm(); ShadowPendingIntent shadowPendingIntent = shadowOf(nextAlarm.operation); Intent expectedIntent = new Intent(context, AlarmReceiver.class); expectedIntent.putExtra(Alarm.ALARM_ID_KEY, alarm.getId()); assertEquals(triggerTime.getMillis(), nextAlarm.triggerAtTime); assertTrue(expectedIntent.filterEquals(shadowPendingIntent.getSavedIntent())); assertEquals(PendingIntent.FLAG_UPDATE_CURRENT, shadowPendingIntent.getFlags()); }
@Test public void onCreate_shouldSetDataUploadServiceAlarm() throws Exception { Token token = new Token("yo", "yo"); activity.setAccessToken(token); activity = initBaseActivityWithMenu(menu); AlarmManager alarmManager = (AlarmManager) Robolectric.application.getSystemService(Context.ALARM_SERVICE); ShadowAlarmManager shadowAlarmManager = Robolectric.shadowOf(alarmManager); assertThat(shadowAlarmManager.getNextScheduledAlarm()).isNotNull(); }
@Test public void shouldDeactivateTheAlert() { panicAlert = getPanicAlert(new TestExecutorService()); when(mockCurrentLocationProvider.getLocation()).thenReturn(mockLocation); panicAlert.activate(); panicAlert.deActivate(); List<ShadowAlarmManager.ScheduledAlarm> scheduledAlarms = shadowAlarmManager.getScheduledAlarms(); assertFalse(ApplicationSettings.isAlertActive(context)); assertEquals(0, scheduledAlarms.size()); }
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); }
public static ShadowAlarmManager shadowOf(AlarmManager instance) { return (ShadowAlarmManager) Robolectric.shadowOf_(instance); }