Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / cronet / android / test / src / org / chromium / net / TestUploadDataStreamHandler.java
blob0d2e6100869a29f4cc2a5857b5b707a77428030e
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;
15 /**
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;
46 /**
47 * Init and returns whether init completes synchronously.
49 public boolean init() {
50 mData = "";
51 nativeInit(mTestUploadDataStreamHandler);
52 mWaitInitCalled.block();
53 mWaitInitCalled.close();
54 return mInitCompletedSynchronously;
57 public void read() {
58 nativeRead(mTestUploadDataStreamHandler);
61 public void reset() {
62 mData = "";
63 nativeReset(mTestUploadDataStreamHandler);
64 mWaitResetComplete.block();
65 mWaitResetComplete.close();
68 /**
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();
78 /**
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() {
89 return mData;
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.
103 @CalledByNative
104 private void onInitCalled(int res) {
105 if (res == 0) {
106 mInitCompletedSynchronously = true;
107 } else {
108 mInitCompletedSynchronously = false;
110 mWaitInitCalled.open();
113 // Called on network thread.
114 @CalledByNative
115 private void onReadCompleted(int bytesRead, String data) {
116 mData = data;
117 mWaitReadComplete.open();
120 // Called on network thread.
121 @CalledByNative
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.
131 @CalledByNative
132 private void onResetCompleted() {
133 mWaitResetComplete.open();
136 // Called on network thread.
137 @CalledByNative
138 private void onCheckInitCallbackNotInvoked(boolean initCallbackNotInvoked) {
139 Assert.assertTrue(initCallbackNotInvoked);
140 mWaitCheckInit.open();
143 // Called on network thread.
144 @CalledByNative
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(
161 long nativePtr);
163 @NativeClassQualifiedName("TestUploadDataStreamHandler")
164 private native void nativeCheckReadCallbackNotInvoked(
165 long nativePtr);
167 @NativeClassQualifiedName("TestUploadDataStreamHandler")
168 private native void nativeDestroy(long nativePtr);
170 private native long nativeCreateTestUploadDataStreamHandler(
171 long uploadDataStream);