diff options
author | Steve Howard <showard@google.com> | 2010-06-09 17:50:07 -0700 |
---|---|---|
committer | Steve Howard <showard@google.com> | 2010-06-28 16:20:22 -0700 |
commit | d6f9b5e72a135365f2358d79b3ea3c9f7cb99c8e (patch) | |
tree | 5c0448812dd65aaed0f0f82018dbcac3ac218867 /tests/src/tests/http/MockWebServer.java | |
parent | aed3c4a57f6040349ef9e789646305c58227c35a (diff) | |
download | android_packages_providers_DownloadProvider-d6f9b5e72a135365f2358d79b3ea3c9f7cb99c8e.tar.gz android_packages_providers_DownloadProvider-d6f9b5e72a135365f2358d79b3ea3c9f7cb99c8e.tar.bz2 android_packages_providers_DownloadProvider-d6f9b5e72a135365f2358d79b3ea3c9f7cb99c8e.zip |
First pass at a functional test for the Download Manager.
This "Large" test sets up an HTTP server on the device using
MockWebServer and then initiates downloads from that server through
the download manager. It uses ServiceTestCase to control the
construction and execution of the DownloadService, and it uses some
logic from ProviderTestCase2 to construct a DownloadProvider and a
ContentResolver that uses it.
This setup gives us some ability to mock dependencies. This commit
includes use of a fake ConnectivityManager to test responses to
connectivity changes, and use of some customizations to MockWebServer
to test resuming an interrupted download. This test is disabled,
though, since it requires a very long sleep.
Avoiding that, and achieving certain other things, will require
changes to the Download Manager code itself to introduce new seams. I
wanted to check this in before I started such changes.
Change-Id: Iefb13b3c3cccdc13fabe5cc18703e13244805539
Diffstat (limited to 'tests/src/tests/http/MockWebServer.java')
-rw-r--r-- | tests/src/tests/http/MockWebServer.java | 30 |
1 files changed, 22 insertions, 8 deletions
diff --git a/tests/src/tests/http/MockWebServer.java b/tests/src/tests/http/MockWebServer.java index e3df2e83..b2cb8d7b 100644 --- a/tests/src/tests/http/MockWebServer.java +++ b/tests/src/tests/http/MockWebServer.java @@ -32,21 +32,22 @@ import java.util.concurrent.BlockingQueue; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; /** * A scriptable web server. Callers supply canned responses and the server * replays them upon request in sequence. + * + * TODO: merge with the version from libcore/support/src/tests/java once it's in. */ public final class MockWebServer { - static final String ASCII = "US-ASCII"; private final BlockingQueue<RecordedRequest> requestQueue = new LinkedBlockingQueue<RecordedRequest>(); private final BlockingQueue<MockResponse> responseQueue - = new LinkedBlockingDeque<MockResponse>(); + = new LinkedBlockingQueue<MockResponse>(); private int bodyLimit = Integer.MAX_VALUE; private final ExecutorService executor = Executors.newCachedThreadPool(); @@ -88,6 +89,16 @@ public final class MockWebServer { return requestQueue.take(); } + public RecordedRequest takeRequestWithTimeout(long timeoutMillis) throws InterruptedException { + return requestQueue.poll(timeoutMillis, TimeUnit.MILLISECONDS); + } + + public List<RecordedRequest> drainRequests() { + List<RecordedRequest> requests = new ArrayList<RecordedRequest>(); + requestQueue.drainTo(requests); + return requests; + } + /** * Starts the server, serves all enqueued requests, and shuts the server * down. @@ -130,7 +141,11 @@ public final class MockWebServer { } } requestQueue.add(request); - writeResponse(out, computeResponse(request)); + MockResponse response = computeResponse(request); + writeResponse(out, response); + if (response.shouldCloseConnectionAfter()) { + break; + } sequenceNumber++; } @@ -146,7 +161,7 @@ public final class MockWebServer { */ private RecordedRequest readRequest(InputStream in, int sequenceNumber) throws IOException { String request = readAsciiUntilCrlf(in); - if (request.isEmpty()) { + if (request.equals("")) { return null; // end of data; no more requests } @@ -154,7 +169,7 @@ public final class MockWebServer { int contentLength = -1; boolean chunked = false; String header; - while (!(header = readAsciiUntilCrlf(in)).isEmpty()) { + while (!(header = readAsciiUntilCrlf(in)).equals("")) { headers.add(header); String lowercaseHeader = header.toLowerCase(); if (contentLength == -1 && lowercaseHeader.startsWith("content-length:")) { @@ -219,7 +234,6 @@ public final class MockWebServer { } out.write(("\r\n").getBytes(ASCII)); out.write(response.getBody()); - out.write(("\r\n").getBytes(ASCII)); out.flush(); } @@ -260,7 +274,7 @@ public final class MockWebServer { private void readEmptyLine(InputStream in) throws IOException { String line = readAsciiUntilCrlf(in); - if (!line.isEmpty()) { + if (!line.equals("")) { throw new IllegalStateException("Expected empty but was: " + line); } } |