diff options
author | Steve Howard <showard@google.com> | 2010-06-22 18:20:55 -0700 |
---|---|---|
committer | Steve Howard <showard@google.com> | 2010-07-01 11:35:26 -0700 |
commit | 23357198c440e6872d3aef3e608295db7f8273bc (patch) | |
tree | 16391d655af983b751b41af5267e8053009a1f2c /src/com/android/providers/downloads/DownloadService.java | |
parent | b0aada69b9e6258bb9a1a7c1b783d0361ef3c6f2 (diff) | |
download | android_packages_providers_DownloadProvider-23357198c440e6872d3aef3e608295db7f8273bc.tar.gz android_packages_providers_DownloadProvider-23357198c440e6872d3aef3e608295db7f8273bc.tar.bz2 android_packages_providers_DownloadProvider-23357198c440e6872d3aef3e608295db7f8273bc.zip |
Stub out the system clock in the download manager, add tests
Introduce SystemFacade, an interface that allows us to stub out the
system clock for testing the download manager. This allows us to test
retrying a failed download without having the test wait 60 seconds.
This interface can include other dependencies in the future as well.
I've also used this to add tests for 503 (retry-after) and 301
(redirect), and I've added a test for download to the cache partition.
Other changes:
* made MockWebServer capable of checking + rethrowing exceptions from child threads
* refactoring + cleanup of DownloadManagerFunctionalTest
Diffstat (limited to 'src/com/android/providers/downloads/DownloadService.java')
-rw-r--r-- | src/com/android/providers/downloads/DownloadService.java | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/src/com/android/providers/downloads/DownloadService.java b/src/com/android/providers/downloads/DownloadService.java index 9e890ea0..f3bc9586 100644 --- a/src/com/android/providers/downloads/DownloadService.java +++ b/src/com/android/providers/downloads/DownloadService.java @@ -17,6 +17,7 @@ package com.android.providers.downloads; import com.google.android.collect.Lists; +import com.google.common.annotations.VisibleForTesting; import android.app.AlarmManager; import android.app.PendingIntent; @@ -62,7 +63,7 @@ public class DownloadService extends Service { /** Observer to get notified when the content observer's data changes */ private DownloadManagerContentObserver mObserver; - + /** Class to handle Notification Manager updates */ private DownloadNotification mNotifier; @@ -109,6 +110,9 @@ public class DownloadService extends Service { */ private CharArrayBuffer mNewChars; + @VisibleForTesting + SystemFacade mSystemFacade; + /* ------------ Inner Classes ------------ */ /** @@ -200,6 +204,10 @@ public class DownloadService extends Service { Log.v(Constants.TAG, "Service onCreate"); } + if (mSystemFacade == null) { + mSystemFacade = new RealSystemFacade(); + } + mDownloads = Lists.newArrayList(); mObserver = new DownloadManagerContentObserver(); @@ -209,7 +217,7 @@ public class DownloadService extends Service { mMediaScannerService = null; mMediaScannerConnecting = false; mMediaScannerConnection = new MediaScannerConnection(); - + mNotifier = new DownloadNotification(this); mNotifier.mNotificationMgr.cancelAll(); mNotifier.updateNotification(); @@ -259,10 +267,10 @@ public class DownloadService extends Service { public UpdateThread() { super("Download Service"); } - + public void run() { Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); - + boolean keepService = false; // for each update from the database, remember which download is // supposed to get restarted soonest in the future @@ -292,7 +300,7 @@ public class DownloadService extends Service { DownloadReceiver.class.getName()); alarms.set( AlarmManager.RTC_WAKEUP, - System.currentTimeMillis() + wakeUp, + mSystemFacade.currentTimeMillis() + wakeUp, PendingIntent.getBroadcast(DownloadService.this, 0, intent, PendingIntent.FLAG_ONE_SHOT)); } @@ -305,7 +313,7 @@ public class DownloadService extends Service { } boolean networkAvailable = Helpers.isNetworkAvailable(DownloadService.this); boolean networkRoaming = Helpers.isNetworkRoaming(DownloadService.this); - long now = System.currentTimeMillis(); + long now = mSystemFacade.currentTimeMillis(); Cursor cursor = getContentResolver().query(Downloads.Impl.CONTENT_URI, null, null, null, Downloads.Impl._ID); @@ -666,7 +674,7 @@ public class DownloadService extends Service { ContentUris.withAppendedId(Downloads.Impl.CONTENT_URI, info.mId), values, null, null); } - DownloadThread downloader = new DownloadThread(this, info); + DownloadThread downloader = new DownloadThread(this, mSystemFacade, info); info.mHasActiveThread = true; downloader.start(); } @@ -757,7 +765,7 @@ public class DownloadService extends Service { getContentResolver().update( ContentUris.withAppendedId(Downloads.Impl.CONTENT_URI, info.mId), values, null, null); - DownloadThread downloader = new DownloadThread(this, info); + DownloadThread downloader = new DownloadThread(this, mSystemFacade, info); info.mHasActiveThread = true; downloader.start(); } |