OLD | NEW |
(Empty) | |
| 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 |
| 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" |
| 13 |
| 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: |
| 38 virtual void SetUp() { |
| 39 env_ = AttachCurrentThread(); |
| 40 ASSERT_THAT(env_, NotNull()); |
| 41 ASSERT_TRUE(android_webview::RegisterInputStream(env_)); |
| 42 ASSERT_TRUE(RegisterNativesImpl(env_)); |
| 43 } |
| 44 |
| 45 scoped_refptr<IOBuffer> DoReadCountedStreamTest(int streamSize, |
| 46 int bytesToRead) { |
| 47 ScopedJavaLocalRef<jobject> counting_jstream = |
| 48 Java_InputStreamUnittest_getCountingStream(env_, streamSize); |
| 49 EXPECT_FALSE(counting_jstream.is_null()); |
| 50 |
| 51 scoped_ptr<InputStream> input_stream( |
| 52 new InputStreamImpl(counting_jstream)); |
| 53 int bytesRead = 0; |
| 54 scoped_refptr<IOBuffer> buffer = new IOBuffer(bytesToRead); |
| 55 |
| 56 EXPECT_TRUE(input_stream->Read(buffer.get(), bytesToRead, &bytesRead)); |
| 57 EXPECT_EQ(bytesToRead, bytesRead); |
| 58 return buffer; |
| 59 } |
| 60 |
| 61 JNIEnv* env_; |
| 62 }; |
| 63 |
| 64 TEST_F(InputStreamTest, ReadEmptyStream) { |
| 65 ScopedJavaLocalRef<jobject> empty_jstream = |
| 66 Java_InputStreamUnittest_getEmptyStream(env_); |
| 67 EXPECT_FALSE(empty_jstream.is_null()); |
| 68 |
| 69 scoped_ptr<InputStream> input_stream(new InputStreamImpl(empty_jstream)); |
| 70 const int bytesToRead = 10; |
| 71 int bytesRead = 0; |
| 72 scoped_refptr<IOBuffer> buffer = new IOBuffer(bytesToRead); |
| 73 |
| 74 EXPECT_TRUE(input_stream->Read(buffer.get(), bytesToRead, &bytesRead)); |
| 75 EXPECT_EQ(0, bytesRead); |
| 76 } |
| 77 |
| 78 TEST_F(InputStreamTest, ReadStreamPartial) { |
| 79 const int bytesToRead = 128; |
| 80 DoReadCountedStreamTest(bytesToRead * 2, bytesToRead); |
| 81 } |
| 82 |
| 83 TEST_F(InputStreamTest, ReadStreamCompletely) { |
| 84 const int bytesToRead = 42; |
| 85 DoReadCountedStreamTest(bytesToRead, bytesToRead); |
| 86 } |
| 87 |
| 88 TEST_F(InputStreamTest, ReadStreamInChunks) { |
| 89 const int bytesToRead = 3 * InputStreamImpl::kBufferSize; |
| 90 DoReadCountedStreamTest(bytesToRead, bytesToRead); |
| 91 } |
| 92 |
| 93 TEST_F(InputStreamTest, CheckContentsReadCorrectly) { |
| 94 const int bytesToRead = 256; |
| 95 scoped_refptr<IOBuffer> buffer = |
| 96 DoReadCountedStreamTest(bytesToRead, bytesToRead); |
| 97 for (int i = 0; i < bytesToRead; ++i) { |
| 98 EXPECT_EQ(i, buffer->data()[i]); |
| 99 } |
| 100 } |
OLD | NEW |