Test to verify extremely large downloads.
Jeff Sharkey [Fri, 22 Feb 2013 19:57:48 +0000 (11:57 -0800)]
Streams a 3GB file to verify that it downloads correctly, using new
MockWebServer streaming API.

Bug: 8209169
Change-Id: Ic36271bfef3176e1ccea2b40edc7abb1044222f1

tests/src/com/android/providers/downloads/AbstractPublicApiTest.java
tests/src/com/android/providers/downloads/FakeInputStream.java [new file with mode: 0644]
tests/src/com/android/providers/downloads/PublicApiFunctionalTest.java

index bff9333..348dbd1 100644 (file)
@@ -18,6 +18,7 @@ package com.android.providers.downloads;
 
 import static android.app.DownloadManager.STATUS_FAILED;
 import static android.app.DownloadManager.STATUS_SUCCESSFUL;
+import static android.text.format.DateUtils.MINUTE_IN_MILLIS;
 import static android.text.format.DateUtils.SECOND_IN_MILLIS;
 
 import android.app.DownloadManager;
@@ -109,11 +110,21 @@ public abstract class AbstractPublicApiTest extends AbstractDownloadProviderFunc
             waitForStatus(status, startMillis);
         }
 
+        void runUntilStatus(int status, long timeout) throws TimeoutException {
+            final long startMillis = mSystemFacade.currentTimeMillis();
+            startService(null);
+            waitForStatus(status, startMillis, timeout);
+        }
+
         void waitForStatus(int expected, long afterMillis) throws TimeoutException {
+            waitForStatus(expected, afterMillis, 15 * SECOND_IN_MILLIS);
+        }
+
+        void waitForStatus(int expected, long afterMillis, long timeout) throws TimeoutException {
             int actual = -1;
 
-            final long timeout = SystemClock.elapsedRealtime() + (15 * SECOND_IN_MILLIS);
-            while (SystemClock.elapsedRealtime() < timeout) {
+            final long elapsedTimeout = SystemClock.elapsedRealtime() + timeout;
+            while (SystemClock.elapsedRealtime() < elapsedTimeout) {
                 if (getLongField(DownloadManager.COLUMN_LAST_MODIFIED_TIMESTAMP) >= afterMillis) {
                     actual = getStatus();
                     if (actual == STATUS_SUCCESSFUL || actual == STATUS_FAILED) {
@@ -122,9 +133,20 @@ public abstract class AbstractPublicApiTest extends AbstractDownloadProviderFunc
                     } else if (actual == expected) {
                         return;
                     }
+
+                    if (timeout > MINUTE_IN_MILLIS) {
+                        final int percent = (int) (100
+                                * getLongField(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)
+                                / getLongField(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
+                        Log.d(LOG_TAG, percent + "% complete");
+                    }
                 }
 
-                SystemClock.sleep(100);
+                if (timeout > MINUTE_IN_MILLIS) {
+                    SystemClock.sleep(SECOND_IN_MILLIS * 3);
+                } else {
+                    SystemClock.sleep(100);
+                }
             }
 
             throw new TimeoutException("Expected status " + expected + "; only reached " + actual);
diff --git a/tests/src/com/android/providers/downloads/FakeInputStream.java b/tests/src/com/android/providers/downloads/FakeInputStream.java
new file mode 100644 (file)
index 0000000..179ae6e
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.providers.downloads;
+
+import java.io.InputStream;
+import java.util.Arrays;
+
+/**
+ * Provides fake data for large transfers.
+ */
+public class FakeInputStream extends InputStream {
+    private long mRemaining;
+
+    public FakeInputStream(long length) {
+        mRemaining = length;
+    }
+
+    @Override
+    public int read() {
+        final int value;
+        if (mRemaining > 0) {
+            mRemaining--;
+            return 0;
+        } else {
+            return -1;
+        }
+    }
+
+    @Override
+    public int read(byte[] buffer, int offset, int length) {
+        Arrays.checkOffsetAndCount(buffer.length, offset, length);
+
+        if (length > mRemaining) {
+            length = (int) mRemaining;
+        }
+        mRemaining -= length;
+
+        if (length == 0) {
+            return -1;
+        } else {
+            return length;
+        }
+    }
+}
index 73f379e..bde9581 100644 (file)
@@ -18,6 +18,7 @@ package com.android.providers.downloads;
 
 import static android.app.DownloadManager.STATUS_FAILED;
 import static android.app.DownloadManager.STATUS_PAUSED;
+import static android.net.TrafficStats.GB_IN_BYTES;
 import static android.text.format.DateUtils.SECOND_IN_MILLIS;
 import static java.net.HttpURLConnection.HTTP_MOVED_TEMP;
 import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
@@ -45,9 +46,12 @@ import android.os.Environment;
 import android.os.SystemClock;
 import android.provider.Downloads;
 import android.test.suitebuilder.annotation.LargeTest;
+import android.test.suitebuilder.annotation.Suppress;
+import android.text.format.DateUtils;
 
 import com.google.mockwebserver.MockResponse;
 import com.google.mockwebserver.RecordedRequest;
+import com.google.mockwebserver.SocketPolicy;
 
 import java.io.File;
 import java.io.FileInputStream;
@@ -130,6 +134,24 @@ public class PublicApiFunctionalTest extends AbstractPublicApiTest {
         checkCompleteDownload(download);
     }
 
+    @Suppress
+    public void testExtremelyLarge() throws Exception {
+        // NOTE: suppressed since this takes several minutes to run
+        final long length = 3 * GB_IN_BYTES;
+        final InputStream body = new FakeInputStream(length);
+
+        enqueueResponse(new MockResponse().setResponseCode(HTTP_OK).setBody(body, length)
+                .setHeader("Content-type", "text/plain")
+                .setSocketPolicy(SocketPolicy.DISCONNECT_AT_END));
+
+        final Download download = enqueueRequest(getRequest()
+                .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "extreme.bin"));
+        download.runUntilStatus(DownloadManager.STATUS_SUCCESSFUL, 10 * DateUtils.MINUTE_IN_MILLIS);
+
+        assertEquals(length, download.getLongField(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
+        assertEquals(length, download.getLongField(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
+    }
+
     private void checkUriContent(Uri uri) throws FileNotFoundException, IOException {
         InputStream inputStream = mResolver.openInputStream(uri);
         try {