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 ~MockFileStream() override
;
30 // FileStream methods.
31 int Seek(base::File::Whence whence
,
33 const Int64CompletionCallback
& callback
) override
;
34 int Read(IOBuffer
* buf
,
36 const CompletionCallback
& callback
) override
;
37 int Write(IOBuffer
* buf
,
39 const CompletionCallback
& callback
) override
;
40 int Flush(const CompletionCallback
& callback
) override
;
42 void set_forced_error_async(int error
) {
43 forced_error_
= error
;
46 void set_forced_error(int error
) {
47 forced_error_
= error
;
50 void clear_forced_error() {
51 forced_error_
= net::OK
;
54 int forced_error() const { return forced_error_
; }
55 const base::FilePath
& get_path() const { return path_
; }
57 // Throttles all asynchronous callbacks, including forced errors, until a
58 // matching ReleaseCallbacks call.
59 void ThrottleCallbacks();
61 // Resumes running asynchronous callbacks and runs any throttled callbacks.
62 void ReleaseCallbacks();
65 int ReturnError(int function_error
) {
66 if (forced_error_
!= net::OK
) {
67 int ret
= forced_error_
;
72 return function_error
;
75 int64
ReturnError64(int64 function_error
) {
76 if (forced_error_
!= net::OK
) {
77 int64 ret
= forced_error_
;
82 return function_error
;
85 // Wrappers for callbacks to make them honor ThrottleCallbacks and
87 void DoCallback(const CompletionCallback
& callback
, int result
);
88 void DoCallback64(const Int64CompletionCallback
& callback
, int64 result
);
90 // Depending on |async_error_|, either synchronously returns |forced_error_|
91 // asynchronously calls |callback| with |async_error_|.
92 int ErrorCallback(const CompletionCallback
& callback
);
93 int64
ErrorCallback64(const Int64CompletionCallback
& callback
);
98 base::Closure throttled_task_
;
101 base::WeakPtrFactory
<MockFileStream
> weak_factory_
;
104 } // namespace testing
108 #endif // NET_BASE_MOCK_FILE_STREAM_H_