| OLD | NEW | 
| (Empty) |  | 
 |    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. | 
 |    4  | 
 |    5 package org.chromium.net; | 
 |    6  | 
 |    7 import android.os.ConditionVariable; | 
 |    8  | 
 |    9 import junit.framework.Assert; | 
 |   10  | 
 |   11 import org.chromium.base.CalledByNative; | 
 |   12 import org.chromium.base.JNINamespace; | 
 |   13 import org.chromium.base.NativeClassQualifiedName; | 
 |   14  | 
 |   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. | 
 |   18  */ | 
 |   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 = ""; | 
 |   33  | 
 |   34     public TestUploadDataStreamHandler(final long uploadDataStream) { | 
 |   35         mTestUploadDataStreamHandler = | 
 |   36                 nativeCreateTestUploadDataStreamHandler(uploadDataStream); | 
 |   37     } | 
 |   38  | 
 |   39     public void destroyNativeObjects() { | 
 |   40         nativeDestroy(mTestUploadDataStreamHandler); | 
 |   41         mTestUploadDataStreamHandler = 0; | 
 |   42     } | 
 |   43  | 
 |   44     /** | 
 |   45      * Init and returns whether init completes synchronously. | 
 |   46      */ | 
 |   47     public boolean init() { | 
 |   48         mData = ""; | 
 |   49         nativeInit(mTestUploadDataStreamHandler); | 
 |   50         mWaitInitCalled.block(); | 
 |   51         mWaitInitCalled.close(); | 
 |   52         return mInitCompletedSynchronously; | 
 |   53     } | 
 |   54  | 
 |   55     public void read() { | 
 |   56         nativeRead(mTestUploadDataStreamHandler); | 
 |   57     } | 
 |   58  | 
 |   59     public void reset() { | 
 |   60         mData = ""; | 
 |   61         nativeReset(mTestUploadDataStreamHandler); | 
 |   62         mWaitResetComplete.block(); | 
 |   63         mWaitResetComplete.close(); | 
 |   64     } | 
 |   65  | 
 |   66     /** | 
 |   67      * Checks that {@link #onInitCompleted} has not invoked asynchronously | 
 |   68      * by the native UploadDataStream. | 
 |   69      */ | 
 |   70     public void checkInitCallbackNotInvoked() { | 
 |   71         nativeCheckInitCallbackNotInvoked(mTestUploadDataStreamHandler); | 
 |   72         mWaitCheckInit.block(); | 
 |   73         mWaitCheckInit.close(); | 
 |   74     } | 
 |   75  | 
 |   76     /** | 
 |   77      * Checks that {@link #onReadCompleted} has not been invoked asynchronously | 
 |   78      * by the native UploadDataStream. | 
 |   79      */ | 
 |   80     public void checkReadCallbackNotInvoked() { | 
 |   81         nativeCheckReadCallbackNotInvoked(mTestUploadDataStreamHandler); | 
 |   82         mWaitCheckRead.block(); | 
 |   83         mWaitCheckRead.close(); | 
 |   84     } | 
 |   85  | 
 |   86     public String getData() { | 
 |   87         return mData; | 
 |   88     } | 
 |   89  | 
 |   90     public void waitForReadComplete() { | 
 |   91         mWaitReadComplete.block(); | 
 |   92         mWaitReadComplete.close(); | 
 |   93     } | 
 |   94  | 
 |   95     public void waitForInitComplete() { | 
 |   96         mWaitInitComplete.block(); | 
 |   97         mWaitInitComplete.close(); | 
 |   98     } | 
 |   99  | 
 |  100     // Called on network thread. | 
 |  101     @CalledByNative | 
 |  102     private void onInitCalled(int res) { | 
 |  103         if (res == 0) { | 
 |  104             mInitCompletedSynchronously = true; | 
 |  105         } else { | 
 |  106             mInitCompletedSynchronously = false; | 
 |  107         } | 
 |  108         mWaitInitCalled.open(); | 
 |  109     } | 
 |  110  | 
 |  111     // Called on network thread. | 
 |  112     @CalledByNative | 
 |  113     private void onReadCompleted(int bytesRead, String data) { | 
 |  114         mData = data; | 
 |  115         mWaitReadComplete.open(); | 
 |  116     } | 
 |  117  | 
 |  118     // Called on network thread. | 
 |  119     @CalledByNative | 
 |  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(); | 
 |  125         } | 
 |  126     } | 
 |  127  | 
 |  128     // Called on network thread. | 
 |  129     @CalledByNative | 
 |  130     private void onResetCompleted() { | 
 |  131         mWaitResetComplete.open(); | 
 |  132     } | 
 |  133  | 
 |  134     // Called on network thread. | 
 |  135     @CalledByNative | 
 |  136     private void onCheckInitCallbackNotInvoked(boolean initCallbackNotInvoked) { | 
 |  137         Assert.assertTrue(initCallbackNotInvoked); | 
 |  138         mWaitCheckInit.open(); | 
 |  139     } | 
 |  140  | 
 |  141     // Called on network thread. | 
 |  142     @CalledByNative | 
 |  143     private void onCheckReadCallbackNotInvoked(boolean readCallbackNotInvoked) { | 
 |  144         Assert.assertTrue(readCallbackNotInvoked); | 
 |  145         mWaitCheckRead.open(); | 
 |  146     } | 
 |  147  | 
 |  148     @NativeClassQualifiedName("TestUploadDataStreamHandler") | 
 |  149     private native void nativeInit(long nativePtr); | 
 |  150  | 
 |  151     @NativeClassQualifiedName("TestUploadDataStreamHandler") | 
 |  152     private native void nativeRead(long nativePtr); | 
 |  153  | 
 |  154     @NativeClassQualifiedName("TestUploadDataStreamHandler") | 
 |  155     private native void nativeReset(long nativePtr); | 
 |  156  | 
 |  157     @NativeClassQualifiedName("TestUploadDataStreamHandler") | 
 |  158     private native void nativeCheckInitCallbackNotInvoked( | 
 |  159             long nativePtr); | 
 |  160  | 
 |  161     @NativeClassQualifiedName("TestUploadDataStreamHandler") | 
 |  162     private native void nativeCheckReadCallbackNotInvoked( | 
 |  163             long nativePtr); | 
 |  164  | 
 |  165     @NativeClassQualifiedName("TestUploadDataStreamHandler") | 
 |  166     private native void nativeDestroy(long nativePtr); | 
 |  167  | 
 |  168     private native long nativeCreateTestUploadDataStreamHandler( | 
 |  169             long uploadDataStream); | 
 |  170 } | 
| OLD | NEW |