Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e4bbb6ac authored by Tsu Chiang Chuang's avatar Tsu Chiang Chuang Committed by Android (Google) Code Review
Browse files

Merge "add upload capabilities to data test."

parents e678e0a5 e888685b
Loading
Loading
Loading
Loading
+36 −0
Original line number Diff line number Diff line
@@ -119,6 +119,42 @@ public class BandwidthTest extends InstrumentationTestCase {
        assertTrue(cleanUpFile(tmpSaveFile));
    }

    /**
     * Ensure that downloading on wifi reports reasonable stats.
     */
    @LargeTest
    public void testWifiUpload() {
        assertTrue(setDeviceWifiAndAirplaneMode(mSsid));
        // Download a file from the server.
        String ts = Long.toString(System.currentTimeMillis());
        String targetUrl = BandwidthTestUtil.buildDownloadUrl(
                mTestServer, FILE_SIZE, mDeviceId, ts);
        File tmpSaveFile = new File(BASE_DIR + File.separator + TMP_FILENAME);
        assertTrue(BandwidthTestUtil.DownloadFromUrl(targetUrl, tmpSaveFile));

        ts = Long.toString(System.currentTimeMillis());
        NetworkStats pre_test_stats = fetchDataFromProc(mUid);
        TrafficStats.startDataProfiling(mContext);
        assertTrue(BandwidthTestUtil.postFileToServer(mTestServer, mDeviceId, ts, tmpSaveFile));
        NetworkStats prof_stats = TrafficStats.stopDataProfiling(mContext);
        Log.d(LOG_TAG, prof_stats.toString());
        NetworkStats post_test_stats = fetchDataFromProc(mUid);
        NetworkStats proc_stats = post_test_stats.subtract(pre_test_stats);

        // Output measurements to instrumentation out, so that it can be compared to that of
        // the server.
        Bundle results = new Bundle();
        results.putString("device_id", mDeviceId);
        results.putString("timestamp", ts);
        results.putInt("size", FILE_SIZE);
        AddStatsToResults(PROF_LABEL, prof_stats, results);
        AddStatsToResults(PROC_LABEL, proc_stats, results);
        getInstrumentation().sendStatus(INSTRUMENTATION_IN_PROGRESS, results);

        // Clean up.
        assertTrue(cleanUpFile(tmpSaveFile));
    }

    /**
     * We want to make sure that if we use the Download Manager to download stuff,
     * accounting still goes to the app making the call and that the numbers still make sense.
+38 −1
Original line number Diff line number Diff line
@@ -18,6 +18,15 @@ package com.android.bandwidthtest.util;

import android.util.Log;

import com.android.internal.http.multipart.FilePart;
import com.android.internal.http.multipart.MultipartEntity;
import com.android.internal.http.multipart.Part;
import com.android.internal.http.multipart.StringPart;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.ByteArrayBuffer;

import java.io.BufferedInputStream;
@@ -80,7 +89,7 @@ public class BandwidthTestUtil {
     * Download a given file from a target url to a given destination file.
     * @param targetUrl the url to download
     * @param file the {@link File} location where to save to
     * @return true if it succeeded.
     * @return true if it succeeded
     */
    public static boolean DownloadFromUrl(String targetUrl, File file) {
        try {
@@ -106,4 +115,32 @@ public class BandwidthTestUtil {
        return true;
    }

    /**
     * Post a given file for a given device and timestamp to the server.
     * @param postUrl {@link String} url used to upload files
     * @param deviceId {@link String} device id that is uploading
     * @param timestamp {@link String} timestamp
     * @param file {@link File} to upload
     * @return true if it succeeded
     */
    public static boolean postFileToServer(String postUrl, String deviceId, String timestamp,
            File file) {
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost postRequest = new HttpPost(postUrl);
            Part[] parts = {
                    new StringPart("device_id", deviceId),
                    new StringPart("timestamp", timestamp),
                    new FilePart("file", file)
            };
            MultipartEntity reqEntity = new MultipartEntity(parts, postRequest.getParams());
            postRequest.setEntity(reqEntity);
            HttpResponse res = httpClient.execute(postRequest);
            res.getEntity().getContent().close();
        } catch (IOException e) {
            Log.e(LOG_TAG, "Could not upload file with error: " + e);
            return false;
        }
        return true;
    }
}
+1 −1

File changed.

Contains only whitespace changes.