Fix and test handling of null/empty download fields.
Steve Howard [Tue, 3 Aug 2010 19:39:35 +0000 (12:39 -0700)]
Minor change to make title/description default to blank stings, and
add test case covering all null/empty download fields.

Change-Id: Id44344bc2e464f46589faed49c8f383a45a8d329

src/com/android/providers/downloads/DownloadProvider.java
tests/src/com/android/providers/downloads/PublicApiFunctionalTest.java

index 4fcae22..f6b091b 100644 (file)
@@ -415,8 +415,8 @@ public final class DownloadProvider extends ContentProvider {
         if (Binder.getCallingUid() == 0) {
             copyInteger(Constants.UID, values, filteredValues);
         }
-        copyString(Downloads.Impl.COLUMN_TITLE, values, filteredValues);
-        copyString(Downloads.Impl.COLUMN_DESCRIPTION, values, filteredValues);
+        copyStringWithDefault(Downloads.Impl.COLUMN_TITLE, values, filteredValues, "");
+        copyStringWithDefault(Downloads.Impl.COLUMN_DESCRIPTION, values, filteredValues, "");
         filteredValues.put(Downloads.Impl.COLUMN_TOTAL_BYTES, -1);
 
         if (isPublicApi) {
@@ -986,6 +986,14 @@ public final class DownloadProvider extends ContentProvider {
         }
     }
 
+    private static final void copyStringWithDefault(String key, ContentValues from,
+            ContentValues to, String defaultValue) {
+        copyString(key, from, to);
+        if (!to.containsKey(key)) {
+            to.put(key, defaultValue);
+        }
+    }
+
     private class ReadOnlyCursorWrapper extends CursorWrapper implements CrossProcessCursor {
         public ReadOnlyCursorWrapper(Cursor cursor) {
             super(cursor);
index 6d60477..cf2b990 100644 (file)
@@ -482,6 +482,18 @@ public class PublicApiFunctionalTest extends AbstractPublicApiTest {
                      download.getLongField(DownloadManager.COLUMN_ERROR_CODE));
     }
 
+    public void testEmptyFields() throws Exception {
+        Download download = enqueueRequest(getRequest());
+        assertNull(download.getStringField(DownloadManager.COLUMN_LOCAL_URI));
+        assertEquals("", download.getStringField(DownloadManager.COLUMN_TITLE));
+        assertEquals("", download.getStringField(DownloadManager.COLUMN_DESCRIPTION));
+        assertNull(download.getStringField(DownloadManager.COLUMN_MEDIA_TYPE));
+        assertEquals(0, download.getLongField(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
+        assertEquals(-1, download.getLongField(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
+        // just ensure no exception is thrown
+        download.getLongField(DownloadManager.COLUMN_ERROR_CODE);
+    }
+
     private void checkCompleteDownload(Download download) throws Exception {
         assertEquals(FILE_CONTENT.length(),
                      download.getLongField(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));