| 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> |
| 6 |
| 5 #include "android_webview/native/input_stream_impl.h" | 7 #include "android_webview/native/input_stream_impl.h" |
| 6 #include "base/android/jni_android.h" | 8 #include "base/android/jni_android.h" |
| 7 #include "base/android/scoped_java_ref.h" | 9 #include "base/android/scoped_java_ref.h" |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "jni/InputStreamUnittest_jni.h" | 10 #include "jni/InputStreamUnittest_jni.h" |
| 10 #include "net/base/io_buffer.h" | 11 #include "net/base/io_buffer.h" |
| 11 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
| 12 #include "net/http/http_byte_range.h" | 13 #include "net/http/http_byte_range.h" |
| 13 | |
| 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; | 18 using android_webview::InputStreamImpl; |
| 19 using base::android::AttachCurrentThread; | 19 using base::android::AttachCurrentThread; |
| 20 using base::android::ScopedJavaLocalRef; | 20 using base::android::ScopedJavaLocalRef; |
| 21 using net::IOBuffer; | 21 using net::IOBuffer; |
| 22 using testing::DoAll; | 22 using testing::DoAll; |
| 23 using testing::Ge; | 23 using testing::Ge; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 42 ASSERT_TRUE(RegisterNativesImpl(env_)); | 42 ASSERT_TRUE(RegisterNativesImpl(env_)); |
| 43 } | 43 } |
| 44 | 44 |
| 45 scoped_refptr<IOBuffer> DoReadCountedStreamTest(int stream_size, | 45 scoped_refptr<IOBuffer> DoReadCountedStreamTest(int stream_size, |
| 46 int bytes_requested, | 46 int bytes_requested, |
| 47 int* bytes_read) { | 47 int* bytes_read) { |
| 48 ScopedJavaLocalRef<jobject> counting_jstream = | 48 ScopedJavaLocalRef<jobject> counting_jstream = |
| 49 Java_InputStreamUnittest_getCountingStream(env_, stream_size); | 49 Java_InputStreamUnittest_getCountingStream(env_, stream_size); |
| 50 EXPECT_FALSE(counting_jstream.is_null()); | 50 EXPECT_FALSE(counting_jstream.is_null()); |
| 51 | 51 |
| 52 scoped_ptr<InputStream> input_stream( | 52 std::unique_ptr<InputStream> input_stream( |
| 53 new InputStreamImpl(counting_jstream)); | 53 new InputStreamImpl(counting_jstream)); |
| 54 scoped_refptr<IOBuffer> buffer = new IOBuffer(bytes_requested); | 54 scoped_refptr<IOBuffer> buffer = new IOBuffer(bytes_requested); |
| 55 | 55 |
| 56 EXPECT_TRUE(input_stream->Read(buffer.get(), bytes_requested, bytes_read)); | 56 EXPECT_TRUE(input_stream->Read(buffer.get(), bytes_requested, bytes_read)); |
| 57 return buffer; | 57 return buffer; |
| 58 } | 58 } |
| 59 | 59 |
| 60 JNIEnv* env_; | 60 JNIEnv* env_; |
| 61 }; | 61 }; |
| 62 | 62 |
| 63 TEST_F(InputStreamTest, ReadEmptyStream) { | 63 TEST_F(InputStreamTest, ReadEmptyStream) { |
| 64 ScopedJavaLocalRef<jobject> empty_jstream = | 64 ScopedJavaLocalRef<jobject> empty_jstream = |
| 65 Java_InputStreamUnittest_getEmptyStream(env_); | 65 Java_InputStreamUnittest_getEmptyStream(env_); |
| 66 EXPECT_FALSE(empty_jstream.is_null()); | 66 EXPECT_FALSE(empty_jstream.is_null()); |
| 67 | 67 |
| 68 scoped_ptr<InputStream> input_stream(new InputStreamImpl(empty_jstream)); | 68 std::unique_ptr<InputStream> input_stream(new InputStreamImpl(empty_jstream)); |
| 69 const int bytes_requested = 10; | 69 const int bytes_requested = 10; |
| 70 int bytes_read = 0; | 70 int bytes_read = 0; |
| 71 scoped_refptr<IOBuffer> buffer = new IOBuffer(bytes_requested); | 71 scoped_refptr<IOBuffer> buffer = new IOBuffer(bytes_requested); |
| 72 | 72 |
| 73 EXPECT_TRUE(input_stream->Read(buffer.get(), bytes_requested, &bytes_read)); | 73 EXPECT_TRUE(input_stream->Read(buffer.get(), bytes_requested, &bytes_read)); |
| 74 EXPECT_EQ(0, bytes_read); | 74 EXPECT_EQ(0, bytes_read); |
| 75 } | 75 } |
| 76 | 76 |
| 77 TEST_F(InputStreamTest, ReadStreamPartial) { | 77 TEST_F(InputStreamTest, ReadStreamPartial) { |
| 78 const int bytes_requested = 128; | 78 const int bytes_requested = 128; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 int bytes_read = 0; | 118 int bytes_read = 0; |
| 119 DoReadCountedStreamTest(bytes_requested, bytes_requested, &bytes_read); | 119 DoReadCountedStreamTest(bytes_requested, bytes_requested, &bytes_read); |
| 120 EXPECT_EQ(bytes_requested, bytes_read); | 120 EXPECT_EQ(bytes_requested, bytes_read); |
| 121 } | 121 } |
| 122 | 122 |
| 123 TEST_F(InputStreamTest, DoesNotCrashWhenExceptionThrown) { | 123 TEST_F(InputStreamTest, DoesNotCrashWhenExceptionThrown) { |
| 124 ScopedJavaLocalRef<jobject> throw_jstream = | 124 ScopedJavaLocalRef<jobject> throw_jstream = |
| 125 Java_InputStreamUnittest_getThrowingStream(env_); | 125 Java_InputStreamUnittest_getThrowingStream(env_); |
| 126 EXPECT_FALSE(throw_jstream.is_null()); | 126 EXPECT_FALSE(throw_jstream.is_null()); |
| 127 | 127 |
| 128 scoped_ptr<InputStream> input_stream(new InputStreamImpl(throw_jstream)); | 128 std::unique_ptr<InputStream> input_stream(new InputStreamImpl(throw_jstream)); |
| 129 | 129 |
| 130 int64_t bytes_skipped; | 130 int64_t bytes_skipped; |
| 131 EXPECT_FALSE(input_stream->Skip(10, &bytes_skipped)); | 131 EXPECT_FALSE(input_stream->Skip(10, &bytes_skipped)); |
| 132 | 132 |
| 133 int bytes_available; | 133 int bytes_available; |
| 134 EXPECT_FALSE(input_stream->BytesAvailable(&bytes_available)); | 134 EXPECT_FALSE(input_stream->BytesAvailable(&bytes_available)); |
| 135 | 135 |
| 136 | 136 |
| 137 const int bytes_requested = 10; | 137 const int bytes_requested = 10; |
| 138 int bytes_read = 0; | 138 int bytes_read = 0; |
| 139 scoped_refptr<IOBuffer> buffer = new IOBuffer(bytes_requested); | 139 scoped_refptr<IOBuffer> buffer = new IOBuffer(bytes_requested); |
| 140 EXPECT_FALSE(input_stream->Read(buffer.get(), bytes_requested, &bytes_read)); | 140 EXPECT_FALSE(input_stream->Read(buffer.get(), bytes_requested, &bytes_read)); |
| 141 EXPECT_EQ(0, bytes_read); | 141 EXPECT_EQ(0, bytes_read); |
| 142 | 142 |
| 143 // This closes the stream. | 143 // This closes the stream. |
| 144 input_stream.reset(NULL); | 144 input_stream.reset(NULL); |
| 145 } | 145 } |
| OLD | NEW |