DownloadManagerHostTests.java revision 40ef0f49ea9fa7c39eb0018fdb4df4b73a11a77d
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
17package android.net;
18
19import android.content.pm.PackageManagerHostTestUtils;
20import android.content.pm.PackageManagerHostTestUtils.CollectingTestRunListener;
21
22import com.android.ddmlib.IDevice;
23import com.android.ddmlib.IShellOutputReceiver;
24import com.android.ddmlib.Log;
25import com.android.ddmlib.MultiLineReceiver;
26import com.android.ddmlib.SyncService;
27import com.android.ddmlib.SyncService.ISyncProgressMonitor;
28import com.android.ddmlib.SyncService.SyncResult;
29import com.android.hosttest.DeviceTestCase;
30import com.android.hosttest.DeviceTestSuite;
31
32import java.io.File;
33import java.io.IOException;
34import java.util.Hashtable;
35
36import 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 */
42public 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.
58    private static String EXTERNAL_DOWNLOAD_URI_VALUE = null;
59
60    Hashtable<String, String> mExtraParams = null;
61
62    public static Test suite() {
63        return new DeviceTestSuite(DownloadManagerHostTests.class);
64    }
65
66    @Override
67    protected void setUp() throws Exception {
68        super.setUp();
69        // ensure apk path has been set before test is run
70        assertNotNull(getTestAppPath());
71        mPMUtils = new PackageManagerHostTestUtils(getDevice());
72        EXTERNAL_DOWNLOAD_URI_VALUE = System.getenv("ANDROID_TEST_EXTERNAL_URI");
73        assertNotNull(EXTERNAL_DOWNLOAD_URI_VALUE);
74        mExtraParams = getExtraParams();
75    }
76
77    /**
78     * Helper function to get extra params that can be used to pass into the helper app.
79     */
80    protected Hashtable<String, String> getExtraParams() {
81        Hashtable<String, String> extraParams = new Hashtable<String, String>();
82        extraParams.put(EXTERNAL_DOWNLOAD_URI_KEY, EXTERNAL_DOWNLOAD_URI_VALUE);
83        return extraParams;
84    }
85
86    /**
87     * Tests that a large download over WiFi
88     * @throws Exception if the test failed at any point
89     */
90    public void testLargeDownloadOverWiFi() throws Exception {
91        mPMUtils.installAppAndVerifyExistsOnDevice(String.format("%s%s%s", getTestAppPath(),
92                File.separator, FILE_DOWNLOAD_APK), FILE_DOWNLOAD_PKG, true);
93
94        boolean testPassed = mPMUtils.runDeviceTestsDidAllTestsPass(FILE_DOWNLOAD_PKG,
95                FILE_DOWNLOAD_CLASS, "runLargeDownloadOverWiFi", DOWNLOAD_TEST_RUNNER_NAME,
96                mExtraParams);
97
98        assertTrue("Failed to install large file over WiFi in < 10 minutes!", testPassed);
99    }
100
101    /**
102     * Spawns a device-based function to initiate a download on the device, reboots the device,
103     * then waits and verifies the download succeeded.
104     *
105     * @throws Exception if the test failed at any point
106     */
107    public void testDownloadManagerSingleReboot() throws Exception {
108        mPMUtils.installAppAndVerifyExistsOnDevice(String.format("%s%s%s", getTestAppPath(),
109                File.separator, FILE_DOWNLOAD_APK), FILE_DOWNLOAD_PKG, true);
110
111        boolean testPassed = mPMUtils.runDeviceTestsDidAllTestsPass(FILE_DOWNLOAD_PKG,
112                FILE_DOWNLOAD_CLASS, "initiateDownload", DOWNLOAD_TEST_RUNNER_NAME,
113                mExtraParams);
114
115        assertTrue("Failed to initiate download properly!", testPassed);
116        mPMUtils.rebootDevice();
117        testPassed = mPMUtils.runDeviceTestsDidAllTestsPass(FILE_DOWNLOAD_PKG,
118                FILE_DOWNLOAD_CLASS, "verifyFileDownloadSucceeded", DOWNLOAD_TEST_RUNNER_NAME,
119                mExtraParams);
120        assertTrue("Failed to verify initiated download completed properyly!", testPassed);
121    }
122
123    /**
124     * Spawns a device-based function to initiate a download on the device, reboots the device three
125     * times (using different intervals), then waits and verifies the download succeeded.
126     *
127     * @throws Exception if the test failed at any point
128     */
129    public void testDownloadManagerMultipleReboots() throws Exception {
130        mPMUtils.installAppAndVerifyExistsOnDevice(String.format("%s%s%s", getTestAppPath(),
131                File.separator, FILE_DOWNLOAD_APK), FILE_DOWNLOAD_PKG, true);
132
133        boolean testPassed = mPMUtils.runDeviceTestsDidAllTestsPass(FILE_DOWNLOAD_PKG,
134                FILE_DOWNLOAD_CLASS, "initiateDownload", DOWNLOAD_TEST_RUNNER_NAME,
135                mExtraParams);
136
137        assertTrue("Failed to initiate download properly!", testPassed);
138        Thread.sleep(5000);
139
140        // Do 3 random reboots - after 13, 9, and 19 seconds
141        Log.i(LOG_TAG, "First reboot...");
142        mPMUtils.rebootDevice();
143        Thread.sleep(13000);
144        Log.i(LOG_TAG, "Second reboot...");
145        mPMUtils.rebootDevice();
146        Thread.sleep(9000);
147        Log.i(LOG_TAG, "Third reboot...");
148        mPMUtils.rebootDevice();
149        Thread.sleep(19000);
150        testPassed = mPMUtils.runDeviceTestsDidAllTestsPass(FILE_DOWNLOAD_PKG,
151                FILE_DOWNLOAD_CLASS, "verifyFileDownloadSucceeded", DOWNLOAD_TEST_RUNNER_NAME,
152                mExtraParams);
153        assertTrue("Failed to verify initiated download completed properyly!", testPassed);
154    }
155
156    /**
157     * Spawns a device-based function to test download while WiFi is enabled/disabled multiple times
158     * during the download.
159     *
160     * @throws Exception if the test failed at any point
161     */
162    public void testDownloadMultipleWiFiEnableDisable() throws Exception {
163        mPMUtils.installAppAndVerifyExistsOnDevice(String.format("%s%s%s", getTestAppPath(),
164                File.separator, FILE_DOWNLOAD_APK), FILE_DOWNLOAD_PKG, true);
165
166        boolean testPassed = mPMUtils.runDeviceTestsDidAllTestsPass(FILE_DOWNLOAD_PKG,
167                FILE_DOWNLOAD_CLASS, "runDownloadMultipleWiFiEnableDisable",
168                DOWNLOAD_TEST_RUNNER_NAME, mExtraParams);
169        assertTrue(testPassed);
170    }
171
172    /**
173     * Spawns a device-based function to test switching on/off both airplane mode and WiFi
174     *
175     * @throws Exception if the test failed at any point
176     */
177    public void testDownloadMultipleSwitching() throws Exception {
178        mPMUtils.installAppAndVerifyExistsOnDevice(String.format("%s%s%s", getTestAppPath(),
179                File.separator, FILE_DOWNLOAD_APK), FILE_DOWNLOAD_PKG, true);
180
181        boolean testPassed = mPMUtils.runDeviceTestsDidAllTestsPass(FILE_DOWNLOAD_PKG,
182                FILE_DOWNLOAD_CLASS, "runDownloadMultipleSwitching",
183                DOWNLOAD_TEST_RUNNER_NAME, mExtraParams);
184        assertTrue(testPassed);
185    }
186
187    /**
188     * Spawns a device-based function to test switching on/off airplane mode multiple times
189     *
190     * @throws Exception if the test failed at any point
191     */
192    public void testDownloadMultipleAirplaneModeEnableDisable() throws Exception {
193        mPMUtils.installAppAndVerifyExistsOnDevice(String.format("%s%s%s", getTestAppPath(),
194                File.separator, FILE_DOWNLOAD_APK), FILE_DOWNLOAD_PKG, true);
195
196        boolean testPassed = mPMUtils.runDeviceTestsDidAllTestsPass(FILE_DOWNLOAD_PKG,
197                FILE_DOWNLOAD_CLASS, "runDownloadMultipleAirplaneModeEnableDisable",
198                DOWNLOAD_TEST_RUNNER_NAME, mExtraParams);
199        assertTrue(testPassed);
200    }
201}
202