Home | History | Annotate | Download | only in net
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.net;
     18 
     19 import android.content.pm.PackageManagerHostTestUtils;
     20 import android.content.pm.PackageManagerHostTestUtils.CollectingTestRunListener;
     21 
     22 import com.android.ddmlib.IDevice;
     23 import com.android.ddmlib.IShellOutputReceiver;
     24 import com.android.ddmlib.Log;
     25 import com.android.ddmlib.MultiLineReceiver;
     26 import com.android.ddmlib.SyncService;
     27 import com.android.ddmlib.SyncService.ISyncProgressMonitor;
     28 import com.android.ddmlib.SyncService.SyncResult;
     29 import com.android.hosttest.DeviceTestCase;
     30 import com.android.hosttest.DeviceTestSuite;
     31 
     32 import java.io.File;
     33 import java.io.IOException;
     34 import java.util.Hashtable;
     35 
     36 import junit.framework.Test;
     37 
     38 /**
     39  * Host-based tests of the DownloadManager API. (Uses a device-based app to actually invoke the
     40  * various tests.)
     41  */
     42 public class DownloadManagerHostTests extends DeviceTestCase {
     43     protected PackageManagerHostTestUtils mPMUtils = null;
     44 
     45     private static final String LOG_TAG = "android.net.DownloadManagerHostTests";
     46     private static final String FILE_DOWNLOAD_APK = "DownloadManagerTestApp.apk";
     47     private static final String FILE_DOWNLOAD_PKG = "com.android.frameworks.downloadmanagertests";
     48     private static final String FILE_DOWNLOAD_CLASS =
     49             "com.android.frameworks.downloadmanagertests.DownloadManagerTestApp";
     50     private static final String DOWNLOAD_TEST_RUNNER_NAME =
     51             "com.android.frameworks.downloadmanagertests.DownloadManagerTestRunner";
     52 
     53     // Extra parameters to pass to the TestRunner
     54     private static final String EXTERNAL_DOWNLOAD_URI_KEY = "external_download_uri";
     55     // Note: External environment variable ANDROID_TEST_EXTERNAL_URI must be set to point to the
     56     // external URI under which the files downloaded by the tests can be found. Note that the Uri
     57     // must be accessible by the device during a test run. Correspondingly,
     58     // ANDROID_TEST_EXTERNAL_LARGE_URI should point to the external URI of the folder containing
     59     // large files.
     60     private static String externalDownloadUriValue = null;
     61 
     62     Hashtable<String, String> mExtraParams = null;
     63 
     64     public static Test suite() {
     65         return new DeviceTestSuite(DownloadManagerHostTests.class);
     66     }
     67 
     68     @Override
     69     protected void setUp() throws Exception {
     70         super.setUp();
     71         // ensure apk path has been set before test is run
     72         assertNotNull(getTestAppPath());
     73         mPMUtils = new PackageManagerHostTestUtils(getDevice());
     74         externalDownloadUriValue = System.getenv("ANDROID_TEST_EXTERNAL_URI");
     75         assertNotNull(externalDownloadUriValue);
     76         mExtraParams = getExtraParams();
     77     }
     78 
     79     /**
     80      * Helper function to get extra params that can be used to pass into the helper app.
     81      */
     82     protected Hashtable<String, String> getExtraParams() {
     83         Hashtable<String, String> extraParams = new Hashtable<String, String>();
     84         extraParams.put(EXTERNAL_DOWNLOAD_URI_KEY, externalDownloadUriValue);
     85         return extraParams;
     86     }
     87 
     88     /**
     89      * Tests that a large download over WiFi
     90      * @throws Exception if the test failed at any point
     91      */
     92     public void testLargeDownloadOverWiFi() throws Exception {
     93         mPMUtils.installAppAndVerifyExistsOnDevice(String.format("%s%s%s", getTestAppPath(),
     94                 File.separator, FILE_DOWNLOAD_APK), FILE_DOWNLOAD_PKG, true);
     95 
     96         boolean testPassed = mPMUtils.runDeviceTestsDidAllTestsPass(FILE_DOWNLOAD_PKG,
     97                 FILE_DOWNLOAD_CLASS, "runLargeDownloadOverWiFi", DOWNLOAD_TEST_RUNNER_NAME,
     98                 mExtraParams);
     99 
    100         assertTrue("Failed to install large file over WiFi in < 10 minutes!", testPassed);
    101     }
    102 
    103     /**
    104      * Spawns a device-based function to initiate a download on the device, reboots the device,
    105      * then waits and verifies the download succeeded.
    106      *
    107      * @throws Exception if the test failed at any point
    108      */
    109     public void testDownloadManagerSingleReboot() throws Exception {
    110         mPMUtils.installAppAndVerifyExistsOnDevice(String.format("%s%s%s", getTestAppPath(),
    111                 File.separator, FILE_DOWNLOAD_APK), FILE_DOWNLOAD_PKG, true);
    112 
    113         boolean testPassed = mPMUtils.runDeviceTestsDidAllTestsPass(FILE_DOWNLOAD_PKG,
    114                 FILE_DOWNLOAD_CLASS, "initiateDownload", DOWNLOAD_TEST_RUNNER_NAME,
    115                 mExtraParams);
    116 
    117         assertTrue("Failed to initiate download properly!", testPassed);
    118         mPMUtils.rebootDevice();
    119         testPassed = mPMUtils.runDeviceTestsDidAllTestsPass(FILE_DOWNLOAD_PKG,
    120                 FILE_DOWNLOAD_CLASS, "verifyFileDownloadSucceeded", DOWNLOAD_TEST_RUNNER_NAME,
    121                 mExtraParams);
    122         assertTrue("Failed to verify initiated download completed properyly!", testPassed);
    123     }
    124 
    125     /**
    126      * Spawns a device-based function to initiate a download on the device, reboots the device three
    127      * times (using different intervals), then waits and verifies the download succeeded.
    128      *
    129      * @throws Exception if the test failed at any point
    130      */
    131     public void testDownloadManagerMultipleReboots() throws Exception {
    132         mPMUtils.installAppAndVerifyExistsOnDevice(String.format("%s%s%s", getTestAppPath(),
    133                 File.separator, FILE_DOWNLOAD_APK), FILE_DOWNLOAD_PKG, true);
    134 
    135         boolean testPassed = mPMUtils.runDeviceTestsDidAllTestsPass(FILE_DOWNLOAD_PKG,
    136                 FILE_DOWNLOAD_CLASS, "initiateDownload", DOWNLOAD_TEST_RUNNER_NAME,
    137                 mExtraParams);
    138 
    139         assertTrue("Failed to initiate download properly!", testPassed);
    140         Thread.sleep(5000);
    141 
    142         // Do 3 random reboots - after 13, 9, and 19 seconds
    143         Log.i(LOG_TAG, "First reboot...");
    144         mPMUtils.rebootDevice();
    145         Thread.sleep(13000);
    146         Log.i(LOG_TAG, "Second reboot...");
    147         mPMUtils.rebootDevice();
    148         Thread.sleep(9000);
    149         Log.i(LOG_TAG, "Third reboot...");
    150         mPMUtils.rebootDevice();
    151         Thread.sleep(19000);
    152         testPassed = mPMUtils.runDeviceTestsDidAllTestsPass(FILE_DOWNLOAD_PKG,
    153                 FILE_DOWNLOAD_CLASS, "verifyFileDownloadSucceeded", DOWNLOAD_TEST_RUNNER_NAME,
    154                 mExtraParams);
    155         assertTrue("Failed to verify initiated download completed properyly!", testPassed);
    156     }
    157 
    158     /**
    159      * Spawns a device-based function to test download while WiFi is enabled/disabled multiple times
    160      * during the download.
    161      *
    162      * @throws Exception if the test failed at any point
    163      */
    164     public void testDownloadMultipleWiFiEnableDisable() throws Exception {
    165         mPMUtils.installAppAndVerifyExistsOnDevice(String.format("%s%s%s", getTestAppPath(),
    166                 File.separator, FILE_DOWNLOAD_APK), FILE_DOWNLOAD_PKG, true);
    167 
    168         boolean testPassed = mPMUtils.runDeviceTestsDidAllTestsPass(FILE_DOWNLOAD_PKG,
    169                 FILE_DOWNLOAD_CLASS, "runDownloadMultipleWiFiEnableDisable",
    170                 DOWNLOAD_TEST_RUNNER_NAME, mExtraParams);
    171         assertTrue(testPassed);
    172     }
    173 
    174     /**
    175      * Spawns a device-based function to test switching on/off both airplane mode and WiFi
    176      *
    177      * @throws Exception if the test failed at any point
    178      */
    179     public void testDownloadMultipleSwitching() throws Exception {
    180         mPMUtils.installAppAndVerifyExistsOnDevice(String.format("%s%s%s", getTestAppPath(),
    181                 File.separator, FILE_DOWNLOAD_APK), FILE_DOWNLOAD_PKG, true);
    182 
    183         boolean testPassed = mPMUtils.runDeviceTestsDidAllTestsPass(FILE_DOWNLOAD_PKG,
    184                 FILE_DOWNLOAD_CLASS, "runDownloadMultipleSwitching",
    185                 DOWNLOAD_TEST_RUNNER_NAME, mExtraParams);
    186         assertTrue(testPassed);
    187     }
    188 
    189     /**
    190      * Spawns a device-based function to test switching on/off airplane mode multiple times
    191      *
    192      * @throws Exception if the test failed at any point
    193      */
    194     public void testDownloadMultipleAirplaneModeEnableDisable() throws Exception {
    195         mPMUtils.installAppAndVerifyExistsOnDevice(String.format("%s%s%s", getTestAppPath(),
    196                 File.separator, FILE_DOWNLOAD_APK), FILE_DOWNLOAD_PKG, true);
    197 
    198         boolean testPassed = mPMUtils.runDeviceTestsDidAllTestsPass(FILE_DOWNLOAD_PKG,
    199                 FILE_DOWNLOAD_CLASS, "runDownloadMultipleAirplaneModeEnableDisable",
    200                 DOWNLOAD_TEST_RUNNER_NAME, mExtraParams);
    201         assertTrue(testPassed);
    202     }
    203 
    204     /**
    205      * Spawns a device-based function to test 15 concurrent downloads of 5,000,000-byte files
    206      *
    207      * @throws Exception if the test failed at any point
    208      */
    209     public void testDownloadMultipleSimultaneously() throws Exception {
    210         mPMUtils.installAppAndVerifyExistsOnDevice(String.format("%s%s%s", getTestAppPath(),
    211                 File.separator, FILE_DOWNLOAD_APK), FILE_DOWNLOAD_PKG, true);
    212 
    213         boolean testPassed = mPMUtils.runDeviceTestsDidAllTestsPass(FILE_DOWNLOAD_PKG,
    214                 FILE_DOWNLOAD_CLASS, "runDownloadMultipleSimultaneously",
    215                 DOWNLOAD_TEST_RUNNER_NAME, mExtraParams);
    216         assertTrue(testPassed);
    217     }
    218 }
    219