From bddde1fa43877914245de874decf842f85214001 Mon Sep 17 00:00:00 2001 From: Ben Kelly Date: Mon, 14 Sep 2015 08:49:09 -0700 Subject: [PATCH] Bug 1203760 P2 Add gtests for nsPipeInputStream AsyncRead(). r=froydnj --- xpcom/tests/gtest/Helpers.cpp | 18 ++++++++ xpcom/tests/gtest/Helpers.h | 17 +++++++ xpcom/tests/gtest/TestPipes.cpp | 82 +++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+) diff --git a/xpcom/tests/gtest/Helpers.cpp b/xpcom/tests/gtest/Helpers.cpp index 8a98a5aa096..e06ef901b80 100644 --- a/xpcom/tests/gtest/Helpers.cpp +++ b/xpcom/tests/gtest/Helpers.cpp @@ -112,4 +112,22 @@ OutputStreamCallback::OnOutputStreamReady(nsIAsyncOutputStream* aStream) return NS_OK; } +NS_IMPL_ISUPPORTS(InputStreamCallback, nsIInputStreamCallback); + +InputStreamCallback::InputStreamCallback() + : mCalled(false) +{ +} + +InputStreamCallback::~InputStreamCallback() +{ +} + +NS_IMETHODIMP +InputStreamCallback::OnInputStreamReady(nsIAsyncInputStream* aStream) +{ + mCalled = true; + return NS_OK; +} + } // namespace testing diff --git a/xpcom/tests/gtest/Helpers.h b/xpcom/tests/gtest/Helpers.h index 186ce5ccd02..a831f6e0ca7 100644 --- a/xpcom/tests/gtest/Helpers.h +++ b/xpcom/tests/gtest/Helpers.h @@ -7,6 +7,7 @@ #ifndef __Helpers_h #define __Helpers_h +#include "nsIAsyncInputStream.h" #include "nsIAsyncOutputStream.h" #include "nsString.h" #include @@ -51,6 +52,22 @@ public: NS_DECL_NSIOUTPUTSTREAMCALLBACK }; +class InputStreamCallback final : public nsIInputStreamCallback +{ +public: + InputStreamCallback(); + + bool Called() const { return mCalled; } + +private: + ~InputStreamCallback(); + + bool mCalled; +public: + NS_DECL_ISUPPORTS + NS_DECL_NSIINPUTSTREAMCALLBACK +}; + } // namespace testing #endif // __Helpers_h diff --git a/xpcom/tests/gtest/TestPipes.cpp b/xpcom/tests/gtest/TestPipes.cpp index 5cded29b7bb..d17c8b781f0 100644 --- a/xpcom/tests/gtest/TestPipes.cpp +++ b/xpcom/tests/gtest/TestPipes.cpp @@ -786,6 +786,88 @@ TEST(Pipes, Write_AsyncWait_Clone_CloseOriginal) ASSERT_TRUE(cb->Called()); } +TEST(Pipes, Read_AsyncWait) +{ + nsCOMPtr reader; + nsCOMPtr writer; + + const uint32_t segmentSize = 1024; + const uint32_t numSegments = 1; + + nsresult rv = NS_NewPipe2(getter_AddRefs(reader), getter_AddRefs(writer), + true, true, // non-blocking - reader, writer + segmentSize, numSegments); + ASSERT_TRUE(NS_SUCCEEDED(rv)); + + nsTArray inputData; + testing::CreateData(segmentSize, inputData); + + nsRefPtr cb = + new testing::InputStreamCallback(); + + rv = reader->AsyncWait(cb, 0, 0, nullptr); + ASSERT_TRUE(NS_SUCCEEDED(rv)); + + ASSERT_FALSE(cb->Called()); + + uint32_t numWritten = 0; + rv = writer->Write(inputData.Elements(), inputData.Length(), &numWritten); + ASSERT_TRUE(NS_SUCCEEDED(rv)); + + ASSERT_TRUE(cb->Called()); + + testing::ConsumeAndValidateStream(reader, inputData); +} + +TEST(Pipes, Read_AsyncWait_Clone) +{ + nsCOMPtr reader; + nsCOMPtr writer; + + const uint32_t segmentSize = 1024; + const uint32_t numSegments = 1; + + nsresult rv = NS_NewPipe2(getter_AddRefs(reader), getter_AddRefs(writer), + true, true, // non-blocking - reader, writer + segmentSize, numSegments); + ASSERT_TRUE(NS_SUCCEEDED(rv)); + + nsCOMPtr clone; + rv = NS_CloneInputStream(reader, getter_AddRefs(clone)); + ASSERT_TRUE(NS_SUCCEEDED(rv)); + + nsCOMPtr asyncClone = do_QueryInterface(clone); + ASSERT_TRUE(asyncClone); + + nsTArray inputData; + testing::CreateData(segmentSize, inputData); + + nsRefPtr cb = + new testing::InputStreamCallback(); + + nsRefPtr cb2 = + new testing::InputStreamCallback(); + + rv = reader->AsyncWait(cb, 0, 0, nullptr); + ASSERT_TRUE(NS_SUCCEEDED(rv)); + + ASSERT_FALSE(cb->Called()); + + rv = asyncClone->AsyncWait(cb2, 0, 0, nullptr); + ASSERT_TRUE(NS_SUCCEEDED(rv)); + + ASSERT_FALSE(cb2->Called()); + + uint32_t numWritten = 0; + rv = writer->Write(inputData.Elements(), inputData.Length(), &numWritten); + ASSERT_TRUE(NS_SUCCEEDED(rv)); + + ASSERT_TRUE(cb->Called()); + ASSERT_TRUE(cb2->Called()); + + testing::ConsumeAndValidateStream(reader, inputData); +} + namespace { NS_METHOD