Chromium Code Reviews| Index: components/cronet/android/test/javatests/src/org/chromium/cronet_test_apk/CronetUrlRequestTest.java |
| diff --git a/components/cronet/android/test/javatests/src/org/chromium/cronet_test_apk/CronetUrlRequestTest.java b/components/cronet/android/test/javatests/src/org/chromium/cronet_test_apk/CronetUrlRequestTest.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2c44755d4b59c1865c278af3d6e6ff91f78ebf34 |
| --- /dev/null |
| +++ b/components/cronet/android/test/javatests/src/org/chromium/cronet_test_apk/CronetUrlRequestTest.java |
| @@ -0,0 +1,215 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.cronet_test_apk; |
| + |
| +import android.os.ConditionVariable; |
| + |
| +import android.test.suitebuilder.annotation.SmallTest; |
| + |
| +import org.chromium.base.test.util.Feature; |
| +import org.chromium.net.ExtendedResponseInfo; |
| +import org.chromium.net.ResponseInfo; |
| +import org.chromium.net.UrlRequest; |
| +import org.chromium.net.UrlRequestException; |
| +import org.chromium.net.UrlRequestListener; |
| + |
| +import java.net.URL; |
| +import java.nio.ByteBuffer; |
| +import java.util.concurrent.ExecutorService; |
| +import java.util.concurrent.Executors; |
| + |
| +/** |
| + * Test basic functionality of UrlRequest. |
| + */ |
| +public class CronetUrlRequestTest extends CronetTestBase { |
| + // URL used for base tests. |
| + private static final String TEST_URL = "http://127.0.0.1:8000"; |
| + private static final String MOCK_CRONET_TEST_SUCCESS_URL = |
| + "http://mock.http/success.txt"; |
| + private static final String MOCK_CRONET_TEST_REDIRECT_URL = |
| + "http://mock.http/redirect.html"; |
| + private static final String MOCK_CRONET_TEST_NOTFOUND_URL = |
| + "http://mock.http/notfound.html"; |
| + private static final String MOCK_CRONET_TEST_FAILED_URL = |
| + "http://mock.failed.request/-2"; |
| + |
| + class SimpleUrlRequestListener implements UrlRequestListener { |
| + ConditionVariable mComplete = new ConditionVariable(); |
| + public int httpResponseDataLength = 0; |
|
xunjieli
2014/10/17 00:31:35
nit: mmenke@ once suggested to use "m" prefix for
mef
2014/10/17 20:19:43
Done.
|
| + public ResponseInfo responseInfo; |
| + public UrlRequestException error; |
| + |
| + public boolean onRedirectCalled = false; |
| + public boolean onResponseStartedCalled = false; |
| + public boolean onDataReceivedCalled = false; |
| + public boolean onCompleteCalled = false; |
| + public boolean onErrorCalled = false; |
| + |
| + public SimpleUrlRequestListener() { |
| + } |
| + |
| + @Override |
| + public void onRedirect(UrlRequest request, ResponseInfo info, |
| + URL newLocation) { |
|
xunjieli
2014/10/17 00:31:35
nit: indent.
mef
2014/10/17 20:19:43
Done.
|
| + onRedirectCalled = true; |
| + } |
| + |
| + @Override |
| + public void onResponseStarted(UrlRequest request, ResponseInfo info) { |
| + onResponseStartedCalled = true; |
| + responseInfo = info; |
| + } |
| + |
| + @Override |
| + public void onDataReceived(UrlRequest request, |
| + ResponseInfo info, |
| + ByteBuffer byteBuffer) { |
| + onDataReceivedCalled = true; |
| + httpResponseDataLength += byteBuffer.capacity(); |
| + } |
| + |
| + @Override |
| + public void onComplete(UrlRequest request, ExtendedResponseInfo info) { |
| + onCompleteCalled = true; |
| + mComplete.open(); |
| + } |
| + |
| + @Override |
| + public void onError(UrlRequest request, |
| + ResponseInfo info, |
| + UrlRequestException error) { |
| + onErrorCalled = true; |
| + this.error = error; |
| + mComplete.open(); |
| + } |
| + |
| + public void blockForComplete() { |
| + mComplete.block(); |
| + } |
| + |
| + public void resetComplete() { |
| + mComplete.close(); |
| + } |
| + } |
| + |
| + public SimpleUrlRequestListener launchAndWaitForComplete(String url) |
| + throws Exception { |
| + CronetTestActivity activity = launchCronetTestApp(); |
| + |
| + // Make sure the activity was created as expected. |
| + waitForActiveShellToBeDoneLoading(); |
| + // AddUrlInterceptors() after native application context is initialized. |
| + MockUrlRequestJobUtil.addUrlInterceptors(); |
| + |
| + ExecutorService executor = Executors.newFixedThreadPool(4); |
| + SimpleUrlRequestListener listener = new SimpleUrlRequestListener(); |
| + |
| + // Create request. |
| + UrlRequest request = activity.mUrlRequestFactory.createRequest( |
| + url, listener, executor); |
| + request.start(); |
| + listener.blockForComplete(); |
| + return listener; |
| + } |
| + |
| + @SmallTest |
| + @Feature({"Cronet"}) |
| + public void testSimpleGet() throws Exception { |
| + SimpleUrlRequestListener listener = launchAndWaitForComplete(TEST_URL); |
| + assertEquals(200, listener.responseInfo.getHttpStatusCode()); |
| + assertTrue(listener.httpResponseDataLength != 0); |
| + } |
| + |
| + @SmallTest |
| + @Feature({"Cronet"}) |
| + public void testMockSuccess() throws Exception { |
| + SimpleUrlRequestListener listener = launchAndWaitForComplete( |
| + MOCK_CRONET_TEST_SUCCESS_URL); |
| + assertEquals(200, listener.responseInfo.getHttpStatusCode()); |
| + assertTrue(listener.httpResponseDataLength != 0); |
| + } |
| + |
| + @SmallTest |
| + @Feature({"Cronet"}) |
| + public void testMockRedirect() throws Exception { |
| + SimpleUrlRequestListener listener = launchAndWaitForComplete( |
| + MOCK_CRONET_TEST_REDIRECT_URL); |
| + ResponseInfo responseInfo = listener.responseInfo; |
| + assertEquals(200, responseInfo.getHttpStatusCode()); |
| + assertEquals(MOCK_CRONET_TEST_SUCCESS_URL, |
| + responseInfo.getUrl()); |
| + assertEquals(2, responseInfo.getUrlChain().length); |
| + assertEquals(MOCK_CRONET_TEST_REDIRECT_URL, |
| + responseInfo.getUrlChain()[0]); |
| + assertEquals(MOCK_CRONET_TEST_SUCCESS_URL, |
| + responseInfo.getUrlChain()[1]); |
| + assertTrue(listener.httpResponseDataLength != 0); |
| + } |
| + |
| + @SmallTest |
| + @Feature({"Cronet"}) |
| + public void testMockNotFound() throws Exception { |
| + SimpleUrlRequestListener listener = launchAndWaitForComplete( |
| + MOCK_CRONET_TEST_NOTFOUND_URL); |
| + assertEquals(404, listener.responseInfo.getHttpStatusCode()); |
| + assertTrue(listener.httpResponseDataLength != 0); |
| + } |
| + |
| + @SmallTest |
| + @Feature({"Cronet"}) |
| + public void testMockFailed() throws Exception { |
| + SimpleUrlRequestListener listener = launchAndWaitForComplete( |
| + MOCK_CRONET_TEST_FAILED_URL); |
| + assertEquals(null, listener.responseInfo); |
| + assertEquals(0, listener.httpResponseDataLength); |
| + assertNotNull(listener.error); |
| + assertEquals(-2, listener.error.netError()); |
| + } |
| + |
| + @SmallTest |
| + @Feature({"Cronet"}) |
| + public void testMockStartSyncError() throws Exception { |
| + SimpleUrlRequestListener listener = launchAndWaitForComplete( |
| + MOCK_CRONET_TEST_SUCCESS_URL + "?start=-3"); |
| + assertNull(listener.responseInfo); |
| + assertNotNull(listener.error); |
| + assertEquals(-3, listener.error.netError()); |
| + assertFalse(listener.onRedirectCalled); |
| + assertFalse(listener.onResponseStartedCalled); |
| + assertFalse(listener.onDataReceivedCalled); |
| + assertFalse(listener.onCompleteCalled); |
| + assertTrue(listener.onErrorCalled); |
| + } |
| + |
| + @SmallTest |
| + @Feature({"Cronet"}) |
| + public void testMockReadDataSyncError() throws Exception { |
| + SimpleUrlRequestListener listener = launchAndWaitForComplete( |
| + MOCK_CRONET_TEST_SUCCESS_URL + "?readsync=-5"); |
| + assertEquals(200, listener.responseInfo.getHttpStatusCode()); |
| + assertNotNull(listener.error); |
| + assertEquals(-5, listener.error.netError()); |
| + assertFalse(listener.onRedirectCalled); |
| + assertTrue(listener.onResponseStartedCalled); |
| + assertFalse(listener.onDataReceivedCalled); |
| + assertFalse(listener.onCompleteCalled); |
| + assertTrue(listener.onErrorCalled); |
| + } |
| + |
| + @SmallTest |
| + @Feature({"Cronet"}) |
| + public void testMockReadDataAsyncError() throws Exception { |
| + SimpleUrlRequestListener listener = launchAndWaitForComplete( |
| + MOCK_CRONET_TEST_SUCCESS_URL + "?readasync=-5"); |
|
xunjieli
2014/10/17 00:31:35
indent: +4.
mef
2014/10/17 20:19:43
Done.
|
| + assertEquals(200, listener.responseInfo.getHttpStatusCode()); |
| + assertNotNull(listener.error); |
| + assertEquals(-5, listener.error.netError()); |
| + assertFalse(listener.onRedirectCalled); |
| + assertTrue(listener.onResponseStartedCalled); |
| + assertFalse(listener.onDataReceivedCalled); |
| + assertFalse(listener.onCompleteCalled); |
| + assertTrue(listener.onErrorCalled); |
| + } |
| +} |