summaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/providers
diff options
context:
space:
mode:
authorJeff Sharkey <jsharkey@android.com>2012-03-29 13:38:24 -0700
committerJeff Sharkey <jsharkey@android.com>2012-03-29 15:10:31 -0700
commitae6856b0fca5215f45619dd031a7e7beae7bd8cc (patch)
treef54a7fcfc663db7a22e57f3f81d77f1c98f70fa9 /tests/src/com/android/providers
parentdb4c69fbb558efe193ec81d593fc3868268ba2cf (diff)
downloadandroid_packages_providers_DownloadProvider-ae6856b0fca5215f45619dd031a7e7beae7bd8cc.tar.gz
android_packages_providers_DownloadProvider-ae6856b0fca5215f45619dd031a7e7beae7bd8cc.tar.bz2
android_packages_providers_DownloadProvider-ae6856b0fca5215f45619dd031a7e7beae7bd8cc.zip
Migrate to shared MockWebServer.
Bug: 4726601 Change-Id: Ibe537bd5c2a092dbf974360cd454751881f7f4ea
Diffstat (limited to 'tests/src/com/android/providers')
-rw-r--r--tests/src/com/android/providers/downloads/AbstractDownloadProviderFunctionalTest.java (renamed from tests/src/com/android/providers/downloads/AbstractDownloadManagerFunctionalTest.java)59
-rw-r--r--tests/src/com/android/providers/downloads/AbstractPublicApiTest.java6
-rw-r--r--tests/src/com/android/providers/downloads/DownloadProviderFunctionalTest.java (renamed from tests/src/com/android/providers/downloads/DownloadManagerFunctionalTest.java)21
-rw-r--r--tests/src/com/android/providers/downloads/PublicApiFunctionalTest.java130
-rw-r--r--tests/src/com/android/providers/downloads/ThreadingTest.java2
5 files changed, 111 insertions, 107 deletions
diff --git a/tests/src/com/android/providers/downloads/AbstractDownloadManagerFunctionalTest.java b/tests/src/com/android/providers/downloads/AbstractDownloadProviderFunctionalTest.java
index d2ecf3e6..1912e84c 100644
--- a/tests/src/com/android/providers/downloads/AbstractDownloadManagerFunctionalTest.java
+++ b/tests/src/com/android/providers/downloads/AbstractDownloadProviderFunctionalTest.java
@@ -29,9 +29,11 @@ import android.test.RenamingDelegatingContext;
import android.test.ServiceTestCase;
import android.test.mock.MockContentResolver;
import android.util.Log;
-import tests.http.MockResponse;
-import tests.http.MockWebServer;
-import tests.http.RecordedRequest;
+
+import com.google.mockwebserver.MockResponse;
+import com.google.mockwebserver.MockWebServer;
+import com.google.mockwebserver.RecordedRequest;
+import com.google.mockwebserver.SocketPolicy;
import java.io.BufferedReader;
import java.io.File;
@@ -39,14 +41,15 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
+import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
-public abstract class AbstractDownloadManagerFunctionalTest extends
+public abstract class AbstractDownloadProviderFunctionalTest extends
ServiceTestCase<DownloadService> {
- protected static final String LOG_TAG = "DownloadManagerFunctionalTest";
+ protected static final String LOG_TAG = "DownloadProviderFunctionalTest";
private static final String PROVIDER_AUTHORITY = "downloads";
protected static final long RETRY_DELAY_MILLIS = 61 * 1000;
protected static final String FILE_CONTENT = "hello world hello world hello world hello world";
@@ -143,7 +146,7 @@ public abstract class AbstractDownloadManagerFunctionalTest extends
}
}
- public AbstractDownloadManagerFunctionalTest(FakeSystemFacade systemFacade) {
+ public AbstractDownloadProviderFunctionalTest(FakeSystemFacade systemFacade) {
super(DownloadService.class);
mSystemFacade = systemFacade;
}
@@ -212,52 +215,42 @@ public abstract class AbstractDownloadManagerFunctionalTest extends
mResolver.delete(Downloads.Impl.CONTENT_URI, null, null);
}
- /**
- * Enqueue a String response from the MockWebServer.
- */
- MockResponse enqueueResponse(int status, String body) {
- MockResponse response = new MockResponse()
- .setResponseCode(status)
- .setBody(body)
- .addHeader("Content-type", "text/plain")
- .setCloseConnectionAfter(true);
- mServer.enqueue(response);
- return response;
+ void enqueueResponse(MockResponse resp) {
+ mServer.enqueue(resp);
}
- /**
- * Enqueue a byte[] response from the MockWebServer.
- */
- MockResponse enqueueResponse(int status, byte[] body) {
- MockResponse response = new MockResponse()
- .setResponseCode(status)
- .setBody(body)
- .addHeader("Content-type", "text/plain")
- .setCloseConnectionAfter(true);
- mServer.enqueue(response);
- return response;
+
+ MockResponse buildResponse(int status, String body) {
+ return new MockResponse().setResponseCode(status).setBody(body)
+ .setHeader("Content-type", "text/plain")
+ .setSocketPolicy(SocketPolicy.DISCONNECT_AT_END);
+ }
+
+ MockResponse buildResponse(int status, byte[] body) {
+ return new MockResponse().setResponseCode(status).setBody(body)
+ .setHeader("Content-type", "text/plain")
+ .setSocketPolicy(SocketPolicy.DISCONNECT_AT_END);
}
- MockResponse enqueueEmptyResponse(int status) {
- return enqueueResponse(status, "");
+ MockResponse buildEmptyResponse(int status) {
+ return buildResponse(status, "");
}
/**
* Fetch the last request received by the MockWebServer.
*/
protected RecordedRequest takeRequest() throws InterruptedException {
- RecordedRequest request = mServer.takeRequestWithTimeout(0);
+ RecordedRequest request = mServer.takeRequest();
assertNotNull("Expected request was not made", request);
return request;
}
- String getServerUri(String path) throws MalformedURLException {
+ String getServerUri(String path) throws MalformedURLException, UnknownHostException {
return mServer.getUrl(path).toString();
}
public void runService() throws Exception {
startService(null);
mSystemFacade.runAllThreads();
- mServer.checkForExceptions();
}
protected String readStream(InputStream inputStream) throws IOException {
diff --git a/tests/src/com/android/providers/downloads/AbstractPublicApiTest.java b/tests/src/com/android/providers/downloads/AbstractPublicApiTest.java
index c38c2f1d..cda607aa 100644
--- a/tests/src/com/android/providers/downloads/AbstractPublicApiTest.java
+++ b/tests/src/com/android/providers/downloads/AbstractPublicApiTest.java
@@ -26,11 +26,12 @@ import android.util.Log;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.MalformedURLException;
+import java.net.UnknownHostException;
/**
* Code common to tests that use the download manager public API.
*/
-public abstract class AbstractPublicApiTest extends AbstractDownloadManagerFunctionalTest {
+public abstract class AbstractPublicApiTest extends AbstractDownloadProviderFunctionalTest {
class Download {
final long mId;
@@ -171,7 +172,8 @@ public abstract class AbstractPublicApiTest extends AbstractDownloadManagerFunct
mManager = new DownloadManager(mResolver, PACKAGE_NAME);
}
- protected DownloadManager.Request getRequest() throws MalformedURLException {
+ protected DownloadManager.Request getRequest()
+ throws MalformedURLException, UnknownHostException {
return getRequest(getServerUri(REQUEST_PATH));
}
diff --git a/tests/src/com/android/providers/downloads/DownloadManagerFunctionalTest.java b/tests/src/com/android/providers/downloads/DownloadProviderFunctionalTest.java
index e01b617f..bbc5c3e0 100644
--- a/tests/src/com/android/providers/downloads/DownloadManagerFunctionalTest.java
+++ b/tests/src/com/android/providers/downloads/DownloadProviderFunctionalTest.java
@@ -25,11 +25,12 @@ import android.provider.Downloads;
import android.test.suitebuilder.annotation.LargeTest;
import android.util.Log;
-import tests.http.MockWebServer;
-import tests.http.RecordedRequest;
+import com.google.mockwebserver.MockWebServer;
+import com.google.mockwebserver.RecordedRequest;
import java.io.InputStream;
import java.net.MalformedURLException;
+import java.net.UnknownHostException;
/**
* This test exercises the entire download manager working together -- it requests downloads through
@@ -38,15 +39,15 @@ import java.net.MalformedURLException;
* device to serve downloads.
*/
@LargeTest
-public class DownloadManagerFunctionalTest extends AbstractDownloadManagerFunctionalTest {
+public class DownloadProviderFunctionalTest extends AbstractDownloadProviderFunctionalTest {
private static final String TAG = "DownloadManagerFunctionalTest";
- public DownloadManagerFunctionalTest() {
+ public DownloadProviderFunctionalTest() {
super(new FakeSystemFacade());
}
public void testDownloadTextFile() throws Exception {
- enqueueResponse(HTTP_OK, FILE_CONTENT);
+ enqueueResponse(buildResponse(HTTP_OK, FILE_CONTENT));
String path = "/download_manager_test_path";
Uri downloadUri = requestDownload(path);
@@ -63,7 +64,8 @@ public class DownloadManagerFunctionalTest extends AbstractDownloadManagerFuncti
}
public void testDownloadToCache() throws Exception {
- enqueueResponse(HTTP_OK, FILE_CONTENT);
+ enqueueResponse(buildResponse(HTTP_OK, FILE_CONTENT));
+
Uri downloadUri = requestDownload("/path");
updateDownload(downloadUri, Downloads.Impl.COLUMN_DESTINATION,
Integer.toString(Downloads.Impl.DESTINATION_CACHE_PARTITION));
@@ -74,11 +76,13 @@ public class DownloadManagerFunctionalTest extends AbstractDownloadManagerFuncti
}
public void testRoaming() throws Exception {
+ enqueueResponse(buildResponse(HTTP_OK, FILE_CONTENT));
+ enqueueResponse(buildResponse(HTTP_OK, FILE_CONTENT));
+
mSystemFacade.mActiveNetworkType = ConnectivityManager.TYPE_MOBILE;
mSystemFacade.mIsRoaming = true;
// for a normal download, roaming is fine
- enqueueResponse(HTTP_OK, FILE_CONTENT);
Uri downloadUri = requestDownload("/path");
runUntilStatus(downloadUri, Downloads.Impl.STATUS_SUCCESS);
@@ -89,7 +93,6 @@ public class DownloadManagerFunctionalTest extends AbstractDownloadManagerFuncti
runUntilStatus(downloadUri, Downloads.Impl.STATUS_WAITING_FOR_NETWORK);
// ...and pick up when we're off roaming
- enqueueResponse(HTTP_OK, FILE_CONTENT);
mSystemFacade.mIsRoaming = false;
runUntilStatus(downloadUri, Downloads.Impl.STATUS_SUCCESS);
}
@@ -145,7 +148,7 @@ public class DownloadManagerFunctionalTest extends AbstractDownloadManagerFuncti
/**
* Request a download from the Download Manager.
*/
- private Uri requestDownload(String path) throws MalformedURLException {
+ private Uri requestDownload(String path) throws MalformedURLException, UnknownHostException {
ContentValues values = new ContentValues();
values.put(Downloads.Impl.COLUMN_URI, getServerUri(path));
values.put(Downloads.Impl.COLUMN_DESTINATION, Downloads.Impl.DESTINATION_EXTERNAL);
diff --git a/tests/src/com/android/providers/downloads/PublicApiFunctionalTest.java b/tests/src/com/android/providers/downloads/PublicApiFunctionalTest.java
index f2a26f12..2f5282ae 100644
--- a/tests/src/com/android/providers/downloads/PublicApiFunctionalTest.java
+++ b/tests/src/com/android/providers/downloads/PublicApiFunctionalTest.java
@@ -16,7 +16,6 @@
package com.android.providers.downloads;
-
import android.app.DownloadManager;
import android.content.Intent;
import android.database.Cursor;
@@ -25,9 +24,10 @@ import android.net.Uri;
import android.os.Environment;
import android.provider.Downloads;
import android.test.suitebuilder.annotation.LargeTest;
+import android.util.Log;
-import tests.http.MockResponse;
-import tests.http.RecordedRequest;
+import com.google.mockwebserver.MockResponse;
+import com.google.mockwebserver.RecordedRequest;
import java.io.File;
import java.io.FileInputStream;
@@ -76,7 +76,7 @@ public class PublicApiFunctionalTest extends AbstractPublicApiTest {
}
public void testBasicRequest() throws Exception {
- enqueueResponse(HTTP_OK, FILE_CONTENT);
+ enqueueResponse(buildResponse(HTTP_OK, FILE_CONTENT));
Download download = enqueueRequest(getRequest());
assertEquals(DownloadManager.STATUS_PENDING,
@@ -126,12 +126,12 @@ public class PublicApiFunctionalTest extends AbstractPublicApiTest {
}
public void testDownloadError() throws Exception {
- enqueueEmptyResponse(HTTP_NOT_FOUND);
+ enqueueResponse(buildEmptyResponse(HTTP_NOT_FOUND));
runSimpleFailureTest(HTTP_NOT_FOUND);
}
public void testUnhandledHttpStatus() throws Exception {
- enqueueEmptyResponse(1234); // some invalid HTTP status
+ enqueueResponse(buildEmptyResponse(1234)); // some invalid HTTP status
runSimpleFailureTest(DownloadManager.ERROR_UNHANDLED_HTTP_CODE);
}
@@ -168,21 +168,21 @@ public class PublicApiFunctionalTest extends AbstractPublicApiTest {
private void enqueueInterruptedDownloadResponses(int initialLength) {
// the first response has normal headers but unexpectedly closes after initialLength bytes
- enqueuePartialResponse(0, initialLength);
+ enqueueResponse(buildPartialResponse(0, initialLength));
// the second response returns partial content for the rest of the data
- enqueuePartialResponse(initialLength, FILE_CONTENT.length());
+ enqueueResponse(buildPartialResponse(initialLength, FILE_CONTENT.length()));
}
- private MockResponse enqueuePartialResponse(int start, int end) {
+ private MockResponse buildPartialResponse(int start, int end) {
int totalLength = FILE_CONTENT.length();
boolean isFirstResponse = (start == 0);
int status = isFirstResponse ? HTTP_OK : HTTP_PARTIAL_CONTENT;
- MockResponse response = enqueueResponse(status, FILE_CONTENT.substring(start, end))
- .addHeader("Content-length", totalLength)
- .addHeader("Etag", ETAG);
+ MockResponse response = buildResponse(status, FILE_CONTENT.substring(start, end))
+ .setHeader("Content-length", totalLength)
+ .setHeader("Etag", ETAG);
if (!isFirstResponse) {
- response.addHeader("Content-range",
- "bytes " + start + "-" + totalLength + "/" + totalLength);
+ response.setHeader(
+ "Content-range", "bytes " + start + "-" + totalLength + "/" + totalLength);
}
return response;
}
@@ -190,20 +190,21 @@ public class PublicApiFunctionalTest extends AbstractPublicApiTest {
// enqueue a huge response to keep the receiveing thread in DownloadThread.java busy for a while
// give enough time to do something (cancel/remove etc) on that downloadrequest
// while it is in progress
- private void enqueueContinuingResponse() {
+ private MockResponse buildContinuingResponse() {
int numPackets = 100;
- int contentLength = STRING_1K.length() * numPackets;
- enqueueResponse(HTTP_OK, STRING_1K)
- .addHeader("Content-length", contentLength)
- .addHeader("Etag", ETAG)
- .setNumPackets(numPackets);
+ int contentLength = STRING_1K.length() * numPackets;
+ return buildResponse(HTTP_OK, STRING_1K)
+ .setHeader("Content-length", contentLength)
+ .setHeader("Etag", ETAG)
+ .setBytesPerSecond(1024);
}
public void testFiltering() throws Exception {
- enqueueEmptyResponse(HTTP_OK);
+ enqueueResponse(buildEmptyResponse(HTTP_OK));
+ enqueueResponse(buildEmptyResponse(HTTP_NOT_FOUND));
+
Download download1 = enqueueRequest(getRequest());
download1.runUntilStatus(DownloadManager.STATUS_SUCCESSFUL);
- enqueueEmptyResponse(HTTP_NOT_FOUND);
mSystemFacade.incrementTimeMillis(1); // ensure downloads are correctly ordered by time
Download download2 = enqueueRequest(getRequest());
@@ -240,17 +241,18 @@ public class PublicApiFunctionalTest extends AbstractPublicApiTest {
}
public void testOrdering() throws Exception {
- enqueueResponse(HTTP_OK, "small contents");
+ enqueueResponse(buildResponse(HTTP_OK, "small contents"));
+ enqueueResponse(buildResponse(HTTP_OK, "large contents large contents"));
+ enqueueResponse(buildEmptyResponse(HTTP_NOT_FOUND));
+
Download download1 = enqueueRequest(getRequest());
download1.runUntilStatus(DownloadManager.STATUS_SUCCESSFUL);
mSystemFacade.incrementTimeMillis(1);
- enqueueResponse(HTTP_OK, "large contents large contents");
Download download2 = enqueueRequest(getRequest());
download2.runUntilStatus(DownloadManager.STATUS_SUCCESSFUL);
mSystemFacade.incrementTimeMillis(1);
- enqueueEmptyResponse(HTTP_NOT_FOUND);
Download download3 = enqueueRequest(getRequest());
download3.runUntilStatus(DownloadManager.STATUS_FAILED);
@@ -299,7 +301,7 @@ public class PublicApiFunctionalTest extends AbstractPublicApiTest {
}
public void testDestination() throws Exception {
- enqueueResponse(HTTP_OK, FILE_CONTENT);
+ enqueueResponse(buildResponse(HTTP_OK, FILE_CONTENT));
Uri destination = getExternalUri();
Download download = enqueueRequest(getRequest().setDestinationUri(destination));
download.runUntilStatus(DownloadManager.STATUS_SUCCESSFUL);
@@ -320,7 +322,7 @@ public class PublicApiFunctionalTest extends AbstractPublicApiTest {
}
public void testRequestHeaders() throws Exception {
- enqueueEmptyResponse(HTTP_OK);
+ enqueueResponse(buildEmptyResponse(HTTP_OK));
Download download = enqueueRequest(getRequest().addRequestHeader("Header1", "value1")
.addRequestHeader("Header2", "value2"));
download.runUntilStatus(DownloadManager.STATUS_SUCCESSFUL);
@@ -342,17 +344,17 @@ public class PublicApiFunctionalTest extends AbstractPublicApiTest {
}
public void testSizeLimitOverMobile() throws Exception {
- mSystemFacade.mMaxBytesOverMobile = (long) FILE_CONTENT.length() - 1;
+ enqueueResponse(buildResponse(HTTP_OK, FILE_CONTENT));
+ enqueueResponse(buildResponse(HTTP_OK, FILE_CONTENT));
+ mSystemFacade.mMaxBytesOverMobile = (long) FILE_CONTENT.length() - 1;
mSystemFacade.mActiveNetworkType = ConnectivityManager.TYPE_MOBILE;
- enqueueResponse(HTTP_OK, FILE_CONTENT);
Download download = enqueueRequest(getRequest());
download.runUntilStatus(DownloadManager.STATUS_PAUSED);
mSystemFacade.mActiveNetworkType = ConnectivityManager.TYPE_WIFI;
// first response was read, but aborted after the DL manager processed the Content-Length
// header, so we need to enqueue a second one
- enqueueResponse(HTTP_OK, FILE_CONTENT);
download.runUntilStatus(DownloadManager.STATUS_SUCCESSFUL);
}
@@ -369,27 +371,28 @@ public class PublicApiFunctionalTest extends AbstractPublicApiTest {
}
public void testNoEtag() throws Exception {
- enqueuePartialResponse(0, 5).removeHeader("Etag");
+ enqueueResponse(buildPartialResponse(0, 5).removeHeader("Etag"));
runSimpleFailureTest(DownloadManager.ERROR_CANNOT_RESUME);
}
public void testSanitizeMediaType() throws Exception {
- enqueueEmptyResponse(HTTP_OK).addHeader("Content-Type", "text/html; charset=ISO-8859-4");
+ enqueueResponse(buildEmptyResponse(HTTP_OK)
+ .setHeader("Content-Type", "text/html; charset=ISO-8859-4"));
Download download = enqueueRequest(getRequest());
download.runUntilStatus(DownloadManager.STATUS_SUCCESSFUL);
assertEquals("text/html", download.getStringField(DownloadManager.COLUMN_MEDIA_TYPE));
}
public void testNoContentLength() throws Exception {
- enqueueEmptyResponse(HTTP_OK).removeHeader("Content-Length");
+ enqueueResponse(buildEmptyResponse(HTTP_OK).removeHeader("Content-length"));
runSimpleFailureTest(DownloadManager.ERROR_HTTP_DATA_ERROR);
}
public void testInsufficientSpace() throws Exception {
// this would be better done by stubbing the system API to check available space, but in the
// meantime, just use an absurdly large header value
- enqueueEmptyResponse(HTTP_OK).addHeader("Content-Length",
- 1024L * 1024 * 1024 * 1024 * 1024);
+ enqueueResponse(buildEmptyResponse(HTTP_OK)
+ .setHeader("Content-Length", 1024L * 1024 * 1024 * 1024 * 1024));
runSimpleFailureTest(DownloadManager.ERROR_INSUFFICIENT_SPACE);
}
@@ -397,7 +400,7 @@ public class PublicApiFunctionalTest extends AbstractPublicApiTest {
mSystemFacade.setStartThreadsWithoutWaiting(true);
// return 'real time' from FakeSystemFacade so that DownloadThread will report progress
mSystemFacade.setReturnActualTime(true);
- enqueueContinuingResponse();
+ enqueueResponse(buildContinuingResponse());
Download download = enqueueRequest(getRequest());
startService(null);
// give the download time to get started and progress to 1% completion
@@ -413,7 +416,7 @@ public class PublicApiFunctionalTest extends AbstractPublicApiTest {
}
public void testDownloadCompleteBroadcast() throws Exception {
- enqueueEmptyResponse(HTTP_OK);
+ enqueueResponse(buildEmptyResponse(HTTP_OK));
Download download = enqueueRequest(getRequest());
download.runUntilStatus(DownloadManager.STATUS_SUCCESSFUL);
@@ -441,12 +444,11 @@ public class PublicApiFunctionalTest extends AbstractPublicApiTest {
}
public void testBasicConnectivityChanges() throws Exception {
+ enqueueResponse(buildResponse(HTTP_OK, FILE_CONTENT));
+
// without connectivity, download immediately pauses
mSystemFacade.mActiveNetworkType = null;
-
- enqueueResponse(HTTP_OK, FILE_CONTENT);
Download download = enqueueRequest(getRequest());
-
download.runUntilStatus(DownloadManager.STATUS_PAUSED);
// connecting should start the download
@@ -455,10 +457,12 @@ public class PublicApiFunctionalTest extends AbstractPublicApiTest {
}
public void testAllowedNetworkTypes() throws Exception {
+ enqueueResponse(buildEmptyResponse(HTTP_OK));
+ enqueueResponse(buildEmptyResponse(HTTP_OK));
+
mSystemFacade.mActiveNetworkType = ConnectivityManager.TYPE_MOBILE;
// by default, use any connection
- enqueueEmptyResponse(HTTP_OK);
Download download = enqueueRequest(getRequest());
download.runUntilStatus(DownloadManager.STATUS_SUCCESSFUL);
@@ -468,15 +472,16 @@ public class PublicApiFunctionalTest extends AbstractPublicApiTest {
download.runUntilStatus(DownloadManager.STATUS_PAUSED);
// ...then enable wifi
mSystemFacade.mActiveNetworkType = ConnectivityManager.TYPE_WIFI;
- enqueueEmptyResponse(HTTP_OK);
download.runUntilStatus(DownloadManager.STATUS_SUCCESSFUL);
}
public void testRoaming() throws Exception {
+ enqueueResponse(buildEmptyResponse(HTTP_OK));
+ enqueueResponse(buildEmptyResponse(HTTP_OK));
+
mSystemFacade.mIsRoaming = true;
// by default, allow roaming
- enqueueEmptyResponse(HTTP_OK);
Download download = enqueueRequest(getRequest());
download.runUntilStatus(DownloadManager.STATUS_SUCCESSFUL);
@@ -485,12 +490,11 @@ public class PublicApiFunctionalTest extends AbstractPublicApiTest {
download.runUntilStatus(DownloadManager.STATUS_PAUSED);
// ...then turn off roaming
mSystemFacade.mIsRoaming = false;
- enqueueEmptyResponse(HTTP_OK);
download.runUntilStatus(DownloadManager.STATUS_SUCCESSFUL);
}
public void testContentObserver() throws Exception {
- enqueueEmptyResponse(HTTP_OK);
+ enqueueResponse(buildEmptyResponse(HTTP_OK));
enqueueRequest(getRequest());
mResolver.resetNotified();
runService();
@@ -498,13 +502,14 @@ public class PublicApiFunctionalTest extends AbstractPublicApiTest {
}
public void testNotifications() throws Exception {
- enqueueEmptyResponse(HTTP_OK);
+ enqueueResponse(buildEmptyResponse(HTTP_OK));
+ enqueueResponse(buildEmptyResponse(HTTP_OK));
+
Download download = enqueueRequest(getRequest().setShowRunningNotification(false));
download.runUntilStatus(DownloadManager.STATUS_SUCCESSFUL);
assertEquals(0, mSystemFacade.mActiveNotifications.size());
assertEquals(0, mSystemFacade.mCanceledNotifications.size());
- enqueueEmptyResponse(HTTP_OK);
download = enqueueRequest(getRequest()); // notifications by default
download.runUntilStatus(DownloadManager.STATUS_SUCCESSFUL);
assertEquals(1, mSystemFacade.mActiveNotifications.size());
@@ -518,43 +523,43 @@ public class PublicApiFunctionalTest extends AbstractPublicApiTest {
public void testRetryAfter() throws Exception {
final int delay = 120;
- enqueueEmptyResponse(HTTP_SERVICE_UNAVAILABLE).addHeader("Retry-after", delay);
+ enqueueResponse(
+ buildEmptyResponse(HTTP_SERVICE_UNAVAILABLE).setHeader("Retry-after", delay));
+ enqueueResponse(buildEmptyResponse(HTTP_OK));
+
Download download = enqueueRequest(getRequest());
download.runUntilStatus(DownloadManager.STATUS_PAUSED);
// download manager adds random 0-30s offset
mSystemFacade.incrementTimeMillis((delay + 31) * 1000);
-
- enqueueEmptyResponse(HTTP_OK);
download.runUntilStatus(DownloadManager.STATUS_SUCCESSFUL);
}
public void testManyInterruptions() throws Exception {
- int bytesPerResponse = 1;
- int start = 0;
+ final int length = FILE_CONTENT.length();
+ for (int i = 0; i < length; i++) {
+ enqueueResponse(buildPartialResponse(i, i + 1));
+ }
Download download = enqueueRequest(getRequest());
- while (start + bytesPerResponse < FILE_CONTENT.length()) {
- enqueuePartialResponse(start, start + bytesPerResponse);
+ for (int i = 0; i < length - 1; i++) {
download.runUntilStatus(DownloadManager.STATUS_PAUSED);
- takeRequest();
- start += bytesPerResponse;
mSystemFacade.incrementTimeMillis(RETRY_DELAY_MILLIS);
}
- enqueuePartialResponse(start, FILE_CONTENT.length());
download.runUntilStatus(DownloadManager.STATUS_SUCCESSFUL);
checkCompleteDownload(download);
}
public void testExistingFile() throws Exception {
+ enqueueResponse(buildEmptyResponse(HTTP_OK));
+
// download a file which already exists.
// downloadservice should simply create filename with "-" and a number attached
// at the end; i.e., download shouldnot fail.
Uri destination = getExternalUri();
new File(destination.getPath()).createNewFile();
- enqueueEmptyResponse(HTTP_OK);
Download download = enqueueRequest(getRequest().setDestinationUri(destination));
download.runUntilStatus(DownloadManager.STATUS_SUCCESSFUL);
}
@@ -571,11 +576,12 @@ public class PublicApiFunctionalTest extends AbstractPublicApiTest {
}
public void testRestart() throws Exception {
- enqueueEmptyResponse(HTTP_NOT_FOUND);
+ enqueueResponse(buildEmptyResponse(HTTP_NOT_FOUND));
+ enqueueResponse(buildEmptyResponse(HTTP_OK));
+
Download download = enqueueRequest(getRequest());
download.runUntilStatus(DownloadManager.STATUS_FAILED);
- enqueueEmptyResponse(HTTP_OK);
mManager.restartDownload(download.mId);
assertEquals(DownloadManager.STATUS_PENDING,
download.getLongField(DownloadManager.COLUMN_STATUS));
@@ -604,8 +610,8 @@ public class PublicApiFunctionalTest extends AbstractPublicApiTest {
*/
private RecordedRequest runRedirectionTest(int status)
throws MalformedURLException, Exception {
- enqueueEmptyResponse(status).addHeader("Location",
- mServer.getUrl(REDIRECTED_PATH).toString());
+ enqueueResponse(buildEmptyResponse(status)
+ .setHeader("Location", mServer.getUrl(REDIRECTED_PATH).toString()));
enqueueInterruptedDownloadResponses(5);
Download download = enqueueRequest(getRequest());
diff --git a/tests/src/com/android/providers/downloads/ThreadingTest.java b/tests/src/com/android/providers/downloads/ThreadingTest.java
index 082579e6..8605c76b 100644
--- a/tests/src/com/android/providers/downloads/ThreadingTest.java
+++ b/tests/src/com/android/providers/downloads/ThreadingTest.java
@@ -46,7 +46,7 @@ public class ThreadingTest extends AbstractPublicApiTest {
* a download.
*/
public void testFloodServiceWithStarts() throws Exception {
- enqueueResponse(HTTP_OK, FILE_CONTENT);
+ enqueueResponse(buildResponse(HTTP_OK, FILE_CONTENT));
Download download = enqueueRequest(getRequest());
while (download.getStatus() != DownloadManager.STATUS_SUCCESSFUL) {
startService(null);