Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ceb99ec5 authored by Vasu Nori's avatar Vasu Nori Committed by Android (Google) Code Review
Browse files

Merge "fix downloadmanager tests. add few more. fix some. isolate flaky ones"

parents 051c7515 82e891b3
Loading
Loading
Loading
Loading
+3 −0
Original line number Original line Diff line number Diff line
@@ -36,6 +36,9 @@
            android:description="@string/permdesc_testDenied" />
            android:description="@string/permdesc_testDenied" />


    <uses-permission android:name="android.permission.ACCESS_CACHE_FILESYSTEM" />
    <uses-permission android:name="android.permission.ACCESS_CACHE_FILESYSTEM" />
    <uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER" />
    <uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER_ADVANCED" />
    <uses-permission android:name="android.permission.DOWNLOAD_CACHE_NON_PURGEABLE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
+56 −21
Original line number Original line Diff line number Diff line
@@ -16,8 +16,12 @@


package android.app;
package android.app;


import coretestutils.http.MockResponse;
import coretestutils.http.MockWebServer;

import android.app.DownloadManager.Query;
import android.app.DownloadManager.Query;
import android.app.DownloadManager.Request;
import android.app.DownloadManager.Request;
import android.app.DownloadManagerBaseTest.DataType;
import android.content.BroadcastReceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Context;
import android.content.Intent;
import android.content.Intent;
@@ -27,11 +31,10 @@ import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo;
import android.net.Uri;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Environment;
import android.os.Environment;
import android.os.ParcelFileDescriptor;
import android.os.ParcelFileDescriptor;
import android.os.SystemClock;
import android.os.ParcelFileDescriptor.AutoCloseInputStream;
import android.os.ParcelFileDescriptor.AutoCloseInputStream;
import android.os.SystemClock;
import android.provider.Settings;
import android.provider.Settings;
import android.test.InstrumentationTestCase;
import android.test.InstrumentationTestCase;
import android.util.Log;
import android.util.Log;
@@ -43,19 +46,12 @@ import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.IOException;
import java.net.URL;
import java.net.URL;
import java.util.concurrent.TimeoutException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Collections;
import java.util.HashSet;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Random;
import java.util.Set;
import java.util.Set;
import java.util.Vector;
import java.util.concurrent.TimeoutException;

import junit.framework.AssertionFailedError;

import coretestutils.http.MockResponse;
import coretestutils.http.MockWebServer;


/**
/**
 * Base class for Instrumented tests for the Download Manager.
 * Base class for Instrumented tests for the Download Manager.
@@ -67,7 +63,7 @@ public class DownloadManagerBaseTest extends InstrumentationTestCase {
    protected String mFileType = "text/plain";
    protected String mFileType = "text/plain";
    protected Context mContext = null;
    protected Context mContext = null;
    protected MultipleDownloadsCompletedReceiver mReceiver = null;
    protected MultipleDownloadsCompletedReceiver mReceiver = null;
    protected static final int DEFAULT_FILE_SIZE = 130 * 1024;  // 130kb
    protected static final int DEFAULT_FILE_SIZE = 10 * 1024;  // 10kb
    protected static final int FILE_BLOCK_READ_SIZE = 1024 * 1024;
    protected static final int FILE_BLOCK_READ_SIZE = 1024 * 1024;


    protected static final String LOG_TAG = "android.net.DownloadManagerBaseTest";
    protected static final String LOG_TAG = "android.net.DownloadManagerBaseTest";
@@ -85,6 +81,9 @@ public class DownloadManagerBaseTest extends InstrumentationTestCase {
    protected static final int MAX_WAIT_FOR_DOWNLOAD_TIME = 5 * 60 * 1000; // 5 minutes
    protected static final int MAX_WAIT_FOR_DOWNLOAD_TIME = 5 * 60 * 1000; // 5 minutes
    protected static final int MAX_WAIT_FOR_LARGE_DOWNLOAD_TIME = 15 * 60 * 1000; // 15 minutes
    protected static final int MAX_WAIT_FOR_LARGE_DOWNLOAD_TIME = 15 * 60 * 1000; // 15 minutes


    protected static final int DOWNLOAD_TO_SYSTEM_CACHE = 1;
    protected static final int DOWNLOAD_TO_DOWNLOAD_CACHE_DIR = 2;

    // Just a few popular file types used to return from a download
    // Just a few popular file types used to return from a download
    protected enum DownloadFileType {
    protected enum DownloadFileType {
        PLAINTEXT,
        PLAINTEXT,
@@ -888,30 +887,46 @@ public class DownloadManagerBaseTest extends InstrumentationTestCase {
     */
     */
    protected void removeAllCurrentDownloads() {
    protected void removeAllCurrentDownloads() {
        Log.i(LOG_TAG, "Removing all current registered downloads...");
        Log.i(LOG_TAG, "Removing all current registered downloads...");
        ArrayList<Long> ids = new ArrayList<Long>();
        Cursor cursor = mDownloadManager.query(new Query());
        Cursor cursor = mDownloadManager.query(new Query());
        try {
        try {
            if (cursor.moveToFirst()) {
            if (cursor.moveToFirst()) {
                do {
                do {
                    int index = cursor.getColumnIndex(DownloadManager.COLUMN_ID);
                    int index = cursor.getColumnIndex(DownloadManager.COLUMN_ID);
                    long downloadId = cursor.getLong(index);
                    long downloadId = cursor.getLong(index);

                    ids.add(downloadId);
                    mDownloadManager.remove(downloadId);
                } while (cursor.moveToNext());
                } while (cursor.moveToNext());
            }
            }
        } finally {
        } finally {
            cursor.close();
            cursor.close();
        }
        }
        // delete all ids
        for (long id : ids) {
            mDownloadManager.remove(id);
        }
        // make sure the database is empty
        cursor = mDownloadManager.query(new Query());
        try {
            assertEquals(0, cursor.getCount());
        } finally {
            cursor.close();
        }
    }
    }


    /**
    /**
     * Helper to perform a standard enqueue of data to the mock server.
     * Helper to perform a standard enqueue of data to the mock server.
     * download is performed to the downloads cache dir (NOT systemcache dir)
     *
     *
     * @param body The body to return in the response from the server
     * @param body The body to return in the response from the server
     */
     */
    protected long doStandardEnqueue(byte[] body) throws Exception {
    protected long doStandardEnqueue(byte[] body) throws Exception {
        return enqueueDownloadRequest(body, DOWNLOAD_TO_DOWNLOAD_CACHE_DIR);
    }

    protected long enqueueDownloadRequest(byte[] body, int location) throws Exception {
        // Prepare the mock server with a standard response
        // Prepare the mock server with a standard response
        enqueueResponse(HTTP_OK, body);
        enqueueResponse(HTTP_OK, body);
        return doCommonStandardEnqueue();
        return doEnqueue(location);
    }
    }


    /**
    /**
@@ -920,9 +935,13 @@ public class DownloadManagerBaseTest extends InstrumentationTestCase {
     * @param body The body to return in the response from the server, contained in the file
     * @param body The body to return in the response from the server, contained in the file
     */
     */
    protected long doStandardEnqueue(File body) throws Exception {
    protected long doStandardEnqueue(File body) throws Exception {
        return enqueueDownloadRequest(body, DOWNLOAD_TO_DOWNLOAD_CACHE_DIR);
    }

    protected long enqueueDownloadRequest(File body, int location) throws Exception {
        // Prepare the mock server with a standard response
        // Prepare the mock server with a standard response
        enqueueResponse(HTTP_OK, body);
        enqueueResponse(HTTP_OK, body);
        return doCommonStandardEnqueue();
        return doEnqueue(location);
    }
    }


    /**
    /**
@@ -930,13 +949,17 @@ public class DownloadManagerBaseTest extends InstrumentationTestCase {
     * doing a standard enqueue request to the server.
     * doing a standard enqueue request to the server.
     */
     */
    protected long doCommonStandardEnqueue() throws Exception {
    protected long doCommonStandardEnqueue() throws Exception {
        return doEnqueue(DOWNLOAD_TO_DOWNLOAD_CACHE_DIR);
    }

    private long doEnqueue(int location) throws Exception {
        Uri uri = getServerUri(DEFAULT_FILENAME);
        Uri uri = getServerUri(DEFAULT_FILENAME);
        Request request = new Request(uri);
        Request request = new Request(uri).setTitle(DEFAULT_FILENAME);
        request.setTitle(DEFAULT_FILENAME);
        if (location == DOWNLOAD_TO_SYSTEM_CACHE) {
            request.setDestinationToSystemCache();
        }


        long dlRequest = mDownloadManager.enqueue(request);
        return mDownloadManager.enqueue(request);
        Log.i(LOG_TAG, "request ID: " + dlRequest);
        return dlRequest;
    }
    }


    /**
    /**
@@ -997,4 +1020,16 @@ public class DownloadManagerBaseTest extends InstrumentationTestCase {
        return cursor;
        return cursor;
    }
    }


    /**
     * Helper that does the actual basic download verification.
     */
    protected long doBasicDownload(byte[] blobData, int location) throws Exception {
        long dlRequest = enqueueDownloadRequest(blobData, location);

        // wait for the download to complete
        waitForDownloadOrTimeout(dlRequest);

        assertEquals(1, mReceiver.numDownloadsCompleted());
        return dlRequest;
    }
}
}
 No newline at end of file
+215 −225

File changed and moved.

Preview size limit exceeded, changes collapsed.

+23 −30
Original line number Original line Diff line number Diff line
@@ -16,19 +16,21 @@


package android.app;
package android.app;


import java.io.File;
import java.util.Random;


import android.app.DownloadManager.Query;
import android.app.DownloadManager.Query;
import android.app.DownloadManager.Request;
import android.app.DownloadManager.Request;
import android.database.Cursor;
import android.database.Cursor;
import android.net.Uri;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.os.ParcelFileDescriptor;
import android.test.suitebuilder.annotation.LargeTest;
import android.test.suitebuilder.annotation.Suppress;
import android.util.Log;
import android.util.Log;


import java.io.File;
import java.util.Random;



public class DownloadManagerStressTest extends DownloadManagerBaseTest {
public class DownloadManagerStressTest extends DownloadManagerBaseTest {
    private static String LOG_TAG = "android.net.DownloadManagerStressTest";


    /**
    /**
     * {@inheritDoc}
     * {@inheritDoc}
@@ -41,9 +43,26 @@ public class DownloadManagerStressTest extends DownloadManagerBaseTest {
        removeAllCurrentDownloads();
        removeAllCurrentDownloads();
    }
    }


    /**
     * {@inheritDoc}
     */
    @Override
    public void tearDown() throws Exception {
        super.tearDown();
        setWiFiStateOn(true);
        removeAllCurrentDownloads();

        if (mReceiver != null) {
            mContext.unregisterReceiver(mReceiver);
            mReceiver = null;
        }
    }

    /**
    /**
     * Attempts to downloading thousands of files simultaneously
     * Attempts to downloading thousands of files simultaneously
     * don't run this test - downloadmanager needs to allow only a few simultaneous downloads.
     */
     */
    @Suppress
    public void testDownloadThousands() throws Exception {
    public void testDownloadThousands() throws Exception {
        int NUM_FILES = 1500;
        int NUM_FILES = 1500;
        int MAX_FILE_SIZE = 3000;
        int MAX_FILE_SIZE = 3000;
@@ -106,6 +125,7 @@ public class DownloadManagerStressTest extends DownloadManagerBaseTest {
    /**
    /**
     * Tests trying to download a large file (50M bytes).
     * Tests trying to download a large file (50M bytes).
     */
     */
    @LargeTest
    public void testDownloadLargeFile() throws Exception {
    public void testDownloadLargeFile() throws Exception {
        long fileSize = 50000000L;  // note: kept relatively small to not exceed /cache dir size
        long fileSize = 50000000L;  // note: kept relatively small to not exceed /cache dir size
        File largeFile = createFileOnSD(null, fileSize, DataType.TEXT, null);
        File largeFile = createFileOnSD(null, fileSize, DataType.TEXT, null);
@@ -129,31 +149,4 @@ public class DownloadManagerStressTest extends DownloadManagerBaseTest {
            largeFile.delete();
            largeFile.delete();
        }
        }
    }
    }

    /**
     * Tests trying to download a large file (~600M bytes) when there's not enough space in cache
     */
    public void testInsufficientSpace() throws Exception {
        // @TODO: Rework this to fill up cache partition with a dynamically calculated size
        long fileSize = 600000000L;
        File largeFile = createFileOnSD(null, fileSize, DataType.TEXT, null);

        Cursor cursor = null;
        try {
            long dlRequest = doStandardEnqueue(largeFile);

             // wait for the download to complete
            waitForDownloadOrTimeout(dlRequest);

            cursor = getCursor(dlRequest);
            verifyInt(cursor, DownloadManager.COLUMN_STATUS, DownloadManager.STATUS_FAILED);
            verifyInt(cursor, DownloadManager.COLUMN_REASON,
                    DownloadManager.ERROR_INSUFFICIENT_SPACE);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            largeFile.delete();
        }
    }
}
}
+1 −2
Original line number Original line Diff line number Diff line
@@ -18,8 +18,7 @@ include $(CLEAR_VARS)


LOCAL_MODULE_TAGS := tests
LOCAL_MODULE_TAGS := tests


LOCAL_SRC_FILES := $(call all-java-files-under, src) \
LOCAL_SRC_FILES := $(call all-java-files-under, src)
				   ../../../coretests/src/android/app/DownloadManagerBaseTest.java


LOCAL_STATIC_JAVA_LIBRARIES := android-common frameworks-core-util-lib
LOCAL_STATIC_JAVA_LIBRARIES := android-common frameworks-core-util-lib
LOCAL_SDK_VERSION := current
LOCAL_SDK_VERSION := current
Loading