Methods Summary |
---|
public void | doErrorTest(android.net.Uri uri, int error)Verifies a particular error code was received from a download
Request request = new Request(uri);
request.setTitle(DEFAULT_FILENAME);
long dlRequest = mDownloadManager.enqueue(request);
waitForDownloadOrTimeout(dlRequest);
Cursor cursor = getCursor(dlRequest);
try {
verifyInt(cursor, DownloadManager.COLUMN_REASON, error);
} finally {
cursor.close();
}
|
public void | setUp(){@inheritDoc}
super.setUp();
setWiFiStateOn(true);
removeAllCurrentDownloads();
|
public void | tearDown(){@inheritDoc}
super.tearDown();
setWiFiStateOn(true);
removeAllCurrentDownloads();
if (mReceiver != null) {
mContext.unregisterReceiver(mReceiver);
mReceiver = null;
}
|
public void | testBinaryDownloadToSystemCache()Test a basic download of a binary file 500k in size.
int fileSize = 1024;
byte[] blobData = generateData(fileSize, DataType.BINARY);
long dlRequest = doBasicDownload(blobData, DOWNLOAD_TO_SYSTEM_CACHE);
verifyDownload(dlRequest, blobData);
mDownloadManager.remove(dlRequest);
|
public void | testDownloadNoWifi()Tests that a download set for Wifi does not progress while Wifi is disabled, but resumes
once Wifi is re-enabled.
long timeout = 60 * 1000; // wait only 60 seconds before giving up
int fileSize = 1024; // 140k
byte[] blobData = generateData(fileSize, DataType.TEXT);
setWiFiStateOn(false);
enqueueResponse(buildResponse(HTTP_OK, blobData));
try {
Uri uri = getServerUri(DEFAULT_FILENAME);
Request request = new Request(uri);
request.setAllowedNetworkTypes(Request.NETWORK_WIFI);
long dlRequest = mDownloadManager.enqueue(request);
// wait for the download to complete
boolean success = waitForDownloadOrTimeoutNoThrow(dlRequest,
WAIT_FOR_DOWNLOAD_POLL_TIME, timeout);
assertFalse("Download proceeded without Wifi connection!", success);
setWiFiStateOn(true);
waitForDownloadOrTimeout(dlRequest);
assertEquals(1, mReceiver.numDownloadsCompleted());
} finally {
setWiFiStateOn(true);
}
|
public void | testDownloadToExternal()Tests trying to download a file to SD card.
String localDownloadDirectory = Environment.getExternalStorageDirectory().getPath();
File downloadedFile = new File(localDownloadDirectory, DEFAULT_FILENAME);
// make sure the file doesn't already exist in the directory
downloadedFile.delete();
try {
byte[] blobData = generateData(DEFAULT_FILE_SIZE, DataType.TEXT);
// Prepare the mock server with a standard response
enqueueResponse(buildResponse(HTTP_OK, blobData));
Uri uri = getServerUri(DEFAULT_FILENAME);
Request request = new Request(uri);
Uri localUri = Uri.fromFile(downloadedFile);
request.setDestinationUri(localUri);
long dlRequest = mDownloadManager.enqueue(request);
// wait for the download to complete
waitForDownloadOrTimeout(dlRequest);
verifyAndCleanupSingleFileDownload(dlRequest, blobData);
assertEquals(1, mReceiver.numDownloadsCompleted());
} finally {
downloadedFile.delete();
}
|
public void | testDownloadToExternal_fileExists()Tests trying to download to SD card when the file with same name already exists.
File existentFile = createFileOnSD(null, 1, DataType.TEXT, null);
byte[] blobData = generateData(DEFAULT_FILE_SIZE, DataType.TEXT);
// Prepare the mock server with a standard response
enqueueResponse(buildResponse(HTTP_OK, blobData));
try {
Uri uri = getServerUri(DEFAULT_FILENAME);
Request request = new Request(uri);
Uri localUri = Uri.fromFile(existentFile);
request.setDestinationUri(localUri);
long dlRequest = mDownloadManager.enqueue(request);
// wait for the download to complete
waitForDownloadOrTimeout(dlRequest);
Cursor cursor = getCursor(dlRequest);
try {
verifyInt(cursor, DownloadManager.COLUMN_STATUS, DownloadManager.STATUS_SUCCESSFUL);
} finally {
cursor.close();
}
} finally {
existentFile.delete();
}
|
public void | testDownloadToProhibitedDirectory()Tests trying to download a file to the system partition.
File downloadedFile = new File(PROHIBITED_DIRECTORY, DEFAULT_FILENAME);
try {
byte[] blobData = generateData(DEFAULT_FILE_SIZE, DataType.TEXT);
// Prepare the mock server with a standard response
enqueueResponse(buildResponse(HTTP_OK, blobData));
Uri uri = getServerUri(DEFAULT_FILENAME);
Request request = new Request(uri);
Uri localUri = Uri.fromFile(downloadedFile);
request.setDestinationUri(localUri);
try {
mDownloadManager.enqueue(request);
fail("Failed to throw SecurityException when trying to write to /system.");
} catch (SecurityException s) {
assertFalse(downloadedFile.exists());
}
} finally {
// Just in case file somehow got created, make sure to delete it
downloadedFile.delete();
}
|
public void | testErrorHttpDataError_invalidRedirect()Tests the download failure error from an unhandled HTTP status code
Uri uri = getServerUri(DEFAULT_FILENAME);
final MockResponse resp = buildResponse(HTTP_REDIRECT);
resp.setHeader("Location", "://blah.blah.blah.com");
enqueueResponse(resp);
doErrorTest(uri, DownloadManager.ERROR_HTTP_DATA_ERROR);
|
public void | testErrorTooManyRedirects()Tests the download failure error after too many redirects (>5).
Uri uri = getServerUri(DEFAULT_FILENAME);
// force 6 redirects
for (int i = 0; i < 6; ++i) {
final MockResponse resp = buildResponse(HTTP_REDIRECT);
resp.setHeader("Location", uri.toString());
enqueueResponse(resp);
}
doErrorTest(uri, DownloadManager.ERROR_TOO_MANY_REDIRECTS);
|
public void | testErrorUnhandledHttpCode()Tests the download failure error from an unhandled HTTP status code
Uri uri = getServerUri(DEFAULT_FILENAME);
enqueueResponse(buildResponse(HTTP_PARTIAL_CONTENT));
doErrorTest(uri, DownloadManager.ERROR_UNHANDLED_HTTP_CODE);
|
public void | testGetDownloadIdOnNotification()Tests that we get the correct download ID from the download notification.
byte[] blobData = generateData(3000, DataType.TEXT); // file size = 3000 bytes
enqueueResponse(buildResponse(HTTP_OK, blobData));
long dlRequest = doCommonStandardEnqueue();
waitForDownloadOrTimeout(dlRequest);
Set<Long> ids = mReceiver.getDownloadIds();
assertEquals(1, ids.size());
Iterator<Long> it = ids.iterator();
assertEquals("Download ID received from notification does not match initial id!",
dlRequest, it.next().longValue());
|
public void | testRemoveDownload()Tests that we can remove a download from the download manager.
int fileSize = 1024;
byte[] blobData = generateData(fileSize, DataType.BINARY);
long dlRequest = doBasicDownload(blobData, DOWNLOAD_TO_DOWNLOAD_CACHE_DIR);
Cursor cursor = mDownloadManager.query(new Query().setFilterById(dlRequest));
try {
assertEquals("The count of downloads with this ID is not 1!", 1, cursor.getCount());
mDownloadManager.remove(dlRequest);
cursor.requery();
assertEquals("The count of downloads with this ID is not 0!", 0, cursor.getCount());
} finally {
cursor.close();
}
|
public void | testServerDropConnection_body()Tests that we get an error code when the server drops the connection during a download.
byte[] blobData = generateData(25000, DataType.TEXT); // file size = 25000 bytes
final MockResponse resp = buildResponse(HTTP_OK, blobData);
resp.setHeader("Content-Length", "50000");
enqueueResponse(resp);
long dlRequest = doCommonStandardEnqueue();
waitForDownloadOrTimeout(dlRequest);
Cursor cursor = getCursor(dlRequest);
try {
verifyInt(cursor, DownloadManager.COLUMN_STATUS, DownloadManager.STATUS_FAILED);
verifyInt(cursor, DownloadManager.COLUMN_REASON,
DownloadManager.ERROR_CANNOT_RESUME);
} finally {
cursor.close();
}
// Even tho the server drops the connection, we should still get a completed notification
assertEquals(1, mReceiver.numDownloadsCompleted());
|
public void | testSetTitle()Tests that we can set the title of a download.
int fileSize = 1024;
byte[] blobData = generateData(fileSize, DataType.BINARY);
enqueueResponse(buildResponse(HTTP_OK, blobData));
// An arbitrary unicode string title
final String title = "\u00a5123;\"\u0152\u017d \u054b \u0a07 \ucce0 \u6820\u03a8\u5c34" +
"\uf4ad\u0da9\uc0c5\uc1a8 \uf4c5 \uf4aa\u0023\'";
Uri uri = getServerUri(DEFAULT_FILENAME);
Request request = new Request(uri);
request.setTitle(title);
long dlRequest = mDownloadManager.enqueue(request);
waitForDownloadOrTimeout(dlRequest);
Cursor cursor = getCursor(dlRequest);
try {
verifyString(cursor, DownloadManager.COLUMN_TITLE, title);
} finally {
cursor.close();
}
|
public void | testTextDownloadToSystemCache()Tests the basic downloading of a text file 300000 bytes in size.
int fileSize = 1024;
byte[] blobData = generateData(fileSize, DataType.TEXT);
long dlRequest = doBasicDownload(blobData, DOWNLOAD_TO_SYSTEM_CACHE);
verifyDownload(dlRequest, blobData);
mDownloadManager.remove(dlRequest);
|
private void | verifyDownload(long requestId, byte[] fileData)Helper to verify a standard single-file download from the mock server, and clean up after
verification
Note that this also calls the Download manager's remove, which cleans up the file from cache.
int fileSize = fileData.length;
ParcelFileDescriptor pfd = mDownloadManager.openDownloadedFile(requestId);
Cursor cursor = mDownloadManager.query(new Query().setFilterById(requestId));
try {
assertEquals(1, cursor.getCount());
assertTrue(cursor.moveToFirst());
verifyFileSize(pfd, fileSize);
verifyFileContents(pfd, fileData);
int colIndex = cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME);
String fileName = cursor.getString(colIndex);
assertTrue(fileName.startsWith(CACHE_DIR));
} finally {
pfd.close();
cursor.close();
}
|