Skip to content

Commit dd25f12

Browse files
committed
more exception updates.
1 parent 1d44fb2 commit dd25f12

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

firebase-ml-modeldownloader/src/main/java/com/google/firebase/ml/modeldownloader/internal/ModelFileDownloadService.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,9 @@ && existTaskCompletionSourceInstance(downloadingModel.getDownloadId())) {
157157
// schedule new download of model file
158158
Long newDownloadId = scheduleModelDownload(customModel);
159159
if (newDownloadId == null) {
160-
return Tasks.forException(new Exception("Failed to schedule the download task"));
160+
return Tasks.forException(
161+
new FirebaseMlException(
162+
"Failed to schedule the download task", FirebaseMlException.INTERNAL));
161163
}
162164

163165
return registerReceiverForDownloadId(newDownloadId, customModel.getName());

firebase-ml-modeldownloader/src/test/java/com/google/firebase/ml/modeldownloader/internal/ModelFileDownloadServiceTest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,47 @@ public void ensureModelDownloaded_downloadFailed_urlExpiry() {
423423
eq(ErrorCode.NO_ERROR));
424424
}
425425

426+
@Test
427+
public void ensureModelDownloaded_downloadFailed_noUrl() {
428+
when(mockDownloadManager.enqueue(any())).thenReturn(DOWNLOAD_ID);
429+
matrixCursor =
430+
new MatrixCursor(
431+
new String[] {DownloadManager.COLUMN_STATUS, DownloadManager.COLUMN_REASON});
432+
matrixCursor.addRow(new Integer[] {DownloadManager.STATUS_FAILED, 400});
433+
when(mockDownloadManager.query(any())).thenReturn(matrixCursor);
434+
435+
TestOnCompleteListener<Void> onCompleteListener = new TestOnCompleteListener<>();
436+
Task<Void> task = modelFileDownloadService.ensureModelDownloaded(CUSTOM_MODEL_NO_URL);
437+
438+
try {
439+
// Complete the download
440+
Intent downloadCompleteIntent = new Intent(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
441+
downloadCompleteIntent.putExtra(DownloadManager.EXTRA_DOWNLOAD_ID, DOWNLOAD_ID);
442+
app.getApplicationContext().sendBroadcast(downloadCompleteIntent);
443+
444+
task.addOnCompleteListener(executor, onCompleteListener);
445+
onCompleteListener.await();
446+
} catch (FirebaseMlException ex) {
447+
assertEquals(ex.getCode(), FirebaseMlException.INTERNAL);
448+
} catch (Exception ex) {
449+
fail("Unexpected error message: " + ex.getMessage());
450+
}
451+
452+
assertTrue(task.isComplete());
453+
assertFalse(task.isSuccessful());
454+
assertTrue(task.getException().getMessage().contains("Failed to schedule"));
455+
assertEquals(sharedPreferencesUtil.getDownloadingCustomModelDetails(MODEL_NAME), null);
456+
457+
verify(mockDownloadManager, never()).enqueue(any());
458+
459+
verify(mockStatsLogger, times(1))
460+
.logDownloadEventWithErrorCode(
461+
eq(CUSTOM_MODEL_NO_URL),
462+
eq(false),
463+
eq(DownloadStatus.EXPLICITLY_REQUESTED),
464+
eq(ErrorCode.NO_ERROR));
465+
}
466+
426467
@Test
427468
public void ensureModelDownloaded_alreadyInProgess_completed() throws Exception {
428469
when(mockDownloadManager.enqueue(any())).thenReturn(DOWNLOAD_ID);

0 commit comments

Comments
 (0)