1 // Copyright (c) 2012 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 #include "android_webview/native/input_stream_impl.h"
6 #include "base/android/jni_android.h"
7 #include "base/android/scoped_java_ref.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "jni/InputStreamUnittest_jni.h"
10 #include "net/base/io_buffer.h"
11 #include "net/base/net_errors.h"
12 #include "net/http/http_byte_range.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 using android_webview::InputStream
;
18 using android_webview::InputStreamImpl
;
19 using base::android::AttachCurrentThread
;
20 using base::android::ScopedJavaLocalRef
;
24 using testing::InSequence
;
27 using testing::NotNull
;
28 using testing::Return
;
29 using testing::SetArgPointee
;
33 class InputStreamTest
: public Test
{
38 virtual void SetUp() {
39 env_
= AttachCurrentThread();
40 ASSERT_THAT(env_
, NotNull());
41 ASSERT_TRUE(android_webview::RegisterInputStream(env_
));
42 ASSERT_TRUE(RegisterNativesImpl(env_
));
45 scoped_refptr
<IOBuffer
> DoReadCountedStreamTest(int stream_size
,
48 ScopedJavaLocalRef
<jobject
> counting_jstream
=
49 Java_InputStreamUnittest_getCountingStream(env_
, stream_size
);
50 EXPECT_FALSE(counting_jstream
.is_null());
52 scoped_ptr
<InputStream
> input_stream(
53 new InputStreamImpl(counting_jstream
));
54 scoped_refptr
<IOBuffer
> buffer
= new IOBuffer(bytes_requested
);
56 EXPECT_TRUE(input_stream
->Read(buffer
.get(), bytes_requested
, bytes_read
));
63 TEST_F(InputStreamTest
, ReadEmptyStream
) {
64 ScopedJavaLocalRef
<jobject
> empty_jstream
=
65 Java_InputStreamUnittest_getEmptyStream(env_
);
66 EXPECT_FALSE(empty_jstream
.is_null());
68 scoped_ptr
<InputStream
> input_stream(new InputStreamImpl(empty_jstream
));
69 const int bytes_requested
= 10;
71 scoped_refptr
<IOBuffer
> buffer
= new IOBuffer(bytes_requested
);
73 EXPECT_TRUE(input_stream
->Read(buffer
.get(), bytes_requested
, &bytes_read
));
74 EXPECT_EQ(0, bytes_read
);
77 TEST_F(InputStreamTest
, ReadStreamPartial
) {
78 const int bytes_requested
= 128;
80 DoReadCountedStreamTest(bytes_requested
* 2, bytes_requested
, &bytes_read
);
81 EXPECT_EQ(bytes_requested
, bytes_read
);
84 TEST_F(InputStreamTest
, ReadStreamCompletely
) {
85 const int bytes_requested
= 42;
87 DoReadCountedStreamTest(bytes_requested
, bytes_requested
, &bytes_read
);
88 EXPECT_EQ(bytes_requested
, bytes_read
);
91 TEST_F(InputStreamTest
, TryReadMoreThanBuffer
) {
92 const int buffer_size
= 3 * InputStreamImpl::kBufferSize
;
94 DoReadCountedStreamTest(buffer_size
, buffer_size
* 2, &bytes_read
);
95 EXPECT_EQ(buffer_size
, bytes_read
);
98 TEST_F(InputStreamTest
, CheckContentsReadCorrectly
) {
99 const int bytes_requested
= 256;
101 scoped_refptr
<IOBuffer
> buffer
=
102 DoReadCountedStreamTest(bytes_requested
, bytes_requested
, &bytes_read
);
103 EXPECT_EQ(bytes_requested
, bytes_read
);
104 for (int i
= 0; i
< bytes_requested
; ++i
) {
105 EXPECT_EQ(i
, (unsigned char)buffer
->data()[i
]);
109 TEST_F(InputStreamTest
, ReadLargeStreamPartial
) {
110 const int bytes_requested
= 3 * InputStreamImpl::kBufferSize
;
112 DoReadCountedStreamTest(bytes_requested
+ 32, bytes_requested
, &bytes_read
);
113 EXPECT_EQ(bytes_requested
, bytes_read
);
116 TEST_F(InputStreamTest
, ReadLargeStreamCompletely
) {
117 const int bytes_requested
= 3 * InputStreamImpl::kBufferSize
;
119 DoReadCountedStreamTest(bytes_requested
, bytes_requested
, &bytes_read
);
120 EXPECT_EQ(bytes_requested
, bytes_read
);