diff options
| author | Jean-Baptiste Queru <jbq@google.com> | 2010-09-08 10:28:41 -0700 |
|---|---|---|
| committer | Android Git Automerger <android-git-automerger@android.com> | 2010-09-08 10:28:41 -0700 |
| commit | 980bd4a12d70e87cdab3c7b39b5b1d7828913933 (patch) | |
| tree | 6722a2a52983e523d925696939893779212bb0ee /core/tests/hosttests | |
| parent | e6fac0df113b04db68a065242bdcee1faee917b4 (diff) | |
| parent | 84f992a249491b9f755e879bf096baac3f9472f5 (diff) | |
| download | frameworks_base-980bd4a12d70e87cdab3c7b39b5b1d7828913933.zip frameworks_base-980bd4a12d70e87cdab3c7b39b5b1d7828913933.tar.gz frameworks_base-980bd4a12d70e87cdab3c7b39b5b1d7828913933.tar.bz2 | |
am 84f992a2: resolve conflicts
Merge commit '84f992a249491b9f755e879bf096baac3f9472f5'
* commit '84f992a249491b9f755e879bf096baac3f9472f5':
Adding Download Manager Integration, stress, and hosts-based tests.
Diffstat (limited to 'core/tests/hosttests')
6 files changed, 838 insertions, 7 deletions
diff --git a/core/tests/hosttests/src/android/content/pm/PackageManagerHostTestUtils.java b/core/tests/hosttests/src/android/content/pm/PackageManagerHostTestUtils.java index 38191b0..f962bab 100644 --- a/core/tests/hosttests/src/android/content/pm/PackageManagerHostTestUtils.java +++ b/core/tests/hosttests/src/android/content/pm/PackageManagerHostTestUtils.java @@ -37,7 +37,9 @@ import java.io.InputStreamReader; import java.io.StringReader; import java.lang.Runtime; import java.lang.Process; +import java.util.Hashtable; import java.util.Map; +import java.util.Map.Entry; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -117,7 +119,14 @@ public class PackageManagerHostTestUtils extends Assert { /** * Helper method to run tests and return the listener that collected the results. + * + * For the optional params, pass null to use the default values. + * @param pkgName Android application package for tests + * @param className (optional) The class containing the method to test + * @param methodName (optional) The method in the class of which to test + * @param runnerName (optional) The name of the TestRunner of the test on the device to be run + * @param params (optional) Any additional parameters to pass into the Test Runner * @return the {@link CollectingTestRunListener} * @throws TimeoutException in case of a timeout on the connection. * @throws AdbCommandRejectedException if adb rejects the command @@ -125,10 +134,24 @@ public class PackageManagerHostTestUtils extends Assert { * a period longer than the max time to output. * @throws IOException if connection to device was lost. */ - private CollectingTestRunListener doRunTests(String pkgName) throws IOException, - TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException { - RemoteAndroidTestRunner testRunner = new RemoteAndroidTestRunner( - pkgName, mDevice); + private CollectingTestRunListener doRunTests(String pkgName, String className, String + methodName, String runnerName, Map<String, String> params) throws IOException, + TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException { + + RemoteAndroidTestRunner testRunner = new RemoteAndroidTestRunner(pkgName, runnerName, + mDevice); + + if (className != null && methodName != null) { + testRunner.setMethodName(className, methodName); + } + + // Add in any additional args to pass into the test + if (params != null) { + for (Entry<String, String> argPair : params.entrySet()) { + testRunner.addInstrumentationArg(argPair.getKey(), argPair.getValue()); + } + } + CollectingTestRunListener listener = new CollectingTestRunListener(); try { testRunner.run(listener); @@ -142,6 +165,23 @@ public class PackageManagerHostTestUtils extends Assert { * Runs the specified packages tests, and returns whether all tests passed or not. * * @param pkgName Android application package for tests + * @param className The class containing the method to test + * @param methodName The method in the class of which to test + * @param runnerName The name of the TestRunner of the test on the device to be run + * @param params Any additional parameters to pass into the Test Runner + * @return true if test passed, false otherwise. + */ + public boolean runDeviceTestsDidAllTestsPass(String pkgName, String className, + String methodName, String runnerName, Map<String, String> params) throws IOException { + CollectingTestRunListener listener = doRunTests(pkgName, className, methodName, + runnerName, params); + return listener.didAllTestsPass(); + } + + /** + * Runs the specified packages tests, and returns whether all tests passed or not. + * + * @param pkgName Android application package for tests * @return true if every test passed, false otherwise. * @throws TimeoutException in case of a timeout on the connection. * @throws AdbCommandRejectedException if adb rejects the command @@ -149,9 +189,9 @@ public class PackageManagerHostTestUtils extends Assert { * a period longer than the max time to output. * @throws IOException if connection to device was lost. */ - public boolean runDeviceTestsDidAllTestsPass(String pkgName) throws IOException, + public boolean runDeviceTestsDidAllTestsPass(String pkgName) throws IOException, TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException { - CollectingTestRunListener listener = doRunTests(pkgName); + CollectingTestRunListener listener = doRunTests(pkgName, null, null, null, null); return listener.didAllTestsPass(); } @@ -535,7 +575,7 @@ public class PackageManagerHostTestUtils extends Assert { } // For collecting results from running device tests - private static class CollectingTestRunListener implements ITestRunListener { + public static class CollectingTestRunListener implements ITestRunListener { private boolean mAllTestsPassed = true; private String mTestRunErrorMessage = null; diff --git a/core/tests/hosttests/src/android/net/DownloadManagerHostTests.java b/core/tests/hosttests/src/android/net/DownloadManagerHostTests.java new file mode 100644 index 0000000..ed280c9 --- /dev/null +++ b/core/tests/hosttests/src/android/net/DownloadManagerHostTests.java @@ -0,0 +1,201 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.net; + +import android.content.pm.PackageManagerHostTestUtils; +import android.content.pm.PackageManagerHostTestUtils.CollectingTestRunListener; + +import com.android.ddmlib.IDevice; +import com.android.ddmlib.IShellOutputReceiver; +import com.android.ddmlib.Log; +import com.android.ddmlib.MultiLineReceiver; +import com.android.ddmlib.SyncService; +import com.android.ddmlib.SyncService.ISyncProgressMonitor; +import com.android.ddmlib.SyncService.SyncResult; +import com.android.hosttest.DeviceTestCase; +import com.android.hosttest.DeviceTestSuite; + +import java.io.File; +import java.io.IOException; +import java.util.Hashtable; + +import junit.framework.Test; + +/** + * Host-based tests of the DownloadManager API. (Uses a device-based app to actually invoke the + * various tests.) + */ +public class DownloadManagerHostTests extends DeviceTestCase { + protected PackageManagerHostTestUtils mPMUtils = null; + + private static final String LOG_TAG = "android.net.DownloadManagerHostTests"; + private static final String FILE_DOWNLOAD_APK = "DownloadManagerTestApp.apk"; + private static final String FILE_DOWNLOAD_PKG = "com.android.frameworks.downloadmanagertests"; + private static final String FILE_DOWNLOAD_CLASS = + "com.android.frameworks.downloadmanagertests.DownloadManagerTestApp"; + private static final String DOWNLOAD_TEST_RUNNER_NAME = + "com.android.frameworks.downloadmanagertests.DownloadManagerTestRunner"; + + // Extra parameters to pass to the TestRunner + private static final String EXTERNAL_DOWNLOAD_URI_KEY = "external_download_uri"; + // Note: External environment variable ANDROID_TEST_EXTERNAL_URI must be set to point to the + // external URI under which the files downloaded by the tests can be found. Note that the Uri + // must be accessible by the device during a test run. + private static String EXTERNAL_DOWNLOAD_URI_VALUE = null; + + Hashtable<String, String> mExtraParams = null; + + public static Test suite() { + return new DeviceTestSuite(DownloadManagerHostTests.class); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + // ensure apk path has been set before test is run + assertNotNull(getTestAppPath()); + mPMUtils = new PackageManagerHostTestUtils(getDevice()); + EXTERNAL_DOWNLOAD_URI_VALUE = System.getenv("ANDROID_TEST_EXTERNAL_URI"); + assertNotNull(EXTERNAL_DOWNLOAD_URI_VALUE); + mExtraParams = getExtraParams(); + } + + /** + * Helper function to get extra params that can be used to pass into the helper app. + */ + protected Hashtable<String, String> getExtraParams() { + Hashtable<String, String> extraParams = new Hashtable<String, String>(); + extraParams.put(EXTERNAL_DOWNLOAD_URI_KEY, EXTERNAL_DOWNLOAD_URI_VALUE); + return extraParams; + } + + /** + * Tests that a large download over WiFi + * @throws Exception if the test failed at any point + */ + public void testLargeDownloadOverWiFi() throws Exception { + mPMUtils.installAppAndVerifyExistsOnDevice(String.format("%s%s%s", getTestAppPath(), + File.separator, FILE_DOWNLOAD_APK), FILE_DOWNLOAD_PKG, true); + + boolean testPassed = mPMUtils.runDeviceTestsDidAllTestsPass(FILE_DOWNLOAD_PKG, + FILE_DOWNLOAD_CLASS, "runLargeDownloadOverWiFi", DOWNLOAD_TEST_RUNNER_NAME, + mExtraParams); + + assertTrue("Failed to install large file over WiFi in < 10 minutes!", testPassed); + } + + /** + * Spawns a device-based function to initiate a download on the device, reboots the device, + * then waits and verifies the download succeeded. + * + * @throws Exception if the test failed at any point + */ + public void testDownloadManagerSingleReboot() throws Exception { + mPMUtils.installAppAndVerifyExistsOnDevice(String.format("%s%s%s", getTestAppPath(), + File.separator, FILE_DOWNLOAD_APK), FILE_DOWNLOAD_PKG, true); + + boolean testPassed = mPMUtils.runDeviceTestsDidAllTestsPass(FILE_DOWNLOAD_PKG, + FILE_DOWNLOAD_CLASS, "initiateDownload", DOWNLOAD_TEST_RUNNER_NAME, + mExtraParams); + + assertTrue("Failed to initiate download properly!", testPassed); + mPMUtils.rebootDevice(); + testPassed = mPMUtils.runDeviceTestsDidAllTestsPass(FILE_DOWNLOAD_PKG, + FILE_DOWNLOAD_CLASS, "verifyFileDownloadSucceeded", DOWNLOAD_TEST_RUNNER_NAME, + mExtraParams); + assertTrue("Failed to verify initiated download completed properyly!", testPassed); + } + + /** + * Spawns a device-based function to initiate a download on the device, reboots the device three + * times (using different intervals), then waits and verifies the download succeeded. + * + * @throws Exception if the test failed at any point + */ + public void testDownloadManagerMultipleReboots() throws Exception { + mPMUtils.installAppAndVerifyExistsOnDevice(String.format("%s%s%s", getTestAppPath(), + File.separator, FILE_DOWNLOAD_APK), FILE_DOWNLOAD_PKG, true); + + boolean testPassed = mPMUtils.runDeviceTestsDidAllTestsPass(FILE_DOWNLOAD_PKG, + FILE_DOWNLOAD_CLASS, "initiateDownload", DOWNLOAD_TEST_RUNNER_NAME, + mExtraParams); + + assertTrue("Failed to initiate download properly!", testPassed); + Thread.sleep(5000); + + // Do 3 random reboots - after 13, 9, and 19 seconds + Log.i(LOG_TAG, "First reboot..."); + mPMUtils.rebootDevice(); + Thread.sleep(13000); + Log.i(LOG_TAG, "Second reboot..."); + mPMUtils.rebootDevice(); + Thread.sleep(9000); + Log.i(LOG_TAG, "Third reboot..."); + mPMUtils.rebootDevice(); + Thread.sleep(19000); + testPassed = mPMUtils.runDeviceTestsDidAllTestsPass(FILE_DOWNLOAD_PKG, + FILE_DOWNLOAD_CLASS, "verifyFileDownloadSucceeded", DOWNLOAD_TEST_RUNNER_NAME, + mExtraParams); + assertTrue("Failed to verify initiated download completed properyly!", testPassed); + } + + /** + * Spawns a device-based function to test download while WiFi is enabled/disabled multiple times + * during the download. + * + * @throws Exception if the test failed at any point + */ + public void testDownloadMultipleWiFiEnableDisable() throws Exception { + mPMUtils.installAppAndVerifyExistsOnDevice(String.format("%s%s%s", getTestAppPath(), + File.separator, FILE_DOWNLOAD_APK), FILE_DOWNLOAD_PKG, true); + + boolean testPassed = mPMUtils.runDeviceTestsDidAllTestsPass(FILE_DOWNLOAD_PKG, + FILE_DOWNLOAD_CLASS, "runDownloadMultipleWiFiEnableDisable", + DOWNLOAD_TEST_RUNNER_NAME, mExtraParams); + assertTrue(testPassed); + } + + /** + * Spawns a device-based function to test switching on/off both airplane mode and WiFi + * + * @throws Exception if the test failed at any point + */ + public void testDownloadMultipleSwitching() throws Exception { + mPMUtils.installAppAndVerifyExistsOnDevice(String.format("%s%s%s", getTestAppPath(), + File.separator, FILE_DOWNLOAD_APK), FILE_DOWNLOAD_PKG, true); + + boolean testPassed = mPMUtils.runDeviceTestsDidAllTestsPass(FILE_DOWNLOAD_PKG, + FILE_DOWNLOAD_CLASS, "runDownloadMultipleSwitching", + DOWNLOAD_TEST_RUNNER_NAME, mExtraParams); + assertTrue(testPassed); + } + + /** + * Spawns a device-based function to test switching on/off airplane mode multiple times + * + * @throws Exception if the test failed at any point + */ + public void testDownloadMultipleAirplaneModeEnableDisable() throws Exception { + mPMUtils.installAppAndVerifyExistsOnDevice(String.format("%s%s%s", getTestAppPath(), + File.separator, FILE_DOWNLOAD_APK), FILE_DOWNLOAD_PKG, true); + + boolean testPassed = mPMUtils.runDeviceTestsDidAllTestsPass(FILE_DOWNLOAD_PKG, + FILE_DOWNLOAD_CLASS, "runDownloadMultipleAirplaneModeEnableDisable", + DOWNLOAD_TEST_RUNNER_NAME, mExtraParams); + assertTrue(testPassed); + } +} diff --git a/core/tests/hosttests/test-apps/DownloadManagerTestApp/Android.mk b/core/tests/hosttests/test-apps/DownloadManagerTestApp/Android.mk new file mode 100644 index 0000000..576765c --- /dev/null +++ b/core/tests/hosttests/test-apps/DownloadManagerTestApp/Android.mk @@ -0,0 +1,29 @@ +# Copyright (C) 2010 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +LOCAL_PATH:= $(call my-dir) + +include $(CLEAR_VARS) + +LOCAL_MODULE_TAGS := tests + +LOCAL_SRC_FILES := $(call all-java-files-under, src) \ + ../../../coretests/src/android/net/DownloadManagerBaseTest.java + +LOCAL_STATIC_JAVA_LIBRARIES := android-common frameworks-core-util-lib +LOCAL_SDK_VERSION := current + +LOCAL_PACKAGE_NAME := DownloadManagerTestApp + +include $(BUILD_PACKAGE) diff --git a/core/tests/hosttests/test-apps/DownloadManagerTestApp/AndroidManifest.xml b/core/tests/hosttests/test-apps/DownloadManagerTestApp/AndroidManifest.xml new file mode 100644 index 0000000..3f2be3c --- /dev/null +++ b/core/tests/hosttests/test-apps/DownloadManagerTestApp/AndroidManifest.xml @@ -0,0 +1,35 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2010 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.android.frameworks.downloadmanagertests"> + + <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> + <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> + <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> + <uses-permission android:name="android.permission.INTERNET" /> + <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> + <uses-permission android:name="android.permission.WRITE_SETTINGS" /> + + <application android:label="DownloadManagerTestApp"> + <uses-library android:name="android.test.runner" /> + </application> + + <instrumentation + android:name=".DownloadManagerTestRunner" + android:targetPackage="com.android.frameworks.downloadmanagertests" + android:label="Frameworks Download Manager Test App" /> + +</manifest> diff --git a/core/tests/hosttests/test-apps/DownloadManagerTestApp/src/com/android/frameworks/DownloadManagerTestApp.java b/core/tests/hosttests/test-apps/DownloadManagerTestApp/src/com/android/frameworks/DownloadManagerTestApp.java new file mode 100644 index 0000000..ef81353 --- /dev/null +++ b/core/tests/hosttests/test-apps/DownloadManagerTestApp/src/com/android/frameworks/DownloadManagerTestApp.java @@ -0,0 +1,463 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.frameworks.downloadmanagertests; + +import android.content.Context; +import android.content.Intent; +import android.database.Cursor; +import android.net.DownloadManager; +import android.net.DownloadManager.Query; +import android.net.DownloadManager.Request; +import android.net.DownloadManagerBaseTest; +import android.net.Uri; +import android.os.Environment; +import android.os.ParcelFileDescriptor; +import android.provider.Settings; +import android.test.suitebuilder.annotation.LargeTest; +import android.util.Log; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.FileWriter; + +import coretestutils.http.MockResponse; +import coretestutils.http.MockWebServer; +import coretestutils.http.RecordedRequest; + +/** + * Class to test downloading files from a real (not mock) external server. + */ +public class DownloadManagerTestApp extends DownloadManagerBaseTest { + protected static String DOWNLOAD_STARTED_FLAG = "DMTEST_DOWNLOAD_STARTED"; + protected static String LOG_TAG = + "com.android.frameworks.downloadmanagertests.DownloadManagerTestApp"; + + protected static String DOWNLOAD_500K_FILENAME = "External541kb.apk"; + protected static long DOWNLOAD_500K_FILESIZE = 570927; + protected static String DOWNLOAD_1MB_FILENAME = "External1mb.apk"; + protected static long DOWNLOAD_1MB_FILESIZE = 1041262; + protected static String DOWNLOAD_10MB_FILENAME = "External10mb.apk"; + protected static long DOWNLOAD_10MB_FILESIZE = 10258741; + + // Values to be obtained from TestRunner + private String externalDownloadUriValue = null; + + /** + * {@inheritDoc } + */ + @Override + public void setUp() throws Exception { + super.setUp(); + DownloadManagerTestRunner mRunner = (DownloadManagerTestRunner)getInstrumentation(); + externalDownloadUriValue = mRunner.externalDownloadUriValue; + assertNotNull(externalDownloadUriValue); + + if (!externalDownloadUriValue.endsWith("/")) { + externalDownloadUriValue += "/"; + } + } + + /** + * Gets the external URL of the file to download + * + * @return the Uri of the external file to download + */ + private Uri getExternalFileUri(String file) { + return Uri.parse(externalDownloadUriValue + file); + } + + /** + * Gets the path to the file that flags that a download has started. The file contains the + * DownloadManager id of the download being trackted between reboot sessions. + * + * @return The path of the file tracking that a download has started + * @throws InterruptedException if interrupted + * @throws Exception if timed out while waiting for SD card to mount + */ + protected String getDownloadStartedFilePath() { + String path = Environment.getExternalStorageDirectory().getPath(); + return path + File.separatorChar + DOWNLOAD_STARTED_FLAG; + } + + /** + * Common setup steps for downloads. + * + * Note that these are not included in setUp, so that individual tests can control their own + * state between reboots, etc. + */ + protected void doCommonDownloadSetup() throws Exception { + setWiFiStateOn(true); + setAirplaneModeOn(false); + waitForExternalStoreMount(); + removeAllCurrentDownloads(); + } + + /** + * 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_1MB_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(dlRequest != -1); + + // Store ID of download for later retrieval + outputFile = new DataOutputStream(fileOutput); + outputFile.writeLong(dlRequest); + } finally { + if (outputFile != null) { + outputFile.flush(); + outputFile.close(); + } + } + } + + /** + * Waits for a previously-initiated download and verifies it has completed successfully. + * + * @throws Exception if unsuccessful + */ + public void verifyFileDownloadSucceeded() throws Exception { + String filename = DOWNLOAD_1MB_FILENAME; + long filesize = DOWNLOAD_1MB_FILESIZE; + long dlRequest = -1; + boolean rebootMarkerValid = false; + DataInputStream dataInputFile = null; + + setWiFiStateOn(true); + setAirplaneModeOn(false); + + try { + FileInputStream inFile = mContext.openFileInput(DOWNLOAD_STARTED_FLAG); + dataInputFile = new DataInputStream(inFile); + dlRequest = dataInputFile.readLong(); + } catch (Exception e) { + // The file was't valid so we just leave the flag false + Log.i(LOG_TAG, "Unable to determine initial download id."); + throw e; + } finally { + if (dataInputFile != null) { + dataInputFile.close(); + } + mContext.deleteFile(DOWNLOAD_STARTED_FLAG); + } + + assertTrue(dlRequest != -1); + Cursor cursor = getCursor(dlRequest); + ParcelFileDescriptor pfd = null; + try { + assertTrue("Unable to query last initiated download!", cursor.moveToFirst()); + + int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS); + int status = cursor.getInt(columnIndex); + int currentWaitTime = 0; + + // Wait until the download finishes + waitForDownloadOrTimeout(dlRequest); + + Log.i(LOG_TAG, "Verifying download information..."); + // Verify specific info about the file (size, name, etc)... + pfd = mDownloadManager.openDownloadedFile(dlRequest); + verifyFileSize(pfd, filesize); + } catch (Exception e) { + Log.i(LOG_TAG, "error: " + e.toString()); + throw e; + } finally { + // Clean up... + cursor.close(); + mDownloadManager.remove(dlRequest); + if (pfd != null) { + pfd.close(); + } + } + } + + /** + * Tests downloading a large file over WiFi (~10 Mb). + * + * @throws Exception if unsuccessful + */ + public void runLargeDownloadOverWiFi() throws Exception { + String filename = DOWNLOAD_10MB_FILENAME; + long filesize = DOWNLOAD_10MB_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.setMediaType(getMimeMapping(DownloadFileType.APK)); + + dlRequest = mDownloadManager.enqueue(request); + + // Rather large file, so wait up to 15 mins... + waitForDownloadOrTimeout(dlRequest, WAIT_FOR_DOWNLOAD_POLL_TIME, 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(); + } + } + + /** + * Tests that downloads resume when switching back and forth from having connectivity to + * having no connectivity using both WiFi and airplane mode. + * + * Note: Device has no mobile access when running this test. + * + * @throws Exception if unsuccessful + */ + public void runDownloadMultipleSwitching() throws Exception { + String filename = DOWNLOAD_500K_FILENAME; + long filesize = DOWNLOAD_500K_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_MOBILE | Request.NETWORK_WIFI); + + dlRequest = mDownloadManager.enqueue(request); + waitForDownloadToStart(dlRequest); + // make sure we're starting to download some data... + waitForFileToGrow(downloadedFile); + + // download disable + setWiFiStateOn(false); + + // download disable + Log.i(LOG_TAG, "Turning on airplane mode..."); + setAirplaneModeOn(true); + Thread.sleep(30 * 1000); // wait 30 secs + + // download disable + setWiFiStateOn(true); + Thread.sleep(30 * 1000); // wait 30 secs + + // download enable + Log.i(LOG_TAG, "Turning off airplane mode..."); + setAirplaneModeOn(false); + Thread.sleep(5 * 1000); // wait 5 seconds + + // download disable + Log.i(LOG_TAG, "Turning off WiFi..."); + setWiFiStateOn(false); + Thread.sleep(30 * 1000); // wait 30 secs + + // finally, turn WiFi back on and finish up the download + Log.i(LOG_TAG, "Turning on WiFi..."); + setWiFiStateOn(true); + Log.i(LOG_TAG, "Waiting up to 3 minutes for download to complete..."); + waitForDownloadsOrTimeout(dlRequest, 3 * 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(); + } + } + + /** + * 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_500K_FILENAME; + long filesize = DOWNLOAD_500K_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? + waitForFileToGrow(downloadedFile); + + // download disable + Log.i(LOG_TAG, "Turning off WiFi..."); + setWiFiStateOn(false); + Thread.sleep(40 * 1000); // wait 40 seconds + + // enable download... + Log.i(LOG_TAG, "Turning on WiFi again..."); + setWiFiStateOn(true); + waitForFileToGrow(downloadedFile); + + // download disable + Log.i(LOG_TAG, "Turning off WiFi..."); + setWiFiStateOn(false); + Thread.sleep(20 * 1000); // wait 20 seconds + + // enable download... + Log.i(LOG_TAG, "Turning on WiFi again..."); + setWiFiStateOn(true); + + Log.i(LOG_TAG, "Waiting up to 3 minutes for download to complete..."); + waitForDownloadsOrTimeout(dlRequest, 3 * 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(); + } + } + + /** + * 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_500K_FILENAME; + long filesize = DOWNLOAD_500K_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? + waitForFileToGrow(downloadedFile); + + // download disable + Log.i(LOG_TAG, "Turning on Airplane mode..."); + setAirplaneModeOn(true); + Thread.sleep(60 * 1000); // wait 1 minute + + // download enable + Log.i(LOG_TAG, "Turning off Airplane mode..."); + setAirplaneModeOn(false); + // make sure we're starting to download some data... + waitForFileToGrow(downloadedFile); + + // reenable the connection to start up the download again + Log.i(LOG_TAG, "Turning on Airplane mode again..."); + setAirplaneModeOn(true); + Thread.sleep(20 * 1000); // wait 20 seconds + + // Finish up the download... + Log.i(LOG_TAG, "Turning off Airplane mode again..."); + setAirplaneModeOn(false); + + Log.i(LOG_TAG, "Waiting up to 3 minutes for donwload to complete..."); + waitForDownloadsOrTimeout(dlRequest, 180 * 1000); // wait up to 3 mins before timeout + ParcelFileDescriptor pfd = mDownloadManager.openDownloadedFile(dlRequest); + verifyFileSize(pfd, filesize); + } finally { + Log.i(LOG_TAG, "Cleaning up files..."); + if (dlRequest != -1) { + mDownloadManager.remove(dlRequest); + } + downloadedFile.delete(); + } + } +} diff --git a/core/tests/hosttests/test-apps/DownloadManagerTestApp/src/com/android/frameworks/DownloadManagerTestRunner.java b/core/tests/hosttests/test-apps/DownloadManagerTestApp/src/com/android/frameworks/DownloadManagerTestRunner.java new file mode 100644 index 0000000..0f16619 --- /dev/null +++ b/core/tests/hosttests/test-apps/DownloadManagerTestApp/src/com/android/frameworks/DownloadManagerTestRunner.java @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2010, The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.frameworks.downloadmanagertests; + +import android.os.Bundle; +import android.test.InstrumentationTestRunner; +import android.test.InstrumentationTestSuite; +import android.util.Log; + +import com.android.frameworks.downloadmanagertests.DownloadManagerTestApp; + +import junit.framework.TestSuite; + +/** + * Instrumentation Test Runner for all download manager tests. + * + * To run the download manager tests: + * + * adb shell am instrument -e external_download_1mb_uri <uri> external_download_500k_uri <uri> \ + * -w com.android.frameworks.downloadmanagertests/.DownloadManagerTestRunner + */ + +public class DownloadManagerTestRunner extends InstrumentationTestRunner { + private static final String EXTERNAL_DOWNLOAD_URI_KEY = "external_download_uri"; + public String externalDownloadUriValue = null; + + @Override + public TestSuite getAllTests() { + TestSuite suite = new InstrumentationTestSuite(this); + suite.addTestSuite(DownloadManagerTestApp.class); + return suite; + } + + @Override + public ClassLoader getLoader() { + return DownloadManagerTestRunner.class.getClassLoader(); + } + + @Override + public void onCreate(Bundle icicle) { + // Extract the extra params passed in from the bundle... + String externalDownloadUri = (String) icicle.get(EXTERNAL_DOWNLOAD_URI_KEY); + if (externalDownloadUri != null) { + externalDownloadUriValue = externalDownloadUri; + } + super.onCreate(icicle); + } + +} |
