use of android.app.DownloadManager.Request in project android_frameworks_base by DirtyUnicorns.
the class DownloadManagerFunctionalTest method testSetTitle.
/**
* Tests that we can set the title of a download.
*/
@LargeTest
public void testSetTitle() throws Exception {
int fileSize = 1024;
byte[] blobData = generateData(fileSize, DataType.BINARY);
enqueueResponse(buildResponse(HTTP_OK, blobData));
// An arbitrary unicode string title
final String title = "¥123;\"ŒŽ Ջ ਇ 쳠 栠Ψ尴" + "ඩ샅솨 #\'";
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();
}
}
use of android.app.DownloadManager.Request in project android_frameworks_base by DirtyUnicorns.
the class DownloadManagerFunctionalTest method testRelativeRedirect.
/**
* Tests the download failure error from an unhandled HTTP status code
*/
@LargeTest
public void testRelativeRedirect() throws Exception {
Uri uri = getServerUri(DEFAULT_FILENAME);
final MockResponse resp = buildResponse(HTTP_REDIRECT);
resp.setHeader("Location", ":" + uri.getSchemeSpecificPart());
enqueueResponse(resp);
byte[] blobData = generateData(DEFAULT_FILE_SIZE, DataType.TEXT);
enqueueResponse(buildResponse(HTTP_OK, blobData));
Request request = new Request(uri);
request.setTitle(DEFAULT_FILENAME);
long dlRequest = mDownloadManager.enqueue(request);
waitForDownloadOrTimeout(dlRequest);
verifyAndCleanupSingleFileDownload(dlRequest, blobData);
assertEquals(1, mReceiver.numDownloadsCompleted());
}
use of android.app.DownloadManager.Request in project android_frameworks_base by DirtyUnicorns.
the class DownloadManagerFunctionalTest method testDownloadToExternal.
/**
* Tests trying to download a file to SD card.
*/
@LargeTest
public void testDownloadToExternal() throws Exception {
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();
}
}
use of android.app.DownloadManager.Request in project android_frameworks_base by DirtyUnicorns.
the class DownloadManagerTestApp method runDownloadMultipleWiFiEnableDisable.
/**
* Tests that downloads resume when switching on/off WiFi at various intervals.
*
* Note: Device has no mobile access when running this test.
*
* @throws Exception if unsuccessful
*/
public void runDownloadMultipleWiFiEnableDisable() throws Exception {
String filename = DOWNLOAD_FILENAME;
long filesize = DOWNLOAD_FILESIZE;
doCommonDownloadSetup();
String localDownloadDirectory = Environment.getExternalStorageDirectory().getPath();
File downloadedFile = new File(localDownloadDirectory, filename);
long dlRequest = -1;
try {
downloadedFile.delete();
// Make sure there are no pending downloads currently going on
removeAllCurrentDownloads();
Uri remoteUri = getExternalFileUri(filename);
Request request = new Request(remoteUri);
// Local destination of downloaded file
Uri localUri = Uri.fromFile(downloadedFile);
Log.i(LOG_TAG, "setting localUri to: " + localUri.getPath());
request.setDestinationUri(localUri);
request.setAllowedNetworkTypes(Request.NETWORK_WIFI);
dlRequest = mDownloadManager.enqueue(request);
waitForDownloadToStart(dlRequest);
// are we making any progress?
waitToReceiveData(dlRequest, EXPECTED_PROGRESS);
// download disable
Log.i(LOG_TAG, "Turning off WiFi...");
setWiFiStateOn(false);
// wait 40 seconds
Thread.sleep(40 * 1000);
// enable download...
Log.i(LOG_TAG, "Turning on WiFi again...");
setWiFiStateOn(true);
waitToReceiveData(dlRequest, EXPECTED_PROGRESS);
// download disable
Log.i(LOG_TAG, "Turning off WiFi...");
setWiFiStateOn(false);
// wait 20 seconds
Thread.sleep(20 * 1000);
// enable download...
Log.i(LOG_TAG, "Turning on WiFi again...");
setWiFiStateOn(true);
Log.i(LOG_TAG, "Waiting up to 10 minutes for download to complete...");
assertTrue("download not finished", waitForDownload(dlRequest, 10 * 60 * 1000));
ParcelFileDescriptor pfd = mDownloadManager.openDownloadedFile(dlRequest);
verifyFileSize(pfd, filesize);
} finally {
Log.i(LOG_TAG, "Cleaning up files...");
if (dlRequest != -1) {
mDownloadManager.remove(dlRequest);
}
downloadedFile.delete();
}
}
use of android.app.DownloadManager.Request in project android_frameworks_base by DirtyUnicorns.
the class DownloadManagerTestApp method initiateDownload.
/**
* Initiates a download.
*
* Queues up a download to the download manager, and saves the DownloadManager's assigned
* download ID for this download to a file.
*
* @throws Exception if unsuccessful
*/
public void initiateDownload() throws Exception {
String filename = DOWNLOAD_FILENAME;
mContext.deleteFile(DOWNLOAD_STARTED_FLAG);
FileOutputStream fileOutput = mContext.openFileOutput(DOWNLOAD_STARTED_FLAG, 0);
DataOutputStream outputFile = null;
doCommonDownloadSetup();
try {
long dlRequest = -1;
// Make sure there are no pending downloads currently going on
removeAllCurrentDownloads();
Uri remoteUri = getExternalFileUri(filename);
Request request = new Request(remoteUri);
dlRequest = mDownloadManager.enqueue(request);
waitForDownloadToStart(dlRequest);
assertTrue("request id is -1 from download manager", dlRequest != -1);
// Store ID of download for later retrieval
outputFile = new DataOutputStream(fileOutput);
outputFile.writeLong(dlRequest);
} finally {
if (outputFile != null) {
outputFile.flush();
outputFile.close();
}
}
}
Aggregations