diff options
author | Santos Cordon <santoscordon@google.com> | 2015-02-22 05:11:51 -0800 |
---|---|---|
committer | Santos Cordon <santoscordon@google.com> | 2015-02-22 05:44:11 -0800 |
commit | 415f5c0ebd5dcf2c54c4e8f8ca1a33708c075f67 (patch) | |
tree | 0626093ff560182e7264491a630e7c8888c75d70 /src | |
parent | fd6ef515b5c582c540b260c87cfb493bbd044643 (diff) | |
download | packages_providers_ContactsProvider-415f5c0ebd5dcf2c54c4e8f8ca1a33708c075f67.zip packages_providers_ContactsProvider-415f5c0ebd5dcf2c54c4e8f8ca1a33708c075f67.tar.gz packages_providers_ContactsProvider-415f5c0ebd5dcf2c54c4e8f8ca1a33708c075f67.tar.bz2 |
Add tests for CallLogBackupAgent.
Adds tests for writing and reading the state.
Change-Id: Ie00b3ec0c976195e32275a0a9d75b47ee245966e
Diffstat (limited to 'src')
-rw-r--r-- | src/com/android/providers/contacts/CallLogBackupAgent.java | 71 |
1 files changed, 45 insertions, 26 deletions
diff --git a/src/com/android/providers/contacts/CallLogBackupAgent.java b/src/com/android/providers/contacts/CallLogBackupAgent.java index 1371635..0cb542d 100644 --- a/src/com/android/providers/contacts/CallLogBackupAgent.java +++ b/src/com/android/providers/contacts/CallLogBackupAgent.java @@ -25,8 +25,12 @@ import android.os.ParcelFileDescriptor; import android.provider.CallLog; import android.util.Log; +import com.android.internal.annotations.VisibleForTesting; + import java.io.BufferedOutputStream; +import java.io.DataInput; import java.io.DataInputStream; +import java.io.DataOutput; import java.io.DataOutputStream; import java.io.EOFException; import java.io.FileInputStream; @@ -40,7 +44,8 @@ import java.util.TreeSet; */ public class CallLogBackupAgent extends BackupAgent { - private static class CallLogBackupState { + @VisibleForTesting + static class CallLogBackupState { int version; SortedSet<Integer> callIds; } @@ -60,7 +65,8 @@ public class CallLogBackupAgent extends BackupAgent { /** Current version of CallLogBackup. Used to track the backup format. */ private static final int VERSION = 1; /** Version indicating that there exists no previous backup entry. */ - private static final int VERSION_NO_PREVIOUS_STATE = 0; + @VisibleForTesting + static final int VERSION_NO_PREVIOUS_STATE = 0; private static final String[] CALL_LOG_PROJECTION = new String[] { CallLog.Calls._ID, @@ -82,7 +88,39 @@ public class CallLogBackupAgent extends BackupAgent { ParcelFileDescriptor newStateDescriptor) throws IOException { // Get the list of the previous calls IDs which were backed up. - CallLogBackupState state = readState(oldStateDescriptor); + DataInputStream dataInput = new DataInputStream( + new FileInputStream(oldStateDescriptor.getFileDescriptor())); + final CallLogBackupState state; + try { + state = readState(dataInput); + } finally { + dataInput.close(); + } + + // Run the actual backup of data + runBackup(state, data); + + // Rewrite the backup state. + DataOutputStream dataOutput = new DataOutputStream(new BufferedOutputStream( + new FileOutputStream(newStateDescriptor.getFileDescriptor()))); + try { + writeState(dataOutput, state); + } finally { + dataOutput.close(); + } + } + + /** ${inheritDoc} */ + @Override + public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) + throws IOException { + if (DEBUG) { + Log.d(TAG, "Performing Restore"); + } + } + + @VisibleForTesting + void runBackup(CallLogBackupState state, BackupDataOutput data) { SortedSet<Integer> callsToRemove = new TreeSet<>(state.callIds); // Get all the existing call log entries. @@ -127,22 +165,11 @@ public class CallLogBackupAgent extends BackupAgent { state.callIds.remove(i); } - // Rewrite the backup state. - writeState(newStateDescriptor, state); } finally { cursor.close(); } } - /** ${inheritDoc} */ - @Override - public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) - throws IOException { - if (DEBUG) { - Log.d(TAG, "Performing Restore"); - } - } - private Cursor getAllCallLogEntries() { // We use the API here instead of querying ContactsDatabaseHelper directly because // CallLogProvider has special locks in place for sychronizing when to read. Using the APIs @@ -151,9 +178,8 @@ public class CallLogBackupAgent extends BackupAgent { return resolver.query(CallLog.Calls.CONTENT_URI, CALL_LOG_PROJECTION, null, null, null); } - private CallLogBackupState readState(ParcelFileDescriptor oldState) throws IOException { - DataInputStream dataInput = new DataInputStream( - new FileInputStream(oldState.getFileDescriptor())); + @VisibleForTesting + CallLogBackupState readState(DataInput dataInput) throws IOException { CallLogBackupState state = new CallLogBackupState(); state.callIds = new TreeSet<>(); @@ -172,18 +198,14 @@ public class CallLogBackupAgent extends BackupAgent { } } catch (EOFException e) { state.version = VERSION_NO_PREVIOUS_STATE; - } finally { - dataInput.close(); } return state; } - private void writeState(ParcelFileDescriptor descriptor, CallLogBackupState state) + @VisibleForTesting + void writeState(DataOutput dataOutput, CallLogBackupState state) throws IOException { - DataOutputStream dataOutput = new DataOutputStream(new BufferedOutputStream( - new FileOutputStream(descriptor.getFileDescriptor()))); - // Write version first of all dataOutput.writeInt(VERSION); @@ -193,9 +215,6 @@ public class CallLogBackupAgent extends BackupAgent { for (Integer i : state.callIds) { dataOutput.writeInt(i); } - - // Done! - dataOutput.close(); } private Call readCallFromCursor(Cursor cursor) { |