Search in sources :

Example 36 with Request

use of android.app.DownloadManager.Request in project platform_frameworks_base by android.

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());
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) Request(android.app.DownloadManager.Request) Uri(android.net.Uri) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 37 with Request

use of android.app.DownloadManager.Request in project platform_frameworks_base by android.

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 38 with Request

use of android.app.DownloadManager.Request in project platform_frameworks_base by android.

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 39 with Request

use of android.app.DownloadManager.Request in project platform_frameworks_base by android.

the class DownloadManagerTestApp method runDownloadMultipleAirplaneModeEnableDisable.

/**
     * Tests that downloads resume when switching on/off Airplane mode numerous times at
     * various intervals.
     *
     * Note: Device has no mobile access when running this test.
     *
     * @throws Exception if unsuccessful
     */
public void runDownloadMultipleAirplaneModeEnableDisable() throws Exception {
    String filename = DOWNLOAD_FILENAME;
    long filesize = DOWNLOAD_FILESIZE;
    // make sure WiFi is enabled, and airplane mode is not on
    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 on Airplane mode...");
        setAirplaneModeOn(true);
        // wait 1 minute
        Thread.sleep(60 * 1000);
        // download enable
        Log.i(LOG_TAG, "Turning off Airplane mode...");
        setAirplaneModeOn(false);
        // make sure we're starting to download some data...
        waitToReceiveData(dlRequest, EXPECTED_PROGRESS);
        // reenable the connection to start up the download again
        Log.i(LOG_TAG, "Turning on Airplane mode again...");
        setAirplaneModeOn(true);
        // wait 20 seconds
        Thread.sleep(20 * 1000);
        // Finish up the download...
        Log.i(LOG_TAG, "Turning off Airplane mode again...");
        setAirplaneModeOn(false);
        Log.i(LOG_TAG, "Waiting up to 10 minutes for donwload to complete...");
        // wait up to 10 mins
        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();
    }
}
Also used : Request(android.app.DownloadManager.Request) ParcelFileDescriptor(android.os.ParcelFileDescriptor) File(java.io.File) Uri(android.net.Uri)

Example 40 with Request

use of android.app.DownloadManager.Request in project platform_frameworks_base by android.

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();
    }
}
Also used : Request(android.app.DownloadManager.Request) ParcelFileDescriptor(android.os.ParcelFileDescriptor) File(java.io.File) Uri(android.net.Uri)

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