• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 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;
6 
7 import android.content.Context;
8 import android.net.http.HttpEngine;
9 import android.os.ConditionVariable;
10 
11 import org.junit.Assert;
12 
13 import org.chromium.base.annotations.CalledByNative;
14 import org.chromium.base.annotations.JNINamespace;
15 import org.chromium.base.annotations.NativeClassQualifiedName;
16 import org.chromium.base.annotations.NativeMethods;
17 import org.chromium.net.impl.CronetUrlRequestContext;
18 
19 /**
20  * A wrapper class on top of the native net::UploadDataStream. This class is
21  * used in tests to drive the native UploadDataStream directly.
22  */
23 @JNINamespace("cronet")
24 public final class TestUploadDataStreamHandler {
25     private final CronetTestUtil.NetworkThreadTestConnector mNetworkThreadTestConnector;
26     private final HttpEngine mCronetEngine;
27     private long mTestUploadDataStreamHandler;
28     private ConditionVariable mWaitInitCalled = new ConditionVariable();
29     private ConditionVariable mWaitInitComplete = new ConditionVariable();
30     private ConditionVariable mWaitReadComplete = new ConditionVariable();
31     private ConditionVariable mWaitResetComplete = new ConditionVariable();
32     // Waits for checkIfInitCallbackInvoked() returns result asynchronously.
33     private ConditionVariable mWaitCheckInit = new ConditionVariable();
34     // Waits for checkIfReadCallbackInvoked() returns result asynchronously.
35     private ConditionVariable mWaitCheckRead = new ConditionVariable();
36     // If true, init completes synchronously.
37     private boolean mInitCompletedSynchronously;
38     private String mData = "";
39 
TestUploadDataStreamHandler(Context context, final long uploadDataStream)40     public TestUploadDataStreamHandler(Context context, final long uploadDataStream) {
41         mCronetEngine = new HttpEngine.Builder(context).build();
42         mNetworkThreadTestConnector = new CronetTestUtil.NetworkThreadTestConnector(mCronetEngine);
43         CronetUrlRequestContext requestContext = (CronetUrlRequestContext) mCronetEngine;
44         mTestUploadDataStreamHandler =
45                 TestUploadDataStreamHandlerJni.get().createTestUploadDataStreamHandler(
46                         this, uploadDataStream, requestContext.getUrlRequestContextAdapter());
47     }
48 
destroyNativeObjects()49     public void destroyNativeObjects() {
50         if (mTestUploadDataStreamHandler != 0) {
51             TestUploadDataStreamHandlerJni.get().destroy(mTestUploadDataStreamHandler);
52             mTestUploadDataStreamHandler = 0;
53             mNetworkThreadTestConnector.shutdown();
54             mCronetEngine.shutdown();
55         }
56     }
57 
58     /**
59      * Init and returns whether init completes synchronously.
60      */
init()61     public boolean init() {
62         mData = "";
63         TestUploadDataStreamHandlerJni.get().init(mTestUploadDataStreamHandler);
64         mWaitInitCalled.block();
65         mWaitInitCalled.close();
66         return mInitCompletedSynchronously;
67     }
68 
read()69     public void read() {
70         TestUploadDataStreamHandlerJni.get().read(mTestUploadDataStreamHandler);
71     }
72 
reset()73     public void reset() {
74         mData = "";
75         TestUploadDataStreamHandlerJni.get().reset(mTestUploadDataStreamHandler);
76         mWaitResetComplete.block();
77         mWaitResetComplete.close();
78     }
79 
80     /**
81      * Checks that {@link #onInitCompleted} has not invoked asynchronously
82      * by the native UploadDataStream.
83      */
checkInitCallbackNotInvoked()84     public void checkInitCallbackNotInvoked() {
85         TestUploadDataStreamHandlerJni.get().checkInitCallbackNotInvoked(
86                 mTestUploadDataStreamHandler);
87         mWaitCheckInit.block();
88         mWaitCheckInit.close();
89     }
90 
91     /**
92      * Checks that {@link #onReadCompleted} has not been invoked asynchronously
93      * by the native UploadDataStream.
94      */
checkReadCallbackNotInvoked()95     public void checkReadCallbackNotInvoked() {
96         TestUploadDataStreamHandlerJni.get().checkReadCallbackNotInvoked(
97                 mTestUploadDataStreamHandler);
98         mWaitCheckRead.block();
99         mWaitCheckRead.close();
100     }
101 
getData()102     public String getData() {
103         return mData;
104     }
105 
waitForReadComplete()106     public void waitForReadComplete() {
107         mWaitReadComplete.block();
108         mWaitReadComplete.close();
109     }
110 
waitForInitComplete()111     public void waitForInitComplete() {
112         mWaitInitComplete.block();
113         mWaitInitComplete.close();
114     }
115 
116     // Called on network thread.
117     @CalledByNative
onInitCalled(int res)118     private void onInitCalled(int res) {
119         if (res == 0) {
120             mInitCompletedSynchronously = true;
121         } else {
122             mInitCompletedSynchronously = false;
123         }
124         mWaitInitCalled.open();
125     }
126 
127     // Called on network thread.
128     @CalledByNative
onReadCompleted(int bytesRead, String data)129     private void onReadCompleted(int bytesRead, String data) {
130         mData = data;
131         mWaitReadComplete.open();
132     }
133 
134     // Called on network thread.
135     @CalledByNative
onInitCompleted(int res)136     private void onInitCompleted(int res) {
137         // If init() completed synchronously, waitForInitComplete() will
138         // not be invoked in the test, so skip mWaitInitComplete.open().
139         if (!mInitCompletedSynchronously) {
140             mWaitInitComplete.open();
141         }
142     }
143 
144     // Called on network thread.
145     @CalledByNative
onResetCompleted()146     private void onResetCompleted() {
147         mWaitResetComplete.open();
148     }
149 
150     // Called on network thread.
151     @CalledByNative
onCheckInitCallbackNotInvoked(boolean initCallbackNotInvoked)152     private void onCheckInitCallbackNotInvoked(boolean initCallbackNotInvoked) {
153         Assert.assertTrue(initCallbackNotInvoked);
154         mWaitCheckInit.open();
155     }
156 
157     // Called on network thread.
158     @CalledByNative
onCheckReadCallbackNotInvoked(boolean readCallbackNotInvoked)159     private void onCheckReadCallbackNotInvoked(boolean readCallbackNotInvoked) {
160         Assert.assertTrue(readCallbackNotInvoked);
161         mWaitCheckRead.open();
162     }
163 
164     @NativeMethods("cronet_tests")
165     interface Natives {
166         @NativeClassQualifiedName("TestUploadDataStreamHandler")
init(long nativePtr)167         void init(long nativePtr);
168 
169         @NativeClassQualifiedName("TestUploadDataStreamHandler")
read(long nativePtr)170         void read(long nativePtr);
171 
172         @NativeClassQualifiedName("TestUploadDataStreamHandler")
reset(long nativePtr)173         void reset(long nativePtr);
174 
175         @NativeClassQualifiedName("TestUploadDataStreamHandler")
checkInitCallbackNotInvoked(long nativePtr)176         void checkInitCallbackNotInvoked(long nativePtr);
177 
178         @NativeClassQualifiedName("TestUploadDataStreamHandler")
checkReadCallbackNotInvoked(long nativePtr)179         void checkReadCallbackNotInvoked(long nativePtr);
180 
181         @NativeClassQualifiedName("TestUploadDataStreamHandler")
destroy(long nativePtr)182         void destroy(long nativePtr);
183 
createTestUploadDataStreamHandler( TestUploadDataStreamHandler obj, long uploadDataStream, long contextAdapter)184         long createTestUploadDataStreamHandler(
185                 TestUploadDataStreamHandler obj, long uploadDataStream, long contextAdapter);
186     }
187 }
188