• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.android.framework.tests;
18 
19 import com.android.ddmlib.Log;
20 import com.android.tradefed.config.Option;
21 import com.android.tradefed.config.Option.Importance;
22 import com.android.tradefed.device.DeviceNotAvailableException;
23 import com.android.tradefed.device.ITestDevice;
24 import com.android.tradefed.log.LogUtil.CLog;
25 import com.android.tradefed.result.ByteArrayInputStreamSource;
26 import com.android.tradefed.result.ITestInvocationListener;
27 import com.android.tradefed.result.LogDataType;
28 import com.android.tradefed.result.ResultForwarder;
29 import com.android.tradefed.result.TestDescription;
30 import com.android.tradefed.testtype.DeviceTestCase;
31 
32 import java.util.Hashtable;
33 
34 /**
35  * Host-based tests of the DownloadManager API. (Uses a device-based app to actually invoke the
36  * various tests.)
37  */
38 public class DownloadManagerHostTests extends DeviceTestCase {
39     protected PackageManagerHostTestUtils mPMUtils = null;
40 
41     private static final String LOG_TAG = "android.net.DownloadManagerHostTests";
42     private static final String FILE_DOWNLOAD_PKG = "com.android.frameworks.downloadmanagertests";
43     private static final String FILE_DOWNLOAD_CLASS =
44             "com.android.frameworks.downloadmanagertests.DownloadManagerTestApp";
45     private static final String DOWNLOAD_TEST_RUNNER_NAME =
46             "com.android.frameworks.downloadmanagertests.DownloadManagerTestRunner";
47 
48     // Extra parameters to pass to the TestRunner
49     private static final String EXTERNAL_DOWNLOAD_URI_KEY = "external_download_uri";
50 
51     Hashtable<String, String> mExtraParams = null;
52 
53     @Option(
54             name = "external-download-uri",
55             description =
56                     "external URI under which the files downloaded by the tests can be found. Uri "
57                             + "must be accessible by the device during a test run.",
58             importance = Importance.IF_UNSET)
59     private String mExternalDownloadUriValue = null;
60 
61     @Option(name = "wifi-network", description = "the name of wifi network to connect to.")
62     private String mWifiNetwork = null;
63 
64     @Option(name = "wifi-psk", description = "WPA-PSK passphrase of wifi network to connect to.")
65     private String mWifiPsk = null;
66 
67     @Option(
68             name = "wifi-attempts",
69             description = "maximum number of attempts to connect to wifi network.")
70     private int mWifiAttempts = 2;
71 
72     private ITestDevice mDevice = null;
73 
74     @Override
setUp()75     protected void setUp() throws Exception {
76         super.setUp();
77         mDevice = getDevice();
78         assertNotNull(mDevice);
79         mPMUtils = new PackageManagerHostTestUtils(mDevice);
80         assertNotNull("Missing external-download-uri option", mExternalDownloadUriValue);
81         mExtraParams = getExtraParams();
82         assertTrue("Failed to connect to wifi!", connectToWifi());
83     }
84 
85     /**
86      * Helper function to connect to wifi
87      *
88      * @throws DeviceNotAvailableException
89      */
connectToWifi()90     protected boolean connectToWifi() throws DeviceNotAvailableException {
91         return PackageManagerHostTestUtils.connectToWifi(
92                 mDevice, mWifiNetwork, mWifiPsk, mWifiAttempts);
93     }
94 
95     /** Helper function to get extra params that can be used to pass into the helper app. */
getExtraParams()96     protected Hashtable<String, String> getExtraParams() {
97         Hashtable<String, String> extraParams = new Hashtable<String, String>();
98         extraParams.put(EXTERNAL_DOWNLOAD_URI_KEY, mExternalDownloadUriValue);
99         return extraParams;
100     }
101 
102     /**
103      * Tests that a large download over WiFi
104      *
105      * @throws Exception if the test failed at any point
106      */
testLargeDownloadOverWiFi()107     public void testLargeDownloadOverWiFi() throws Exception {
108         boolean testPassed =
109                 mPMUtils.runDeviceTestsDidAllTestsPass(
110                         FILE_DOWNLOAD_PKG,
111                         FILE_DOWNLOAD_CLASS,
112                         "runLargeDownloadOverWiFi",
113                         DOWNLOAD_TEST_RUNNER_NAME,
114                         mExtraParams);
115         assertTrue("Failed to install large file over WiFi in < 10 minutes!", testPassed);
116     }
117 
118     /**
119      * Spawns a device-based function to initiate a download on the device, reboots the device, then
120      * waits and verifies the download succeeded.
121      *
122      * @throws Exception if the test failed at any point
123      */
testDownloadManagerSingleReboot()124     public void testDownloadManagerSingleReboot() throws Exception {
125         boolean testPassed =
126                 mPMUtils.runDeviceTestsDidAllTestsPass(
127                         FILE_DOWNLOAD_PKG,
128                         FILE_DOWNLOAD_CLASS,
129                         "initiateDownload",
130                         DOWNLOAD_TEST_RUNNER_NAME,
131                         mExtraParams);
132 
133         assertTrue("Failed to initiate download properly!", testPassed);
134         mDevice.reboot();
135         assertTrue("Failed to connect to wifi after reboot!", connectToWifi());
136         testPassed =
137                 mPMUtils.runDeviceTestsDidAllTestsPass(
138                         FILE_DOWNLOAD_PKG,
139                         FILE_DOWNLOAD_CLASS,
140                         "verifyFileDownloadSucceeded",
141                         DOWNLOAD_TEST_RUNNER_NAME,
142                         mExtraParams);
143         assertTrue("Failed to verify initiated download completed properly!", testPassed);
144     }
145 
146     /**
147      * Spawns a device-based function to initiate a download on the device, reboots the device three
148      * times (using different intervals), then waits and verifies the download succeeded.
149      *
150      * @throws Exception if the test failed at any point
151      */
testDownloadManagerMultipleReboots()152     public void testDownloadManagerMultipleReboots() throws Exception {
153         boolean testPassed =
154                 mPMUtils.runDeviceTestsDidAllTestsPass(
155                         FILE_DOWNLOAD_PKG,
156                         FILE_DOWNLOAD_CLASS,
157                         "initiateDownload",
158                         DOWNLOAD_TEST_RUNNER_NAME,
159                         mExtraParams);
160 
161         assertTrue("Failed to initiate download properly!", testPassed);
162         Thread.sleep(5000);
163 
164         // Do 3 random reboots - after 9, 5, and 6 seconds
165         Log.i(LOG_TAG, "First reboot...");
166         mDevice.reboot();
167         assertTrue("Failed to connect to wifi after reboot!", connectToWifi());
168         Thread.sleep(9000);
169         Log.i(LOG_TAG, "Second reboot...");
170         mDevice.reboot();
171         assertTrue("Failed to connect to wifi after reboot!", connectToWifi());
172         Thread.sleep(5000);
173         Log.i(LOG_TAG, "Third reboot...");
174         mDevice.reboot();
175         assertTrue("Failed to connect to wifi after reboot!", connectToWifi());
176         Thread.sleep(6000);
177         testPassed =
178                 mPMUtils.runDeviceTestsDidAllTestsPass(
179                         FILE_DOWNLOAD_PKG,
180                         FILE_DOWNLOAD_CLASS,
181                         "verifyFileDownloadSucceeded",
182                         DOWNLOAD_TEST_RUNNER_NAME,
183                         mExtraParams);
184         assertTrue("Failed to verify initiated download completed properyly!", testPassed);
185     }
186 
187     /**
188      * Spawns a device-based function to test download while WiFi is enabled/disabled multiple times
189      * during the download.
190      *
191      * @throws Exception if the test failed at any point
192      */
testDownloadMultipleWiFiEnableDisable()193     public void testDownloadMultipleWiFiEnableDisable() throws Exception {
194         boolean testPassed =
195                 mPMUtils.runDeviceTestsDidAllTestsPass(
196                         FILE_DOWNLOAD_PKG,
197                         FILE_DOWNLOAD_CLASS,
198                         "runDownloadMultipleWiFiEnableDisable",
199                         DOWNLOAD_TEST_RUNNER_NAME,
200                         mExtraParams);
201         assertTrue(testPassed);
202     }
203 
204     /**
205      * Spawns a device-based function to test switching on/off both airplane mode and WiFi
206      *
207      * @throws Exception if the test failed at any point
208      */
testDownloadMultipleSwitching()209     public void testDownloadMultipleSwitching() throws Exception {
210         boolean testPassed =
211                 mPMUtils.runDeviceTestsDidAllTestsPass(
212                         FILE_DOWNLOAD_PKG,
213                         FILE_DOWNLOAD_CLASS,
214                         "runDownloadMultipleSwitching",
215                         DOWNLOAD_TEST_RUNNER_NAME,
216                         mExtraParams);
217         assertTrue(testPassed);
218     }
219 
220     /**
221      * Spawns a device-based function to test switching on/off airplane mode multiple times
222      *
223      * @throws Exception if the test failed at any point
224      */
testDownloadMultipleAirplaneModeEnableDisable()225     public void testDownloadMultipleAirplaneModeEnableDisable() throws Exception {
226         boolean testPassed =
227                 mPMUtils.runDeviceTestsDidAllTestsPass(
228                         FILE_DOWNLOAD_PKG,
229                         FILE_DOWNLOAD_CLASS,
230                         "runDownloadMultipleAirplaneModeEnableDisable",
231                         DOWNLOAD_TEST_RUNNER_NAME,
232                         mExtraParams);
233         assertTrue(testPassed);
234     }
235 
236     /**
237      * Spawns a device-based function to test 15 concurrent downloads of 5,000,000-byte files
238      *
239      * @throws Exception if the test failed at any point
240      */
testDownloadMultipleSimultaneously()241     public void testDownloadMultipleSimultaneously() throws Exception {
242         boolean testPassed =
243                 mPMUtils.runDeviceTestsDidAllTestsPass(
244                         FILE_DOWNLOAD_PKG,
245                         FILE_DOWNLOAD_CLASS,
246                         "runDownloadMultipleSimultaneously",
247                         DOWNLOAD_TEST_RUNNER_NAME,
248                         mExtraParams);
249         assertTrue(testPassed);
250     }
251 
252     /** Saves dumpsys wifi log output if one of the tests fail. */
253     private class WifiLogSaver extends ResultForwarder {
254 
WifiLogSaver(ITestInvocationListener listener)255         public WifiLogSaver(ITestInvocationListener listener) {
256             super(listener);
257         }
258 
259         /** Take dumpsys wifi when test fails. */
260         @Override
testFailed(TestDescription test, String trace)261         public void testFailed(TestDescription test, String trace) {
262             try {
263                 String output = mDevice.executeShellCommand("dumpsys wifi");
264                 if (output == null) {
265                     CLog.w("dumpsys wifi did not return output");
266                 } else {
267                     String name = test.getTestName() + "-dumpsys-wifi";
268                     try (ByteArrayInputStreamSource stream =
269                             new ByteArrayInputStreamSource(output.getBytes())) {
270                         super.testLog(name, LogDataType.TEXT, stream);
271                     }
272                 }
273             } catch (DeviceNotAvailableException e) {
274                 CLog.e("Error getting dumpsys wifi");
275                 CLog.e(e);
276             } finally {
277                 super.testFailed(test, trace);
278             }
279         }
280     }
281 
282     @Override
run(ITestInvocationListener listener)283     public void run(ITestInvocationListener listener) throws DeviceNotAvailableException {
284         WifiLogSaver proxy = new WifiLogSaver(listener);
285         super.run(proxy);
286     }
287 }
288