Search in sources :

Example 21 with Request

use of android.app.DownloadManager.Request in project android_frameworks_base by DirtyUnicorns.

the class DownloadManagerFunctionalTest method testDownloadToExternal_fileExists.

/**
     * Tests trying to download to SD card when the file with same name already exists.
     */
@LargeTest
public void testDownloadToExternal_fileExists() throws Exception {
    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();
    }
}
Also used : Request(android.app.DownloadManager.Request) Cursor(android.database.Cursor) File(java.io.File) Uri(android.net.Uri) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 22 with Request

use of android.app.DownloadManager.Request in project android_frameworks_base by DirtyUnicorns.

the class DownloadManagerFunctionalTest method doErrorTest.

/**
     * Verifies a particular error code was received from a download
     *
     * @param uri The uri to enqueue to the DownloadManager
     * @param error The error code expected
     * @throws Exception if the test fails
     */
public void doErrorTest(Uri uri, int error) throws Exception {
    Request request = new Request(uri);
    request.setTitle(DEFAULT_FILENAME);
    long dlRequest = mDownloadManager.enqueue(request);
    try {
        waitForDownloadOrTimeout(dlRequest);
    } catch (TimeoutException ex) {
    // it is expected to timeout as download never finishes
    }
    Cursor cursor = getCursor(dlRequest);
    try {
        verifyInt(cursor, DownloadManager.COLUMN_REASON, error);
    } finally {
        cursor.close();
    }
}
Also used : Request(android.app.DownloadManager.Request) Cursor(android.database.Cursor) TimeoutException(java.util.concurrent.TimeoutException)

Example 23 with Request

use of android.app.DownloadManager.Request 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();
    }
}
Also used : Random(java.util.Random) Query(android.app.DownloadManager.Query) Request(android.app.DownloadManager.Request) Cursor(android.database.Cursor) Uri(android.net.Uri) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 24 with Request

use of android.app.DownloadManager.Request in project android_frameworks_base by DirtyUnicorns.

the class DownloadManagerTestApp method runLargeDownloadOverWiFi.

/**
     * Tests downloading a large file over WiFi (~10 Mb).
     *
     * @throws Exception if unsuccessful
     */
public void runLargeDownloadOverWiFi() throws Exception {
    String filename = DOWNLOAD_FILENAME;
    long filesize = DOWNLOAD_FILESIZE;
    long dlRequest = -1;
    doCommonDownloadSetup();
    // Make sure there are no pending downloads currently going on
    removeAllCurrentDownloads();
    Uri remoteUri = getExternalFileUri(filename);
    Request request = new Request(remoteUri);
    request.setMimeType("application/vnd.android.package-archive");
    dlRequest = mDownloadManager.enqueue(request);
    // Rather large file, so wait up to 15 mins...
    assertTrue("download not finished", waitForDownload(dlRequest, 15 * 60 * 1000));
    Cursor cursor = getCursor(dlRequest);
    ParcelFileDescriptor pfd = null;
    try {
        Log.i(LOG_TAG, "Verifying download information...");
        // Verify specific info about the file (size, name, etc)...
        pfd = mDownloadManager.openDownloadedFile(dlRequest);
        verifyFileSize(pfd, filesize);
    } finally {
        if (pfd != null) {
            pfd.close();
        }
        mDownloadManager.remove(dlRequest);
        cursor.close();
    }
}
Also used : Request(android.app.DownloadManager.Request) ParcelFileDescriptor(android.os.ParcelFileDescriptor) Cursor(android.database.Cursor) Uri(android.net.Uri)

Example 25 with Request

use of android.app.DownloadManager.Request in project android_frameworks_base by DirtyUnicorns.

the class DownloadManagerTestApp method runDownloadMultipleSimultaneously.

/**
     * Tests 15 concurrent downloads of 1,000,000-byte files.
     *
     * @throws Exception if test failed
     */
public void runDownloadMultipleSimultaneously() throws Exception {
    final int TOTAL_DOWNLOADS = 15;
    HashSet<Long> downloadIds = new HashSet<Long>(TOTAL_DOWNLOADS);
    // Make sure there are no pending downloads currently going on
    removeAllCurrentDownloads();
    try {
        for (int i = 0; i < TOTAL_DOWNLOADS; ++i) {
            long dlRequest = -1;
            String filename = FILE_CONCURRENT_DOWNLOAD_FILE_PREFIX + i + FILE_CONCURRENT_DOWNLOAD_FILE_EXTENSION;
            Uri remoteUri = getExternalFileUri(filename);
            Request request = new Request(remoteUri);
            request.setTitle(filename);
            dlRequest = mDownloadManager.enqueue(request);
            assertTrue("request id is -1 from download manager", dlRequest != -1);
            downloadIds.add(dlRequest);
        }
        // wait 15 mins max
        assertTrue("download not finished", waitForMultipleDownloads(downloadIds, 15 * 60 * 2000));
    } finally {
        removeAllCurrentDownloads();
    }
}
Also used : Request(android.app.DownloadManager.Request) Uri(android.net.Uri) HashSet(java.util.HashSet)

Aggregations

Request (android.app.DownloadManager.Request)122 Uri (android.net.Uri)86 LargeTest (android.test.suitebuilder.annotation.LargeTest)41 File (java.io.File)38 Cursor (android.database.Cursor)32 ParcelFileDescriptor (android.os.ParcelFileDescriptor)24 DownloadManager (android.app.DownloadManager)17 Suppress (android.test.suitebuilder.annotation.Suppress)10 Query (android.app.DownloadManager.Query)6 DataOutputStream (java.io.DataOutputStream)6 FileOutputStream (java.io.FileOutputStream)6 HashSet (java.util.HashSet)6 Random (java.util.Random)6 Resources (android.content.res.Resources)5 UiObject (android.support.test.uiautomator.UiObject)5 MockResponse (com.google.mockwebserver.MockResponse)4 TimeoutException (java.util.concurrent.TimeoutException)4 AlertDialog (android.app.AlertDialog)3 Context (android.content.Context)3 DialogInterface (android.content.DialogInterface)3