| 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" |
| 13 #include "net/base/file_stream.h" | 14 #include "net/base/file_stream.h" |
| 14 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
| 15 | 16 |
| 16 namespace net { | 17 namespace net { |
| 17 | 18 |
| 18 class IOBuffer; | 19 class IOBuffer; |
| 19 | 20 |
| 20 namespace testing { | 21 namespace testing { |
| 21 | 22 |
| 22 class MockFileStream : public net::FileStream { | 23 class MockFileStream : public net::FileStream { |
| 23 public: | 24 public: |
| 24 MockFileStream(net::NetLog* net_log) | 25 MockFileStream(net::NetLog* net_log); |
| 25 : net::FileStream(net_log), forced_error_(net::OK) {} | 26 MockFileStream(base::PlatformFile file, int flags, net::NetLog* net_log); |
| 26 | 27 MockFileStream(base::PlatformFile file, int flags, net::NetLog* net_log, |
| 27 MockFileStream(base::PlatformFile file, int flags, net::NetLog* net_log) | 28 const scoped_refptr<base::TaskRunner>& task_runner); |
| 28 : net::FileStream(file, flags, net_log), forced_error_(net::OK) {} | |
| 29 | 29 |
| 30 // FileStream methods. | 30 // FileStream methods. |
| 31 virtual int OpenSync(const base::FilePath& path, int open_flags) OVERRIDE; | 31 virtual int OpenSync(const base::FilePath& path, int open_flags) OVERRIDE; |
| 32 virtual int Seek(net::Whence whence, int64 offset, | 32 virtual int Seek(net::Whence whence, int64 offset, |
| 33 const Int64CompletionCallback& callback) OVERRIDE; | 33 const Int64CompletionCallback& callback) OVERRIDE; |
| 34 virtual int64 SeekSync(net::Whence whence, int64 offset) OVERRIDE; | 34 virtual int64 SeekSync(net::Whence whence, int64 offset) OVERRIDE; |
| 35 virtual int64 Available() OVERRIDE; | 35 virtual int64 Available() OVERRIDE; |
| 36 virtual int Read(IOBuffer* buf, | 36 virtual int Read(IOBuffer* buf, |
| 37 int buf_len, | 37 int buf_len, |
| 38 const CompletionCallback& callback) OVERRIDE; | 38 const CompletionCallback& callback) OVERRIDE; |
| 39 virtual int ReadSync(char* buf, int buf_len) OVERRIDE; | 39 virtual int ReadSync(char* buf, int buf_len) OVERRIDE; |
| 40 virtual int ReadUntilComplete(char *buf, int buf_len) OVERRIDE; | 40 virtual int ReadUntilComplete(char *buf, int buf_len) OVERRIDE; |
| 41 virtual int Write(IOBuffer* buf, | 41 virtual int Write(IOBuffer* buf, |
| 42 int buf_len, | 42 int buf_len, |
| 43 const CompletionCallback& callback) OVERRIDE; | 43 const CompletionCallback& callback) OVERRIDE; |
| 44 virtual int WriteSync(const char* buf, int buf_len) OVERRIDE; | 44 virtual int WriteSync(const char* buf, int buf_len) OVERRIDE; |
| 45 virtual int64 Truncate(int64 bytes) OVERRIDE; | 45 virtual int64 Truncate(int64 bytes) OVERRIDE; |
| 46 virtual int Flush(const CompletionCallback& callback) OVERRIDE; | 46 virtual int Flush(const CompletionCallback& callback) OVERRIDE; |
| 47 virtual int FlushSync() OVERRIDE; | 47 virtual int FlushSync() OVERRIDE; |
| 48 | 48 |
| 49 void set_forced_error_async(int error) { |
| 50 forced_error_ = error; |
| 51 async_error_ = true; |
| 52 } |
| 49 void set_forced_error(int error) { forced_error_ = error; } | 53 void set_forced_error(int error) { forced_error_ = error; } |
| 50 void clear_forced_error() { forced_error_ = net::OK; } | 54 void clear_forced_error() { |
| 55 forced_error_ = net::OK; |
| 56 async_error_ = false; |
| 57 } |
| 51 int forced_error() const { return forced_error_; } | 58 int forced_error() const { return forced_error_; } |
| 52 const base::FilePath& get_path() const { return path_; } | 59 const base::FilePath& get_path() const { return path_; } |
| 53 | 60 |
| 61 void ThrottleCallbacks(); |
| 62 void ReleaseCallbacks(); |
| 63 |
| 54 private: | 64 private: |
| 55 int ReturnError(int function_error) { | 65 int ReturnError(int function_error) { |
| 56 if (forced_error_ != net::OK) { | 66 if (forced_error_ != net::OK) { |
| 57 int ret = forced_error_; | 67 int ret = forced_error_; |
| 58 clear_forced_error(); | 68 clear_forced_error(); |
| 59 return ret; | 69 return ret; |
| 60 } | 70 } |
| 61 | 71 |
| 62 return function_error; | 72 return function_error; |
| 63 } | 73 } |
| 64 | 74 |
| 65 int64 ReturnError64(int64 function_error) { | 75 int64 ReturnError64(int64 function_error) { |
| 66 if (forced_error_ != net::OK) { | 76 if (forced_error_ != net::OK) { |
| 67 int64 ret = forced_error_; | 77 int64 ret = forced_error_; |
| 68 clear_forced_error(); | 78 clear_forced_error(); |
| 69 return ret; | 79 return ret; |
| 70 } | 80 } |
| 71 | 81 |
| 72 return function_error; | 82 return function_error; |
| 73 } | 83 } |
| 74 | 84 |
| 85 void DoCallback(const CompletionCallback& callback, int result); |
| 86 void DoCallback64(const Int64CompletionCallback& callback, int64 result); |
| 87 |
| 88 int ErrorCallback(const CompletionCallback& callback); |
| 89 int64 ErrorCallback64(const Int64CompletionCallback& callback); |
| 90 |
| 75 int forced_error_; | 91 int forced_error_; |
| 92 bool async_error_; |
| 93 bool throttled_; |
| 94 base::Closure throttled_task_; |
| 76 base::FilePath path_; | 95 base::FilePath path_; |
| 96 |
| 97 base::WeakPtrFactory<MockFileStream> weak_factory_; |
| 77 }; | 98 }; |
| 78 | 99 |
| 79 } // namespace testing | 100 } // namespace testing |
| 80 | 101 |
| 81 } // namespace net | 102 } // namespace net |
| 82 | 103 |
| 83 #endif // NET_BASE_MOCK_FILE_STREAM_H_ | 104 #endif // NET_BASE_MOCK_FILE_STREAM_H_ |
| OLD | NEW |