### Question:
KeyValueBackupTask implements BackupRestoreTask, Runnable { private void deletePmStateFile() { new File(mStateDirectory, PM_PACKAGE).delete(); } @VisibleForTesting  KeyValueBackupTask(
            UserBackupManagerService backupManagerService,
            TransportClient transportClient,
            String transportDirName,
            List<String> queue,
            @Nullable DataChangedJournal journal,
            KeyValueBackupReporter reporter,
            OnTaskFinishedListener taskFinishedListener,
            List<String> pendingFullBackups,
            boolean userInitiated,
            boolean nonIncremental); static KeyValueBackupTask start(
            UserBackupManagerService backupManagerService,
            TransportClient transportClient,
            String transportDirName,
            List<String> queue,
            @Nullable DataChangedJournal dataChangedJournal,
            IBackupObserver observer,
            @Nullable IBackupManagerMonitor monitor,
            OnTaskFinishedListener listener,
            List<String> pendingFullBackups,
            boolean userInitiated,
            boolean nonIncremental); @Override void run(); @Override void execute(); @Override void operationComplete(long unusedResult); @Override void handleCancel(boolean cancelAll); @VisibleForTesting void markCancel(); @VisibleForTesting void waitCancel(); @VisibleForTesting static final String STAGING_FILE_SUFFIX; @VisibleForTesting static final String NEW_STATE_FILE_SUFFIX; }### Answer:
@Test public void testRunTask_whenOnePackageAndNoPmState_initializesTransportAndResetsState() throws Exception { TransportMock transportMock = setUpTransport(mTransport); setUpAgentsWithData(PACKAGE_1, PACKAGE_2); KeyValueBackupTask task = createKeyValueBackupTask(transportMock, PACKAGE_1); deletePmStateFile(); Files.write(getStateFile(mTransport, PACKAGE_2), "package2State".getBytes()); runTask(task); verify(transportMock.transport).initializeDevice(); verify(mBackupManagerService).resetBackupState(getStateDirectory(mTransport).toFile()); assertThat(Files.exists(getStateFile(mTransport, PACKAGE_2))).isFalse(); assertEventLogged(EventLogTags.BACKUP_INITIALIZE); }
@Test public void testRunTask_whenTransportReturnsErrorForInitialization() throws Exception { TransportMock transportMock = setUpTransport(mTransport); when(transportMock.transport.initializeDevice()) .thenReturn(BackupTransport.TRANSPORT_ERROR); AgentMock agentMock = setUpAgentWithData(PACKAGE_1); KeyValueBackupTask task = createKeyValueBackupTask(transportMock, PACKAGE_1); deletePmStateFile(); runTask(task); verify(mBackupManagerService, times(2)) .resetBackupState(getStateDirectory(mTransport).toFile()); verify(agentMock.agent, never()).onBackup(any(), any(), any()); verify(transportMock.transport, never()).performBackup(any(), any(), anyInt()); assertBackupPendingFor(PACKAGE_1); assertEventLogged(EventLogTags.BACKUP_TRANSPORT_FAILURE, "(initialize)"); }
@Test public void testRunTask_whenTransportThrowsDuringInitialization() throws Exception { TransportMock transportMock = setUpTransport(mTransport); when(transportMock.transport.initializeDevice()).thenThrow(RemoteException.class); AgentMock agentMock = setUpAgentWithData(PACKAGE_1); KeyValueBackupTask task = createKeyValueBackupTask(transportMock, PACKAGE_1); deletePmStateFile(); runTask(task); verify(mBackupManagerService, times(2)) .resetBackupState(getStateDirectory(mTransport).toFile()); verify(agentMock.agent, never()).onBackup(any(), any(), any()); verify(transportMock.transport, never()).performBackup(any(), any(), anyInt()); assertBackupPendingFor(PACKAGE_1); }