Add DefaultDownloadIndex setManualStopReason variant for single download

PiperOrigin-RevId: 240108843
This commit is contained in:
eguven 2019-03-25 10:37:35 +00:00 committed by Toni
parent afd9014b82
commit 6f9d33dbd3
2 changed files with 79 additions and 1 deletions

View File

@ -220,6 +220,7 @@ public final class DefaultDownloadIndex implements DownloadIndex {
/** /**
* Removes the {@link DownloadState} with the given {@code id}. * Removes the {@link DownloadState} with the given {@code id}.
* *
* @param id ID of a {@link DownloadState}.
* @throws DatabaseIOException If an error occurs removing the state. * @throws DatabaseIOException If an error occurs removing the state.
*/ */
public void removeDownloadState(String id) throws DatabaseIOException { public void removeDownloadState(String id) throws DatabaseIOException {
@ -250,6 +251,30 @@ public final class DefaultDownloadIndex implements DownloadIndex {
} }
} }
/**
* Sets the manual stop reason of the download with the given {@code id} in a terminal state
* ({@link DownloadState#STATE_COMPLETED}, {@link DownloadState#STATE_FAILED}).
*
* <p>If there's no {@link DownloadState} with the given {@code id} or it isn't in a terminal
* state, then nothing happens.
*
* @param id ID of a {@link DownloadState}.
* @param manualStopReason The manual stop reason.
* @throws DatabaseIOException If an error occurs updating the state.
*/
public void setManualStopReason(String id, int manualStopReason) throws DatabaseIOException {
ensureInitialized();
try {
ContentValues values = new ContentValues();
values.put(COLUMN_MANUAL_STOP_REASON, manualStopReason);
SQLiteDatabase writableDatabase = databaseProvider.getWritableDatabase();
writableDatabase.update(
TABLE_NAME, values, WHERE_STATE_TERMINAL + " AND " + WHERE_ID_EQUALS, new String[] {id});
} catch (SQLException e) {
throw new DatabaseIOException(e);
}
}
private void ensureInitialized() throws DatabaseIOException { private void ensureInitialized() throws DatabaseIOException {
if (initialized) { if (initialized) {
return; return;

View File

@ -221,7 +221,7 @@ public class DefaultDownloadIndexTest {
} }
@Test @Test
public void setManualStopReason_setToReasonNone() throws Exception { public void setManualStopReason_setReasonToNone() throws Exception {
String id = "id"; String id = "id";
DownloadStateBuilder downloadStateBuilder = DownloadStateBuilder downloadStateBuilder =
new DownloadStateBuilder(id) new DownloadStateBuilder(id)
@ -271,4 +271,57 @@ public class DefaultDownloadIndexTest {
DownloadState readDownloadState = downloadIndex.getDownloadState(id); DownloadState readDownloadState = downloadIndex.getDownloadState(id);
DownloadStateTest.assertEqual(readDownloadState, downloadState); DownloadStateTest.assertEqual(readDownloadState, downloadState);
} }
@Test
public void setSingleDownloadManualStopReason_setReasonToNone() throws Exception {
String id = "id";
DownloadStateBuilder downloadStateBuilder =
new DownloadStateBuilder(id)
.setState(DownloadState.STATE_COMPLETED)
.setManualStopReason(0x12345678);
DownloadState downloadState = downloadStateBuilder.build();
downloadIndex.putDownloadState(downloadState);
downloadIndex.setManualStopReason(id, DownloadState.MANUAL_STOP_REASON_NONE);
DownloadState readDownloadState = downloadIndex.getDownloadState(id);
DownloadState expectedDownloadState =
downloadStateBuilder.setManualStopReason(DownloadState.MANUAL_STOP_REASON_NONE).build();
DownloadStateTest.assertEqual(readDownloadState, expectedDownloadState);
}
@Test
public void setSingleDownloadManualStopReason_setReason() throws Exception {
String id = "id";
DownloadStateBuilder downloadStateBuilder =
new DownloadStateBuilder(id)
.setState(DownloadState.STATE_FAILED)
.setFailureReason(DownloadState.FAILURE_REASON_UNKNOWN);
DownloadState downloadState = downloadStateBuilder.build();
downloadIndex.putDownloadState(downloadState);
int manualStopReason = 0x12345678;
downloadIndex.setManualStopReason(id, manualStopReason);
DownloadState readDownloadState = downloadIndex.getDownloadState(id);
DownloadState expectedDownloadState =
downloadStateBuilder.setManualStopReason(manualStopReason).build();
DownloadStateTest.assertEqual(readDownloadState, expectedDownloadState);
}
@Test
public void setSingleDownloadManualStopReason_notTerminalState_doesNotSetManualStopReason()
throws Exception {
String id = "id";
DownloadStateBuilder downloadStateBuilder =
new DownloadStateBuilder(id).setState(DownloadState.STATE_DOWNLOADING);
DownloadState downloadState = downloadStateBuilder.build();
downloadIndex.putDownloadState(downloadState);
int notMetRequirements = 0x12345678;
downloadIndex.setManualStopReason(id, notMetRequirements);
DownloadState readDownloadState = downloadIndex.getDownloadState(id);
DownloadStateTest.assertEqual(readDownloadState, downloadState);
}
} }