private void mockAllFetchEmpty() { when(fetcher.getItemsForDataType(any(DataType.class), any(ContactGroupIds.class), anyInt())).thenReturn(emptyCursor()); } }
/** * Gets the most recent timestamp for given datatype. * @param dataType the data type * @return timestamp * @throws SecurityException if app does not hold necessary permissions */ public long getMostRecentTimestamp(DataType dataType) { return getMostRecentTimestampForQuery(queryBuilder.buildMostRecentQueryForDataType(dataType)); }
private long getMostRecentTimestampForQuery(BackupQueryBuilder.Query query) { Cursor cursor = performQuery(query); try { if (cursor.moveToFirst()) { return cursor.getLong(0); } else { return DataType.Defaults.MAX_SYNCED_DATE; } } finally { cursor.close(); } }
public @NonNull BackupCursors fetch(final @NonNull EnumSet<DataType> types, final @Nullable ContactGroupIds groups, final int maxItems) { int max = maxItems; BackupCursors cursors = new BackupCursors(); for (DataType type : types) { Cursor cursor = itemsFetcher.getItemsForDataType(type, groups, max); cursors.add(type, cursor); if (max > 0) { max = Math.max(max - cursor.getCount(), 0); } if (max == 0) break; } return cursors; }
private BackupState skip(Iterable<DataType> types) { appLog(R.string.app_log_skip_backup_skip_messages); for (DataType type : types) { try { preferences.getDataTypePreferences().setMaxSyncedDate(type, fetcher.getMostRecentTimestamp(type)); } catch (SecurityException e ) { return new BackupState(ERROR, 0, 0, MANUAL, type, e); } } Log.i(TAG, "All messages skipped."); return new BackupState(FINISHED_BACKUP, 0, 0, MANUAL, null, null); }
@Before public void before() { initMocks(this); context = RuntimeEnvironment.application; preferences = new Preferences(context); fetcher = new BackupItemsFetcher( resolver, queryBuilder); }
@SuppressLint("Recycle") private @NonNull Cursor performQuery(@Nullable BackupQueryBuilder.Query query) { if (query == null) return emptyCursor(); try { final Cursor cursor = resolver.query( query.uri, query.projection, query.selection, query.selectionArgs, query.sortOrder ); return cursor == null ? emptyCursor() : cursor; } catch (SQLiteException e) { Log.w(TAG, "error querying DB", e); return emptyCursor(); } catch (NullPointerException e) { Log.w(TAG, "error querying DB", e); return emptyCursor(); } }
private void mockFetch(DataType type, final int n) { when(fetcher.getItemsForDataType(eq(type), any(ContactGroupIds.class), anyInt())).then(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocationOnMock) throws Throwable { return testMessages(n); } }); }
@Test public void shouldReturnDefaultIfDataTypeCannotBeRead() throws Exception { for (DataType type : DataType.values()) { assertThat(fetcher.getMostRecentTimestamp(type)).isEqualTo(-1); } }
BackupTask(@NonNull SmsBackupService service) { final Context context = service.getApplicationContext(); this.service = service; this.authPreferences = service.getAuthPreferences(); this.preferences = service.getPreferences(); this.fetcher = new BackupItemsFetcher( context.getContentResolver(), new BackupQueryBuilder(preferences.getDataTypePreferences())); PersonLookup personLookup = new PersonLookup(service.getContentResolver()); this.contactAccessor = new ContactAccessor(); this.converter = new MessageConverter(context, service.getPreferences(), authPreferences.getUserEmail(), personLookup, contactAccessor); if (preferences.isCallLogCalendarSyncEnabled()) { calendarSyncer = new CalendarSyncer( CalendarAccessor.Get.instance(service.getContentResolver()), preferences.getCallLogCalendarId(), personLookup, new CallFormatter(context.getResources()) ); } else { calendarSyncer = null; } this.tokenRefresher = new TokenRefresher(service, new OAuth2Client(authPreferences.getOAuth2ClientId()), authPreferences); }
public static CursorAndType empty() { return new CursorAndType(DataType.SMS, emptyCursor()); } }
@Test public void shouldFetchAllItemsRespectingMaxItems() throws Exception { when(fetcher.getItemsForDataType(SMS, null, 5)).thenReturn(cursor(5)); BackupCursors cursors = bulkFetcher.fetch(EnumSet.of(SMS, MMS), null, 5); assertThat(cursors.count()).isEqualTo(5); assertThat(cursors.count(SMS)).isEqualTo(5); verify(fetcher, never()).getItemsForDataType(eq(DataType.MMS), any(ContactGroupIds.class), anyInt()); }
@Test public void shouldMostRecentTimestampForItemTypeMMS() throws Exception { mockMostRecentTimestampForType(MMS, 23L); assertThat(fetcher.getMostRecentTimestamp(MMS)).isEqualTo(23L); }
public @NonNull Cursor getItemsForDataType(DataType dataType, ContactGroupIds group, int max) { if (LOCAL_LOGV) Log.v(TAG, "getItemsForDataType(type=" + dataType + ", max=" + max + ")"); return performQuery(queryBuilder.buildQueryForDataType(dataType, group, max)); }
@Test public void shouldCatchNullPointerExceptions() throws Exception { preferences.getDataTypePreferences().setBackupEnabled(true, SMS); when(resolver.query(any(Uri.class), any(String[].class), anyString(), any(String[].class), anyString())) .thenThrow(new NullPointerException()); mockEmptyQuery(); assertThat(fetcher.getItemsForDataType(SMS, null, -1).getCount()).isEqualTo(0); }
@Test public void shouldGetMostRecentTimestampForItemTypeSMS() throws Exception { mockMostRecentTimestampForType(SMS, 23L); assertThat(fetcher.getMostRecentTimestamp(SMS)).isEqualTo(23L); }
@Test public void shouldCatchSQLiteExceptions() throws Exception { preferences.getDataTypePreferences().setBackupEnabled(true, SMS); when(resolver.query(any(Uri.class), any(String[].class), anyString(), any(String[].class), anyString())) .thenThrow(new SQLiteException()); mockEmptyQuery(); assertThat(fetcher.getItemsForDataType(SMS, null, -1).getCount()).isEqualTo(0); }
@Test public void shouldGetMostRecentTimestampForItemTypeCallLog() throws Exception { mockMostRecentTimestampForType(CALLLOG, 23L); assertThat(fetcher.getMostRecentTimestamp(CALLLOG)).isEqualTo(23L); }
@Test public void shouldGetItemsForDataType() throws Exception { preferences.getDataTypePreferences().setBackupEnabled(true, SMS); assertThat(fetcher.getItemsForDataType(SMS, null, -1).getCount()).isEqualTo(0); verifyZeroInteractions(resolver); }
@Test public void shouldSkipItems() throws Exception { when(fetcher.getMostRecentTimestamp(any(DataType.class))).thenReturn(-23L); BackupState finalState = task.doInBackground(new BackupConfig( store, 0, 100, new ContactGroup(-1), BackupType.SKIP, EnumSet.of(SMS), false ) ); verify(dataTypePreferences).setMaxSyncedDate(DataType.SMS, -23); verifyZeroInteractions(dataTypePreferences); assertThat(finalState).isNotNull(); assertThat(finalState.isFinished()).isTrue(); }