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

Unified 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: reinterpret_cast->static_cast 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 side-by-side diff with in-line comments
Download patch
Index: android_webview/native/input_stream_unittest.cc
diff --git a/android_webview/native/input_stream_unittest.cc b/android_webview/native/input_stream_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..e047d70b2d952720b2ef0119a84a87d9b8d10122
--- /dev/null
+++ b/android_webview/native/input_stream_unittest.cc
@@ -0,0 +1,100 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "android_webview/native/input_stream_impl.h"
+#include "base/android/jni_android.h"
+#include "base/android/scoped_java_ref.h"
+#include "base/memory/scoped_ptr.h"
+#include "jni/InputStreamUnittest_jni.h"
+#include "net/base/io_buffer.h"
+#include "net/base/net_errors.h"
+#include "net/http/http_byte_range.h"
+
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+using android_webview::InputStream;
+using android_webview::InputStreamImpl;
+using base::android::AttachCurrentThread;
+using base::android::ScopedJavaLocalRef;
+using net::IOBuffer;
+using testing::DoAll;
+using testing::Ge;
+using testing::InSequence;
+using testing::Lt;
+using testing::Ne;
+using testing::NotNull;
+using testing::Return;
+using testing::SetArgPointee;
+using testing::Test;
+using testing::_;
+
+class InputStreamTest : public Test {
+ public:
+ InputStreamTest() {
+ }
+ protected:
+ virtual void SetUp() {
+ env_ = AttachCurrentThread();
+ ASSERT_THAT(env_, NotNull());
+ ASSERT_TRUE(android_webview::RegisterInputStream(env_));
+ ASSERT_TRUE(RegisterNativesImpl(env_));
+ }
+
+ scoped_refptr<IOBuffer> DoReadCountedStreamTest(int streamSize,
+ int bytesToRead) {
+ ScopedJavaLocalRef<jobject> empty_jstream =
benm (inactive) 2012/11/16 15:43:20 counting_jstream ?
mkosiba (inactive) 2012/11/19 15:29:33 Done.
+ Java_InputStreamUnittest_getCountingStream(env_, streamSize);
+ EXPECT_FALSE(empty_jstream.is_null());
+
+ scoped_ptr<InputStream> input_stream(new InputStreamImpl(empty_jstream));
+ int bytesRead = 0;
+ scoped_refptr<IOBuffer> buffer = new IOBuffer(bytesToRead);
+
+ EXPECT_TRUE(input_stream->Read(
+ env_, buffer.get(), bytesToRead, &bytesRead));
+ EXPECT_EQ(bytesToRead, bytesRead);
+ return buffer;
+ }
+
+ JNIEnv* env_;
+};
+
+TEST_F(InputStreamTest, ReadEmptyStream) {
+ ScopedJavaLocalRef<jobject> empty_jstream =
+ Java_InputStreamUnittest_getEmptyStream(env_);
+ EXPECT_FALSE(empty_jstream.is_null());
+
+ scoped_ptr<InputStream> input_stream(new InputStreamImpl(empty_jstream));
+ const int bytesToRead = 10;
+ int bytesRead = 0;
+ scoped_refptr<IOBuffer> buffer = new IOBuffer(bytesToRead);
+
+ EXPECT_TRUE(input_stream->Read(env_, buffer.get(), bytesToRead, &bytesRead));
+ EXPECT_EQ(0, bytesRead);
+}
+
+TEST_F(InputStreamTest, ReadStreamPartial) {
+ const int bytesToRead = 128;
+ DoReadCountedStreamTest(bytesToRead * 2, bytesToRead);
+}
+
+TEST_F(InputStreamTest, ReadStreamCompletely) {
+ const int bytesToRead = 42;
+ DoReadCountedStreamTest(bytesToRead, bytesToRead);
+}
+
+TEST_F(InputStreamTest, ReadStreamInChunks) {
+ const int bytesToRead = 3 * InputStreamImpl::kBufferSize;
+ DoReadCountedStreamTest(bytesToRead, bytesToRead);
+}
+
+TEST_F(InputStreamTest, CheckContentsReadCorrectly) {
+ const int bytesToRead = 256;
+ scoped_refptr<IOBuffer> buffer =
+ DoReadCountedStreamTest(bytesToRead, bytesToRead);
+ for (int i = 0; i < bytesToRead; ++i) {
+ ASSERT_EQ(i, buffer->data()[i]);
benm (inactive) 2012/11/16 15:43:20 EXPECT?
mkosiba (inactive) 2012/11/19 15:29:33 Done.
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698