| 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..3e9683dc86e4d561bbea067e9ee8377cc5aa80fe
|
| --- /dev/null
|
| +++ b/components/cronet/android/test/javatests/src/org/chromium/cronet_test_apk/CronetUrlRequestContextTest.java
|
| @@ -0,0 +1,150 @@
|
| +// 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.net.URL;
|
| +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,
|
| + URL 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();
|
| + }
|
| +
|
| + 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() };
|
| + mActivity = launchCronetTestAppWithUrlAndCommandLineArgs(TEST_URL,
|
| + commandLineArgs);
|
| + // Make sure the activity was created as expected.
|
| + assertNotNull(mActivity);
|
| + 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);
|
| + mActivity.mUrlRequestContext.shutdown();
|
| + }
|
| +}
|
|
|