Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / cronet / android / test / test_upload_data_stream_handler.h
blobdddfbf4a77067e0038f2acc7e79351d50a93cea1
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 #ifndef COMPONENTS_CRONET_ANDROID_TEST_TEST_UPLOAD_DATA_STREAM_HANDLER_H_
6 #define COMPONENTS_CRONET_ANDROID_TEST_TEST_UPLOAD_DATA_STREAM_HANDLER_H_
8 #include <jni.h>
10 #include "base/android/scoped_java_ref.h"
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/threading/thread.h"
15 #include "net/base/io_buffer.h"
16 #include "net/base/upload_data_stream.h"
18 namespace cronet {
20 /**
21 * This class allows a net::UploadDataStream to be driven directly from
22 * Java, for use in tests.
24 class TestUploadDataStreamHandler {
25 public:
26 TestUploadDataStreamHandler(
27 scoped_ptr<net::UploadDataStream> upload_data_stream,
28 JNIEnv* env,
29 jobject jtest_upload_data_stream_handler);
31 ~TestUploadDataStreamHandler();
33 // Destroys |network_thread_| created by this class.
34 void Destroy(JNIEnv* env, jobject jcaller);
36 // Posts a task to |network_thread_| to call the corresponding method of
37 // net::UploadDataStream on |upload_data_stream_|.
39 void Init(JNIEnv* env, jobject jcaller);
40 void Read(JNIEnv* env, jobject jcaller);
41 void Reset(JNIEnv* env, jobject jcaller);
43 // Posts a task to |network_thread_| to check whether init complete callback
44 // has been invoked by net::UploadDataStream asynchronously, and notifies the
45 // Java side of the result.
46 void CheckInitCallbackNotInvoked(JNIEnv* env, jobject jcaller);
47 // Posts a task to |network_thread_| to check whether read complete callback
48 // has been invoked by net::UploadDataStream asynchronously, and notifies the
49 // Java side of the result.
50 void CheckReadCallbackNotInvoked(JNIEnv* env, jobject jcaller);
52 private:
53 // Complete callbacks that are passed to the |upload_data_stream_|.
54 void OnInitCompleted(int res);
55 void OnReadCompleted(int res);
57 // Helper methods that run corresponding task on |network_thread_|.
59 void InitOnNetworkThread();
60 void ReadOnNetworkThread();
61 void ResetOnNetworkThread();
62 void CheckInitCallbackNotInvokedOnNetworkThread();
63 void CheckReadCallbackNotInvokedOnNetworkThread();
65 // Notify the Java TestUploadDataStreamHandler that read has completed.
66 void NotifyJavaReadCompleted();
68 // True if |OnInitCompleted| callback has been invoked. It is set to false
69 // when init or reset is called again. Created on a Java thread, but is only
70 // accessed from |network_thread_|.
71 bool init_callback_invoked_;
72 // True if |OnReadCompleted| callback has been invoked. It is set to false
73 // when init or reset is called again. Created on a Java thread, but is only
74 // accessed from |network_thread_|.
75 bool read_callback_invoked_;
76 // Indicates the number of bytes read. It is reset to 0 when init, reset, or
77 // read is called again. Created on a Java thread, but is only accessed from
78 // |network_thread_|.
79 int bytes_read_;
81 // Created and destroyed on the same Java thread. This is where methods of
82 // net::UploadDataStream run on.
83 scoped_ptr<base::Thread> network_thread_;
84 // Created on a Java thread. Accessed only on |network_thread_|.
85 scoped_ptr<net::UploadDataStream> upload_data_stream_;
86 // Created and accessed only on |network_thread_|.
87 scoped_refptr<net::IOBufferWithSize> read_buffer_;
88 // A Java reference pointer for calling methods on the Java
89 // TestUploadDataStreamHandler object. Initialized during construction.
90 base::android::ScopedJavaGlobalRef<jobject> jtest_upload_data_stream_handler_;
92 DISALLOW_COPY_AND_ASSIGN(TestUploadDataStreamHandler);
95 bool TestUploadDataStreamHandlerRegisterJni(JNIEnv* env);
97 } // namespace cronet
99 #endif // COMPONENTS_CRONET_ANDROID_TEST_TEST_UPLOAD_DATA_STREAM_HANDLER_H_