summaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/providers/downloads/PublicApiFunctionalTest.java
diff options
context:
space:
mode:
authorSteve Howard <showard@google.com>2010-07-02 12:59:05 -0700
committerSteve Howard <showard@google.com>2010-07-13 11:29:07 -0700
commitdf71633a02ce2f1b6228dad1ba3198185edf6107 (patch)
treee2ae80bae49a9d41bd192d8e3013e0c6a4969072 /tests/src/com/android/providers/downloads/PublicApiFunctionalTest.java
parent73134381d2f28a160203e6302106d56a02426519 (diff)
downloadandroid_packages_providers_DownloadProvider-df71633a02ce2f1b6228dad1ba3198185edf6107.tar.gz
android_packages_providers_DownloadProvider-df71633a02ce2f1b6228dad1ba3198185edf6107.tar.bz2
android_packages_providers_DownloadProvider-df71633a02ce2f1b6228dad1ba3198185edf6107.zip
New functional test for download manager public API.
I refactored a lot of the logic from DownloadManagerFunctionalTest into a new abstract class so that it could be shared with the new PublicApiFunctionalTest. There could be even more sharing, but it's not entirely clear it'd be worthwhile at this point. The new test covers most features of the new public API. Change-Id: I8c6859411f44b6430f8b348cf26a61225f5768cb
Diffstat (limited to 'tests/src/com/android/providers/downloads/PublicApiFunctionalTest.java')
-rw-r--r--tests/src/com/android/providers/downloads/PublicApiFunctionalTest.java238
1 files changed, 238 insertions, 0 deletions
diff --git a/tests/src/com/android/providers/downloads/PublicApiFunctionalTest.java b/tests/src/com/android/providers/downloads/PublicApiFunctionalTest.java
new file mode 100644
index 00000000..1927545f
--- /dev/null
+++ b/tests/src/com/android/providers/downloads/PublicApiFunctionalTest.java
@@ -0,0 +1,238 @@
+/*
+ * Copyright (C) 2010 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 com.android.providers.downloads;
+
+import android.database.Cursor;
+import android.net.DownloadManager;
+import android.net.Uri;
+import android.os.Environment;
+import tests.http.RecordedRequest;
+
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+
+public class PublicApiFunctionalTest extends AbstractDownloadManagerFunctionalTest {
+ /**
+ *
+ */
+ private static final String REQUEST_PATH = "/path";
+
+ class Download implements StatusReader {
+ final long mId;
+
+ private Download(long downloadId) {
+ this.mId = downloadId;
+ }
+
+ public int getStatus() {
+ return (int) getLongField(DownloadManager.COLUMN_STATUS);
+ }
+
+ public boolean isComplete(int status) {
+ return status != DownloadManager.STATUS_PENDING
+ && status != DownloadManager.STATUS_RUNNING
+ && status != DownloadManager.STATUS_PAUSED;
+ }
+
+ String getStringField(String field) {
+ Cursor cursor = mManager.query(new DownloadManager.Query().setFilterById(mId));
+ try {
+ assertEquals(1, cursor.getCount());
+ cursor.moveToFirst();
+ return cursor.getString(cursor.getColumnIndexOrThrow(field));
+ } finally {
+ cursor.close();
+ }
+ }
+
+ long getLongField(String field) {
+ Cursor cursor = mManager.query(new DownloadManager.Query().setFilterById(mId));
+ try {
+ assertEquals(1, cursor.getCount());
+ cursor.moveToFirst();
+ return cursor.getLong(cursor.getColumnIndexOrThrow(field));
+ } finally {
+ cursor.close();
+ }
+ }
+
+ String getContents() throws Exception {
+ InputStream stream = new FileInputStream(
+ mManager.openDownloadedFile(mId).getFileDescriptor());
+ try {
+ return readStream(stream);
+ } finally {
+ stream.close();
+ }
+ }
+
+ RecordedRequest runUntilStatus(int status) throws Exception {
+ return PublicApiFunctionalTest.this.runUntilStatus(this, status);
+ }
+ }
+
+ private DownloadManager mManager;
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ mManager = new DownloadManager(mResolver);
+ }
+
+ public void testBasicRequest() throws Exception {
+ enqueueResponse(HTTP_OK, FILE_CONTENT);
+
+ Download download = enqueueRequest(getRequest());
+ assertEquals(DownloadManager.STATUS_PENDING,
+ download.getLongField(DownloadManager.COLUMN_STATUS));
+ assertEquals(getServerUri(REQUEST_PATH),
+ download.getStringField(DownloadManager.COLUMN_URI));
+ assertEquals(download.mId, download.getLongField(DownloadManager.COLUMN_ID));
+
+ RecordedRequest request = download.runUntilStatus(DownloadManager.STATUS_SUCCESSFUL);
+ assertEquals("GET", request.getMethod());
+ assertEquals(REQUEST_PATH, request.getPath());
+
+ Uri localUri = Uri.parse(download.getStringField(DownloadManager.COLUMN_LOCAL_URI));
+ assertEquals("file", localUri.getScheme());
+ assertStartsWith(Environment.getDownloadCacheDirectory().getPath(),
+ localUri.getSchemeSpecificPart());
+ assertEquals("text/plain", download.getStringField(DownloadManager.COLUMN_MEDIA_TYPE));
+
+ int size = FILE_CONTENT.length();
+ assertEquals(size, download.getLongField(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
+ assertEquals(size, download.getLongField(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
+
+ assertEquals(FILE_CONTENT, download.getContents());
+ }
+
+ public void testTitleAndDescription() throws Exception {
+ Download download = enqueueRequest(getRequest()
+ .setTitle("my title")
+ .setDescription("my description"));
+ assertEquals("my title", download.getStringField(DownloadManager.COLUMN_TITLE));
+ assertEquals("my description",
+ download.getStringField(DownloadManager.COLUMN_DESCRIPTION));
+ }
+
+ public void testDownloadError() throws Exception {
+ enqueueEmptyResponse(HTTP_NOT_FOUND);
+ Download download = enqueueRequest(getRequest());
+ download.runUntilStatus(DownloadManager.STATUS_FAILED);
+ assertEquals(HTTP_NOT_FOUND, download.getLongField(DownloadManager.COLUMN_ERROR_CODE));
+ }
+
+ public void testUnhandledHttpStatus() throws Exception {
+ enqueueEmptyResponse(1234); // some invalid HTTP status
+ Download download = enqueueRequest(getRequest());
+ download.runUntilStatus(DownloadManager.STATUS_FAILED);
+ assertEquals(DownloadManager.ERROR_UNHANDLED_HTTP_CODE,
+ download.getLongField(DownloadManager.COLUMN_ERROR_CODE));
+ }
+
+ public void testInterruptedDownload() throws Exception {
+ int initialLength = 5;
+ String etag = "my_etag";
+ int totalLength = FILE_CONTENT.length();
+ // the first response has normal headers but unexpectedly closes after initialLength bytes
+ enqueueResponse(HTTP_OK, FILE_CONTENT.substring(0, initialLength))
+ .addHeader("Content-length", totalLength)
+ .addHeader("Etag", etag)
+ .setCloseConnectionAfter(true);
+ Download download = enqueueRequest(getRequest());
+
+ download.runUntilStatus(DownloadManager.STATUS_PAUSED);
+ assertEquals(initialLength,
+ download.getLongField(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
+
+ mSystemFacade.incrementTimeMillis(RETRY_DELAY_MILLIS);
+ // the second response returns partial content for the rest of the data
+ enqueueResponse(HTTP_PARTIAL_CONTENT, FILE_CONTENT.substring(initialLength))
+ .addHeader("Content-range",
+ "bytes " + initialLength + "-" + totalLength + "/" + totalLength)
+ .addHeader("Etag", etag);
+ download.runUntilStatus(DownloadManager.STATUS_SUCCESSFUL);
+ assertEquals(totalLength,
+ download.getLongField(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
+ }
+
+ public void testFiltering() throws Exception {
+ enqueueEmptyResponse(HTTP_OK);
+ Download download1 = enqueueRequest(getRequest());
+ download1.runUntilStatus(DownloadManager.STATUS_SUCCESSFUL);
+ enqueueEmptyResponse(HTTP_NOT_FOUND);
+ Download download2 = enqueueRequest(getRequest());
+ download2.runUntilStatus(DownloadManager.STATUS_FAILED);
+ Download download3 = enqueueRequest(getRequest());
+
+ Cursor cursor = mManager.query(new DownloadManager.Query());
+ checkAndCloseCursor(cursor, download1, download2, download3);
+
+ cursor = mManager.query(new DownloadManager.Query().setFilterById(download2.mId));
+ checkAndCloseCursor(cursor, download2);
+
+ cursor = mManager.query(new DownloadManager.Query()
+ .setFilterByStatus(DownloadManager.STATUS_PENDING));
+ checkAndCloseCursor(cursor, download3);
+
+ cursor = mManager.query(new DownloadManager.Query()
+ .setFilterByStatus(DownloadManager.STATUS_FAILED
+ | DownloadManager.STATUS_SUCCESSFUL));
+ checkAndCloseCursor(cursor, download1, download2);
+
+ cursor = mManager.query(new DownloadManager.Query()
+ .setFilterByStatus(DownloadManager.STATUS_RUNNING));
+ checkAndCloseCursor(cursor);
+ }
+
+ private void checkAndCloseCursor(Cursor cursor, Download... downloads) {
+ try {
+ int idIndex = cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_ID);
+ assertEquals(downloads.length, cursor.getCount());
+ cursor.moveToFirst();
+ for (Download download : downloads) {
+ assertEquals(download.mId, cursor.getLong(idIndex));
+ cursor.moveToNext();
+ }
+ } finally {
+ cursor.close();
+ }
+ }
+
+ public void testInvalidUri() throws Exception {
+ try {
+ enqueueRequest(getRequest("/no_host"));
+ } catch (IllegalArgumentException exc) { // expected
+ return;
+ }
+
+ fail("No exception thrown for invalid URI");
+ }
+
+ private DownloadManager.Request getRequest() throws MalformedURLException {
+ return getRequest(getServerUri(REQUEST_PATH));
+ }
+
+ private DownloadManager.Request getRequest(String path) {
+ return new DownloadManager.Request(Uri.parse(path));
+ }
+
+ private Download enqueueRequest(DownloadManager.Request request) {
+ return new Download(mManager.enqueue(request));
+ }
+}