/src/mozilla-central/xpcom/tests/gtest/Helpers.h
Line | Count | Source (jump to first uncovered line) |
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 "nsIInputStreamLength.h" |
14 | | #include "nsString.h" |
15 | | #include "nsStringStream.h" |
16 | | #include "nsTArrayForwardDeclare.h" |
17 | | #include "nsThreadUtils.h" |
18 | | #include <stdint.h> |
19 | | |
20 | | class nsIInputStream; |
21 | | class nsIOutputStream; |
22 | | |
23 | | namespace testing { |
24 | | |
25 | | void |
26 | | CreateData(uint32_t aNumBytes, nsTArray<char>& aDataOut); |
27 | | |
28 | | void |
29 | | Write(nsIOutputStream* aStream, const nsTArray<char>& aData, uint32_t aOffset, |
30 | | uint32_t aNumBytes); |
31 | | |
32 | | void |
33 | | WriteAllAndClose(nsIOutputStream* aStream, const nsTArray<char>& aData); |
34 | | |
35 | | void |
36 | | ConsumeAndValidateStream(nsIInputStream* aStream, |
37 | | const nsTArray<char>& aExpectedData); |
38 | | |
39 | | void |
40 | | ConsumeAndValidateStream(nsIInputStream* aStream, |
41 | | const nsACString& aExpectedData); |
42 | | |
43 | | class OutputStreamCallback final : public nsIOutputStreamCallback |
44 | | { |
45 | | public: |
46 | | OutputStreamCallback(); |
47 | | |
48 | 0 | bool Called() const { return mCalled; } |
49 | | |
50 | | private: |
51 | | ~OutputStreamCallback(); |
52 | | |
53 | | bool mCalled; |
54 | | public: |
55 | | NS_DECL_ISUPPORTS |
56 | | NS_DECL_NSIOUTPUTSTREAMCALLBACK |
57 | | }; |
58 | | |
59 | | class InputStreamCallback final : public nsIInputStreamCallback |
60 | | { |
61 | | public: |
62 | | InputStreamCallback(); |
63 | | |
64 | 0 | bool Called() const { return mCalled; } |
65 | | |
66 | | private: |
67 | | ~InputStreamCallback(); |
68 | | |
69 | | bool mCalled; |
70 | | public: |
71 | | NS_DECL_ISUPPORTS |
72 | | NS_DECL_NSIINPUTSTREAMCALLBACK |
73 | | }; |
74 | | |
75 | | class AsyncStringStream final : public nsIAsyncInputStream |
76 | | { |
77 | | nsCOMPtr<nsIInputStream> mStream; |
78 | | |
79 | | public: |
80 | | NS_DECL_THREADSAFE_ISUPPORTS |
81 | | NS_DECL_NSIINPUTSTREAM |
82 | | NS_DECL_NSIASYNCINPUTSTREAM |
83 | | |
84 | | explicit AsyncStringStream(const nsACString& aBuffer); |
85 | | |
86 | | private: |
87 | 0 | ~AsyncStringStream() = default; |
88 | | |
89 | | void |
90 | | MaybeExecCallback(nsIInputStreamCallback* aCallback, |
91 | | nsIEventTarget* aEventTarget); |
92 | | |
93 | | nsCOMPtr<nsIInputStreamCallback> mCallback; |
94 | | nsCOMPtr<nsIEventTarget> mCallbackEventTarget; |
95 | | }; |
96 | | |
97 | | // This class implements a simple nsIInputStreamLength stream. |
98 | | class LengthInputStream : public nsIInputStream |
99 | | , public nsIInputStreamLength |
100 | | , public nsIAsyncInputStreamLength |
101 | | { |
102 | | nsCOMPtr<nsIInputStream> mStream; |
103 | | bool mIsInputStreamLength; |
104 | | bool mIsAsyncInputStreamLength; |
105 | | nsresult mLengthRv; |
106 | | bool mNegativeValue; |
107 | | |
108 | | public: |
109 | | NS_DECL_THREADSAFE_ISUPPORTS |
110 | | |
111 | | LengthInputStream(const nsACString& aBuffer, |
112 | | bool aIsInputStreamLength, |
113 | | bool aIsAsyncInputStreamLength, |
114 | | nsresult aLengthRv = NS_OK, |
115 | | bool aNegativeValue = false) |
116 | | : mIsInputStreamLength(aIsInputStreamLength) |
117 | | , mIsAsyncInputStreamLength(aIsAsyncInputStreamLength) |
118 | | , mLengthRv(aLengthRv) |
119 | | , mNegativeValue(aNegativeValue) |
120 | 0 | { |
121 | 0 | NS_NewCStringInputStream(getter_AddRefs(mStream), aBuffer); |
122 | 0 | } |
123 | | |
124 | | NS_IMETHOD |
125 | | Available(uint64_t* aLength) override |
126 | 0 | { |
127 | 0 | return mStream->Available(aLength); |
128 | 0 | } |
129 | | |
130 | | NS_IMETHOD |
131 | | Read(char* aBuffer, uint32_t aCount, uint32_t* aReadCount) override |
132 | 0 | { |
133 | 0 | return mStream->Read(aBuffer, aCount, aReadCount); |
134 | 0 | } |
135 | | |
136 | | NS_IMETHOD |
137 | | ReadSegments(nsWriteSegmentFun aWriter, void* aClosure, |
138 | | uint32_t aCount, uint32_t *aResult) override |
139 | 0 | { |
140 | 0 | return mStream->ReadSegments(aWriter, aClosure, aCount, aResult); |
141 | 0 | } |
142 | | |
143 | | NS_IMETHOD |
144 | | Close() override |
145 | 0 | { |
146 | 0 | return mStream->Close(); |
147 | 0 | } |
148 | | |
149 | | NS_IMETHOD |
150 | | IsNonBlocking(bool* aNonBlocking) override |
151 | 0 | { |
152 | 0 | return mStream->IsNonBlocking(aNonBlocking); |
153 | 0 | } |
154 | | |
155 | | NS_IMETHOD |
156 | | Length(int64_t* aLength) override |
157 | 0 | { |
158 | 0 | if (mNegativeValue) { |
159 | 0 | *aLength = -1; |
160 | 0 | } else { |
161 | 0 | mStream->Available((uint64_t*)aLength); |
162 | 0 | } |
163 | 0 | return mLengthRv; |
164 | 0 | } |
165 | | |
166 | | NS_IMETHOD |
167 | | AsyncLengthWait(nsIInputStreamLengthCallback* aCallback, |
168 | | nsIEventTarget* aEventTarget) override |
169 | 0 | { |
170 | 0 | RefPtr<LengthInputStream> self = this; |
171 | 0 | nsCOMPtr<nsIInputStreamLengthCallback> callback = aCallback; |
172 | 0 |
|
173 | 0 | nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction( |
174 | 0 | "AsyncLengthWait", [self, callback]() { |
175 | 0 | int64_t length; |
176 | 0 | self->Length(&length); |
177 | 0 | callback->OnInputStreamLengthReady(self, length); |
178 | 0 | }); |
179 | 0 |
|
180 | 0 | return aEventTarget->Dispatch(r.forget(), NS_DISPATCH_NORMAL); |
181 | 0 | } |
182 | | |
183 | | protected: |
184 | 0 | virtual ~LengthInputStream() = default; |
185 | | }; |
186 | | |
187 | | class LengthCallback final : public nsIInputStreamLengthCallback |
188 | | { |
189 | | bool mCalled; |
190 | | int64_t mSize; |
191 | | |
192 | | public: |
193 | | NS_DECL_THREADSAFE_ISUPPORTS |
194 | | |
195 | | LengthCallback() |
196 | | : mCalled(false) |
197 | | , mSize(0) |
198 | 0 | {} |
199 | | |
200 | | NS_IMETHOD |
201 | | OnInputStreamLengthReady(nsIAsyncInputStreamLength* aStream, |
202 | | int64_t aLength) override |
203 | 0 | { |
204 | 0 | mCalled = true; |
205 | 0 | mSize = aLength; |
206 | 0 | return NS_OK; |
207 | 0 | } |
208 | | |
209 | | bool |
210 | | Called() const |
211 | 0 | { |
212 | 0 | return mCalled; |
213 | 0 | } |
214 | | |
215 | | int64_t |
216 | | Size() const |
217 | 0 | { |
218 | 0 | return mSize; |
219 | 0 | } |
220 | | |
221 | | private: |
222 | | ~LengthCallback() = default; |
223 | | }; |
224 | | |
225 | | } // namespace testing |
226 | | |
227 | | #endif // __Helpers_h |