| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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 #ifndef NET_FILTER_MOCK_SOURCE_STREAM_H_ |
| 6 #define NET_FILTER_MOCK_SOURCE_STREAM_H_ |
| 7 |
| 8 #include <queue> |
| 9 #include <string> |
| 10 |
| 11 #include "base/macros.h" |
| 12 #include "base/memory/ref_counted.h" |
| 13 #include "net/base/completion_callback.h" |
| 14 #include "net/base/net_errors.h" |
| 15 #include "net/filter/source_stream.h" |
| 16 |
| 17 namespace net { |
| 18 |
| 19 class IOBuffer; |
| 20 |
| 21 // A SourceStream implementation used in tests. This allows tests to specify |
| 22 // what data to return for each Read() call. |
| 23 class MockSourceStream : public SourceStream { |
| 24 public: |
| 25 enum Mode { |
| 26 SYNC, |
| 27 ASYNC, |
| 28 }; |
| 29 MockSourceStream(); |
| 30 // The destructor will crash in debug build if there is any pending read. |
| 31 ~MockSourceStream() override; |
| 32 |
| 33 // SourceStream implementation |
| 34 int Read(IOBuffer* dest_buffer, |
| 35 int buffer_size, |
| 36 const CompletionCallback& callback) override; |
| 37 std::string Description() const override; |
| 38 |
| 39 // Enqueues a result to be returned by |Read|. This method does not make a |
| 40 // copy of |data|, so |data| must outlive this object. If |mode| is SYNC, |
| 41 // |Read| will return the supplied data synchronously; otherwise, consumer |
| 42 // needs to call |CompleteNextRead| |
| 43 void AddReadResult(const char* data, int len, Error error, Mode mode); |
| 44 |
| 45 // Completes a pending Read() call. Crash in debug build if there is no |
| 46 // pending read. |
| 47 void CompleteNextRead(); |
| 48 |
| 49 private: |
| 50 struct QueuedResult { |
| 51 QueuedResult(const char* data, int len, Error error, Mode mode); |
| 52 |
| 53 const char* data; |
| 54 const int len; |
| 55 const Error error; |
| 56 const Mode mode; |
| 57 }; |
| 58 |
| 59 std::queue<QueuedResult> results_; |
| 60 bool awaiting_completion_; |
| 61 scoped_refptr<IOBuffer> dest_buffer_; |
| 62 CompletionCallback callback_; |
| 63 int dest_buffer_size_; |
| 64 |
| 65 DISALLOW_COPY_AND_ASSIGN(MockSourceStream); |
| 66 }; |
| 67 |
| 68 } // namespace net |
| 69 |
| 70 #endif // NET_FILTER_MOCK_SOURCE_STREAM_H_ |
| OLD | NEW |