Chromium Code Reviews| Index: components/cronet/android/test/smoketests/src/org/chromium/net/smoke/TestUrlRequestCallback.java |
| diff --git a/components/cronet/android/test/smoketests/src/org/chromium/net/smoke/TestUrlRequestCallback.java b/components/cronet/android/test/smoketests/src/org/chromium/net/smoke/TestUrlRequestCallback.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9df5ecd992027a04b502aef2864102e144cacf4a |
| --- /dev/null |
| +++ b/components/cronet/android/test/smoketests/src/org/chromium/net/smoke/TestUrlRequestCallback.java |
| @@ -0,0 +1,112 @@ |
| +// Copyright 2016 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.net.smoke; |
| + |
| +import static junit.framework.Assert.assertTrue; |
| + |
| +import android.os.ConditionVariable; |
| + |
| +import org.chromium.net.CronetException; |
| +import org.chromium.net.UrlRequest; |
| +import org.chromium.net.UrlResponseInfo; |
| + |
| +import java.nio.ByteBuffer; |
| +import java.util.concurrent.Executor; |
| +import java.util.concurrent.ExecutorService; |
| +import java.util.concurrent.Executors; |
| + |
| +/** |
| + * A simple boilerplate implementation of {@link UrlRequest.Callback} that is used by smoke tests. |
| + */ |
| +class TestUrlRequestCallback extends UrlRequest.Callback { |
|
mef
2016/12/20 20:17:45
Nit: maybe rename to SmokeTestCallback?
kapishnikov
2016/12/22 19:21:13
Renamed to SmokeTestRequestCallback
|
| + private static final int READ_BUFFER_SIZE = 10000; |
| + |
| + // An executor that is used to execute {@link UrlRequest.Callback UrlRequest callbacks}. |
| + private ExecutorService mExecutor = Executors.newSingleThreadExecutor(); |
| + |
| + // Signals when the request is done either successfully or not. |
| + private final ConditionVariable mDone = new ConditionVariable(); |
| + |
| + // The state of the request. |
| + public enum State { NotSet, Succeeded, Failed, Canceled } |
| + |
| + // The current state of the request. |
| + private State mState = State.NotSet; |
| + |
| + // Response info passed to the last invoked {@link UrlRequest.Callback} method. |
| + private UrlResponseInfo mResponseInfo; |
| + |
| + @Override |
| + public void onRedirectReceived(UrlRequest request, UrlResponseInfo info, String newLocationUrl) |
| + throws Exception { |
| + mResponseInfo = info; |
| + request.followRedirect(); |
| + } |
| + |
| + @Override |
| + public void onResponseStarted(UrlRequest request, UrlResponseInfo info) throws Exception { |
| + mResponseInfo = info; |
| + request.read(ByteBuffer.allocateDirect(READ_BUFFER_SIZE)); |
| + } |
| + |
| + @Override |
| + public void onReadCompleted(UrlRequest request, UrlResponseInfo info, ByteBuffer byteBuffer) |
| + throws Exception { |
| + mResponseInfo = info; |
| + request.read(ByteBuffer.allocateDirect(READ_BUFFER_SIZE)); |
| + } |
| + |
| + @Override |
| + public void onSucceeded(UrlRequest request, UrlResponseInfo info) { |
| + assertTrue(mState == State.NotSet); |
| + mResponseInfo = info; |
| + mState = State.Succeeded; |
| + mDone.open(); |
| + } |
| + |
| + @Override |
| + public void onFailed(UrlRequest request, UrlResponseInfo info, CronetException error) { |
| + super.onFailed(request, info, error); |
|
mef
2016/12/20 20:17:45
why does this call super.onFailed?
kapishnikov
2016/12/22 19:21:13
Removed.
|
| + assertTrue(mState == State.NotSet); |
| + mResponseInfo = info; |
| + mState = State.Failed; |
| + mDone.open(); |
| + } |
| + |
| + @Override |
| + public void onCanceled(UrlRequest request, UrlResponseInfo info) { |
| + super.onCanceled(request, info); |
|
mef
2016/12/20 20:17:45
why does this call super.onCanceled?
kapishnikov
2016/12/22 19:21:13
Removed.
|
| + assertTrue(mState == State.NotSet); |
| + mResponseInfo = info; |
| + mState = State.Canceled; |
| + mDone.open(); |
| + } |
| + |
| + /** |
| + * Returns the request executor. |
| + * |
| + * @return the executor. |
| + */ |
| + public Executor getExecutor() { |
| + return mExecutor; |
| + } |
| + |
| + /** |
| + * Blocks until the request is either succeeded, failed or canceled. |
| + */ |
| + public void blockForDone() { |
| + mDone.block(); |
| + } |
| + |
| + /** |
| + * Returns {@link UrlResponseInfo} that was passed to the last invoked |
| + * {@link UrlRequest.Callback} method. |
| + * |
| + * @return the last URL response info object. |
| + */ |
| + public UrlResponseInfo getResponseInfo() { |
| + return mResponseInfo; |
| + } |
| +} |