blob: 47ad42cf88064add5e9e88a9213920aea426713c [file] [log] [blame]
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.app.cts;
import android.app.DownloadManager;
import android.app.DownloadManager.Query;
import android.app.DownloadManager.Request;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.os.ParcelFileDescriptor;
import android.test.AndroidTestCase;
import android.view.animation.cts.DelayedCheck;
import android.webkit.cts.CtsTestServer;
import java.io.File;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
public class DownloadManagerTest extends AndroidTestCase {
/**
* According to the CDD Section 7.6.1, the DownloadManager implementation must be able to
* download individual files of 55 MB.
*/
private static final int MINIMUM_DOWNLOAD_BYTES = 55 * 1024 * 1024;
private DownloadManager mDownloadManager;
private CtsTestServer mWebServer;
@Override
protected void setUp() throws Exception {
super.setUp();
mDownloadManager = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
mWebServer = new CtsTestServer(mContext);
clearDownloads();
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
mWebServer.shutdown();
}
public void testDownloadManager() throws Exception {
DownloadCompleteReceiver receiver =
new DownloadCompleteReceiver(2, TimeUnit.SECONDS.toMillis(3));
try {
IntentFilter intentFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
mContext.registerReceiver(receiver, intentFilter);
long goodId = mDownloadManager.enqueue(new Request(getGoodUrl()));
long badId = mDownloadManager.enqueue(new Request(getBadUrl()));
int allDownloads = getTotalNumberDownloads();
assertEquals(2, allDownloads);
assertDownloadQueryableById(goodId);
assertDownloadQueryableById(badId);
receiver.waitForDownloadComplete();
assertDownloadQueryableByStatus(DownloadManager.STATUS_SUCCESSFUL);
assertDownloadQueryableByStatus(DownloadManager.STATUS_FAILED);
assertRemoveDownload(goodId, allDownloads - 1);
assertRemoveDownload(badId, allDownloads - 2);
} finally {
mContext.unregisterReceiver(receiver);
}
}
public void testMinimumDownload() throws Exception {
DownloadCompleteReceiver receiver =
new DownloadCompleteReceiver(1, TimeUnit.MINUTES.toMillis(2));
try {
IntentFilter intentFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
mContext.registerReceiver(receiver, intentFilter);
long id = mDownloadManager.enqueue(new Request(getMinimumDownloadUrl()));
receiver.waitForDownloadComplete();
ParcelFileDescriptor fileDescriptor = mDownloadManager.openDownloadedFile(id);
assertEquals(MINIMUM_DOWNLOAD_BYTES, fileDescriptor.getStatSize());
Cursor cursor = null;
try {
cursor = mDownloadManager.query(new Query().setFilterById(id));
assertTrue(cursor.moveToNext());
assertEquals(DownloadManager.STATUS_SUCCESSFUL, cursor.getInt(
cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)));
assertEquals(MINIMUM_DOWNLOAD_BYTES, cursor.getInt(
cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)));
assertFalse(cursor.moveToNext());
} finally {
if (cursor != null) {
cursor.close();
}
}
assertRemoveDownload(id, 0);
} finally {
mContext.unregisterReceiver(receiver);
}
}
/**
* Set download locations and verify that file is downloaded to correct location.
*
* Checks three different methods of setting location: directly via setDestinationUri, and
* indirectly through setDestinationInExternalFilesDir and setDestinationinExternalPublicDir.
*/
public void testDownloadManagerDestination() throws Exception {
File uriLocation = new File(mContext.getExternalFilesDir(null), "uriFile");
if (uriLocation.exists()) {
assertTrue(uriLocation.delete());
}
File extFileLocation = new File(mContext.getExternalFilesDir(null), "extFile");
if (extFileLocation.exists()) {
assertTrue(extFileLocation.delete());
}
File publicLocation = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
"publicFile");
if (publicLocation.exists()) {
assertTrue(publicLocation.delete());
}
DownloadCompleteReceiver receiver =
new DownloadCompleteReceiver(3, TimeUnit.SECONDS.toMillis(5));
try {
IntentFilter intentFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
mContext.registerReceiver(receiver, intentFilter);
Request requestUri = new Request(getGoodUrl());
requestUri.setDestinationUri(Uri.fromFile(uriLocation));
long uriId = mDownloadManager.enqueue(requestUri);
Request requestExtFile = new Request(getGoodUrl());
requestExtFile.setDestinationInExternalFilesDir(mContext, null, "extFile");
long extFileId = mDownloadManager.enqueue(requestExtFile);
Request requestPublic = new Request(getGoodUrl());
requestPublic.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
"publicFile");
long publicId = mDownloadManager.enqueue(requestPublic);
int allDownloads = getTotalNumberDownloads();
assertEquals(3, allDownloads);
receiver.waitForDownloadComplete();
assertSuccessfulDownload(uriId, uriLocation);
assertSuccessfulDownload(extFileId, extFileLocation);
assertSuccessfulDownload(publicId, publicLocation);
assertRemoveDownload(uriId, allDownloads - 1);
assertRemoveDownload(extFileId, allDownloads - 2);
assertRemoveDownload(publicId, allDownloads - 3);
} finally {
mContext.unregisterReceiver(receiver);
}
}
private class DownloadCompleteReceiver extends BroadcastReceiver {
private final CountDownLatch mReceiveLatch;
private final long waitTimeMs;
public DownloadCompleteReceiver(int numDownload, long waitTimeMs) {
this.mReceiveLatch = new CountDownLatch(numDownload);
this.waitTimeMs = waitTimeMs;
}
@Override
public void onReceive(Context context, Intent intent) {
mReceiveLatch.countDown();
}
public void waitForDownloadComplete() throws InterruptedException {
assertTrue("Make sure you have WiFi or some other connectivity for this test.",
mReceiveLatch.await(waitTimeMs, TimeUnit.MILLISECONDS));
}
}
private void clearDownloads() {
if (getTotalNumberDownloads() > 0) {
Cursor cursor = null;
try {
Query query = new Query();
cursor = mDownloadManager.query(query);
int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_ID);
long[] removeIds = new long[cursor.getCount()];
for (int i = 0; cursor.moveToNext(); i++) {
removeIds[i] = cursor.getLong(columnIndex);
}
assertEquals(removeIds.length, mDownloadManager.remove(removeIds));
assertEquals(0, getTotalNumberDownloads());
} finally {
if (cursor != null) {
cursor.close();
}
}
}
}
private Uri getGoodUrl() {
return Uri.parse(mWebServer.getTestDownloadUrl("cts-good-download", 0));
}
private Uri getBadUrl() {
return Uri.parse(mWebServer.getBaseUri() + "/nosuchurl");
}
private Uri getMinimumDownloadUrl() {
return Uri.parse(mWebServer.getTestDownloadUrl("cts-minimum-download",
MINIMUM_DOWNLOAD_BYTES));
}
private int getTotalNumberDownloads() {
Cursor cursor = null;
try {
Query query = new Query();
cursor = mDownloadManager.query(query);
return cursor.getCount();
} finally {
if (cursor != null) {
cursor.close();
}
}
}
private void assertDownloadQueryableById(long downloadId) {
Cursor cursor = null;
try {
Query query = new Query().setFilterById(downloadId);
cursor = mDownloadManager.query(query);
assertEquals(1, cursor.getCount());
} finally {
if (cursor != null) {
cursor.close();
}
}
}
private void assertDownloadQueryableByStatus(final int status) {
new DelayedCheck() {
@Override
protected boolean check() {
Cursor cursor= null;
try {
Query query = new Query().setFilterByStatus(status);
cursor = mDownloadManager.query(query);
return 1 == cursor.getCount();
} finally {
if (cursor != null) {
cursor.close();
}
}
}
}.run();
}
private void assertSuccessfulDownload(long id, File location) {
Cursor cursor = null;
try {
cursor = mDownloadManager.query(new Query().setFilterById(id));
assertTrue(cursor.moveToNext());
assertEquals(DownloadManager.STATUS_SUCCESSFUL, cursor.getInt(
cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)));
assertEquals(Uri.fromFile(location).toString(),
cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)));
assertTrue(location.exists());
} finally {
if (cursor != null) {
cursor.close();
}
}
}
private void assertRemoveDownload(long removeId, int expectedNumDownloads) {
Cursor cursor = null;
try {
assertEquals(1, mDownloadManager.remove(removeId));
Query query = new Query();
cursor = mDownloadManager.query(query);
assertEquals(expectedNumDownloads, cursor.getCount());
} finally {
if (cursor != null) {
cursor.close();
}
}
}
}