1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef __Helpers_h 8 #define __Helpers_h 9 10 #include "nsCOMPtr.h" 11 #include "nsIAsyncInputStream.h" 12 #include "nsIAsyncOutputStream.h" 13 #include "nsString.h" 14 #include "nsStringStream.h" 15 #include "nsTArrayForwardDeclare.h" 16 #include <stdint.h> 17 18 class nsIInputStream; 19 class nsIOutputStream; 20 21 namespace testing { 22 23 void 24 CreateData(uint32_t aNumBytes, nsTArray<char>& aDataOut); 25 26 void 27 Write(nsIOutputStream* aStream, const nsTArray<char>& aData, uint32_t aOffset, 28 uint32_t aNumBytes); 29 30 void 31 WriteAllAndClose(nsIOutputStream* aStream, const nsTArray<char>& aData); 32 33 void 34 ConsumeAndValidateStream(nsIInputStream* aStream, 35 const nsTArray<char>& aExpectedData); 36 37 void 38 ConsumeAndValidateStream(nsIInputStream* aStream, 39 const nsACString& aExpectedData); 40 41 class OutputStreamCallback final : public nsIOutputStreamCallback 42 { 43 public: 44 OutputStreamCallback(); 45 Called()46 bool Called() const { return mCalled; } 47 48 private: 49 ~OutputStreamCallback(); 50 51 bool mCalled; 52 public: 53 NS_DECL_ISUPPORTS 54 NS_DECL_NSIOUTPUTSTREAMCALLBACK 55 }; 56 57 class InputStreamCallback final : public nsIInputStreamCallback 58 { 59 public: 60 InputStreamCallback(); 61 Called()62 bool Called() const { return mCalled; } 63 64 private: 65 ~InputStreamCallback(); 66 67 bool mCalled; 68 public: 69 NS_DECL_ISUPPORTS 70 NS_DECL_NSIINPUTSTREAMCALLBACK 71 }; 72 73 class AsyncStringStream final : public nsIAsyncInputStream 74 { 75 nsCOMPtr<nsIInputStream> mStream; 76 77 public: 78 NS_DECL_THREADSAFE_ISUPPORTS 79 NS_DECL_NSIINPUTSTREAM 80 NS_DECL_NSIASYNCINPUTSTREAM 81 82 explicit AsyncStringStream(const nsACString& aBuffer); 83 84 private: 85 ~AsyncStringStream() = default; 86 87 void 88 MaybeExecCallback(nsIInputStreamCallback* aCallback, 89 nsIEventTarget* aEventTarget); 90 91 nsCOMPtr<nsIInputStreamCallback> mCallback; 92 nsCOMPtr<nsIEventTarget> mCallbackEventTarget; 93 }; 94 95 } // namespace testing 96 97 #endif // __Helpers_h 98