Add test to check that DownloadManager maintains extensions.

HC introduced a regression where the extension of a file was changed
to match the mimetype of the file, whereas GB left the requested file
name alone.  This will be fixed for ICS, and this test should ensure
that no further regressions occur.

Bug: 5196436
Change-Id: I68f6295a2d86022f1cdecb250ff8d5b3ab670019
diff --git a/tests/assets/noiseandchirps b/tests/assets/noiseandchirps
new file mode 100644
index 0000000..a819efc
--- /dev/null
+++ b/tests/assets/noiseandchirps
Binary files differ
diff --git a/tests/assets/noiseandchirps.wrong b/tests/assets/noiseandchirps.wrong
new file mode 100644
index 0000000..a819efc
--- /dev/null
+++ b/tests/assets/noiseandchirps.wrong
Binary files differ
diff --git a/tests/tests/app/src/android/app/cts/DownloadManagerTest.java b/tests/tests/app/src/android/app/cts/DownloadManagerTest.java
index 47ad42c..ee68847 100644
--- a/tests/tests/app/src/android/app/cts/DownloadManagerTest.java
+++ b/tests/tests/app/src/android/app/cts/DownloadManagerTest.java
@@ -182,6 +182,51 @@
         }
     }
 
+    /**
+     * Set the download location and verify that the extension of the file name is left unchanged.
+     */
+    public void testDownloadManagerDestinationExtension() throws Exception {
+        String noExt = "noiseandchirps";
+        File noExtLocation = new File(mContext.getExternalFilesDir(null), noExt);
+        if (noExtLocation.exists()) {
+            assertTrue(noExtLocation.delete());
+        }
+
+        String wrongExt = "noiseandchirps.wrong";
+        File wrongExtLocation = new File(mContext.getExternalFilesDir(null), wrongExt);
+        if (wrongExtLocation.exists()) {
+            assertTrue(wrongExtLocation.delete());
+        }
+
+        DownloadCompleteReceiver receiver =
+            new DownloadCompleteReceiver(2, TimeUnit.SECONDS.toMillis(5));
+        try {
+            IntentFilter intentFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
+            mContext.registerReceiver(receiver, intentFilter);
+
+            Request requestNoExt = new Request(getAssetUrl(noExt));
+            requestNoExt.setDestinationUri(Uri.fromFile(noExtLocation));
+            long noExtId = mDownloadManager.enqueue(requestNoExt);
+
+            Request requestWrongExt = new Request(getAssetUrl(wrongExt));
+            requestWrongExt.setDestinationUri(Uri.fromFile(wrongExtLocation));
+            long wrongExtId = mDownloadManager.enqueue(requestWrongExt);
+
+            int allDownloads = getTotalNumberDownloads();
+            assertEquals(2, allDownloads);
+
+            receiver.waitForDownloadComplete();
+
+            assertSuccessfulDownload(noExtId, noExtLocation);
+            assertSuccessfulDownload(wrongExtId, wrongExtLocation);
+
+            assertRemoveDownload(noExtId, allDownloads - 1);
+            assertRemoveDownload(wrongExtId, allDownloads - 2);
+        } finally {
+            mContext.unregisterReceiver(receiver);
+        }
+    }
+
     private class DownloadCompleteReceiver extends BroadcastReceiver {
 
         private final CountDownLatch mReceiveLatch;
@@ -238,6 +283,10 @@
                 MINIMUM_DOWNLOAD_BYTES));
     }
 
+    private Uri getAssetUrl(String asset) {
+        return Uri.parse(mWebServer.getAssetUrl(asset));
+    }
+
     private int getTotalNumberDownloads() {
         Cursor cursor = null;
         try {