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..31fb3ccd7cba2ec4bca2221926279eedbf5a246e |
| --- /dev/null |
| +++ b/components/cronet/android/test/javatests/src/org/chromium/cronet_test_apk/CronetUrlRequestContextTest.java |
| @@ -0,0 +1,149 @@ |
| +// 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; |
| +import java.util.concurrent.TimeUnit; |
| + |
| +/** |
| + * Test CronetUrlRequestContext. |
| + */ |
| +public class CronetUrlRequestContextTest extends CronetTestBase { |
| + // URL used for base tests. |
| + private static final String TEST_URL = "http://127.0.0.1:8000"; |
| + |
| + // Executor for Cronet callbacks. |
| + ExecutorService mExecutor = Executors.newSingleThreadExecutor(); |
| + CronetTestActivity mActivity; |
| + |
| + class BlockingUrlRequestListener implements UrlRequestListener { |
| + public UrlRequestException mError; |
| + public boolean mOnErrorCalled = false; |
| + public byte[] mLastDataReceivedAsBytes; |
| + public String mResponseAsString = ""; |
| + |
| + private ConditionVariable mComplete = new ConditionVariable(); |
| + private ConditionVariable mStarted; |
| + |
| + public BlockingUrlRequestListener(boolean blockOnStarted) { |
| + 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) { |
| + openComplete(); |
| + } |
| + |
| + @Override |
| + public void onError(UrlRequest request, |
| + ResponseInfo info, |
| + UrlRequestException error) { |
| + mOnErrorCalled = true; |
| + mError = error; |
| + openComplete(); |
|
mmenke
2014/10/30 21:47:57
Per earlier comments, should rename this stuff...o
mef
2014/10/30 22:32:55
Done.
|
| + } |
| + |
| + public void blockForComplete() { |
| + mComplete.block(); |
| + } |
| + |
| + protected void openComplete() { |
| + mComplete.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() }; |
|
mmenke
2014/10/30 21:47:56
Unless it makes the Java style checker sad (In whi
mef
2014/10/30 22:32:56
Done.
|
| + mActivity = launchCronetTestAppWithUrlAndCommandLineArgs(TEST_URL, |
| + commandLineArgs); |
| + // Make sure the activity was created as expected. |
| + assertNotNull(mActivity); |
|
mmenke
2014/10/30 21:47:56
Can we just put this in "launchCronetTestAppWithUr
mef
2014/10/30 22:32:56
Done.
|
| + waitForActiveShellToBeDoneLoading(); |
| + assertTrue(UploadTestServer.startUploadTestServer()); |
| + |
| + BlockingUrlRequestListener listener = |
| + new BlockingUrlRequestListener(false); |
| + UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest( |
| + UploadTestServer.getEchoHeaderURL(userAgentName), listener, |
| + mExecutor); |
| + urlRequest.start(); |
| + listener.blockForComplete(); |
| + assertEquals(userAgentValue, listener.mResponseAsString); |
| + } |
| + |
| + @SmallTest |
| + @Feature({"Cronet"}) |
| + public void testShutdown() throws Exception { |
| + mActivity = launchCronetTestApp(); |
| + // Make sure the activity was created as expected. |
| + assertNotNull(mActivity); |
| + waitForActiveShellToBeDoneLoading(); |
| + 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()); |
| + } |
| + listener.openStarted(); |
| + listener.blockForComplete(); |
| + // Wait for request to get destroyed. |
| + mExecutor.shutdown(); |
| + mExecutor.awaitTermination(5, TimeUnit.SECONDS); |
|
mmenke
2014/10/30 21:47:56
I don't think we should force the embedder to wait
mef
2014/10/30 22:32:56
Done.
|
| + mActivity.mUrlRequestContext.shutdown(); |
| + } |
| +} |