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.
5 // This file defines MockFileStream, a test class for FileStream.
7 #ifndef NET_BASE_MOCK_FILE_STREAM_H_
8 #define NET_BASE_MOCK_FILE_STREAM_H_
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"
23 class MockFileStream
: public net::FileStream
{
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();
30 // FileStream methods.
31 virtual int Seek(base::File::Whence whence
, int64 offset
,
32 const Int64CompletionCallback
& callback
) OVERRIDE
;
33 virtual int Read(IOBuffer
* buf
,
35 const CompletionCallback
& callback
) OVERRIDE
;
36 virtual int Write(IOBuffer
* buf
,
38 const CompletionCallback
& callback
) OVERRIDE
;
39 virtual int Flush(const CompletionCallback
& callback
) OVERRIDE
;
41 void set_forced_error_async(int error
) {
42 forced_error_
= error
;
45 void set_forced_error(int error
) {
46 forced_error_
= error
;
49 void clear_forced_error() {
50 forced_error_
= net::OK
;
53 int forced_error() const { return forced_error_
; }
54 const base::FilePath
& get_path() const { return path_
; }
56 // Throttles all asynchronous callbacks, including forced errors, until a
57 // matching ReleaseCallbacks call.
58 void ThrottleCallbacks();
60 // Resumes running asynchronous callbacks and runs any throttled callbacks.
61 void ReleaseCallbacks();
64 int ReturnError(int function_error
) {
65 if (forced_error_
!= net::OK
) {
66 int ret
= forced_error_
;
71 return function_error
;
74 int64
ReturnError64(int64 function_error
) {
75 if (forced_error_
!= net::OK
) {
76 int64 ret
= forced_error_
;
81 return function_error
;
84 // Wrappers for callbacks to make them honor ThrottleCallbacks and
86 void DoCallback(const CompletionCallback
& callback
, int result
);
87 void DoCallback64(const Int64CompletionCallback
& callback
, int64 result
);
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
);
97 base::Closure throttled_task_
;
100 base::WeakPtrFactory
<MockFileStream
> weak_factory_
;
103 } // namespace testing
107 #endif // NET_BASE_MOCK_FILE_STREAM_H_