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