| 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..a237f9edb025265a55d23d5b0cdef6684cf27a74
|
| --- /dev/null
|
| +++ b/components/cronet/android/test/javatests/src/org/chromium/cronet_test_apk/CronetUrlRequestContextTest.java
|
| @@ -0,0 +1,129 @@
|
| +// 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.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 static org.chromium.cronet_test_apk.TestUrlRequestListener.FailureType;
|
| +import static org.chromium.cronet_test_apk.TestUrlRequestListener.ResponseStep;
|
| +
|
| +/**
|
| + * Test CronetUrlRequestContext.
|
| + */
|
| +public class CronetUrlRequestContextTest extends CronetTestBase {
|
| + // URLs used for tests.
|
| + 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";
|
| +
|
| + CronetTestActivity mActivity;
|
| +
|
| + /**
|
| + * Listener that shutdowns the request context when request has succeeded
|
| + * or failed.
|
| + */
|
| + class ShutdownTestUrlRequestListener extends TestUrlRequestListener {
|
| + @Override
|
| + public void onSucceeded(UrlRequest request, ExtendedResponseInfo info) {
|
| + super.onSucceeded(request, info);
|
| + mActivity.mUrlRequestContext.shutdown();
|
| + }
|
| +
|
| + @Override
|
| + public void onFailed(UrlRequest request,
|
| + ResponseInfo info,
|
| + UrlRequestException error) {
|
| + super.onFailed(request, info, error);
|
| + mActivity.mUrlRequestContext.shutdown();
|
| + }
|
| + }
|
| +
|
| + @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());
|
| + TestUrlRequestListener listener = new TestUrlRequestListener();
|
| + UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest(
|
| + UploadTestServer.getEchoHeaderURL(userAgentName), listener,
|
| + listener.getExecutor());
|
| + urlRequest.start();
|
| + listener.blockForDone();
|
| + assertEquals(userAgentValue, listener.mResponseAsString);
|
| + }
|
| +
|
| + @SmallTest
|
| + @Feature({"Cronet"})
|
| + public void testShutdown() throws Exception {
|
| + mActivity = launchCronetTestApp();
|
| + TestUrlRequestListener listener = new ShutdownTestUrlRequestListener();
|
| + // Block listener when response starts to verify that shutdown fails
|
| + // if there are active requests.
|
| + listener.setFailure(FailureType.BLOCK, ResponseStep.ON_RESPONSE_STARTED);
|
| + UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest(
|
| + TEST_URL, listener, listener.getExecutor());
|
| + urlRequest.start();
|
| + try {
|
| + mActivity.mUrlRequestContext.shutdown();
|
| + fail("Should throw an exception");
|
| + } catch (Exception e) {
|
| + assertEquals("Cannot shutdown with active requests.",
|
| + e.getMessage());
|
| + }
|
| + listener.openBlockedStep();
|
| + listener.blockForDone();
|
| + }
|
| +
|
| + @SmallTest
|
| + @Feature({"Cronet"})
|
| + public void testShutdownAfterError() throws Exception {
|
| + mActivity = launchCronetTestApp();
|
| + TestUrlRequestListener listener = new ShutdownTestUrlRequestListener();
|
| + UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest(
|
| + MOCK_CRONET_TEST_FAILED_URL, listener, listener.getExecutor());
|
| + urlRequest.start();
|
| + listener.blockForDone();
|
| + assertTrue(listener.mOnErrorCalled);
|
| + }
|
| +
|
| + @SmallTest
|
| + @Feature({"Cronet"})
|
| + public void testShutdownAfterCancel() throws Exception {
|
| + mActivity = launchCronetTestApp();
|
| + TestUrlRequestListener listener = new TestUrlRequestListener();
|
| + // Block listener when response starts to verify that shutdown fails
|
| + // if there are active requests.
|
| + listener.setFailure(FailureType.BLOCK, ResponseStep.ON_RESPONSE_STARTED);
|
| + UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest(
|
| + TEST_URL, listener, listener.getExecutor());
|
| + 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();
|
| + }
|
| +}
|
|
|