@@ -89,8 +89,27 @@ protected void tearDown() throws Exception {
8989 * Ensure that downloading on wifi reports reasonable stats.
9090 */
9191 @ LargeTest
92- public void testWifiDownload () {
93- assertTrue (setDeviceWifiAndAirplaneMode (mSsid ));
92+ public void testWifiDownload () throws Exception {
93+ assertTrue ("Could not connect to wifi!" , setDeviceWifiAndAirplaneMode (mSsid ));
94+ downloadFile ();
95+ }
96+
97+ /**
98+ * Ensure that downloading on mobile reports reasonable stats.
99+ */
100+ @ LargeTest
101+ public void testMobileDownload () throws Exception {
102+ // As part of the setup we disconnected from wifi; make sure we are connected to mobile and
103+ // that we have data.
104+ assertTrue ("Do not have mobile data!" , hasMobileData ());
105+ downloadFile ();
106+ }
107+
108+ /**
109+ * Helper method that downloads a file using http connection from a test server and reports the
110+ * data usage stats to instrumentation out.
111+ */
112+ protected void downloadFile () throws Exception {
94113 NetworkStats pre_test_stats = fetchDataFromProc (mUid );
95114 String ts = Long .toString (System .currentTimeMillis ());
96115
@@ -120,11 +139,28 @@ public void testWifiDownload() {
120139 }
121140
122141 /**
123- * Ensure that downloading on wifi reports reasonable stats.
142+ * Ensure that uploading on wifi reports reasonable stats.
124143 */
125144 @ LargeTest
126145 public void testWifiUpload () {
127146 assertTrue (setDeviceWifiAndAirplaneMode (mSsid ));
147+ uploadFile ();
148+ }
149+
150+ /**
151+ * Ensure that uploading on wifi reports reasonable stats.
152+ */
153+ @ LargeTest
154+ public void testMobileUpload () throws Exception {
155+ assertTrue (hasMobileData ());
156+ uploadFile ();
157+ }
158+
159+ /**
160+ * Helper method that downloads a test file to upload. The stats reported to instrumentation out
161+ * only include upload stats.
162+ */
163+ protected void uploadFile () throws Exception {
128164 // Download a file from the server.
129165 String ts = Long .toString (System .currentTimeMillis ());
130166 String targetUrl = BandwidthTestUtil .buildDownloadUrl (
@@ -156,12 +192,30 @@ public void testWifiUpload() {
156192 }
157193
158194 /**
159- * We want to make sure that if we use the Download Manager to download stuff,
195+ * We want to make sure that if we use wifi and the Download Manager to download stuff,
160196 * accounting still goes to the app making the call and that the numbers still make sense.
161197 */
162198 @ LargeTest
163199 public void testWifiDownloadWithDownloadManager () {
164200 assertTrue (setDeviceWifiAndAirplaneMode (mSsid ));
201+ downloadFileUsingDownloadManager ();
202+ }
203+
204+ /**
205+ * We want to make sure that if we use mobile data and the Download Manager to download stuff,
206+ * accounting still goes to the app making the call and that the numbers still make sense.
207+ */
208+ @ LargeTest
209+ public void testMobileDownloadWithDownloadManager () throws Exception {
210+ assertTrue (hasMobileData ());
211+ downloadFileUsingDownloadManager ();
212+ }
213+
214+ /**
215+ * Helper method that downloads a file from a test server using the download manager and reports
216+ * the stats to instrumentation out.
217+ */
218+ protected void downloadFileUsingDownloadManager () throws Exception {
165219 // If we are using the download manager, then the data that is written to /proc/uid_stat/
166220 // is accounted against download manager's uid, since it uses pre-ICS API.
167221 int downloadManagerUid = mConnectionUtil .downloadManagerUid ();
@@ -195,6 +249,7 @@ public void testWifiDownloadWithDownloadManager() {
195249
196250 /**
197251 * Fetch network data from /proc/uid_stat/uid
252+ *
198253 * @return populated {@link NetworkStats}
199254 */
200255 public NetworkStats fetchDataFromProc (int uid ) {
@@ -210,7 +265,8 @@ public NetworkStats fetchDataFromProc(int uid) {
210265 }
211266
212267 /**
213- * Turn on Airplane mode and connect to the wifi
268+ * Turn on Airplane mode and connect to the wifi.
269+ *
214270 * @param ssid of the wifi to connect to
215271 * @return true if we successfully connected to a given network.
216272 */
@@ -219,12 +275,25 @@ public boolean setDeviceWifiAndAirplaneMode(String ssid) {
219275 assertTrue (mConnectionUtil .connectToWifi (ssid ));
220276 assertTrue (mConnectionUtil .waitForWifiState (WifiManager .WIFI_STATE_ENABLED ,
221277 ConnectionUtil .LONG_TIMEOUT ));
222- return mConnectionUtil .waitForNetworkState (ConnectivityManager .TYPE_WIFI , State .CONNECTED ,
223- ConnectionUtil .LONG_TIMEOUT );
278+ assertTrue (mConnectionUtil .waitForNetworkState (ConnectivityManager .TYPE_WIFI ,
279+ State .CONNECTED , ConnectionUtil .LONG_TIMEOUT ));
280+ return mConnectionUtil .hasData ();
281+ }
282+
283+ /**
284+ * Helper method to make sure we are connected to mobile data.
285+ *
286+ * @return true if we successfully connect to mobile data.
287+ */
288+ public boolean hasMobileData () {
289+ assertTrue ("Not connected to mobile" , mConnectionUtil .isConnectedToMobile ());
290+ assertFalse ("Still connected to wifi." , mConnectionUtil .isConnectedToWifi ());
291+ return mConnectionUtil .hasData ();
224292 }
225293
226294 /**
227295 * Output the {@link NetworkStats} to Instrumentation out.
296+ *
228297 * @param label to attach to this given stats.
229298 * @param stats {@link NetworkStats} to add.
230299 * @param results {@link Bundle} to be added to.
@@ -281,4 +350,4 @@ private boolean cleanUpFile(File file) {
281350 }
282351 return true ;
283352 }
284- }
353+ }
0 commit comments