• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.net.smoke;
6 
7 import static junit.framework.Assert.assertTrue;
8 
9 import android.os.ConditionVariable;
10 
11 import org.chromium.net.CronetException;
12 import org.chromium.net.UrlRequest;
13 import org.chromium.net.UrlResponseInfo;
14 
15 import java.nio.ByteBuffer;
16 import java.util.concurrent.Executor;
17 import java.util.concurrent.ExecutorService;
18 import java.util.concurrent.Executors;
19 
20 /**
21  * A simple boilerplate implementation of {@link UrlRequest.Callback} that is used by smoke tests.
22  */
23 class SmokeTestRequestCallback extends UrlRequest.Callback {
24     private static final int READ_BUFFER_SIZE = 10000;
25 
26     // An executor that is used to execute {@link UrlRequest.Callback UrlRequest callbacks}.
27     private ExecutorService mExecutor = Executors.newSingleThreadExecutor();
28 
29     // Signals when the request is done either successfully or not.
30     private final ConditionVariable mDone = new ConditionVariable();
31 
32     // The state of the request.
33     public enum State { NotSet, Succeeded, Failed, Canceled }
34 
35     // The current state of the request.
36     private State mState = State.NotSet;
37 
38     // Response info of the finished request.
39     private UrlResponseInfo mResponseInfo;
40 
41     // Holds an error if the request failed.
42     private CronetException mError;
43 
44     @Override
onRedirectReceived(UrlRequest request, UrlResponseInfo info, String newLocationUrl)45     public void onRedirectReceived(UrlRequest request, UrlResponseInfo info, String newLocationUrl)
46             throws Exception {
47         request.followRedirect();
48     }
49 
50     @Override
onResponseStarted(UrlRequest request, UrlResponseInfo info)51     public void onResponseStarted(UrlRequest request, UrlResponseInfo info) throws Exception {
52         request.read(ByteBuffer.allocateDirect(READ_BUFFER_SIZE));
53     }
54 
55     @Override
onReadCompleted(UrlRequest request, UrlResponseInfo info, ByteBuffer byteBuffer)56     public void onReadCompleted(UrlRequest request, UrlResponseInfo info, ByteBuffer byteBuffer)
57             throws Exception {
58         request.read(ByteBuffer.allocateDirect(READ_BUFFER_SIZE));
59     }
60 
61     @Override
onSucceeded(UrlRequest request, UrlResponseInfo info)62     public void onSucceeded(UrlRequest request, UrlResponseInfo info) {
63         done(State.Succeeded, info);
64     }
65 
66     @Override
onFailed(UrlRequest request, UrlResponseInfo info, CronetException error)67     public void onFailed(UrlRequest request, UrlResponseInfo info, CronetException error) {
68         mError = error;
69         done(State.Failed, info);
70     }
71 
72     @Override
onCanceled(UrlRequest request, UrlResponseInfo info)73     public void onCanceled(UrlRequest request, UrlResponseInfo info) {
74         done(State.Canceled, info);
75     }
76 
77     /**
78      * Returns the request executor.
79      *
80      * @return the executor.
81      */
getExecutor()82     public Executor getExecutor() {
83         return mExecutor;
84     }
85 
86     /**
87      * Blocks until the request is either succeeded, failed or canceled.
88      */
blockForDone()89     public void blockForDone() {
90         mDone.block();
91     }
92 
93     /**
94      * Returns the final state of the request.
95      *
96      * @return the state.
97      */
getFinalState()98     public State getFinalState() {
99         return mState;
100     }
101 
102     /**
103      * Returns an error that was passed to {@link #onFailed}  when the request failed.
104      *
105      * @return the error if the request failed; {@code null} otherwise.
106      */
getFailureError()107     public CronetException getFailureError() {
108         return mError;
109     }
110 
111     /**
112      * Returns {@link UrlResponseInfo} of the finished response.
113      *
114      * @return the response info. {@code null} if the request hasn't completed yet.
115      */
getResponseInfo()116     public UrlResponseInfo getResponseInfo() {
117         return mResponseInfo;
118     }
119 
done(State finalState, UrlResponseInfo responseInfo)120     private void done(State finalState, UrlResponseInfo responseInfo) {
121         assertTrue(mState == State.NotSet);
122         mResponseInfo = responseInfo;
123         mState = finalState;
124         mDone.open();
125     }
126 }
127