Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(142)

Side by Side Diff: android_webview/native/input_stream_unittest.cc

Issue 11363123: [android_webview] Don't block the IO thread when reading from an InputStream. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix clang error Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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.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 base::android::AttachCurrentThread;
19 using base::android::ScopedJavaLocalRef;
20 using net::IOBuffer;
21 using testing::DoAll;
22 using testing::Ge;
23 using testing::InSequence;
24 using testing::Lt;
25 using testing::Ne;
26 using testing::NotNull;
27 using testing::Return;
28 using testing::SetArgPointee;
29 using testing::Test;
30 using testing::_;
31
32 class InputStreamTest : public Test {
33 public:
34 InputStreamTest() {
35 }
36 protected:
37 virtual void SetUp() {
38 env_ = AttachCurrentThread();
39 ASSERT_THAT(env_, NotNull());
40 ASSERT_TRUE(android_webview::RegisterInputStream(env_));
41 ASSERT_TRUE(RegisterNativesImpl(env_));
42 }
43
44 scoped_refptr<IOBuffer> DoReadCountedStreamTest(int streamSize,
45 int bytesToRead) {
46 ScopedJavaLocalRef<jobject> empty_jstream =
47 Java_InputStreamUnittest_getCountingStream(env_, streamSize);
48 EXPECT_FALSE(empty_jstream.is_null());
49
50 scoped_ptr<InputStream> input_stream(new InputStream(empty_jstream));
51 int bytesRead = 0;
52 scoped_refptr<IOBuffer> buffer = new IOBuffer(bytesToRead);
53
54 EXPECT_TRUE(input_stream->Read(
55 env_, buffer.get(), bytesToRead, &bytesRead));
56 EXPECT_EQ(bytesToRead, bytesRead);
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
68 scoped_ptr<InputStream> input_stream(new InputStream(empty_jstream));
69 const int bytesToRead = 10;
70 int bytesRead = 0;
71 scoped_refptr<IOBuffer> buffer = new IOBuffer(bytesToRead);
72
73 EXPECT_TRUE(input_stream->Read(env_, buffer.get(), bytesToRead, &bytesRead));
74 EXPECT_EQ(0, bytesRead);
75 }
76
77 TEST_F(InputStreamTest, ReadStreamPartial) {
78 const int bytesToRead = 128;
79 DoReadCountedStreamTest(bytesToRead * 2, bytesToRead);
80 }
81
82 TEST_F(InputStreamTest, ReadStreamCompletely) {
83 const int bytesToRead = 42;
84 DoReadCountedStreamTest(bytesToRead, bytesToRead);
85 }
86
87 TEST_F(InputStreamTest, ReadStreamInChunks) {
88 const int bytesToRead = 3 * InputStream::kBufferSize;
89 DoReadCountedStreamTest(bytesToRead, bytesToRead);
90 }
91
92 TEST_F(InputStreamTest, CheckContentsReadCorrectly) {
93 const int bytesToRead = 256;
94 scoped_refptr<IOBuffer> buffer =
95 DoReadCountedStreamTest(bytesToRead, bytesToRead);
96 for (int i = 0; i < bytesToRead; ++i) {
97 ASSERT_EQ(i, buffer->data()[i]);
98 }
99 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698