SmsBackupService extends ServiceBase { @Override protected void handleIntent(final Intent intent) { if (intent == null) return; final BackupType backupType = BackupType.fromIntent(intent); if (LOCAL_LOGV) { Log.v(TAG, "handleIntent(" + intent + ", " + (intent.getExtras() == null ? "null" : intent.getExtras().keySet()) + ", " + intent.getAction() + ", type="+backupType+")"); } appLog(R.string.app_log_backup_requested, getString(backupType.resId)); if (!isWorking() && SmsRestoreService.isServiceIdle()) { backup(backupType); } else { appLog(R.string.app_log_skip_backup_already_running); } }  @Override @NonNull BackupState getState(); @Override void onCreate(); @Override void onDestroy(); @Produce BackupState produceLastState(); @Subscribe void backupStateChanged(BackupState state); static boolean isServiceWorking(); BackupState transition(SmsSyncState newState, Exception e);  }
@Test public void shouldTriggerBackupWithManualIntent() throws Exception { Intent intent = new Intent(MANUAL.name()); service.handleIntent(intent); verify(backupTask).execute(any(BackupConfig.class)); }
@Test public void shouldNotCheckForConnectivityBeforeBackingUpWithNewScheduler() throws Exception { when(preferences.isUseOldScheduler()).thenReturn(false); Intent intent = new Intent(REGULAR.name()); shadowConnectivityManager.setActiveNetworkInfo(null); shadowConnectivityManager.setBackgroundDataSetting(true); service.handleIntent(intent); verify(backupTask).execute(any(BackupConfig.class)); }
@Test public void shouldPassInCorrectBackupConfig() throws Exception { Intent intent = new Intent(MANUAL.name()); ArgumentCaptor<BackupConfig> config = ArgumentCaptor.forClass(BackupConfig.class); service.handleIntent(intent); verify(backupTask).execute(config.capture()); BackupConfig backupConfig = config.getValue(); assertThat(backupConfig.backupType).isEqualTo(MANUAL); assertThat(backupConfig.currentTry).isEqualTo(0); }
@Test public void shouldNotifyUserAboutErrorInManualMode() throws Exception { when(authPreferences.getStoreUri()).thenReturn("invalid"); Intent intent = new Intent(MANUAL.name()); service.handleIntent(intent); verifyZeroInteractions(backupTask); assertNotificationShown("SMSBackup+ error", "No valid IMAP URI: invalid"); assertThat(shadowOf(service).isStoppedBySelf()).isTrue(); assertThat(shadowOf(service).isForegroundStopped()).isTrue(); }