diff options
author | Jeff Sharkey <jsharkey@android.com> | 2013-01-03 22:59:50 -0800 |
---|---|---|
committer | Jeff Sharkey <jsharkey@android.com> | 2013-01-08 14:11:28 -0800 |
commit | 8ac10e0e0667a4fe35191deebb5fa9786bf4226c (patch) | |
tree | 8edfa0be4348b8f25a8dadaad6a9e8ae80525323 /tests/src/com/android/providers/downloads/AbstractPublicApiTest.java | |
parent | 701d66efeff513a7509eeaafab6e47f4f6edb857 (diff) | |
download | android_packages_providers_DownloadProvider-8ac10e0e0667a4fe35191deebb5fa9786bf4226c.tar.gz android_packages_providers_DownloadProvider-8ac10e0e0667a4fe35191deebb5fa9786bf4226c.tar.bz2 android_packages_providers_DownloadProvider-8ac10e0e0667a4fe35191deebb5fa9786bf4226c.zip |
Clean up DownloadManager threading tests.
Change runUntilStatus() methods to polling with timeout instead of
requiring internal knowledge about threading.
Fix notification tests, and move opening of InputStream until after
handling headers to avoid FNFE. Always reset facade to defaults
before each test.
Change-Id: I6b2d6cfc4e685d2090c1133b1b2e89ae12760f8b
Diffstat (limited to 'tests/src/com/android/providers/downloads/AbstractPublicApiTest.java')
-rw-r--r-- | tests/src/com/android/providers/downloads/AbstractPublicApiTest.java | 48 |
1 files changed, 30 insertions, 18 deletions
diff --git a/tests/src/com/android/providers/downloads/AbstractPublicApiTest.java b/tests/src/com/android/providers/downloads/AbstractPublicApiTest.java index cda607aa..12c04f6a 100644 --- a/tests/src/com/android/providers/downloads/AbstractPublicApiTest.java +++ b/tests/src/com/android/providers/downloads/AbstractPublicApiTest.java @@ -16,17 +16,22 @@ 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.SECOND_IN_MILLIS; + import android.app.DownloadManager; import android.database.Cursor; import android.net.Uri; import android.os.ParcelFileDescriptor; -import android.provider.Downloads; +import android.os.SystemClock; import android.util.Log; import java.io.FileInputStream; import java.io.InputStream; import java.net.MalformedURLException; import java.net.UnknownHostException; +import java.util.concurrent.TimeoutException; /** * Code common to tests that use the download manager public API. @@ -94,9 +99,28 @@ public abstract class AbstractPublicApiTest extends AbstractDownloadProviderFunc } } - void runUntilStatus(int status) throws Exception { - runService(); - assertEquals(status, getStatus()); + void runUntilStatus(int status) throws TimeoutException { + startService(null); + waitForStatus(status); + } + + void waitForStatus(int expected) throws TimeoutException { + int actual = -1; + + final long timeout = SystemClock.elapsedRealtime() + (15 * SECOND_IN_MILLIS); + while (SystemClock.elapsedRealtime() < timeout) { + actual = getStatus(); + if (actual == STATUS_SUCCESSFUL || actual == STATUS_FAILED) { + assertEquals(expected, actual); + return; + } else if (actual == expected) { + return; + } + + SystemClock.sleep(100); + } + + throw new TimeoutException("Expected status " + expected + "; only reached " + actual); } // max time to wait before giving up on the current download operation. @@ -105,22 +129,10 @@ public abstract class AbstractPublicApiTest extends AbstractDownloadProviderFunc // download thread private static final int TIME_TO_SLEEP = 1000; - int runUntilDone() throws InterruptedException { - int sleepCounter = MAX_TIME_TO_WAIT_FOR_OPERATION * 1000 / TIME_TO_SLEEP; - for (int i = 0; i < sleepCounter; i++) { - int status = getStatusIfExists(); - if (status == -1 || Downloads.Impl.isStatusCompleted(getStatus())) { - // row doesn't exist or the download is done - return status; - } - // download not done yet. sleep a while and try again - Thread.sleep(TIME_TO_SLEEP); - } - return 0; // failed - } - // waits until progress_so_far is >= (progress)% boolean runUntilProgress(int progress) throws InterruptedException { + startService(null); + int sleepCounter = MAX_TIME_TO_WAIT_FOR_OPERATION * 1000 / TIME_TO_SLEEP; int numBytesReceivedSoFar = 0; int totalBytes = 0; |