1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org
.chromium
.net
;
7 import android
.os
.ConditionVariable
;
9 import junit
.framework
.Assert
;
11 import org
.chromium
.base
.annotations
.CalledByNative
;
12 import org
.chromium
.base
.annotations
.JNINamespace
;
13 import org
.chromium
.base
.annotations
.NativeClassQualifiedName
;
16 * A wrapper class on top of the native net::UploadDataStream. This class is
17 * used in tests to drive the native UploadDataStream directly.
19 @JNINamespace("cronet")
20 public final class TestUploadDataStreamHandler
{
21 private long mTestUploadDataStreamHandler
;
22 private ConditionVariable mWaitInitCalled
= new ConditionVariable();
23 private ConditionVariable mWaitInitComplete
= new ConditionVariable();
24 private ConditionVariable mWaitReadComplete
= new ConditionVariable();
25 private ConditionVariable mWaitResetComplete
= new ConditionVariable();
26 // Waits for checkIfInitCallbackInvoked() returns result asynchronously.
27 private ConditionVariable mWaitCheckInit
= new ConditionVariable();
28 // Waits for checkIfReadCallbackInvoked() returns result asynchronously.
29 private ConditionVariable mWaitCheckRead
= new ConditionVariable();
30 // If true, init completes synchronously.
31 private boolean mInitCompletedSynchronously
= false;
32 private String mData
= "";
34 public TestUploadDataStreamHandler(final long uploadDataStream
) {
35 mTestUploadDataStreamHandler
=
36 nativeCreateTestUploadDataStreamHandler(uploadDataStream
);
39 public void destroyNativeObjects() {
40 if (mTestUploadDataStreamHandler
!= 0) {
41 nativeDestroy(mTestUploadDataStreamHandler
);
42 mTestUploadDataStreamHandler
= 0;
47 * Init and returns whether init completes synchronously.
49 public boolean init() {
51 nativeInit(mTestUploadDataStreamHandler
);
52 mWaitInitCalled
.block();
53 mWaitInitCalled
.close();
54 return mInitCompletedSynchronously
;
58 nativeRead(mTestUploadDataStreamHandler
);
63 nativeReset(mTestUploadDataStreamHandler
);
64 mWaitResetComplete
.block();
65 mWaitResetComplete
.close();
69 * Checks that {@link #onInitCompleted} has not invoked asynchronously
70 * by the native UploadDataStream.
72 public void checkInitCallbackNotInvoked() {
73 nativeCheckInitCallbackNotInvoked(mTestUploadDataStreamHandler
);
74 mWaitCheckInit
.block();
75 mWaitCheckInit
.close();
79 * Checks that {@link #onReadCompleted} has not been invoked asynchronously
80 * by the native UploadDataStream.
82 public void checkReadCallbackNotInvoked() {
83 nativeCheckReadCallbackNotInvoked(mTestUploadDataStreamHandler
);
84 mWaitCheckRead
.block();
85 mWaitCheckRead
.close();
88 public String
getData() {
92 public void waitForReadComplete() {
93 mWaitReadComplete
.block();
94 mWaitReadComplete
.close();
97 public void waitForInitComplete() {
98 mWaitInitComplete
.block();
99 mWaitInitComplete
.close();
102 // Called on network thread.
104 private void onInitCalled(int res
) {
106 mInitCompletedSynchronously
= true;
108 mInitCompletedSynchronously
= false;
110 mWaitInitCalled
.open();
113 // Called on network thread.
115 private void onReadCompleted(int bytesRead
, String data
) {
117 mWaitReadComplete
.open();
120 // Called on network thread.
122 private void onInitCompleted(int res
) {
123 // If init() completed synchronously, waitForInitComplete() will
124 // not be invoked in the test, so skip mWaitInitComplete.open().
125 if (!mInitCompletedSynchronously
) {
126 mWaitInitComplete
.open();
130 // Called on network thread.
132 private void onResetCompleted() {
133 mWaitResetComplete
.open();
136 // Called on network thread.
138 private void onCheckInitCallbackNotInvoked(boolean initCallbackNotInvoked
) {
139 Assert
.assertTrue(initCallbackNotInvoked
);
140 mWaitCheckInit
.open();
143 // Called on network thread.
145 private void onCheckReadCallbackNotInvoked(boolean readCallbackNotInvoked
) {
146 Assert
.assertTrue(readCallbackNotInvoked
);
147 mWaitCheckRead
.open();
150 @NativeClassQualifiedName("TestUploadDataStreamHandler")
151 private native void nativeInit(long nativePtr
);
153 @NativeClassQualifiedName("TestUploadDataStreamHandler")
154 private native void nativeRead(long nativePtr
);
156 @NativeClassQualifiedName("TestUploadDataStreamHandler")
157 private native void nativeReset(long nativePtr
);
159 @NativeClassQualifiedName("TestUploadDataStreamHandler")
160 private native void nativeCheckInitCallbackNotInvoked(
163 @NativeClassQualifiedName("TestUploadDataStreamHandler")
164 private native void nativeCheckReadCallbackNotInvoked(
167 @NativeClassQualifiedName("TestUploadDataStreamHandler")
168 private native void nativeDestroy(long nativePtr
);
170 private native long nativeCreateTestUploadDataStreamHandler(
171 long uploadDataStream
);