Chromium Code Reviews| Index: components/cronet/android/test/javatests/src/org/chromium/cronet_test_apk/CronetUrlRequestContextTest.java |
| diff --git a/components/cronet/android/test/javatests/src/org/chromium/cronet_test_apk/CronetUrlRequestContextTest.java b/components/cronet/android/test/javatests/src/org/chromium/cronet_test_apk/CronetUrlRequestContextTest.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6f2bfbb5ccac7274227cc9f1a7d141988ceaf909 |
| --- /dev/null |
| +++ b/components/cronet/android/test/javatests/src/org/chromium/cronet_test_apk/CronetUrlRequestContextTest.java |
| @@ -0,0 +1,176 @@ |
| +// 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.UrlRequestContextConfig; |
| +import org.chromium.net.UrlRequestException; |
| +import org.chromium.net.UrlRequestListener; |
| + |
| +import java.nio.ByteBuffer; |
| +import java.util.concurrent.ExecutorService; |
| +import java.util.concurrent.Executors; |
| + |
| +/** |
| + * Test CronetUrlRequestContext. |
| + */ |
| +public class CronetUrlRequestContextTest extends CronetTestBase { |
| + // URL used for base tests. |
|
mmenke
2014/10/31 15:49:23
unclear what a "base" test is.
mef
2014/10/31 20:39:16
Acknowledged.
|
| + private static final String TEST_URL = "http://127.0.0.1:8000"; |
| + private static final String MOCK_CRONET_TEST_FAILED_URL = |
| + "http://mock.failed.request/-2"; |
| + // Executor for Cronet callbacks. |
| + ExecutorService mExecutor = Executors.newSingleThreadExecutor(); |
| + CronetTestActivity mActivity; |
| + |
| + class BlockingUrlRequestListener implements UrlRequestListener { |
|
mmenke
2014/10/31 15:49:23
Should comment this class
mef
2014/10/31 20:39:17
Done.
|
| + public UrlRequestException mError; |
| + public boolean mOnErrorCalled = false; |
| + public byte[] mLastDataReceivedAsBytes; |
|
mmenke
2014/10/31 15:49:23
nit: No need for this
mef
2014/10/31 20:39:16
Done.
|
| + public String mResponseAsString = ""; |
| + |
| + private ConditionVariable mDone = new ConditionVariable(); |
| + private ConditionVariable mStarted; |
| + |
| + public BlockingUrlRequestListener(boolean blockOnStarted) { |
|
mmenke
2014/10/31 15:49:23
Should have a comment about the argument.
mef
2014/10/31 20:39:17
Done.
|
| + mStarted = new ConditionVariable(!blockOnStarted); |
| + } |
| + |
| + @Override |
| + public void onRedirect(UrlRequest request, |
| + ResponseInfo info, |
| + String newLocation) { |
| + } |
| + |
| + @Override |
| + public void onResponseStarted(UrlRequest request, ResponseInfo info) { |
| + mStarted.block(); |
| + } |
| + |
| + @Override |
| + public void onDataReceived(UrlRequest request, |
| + ResponseInfo info, |
| + ByteBuffer byteBuffer) { |
| + mLastDataReceivedAsBytes = new byte[byteBuffer.capacity()]; |
| + byteBuffer.get(mLastDataReceivedAsBytes); |
| + mResponseAsString += new String(mLastDataReceivedAsBytes); |
| + } |
| + |
| + @Override |
| + public void onComplete(UrlRequest request, ExtendedResponseInfo info) { |
| + openDone(); |
| + } |
| + |
| + @Override |
| + public void onError(UrlRequest request, |
| + ResponseInfo info, |
| + UrlRequestException error) { |
| + mOnErrorCalled = true; |
| + mError = error; |
| + openDone(); |
| + } |
| + |
| + public void blockForDone() { |
| + mDone.block(); |
| + } |
| + |
| + protected void openDone() { |
| + mDone.open(); |
| + } |
| + |
| + protected void openStarted() { |
| + mStarted.open(); |
| + } |
| + } |
| + |
| + @SmallTest |
| + @Feature({"Cronet"}) |
| + public void testConfigUserAgent() throws Exception { |
| + String userAgentName = "User-Agent"; |
| + String userAgentValue = "User-Agent-Value"; |
| + UrlRequestContextConfig config = new UrlRequestContextConfig(); |
| + config.setUserAgent(userAgentValue); |
| + config.setLibraryName("cronet_tests"); |
| + String[] commandLineArgs = { |
| + CronetTestActivity.CONFIG_KEY, config.toString() |
| + }; |
| + mActivity = launchCronetTestAppWithUrlAndCommandLineArgs(TEST_URL, |
| + commandLineArgs); |
| + waitForActiveShellToBeDoneLoading(); |
| + assertTrue(UploadTestServer.startUploadTestServer()); |
| + BlockingUrlRequestListener listener = |
| + new BlockingUrlRequestListener(false); |
| + UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest( |
| + UploadTestServer.getEchoHeaderURL(userAgentName), listener, |
| + mExecutor); |
| + urlRequest.start(); |
| + listener.blockForDone(); |
| + assertEquals(userAgentValue, listener.mResponseAsString); |
| + } |
| + |
| + @SmallTest |
| + @Feature({"Cronet"}) |
| + public void testShutdown() throws Exception { |
| + mActivity = launchCronetTestApp(); |
| + BlockingUrlRequestListener listener = |
| + new BlockingUrlRequestListener(true); |
|
mmenke
2014/10/31 15:49:23
Should mention in the two tests that block for sta
mef
2014/10/31 20:39:16
Done.
|
| + UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest( |
| + TEST_URL, listener, |
| + mExecutor); |
| + urlRequest.start(); |
| + try { |
| + mActivity.mUrlRequestContext.shutdown(); |
| + fail("Should throw an exception"); |
| + } catch (Exception e) { |
| + assertEquals("Cannot shutdown with active requests.", |
| + e.getMessage()); |
| + } |
| + listener.openStarted(); |
| + listener.blockForDone(); |
| + mActivity.mUrlRequestContext.shutdown(); |
|
mmenke
2014/10/31 15:49:23
Actually, could we have the listener call shutdown
mef
2014/10/31 20:39:17
Done.
|
| + } |
| + |
| + @SmallTest |
| + @Feature({"Cronet"}) |
| + public void testShutdownAfterError() throws Exception { |
| + mActivity = launchCronetTestApp(); |
| + BlockingUrlRequestListener listener = |
| + new BlockingUrlRequestListener(false); |
| + UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest( |
| + MOCK_CRONET_TEST_FAILED_URL, listener, |
| + mExecutor); |
| + urlRequest.start(); |
| + listener.blockForDone(); |
| + assertTrue(listener.mOnErrorCalled); |
| + mActivity.mUrlRequestContext.shutdown(); |
| + } |
| + |
| + @SmallTest |
| + @Feature({"Cronet"}) |
| + public void testShutdownAfterCancel() throws Exception { |
| + mActivity = launchCronetTestApp(); |
| + BlockingUrlRequestListener listener = |
| + new BlockingUrlRequestListener(true); |
| + UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest( |
| + TEST_URL, listener, |
| + mExecutor); |
| + urlRequest.start(); |
| + try { |
| + mActivity.mUrlRequestContext.shutdown(); |
| + fail("Should throw an exception"); |
| + } catch (Exception e) { |
| + assertEquals("Cannot shutdown with active requests.", |
| + e.getMessage()); |
| + } |
| + urlRequest.cancel(); |
| + mActivity.mUrlRequestContext.shutdown(); |
| + } |
| +} |