### Question:
KeyValueBackupTask implements BackupRestoreTask, Runnable { @VisibleForTesting public void waitCancel() { mCancelAcknowledged.block(); } @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_whenMarkCancelDuringFirstAgentOnBackup_doesNotCallTransportAfterWaitCancel() throws Exception { TransportMock transportMock = setUpInitializedTransport(mTransport); AgentMock agentMock = setUpAgent(PACKAGE_1); setUpAgentsWithData(PACKAGE_2); KeyValueBackupTask task = createKeyValueBackupTask(transportMock, PACKAGE_1, PACKAGE_2); agentOnBackupDo( agentMock, (oldState, dataOutput, newState) -> { writeData(dataOutput, "key", "data".getBytes()); writeState(newState, "newState".getBytes()); runInWorkerThread(task::markCancel); }); ConditionVariable taskFinished = runTaskAsync(task); verifyAndUnblockAgentCalls(2); task.waitCancel(); reset(transportMock.transport); taskFinished.block(); verifyZeroInteractions(transportMock.transport); }
@Test public void testWaitCancel_whenMarkCancelDuringAgentOnBackup_unregistersTask() throws Exception { TransportMock transportMock = setUpInitializedTransport(mTransport); setUpAgentWithData(PACKAGE_1); AgentMock agentMock = setUpAgent(PACKAGE_1); KeyValueBackupTask task = createKeyValueBackupTask(transportMock, PACKAGE_1); agentOnBackupDo( agentMock, (oldState, dataOutput, newState) -> { writeData(dataOutput, "key", "data".getBytes()); writeState(newState, "newState".getBytes()); runInWorkerThread(task::markCancel); }); ConditionVariable taskFinished = runTaskAsync(task); verifyAndUnblockAgentCalls(1); boolean backupInProgressDuringBackup = mBackupManagerService.isBackupOperationInProgress(); assertThat(backupInProgressDuringBackup).isTrue(); verifyAndUnblockAgentCalls(1); task.waitCancel(); boolean backupInProgressAfterWaitCancel = mBackupManagerService.isBackupOperationInProgress(); assertThat(backupInProgressDuringBackup).isTrue(); assertThat(backupInProgressAfterWaitCancel).isFalse(); taskFinished.block(); }