private BackupState transition(SmsSyncState smsSyncState, Exception exception) { return service.transition(smsSyncState, exception); }
private void publish(SmsSyncState state) { publishProgress(service.transition(state, null)); } }
@Test public void shouldHandleAuthErrorAndTokenCouldBeRefreshed() throws Exception { mockFetch(SMS, 1); when(converter.convertMessages(any(Cursor.class), notNull(DataType.class))).thenReturn(result(SMS, 1)); XOAuth2AuthenticationFailedException exception = mock(XOAuth2AuthenticationFailedException.class); when(exception.getStatus()).thenReturn(400); when(store.getFolder(notNull(DataType.class), same(dataTypePreferences))).thenThrow(exception); when(service.getBackupImapStore()).thenReturn(store); task.doInBackground(config); verify(tokenRefresher).refreshOAuth2Token(); verify(service, times(2)).transition(SmsSyncState.LOGIN, null); verify(service, times(2)).transition(SmsSyncState.CALC, null); verify(service).transition(SmsSyncState.ERROR, exception); // make sure locks only get acquired+released once verify(service).acquireLocks(); verify(service).releaseLocks(); }
@Test public void shouldAcquireAndReleaseLocksDuringBackup() throws Exception { mockAllFetchEmpty(); task.doInBackground(config); verify(service).acquireLocks(); verify(service).releaseLocks(); verify(service).transition(SmsSyncState.FINISHED_BACKUP, null); }
@Test public void shouldBackupItems() throws Exception { mockFetch(SMS, 1); when(converter.convertMessages(any(Cursor.class), eq(SMS))).thenReturn(result(SMS, 1)); when(store.getFolder(notNull(DataType.class), same(dataTypePreferences))).thenReturn(folder); BackupState finalState = task.doInBackground(config); verify(folder).appendMessages(anyListOf(Message.class)); verify(service).transition(SmsSyncState.LOGIN, null); verify(service).transition(SmsSyncState.CALC, null); assertThat(finalState).isNotNull(); assertThat(finalState.isFinished()).isTrue(); assertThat(finalState.currentSyncedItems).isEqualTo(1); assertThat(finalState.itemsToSync).isEqualTo(1); assertThat(finalState.backupType).isEqualTo(config.backupType); }
@Test public void shouldHandleAuthErrorAndTokenCannotBeRefreshed() throws Exception { mockFetch(SMS, 1); when(converter.convertMessages(any(Cursor.class), notNull(DataType.class))).thenReturn(result(SMS, 1)); XOAuth2AuthenticationFailedException exception = mock(XOAuth2AuthenticationFailedException.class); when(exception.getStatus()).thenReturn(400); when(store.getFolder(notNull(DataType.class), same(dataTypePreferences))).thenThrow(exception); doThrow(new TokenRefreshException("failed")).when(tokenRefresher).refreshOAuth2Token(); task.doInBackground(config); verify(tokenRefresher, times(1)).refreshOAuth2Token(); verify(service).transition(SmsSyncState.ERROR, exception); // make sure locks only get acquired+released once verify(service).acquireLocks(); verify(service).releaseLocks(); }
@Test public void shouldScheduleNextRegularBackupAfterFinished() throws Exception { shadowConnectivityManager.setBackgroundDataSetting(true); Intent intent = new Intent(REGULAR.name()); service.handleIntent(intent); verify(backupTask).execute(any(BackupConfig.class)); service.backupStateChanged(service.transition(SmsSyncState.FINISHED_BACKUP, null)); verify(backupJobs).scheduleRegular(); assertThat(shadowOf(service).isStoppedBySelf()).isTrue(); assertThat(shadowOf(service).isForegroundStopped()).isTrue(); }