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 virtual ~MockFileStream(); 29 30 // FileStream methods. 31 virtual int Seek(base::File::Whence whence, int64 offset, 32 const Int64CompletionCallback& callback) OVERRIDE; 33 virtual int Read(IOBuffer* buf, 34 int buf_len, 35 const CompletionCallback& callback) OVERRIDE; 36 virtual int Write(IOBuffer* buf, 37 int buf_len, 38 const CompletionCallback& callback) OVERRIDE; 39 virtual int Flush(const CompletionCallback& callback) OVERRIDE; 40 41 void set_forced_error_async(int error) { 42 forced_error_ = error; 43 async_error_ = true; 44 } 45 void set_forced_error(int error) { 46 forced_error_ = error; 47 async_error_ = false; 48 } 49 void clear_forced_error() { 50 forced_error_ = net::OK; 51 async_error_ = false; 52 } 53 int forced_error() const { return forced_error_; } 54 const base::FilePath& get_path() const { return path_; } 55 56 // Throttles all asynchronous callbacks, including forced errors, until a 57 // matching ReleaseCallbacks call. 58 void ThrottleCallbacks(); 59 60 // Resumes running asynchronous callbacks and runs any throttled callbacks. 61 void ReleaseCallbacks(); 62 63 private: 64 int ReturnError(int function_error) { 65 if (forced_error_ != net::OK) { 66 int ret = forced_error_; 67 clear_forced_error(); 68 return ret; 69 } 70 71 return function_error; 72 } 73 74 int64 ReturnError64(int64 function_error) { 75 if (forced_error_ != net::OK) { 76 int64 ret = forced_error_; 77 clear_forced_error(); 78 return ret; 79 } 80 81 return function_error; 82 } 83 84 // Wrappers for callbacks to make them honor ThrottleCallbacks and 85 // ReleaseCallbacks. 86 void DoCallback(const CompletionCallback& callback, int result); 87 void DoCallback64(const Int64CompletionCallback& callback, int64 result); 88 89 // Depending on |async_error_|, either synchronously returns |forced_error_| 90 // asynchronously calls |callback| with |async_error_|. 91 int ErrorCallback(const CompletionCallback& callback); 92 int64 ErrorCallback64(const Int64CompletionCallback& callback); 93 94 int forced_error_; 95 bool async_error_; 96 bool throttled_; 97 base::Closure throttled_task_; 98 base::FilePath path_; 99 100 base::WeakPtrFactory<MockFileStream> weak_factory_; 101 }; 102 103 } // namespace testing 104 105 } // namespace net 106 107 #endif // NET_BASE_MOCK_FILE_STREAM_H_ 108