use of android.app.DownloadManager.Query in project android_frameworks_base by DirtyUnicorns.
the class DownloadManagerStressTest method testMultipleDownloads.
/**
* Attempts to download several files simultaneously
*/
@LargeTest
public void testMultipleDownloads() throws Exception {
// need to be sure all current downloads have stopped first
removeAllCurrentDownloads();
int NUM_FILES = 10;
// 10 kb
int MAX_FILE_SIZE = 10 * 1024;
Random r = new LoggingRng();
for (int i = 0; i < NUM_FILES; ++i) {
int size = r.nextInt(MAX_FILE_SIZE);
byte[] blobData = generateData(size, DataType.TEXT);
Uri uri = getServerUri(DEFAULT_FILENAME + i);
Request request = new Request(uri);
request.setTitle(String.format("%s--%d", DEFAULT_FILENAME + i, i));
// Prepare the mock server with a standard response
enqueueResponse(buildResponse(HTTP_OK, blobData));
long requestID = mDownloadManager.enqueue(request);
}
waitForDownloadsOrTimeout(WAIT_FOR_DOWNLOAD_POLL_TIME, MAX_WAIT_FOR_DOWNLOAD_TIME);
Cursor cursor = mDownloadManager.query(new Query());
try {
assertEquals(NUM_FILES, cursor.getCount());
if (cursor.moveToFirst()) {
do {
int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
String filename = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_URI));
String errorString = String.format("File %s failed to download successfully. Status code: %d", filename, status);
assertEquals(errorString, DownloadManager.STATUS_SUCCESSFUL, status);
} while (cursor.moveToNext());
}
assertEquals(NUM_FILES, mReceiver.numDownloadsCompleted());
} finally {
cursor.close();
}
}
use of android.app.DownloadManager.Query in project android_frameworks_base by DirtyUnicorns.
the class DownloadManagerBaseTest method getCursor.
/**
* Performs a query based on ID and returns a Cursor for the query.
*
* @param id The id of the download in DL Manager; pass -1 to query all downloads
* @return A cursor for the query results
*/
protected Cursor getCursor(long id) throws Exception {
Query query = new Query();
if (id != -1) {
query.setFilterById(id);
}
Cursor cursor = mDownloadManager.query(query);
int currentWaitTime = 0;
try {
while (!cursor.moveToFirst()) {
Thread.sleep(DEFAULT_WAIT_POLL_TIME);
currentWaitTime += DEFAULT_WAIT_POLL_TIME;
if (currentWaitTime > DEFAULT_MAX_WAIT_TIME) {
fail("timed out waiting for a non-null query result");
}
cursor.requery();
}
} catch (Exception e) {
cursor.close();
throw e;
}
return cursor;
}
use of android.app.DownloadManager.Query in project android_frameworks_base by DirtyUnicorns.
the class DownloadManagerBaseTest method hasDownloadFinished.
/**
* Checks with the download manager if the give download is finished.
* @param id id of the download to check
* @return true if download is finished, false otherwise.
*/
private boolean hasDownloadFinished(long id) {
Query q = new Query();
q.setFilterById(id);
q.setFilterByStatus(DownloadManager.STATUS_SUCCESSFUL);
Cursor cursor = mDownloadManager.query(q);
boolean finished = cursor.getCount() == 1;
cursor.close();
return finished;
}
use of android.app.DownloadManager.Query in project android_frameworks_base by AOSPA.
the class DownloadManagerStressTest method testMultipleDownloads.
/**
* Attempts to download several files simultaneously
*/
@LargeTest
public void testMultipleDownloads() throws Exception {
// need to be sure all current downloads have stopped first
removeAllCurrentDownloads();
int NUM_FILES = 10;
// 10 kb
int MAX_FILE_SIZE = 10 * 1024;
Random r = new LoggingRng();
for (int i = 0; i < NUM_FILES; ++i) {
int size = r.nextInt(MAX_FILE_SIZE);
byte[] blobData = generateData(size, DataType.TEXT);
Uri uri = getServerUri(DEFAULT_FILENAME + i);
Request request = new Request(uri);
request.setTitle(String.format("%s--%d", DEFAULT_FILENAME + i, i));
// Prepare the mock server with a standard response
enqueueResponse(buildResponse(HTTP_OK, blobData));
long requestID = mDownloadManager.enqueue(request);
}
waitForDownloadsOrTimeout(WAIT_FOR_DOWNLOAD_POLL_TIME, MAX_WAIT_FOR_DOWNLOAD_TIME);
Cursor cursor = mDownloadManager.query(new Query());
try {
assertEquals(NUM_FILES, cursor.getCount());
if (cursor.moveToFirst()) {
do {
int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
String filename = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_URI));
String errorString = String.format("File %s failed to download successfully. Status code: %d", filename, status);
assertEquals(errorString, DownloadManager.STATUS_SUCCESSFUL, status);
} while (cursor.moveToNext());
}
assertEquals(NUM_FILES, mReceiver.numDownloadsCompleted());
} finally {
cursor.close();
}
}
use of android.app.DownloadManager.Query in project android_frameworks_base by AOSPA.
the class DownloadManagerFunctionalTest method testRemoveDownload.
/**
* Tests that we can remove a download from the download manager.
*/
@LargeTest
public void testRemoveDownload() throws Exception {
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();
}
}
Aggregations