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
.CalledByNative
;
12 import org
.chromium
.base
.JNINamespace
;
13 import org
.chromium
.base
.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 nativeDestroy(mTestUploadDataStreamHandler
);
41 mTestUploadDataStreamHandler
= 0;
45 * Init and returns whether init completes synchronously.
47 public boolean init() {
49 nativeInit(mTestUploadDataStreamHandler
);
50 mWaitInitCalled
.block();
51 mWaitInitCalled
.close();
52 return mInitCompletedSynchronously
;
56 nativeRead(mTestUploadDataStreamHandler
);
61 nativeReset(mTestUploadDataStreamHandler
);
62 mWaitResetComplete
.block();
63 mWaitResetComplete
.close();
67 * Checks that {@link #onInitCompleted} has not invoked asynchronously
68 * by the native UploadDataStream.
70 public void checkInitCallbackNotInvoked() {
71 nativeCheckInitCallbackNotInvoked(mTestUploadDataStreamHandler
);
72 mWaitCheckInit
.block();
73 mWaitCheckInit
.close();
77 * Checks that {@link #onReadCompleted} has not been invoked asynchronously
78 * by the native UploadDataStream.
80 public void checkReadCallbackNotInvoked() {
81 nativeCheckReadCallbackNotInvoked(mTestUploadDataStreamHandler
);
82 mWaitCheckRead
.block();
83 mWaitCheckRead
.close();
86 public String
getData() {
90 public void waitForReadComplete() {
91 mWaitReadComplete
.block();
92 mWaitReadComplete
.close();
95 public void waitForInitComplete() {
96 mWaitInitComplete
.block();
97 mWaitInitComplete
.close();
100 // Called on network thread.
102 private void onInitCalled(int res
) {
104 mInitCompletedSynchronously
= true;
106 mInitCompletedSynchronously
= false;
108 mWaitInitCalled
.open();
111 // Called on network thread.
113 private void onReadCompleted(int bytesRead
, String data
) {
115 mWaitReadComplete
.open();
118 // Called on network thread.
120 private void onInitCompleted(int res
) {
121 // If init() completed synchronously, waitForInitComplete() will
122 // not be invoked in the test, so skip mWaitInitComplete.open().
123 if (!mInitCompletedSynchronously
) {
124 mWaitInitComplete
.open();
128 // Called on network thread.
130 private void onResetCompleted() {
131 mWaitResetComplete
.open();
134 // Called on network thread.
136 private void onCheckInitCallbackNotInvoked(boolean initCallbackNotInvoked
) {
137 Assert
.assertTrue(initCallbackNotInvoked
);
138 mWaitCheckInit
.open();
141 // Called on network thread.
143 private void onCheckReadCallbackNotInvoked(boolean readCallbackNotInvoked
) {
144 Assert
.assertTrue(readCallbackNotInvoked
);
145 mWaitCheckRead
.open();
148 @NativeClassQualifiedName("TestUploadDataStreamHandler")
149 private native void nativeInit(long nativePtr
);
151 @NativeClassQualifiedName("TestUploadDataStreamHandler")
152 private native void nativeRead(long nativePtr
);
154 @NativeClassQualifiedName("TestUploadDataStreamHandler")
155 private native void nativeReset(long nativePtr
);
157 @NativeClassQualifiedName("TestUploadDataStreamHandler")
158 private native void nativeCheckInitCallbackNotInvoked(
161 @NativeClassQualifiedName("TestUploadDataStreamHandler")
162 private native void nativeCheckReadCallbackNotInvoked(
165 @NativeClassQualifiedName("TestUploadDataStreamHandler")
166 private native void nativeDestroy(long nativePtr
);
168 private native long nativeCreateTestUploadDataStreamHandler(
169 long uploadDataStream
);