Search in sources :

Example 1 with ShadowAlarmManager

use of org.robolectric.shadows.ShadowAlarmManager in project OneSignal-Android-SDK by OneSignal.

the class MainOneSignalClassRunner method shouldSyncPendingChangesFromSyncService.

@Test
@Config(sdk = 19)
public void shouldSyncPendingChangesFromSyncService() throws Exception {
    OneSignalInit();
    threadAndTaskWait();
    OneSignal.sendTag("key", "value");
    // App is swiped away
    blankActivityController.pause();
    assertEquals(2, ShadowOneSignalRestClient.networkCallCount);
    fastColdRestartApp();
    threadAndTaskWait();
    // Tags did not get synced so SyncService should be scheduled
    AlarmManager alarmManager = (AlarmManager) ApplicationProvider.getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    ShadowAlarmManager shadowAlarmManager = shadowOf(alarmManager);
    assertEquals(1, shadowAlarmManager.getScheduledAlarms().size());
    assertEquals(SyncService.class, shadowOf(shadowOf(shadowAlarmManager.getNextScheduledAlarm().operation).getSavedIntent()).getIntentClass());
    shadowAlarmManager.getScheduledAlarms().clear();
    // Test running the service
    Robolectric.buildService(SyncService.class).startCommand(0, 0);
    threadAndTaskWait();
    assertEquals("value", ShadowOneSignalRestClient.lastPost.getJSONObject("tags").getString("key"));
    // Remote params are called before players call due to initWithContext call inside SyncService
    assertEquals(4, ShadowOneSignalRestClient.networkCallCount);
    // Test starting app
    OneSignalInit();
    threadAndTaskWait();
    // No new changes, don't schedule another restart.
    // App is swiped away
    blankActivityController.pause();
    threadAndTaskWait();
    // No Focus service should be scheduled
    assertEquals(0, shadowOf(alarmManager).getScheduledAlarms().size());
    assertEquals(4, ShadowOneSignalRestClient.networkCallCount);
}
Also used : AlarmManager(android.app.AlarmManager) ShadowAlarmManager(org.robolectric.shadows.ShadowAlarmManager) ShadowAlarmManager(org.robolectric.shadows.ShadowAlarmManager) TestHelpers.assertAndRunSyncService(com.test.onesignal.TestHelpers.assertAndRunSyncService) SyncService(com.onesignal.SyncService) Test(org.junit.Test) Config(org.robolectric.annotation.Config)

Example 2 with ShadowAlarmManager

use of org.robolectric.shadows.ShadowAlarmManager in project OneSignal-Android-SDK by OneSignal.

the class TestHelpers method resetAlarmManager.

private static void resetAlarmManager() {
    AlarmManager alarmManager = (AlarmManager) ApplicationProvider.getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    ShadowAlarmManager shadowAlarmManager = shadowOf(alarmManager);
    shadowAlarmManager.getScheduledAlarms().clear();
}
Also used : AlarmManager(android.app.AlarmManager) ShadowAlarmManager(org.robolectric.shadows.ShadowAlarmManager) ShadowAlarmManager(org.robolectric.shadows.ShadowAlarmManager)

Example 3 with ShadowAlarmManager

use of org.robolectric.shadows.ShadowAlarmManager in project sms-backup-plus by jberkel.

the class AlarmManagerDriverTest method assertAlarmScheduled.

private Intent assertAlarmScheduled(String ofExpectedType) {
    AlarmManager alarmManager = (AlarmManager) RuntimeEnvironment.application.getSystemService(Context.ALARM_SERVICE);
    ShadowAlarmManager shadow = shadowOf(alarmManager);
    ShadowAlarmManager.ScheduledAlarm nextScheduledAlarm = shadow.getNextScheduledAlarm();
    assertThat(nextScheduledAlarm.type).isEqualTo(AlarmManager.RTC_WAKEUP);
    assertThat(nextScheduledAlarm.triggerAtTime).isGreaterThan(0L);
    PendingIntent pendingIntent = nextScheduledAlarm.operation;
    ShadowPendingIntent shadowPendingIntent = shadowOf(pendingIntent);
    ComponentName component = shadowPendingIntent.getSavedIntent().getComponent();
    assertThat(component.getPackageName()).isEqualTo("com.zegoggles.smssync");
    assertThat(component.getClassName()).isEqualTo("com.zegoggles.smssync.service.SmsBackupService");
    assertThat(shadowPendingIntent.getFlags()).isEqualTo(FLAG_UPDATE_CURRENT);
    assertThat(shadowPendingIntent.getSavedIntent().getAction()).isNotEmpty();
    assertThat(shadowPendingIntent.getSavedIntent().getAction()).isEqualTo(ofExpectedType);
    return shadowPendingIntent.getSavedIntent();
}
Also used : AlarmManager(android.app.AlarmManager) ShadowAlarmManager(org.robolectric.shadows.ShadowAlarmManager) ComponentName(android.content.ComponentName) PendingIntent(android.app.PendingIntent) ShadowPendingIntent(org.robolectric.shadows.ShadowPendingIntent) ShadowAlarmManager(org.robolectric.shadows.ShadowAlarmManager) ShadowPendingIntent(org.robolectric.shadows.ShadowPendingIntent)

Example 4 with ShadowAlarmManager

use of org.robolectric.shadows.ShadowAlarmManager in project android-job by evernote.

the class JobProxyTest method verifyAlarmCount.

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);
}
Also used : Field(java.lang.reflect.Field) ShadowAlarmManager(org.robolectric.shadows.ShadowAlarmManager)

Aggregations

ShadowAlarmManager (org.robolectric.shadows.ShadowAlarmManager)4 AlarmManager (android.app.AlarmManager)3 PendingIntent (android.app.PendingIntent)1 ComponentName (android.content.ComponentName)1 SyncService (com.onesignal.SyncService)1 TestHelpers.assertAndRunSyncService (com.test.onesignal.TestHelpers.assertAndRunSyncService)1 Field (java.lang.reflect.Field)1 Test (org.junit.Test)1 Config (org.robolectric.annotation.Config)1 ShadowPendingIntent (org.robolectric.shadows.ShadowPendingIntent)1