Skip to content

Commit 06875af

Browse files
tommynnguyenAndroid (Google) Code Review
authored andcommitted
Merge "A little tweaking of DL Manager tests and adding better debugging output." into gingerbread
2 parents 6e8d6bd + 65c36e6 commit 06875af

File tree

3 files changed

+96
-19
lines changed

3 files changed

+96
-19
lines changed

core/tests/coretests/src/android/app/DownloadManagerBaseTest.java

Lines changed: 82 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,34 @@ public static class MultipleDownloadsCompletedReceiver extends BroadcastReceiver
132132
*/
133133
@Override
134134
public void onReceive(Context context, Intent intent) {
135-
Log.i(LOG_TAG, "Received Notification:");
136135
if (intent.getAction().equalsIgnoreCase(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
137136
synchronized(this) {
138-
++mNumDownloadsCompleted;
139-
Log.i(LOG_TAG, "MultipleDownloadsCompletedReceiver got intent: " +
140-
intent.getAction() + " --> total count: " + mNumDownloadsCompleted);
141-
Bundle extras = intent.getExtras();
142-
downloadIds.add(new Long(extras.getLong(DownloadManager.EXTRA_DOWNLOAD_ID)));
137+
long id = intent.getExtras().getLong(DownloadManager.EXTRA_DOWNLOAD_ID);
138+
Log.i(LOG_TAG, "Received Notification for download: " + id);
139+
if (!downloadIds.contains(id)) {
140+
++mNumDownloadsCompleted;
141+
Log.i(LOG_TAG, "MultipleDownloadsCompletedReceiver got intent: " +
142+
intent.getAction() + " --> total count: " + mNumDownloadsCompleted);
143+
downloadIds.add(id);
144+
145+
DownloadManager dm = (DownloadManager)context.getSystemService(
146+
Context.DOWNLOAD_SERVICE);
147+
148+
Cursor cursor = dm.query(new Query().setFilterById(id));
149+
try {
150+
if (cursor.moveToFirst()) {
151+
int status = cursor.getInt(cursor.getColumnIndex(
152+
DownloadManager.COLUMN_STATUS));
153+
Log.i(LOG_TAG, "Download status is: " + status);
154+
} else {
155+
fail("No status found for completed download!");
156+
}
157+
} finally {
158+
cursor.close();
159+
}
160+
} else {
161+
Log.i(LOG_TAG, "Notification for id: " + id + " has already been made.");
162+
}
143163
}
144164
}
145165
}
@@ -621,28 +641,48 @@ protected File createFileOnSD(String filename, long fileSize, DataType type,
621641
/**
622642
* Helper to wait for a particular download to finish, or else a timeout to occur
623643
*
644+
* Does not wait for a receiver notification of the download.
645+
*
624646
* @param id The download id to query on (wait for)
625647
*/
626-
protected void waitForDownloadOrTimeout(long id) throws TimeoutException,
648+
protected void waitForDownloadOrTimeout_skipNotification(long id) throws TimeoutException,
627649
InterruptedException {
628650
waitForDownloadOrTimeout(id, WAIT_FOR_DOWNLOAD_POLL_TIME, MAX_WAIT_FOR_DOWNLOAD_TIME);
629651
}
630652

631653
/**
632654
* Helper to wait for a particular download to finish, or else a timeout to occur
633655
*
656+
* Also guarantees a notification has been posted for the download.
657+
*
658+
* @param id The download id to query on (wait for)
659+
*/
660+
protected void waitForDownloadOrTimeout(long id) throws TimeoutException,
661+
InterruptedException {
662+
waitForDownloadOrTimeout_skipNotification(id);
663+
waitForReceiverNotifications(1);
664+
}
665+
666+
/**
667+
* Helper to wait for a particular download to finish, or else a timeout to occur
668+
*
669+
* Also guarantees a notification has been posted for the download.
670+
*
634671
* @param id The download id to query on (wait for)
635672
* @param poll The amount of time to wait
636673
* @param timeoutMillis The max time (in ms) to wait for the download(s) to complete
637674
*/
638675
protected void waitForDownloadOrTimeout(long id, long poll, long timeoutMillis)
639676
throws TimeoutException, InterruptedException {
640677
doWaitForDownloadsOrTimeout(new Query().setFilterById(id), poll, timeoutMillis);
678+
waitForReceiverNotifications(1);
641679
}
642680

643681
/**
644682
* Helper to wait for all downloads to finish, or else a specified timeout to occur
645683
*
684+
* Makes no guaranee that notifications have been posted for all downloads.
685+
*
646686
* @param poll The amount of time to wait
647687
* @param timeoutMillis The max time (in ms) to wait for the download(s) to complete
648688
*/
@@ -654,6 +694,8 @@ protected void waitForDownloadsOrTimeout(long poll, long timeoutMillis) throws T
654694
/**
655695
* Helper to wait for all downloads to finish, or else a timeout to occur, but does not throw
656696
*
697+
* Also guarantees a notification has been posted for the download.
698+
*
657699
* @param id The id of the download to query against
658700
* @param poll The amount of time to wait
659701
* @param timeoutMillis The max time (in ms) to wait for the download(s) to complete
@@ -662,6 +704,7 @@ protected void waitForDownloadsOrTimeout(long poll, long timeoutMillis) throws T
662704
protected boolean waitForDownloadOrTimeoutNoThrow(long id, long poll, long timeoutMillis) {
663705
try {
664706
doWaitForDownloadsOrTimeout(new Query().setFilterById(id), poll, timeoutMillis);
707+
waitForReceiverNotifications(1);
665708
} catch (TimeoutException e) {
666709
return false;
667710
}
@@ -717,9 +760,8 @@ protected void doWaitForDownloadsOrTimeout(Query query, long poll, long timeoutM
717760
Cursor cursor = mDownloadManager.query(query);
718761

719762
try {
720-
// @TODO: there may be a little cleaner way to check for success, perhaps
721-
// via STATUS_SUCCESSFUL and/or STATUS_FAILED
722-
if (cursor.getCount() == 0 && mReceiver.numDownloadsCompleted() > 0) {
763+
if (cursor.getCount() == 0) {
764+
Log.i(LOG_TAG, "All downloads should be done...");
723765
break;
724766
}
725767
currentWaitTime = timeoutWait(currentWaitTime, poll, timeoutMillis,
@@ -778,6 +820,36 @@ protected void waitForDownloadToStart(long dlRequest) throws Exception {
778820
}
779821
}
780822

823+
/**
824+
* Convenience function to wait for just 1 notification of a download.
825+
*
826+
* @throws Exception if timed out while waiting
827+
*/
828+
protected void waitForReceiverNotification() throws Exception {
829+
waitForReceiverNotifications(1);
830+
}
831+
832+
/**
833+
* Synchronously waits for our receiver to receive notification for a given number of
834+
* downloads.
835+
*
836+
* @param targetNumber The number of notifications for unique downloads to wait for; pass in
837+
* -1 to not wait for notification.
838+
* @throws Exception if timed out while waiting
839+
*/
840+
protected void waitForReceiverNotifications(int targetNumber) throws TimeoutException {
841+
int count = mReceiver.numDownloadsCompleted();
842+
int currentWaitTime = 0;
843+
844+
while (count < targetNumber) {
845+
Log.i(LOG_TAG, "Waiting for notification of downloads...");
846+
currentWaitTime = timeoutWait(currentWaitTime, WAIT_FOR_DOWNLOAD_POLL_TIME,
847+
MAX_WAIT_FOR_DOWNLOAD_TIME, "Timed out waiting for download notifications!"
848+
+ " Received " + count + "notifications.");
849+
count = mReceiver.numDownloadsCompleted();
850+
}
851+
}
852+
781853
/**
782854
* Synchronously waits for a file to increase in size (such as to monitor that a download is
783855
* progressing).

core/tests/coretests/src/android/app/DownloadManagerIntegrationTest.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,23 +197,23 @@ public void testServerDropConnection_body() throws Exception {
197197
public void testMultipleDownloads() throws Exception {
198198
// need to be sure all current downloads have stopped first
199199
removeAllCurrentDownloads();
200-
int NUM_FILES = 50;
200+
int NUM_FILES = 10;
201201
int MAX_FILE_SIZE = 500 * 1024; // 500 kb
202202

203203
Random r = new LoggingRng();
204204
for (int i=0; i<NUM_FILES; ++i) {
205205
int size = r.nextInt(MAX_FILE_SIZE);
206206
byte[] blobData = generateData(size, DataType.TEXT);
207207

208-
Uri uri = getServerUri(DEFAULT_FILENAME);
208+
Uri uri = getServerUri(DEFAULT_FILENAME + i);
209209
Request request = new Request(uri);
210-
request.setTitle(String.format("%s--%d", DEFAULT_FILENAME, i));
210+
request.setTitle(String.format("%s--%d", DEFAULT_FILENAME + i, i));
211211

212212
// Prepare the mock server with a standard response
213213
enqueueResponse(HTTP_OK, blobData);
214214

215-
Log.i(LOG_TAG, "request: " + i);
216-
mDownloadManager.enqueue(request);
215+
long requestID = mDownloadManager.enqueue(request);
216+
Log.i(LOG_TAG, "request: " + i + " -- requestID: " + requestID);
217217
}
218218

219219
waitForDownloadsOrTimeout(WAIT_FOR_DOWNLOAD_POLL_TIME, MAX_WAIT_FOR_DOWNLOAD_TIME);
@@ -236,6 +236,8 @@ public void testMultipleDownloads() throws Exception {
236236

237237
assertEquals(NUM_FILES, mReceiver.numDownloadsCompleted());
238238
} finally {
239+
Log.i(LOG_TAG, "All download IDs: " + mReceiver.getDownloadIds().toString());
240+
Log.i(LOG_TAG, "Total downloads completed: " + mReceiver.getDownloadIds().size());
239241
cursor.close();
240242
}
241243
}

core/tests/hosttests/test-apps/DownloadManagerTestApp/src/com/android/frameworks/DownloadManagerTestApp.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public class DownloadManagerTestApp extends DownloadManagerBaseTest {
5353
protected static long DOWNLOAD_500K_FILESIZE = 570927;
5454
protected static String DOWNLOAD_1MB_FILENAME = "External1mb.apk";
5555
protected static long DOWNLOAD_1MB_FILESIZE = 1041262;
56+
protected static String DOWNLOAD_5MB_FILENAME = "External5mb.apk";
57+
protected static long DOWNLOAD_5MB_FILESIZE = 5138700;
5658
protected static String DOWNLOAD_10MB_FILENAME = "External10mb.apk";
5759
protected static long DOWNLOAD_10MB_FILESIZE = 10258741;
5860

@@ -135,7 +137,7 @@ protected void doCommonDownloadSetup() throws Exception {
135137
* @throws Exception if unsuccessful
136138
*/
137139
public void initiateDownload() throws Exception {
138-
String filename = DOWNLOAD_1MB_FILENAME;
140+
String filename = DOWNLOAD_5MB_FILENAME;
139141
mContext.deleteFile(DOWNLOAD_STARTED_FLAG);
140142
FileOutputStream fileOutput = mContext.openFileOutput(DOWNLOAD_STARTED_FLAG, 0);
141143
DataOutputStream outputFile = null;
@@ -171,8 +173,8 @@ public void initiateDownload() throws Exception {
171173
* @throws Exception if unsuccessful
172174
*/
173175
public void verifyFileDownloadSucceeded() throws Exception {
174-
String filename = DOWNLOAD_1MB_FILENAME;
175-
long filesize = DOWNLOAD_1MB_FILESIZE;
176+
String filename = DOWNLOAD_5MB_FILENAME;
177+
long filesize = DOWNLOAD_5MB_FILESIZE;
176178
long dlRequest = -1;
177179
boolean rebootMarkerValid = false;
178180
DataInputStream dataInputFile = null;
@@ -205,7 +207,8 @@ public void verifyFileDownloadSucceeded() throws Exception {
205207
int status = cursor.getInt(columnIndex);
206208
int currentWaitTime = 0;
207209

208-
// Wait until the download finishes
210+
// Wait until the download finishes; don't wait for a notification b/c
211+
// the download may well have been completed before the last reboot.
209212
waitForDownloadOrTimeout(dlRequest);
210213

211214
Log.i(LOG_TAG, "Verifying download information...");

0 commit comments

Comments
 (0)