| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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 // This file defines MockFileStream, a test class for FileStream. | |
| 6 | |
| 7 #ifndef NET_BASE_MOCK_FILE_STREAM_H_ | |
| 8 #define NET_BASE_MOCK_FILE_STREAM_H_ | |
| 9 | |
| 10 #include "base/basictypes.h" | |
| 11 #include "base/compiler_specific.h" | |
| 12 #include "base/files/file_path.h" | |
| 13 #include "base/memory/weak_ptr.h" | |
| 14 #include "net/base/file_stream.h" | |
| 15 #include "net/base/net_errors.h" | |
| 16 | |
| 17 namespace net { | |
| 18 | |
| 19 class IOBuffer; | |
| 20 | |
| 21 namespace testing { | |
| 22 | |
| 23 class MockFileStream : public net::FileStream { | |
| 24 public: | |
| 25 explicit MockFileStream(const scoped_refptr<base::TaskRunner>& task_runner); | |
| 26 MockFileStream(base::File file, | |
| 27 const scoped_refptr<base::TaskRunner>& task_runner); | |
| 28 ~MockFileStream() override; | |
| 29 | |
| 30 // FileStream methods. | |
| 31 int Seek(base::File::Whence whence, | |
| 32 int64 offset, | |
| 33 const Int64CompletionCallback& callback) override; | |
| 34 int Read(IOBuffer* buf, | |
| 35 int buf_len, | |
| 36 const CompletionCallback& callback) override; | |
| 37 int Write(IOBuffer* buf, | |
| 38 int buf_len, | |
| 39 const CompletionCallback& callback) override; | |
| 40 int Flush(const CompletionCallback& callback) override; | |
| 41 | |
| 42 void set_forced_error_async(int error) { | |
| 43 forced_error_ = error; | |
| 44 async_error_ = true; | |
| 45 } | |
| 46 void set_forced_error(int error) { | |
| 47 forced_error_ = error; | |
| 48 async_error_ = false; | |
| 49 } | |
| 50 void clear_forced_error() { | |
| 51 forced_error_ = net::OK; | |
| 52 async_error_ = false; | |
| 53 } | |
| 54 int forced_error() const { return forced_error_; } | |
| 55 const base::FilePath& get_path() const { return path_; } | |
| 56 | |
| 57 // Throttles all asynchronous callbacks, including forced errors, until a | |
| 58 // matching ReleaseCallbacks call. | |
| 59 void ThrottleCallbacks(); | |
| 60 | |
| 61 // Resumes running asynchronous callbacks and runs any throttled callbacks. | |
| 62 void ReleaseCallbacks(); | |
| 63 | |
| 64 private: | |
| 65 int ReturnError(int function_error) { | |
| 66 if (forced_error_ != net::OK) { | |
| 67 int ret = forced_error_; | |
| 68 clear_forced_error(); | |
| 69 return ret; | |
| 70 } | |
| 71 | |
| 72 return function_error; | |
| 73 } | |
| 74 | |
| 75 int64 ReturnError64(int64 function_error) { | |
| 76 if (forced_error_ != net::OK) { | |
| 77 int64 ret = forced_error_; | |
| 78 clear_forced_error(); | |
| 79 return ret; | |
| 80 } | |
| 81 | |
| 82 return function_error; | |
| 83 } | |
| 84 | |
| 85 // Wrappers for callbacks to make them honor ThrottleCallbacks and | |
| 86 // ReleaseCallbacks. | |
| 87 void DoCallback(const CompletionCallback& callback, int result); | |
| 88 void DoCallback64(const Int64CompletionCallback& callback, int64 result); | |
| 89 | |
| 90 // Depending on |async_error_|, either synchronously returns |forced_error_| | |
| 91 // asynchronously calls |callback| with |async_error_|. | |
| 92 int ErrorCallback(const CompletionCallback& callback); | |
| 93 int64 ErrorCallback64(const Int64CompletionCallback& callback); | |
| 94 | |
| 95 int forced_error_; | |
| 96 bool async_error_; | |
| 97 bool throttled_; | |
| 98 base::Closure throttled_task_; | |
| 99 base::FilePath path_; | |
| 100 | |
| 101 base::WeakPtrFactory<MockFileStream> weak_factory_; | |
| 102 }; | |
| 103 | |
| 104 } // namespace testing | |
| 105 | |
| 106 } // namespace net | |
| 107 | |
| 108 #endif // NET_BASE_MOCK_FILE_STREAM_H_ | |
| OLD | NEW |