Chromium Code Reviews| 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); |
|
mmenke
2013/12/04 20:55:48
While you're here, this should be explicit.
davidben
2013/12/04 22:44:04
Done.
| |
| 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 virtual ~MockFileStream(); |
| 29 | 30 |
| 30 // FileStream methods. | 31 // FileStream methods. |
| 31 virtual int OpenSync(const base::FilePath& path, int open_flags) OVERRIDE; | 32 virtual int OpenSync(const base::FilePath& path, int open_flags) OVERRIDE; |
| 32 virtual int Seek(net::Whence whence, int64 offset, | 33 virtual int Seek(net::Whence whence, int64 offset, |
| 33 const Int64CompletionCallback& callback) OVERRIDE; | 34 const Int64CompletionCallback& callback) OVERRIDE; |
| 34 virtual int64 SeekSync(net::Whence whence, int64 offset) OVERRIDE; | 35 virtual int64 SeekSync(net::Whence whence, int64 offset) OVERRIDE; |
| 35 virtual int64 Available() OVERRIDE; | 36 virtual int64 Available() OVERRIDE; |
| 36 virtual int Read(IOBuffer* buf, | 37 virtual int Read(IOBuffer* buf, |
| 37 int buf_len, | 38 int buf_len, |
| 38 const CompletionCallback& callback) OVERRIDE; | 39 const CompletionCallback& callback) OVERRIDE; |
| 39 virtual int ReadSync(char* buf, int buf_len) OVERRIDE; | 40 virtual int ReadSync(char* buf, int buf_len) OVERRIDE; |
| 40 virtual int ReadUntilComplete(char *buf, int buf_len) OVERRIDE; | 41 virtual int ReadUntilComplete(char *buf, int buf_len) OVERRIDE; |
| 41 virtual int Write(IOBuffer* buf, | 42 virtual int Write(IOBuffer* buf, |
| 42 int buf_len, | 43 int buf_len, |
| 43 const CompletionCallback& callback) OVERRIDE; | 44 const CompletionCallback& callback) OVERRIDE; |
| 44 virtual int WriteSync(const char* buf, int buf_len) OVERRIDE; | 45 virtual int WriteSync(const char* buf, int buf_len) OVERRIDE; |
| 45 virtual int64 Truncate(int64 bytes) OVERRIDE; | 46 virtual int64 Truncate(int64 bytes) OVERRIDE; |
| 46 virtual int Flush(const CompletionCallback& callback) OVERRIDE; | 47 virtual int Flush(const CompletionCallback& callback) OVERRIDE; |
| 47 virtual int FlushSync() OVERRIDE; | 48 virtual int FlushSync() OVERRIDE; |
| 48 | 49 |
| 50 void set_forced_error_async(int error) { | |
| 51 forced_error_ = error; | |
| 52 async_error_ = true; | |
| 53 } | |
| 49 void set_forced_error(int error) { forced_error_ = error; } | 54 void set_forced_error(int error) { forced_error_ = error; } |
|
mmenke
2013/12/04 20:55:48
This should probably set async_error_ to false.
davidben
2013/12/04 22:44:04
Done.
| |
| 50 void clear_forced_error() { forced_error_ = net::OK; } | 55 void clear_forced_error() { |
| 56 forced_error_ = net::OK; | |
| 57 async_error_ = false; | |
| 58 } | |
| 51 int forced_error() const { return forced_error_; } | 59 int forced_error() const { return forced_error_; } |
| 52 const base::FilePath& get_path() const { return path_; } | 60 const base::FilePath& get_path() const { return path_; } |
| 53 | 61 |
| 62 void ThrottleCallbacks(); | |
| 63 void ReleaseCallbacks(); | |
|
mmenke
2013/12/04 20:55:48
These really need comments.
davidben
2013/12/04 22:44:04
Done.
| |
| 64 | |
| 54 private: | 65 private: |
| 55 int ReturnError(int function_error) { | 66 int ReturnError(int function_error) { |
| 56 if (forced_error_ != net::OK) { | 67 if (forced_error_ != net::OK) { |
| 57 int ret = forced_error_; | 68 int ret = forced_error_; |
| 58 clear_forced_error(); | 69 clear_forced_error(); |
| 59 return ret; | 70 return ret; |
| 60 } | 71 } |
| 61 | 72 |
| 62 return function_error; | 73 return function_error; |
| 63 } | 74 } |
| 64 | 75 |
| 65 int64 ReturnError64(int64 function_error) { | 76 int64 ReturnError64(int64 function_error) { |
| 66 if (forced_error_ != net::OK) { | 77 if (forced_error_ != net::OK) { |
| 67 int64 ret = forced_error_; | 78 int64 ret = forced_error_; |
| 68 clear_forced_error(); | 79 clear_forced_error(); |
| 69 return ret; | 80 return ret; |
| 70 } | 81 } |
| 71 | 82 |
| 72 return function_error; | 83 return function_error; |
| 73 } | 84 } |
| 74 | 85 |
| 86 void DoCallback(const CompletionCallback& callback, int result); | |
| 87 void DoCallback64(const Int64CompletionCallback& callback, int64 result); | |
| 88 | |
| 89 int ErrorCallback(const CompletionCallback& callback); | |
| 90 int64 ErrorCallback64(const Int64CompletionCallback& callback); | |
|
mmenke
2013/12/04 20:55:48
Think these could use some comments.
davidben
2013/12/04 22:44:04
Done.
| |
| 91 | |
| 75 int forced_error_; | 92 int forced_error_; |
| 93 bool async_error_; | |
| 94 bool throttled_; | |
| 95 base::Closure throttled_task_; | |
| 76 base::FilePath path_; | 96 base::FilePath path_; |
| 97 | |
| 98 base::WeakPtrFactory<MockFileStream> weak_factory_; | |
| 77 }; | 99 }; |
| 78 | 100 |
| 79 } // namespace testing | 101 } // namespace testing |
| 80 | 102 |
| 81 } // namespace net | 103 } // namespace net |
| 82 | 104 |
| 83 #endif // NET_BASE_MOCK_FILE_STREAM_H_ | 105 #endif // NET_BASE_MOCK_FILE_STREAM_H_ |
| OLD | NEW |