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 <memory> | |
6 | |
7 #include "android_webview/native/input_stream_impl.h" | |
8 #include "base/android/jni_android.h" | |
9 #include "base/android/scoped_java_ref.h" | |
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" | |
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 void SetUp() override { | |
39 env_ = AttachCurrentThread(); | |
40 ASSERT_THAT(env_, NotNull()); | |
41 } | |
42 | |
43 scoped_refptr<IOBuffer> DoReadCountedStreamTest(int stream_size, | |
44 int bytes_requested, | |
45 int* bytes_read) { | |
46 ScopedJavaLocalRef<jobject> counting_jstream = | |
47 Java_InputStreamUnittest_getCountingStream(env_, stream_size); | |
48 EXPECT_FALSE(counting_jstream.is_null()); | |
49 | |
50 std::unique_ptr<InputStream> input_stream( | |
51 new InputStreamImpl(counting_jstream)); | |
52 scoped_refptr<IOBuffer> buffer = new IOBuffer(bytes_requested); | |
53 | |
54 EXPECT_TRUE(input_stream->Read(buffer.get(), bytes_requested, bytes_read)); | |
55 return buffer; | |
56 } | |
57 | |
58 JNIEnv* env_; | |
59 }; | |
60 | |
61 TEST_F(InputStreamTest, ReadEmptyStream) { | |
62 ScopedJavaLocalRef<jobject> empty_jstream = | |
63 Java_InputStreamUnittest_getEmptyStream(env_); | |
64 EXPECT_FALSE(empty_jstream.is_null()); | |
65 | |
66 std::unique_ptr<InputStream> input_stream(new InputStreamImpl(empty_jstream)); | |
67 const int bytes_requested = 10; | |
68 int bytes_read = 0; | |
69 scoped_refptr<IOBuffer> buffer = new IOBuffer(bytes_requested); | |
70 | |
71 EXPECT_TRUE(input_stream->Read(buffer.get(), bytes_requested, &bytes_read)); | |
72 EXPECT_EQ(0, bytes_read); | |
73 } | |
74 | |
75 TEST_F(InputStreamTest, ReadStreamPartial) { | |
76 const int bytes_requested = 128; | |
77 int bytes_read = 0; | |
78 DoReadCountedStreamTest(bytes_requested * 2, bytes_requested, &bytes_read); | |
79 EXPECT_EQ(bytes_requested, bytes_read); | |
80 } | |
81 | |
82 TEST_F(InputStreamTest, ReadStreamCompletely) { | |
83 const int bytes_requested = 42; | |
84 int bytes_read = 0; | |
85 DoReadCountedStreamTest(bytes_requested, bytes_requested, &bytes_read); | |
86 EXPECT_EQ(bytes_requested, bytes_read); | |
87 } | |
88 | |
89 TEST_F(InputStreamTest, TryReadMoreThanBuffer) { | |
90 const int buffer_size = 3 * InputStreamImpl::kBufferSize; | |
91 int bytes_read = 0; | |
92 DoReadCountedStreamTest(buffer_size, buffer_size * 2, &bytes_read); | |
93 EXPECT_EQ(buffer_size, bytes_read); | |
94 } | |
95 | |
96 TEST_F(InputStreamTest, CheckContentsReadCorrectly) { | |
97 const int bytes_requested = 256; | |
98 int bytes_read = 0; | |
99 scoped_refptr<IOBuffer> buffer = | |
100 DoReadCountedStreamTest(bytes_requested, bytes_requested, &bytes_read); | |
101 EXPECT_EQ(bytes_requested, bytes_read); | |
102 for (int i = 0; i < bytes_requested; ++i) { | |
103 EXPECT_EQ(i, (unsigned char)buffer->data()[i]); | |
104 } | |
105 } | |
106 | |
107 TEST_F(InputStreamTest, ReadLargeStreamPartial) { | |
108 const int bytes_requested = 3 * InputStreamImpl::kBufferSize; | |
109 int bytes_read = 0; | |
110 DoReadCountedStreamTest(bytes_requested + 32, bytes_requested, &bytes_read); | |
111 EXPECT_EQ(bytes_requested, bytes_read); | |
112 } | |
113 | |
114 TEST_F(InputStreamTest, ReadLargeStreamCompletely) { | |
115 const int bytes_requested = 3 * InputStreamImpl::kBufferSize; | |
116 int bytes_read = 0; | |
117 DoReadCountedStreamTest(bytes_requested, bytes_requested, &bytes_read); | |
118 EXPECT_EQ(bytes_requested, bytes_read); | |
119 } | |
120 | |
121 TEST_F(InputStreamTest, DoesNotCrashWhenExceptionThrown) { | |
122 ScopedJavaLocalRef<jobject> throw_jstream = | |
123 Java_InputStreamUnittest_getThrowingStream(env_); | |
124 EXPECT_FALSE(throw_jstream.is_null()); | |
125 | |
126 std::unique_ptr<InputStream> input_stream(new InputStreamImpl(throw_jstream)); | |
127 | |
128 int64_t bytes_skipped; | |
129 EXPECT_FALSE(input_stream->Skip(10, &bytes_skipped)); | |
130 | |
131 int bytes_available; | |
132 EXPECT_FALSE(input_stream->BytesAvailable(&bytes_available)); | |
133 | |
134 | |
135 const int bytes_requested = 10; | |
136 int bytes_read = 0; | |
137 scoped_refptr<IOBuffer> buffer = new IOBuffer(bytes_requested); | |
138 EXPECT_FALSE(input_stream->Read(buffer.get(), bytes_requested, &bytes_read)); | |
139 EXPECT_EQ(0, bytes_read); | |
140 | |
141 // This closes the stream. | |
142 input_stream.reset(NULL); | |
143 } | |
OLD | NEW |