| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <memory> | 5 #include <memory> |
| 6 | 6 |
| 7 #include "android_webview/browser/input_stream_impl.h" | 7 #include "android_webview/browser/input_stream.h" |
| 8 #include "base/android/jni_android.h" | 8 #include "base/android/jni_android.h" |
| 9 #include "base/android/scoped_java_ref.h" | 9 #include "base/android/scoped_java_ref.h" |
| 10 #include "jni/InputStreamUnittest_jni.h" | 10 #include "jni/InputStreamUnittest_jni.h" |
| 11 #include "net/base/io_buffer.h" | 11 #include "net/base/io_buffer.h" |
| 12 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
| 13 #include "net/http/http_byte_range.h" | 13 #include "net/http/http_byte_range.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 16 |
| 17 using android_webview::InputStream; | 17 using android_webview::InputStream; |
| 18 using android_webview::InputStreamImpl; | |
| 19 using base::android::AttachCurrentThread; | 18 using base::android::AttachCurrentThread; |
| 20 using base::android::ScopedJavaLocalRef; | 19 using base::android::ScopedJavaLocalRef; |
| 21 using net::IOBuffer; | 20 using net::IOBuffer; |
| 22 using testing::DoAll; | 21 using testing::DoAll; |
| 23 using testing::Ge; | 22 using testing::Ge; |
| 24 using testing::InSequence; | 23 using testing::InSequence; |
| 25 using testing::Lt; | 24 using testing::Lt; |
| 26 using testing::Ne; | 25 using testing::Ne; |
| 27 using testing::NotNull; | 26 using testing::NotNull; |
| 28 using testing::Return; | 27 using testing::Return; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 41 } | 40 } |
| 42 | 41 |
| 43 scoped_refptr<IOBuffer> DoReadCountedStreamTest(int stream_size, | 42 scoped_refptr<IOBuffer> DoReadCountedStreamTest(int stream_size, |
| 44 int bytes_requested, | 43 int bytes_requested, |
| 45 int* bytes_read) { | 44 int* bytes_read) { |
| 46 ScopedJavaLocalRef<jobject> counting_jstream = | 45 ScopedJavaLocalRef<jobject> counting_jstream = |
| 47 Java_InputStreamUnittest_getCountingStream(env_, stream_size); | 46 Java_InputStreamUnittest_getCountingStream(env_, stream_size); |
| 48 EXPECT_FALSE(counting_jstream.is_null()); | 47 EXPECT_FALSE(counting_jstream.is_null()); |
| 49 | 48 |
| 50 std::unique_ptr<InputStream> input_stream( | 49 std::unique_ptr<InputStream> input_stream( |
| 51 new InputStreamImpl(counting_jstream)); | 50 new InputStream(counting_jstream)); |
| 52 scoped_refptr<IOBuffer> buffer = new IOBuffer(bytes_requested); | 51 scoped_refptr<IOBuffer> buffer = new IOBuffer(bytes_requested); |
| 53 | 52 |
| 54 EXPECT_TRUE(input_stream->Read(buffer.get(), bytes_requested, bytes_read)); | 53 EXPECT_TRUE(input_stream->Read(buffer.get(), bytes_requested, bytes_read)); |
| 55 return buffer; | 54 return buffer; |
| 56 } | 55 } |
| 57 | 56 |
| 58 JNIEnv* env_; | 57 JNIEnv* env_; |
| 59 }; | 58 }; |
| 60 | 59 |
| 61 TEST_F(InputStreamTest, ReadEmptyStream) { | 60 TEST_F(InputStreamTest, ReadEmptyStream) { |
| 62 ScopedJavaLocalRef<jobject> empty_jstream = | 61 ScopedJavaLocalRef<jobject> empty_jstream = |
| 63 Java_InputStreamUnittest_getEmptyStream(env_); | 62 Java_InputStreamUnittest_getEmptyStream(env_); |
| 64 EXPECT_FALSE(empty_jstream.is_null()); | 63 EXPECT_FALSE(empty_jstream.is_null()); |
| 65 | 64 |
| 66 std::unique_ptr<InputStream> input_stream(new InputStreamImpl(empty_jstream)); | 65 std::unique_ptr<InputStream> input_stream(new InputStream(empty_jstream)); |
| 67 const int bytes_requested = 10; | 66 const int bytes_requested = 10; |
| 68 int bytes_read = 0; | 67 int bytes_read = 0; |
| 69 scoped_refptr<IOBuffer> buffer = new IOBuffer(bytes_requested); | 68 scoped_refptr<IOBuffer> buffer = new IOBuffer(bytes_requested); |
| 70 | 69 |
| 71 EXPECT_TRUE(input_stream->Read(buffer.get(), bytes_requested, &bytes_read)); | 70 EXPECT_TRUE(input_stream->Read(buffer.get(), bytes_requested, &bytes_read)); |
| 72 EXPECT_EQ(0, bytes_read); | 71 EXPECT_EQ(0, bytes_read); |
| 73 } | 72 } |
| 74 | 73 |
| 75 TEST_F(InputStreamTest, ReadStreamPartial) { | 74 TEST_F(InputStreamTest, ReadStreamPartial) { |
| 76 const int bytes_requested = 128; | 75 const int bytes_requested = 128; |
| 77 int bytes_read = 0; | 76 int bytes_read = 0; |
| 78 DoReadCountedStreamTest(bytes_requested * 2, bytes_requested, &bytes_read); | 77 DoReadCountedStreamTest(bytes_requested * 2, bytes_requested, &bytes_read); |
| 79 EXPECT_EQ(bytes_requested, bytes_read); | 78 EXPECT_EQ(bytes_requested, bytes_read); |
| 80 } | 79 } |
| 81 | 80 |
| 82 TEST_F(InputStreamTest, ReadStreamCompletely) { | 81 TEST_F(InputStreamTest, ReadStreamCompletely) { |
| 83 const int bytes_requested = 42; | 82 const int bytes_requested = 42; |
| 84 int bytes_read = 0; | 83 int bytes_read = 0; |
| 85 DoReadCountedStreamTest(bytes_requested, bytes_requested, &bytes_read); | 84 DoReadCountedStreamTest(bytes_requested, bytes_requested, &bytes_read); |
| 86 EXPECT_EQ(bytes_requested, bytes_read); | 85 EXPECT_EQ(bytes_requested, bytes_read); |
| 87 } | 86 } |
| 88 | 87 |
| 89 TEST_F(InputStreamTest, TryReadMoreThanBuffer) { | 88 TEST_F(InputStreamTest, TryReadMoreThanBuffer) { |
| 90 const int buffer_size = 3 * InputStreamImpl::kBufferSize; | 89 const int buffer_size = 3 * InputStream::kBufferSize; |
| 91 int bytes_read = 0; | 90 int bytes_read = 0; |
| 92 DoReadCountedStreamTest(buffer_size, buffer_size * 2, &bytes_read); | 91 DoReadCountedStreamTest(buffer_size, buffer_size * 2, &bytes_read); |
| 93 EXPECT_EQ(buffer_size, bytes_read); | 92 EXPECT_EQ(buffer_size, bytes_read); |
| 94 } | 93 } |
| 95 | 94 |
| 96 TEST_F(InputStreamTest, CheckContentsReadCorrectly) { | 95 TEST_F(InputStreamTest, CheckContentsReadCorrectly) { |
| 97 const int bytes_requested = 256; | 96 const int bytes_requested = 256; |
| 98 int bytes_read = 0; | 97 int bytes_read = 0; |
| 99 scoped_refptr<IOBuffer> buffer = | 98 scoped_refptr<IOBuffer> buffer = |
| 100 DoReadCountedStreamTest(bytes_requested, bytes_requested, &bytes_read); | 99 DoReadCountedStreamTest(bytes_requested, bytes_requested, &bytes_read); |
| 101 EXPECT_EQ(bytes_requested, bytes_read); | 100 EXPECT_EQ(bytes_requested, bytes_read); |
| 102 for (int i = 0; i < bytes_requested; ++i) { | 101 for (int i = 0; i < bytes_requested; ++i) { |
| 103 EXPECT_EQ(i, (unsigned char)buffer->data()[i]); | 102 EXPECT_EQ(i, (unsigned char)buffer->data()[i]); |
| 104 } | 103 } |
| 105 } | 104 } |
| 106 | 105 |
| 107 TEST_F(InputStreamTest, ReadLargeStreamPartial) { | 106 TEST_F(InputStreamTest, ReadLargeStreamPartial) { |
| 108 const int bytes_requested = 3 * InputStreamImpl::kBufferSize; | 107 const int bytes_requested = 3 * InputStream::kBufferSize; |
| 109 int bytes_read = 0; | 108 int bytes_read = 0; |
| 110 DoReadCountedStreamTest(bytes_requested + 32, bytes_requested, &bytes_read); | 109 DoReadCountedStreamTest(bytes_requested + 32, bytes_requested, &bytes_read); |
| 111 EXPECT_EQ(bytes_requested, bytes_read); | 110 EXPECT_EQ(bytes_requested, bytes_read); |
| 112 } | 111 } |
| 113 | 112 |
| 114 TEST_F(InputStreamTest, ReadLargeStreamCompletely) { | 113 TEST_F(InputStreamTest, ReadLargeStreamCompletely) { |
| 115 const int bytes_requested = 3 * InputStreamImpl::kBufferSize; | 114 const int bytes_requested = 3 * InputStream::kBufferSize; |
| 116 int bytes_read = 0; | 115 int bytes_read = 0; |
| 117 DoReadCountedStreamTest(bytes_requested, bytes_requested, &bytes_read); | 116 DoReadCountedStreamTest(bytes_requested, bytes_requested, &bytes_read); |
| 118 EXPECT_EQ(bytes_requested, bytes_read); | 117 EXPECT_EQ(bytes_requested, bytes_read); |
| 119 } | 118 } |
| 120 | 119 |
| 121 TEST_F(InputStreamTest, DoesNotCrashWhenExceptionThrown) { | 120 TEST_F(InputStreamTest, DoesNotCrashWhenExceptionThrown) { |
| 122 ScopedJavaLocalRef<jobject> throw_jstream = | 121 ScopedJavaLocalRef<jobject> throw_jstream = |
| 123 Java_InputStreamUnittest_getThrowingStream(env_); | 122 Java_InputStreamUnittest_getThrowingStream(env_); |
| 124 EXPECT_FALSE(throw_jstream.is_null()); | 123 EXPECT_FALSE(throw_jstream.is_null()); |
| 125 | 124 |
| 126 std::unique_ptr<InputStream> input_stream(new InputStreamImpl(throw_jstream)); | 125 std::unique_ptr<InputStream> input_stream(new InputStream(throw_jstream)); |
| 127 | 126 |
| 128 int64_t bytes_skipped; | 127 int64_t bytes_skipped; |
| 129 EXPECT_FALSE(input_stream->Skip(10, &bytes_skipped)); | 128 EXPECT_FALSE(input_stream->Skip(10, &bytes_skipped)); |
| 130 | 129 |
| 131 int bytes_available; | 130 int bytes_available; |
| 132 EXPECT_FALSE(input_stream->BytesAvailable(&bytes_available)); | 131 EXPECT_FALSE(input_stream->BytesAvailable(&bytes_available)); |
| 133 | 132 |
| 134 const int bytes_requested = 10; | 133 const int bytes_requested = 10; |
| 135 int bytes_read = 0; | 134 int bytes_read = 0; |
| 136 scoped_refptr<IOBuffer> buffer = new IOBuffer(bytes_requested); | 135 scoped_refptr<IOBuffer> buffer = new IOBuffer(bytes_requested); |
| 137 EXPECT_FALSE(input_stream->Read(buffer.get(), bytes_requested, &bytes_read)); | 136 EXPECT_FALSE(input_stream->Read(buffer.get(), bytes_requested, &bytes_read)); |
| 138 EXPECT_EQ(0, bytes_read); | 137 EXPECT_EQ(0, bytes_read); |
| 139 | 138 |
| 140 // This closes the stream. | 139 // This closes the stream. |
| 141 input_stream.reset(NULL); | 140 input_stream.reset(NULL); |
| 142 } | 141 } |
| OLD | NEW |