[email protected] | 1737386 | 2012-11-22 16:21:26 | [diff] [blame] | 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. |
| 4 | |
dcheng | 08daa07 | 2016-04-01 01:47:21 | [diff] [blame] | 5 | #include <memory> |
| 6 | |
[email protected] | 1737386 | 2012-11-22 16:21:26 | [diff] [blame] | 7 | #include "android_webview/native/input_stream_impl.h" |
| 8 | #include "base/android/jni_android.h" |
| 9 | #include "base/android/scoped_java_ref.h" |
[email protected] | 1737386 | 2012-11-22 16:21:26 | [diff] [blame] | 10 | #include "jni/InputStreamUnittest_jni.h" |
| 11 | #include "net/base/io_buffer.h" |
| 12 | #include "net/base/net_errors.h" |
| 13 | #include "net/http/http_byte_range.h" |
[email protected] | 1737386 | 2012-11-22 16:21:26 | [diff] [blame] | 14 | #include "testing/gmock/include/gmock/gmock.h" |
| 15 | #include "testing/gtest/include/gtest/gtest.h" |
| 16 | |
| 17 | using android_webview::InputStream; |
| 18 | using android_webview::InputStreamImpl; |
| 19 | using base::android::AttachCurrentThread; |
| 20 | using base::android::ScopedJavaLocalRef; |
| 21 | using net::IOBuffer; |
| 22 | using testing::DoAll; |
| 23 | using testing::Ge; |
| 24 | using testing::InSequence; |
| 25 | using testing::Lt; |
| 26 | using testing::Ne; |
| 27 | using testing::NotNull; |
| 28 | using testing::Return; |
| 29 | using testing::SetArgPointee; |
| 30 | using testing::Test; |
| 31 | using testing::_; |
| 32 | |
| 33 | class InputStreamTest : public Test { |
| 34 | public: |
| 35 | InputStreamTest() { |
| 36 | } |
| 37 | protected: |
dcheng | 9b4dd1aa | 2015-02-04 02:56:54 | [diff] [blame] | 38 | void SetUp() override { |
[email protected] | 1737386 | 2012-11-22 16:21:26 | [diff] [blame] | 39 | env_ = AttachCurrentThread(); |
| 40 | ASSERT_THAT(env_, NotNull()); |
| 41 | ASSERT_TRUE(android_webview::RegisterInputStream(env_)); |
| 42 | ASSERT_TRUE(RegisterNativesImpl(env_)); |
| 43 | } |
| 44 | |
[email protected] | 681739f | 2012-12-05 14:59:58 | [diff] [blame] | 45 | scoped_refptr<IOBuffer> DoReadCountedStreamTest(int stream_size, |
| 46 | int bytes_requested, |
| 47 | int* bytes_read) { |
[email protected] | 1737386 | 2012-11-22 16:21:26 | [diff] [blame] | 48 | ScopedJavaLocalRef<jobject> counting_jstream = |
[email protected] | 681739f | 2012-12-05 14:59:58 | [diff] [blame] | 49 | Java_InputStreamUnittest_getCountingStream(env_, stream_size); |
[email protected] | 1737386 | 2012-11-22 16:21:26 | [diff] [blame] | 50 | EXPECT_FALSE(counting_jstream.is_null()); |
| 51 | |
dcheng | 08daa07 | 2016-04-01 01:47:21 | [diff] [blame] | 52 | std::unique_ptr<InputStream> input_stream( |
[email protected] | 1737386 | 2012-11-22 16:21:26 | [diff] [blame] | 53 | new InputStreamImpl(counting_jstream)); |
[email protected] | 681739f | 2012-12-05 14:59:58 | [diff] [blame] | 54 | scoped_refptr<IOBuffer> buffer = new IOBuffer(bytes_requested); |
[email protected] | 1737386 | 2012-11-22 16:21:26 | [diff] [blame] | 55 | |
[email protected] | 681739f | 2012-12-05 14:59:58 | [diff] [blame] | 56 | EXPECT_TRUE(input_stream->Read(buffer.get(), bytes_requested, bytes_read)); |
[email protected] | 1737386 | 2012-11-22 16:21:26 | [diff] [blame] | 57 | return buffer; |
| 58 | } |
| 59 | |
| 60 | JNIEnv* env_; |
| 61 | }; |
| 62 | |
| 63 | TEST_F(InputStreamTest, ReadEmptyStream) { |
| 64 | ScopedJavaLocalRef<jobject> empty_jstream = |
| 65 | Java_InputStreamUnittest_getEmptyStream(env_); |
| 66 | EXPECT_FALSE(empty_jstream.is_null()); |
| 67 | |
dcheng | 08daa07 | 2016-04-01 01:47:21 | [diff] [blame] | 68 | std::unique_ptr<InputStream> input_stream(new InputStreamImpl(empty_jstream)); |
[email protected] | 681739f | 2012-12-05 14:59:58 | [diff] [blame] | 69 | const int bytes_requested = 10; |
| 70 | int bytes_read = 0; |
| 71 | scoped_refptr<IOBuffer> buffer = new IOBuffer(bytes_requested); |
[email protected] | 1737386 | 2012-11-22 16:21:26 | [diff] [blame] | 72 | |
[email protected] | 681739f | 2012-12-05 14:59:58 | [diff] [blame] | 73 | EXPECT_TRUE(input_stream->Read(buffer.get(), bytes_requested, &bytes_read)); |
| 74 | EXPECT_EQ(0, bytes_read); |
[email protected] | 1737386 | 2012-11-22 16:21:26 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | TEST_F(InputStreamTest, ReadStreamPartial) { |
[email protected] | 681739f | 2012-12-05 14:59:58 | [diff] [blame] | 78 | const int bytes_requested = 128; |
| 79 | int bytes_read = 0; |
| 80 | DoReadCountedStreamTest(bytes_requested * 2, bytes_requested, &bytes_read); |
| 81 | EXPECT_EQ(bytes_requested, bytes_read); |
[email protected] | 1737386 | 2012-11-22 16:21:26 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | TEST_F(InputStreamTest, ReadStreamCompletely) { |
[email protected] | 681739f | 2012-12-05 14:59:58 | [diff] [blame] | 85 | const int bytes_requested = 42; |
| 86 | int bytes_read = 0; |
| 87 | DoReadCountedStreamTest(bytes_requested, bytes_requested, &bytes_read); |
| 88 | EXPECT_EQ(bytes_requested, bytes_read); |
[email protected] | 1737386 | 2012-11-22 16:21:26 | [diff] [blame] | 89 | } |
| 90 | |
[email protected] | 681739f | 2012-12-05 14:59:58 | [diff] [blame] | 91 | TEST_F(InputStreamTest, TryReadMoreThanBuffer) { |
[email protected] | 81959dd | 2013-09-19 06:17:48 | [diff] [blame] | 92 | const int buffer_size = 3 * InputStreamImpl::kBufferSize; |
[email protected] | 681739f | 2012-12-05 14:59:58 | [diff] [blame] | 93 | int bytes_read = 0; |
[email protected] | 81959dd | 2013-09-19 06:17:48 | [diff] [blame] | 94 | DoReadCountedStreamTest(buffer_size, buffer_size * 2, &bytes_read); |
| 95 | EXPECT_EQ(buffer_size, bytes_read); |
[email protected] | 1737386 | 2012-11-22 16:21:26 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | TEST_F(InputStreamTest, CheckContentsReadCorrectly) { |
[email protected] | 681739f | 2012-12-05 14:59:58 | [diff] [blame] | 99 | const int bytes_requested = 256; |
| 100 | int bytes_read = 0; |
[email protected] | 1737386 | 2012-11-22 16:21:26 | [diff] [blame] | 101 | scoped_refptr<IOBuffer> buffer = |
[email protected] | 681739f | 2012-12-05 14:59:58 | [diff] [blame] | 102 | DoReadCountedStreamTest(bytes_requested, bytes_requested, &bytes_read); |
| 103 | EXPECT_EQ(bytes_requested, bytes_read); |
| 104 | for (int i = 0; i < bytes_requested; ++i) { |
[email protected] | 11a5e05 | 2013-01-18 10:04:06 | [diff] [blame] | 105 | EXPECT_EQ(i, (unsigned char)buffer->data()[i]); |
[email protected] | 1737386 | 2012-11-22 16:21:26 | [diff] [blame] | 106 | } |
| 107 | } |
[email protected] | 81959dd | 2013-09-19 06:17:48 | [diff] [blame] | 108 | |
| 109 | TEST_F(InputStreamTest, ReadLargeStreamPartial) { |
| 110 | const int bytes_requested = 3 * InputStreamImpl::kBufferSize; |
| 111 | int bytes_read = 0; |
| 112 | DoReadCountedStreamTest(bytes_requested + 32, bytes_requested, &bytes_read); |
| 113 | EXPECT_EQ(bytes_requested, bytes_read); |
| 114 | } |
| 115 | |
| 116 | TEST_F(InputStreamTest, ReadLargeStreamCompletely) { |
| 117 | const int bytes_requested = 3 * InputStreamImpl::kBufferSize; |
| 118 | int bytes_read = 0; |
| 119 | DoReadCountedStreamTest(bytes_requested, bytes_requested, &bytes_read); |
| 120 | EXPECT_EQ(bytes_requested, bytes_read); |
| 121 | } |
[email protected] | 17fd3aa | 2014-07-29 16:35:42 | [diff] [blame] | 122 | |
| 123 | TEST_F(InputStreamTest, DoesNotCrashWhenExceptionThrown) { |
| 124 | ScopedJavaLocalRef<jobject> throw_jstream = |
| 125 | Java_InputStreamUnittest_getThrowingStream(env_); |
| 126 | EXPECT_FALSE(throw_jstream.is_null()); |
| 127 | |
dcheng | 08daa07 | 2016-04-01 01:47:21 | [diff] [blame] | 128 | std::unique_ptr<InputStream> input_stream(new InputStreamImpl(throw_jstream)); |
[email protected] | 17fd3aa | 2014-07-29 16:35:42 | [diff] [blame] | 129 | |
| 130 | int64_t bytes_skipped; |
| 131 | EXPECT_FALSE(input_stream->Skip(10, &bytes_skipped)); |
| 132 | |
| 133 | int bytes_available; |
| 134 | EXPECT_FALSE(input_stream->BytesAvailable(&bytes_available)); |
| 135 | |
| 136 | |
| 137 | const int bytes_requested = 10; |
| 138 | int bytes_read = 0; |
| 139 | scoped_refptr<IOBuffer> buffer = new IOBuffer(bytes_requested); |
| 140 | EXPECT_FALSE(input_stream->Read(buffer.get(), bytes_requested, &bytes_read)); |
| 141 | EXPECT_EQ(0, bytes_read); |
| 142 | |
| 143 | // This closes the stream. |
| 144 | input_stream.reset(NULL); |
| 145 | } |